-
Notifications
You must be signed in to change notification settings - Fork 4
/
symbol.py
422 lines (371 loc) · 13.6 KB
/
symbol.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
from affft import *
from gen_xor_equation import *
class Bit:
counter = 0
def __init__(self):
self.id = self.new_id()
@classmethod
def new_id(cls):
id = cls.counter
cls.counter += 1
return id
def __repr__(self):
return "Bit_"+str(self.id)
class Var:
counter = 0
def __init__(self, num_bit):
self.bits = [Bit() for i in range(num_bit)]
self.id = self.new_id()
def __repr__(self):
return "Var_"+str(self.id)
@classmethod
def new_id(cls):
id = cls.counter
cls.counter += 1
return id
class Exp:
def __init__(self):
self.traverse = None
class Mul(Exp):
def __init__(self, var0, var1):
super().__init__()
self.var0 = var0
self.var1 = var1
def __repr__(self):
return str(self.var0) + " * " + str(self.var1)
class Mul_Const(Exp):
def __init__(self, var0, var1):
super().__init__()
self.var0 = var0
self.var1 = var1
def __repr__(self):
return str(self.var0) + " * " + str(self.var1)
class Add(Exp):
def __init__(self, var0, var1):
super().__init__()
self.var0 = var0
self.var1 = var1
def __repr__(self):
return str(self.var0) + " + " + str(self.var1)
class LowerHalf(Exp):
def __init__(self, var0):
super().__init__()
self.var0 = var0
def __repr__(self):
return "LowerHalf(" + str(self.var0)+ ")"
class UpperHalf(Exp):
def __init__(self, var0):
super().__init__()
self.var0 = var0
def __repr__(self):
return "UpperHalf(" + str(self.var0)+ ")"
class MergeHalf(Exp):
def __init__(self, var0, var1):
super().__init__()
self.var0 = var0
self.var1 = var1
def __repr__(self):
return "MergeHalf(" + str(self.var0)+ "," + str(self.var1) + ")"
class Identity(Exp):
def __init__(self, var0):
super().__init__()
self.var0 = var0
def __repr__(self):
return "Identity(" + str(self.var0)+ ")"
class Butterfly(Exp):
def __init__(self, var0, var1, var2):
super().__init__()
self.var0 = var0
self.var1 = var1
self.var2 = var2
def __repr__(self):
return "Butterfly(" + str(self.var0)+ "," + str(self.var1) + "," + str(self.var2) + ")"
def cse(in_length, out_length, linear_deps):
ncol = in_length
nrow = out_length
while(True):
order = list(range(ncol))
#random.shuffle(order)
hmax = 0
for ii in range(ncol):
for jj in range(ii+1,ncol):
i = order[ii]
j = order[jj]
hw = 0
for s in linear_deps:
if i in s and j in s:
hw += 1
if hw > hmax: # or (hw==hmax and random.randint(0,1)==0):
hmax = hw
maxi = i
maxj = j
if hmax > 1:
linear_deps.append({maxi,maxj})
for i in range(nrow):
s = linear_deps[i]
if maxi in s and maxj in s:
s.remove(maxi)
s.remove(maxj)
s.add(ncol)
ncol+=1
else:
break
return (ncol-in_length,linear_deps)
def gen_assign(out_var_name,in_var_name,xor_list):
assign_str = ""
assign_str += "{} = ".format(out_var_name)
equation = []
for j in sorted(list(xor_list)):
if j == -1:
equation.append("1")
else:
equation.append( str(in_var_name[j]))
assign_str += " ^ ".join(equation)
if len(equation) == 0 :
assign_str += "0"
assign_str += ";\n"
return assign_str
def gen_code(in_var_name ,in_length, out_var_name, out_length, linear_func, prefix=""):
ret = ""
linear_deps = get_linear_deps(in_length,out_length,linear_func)
ncs,linear_deps = cse(in_length,out_length,linear_deps)
cs_var = Var(ncs)
out_var_bits = out_var_name.bits + cs_var.bits
for i in list(range(out_length,out_length+ncs))+ list(range(out_length)):
ret += gen_assign(prefix + str(out_var_bits[i]),in_var_name + cs_var.bits,
linear_deps[i])
return ret
def exp_graph_to_eqs(exp,eqs):
if exp.__class__ == Var:
return exp
if exp.traverse is not None:
return exp.traverse
if exp.__class__ == Mul_Const:
a = exp_graph_to_eqs(exp.var0,eqs)
b = Var(len(a.bits))
eqs.append((b,Mul_Const(a,exp.var1)))
#TODO bits length maybe wrong because of mul constant
exp.traverse = b
return b
elif exp.__class__ == Add:
a = exp_graph_to_eqs(exp.var0,eqs)
b = exp_graph_to_eqs(exp.var1,eqs)
c = Var(len(a.bits))
eqs.append((c,Add(a,b)))
exp.traverse = c
return c
elif exp.__class__ == UpperHalf:
a = exp_graph_to_eqs(exp.var0,eqs)
b = Var(len(a.bits)//2)
eqs.append((b,UpperHalf(a)))
exp.traverse = b
return b
elif exp.__class__ == LowerHalf:
a = exp_graph_to_eqs(exp.var0,eqs)
b = Var(len(a.bits)//2)
eqs.append((b,LowerHalf(a)))
exp.traverse = b
return b
elif exp.__class__ == Mul:
a = exp_graph_to_eqs(exp.var0,eqs)
b = exp_graph_to_eqs(exp.var1,eqs)
c = Var(len(a.bits))
eqs.append((c,Mul(a,b)))
exp.traverse = c
return c
elif exp.__class__ == MergeHalf:
a = exp_graph_to_eqs(exp.var0,eqs)
b = exp_graph_to_eqs(exp.var1,eqs)
if len(a.bits)!=len(b.bits):
raise "NOT MERGE HALF!"
c = Var(len(a.bits)+len(b.bits))
eqs.append((c,MergeHalf(a,b)))
exp.traverse = c
return c
else:
raise
def eq_to_str(eqs):
code = ""
for lhs,rhs in eqs:
#print(lhs,rhs)
if rhs.__class__ == Mul_Const:
#code+="{} = mul({}, {})\n".format(lhs,rhs.var0,rhs.var1)
tmp = [i for i in rhs.var0.bits]
tmp += [0]*(len(lhs.bits)-len(tmp))
#print(tmp)
if len(lhs.bits)==2:
if rhs.var1 == 0:
code+="{} = 0;\n".format(lhs.bits[0])
code+="{} = 0;\n".format(lhs.bits[1])
if rhs.var1 == 1:
code+="{} = {}\n".format(lhs.bits[0],tmp[0])
code+="{} = {}\n".format(lhs.bits[1],tmp[1])
if rhs.var1 == 2:
code+="{} = {}\n".format(lhs.bits[0],tmp[1])
code+="{} = {} ^ {}\n".format(lhs.bits[1],tmp[0],tmp[1])
if rhs.var1 == 3:
code+="{} = {} ^ {}\n".format(lhs.bits[0],tmp[0],tmp[1])
code+="{} = {}\n".format(lhs.bits[1],tmp[0])
else:
code+=gen_code(rhs.var0.bits,len(rhs.var0.bits),lhs,len(lhs.bits),
lambda x:gf832_mul(x,rhs.var1))
elif rhs.__class__ == Butterfly:
#code+="{} = mul({}, {})\n".format(lhs,rhs.var0,rhs.var1)
#print(tmp)
c0 = rhs.var0
c1 = rhs.var1
hh0 = Add(c0,Mul_Const(c1,rhs.var2))
tmp = []
r = exp_graph_to_eqs(hh0,tmp)
hh1 = Var(len(c1.bits))
tmp.append((hh1,Add(r,c1)))
tmp.append((lhs,MergeHalf(r,hh1)))
tmp_code1 = eq_to_str(tmp)
c0 = rhs.var0
c1 = rhs.var1
hh1 = Add(c0,Mul_Const(c1,rhs.var2 ^ 1))
tmp = []
r = exp_graph_to_eqs(hh1,tmp)
hh0 = Var(len(c0.bits))
tmp.append((hh0,Add(r,c1)))
tmp.append((lhs,MergeHalf(hh0,r)))
tmp_code2 = eq_to_str(tmp)
l = len(lhs.bits)//2
def butterfly(x,w):
c0 = x&(2**l - 1)
c1 = x>>l
hh0 = gf832_mul(c1,w) ^ c0
hh1 = gf832_mul(c1,(w^1)) ^ c0
return (hh1<<l)|hh0
tmp_code3 = gen_code(rhs.var0.bits + rhs.var1.bits ,len(rhs.var0.bits)*2,lhs,l*2,
lambda x:butterfly(x,rhs.var2))
tmp_code1 = tmp_code1.replace(";","")
tmp_code2 = tmp_code2.replace(";","")
tmp_code3 = tmp_code3.replace(";","")
if tmp_code2.count("^")<tmp_code1.count("^"):
if tmp_code3.count("^")<tmp_code2.count("^"):
code+=tmp_code3
#code+=tmp_code2
else:
code+=tmp_code2
else:
if tmp_code3.count("^")<tmp_code1.count("^"):
code+=tmp_code3
#print(">>>>>>")
#print(tmp_code3)
#print("=====")
#print(tmp_code1)
#print("<<<<<<")
#print(tmp_code3)
#print(tmp_code3.count("^"),tmp_code1.count("^"),tmp_code2.count("^"))
#code+=tmp_code1
else:
code+=tmp_code1
#[ print(i) for i in tmp]
elif rhs.__class__ == Add:
#code+="{} = {} ^ {}\n".format(lhs,rhs.var0,rhs.var1)
for i,v in enumerate(lhs.bits):
if i<len(rhs.var0.bits):
if i<len(rhs.var1.bits):
code+="{} = {} ^ {}\n".format(lhs.bits[i],rhs.var0.bits[i],rhs.var1.bits[i])
else:
code+="{} = {}\n".format(lhs.bits[i],rhs.var0.bits[i])
else:
if i<len(rhs.var1.bits):
code+="{} = {}\n".format(lhs.bits[i],rhs.var1.bits[i])
else:
code+="{} = 0\n".format(lhs.bits[i])
#if len(rhs.var0.bits) != len(rhs.var1.bits):
# print(len(rhs.var0.bits), len(rhs.var1.bits))
elif rhs.__class__ == UpperHalf:
for i,v in enumerate(lhs.bits):
code+="{} = {}\n".format(lhs.bits[i],rhs.var0.bits[i+len(lhs.bits)])
elif rhs.__class__ == LowerHalf:
for i,v in enumerate(lhs.bits):
code+="{} = {}\n".format(lhs.bits[i],rhs.var0.bits[i])
elif rhs.__class__ == Mul:
var0 = rhs.var0
var1 = rhs.var1
if len(lhs.bits) == 1:
code+="{} = {} & {}\n".format(lhs.bits[0],var0.bits[0],var1.bits[0])
if len(lhs.bits) == 2:
a = rhs.var0
a0 = LowerHalf(a)
a1 = UpperHalf(a)
b = rhs.var1
b0 = LowerHalf(b)
b1 = UpperHalf(b)
ab0=Mul(a0,b0)
ab1 = Add(Mul(a1,b0),Mul(a0,b1))
ab2 = Mul(a1,b1)
ab0 = Add(ab0,ab2)
ab1 = Add(ab1,ab2)
tmp = []
r = exp_graph_to_eqs(MergeHalf(ab0,ab1),tmp)
#[ print(i) for i in tmp]
tmp.append((lhs,Identity(r)))
code+=eq_to_str(tmp)
if len(lhs.bits) == 4:
a = rhs.var0
a0 = LowerHalf(a)
a1 = UpperHalf(a)
b = rhs.var1
b0 = LowerHalf(b)
b1 = UpperHalf(b)
a0b0=Mul(a0,b0)
a1b1=Mul(a1,b1)
a0a1xb0b1_a0b0 = Add(Mul(Add(a0,a1),Add(b0,b1)),a0b0)
rd0=Mul_Const(a1b1,2)
a0b0 = Add(a0b0,rd0)
merge = MergeHalf(a0b0,a0a1xb0b1_a0b0)
tmp = []
r = exp_graph_to_eqs(merge,tmp)
#[ print(i) for i in tmp]
tmp.append((lhs,Identity(r)))
code+=eq_to_str(tmp)
if len(lhs.bits) == 8:
a = rhs.var0
a0 = LowerHalf(a)
a1 = UpperHalf(a)
b = rhs.var1
b0 = LowerHalf(b)
b1 = UpperHalf(b)
a0b0 = Mul(a0,b0)
ab = Mul(Add(a0,a1),Add(b0,b1))
a1b1 = Mul(a1,b1)
a0b1_a1b0_a1b1 = Add(ab,a0b0)
a1b10x8 = Mul_Const(a1b1,8)
merge = MergeHalf(Add(a1b10x8,a0b0),a0b1_a1b0_a1b1)
tmp = []
r = exp_graph_to_eqs(merge,tmp)
tmp.append((lhs,Identity(r)))
code+=eq_to_str(tmp)
if len(lhs.bits) == 16:
a = rhs.var0
a0 = LowerHalf(a)
a1 = UpperHalf(a)
b = rhs.var1
b0 = LowerHalf(b)
b1 = UpperHalf(b)
a0b0 = Mul(a0,b0)
ab = Mul(Add(a0,a1),Add(b0,b1))
a1b1 = Mul(a1,b1)
a0b1_a1b0_a1b1 = Add(ab,a0b0)
a1b10x8 = Mul_Const(a1b1,0x80)
merge = MergeHalf(Add(a1b10x8,a0b0),a0b1_a1b0_a1b1)
tmp = []
r = exp_graph_to_eqs(merge,tmp)
tmp.append((lhs,Identity(r)))
code+=eq_to_str(tmp)
elif rhs.__class__ == Identity:
for i,v in enumerate(lhs.bits):
code+="{} = {}\n".format(lhs.bits[i],rhs.var0.bits[i])
elif rhs.__class__ == MergeHalf:
for i,v in enumerate(rhs.var0.bits):
code+="{} = {}\n".format(lhs.bits[i],rhs.var0.bits[i])
for i,v in enumerate(rhs.var0.bits):
code+="{} = {}\n".format(lhs.bits[i+len(rhs.var0.bits)],rhs.var1.bits[i])
else:
print(rhs.__class__)
raise
return code