-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathML_KEM.cs
More file actions
1379 lines (1149 loc) · 40.8 KB
/
ML_KEM.cs
File metadata and controls
1379 lines (1149 loc) · 40.8 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
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
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using Waher.Security.SHA3;
namespace Waher.Security.PQC
{
/// <summary>
/// Implements the ML-KEM algorithm for post-quantum cryptography, as defined in
/// NIST FIPS 203: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.203.pdf
/// </summary>
public class ML_KEM : ML_Common
{
/// <summary>
/// Model parameters for a required RBG strength 128 (cryptographic security strength),
/// as defined in §8.
/// </summary>
public static readonly ML_KEM ML_KEM_512 = new ML_KEM(2, 3, 2, 10, 4);
/// <summary>
/// Model parameters for a required RBG strength 192 (cryptographic security strength),
/// as defined in §8.
/// </summary>
public static readonly ML_KEM ML_KEM_768 = new ML_KEM(3, 2, 2, 10, 4);
/// <summary>
/// Model parameters for a required RBG strength 256 (cryptographic security strength),
/// as defined in §8.
/// </summary>
public static readonly ML_KEM ML_KEM_1024 = new ML_KEM(4, 2, 2, 11, 5);
/// <summary>
/// Gets a model by name, as defined in §8.
/// </summary>
/// <param name="Name">Name of model.</param>
/// <returns>Reference to model.</returns>
/// <exception cref="ArgumentException">Model name not recognized.</exception>
public static ML_KEM GetModel(string Name)
{
switch (Name.ToUpper())
{
case "ML-KEM-512":
return ML_KEM_512;
case "ML-KEM-768":
return ML_KEM_768;
case "ML-KEM-1024":
return ML_KEM_1024;
default:
throw new ArgumentException("Unknown model name: " + Name, nameof(Name));
}
}
/// <summary>
/// The following 128 numbers are the values of 𝜁^BitRev7(𝑖) mod 𝑞 for 𝑖 ∈ {0,…,127}.
/// These numbers are used in Algorithms 9 and 10.
/// </summary>
private static readonly ushort[] nttTransformZeta =
{
1, 1729, 2580, 3289, 2642, 630, 1897, 848,
1062, 1919, 193, 797, 2786, 3260, 569, 1746,
296, 2447, 1339, 1476, 3046, 56, 2240, 1333,
1426, 2094, 535, 2882, 2393, 2879, 1974, 821,
289, 331, 3253, 1756, 1197, 2304, 2277, 2055,
650, 1977, 2513, 632, 2865, 33, 1320, 1915,
2319, 1435, 807, 452, 1438, 2868, 1534, 2402,
2647, 2617, 1481, 648, 2474, 3110, 1227, 910,
17, 2761, 583, 2649, 1637, 723, 2288, 1100,
1409, 2662, 3281, 233, 756, 2156, 3015, 3050,
1703, 1651, 2789, 1789, 1847, 952, 1461, 2687,
939, 2308, 2437, 2388, 733, 2337, 268, 641,
1584, 2298, 2037, 3220, 375, 2549, 2090, 1645,
1063, 319, 2773, 757, 2099, 561, 2466, 2594,
2804, 1092, 403, 1026, 1143, 2150, 2775, 886,
1722, 1212, 1874, 1029, 2110, 2935, 885, 2154
};
/// <summary>
/// When implementing Algorithm 11, the values 𝜁^2BitRev7(𝑖)+1 mod 𝑞 need to be
/// computed. The following array contains these values for 𝑖 ∈ {0,…,127}:
/// </summary>
private static readonly ushort[] nttTransformZeta2 =
{
17, q - 17, 2761, q - 2761, 583, q - 583, 2649, q - 2649,
1637, q - 1637, 723, q - 723, 2288, q - 2288, 1100, q - 1100,
1409, q - 1409, 2662, q - 2662, 3281, q - 3281, 233, q - 233,
756, q - 756, 2156, q - 2156, 3015, q - 3015, 3050, q - 3050,
1703, q - 1703, 1651, q - 1651, 2789, q - 2789, 1789, q - 1789,
1847, q - 1847, 952, q - 952, 1461, q - 1461, 2687, q - 2687,
939, q - 939, 2308, q - 2308, 2437, q - 2437, 2388, q - 2388,
733, q - 733, 2337, q - 2337, 268, q - 268, 641, q - 641,
1584, q - 1584, 2298, q - 2298, 2037, q - 2037, 3220, q - 3220,
375, q - 375, 2549, q - 2549, 2090, q - 2090, 1645, q - 1645,
1063, q - 1063, 319, q - 319, 2773, q - 2773, 757, q - 757,
2099, q - 2099, 561, q - 561, 2466, q - 2466, 2594, q - 2594,
2804, q - 2804, 1092, q - 1092, 403, q - 403, 1026, q - 1026,
1143, q - 1143, 2150, q - 2150, 2775, q - 2775, 886, q - 886,
1722, q - 1722, 1212, q - 1212, 1874, q - 1874, 1029, q - 1029,
2110, q - 2110, 2935, q - 2935, 885, q - 885, 2154, q - 2154
};
private const int n = 256;
private const ushort q = 3329;
private const ushort half_q_rounded = (q + 1) >> 1;
private readonly int k384;
private readonly int cipherTextLength;
private readonly byte k;
private readonly int η1;
private readonly int η2;
private readonly int dᵤ;
private readonly int dᵥ;
/// <summary>
/// Implements the ML-KEM algorithm for post-quantum cryptography, as defined in
/// NIST FIPS 203: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.203.pdf
/// </summary>
/// <param name="k">Moduel dimension 𝑘 ∈ {2,3,4}</param>
/// <param name="η1">Specifies the distribution for generating the vectors s and s.</param>
/// <param name="η2">Specifies the distribution for generating the vectors 𝐞1 and e2.</param>
/// <param name="dᵤ">Number of bits used to compress the vector u.</param>
/// <param name="dᵥ">Number of bits used to compress the vector v.</param>
public ML_KEM(byte k, int η1, int η2, int dᵤ, int dᵥ)
{
this.k = k;
this.η1 = η1;
this.η2 = η2;
this.dᵤ = dᵤ;
this.dᵥ = dᵥ;
this.k384 = this.k * 384;
this.cipherTextLength = 32 * (this.dᵤ * this.k + this.dᵥ);
}
/// <summary>
/// Length of the public key.
/// </summary>
public override int PublicKeyLength => 384 * this.k + 32;
/// <summary>
/// Length of the private key.
/// </summary>
public override int PrivateKeyLength => 768 * this.k + 96;
/// <summary>
/// Pseudorandom function (PRF), as defined in §4.1.
/// </summary>
/// <param name="Seed">32-byte input.</param>
/// <param name="Data">1-byte input.</param>
/// <param name="η">2 or 3</param>
/// <returns>Pseudo-random output.</returns>
public static byte[] PRF(byte[] Seed, byte Data, int η)
{
SHAKE256 HashFunction = new SHAKE256(8 * 64 * η);
int c = Seed.Length;
byte[] Bin = new byte[c + 1];
Buffer.BlockCopy(Seed, 0, Bin, 0, c);
Bin[c] = Data;
byte[] Result = HashFunction.ComputeVariable(Bin);
Clear(Bin);
return Result;
}
/// <summary>
/// Hash function H, as defined in §4.1.
/// </summary>
/// <param name="Data">Input data.</param>
/// <returns>Hash digest.</returns>
public static byte[] H(byte[] Data)
{
return new SHA3_256().ComputeVariable(Data);
}
/// <summary>
/// Hash function J, as defined in §4.1.
/// </summary>
/// <param name="Data">Input data.</param>
/// <returns>Hash digest.</returns>
public static byte[] J(byte[] Data)
{
return new SHAKE256(n).ComputeVariable(Data);
}
/// <summary>
/// Hash function G, as defined in §4.1.
/// </summary>
/// <param name="Data">Input data.</param>
/// <returns>Hash digest.</returns>
public static byte[] G(byte[] Data)
{
return new SHA3_512().ComputeVariable(Data);
}
/// <summary>
/// eXtendable-Output Function (XOF), as defined in §4.1.
/// </summary>
/// <param name="Input">Input data.</param>
/// <param name="OutputLength">Number of bytes requested for the output data.</param>
/// <returns>Output data.</returns>
public static byte[] XOF(byte[] Input, int OutputLength)
{
return new SHAKE128(OutputLength << 3).ComputeVariable(Input);
}
/// <summary>
/// Compress function, as defined in §4.2.1.
/// </summary>
/// <param name="Value">Value</param>
/// <param name="d">Number of bits between 1 and 12.</param>
/// <returns>Compressed value.</returns>
/// <exception cref="ArgumentOutOfRangeException">If d lies outside of the valid range.</exception>
public static ushort Compress(ushort Value, int d)
{
if (d < 1 || d > 12)
throw new ArgumentOutOfRangeException(nameof(d), "d must be between 1 and 12.");
uint i = Value;
i <<= d + 1;
i /= q;
i++;
i >>= 1;
return (ushort)(i & ushortBitMask[d]);
}
/// <summary>
/// Canonical extension of Compress function, as defined in §4.2.1.
/// </summary>
/// <param name="Value">Vector to be compressed.</param>
/// <param name="d">Number of bits between 1 and 12.</param>
/// <exception cref="ArgumentOutOfRangeException">If d lies outside of the valid range.</exception>
public static void Compress(ushort[] Value, int d)
{
if (d < 1 || d > 12)
throw new ArgumentOutOfRangeException(nameof(d), "d must be between 1 and 12.");
int j, c = Value.Length;
uint i;
d++;
for (j = 0; j < c; j++)
{
i = Value[j];
i <<= d;
i /= q;
i++;
i >>= 1;
Value[j] = (ushort)i;
}
}
/// <summary>
/// Canonical extension of Compress function, as defined in §4.2.1.
/// </summary>
/// <param name="Value">Array of vectors to be compressed.</param>
/// <param name="d">Number of bits between 1 and 12.</param>
/// <exception cref="ArgumentOutOfRangeException">If d lies outside of the valid range.</exception>
public static void Compress(ushort[][] Value, int d)
{
int i, c = Value.Length;
for (i = 0; i < c; i++)
Compress(Value[i], d);
}
/// <summary>
/// Decompress function, as defined in §4.2.1.
/// </summary>
/// <param name="Value">Value</param>
/// <param name="d">Number of bits between 1 and 12.</param>
/// <returns>Decompressed value.</returns>
/// <exception cref="ArgumentOutOfRangeException">If d lies outside of the valid range.</exception>
public static ushort Decompress(ushort Value, int d)
{
if (d < 1 || d > 12)
throw new ArgumentOutOfRangeException(nameof(d), "d must be between 1 and 12.");
uint i = Value;
i *= q;
i >>= d - 1;
i++;
i >>= 1;
return (ushort)i;
}
/// <summary>
/// Canonical extension of Decompress function, as defined in §4.2.1.
/// </summary>
/// <param name="Value">Vector to be decompressed.</param>
/// <param name="d">Number of bits between 1 and 12.</param>
/// <exception cref="ArgumentOutOfRangeException">If d lies outside of the valid range.</exception>
public static void Decompress(ushort[] Value, int d)
{
if (d < 1 || d > 12)
throw new ArgumentOutOfRangeException(nameof(d), "d must be between 1 and 12.");
int j, c = Value.Length;
uint i;
for (j = 0; j < c; j++)
{
i = Value[j];
i *= q;
i >>= d - 1;
i++;
i >>= 1;
Value[j] = (ushort)i;
}
}
/// <summary>
/// Encodes an array of integers (mod 2^d) into a byte array, as defined by
/// Algorithm 5 in §4.2.1.
/// </summary>
/// <param name="Values">Array of integers.</param>
/// <param name="d">Number of bits, between 1 and 12.</param>
/// <returns>Byte array.</returns>
/// <exception cref="ArgumentOutOfRangeException">If d lies outside of the valid range.</exception>
public static byte[] ByteEncode(ushort[] Values, int d)
{
int c = Values.Length;
int NrBits = c * d;
int NrBytes = (NrBits + 7) >> 3;
byte[] Result = new byte[NrBytes];
ByteEncode(Values, d, Result, 0);
return Result;
}
/// <summary>
/// Encodes an array of integers (mod 2^d) into a byte array, as defined by
/// Algorithm 5 in §4.2.1.
/// </summary>
/// <param name="Values">Array of integers.</param>
/// <param name="d">Number of bits, between 1 and 12.</param>
/// <param name="Output">Bytes will be encoded into this array.</param>
/// <param name="Index">Index into output array where encoding will begin.</param>
/// <returns>Number of bytes encoded.</returns>
/// <exception cref="ArgumentOutOfRangeException">If d lies outside of the valid range.</exception>
public static int ByteEncode(ushort[] Values, int d, byte[] Output, int Index)
{
if (d < 1 || d > 12)
throw new ArgumentOutOfRangeException(nameof(d), "d must be between 1 and 12.");
int i, c = Values.Length;
int BitOffset = 0;
int Index0 = Index;
for (i = 0; i < c; i++)
{
ushort Value = Values[i];
Value &= ushortBitMask[d];
Output[Index] |= (byte)(Value << BitOffset);
BitOffset += d;
while (BitOffset >= 8)
{
Index++;
BitOffset -= 8;
if (BitOffset > 0)
Output[Index] = (byte)(Value >> (d - BitOffset));
}
}
return Index - Index0;
}
/// <summary>
/// Encodes an array of vectors of integers (mod 2^d) into a byte array. Canonical
/// extension of <see cref="ByteEncode(ushort[], int, byte[], int)"/>.
/// </summary>
/// <param name="Values">Array of integers.</param>
/// <param name="d">Number of bits, between 1 and 12.</param>
/// <param name="Output">Bytes will be encoded into this array.</param>
/// <param name="Index">Index into output array where encoding will begin.</param>
/// <returns>Number of bytes encoded.</returns>
/// <exception cref="ArgumentOutOfRangeException">If d lies outside of the valid range.</exception>
public static int ByteEncode(ushort[][] Values, int d, byte[] Output, int Index)
{
int Pos = Index;
int i, c = Values.Length;
for (i = 0; i < c; i++)
{
int NrBytes = ByteEncode(Values[i], d, Output, Pos);
Pos += NrBytes;
}
return Pos - Index;
}
/// <summary>
/// Decodes an array of integers (mod 2^d) from a byte array, as defined by
/// Algorithm 6 in §4.2.1.
/// </summary>
/// <param name="Data">Array of bytes.</param>
/// <param name="d">Number of bits, between 1 and 12.</param>
/// <returns>Integer array.</returns>
/// <exception cref="ArgumentOutOfRangeException">If d lies outside of the valid range.</exception>
public static ushort[] ByteDecode(byte[] Data, int d)
{
return ByteDecode(Data, 0, Data.Length, d);
}
/// <summary>
/// Decodes an array of integers (mod 2^d) from a byte array, as defined by
/// Algorithm 6 in §4.2.1.
/// </summary>
/// <param name="Data">Array of bytes.</param>
/// <param name="Offset">Start offset into byte array to decode.</param>
/// <param name="Length">Number of bytes to decode.</param>
/// <param name="d">Number of bits, between 1 and 12.</param>
/// <returns>Integer array.</returns>
/// <exception cref="ArgumentOutOfRangeException">If d lies outside of the valid range.</exception>
public static ushort[] ByteDecode(byte[] Data, int Offset, int Length, int d)
{
if (d < 1 || d > 12)
throw new ArgumentOutOfRangeException(nameof(d), "d must be between 1 and 12.");
int NrBits = Length << 3;
int NrIntegers = (NrBits + d - 1) / d;
ushort[] Result = new ushort[NrIntegers];
int Pos = 0;
int BitOffset = 0;
int BitsLeft;
int BitsToUse;
while (Length-- > 0)
{
byte b = Data[Offset++];
BitsLeft = 8;
while (BitsLeft > 0)
{
BitsToUse = d - BitOffset;
if (BitsToUse > BitsLeft)
BitsToUse = BitsLeft;
Result[Pos] |= (ushort)((b & ushortBitMask[BitsToUse]) << BitOffset);
BitOffset += BitsToUse;
BitsLeft -= BitsToUse;
b >>= BitsToUse;
if (BitOffset >= d)
{
Pos++;
BitOffset = 0;
}
}
}
return Result;
}
/// <summary>
/// The algorithm SampleNTT (Algorithm 7, §4.2.1) converts a seed together with two
/// indexing bytes into a polynomial in the NTT domain. If the seed is uniformly
/// random, the resulting polynomial will be drawn from a distribution that is
/// computationally indistinguishable from the uniform distribution 𝑇𝑞.
/// </summary>
/// <param name="Seed">Seed value</param>
/// <returns>Sample in 𝑇𝑞</returns>
private static ushort[] SampleNTT(byte[] Seed)
{
SHAKE128 HashFunction = new SHAKE128(0);
Keccak1600.Context Context = HashFunction.Absorb(Seed);
ushort[] Result = new ushort[n];
int Pos = 0;
while (Pos < n)
{
byte[] C = Context.Squeeze(3);
ushort d1 = (ushort)(C[0] + ((C[1] & 15) << 8));
ushort d2 = (ushort)((C[1] >> 4) + (C[2] << 4));
if (d1 < q)
Result[Pos++] = d1;
if (d2 < q && Pos < n)
Result[Pos++] = d2;
}
return Result;
}
/// <summary>
/// The algorithm SamplePolyCBD (Algorithm 8, §4.2.1) samples the coefficient array
/// of a polynomial 𝑓 ∈ 𝑅𝑞 according to the distribution D𝜂(𝑅𝑞), provided that
/// its input is a stream of uniformly random bytes.
/// </summary>
/// <param name="η">Can be 2 or 3.</param>
/// <returns>Sample polynomial</returns>
public static ushort[] SamplePolyCBD(int η)
{
if (η < 2 || η > 3)
throw new ArgumentOutOfRangeException(nameof(η), "η must be either 2 or 3.");
return SamplePolyCBD(CreateSeed(η << 6));
}
/// <summary>
/// The algorithm SamplePolyCBD (Algorithm 8, §4.2.1) samples the coefficient array
/// of a polynomial 𝑓 ∈ 𝑅𝑞 according to the distribution D𝜂(𝑅𝑞), provided that
/// its input is a stream of uniformly random bytes.
/// </summary>
/// <param name="Seed">128 (𝜂=2) or 192 (𝜂=3) bytes seed value.</param>
/// <returns>Sample polynomial</returns>
public static ushort[] SamplePolyCBD(byte[] Seed)
{
int c = Seed.Length;
if (c != 128 && c != 192)
throw new ArgumentOutOfRangeException(nameof(Seed), "Seed must be either 128 or 192 bytes.");
int η = c >> 6;
int i, j, k;
ushort x, y;
ushort dx, dy;
ushort[] Result = new ushort[n];
for (i = 0; i < n; i++)
{
for (j = x = y = 0; j < η; j++)
{
k = 2 * i * η + j;
dx = (Seed[k >> 3] & (1 << (k & 7))) != 0 ? (ushort)1 : (ushort)0;
x += dx; // To avoid different CPU instructions to execute based on if bit is 0 or 1.
k += η;
dy = (Seed[k >> 3] & (1 << (k & 7))) != 0 ? (ushort)1 : (ushort)0;
y += dy; // To avoid different CPU instructions to execute based on if bit is 0 or 1.
}
if (x < y)
Result[i] = (ushort)(q + x - y);
else
Result[i] = (ushort)(x - y);
}
return Result;
}
/// <summary>
/// Computes the NTT representation f̂ of the given polynomial f ∈ 𝑅𝑞.
/// (Algorithm 9 in §4.3)
/// </summary>
/// <param name="f">Polynomial in 𝑅𝑞</param>
public static void NTT(ushort[] f)
{
if (f.Length != n)
throw new ArgumentException("Polynomial must have " + n + " coefficients.", nameof(f));
int i = 1;
int j;
int Len;
int Start;
ushort ζ;
ushort t;
for (Len = n >> 1; Len >= 2; Len >>= 1)
{
for (Start = 0; Start < n; Start += Len << 1)
{
ζ = nttTransformZeta[i++];
for (j = Start; j < Start + Len; j++)
{
t = (ushort)(ζ * f[j + Len] % q);
f[j + Len] = (ushort)((f[j] + q - t) % q);
f[j] = (ushort)((f[j] + t) % q);
}
}
}
}
/// <summary>
/// Canonical extension of <see cref="NTT(ushort[])"/>.
/// </summary>
/// <param name="f">Array of polynomials in 𝑅𝑞</param>
public static void NTT(ushort[][] f)
{
int i, c = f.Length;
for (i = 0; i < c; i++)
NTT(f[i]);
}
/// <summary>
/// Computes the NTT^-1 representation f of the given polynomial f̂ ∈ 𝑇𝑞.
/// (Algorithm 10 in §4.3)
/// </summary>
/// <param name="f">polynomial f̂ ∈ 𝑇𝑞</param>
public static void InverseNTT(ushort[] f)
{
if (f.Length != n)
throw new ArgumentException("Polynomial must have " + n + " coefficients.", nameof(f));
int i = 127;
int j;
int Len;
int Len2;
int StartLen;
int Start;
ushort ζ;
ushort t;
for (Len = 2; Len <= 128; Len <<= 1)
{
Len2 = Len << 1;
for (Start = 0; Start < n; Start += Len2)
{
ζ = nttTransformZeta[i--];
StartLen = Start + Len;
for (j = Start; j < StartLen; j++)
{
t = f[j];
f[j] = (ushort)((t + f[j + Len]) % q);
f[j + Len] = (ushort)(ζ * (f[j + Len] + q - t) % q);
}
}
}
for (i = 0; i < n; i++)
f[i] = (ushort)(3303 * f[i] % q); // 3303 = 128^-1 mod q
}
/// <summary>
/// Canonical extension of <see cref="InverseNTT(ushort[])"/>.
/// </summary>
/// <param name="f">Array of polynomials f̂ ∈ 𝑇𝑞</param>
public static void InverseNTT(ushort[][] f)
{
int i, c = f.Length;
for (i = 0; i < c; i++)
InverseNTT(f[i]);
}
/// <summary>
/// Computes the product (in the ring 𝑇𝑞) of two NTT representations.
/// (Algorithm 11 in §4.3.1)
/// </summary>
/// <param name="f">Polynomial 1</param>
/// <param name="g">Polynomial 2</param>
/// <returns>f*g in 𝑇𝑞</returns>
/// <exception cref="ArgumentException">If polynomials are not of the correct size.</exception>
public static ushort[] MultiplyNTTs(ushort[] f, ushort[] g)
{
ushort[] Result = new ushort[n];
MultiplyNTTsAndAdd(f, g, Result);
return Result;
}
/// <summary>
/// Computes the product (in the ring 𝑇𝑞) of two NTT representations
/// (Algorithm 11 in §4.3.1) and adds the result to a result vector.
/// </summary>
/// <param name="f">Polynomial 1</param>
/// <param name="g">Polynomial 2</param>
/// <param name="Result">Result vector.</param>
/// <returns>f*g in 𝑇𝑞</returns>
/// <exception cref="ArgumentException">If polynomials are not of the correct size.</exception>
public static void MultiplyNTTsAndAdd(ushort[] f, ushort[] g, ushort[] Result)
{
if (f.Length != n || g.Length != n)
throw new ArgumentException("Polynomials must have " + n + " coefficients.", nameof(f));
int i;
ushort a0, a1, b0, b1, γ;
for (i = 0; i < n; i += 2)
{
a0 = f[i];
a1 = f[i + 1];
b0 = g[i];
b1 = g[i + 1];
γ = nttTransformZeta2[i >> 1];
Result[i] = (ushort)((Result[i] + a0 * b0 + γ * a1 % q * b1) % q); // Three multiplications without modulus can generate integer overflow.
Result[i + 1] = (ushort)((Result[i + 1] + a0 * b1 + a1 * b0) % q);
}
}
/// <summary>
/// Adds <paramref name="g"/> to <paramref name="f"/>.
/// </summary>
/// <param name="f">Polynomial that will be incremented by <paramref name="g"/>.</param>
/// <param name="g">Polynomial to add to <paramref name="f"/>.</param>
public static void AddTo(ushort[] f, ushort[] g)
{
int i;
for (i = 0; i < n; i++)
f[i] = (ushort)((f[i] + g[i]) % q);
}
/// <summary>
/// Adds vector <paramref name="g"/> to vector <paramref name="f"/>.
/// </summary>
/// <param name="f">Vector of polynomials that will be incremented by <paramref name="g"/>.</param>
/// <param name="g">Vector of polynomials to add to <paramref name="f"/>.</param>
public static void AddTo(ushort[][] f, ushort[][] g)
{
int i, c = f.Length;
if (g.Length != c)
throw new ArgumentException("Vectors must have the same number of polynomials.", nameof(g));
for (i = 0; i < c; i++)
AddTo(f[i], g[i]);
}
/// <summary>
/// Negates a polynomial in 𝑅𝑞.
/// </summary>
/// <param name="f">Polynomial</param>
public static void Negate(ushort[] f)
{
int i, c = f.Length;
for (i = 0; i < c; i++)
f[i] = (ushort)((q - f[i]) % q); // To avoid different CPU instructions to execute based on if bit is 0 or 1.
}
/// <summary>
/// Computes the dot product of two vectors of polynomials in 𝑇𝑞.
/// </summary>
/// <param name="v1">First vector.</param>
/// <param name="v2">Second vector.</param>
/// <returns>Dot product in 𝑇𝑞</returns>
/// <exception cref="ArgumentException"></exception>
public static ushort[] DotProductNTT(ushort[][] v1, ushort[][] v2)
{
int i, c = v1.Length;
if (v2.Length != c)
throw new ArgumentException("Vectors must have the same number of polynomials.", nameof(v2));
ushort[] Result = new ushort[n];
for (i = 0; i < c; i++)
MultiplyNTTsAndAdd(v1[i], v2[i], Result);
return Result;
}
/// <summary>
/// Uses randomness to generate an encryption key and a corresponding decryption key.
/// (Algorithm 13 K-PKE.KeyGen(𝑑) in §5.1)
/// </summary>
/// <returns>Public Encryption Key (384k+32 bytes) and Private Decryption Key
/// (384k bytes). Matrix used to calculate public key is also provided.</returns>
public K_PKE_Keys K_PKE_KeyGen()
{
byte[] Seed = CreateSeed();
K_PKE_Keys Result = this.K_PKE_KeyGen(Seed);
Clear(Seed);
return Result;
}
/// <summary>
/// Uses randomness to generate an encryption key and a corresponding decryption key.
/// (Algorithm 13 K-PKE.KeyGen(𝑑) in §5.1)
/// </summary>
/// <param name="d">Randomness (32 bytes)</param>
/// <returns>Public Encryption Key (384k+32 bytes) and Private Decryption Key
/// (384k bytes). Matrix used to calculate public key is also provided.</returns>
public K_PKE_Keys K_PKE_KeyGen(byte[] d)
{
if (d.Length != 32)
throw new ArgumentException("Seed must be 32 bytes long.", nameof(d));
byte[] Temp = new byte[33];
Buffer.BlockCopy(d, 0, Temp, 0, 32);
Temp[32] = this.k;
byte[] Bin = G(Temp);
Clear(Temp);
byte[] ρ = new byte[32];
Buffer.BlockCopy(Bin, 0, ρ, 0, 32);
byte[] σ = new byte[32];
Buffer.BlockCopy(Bin, 32, σ, 0, 32);
Clear(Bin);
byte N = 0;
int i, j;
ushort[,][] Â = new ushort[this.k, this.k][];
ushort[][] s = new ushort[this.k][];
ushort[][] e = new ushort[this.k][];
byte[] B = new byte[34];
Buffer.BlockCopy(ρ, 0, B, 0, 32);
for (i = 0; i < this.k; i++)
{
B[33] = (byte)i;
for (j = 0; j < this.k; j++)
{
B[32] = (byte)j;
Â[i, j] = SampleNTT(B);
}
}
Clear(B);
for (i = 0; i < this.k; i++)
s[i] = SamplePolyCBD(PRF(σ, N++, this.η1));
for (i = 0; i < this.k; i++)
e[i] = SamplePolyCBD(PRF(σ, N++, this.η1));
NTT(s);
NTT(e);
ushort[][] t = new ushort[this.k][];
ushort[] f;
for (i = 0; i < this.k; i++)
{
t[i] = f = (ushort[])e[i].Clone();
for (j = 0; j < this.k; j++)
MultiplyNTTsAndAdd(Â[i, j], s[j], f);
}
byte[] EncryptionKey = new byte[32 + this.k384];
byte[] DecryptionKey = new byte[this.k384];
int Pos = 0;
for (i = 0; i < this.k; i++)
{
ByteEncode(t[i], 12, EncryptionKey, Pos);
ByteEncode(s[i], 12, DecryptionKey, Pos);
Pos += 384;
}
Buffer.BlockCopy(ρ, 0, EncryptionKey, Pos, 32);
Clear(ρ);
Clear(σ);
Clear(s);
Clear(t);
Clear(e);
return new K_PKE_Keys(Â, EncryptionKey, DecryptionKey);
}
/// <summary>
/// Uses the encryption key to encrypt a plaintext message using the given randomness
/// and algorithm 14 (K-PKE.Encrypt) of §5.2.
/// </summary>
/// <param name="EncryptionKey">Encryption key (384k+32 bytes)</param>
/// <param name="Message">Plain text message (32 bytes)</param>
/// <returns>Ciphertext (32*(dᵤk+dᵥ) bytes)</returns>
public byte[] K_PKE_Encrypt(byte[] EncryptionKey, byte[] Message)
{
return this.K_PKE_Encrypt(ML_KEM_Keys.FromEncapsulationKey(EncryptionKey), Message);
}
/// <summary>
/// Uses the encryption key to encrypt a plaintext message using the given randomness
/// and algorithm 14 (K-PKE.Encrypt) of §5.2.
/// </summary>
/// <param name="Keys">Encryption key (384k+32 bytes)</param>
/// <param name="Message">Plain text message (32 bytes)</param>
/// <returns>Ciphertext (32*(dᵤk+dᵥ) bytes)</returns>
public byte[] K_PKE_Encrypt(ML_KEM_Keys Keys, byte[] Message)
{
byte[] Seed = CreateSeed();
byte[] Result = this.K_PKE_Encrypt(Keys, Message, Seed);
Clear(Seed);
return Result;
}
/// <summary>
/// Uses the encryption key to encrypt a plaintext message using the given randomness
/// and algorithm 14 (K-PKE.Encrypt) of §5.2.
/// </summary>
/// <param name="EncryptionKey">Encryption key (384k+32 bytes)</param>
/// <param name="Message">Plain text message (32 bytes)</param>
/// <param name="Seed">Randomness (32 bytes)</param>
/// <returns>Ciphertext (32*(dᵤk+dᵥ) bytes)</returns>
public byte[] K_PKE_Encrypt(byte[] EncryptionKey, byte[] Message, byte[] Seed)
{
return this.K_PKE_Encrypt(ML_KEM_Keys.FromEncapsulationKey(EncryptionKey), Message, Seed);
}
/// <summary>
/// Uses the encryption key to encrypt a plaintext message using the given randomness
/// and algorithm 14 (K-PKE.Encrypt) of §5.2.
/// </summary>
/// <param name="Keys">Encryption key (384k+32 bytes)</param>
/// <param name="Message">Plain text message (32 bytes)</param>
/// <param name="Seed">Randomness (32 bytes)</param>
/// <returns>Ciphertext (32*(dᵤk+dᵥ) bytes)</returns>
public byte[] K_PKE_Encrypt(ML_KEM_Keys Keys, byte[] Message, byte[] Seed)
{
if (Keys is null)
throw new ArgumentNullException(nameof(Keys), "Encryption key cannot be null.");
if (Keys.EncapsulationKey.Length != this.k384 + 32)
throw new ArgumentException("Encryption key must be 384k+32 bytes long.", nameof(Keys));
if (Message.Length != 32)
throw new ArgumentException("Message must be 32 bytes long.", nameof(Message));
ushort[][] t = new ushort[this.k][];
byte[] ρ = new byte[32];
int Pos;
int i, j;
byte N = 0;
byte k;
for (i = Pos = 0; i < this.k; i++)
{
t[i] = ByteDecode(Keys.EncapsulationKey, Pos, 384, 12);
Pos += 384;
}
Buffer.BlockCopy(Keys.EncapsulationKey, Pos, ρ, 0, 32);
ushort[][] y = new ushort[this.k][];
ushort[][] e1 = new ushort[this.k][];
ushort[] e2;
ushort[,][] Â = Keys.Â;
if (Â is null)
{
Keys. =  = new ushort[this.k, this.k][];
byte[] B = new byte[34];
Buffer.BlockCopy(ρ, 0, B, 0, 32);
for (i = 0; i < this.k; i++)
{
B[33] = (byte)i;
for (j = 0; j < this.k; j++)
{
B[32] = (byte)j;
Â[i, j] = SampleNTT(B);
}
}
Clear(B);
}
else if (Â.GetLength(0) != this.k || Â.GetLength(1) != this.k)
throw new ArgumentException("Matrix  must be " + this.k + "x" + this.k + ".", nameof(Â));
for (i = 0; i < this.k; i++)
y[i] = SamplePolyCBD(PRF(Seed, N++, this.η1));
for (i = 0; i < this.k; i++)
e1[i] = SamplePolyCBD(PRF(Seed, N++, this.η2));
e2 = SamplePolyCBD(PRF(Seed, N++, this.η2));
NTT(y);
ushort[][] u = new ushort[this.k][];
ushort[] f;
for (i = 0; i < this.k; i++)
{
u[i] = f = new ushort[n];
for (j = 0; j < this.k; j++)
MultiplyNTTsAndAdd(Â[j, i], y[j], f);
}
InverseNTT(u);
AddTo(u, e1);
ushort[] v = DotProductNTT(t, y);
InverseNTT(v);
ushort[] μ = new ushort[n];
for (i = j = 0, k = 1; i < n; i++)
{
μ[i] = (Message[j] & k) != 0 ? half_q_rounded : (ushort)0;
k <<= 1;
if (k == 0)
{
k = 1;
j++;
}
}