-
Notifications
You must be signed in to change notification settings - Fork 5
/
polyr.py
242 lines (224 loc) · 12.2 KB
/
polyr.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
"""
polyr.py
Copyright (c) 2023 Raccoon Signature Team. See LICENSE.
=== Number Theoretic Transforms: Polynomial Ring Arithmetic.
"""
RACC_N = 512 # degree n
RACC_Q = 549824583172097 # (63 * 2**18 + 1) * (127 * 2**18 + 1)
RACC_NI = 548750707033089 # n^-1 (mod q)
# These constants can be calculated with _calc_w() in test_ntt.py
RACC_W = [ 1, 470718232853389, 11692956810271, 169624605792594,
443988556913998, 32836745845391, 30726030888888, 351640061790432,
545272569051008, 48390000295466, 254666751490752, 217375765609799,
238966215688888, 166269360706841, 255019738295721, 285804717594966,
346208067933352, 401520844982723, 233239303635666, 541016418062861,
19300758072423, 38362090537789, 44485968533127, 220414929121067,
315678945994797, 144225082187849, 32960899052263, 375710787005058,
187644456811731, 394897347557658, 265491339396023, 83596053585948,
476550616242972, 107573236949780, 171227281312680, 143008167125462,
46673662536089, 204146011959692, 340945652121467, 263971468643996,
507955734349325, 331853864505628, 115638851235467, 395461911112308,
549206457764026, 506421801950985, 142897702317494, 401479244313772,
232776779574807, 425736009385047, 22433064922577, 482647518635690,
39901204103516, 426110851766897, 258247064910301, 139522184118989,
231284989313893, 379271096732099, 259772980211050, 138933938786427,
127538891770625, 390225434405475, 330503265440705, 94836486254118,
231917586526577, 404536503825419, 87558635709546, 260463893069332,
548440526640682, 312892929498034, 279091343680297, 120096566397312,
433437613336070, 464347399528726, 538995537331298, 96618146128529,
146957345488932, 11924365918116, 259789907054381, 330433603886370,
339231383491058, 17344855893946, 249279642868950, 482322134457230,
202823501374082, 278966637990903, 215750115594835, 295270368120909,
421656731636837, 197816947104079, 365558191256571, 226503213869618,
236451035148112, 533275911386710, 38883498425593, 386919453802296,
159303319662963, 437482997429307, 44863592604307, 153575464089326,
243972216117820, 304465986148927, 96304470410975, 135094939420918,
332689349225871, 343630257253939, 320811105176862, 163546698642700,
378025220890997, 387996091067830, 200273856383478, 499687368537592,
286524399320609, 240855976753218, 429260593451043, 36389829488736,
27543848364247, 509106615831019, 452588110797931, 178324837030901,
479396316302362, 276421039759174, 151274871587976, 51567150360204,
448346641687733, 144511313103896, 160184286378962, 530197208096127,
105990352635328, 468768367162141, 227709457427946, 455762895247423,
253492110833015, 502024569038649, 212813931873366, 97118240377769,
69493961238691, 173732260588459, 132117310225741, 398926869514463,
489382008736086, 482375741653014, 397565599387813, 452946975897740,
264874316195503, 147713906417358, 4698302164142, 527110960165805,
73787228599916, 138772725867404, 71035104537676, 186202221873852,
471008470245637, 203338628919065, 64789479163269, 262738735674708,
538077036313333, 317497292090328, 440179637933651, 449775587285980,
132716111504099, 102591394209850, 104225917706286, 210149052554234,
81658564883933, 241553415491351, 517237708063648, 463111827676046,
142199410916681, 139131876293840, 48379047209920, 285462858841029,
345619880470912, 168201172499201, 465549233237869, 242914515901602,
295056373699309, 8014942883325, 109973442872078, 222069859039777,
380480265491707, 254841796609859, 231958898197261, 499174739531706,
508948499247490, 168718141981110, 194002063394876, 228217071655405,
464628298795222, 401040971170279, 103606255990835, 105452779952672,
495552876251692, 491445973749310, 376237235858756, 541774795280391,
375712775231172, 23705476192857, 116916891417545, 356421147626109,
300594287295539, 466338284337019, 409522315522665, 129790247592088,
60313727155472, 360106789084871, 88035064290282, 112097319419665,
439178412407738, 462202386851657, 102383486005007, 115602278931759,
405405461813917, 152158679192131, 132770838685582, 527598490509223,
56195071689779, 72394938829644, 241426551323751, 377775649653758,
431936477189201, 489850169079800, 370075394350590, 37133423866950,
426467718536314, 546169080247045, 527213436280340, 347178002694219,
343079214711726, 418820131729305, 160295444495122, 38640684573610,
74610727673084, 458820587169721, 95721654794469, 394502719387301,
475415924627804, 342672472663314, 420110567094801, 469911345328696,
72907481819066, 360800496480429, 543983599757563, 500499710042973,
383442584771739, 132476030733073, 487433793914270, 28618813569766,
366113211404988, 134155165286584, 113043172587166, 444247537613478,
235546736441588, 182599959569284, 210002744510400, 242205441457960,
358453792785495, 107340173532191, 137921474328098, 230869417290309,
185621575752219, 95861695293393, 13319030242879, 91239479586056,
325088919123932, 164308157238504, 401176224677202, 339829097876420,
545719555804288, 207124965359185, 401407284557514, 165387177313636,
317504746319203, 320442559392760, 471627948421648, 251836299699697,
137490201724449, 536128007848089, 535073554288746, 84987772078781,
440229835833881, 341339647624964, 387589769966455, 355447381117347,
472810548128504, 99755800274422, 88945718061941, 14561686069490,
195558687827532, 520702388006422, 468705882934587, 511373813961295,
378757719370728, 294645681725519, 253180485219617, 474883248872029,
229244657504577, 34551917008273, 360638868246814, 414636844251930,
348854490550871, 380782894760197, 324201992101049, 335755643091919,
53072375467547, 408511600810840, 332328633447736, 500733774401513,
431030263341357, 236377814097742, 493792294593845, 313338207706548,
236065417035015, 110258202630593, 538929724520487, 70962605754406,
453228288142436, 45754937073660, 204357314650072, 294371020604466,
114643539513201, 59015865515392, 263551163432683, 113902766581756,
168059414396372, 240101455387405, 445932372099090, 279987331894357,
383333885560334, 34826572561148, 351008136025866, 355898803976412,
137099103425598, 327030628407298, 61523998152642, 485800348258106,
90043532023550, 246136841996737, 190557934720653, 337957607660742,
192510374954830, 152152195689293, 14688418821496, 261247910849614,
75555632713678, 63509562184269, 542634707057623, 449045578054619,
39090821886498, 131615643942324, 59825727024961, 24921223948079,
453435450183758, 261777247259375, 536442672947138, 533586458086653,
515125935843774, 293239295486404, 227495255036045, 163385592764659,
253475986073724, 346535660170881, 73082408446744, 465825709602133,
542469339325613, 511502505355777, 432356434210900, 47955010423774,
65722039993910, 307023421113992, 371118234723170, 32623879453803,
36601478908797, 316124539338223, 367935034292394, 19789208152386,
103571856461643, 254482856273413, 221334538614805, 227958926879385,
300453922116292, 117841689365672, 1148351070060, 355345214759587,
276277488735297, 280456491564896, 416296490421318, 51891562856801,
400082347691777, 291879854465485, 194528311530861, 527392318439518,
340021607365896, 51302818806726, 62874149441107, 157569291455966,
127990229424856, 391059177011430, 383557907902760, 368726182982534,
16258017201572, 298050229670261, 444231940306599, 49628836364847,
80842866187348, 482556879796621, 481142494981554, 434535456882033,
275662452182603, 487799562382861, 467409284879901, 425082936971590,
345181336829982, 527406882880421, 21514694144079, 441538645838304,
428248412696843, 428818773033045, 475784836919417, 422887542560250,
48924541576647, 44178002839227, 530269646567288, 457311713914675,
443341362442823, 528032860087446, 494603916640591, 483453338142402,
515458245738555, 281212887100215, 252165282538670, 157158791902730,
304829884633795, 64303679173069, 523753050226429, 436519702825287,
196940683166278, 157326701181732, 226167988424166, 361159100036024,
116137345524154, 354453642230694, 4268935843859, 506320774702581,
399018132738711, 511748566405189, 336385541216517, 59068446460136,
286756910630583, 83251213323330, 136319039446904, 241791134571733,
283253381696095, 329614334680705, 449742592582309, 534499679629608,
322368123631369, 273769739240401, 193218037538978, 277687889440735,
184089251753197, 402109066266663, 102723317481121, 219877708533774,
64945408496206, 158822078578062, 461060719403022, 159962511354131,
24950699755960, 423890873631485, 126804684182220, 165378130975867,
318860144914289, 106286823933542, 261194197516321, 464359728987909,
194894271486062, 292944636249907, 93156068783182, 285783662891743,
195390130827712, 113211707643670, 94943455138466, 501287721614283,
342341233379669, 4591454169286, 124501828259940, 498366100956155,
371695792715041, 547077885205037, 3590443120869, 358995244900891,
126315814836548, 537990190881158, 41184774294973, 330885597084157,
298387722333847, 85662527435343, 333651491191760, 456131120517616,
427076078762813, 95573837960663, 39569327955950, 147943328386625,
147065038336829, 178960072186312, 334648381969512, 269936308065607,
153164625260099, 331139029872281, 123678867019268, 510554661377924, ]
### Internal functions: Polynomial arithmetic
def ntt(f, n=RACC_N, w=RACC_W, q=RACC_Q):
"""Forward NTT (negacyclic - x^n+1.) Note: Transforms f in place."""
l = n // 2
wi = 0
while l > 0:
for i in range(0, n, 2 * l):
wi += 1
z = w[wi]
for j in range(i, i + l):
x = f[j]
y = (f[j + l] * z) % q
f[j] = (x + y) % q
f[j + l] = (x - y) % q
l >>= 1
return f
def intt(f, n=RACC_N, w=RACC_W, ni=RACC_NI, q=RACC_Q):
"""Inverse NTT (negacyclic - x^n+1.) Note: Transforms f in place."""
l = 1
wi = n
while l < n:
for i in range(0, n, 2 * l):
wi -= 1
z = w[wi]
for j in range(i, i + l):
x = f[j]
y = f[j + l]
f[j] = (x + y) % q
f[j + l] = (z * (y - x)) % q
l <<= 1
# normalize: ni = n^-1 (mod q)
for i in range(n):
f[i] = (ni * f[i]) % q
return f
def mat_ntt(m):
"""NTT on a matrix-like object of polynomials (in place)."""
for m_i in m:
for m_ij in m_i:
ntt(m_ij)
return m
def mat_intt(m):
"""NTT^-1 on a matrix-like object of polynomials (in place)."""
for m_i in m:
for m_ij in m_i:
intt(m_ij)
return m
def mul_ntt(f, g, q=RACC_Q):
"""Multiplication of two polynomials (NTT domain.)"""
return [(fi * gi) % q for fi,gi in zip(f,g)]
def poly_add(f, g, q=RACC_Q):
"""Add polynomials: return f + g (mod q)."""
return [ (fi + gi) % q for fi,gi in zip(f,g) ]
def poly_sub(f, g, q=RACC_Q):
"""Add polynomials: return f - g (mod q)."""
return [ (fi - gi) % q for fi,gi in zip(f,g) ]
def poly_lshift(f, u, q=RACC_Q):
"""Left shift: Multiply coefficients by 2^u."""
return [ (x << u) % q for x in f ]
def poly_rshift(f, u, q=RACC_Q):
"""Right shift: Rounding divide by 2^u (in place)."""
mid = 1 << (u - 1)
return [ ((x + mid) >> u) % q for x in f ]
def poly_center(f, q=RACC_Q):
"""Center the modular coefficients of a polynomial around 0."""
mid = q >> 1
return [ ((fi + mid) % q) - mid for fi in f ]
def mul_mat_vec_ntt(m, v):
"""Multiply NTT domain k*ell matrix "m" with column vector v."""
k = len(m)
ell = len(m[0])
r = [[0] * RACC_N for _ in range(k)]
for i in range(k):
for j in range(ell):
r[i] = poly_add(r[i], mul_ntt(m[i][j], v[j]) )
return r
def mul_mat_mvec_ntt(m, v):
"""Multiply NTT domain k*ell matrix "m" with masked column vector v."""
k = len(m)
ell = len(m[0])
d = len(v[0])
r = [[[0] * RACC_N for _ in range(d)] for _ in range(k)]
for i in range(k):
for j in range(ell):
for i_d in range(d):
r[i][i_d] = poly_add(r[i][i_d],
mul_ntt(m[i][j], v[j][i_d]))
return r