Files
MP-SPDZ/Processor/Processor.hpp
2020-04-02 09:09:45 +02:00

585 lines
17 KiB
C++

#ifndef PROCESSOR_PROCESSOR_HPP_
#define PROCESSOR_PROCESSOR_HPP_
#include "Processor/Processor.h"
#include "Processor/Program.h"
#include "Protocols/fake-stuff.h"
#include "GC/square64.h"
#include "Protocols/ReplicatedInput.hpp"
#include "Protocols/ReplicatedPrivateOutput.hpp"
#include "Processor/ProcessorBase.hpp"
#include "GC/Processor.hpp"
#include "GC/ShareThread.hpp"
#include <sodium.h>
#include <string>
template <class T>
SubProcessor<T>::SubProcessor(ArithmeticProcessor& Proc, typename T::MAC_Check& MC,
Preprocessing<T>& DataF, Player& P) :
SubProcessor<T>(MC, DataF, P, &Proc)
{
}
template <class T>
SubProcessor<T>::SubProcessor(typename T::MAC_Check& MC,
Preprocessing<T>& DataF, Player& P, ArithmeticProcessor* Proc) :
Proc(Proc), MC(MC), P(P), DataF(DataF), protocol(P), input(*this, MC),
bit_prep(bit_usage)
{
DataF.set_proc(this);
DataF.set_protocol(protocol);
bit_usage.set_num_players(P.num_players());
personal_bit_preps.resize(P.num_players());
for (int i = 0; i < P.num_players(); i++)
personal_bit_preps[i] = new typename BT::LivePrep(bit_usage, i);
}
template<class T>
SubProcessor<T>::~SubProcessor()
{
for (size_t i = 0; i < personal_bit_preps.size(); i++)
{
auto& x = personal_bit_preps[i];
#ifdef VERBOSE
if (x->data_sent())
cerr << "Sent for personal bit preprocessing threads of player " << i << ": " <<
x->data_sent() * 1e-6 << " MB" << endl;
#endif
delete x;
}
#ifdef VERBOSE
if (bit_prep.data_sent())
cerr << "Sent for global bit preprocessing threads: " <<
bit_prep.data_sent() * 1e-6 << " MB" << endl;
#endif
}
template<class sint, class sgf2n>
Processor<sint, sgf2n>::Processor(int thread_num,Player& P,
typename sgf2n::MAC_Check& MC2,typename sint::MAC_Check& MCp,
Machine<sint, sgf2n>& machine,
const Program& program)
: ArithmeticProcessor(machine.opts, thread_num),DataF(machine, &Procp, &Proc2),P(P),
MC2(MC2),MCp(MCp),machine(machine),
share_thread(machine.get_N(), machine.opts, P, machine.get_bit_mac_key(), DataF.usage),
Procb(machine.bit_memories),
Proc2(*this,MC2,DataF.DataF2,P),Procp(*this,MCp,DataF.DataFp,P),
privateOutput2(Proc2),privateOutputp(Procp),
external_clients(P.my_num(), machine.prep_dir_prefix),
binary_file_io(Binary_File_IO())
{
reset(program,0);
public_input.open(get_filename("Programs/Public-Input/",false).c_str());
private_input_filename = (get_filename(PREP_DIR "Private-Input-",true));
private_input.open(private_input_filename.c_str());
public_output.open(get_filename(PREP_DIR "Public-Output-",true).c_str(), ios_base::out);
private_output.open(get_filename(PREP_DIR "Private-Output-",true).c_str(), ios_base::out);
open_input_file(P.my_num(), thread_num);
secure_prng.ReSeed();
shared_prng.SeedGlobally(P);
out.activate(P.my_num() == 0 or machine.opts.interactive);
}
template<class sint, class sgf2n>
Processor<sint, sgf2n>::~Processor()
{
share_thread.post_run();
#ifdef VERBOSE
if (sent)
cerr << "Opened " << sent << " elements in " << rounds << " rounds" << endl;
#endif
}
template<class sint, class sgf2n>
string Processor<sint, sgf2n>::get_filename(const char* prefix, bool use_number)
{
stringstream filename;
filename << prefix;
if (!use_number)
filename << machine.progname;
if (use_number)
filename << P.my_num();
if (thread_num > 0)
filename << "-" << thread_num;
#ifdef DEBUG_FILES
cerr << "Opening file " << filename.str() << endl;
#endif
return filename.str();
}
template<class sint, class sgf2n>
void Processor<sint, sgf2n>::reset(const Program& program,int arg)
{
reg_max2 = program.num_reg(GF2N);
reg_maxp = program.num_reg(MODP);
reg_maxi = program.num_reg(INT);
Proc2.resize(reg_max2);
Procp.resize(reg_maxp);
Ci.resize(reg_maxi);
this->arg = arg;
Procb.reset(program);
}
template<class sint, class sgf2n>
void Processor<sint, sgf2n>::dabit(const Instruction& instruction)
{
int size = instruction.get_size();
int unit = sint::bit_type::default_length;
for (int i = 0; i < DIV_CEIL(size, unit); i++)
{
Procb.S[instruction.get_r(1) + i] = {};
}
for (int i = 0; i < size; i++)
{
typename sint::bit_type tmp;
Procp.DataF.get_dabit(Procp.get_S_ref(instruction.get_r(0) + i), tmp);
Procb.S[instruction.get_r(1) + i / unit] ^= tmp << (i % unit);
}
}
template<class sint, class sgf2n>
void Processor<sint, sgf2n>::edabit(const Instruction& instruction, bool strict)
{
auto& regs = instruction.get_start();
int size = instruction.get_size();
Procp.DataF.get_edabits(strict, size,
&Procp.get_S_ref(instruction.get_r(0)), Procb.S, regs);
}
template<class sint, class sgf2n>
void Processor<sint, sgf2n>::convcbitvec(const Instruction& instruction)
{
for (size_t i = 0; i < instruction.get_n(); i++)
{
int i1 = i / GC::Clear::N_BITS;
int i2 = i % GC::Clear::N_BITS;
Ci[instruction.get_r(0) + i] = Procb.C[instruction.get_r(1) + i1].get_bit(i2);
}
}
template<class sint, class sgf2n>
void Processor<sint, sgf2n>::convcintvec(const Instruction& instruction)
{
int unit = GC::Clear::N_BITS;
assert(unit == 64);
int n_inputs = instruction.get_size();
int n_bits = instruction.get_start().size();
for (int i = 0; i < DIV_CEIL(n_inputs, unit); i++)
{
for (int j = 0; j < DIV_CEIL(n_bits, unit); j++)
{
square64 square;
int n_rows = min(n_inputs - i * unit, unit);
int n_cols = min(n_bits - j * unit, unit);
for (int k = 0; k < n_rows; k++)
square.rows[k] =
Integer(Procp.C[instruction.get_r(0) + i * unit + k]
>> (j * unit)).get();
square.transpose(n_rows, n_cols);
for (int k = 0; k < n_cols; k++)
Procb.C[instruction.get_start()[k + j * unit] + i] = square.rows[k];
}
}
}
template<class sint, class sgf2n>
void Processor<sint, sgf2n>::convcbit2s(const Instruction& instruction)
{
int unit = GC::Clear::N_BITS;
for (int i = 0; i < DIV_CEIL(instruction.get_n(), unit); i++)
Procb.S[instruction.get_r(0) + i] = sint::bit_type::constant(
Procb.C[instruction.get_r(1) + i], P.my_num(),
share_thread.MC->get_alphai());
}
template<class sint, class sgf2n>
void Processor<sint, sgf2n>::split(const Instruction& instruction)
{
int n = instruction.get_n();
assert (instruction.get_start().size() % n == 0);
int unit = GC::Clear::N_BITS;
assert(unit == 64);
int n_inputs = instruction.get_size();
int n_bits = instruction.get_start().size() / n;
sint::split(Procb.S, instruction.get_start(), n_bits,
&read_Sp(instruction.get_r(0)), n_inputs, P);
}
#include "Networking/sockets.h"
#include "Math/Setup.h"
// Write socket (typically SPDZ engine -> external client), for different register types.
// RegType and SecrecyType determines how registers are read and the socket stream is packed.
// If message_type is > 0, send message_type in bytes 0 - 3, to allow an external client to
// determine the data structure being sent in a message.
template<class sint, class sgf2n>
void Processor<sint, sgf2n>::write_socket(const RegType reg_type, const SecrecyType secrecy_type, const bool send_macs,
int socket_id, int message_type, const vector<int>& registers)
{
int m = registers.size();
socket_stream.reset_write_head();
//First 4 bytes is message_type (unless indicate not needed)
if (message_type != 0) {
socket_stream.store(message_type);
}
for (int i = 0; i < m; i++)
{
if (reg_type == MODP && secrecy_type == SECRET) {
// Send vector of secret shares and optionally macs
if (send_macs)
get_Sp_ref(registers[i]).pack(socket_stream);
else
get_Sp_ref(registers[i]).pack(socket_stream,
sint::get_rec_factor(P.my_num(), P.num_players()));
}
else if (reg_type == MODP && secrecy_type == CLEAR) {
// Send vector of clear public field elements
get_Cp_ref(registers[i]).pack(socket_stream);
}
else if (reg_type == INT && secrecy_type == CLEAR) {
// Send vector of 32-bit clear ints
socket_stream.store((int&)get_Ci_ref(registers[i]));
}
else {
stringstream ss;
ss << "Write socket instruction with unknown reg type " << reg_type <<
" and secrecy type " << secrecy_type << "." << endl;
throw Processor_Error(ss.str());
}
}
try {
socket_stream.Send(external_clients.get_socket(socket_id));
}
catch (bad_value& e) {
cerr << "Send error thrown when writing " << m << " values of type " << reg_type << " to socket id "
<< socket_id << "." << endl;
}
}
// Receive vector of 32-bit clear ints
template<class sint, class sgf2n>
void Processor<sint, sgf2n>::read_socket_ints(int client_id, const vector<int>& registers)
{
int m = registers.size();
socket_stream.reset_write_head();
socket_stream.Receive(external_clients.get_socket(client_id));
for (int i = 0; i < m; i++)
{
int val;
socket_stream.get(val);
write_Ci(registers[i], (long)val);
}
}
// Receive vector of public field elements
template<class sint, class sgf2n>
void Processor<sint, sgf2n>::read_socket_vector(int client_id, const vector<int>& registers)
{
int m = registers.size();
socket_stream.reset_write_head();
socket_stream.Receive(external_clients.get_socket(client_id));
for (int i = 0; i < m; i++)
{
get_Cp_ref(registers[i]).unpack(socket_stream);
}
}
// Receive vector of field element shares over private channel
template<class sint, class sgf2n>
void Processor<sint, sgf2n>::read_socket_private(int client_id, const vector<int>& registers, bool read_macs)
{
int m = registers.size();
socket_stream.reset_write_head();
socket_stream.Receive(external_clients.get_socket(client_id));
for (int i = 0; i < m; i++)
{
get_Sp_ref(registers[i]).unpack(socket_stream, read_macs);
}
}
// Read share data from a file starting at file_pos until registers filled.
// file_pos_register is written with new file position (-1 is eof).
// Tolerent to no file if no shares yet persisted.
template<class sint, class sgf2n>
void Processor<sint, sgf2n>::read_shares_from_file(int start_file_posn, int end_file_pos_register, const vector<int>& data_registers) {
string filename;
filename = "Persistence/Transactions-P" + to_string(P.my_num()) + ".data";
unsigned int size = data_registers.size();
vector< sint > outbuf(size);
int end_file_posn = start_file_posn;
try {
binary_file_io.read_from_file(filename, outbuf, start_file_posn, end_file_posn);
for (unsigned int i = 0; i < size; i++)
{
get_Sp_ref(data_registers[i]) = outbuf[i];
}
write_Ci(end_file_pos_register, (long)end_file_posn);
}
catch (file_missing& e) {
cerr << "Got file missing error, will return -2. " << e.what() << endl;
write_Ci(end_file_pos_register, (long)-2);
}
}
// Append share data in data_registers to end of file. Expects Persistence directory to exist.
template<class sint, class sgf2n>
void Processor<sint, sgf2n>::write_shares_to_file(const vector<int>& data_registers) {
string filename;
filename = "Persistence/Transactions-P" + to_string(P.my_num()) + ".data";
unsigned int size = data_registers.size();
vector< sint > inpbuf (size);
for (unsigned int i = 0; i < size; i++)
{
inpbuf[i] = get_Sp_ref(data_registers[i]);
}
binary_file_io.write_to_file(filename, inpbuf);
}
template <class T>
void SubProcessor<T>::POpen(const vector<int>& reg,const Player& P,int size)
{
assert(reg.size() % 2 == 0);
int sz=reg.size() / 2;
Sh_PO.clear();
Sh_PO.reserve(sz*size);
if (size>1)
{
for (typename vector<int>::const_iterator reg_it=reg.begin() + 1;
reg_it < reg.end(); reg_it += 2)
{
auto begin=S.begin()+*reg_it;
Sh_PO.insert(Sh_PO.end(),begin,begin+size);
}
}
else
{
for (int i=0; i<sz; i++)
{ Sh_PO.push_back(S[reg[2 * i + 1]]); }
}
PO.resize(sz*size);
MC.POpen(PO,Sh_PO,P);
if (size>1)
{
auto PO_it=PO.begin();
for (typename vector<int>::const_iterator reg_it=reg.begin();
reg_it!=reg.end(); reg_it += 2)
{
for (auto C_it=C.begin()+*reg_it;
C_it!=C.begin()+*reg_it+size; C_it++)
{
*C_it=*PO_it;
PO_it++;
}
}
}
else
{
for (unsigned int i = 0; i < reg.size() / 2; i++)
{
C[reg[2 * i]] = PO[i];
}
}
if (Proc != 0)
{
Proc->sent += reg.size() * size;
Proc->rounds++;
}
}
template<class T>
void SubProcessor<T>::muls(const vector<int>& reg, int size)
{
assert(reg.size() % 3 == 0);
int n = reg.size() / 3;
SubProcessor<T>& proc = *this;
protocol.init_mul(&proc);
for (int i = 0; i < n; i++)
for (int j = 0; j < size; j++)
{
auto& x = proc.S[reg[3 * i + 1] + j];
auto& y = proc.S[reg[3 * i + 2] + j];
protocol.prepare_mul(x, y);
}
protocol.exchange();
for (int i = 0; i < n; i++)
for (int j = 0; j < size; j++)
{
proc.S[reg[3 * i] + j] = protocol.finalize_mul();
}
protocol.counter += n * size;
}
template<class T>
void SubProcessor<T>::mulrs(const vector<int>& reg)
{
assert(reg.size() % 4 == 0);
int n = reg.size() / 4;
SubProcessor<T>& proc = *this;
protocol.init_mul(&proc);
for (int i = 0; i < n; i++)
for (int j = 0; j < reg[4 * i]; j++)
{
auto& x = proc.S[reg[4 * i + 2] + j];
auto& y = proc.S[reg[4 * i + 3]];
protocol.prepare_mul(x, y);
}
protocol.exchange();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < reg[4 * i]; j++)
{
proc.S[reg[4 * i + 1] + j] = protocol.finalize_mul();
}
protocol.counter += reg[4 * i];
}
}
template<class T>
void SubProcessor<T>::dotprods(const vector<int>& reg, int size)
{
protocol.init_dotprod(this);
for (int i = 0; i < size; i++)
{
auto it = reg.begin();
while (it != reg.end())
{
auto next = it + *it;
it += 2;
while (it != next)
{
protocol.prepare_dotprod(S[*it + i], S[*(it + 1) + i]);
it += 2;
}
protocol.next_dotprod();
}
}
protocol.exchange();
for (int i = 0; i < size; i++)
{
auto it = reg.begin();
while (it != reg.end())
{
auto next = it + *it;
it++;
S[*it + i] = protocol.finalize_dotprod((next - it) / 2);
it = next;
}
}
}
template<class T>
void SubProcessor<T>::matmuls(const vector<T>& source,
const Instruction& instruction, int a, int b)
{
auto& dim = instruction.get_start();
auto A = source.begin() + a;
auto B = source.begin() + b;
auto C = S.begin() + (instruction.get_r(0));
assert(A + dim[0] * dim[1] <= source.end());
assert(B + dim[1] * dim[2] <= source.end());
assert(C + dim[0] * dim[2] <= S.end());
protocol.init_dotprod(this);
for (int i = 0; i < dim[0]; i++)
for (int j = 0; j < dim[2]; j++)
{
for (int k = 0; k < dim[1]; k++)
protocol.prepare_dotprod(*(A + i * dim[1] + k),
*(B + k * dim[2] + j));
protocol.next_dotprod();
}
protocol.exchange();
for (int i = 0; i < dim[0]; i++)
for (int j = 0; j < dim[2]; j++)
*(C + i * dim[2] + j) = protocol.finalize_dotprod(dim[1]);
}
template<class T>
void SubProcessor<T>::matmulsm(const CheckVector<T>& source,
const Instruction& instruction, int a, int b)
{
auto& dim = instruction.get_start();
auto C = S.begin() + (instruction.get_r(0));
assert(C + dim[0] * dim[2] <= S.end());
assert(Proc);
protocol.init_dotprod(this);
for (int i = 0; i < dim[0]; i++)
{
auto ii = Proc->get_Ci().at(dim[3] + i);
for (int j = 0; j < dim[2]; j++)
{
auto jj = Proc->get_Ci().at(dim[6] + j);
for (int k = 0; k < dim[1]; k++)
{
auto kk = Proc->get_Ci().at(dim[4] + k);
auto ll = Proc->get_Ci().at(dim[5] + k);
protocol.prepare_dotprod(source.at(a + ii * dim[7] + kk),
source.at(b + ll * dim[8] + jj));
}
protocol.next_dotprod();
}
}
protocol.exchange();
for (int i = 0; i < dim[0]; i++)
for (int j = 0; j < dim[2]; j++)
*(C + i * dim[2] + j) = protocol.finalize_dotprod(dim[1]);
}
template<class sint, class sgf2n>
ostream& operator<<(ostream& s,const Processor<sint, sgf2n>& P)
{
s << "Processor State" << endl;
s << "Char 2 Registers" << endl;
s << "Val\tClearReg\tSharedReg" << endl;
for (int i=0; i<P.reg_max2; i++)
{ s << i << "\t";
P.read_C2(i).output(s,true);
s << "\t";
P.read_S2(i).output(s,true);
s << endl;
}
s << "Char p Registers" << endl;
s << "Val\tClearReg\tSharedReg" << endl;
for (int i=0; i<P.reg_maxp; i++)
{ s << i << "\t";
P.read_Cp(i).output(s,true);
s << "\t";
P.read_Sp(i).output(s,true);
s << endl;
}
return s;
}
#endif