forked from tuneinsight/lattigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector_ops.go
731 lines (628 loc) · 24.8 KB
/
vector_ops.go
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
package ring
import (
"unsafe"
)
// AddVec returns p3 = p1 + p2 mod qi.
func AddVec(p1, p2, p3 []uint64, qi uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
y := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z := (*[8]uint64)(unsafe.Pointer(&p3[j]))
z[0] = CRed(x[0]+y[0], qi)
z[1] = CRed(x[1]+y[1], qi)
z[2] = CRed(x[2]+y[2], qi)
z[3] = CRed(x[3]+y[3], qi)
z[4] = CRed(x[4]+y[4], qi)
z[5] = CRed(x[5]+y[5], qi)
z[6] = CRed(x[6]+y[6], qi)
z[7] = CRed(x[7]+y[7], qi)
}
}
// AddVecNoMod returns p3 = p1 + p2.
func AddVecNoMod(p1, p2, p3 []uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
y := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z := (*[8]uint64)(unsafe.Pointer(&p3[j]))
z[0] = x[0] + y[0]
z[1] = x[1] + y[1]
z[2] = x[2] + y[2]
z[3] = x[3] + y[3]
z[4] = x[4] + y[4]
z[5] = x[5] + y[5]
z[6] = x[6] + y[6]
z[7] = x[7] + y[7]
}
}
// SubVec returns p3 = p1 - p2 mod qi.
func SubVec(p1, p2, p3 []uint64, qi uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
y := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z := (*[8]uint64)(unsafe.Pointer(&p3[j]))
z[0] = CRed((x[0]+qi)-y[0], qi)
z[1] = CRed((x[1]+qi)-y[1], qi)
z[2] = CRed((x[2]+qi)-y[2], qi)
z[3] = CRed((x[3]+qi)-y[3], qi)
z[4] = CRed((x[4]+qi)-y[4], qi)
z[5] = CRed((x[5]+qi)-y[5], qi)
z[6] = CRed((x[6]+qi)-y[6], qi)
z[7] = CRed((x[7]+qi)-y[7], qi)
}
}
// SubVecNomod returns p3 = p1 + qi - p2.
func SubVecNomod(p1, p2, p3 []uint64, qi uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
y := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z := (*[8]uint64)(unsafe.Pointer(&p3[j]))
z[0] = x[0] + qi - y[0]
z[1] = x[1] + qi - y[1]
z[2] = x[2] + qi - y[2]
z[3] = x[3] + qi - y[3]
z[4] = x[4] + qi - y[4]
z[5] = x[5] + qi - y[5]
z[6] = x[6] + qi - y[6]
z[7] = x[7] + qi - y[7]
}
}
// NegVec returns p2 = -p1 mod qi.
func NegVec(p1, p2 []uint64, qi uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
z := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z[0] = qi - x[0]
z[1] = qi - x[1]
z[2] = qi - x[2]
z[3] = qi - x[3]
z[4] = qi - x[4]
z[5] = qi - x[5]
z[6] = qi - x[6]
z[7] = qi - x[7]
}
}
// ReduceVec returns p2 = p1 mod qi.
func ReduceVec(p1, p2 []uint64, qi uint64, bredParams []uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
z := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z[0] = BRedAdd(x[0], qi, bredParams)
z[1] = BRedAdd(x[1], qi, bredParams)
z[2] = BRedAdd(x[2], qi, bredParams)
z[3] = BRedAdd(x[3], qi, bredParams)
z[4] = BRedAdd(x[4], qi, bredParams)
z[5] = BRedAdd(x[5], qi, bredParams)
z[6] = BRedAdd(x[6], qi, bredParams)
z[7] = BRedAdd(x[7], qi, bredParams)
}
}
// ReduceConstantVec returns p2 = p1 mod qi with output coefficients range [0, 2qi-1].
func ReduceConstantVec(p1, p2 []uint64, qi uint64, bredParams []uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
z := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z[0] = BRedAddConstant(x[0], qi, bredParams)
z[1] = BRedAddConstant(x[1], qi, bredParams)
z[2] = BRedAddConstant(x[2], qi, bredParams)
z[3] = BRedAddConstant(x[3], qi, bredParams)
z[4] = BRedAddConstant(x[4], qi, bredParams)
z[5] = BRedAddConstant(x[5], qi, bredParams)
z[6] = BRedAddConstant(x[6], qi, bredParams)
z[7] = BRedAddConstant(x[7], qi, bredParams)
}
}
// ModVec returns p2 = p1 mod m.
func ModVec(p1, p2 []uint64, m uint64, bredParams []uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
z := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z[0] = BRedAdd(x[0], m, bredParams)
z[1] = BRedAdd(x[1], m, bredParams)
z[2] = BRedAdd(x[2], m, bredParams)
z[3] = BRedAdd(x[3], m, bredParams)
z[4] = BRedAdd(x[4], m, bredParams)
z[5] = BRedAdd(x[5], m, bredParams)
z[6] = BRedAdd(x[6], m, bredParams)
z[7] = BRedAdd(x[7], m, bredParams)
}
}
// MulCoeffsNoModVec returns p3 = p1*p2.
func MulCoeffsNoModVec(p1, p2, p3 []uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
y := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z := (*[8]uint64)(unsafe.Pointer(&p3[j]))
z[0] = x[0] * y[0]
z[1] = x[1] * y[1]
z[2] = x[2] * y[2]
z[3] = x[3] * y[3]
z[4] = x[4] * y[4]
z[5] = x[5] * y[5]
z[6] = x[6] * y[6]
z[7] = x[7] * y[7]
}
}
// MulCoeffsNoModAndAddNoModVec returns p3 += p1*p2.
func MulCoeffsNoModAndAddNoModVec(p1, p2, p3 []uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
y := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z := (*[8]uint64)(unsafe.Pointer(&p3[j]))
z[0] += x[0] * y[0]
z[1] += x[1] * y[1]
z[2] += x[2] * y[2]
z[3] += x[3] * y[3]
z[4] += x[4] * y[4]
z[5] += x[5] * y[5]
z[6] += x[6] * y[6]
z[7] += x[7] * y[7]
}
}
// MulCoeffsVec returns p3 = p1*p2 mod qi.
func MulCoeffsVec(p1, p2, p3 []uint64, qi uint64, bredParams []uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
y := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z := (*[8]uint64)(unsafe.Pointer(&p3[j]))
z[0] = BRed(x[0], y[0], qi, bredParams)
z[1] = BRed(x[1], y[1], qi, bredParams)
z[2] = BRed(x[2], y[2], qi, bredParams)
z[3] = BRed(x[3], y[3], qi, bredParams)
z[4] = BRed(x[4], y[4], qi, bredParams)
z[5] = BRed(x[5], y[5], qi, bredParams)
z[6] = BRed(x[6], y[6], qi, bredParams)
z[7] = BRed(x[7], y[7], qi, bredParams)
}
}
// MulCoeffsAndAddVec returns p3 = p3 + (p1*p2) mod qi.
func MulCoeffsAndAddVec(p1, p2, p3 []uint64, qi uint64, bredParams []uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
y := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z := (*[8]uint64)(unsafe.Pointer(&p3[j]))
z[0] = CRed(z[0]+BRed(x[0], y[0], qi, bredParams), qi)
z[1] = CRed(z[1]+BRed(x[1], y[1], qi, bredParams), qi)
z[2] = CRed(z[2]+BRed(x[2], y[2], qi, bredParams), qi)
z[3] = CRed(z[3]+BRed(x[3], y[3], qi, bredParams), qi)
z[4] = CRed(z[4]+BRed(x[4], y[4], qi, bredParams), qi)
z[5] = CRed(z[5]+BRed(x[5], y[5], qi, bredParams), qi)
z[6] = CRed(z[6]+BRed(x[6], y[6], qi, bredParams), qi)
z[7] = CRed(z[7]+BRed(x[7], y[7], qi, bredParams), qi)
}
}
// MulCoeffsAndAddNoModVec returns p3 = p3 + (p1*p2 mod qi).
func MulCoeffsAndAddNoModVec(p1, p2, p3 []uint64, qi uint64, bredParams []uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
y := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z := (*[8]uint64)(unsafe.Pointer(&p3[j]))
z[0] += BRed(x[0], y[0], qi, bredParams)
z[1] += BRed(x[1], y[1], qi, bredParams)
z[2] += BRed(x[2], y[2], qi, bredParams)
z[3] += BRed(x[3], y[3], qi, bredParams)
z[4] += BRed(x[4], y[4], qi, bredParams)
z[5] += BRed(x[5], y[5], qi, bredParams)
z[6] += BRed(x[6], y[6], qi, bredParams)
z[7] += BRed(x[7], y[7], qi, bredParams)
}
}
// MulCoeffsMontgomeryVec returns p3 = p1*p2 mod qi.
func MulCoeffsMontgomeryVec(p1, p2, p3 []uint64, qi, mredParams uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
y := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z := (*[8]uint64)(unsafe.Pointer(&p3[j]))
z[0] = MRed(x[0], y[0], qi, mredParams)
z[1] = MRed(x[1], y[1], qi, mredParams)
z[2] = MRed(x[2], y[2], qi, mredParams)
z[3] = MRed(x[3], y[3], qi, mredParams)
z[4] = MRed(x[4], y[4], qi, mredParams)
z[5] = MRed(x[5], y[5], qi, mredParams)
z[6] = MRed(x[6], y[6], qi, mredParams)
z[7] = MRed(x[7], y[7], qi, mredParams)
}
}
// MulCoeffsMontgomeryConstantVec returns p3 = p1*p2 mod qi with output coefficients in range [0, 2qi-1].
func MulCoeffsMontgomeryConstantVec(p1, p2, p3 []uint64, qi, mredParams uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
y := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z := (*[8]uint64)(unsafe.Pointer(&p3[j]))
z[0] = MRedConstant(x[0], y[0], qi, mredParams)
z[1] = MRedConstant(x[1], y[1], qi, mredParams)
z[2] = MRedConstant(x[2], y[2], qi, mredParams)
z[3] = MRedConstant(x[3], y[3], qi, mredParams)
z[4] = MRedConstant(x[4], y[4], qi, mredParams)
z[5] = MRedConstant(x[5], y[5], qi, mredParams)
z[6] = MRedConstant(x[6], y[6], qi, mredParams)
z[7] = MRedConstant(x[7], y[7], qi, mredParams)
}
}
// MulCoeffsMontgomeryAndAddVec returns p3 = p3 + (p1*p2) mod qi.
func MulCoeffsMontgomeryAndAddVec(p1, p2, p3 []uint64, qi, mredParams uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
y := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z := (*[8]uint64)(unsafe.Pointer(&p3[j]))
z[0] = CRed(z[0]+MRed(x[0], y[0], qi, mredParams), qi)
z[1] = CRed(z[1]+MRed(x[1], y[1], qi, mredParams), qi)
z[2] = CRed(z[2]+MRed(x[2], y[2], qi, mredParams), qi)
z[3] = CRed(z[3]+MRed(x[3], y[3], qi, mredParams), qi)
z[4] = CRed(z[4]+MRed(x[4], y[4], qi, mredParams), qi)
z[5] = CRed(z[5]+MRed(x[5], y[5], qi, mredParams), qi)
z[6] = CRed(z[6]+MRed(x[6], y[6], qi, mredParams), qi)
z[7] = CRed(z[7]+MRed(x[7], y[7], qi, mredParams), qi)
}
}
// MulCoeffsMontgomeryAndAddNoModVec returns p3 = p3 + (p1*p2 mod qi).
func MulCoeffsMontgomeryAndAddNoModVec(p1, p2, p3 []uint64, qi, mredParams uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
y := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z := (*[8]uint64)(unsafe.Pointer(&p3[j]))
z[0] += MRed(x[0], y[0], qi, mredParams)
z[1] += MRed(x[1], y[1], qi, mredParams)
z[2] += MRed(x[2], y[2], qi, mredParams)
z[3] += MRed(x[3], y[3], qi, mredParams)
z[4] += MRed(x[4], y[4], qi, mredParams)
z[5] += MRed(x[5], y[5], qi, mredParams)
z[6] += MRed(x[6], y[6], qi, mredParams)
z[7] += MRed(x[7], y[7], qi, mredParams)
}
}
// MulCoeffsMontgomeryConstantAndAddNoModVec returns p3 = p3 + p1*p2 mod qi with output coefficients in range [0, 3qi-2].
func MulCoeffsMontgomeryConstantAndAddNoModVec(p1, p2, p3 []uint64, qi, mredParams uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
y := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z := (*[8]uint64)(unsafe.Pointer(&p3[j]))
z[0] += MRedConstant(x[0], y[0], qi, mredParams)
z[1] += MRedConstant(x[1], y[1], qi, mredParams)
z[2] += MRedConstant(x[2], y[2], qi, mredParams)
z[3] += MRedConstant(x[3], y[3], qi, mredParams)
z[4] += MRedConstant(x[4], y[4], qi, mredParams)
z[5] += MRedConstant(x[5], y[5], qi, mredParams)
z[6] += MRedConstant(x[6], y[6], qi, mredParams)
z[7] += MRedConstant(x[7], y[7], qi, mredParams)
}
}
// MulCoeffsMontgomeryAndSubVec returns p3 = p3 - p1*p2 mod qi.
func MulCoeffsMontgomeryAndSubVec(p1, p2, p3 []uint64, qi, mredParams uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
y := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z := (*[8]uint64)(unsafe.Pointer(&p3[j]))
z[0] = CRed(z[0]+(qi-MRed(x[0], y[0], qi, mredParams)), qi)
z[1] = CRed(z[1]+(qi-MRed(x[1], y[1], qi, mredParams)), qi)
z[2] = CRed(z[2]+(qi-MRed(x[2], y[2], qi, mredParams)), qi)
z[3] = CRed(z[3]+(qi-MRed(x[3], y[3], qi, mredParams)), qi)
z[4] = CRed(z[4]+(qi-MRed(x[4], y[4], qi, mredParams)), qi)
z[5] = CRed(z[5]+(qi-MRed(x[5], y[5], qi, mredParams)), qi)
z[6] = CRed(z[6]+(qi-MRed(x[6], y[6], qi, mredParams)), qi)
z[7] = CRed(z[7]+(qi-MRed(x[7], y[7], qi, mredParams)), qi)
}
}
// MulCoeffsMontgomeryAndSubNoMod returns p3 = p3 - p1*p2 mod qi with output coefficients in range [0, 2qi-2].
func MulCoeffsMontgomeryAndSubNoMod(p1, p2, p3 []uint64, qi, mredParams uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
y := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z := (*[8]uint64)(unsafe.Pointer(&p3[j]))
z[0] += (qi - MRed(x[0], y[0], qi, mredParams))
z[1] += (qi - MRed(x[1], y[1], qi, mredParams))
z[2] += (qi - MRed(x[2], y[2], qi, mredParams))
z[3] += (qi - MRed(x[3], y[3], qi, mredParams))
z[4] += (qi - MRed(x[4], y[4], qi, mredParams))
z[5] += (qi - MRed(x[5], y[5], qi, mredParams))
z[6] += (qi - MRed(x[6], y[6], qi, mredParams))
z[7] += (qi - MRed(x[7], y[7], qi, mredParams))
}
}
// MulCoeffsMontgomeryConstantAndSubNoMod returns p3 = p3 - p1*p2 mod qi with output coefficients in range [0, 3qi-2].
func MulCoeffsMontgomeryConstantAndSubNoMod(p1, p2, p3 []uint64, qi, mredParams uint64) {
twoQi := qi << 1
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
y := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z := (*[8]uint64)(unsafe.Pointer(&p3[j]))
z[0] += twoQi - MRedConstant(x[0], y[0], qi, mredParams)
z[1] += twoQi - MRedConstant(x[1], y[1], qi, mredParams)
z[2] += twoQi - MRedConstant(x[2], y[2], qi, mredParams)
z[3] += twoQi - MRedConstant(x[3], y[3], qi, mredParams)
z[4] += twoQi - MRedConstant(x[4], y[4], qi, mredParams)
z[5] += twoQi - MRedConstant(x[5], y[5], qi, mredParams)
z[6] += twoQi - MRedConstant(x[6], y[6], qi, mredParams)
z[7] += twoQi - MRedConstant(x[7], y[7], qi, mredParams)
}
}
// MulCoeffsMontgomeryConstantAndNeg returns p3 = - p1*p2 mod qi with output coefficients in range [0, 2qi-2].
func MulCoeffsMontgomeryConstantAndNeg(p1, p2, p3 []uint64, qi, mredParams uint64) {
twoqi := qi << 1
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
y := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z := (*[8]uint64)(unsafe.Pointer(&p3[j]))
z[0] = twoqi - MRedConstant(x[0], y[0], qi, mredParams)
z[1] = twoqi - MRedConstant(x[1], y[1], qi, mredParams)
z[2] = twoqi - MRedConstant(x[2], y[2], qi, mredParams)
z[3] = twoqi - MRedConstant(x[3], y[3], qi, mredParams)
z[4] = twoqi - MRedConstant(x[4], y[4], qi, mredParams)
z[5] = twoqi - MRedConstant(x[5], y[5], qi, mredParams)
z[6] = twoqi - MRedConstant(x[6], y[6], qi, mredParams)
z[7] = twoqi - MRedConstant(x[7], y[7], qi, mredParams)
}
}
// MulCoeffsConstantVec returns p3 = p1*p2 mod qi with output coefficients in range [0, 2qi-1].
func MulCoeffsConstantVec(p1, p2, p3 []uint64, qi uint64, bredParams []uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
y := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z := (*[8]uint64)(unsafe.Pointer(&p3[j]))
z[0] = BRedConstant(x[0], y[0], qi, bredParams)
z[1] = BRedConstant(x[1], y[1], qi, bredParams)
z[2] = BRedConstant(x[2], y[2], qi, bredParams)
z[3] = BRedConstant(x[3], y[3], qi, bredParams)
z[4] = BRedConstant(x[4], y[4], qi, bredParams)
z[5] = BRedConstant(x[5], y[5], qi, bredParams)
z[6] = BRedConstant(x[6], y[6], qi, bredParams)
z[7] = BRedConstant(x[7], y[7], qi, bredParams)
}
}
// AddVecNoModAndMulScalarMontgomeryVec returns p3 = (p1+p2)*scalarMont mod qi.
func AddVecNoModAndMulScalarMontgomeryVec(p1, p2, p3 []uint64, scalarMont, qi, mredParams uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
y := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z := (*[8]uint64)(unsafe.Pointer(&p3[j]))
z[0] = MRed(x[0]+y[0], scalarMont, qi, mredParams)
z[1] = MRed(x[1]+y[1], scalarMont, qi, mredParams)
z[2] = MRed(x[2]+y[2], scalarMont, qi, mredParams)
z[3] = MRed(x[3]+y[3], scalarMont, qi, mredParams)
z[4] = MRed(x[4]+y[4], scalarMont, qi, mredParams)
z[5] = MRed(x[5]+y[5], scalarMont, qi, mredParams)
z[6] = MRed(x[6]+y[6], scalarMont, qi, mredParams)
z[7] = MRed(x[7]+y[7], scalarMont, qi, mredParams)
}
}
// AddScalarNoModAndMulScalarMontgomeryVec returns p3 = (scalarMont0+p2)*scalarMont1 mod qi.
func AddScalarNoModAndMulScalarMontgomeryVec(p1, p2 []uint64, scalar0, scalarMont1, qi, mredParams uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
z := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z[0] = MRed(x[0]+scalar0, scalarMont1, qi, mredParams)
z[1] = MRed(x[1]+scalar0, scalarMont1, qi, mredParams)
z[2] = MRed(x[2]+scalar0, scalarMont1, qi, mredParams)
z[3] = MRed(x[3]+scalar0, scalarMont1, qi, mredParams)
z[4] = MRed(x[4]+scalar0, scalarMont1, qi, mredParams)
z[5] = MRed(x[5]+scalar0, scalarMont1, qi, mredParams)
z[6] = MRed(x[6]+scalar0, scalarMont1, qi, mredParams)
z[7] = MRed(x[7]+scalar0, scalarMont1, qi, mredParams)
}
}
// AddScalarVec returns p2 = p1 + scalar mod qi.
func AddScalarVec(p1, p2 []uint64, scalar, qi uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
z := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z[0] = CRed(x[0]+scalar, qi)
z[1] = CRed(x[1]+scalar, qi)
z[2] = CRed(x[2]+scalar, qi)
z[3] = CRed(x[3]+scalar, qi)
z[4] = CRed(x[4]+scalar, qi)
z[5] = CRed(x[5]+scalar, qi)
z[6] = CRed(x[6]+scalar, qi)
z[7] = CRed(x[7]+scalar, qi)
}
}
// AddScalarNoModVec returns p2 = p1 + scalar.
func AddScalarNoModVec(p1, p2 []uint64, scalar uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
z := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z[0] = x[0] + scalar
z[1] = x[1] + scalar
z[2] = x[2] + scalar
z[3] = x[3] + scalar
z[4] = x[4] + scalar
z[5] = x[5] + scalar
z[6] = x[6] + scalar
z[7] = x[7] + scalar
}
}
// AddScalarNoModAndNegTwoQiNoModVec returns p2 = 2*qi - p1 + scalar.
func AddScalarNoModAndNegTwoQiNoModVec(p1, p2 []uint64, scalar, qi uint64) {
twoqi := qi << 1
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
z := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z[0] = scalar + twoqi - x[0]
z[1] = scalar + twoqi - x[1]
z[2] = scalar + twoqi - x[2]
z[3] = scalar + twoqi - x[3]
z[4] = scalar + twoqi - x[4]
z[5] = scalar + twoqi - x[5]
z[6] = scalar + twoqi - x[6]
z[7] = scalar + twoqi - x[7]
}
}
// SubScalarVec returns p2 = p1 - scalar mod qi.
func SubScalarVec(p1, p2 []uint64, scalar, qi uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
z := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z[0] = CRed(x[0]+qi-scalar, qi)
z[1] = CRed(x[1]+qi-scalar, qi)
z[2] = CRed(x[2]+qi-scalar, qi)
z[3] = CRed(x[3]+qi-scalar, qi)
z[4] = CRed(x[4]+qi-scalar, qi)
z[5] = CRed(x[5]+qi-scalar, qi)
z[6] = CRed(x[6]+qi-scalar, qi)
z[7] = CRed(x[7]+qi-scalar, qi)
}
}
// MulScalarMontgomeryVec returns p2 = p1*scalarMont mod qi.
func MulScalarMontgomeryVec(p1, p2 []uint64, scalarMont, qi, mredParams uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
z := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z[0] = MRed(x[0], scalarMont, qi, mredParams)
z[1] = MRed(x[1], scalarMont, qi, mredParams)
z[2] = MRed(x[2], scalarMont, qi, mredParams)
z[3] = MRed(x[3], scalarMont, qi, mredParams)
z[4] = MRed(x[4], scalarMont, qi, mredParams)
z[5] = MRed(x[5], scalarMont, qi, mredParams)
z[6] = MRed(x[6], scalarMont, qi, mredParams)
z[7] = MRed(x[7], scalarMont, qi, mredParams)
}
}
// MulScalarMontgomeryConstantVec returns p2 = p1*scalarMont mod qi with output coefficients in range [0, 2qi-1].
func MulScalarMontgomeryConstantVec(p1, p2 []uint64, scalarMont, qi, mredParams uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
z := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z[0] = MRedConstant(x[0], scalarMont, qi, mredParams)
z[1] = MRedConstant(x[1], scalarMont, qi, mredParams)
z[2] = MRedConstant(x[2], scalarMont, qi, mredParams)
z[3] = MRedConstant(x[3], scalarMont, qi, mredParams)
z[4] = MRedConstant(x[4], scalarMont, qi, mredParams)
z[5] = MRedConstant(x[5], scalarMont, qi, mredParams)
z[6] = MRedConstant(x[6], scalarMont, qi, mredParams)
z[7] = MRedConstant(x[7], scalarMont, qi, mredParams)
}
}
// MulScalarMontgomeryAndAddVec returns p2 = p2 + p1*scalarMont mod qi.
func MulScalarMontgomeryAndAddVec(p1, p2 []uint64, scalarMont, qi, mredParams uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
z := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z[0] = CRed(z[0]+MRed(x[0], scalarMont, qi, mredParams), qi)
z[1] = CRed(z[1]+MRed(x[1], scalarMont, qi, mredParams), qi)
z[2] = CRed(z[2]+MRed(x[2], scalarMont, qi, mredParams), qi)
z[3] = CRed(z[3]+MRed(x[3], scalarMont, qi, mredParams), qi)
z[4] = CRed(z[4]+MRed(x[4], scalarMont, qi, mredParams), qi)
z[5] = CRed(z[5]+MRed(x[5], scalarMont, qi, mredParams), qi)
z[6] = CRed(z[6]+MRed(x[6], scalarMont, qi, mredParams), qi)
z[7] = CRed(z[7]+MRed(x[7], scalarMont, qi, mredParams), qi)
}
}
// MulScalarMontgomeryAndAddScalarVec returns p2 = scalar + p1*scalarMont mod qi.
func MulScalarMontgomeryAndAddScalarVec(p1, p2 []uint64, scalar0, scalarMont1, qi, mredParams uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
z := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z[0] = CRed(MRed(x[0], scalarMont1, qi, mredParams)+scalar0, qi)
z[1] = CRed(MRed(x[1], scalarMont1, qi, mredParams)+scalar0, qi)
z[2] = CRed(MRed(x[2], scalarMont1, qi, mredParams)+scalar0, qi)
z[3] = CRed(MRed(x[3], scalarMont1, qi, mredParams)+scalar0, qi)
z[4] = CRed(MRed(x[4], scalarMont1, qi, mredParams)+scalar0, qi)
z[5] = CRed(MRed(x[5], scalarMont1, qi, mredParams)+scalar0, qi)
z[6] = CRed(MRed(x[6], scalarMont1, qi, mredParams)+scalar0, qi)
z[7] = CRed(MRed(x[7], scalarMont1, qi, mredParams)+scalar0, qi)
}
}
// SubVecAndMulScalarMontgomeryTwoQiVec returns p3 = (p1 + twoqi - p2) * scalarMont mod qi.
func SubVecAndMulScalarMontgomeryTwoQiVec(p1, p2, p3 []uint64, scalarMont, qi, mredParams uint64) {
twoqi := qi << 1
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
y := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z := (*[8]uint64)(unsafe.Pointer(&p3[j]))
z[0] = MRed(twoqi-y[0]+x[0], scalarMont, qi, mredParams)
z[1] = MRed(twoqi-y[1]+x[1], scalarMont, qi, mredParams)
z[2] = MRed(twoqi-y[2]+x[2], scalarMont, qi, mredParams)
z[3] = MRed(twoqi-y[3]+x[3], scalarMont, qi, mredParams)
z[4] = MRed(twoqi-y[4]+x[4], scalarMont, qi, mredParams)
z[5] = MRed(twoqi-y[5]+x[5], scalarMont, qi, mredParams)
z[6] = MRed(twoqi-y[6]+x[6], scalarMont, qi, mredParams)
z[7] = MRed(twoqi-y[7]+x[7], scalarMont, qi, mredParams)
}
}
// MFormVec returns p2 = p1 * 2^64 mod qi.
func MFormVec(p1, p2 []uint64, qi uint64, bredParams []uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
z := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z[0] = MForm(x[0], qi, bredParams)
z[1] = MForm(x[1], qi, bredParams)
z[2] = MForm(x[2], qi, bredParams)
z[3] = MForm(x[3], qi, bredParams)
z[4] = MForm(x[4], qi, bredParams)
z[5] = MForm(x[5], qi, bredParams)
z[6] = MForm(x[6], qi, bredParams)
z[7] = MForm(x[7], qi, bredParams)
}
}
// MFormConstantVec returns p2 = p1 * 2^64 mod qi with result in the range [0, 2q-1]
func MFormConstantVec(p1, p2 []uint64, qi uint64, bredParams []uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
z := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z[0] = MFormConstant(x[0], qi, bredParams)
z[1] = MFormConstant(x[1], qi, bredParams)
z[2] = MFormConstant(x[2], qi, bredParams)
z[3] = MFormConstant(x[3], qi, bredParams)
z[4] = MFormConstant(x[4], qi, bredParams)
z[5] = MFormConstant(x[5], qi, bredParams)
z[6] = MFormConstant(x[6], qi, bredParams)
z[7] = MFormConstant(x[7], qi, bredParams)
}
}
// InvMFormVec returns p2 = p1 * (2^64)^-1 mod qi.
func InvMFormVec(p1, p2 []uint64, qi, mredParams uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
z := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z[0] = InvMForm(x[0], qi, mredParams)
z[1] = InvMForm(x[1], qi, mredParams)
z[2] = InvMForm(x[2], qi, mredParams)
z[3] = InvMForm(x[3], qi, mredParams)
z[4] = InvMForm(x[4], qi, mredParams)
z[5] = InvMForm(x[5], qi, mredParams)
z[6] = InvMForm(x[6], qi, mredParams)
z[7] = InvMForm(x[7], qi, mredParams)
}
}
// MulByPow2Vec returns p2 = p1 * 2^pow2 mod qi.
func MulByPow2Vec(p1, p2 []uint64, pow2 int, qi, mredParams uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
z := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z[0] = PowerOf2(x[0], pow2, qi, mredParams)
z[1] = PowerOf2(x[1], pow2, qi, mredParams)
z[2] = PowerOf2(x[2], pow2, qi, mredParams)
z[3] = PowerOf2(x[3], pow2, qi, mredParams)
z[4] = PowerOf2(x[4], pow2, qi, mredParams)
z[5] = PowerOf2(x[5], pow2, qi, mredParams)
z[6] = PowerOf2(x[6], pow2, qi, mredParams)
z[7] = PowerOf2(x[7], pow2, qi, mredParams)
}
}
// ZeroVec sets all values of p1 to zero.
func ZeroVec(p1 []uint64) {
for j := 0; j < len(p1); j = j + 8 {
z := (*[8]uint64)(unsafe.Pointer(&p1[j]))
z[0] = 0
z[1] = 0
z[2] = 0
z[3] = 0
z[4] = 0
z[5] = 0
z[6] = 0
z[7] = 0
}
}
// MaskVec returns p2 = (p1>>w) & mask
func MaskVec(p1, p2 []uint64, w int, mask uint64) {
for j := 0; j < len(p1); j = j + 8 {
x := (*[8]uint64)(unsafe.Pointer(&p1[j]))
z := (*[8]uint64)(unsafe.Pointer(&p2[j]))
z[0] = (x[0] >> w) & mask
z[1] = (x[1] >> w) & mask
z[2] = (x[2] >> w) & mask
z[3] = (x[3] >> w) & mask
z[4] = (x[4] >> w) & mask
z[5] = (x[5] >> w) & mask
z[6] = (x[6] >> w) & mask
z[7] = (x[7] >> w) & mask
}
}