123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- #include <bsd/stdlib.h> // arc4random_buf
- #include "online.hpp"
- #include "mpcops.hpp"
- #include "rdpf.hpp"
- static void online_test(MPCIO &mpcio, const PRACOptions &opts, char **args)
- {
- nbits_t nbits = VALUE_BITS;
- if (*args) {
- nbits = atoi(*args);
- }
- size_t memsize = 9;
- MPCTIO tio(mpcio, 0);
- bool is_server = (mpcio.player == 2);
- RegAS *A = new RegAS[memsize];
- value_t V;
- RegBS F0, F1;
- RegXS X;
- if (!is_server) {
- A[0].randomize();
- A[1].randomize();
- F0.randomize();
- A[4].randomize();
- F1.randomize();
- A[6].randomize();
- A[7].randomize();
- X.randomize();
- arc4random_buf(&V, sizeof(V));
- printf("A:\n"); for (size_t i=0; i<memsize; ++i) printf("%3lu: %016lX\n", i, A[i].ashare);
- printf("V : %016lX\n", V);
- printf("F0 : %01X\n", F0.bshare);
- printf("F1 : %01X\n", F1.bshare);
- printf("X : %016lX\n", X.xshare);
- }
- std::vector<coro_t> coroutines;
- coroutines.emplace_back(
- [&](yield_t &yield) {
- mpc_mul(tio, yield, A[2], A[0], A[1], nbits);
- });
- coroutines.emplace_back(
- [&](yield_t &yield) {
- mpc_valuemul(tio, yield, A[3], V, nbits);
- });
- coroutines.emplace_back(
- [&](yield_t &yield) {
- mpc_flagmult(tio, yield, A[5], F0, A[4], nbits);
- });
- coroutines.emplace_back(
- [&](yield_t &yield) {
- mpc_oswap(tio, yield, A[6], A[7], F1, nbits);
- });
- coroutines.emplace_back(
- [&](yield_t &yield) {
- mpc_xs_to_as(tio, yield, A[8], X, nbits);
- });
- run_coroutines(tio, coroutines);
- if (!is_server) {
- printf("\n");
- printf("A:\n"); for (size_t i=0; i<memsize; ++i) printf("%3lu: %016lX\n", i, A[i].ashare);
- }
- // Check the answers
- if (mpcio.player == 1) {
- tio.queue_peer(A, memsize*sizeof(RegAS));
- tio.queue_peer(&V, sizeof(V));
- tio.queue_peer(&F0, sizeof(RegBS));
- tio.queue_peer(&F1, sizeof(RegBS));
- tio.queue_peer(&X, sizeof(RegXS));
- tio.send();
- } else if (mpcio.player == 0) {
- RegAS *B = new RegAS[memsize];
- RegBS BF0, BF1;
- RegXS BX;
- value_t BV;
- value_t *S = new value_t[memsize];
- bit_t SF0, SF1;
- value_t SX;
- tio.recv_peer(B, memsize*sizeof(RegAS));
- tio.recv_peer(&BV, sizeof(BV));
- tio.recv_peer(&BF0, sizeof(RegBS));
- tio.recv_peer(&BF1, sizeof(RegBS));
- tio.recv_peer(&BX, sizeof(RegXS));
- for(size_t i=0; i<memsize; ++i) S[i] = A[i].ashare+B[i].ashare;
- SF0 = F0.bshare ^ BF0.bshare;
- SF1 = F1.bshare ^ BF1.bshare;
- SX = X.xshare ^ BX.xshare;
- printf("S:\n"); for (size_t i=0; i<memsize; ++i) printf("%3lu: %016lX\n", i, S[i]);
- printf("SF0: %01X\n", SF0);
- printf("SF1: %01X\n", SF1);
- printf("SX : %016lX\n", SX);
- printf("\n%016lx\n", S[0]*S[1]-S[2]);
- printf("%016lx\n", (V*BV)-S[3]);
- printf("%016lx\n", (SF0*S[4])-S[5]);
- printf("%016lx\n", S[8]-SX);
- delete[] B;
- delete[] S;
- }
- delete[] A;
- }
- static void lamport_test(MPCIO &mpcio, const PRACOptions &opts, char **args)
- {
- // Create a bunch of threads and send a bunch of data to the other
- // peer, and receive their data. If an arg is specified, repeat
- // that many times. The Lamport clock at the end should be just the
- // number of repetitions. Subsequent args are the chunk size and
- // the number of chunks per message
- size_t niters = 1;
- size_t chunksize = 1<<20;
- size_t numchunks = 1;
- if (*args) {
- niters = atoi(*args);
- ++args;
- }
- if (*args) {
- chunksize = atoi(*args);
- ++args;
- }
- if (*args) {
- numchunks = atoi(*args);
- ++args;
- }
- int num_threads = opts.num_threads;
- boost::asio::thread_pool pool(num_threads);
- for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
- boost::asio::post(pool, [&mpcio, thread_num, niters, chunksize, numchunks] {
- MPCTIO tio(mpcio, thread_num);
- char *sendbuf = new char[chunksize];
- char *recvbuf = new char[chunksize*numchunks];
- for (size_t i=0; i<niters; ++i) {
- for (size_t chunk=0; chunk<numchunks; ++chunk) {
- arc4random_buf(sendbuf, chunksize);
- tio.queue_peer(sendbuf, chunksize);
- }
- tio.send();
- tio.recv_peer(recvbuf, chunksize*numchunks);
- }
- delete[] recvbuf;
- delete[] sendbuf;
- });
- }
- pool.join();
- }
- static void rdpf_test(MPCIO &mpcio, const PRACOptions &opts, char **args)
- {
- nbits_t depth=6;
- if (*args) {
- depth = atoi(*args);
- ++args;
- }
- int num_threads = opts.num_threads;
- boost::asio::thread_pool pool(num_threads);
- for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
- boost::asio::post(pool, [&mpcio, thread_num, depth] {
- MPCTIO tio(mpcio, thread_num);
- size_t &op_counter = tio.aes_ops();
- if (mpcio.player == 2) {
- RDPFPair dp = tio.rdpfpair(depth);
- for (int i=0;i<2;++i) {
- const RDPF &dpf = dp.dpf[i];
- for (address_t x=0;x<(address_t(1)<<depth);++x) {
- DPFnode leaf = dpf.leaf(x, op_counter);
- RegBS ub = dpf.unit_bs(leaf);
- RegAS ua = dpf.unit_as(leaf);
- RegXS sx = dpf.scaled_xs(leaf);
- RegAS sa = dpf.scaled_as(leaf);
- printf("%04x %x %016lx %016lx %016lx\n", x,
- ub.bshare, ua.ashare, sx.xshare, sa.ashare);
- }
- printf("\n");
- }
- } else {
- RDPFTriple dt = tio.rdpftriple(depth);
- for (int i=0;i<3;++i) {
- const RDPF &dpf = dt.dpf[i];
- RegXS peer_scaled_xor;
- RegAS peer_scaled_sum;
- if (mpcio.player == 1) {
- tio.iostream_peer() << dpf.scaled_xor << dpf.scaled_sum;
- } else {
- tio.iostream_peer() >> peer_scaled_xor >> peer_scaled_sum;
- peer_scaled_sum += dpf.scaled_sum;
- peer_scaled_xor ^= dpf.scaled_xor;
- }
- for (address_t x=0;x<(address_t(1)<<depth);++x) {
- DPFnode leaf = dpf.leaf(x, op_counter);
- RegBS ub = dpf.unit_bs(leaf);
- RegAS ua = dpf.unit_as(leaf);
- RegXS sx = dpf.scaled_xs(leaf);
- RegAS sa = dpf.scaled_as(leaf);
- printf("%04x %x %016lx %016lx %016lx\n", x,
- ub.bshare, ua.ashare, sx.xshare, sa.ashare);
- if (mpcio.player == 1) {
- tio.iostream_peer() << ub << ua << sx << sa;
- } else {
- RegBS peer_ub;
- RegAS peer_ua;
- RegXS peer_sx;
- RegAS peer_sa;
- tio.iostream_peer() >> peer_ub >> peer_ua >>
- peer_sx >> peer_sa;
- ub ^= peer_ub;
- ua += peer_ua;
- sx ^= peer_sx;
- sa += peer_sa;
- if (ub.bshare || ua.ashare || sx.xshare || sa.ashare) {
- printf("**** %x %016lx %016lx %016lx\n",
- ub.bshare, ua.ashare, sx.xshare, sa.ashare);
- printf("SCALE %016lx %016lx\n",
- peer_scaled_xor.xshare, peer_scaled_sum.ashare);
- }
- }
- }
- printf("\n");
- }
- }
- tio.send();
- });
- }
- pool.join();
- }
- static void rdpf_timing(MPCIO &mpcio, const PRACOptions &opts, char **args)
- {
- nbits_t depth=6;
- if (*args) {
- depth = atoi(*args);
- ++args;
- }
- int num_threads = opts.num_threads;
- boost::asio::thread_pool pool(num_threads);
- for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
- boost::asio::post(pool, [&mpcio, thread_num, depth] {
- MPCTIO tio(mpcio, thread_num);
- size_t &op_counter = tio.aes_ops();
- if (mpcio.player == 2) {
- RDPFPair dp = tio.rdpfpair(depth);
- for (int i=0;i<2;++i) {
- RDPF &dpf = dp.dpf[i];
- dpf.expand(op_counter);
- RegXS scaled_xor;
- scaled_xor.xshare = 0;
- for (address_t x=0;x<(address_t(1)<<depth);++x) {
- DPFnode leaf = dpf.leaf(x, op_counter);
- RegXS sx = dpf.scaled_xs(leaf);
- scaled_xor ^= sx;
- }
- printf("%016lx\n%016lx\n", scaled_xor.xshare,
- dpf.scaled_xor.xshare);
- printf("\n");
- }
- } else {
- RDPFTriple dt = tio.rdpftriple(depth);
- for (int i=0;i<3;++i) {
- RDPF &dpf = dt.dpf[i];
- dpf.expand(op_counter);
- RegXS scaled_xor;
- scaled_xor.xshare = 0;
- for (address_t x=0;x<(address_t(1)<<depth);++x) {
- DPFnode leaf = dpf.leaf(x, op_counter);
- RegXS sx = dpf.scaled_xs(leaf);
- scaled_xor ^= sx;
- }
- printf("%016lx\n%016lx\n", scaled_xor.xshare,
- dpf.scaled_xor.xshare);
- printf("\n");
- }
- }
- tio.send();
- });
- }
- pool.join();
- }
- static void rdpfeval_timing(MPCIO &mpcio, const PRACOptions &opts, char **args)
- {
- nbits_t depth=6;
- address_t start=0;
- if (*args) {
- depth = atoi(*args);
- ++args;
- }
- if (*args) {
- start = atoi(*args);
- ++args;
- }
- int num_threads = opts.num_threads;
- boost::asio::thread_pool pool(num_threads);
- for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
- boost::asio::post(pool, [&mpcio, thread_num, depth, start] {
- MPCTIO tio(mpcio, thread_num);
- size_t &op_counter = tio.aes_ops();
- if (mpcio.player == 2) {
- RDPFPair dp = tio.rdpfpair(depth);
- for (int i=0;i<2;++i) {
- RDPF &dpf = dp.dpf[i];
- RegXS scaled_xor;
- scaled_xor.xshare = 0;
- auto ev = StreamEval(dpf, start, op_counter, false);
- for (address_t x=0;x<(address_t(1)<<depth);++x) {
- DPFnode leaf = ev.next();
- RegXS sx = dpf.scaled_xs(leaf);
- scaled_xor ^= sx;
- }
- printf("%016lx\n%016lx\n", scaled_xor.xshare,
- dpf.scaled_xor.xshare);
- printf("\n");
- }
- } else {
- RDPFTriple dt = tio.rdpftriple(depth);
- for (int i=0;i<3;++i) {
- RDPF &dpf = dt.dpf[i];
- RegXS scaled_xor;
- scaled_xor.xshare = 0;
- auto ev = StreamEval(dpf, start, op_counter, false);
- for (address_t x=0;x<(address_t(1)<<depth);++x) {
- DPFnode leaf = ev.next();
- RegXS sx = dpf.scaled_xs(leaf);
- scaled_xor ^= sx;
- }
- printf("%016lx\n%016lx\n", scaled_xor.xshare,
- dpf.scaled_xor.xshare);
- printf("\n");
- }
- }
- tio.send();
- });
- }
- pool.join();
- }
- static void tupleeval_timing(MPCIO &mpcio, const PRACOptions &opts, char **args)
- {
- nbits_t depth=6;
- address_t start=0;
- if (*args) {
- depth = atoi(*args);
- ++args;
- }
- if (*args) {
- start = atoi(*args);
- ++args;
- }
- int num_threads = opts.num_threads;
- boost::asio::thread_pool pool(num_threads);
- for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
- boost::asio::post(pool, [&mpcio, thread_num, depth, start] {
- MPCTIO tio(mpcio, thread_num);
- size_t &op_counter = tio.aes_ops();
- if (mpcio.player == 2) {
- RDPFPair dp = tio.rdpfpair(depth);
- RegXS scaled_xor0, scaled_xor1;
- scaled_xor0.xshare = 0;
- scaled_xor1.xshare = 0;
- auto ev = StreamEval(dp, start, op_counter, false);
- for (address_t x=0;x<(address_t(1)<<depth);++x) {
- auto [L0, L1] = ev.next();
- RegXS sx0 = dp.dpf[0].scaled_xs(L0);
- RegXS sx1 = dp.dpf[1].scaled_xs(L1);
- scaled_xor0 ^= sx0;
- scaled_xor1 ^= sx1;
- }
- printf("%016lx\n%016lx\n", scaled_xor0.xshare,
- dp.dpf[0].scaled_xor.xshare);
- printf("\n");
- printf("%016lx\n%016lx\n", scaled_xor1.xshare,
- dp.dpf[1].scaled_xor.xshare);
- printf("\n");
- } else {
- RDPFTriple dt = tio.rdpftriple(depth);
- RegXS scaled_xor0, scaled_xor1, scaled_xor2;
- scaled_xor0.xshare = 0;
- scaled_xor1.xshare = 0;
- scaled_xor2.xshare = 0;
- auto ev = StreamEval(dt, start, op_counter, false);
- for (address_t x=0;x<(address_t(1)<<depth);++x) {
- auto [L0, L1, L2] = ev.next();
- RegXS sx0 = dt.dpf[0].scaled_xs(L0);
- RegXS sx1 = dt.dpf[1].scaled_xs(L1);
- RegXS sx2 = dt.dpf[2].scaled_xs(L2);
- scaled_xor0 ^= sx0;
- scaled_xor1 ^= sx1;
- scaled_xor2 ^= sx2;
- }
- printf("%016lx\n%016lx\n", scaled_xor0.xshare,
- dt.dpf[0].scaled_xor.xshare);
- printf("\n");
- printf("%016lx\n%016lx\n", scaled_xor1.xshare,
- dt.dpf[1].scaled_xor.xshare);
- printf("\n");
- printf("%016lx\n%016lx\n", scaled_xor2.xshare,
- dt.dpf[2].scaled_xor.xshare);
- printf("\n");
- }
- tio.send();
- });
- }
- pool.join();
- }
- void online_main(MPCIO &mpcio, const PRACOptions &opts, char **args)
- {
- if (!*args) {
- std::cerr << "Mode is required as the first argument when not preprocessing.\n";
- return;
- } else if (!strcmp(*args, "test")) {
- ++args;
- online_test(mpcio, opts, args);
- } else if (!strcmp(*args, "lamporttest")) {
- ++args;
- lamport_test(mpcio, opts, args);
- } else if (!strcmp(*args, "rdpftest")) {
- ++args;
- rdpf_test(mpcio, opts, args);
- } else if (!strcmp(*args, "rdpftime")) {
- ++args;
- rdpf_timing(mpcio, opts, args);
- } else if (!strcmp(*args, "evaltime")) {
- ++args;
- rdpfeval_timing(mpcio, opts, args);
- } else if (!strcmp(*args, "tupletime")) {
- ++args;
- tupleeval_timing(mpcio, opts, args);
- } else {
- std::cerr << "Unknown mode " << *args << "\n";
- }
- }
|