kpqc 0.1.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 (191) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE +21 -0
  4. data/README.md +174 -0
  5. data/Rakefile +57 -0
  6. data/THIRD_PARTY_NOTICES.md +53 -0
  7. data/ext/kpqc/aimer128f/extconf.rb +4 -0
  8. data/ext/kpqc/aimer128s/extconf.rb +4 -0
  9. data/ext/kpqc/aimer192f/extconf.rb +4 -0
  10. data/ext/kpqc/aimer192s/extconf.rb +4 -0
  11. data/ext/kpqc/aimer256f/extconf.rb +4 -0
  12. data/ext/kpqc/aimer256s/extconf.rb +4 -0
  13. data/ext/kpqc/extconf_helper.rb +93 -0
  14. data/ext/kpqc/haetae2/extconf.rb +4 -0
  15. data/ext/kpqc/haetae3/extconf.rb +4 -0
  16. data/ext/kpqc/haetae5/extconf.rb +4 -0
  17. data/ext/kpqc/module.c +195 -0
  18. data/ext/kpqc/ntruplus1152/extconf.rb +4 -0
  19. data/ext/kpqc/ntruplus768/extconf.rb +4 -0
  20. data/ext/kpqc/ntruplus864/extconf.rb +4 -0
  21. data/ext/kpqc/randombytes.c +107 -0
  22. data/ext/kpqc/smaugt128/extconf.rb +4 -0
  23. data/ext/kpqc/smaugt192/extconf.rb +4 -0
  24. data/ext/kpqc/smaugt256/extconf.rb +4 -0
  25. data/ext/kpqc/timer/extconf.rb +4 -0
  26. data/kpqc.gemspec +41 -0
  27. data/lib/kpqc/aimer.rb +38 -0
  28. data/lib/kpqc/api.rb +129 -0
  29. data/lib/kpqc/haetae.rb +25 -0
  30. data/lib/kpqc/ntruplus.rb +25 -0
  31. data/lib/kpqc/smaugt.rb +29 -0
  32. data/lib/kpqc/types.rb +103 -0
  33. data/lib/kpqc/version.rb +6 -0
  34. data/lib/kpqc.rb +33 -0
  35. data/test/algorithm_test.rb +111 -0
  36. data/test/api_test.rb +52 -0
  37. data/test/interop.mjs +34 -0
  38. data/test/interop_test.rb +82 -0
  39. data/test/kat/aimer128f.rsp +10 -0
  40. data/test/kat/aimer128s.rsp +10 -0
  41. data/test/kat/aimer192f.rsp +10 -0
  42. data/test/kat/aimer192s.rsp +10 -0
  43. data/test/kat/aimer256f.rsp +10 -0
  44. data/test/kat/aimer256s.rsp +10 -0
  45. data/test/kat/haetae2.rsp +10 -0
  46. data/test/kat/haetae3.rsp +10 -0
  47. data/test/kat/haetae5.rsp +10 -0
  48. data/test/kat/ntruplus1152.rsp +8 -0
  49. data/test/kat/ntruplus768.rsp +8 -0
  50. data/test/kat/ntruplus864.rsp +8 -0
  51. data/test/kat/smaugt128.rsp +8 -0
  52. data/test/kat/smaugt192.rsp +8 -0
  53. data/test/kat/smaugt256.rsp +8 -0
  54. data/test/kat/timer.rsp +8 -0
  55. data/test/kat_drbg.rb +155 -0
  56. data/test/kat_test.rb +99 -0
  57. data/test/test_helper.rb +21 -0
  58. data/vendor/AIMer/LICENSE +21 -0
  59. data/vendor/AIMer/aim2.c +163 -0
  60. data/vendor/AIMer/aim2.h +36 -0
  61. data/vendor/AIMer/aim2_constant.h +74 -0
  62. data/vendor/AIMer/api.h +42 -0
  63. data/vendor/AIMer/common/crypto_declassify.h +7 -0
  64. data/vendor/AIMer/common/fips202.c +878 -0
  65. data/vendor/AIMer/common/fips202.h +166 -0
  66. data/vendor/AIMer/common/rng.h +56 -0
  67. data/vendor/AIMer/field.h +43 -0
  68. data/vendor/AIMer/field128.c +454 -0
  69. data/vendor/AIMer/field192.c +485 -0
  70. data/vendor/AIMer/field256.c +564 -0
  71. data/vendor/AIMer/hash.c +50 -0
  72. data/vendor/AIMer/hash.h +37 -0
  73. data/vendor/AIMer/params/params-aimer-128f.h +34 -0
  74. data/vendor/AIMer/params/params-aimer-128s.h +34 -0
  75. data/vendor/AIMer/params/params-aimer-192f.h +34 -0
  76. data/vendor/AIMer/params/params-aimer-192s.h +34 -0
  77. data/vendor/AIMer/params/params-aimer-256f.h +34 -0
  78. data/vendor/AIMer/params/params-aimer-256s.h +34 -0
  79. data/vendor/AIMer/params.h +5 -0
  80. data/vendor/AIMer/sign.c +631 -0
  81. data/vendor/AIMer/sign.h +40 -0
  82. data/vendor/AIMer/tree.c +75 -0
  83. data/vendor/AIMer/tree.h +26 -0
  84. data/vendor/HAETAE/LICENSE +22 -0
  85. data/vendor/HAETAE/include/api.h +54 -0
  86. data/vendor/HAETAE/include/common.h +12 -0
  87. data/vendor/HAETAE/include/config.h +42 -0
  88. data/vendor/HAETAE/include/decompose.h +19 -0
  89. data/vendor/HAETAE/include/encoding.h +20 -0
  90. data/vendor/HAETAE/include/fft.h +26 -0
  91. data/vendor/HAETAE/include/fips202.h +40 -0
  92. data/vendor/HAETAE/include/fixpoint.h +125 -0
  93. data/vendor/HAETAE/include/ntt.h +16 -0
  94. data/vendor/HAETAE/include/packing.h +58 -0
  95. data/vendor/HAETAE/include/params.h +117 -0
  96. data/vendor/HAETAE/include/poly.h +65 -0
  97. data/vendor/HAETAE/include/polyfix.h +65 -0
  98. data/vendor/HAETAE/include/polymat.h +32 -0
  99. data/vendor/HAETAE/include/polyvec.h +131 -0
  100. data/vendor/HAETAE/include/randombytes.h +39 -0
  101. data/vendor/HAETAE/include/rans_byte.h +335 -0
  102. data/vendor/HAETAE/include/reduce.h +31 -0
  103. data/vendor/HAETAE/include/sampler.h +23 -0
  104. data/vendor/HAETAE/include/symmetric.h +52 -0
  105. data/vendor/HAETAE/src/decompose.c +73 -0
  106. data/vendor/HAETAE/src/encoding.c +700 -0
  107. data/vendor/HAETAE/src/fft.c +235 -0
  108. data/vendor/HAETAE/src/fips202.c +745 -0
  109. data/vendor/HAETAE/src/fixpoint.c +143 -0
  110. data/vendor/HAETAE/src/ntt.c +98 -0
  111. data/vendor/HAETAE/src/packing.c +595 -0
  112. data/vendor/HAETAE/src/poly.c +371 -0
  113. data/vendor/HAETAE/src/polyfix.c +298 -0
  114. data/vendor/HAETAE/src/polymat.c +82 -0
  115. data/vendor/HAETAE/src/polyvec.c +534 -0
  116. data/vendor/HAETAE/src/reduce.c +96 -0
  117. data/vendor/HAETAE/src/sampler.c +310 -0
  118. data/vendor/HAETAE/src/sign.c +558 -0
  119. data/vendor/HAETAE/src/symmetric-shake.c +50 -0
  120. data/vendor/NTRUplus/LICENSE +21 -0
  121. data/vendor/NTRUplus/NTRU+1152/api.h +24 -0
  122. data/vendor/NTRUplus/NTRU+1152/fips202/fips202.c +878 -0
  123. data/vendor/NTRUplus/NTRU+1152/fips202/fips202.h +166 -0
  124. data/vendor/NTRUplus/NTRU+1152/kem.c +286 -0
  125. data/vendor/NTRUplus/NTRU+1152/ntt.c +394 -0
  126. data/vendor/NTRUplus/NTRU+1152/ntt.h +16 -0
  127. data/vendor/NTRUplus/NTRU+1152/params.h +18 -0
  128. data/vendor/NTRUplus/NTRU+1152/poly.c +318 -0
  129. data/vendor/NTRUplus/NTRU+1152/poly.h +31 -0
  130. data/vendor/NTRUplus/NTRU+1152/randombytes.h +9 -0
  131. data/vendor/NTRUplus/NTRU+1152/symmetric.c +39 -0
  132. data/vendor/NTRUplus/NTRU+1152/symmetric.h +12 -0
  133. data/vendor/NTRUplus/NTRU+768/api.h +24 -0
  134. data/vendor/NTRUplus/NTRU+768/fips202/fips202.c +878 -0
  135. data/vendor/NTRUplus/NTRU+768/fips202/fips202.h +166 -0
  136. data/vendor/NTRUplus/NTRU+768/kem.c +286 -0
  137. data/vendor/NTRUplus/NTRU+768/ntt.c +379 -0
  138. data/vendor/NTRUplus/NTRU+768/ntt.h +16 -0
  139. data/vendor/NTRUplus/NTRU+768/params.h +18 -0
  140. data/vendor/NTRUplus/NTRU+768/poly.c +320 -0
  141. data/vendor/NTRUplus/NTRU+768/poly.h +31 -0
  142. data/vendor/NTRUplus/NTRU+768/randombytes.h +9 -0
  143. data/vendor/NTRUplus/NTRU+768/symmetric.c +39 -0
  144. data/vendor/NTRUplus/NTRU+768/symmetric.h +12 -0
  145. data/vendor/NTRUplus/NTRU+864/api.h +24 -0
  146. data/vendor/NTRUplus/NTRU+864/fips202/fips202.c +878 -0
  147. data/vendor/NTRUplus/NTRU+864/fips202/fips202.h +166 -0
  148. data/vendor/NTRUplus/NTRU+864/kem.c +286 -0
  149. data/vendor/NTRUplus/NTRU+864/ntt.c +383 -0
  150. data/vendor/NTRUplus/NTRU+864/ntt.h +16 -0
  151. data/vendor/NTRUplus/NTRU+864/params.h +18 -0
  152. data/vendor/NTRUplus/NTRU+864/poly.c +314 -0
  153. data/vendor/NTRUplus/NTRU+864/poly.h +31 -0
  154. data/vendor/NTRUplus/NTRU+864/randombytes.h +9 -0
  155. data/vendor/NTRUplus/NTRU+864/symmetric.c +39 -0
  156. data/vendor/NTRUplus/NTRU+864/symmetric.h +12 -0
  157. data/vendor/SMAUG-T/LICENSE +22 -0
  158. data/vendor/SMAUG-T/include/api.h +37 -0
  159. data/vendor/SMAUG-T/include/cbd.h +15 -0
  160. data/vendor/SMAUG-T/include/ciphertext.h +45 -0
  161. data/vendor/SMAUG-T/include/common.h +12 -0
  162. data/vendor/SMAUG-T/include/config.h +48 -0
  163. data/vendor/SMAUG-T/include/dg.h +28 -0
  164. data/vendor/SMAUG-T/include/fips202.h +41 -0
  165. data/vendor/SMAUG-T/include/hash.h +21 -0
  166. data/vendor/SMAUG-T/include/hwt.h +28 -0
  167. data/vendor/SMAUG-T/include/indcpa.h +33 -0
  168. data/vendor/SMAUG-T/include/key.h +34 -0
  169. data/vendor/SMAUG-T/include/pack.h +78 -0
  170. data/vendor/SMAUG-T/include/packring.h +50 -0
  171. data/vendor/SMAUG-T/include/params.h +84 -0
  172. data/vendor/SMAUG-T/include/poly.h +41 -0
  173. data/vendor/SMAUG-T/include/randombytes.h +11 -0
  174. data/vendor/SMAUG-T/include/toomcook.h +14 -0
  175. data/vendor/SMAUG-T/include/verify.h +16 -0
  176. data/vendor/SMAUG-T/src/cbd.c +159 -0
  177. data/vendor/SMAUG-T/src/ciphertext.c +95 -0
  178. data/vendor/SMAUG-T/src/dg.c +112 -0
  179. data/vendor/SMAUG-T/src/fips202.c +745 -0
  180. data/vendor/SMAUG-T/src/hash.c +15 -0
  181. data/vendor/SMAUG-T/src/hwt.c +121 -0
  182. data/vendor/SMAUG-T/src/indcpa.c +187 -0
  183. data/vendor/SMAUG-T/src/kem.c +194 -0
  184. data/vendor/SMAUG-T/src/key.c +102 -0
  185. data/vendor/SMAUG-T/src/pack.c +349 -0
  186. data/vendor/SMAUG-T/src/packring.c +503 -0
  187. data/vendor/SMAUG-T/src/poly.c +200 -0
  188. data/vendor/SMAUG-T/src/toomcook.c +256 -0
  189. data/vendor/SMAUG-T/src/verify.c +48 -0
  190. data/vendor/SOURCES.json +130 -0
  191. metadata +280 -0
@@ -0,0 +1,564 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ #include "field.h"
4
+ #include <stddef.h>
5
+ #include <stdint.h>
6
+
7
+ void gf_to_bytes(uint8_t *out, const gf in)
8
+ {
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
+ }
20
+
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
+ }
34
+
35
+ void gf_set0(gf a)
36
+ {
37
+ a[0] = 0;
38
+ a[1] = 0;
39
+ a[2] = 0;
40
+ a[3] = 0;
41
+ }
42
+
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
+ }
50
+
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;
242
+ }
243
+
244
+ void gf_mul(gf c, const gf a, const gf b)
245
+ {
246
+ uint64_t t[4] = {0,};
247
+ uint64_t add[4] = {0,};
248
+ uint64_t temp[8] = {0,};
249
+
250
+ poly64_mul(&t[0], &temp[0], a[0], b[0]);
251
+ poly64_mul(&t[2], &t[1], a[1], b[1]);
252
+ t[0] ^= t[1];
253
+
254
+ poly64_mul(&t[3], &t[1], a[2], b[2]);
255
+ t[1] ^= t[2];
256
+
257
+ poly64_mul(&temp[7], &t[2], a[3], b[3]);
258
+ t[2] ^= t[3];
259
+
260
+ temp[6] = temp[7] ^ t[2];
261
+ temp[3] = t[2] ^ t[1];
262
+ temp[2] = t[1] ^ t[0];
263
+ temp[1] = t[0] ^ temp[0];
264
+
265
+ poly64_mul(&t[1], &t[0], (a[0] ^ a[1]), (b[0] ^ b[1]));
266
+ temp[1] ^= t[0];
267
+ temp[2] ^= t[1];
268
+
269
+ poly64_mul(&t[1], &t[0], (a[2] ^ a[3]), (b[2] ^ b[3]));
270
+ temp[3] ^= t[0];
271
+ temp[6] ^= t[1];
272
+
273
+ temp[5] = temp[7] ^ temp[3];
274
+ temp[4] = temp[6] ^ temp[2];
275
+ temp[3] ^= temp[1];
276
+ temp[2] ^= temp[0];
277
+
278
+ add[0] = a[0] ^ a[2];
279
+ add[1] = a[1] ^ a[3];
280
+ add[2] = b[0] ^ b[2];
281
+ add[3] = b[1] ^ b[3];
282
+ poly64_mul(&t[1], &t[0], add[0], add[2]);
283
+ poly64_mul(&t[3], &t[2], add[1], add[3]);
284
+ t[1] ^= t[2];
285
+ t[2] = t[1] ^ t[3];
286
+ t[1] ^= t[0];
287
+
288
+ temp[2] ^= t[0];
289
+ temp[3] ^= t[1];
290
+ temp[4] ^= t[2];
291
+ temp[5] ^= t[3];
292
+
293
+ poly64_mul(&t[1], &t[0], (add[0] ^ add[1]), (add[2] ^ add[3]));
294
+ temp[3] ^= t[0];
295
+ temp[4] ^= t[1];
296
+
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;
418
+ }
419
+
420
+ void gf_sqr(gf c, const gf a)
421
+ {
422
+ uint64_t t = 0;
423
+ uint64_t temp[8] = {0,};
424
+
425
+ poly64_sqr(&temp[1], &temp[0], a[0]);
426
+ poly64_sqr(&temp[3], &temp[2], a[1]);
427
+ poly64_sqr(&temp[5], &temp[4], a[2]);
428
+ poly64_sqr(&temp[7], &temp[6], a[3]);
429
+
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
+ }
472
+ }
473
+
474
+ void gf_mat_vec_mul(gf c, const gf a, const gf b[AIM2_NUM_BITS_FIELD])
475
+ {
476
+ const uint64_t *a_ptr = a;
477
+ const gf *b_ptr = b;
478
+
479
+ uint64_t temp_c0 = 0;
480
+ uint64_t temp_c1 = 0;
481
+ uint64_t temp_c2 = 0;
482
+ uint64_t temp_c3 = 0;
483
+ uint64_t mask;
484
+ for (size_t i = AIM2_NUM_WORDS_FIELD; i; --i, ++a_ptr)
485
+ {
486
+ uint64_t index = *a_ptr;
487
+ for (size_t j = AIM2_NUM_BITS_WORD; j; j -= 4, index >>= 4, b_ptr += 4)
488
+ {
489
+ mask = -(index & 1);
490
+ temp_c0 ^= (b_ptr[0][0] & mask);
491
+ temp_c1 ^= (b_ptr[0][1] & mask);
492
+ temp_c2 ^= (b_ptr[0][2] & mask);
493
+ temp_c3 ^= (b_ptr[0][3] & mask);
494
+
495
+ mask = -((index >> 1) & 1);
496
+ temp_c0 ^= (b_ptr[1][0] & mask);
497
+ temp_c1 ^= (b_ptr[1][1] & mask);
498
+ temp_c2 ^= (b_ptr[1][2] & mask);
499
+ temp_c3 ^= (b_ptr[1][3] & mask);
500
+
501
+ mask = -((index >> 2) & 1);
502
+ temp_c0 ^= (b_ptr[2][0] & mask);
503
+ temp_c1 ^= (b_ptr[2][1] & mask);
504
+ temp_c2 ^= (b_ptr[2][2] & mask);
505
+ temp_c3 ^= (b_ptr[2][3] & mask);
506
+
507
+ mask = -((index >> 3) & 1);
508
+ temp_c0 ^= (b_ptr[3][0] & mask);
509
+ temp_c1 ^= (b_ptr[3][1] & mask);
510
+ temp_c2 ^= (b_ptr[3][2] & mask);
511
+ temp_c3 ^= (b_ptr[3][3] & mask);
512
+ }
513
+ }
514
+ c[0] = temp_c0;
515
+ c[1] = temp_c1;
516
+ c[2] = temp_c2;
517
+ c[3] = temp_c3;
518
+ }
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,50 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ #include "hash.h"
4
+ #include <stddef.h>
5
+ #include <stdint.h>
6
+
7
+ #if SECURITY_BITS == 128
8
+ #define shake_inc_init shake128_inc_init
9
+ #define shake_inc_absorb shake128_inc_absorb
10
+ #define shake_inc_final shake128_inc_finalize
11
+ #define shake_inc_squeeze shake128_inc_squeeze
12
+ #define shake_inc_ctx_release shake128_inc_ctx_release
13
+ #else
14
+ #define shake_inc_init shake256_inc_init
15
+ #define shake_inc_absorb shake256_inc_absorb
16
+ #define shake_inc_final shake256_inc_finalize
17
+ #define shake_inc_squeeze shake256_inc_squeeze
18
+ #define shake_inc_ctx_release shake256_inc_ctx_release
19
+ #endif
20
+
21
+ void hash_init(hash_instance *ctx)
22
+ {
23
+ shake_inc_init(ctx);
24
+ }
25
+
26
+ void hash_init_prefix(hash_instance *ctx, uint8_t prefix)
27
+ {
28
+ shake_inc_init(ctx);
29
+ shake_inc_absorb(ctx, &prefix, sizeof(prefix));
30
+ }
31
+
32
+ void hash_update(hash_instance *ctx, const uint8_t *data, size_t data_len)
33
+ {
34
+ shake_inc_absorb(ctx, data, data_len);
35
+ }
36
+
37
+ void hash_final(hash_instance *ctx)
38
+ {
39
+ shake_inc_final(ctx);
40
+ }
41
+
42
+ void hash_squeeze(hash_instance *ctx, uint8_t *buffer, size_t buffer_len)
43
+ {
44
+ shake_inc_squeeze(buffer, buffer_len, ctx);
45
+ }
46
+
47
+ void hash_ctx_release(hash_instance *ctx)
48
+ {
49
+ shake_inc_ctx_release(ctx);
50
+ }
@@ -0,0 +1,37 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ #ifndef HASH_H
4
+ #define HASH_H
5
+
6
+ #include "params.h"
7
+ #include "common/fips202.h"
8
+ #include <stddef.h>
9
+ #include <stdint.h>
10
+
11
+ static const uint8_t HASH_PREFIX_0 = (uint8_t)AIMER_HASH_PREFIX_0;
12
+ static const uint8_t HASH_PREFIX_1 = 1;
13
+ static const uint8_t HASH_PREFIX_2 = 2;
14
+ static const uint8_t HASH_PREFIX_3 = 3;
15
+ static const uint8_t HASH_PREFIX_4 = 4;
16
+ static const uint8_t HASH_PREFIX_5 = 5;
17
+
18
+ #if SECURITY_BITS == 128
19
+ typedef shake128incctx hash_instance;
20
+ #else
21
+ typedef shake256incctx hash_instance;
22
+ #endif
23
+
24
+ #define hash_init AIMER_NAMESPACE(hash_init)
25
+ void hash_init(hash_instance *ctx);
26
+ #define hash_init_prefix AIMER_NAMESPACE(hash_init_prefix)
27
+ void hash_init_prefix(hash_instance *ctx, uint8_t prefix);
28
+ #define hash_update AIMER_NAMESPACE(hash_update)
29
+ void hash_update(hash_instance *ctx, const uint8_t *data, size_t data_len);
30
+ #define hash_final AIMER_NAMESPACE(hash_final)
31
+ void hash_final(hash_instance *ctx);
32
+ #define hash_squeeze AIMER_NAMESPACE(hash_squeeze)
33
+ void hash_squeeze(hash_instance *ctx, uint8_t *buffer, size_t buffer_len);
34
+ #define hash_ctx_release AIMER_NAMESPACE(hash_ctx_release)
35
+ void hash_ctx_release(hash_instance *ctx);
36
+
37
+ #endif // HASH_H
@@ -0,0 +1,34 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ #ifndef PARAMS_H
4
+ #define PARAMS_H
5
+
6
+ #define AIMER_NAMESPACE(s) samsungsds_aimer_128f_ref_##s
7
+
8
+ #define SECURITY_BITS 128 // security parameter
9
+ #define SECURITY_BYTES (SECURITY_BITS / 8) // byte size of security parameter
10
+
11
+ #define AIM2_NUM_BITS_FIELD SECURITY_BITS // number of bits in field element
12
+ #define AIM2_NUM_BYTES_FIELD SECURITY_BYTES // number of bytes in field element
13
+ #define AIM2_NUM_WORDS_FIELD (SECURITY_BITS / 64) // number of 64-bit words in element
14
+ #define AIM2_NUM_BITS_WORD 64 // number of bits in word
15
+ #define AIM2_IV_SIZE SECURITY_BYTES // byte size of AIM2 initial vector
16
+
17
+ #define AIM2_NUM_INPUT_SBOX 2 // number of AIM2 input S-boxes
18
+
19
+ #define AIMER_SALT_SIZE SECURITY_BYTES // byte size of salt
20
+ #define AIMER_SEED_SIZE SECURITY_BYTES // byte size of seed
21
+ #define AIMER_COMMIT_SIZE (SECURITY_BYTES * 2) // byte size of commitment
22
+
23
+ #define AIMER_L AIM2_NUM_INPUT_SBOX
24
+ #define AIMER_T 33 // number of parallel repetitions (Tau)
25
+ #define AIMER_N 16 // number of MPC parties (N)
26
+ #define AIMER_LOGN 4 // log_2(N)
27
+
28
+ #define AIMER_PK_BYTES 32 // byte size of public key
29
+ #define AIMER_SK_BYTES 48 // byte size of secret key
30
+ #define AIMER_SIG_BYTES 5888 // byte size of signature
31
+
32
+ #define AIMER_HASH_PREFIX_0 0x00
33
+
34
+ #endif // PARAMS_H