/* * BrainPrep.cpp * */ #include "BrainPrep.h" #include "Processor/Processor.h" #include "Protocols/MaliciousRepMC.h" #include "Tools/Subroutines.h" #include "Math/gfp.h" template class ZProtocol; template class Zint : public SignedZ2 { typedef SignedZ2 super; public: static string type_string() { return "Zint" + to_string(L); } Zint() { } template Zint(const T& other) : super(other) { } void randomize(PRNG& G, int n = -1) { (void) n; *this = G.get>(); } }; template class ZShare : public Rep3Share> { public: typedef ZProtocol Protocol; typedef ReplicatedMC MAC_Check; ZShare() { } template ZShare(const FixedVec& other) { FixedVec::operator=(other); } }; template class ZProtocol : public Replicated>> { typedef Rep3Share> T; vector random; SeededPRNG G; ReplicatedInput>> input; public: ZProtocol(Player& P) : Replicated(P), input(0, this->P) { } T get_random() { if (random.empty()) { int buffer_size = OnlineOptions::singleton.batch_size; input.reset_all(this->P); for (int i = 0; i < buffer_size; i++) { typename U::clear tmp; tmp.randomize(G); input.add_mine(tmp); } for (int i = 0; i < this->P.num_players(); i++) input.add_other(i); input.exchange(); for (int i = 0; i < buffer_size; i++) { random.push_back({}); for (int j = 0; j < 3; j++) random.back() += input.finalize(j); } } auto res = random.back(); random.pop_back(); return res; } }; template void BrainPrep::basic_setup(Player&) { gfp2::init_default(DIV_CEIL(T::Z_BITS + 3, 8) * 8); } template void BrainPrep::buffer_triples() { if(gfp2::get_ZpD().pr_bit_length <= ZProtocol::share_type::clear::N_BITS) throw runtime_error( to_string(gfp2::get_ZpD().pr_bit_length) + "-bit prime too short for " + to_string(ZProtocol::share_type::clear::N_BITS) + "-bit integer computation"); typedef Rep3Share pShare; auto buffer_size = BaseMachine::batch_size(DATA_TRIPLE); Player& P = this->protocol->P; vector, 3>> triples; vector, 3>> check_triples; DataPositions usage; HashMaliciousRepMC MC; vector> masked, checks; vector opened; ZProtocol Z_protocol(P); Replicated p_protocol(P); generate_triples(triples, buffer_size, &Z_protocol); check_triples.resize(buffer_size); p_protocol.init_mul(); for (int i = 0; i < buffer_size; i++) { pShare a = p_protocol.get_random(); pShare b = triples[i][1]; p_protocol.prepare_mul(a, b); check_triples[i][0] = a; check_triples[i][1] = b; } p_protocol.exchange(); auto t = Create_Random(P); for (int i = 0; i < buffer_size; i++) { check_triples[i][2] = p_protocol.finalize_mul(); pShare a = triples[i][0]; auto& f = check_triples[i][0]; masked.push_back(a * t - f); } MC.POpen(opened, masked, P); for (int i = 0; i < buffer_size; i++) { auto& b = check_triples[i][1]; pShare c = triples[i][2]; auto& h = check_triples[i][2]; auto& rho = opened[i]; checks.push_back(t * c - h - rho * b); } MC.CheckFor(0, checks, P); MC.Check(P); for (auto& x : triples) this->triples.push_back({{x[0], x[1], x[2]}}); } template void BrainPrep::buffer_inputs(int player) { this->buffer_inputs_as_usual(player, this->proc); }