Skip to content

Commit

Permalink
Shamir secret sharing.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkskeller committed Jan 5, 2019
1 parent 343f484 commit 216fbdf
Show file tree
Hide file tree
Showing 57 changed files with 1,375 additions and 215 deletions.
6 changes: 6 additions & 0 deletions Auth/MAC_Check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "Math/BitVec.h"
#include "Math/Rep3Share.h"
#include "Math/MaliciousRep3Share.h"
#include "Math/ShamirShare.h"
#include "Math/MaliciousShamirShare.h"

#include <algorithm>

Expand Down Expand Up @@ -460,3 +462,7 @@ template class MAC_Check_Base<Rep3Share<gf2n>>;
template class MAC_Check_Base<Rep3Share<Integer>>;
template class MAC_Check_Base<MaliciousRep3Share<gfp>>;
template class MAC_Check_Base<MaliciousRep3Share<gf2n>>;
template class MAC_Check_Base<ShamirShare<gfp>>;
template class MAC_Check_Base<ShamirShare<gf2n>>;
template class MAC_Check_Base<MaliciousShamirShare<gfp>>;
template class MAC_Check_Base<MaliciousShamirShare<gf2n>>;
35 changes: 35 additions & 0 deletions Auth/MaliciousShamirMC.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* MaliciousShamirMC.h
*
*/

#ifndef AUTH_MALICIOUSSHAMIRMC_H_
#define AUTH_MALICIOUSSHAMIRMC_H_

#include "ShamirMC.h"

template<class T>
class MaliciousShamirMC : public ShamirMC<T>
{
vector<vector<typename T::clear>> reconstructions;

public:
MaliciousShamirMC();

// emulate MAC_Check
MaliciousShamirMC(const typename T::value_type& _, int __ = 0, int ___ = 0) :
MaliciousShamirMC()
{ (void)_; (void)__; (void)___; }

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


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

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

#include "MaliciousShamirMC.h"
#include "Processor/ShamirMachine.h"

template<class T>
MaliciousShamirMC<T>::MaliciousShamirMC()
{
this->threshold = 2 * ShamirMachine::s().threshold;
}

template<class T>
void MaliciousShamirMC<T>::POpen_End(vector<typename T::clear>& values,
const vector<T>& S, const Player& P)
{
(void) P;
int threshold = ShamirMachine::s().threshold;
if (reconstructions.empty())
{
reconstructions.resize(2 * threshold + 2);
for (int i = threshold + 1; i <= 2 * threshold + 1; i++)
{
reconstructions[i].resize(i);
for (int j = 0; j < i; j++)
reconstructions[i][j] = Shamir<T>::get_rec_factor(j, i);
}
}

values.clear();
values.resize(S.size());
vector<T> shares(2 * threshold + 1);
for (size_t i = 0; i < values.size(); i++)
{
for (size_t j = 0; j < shares.size(); j++)
shares[j].unpack(this->os[j]);
T value = 0;
for (int j = 0; j < threshold + 1; j++)
value += shares[j] * reconstructions[threshold + 1][j];
for (int j = threshold + 2; j <= 2 * threshold + 1; j++)
{
T check = 0;
for (int k = 0; k < j; k++)
check += shares[k] * reconstructions[j][k];
if (check != value)
throw mac_fail();
}
values[i] = value;
}
}
40 changes: 40 additions & 0 deletions Auth/ShamirMC.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* ShamirMC.h
*
*/

#ifndef AUTH_SHAMIRMC_H_
#define AUTH_SHAMIRMC_H_

#include "MAC_Check.h"
#include "Math/ShamirShare.h"
#include "Processor/ShamirMachine.h"

template<class T>
class ShamirMC : public MAC_Check_Base<T>
{
vector<typename T::clear> reconstruction;

protected:
vector<octetStream> os;
int threshold;

public:
ShamirMC() : threshold(ShamirMachine::s().threshold) {}

// emulate MAC_Check
ShamirMC(const typename T::value_type& _, int __ = 0, int ___ = 0) : ShamirMC()
{ (void)_; (void)__; (void)___; }

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

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

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

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

#include "ShamirMC.h"

template<class T>
void ShamirMC<T>::POpen_Begin(vector<typename T::clear>& values,
const vector<T>& S, const Player& P)
{
(void) values;
os.clear();
os.resize(P.num_players());
if (P.my_num() <= threshold)
{
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);
}

template<class T>
void ShamirMC<T>::POpen_End(vector<typename T::clear>& values,
const vector<T>& S, const Player& P)
{
(void) P;
int n_relevant_players = ShamirMachine::s().threshold + 1;
if (reconstruction.empty())
{
reconstruction.resize(n_relevant_players, 1);
for (int i = 0; i < n_relevant_players; i++)
reconstruction[i] = Shamir<typename T::clear>::get_rec_factor(i,
n_relevant_players);
}

values.clear();
values.resize(S.size());
for (size_t i = 0; i < values.size(); i++)
for (int j = 0; j < n_relevant_players; j++)
values[i] += os[j].template get<typename T::clear>() * reconstruction[j];
}
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
The changelog explains changes pulled through from the private development repository. Bug fixes and small enchancements are committed between releases and not documented here.

## 0.0.6 (Jan 5, 2019)

- Shamir secret sharing

## 0.0.5 (Nov 5, 2018)

- More three-party replicated secret sharing
- Encrypted communication for replicated secret sharing

## 0.0.4 (Oct 11, 2018)
Expand Down
8 changes: 7 additions & 1 deletion Compiler/GC/types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from Compiler.types import MemValue, read_mem_value, regint, Array
from Compiler.types import _bitint, _number, _fix
from Compiler.types import _bitint, _number, _fix, _structure
from Compiler.program import Tape, Program
from Compiler.exceptions import *
from Compiler import util, oram, floatingpoint
Expand Down Expand Up @@ -66,6 +66,12 @@ def bit_decompose(self, bit_length=None):
else:
return self.decomposed[:n] + suffix
@classmethod
def malloc(cls, size):
return Program.prog.malloc(size, cls)
@staticmethod
def n_elements():
return 1
@classmethod
def load_mem(cls, address, mem_type=None):
res = cls()
if mem_type == 'sd':
Expand Down
32 changes: 22 additions & 10 deletions Compiler/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1773,7 +1773,7 @@ class cfix(_number, _structure):
""" Clear fixed point type. """
__slots__ = ['value', 'f', 'k', 'size']
reg_type = 'c'
scalars = (int, long, float)
scalars = (int, long, float, regint)
@classmethod
def set_precision(cls, f, k = None):
# k is the whole bitlength of fixed point
Expand Down Expand Up @@ -1829,7 +1829,12 @@ def __init__(self, v=None, size=None):
if isinstance(v, cint):
self.v = cint(v,size=self.size)
elif isinstance(v, cfix.scalars):
self.v = cint(int(round(v * (2 ** f))),size=self.size)
v = v * (2 ** f)
try:
v = int(round(v))
except TypeError:
pass
self.v = cint(v, size=self.size)
elif isinstance(v, cfix):
self.v = v.v
elif isinstance(v, MemValue):
Expand Down Expand Up @@ -2072,9 +2077,11 @@ def __init__(self, _v=None, size=None):
self.v = _v.v
elif isinstance(_v, (MemValue, MemFix)):
#this is a memvalue object
self.v = self.conv(_v.read())
self.v = sfix(_v.read()).v
else:
raise CompilerError('cannot convert %s to sfix' % _v)
if not isinstance(self.v, self.int_type):
raise CompilerError('sfix conversion failure: %s/%s' % (_v, self.v))

@vectorize
def load_int(self, v):
Expand Down Expand Up @@ -2304,16 +2311,17 @@ def __init__(self, v, p=None, z=None, s=None, size=None):
z = v.z
s = v.s
v = v.v
elif isinstance(v, sint):
v, p, z, s = floatingpoint.Int2FL(v, program.bit_length,
self.vlen, self.kappa)
elif isinstance(v, sfix):
f = v.f
v, p, z, s = floatingpoint.Int2FL(v.v, v.k,
self.vlen, self.kappa)
p = p - f
else:
elif util.is_constant_float(v):
v, p, z, s = self.convert_float(v, self.vlen, self.plen)
else:
v, p, z, s = floatingpoint.Int2FL(sint.conv(v),
program.bit_length,
self.vlen, self.kappa)
if isinstance(v, int):
if not ((v >= 2**(self.vlen-1) and v < 2**(self.vlen)) or v == 0):
raise CompilerError('Floating point number malformed: significand')
Expand Down Expand Up @@ -2554,6 +2562,11 @@ def print_float_plain(self):
'ci': regint,
}

def _get_type(t):
if t in _types:
return _types[t]
else:
return t

class Array(object):
@classmethod
Expand All @@ -2566,8 +2579,7 @@ def create_from(cls, l):
return res

def __init__(self, length, value_type, address=None, debug=None):
if value_type in _types:
value_type = _types[value_type]
value_type = _get_type(value_type)
self.address = address
self.length = length
self.value_type = value_type
Expand Down Expand Up @@ -2695,7 +2707,7 @@ def reveal(self):
class SubMultiArray(object):
def __init__(self, sizes, value_type, address, index, debug=None):
self.sizes = sizes
self.value_type = value_type
self.value_type = _get_type(value_type)
self.address = address + index * reduce(operator.mul, self.sizes) * \
self.value_type.n_elements()
self.sub_cache = {}
Expand Down
3 changes: 3 additions & 0 deletions Compiler/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ def reveal(x):
def is_constant(x):
return isinstance(x, (int, long, bool))

def is_constant_float(x):
return isinstance(x, float) or is_constant(x)

def is_zero(x):
try:
return int(x) is 0
Expand Down
1 change: 1 addition & 0 deletions GC/Machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "Thread.hpp"
#include "ThreadMaster.hpp"
#include "Auth/MaliciousRepMC.hpp"
#include "Processor/Replicated.hpp"

namespace GC
{
Expand Down
2 changes: 1 addition & 1 deletion License.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CSIRO Open Source Software Licence Agreement (variation of the BSD / MIT License)
Copyright (c) 2018, Commonwealth Scientific and Industrial Research Organisation (CSIRO) ABN 41 687 119 230.
Copyright (c) 2019, Commonwealth Scientific and Industrial Research Organisation (CSIRO) ABN 41 687 119 230.
All rights reserved. CSIRO is willing to grant you a licence to this MP-SPDZ sofware on the following terms, except where otherwise indicated for third party material.
Redistribution and use of this software in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Expand Down
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ OBJS = $(BMR) $(FHEOFFLINE) $(TINYOTOFFLINE) $(YAO) $(COMPLETE)
DEPS := $(OBJS:.o=.d)


all: gen_input online offline externalIO yao replicated
all: gen_input online offline externalIO yao replicated shamir

ifeq ($(USE_GF2N_LONG),1)
all: bmr
Expand Down Expand Up @@ -80,6 +80,8 @@ replicated: rep-field rep-ring rep-bin

tldr: malicious-rep-field-party.x Setup.x

shamir: shamir-party.x malicious-shamir-party.x galois-degree.x

Fake-Offline.x: Fake-Offline.cpp $(COMMON) $(PROCESSOR)
$(CXX) $(CFLAGS) -o $@ Fake-Offline.cpp $(COMMON) $(PROCESSOR) $(LDLIBS)

Expand Down Expand Up @@ -179,6 +181,12 @@ replicated-field-party.x: replicated-field-party.cpp $(PROCESSOR) $(COMMON)
malicious-rep-field-party.x: malicious-rep-field-party.cpp $(PROCESSOR) $(COMMON)
$(CXX) $(CFLAGS) -o $@ $^ $(LDLIBS)

shamir-party.x: shamir-party.cpp $(PROCESSOR) $(COMMON)
$(CXX) $(CFLAGS) -o $@ $^ $(LDLIBS)

malicious-shamir-party.x: malicious-shamir-party.cpp $(PROCESSOR) $(COMMON)
$(CXX) $(CFLAGS) -o $@ $^ $(LDLIBS)

$(LIBSIMPLEOT): SimpleOT/Makefile
$(MAKE) -C SimpleOT

Expand Down
1 change: 1 addition & 0 deletions Math/MaliciousRep3Share.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class MaliciousRep3Share : public Rep3Share<T>
typedef MAC_Check Direct_MC;
typedef ReplicatedInput<MaliciousRep3Share<T>> Input;
typedef ReplicatedPrivateOutput<MaliciousRep3Share<T>> PrivateOutput;
typedef Rep3Share<T> Honest;

static string type_short()
{
Expand Down
Loading

0 comments on commit 216fbdf

Please sign in to comment.