def test_make_proof(self): wrapper = Mixer(NATIVE_LIB_PATH, VK_PATH, PK_PATH) tree_depth = wrapper.tree_depth n_items = 2 << (tree_depth - 1) tree = MerkleTree(n_items) for n in range(0, 2): tree.append(int(FQ.random())) wallet_address = int(FQ.random()) nullifier_secret = int(FQ.random()) nullifier_hash = mimc_hash([nullifier_secret, nullifier_secret]) leaf_hash = int( get_sha256_hash(to_hex(nullifier_secret), to_hex(wallet_address)), 16) leaf_idx = tree.append(leaf_hash) self.assertEqual(leaf_idx, tree.index(leaf_hash)) # Verify it exists in true leaf_proof = tree.proof(leaf_idx) self.assertTrue(leaf_proof.verify(tree.root)) # Generate proof snark_proof = wrapper.prove( tree.root, wallet_address, nullifier_hash, nullifier_secret, # (index)_2 bits reversed, i.e. [LSB, ... , MSB] leaf_proof.address, leaf_proof.path) self.assertTrue(wrapper.verify(snark_proof))
def test_make_proof(self): n_items = 2<<28 tree = MerkleTree(n_items) for n in range(0, 2): tree.append(int(FQ.random())) exthash = int(FQ.random()) secret = int(FQ.random()) leaf_hash = mimc_hash([secret]) leaf_idx = tree.append(leaf_hash) self.assertEqual(leaf_idx, tree.index(leaf_hash)) # Verify it exists in true leaf_proof = tree.proof(leaf_idx) self.assertTrue(leaf_proof.verify(tree.root)) # Generate proof wrapper = Miximus(NATIVE_LIB_PATH, VK_PATH, PK_PATH) tree_depth = wrapper.tree_depth snark_proof = wrapper.prove( tree.root, secret, exthash, leaf_proof.address, leaf_proof.path) self.assertTrue(wrapper.verify(snark_proof))
def test_make_proof(self): n_items = 2 << 28 tree = MerkleTree(n_items) for n in range(0, 2): tree.append(random_element()) exthash = random_element() nullifier = random_element() spend_preimage = random_element() spend_hash_IV = 0 spend_hash = LongsightL12p5_MP([spend_preimage, nullifier], spend_hash_IV) leaf_hash_IV = 0 leaf_hash = LongsightL12p5_MP([nullifier, spend_hash], leaf_hash_IV) leaf_idx = tree.append(leaf_hash) self.assertEqual(leaf_idx, tree.index(leaf_hash)) # Verify it exists in true leaf_proof = tree.proof(leaf_idx) self.assertTrue(leaf_proof.verify(tree.root)) # Generate proof wrapper = Miximus(NATIVE_LIB_PATH, VK_PATH, PK_PATH) tree_depth = wrapper.tree_depth snark_proof = wrapper.prove(tree.root, nullifier, spend_preimage, exthash, leaf_proof.address, leaf_proof.path) self.assertTrue(wrapper.verify(snark_proof))
def test_known_2pow28(self): tree = MerkleTree(2<<28) item_a = 3703141493535563179657531719960160174296085208671919316200479060314459804651 tree.append(item_a) item_b = 134551314051432487569247388144051420116740427803855572138106146683954151557 tree.append(item_b) self.assertEqual(tree.root, 14972246236048249827985830600768475898195156734731557762844426864943654467818) proof_a = tree.proof(0) self.assertTrue(proof_a.verify(tree.root)) proof_b = tree.proof(1) self.assertTrue(proof_b.verify(tree.root)) self.assertEqual(tree.leaf(0, 0), 3703141493535563179657531719960160174296085208671919316200479060314459804651) self.assertEqual(tree.leaf(1, 0), 3075442268020138823380831368198734873612490112867968717790651410945045657947) self.assertEqual(tree.leaf(2, 0), 10399465128272526817755257959020023025563587559350936053132523411421423507430) self.assertEqual(tree.leaf(1, 1), 17296471688945713021042054900108821045192859417413320566181654591511652308323) self.assertEqual(tree.leaf(2, 1), 4832852105446597958495745596582249246190817345027389430471458078394903639834) self.assertEqual(tree.leaf(13, 1), 14116139569958633576637617144876714429777518811711593939929091541932333542283) self.assertEqual(tree.leaf(22, 1), 16077039334695461958102978289003547153551663194787878097275872631374489043531)
def test_update(self): """ Verify that items in the tree can be updated """ tree = MerkleTree(2) tree.append(FQ.random()) tree.append(FQ.random()) proof_0_before = tree.proof(0) proof_1_before = tree.proof(1) root_before = tree.root self.assertTrue(proof_0_before.verify(tree.root)) self.assertTrue(proof_1_before.verify(tree.root)) leaf_0_after = FQ.random() tree.update(0, leaf_0_after) root_after_0 = tree.root proof_0_after = tree.proof(0) self.assertTrue(proof_0_after.verify(tree.root)) self.assertNotEqual(root_before, root_after_0) leaf_1_after = FQ.random() tree.update(1, leaf_1_after) root_after_1 = tree.root proof_1_after = tree.proof(1) self.assertTrue(proof_1_after.verify(tree.root)) self.assertNotEqual(root_before, root_after_1) self.assertNotEqual(root_after_0, root_after_1)
def test_make_proof(self): n_items = 2 << 28 tree = MerkleTree(n_items) for n in range(0, 2): tree.append(int(FQ.random())) exthash = int(FQ.random()) prehash = int(FQ.random()) secret = int(FQ.random()) msg = int(FQ.random()) leaf_hash = mimc_hash([secret]) sig_msg_hash = mimc_hash([msg]) leaf_idx = tree.append(leaf_hash) self.assertEqual(leaf_idx, tree.index(leaf_hash)) # Verify it exists in true leaf_proof = tree.proof(leaf_idx) self.assertTrue(leaf_proof.verify(tree.root)) self.assertTrue(prehash, sig_msg_hash) # Generate proof wrapper = Ssles(NATIVE_LIB_PATH, VK_PATH, PK_PATH) tree_depth = wrapper.tree_depth snark_proof = wrapper.prove(tree.root, secret, msg, exthash, prehash, leaf_proof.address, leaf_proof.path) self.assertTrue(wrapper.verify(snark_proof)) if __name__ == "__main__": unittest.main()
def __init__(self, operator, root_chain): self.operator = operator self.root_chain = root_chain self.chain = {} self.tree = MerkleTree(2) self.roots = {} self.nullifiers = {} self.height = 0 self.current_block = Block()
def test_known_2pow28(self): tree = MerkleTree(2 << 28) item_a = 3703141493535563179657531719960160174296085208671919316200479060314459804651 tree.append(item_a) item_b = 134551314051432487569247388144051420116740427803855572138106146683954151557 tree.append(item_b) self.assertEqual( tree.root, 10928083011190212400724282287039881565290562079447442292540304400330695864757 )
def test_known_2pow28(self): tree = MerkleTree(2 << 28) item_a = 3703141493535563179657531719960160174296085208671919316200479060314459804651 tree.append(item_a) item_b = 134551314051432487569247388144051420116740427803855572138106146683954151557 tree.append(item_b) self.assertEqual( tree.root, 12880293998234311228895747943713504338160238149993004139365982527556885579681 ) proof_a = tree.proof(0) self.assertTrue(proof_a.verify(tree.root)) proof_b = tree.proof(1) self.assertTrue(proof_b.verify(tree.root)) self.assertEqual( tree.leaf(0, 0), 3703141493535563179657531719960160174296085208671919316200479060314459804651 ) self.assertEqual( tree.leaf(1, 0), 13981856331482487152452149678096232821987624395720231314895268163963385035507 ) self.assertEqual( tree.leaf(2, 0), 4008582766686307301250960594183893449903725811265984514032955228389672705119 ) self.assertEqual( tree.leaf(1, 1), 17296471688945713021042054900108821045192859417413320566181654591511652308323 ) self.assertEqual( tree.leaf(2, 1), 4832852105446597958495745596582249246190817345027389430471458078394903639834 ) self.assertEqual( tree.leaf(13, 1), 14116139569958633576637617144876714429777518811711593939929091541932333542283 ) self.assertEqual( tree.leaf(22, 1), 16077039334695461958102978289003547153551663194787878097275872631374489043531 )
def test_known1(self): tree = MerkleTree(2) item_a = 3703141493535563179657531719960160174296085208671919316200479060314459804651 tree.append(item_a) item_b = 134551314051432487569247388144051420116740427803855572138106146683954151557 tree.append(item_b) self.assertEqual(tree.root, 3075442268020138823380831368198734873612490112867968717790651410945045657947) proof_a = tree.proof(0) self.assertEqual(proof_a.path, [item_b]) proof_b = tree.proof(1) self.assertEqual(proof_b.path, [item_a])
def test_known1(self): tree = MerkleTree(2) item_a = 3703141493535563179657531719960160174296085208671919316200479060314459804651 tree.append(item_a) item_b = 134551314051432487569247388144051420116740427803855572138106146683954151557 tree.append(item_b) self.assertEqual( tree.root, 13981856331482487152452149678096232821987624395720231314895268163963385035507 ) proof_a = tree.proof(0) self.assertEqual(proof_a.path, [item_b]) proof_b = tree.proof(1) self.assertEqual(proof_b.path, [item_a])
def test_known1(self): tree = MerkleTree(2) item_a = 3703141493535563179657531719960160174296085208671919316200479060314459804651 tree.append(item_a) item_b = 134551314051432487569247388144051420116740427803855572138106146683954151557 tree.append(item_b) self.assertEqual( tree.root, 12232803403448551110711645741717605608347940439638387632993385741901727947062 ) proof_a = tree.proof(0) self.assertEqual(proof_a.path, [item_b]) proof_b = tree.proof(1) self.assertEqual(proof_b.path, [item_a])
def test_incremental_sha256(self): n_items = 100 tree = MerkleTree(n_items, MerkleHasherSHA256) self.assertEqual(tree.root, None) self.assertEqual(len(tree), 0) previous_root = None for n in range(0, n_items): item = bytes([n]) * 32 tree.append(item) self.assertEqual(len(tree), n + 1) self.assertNotEqual(tree.root, previous_root) previous_root = tree.root proof = tree.proof(n) self.assertTrue(proof.verify(tree.root)) # Then verify all existing items can also be proven to be in the tree for m in range(0, len(tree) - 1): self.assertTrue(tree.proof(m).verify(tree.root))
def test_tree(self): n_items = 32 tree = MerkleTree(n_items) self.assertEqual(tree.root, None) self.assertEqual(len(tree), 0) previous_root = None hasher = hashlib.sha256() for n in range(0, n_items): hasher.update(bytes([n]) * 32) item = int.from_bytes(hasher.digest(), 'big') % SNARK_SCALAR_FIELD tree.append(item) self.assertEqual(len(tree), n + 1) self.assertNotEqual(tree.root, previous_root) previous_root = tree.root proof = tree.proof(n) self.assertTrue(proof.verify(tree.root)) # Then verify all existing items can also be proven to be in the tree for m in range(0, len(tree) - 1): self.assertTrue(tree.proof(m).verify(tree.root))
def test_incremental_longsight(self): n_items = 100 tree = MerkleTree(n_items, MerkleHasherLongsightF) self.assertEqual(tree.root, None) self.assertEqual(len(tree), 0) previous_root = None for n in range(0, n_items): hasher = hashlib.sha256() hasher.update(bytes([n]) * 32) item = int.from_bytes(hasher.digest(), 'little') % curve_order tree.append(item) self.assertEqual(len(tree), n + 1) self.assertNotEqual(tree.root, previous_root) previous_root = tree.root proof = tree.proof(n) self.assertTrue(proof.verify(tree.root)) # Then verify all existing items can also be proven to be in the tree for m in range(0, len(tree) - 1): self.assertTrue(tree.proof(m).verify(tree.root))
def __init__(self, tree_size): self._accounts = [] self._key2idx = dict() self._tree = MerkleTree(tree_size)