async def _run(peers, n, t, my_id, batch_size): g, h, pks, sks = get_avss_params(n + 1, t) async with ProcessProgramRunner(peers, n + 1, t, my_id) as runner: send, recv = runner.get_send_recv("HBAVSS_BATCH") crs = gen_pc_const_crs(t, g=g, h=h) values = None dealer_id = n if my_id == dealer_id: # Dealer values = [ZR.random(0)] * batch_size logger.info("Starting DEALER") logger.info(f"Dealer timestamp: {time.time()}") else: logger.info("Starting RECIPIENT: %d", my_id) with HbAvssBatch(pks, sks[my_id], crs, n, t, my_id, send, recv) as hbavss: begin_time = time.time() if my_id != dealer_id: hbavss_task = asyncio.create_task( hbavss.avss(0, dealer_id=dealer_id, values=values, client_mode=True)) await hbavss.output_queue.get() end_time = time.time() logger.info(f"Recipient time: {(end_time - begin_time)}") hbavss_task.cancel() else: await hbavss.avss(0, dealer_id=dealer_id, values=values, client_mode=True) end_time = time.time() logger.info(f"Dealer time: {(end_time - begin_time)}")
def test_benchmark_commit(benchmark, t): alpha = ZR.random() g = G1.rand() h = G1.rand() crs = gen_pc_const_crs(t, alpha=alpha, g=g, h=h) pc = PolyCommitConst(crs) phi = polynomials_over(ZR).random(t) benchmark(pc.commit, phi)
def test_benchmark_create_witness(benchmark, t): alpha = ZR.random() g = G1.rand() h = G1.rand() crs = gen_pc_const_crs(t, alpha=alpha, g=g, h=h) pc = PolyCommitConst(crs) phi = polynomials_over(ZR).random(t) c, phi_hat = pc.commit(phi) pc.preprocess_prover(10) i = ZR.random() benchmark(pc.create_witness, phi, phi_hat, i)
def test_pc_const(): t = 3 alpha = ZR.random() g = G1.rand() h = G1.rand() crs = gen_pc_const_crs(t, alpha=alpha, g=g, h=h) pc = PolyCommitConst(crs) phi = polynomials_over(ZR).random(t) c, phi_hat = pc.commit(phi) witness = pc.create_witness(phi, phi_hat, 3) assert c == g**phi(alpha) * h**phi_hat(alpha) assert pc.verify_eval(c, 3, phi(3), phi_hat(3), witness) assert not pc.verify_eval(c, 4, phi(3), phi_hat(3), witness)
def test_benchmark_hbavss_dealer(test_router, benchmark, t, k): loop = asyncio.get_event_loop() n = 3 * t + 1 field = GF(Subgroup.BLS12_381) g, h, pks, sks = get_avss_params(n + 1, t) crs = gen_pc_const_crs(t, g=g, h=h) pc = PolyCommitConst(crs, field=field) pc.preprocess_prover(8) pc.preprocess_verifier(8) values = [field.random() for _ in range(k)] params = (t, n, g, h, pks, sks, crs, pc, values, field) def _prog(): loop.run_until_complete(hbavss_multibatch_dealer(test_router, params)) benchmark(_prog)
async def test_hbavss_batch_batch_gf(test_router): t = 2 n = 3 * t + 1 g, h, pks, sks = get_avss_params(n, t) sends, recvs, _ = test_router(n) crs = gen_pc_const_crs(t, g=g, h=h) field = GF(Subgroup.BLS12_381) values = [field.random() for _ in range(50)] avss_tasks = [None] * n dealer_id = randint(0, n - 1) shares = [None] * n with ExitStack() as stack: hbavss_list = [None] * n for i in range(n): hbavss = HbAvssBatch(pks, sks[i], crs, n, t, i, sends[i], recvs[i], field=field) hbavss_list[i] = hbavss stack.enter_context(hbavss) if i == dealer_id: avss_tasks[i] = asyncio.create_task( hbavss.avss(0, values=values)) else: avss_tasks[i] = asyncio.create_task( hbavss.avss(0, dealer_id=dealer_id)) avss_tasks[i].add_done_callback(print_exception_callback) outputs = await asyncio.gather( *[hbavss_list[i].output_queue.get() for i in range(n)]) shares = [output[2] for output in outputs] for task in avss_tasks: task.cancel() fliped_shares = list(map(list, zip(*shares))) recovered_values = [] for item in fliped_shares: recovered_values.append( polynomials_over(field).interpolate_at(zip(range(1, n + 1), item))) assert recovered_values == values
async def test_hbavss_batch_client_mode(test_router): t = 2 n = 3 * t + 1 g, h, pks, sks = get_avss_params(n + 1, t) sends, recvs, _ = test_router(n + 1) crs = gen_pc_const_crs(t, g=g, h=h) values = [ZR.random()] * (t + 1) avss_tasks = [None] * (n + 1) hbavss_list = [None] * n dealer_id = n with ExitStack() as stack: client_hbavss = HbAvssBatch(pks, None, crs, n, t, dealer_id, sends[dealer_id], recvs[dealer_id]) stack.enter_context(client_hbavss) avss_tasks[n] = asyncio.create_task( client_hbavss.avss(0, values=values, client_mode=True)) for i in range(n): hbavss = HbAvssBatch(pks, sks[i], crs, n, t, i, sends[i], recvs[i]) hbavss_list[i] = hbavss stack.enter_context(hbavss) avss_tasks[i] = asyncio.create_task( hbavss.avss(0, dealer_id=dealer_id, client_mode=True)) avss_tasks[i].add_done_callback(print_exception_callback) outputs = await asyncio.gather( *[hbavss_list[i].output_queue.get() for i in range(n)]) shares = [output[2] for output in outputs] for task in avss_tasks: task.cancel() fliped_shares = list(map(list, zip(*shares))) recovered_values = [] for item in fliped_shares: recovered_values.append( polynomials_over(ZR).interpolate_at(zip(range(1, n + 1), item))) assert recovered_values == values
async def test_hbavss_batch_encryption_fault(test_router): class BadDealer(HbAvssBatch): def _get_dealer_msg(self, values, n): fault_n = randint(1, n - 1) secret_size = len(values) phi = [None] * secret_size commitments = [None] * secret_size aux_poly = [None] * secret_size for k in range(secret_size): phi[k] = self.poly.random(self.t, values[k]) commitments[k], aux_poly[k] = self.poly_commit.commit(phi[k]) ephemeral_secret_key = self.field.random() ephemeral_public_key = pow(self.g, ephemeral_secret_key) dispersal_msg_list = [None] * n for i in range(n): shared_key = pow(self.public_keys[i], ephemeral_secret_key) z = [None] * secret_size for k in range(secret_size): witness = self.poly_commit.create_witness( phi[k], aux_poly[k], i + 1) if i == fault_n: z[k] = (ZR.random(), ZR.random(), witness) else: z[k] = (phi[k](i + 1), aux_poly[k](i + 1), witness) zz = SymmetricCrypto.encrypt(str(shared_key).encode(), z) dispersal_msg_list[i] = zz return dumps( (commitments, ephemeral_public_key)), dispersal_msg_list t = 2 n = 3 * t + 1 g, h, pks, sks = get_avss_params(n, t) sends, recvs, _ = test_router(n) crs = gen_pc_const_crs(t, g=g, h=h) values = [ZR.random()] * (t + 1) avss_tasks = [None] * n dealer_id = randint(0, n - 1) with ExitStack() as stack: hbavss_list = [] for i in range(n): if i == dealer_id: hbavss = BadDealer(pks, sks[i], crs, n, t, i, sends[i], recvs[i]) else: hbavss = HbAvssBatch(pks, sks[i], crs, n, t, i, sends[i], recvs[i]) hbavss_list.append(hbavss) stack.enter_context(hbavss) if i == dealer_id: avss_tasks[i] = asyncio.create_task( hbavss.avss(0, values=values)) avss_tasks[i].add_done_callback(print_exception_callback) else: avss_tasks[i] = asyncio.create_task( hbavss.avss(0, dealer_id=dealer_id)) avss_tasks[i].add_done_callback(print_exception_callback) outputs = await asyncio.gather( *[hbavss_list[i].output_queue.get() for i in range(n)]) shares = [output[2] for output in outputs] for task in avss_tasks: task.cancel() fliped_shares = list(map(list, zip(*shares))) recovered_values = [] for item in fliped_shares: recovered_values.append( polynomials_over(ZR).interpolate_at(zip(range(1, n + 1), item))) assert recovered_values == values