-
Notifications
You must be signed in to change notification settings - Fork 0
/
poly_Rq_mul.c
408 lines (326 loc) · 14.2 KB
/
poly_Rq_mul.c
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
#include "poly_Rq_mul.h"
void Toep_3x3x3x2x2x8(uint16_t *toep, uint16_t *vec, uint16_t *r);
void Toep_3x3x2x2x8 (uint16_t *toep, uint16_t *vec, uint16_t *r);
void Toep_3x2x2x8 (uint16_t *toep, uint16_t *vec, uint16_t *r);
void Toep_2x2x8 (uint16_t *toep, uint16_t *vec, uint16_t *r);
void Toep_2x8 (uint16_t *toep, uint16_t *vec, uint16_t *r);
void TMVP_8x8_on_8 (uint16_t *toep, uint16_t *vec, uint16_t *r);
void poly_Rq_mul(poly *r, poly *a, poly *b) {
// Convert coefficients of a into the anti-diagonal of 32x32 Toep matrix:
// toep_a = {0, ...(43)..., 0, a_821, ..., a_1, a_0, a_821, ..., a_1, a_0, 0, ...(43)..., 0}
uint16_t toep_a[216*SIZE] = {0}; // initialized to zeros
// vec_b
uint16_t vec_b[108*SIZE] = {0};
for (uint16_t i=0; i < NTRU_N; i++){
toep_a[108*SIZE - 1 - i] = a->coeffs[i];
toep_a[108*SIZE - 1 + NTRU_N - i] = a->coeffs[i];
vec_b[i] = b->coeffs[i];
}
// perform the multiplication
uint16_t result[108*SIZE];
Toep_3x3x3x2x2x8(toep_a, vec_b, result);
// modulo 4096
for (uint16_t i=0; i<108*SIZE; i++){
result[i] = result[i] & 4095;
}
// extract the answer part
for (uint16_t i=0; i<NTRU_N; i++){
r->coeffs[i] = result[i];
}
return ;
}
void Toep_3x3x3x2x2x8(uint16_t *toep, uint16_t *vec, uint16_t *r){
// a is the anti-diagonal elements of a 108 SIZE x 108 SIZE toep, consists of 216 SIZE elements. (In fact the last one is of no use)
// b is the vector to be multiplied with, consists of 108 SIZE elements
// r is the result vector
uint16_t toep_m2[72*SIZE];
uint16_t toep_m1[72*SIZE];
uint16_t toep_0[72*SIZE];
uint16_t toep_p1[72*SIZE];
uint16_t toep_p2[72*SIZE];
uint16_t toep_0_m1_m2[72*SIZE];
uint16_t toep_p1_0_m1[72*SIZE];
uint16_t toep_p2_p1_0[72*SIZE];
uint16_t vec_0[36*SIZE];
uint16_t vec_1[36*SIZE];
uint16_t vec_2[36*SIZE];
uint16_t vec_0_minus_1[36*SIZE];
uint16_t vec_1_minus_2[36*SIZE];
uint16_t vec_2_minus_0[36*SIZE];
uint16_t intermediate_0[36*SIZE]={0};
uint16_t intermediate_1[36*SIZE]={0};
uint16_t intermediate_2[36*SIZE]={0};
uint16_t intermediate_3[36*SIZE]={0};
uint16_t intermediate_4[36*SIZE]={0};
uint16_t intermediate_5[36*SIZE]={0};
// Block Matrix form of Toeplitz matrix
for (uint16_t i=0; i<72*SIZE; i++){
// i=0,1,...,72xSIZE-1
toep_p2[i]=toep[i ];
toep_p1[i]=toep[i + 36*SIZE];
toep_0[i] =toep[i + 72*SIZE];
toep_m1[i]=toep[i + 108*SIZE];
toep_m2[i]=toep[i + 144*SIZE];
//toep_0_m1_m2[i] = toep_0[i] + toep_m1[i] + toep_m2[i];
//toep_p1_0_m1[i] = toep_p1[i] + toep_0[i] + toep_m1[i];
//toep_p2_p1_0[i] = toep_p2[i] + toep_p1[i] + toep_0[i];
}
uint16x8_t vtoep_m2, vtoep_m1, vtoep_0, vtoep_p1, vtoep_p2;
for (uint16_t i=0; i<72; i++){
vtoep_m2 = vld1q_u16(&toep_m2[i * SIZE]);
vtoep_m1 = vld1q_u16(&toep_m1[i * SIZE]);
vtoep_0 = vld1q_u16(&toep_0 [i * SIZE]);
vtoep_p1 = vld1q_u16(&toep_p1[i * SIZE]);
vtoep_p2 = vld1q_u16(&toep_p2[i * SIZE]);
vst1q_u16(&toep_0_m1_m2[i * SIZE], vaddq_u16(vtoep_0 , vaddq_u16(vtoep_m1, vtoep_m2)));
vst1q_u16(&toep_p1_0_m1[i * SIZE], vaddq_u16(vtoep_p1, vaddq_u16(vtoep_0 , vtoep_m1)));
vst1q_u16(&toep_p2_p1_0[i * SIZE], vaddq_u16(vtoep_p2, vaddq_u16(vtoep_p1, vtoep_0 )));
}
for (uint16_t i=0; i<36*SIZE; i++) {
vec_0[i] = vec[i ];
vec_1[i] = vec[i + 36*SIZE];
vec_2[i] = vec[i + 72*SIZE];
vec_0_minus_1[i] = vec_0[i] - vec_1[i];
vec_1_minus_2[i] = vec_1[i] - vec_2[i];
vec_2_minus_0[i] = vec_2[i] - vec_0[i];
}
Toep_3x3x2x2x8(toep_0_m1_m2, vec_2, intermediate_0);
Toep_3x3x2x2x8(toep_p1_0_m1, vec_1, intermediate_1);
Toep_3x3x2x2x8(toep_p2_p1_0, vec_0, intermediate_2);
Toep_3x3x2x2x8(toep_0, vec_2_minus_0, intermediate_3);
Toep_3x3x2x2x8(toep_p1, vec_0_minus_1, intermediate_4);
Toep_3x3x2x2x8(toep_m1, vec_1_minus_2, intermediate_5);
// Recombination
for (uint16_t i=0; i<36*SIZE; i++){
r[i ] = intermediate_0[i] - intermediate_3[i] + intermediate_5[i];
r[i + 36*SIZE] = intermediate_1[i] - intermediate_5[i] + intermediate_4[i];
r[i + 72*SIZE] = intermediate_2[i] - intermediate_4[i] + intermediate_3[i];
}
}
void Toep_3x3x2x2x8(uint16_t *toep, uint16_t *vec, uint16_t *r){
// a is the anti-diagonal elements of a 36 SIZE x 36 SIZE toep, consists of 72 SIZE elements. (In fact the last one is of no use)
// b is the vector to be multiplied with, consists of 36 SIZE elements
// r is the result vector
uint16_t toep_m2[24*SIZE];
uint16_t toep_m1[24*SIZE];
uint16_t toep_0[24*SIZE];
uint16_t toep_p1[24*SIZE];
uint16_t toep_p2[24*SIZE];
uint16_t toep_0_m1_m2[24*SIZE];
uint16_t toep_p1_0_m1[24*SIZE];
uint16_t toep_p2_p1_0[24*SIZE];
uint16_t vec_0[12*SIZE];
uint16_t vec_1[12*SIZE];
uint16_t vec_2[12*SIZE];
uint16_t vec_0_minus_1[12*SIZE];
uint16_t vec_1_minus_2[12*SIZE];
uint16_t vec_2_minus_0[12*SIZE];
uint16_t intermediate_0[12*SIZE]={0};
uint16_t intermediate_1[12*SIZE]={0};
uint16_t intermediate_2[12*SIZE]={0};
uint16_t intermediate_3[12*SIZE]={0};
uint16_t intermediate_4[12*SIZE]={0};
uint16_t intermediate_5[12*SIZE]={0};
// Block Matrix form of Toeplitz matrix
for (uint16_t i=0; i<24*SIZE; i++){
// i=0,1,...,8xSIZE-1=63
toep_p2[i]=toep[i ];
toep_p1[i]=toep[i + 12*SIZE];
toep_0[i] =toep[i + 24*SIZE];
toep_m1[i]=toep[i + 36*SIZE];
toep_m2[i]=toep[i + 48*SIZE];
toep_0_m1_m2[i] = toep_0[i] + toep_m1[i] + toep_m2[i];
toep_p1_0_m1[i] = toep_p1[i] + toep_0[i] + toep_m1[i];
toep_p2_p1_0[i] = toep_p2[i] + toep_p1[i] + toep_0[i];
}
for (uint16_t i=0; i<12*SIZE; i++) {
vec_0[i] = vec[i ];
vec_1[i] = vec[i + 12*SIZE];
vec_2[i] = vec[i + 24*SIZE];
vec_0_minus_1[i] = vec_0[i] - vec_1[i];
vec_1_minus_2[i] = vec_1[i] - vec_2[i];
vec_2_minus_0[i] = vec_2[i] - vec_0[i];
}
Toep_3x2x2x8(toep_0_m1_m2, vec_2, intermediate_0);
Toep_3x2x2x8(toep_p1_0_m1, vec_1, intermediate_1);
Toep_3x2x2x8(toep_p2_p1_0, vec_0, intermediate_2);
Toep_3x2x2x8(toep_0, vec_2_minus_0, intermediate_3);
Toep_3x2x2x8(toep_p1, vec_0_minus_1, intermediate_4);
Toep_3x2x2x8(toep_m1, vec_1_minus_2, intermediate_5);
// Recombination
for (uint16_t i=0; i<12*SIZE; i++){
r[i ] = intermediate_0[i] - intermediate_3[i] + intermediate_5[i];
r[i + 12*SIZE] = intermediate_1[i] - intermediate_5[i] + intermediate_4[i];
r[i + 24*SIZE] = intermediate_2[i] - intermediate_4[i] + intermediate_3[i];
}
}
void Toep_3x2x2x8(uint16_t *toep, uint16_t *vec, uint16_t *r){
// a is the anti-diagonal elements of a 96x96 toep, consists of 192 elements. (In fact 191 of 192 will be used)
// b is the vector to be multiplied with, consists of 96 elements
// r is the result vector
uint16_t toep_m2[8*SIZE];
uint16_t toep_m1[8*SIZE];
uint16_t toep_0[8*SIZE];
uint16_t toep_p1[8*SIZE];
uint16_t toep_p2[8*SIZE];
uint16_t toep_0_m1_m2[8*SIZE];
uint16_t toep_p1_0_m1[8*SIZE];
uint16_t toep_p2_p1_0[8*SIZE];
uint16_t vec_0[4*SIZE];
uint16_t vec_1[4*SIZE];
uint16_t vec_2[4*SIZE];
uint16_t vec_0_minus_1[4*SIZE];
uint16_t vec_1_minus_2[4*SIZE];
uint16_t vec_2_minus_0[4*SIZE];
uint16_t intermediate_0[4*SIZE]={0};
uint16_t intermediate_1[4*SIZE]={0};
uint16_t intermediate_2[4*SIZE]={0};
uint16_t intermediate_3[4*SIZE]={0};
uint16_t intermediate_4[4*SIZE]={0};
uint16_t intermediate_5[4*SIZE]={0};
for (uint16_t i=0; i<8*SIZE; i++){
// i=0,1,...,8xSIZE-1=63
toep_p2[i]=toep[i ];
toep_p1[i]=toep[i + 4*SIZE];
toep_0[i] =toep[i + 8*SIZE];
toep_m1[i]=toep[i + 12*SIZE];
toep_m2[i]=toep[i + 16*SIZE];
toep_0_m1_m2[i] = toep_0[i] + toep_m1[i] + toep_m2[i];
toep_p1_0_m1[i] = toep_p1[i] + toep_0[i] + toep_m1[i];
toep_p2_p1_0[i] = toep_p2[i] + toep_p1[i] + toep_0[i];
}
for (uint16_t i=0; i<4*SIZE; i++) {
vec_0[i] = vec[i ];
vec_1[i] = vec[i + 4*SIZE];
vec_2[i] = vec[i + 8*SIZE];
vec_0_minus_1[i] = vec_0[i] - vec_1[i];
vec_1_minus_2[i] = vec_1[i] - vec_2[i];
vec_2_minus_0[i] = vec_2[i] - vec_0[i];
}
Toep_2x2x8(toep_0_m1_m2, vec_2, intermediate_0);
Toep_2x2x8(toep_p1_0_m1, vec_1, intermediate_1);
Toep_2x2x8(toep_p2_p1_0, vec_0, intermediate_2);
Toep_2x2x8(toep_0, vec_2_minus_0, intermediate_3);
Toep_2x2x8(toep_p1, vec_0_minus_1, intermediate_4);
Toep_2x2x8(toep_m1, vec_1_minus_2, intermediate_5);
// Recombination
for (uint16_t i=0; i<4*SIZE; i++){
r[i ] = intermediate_0[i] - intermediate_3[i] + intermediate_5[i];
r[i + 4*SIZE] = intermediate_1[i] - intermediate_5[i] + intermediate_4[i];
r[i + 8*SIZE] = intermediate_2[i] - intermediate_4[i] + intermediate_3[i];
}
}
void Toep_2x2x8(uint16_t *toep, uint16_t *vec, uint16_t *r){
// a is the anti-diagonal elements of a 32x32 toep, consists of 64 elements. (In fact 63 of 64 will be used)
// b is the vector to be multiplied with, consists of 32 elements
// r is the result vector
// prepare the anti-diagonal elements of a 16x16 toep
uint16_t toep_m1[ 4*SIZE ];
uint16_t toep_0 [ 4*SIZE ];
uint16_t toep_p1[ 4*SIZE ];
uint16_t toep_0_m1[ 4*SIZE ];
uint16_t toep_p1_0[ 4*SIZE ];
uint16_t vec_0[ 2*SIZE ];
uint16_t vec_1[ 2*SIZE ];
uint16_t vec_1_minus_0[ 2*SIZE ];
uint16_t intermdiate_0[ 2*SIZE ];
uint16_t intermdiate_1[ 2*SIZE ];
uint16_t intermdiate_2[ 2*SIZE ];
for (uint16_t i=0; i<4*SIZE; i++){
// i = 0, 1, ... 4*SIZE-1 = 31
toep_p1[i] = toep[i ]; // toep[0 - 31]
toep_0 [i] = toep[i + 2*SIZE]; // toep[16 - 47]
toep_m1[i] = toep[i + 4*SIZE]; // toep[32 - 63]
toep_0_m1[i] = toep_0[i] + toep_m1[i];
toep_p1_0[i] = toep_p1[i] + toep_0 [i];
}
for (uint16_t i=0; i<2*SIZE; i++){
vec_0[i] = vec[ i];
vec_1[i] = vec[2*SIZE + i];
vec_1_minus_0[i] = vec_1[i] - vec_0[i];
}
Toep_2x8(toep_0_m1, vec_1, intermdiate_0);
Toep_2x8(toep_0, vec_1_minus_0, intermdiate_1);
Toep_2x8(toep_p1_0, vec_0, intermdiate_2);
// Recombination
for (uint16_t i=0; i<2*SIZE; i++){
r[i] = intermdiate_0[i] - intermdiate_1[i];
r[2*SIZE+i] = intermdiate_1[i] + intermdiate_2[i];
}
}
void Toep_2x8(uint16_t *toep, uint16_t *vec, uint16_t *r){
// a is the anti-diagonal elements of a 16x16 toep, consists of 64 elements. (In fact 31 of 32 will be used)
// b is the vector to be multiplied with, consists of 16 elements
// r is the result vector
// prepare the anti-diagonal elements of a 8x8 toep
uint16_t toep_m1[ 2*SIZE ];
uint16_t toep_0 [ 2*SIZE ];
uint16_t toep_p1[ 2*SIZE ];
uint16_t toep_0_m1[ 2*SIZE ];
uint16_t toep_p1_0[ 2*SIZE ];
uint16_t vec_0[ SIZE ];
uint16_t vec_1[ SIZE ];
uint16_t vec_1_minus_0[ SIZE ];
uint16_t intermdiate_0[ SIZE ];
uint16_t intermdiate_1[ SIZE ];
uint16_t intermdiate_2[ SIZE ];
for (uint16_t i=0; i<2*SIZE; i++){
// i = 0, 1, ... (2*SIZE)/2-1 = 31
toep_p1[i] = toep[i ]; // toep[0 - 31]
toep_0 [i] = toep[i + 1*SIZE]; // toep[16 - 47]
toep_m1[i] = toep[i + 2*SIZE]; // toep[32 - 63]
toep_0_m1[i] = toep_0[i] + toep_m1[i];
toep_p1_0[i] = toep_p1[i] + toep_0 [i];
}
// for (uint16_t i=0; i<2*SIZE; i++){
// printf("%d ", toep_m1[i]);
// }
// printf("\n");
for (uint16_t i=0; i<SIZE; i++){
vec_0[i] = vec[ i];
vec_1[i] = vec[SIZE + i];
vec_1_minus_0[i] = vec_1[i] - vec_0[i];
}
TMVP_8x8_on_8(toep_0_m1, vec_1, intermdiate_0);
TMVP_8x8_on_8(toep_0, vec_1_minus_0, intermdiate_1);
TMVP_8x8_on_8(toep_p1_0, vec_0, intermdiate_2);
// Recombination
for (uint16_t i=0; i<SIZE; i++){
r[i] = intermdiate_0[i] - intermdiate_1[i];
r[SIZE+i] = intermdiate_1[i] + intermdiate_2[i];
}
}
void TMVP_8x8_on_8 (uint16_t *toep, uint16_t *vec, uint16_t *r){
uint16x8_t result_vector = vdupq_n_u16(0);
uint16_t scalar;
uint16x8_t toep_vector;
scalar = vec[0];
toep_vector = vld1q_u16(&toep[0]);
result_vector = vmlaq_n_u16(result_vector, toep_vector, scalar);
scalar = vec[1];
toep_vector = vld1q_u16(&toep[1]);
result_vector = vmlaq_n_u16(result_vector, toep_vector, scalar);
scalar = vec[2];
toep_vector = vld1q_u16(&toep[2]);
result_vector = vmlaq_n_u16(result_vector, toep_vector, scalar);
scalar = vec[3];
toep_vector = vld1q_u16(&toep[3]);
result_vector = vmlaq_n_u16(result_vector, toep_vector, scalar);
scalar = vec[4];
toep_vector = vld1q_u16(&toep[4]);
result_vector = vmlaq_n_u16(result_vector, toep_vector, scalar);
scalar = vec[5];
toep_vector = vld1q_u16(&toep[5]);
result_vector = vmlaq_n_u16(result_vector, toep_vector, scalar);
scalar = vec[6];
toep_vector = vld1q_u16(&toep[6]);
result_vector = vmlaq_n_u16(result_vector, toep_vector, scalar);
scalar = vec[7];
toep_vector = vld1q_u16(&toep[7]);
result_vector = vmlaq_n_u16(result_vector, toep_vector, scalar);
uint16_t r_temp[SIZE];
vst1q_u16(r_temp, result_vector);
for (uint16_t i=0; i<SIZE; i++){
r[i] = r_temp[(SIZE - 1) - i];
}
}