class TripleGenerator(PreProcessingBase): def __init__(self, n, t, my_id, send, recv, batch_size=10): super(TripleGenerator, self).__init__( n, t, my_id, send, recv, "triple", batch_size, avss_value_processor_chunk_size=3, ) self.field = GF(Subgroup.BLS12_381) def _get_input_batch(self): inputs = [] for _ in range(self.batch_size): a, b = self.field.random(), self.field.random() ab = a * b inputs += [a.value, b.value, ab.value] return inputs async def _extract(self): while True: async for batch in self._get_output_batch(3): triple_shares_int = await asyncio.gather(*batch) # Number of nodes which have contributed values to this batch n = len(triple_shares_int) assert n % 3 == 0 for i in range(0, n, 3): a, b, ab = triple_shares_int[i : i + 3] self.output_queue.put_nowait((a, b, ab))
async def test_mimc_jubjub_pkc(test_runner): field = GF(Subgroup.BLS12_381) plaintext = [field.random().value] seed = field.random().value async def _prog(context): # Key Generation priv_key, pub_key = await key_generation(context) # Encryption & Decryption cipher = mimc_encrypt(pub_key, plaintext, seed) decrypted_value = await mimc_decrypt(context, priv_key, cipher) decrypted = await asyncio.gather(*[d.open() for d in decrypted_value]) assert decrypted == plaintext await test_runner(_prog, n, t, PREPROCESSING, k, STANDARD_ARITHMETIC_MIXINS)
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
class RandomGenerator(PreProcessingBase): def __init__(self, n, t, my_id, send, recv, batch_size=10): super(RandomGenerator, self).__init__(n, t, my_id, send, recv, "rand", batch_size) self.field = GF(Subgroup.BLS12_381) def _get_input_batch(self): return [self.field.random().value for _ in range(self.batch_size)] async def _extract(self): while True: async for batch in self._get_output_batch(): random_shares_int = await asyncio.gather(*batch) output_shares_int = refine_randoms(self.n, self.t, self.field, random_shares_int) for value in output_shares_int: self.output_queue.put_nowait(self.field(value))
async def test_hbavss_light_gf(test_router): t = 2 n = 3 * t + 1 g, h, pks, sks = get_avss_params(n, t) sends, recvs, _ = test_router(n) crs = [g, h] field = GF(Subgroup.BLS12_381) value = field.random() avss_tasks = [None] * n hbavss_list = [None] * n dealer_id = randint(0, n - 1) with ExitStack() as stack: for i in range(n): hbavss = HbAvssLight(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, value=value)) else: avss_tasks[i] = asyncio.create_task( hbavss.avss(0, dealer_id=dealer_id)) avss_tasks[i].add_done_callback(print_exception_callback) # shares = await asyncio.gather(*avss_tasks) outputs = await asyncio.gather( *[hbavss_list[i].output_queue.get() for i in range(n)]) for task in avss_tasks: task.cancel() shares = [] for item in outputs: shares.append(item[2]) assert polynomials_over(field).interpolate_at(zip(range(1, n + 1), shares)) == value
def test_benchmark_gao_robust_decode(benchmark, t, galois_field): n = 3 * t + 1 galois_field = GF(Subgroup.BLS12_381) point = EvalPoint(galois_field, n) dec = GaoRobustDecoder(t, point) parties = [_ for _ in range(n)] poly = polynomials_over(galois_field) truepoly = poly.random(degree=t) faults = [] while len(faults) < t: r = randint(0, n - 1) if r not in faults: faults.append(r) shares_with_faults = [] for i in parties: if i in faults: shares_with_faults.append(int(galois_field.random())) else: shares_with_faults.append(int(truepoly(i + 1))) benchmark(dec.robust_decode, parties, shares_with_faults)