kpqc 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +15 -0
  3. data/README.md +9 -6
  4. data/THIRD_PARTY_NOTICES.md +14 -47
  5. data/ext/kpqc/extconf_helper.rb +6 -3
  6. data/ext/kpqc/secure_clear.c +8 -0
  7. data/kpqc.gemspec +4 -2
  8. data/lib/kpqc/aimer.rb +6 -6
  9. data/lib/kpqc/version.rb +1 -1
  10. data/test/kat/aimer128f.rsp +4 -4
  11. data/test/kat/aimer128s.rsp +4 -4
  12. data/test/kat/aimer192f.rsp +4 -4
  13. data/test/kat/aimer192s.rsp +4 -4
  14. data/test/kat/aimer256f.rsp +4 -4
  15. data/test/kat/aimer256s.rsp +4 -4
  16. data/test/kat/haetae2.rsp +1 -1
  17. data/test/kat/haetae3.rsp +1 -1
  18. data/test/kat/haetae5.rsp +1 -1
  19. data/vendor/AIMer/LICENSE +1 -1
  20. data/vendor/AIMer/aim3.c +227 -0
  21. data/vendor/AIMer/aim3.h +63 -0
  22. data/vendor/AIMer/field.h +12 -4
  23. data/vendor/AIMer/field128.c +24 -381
  24. data/vendor/AIMer/field192.c +24 -400
  25. data/vendor/AIMer/field256.c +28 -447
  26. data/vendor/AIMer/field_common.c +98 -0
  27. data/vendor/AIMer/field_internal.h +218 -0
  28. data/vendor/AIMer/params/params-aimer-128f.h +8 -8
  29. data/vendor/AIMer/params/params-aimer-128s.h +8 -8
  30. data/vendor/AIMer/params/params-aimer-192f.h +8 -8
  31. data/vendor/AIMer/params/params-aimer-192s.h +8 -8
  32. data/vendor/AIMer/params/params-aimer-256f.h +8 -8
  33. data/vendor/AIMer/params/params-aimer-256s.h +8 -8
  34. data/vendor/AIMer/params.h +5 -0
  35. data/vendor/AIMer/sign.c +246 -167
  36. data/vendor/AIMer/sign.h +6 -6
  37. data/vendor/AIMer/tree.c +10 -4
  38. data/vendor/AIMer/tree.h +1 -1
  39. data/vendor/HAETAE/include/fixpoint.h +14 -14
  40. data/vendor/HAETAE/include/rans_byte.h +10 -9
  41. data/vendor/HAETAE/src/decompose.c +8 -4
  42. data/vendor/HAETAE/src/encoding.c +5 -5
  43. data/vendor/HAETAE/src/fft.c +1 -1
  44. data/vendor/HAETAE/src/fips202.c +26 -14
  45. data/vendor/HAETAE/src/fixpoint.c +21 -18
  46. data/vendor/HAETAE/src/ntt.c +2 -0
  47. data/vendor/HAETAE/src/packing.c +15 -15
  48. data/vendor/HAETAE/src/poly.c +30 -3
  49. data/vendor/HAETAE/src/polyfix.c +2 -2
  50. data/vendor/HAETAE/src/polymat.c +2 -2
  51. data/vendor/HAETAE/src/polyvec.c +7 -5
  52. data/vendor/HAETAE/src/reduce.c +2 -0
  53. data/vendor/HAETAE/src/sampler.c +231 -47
  54. data/vendor/HAETAE/src/sign.c +11 -10
  55. data/vendor/NTRUplus/NTRU+1152/api.h +27 -8
  56. data/vendor/NTRUplus/NTRU+1152/fips202/fips202.c +12 -269
  57. data/vendor/NTRUplus/NTRU+1152/fips202/fips202.h +4 -113
  58. data/vendor/NTRUplus/NTRU+1152/kem.c +110 -40
  59. data/vendor/NTRUplus/NTRU+1152/ntt.c +50 -54
  60. data/vendor/NTRUplus/NTRU+1152/poly.c +21 -13
  61. data/vendor/NTRUplus/NTRU+1152/poly.h +3 -3
  62. data/vendor/NTRUplus/NTRU+1152/randombytes.h +1 -0
  63. data/vendor/NTRUplus/NTRU+1152/symmetric.c +3 -9
  64. data/vendor/NTRUplus/NTRU+1152/symmetric.h +13 -3
  65. data/vendor/NTRUplus/NTRU+1152/util.h +15 -0
  66. data/vendor/NTRUplus/NTRU+768/api.h +27 -8
  67. data/vendor/NTRUplus/NTRU+768/fips202/fips202.c +12 -269
  68. data/vendor/NTRUplus/NTRU+768/fips202/fips202.h +4 -113
  69. data/vendor/NTRUplus/NTRU+768/kem.c +110 -40
  70. data/vendor/NTRUplus/NTRU+768/ntt.c +50 -54
  71. data/vendor/NTRUplus/NTRU+768/poly.c +23 -13
  72. data/vendor/NTRUplus/NTRU+768/poly.h +3 -3
  73. data/vendor/NTRUplus/NTRU+768/randombytes.h +1 -0
  74. data/vendor/NTRUplus/NTRU+768/symmetric.c +3 -9
  75. data/vendor/NTRUplus/NTRU+768/symmetric.h +13 -3
  76. data/vendor/NTRUplus/NTRU+768/util.h +15 -0
  77. data/vendor/NTRUplus/NTRU+864/api.h +27 -8
  78. data/vendor/NTRUplus/NTRU+864/fips202/fips202.c +12 -269
  79. data/vendor/NTRUplus/NTRU+864/fips202/fips202.h +4 -113
  80. data/vendor/NTRUplus/NTRU+864/kem.c +110 -40
  81. data/vendor/NTRUplus/NTRU+864/ntt.c +24 -28
  82. data/vendor/NTRUplus/NTRU+864/poly.c +26 -12
  83. data/vendor/NTRUplus/NTRU+864/poly.h +3 -3
  84. data/vendor/NTRUplus/NTRU+864/randombytes.h +1 -0
  85. data/vendor/NTRUplus/NTRU+864/symmetric.c +3 -9
  86. data/vendor/NTRUplus/NTRU+864/symmetric.h +13 -3
  87. data/vendor/NTRUplus/NTRU+864/util.h +15 -0
  88. data/vendor/SMAUG-T/src/cbd.c +4 -4
  89. data/vendor/SMAUG-T/src/ciphertext.c +2 -2
  90. data/vendor/SMAUG-T/src/dg.c +2 -2
  91. data/vendor/SMAUG-T/src/hwt.c +4 -4
  92. data/vendor/SMAUG-T/src/indcpa.c +5 -5
  93. data/vendor/SMAUG-T/src/kem.c +9 -7
  94. data/vendor/SMAUG-T/src/key.c +2 -2
  95. data/vendor/SMAUG-T/src/pack.c +8 -8
  96. metadata +16 -13
  97. data/vendor/AIMer/aim2.c +0 -163
  98. data/vendor/AIMer/aim2.h +0 -36
  99. data/vendor/AIMer/aim2_constant.h +0 -74
  100. data/vendor/SOURCES.json +0 -130
@@ -1,244 +1,33 @@
1
1
  // SPDX-License-Identifier: MIT
2
2
 
3
3
  #include "field.h"
4
+ #include "field_internal.h"
4
5
  #include <stddef.h>
5
6
  #include <stdint.h>
6
7
 
7
- void gf_to_bytes(uint8_t *out, const gf in)
8
+ static void gf_mod(gf c, const uint64_t a[2 * AIM3_NUM_WORDS_FIELD])
8
9
  {
9
- int i, j;
10
- for (i = 0; i < AIM2_NUM_WORDS_FIELD; i++)
11
- {
12
- uint64_t u = in[i];
13
- for (j = 0; j < 8; j++)
14
- {
15
- *out++ = u;
16
- u >>= 8;
17
- }
18
- }
19
- }
10
+ uint64_t t = a[4] ^ ((a[7] >> 54) ^ (a[7] >> 59) ^ (a[7] >> 62));
20
11
 
21
- void gf_from_bytes(gf out, const uint8_t *in)
22
- {
23
- int i, j;
24
- for (i = 0; i < AIM2_NUM_WORDS_FIELD; i++)
25
- {
26
- uint64_t u = 0;
27
- for (j = 7; j >= 0; j--)
28
- {
29
- u = (u << 8) | in[8 * i + j];
30
- }
31
- out[i] = u;
32
- }
33
- }
12
+ c[3] = a[3] ^ a[7];
13
+ c[3] ^= (a[7] << 10) | (a[6] >> 54);
14
+ c[3] ^= (a[7] << 5) | (a[6] >> 59);
15
+ c[3] ^= (a[7] << 2) | (a[6] >> 62);
34
16
 
35
- void gf_set0(gf a)
36
- {
37
- a[0] = 0;
38
- a[1] = 0;
39
- a[2] = 0;
40
- a[3] = 0;
41
- }
17
+ c[2] = a[2] ^ a[6];
18
+ c[2] ^= (a[6] << 10) | (a[5] >> 54);
19
+ c[2] ^= (a[6] << 5) | (a[5] >> 59);
20
+ c[2] ^= (a[6] << 2) | (a[5] >> 62);
42
21
 
43
- void gf_copy(gf out, const gf in)
44
- {
45
- out[0] = in[0];
46
- out[1] = in[1];
47
- out[2] = in[2];
48
- out[3] = in[3];
49
- }
22
+ c[1] = a[1] ^ a[5];
23
+ c[1] ^= (a[5] << 10) | (t >> 54);
24
+ c[1] ^= (a[5] << 5) | (t >> 59);
25
+ c[1] ^= (a[5] << 2) | (t >> 62);
50
26
 
51
- void gf_add(gf c, const gf a, const gf b)
52
- {
53
- c[0] = a[0] ^ b[0];
54
- c[1] = a[1] ^ b[1];
55
- c[2] = a[2] ^ b[2];
56
- c[3] = a[3] ^ b[3];
57
- }
58
-
59
- static void poly64_mul(uint64_t *z1, uint64_t *z0, uint64_t x0, uint64_t y0)
60
- {
61
- const uint64_t C0 = 0x5555555555555555;
62
- const uint64_t C1 = 0x3333333333333333;
63
- const uint64_t C2 = 0x0f0f0f0f0f0f0f0f;
64
- const uint64_t C3 = 0x00ff00ff00ff00ff;
65
- const uint64_t C4 = 0x0000ffff0000ffff;
66
- const uint64_t C5 = 0x00000000ffffffff;
67
- uint64_t x1, x2, x3, x4, x5, x6, x7;
68
- uint64_t y1, y2, y3, y4, y5, y6, y7;
69
- uint64_t f0, f1, f2, f3, f4, f5, f6, f7;
70
- uint64_t g0, g1, g2, g3, g4, g5, g6, g7;
71
- uint64_t s, t;
72
-
73
- x1 = x0 ^ (x0 >> 32); // will ignore outside C5
74
- x2 = ((x0 ^ (x0 >> 16)) & C4) | ((x1 ^ (x1 << 16)) & (C4 << 16));
75
- x3 = ((x0 ^ (x0 >> 8)) & C3) | ((x1 ^ (x1 << 8)) & (C3 << 8));
76
- x4 = x2 ^ (x2 >> 8); // will ignore outside C3
77
- x5 = ((x0 ^ (x0 >> 4)) & C2) | ((x1 ^ (x1 << 4)) & (C2 << 4));
78
- x6 = ((x2 ^ (x2 >> 4)) & C2) | ((x3 ^ (x3 << 4)) & (C2 << 4));
79
- x7 = x4 ^ (x4 >> 4); // will ignore outside C2
80
- s = ((x0 >> 2) ^ x2) & C1;
81
- x0 ^= s << 2;
82
- x2 ^= s;
83
- s = ((x1 >> 2) ^ x3) & C1;
84
- x1 ^= s << 2;
85
- x3 ^= s;
86
- s = ((x4 >> 2) ^ x6) & C1;
87
- x4 ^= s << 2;
88
- x6 ^= s;
89
- s = ((x5 >> 2) ^ x7) & C1;
90
- x5 ^= s << 2;
91
- x7 ^= s;
92
- s = ((x0 >> 1) ^ x1) & C0;
93
- x0 ^= s << 1;
94
- x1 ^= s;
95
- s = ((x2 >> 1) ^ x3) & C0;
96
- x2 ^= s << 1;
97
- x3 ^= s;
98
- s = ((x4 >> 1) ^ x5) & C0;
99
- x4 ^= s << 1;
100
- x5 ^= s;
101
- s = ((x6 >> 1) ^ x7) & C0;
102
- x6 ^= s << 1;
103
- x7 ^= s;
104
-
105
- y1 = y0 ^ (y0 >> 32);
106
- y2 = ((y0 ^ (y0 >> 16)) & C4) | ((y1 ^ (y1 << 16)) & (C4 << 16));
107
- y3 = ((y0 ^ (y0 >> 8)) & C3) | ((y1 ^ (y1 << 8)) & (C3 << 8));
108
- y4 = y2 ^ (y2 >> 8);
109
- y5 = ((y0 ^ (y0 >> 4)) & C2) | ((y1 ^ (y1 << 4)) & (C2 << 4));
110
- y6 = ((y2 ^ (y2 >> 4)) & C2) | ((y3 ^ (y3 << 4)) & (C2 << 4));
111
- y7 = y4 ^ (y4 >> 4);
112
- s = ((y0 >> 2) ^ y2) & C1;
113
- y0 ^= s << 2;
114
- y2 ^= s;
115
- s = ((y1 >> 2) ^ y3) & C1;
116
- y1 ^= s << 2;
117
- y3 ^= s;
118
- s = ((y4 >> 2) ^ y6) & C1;
119
- y4 ^= s << 2;
120
- y6 ^= s;
121
- s = ((y5 >> 2) ^ y7) & C1;
122
- y5 ^= s << 2;
123
- y7 ^= s;
124
- s = ((y0 >> 1) ^ y1) & C0;
125
- y0 ^= s << 1;
126
- y1 ^= s;
127
- s = ((y2 >> 1) ^ y3) & C0;
128
- y2 ^= s << 1;
129
- y3 ^= s;
130
- s = ((y4 >> 1) ^ y5) & C0;
131
- y4 ^= s << 1;
132
- y5 ^= s;
133
- s = ((y6 >> 1) ^ y7) & C0;
134
- y6 ^= s << 1;
135
- y7 ^= s;
136
-
137
- f0 = x0 & y0;
138
- f1 = (x0 & y1) ^ (x1 & y0);
139
- f2 = (x0 & y2) ^ (x1 & y1) ^ (x2 & y0);
140
- f3 = (x0 & y3) ^ (x1 & y2) ^ (x2 & y1) ^ (x3 & y0);
141
- g0 = (x1 & y3) ^ (x2 & y2) ^ (x3 & y1);
142
- g1 = (x2 & y3) ^ (x3 & y2);
143
- g2 = x3 & y3;
144
-
145
- f4 = x4 & y4;
146
- f5 = (x4 & y5) ^ (x5 & y4);
147
- f6 = (x4 & y6) ^ (x5 & y5) ^ (x6 & y4);
148
- f7 = (x4 & y7) ^ (x5 & y6) ^ (x6 & y5) ^ (x7 & y4);
149
- g4 = (x5 & y7) ^ (x6 & y6) ^ (x7 & y5);
150
- g5 = (x6 & y7) ^ (x7 & y6);
151
- g6 = x7 & y7;
152
-
153
- s = ((f0 >> 1) ^ f1) & C0;
154
- f0 ^= s << 1;
155
- f1 ^= s;
156
- s = ((f2 >> 1) ^ f3) & C0;
157
- f2 ^= s << 1;
158
- f3 ^= s;
159
- s = ((f0 >> 2) ^ f2) & C1;
160
- f0 ^= s << 2;
161
- f2 ^= s;
162
- s = ((f1 >> 2) ^ f3) & C1;
163
- f1 ^= s << 2;
164
- f3 ^= s;
165
- s = ((g0 >> 1) ^ g1) & C0;
166
- g0 ^= s << 1;
167
- g1 ^= s;
168
- s = (g2 >> 1) & C0;
169
- g2 ^= s << 1;
170
- g3 = s;
171
- s = ((g0 >> 2) ^ g2) & C1;
172
- g0 ^= s << 2;
173
- g2 ^= s;
174
- s = ((g1 >> 2) ^ g3) & C1;
175
- g1 ^= s << 2;
176
- g3 ^= s;
177
-
178
- s = ((f4 >> 1) ^ f5) & C0;
179
- f4 ^= s << 1;
180
- f5 ^= s;
181
- s = ((f6 >> 1) ^ f7) & C0;
182
- f6 ^= s << 1;
183
- f7 ^= s;
184
- s = ((f4 >> 2) ^ f6) & C1;
185
- f4 ^= s << 2;
186
- f6 ^= s;
187
- s = ((f5 >> 2) ^ f7) & C1;
188
- f5 ^= s << 2;
189
- f7 ^= s;
190
- s = ((g4 >> 1) ^ g5) & C0;
191
- g4 ^= s << 1;
192
- g5 ^= s;
193
- s = (g6 >> 1) & C0;
194
- g6 ^= s << 1;
195
- g7 = s;
196
- s = ((g4 >> 2) ^ g6) & C1;
197
- g4 ^= s << 2;
198
- g6 ^= s;
199
- s = ((g5 >> 2) ^ g7) & C1;
200
- g5 ^= s << 2;
201
- g7 ^= s;
202
-
203
- t = f0 ^ g0;
204
- f0 ^= ((f5 ^ t) & C2) << 4;
205
- g0 ^= (g5 ^ (t >> 4)) & C2;
206
- t = f1 ^ g1;
207
- f1 ^= (f5 ^ (t << 4)) & (C2 << 4);
208
- g1 ^= ((g5 ^ t) >> 4) & C2;
209
- t = f2 ^ g2;
210
- f2 ^= ((f6 ^ t) & C2) << 4;
211
- g2 ^= (g6 ^ (t >> 4)) & C2;
212
- t = f3 ^ g3;
213
- f3 ^= (f6 ^ (t << 4)) & (C2 << 4);
214
- g3 ^= ((g6 ^ t) >> 4) & C2;
215
- t = f4 ^ g4;
216
- f4 ^= ((f7 ^ t) & C2) << 4;
217
- g4 ^= (g7 ^ (t >> 4)) & C2;
218
-
219
- t = f0 ^ g0;
220
- f0 ^= ((f3 ^ t) & C3) << 8;
221
- g0 ^= (g3 ^ (t >> 8)) & C3;
222
- t = f1 ^ g1;
223
- f1 ^= (f3 ^ (t << 8)) & (C3 << 8);
224
- g1 ^= ((g3 ^ t) >> 8) & C3;
225
- t = f2 ^ g2;
226
- f2 ^= ((f4 ^ t) & C3) << 8;
227
- g2 ^= (g4 ^ (t >> 8)) & C3;
228
-
229
- t = f0 ^ g0;
230
- f0 ^= ((f2 ^ t) & C4) << 16;
231
- g0 ^= (g2 ^ (t >> 16)) & C4;
232
- t = f1 ^ g1;
233
- f1 ^= (f2 ^ (t << 16)) & (C4 << 16);
234
- g1 ^= ((g2 ^ t) >> 16) & C4;
235
-
236
- t = f0 ^ g0;
237
- f0 ^= ((t ^ f1) & C5) << 32;
238
- g0 ^= ((t >> 32) ^ g1) & C5;
239
-
240
- *z0 = f0;
241
- *z1 = g0;
27
+ c[0] = a[0] ^ t;
28
+ c[0] ^= (t << 10);
29
+ c[0] ^= (t << 5);
30
+ c[0] ^= (t << 2);
242
31
  }
243
32
 
244
33
  void gf_mul(gf c, const gf a, const gf b)
@@ -294,132 +83,11 @@ void gf_mul(gf c, const gf a, const gf b)
294
83
  temp[3] ^= t[0];
295
84
  temp[4] ^= t[1];
296
85
 
297
- t[0] = temp[4] ^ ((temp[7] >> 54) ^ (temp[7] >> 59) ^ (temp[7] >> 62));
298
-
299
- c[3] = temp[3] ^ temp[7];
300
- c[3] ^= (temp[7] << 10) | (temp[6] >> 54);
301
- c[3] ^= (temp[7] << 5) | (temp[6] >> 59);
302
- c[3] ^= (temp[7] << 2) | (temp[6] >> 62);
303
-
304
- c[2] = temp[2] ^ temp[6];
305
- c[2] ^= (temp[6] << 10) | (temp[5] >> 54);
306
- c[2] ^= (temp[6] << 5) | (temp[5] >> 59);
307
- c[2] ^= (temp[6] << 2) | (temp[5] >> 62);
308
-
309
- c[1] = temp[1] ^ temp[5];
310
- c[1] ^= (temp[5] << 10) | (t[0] >> 54);
311
- c[1] ^= (temp[5] << 5) | (t[0] >> 59);
312
- c[1] ^= (temp[5] << 2) | (t[0] >> 62);
313
-
314
- c[0] = temp[0] ^ t[0];
315
- c[0] ^= (t[0] << 10);
316
- c[0] ^= (t[0] << 5);
317
- c[0] ^= (t[0] << 2);
318
- }
319
-
320
- void gf_mul_add(gf c, const gf a, const gf b)
321
- {
322
- uint64_t t[4] = {0,};
323
- uint64_t add[4] = {0,};
324
- uint64_t temp[8] = {0,};
325
-
326
- poly64_mul(&t[0], &temp[0], a[0], b[0]);
327
- poly64_mul(&t[2], &t[1], a[1], b[1]);
328
- t[0] ^= t[1];
329
-
330
- poly64_mul(&t[3], &t[1], a[2], b[2]);
331
- t[1] ^= t[2];
332
-
333
- poly64_mul(&temp[7], &t[2], a[3], b[3]);
334
- t[2] ^= t[3];
335
-
336
- temp[6] = temp[7] ^ t[2];
337
- temp[3] = t[2] ^ t[1];
338
- temp[2] = t[1] ^ t[0];
339
- temp[1] = t[0] ^ temp[0];
340
-
341
- poly64_mul(&t[1], &t[0], (a[0] ^ a[1]), (b[0] ^ b[1]));
342
- temp[1] ^= t[0];
343
- temp[2] ^= t[1];
344
-
345
- poly64_mul(&t[1], &t[0], (a[2] ^ a[3]), (b[2] ^ b[3]));
346
- temp[3] ^= t[0];
347
- temp[6] ^= t[1];
348
-
349
- temp[5] = temp[7] ^ temp[3];
350
- temp[4] = temp[6] ^ temp[2];
351
- temp[3] ^= temp[1];
352
- temp[2] ^= temp[0];
353
-
354
- add[0] = a[0] ^ a[2];
355
- add[1] = a[1] ^ a[3];
356
- add[2] = b[0] ^ b[2];
357
- add[3] = b[1] ^ b[3];
358
- poly64_mul(&t[1], &t[0], add[0], add[2]);
359
- poly64_mul(&t[3], &t[2], add[1], add[3]);
360
- t[1] ^= t[2];
361
- t[2] = t[1] ^ t[3];
362
- t[1] ^= t[0];
363
-
364
- temp[2] ^= t[0];
365
- temp[3] ^= t[1];
366
- temp[4] ^= t[2];
367
- temp[5] ^= t[3];
368
-
369
- poly64_mul(&t[1], &t[0], (add[0] ^ add[1]), (add[2] ^ add[3]));
370
- temp[3] ^= t[0];
371
- temp[4] ^= t[1];
372
-
373
- t[0] = temp[4] ^ ((temp[7] >> 54) ^ (temp[7] >> 59) ^ (temp[7] >> 62));
374
-
375
- c[3] ^= temp[3] ^ temp[7];
376
- c[3] ^= (temp[7] << 10) | (temp[6] >> 54);
377
- c[3] ^= (temp[7] << 5) | (temp[6] >> 59);
378
- c[3] ^= (temp[7] << 2) | (temp[6] >> 62);
379
-
380
- c[2] ^= temp[2] ^ temp[6];
381
- c[2] ^= (temp[6] << 10) | (temp[5] >> 54);
382
- c[2] ^= (temp[6] << 5) | (temp[5] >> 59);
383
- c[2] ^= (temp[6] << 2) | (temp[5] >> 62);
384
-
385
- c[1] ^= temp[1] ^ temp[5];
386
- c[1] ^= (temp[5] << 10) | (t[0] >> 54);
387
- c[1] ^= (temp[5] << 5) | (t[0] >> 59);
388
- c[1] ^= (temp[5] << 2) | (t[0] >> 62);
389
-
390
- c[0] ^= temp[0] ^ t[0];
391
- c[0] ^= (t[0] << 10);
392
- c[0] ^= (t[0] << 5);
393
- c[0] ^= (t[0] << 2);
394
- }
395
-
396
- static void poly64_sqr(uint64_t *z1, uint64_t *z0, uint64_t x)
397
- {
398
- const uint64_t C0 = 0x5555555555555555;
399
- const uint64_t C1 = 0x3333333333333333;
400
- const uint64_t C2 = 0x0f0f0f0f0f0f0f0f;
401
- const uint64_t C3 = 0x00ff00ff00ff00ff;
402
- const uint64_t C4 = 0x0000ffff0000ffff;
403
- const uint64_t C5 = 0x00000000ffffffff;
404
- uint64_t y = x >> 32;
405
- x &= C5;
406
- x = (x | (x << 16)) & C4;
407
- y = (y | (y << 16)) & C4;
408
- x = (x | (x << 8)) & C3;
409
- y = (y | (y << 8)) & C3;
410
- x = (x | (x << 4)) & C2;
411
- y = (y | (y << 4)) & C2;
412
- x = (x | (x << 2)) & C1;
413
- y = (y | (y << 2)) & C1;
414
- x = (x | (x << 1)) & C0;
415
- y = (y | (y << 1)) & C0;
416
- *z0 = x;
417
- *z1 = y;
86
+ gf_mod(c, temp);
418
87
  }
419
88
 
420
89
  void gf_sqr(gf c, const gf a)
421
90
  {
422
- uint64_t t = 0;
423
91
  uint64_t temp[8] = {0,};
424
92
 
425
93
  poly64_sqr(&temp[1], &temp[0], a[0]);
@@ -427,51 +95,10 @@ void gf_sqr(gf c, const gf a)
427
95
  poly64_sqr(&temp[5], &temp[4], a[2]);
428
96
  poly64_sqr(&temp[7], &temp[6], a[3]);
429
97
 
430
- t = temp[4] ^ ((temp[7] >> 54) ^ (temp[7] >> 59) ^ (temp[7] >> 62));
431
-
432
- c[3] = temp[3] ^ temp[7];
433
- c[3] ^= (temp[7] << 10) | (temp[6] >> 54);
434
- c[3] ^= (temp[7] << 5) | (temp[6] >> 59);
435
- c[3] ^= (temp[7] << 2) | (temp[6] >> 62);
436
-
437
- c[2] = temp[2] ^ temp[6];
438
- c[2] ^= (temp[6] << 10) | (temp[5] >> 54);
439
- c[2] ^= (temp[6] << 5) | (temp[5] >> 59);
440
- c[2] ^= (temp[6] << 2) | (temp[5] >> 62);
441
-
442
- c[1] = temp[1] ^ temp[5];
443
- c[1] ^= (temp[5] << 10) | (t >> 54);
444
- c[1] ^= (temp[5] << 5) | (t >> 59);
445
- c[1] ^= (temp[5] << 2) | (t >> 62);
446
-
447
- c[0] = temp[0] ^ t;
448
- c[0] ^= (t << 10);
449
- c[0] ^= (t << 5);
450
- c[0] ^= (t << 2);
451
- }
452
-
453
- void gf_exp(gf out, const gf in, const uint64_t *exp)
454
- {
455
- gf temp;
456
-
457
- gf_copy(temp, in);
458
- gf_set0(out);
459
- out[0] = 1;
460
- for (size_t i = 0; i < AIM2_NUM_WORDS_FIELD; i++)
461
- {
462
- uint64_t e = exp[i];
463
- for (size_t j = 0; j < AIM2_NUM_BITS_WORD; j++, e >>= 1)
464
- {
465
- if (e & 1)
466
- {
467
- gf_mul(out, out, temp);
468
- }
469
- gf_sqr(temp, temp);
470
- }
471
- }
98
+ gf_mod(c, temp);
472
99
  }
473
100
 
474
- void gf_mat_vec_mul(gf c, const gf a, const gf b[AIM2_NUM_BITS_FIELD])
101
+ void gf_mat_vec_mul(gf c, const gf a, const gf b[AIM3_NUM_BITS_FIELD])
475
102
  {
476
103
  const uint64_t *a_ptr = a;
477
104
  const gf *b_ptr = b;
@@ -481,30 +108,30 @@ void gf_mat_vec_mul(gf c, const gf a, const gf b[AIM2_NUM_BITS_FIELD])
481
108
  uint64_t temp_c2 = 0;
482
109
  uint64_t temp_c3 = 0;
483
110
  uint64_t mask;
484
- for (size_t i = AIM2_NUM_WORDS_FIELD; i; --i, ++a_ptr)
111
+ for (size_t i = AIM3_NUM_WORDS_FIELD; i; --i, ++a_ptr)
485
112
  {
486
113
  uint64_t index = *a_ptr;
487
- for (size_t j = AIM2_NUM_BITS_WORD; j; j -= 4, index >>= 4, b_ptr += 4)
114
+ for (size_t j = AIM3_NUM_BITS_WORD; j; j -= 4, index >>= 4, b_ptr += 4)
488
115
  {
489
- mask = -(index & 1);
116
+ mask = 0U - (index & 1);
490
117
  temp_c0 ^= (b_ptr[0][0] & mask);
491
118
  temp_c1 ^= (b_ptr[0][1] & mask);
492
119
  temp_c2 ^= (b_ptr[0][2] & mask);
493
120
  temp_c3 ^= (b_ptr[0][3] & mask);
494
121
 
495
- mask = -((index >> 1) & 1);
122
+ mask = 0U - ((index >> 1) & 1);
496
123
  temp_c0 ^= (b_ptr[1][0] & mask);
497
124
  temp_c1 ^= (b_ptr[1][1] & mask);
498
125
  temp_c2 ^= (b_ptr[1][2] & mask);
499
126
  temp_c3 ^= (b_ptr[1][3] & mask);
500
127
 
501
- mask = -((index >> 2) & 1);
128
+ mask = 0U - ((index >> 2) & 1);
502
129
  temp_c0 ^= (b_ptr[2][0] & mask);
503
130
  temp_c1 ^= (b_ptr[2][1] & mask);
504
131
  temp_c2 ^= (b_ptr[2][2] & mask);
505
132
  temp_c3 ^= (b_ptr[2][3] & mask);
506
133
 
507
- mask = -((index >> 3) & 1);
134
+ mask = 0U - ((index >> 3) & 1);
508
135
  temp_c0 ^= (b_ptr[3][0] & mask);
509
136
  temp_c1 ^= (b_ptr[3][1] & mask);
510
137
  temp_c2 ^= (b_ptr[3][2] & mask);
@@ -516,49 +143,3 @@ void gf_mat_vec_mul(gf c, const gf a, const gf b[AIM2_NUM_BITS_FIELD])
516
143
  c[2] = temp_c2;
517
144
  c[3] = temp_c3;
518
145
  }
519
-
520
- void gf_mat_vec_mul_add(gf c, const gf a, const gf b[AIM2_NUM_BITS_FIELD])
521
- {
522
- const uint64_t *a_ptr = a;
523
- const gf *b_ptr = b;
524
-
525
- uint64_t temp_c0 = 0;
526
- uint64_t temp_c1 = 0;
527
- uint64_t temp_c2 = 0;
528
- uint64_t temp_c3 = 0;
529
- uint64_t mask;
530
- for (size_t i = AIM2_NUM_WORDS_FIELD; i; --i, ++a_ptr)
531
- {
532
- uint64_t index = *a_ptr;
533
- for (size_t j = AIM2_NUM_BITS_WORD; j; j -= 4, index >>= 4, b_ptr += 4)
534
- {
535
- mask = -(index & 1);
536
- temp_c0 ^= (b_ptr[0][0] & mask);
537
- temp_c1 ^= (b_ptr[0][1] & mask);
538
- temp_c2 ^= (b_ptr[0][2] & mask);
539
- temp_c3 ^= (b_ptr[0][3] & mask);
540
-
541
- mask = -((index >> 1) & 1);
542
- temp_c0 ^= (b_ptr[1][0] & mask);
543
- temp_c1 ^= (b_ptr[1][1] & mask);
544
- temp_c2 ^= (b_ptr[1][2] & mask);
545
- temp_c3 ^= (b_ptr[1][3] & mask);
546
-
547
- mask = -((index >> 2) & 1);
548
- temp_c0 ^= (b_ptr[2][0] & mask);
549
- temp_c1 ^= (b_ptr[2][1] & mask);
550
- temp_c2 ^= (b_ptr[2][2] & mask);
551
- temp_c3 ^= (b_ptr[2][3] & mask);
552
-
553
- mask = -((index >> 3) & 1);
554
- temp_c0 ^= (b_ptr[3][0] & mask);
555
- temp_c1 ^= (b_ptr[3][1] & mask);
556
- temp_c2 ^= (b_ptr[3][2] & mask);
557
- temp_c3 ^= (b_ptr[3][3] & mask);
558
- }
559
- }
560
- c[0] ^= temp_c0;
561
- c[1] ^= temp_c1;
562
- c[2] ^= temp_c2;
563
- c[3] ^= temp_c3;
564
- }
@@ -0,0 +1,98 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ #include "field.h"
4
+ #include <stdbool.h>
5
+ #include <stddef.h>
6
+ #include <stdint.h>
7
+
8
+ void gf_to_bytes(uint8_t *out, const gf in)
9
+ {
10
+ for (size_t i = 0; i < AIM3_NUM_WORDS_FIELD; i++)
11
+ {
12
+ uint64_t u = in[i];
13
+ for (size_t j = 0; j < 8; j++)
14
+ {
15
+ out[8 * i + j] = (uint8_t)u;
16
+ u >>= 8;
17
+ }
18
+ }
19
+ }
20
+
21
+ void gf_from_bytes(gf out, const uint8_t *in)
22
+ {
23
+ for (size_t i = 0; i < AIM3_NUM_WORDS_FIELD; i++)
24
+ {
25
+ uint64_t u = 0;
26
+ for (int j = 7; j >= 0; j--)
27
+ {
28
+ u = (u << 8) | in[8 * i + j];
29
+ }
30
+ out[i] = u;
31
+ }
32
+ }
33
+
34
+ void gf_set0(gf a)
35
+ {
36
+ for (size_t i = 0; i < AIM3_NUM_WORDS_FIELD; i++)
37
+ {
38
+ a[i] = 0;
39
+ }
40
+ }
41
+
42
+ bool gf_is0(const gf a)
43
+ {
44
+ uint64_t result = 0;
45
+ for (size_t i = 0; i < AIM3_NUM_WORDS_FIELD; i++)
46
+ {
47
+ result |= a[i];
48
+ }
49
+ return result == 0;
50
+ }
51
+
52
+ void gf_copy(gf out, const gf in)
53
+ {
54
+ for (size_t i = 0; i < AIM3_NUM_WORDS_FIELD; i++)
55
+ {
56
+ out[i] = in[i];
57
+ }
58
+ }
59
+
60
+ void gf_add(gf c, const gf a, const gf b)
61
+ {
62
+ for (size_t i = 0; i < AIM3_NUM_WORDS_FIELD; i++)
63
+ {
64
+ c[i] = a[i] ^ b[i];
65
+ }
66
+ }
67
+
68
+ void gf_inv(gf c, const gf a)
69
+ {
70
+ gf temp;
71
+
72
+ gf_copy(temp, a);
73
+ gf_set0(c);
74
+ c[0] = 1;
75
+
76
+ // c = a^{2^n - 2} = prod_{i = 1}^{n-1} a^{2^i}
77
+ for (size_t i = 1; i < AIM3_NUM_BITS_FIELD; i++)
78
+ {
79
+ gf_sqr(temp, temp); // temp = a^{2^i}
80
+ gf_mul(c, c, temp);
81
+ }
82
+ }
83
+
84
+ // c += a * b
85
+ void gf_mul_add(gf c, const gf a, const gf b)
86
+ {
87
+ gf r;
88
+ gf_mul(r, a, b);
89
+ gf_add(c, c, r);
90
+ }
91
+
92
+ // c += sum_i a[i] * b[i]
93
+ void gf_mat_vec_mul_add(gf c, const gf a, const gf b[AIM3_NUM_BITS_FIELD])
94
+ {
95
+ gf r;
96
+ gf_mat_vec_mul(r, a, b);
97
+ gf_add(c, c, r);
98
+ }