-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposeidon.py
More file actions
197 lines (185 loc) · 10 KB
/
poseidon.py
File metadata and controls
197 lines (185 loc) · 10 KB
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
import numpy as np
from numpy import array
from field import FieldElement
import math
ARC = [array([457162718, 1515798002, 329992198, -740952278, 843128639, 489990171,
-485198517, 1047819201], dtype=object),
array([708071139, 1491854281, 416774859, 774602014, -1254191346,
-1197422310, 864502411, -600058068], dtype=object),
array([-1485016750, 730870515, -1501710322, 1589430865, 690540300,
-523480551, -1317946135, -922452154], dtype=object),
array([1456242056, 257294687, -1516422369, -1297022742, -524982363,
366953150, 1035045094, 402379448], dtype=object),
array([1386495431, 1285006923, -971347576, -1242776413, 209341693,
288870120, 960434083, 1362502680], dtype=object),
array([-1292891431, 923126837, -404910730, 204010009, 1397507599,
1392895535, -888912834, -71498560], dtype=object),
array([-1437195216, -891665806, 157306807, 673158506, 1271676333,
146530699, -95231060, 1102286627], dtype=object),
array([1098934264, 1082578544, 976388969, 479337709, -217162316,
409679738, 614404648, -1375480354], dtype=object),
array([1411106875, -1550314502, -1361693272, -1001068560, 198027268,
-477794721, 371592721, 246865617], dtype=object),
array([1153335920, -1251551497, 1464419966, 809704084, -1028447447,
-582658003, -500977600, -1281913215], dtype=object),
array([646889709, 404974679, -1165303492, -330438128, -592318748,
-1462906, -455464748, 1475484901], dtype=object),
array([855631586, 111097201, -476733825, -899228804, -534403651,
621975577, 1548736799, -1148749739], dtype=object),
array([-614794889, -1367849135, 1610079799, -124641493, 1083917197,
-303266016, -942989698, -1197468993], dtype=object),
array([820663393, -21050605, -1348457814, -271481275, -859464903,
-1079713932, 95348584, -1489911027], dtype=object),
array([-720543588, -1508407513, -1003611416, 649599897, 854693896,
-723434705, 325385372, 485355135], dtype=object),
array([853186883, 1139502291, 934662668, -224123557, -1183517638,
-406013102, -1342436066, -230804006], dtype=object),
array([-1547173995, 852986636, -943371805, 1250915897, -185652539,
-355667659, -687454570, 1348698933], dtype=object),
array([-982935529, 18104664, -147175836, 1568399757, 1002512844,
-309153589, -486032121, 1238444880], dtype=object),
array([-327040048, 1297059675, -1540331630, 533099574, -1036464697,
1536249350, -206910547, 388691460], dtype=object),
array([1001540836, 1296138654, 165833012, 113002783, -860126424,
1453097620, 21884016, 926488598], dtype=object),
array([698275306, 106876587, 1348714916, -438932996, -135647319,
-1319375741, 1555108161, 240974697], dtype=object),
array([93422285, -1348934712, -914486039, -556971828, 39485853,
-582801882, 509757633, -531506394], dtype=object),
array([79028655, 632940727, -1569028690, 1079405823, 24820361, 839528628,
1046571043, 1530261171], dtype=object),
array([1325627818, -1247456906, 1373061097, -324023198, 766643165,
460885793, -1143473431, -407532365], dtype=object),
array([-515666847, 1397894834, 301488213, 1215324033, 247415935,
-1041546930, 187316200, -778384549], dtype=object),
array([926006522, 352730153, 1515141863, 112000064, 1118108698,
-1350013259, 1418996455, 477951638], dtype=object),
array([157956394, -131193321, 1590116230, -676669845, 379937356,
-833162530, -580659573, -939643712], dtype=object),
array([611292051, 1345529405, 1504505007, -394911044, -224482550,
-1519818340, 1498097790, -342023862], dtype=object),
array([-566152905, -628865670, -282568758, -701668881, -1256095901,
189431982, -1302357527, -1585065370], dtype=object),
array([-1393980507, 891023348, 1195304018, -836819359, -754214649,
413433550, 1276384020, -898567263], dtype=object),
array([491427365, -955487210, 388100505, 545084640, 1577520977,
-210779466, 262465403, 968351927], dtype=object),
array([-171540250, -1517413376, -593582727, 900646391, 1463351536,
-860840686, 1006827890, 1318350918], dtype=object),
array([-1136748496, -1264889446, -1062380316, 427999599, -301130501,
-1531285741, -302314921, -917134908], dtype=object),
array([1032077010, -572048657, 477898453, 795872689, 283723875,
-1073434710, -1230635962, -314466727], dtype=object),
array([1051800163, -1496091902, 373715520, 1234819090, -1149652576,
229024434, -161659552, 1497746505], dtype=object),
array([-142093449, 1602598951, -936940162, -1158452145, 101859518,
647518125, 720910307, -1290309050], dtype=object),
array([-225207608, -1590584422, -113152671, -750720650, -1179927664,
1361215021, -267944624, -609789513], dtype=object),
array([375363302, 617551499, -260797974, -326658953, -553493034,
988403153, 1075805130, 599396518], dtype=object),
array([948970405, -827001704, 1525325995, 1261903669, 122790668, 25937974,
-426100579, 85352229], dtype=object),
array([1122110791, 640754255, -781437433, 96853127, -853713799,
-865200615, 1049573816, 248595763], dtype=object),
array([99346742, 484276034, 830982279, 1014672696, -45982633, 1172541303,
-466914172, 929064661], dtype=object),
array([-368834384, -1586483944, 1325149443, 1394070421, -1111233074,
-819726735, -545268569, 1059067371], dtype=object),
array([-1243643182, 493485057, -1500598241, -1040128614, -343676861,
-836697856, 1423879617, -625143777], dtype=object),
array([-253362557, -705638438, 579955567, -926229474, 38954401, 88991943,
-119720331, 1296087987], dtype=object),
array([553462551, -1150212945, -717687590, 141475708, 1565894075,
-1392421471, 707876416, 852747777], dtype=object),
array([-422208050, -1123332497, 182591695, 1135797946, -1502994342,
-487047105, -99318075, 252629453], dtype=object),
array([-881842669, -1493753758, -695348276, 1054044664, -1160768175,
-770476510, 582068998, 680727403], dtype=object),
array([-1536609383, 1062499627, -1556507278, 104439046, 1296441893,
-378461877, -541905534, -133210423], dtype=object),
array([558461998, -1541487073, -26342084, -1374691478, -1584380212,
-839349406, -125213754, -1271291911], dtype=object),
array([708655414, 1037094079, 180243522, 178693894, 1192763165, 602403147,
619904021, -804790097], dtype=object),
array([436098314, -1011449728, 857580308, -1181431992, -1524389392,
-559123646, 1295293741, -342955133], dtype=object),
array([-14584547, 991591680, -1056012213, -578175934, 1176651215,
-442502771, 1251425030, 985487268], dtype=object),
array([-1468433126, -1607445530, 1152456015, -1060562580, 829706690,
-2243439, 780611233, 795140440], dtype=object),
array([-1606809436, -1258607811, 1204760531, 1210572533, -316145555,
1441449078, -532377706, 286635753], dtype=object),
array([-230687935, -1578558401, 736883342, 865429572, 43666878,
-862185625, 1515818233, -266897873], dtype=object),
array([-618699276, 754258477, -606329694, -204449552, 1400288519,
801648255, -1198106371, 340893552], dtype=object),
array([232359651, -1036501657, -1422870277, -202570273, -1294930403,
1368837276, 1538092150, -1121402395], dtype=object),
array([-567891276, 1475331579, -595490768, 1536360822, 1055839652,
637358940, -781199590, -1182284081], dtype=object),
array([-1205522660, -841047749, -1262705423, 1597269369, 1392077739,
604446027, 1352562651, -655129775], dtype=object),
array([567948973, 843243731, -1183125933, 463574300, 1102130262,
-1432389393, 1205684263, -1088407904], dtype=object),
array([-810716302, 368926484, -911995159, -838758940, -1379326030,
634335577, 655492736, -572544235], dtype=object),
array([181105951, 1475514653, -1477228982, -696630351, -279155284,
-76018337, -522964931, -218956597], dtype=object),
array([-1183368050, 85257829, 241486067, -186929267, -446053955,
786747505, 1071154291, 1450190132], dtype=object),
array([-380820367, -4200961, 84354881, -1027598722, 1341252860, 621381947,
-1394076465, 541366403], dtype=object),
array([-113476487, -595677826, -71738811, -1453949251, 1231728448,
-679071636, -907169194, 1172815745], dtype=object)]
class Sponge():
def __init__(self, rate, capacity, permutation, r_f, r_p, rc):
self.r = rate
self.c = capacity
self.pi = permutation
self.state = None
self.r_f = r_f
self.r_p = r_p
self.mds = generate_cauchy_matrix(rate+capacity)
self.rc = ARC
self.trace = []
def apply(self,data):
self.state = np.array([FieldElement(0) for i in range(0,self.c+self.r)])
self.trace = [self.state]
#remaining = self.pad(remaining)
chunks = [data[self.r*i:self.r*(i+1)] for i in range(0,len(data)//self.r)]
i = 0
while(len(chunks) > 0):
i+=1
self.state, trace = self.pi(self.state + np.pad(chunks[0], (0,self.c), mode='constant'), self.r_f, self.r_p, self.mds, self.rc)
chunks = chunks[1:]
self.trace.extend(trace)
return self.state[0:self.c], self.trace
def generate_cauchy_matrix(t):
x = [FieldElement(e) for e in range(0,t)]
y = [FieldElement(e) for e in range(t, 2*t)]
return np.array([[FieldElement(1)/(x[i]-y[j]) for j in range(0,len(y))] for i in range(0,len(x))])
def arc(state, rc):
return np.array(state) + rc
def mix(state, mds):
return mds @ np.array(state)
def full_s_box(state):
return np.array(state) ** 5
def partial_s_box(state):
return [state[0] ** 5, *state[1:]]
def hades_round_permutation(state, r_f, r_p, mds, rc):
trace = []
for r in range(0,r_f+r_p):
state = arc(state, rc[r])
if(r_f/2 <= r < r_f/2+r_p):
state = partial_s_box(state)
else:
state = full_s_box(state)
state = mix(state, mds)
trace.append(state)
return state, trace
class Poseidon():
def __init__(self):
self.sponge = Sponge(7,1,hades_round_permutation,8,57, ARC)
def hash(self, data):
return self.sponge.apply(data)