Skip to content

Commit

Permalink
BMR, Use your Brain, Semi/Semi2k.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkskeller committed Apr 30, 2019
1 parent 9cde954 commit b5d8123
Show file tree
Hide file tree
Showing 214 changed files with 4,223 additions and 1,540 deletions.
10 changes: 0 additions & 10 deletions Auth/MAC_Check.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@
#include "Tools/int.h"
#include "Tools/benchmarking.h"

#include "Math/gfp.h"
#include "Math/gf2n.h"
#include "Math/BitVec.h"
#include "Math/Rep3Share.h"
#include "Math/MaliciousRep3Share.h"
#include "Math/ShamirShare.h"
#include "Math/MaliciousShamirShare.h"
#include "Math/Z2k.h"
#include "Math/Spdz2kShare.h"

#include <algorithm>

template<class T>
Expand Down
2 changes: 0 additions & 2 deletions Auth/MaliciousRepMC.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#define AUTH_MALICIOUSREPMC_H_

#include "ReplicatedMC.h"
#include "GC/MaliciousRepSecret.h"
#include "GC/Machine.h"

template<class T>
class MaliciousRepMC : public ReplicatedMC<T>
Expand Down
1 change: 1 addition & 0 deletions Auth/MaliciousRepMC.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "MaliciousRepMC.h"
#include "GC/Machine.h"
#include "Math/BitVec.h"

#include "ReplicatedMC.hpp"

Expand Down
29 changes: 29 additions & 0 deletions Auth/SemiMC.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* SemiMC.h
*
*/

#ifndef AUTH_SEMIMC_H_
#define AUTH_SEMIMC_H_

#include "MAC_Check.h"

template<class T>
class SemiMC : public TreeSum<typename T::open_type>, public MAC_Check_Base<T>
{
public:
// emulate MAC_Check
SemiMC(const typename T::mac_key_type& _ = {}, int __ = 0, int ___ = 0)
{ (void)_; (void)__; (void)___; }

// emulate Direct_MAC_Check
SemiMC(const typename T::mac_key_type& _, Names& ____, int __ = 0, int ___ = 0)
{ (void)_; (void)__; (void)___; (void)____; }

void POpen_Begin(vector<typename T::open_type>& values,const vector<T>& S,const Player& P);
void POpen_End(vector<typename T::open_type>& values,const vector<T>& S,const Player& P);

void Check(const Player& P) { (void)P; }
};

#endif /* AUTH_SEMIMC_H_ */
24 changes: 24 additions & 0 deletions Auth/SemiMC.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* SemiMC.cpp
*
*/

#include "SemiMC.h"

template<class T>
void SemiMC<T>::POpen_Begin(vector<typename T::open_type>& values,
const vector<T>& S, const Player& P)
{
values.clear();
for (auto& x : S)
values.push_back(x);
this->start(values, P);
}

template<class T>
void SemiMC<T>::POpen_End(vector<typename T::open_type>& values,
const vector<T>& S, const Player& P)
{
(void) S;
this->finish(values, P);
}
22 changes: 15 additions & 7 deletions Auth/ShamirMC.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,25 @@ void ShamirMC<T>::POpen_Begin(vector<typename T::clear>& values,
(void) values;
os.clear();
os.resize(P.num_players());
if (P.my_num() <= threshold)
bool send = P.my_num() <= threshold;
if (send)
{
for (auto& share : S)
share.pack(os[P.my_num()]);
for (int i = 0; i < P.num_players(); i++)
if (i != P.my_num())
P.send_to(i, os[P.my_num()], true);
}
for (int i = 0; i <= threshold; i++)
if (i != P.my_num())
P.receive_player(i, os[i], true);
for (int offset = 1; offset < P.num_players(); offset++)
{
int send_to = P.get_player(offset);
int receive_from = P.get_player(-offset);
bool receive = receive_from <= threshold;
if (send)
if (receive)
P.pass_around(os[P.my_num()], os[receive_from], offset);
else
P.send_to(send_to, os[P.my_num()], true);
else if (receive)
P.receive_player(receive_from, os[receive_from], true);
}
}

template<class T>
Expand Down
2 changes: 2 additions & 0 deletions Auth/Subroutines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,5 @@ template void Create_Random(gf2n_short& ans,const Player& P);
#endif

template void Create_Random(gfp& ans,const Player& P);
template void Create_Random(gfp1& ans,const Player& P);
template void Create_Random(gfp2& ans,const Player& P);
7 changes: 3 additions & 4 deletions Auth/fake-stuff.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

#include "Math/gf2n.h"
#include "Math/gfp.h"
#include "Math/Z2k.h"
#include "Math/Share.h"
#include "Math/Rep3Share.h"
#include "GC/MaliciousRepSecret.h"

#include <fstream>
using namespace std;
Expand All @@ -29,6 +25,9 @@ void generate_keys(const string& directory, int nplayers);
template <class T, class U>
void write_mac_keys(const string& directory, int player_num, int nplayers, U keyp, T key2);

template <class T, class U>
void read_mac_keys(const string& directory, int player_num, int nplayers, U& keyp, T& key2);

// Read MAC key shares and compute keys
void read_keys(const string& directory, gfp& keyp, gf2n& key2, int nplayers);

Expand Down
41 changes: 41 additions & 0 deletions Auth/fake-stuff.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Math/gfp.h"
#include "Math/Z2k.h"
#include "Math/Share.h"
#include "Math/SemiShare.h"
#include "Auth/fake-stuff.h"
#include "Tools/benchmarking.h"
#include "Processor/config.h"
Expand All @@ -29,6 +30,21 @@ void make_share(Share<T>* Sa,const U& a,int N,const V& key,PRNG& G)
Sa[N-1]=S;
}

template<class T>
void make_share(SemiShare<T>* Sa,const T& a,int N,const T& key,PRNG& G)
{
(void) key;
insecure("share generation", false);
T x, S = a;
for (int i=0; i<N-1; i++)
{
x.randomize(G);
Sa[i] = x;
S -= x;
}
Sa[N-1]=S;
}

template<class T>
void make_share(FixedVec<T, 2>* Sa, const T& a, int N, const T& key, PRNG& G);

Expand Down Expand Up @@ -154,6 +170,31 @@ void write_mac_keys(const string& directory, int i, int nplayers, U macp, T mac2
outf.close();
}

template <class T, class U>
void read_mac_keys(const string& directory, int player_num, int nplayers, U& keyp, T& key2)
{
int nn;

string filename = directory + "Player-MAC-Keys-P" + to_string(player_num);
ifstream inpf;
inpf.open(filename);
if (inpf.fail())
{
cerr << "Could not open MAC key file. Perhaps it needs to be generated?\n";
throw file_error(filename);
}
inpf >> nn;
if (nn!=nplayers)
{ cerr << "KeyGen was last run with " << nn << " players." << endl;
cerr << " - You are running Online with " << nplayers << " players." << endl;
exit(1);
}

keyp.input(inpf,true);
key2.input(inpf,true);
inpf.close();
}

inline void read_keys(const string& directory, gfp& keyp, gf2n& key2, int nplayers)
{
gfp sharep;
Expand Down
111 changes: 31 additions & 80 deletions BMR/CommonParty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@

CommonParty* CommonParty::singleton = 0;

CommonParty::CommonParty() :
_node(0), gate_counter(0), gate_counter2(0), garbled_tbl_size(0),
cpu_timer(CLOCK_PROCESS_CPUTIME_ID), buffers(TYPE_MAX)
CommonFakeParty::CommonFakeParty() :
_node(0), buffers(TYPE_MAX)
{
insecure("MPC emulation");
}

CommonParty::CommonParty() :
gate_counter(0), gate_counter2(0), garbled_tbl_size(0),
cpu_timer(CLOCK_PROCESS_CPUTIME_ID)
{
if (singleton != 0)
throw runtime_error("there can only be one");
singleton = this;
Expand All @@ -29,20 +34,27 @@ CommonParty::CommonParty() :
mac_key.randomize(prng);
}

CommonFakeParty::~CommonFakeParty()
{
if (_node)
delete _node;
}

CommonParty::~CommonParty()
{
if (_node)
delete _node;
cout << "Wire storage: " << 1e-9 * wires.capacity() << " GB" << endl;
cout << "CPU time: " << cpu_timer.elapsed() << endl;
cout << "Total time: " << timer.elapsed() << endl;
cout << "First phase time: " << timers[0].elapsed() << endl;
cout << "Second phase time: " << timers[1].elapsed() << endl;
cout << "Number of gates: " << gate_counter << endl;
cerr << "Total time: " << timer.elapsed() << endl;
#ifdef VERBOSE
cerr << "Wire storage: " << 1e-9 * wires.capacity() << " GB" << endl;
cerr << "CPU time: " << cpu_timer.elapsed() << endl;
cerr << "First phase time: " << timers[0].elapsed() << endl;
cerr << "Second phase time: " << timers[1].elapsed() << endl;
cerr << "Number of gates: " << gate_counter << endl;
#endif
}

void CommonParty::init(const char* netmap_file, int id, int n_parties)
void CommonParty::check(int n_parties)
{
(void) n_parties;
#ifdef N_PARTIES
if (n_parties != N_PARTIES)
throw runtime_error("wrong number of parties");
Expand All @@ -53,6 +65,11 @@ void CommonParty::init(const char* netmap_file, int id, int n_parties)
#endif
_N = n_parties;
#endif // N_PARTIES
}

void CommonFakeParty::init(const char* netmap_file, int id, int n_parties)
{
check(n_parties);
printf("netmap_file: %s\n", netmap_file);
if (0 == strcmp(netmap_file, LOOPBACK_STR)) {
_node = new Node( NULL, id, this, _N + 1);
Expand All @@ -61,7 +78,7 @@ void CommonParty::init(const char* netmap_file, int id, int n_parties)
}
}

int CommonParty::init(const char* netmap_file, int id)
int CommonFakeParty::init(const char* netmap_file, int id)
{
int n_parties;
if (string(netmap_file) != string(LOOPBACK_STR))
Expand Down Expand Up @@ -93,7 +110,7 @@ void CommonParty::next_gate(GarbledGate& gate)
gate.init_inputs(gate_counter2, _N);
}

SendBuffer& CommonParty::get_buffer(MSG_TYPE type)
SendBuffer& CommonFakeParty::get_buffer(MSG_TYPE type)
{
SendBuffer& buffer = buffers[type];
buffer.clear();
Expand Down Expand Up @@ -122,52 +139,6 @@ void CommonCircuitParty::print_outputs(const vector<int>& indices)
}


template <class T, class U>
GC::BreakType CommonParty::first_phase(GC::Program<U>& program,
GC::Processor<T>& processor, GC::Machine<T>& machine)
{
(void)machine;
timers[0].start();
reset();
wires.clear();
GC::BreakType next = (reinterpret_cast<GC::Program<T>*>(&program))->execute(processor);
#ifdef DEBUG_ROUNDS
cout << "finished first phase at pc " << processor.PC
<< " reason " << next << endl;
#endif
timers[0].stop();
cout << "First round time: " << timers[0].elapsed() << " / "
<< timer.elapsed() << endl;
#ifdef DEBUG_WIRES
cout << "Storing wires with " << 1e-9 * wires.size() << " GB on disk" << endl;
#endif
wire_storage.push(wires);
return next;
}

template<class T>
GC::BreakType CommonParty::second_phase(GC::Program<T>& program,
GC::Processor<T>& processor, GC::Machine<T>& machine)
{
(void)machine;
wire_storage.pop(wires);
wires.reset_head();
timers[1].start();
GC::BreakType next = GC::TIME_BREAK;
next = program.execute(processor);
#ifdef DEBUG_ROUNDS
cout << "finished second phase at " << processor.PC
<< " reason " << next << endl;
#endif
timers[1].stop();
// cout << "Second round time: " << timers[1].elapsed() << ", ";
// cout << "total time: " << timer.elapsed() << endl;
if (false)
return GC::CAP_BREAK;
else
return next;
}

void CommonCircuitParty::prepare_input_regs(party_id_t from)
{
party_t sender = _circuit->_parties[from];
Expand All @@ -188,23 +159,3 @@ void CommonCircuitParty::prepare_output_regs()
for (size_t i = 0; i < _OW; i++)
output_regs.push_back(_circuit->OutWiresStart()+i);
}

template GC::BreakType CommonParty::first_phase(
GC::Program<GC::Secret<GarbleRegister> >& program,
GC::Processor<GC::Secret<RandomRegister> >& processor,
GC::Machine<GC::Secret<RandomRegister> >& machine);

template GC::BreakType CommonParty::first_phase(
GC::Program<GC::Secret<EvalRegister> >& program,
GC::Processor<GC::Secret<PRFRegister> >& processor,
GC::Machine<GC::Secret<PRFRegister> >& machine);

template GC::BreakType CommonParty::second_phase(
GC::Program<GC::Secret<GarbleRegister> >& program,
GC::Processor<GC::Secret<GarbleRegister> >& processor,
GC::Machine<GC::Secret<GarbleRegister> >& machine);

template GC::BreakType CommonParty::second_phase(
GC::Program<GC::Secret<EvalRegister> >& program,
GC::Processor<GC::Secret<EvalRegister> >& processor,
GC::Machine<GC::Secret<EvalRegister> >& machine);
Loading

0 comments on commit b5d8123

Please sign in to comment.