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()) nullifier = int(FQ.random()) spend_preimage = int(FQ.random()) spend_hash = mimc_hash([spend_preimage, nullifier]) leaf_hash = mimc_hash([nullifier, spend_hash]) 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_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 merkle_root(data, iv=0): if len(data) == 0: raise Exception('merkle_root: empty data') if len(data) == 1: return mimc_hash(data, iv) with Pool(cpu_count()) as p: nodes = p.map( partial(mimc_hash_partial, iv), [data[i * 2:(i + 1) * 2] for i in range(0, len(data) // 2)]) if len(data) % 2 == 1: nodes.append(data[-1]) if len(nodes) == 1: return nodes[0] return merkle_root(nodes, mimc_hash([iv], iv))
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 hash(self): return mimc_hash([ int(self.publicKeyX), int(self.publicKeyY), int(self.nonce), int(self._balancesTree._root) ], 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()) # Cryptonian.base 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) logging.info(tree.root) logging.info(secret) logging.info(leaf_proof.address) logging.info(leaf_proof.path) tree_depth = wrapper.tree_depth snark_proof = wrapper.prove(tree.root, secret, leaf_proof.address, leaf_proof.path) # exthash, # Cryptonian.base Out!! self.assertTrue(wrapper.verify(snark_proof))
def test_hardcoded(self): # Verify cross-compatibility with EVM/Solidity implementation m0 = 3703141493535563179657531719960160174296085208671919316200479060314459804651 m1 = 134551314051432487569247388144051420116740427803855572138106146683954151557 assert mimc(m0, m1) == 11437467823393790387399137249441941313717686441929791910070352316474327319704 k = 918403109389145570117360101535982733651217667914747213867238065296420114726 assert mimc_hash([m0, m1], k) == 15683951496311901749339509118960676303290224812129752890706581988986633412003
def hash_public_inputs(self, serialNumbers, newRecords, memo, in_root): inputs_to_hash = [] for sn in serialNumbers: inputs_to_hash.append(int(sn)) for commit in newRecords: inputs_to_hash.append(int(commit)) for m in memo: inputs_to_hash.append(int(m)) inputs_to_hash.append(int(in_root)) return mimc_hash(inputs_to_hash)
def mimc_hash_partial(iv, data): return mimc_hash(data, iv)
def hash(self): return mimc_hash([int(self.balance), int(self._tradingHistoryTree._root)], 1)
def hash(self): return mimc_hash([int(self.filled), int(self.cancelled), int(self.orderID)], 1)