Skip to content

Commit 24926df

Browse files
committed
Bristol Fashion.
1 parent cb8e46d commit 24926df

98 files changed

Lines changed: 1263 additions & 2654 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
[submodule "mpir"]
55
path = mpir
66
url = git://github.com/wbhart/mpir.git
7+
[submodule "Programs/Circuits"]
8+
path = Programs/Circuits
9+
url = https://github.com/mkskeller/bristol-fashion

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
The changelog explains changes pulled through from the private development repository. Bug fixes and small enhancements are committed between releases and not documented here.
22

3+
## 0.1.6 (Apr 2, 2020)
4+
5+
- Bristol Fashion circuits
6+
- Semi-honest computation with somewhat homomorphic encryption
7+
- Use SSL for client connections
8+
- Client facilities for all arithmetic protocols
9+
310
## 0.1.5 (Mar 20, 2020)
411

512
- Faster conversion between arithmetic and binary secret sharing using [extended daBits](https://eprint.iacr.org/2020/338)

Compiler/GC/instructions.py

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,33 @@ class BinaryVectorInstruction(base.Instruction):
6767
def copy(self, size, subs):
6868
return type(self)(*self.get_new_args(size, subs))
6969

70+
class NonVectorInstruction(base.Instruction):
71+
is_vec = lambda self: False
72+
73+
def __init__(self, *args, **kwargs):
74+
assert(args[0].n <= args[0].unit)
75+
super(NonVectorInstruction, self).__init__(*args, **kwargs)
76+
77+
class NonVectorInstruction1(base.Instruction):
78+
is_vec = lambda self: False
79+
80+
def __init__(self, *args, **kwargs):
81+
assert(args[1].n <= args[1].unit)
82+
super(NonVectorInstruction1, self).__init__(*args, **kwargs)
83+
7084
class xors(BinaryVectorInstruction):
7185
code = opcodes['XORS']
7286
arg_format = tools.cycle(['int','sbw','sb','sb'])
7387

74-
class xorm(base.Instruction):
88+
class xorm(NonVectorInstruction):
7589
code = opcodes['XORM']
7690
arg_format = ['int','sbw','sb','cb']
7791

78-
class xorcb(base.Instruction):
92+
class xorcb(NonVectorInstruction):
7993
code = opcodes['XORCB']
8094
arg_format = ['cbw','cb','cb']
8195

82-
class xorcbi(base.Instruction):
96+
class xorcbi(NonVectorInstruction):
8397
code = opcodes['XORCBI']
8498
arg_format = ['cbw','cb','int']
8599

@@ -101,67 +115,69 @@ class andm(BinaryVectorInstruction):
101115
code = opcodes['ANDM']
102116
arg_format = ['int','sbw','sb','cb']
103117

104-
class addcb(base.Instruction):
118+
class addcb(NonVectorInstruction):
105119
code = opcodes['ADDCB']
106120
arg_format = ['cbw','cb','cb']
107121

108-
class addcbi(base.Instruction):
122+
class addcbi(NonVectorInstruction):
109123
code = opcodes['ADDCBI']
110124
arg_format = ['cbw','cb','int']
111125

112-
class mulcbi(base.Instruction):
126+
class mulcbi(NonVectorInstruction):
113127
code = opcodes['MULCBI']
114128
arg_format = ['cbw','cb','int']
115129

116-
class bitdecs(base.VarArgsInstruction):
130+
class bitdecs(NonVectorInstruction, base.VarArgsInstruction):
117131
code = opcodes['BITDECS']
118132
arg_format = tools.chain(['sb'], itertools.repeat('sbw'))
119133

120-
class bitcoms(base.VarArgsInstruction):
134+
class bitcoms(NonVectorInstruction, base.VarArgsInstruction):
121135
code = opcodes['BITCOMS']
122136
arg_format = tools.chain(['sbw'], itertools.repeat('sb'))
123137

124-
class bitdecc(base.VarArgsInstruction):
138+
class bitdecc(NonVectorInstruction, base.VarArgsInstruction):
125139
code = opcodes['BITDECC']
126140
arg_format = tools.chain(['cb'], itertools.repeat('cbw'))
127141

128-
class shrcbi(base.Instruction):
142+
class shrcbi(NonVectorInstruction):
129143
code = opcodes['SHRCBI']
130144
arg_format = ['cbw','cb','int']
131145

132-
class shlcbi(base.Instruction):
146+
class shlcbi(NonVectorInstruction):
133147
code = opcodes['SHLCBI']
134148
arg_format = ['cbw','cb','int']
135149

136-
class ldbits(base.Instruction):
150+
class ldbits(NonVectorInstruction):
137151
code = opcodes['LDBITS']
138152
arg_format = ['sbw','i','i']
139153

140-
class ldmsb(base.DirectMemoryInstruction, base.ReadMemoryInstruction):
154+
class ldmsb(base.DirectMemoryInstruction, base.ReadMemoryInstruction,
155+
base.VectorInstruction):
141156
code = opcodes['LDMSB']
142157
arg_format = ['sbw','int']
143158

144-
class stmsb(base.DirectMemoryWriteInstruction):
159+
class stmsb(base.DirectMemoryWriteInstruction, base.VectorInstruction):
145160
code = opcodes['STMSB']
146161
arg_format = ['sb','int']
147162
# def __init__(self, *args, **kwargs):
148163
# super(type(self), self).__init__(*args, **kwargs)
149164
# import inspect
150165
# self.caller = [frame[1:] for frame in inspect.stack()[1:]]
151166

152-
class ldmcb(base.DirectMemoryInstruction, base.ReadMemoryInstruction):
167+
class ldmcb(base.DirectMemoryInstruction, base.ReadMemoryInstruction,
168+
base.VectorInstruction):
153169
code = opcodes['LDMCB']
154170
arg_format = ['cbw','int']
155171

156-
class stmcb(base.DirectMemoryWriteInstruction):
172+
class stmcb(base.DirectMemoryWriteInstruction, base.VectorInstruction):
157173
code = opcodes['STMCB']
158174
arg_format = ['cb','int']
159175

160-
class ldmsbi(base.ReadMemoryInstruction):
176+
class ldmsbi(base.ReadMemoryInstruction, base.VectorInstruction):
161177
code = opcodes['LDMSBI']
162178
arg_format = ['sbw','ci']
163179

164-
class stmsbi(base.WriteMemoryInstruction):
180+
class stmsbi(base.WriteMemoryInstruction, base.VectorInstruction):
165181
code = opcodes['STMSBI']
166182
arg_format = ['sb','ci']
167183

@@ -185,15 +201,15 @@ class stmsdci(base.WriteMemoryInstruction):
185201
code = opcodes['STMSDCI']
186202
arg_format = tools.cycle(['cb','cb'])
187203

188-
class convsint(base.Instruction):
204+
class convsint(NonVectorInstruction1):
189205
code = opcodes['CONVSINT']
190206
arg_format = ['int','sbw','ci']
191207

192-
class convcint(base.Instruction):
208+
class convcint(NonVectorInstruction):
193209
code = opcodes['CONVCINT']
194210
arg_format = ['cbw','ci']
195211

196-
class convcbit(base.Instruction):
212+
class convcbit(NonVectorInstruction1):
197213
code = opcodes['CONVCBIT']
198214
arg_format = ['ciw','cb']
199215

@@ -222,18 +238,19 @@ def __init__(self, *args, **kwargs):
222238
super(split_class, self).__init__(*args, **kwargs)
223239
assert (len(args) - 2) % args[0] == 0
224240

225-
class movsb(base.Instruction):
241+
class movsb(NonVectorInstruction):
226242
code = opcodes['MOVSB']
227243
arg_format = ['sbw','sb']
228244

229245
class trans(base.VarArgsInstruction):
230246
code = opcodes['TRANS']
247+
is_vec = lambda self: True
231248
def __init__(self, *args):
232249
self.arg_format = ['int'] + ['sbw'] * args[0] + \
233250
['sb'] * (len(args) - 1 - args[0])
234251
super(trans, self).__init__(*args)
235252

236-
class bitb(base.Instruction):
253+
class bitb(NonVectorInstruction):
237254
code = opcodes['BITB']
238255
arg_format = ['sbw']
239256

@@ -245,20 +262,22 @@ class inputb(base.DoNotEliminateInstruction, base.VarArgsInstruction):
245262
__slots__ = []
246263
code = opcodes['INPUTB']
247264
arg_format = tools.cycle(['p','int','int','sbw'])
265+
is_vec = lambda self: True
248266

249-
class print_regb(base.IOInstruction):
267+
class print_regb(base.VectorInstruction, base.IOInstruction):
250268
code = opcodes['PRINTREGB']
251269
arg_format = ['cb','i']
252270
def __init__(self, reg, comment=''):
253271
super(print_regb, self).__init__(reg, self.str_to_int(comment))
254272

255-
class print_reg_plainb(base.IOInstruction):
273+
class print_reg_plainb(NonVectorInstruction, base.IOInstruction):
256274
code = opcodes['PRINTREGPLAINB']
257275
arg_format = ['cb']
258276

259277
class print_reg_signed(base.IOInstruction):
260278
code = opcodes['PRINTREGSIGNED']
261279
arg_format = ['int','cb']
280+
is_vec = lambda self: True
262281

263282
class print_float_plainb(base.IOInstruction):
264283
__slots__ = []

Compiler/GC/types.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ def malloc(cls, size):
7777
def n_elements():
7878
return 1
7979
@classmethod
80+
def mem_size(cls):
81+
return math.ceil(cls.n / cls.unit)
82+
@classmethod
8083
def load_mem(cls, address, mem_type=None, size=None):
8184
if size not in (None, 1):
8285
v = [cls.load_mem(address + i) for i in range(size)]
@@ -101,9 +104,8 @@ def __init__(self, value=None, n=None, size=None):
101104
def copy(self):
102105
return type(self)(n=instructions_base.get_global_vector_size())
103106
def set_length(self, n):
104-
if n > self.max_length:
105-
print(self.max_length)
106-
raise Exception('too long: %d' % n)
107+
if n > self.n:
108+
raise Exception('too long: %d/%d' % (n, self.n))
107109
self.n = n
108110
def set_size(self, size):
109111
pass
@@ -135,7 +137,7 @@ def __repr__(self):
135137
if self.n != None:
136138
suffix = '%d' % self.n
137139
if type(self).n != None and type(self).n != self.n:
138-
suffice += '/%d' % type(self).n
140+
suffix += '/%d' % type(self).n
139141
else:
140142
suffix = 'undef'
141143
return '%s(%s)' % (super(bits, self).__repr__(), suffix)
@@ -237,6 +239,7 @@ class sbits(bits):
237239
bitdec = inst.bitdecs
238240
bitcom = inst.bitcoms
239241
conv_regint = inst.convsint
242+
one_cache = {}
240243
@classmethod
241244
def conv_regint_by_bit(cls, n, res, other):
242245
tmp = cbits.get_type(n)()
@@ -285,14 +288,12 @@ def load_int(self, value):
285288
% (value, self.n))
286289
if self.n <= 32:
287290
inst.ldbits(self, self.n, value)
288-
elif self.n <= 64:
289-
self.load_other(regint(value, size=1))
290-
elif self.n <= 128:
291-
lower = sbits.get_type(64)(value % 2**64)
292-
upper = sbits.get_type(self.n - 64)(value >> 64)
293-
self.mov(self, lower + (upper << 64))
294291
else:
295-
raise NotImplementedError('more than 128 bits wanted')
292+
size = math.ceil(self.n / self.unit)
293+
tmp = regint(size=size)
294+
for i in range(size):
295+
tmp[i].load_int((value >> (i * 64)) % 2**64)
296+
self.load_other(tmp)
296297
def load_other(self, other):
297298
if isinstance(other, cbits) and self.n == other.n:
298299
inst.convcbit2s(self.n, self, other)
@@ -393,11 +394,10 @@ def __invert__(self):
393394
# res = type(self)(n=self.n)
394395
# inst.nots(res, self)
395396
# return res
396-
if self.n == None or self.n > self.unit:
397-
one = self.get_type(self.n)()
398-
self.conv_regint_by_bit(self.n, one, regint(1, size=self.n))
399-
else:
400-
one = self.new(value=self.long_one(), n=self.n)
397+
key = self.n, library.get_block()
398+
if key not in self.one_cache:
399+
self.one_cache[key] = self.new(value=self.long_one(), n=self.n)
400+
one = self.one_cache[key]
401401
return self + one
402402
def __neg__(self):
403403
return self
@@ -432,12 +432,12 @@ def popcnt(self):
432432
@classmethod
433433
def trans(cls, rows):
434434
rows = list(rows)
435-
if len(rows) == 1:
435+
if len(rows) == 1 and rows[0].n <= rows[0].unit:
436436
return rows[0].bit_decompose()
437437
n_columns = rows[0].n
438438
for row in rows:
439439
assert(row.n == n_columns)
440-
if n_columns == 1:
440+
if n_columns == 1 and len(rows) <= cls.unit:
441441
return [cls.bit_compose(rows)]
442442
else:
443443
res = [cls.new(n=len(rows)) for i in range(n_columns)]
@@ -452,6 +452,10 @@ def bit_adder(*args, **kwargs):
452452
@staticmethod
453453
def ripple_carry_adder(*args, **kwargs):
454454
return sbitint.ripple_carry_adder(*args, **kwargs)
455+
def to_sint(self, n_bits):
456+
bits = sbitvec.from_vec(sbitvec([self]).v[:n_bits]).elements()[0]
457+
bits = sint(bits, size=n_bits)
458+
return sint.bit_compose(bits)
455459

456460
class sbitvec(_vec):
457461
@classmethod
@@ -524,6 +528,8 @@ def __iter__(self):
524528
return iter(self.v)
525529
def __len__(self):
526530
return len(self.v)
531+
def __getitem__(self, index):
532+
return self.v[index]
527533
@classmethod
528534
def conv(cls, other):
529535
return cls.from_vec(other.v)

0 commit comments

Comments
 (0)