-
Notifications
You must be signed in to change notification settings - Fork 0
/
RtF_bench_test.go
769 lines (659 loc) · 24.9 KB
/
RtF_bench_test.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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
package ckks_fv
import (
"crypto/rand"
"fmt"
"math"
"testing"
"github.com/ldsec/lattigo/v2/ring"
"github.com/ldsec/lattigo/v2/utils"
"golang.org/x/crypto/sha3"
)
// Benchmark RtF framework with HERA for 80-bit security full-slots parameter
func BenchmarkRtFHera80f(b *testing.B) {
benchmarkRtFHera(b, "80f", 4, 0, 2, true)
}
// Benchmark RtF framework with HERA for 80-bit security 4-slots parameter
func BenchmarkRtFHera80s(b *testing.B) {
benchmarkRtFHera(b, "80s", 4, 1, 0, false)
}
// Benchmark RtF framework with HERA for 80-bit security full-slots parameter with arcsine evaluation
func BenchmarkRtFHera80af(b *testing.B) {
benchmarkRtFHera(b, "80af", 4, 2, 2, true)
}
// Benchmark RtF framework with HERA for 80-bit security 4-slots parameter with arcsine evaluation
func BenchmarkRtFHera80as(b *testing.B) {
benchmarkRtFHera(b, "80as", 4, 3, 0, false)
}
// Benchmark RtF framework with HERA for 128-bit security full-slots parameter
func BenchmarkRtFHera128f(b *testing.B) {
benchmarkRtFHera(b, "128f", 5, 0, 2, true)
}
// Benchmark RtF framework with HERA for 128-bit security 4-slots parameter
func BenchmarkRtFHera128s(b *testing.B) {
benchmarkRtFHera(b, "128s", 5, 1, 0, false)
}
// Benchmark RtF framework with HERA for 128-bit security full-slots parameter with arcsine evaluation
func BenchmarkRtFHera128af(b *testing.B) {
benchmarkRtFHera(b, "128af", 5, 2, 2, true)
}
// Benchmark RtF framework with HERA for 128-bit security 4-slots parameter with arcsine evaluation
func BenchmarkRtFHera128as(b *testing.B) {
benchmarkRtFHera(b, "128as", 5, 3, 2, false)
}
// Benchmark RtF framework with Rubato80S
func BenchmarkRtFRubato80S(b *testing.B) {
benchmarkRtFRubato(b, RUBATO80S)
}
// Benchmark RtF framework with Rubato80M
func BenchmarkRtFRubato80M(b *testing.B) {
benchmarkRtFRubato(b, RUBATO80M)
}
// Benchmark RtF framework with Rubato80L
func BenchmarkRtFRubato80L(b *testing.B) {
benchmarkRtFRubato(b, RUBATO80L)
}
// Benchmark RtF framework with Rubato128S
func BenchmarkRtFRubato128S(b *testing.B) {
benchmarkRtFRubato(b, RUBATO128S)
}
// Benchmark RtF framework with Rubato128M
func BenchmarkRtFRubato128M(b *testing.B) {
benchmarkRtFRubato(b, RUBATO128M)
}
// Benchmark RtF framework with Rubato128L
func BenchmarkRtFRubato128L(b *testing.B) {
benchmarkRtFRubato(b, RUBATO128L)
}
func benchmarkRtFHera(b *testing.B, name string, numRound int, paramIndex int, radix int, fullCoeffs bool) {
var err error
var hbtp *HalfBootstrapper
var kgen KeyGenerator
var fvEncoder MFVEncoder
var ckksEncoder CKKSEncoder
var ckksDecryptor CKKSDecryptor
var sk *SecretKey
var pk *PublicKey
var fvEncryptor MFVEncryptor
var fvEvaluator MFVEvaluator
var plainCKKSRingTs []*PlaintextRingT
var plaintexts []*Plaintext
var hera MFVHera
var data [][]float64
var nonces [][]byte
var key []uint64
var keystream [][]uint64
var fvKeystreams []*Ciphertext
// RtF Hera parameters
// Four sets of parameters (index 0 to 3) ensuring 128 bit of security
// are available in github.com/smilecjf/lattigo/v2/ckks_fv/rtf_params
// LogSlots is hardcoded in the parameters, but can be changed from 4 to 15.
// When changing logSlots make sure that the number of levels allocated to CtS is
// smaller or equal to logSlots.
hbtpParams := RtFHeraParams[paramIndex]
params, err := hbtpParams.Params()
if err != nil {
panic(err)
}
messageScaling := float64(params.PlainModulus()) / hbtpParams.MessageRatio
// HERA parameters in RtF
var heraModDown, stcModDown []int
if numRound == 4 {
heraModDown = HeraModDownParams80[paramIndex].CipherModDown
stcModDown = HeraModDownParams80[paramIndex].StCModDown
} else {
heraModDown = HeraModDownParams128[paramIndex].CipherModDown
stcModDown = HeraModDownParams128[paramIndex].StCModDown
}
// fullCoeffs denotes whether full coefficients are used for data encoding
if fullCoeffs {
params.SetLogFVSlots(params.LogN())
} else {
params.SetLogFVSlots(params.LogSlots())
}
// Scheme context and keys
kgen = NewKeyGenerator(params)
sk, pk = kgen.GenKeyPairSparse(hbtpParams.H)
fvEncoder = NewMFVEncoder(params)
ckksEncoder = NewCKKSEncoder(params)
fvEncryptor = NewMFVEncryptorFromPk(params, pk)
ckksDecryptor = NewCKKSDecryptor(params, sk)
// Generating half-bootstrapping keys
rotationsHalfBoot := kgen.GenRotationIndexesForHalfBoot(params.LogSlots(), hbtpParams)
pDcds := fvEncoder.GenSlotToCoeffMatFV(radix)
rotationsStC := kgen.GenRotationIndexesForSlotsToCoeffsMat(pDcds)
rotations := append(rotationsHalfBoot, rotationsStC...)
if !fullCoeffs {
rotations = append(rotations, params.Slots()/2)
}
rotkeys := kgen.GenRotationKeysForRotations(rotations, true, sk)
rlk := kgen.GenRelinearizationKey(sk)
hbtpKey := BootstrappingKey{Rlk: rlk, Rtks: rotkeys}
if hbtp, err = NewHalfBootstrapper(params, hbtpParams, hbtpKey); err != nil {
panic(err)
}
// Encode float data added by keystream to plaintext coefficients
fvEvaluator = NewMFVEvaluator(params, EvaluationKey{Rlk: rlk, Rtks: rotkeys}, pDcds)
coeffs := make([][]float64, 16)
for s := 0; s < 16; s++ {
coeffs[s] = make([]float64, params.N())
}
key = make([]uint64, 16)
for i := 0; i < 16; i++ {
key[i] = uint64(i + 1) // Use (1, ..., 16) for testing
}
if fullCoeffs {
data = make([][]float64, 16)
for s := 0; s < 16; s++ {
data[s] = make([]float64, params.N())
for i := 0; i < params.N(); i++ {
data[s][i] = utils.RandFloat64(-1, 1)
}
}
nonces = make([][]byte, params.N())
for i := 0; i < params.N(); i++ {
nonces[i] = make([]byte, 64)
rand.Read(nonces[i])
}
keystream = make([][]uint64, params.N())
for i := 0; i < params.N(); i++ {
keystream[i] = plainHera(numRound, nonces[i], key, params.PlainModulus())
}
for s := 0; s < 16; s++ {
for i := 0; i < params.N()/2; i++ {
j := utils.BitReverse64(uint64(i), uint64(params.LogN()-1))
coeffs[s][j] = data[s][i]
coeffs[s][j+uint64(params.N()/2)] = data[s][i+params.N()/2]
}
}
plainCKKSRingTs = make([]*PlaintextRingT, 16)
for s := 0; s < 16; s++ {
plainCKKSRingTs[s] = ckksEncoder.EncodeCoeffsRingTNew(coeffs[s], messageScaling)
poly := plainCKKSRingTs[s].Value()[0]
for i := 0; i < params.N(); i++ {
j := utils.BitReverse64(uint64(i), uint64(params.LogN()))
poly.Coeffs[0][j] = (poly.Coeffs[0][j] + keystream[i][s]) % params.PlainModulus()
}
}
} else {
data = make([][]float64, 16)
for s := 0; s < 16; s++ {
data[s] = make([]float64, params.Slots())
for i := 0; i < params.Slots(); i++ {
data[s][i] = utils.RandFloat64(-1, 1)
}
}
nonces = make([][]byte, params.Slots())
for i := 0; i < params.Slots(); i++ {
nonces[i] = make([]byte, 64)
rand.Read(nonces[i])
}
keystream = make([][]uint64, params.Slots())
for i := 0; i < params.Slots(); i++ {
keystream[i] = plainHera(numRound, nonces[i], key, params.PlainModulus())
}
for s := 0; s < 16; s++ {
for i := 0; i < params.Slots()/2; i++ {
j := utils.BitReverse64(uint64(i), uint64(params.LogN()-1))
coeffs[s][j] = data[s][i]
coeffs[s][j+uint64(params.N()/2)] = data[s][i+params.Slots()/2]
}
}
plainCKKSRingTs = make([]*PlaintextRingT, 16)
for s := 0; s < 16; s++ {
plainCKKSRingTs[s] = ckksEncoder.EncodeCoeffsRingTNew(coeffs[s], messageScaling)
poly := plainCKKSRingTs[s].Value()[0]
for i := 0; i < params.Slots(); i++ {
j := utils.BitReverse64(uint64(i), uint64(params.LogN()))
poly.Coeffs[0][j] = (poly.Coeffs[0][j] + keystream[i][s]) % params.PlainModulus()
}
}
}
plaintexts = make([]*Plaintext, 16)
for s := 0; s < 16; s++ {
plaintexts[s] = NewPlaintextFVLvl(params, 0)
fvEncoder.FVScaleUp(plainCKKSRingTs[s], plaintexts[s])
}
hera = NewMFVHera(numRound, params, fvEncoder, fvEncryptor, fvEvaluator, heraModDown[0])
kCt := hera.EncKey(key)
// FV Keystream
benchOffLat := fmt.Sprintf("RtF HERA Offline Latency")
b.Run(benchOffLat, func(b *testing.B) {
fvKeystreams = hera.Crypt(nonces, kCt, heraModDown)
for i := 0; i < 1; i++ {
fvKeystreams[i] = fvEvaluator.SlotsToCoeffs(fvKeystreams[i], stcModDown)
fvEvaluator.ModSwitchMany(fvKeystreams[i], fvKeystreams[i], fvKeystreams[i].Level())
}
})
/* We assume that b.N == 1 */
benchOffThrput := fmt.Sprintf("RtF HERA Offline Throughput")
b.Run(benchOffThrput, func(b *testing.B) {
for i := 1; i < 16; i++ {
fvKeystreams[i] = fvEvaluator.SlotsToCoeffs(fvKeystreams[i], stcModDown)
fvEvaluator.ModSwitchMany(fvKeystreams[i], fvKeystreams[i], fvKeystreams[i].Level())
}
})
var ctBoot *Ciphertext
benchOnline := fmt.Sprintf("RtF HERA Online Lat x1")
b.Run(benchOnline, func(b *testing.B) {
// Encrypt and mod switch to the lowest level
ciphertext := NewCiphertextFVLvl(params, 1, 0)
ciphertext.Value()[0] = plaintexts[0].Value()[0].CopyNew()
fvEvaluator.Sub(ciphertext, fvKeystreams[0], ciphertext)
fvEvaluator.TransformToNTT(ciphertext, ciphertext)
ciphertext.SetScale(math.Exp2(math.Round(math.Log2(float64(params.Qi()[0]) / float64(params.PlainModulus()) * messageScaling))))
// Half-Bootstrap the ciphertext (homomorphic evaluation of ModRaise -> SubSum -> CtS -> EvalMod)
// It takes a ciphertext at level 0 (if not at level 0, then it will reduce it to level 0)
// and returns a ciphertext at level MaxLevel - k, where k is the depth of the bootstrapping circuit.
// Difference from the bootstrapping is that the last StC is missing.
// CAUTION: the scale of the ciphertext MUST be equal (or very close) to params.Scale
// To equalize the scale, the function evaluator.SetScale(ciphertext, parameters.Scale) can be used at the expense of one level.
if fullCoeffs {
ctBoot, _ = hbtp.HalfBoot(ciphertext, false)
} else {
ctBoot, _ = hbtp.HalfBoot(ciphertext, true)
}
})
valuesWant := make([]complex128, params.Slots())
for i := 0; i < params.Slots(); i++ {
valuesWant[i] = complex(data[0][i], 0)
}
fmt.Println("Precision of HalfBoot(ciphertext)")
printDebug(params, ctBoot, valuesWant, ckksDecryptor, ckksEncoder)
}
func benchmarkRtFRubato(b *testing.B, rubatoParam int) {
var err error
var hbtp *HalfBootstrapper
var kgen KeyGenerator
var fvEncoder MFVEncoder
var ckksEncoder CKKSEncoder
var ckksDecryptor CKKSDecryptor
var sk *SecretKey
var pk *PublicKey
var fvEncryptor MFVEncryptor
var fvEvaluator MFVEvaluator
var plainCKKSRingTs []*PlaintextRingT
var plaintexts []*Plaintext
var rubato MFVRubato
var data [][]float64
var nonces [][]byte
var counter []byte
var key []uint64
var keystream [][]uint64
var fvKeystreams []*Ciphertext
// Rubato parameter
blocksize := RubatoParams[rubatoParam].Blocksize
outputsize := blocksize - 4
numRound := RubatoParams[rubatoParam].NumRound
plainModulus := RubatoParams[rubatoParam].PlainModulus
sigma := RubatoParams[rubatoParam].Sigma
// RtF Rubato parameters
hbtpParams := RtFRubatoParams[0]
params, err := hbtpParams.Params()
if err != nil {
panic(err)
}
params.SetPlainModulus(plainModulus)
params.SetLogFVSlots(params.LogN())
messageScaling := float64(params.PlainModulus()) / hbtpParams.MessageRatio
rubatoModDown := RubatoModDownParams[rubatoParam].CipherModDown
stcModDown := RubatoModDownParams[rubatoParam].StCModDown
// Scheme context and keys
kgen = NewKeyGenerator(params)
sk, pk = kgen.GenKeyPairSparse(hbtpParams.H)
fvEncoder = NewMFVEncoder(params)
ckksEncoder = NewCKKSEncoder(params)
fvEncryptor = NewMFVEncryptorFromPk(params, pk)
ckksDecryptor = NewCKKSDecryptor(params, sk)
// Generating half-bootstrapping keys
rotationsHalfBoot := kgen.GenRotationIndexesForHalfBoot(params.LogSlots(), hbtpParams)
pDcds := fvEncoder.GenSlotToCoeffMatFV(2) // radix = 2
rotationsStC := kgen.GenRotationIndexesForSlotsToCoeffsMat(pDcds)
rotations := append(rotationsHalfBoot, rotationsStC...)
rotkeys := kgen.GenRotationKeysForRotations(rotations, true, sk)
rlk := kgen.GenRelinearizationKey(sk)
hbtpKey := BootstrappingKey{Rlk: rlk, Rtks: rotkeys}
if hbtp, err = NewHalfBootstrapper(params, hbtpParams, hbtpKey); err != nil {
panic(err)
}
// Encode float data added by keystream to plaintext coefficients
fvEvaluator = NewMFVEvaluator(params, EvaluationKey{Rlk: rlk, Rtks: rotkeys}, pDcds)
coeffs := make([][]float64, outputsize)
for s := 0; s < outputsize; s++ {
coeffs[s] = make([]float64, params.N())
}
key = make([]uint64, blocksize)
for i := 0; i < blocksize; i++ {
key[i] = uint64(i + 1) // Use (1, ..., 16) for testing
}
// Get random data in [-1, 1]
data = make([][]float64, outputsize)
for s := 0; s < outputsize; s++ {
data[s] = make([]float64, params.N())
for i := 0; i < params.N(); i++ {
data[s][i] = utils.RandFloat64(-1, 1)
}
}
nonces = make([][]byte, params.N())
for i := 0; i < params.N(); i++ {
nonces[i] = make([]byte, 8)
rand.Read(nonces[i])
}
counter = make([]byte, 8)
rand.Read(counter)
// Get keystream
keystream = make([][]uint64, params.N())
for i := 0; i < params.N(); i++ {
keystream[i] = plainRubato(blocksize, numRound, nonces[i], counter, key, plainModulus, sigma)
}
for s := 0; s < outputsize; s++ {
for i := 0; i < params.N()/2; i++ {
j := utils.BitReverse64(uint64(i), uint64(params.LogN()-1))
coeffs[s][j] = data[s][i]
coeffs[s][j+uint64(params.N()/2)] = data[s][i+params.N()/2]
}
}
// Encode plaintext
plainCKKSRingTs = make([]*PlaintextRingT, outputsize)
for s := 0; s < outputsize; s++ {
plainCKKSRingTs[s] = ckksEncoder.EncodeCoeffsRingTNew(coeffs[s], messageScaling)
poly := plainCKKSRingTs[s].Value()[0]
for i := 0; i < params.N(); i++ {
j := utils.BitReverse64(uint64(i), uint64(params.LogN()))
poly.Coeffs[0][j] = (poly.Coeffs[0][j] + keystream[i][s]) % params.PlainModulus()
}
}
plaintexts = make([]*Plaintext, outputsize)
for s := 0; s < outputsize; s++ {
plaintexts[s] = NewPlaintextFVLvl(params, 0)
fvEncoder.FVScaleUp(plainCKKSRingTs[s], plaintexts[s])
}
// FV Keystream
rubato = NewMFVRubato(rubatoParam, params, fvEncoder, fvEncryptor, fvEvaluator, rubatoModDown[0])
kCt := rubato.EncKey(key)
benchOffLat := fmt.Sprintf("RtF Rubato Offline Latency")
b.Run(benchOffLat, func(b *testing.B) {
fvKeystreams = rubato.Crypt(nonces, counter, kCt, rubatoModDown)
for i := 0; i < 1; i++ {
fvKeystreams[i] = fvEvaluator.SlotsToCoeffs(fvKeystreams[i], stcModDown)
fvEvaluator.ModSwitchMany(fvKeystreams[i], fvKeystreams[i], fvKeystreams[i].Level())
}
})
/* We assume that b.N == 1 */
benchOffThrput := fmt.Sprintf("RtF Rubato Offline Throughput")
b.Run(benchOffThrput, func(b *testing.B) {
for i := 1; i < outputsize; i++ {
fvKeystreams[i] = fvEvaluator.SlotsToCoeffs(fvKeystreams[i], stcModDown)
fvEvaluator.ModSwitchMany(fvKeystreams[i], fvKeystreams[i], fvKeystreams[i].Level())
}
})
var ctBoot *Ciphertext
benchOnline := fmt.Sprintf("RtF Rubato Online Lat x1")
b.Run(benchOnline, func(b *testing.B) {
// Encrypt and mod switch to the lowest level
ciphertext := NewCiphertextFVLvl(params, 1, 0)
ciphertext.Value()[0] = plaintexts[0].Value()[0].CopyNew()
fvEvaluator.Sub(ciphertext, fvKeystreams[0], ciphertext)
fvEvaluator.TransformToNTT(ciphertext, ciphertext)
ciphertext.SetScale(math.Exp2(math.Round(math.Log2(float64(params.Qi()[0]) / float64(params.PlainModulus()) * messageScaling))))
// Half-Bootstrap the ciphertext (homomorphic evaluation of ModRaise -> SubSum -> CtS -> EvalMod)
// It takes a ciphertext at level 0 (if not at level 0, then it will reduce it to level 0)
// and returns a ciphertext at level MaxLevel - k, where k is the depth of the bootstrapping circuit.
// Difference from the bootstrapping is that the last StC is missing.
// CAUTION: the scale of the ciphertext MUST be equal (or very close) to params.Scale
// To equalize the scale, the function evaluator.SetScale(ciphertext, parameters.Scale) can be used at the expense of one level.
ctBoot, _ = hbtp.HalfBoot(ciphertext, false)
})
valuesWant := make([]complex128, params.Slots())
for i := 0; i < params.Slots(); i++ {
valuesWant[i] = complex(data[0][i], 0)
}
fmt.Println("Precision of HalfBoot(ciphertext)")
printDebug(params, ctBoot, valuesWant, ckksDecryptor, ckksEncoder)
}
func printDebug(params *Parameters, ciphertext *Ciphertext, valuesWant []complex128, decryptor CKKSDecryptor, encoder CKKSEncoder) {
valuesTest := encoder.DecodeComplex(decryptor.DecryptNew(ciphertext), params.LogSlots())
logSlots := params.LogSlots()
sigma := params.Sigma()
fmt.Printf("Level: %d (logQ = %d)\n", ciphertext.Level(), params.LogQLvl(ciphertext.Level()))
fmt.Printf("Scale: 2^%f\n", math.Log2(ciphertext.Scale()))
fmt.Printf("ValuesTest: %6.10f %6.10f %6.10f %6.10f...\n", valuesTest[0], valuesTest[1], valuesTest[2], valuesTest[3])
fmt.Printf("ValuesWant: %6.10f %6.10f %6.10f %6.10f...\n", valuesWant[0], valuesWant[1], valuesWant[2], valuesWant[3])
precStats := GetPrecisionStats(params, encoder, nil, valuesWant, valuesTest, logSlots, sigma)
fmt.Println(precStats.String())
}
func plainHera(roundNum int, nonce []byte, key []uint64, plainModulus uint64) (state []uint64) {
nr := roundNum
xof := sha3.NewShake256()
xof.Write(nonce)
state = make([]uint64, 16)
rks := make([][]uint64, nr+1)
for r := 0; r <= nr; r++ {
rks[r] = make([]uint64, 16)
for st := 0; st < 16; st++ {
rks[r][st] = SampleZqx(xof, plainModulus) * key[st] % plainModulus
}
}
for i := 0; i < 16; i++ {
state[i] = uint64(i + 1)
}
// round0
for st := 0; st < 16; st++ {
state[st] = (state[st] + rks[0][st]) % plainModulus
}
for r := 1; r < roundNum; r++ {
for col := 0; col < 4; col++ {
y0 := 2*state[col] + 3*state[col+4] + 1*state[col+8] + 1*state[col+12]
y1 := 2*state[col+4] + 3*state[col+8] + 1*state[col+12] + 1*state[col]
y2 := 2*state[col+8] + 3*state[col+12] + 1*state[col] + 1*state[col+4]
y3 := 2*state[col+12] + 3*state[col] + 1*state[col+4] + 1*state[col+8]
state[col] = y0 % plainModulus
state[col+4] = y1 % plainModulus
state[col+8] = y2 % plainModulus
state[col+12] = y3 % plainModulus
}
for row := 0; row < 4; row++ {
y0 := 2*state[4*row] + 3*state[4*row+1] + 1*state[4*row+2] + 1*state[4*row+3]
y1 := 2*state[4*row+1] + 3*state[4*row+2] + 1*state[4*row+3] + 1*state[4*row]
y2 := 2*state[4*row+2] + 3*state[4*row+3] + 1*state[4*row] + 1*state[4*row+1]
y3 := 2*state[4*row+3] + 3*state[4*row] + 1*state[4*row+1] + 1*state[4*row+2]
state[4*row] = y0 % plainModulus
state[4*row+1] = y1 % plainModulus
state[4*row+2] = y2 % plainModulus
state[4*row+3] = y3 % plainModulus
}
for st := 0; st < 16; st++ {
state[st] = (state[st] * state[st] % plainModulus) * state[st] % plainModulus
}
for st := 0; st < 16; st++ {
state[st] = (state[st] + rks[r][st]) % plainModulus
}
}
for col := 0; col < 4; col++ {
y0 := 2*state[col] + 3*state[col+4] + 1*state[col+8] + 1*state[col+12]
y1 := 2*state[col+4] + 3*state[col+8] + 1*state[col+12] + 1*state[col]
y2 := 2*state[col+8] + 3*state[col+12] + 1*state[col] + 1*state[col+4]
y3 := 2*state[col+12] + 3*state[col] + 1*state[col+4] + 1*state[col+8]
state[col] = y0 % plainModulus
state[col+4] = y1 % plainModulus
state[col+8] = y2 % plainModulus
state[col+12] = y3 % plainModulus
}
for row := 0; row < 4; row++ {
y0 := 2*state[4*row] + 3*state[4*row+1] + 1*state[4*row+2] + 1*state[4*row+3]
y1 := 2*state[4*row+1] + 3*state[4*row+2] + 1*state[4*row+3] + 1*state[4*row]
y2 := 2*state[4*row+2] + 3*state[4*row+3] + 1*state[4*row] + 1*state[4*row+1]
y3 := 2*state[4*row+3] + 3*state[4*row] + 1*state[4*row+1] + 1*state[4*row+2]
state[4*row] = y0 % plainModulus
state[4*row+1] = y1 % plainModulus
state[4*row+2] = y2 % plainModulus
state[4*row+3] = y3 % plainModulus
}
for st := 0; st < 16; st++ {
state[st] = (state[st] * state[st] % plainModulus) * state[st] % plainModulus
}
for col := 0; col < 4; col++ {
y0 := 2*state[col] + 3*state[col+4] + 1*state[col+8] + 1*state[col+12]
y1 := 2*state[col+4] + 3*state[col+8] + 1*state[col+12] + 1*state[col]
y2 := 2*state[col+8] + 3*state[col+12] + 1*state[col] + 1*state[col+4]
y3 := 2*state[col+12] + 3*state[col] + 1*state[col+4] + 1*state[col+8]
state[col] = y0 % plainModulus
state[col+4] = y1 % plainModulus
state[col+8] = y2 % plainModulus
state[col+12] = y3 % plainModulus
}
for row := 0; row < 4; row++ {
y0 := 2*state[4*row] + 3*state[4*row+1] + 1*state[4*row+2] + 1*state[4*row+3]
y1 := 2*state[4*row+1] + 3*state[4*row+2] + 1*state[4*row+3] + 1*state[4*row]
y2 := 2*state[4*row+2] + 3*state[4*row+3] + 1*state[4*row] + 1*state[4*row+1]
y3 := 2*state[4*row+3] + 3*state[4*row] + 1*state[4*row+1] + 1*state[4*row+2]
state[4*row] = y0 % plainModulus
state[4*row+1] = y1 % plainModulus
state[4*row+2] = y2 % plainModulus
state[4*row+3] = y3 % plainModulus
}
for st := 0; st < 16; st++ {
state[st] = (state[st] + rks[roundNum][st]) % plainModulus
}
return
}
func plainRubato(blocksize int, numRound int, nonce []byte, counter []byte, key []uint64, plainModulus uint64, sigma float64) (state []uint64) {
xof := sha3.NewShake256()
xof.Write(nonce)
xof.Write(counter)
state = make([]uint64, blocksize)
prng, err := utils.NewPRNG()
if err != nil {
panic(err)
}
gaussianSampler := ring.NewGaussianSampler(prng)
rks := make([][]uint64, numRound+1)
for r := 0; r <= numRound; r++ {
rks[r] = make([]uint64, blocksize)
for i := 0; i < blocksize; i++ {
rks[r][i] = SampleZqx(xof, plainModulus) * key[i] % plainModulus
}
}
for i := 0; i < blocksize; i++ {
state[i] = uint64(i + 1)
}
// Initial AddRoundKey
for i := 0; i < blocksize; i++ {
state[i] = (state[i] + rks[0][i]) % plainModulus
}
// Round Functions
for r := 1; r < numRound; r++ {
rubatoLinearLayer(state, plainModulus)
rubatoFeistel(state, plainModulus)
for i := 0; i < blocksize; i++ {
state[i] = (state[i] + rks[r][i]) % plainModulus
}
}
// Finalization
rubatoLinearLayer(state, plainModulus)
rubatoFeistel(state, plainModulus)
rubatoLinearLayer(state, plainModulus)
if sigma > 0 {
rubatoAddGaussianNoise(state, plainModulus, gaussianSampler, sigma)
}
for i := 0; i < blocksize; i++ {
state[i] = (state[i] + rks[numRound][i]) % plainModulus
}
state = state[0 : blocksize-4]
return
}
func rubatoLinearLayer(state []uint64, plainModulus uint64) {
blocksize := len(state)
buf := make([]uint64, blocksize)
if blocksize == 16 {
// MixColumns
for row := 0; row < 4; row++ {
for col := 0; col < 4; col++ {
buf[row*4+col] = 2 * state[row*4+col]
buf[row*4+col] += 3 * state[((row+1)%4)*4+col]
buf[row*4+col] += state[((row+2)%4)*4+col]
buf[row*4+col] += state[((row+3)%4)*4+col]
buf[row*4+col] %= plainModulus
}
}
// MixRows
for row := 0; row < 4; row++ {
for col := 0; col < 4; col++ {
state[row*4+col] = 2 * buf[row*4+col]
state[row*4+col] += 3 * buf[row*4+(col+1)%4]
state[row*4+col] += buf[row*4+(col+2)%4]
state[row*4+col] += buf[row*4+(col+3)%4]
state[row*4+col] %= plainModulus
}
}
} else if blocksize == 36 {
// MixColumns
for row := 0; row < 6; row++ {
for col := 0; col < 6; col++ {
buf[row*6+col] = 4 * state[row*6+col]
buf[row*6+col] += 2 * state[((row+1)%6)*6+col]
buf[row*6+col] += 4 * state[((row+2)%6)*6+col]
buf[row*6+col] += 3 * state[((row+3)%6)*6+col]
buf[row*6+col] += state[((row+4)%6)*6+col]
buf[row*6+col] += state[((row+5)%6)*6+col]
buf[row*6+col] %= plainModulus
}
}
// MixRows
for row := 0; row < 6; row++ {
for col := 0; col < 6; col++ {
state[row*6+col] = 4 * buf[row*6+col]
state[row*6+col] += 2 * buf[row*6+(col+1)%6]
state[row*6+col] += 4 * buf[row*6+(col+2)%6]
state[row*6+col] += 3 * buf[row*6+(col+3)%6]
state[row*6+col] += buf[row*6+(col+4)%6]
state[row*6+col] += buf[row*6+(col+5)%6]
state[row*6+col] %= plainModulus
}
}
} else if blocksize == 64 {
// MixColumns
for row := 0; row < 8; row++ {
for col := 0; col < 8; col++ {
buf[row*8+col] = 5 * state[row*8+col]
buf[row*8+col] += 3 * state[((row+1)%8)*8+col]
buf[row*8+col] += 4 * state[((row+2)%8)*8+col]
buf[row*8+col] += 3 * state[((row+3)%8)*8+col]
buf[row*8+col] += 6 * state[((row+4)%8)*8+col]
buf[row*8+col] += 2 * state[((row+5)%8)*8+col]
buf[row*8+col] += state[((row+6)%8)*8+col]
buf[row*8+col] += state[((row+7)%8)*8+col]
buf[row*8+col] %= plainModulus
}
}
// MixRows
for row := 0; row < 8; row++ {
for col := 0; col < 8; col++ {
state[row*8+col] = 5 * buf[row*8+col]
state[row*8+col] += 3 * buf[row*8+(col+1)%8]
state[row*8+col] += 4 * buf[row*8+(col+2)%8]
state[row*8+col] += 3 * buf[row*8+(col+3)%8]
state[row*8+col] += 6 * buf[row*8+(col+4)%8]
state[row*8+col] += 2 * buf[row*8+(col+5)%8]
state[row*8+col] += buf[row*8+(col+6)%8]
state[row*8+col] += buf[row*8+(col+7)%8]
state[row*8+col] %= plainModulus
}
}
} else {
panic("Invalid blocksize")
}
}
func rubatoFeistel(state []uint64, plainModulus uint64) {
blocksize := len(state)
buf := make([]uint64, blocksize)
for i := 0; i < blocksize; i++ {
buf[i] = state[i]
}
for i := 1; i < blocksize; i++ {
state[i] = (buf[i] + buf[i-1]*buf[i-1]) % plainModulus
}
}
func rubatoAddGaussianNoise(state []uint64, plainModulus uint64, gaussianSampler *ring.GaussianSampler, sigma float64) {
bound := int(6 * sigma)
gaussianSampler.AGN(state, plainModulus, sigma, bound)
}