#ifndef _Processor #define _Processor /* This is a representation of a processing element */ #include "Math/Integer.h" #include "Tools/Exceptions.h" #include "Networking/Player.h" #include "Data_Files.h" #include "Input.h" #include "PrivateOutput.h" #include "ExternalClients.h" #include "Binary_File_IO.h" #include "Instruction.h" #include "ProcessorBase.h" #include "OnlineOptions.h" #include "Tools/SwitchableOutput.h" #include "Tools/CheckVector.h" #include "GC/Processor.h" #include "GC/ShareThread.h" #include "Protocols/SecureShuffle.h" class Program; template class SubProcessor { CheckVector C; CheckVector S; DataPositions bit_usage; typename T::Protocol::Shuffler shuffler; void resize(size_t size) { C.resize(size); S.resize(size); } void matmulsm_prep(int ii, int j, const MemoryPart& source, const vector& dim, size_t a, size_t b); void matmulsm_finalize(int i, int j, const vector& dim, typename vector::iterator C); template friend class Processor; template friend class SPDZ; template friend class ProtocolBase; template friend class Beaver; typedef typename T::bit_type::part_type BT; typedef typename T::Protocol::Shuffler::store_type ShuffleStore; public: ArithmeticProcessor* Proc; typename T::MAC_Check& MC; Player& P; Preprocessing& DataF; typename T::Protocol protocol; typename T::Input input; typename BT::LivePrep bit_prep; vector personal_bit_preps; SubProcessor(ArithmeticProcessor& Proc, typename T::MAC_Check& MC, Preprocessing& DataF, Player& P); SubProcessor(typename T::MAC_Check& MC, Preprocessing& DataF, Player& P, ArithmeticProcessor* Proc = 0); ~SubProcessor(); void check(); // Access to PO (via calls to POpen start/stop) void POpen(const Instruction& inst); void muls(const vector& reg); void mulrs(const vector& reg); void dotprods(const vector& reg, int size); void matmuls(const vector& source, const Instruction& instruction); void matmulsm(const MemoryPart& source, const Instruction& instruction, size_t a, size_t b); void conv2ds(const Instruction& instruction); void secure_shuffle(const Instruction& instruction); size_t generate_secure_shuffle(const Instruction& instruction, ShuffleStore& shuffle_store); void apply_shuffle(const Instruction& instruction, int handle, ShuffleStore& shuffle_store); void inverse_permutation(const Instruction& instruction); void input_personal(const vector& args); void send_personal(const vector& args); void private_output(const vector& args); CheckVector& get_S() { return S; } CheckVector& get_C() { return C; } T& get_S_ref(size_t i) { return S[i]; } typename T::clear& get_C_ref(size_t i) { return C[i]; } void inverse_permutation(const Instruction &instruction, int handle); }; class ArithmeticProcessor : public ProcessorBase { protected: CheckVector Ci; ofstream public_output; ofstream binary_output; public: int thread_num; PRNG secure_prng; PRNG shared_prng; string private_input_filename; string public_input_filename; string binary_input_filename; ifstream private_input; ifstream public_input; ifstream binary_input; int sent, rounds; OnlineOptions opts; SwitchableOutput out; ArithmeticProcessor() : ArithmeticProcessor(OnlineOptions::singleton, BaseMachine::thread_num) { } ArithmeticProcessor(OnlineOptions opts, int thread_num) : thread_num(thread_num), sent(0), rounds(0), opts(opts) {} virtual ~ArithmeticProcessor() { } bool use_stdin() { return thread_num == 0 and opts.interactive; } int get_thread_num() { return thread_num; } long read_Ci(size_t i) const { return Ci[i].get(); } Integer& get_Ci_ref(size_t i) { return Ci[i]; } void write_Ci(size_t i, const long& x) { Ci[i]=x; } CheckVector& get_Ci() { return Ci; } virtual ofstream& get_public_output() { throw not_implemented(); } virtual ofstream& get_binary_output() { throw not_implemented(); } void shuffle(const Instruction& instruction); void bitdecint(const Instruction& instruction); }; template class Processor : public ArithmeticProcessor { typedef typename sint::clear cint; // Data structure used for reading/writing data to/from a socket (i.e. an external party to SPDZ) octetStream socket_stream; // avoid re-computation of expensive division vector inverses2m; public: Data_Files DataF; Player& P; typename sgf2n::MAC_Check& MC2; typename sint::MAC_Check& MCp; Machine& machine; GC::ShareThread share_thread; GC::Processor Procb; SubProcessor Proc2; SubProcessor Procp; unsigned int PC; TempVars temp; ExternalClients& external_clients; Binary_File_IO binary_file_io; CommStats client_stats; Timer& client_timer; void reset(const Program& program,int arg); // Reset the state of the processor string get_filename(const char* basename, bool use_number); Processor(int thread_num,Player& P, typename sgf2n::MAC_Check& MC2,typename sint::MAC_Check& MCp, Machine& machine, const Program& program); ~Processor(); const typename sgf2n::clear& read_C2(size_t i) const { return Proc2.C[i]; } const sgf2n& read_S2(size_t i) const { return Proc2.S[i]; } typename sgf2n::clear& get_C2_ref(size_t i) { return Proc2.C[i]; } sgf2n& get_S2_ref(size_t i) { return Proc2.S[i]; } void write_C2(size_t i,const typename sgf2n::clear& x) { Proc2.C[i]=x; } void write_S2(size_t i,const sgf2n& x) { Proc2.S[i]=x; } const typename sint::clear& read_Cp(size_t i) const { return Procp.C[i]; } const sint & read_Sp(size_t i) const { return Procp.S[i]; } typename sint::clear& get_Cp_ref(size_t i) { return Procp.C[i]; } sint & get_Sp_ref(size_t i) { return Procp.S[i]; } void write_Cp(size_t i,const typename sint::clear& x) { Procp.C[i]=x; } void write_Sp(size_t i,const sint & x) { Procp.S[i]=x; } void check(); void dabit(const Instruction& instruction); void edabit(const Instruction& instruction, bool strict = false); void convcbitvec(const Instruction& instruction); void convcintvec(const Instruction& instruction); void convcbit2s(const Instruction& instruction); void split(const Instruction& instruction); // Access to external client sockets for reading clear/shared data void read_socket_ints(int client_id, const vector& registers, int size); void write_socket(const RegType reg_type, bool send_macs, int socket_id, int message_type, const vector& registers, int size); void read_socket_vector(int client_id, const vector& registers, int size); void read_socket_private(int client_id, const vector& registers, int size, bool send_macs); // Read and write secret numeric data to file (name hardcoded at present) void read_shares_from_file(int start_file_pos, int end_file_pos_register, const vector& data_registers); void write_shares_to_file(long start_pos, const vector& data_registers); cint get_inverse2(unsigned m); void fixinput(const Instruction& instruction); // synchronize in asymmetric protocols long sync(long x) const; ofstream& get_public_output(); ofstream& get_binary_output(); private: template friend class SPDZ; template friend class SubProcessor; }; #endif