Skip to content

Commit

Permalink
=Update to v1.4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
NigelSmart committed Jun 17, 2019
1 parent d1cef18 commit e2c4ef6
Show file tree
Hide file tree
Showing 61 changed files with 44,686 additions and 46,203 deletions.
3,918 changes: 1,915 additions & 2,003 deletions Auto-Test-Data/1/ConversionCircuit-LSSS_to_GC.txt

Large diffs are not rendered by default.

3,918 changes: 1,915 additions & 2,003 deletions Auto-Test-Data/10/ConversionCircuit-LSSS_to_GC.txt

Large diffs are not rendered by default.

3,918 changes: 1,915 additions & 2,003 deletions Auto-Test-Data/11/ConversionCircuit-LSSS_to_GC.txt

Large diffs are not rendered by default.

3,918 changes: 1,915 additions & 2,003 deletions Auto-Test-Data/12/ConversionCircuit-LSSS_to_GC.txt

Large diffs are not rendered by default.

3,854 changes: 1,885 additions & 1,969 deletions Auto-Test-Data/13/ConversionCircuit-LSSS_to_GC.txt

Large diffs are not rendered by default.

3,854 changes: 1,885 additions & 1,969 deletions Auto-Test-Data/14/ConversionCircuit-LSSS_to_GC.txt

Large diffs are not rendered by default.

3,854 changes: 1,885 additions & 1,969 deletions Auto-Test-Data/15/ConversionCircuit-LSSS_to_GC.txt

Large diffs are not rendered by default.

3,854 changes: 1,885 additions & 1,969 deletions Auto-Test-Data/16/ConversionCircuit-LSSS_to_GC.txt

Large diffs are not rendered by default.

3,854 changes: 1,885 additions & 1,969 deletions Auto-Test-Data/17/ConversionCircuit-LSSS_to_GC.txt

Large diffs are not rendered by default.

3,774 changes: 1,845 additions & 1,929 deletions Auto-Test-Data/18/ConversionCircuit-LSSS_to_GC.txt

Large diffs are not rendered by default.

3,774 changes: 1,845 additions & 1,929 deletions Auto-Test-Data/19/ConversionCircuit-LSSS_to_GC.txt

Large diffs are not rendered by default.

3,918 changes: 1,915 additions & 2,003 deletions Auto-Test-Data/2/ConversionCircuit-LSSS_to_GC.txt

Large diffs are not rendered by default.

3,918 changes: 1,915 additions & 2,003 deletions Auto-Test-Data/22/ConversionCircuit-LSSS_to_GC.txt

Large diffs are not rendered by default.

3,918 changes: 1,915 additions & 2,003 deletions Auto-Test-Data/23/ConversionCircuit-LSSS_to_GC.txt

Large diffs are not rendered by default.

3,918 changes: 1,915 additions & 2,003 deletions Auto-Test-Data/24/ConversionCircuit-LSSS_to_GC.txt

Large diffs are not rendered by default.

3,918 changes: 1,915 additions & 2,003 deletions Auto-Test-Data/25/ConversionCircuit-LSSS_to_GC.txt

Large diffs are not rendered by default.

3,918 changes: 1,915 additions & 2,003 deletions Auto-Test-Data/3/ConversionCircuit-LSSS_to_GC.txt

Large diffs are not rendered by default.

3,918 changes: 1,915 additions & 2,003 deletions Auto-Test-Data/4/ConversionCircuit-LSSS_to_GC.txt

Large diffs are not rendered by default.

3,918 changes: 1,915 additions & 2,003 deletions Auto-Test-Data/5/ConversionCircuit-LSSS_to_GC.txt

Large diffs are not rendered by default.

3,918 changes: 1,915 additions & 2,003 deletions Auto-Test-Data/6/ConversionCircuit-LSSS_to_GC.txt

Large diffs are not rendered by default.

3,918 changes: 1,915 additions & 2,003 deletions Auto-Test-Data/7/ConversionCircuit-LSSS_to_GC.txt

Large diffs are not rendered by default.

3,918 changes: 1,915 additions & 2,003 deletions Auto-Test-Data/8/ConversionCircuit-LSSS_to_GC.txt

Large diffs are not rendered by default.

3,918 changes: 1,915 additions & 2,003 deletions Auto-Test-Data/9/ConversionCircuit-LSSS_to_GC.txt

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion CONFIG
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ OSSL = /users/cosic/nsmart/local/openssl/
# MAX_MOD = 7 should be OK for 128 bit based FHE operations
MAX_MOD = 7

#FLAGS = -DSH_DEBUG -DDEBUG -DMAX_MOD_SZ=$(MAX_MOD) -DDETERMINISTIC
# Benchmark flags
# BENCH_NETDATA = Enable benchmarking of network data
# BENCH_MEMORY = Enable benchmarking of memory

#FLAGS = -DSH_DEBUG -DDEBUG -DMAX_MOD_SZ=$(MAX_MOD) -DDETERMINISTIC -DBENCH_NETDATA -DBENCH_MEMORY
FLAGS = -DMAX_MOD_SZ=$(MAX_MOD)

OPT = -O3
Expand Down
2 changes: 0 additions & 2 deletions Circuits/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ The list of all main files (excluding test ones) to convert (so far) are....
./convert.x mult64
./convert.x neg64
./convert.x zero_equal
./convert.x compare_eq
./convert.x compare
./convert.x ModAdd512
./convert.x LSSS_to_GC

2 changes: 0 additions & 2 deletions Circuits/convert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@
./convert.x divide64
./convert.x neg64
./convert.x zero_equal
./convert.x compare_eq
./convert.x compare
./convert.x ModAdd512
./convert.x LSSS_to_GC
48 changes: 32 additions & 16 deletions Compiler/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,15 +450,28 @@ def decorator(loop_body):
return decorator

class Array(list):
def __init__(self, length, reg_type, address=None):
self[:] = range(length or 1000)
self.address = address
def __init__(self, length, reg_type, address=0):
try:
reg_type(0)
except:
reg_type = lambda x: x
self.value_type = reg_type
self[:] = (reg_type(i) for i in range(length or 1000))
self.address = address
def assign(self, other):
self[:] = other
self[:] = (self.value_type(x) for x in other)
def assign_all(self, other):
self[:] = [other] * len(self)
Matrix = lambda *args: defaultdict(lambda: Array(None, None))
self[:] = [self.value_type(other)] * len(self)
def __setitem__(self, index, value):
if isinstance(index, slice):
list.__setitem__(self, index, (self.value_type(x) for x in value))
else:
list.__setitem__(self, index, self.value_type(value))

class Matrix(list):
def __init__(self,n,m,t,*args):
self[:] = [Array(m, t) for i in range(n)]

mergesort = lambda x: x.sort()
and_ = lambda *args: lambda: reduce(lambda x,y: x and y(), args, True)
or_ = lambda *args: lambda: reduce(lambda x,y: x or y(), args, False)
Expand Down Expand Up @@ -495,18 +508,21 @@ def __init__(x, y):
read = lambda x: x.value


class GenericArray(Array):
def __init__(self, n, value_type, address=None):
Array.__init__(self, n, value_type, address)
sint.MemValue = lambda value: MemValue(sint(value))
sint.Array = lambda size, addr=None: Array(size, sint, addr)
sint.Matrix = lambda n, m, addr=None: Matrix(n, m, sint, addr)

sfix.MemValue = lambda value: MemValue(sfix(value))
sfix.Array = lambda size, addr=None: Array(size, sfix, addr)
sfix.Matrix = lambda n, m, addr=None: Matrix(n, m, sfix, addr)

def get_specific_array(value_type):
class A(Array):
def __init__(self, n, address=None):
Array.__init__(self, n, value_type, address)
return A
cfix.MemValue = lambda value: MemValue(cfix(value))
cfix.Array = lambda size, addr=None: Array(size, cfix, addr)
cfix.Matrix = lambda n, m, addr=None: Matrix(n, m, cfix, addr)

for value_type in [sint, cint, sfix, cfix, sfloat]:
value_type.Array = get_specific_array(value_type)
sfloat.MemValue = lambda value: MemValue(sfloat(value))
sfloat.Array = lambda size: Array(size, sfloat)
sfloat.Matrix = lambda n, m: Matrix(n, m, sfloat)

gprint_reg = lambda x,y=None: None
time = lambda: None
Expand Down
39 changes: 19 additions & 20 deletions Compiler/instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@
PRINTINT= 0xB9,
# Comparison of sregints
LTSINT = 0xD0,
GTSINT = 0xD1,
EQSINT = 0xD2,
EQZSINT = 0xD0,
LTZSINT = 0xD1,
BITSINT = 0xD2,
# Bitwise logical operations on sregints
ANDSINT = 0xD3,
Expand Down Expand Up @@ -574,36 +574,35 @@ class negb(base.Instruction):


@base.vectorize
class ltsint(base.Instruction):
r""" LTSINT i j K
Secret less than of secret registers sb_i = ~sr_j < sr_k.
class ltzsint(base.Instruction):
r""" LTZSINT i j
Secret less than of zero of secret register sb_i = ~sr_j < 0.
This instruction is vectorizable
"""
__slots__ = []
arg_format = ['sbw', 'sr', 'sr']
code = base.opcodes['LTSINT']
arg_format = ['sbw', 'sr']
code = base.opcodes['LTZSINT']


@base.vectorize
class gtsint(base.Instruction):
r""" GTSINT i j K
Secret greater than of secret registers sb_i = ~sr_j > sr_k.
class eqzsint(base.Instruction):
r""" EQZSINT i j
Secret equality test to zero of secret register sb_i = ~sr_j == 0.
This instruction is vectorizable
"""
__slots__ = []
arg_format = ['sbw', 'sr', 'sr']
code = base.opcodes['GTSINT']

arg_format = ['sbw', 'sr']
code = base.opcodes['EQZSINT']

@base.vectorize
class eqsint(base.Instruction):
r""" EQSINT i j K
Secret equality test of secret registers sb_i = ~sr_j == sr_k.
class bitsint(base.Instruction):
r""" BITSINT i j n
Assigns sbit register sb_i the n-th bit of register sr_j.
This instruction is vectorizable
"""
__slots__ = []
arg_format = ['sbw', 'sr', 'sr']
code = base.opcodes['EQSINT']
__slots__ = ["code"]
code = base.opcodes['BITSINT']
arg_format = ['sbw', 'sr', 'int']


# Conversion opcodes
Expand Down
6 changes: 3 additions & 3 deletions Compiler/instructions_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@
PRINTINT= 0xB9,

# Comparison of sregints
LTSINT = 0xD0,
GTSINT = 0xD1,
EQSINT = 0xD2,
EQZSINT = 0xD0,
LTZSINT = 0xD1,
BITSINT = 0xD2,

# Bitwise logical operations on sregints
ANDSINT = 0xD3,
Expand Down
105 changes: 82 additions & 23 deletions Compiler/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,6 @@ def __rdiv__(self, other):
return self.div(other, True)

def __lshift__(self, other):

return self.secret_op(other, shlsint)


Expand Down Expand Up @@ -1152,7 +1151,7 @@ def __neg__(self):
return res

##
# Base 2^64 secet int type
# Base 2^64 secret int type
# It uses the new opcodes from mod 2^n arithmetic
class sregint(_secretInt):
""" Shared mod 2^64 integer type. """
Expand Down Expand Up @@ -1197,26 +1196,40 @@ def mul_2_sint(self, other):
mul2sint(msw, lsw, self, other)
return msw, lsw

def eqz(self):
res = sbit()
eqzsint(res, self)
return res

def ltz(self):
res = sbit()
ltzint(res, self)
return res


# comparison implementation for new opcodes
def __lt__(self, other):
res = sbit()
other_local = parse_sregint(other)
if isinstance(other_local, type(self)):
ltsint(res, self, other_local)
other_local = self-other_local;
ltzsint(res, other_local)
return res

def __gt__(self, other):
res = sbit()
other_local = parse_sregint(other)
if isinstance(other_local, type(self)):
gtsint(res, self, other_local)
other_local=other_local-self;
ltzsint(res, other_local)
return res

def __eq__(self, other):
res = sbit()
other_local = parse_sregint(other)
if isinstance(other_local, type(self)):
eqsint(res, self, other_local)
other_local = self-other_local;
eqzsint(res, other_local)
return res

def __le__(self, other):
Expand Down Expand Up @@ -1532,7 +1545,7 @@ class cfix(_number):
""" 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):
Expand Down Expand Up @@ -1571,7 +1584,12 @@ def __init__(self, v, 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 All @@ -1584,8 +1602,20 @@ def load_int(self, v):
r"""Converts the integer v to a cfix value, basically multiplies v by 2^f """
self.v = cint(v) * (2 ** self.f)

def conv(self):
return self
@classmethod
def conv(cls, other):
if isinstance(other, MemValue):
other = other.read()
if isinstance(other, cls):
return other
else:
try:
res = cfix()
res.load_int(other)
return res
except (TypeError, CompilerError):
pass
return cls(other)

def store_in_mem(self, address):
r"""Stores the cfix value x to the cint memory with address
Expand Down Expand Up @@ -1777,8 +1807,20 @@ def set_precision(cls, f, k=None):
else:
cls.k = k

def conv(self):
return self.v
@classmethod
def conv(cls, other):
if isinstance(other, MemValue):
other = other.read()
if isinstance(other, cls):
return other
else:
try:
res = sfix()
sfix.load_int(other)
return res
except (TypeError, CompilerError):
pass
return cls(other)

@vectorized_classmethod
def load_mem(cls, address, mem_type=None):
Expand Down Expand Up @@ -1809,7 +1851,12 @@ def __init__(self, _v=None, size=None):
if isinstance(_v, sint):
self.v = _v
elif isinstance(_v, cfix.scalars):
self.v = sint(int(round(_v * (2 ** f))), size=self.size)
v = _v * (2 ** f)
try:
v = int(round(v))
except TypeError:
pass
self.v = sint(v, size=self.size)
elif isinstance(_v, sfloat):
p = (f + _v.p)
b = (p >= 0)
Expand Down Expand Up @@ -2772,37 +2819,49 @@ def __getitem__(self, index):
return sfloatArray(self.columns, self.multi_array[index].address)


class sfixArray(Array):
class _fixArray(Array):
def __init__(self, length, address=None):
# self.address = address
self.array = Array(length, sint, address)
self.array = Array(length, self.int_type, address)
self.address = self.array.address
self.length = length
self.value_type = sfix

def __getitem__(self, index):
if isinstance(index, slice):
return Array.__getitem__(self, index)
return sfix(self.array[index])
return self.value_type(self.array[index])

def __setitem__(self, index, value):
if isinstance(index, slice):
return Array.__setitem__(self, index, value)
self.array[index] = sfix(value).v
self.array[index] = self.value_type.conv(value).v

def get_address(self, index):
return self.array.get_address(index)

class cfixArray(_fixArray):
int_type = cint
value_type = cfix

class sfixMatrix(Matrix):
class sfixArray(_fixArray):
int_type = sint
value_type = sfix

class _fixMatrix(Matrix):
def __init__(self, rows, columns, address=None):
self.rows = rows
self.columns = columns
self.multi_array = Matrix(rows, columns, sint, address)
self.multi_array = Matrix(rows, columns, self.int_type, address)

def __getitem__(self, index):
return sfixArray(self.columns, self.multi_array[index].address)
return self.value_type.Array(self.columns, self.multi_array[index].address)

class cfixMatrix(_fixMatrix):
int_type = cint
value_type = cfix

class sfixMatrix(_fixMatrix):
int_type = sint
value_type = sfix

class _mem(_number):
__add__ = lambda self, other: self.read() + other
Expand Down Expand Up @@ -3053,8 +3112,8 @@ def __init__(self, n, m, address=None):
cint.Array = cintArray
cint.Matrix = cintMatrix

cfix.Array = cintArray
cfix.Matrix = cintMatrix
cfix.Array = cfixArray
cfix.Matrix = cfixMatrix
cfix.MemValue = MemFix

sint.MemValue = MemValue
Expand Down
Loading

0 comments on commit e2c4ef6

Please sign in to comment.