Skip to content

Commit

Permalink
SPDZ2k offline phase.
Browse files Browse the repository at this point in the history
  • Loading branch information
n1v0lg authored and mkskeller committed Mar 26, 2019
1 parent 5a0413d commit 881b440
Show file tree
Hide file tree
Showing 49 changed files with 2,119 additions and 374 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ keys/*
##############################
CONFIG.mine
config_mine.py
HOSTS

# Temporary files #
###################
Expand Down
134 changes: 129 additions & 5 deletions Auth/MAC_Check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,19 @@ MAC_Check<T>::~MAC_Check()
}

template<class T>
void MAC_Check<T>::POpen_Begin(vector<T>& values,const vector<Share<T> >& S,const Player& P)
void MAC_Check<T>::PrepareSending(vector<T>& values, const vector<Share<T> >& S)
{
AddToMacs(S);

values.resize(S.size());
for (unsigned int i=0; i<S.size(); i++)
{ values[i]=S[i].get_share(); }
}

template<class T>
void MAC_Check<T>::POpen_Begin(vector<T>& values,const vector<Share<T> >& S,const Player& P)
{
AddToMacs(S);

PrepareSending(values, S);

this->start(values, P);

Expand Down Expand Up @@ -115,9 +121,9 @@ void MAC_Check<T>::CheckIfNeeded(const Player& P)


template <class T>
void MAC_Check<T>::AddToCheck(const T& mac, const T& value, const Player& P)
void MAC_Check<T>::AddToCheck(const Share<T>& share, const T& value, const Player& P)
{
macs.push_back(mac);
macs.push_back(share.get_mac());
vals.push_back(value);
popen_cnt++;
CheckIfNeeded(P);
Expand Down Expand Up @@ -179,6 +185,115 @@ int mc_base_id(int function_id, int thread_num)
return (function_id << 28) + ((T::field_type() + 1) << 24) + (thread_num << 16);
}

template<class T, class U, class V>
MAC_Check_Z2k<T, U, V>::MAC_Check_Z2k(const T& ai, const Share<T>& dummy_element, int opening_sum, int max_broadcast, int send_player) :
MAC_Check<T>(ai, opening_sum, max_broadcast, send_player),
dummy_element(dummy_element)
{
}

template<class T, class U, class V>
void MAC_Check_Z2k<T, U, V>::AddToCheck(const Share<T>& share, const T& value, const Player& P)
{
shares.push_back(share.get_share());
MAC_Check<T>::AddToCheck(share, value, P);
}

template<class T, class U, class V>
void MAC_Check_Z2k<T, U, V>::AddToMacs(const vector<Share<T> >& shares)
{
for (auto& share : shares)
this->shares.push_back(share.get_share());
MAC_Check<T>::AddToMacs(shares);
}

template<class T, class U, class V>
void MAC_Check_Z2k<T, U, V>::PrepareSending(vector<T>& values,
const vector<Share<T> >& S)
{
values.clear();
values.reserve(S.size());
for (auto& share : S)
values.push_back(V(share.get_share()));
}

template<class T, class U, class V>
Share<T> MAC_Check_Z2k<T, U, V>::get_random_element() {
return dummy_element;
}

template<class T, class U, class V>
void MAC_Check_Z2k<T, U, V>::set_random_element(const Share<T>& random_element) {
this->dummy_element = random_element;
}

template<class T, class U, class V>
void MAC_Check_Z2k<T, U, V>::Check(const Player& P)
{
if (this->WaitingForCheck() == 0)
return;

int k = V::N_BITS;
octet seed[SEED_SIZE];
Create_Random_Seed(seed,P,SEED_SIZE);
PRNG G;
G.SetSeed(seed);

T y, mj;
y.assign_zero();
mj.assign_zero();
vector<U> chi;
for (int i = 0; i < this->popen_cnt; ++i)
{
U temp_chi;
temp_chi.randomize(G);
T xi = this->vals[i];
y += xi * temp_chi;
T mji = this->macs[i];
mj += temp_chi * mji;
chi.push_back(temp_chi);
}

Share<T> r = get_random_element();
T lj = r.get_mac();
U pj;
pj.assign_zero();
for (int i = 0; i < this->popen_cnt; ++i)
{
T xji = shares[i];
V xbarji = xji;
U pji = U((xji - xbarji) >> k);
pj += chi[i] * pji;
}
pj += U(r.get_share());

U pbar(pj);
vector<octetStream> pj_stream(P.num_players());
pj.pack(pj_stream[P.my_num()]);
P.Broadcast_Receive(pj_stream, true);
for (int j=0; j<P.num_players(); j++) {
if (j!=P.my_num()) {
pbar += pj_stream[j].consume(U::size());
}
}

T zj = mj - (this->alphai * y) - (((this->alphai * pbar)) << k) + (lj << k);
vector<T> zjs(P.num_players());
zjs[P.my_num()] = zj;
Commit_And_Open(zjs, P);

T zj_sum;
zj_sum.assign_zero();
for (int i = 0; i < P.num_players(); ++i)
zj_sum += zjs[i];

this->vals.erase(this->vals.begin(), this->vals.begin() + this->popen_cnt);
this->macs.erase(this->macs.begin(), this->macs.begin() + this->popen_cnt);
this->shares.erase(this->shares.begin(), this->shares.begin() + this->popen_cnt);
this->popen_cnt=0;
if (!zj_sum.is_zero()) { throw mac_fail(); }
}

template<class T>
Separate_MAC_Check<T>::Separate_MAC_Check(const T& ai, Names& Nms,
int thread_num, int opening_sum, int max_broadcast, int send_player) :
Expand Down Expand Up @@ -381,4 +496,13 @@ template class Direct_MAC_Check<gf2n_short>;
template class Parallel_MAC_Check<gf2n_short>;
#endif

template class MAC_Check_Z2k<Z2<64>, Z2<32>, Z2<32> >;
template class MAC_Check_Z2k<Z2<128>, Z2<64>, Z2<64> >;
template class MAC_Check_Z2k<Z2<96>, Z2<32>, Z2<64> >;
template class MAC_Check_Z2k<Z2<160>, Z2<96>, Z2<64> >;
template class MAC_Check_Z2k<Z2<192>, Z2<64>, Z2<128> >;
template class MAC_Check_Z2k<Z2<256>, Z2<96>, Z2<160> >;
template class MAC_Check<Z2<96> >;
template class MAC_Check<Z2<160> >;
template class MAC_Check<Z2<192> >;
template class MAC_Check<Z2<256> >;
24 changes: 22 additions & 2 deletions Auth/MAC_Check.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ class MAC_Check : public TreeSum<T>
/* MAC Share */
T alphai;

void AddToMacs(const vector< Share<T> >& shares);
virtual void AddToMacs(const vector< Share<T> >& shares);
virtual void PrepareSending(vector<T>& values,const vector<Share<T> >& S);
void AddToValues(vector<T>& values);
void GetValues(vector<T>& values);
void CheckIfNeeded(const Player& P);
Expand All @@ -99,14 +100,33 @@ class MAC_Check : public TreeSum<T>
*/
virtual void POpen_Begin(vector<T>& values,const vector<Share<T> >& S,const Player& P);
virtual void POpen_End(vector<T>& values,const vector<Share<T> >& S,const Player& P);
void AddToCheck(const T& mac, const T& value, const Player& P);
virtual void AddToCheck(const Share<T>& share, const T& value, const Player& P);
virtual void Check(const Player& P);

int number() const { return values_opened; }

const T& get_alphai() const { return alphai; }
};

template<class T, class U, class V>
class MAC_Check_Z2k : public MAC_Check<T>
{
protected:
vector<T> shares;
Share<T> dummy_element;
Share<T> get_random_element();

void AddToMacs(const vector< Share<T> >& shares);
void PrepareSending(vector<T>& values,const vector<Share<T> >& S);

public:
void AddToCheck(const Share<T>& share, const T& value, const Player& P);
MAC_Check_Z2k(const T& ai, const Share<T>& dummy_element = {}, int opening_sum=10, int max_broadcast=10, int send_player=0);
virtual void Check(const Player& P);
void set_random_element(const Share<T>& random_element);
virtual ~MAC_Check_Z2k() {};
};

template <class T, class U, class V>
using MAC_Check_ = MAC_Check<T>;

Expand Down
5 changes: 5 additions & 0 deletions Auth/Subroutines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,9 @@ template void Create_Random(gf2n_short& ans,const Player& P);
template void Commit_And_Open(vector<gfp>& data,const Player& P);
template void Create_Random(gfp& ans,const Player& P);

template void Commit_And_Open(vector<Z2<64> >& data,const Player& P);
template void Commit_And_Open(vector<Z2<96> >& data,const Player& P);
template void Commit_And_Open(vector<Z2<128> >& data,const Player& P);
template void Commit_And_Open(vector<Z2<160> >& data,const Player& P);
template void Commit_And_Open(vector<Z2<192> >& data,const Player& P);
template void Commit_And_Open(vector<Z2<256> >& data,const Player& P);
49 changes: 48 additions & 1 deletion Auth/fake-stuff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,34 @@ void check_share(vector<Share<T> >& Sa,T& value,T& mac,int N,const T& key)
}
}

template<class T, class V>
void check_share(vector<Share<T> >& Sa,
V& value,
T& mac,
int N,
const T& key)
{
value.assign(0);
mac.assign(0);

for (int i=0; i<N; i++)
{
value.add(Sa[i].get_share());
mac.add(Sa[i].get_mac());
}

V res;
res.mul(value, key);
if (res != mac)
{
cout << "Value: " << value << endl;
cout << "Input MAC: " << mac << endl;
cout << "Actual MAC: " << res << endl;
cout << "MAC key: " << key << endl;
throw mac_fail();
}
}

template void make_share(vector<Share<gf2n> >& Sa,const gf2n& a,int N,const gf2n& key,PRNG& G);
template void make_share(vector<Share<gfp> >& Sa,const gfp& a,int N,const gfp& key,PRNG& G);

Expand All @@ -66,7 +94,26 @@ template void make_share(vector<Share<gf2n_short> >& Sa,const gf2n_short& a,int
template void check_share(vector<Share<gf2n_short> >& Sa,gf2n_short& value,gf2n_short& mac,int N,const gf2n_short& key);
#endif

template void check_share(vector<Share<Z2<64> > >& Sa,Z2<64>& value,Z2<64>& mac,int N,const Z2<64>& key);
template void check_share(
vector<Share<Z2<160> > >& Sa,
Z2<64>& value,
Z2<160>& mac,
int N,
const Z2<160>& key);

template void check_share(
vector<Share<Z2<128> > >& Sa,
Z2<64>& value,
Z2<128>& mac,
int N,
const Z2<128>& key);

template void check_share(
vector<Share<Z2<64> > >& Sa,
Z2<32>& value,
Z2<64>& mac,
int N,
const Z2<64>& key);

// Expansion is by x=y^5+1 (as we embed GF(256) into GF(2^40)
void expand_byte(gf2n_short& a,int b)
Expand Down
8 changes: 8 additions & 0 deletions Auth/fake-stuff.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "Math/gf2n.h"
#include "Math/gfp.h"
#include "Math/Z2k.h"
#include "Math/Share.h"

#include <fstream>
Expand All @@ -17,6 +18,13 @@ void make_share(vector<Share<T> >& Sa,const T& a,int N,const T& key,PRNG& G);
template<class T>
void check_share(vector<Share<T> >& Sa,T& value,T& mac,int N,const T& key);

template<class T, class V>
void check_share(vector<Share<T> >& Sa,
V& value,
T& mac,
int N,
const T& key);

void expand_byte(gf2n_short& a,int b);
void collapse_byte(int& b,const gf2n_short& a);

Expand Down
18 changes: 15 additions & 3 deletions CONFIG
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,29 @@ PREP_DIR = '-DPREP_DIR="Player-Data/"'
# set for 128-bit GF(2^n) and/or OT preprocessing
USE_GF2N_LONG = 0

# set SPDZ_2K bit length parameters K and S
SPDZ2K_K = -DSPDZ2K_K=64
SPDZ2K_S = -DSPDZ2K_S=64

# use additional optimizations in vole protocol
USE_OPT_VOLE = 1
NUM_VOLE_CHALLENGES = -DNUM_VOLE_CHALLENGES=3

# set to -march=<architecture> for optimization
# AVX2 support (Haswell or later) changes the bit matrix transpose
ARCH = -mtune=native -mavx
ARCH = -mtune=native -mavx -march=native

#use CONFIG.mine to overwrite DIR settings
# use CONFIG.mine to overwrite DIR settings
-include CONFIG.mine

ifeq ($(USE_GF2N_LONG),1)
GF2N_LONG = -DUSE_GF2N_LONG
endif

ifeq ($(USE_OPT_VOLE),1)
OPT_VOLE = -DUSE_OPT_VOLE
endif

# MAX_MOD_SZ must be at least ceil(len(p)/len(word))
# Default is 2, which suffices for 128-bit p
# MOD = -DMAX_MOD_SZ=2
Expand All @@ -40,7 +52,7 @@ LDLIBS += -lrt
endif

CXX = g++
CFLAGS = $(ARCH) $(MY_CFLAGS) -g -Wextra -Wall $(OPTIM) -I$(ROOT) -pthread $(PROF) $(DEBUG) $(MOD) $(MEMPROTECT) $(GF2N_LONG) $(PREP_DIR) -maes -mpclmul -msse4.1 --std=c++11 -Werror
CFLAGS = $(ARCH) $(MY_CFLAGS) -g -Wextra -Wall $(OPTIM) -I$(ROOT) -pthread $(PROF) $(DEBUG) $(MOD) $(MOD) $(SPDZ2K_K) $(SPDZ2K_S) $(OPT_VOLE) $(NUM_VOLE_CHALLENGES) $(MEMPROTECT) $(GF2N_LONG) $(PREP_DIR) -maes -mpclmul -msse4.1 -mbmi2 --std=c++11 -Werror -no-pie
CPPFLAGS = $(CFLAGS)
LD = g++

Loading

0 comments on commit 881b440

Please sign in to comment.