123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- #include "mpcops.hpp"
- #include "bitutils.hpp"
- void mpc_mul(MPCTIO &tio, yield_t &yield,
- RegAS &z, RegAS x, RegAS y,
- nbits_t nbits)
- {
- const value_t mask = MASKBITS(nbits);
-
- mpc_cross(tio, yield, z, x, y, nbits);
-
- z.ashare = (z.ashare + x.ashare * y.ashare) & mask;
- }
- void mpc_cross(MPCTIO &tio, yield_t &yield,
- RegAS &z, RegAS x, RegAS y,
- nbits_t nbits)
- {
- const value_t mask = MASKBITS(nbits);
- size_t nbytes = BITBYTES(nbits);
- auto [X, Y, Z] = tio.triple();
-
- value_t blind_x = (x.ashare + X) & mask;
- value_t blind_y = (y.ashare + Y) & mask;
- tio.queue_peer(&blind_x, nbytes);
- tio.queue_peer(&blind_y, nbytes);
- yield();
-
- value_t peer_blind_x, peer_blind_y;
- tio.recv_peer(&peer_blind_x, nbytes);
- tio.recv_peer(&peer_blind_y, nbytes);
- z.ashare = ((x.ashare * peer_blind_y) - (Y * peer_blind_x) + Z) & mask;
- }
- void mpc_valuemul(MPCTIO &tio, yield_t &yield,
- RegAS &z, value_t x,
- nbits_t nbits)
- {
- const value_t mask = MASKBITS(nbits);
- size_t nbytes = BITBYTES(nbits);
- auto [X, Z] = tio.halftriple();
-
- value_t blind_x = (x + X) & mask;
- tio.queue_peer(&blind_x, nbytes);
- yield();
-
- value_t peer_blind_y;
- tio.recv_peer(&peer_blind_y, nbytes);
- if (tio.player() == 0) {
- z.ashare = ((x * peer_blind_y) + Z) & mask;
- } else if (tio.player() == 1) {
- z.ashare = ((-X * peer_blind_y) + Z) & mask;
- }
- }
- void mpc_flagmult(MPCTIO &tio, yield_t &yield,
- RegAS &z, RegBS f, RegAS y,
- nbits_t nbits)
- {
- const value_t mask = MASKBITS(nbits);
-
- value_t bs_fval = value_t(f.bshare);
- RegAS fval;
- fval.ashare = bs_fval;
- mpc_cross(tio, yield, z, y*(1-2*bs_fval), fval, nbits);
-
- z.ashare = (z.ashare + bs_fval*y.ashare) & mask;
-
-
-
-
- }
- void mpc_select(MPCTIO &tio, yield_t &yield,
- RegAS &z, RegBS f, RegAS x, RegAS y,
- nbits_t nbits)
- {
- const value_t mask = MASKBITS(nbits);
-
- mpc_flagmult(tio, yield, z, f, y-x, nbits);
- z.ashare = (z.ashare + x.ashare) & mask;
- }
- void mpc_oswap(MPCTIO &tio, yield_t &yield,
- RegAS &x, RegAS &y, RegBS f,
- nbits_t nbits)
- {
- const value_t mask = MASKBITS(nbits);
-
-
- RegAS s;
- mpc_flagmult(tio, yield, s, f, y-x, nbits);
- x.ashare = (x.ashare + s.ashare) & mask;
- y.ashare = (y.ashare - s.ashare) & mask;
- }
- void mpc_xs_to_as(MPCTIO &tio, yield_t &yield,
- RegAS &as_x, RegXS xs_x,
- nbits_t nbits)
- {
- const value_t mask = MASKBITS(nbits);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- RegAS as_bitand[nbits-1];
- std::vector<coro_t> coroutines;
- for (nbits_t i=0; i<nbits-1; ++i) {
- coroutines.emplace_back(
- [&](yield_t &yield) {
- mpc_valuemul(tio, yield, as_bitand[i], (xs_x.xshare>>i)&1, nbits);
- });
- }
- run_coroutines(yield, coroutines);
- value_t as_C = 0;
- for (nbits_t i=0; i<nbits-1; ++i) {
- as_C += (as_bitand[i].ashare<<(i+1));
- }
- as_x.ashare = (xs_x.xshare - as_C) & mask;
- }
- void mpc_reconstruct_choice(MPCTIO &tio, yield_t &yield,
- DPFnode &z, RegBS f, DPFnode x, DPFnode y)
- {
-
- DPFnode fext = if128_mask[f.bshare];
-
- auto [X, Y, Z] = tio.selecttriple();
- bit_t blind_f = f.bshare ^ X;
- DPFnode d = x ^ y;
- DPFnode blind_d = d ^ Y;
-
- tio.queue_peer(&blind_f, sizeof(blind_f));
- tio.queue_peer(&blind_d, sizeof(blind_d));
- yield();
-
- bit_t peer_blind_f = 0;
- DPFnode peer_blind_d;
- tio.recv_peer(&peer_blind_f, sizeof(peer_blind_f));
- tio.recv_peer(&peer_blind_d, sizeof(peer_blind_d));
-
- DPFnode peer_blind_fext = if128_mask[peer_blind_f];
- DPFnode zshare =
- (fext & peer_blind_d) ^ (Y & peer_blind_fext) ^
- (fext & d) ^ (Z ^ x);
-
- tio.queue_peer(&zshare, sizeof(zshare));
- yield();
- DPFnode peer_zshare;
- tio.recv_peer(&peer_zshare, sizeof(peer_zshare));
- z = zshare ^ peer_zshare;
- }
|