def test_med_batch_insert(self): values = [random_string() for _ in range(100)] leaves = [ random_index(digest_size=SparseMerkleTree.TREE_DEPTH) for _ in range(100) ] self.T.batch_insert({k: v for (k, v) in zip(leaves, values)}) for idx in leaves: proof = self.T.generate_proof(idx) self.assertEqual(len(proof.copath), SparseMerkleTree.TREE_DEPTH) self.assertTrue(self.T.verify_proof(proof))
def test_membership_large(self): indices = [ random_index(digest_size=SparseMerkleTree.TREE_DEPTH) for i in range(NUM_ITERATIONS) ] for number, index in enumerate(indices): data = "angela{}".format(number) self.T.insert(index, data.encode()) proofs = list() for index in indices: proofs.append(self.T.generate_proof(index)) for proof in proofs: self.assertTrue(self.T.verify_proof(proof))
def test_practice(self): indices = [util.random_index() for i in range(128)] data = [util.random_string() for i in range(128)] print("Inserting") for i in range(128): Client.insert_leaf(indices[i], data[i]) print("Getting root") root = Client.get_signed_root() print("Proving") for i in range(128): proof = Client.generate_proof(indices[i]) d = data[i] self.assertTrue(Client.verify_proof(proof, d, root))
def test_non_membership_large(self): indices = list() for i in range(NUM_ITERATIONS): index = random_index(digest_size=SparseMerkleTree.TREE_DEPTH) is_member = flip_coin() if is_member: data = "angela{}".format(i) self.T.insert(index, data.encode()) indices.append((index, is_member)) i = True for index, is_member in indices: proof = self.T.generate_proof(index) # self.assertEqual(len(proof.proof_id), len(proof.copath[0][0])) # ^ this assert will fail if there is no copath self.assertEqual(proof.proof_type, is_member) self.assertTrue(self.T.verify_proof(proof))
def benchmark(): global request_count num_inserts = int(request.args.get('num_inserts')) print("Generating workload...") indices = [random_index() for i in range(num_inserts)] data = [random_string() for i in range(num_inserts)] for i in range(num_inserts): worker_id = calculate_worker_id(indices[i], ray_info['prefix_length']) print("Sending insert request to worker {}".format(worker_id)) ray.get(ray_info['leaf_workers'][worker_id].queue_write.remote( indices[i], data[i])) request_count += num_inserts print("Workload generated") return "Workload generated", 200
str(epoch_number) ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) time.sleep(5) iterations = int(max_batch / batch_size) for i in range(iterations): print(" [Iteration {}]".format(i)) inserts = 0 indices = list() datas = list() while inserts < batch_size: is_member = util.flip_coin(bias=0.75) index = util.random_index() data = util.random_string() if is_member: Client.insert_leaf(index, data) inserts += 1 indices.append(index) datas.append(data) soundness_total += 1 root = Client.get_signed_root() print("Root received") for i in range(batch_size): proof = Client.generate_proof(indices[i]) data = datas[i] if not Client.verify_proof(proof, data, root): # print("Proof Type IN TEST SOUNDNESS", proof.ProofType) print("FAILED")
def test_membership(self): index = random_index(digest_size=SparseMerkleTree.TREE_DEPTH) self.T.insert(index, b"angela") proof = self.T.generate_proof(index) self.assertEqual(len(proof.copath), SparseMerkleTree.TREE_DEPTH) self.assertTrue(self.T.verify_proof(proof))
def test_non_membership(self): index = random_index(digest_size=SparseMerkleTree.TREE_DEPTH) proof = self.T.generate_proof(index) self.assertFalse(proof.proof_type) self.assertTrue(self.T.verify_proof(proof))
import cProfile, pstats, merkle.smt from common import util keys = [util.random_index() for i in range(100)] values = [util.random_string() for i in range(100)] proofs = list() T = merkle.smt.SparseMerkleTree("sha256") T_batch = merkle.smt.SparseMerkleTree("sha256") def profile_inserts(): pr = cProfile.Profile() for i in range(100): pr.enable() T.insert(keys[i], values[i]) pr.disable() with open("profiles/profile_inserts.txt", 'w') as output: p = pstats.Stats(pr, stream=output) p.sort_stats('time') p.print_stats() def profile_prover(): pr = cProfile.Profile() for i in range(100): pr.enable() proofs.append(T.generate_proof(keys[i]))