def test_add_element(self): n, A0, S = setup() x0 = secrets.randbelow(pow(2, 256)) x1 = secrets.randbelow(pow(2, 256)) # first addition A1 = add(A0, S, x0, n) nonce = S[x0] proof = prove_membership(A0, S, x0, n) self.assertEqual(len(S), 1) self.assertEqual(A0, proof) self.assertTrue(verify_membership(A1, x0, nonce, proof, n)) # second addition A2 = add(A1, S, x1, n) nonce = S[x1] proof = prove_membership(A0, S, x1, n) self.assertEqual(len(S), 2) self.assertEqual(A1, proof) self.assertTrue(verify_membership(A2, x1, nonce, proof, n)) # delete A1_new = delete(A0, A2, S, x0, n) proof = prove_membership(A0, S, x1, n) proof_none = prove_membership(A0, S, x0, n) self.assertEqual(len(S), 1) self.assertEqual(proof_none, None) self.assertTrue(verify_membership(A1_new, x1, nonce, proof, n))
def test_shamir_trick_2(self): n, A0, S = setup() elements_list = create_list(2) A1 = add(A0, S, elements_list[0], n) A2 = add(A1, S, elements_list[1], n) prime0 = hash_to_prime(elements_list[0], nonce=S[elements_list[0]])[0] prime1 = hash_to_prime(elements_list[1], nonce=S[elements_list[1]])[0] proof0 = prove_membership(A0, S, elements_list[0], n) proof1 = prove_membership(A0, S, elements_list[1], n) agg_proof = shamir_trick(proof0, proof1, prime0, prime1, n) is_valid = pow(agg_proof, prime0 * prime1, n) == A2 self.assertTrue(is_valid)
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_delete(self): n, A0, S = setup() elements_list = create_list(5) A = A0 for i in range(len(elements_list)): A = add(A, S, elements_list[i], n) A_pre_delete = A elements_to_delete_list = [elements_list[0], elements_list[2], elements_list[4]] nonces_list = list(map(lambda e: hash_to_prime(e)[1], elements_to_delete_list)) proofs = list(map(lambda x: prove_membership(A0, S, x, n), elements_to_delete_list)) A_post_delete, nipoe = batch_delete_using_membership_proofs(A_pre_delete, S, elements_to_delete_list, proofs, n) is_valid = batch_verify_membership_with_NIPoE(nipoe[0], nipoe[1], A_post_delete, elements_to_delete_list, nonces_list, A_pre_delete, n) self.assertTrue(is_valid)
# This file is used to generate a proof for the RSAAccumulator smart contract import sys import secrets from main import setup, add, prove_membership from helpfunctions import hash_to_prime def to_padded_num_str(num, length_in_bytes): length_in_hex_str = length_in_bytes * 2 + 2 num_str = format(num, '#0' + str(length_in_hex_str) + 'x') return num_str n, A0, S = setup() x = secrets.randbelow(pow(2, 256)) A1 = add(A0, S, x, n) nonce = S[x] proof = prove_membership(A0, S, x, n) prime, nonce = hash_to_prime(x=x, nonce=nonce) print( to_padded_num_str(n, 384) + ',' + to_padded_num_str(proof, 384) + ',' + to_padded_num_str(prime, 32) + ',' + to_padded_num_str(A1, 384)) sys.stdout.flush()