def test_batch_add(total_utxo_set_size_for_accumulator, add_element_size): print("--> initialize and fill up accumulator state") n, A0, S = setup() elements_for_accumulator = create_random_list( total_utxo_set_size_for_accumulator) tik = time.time() A1, proof1 = batch_add(A0, S, elements_for_accumulator, n) tok = time.time() acc_batch_add_genesis_timing.append(tok - tik) print("<-- Done.", acc_batch_add_genesis_timing[-1]) add_elements = create_random_list(add_element_size) print("--> ADD elements to accumulator") tik = time.time() A2, proof2 = batch_add(A1, S, add_elements, n) tok = time.time() print("<-- Done", tok - tik) nonces_list = [] for x in add_elements: _, nonce = hash_to_prime(x=x, nonce=0) nonces_list.append(nonce) print("--> verify NI_POES") tik = time.time() print( batch_verify_membership_with_NIPoE(proof2[0], proof2[1], A1, add_elements, nonces_list, A2, n)) tok = time.time() print("<-- Done", tok - tik)
def test_non_membership_witness_update_on_batch_add( total_utxo_set_size_for_accumulator, add_element_size): print("total_utxo_set_size_for_accumulator =", total_utxo_set_size_for_accumulator) print("--> initialize and fill up accumulator state") n, A0, S, order = setup() elements_for_accumulator = create_random_list( total_utxo_set_size_for_accumulator) tik = time.time() A, _ = batch_add(A0, S, elements_for_accumulator, n, order) tok = time.time() print("<-- Done.", tok - tik) test_element = random.randint(1, pow(2, 256)) if test_element in S.keys(): print( "test_element is included in accumulator, change it to other value!" ) print("--> generate non-membership witness for ", test_element) d, b = prove_non_membership(A0, S, test_element, 0, n, order) print("<-- Done.") print("--> prove non-membership without add") result = verify_non_membership(A0, A, d, b, test_element, 0, n) print("<-- Done.") print("verify result ", result) print("--> add random elements, size:", add_element_size) elements_for_add = create_random_list(add_element_size) A_add = batch_add(A, S, elements_for_add, n, order)[0] print("<-- Done.") print("--> update non-membership witness for batch add") # d, b = acc_batch_add_non_membership_witness_update(A, S, test_element, d, b, elements_for_add, n) # add_elements_product is included in block header, not right, it is too big tik = time.time() add_elements_product = calculate_primes_product(elements_for_add, n) d, b = acc_batch_add_prime_non_membership_witness_update( A, S, test_element, d, b, add_elements_product, n, order) tok = time.time() acc_non_membership_update_result_time.append(tok - tik) print("<-- Done.", tok - tik) print("--> prove non-membership after add elements for size:", add_element_size) result = verify_non_membership(A0, A_add, d, b, test_element, 0, n) print("<-- Done.") acc_non_membership_update_result.append(result)
def test_mem_witness_up_on_delete(total_utxo_set_size_for_accumulator, delete_element_index): if delete_element_index >= total_utxo_set_size_for_accumulator: print("delete element index should smaller than total_utxo_set_size") print("total_utxo_set_size_for_accumulator =", total_utxo_set_size_for_accumulator) print("--> initialize and fill up accumulator state") n, A0, S, order = setup() elements_for_accumulator = create_random_list( total_utxo_set_size_for_accumulator) tik = time.time() A_post, _ = batch_add(A0, S, elements_for_accumulator, n) tok = time.time() print("<-- Done.", tok - tik) print("--> generate other element proof") tik = time.time() other_element_proofs = {} for i in range(len(elements_for_accumulator)): if i != delete_element_index: other_element_proofs[ elements_for_accumulator[i]] = prove_membership( A0, S, elements_for_accumulator[i], n) tok = time.time() print("<-- Done.", tok - tik) print("--> delete element and generate A_del accumulator value.") tik = time.time() delete_element = elements_for_accumulator[delete_element_index] A_del = delete(A0, A_post, S, delete_element, n) tok = time.time() print("<-- Done.", tok - tik) print("--> update proof on delete element") update_proofs = {} tik = time.time() for x, proof in other_element_proofs.items(): update_proofs[x] = acc_del_mem_witness_update(x, proof, delete_element, A_del, n) tok = time.time() print("<-- Done.", tok - tik) print("--> verify updated proof on delete element") tik = time.time() result = [] for x, proof in update_proofs.items(): result.append(verify_membership(A_del, x, S[x], proof, n)) tok = time.time() print("<-- Done.", tok - tik) print("verify results:") print(result)
def test_witness_update_on_del_add(total_utxo_set_size_for_accumulator, delete_element_size, add_element_size): if delete_element_size >= total_utxo_set_size_for_accumulator: print("delete element size should smaller than total_utxo_set_size") print("total_utxo_set_size_for_accumulator =", total_utxo_set_size_for_accumulator) print("--> initialize and fill up accumulator state") n, A0, S = setup() elements_for_accumulator = create_random_list( total_utxo_set_size_for_accumulator) tik = time.time() A_post, _ = batch_add(A0, S, elements_for_accumulator, n) tok = time.time() print("<-- Done.", tok - tik) print("--> generate the last element proof") tik = time.time() last_element = elements_for_accumulator[-1] last_element_proof = prove_membership(A0, S, elements_for_accumulator[-1], n) tok = time.time() print("<-- Done.", tok - tik) print("--> delete elements") delete_elements = [] for i in range(delete_element_size): delete_elements.append(elements_for_accumulator[i]) A_post_del = batch_delete(A0, S, delete_elements, n) print("<-- Done.") print("--> witness update on batch delete") tik = time.time() new_proof_del = acc_batch_del_mem_witness_update(last_element, last_element_proof, delete_elements, A_post_del, n) tok = time.time() print("<-- Done.", tok - tik) print("--> verify new proof") result = verify_membership(A_post_del, last_element, S[last_element], new_proof_del, n) print("Done") print(result) elements_for_add = create_random_list(add_element_size) print("--> witness update on batch add") tik = time.time() acc_batch_add_witness_update(S, last_element, new_proof_del, elements_for_add, n) tok = time.time() print("<-- Done.", tok - tik)
def test_batch_add(self): n, A0, S = setup() elements_list = create_list(10) A_post_add, nipoe = batch_add(A0, S, elements_list, n) self.assertEqual(len(S), 10) nonces_list = list(map(lambda e: hash_to_prime(e)[1], elements_list)) is_valid = batch_verify_membership_with_NIPoE(nipoe[0], nipoe[1], A0, elements_list, nonces_list, A_post_add, n) self.assertTrue(is_valid)
def test_create_all_membership_witnesses(self): n, A0, S = setup() elements_list = create_list(3) A1, nipoe = batch_add(A0, S, elements_list, n) witnesses = create_all_membership_witnesses(A0, S, n) elements_list = list(S.keys()) # this specific order is important for i, witness in enumerate(witnesses): self.assertTrue(verify_membership(A1, elements_list[i], S[elements_list[i]], witness, n))
def test_create_all_membership_witnesses(total_utxo_set_size_for_accumulator): print("total_utxo_set_size_for_accumulator =", total_utxo_set_size_for_accumulator) print("--> initialize and fill up accumulator state") n, A0, S, order = setup() elements_for_accumulator = create_random_list( total_utxo_set_size_for_accumulator) tik = time.time() batch_add(A0, S, elements_for_accumulator, n) tok = time.time() print("<-- Done.", tok - tik) print("--> create all membership witness") tik = time.time() create_all_membership_witnesses(A0, S, n) tok = time.time() acc_create_all_membership_witness.append(tok - tik) print("<-- Done.", tok - tik)
def test_agg_mem_witnesses(self): n, A0, S = setup() elements_list = create_list(3) A1, nipoe = batch_add(A0, S, elements_list, n) witnesses = create_all_membership_witnesses(A0, S, n) elements_list = list(S.keys()) # this specific order is important for i, witness in enumerate(witnesses): self.assertTrue(verify_membership(A1, elements_list[i], S[elements_list[i]], witness, n)) nonces_list = [S[x] for x in elements_list] agg_wit, nipoe = aggregate_membership_witnesses(A1, witnesses, elements_list, nonces_list, n) is_valid = batch_verify_membership_with_NIPoE(nipoe[0], nipoe[1], agg_wit, elements_list, nonces_list, A1, n) self.assertTrue(is_valid)
def test_non_membership_witness_update_on_add( total_utxo_set_size_for_accumulator): print("total_utxo_set_size_for_accumulator =", total_utxo_set_size_for_accumulator) print("--> initialize and fill up accumulator state") n, A0, S, order = setup() elements_for_accumulator = create_random_list( total_utxo_set_size_for_accumulator) tik = time.time() A, _ = batch_add(A0, S, elements_for_accumulator, n) tok = time.time() print("<-- Done.", tok - tik) test_element = random.randint(1, pow(2, 256)) if test_element in S.keys(): print( "test_element is included in accumulator, change it to other value!" ) print("--> generate non-membership witness for ", test_element) d, b = prove_non_membership(A0, S, test_element, 0, n) print("<-- Done.") print("--> prove non-membership without add") result = verify_non_membership(A0, A, d, b, test_element, 0, n) print("<-- Done.") print("verify result ", result) print("--> add random element and update non-membership witness for", test_element) element_add = random.randint(1, pow(2, 256)) A_add = add(A, S, element_add, n) d, b = acc_add_non_membership_witness_update(A, S, test_element, d, b, element_add, n) print("<-- Done.") print("--> prove non-membership after add", element_add) result = verify_non_membership(A0, A_add, d, b, test_element, 0, n) print("<-- Done.") acc_non_membership_update_result.append(result)
def test_mining(total_utxo_set_size_for_merkle_tree, total_utxo_set_size_for_accumulator, num_of_inputs_in_tx, num_of_outputs_in_tx, num_of_txs_in_block): print("----------------------") print("total_utxo_set_size_for_merkle_tree =", total_utxo_set_size_for_merkle_tree) print("total_utxo_set_size_for_accumulator =", total_utxo_set_size_for_accumulator) print("num_of_inputs_in_tx =", num_of_inputs_in_tx) print("num_of_outputs_in_tx =", num_of_outputs_in_tx) print("num_of_txs_in_block =", num_of_txs_in_block) print("--> initialize and fill up Merkle tree state") merkle_tree = merkletools.MerkleTools() elements_for_merkle_tree = create_random_list( total_utxo_set_size_for_merkle_tree) tik = time.time() for i in range(len(elements_for_merkle_tree)): merkle_tree.add_leaf(str(i), True) merkle_tree.make_tree() tok = time.time() merkle_add_timing.append(tok - tik) print("<-- Done.", merkle_add_timing[-1]) print("--> initialize and fill up accumulator state") n, A0, S, order = setup() if total_utxo_set_size_for_accumulator < num_of_inputs_in_tx * num_of_txs_in_block: print("please select larger total_utxo_set_size_for_accumulator.") return None elements_for_accumulator = create_random_list( total_utxo_set_size_for_accumulator) inputs_for_accumulator = elements_for_accumulator[0:(num_of_inputs_in_tx * num_of_txs_in_block)] outputs_for_accumulator = create_random_list(num_of_outputs_in_tx * num_of_txs_in_block) tik = time.time() A_post_batch_add, proof = batch_add(A0, S, elements_for_accumulator, n) inputs_nonces_list = [S[x] for x in inputs_for_accumulator] tok = time.time() acc_batch_add_genesis_timing.append(tok - tik) print("<-- Done.", acc_batch_add_genesis_timing[-1]) print("--> prove membership Merkle tree") times = [] merkle_proofs = [] for i in range(num_of_inputs_in_tx * num_of_txs_in_block): tik = time.time() merkle_proofs.append(merkle_tree.get_proof(i)) tok = time.time() times.append(tok - tik) sum_times = sum(times) merkle_proof_timing.append(sum_times / num_of_inputs_in_tx) # average ; per tx print("<-- Done. total:", sum_times, "; per tx:", merkle_proof_timing[-1]) print("--> prove membership accumulator") times = [] acc_mem_proofs = [] for i in range(num_of_txs_in_block): tik = time.time() inputs_list = [] for j in range(num_of_inputs_in_tx): inputs_list.append(inputs_for_accumulator[num_of_inputs_in_tx * i + j]) acc_mem_proofs.append(batch_prove_membership(A0, S, inputs_list, n)) tok = time.time() times.append(tok - tik) sum_times = sum(times) acc_batch_prove_mem_timing.append(sum_times / len(times)) # average print("<-- Done. total:", sum_times, "; per tx:", acc_batch_prove_mem_timing[-1]) print("--> prove membership accumulator with NI-PoE") times = [] acc_mem_proofs_with_NIPoE = [] for i in range(num_of_txs_in_block): tik = time.time() inputs_list = [] for j in range(num_of_inputs_in_tx): inputs_list.append(inputs_for_accumulator[num_of_inputs_in_tx * i + j]) acc_mem_proofs_with_NIPoE.append( batch_prove_membership_with_NIPoE(A0, S, inputs_list, n, A_post_batch_add)) tok = time.time() times.append(tok - tik) sum_times = sum(times) acc_batch_prove_mem_with_NIPoE_timing.append(sum_times / len(times)) # average print("<-- Done. total:", sum_times, "; per tx:", acc_batch_prove_mem_with_NIPoE_timing[-1]) print("--> Merkle tree verify membership") merkle_root = merkle_tree.get_merkle_root() merkle_leaves = [] for i in range(num_of_inputs_in_tx * num_of_txs_in_block): merkle_leaves.append(merkle_tree.get_leaf(i)) tik = time.time() for i in range(num_of_txs_in_block): for j in range(num_of_inputs_in_tx): assert merkle_tree.validate_proof(merkle_proofs[i], merkle_leaves[i], merkle_root) tok = time.time() merkle_verify_mem_per_block_timing.append(tok - tik) merkle_verify_mem_per_tx_timing.append( (tok - tik) / num_of_txs_in_block) # average print("<-- Done. total (per block):", merkle_verify_mem_per_block_timing[-1], "; per tx:", merkle_verify_mem_per_tx_timing[-1]) print("--> accumulator batch verify membership") tik = time.time() for i in range(num_of_txs_in_block): inputs_list = [] for j in range(num_of_inputs_in_tx): inputs_list.append(inputs_for_accumulator[num_of_inputs_in_tx * i + j]) # TODO: nonces should be given by the proofs? nonces_list = list(map(lambda x: S[x], inputs_list)) assert batch_verify_membership(A_post_batch_add, inputs_list, nonces_list, acc_mem_proofs[i], n) tok = time.time() acc_batch_verify_mem_per_block_timing.append(tok - tik) acc_batch_verify_mem_per_tx_timing.append( (tok - tik) / num_of_txs_in_block) # average print("<-- Done. total (per block):", acc_batch_verify_mem_per_block_timing[-1], "; per tx:", acc_batch_verify_mem_per_tx_timing[-1]) print("--> accumulator batch verify membership with NIPoE") tik = time.time() for i in range(num_of_txs_in_block): inputs_list = [] for j in range(num_of_inputs_in_tx): inputs_list.append(inputs_for_accumulator[num_of_inputs_in_tx * i + j]) # TODO: nonces should be given by the proofs? nonces_list = list(map(lambda x: S[x], inputs_list)) assert batch_verify_membership_with_NIPoE( acc_mem_proofs_with_NIPoE[i][0], acc_mem_proofs_with_NIPoE[i][1], acc_mem_proofs_with_NIPoE[i][2], inputs_list, nonces_list, A_post_batch_add, n) tok = time.time() acc_batch_verify_mem_with_NIPoE_per_block_timing.append(tok - tik) acc_batch_verify_mem_with_NIPoE_per_tx_timing.append( (tok - tik) / num_of_txs_in_block) # average print("<-- Done. total (per block):", acc_batch_verify_mem_with_NIPoE_per_block_timing[-1], "; per tx:", acc_batch_verify_mem_with_NIPoE_per_tx_timing[-1]) print("--> accumulator batch delete spent TXOs + first NI-PoE") tik = time.time() agg_inputs_indexes = [] for i in range(num_of_txs_in_block): agg_inputs_indexes.append( [num_of_inputs_in_tx * i, num_of_inputs_in_tx * (i + 1)]) # TODO: can we get the NI-PoE proofs here? A_post_batch_delete, niope1 = batch_delete_using_membership_proofs( A_post_batch_add, S, inputs_for_accumulator, acc_mem_proofs, n, agg_inputs_indexes) tok = time.time() acc_batch_delete_timing.append(tok - tik) print("<-- Done.", acc_batch_delete_timing[-1]) print("--> accumulator batch add new UTXOs + second NI-PoE") tik = time.time() A_post_batch_add_new, niope2 = batch_add(A_post_batch_delete, S, outputs_for_accumulator, n) outputs_nonces_list = [S[x] for x in outputs_for_accumulator] tok = time.time() acc_batch_add_per_block_timing.append(tok - tik) print("<-- Done.", acc_batch_add_per_block_timing[-1]) print("--> accumulator verify first NI-PoE & second NI-PoE") tik = time.time() assert batch_verify_membership_with_NIPoE(niope1[0], niope1[1], A_post_batch_delete, inputs_for_accumulator, inputs_nonces_list, A_post_batch_add, n) assert batch_verify_membership_with_NIPoE(niope2[0], niope2[1], A_post_batch_delete, outputs_for_accumulator, outputs_nonces_list, A_post_batch_add_new, n) tok = time.time() acc_batch_verify_two_NIPoE_post_mining.append(tok - tik) print("<-- Done.", acc_batch_verify_two_NIPoE_post_mining[-1])