Exemplo n.º 1
0
async def main():
    Prio_init()
    n_clients = 4
    n_data = 133
    server_secret = prio.PRGSeed()
    skA, pkA = prio.create_keypair()
    skB, pkB = prio.create_keypair()

    cfg = prio.Config(n_data, pkA, pkB, b"test_batch")
    sA = prio.Server(cfg, prio.PRIO_SERVER_A, skA, server_secret)
    sB = prio.Server(cfg, prio.PRIO_SERVER_B, skB, server_secret)

    data_items = bytes([(i % 3 == 1) or (i % 5 == 1) for i in range(n_data)])

    logger.info("Starting asyncio prio pipeline.")
    client = prio.Client(cfg)
    queue_a = asyncio.Queue()
    queue_b = asyncio.Queue()

    await client_produce(client, data_items, queue_a, queue_b, n_clients)

    consumers = asyncio.ensure_future(
        asyncio.gather(server_consume(sA, queue_a, queue_b),
                       server_consume(sB, queue_b, queue_a)))

    await asyncio.gather(queue_a.join(), queue_b.join())

    t_a = sA.total_shares()
    t_b = sB.total_shares()

    output = prio.total_share_final(cfg, t_a, t_b)

    expected = [item * n_clients for item in list(data_items)]
    assert list(output) == expected

    consumers.cancel()
    Prio_clear()
    logger.info("Done!")
Exemplo n.º 2
0
async def run_server(pubkey, pvtkey, pubkey_other, server_id, n_fields,
                     batch_id, shared_seed):
    connection = await aio_pika.connect_robust(
        "amqp://*****:*****@rabbitmq:5672/")
    channel = await connection.channel()
    queue = await channel.declare_queue(f"prio.{server_id}")

    pk = prio.PublicKey().import_hex(pubkey)
    sk = prio.PrivateKey().import_hex(pvtkey, pubkey)
    pk_other = prio.PublicKey().import_hex(pubkey_other)

    seed = prio.PRGSeed()
    seed.instance = shared_seed

    config = prio.Config(n_fields, pk, pk_other, batch_id)
    server = prio.Server(config, server_id, sk, seed)

    cache = {}

    async for message in queue:
        with message.process():
            pid = message.message_id
            v, p1, p2 = cache.get(pid, (None, None, None))

            def log(line):
                logger.info("Message {}: {}".format(pid, line))

            ptype = message.type
            routing_key = "prio.{}".format(get_other_server(server_id))

            if (ptype == "verify1" and not p1) or (ptype == "verify2"
                                                   and not p2):
                log("Re-queuing message!")
                await channel.default_exchange.publish(
                    aio_pika.Message(
                        body=message.body,
                        message_id=message.message_id,
                        type=message.type,
                    ),
                    routing_key="prio.{}".format(server_id),
                )
            elif ptype == "data":
                log("Generating verify packet 1")
                v = server.create_verifier(message.body)
                p1 = v.create_verify1()
                await channel.default_exchange.publish(
                    aio_pika.Message(
                        body=pickle.dumps(p1),
                        message_id=message.message_id,
                        type="verify1",
                    ),
                    routing_key=routing_key,
                )
            elif ptype == "verify1":
                log("Generating verify packet 2")
                p2 = v.create_verify2(p1, pickle.loads(message.body))
                await channel.default_exchange.publish(
                    aio_pika.Message(
                        body=pickle.dumps(p2),
                        message_id=message.message_id,
                        type="verify2",
                    ),
                    routing_key=routing_key,
                )
            elif ptype == "verify2":
                if v.is_valid(p2, pickle.loads(message.body)):
                    log("Aggregate data")
                    server.aggregate(v)
                else:
                    log("Invalid data")
                del cache[pid]
            else:
                log("Bad message type {}".format(ptype))

            cache[pid] = (v, p1, p2)
Exemplo n.º 3
0
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from prio_processor.prio import wrapper as prio
from prio import PrioContext
import sys

with PrioContext():
    skA, pkA = prio.create_keypair()
    skB, pkB = prio.create_keypair()

    n_data = 133
    batch_id = b"test_batch"
    cfg = prio.Config(n_data, pkA, pkB, batch_id)

    server_secret = prio.PRGSeed()

    sA = prio.Server(cfg, prio.PRIO_SERVER_A, skA, server_secret)
    sB = prio.Server(cfg, prio.PRIO_SERVER_B, skB, server_secret)

    client = prio.Client(cfg)

    data_items = bytes([(i % 3 == 1) or (i % 5 == 1) for i in range(n_data)])
    for_server_a, for_server_b = client.encode(data_items)

    # Setup verification
    vA = sA.create_verifier(for_server_a)
    vB = sB.create_verifier(for_server_b)

    # Produce a packet1 and send to the other party
Exemplo n.º 4
0
 def size(n):
     cfg = prio.Config(n, pubkey, pubkey, b"test")
     a, b = prio.Client(cfg).encode(bytes([1] * k))
     return [k, len(a), len(b)]
def config(serverA_keypair, serverB_keypair):
    _, pkA = serverA_keypair
    _, pkB = serverB_keypair
    return prio.Config(133, pkA, pkB, b"test_batch")