async def test_commonsubset(test_router):
    n, f, seed = 4, 1, None
    # Generate keys
    sid = "sidA"
    pk, sks = dealer(n, f + 1, seed=seed)
    rnd = random.Random(seed)
    # print('SEED:', seed)
    router_seed = rnd.random()
    sends, recvs, bcasts = test_router(n, seed=router_seed)

    inputs = [None] * n
    threads = [None] * n
    for i in range(n):
        inputs[i] = asyncio.Queue(1)

        threads[i] = make_commonsubset(sid, i, n, f, pk, sks[i], inputs[i].get,
                                       sends[i], recvs[i], bcasts[i])

    await asyncio.gather(
        *[inputs[i].put("<[ACS Input %d]>" % i) for i in range(n)])
    results = await asyncio.gather(*threads)
    acs, recv_task_lists, work_task_lists = zip(*results)
    outs = await asyncio.gather(*acs)
    for work_task_list in work_task_lists:
        await asyncio.gather(*work_task_list)
    for recv_task_list in recv_task_lists:
        for task in recv_task_list:
            task.cancel()
    assert len(set(outs)) == 1
Esempio n. 2
0
    def __enter__(self):
        n, t, my_id = self.n, self.t, self.my_id
        send, recv = self.get_send_recv(f"{self.tag}-AVSS")
        g, h, pks, sk = get_avss_params(n, t, my_id)
        crs = [g, h]
        self.avss_instance = HbAvssLight(pks, sk, crs, n, t, my_id, send, recv)
        self.avss_instance.__enter__()
        self.tasks.append(asyncio.create_task(self._runner()))

        send, recv = self.get_send_recv(f"{self.tag}-AVSS_VALUE_PROCESSOR")
        pk, sks = dealer(n, t + 1, seed=17)
        self.avss_value_processor = AvssValueProcessor(
            pk,
            sks[my_id],
            n,
            t,
            my_id,
            send,
            recv,
            self.avss_instance.output_queue.get,
            self.avss_value_processor_chunk_size,
        )
        self.avss_value_processor.__enter__()
        self.tasks.append(asyncio.create_task(self._extract()))
        return self
Esempio n. 3
0
async def _make_coins(test_router, sid, n, f, seed):
    # Generate keys
    pk, sks = dealer(n, f + 1)
    _, recvs, sends = test_router(n, seed=seed)
    result = await gather(
        *[shared_coin(sid, i, n, f, pk, sks[i], sends[i], recvs[i]) for i in range(n)]
    )
    return zip(*result)
Esempio n. 4
0
async def test_when_redundant_signature_share_is_received():
    n, f, seed = 4, 1, None
    pk, sks = dealer(n, f + 1)
    sid = "sidA"
    sends, recvs = byzantine_router(n, seed=seed, node=2, sig_redundant=True)
    result = await asyncio.gather(*[
        shared_coin(sid, i, n, f, pk, sks[i], sends[i], recvs[i])
        for i in range(n)
    ])
    coins, recv_tasks = zip(*result)

    for i in range(10):
        assert len(set(await asyncio.gather(*[c(i) for c in coins]))) == 1
    for task in recv_tasks:
        task.cancel()
Esempio n. 5
0
async def test_commoncoin(test_router):
    n, f, seed = 4, 1, None
    # Generate keys
    pk, sks = dealer(n, f + 1)
    sid = "sidA"
    # Test everything when runs are OK
    _, recvs, sends = test_router(n, seed=seed)
    result = await asyncio.gather(*[
        shared_coin(sid, i, n, f, pk, sks[i], sends[i], recvs[i])
        for i in range(n)
    ])
    coins, recv_tasks = zip(*result)

    for i in range(10):
        assert len(set(await asyncio.gather(*[c(i) for c in coins]))) == 1
    for task in recv_tasks:
        task.cancel()
Esempio n. 6
0
def test_boldyreva():
    global PK, SKs
    PK, SKs = dealer(players=16, k=5)

    global sigs, h
    sigs = {}
    h = PK.hash_message("hi")
    h.initPP()

    for sk in SKs:
        sigs[sk.i] = sk.sign(h)

    ss = list(range(PK.l))
    for _ in range(10):
        random.shuffle(ss)
        s = set(ss[: PK.k])
        sig = PK.combine_shares(dict((s, sigs[s]) for s in s))
        assert PK.verify_signature(sig, h)
Esempio n. 7
0
async def test_avss_value_processor_with_diff_inputs(test_router):
    n, t = 4, 1
    sends, recvs, _ = test_router(n)

    node_inputs = [
        [(0, 0, "00"), (1, 0, "10"), (2, 0, "20")],
        [(0, 0, "01")],
        [(0, 0, "02"), (2, 0, "22"), (3, 0, "32")],
        [(3, 0, "33")],
    ]

    get_tasks = [None] * n
    pk, sks = dealer(n, t + 1)
    avss_value_procs = [None] * n
    input_qs = [None] * n
    with ExitStack() as stack:
        for i in range(n):
            input_qs[i] = asyncio.Queue()
            for node_input in node_inputs[i]:
                input_qs[i].put_nowait(node_input)

            avss_value_procs[i] = AvssValueProcessor(
                pk, sks[i], n, t, i, sends[i], recvs[i], input_qs[i].get
            )
            stack.enter_context(avss_value_procs[i])
            get_tasks[i] = asyncio.create_task(avss_value_procs[i].get())

        futures = await asyncio.gather(*get_tasks)
        for i, future in enumerate(futures):
            assert type(future) is asyncio.Future
            if i == 3:
                # node 3 does not receive the value dealt from node 0
                assert not future.done()
            else:
                # all other nodes have received the value dealt from node 0
                assert (await future) == f"0{i}"

        # this is based on node_inputs
        inputs = [[1, 1, 1, 0], [1, 0, 0, 0], [1, 0, 1, 1], [0, 0, 0, 1]]
        # this is based on the fact that only values dealt by 0 and 2 have been agreed
        outputs = [[1, 0, 1, 1], [1, 0, 1, 1], [1, 0, 1, 1], [1, 0, 1, 1]]

        for j, proc in enumerate(avss_value_procs):
            assert [len(proc.inputs_per_dealer[i]) for i in range(n)] == inputs[j]
            assert [len(proc.outputs_per_dealer[i]) for i in range(n)] == outputs[j]
            # 1 value was already retrieved, two values and 1 batch delimiter expected
            assert proc.output_queue.qsize() == 3
            # The values from 0, 2 and 3 have been added to the queue so their
            # next indices should be updated
            assert proc.next_idx_to_return_per_dealer == [1, 0, 1, 1]

        for i in range(n):
            if i in [0, 2]:
                # only nodes 0 and 2 have received the value dealt by 2
                # executing this sequentially also ensurs that ACS is not run again
                # since this value is already available
                assert (await (await avss_value_procs[i].get())) == f"2{i}"
            else:
                # nodes 1 and 3 have not received the value dealt by 2
                assert not (await avss_value_procs[i].get()).done()

        for j, proc in enumerate(avss_value_procs):
            assert [len(proc.inputs_per_dealer[i]) for i in range(n)] == inputs[j]
            assert [len(proc.outputs_per_dealer[i]) for i in range(n)] == outputs[j]
            # values from node 0 and 1 have been requested
            assert proc.next_idx_to_return_per_dealer == [1, 0, 1, 1]