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,314 @@
1
+ #include <stdint.h>
2
+ #include <string.h>
3
+ #include "params.h"
4
+ #include "poly.h"
5
+ #include "ntt.h"
6
+ #include "symmetric.h"
7
+
8
+ /*************************************************
9
+ * Name: crepmod3
10
+ *
11
+ * Description: Compute modulus 3 operation
12
+ *
13
+ * Arguments: - poly *a: pointer to intput integer to be reduced
14
+ *
15
+ * Returns: integer in {-1,0,1} congruent to a modulo 3.
16
+ **************************************************/
17
+ static inline int16_t crepmod3(int16_t a)
18
+ {
19
+ int16_t t;
20
+ const int16_t v = ((1<<15) + 3/2)/3;
21
+
22
+ a += (a >> 15) & NTRUPLUS_Q;
23
+ a -= (NTRUPLUS_Q+1)/2;
24
+ a += (a >> 15) & NTRUPLUS_Q;
25
+ a -= (NTRUPLUS_Q-1)/2;
26
+
27
+ t = ((int32_t)v*a + (1<<14)) >> 15;
28
+ t *= 3;
29
+ return a - t;
30
+ }
31
+
32
+ /*************************************************
33
+ * Name: poly_tobytes
34
+ *
35
+ * Description: Serialization of a polynomial
36
+ *
37
+ * Arguments: - uint8_t *r: pointer to output byte array
38
+ * (needs space for NTRUPLUS_POLYBYTES bytes)
39
+ * - poly *a: pointer to input polynomial
40
+ **************************************************/
41
+ void poly_tobytes(uint8_t r[NTRUPLUS_POLYBYTES], const poly *a)
42
+ {
43
+ int16_t t[2];
44
+
45
+ #pragma GCC unroll 2
46
+ for(size_t i = 0; i < NTRUPLUS_N/2; i++)
47
+ {
48
+ t[0] = a->coeffs[2*i];
49
+ t[0] += (t[0] >> 15) & NTRUPLUS_Q;
50
+ t[1] = a->coeffs[2*i+1];
51
+ t[1] += (t[1] >> 15) & NTRUPLUS_Q;
52
+
53
+ r[3*i+0] = (t[0] >> 0);
54
+ r[3*i+1] = (t[0] >> 8) | (t[1] << 4);
55
+ r[3*i+2] = (t[1] >> 4);
56
+ }
57
+ }
58
+
59
+ /*************************************************
60
+ * Name: poly_frombytes
61
+ *
62
+ * Description: De-serialization of a polynomial;
63
+ * inverse of poly_tobytes
64
+ *
65
+ * Arguments: - poly *r: pointer to output polynomial
66
+ * - const uint8_t *a: pointer to input byte array
67
+ * (of NTRUPLUS_POLYBYTES bytes)
68
+ **************************************************/
69
+ void poly_frombytes(poly *r, const uint8_t a[NTRUPLUS_POLYBYTES])
70
+ {
71
+ for(size_t i = 0; i < NTRUPLUS_N/2; i++)
72
+ {
73
+ r->coeffs[2*i] = ((a[3*i+0] >> 0) | ((uint16_t)a[3*i+1] << 8)) & 0xFFF;
74
+ r->coeffs[2*i+1] = ((a[3*i+1] >> 4) | ((uint16_t)a[3*i+2] << 4)) & 0xFFF;
75
+ }
76
+ }
77
+
78
+ /*************************************************
79
+ * Name: poly_cbd1
80
+ *
81
+ * Description: Sample a polynomial deterministically from a random,
82
+ * with output polynomial close to centered binomial distribution
83
+ *
84
+ * Arguments: - poly *r: pointer to output polynomial
85
+ * - const uint8_t *buf: pointer to input random
86
+ * (of length NTRUPLUS_N/4 bytes)
87
+ **************************************************/
88
+ void poly_cbd1(poly *r, const unsigned char buf[NTRUPLUS_N/4])
89
+ {
90
+ uint8_t t1, t2;
91
+
92
+ for(size_t i = 0; i < NTRUPLUS_N / 8; i++)
93
+ {
94
+ t1 = buf[i];
95
+ t2 = buf[i + NTRUPLUS_N / 8];
96
+
97
+ for(size_t j = 0; j < 8; j++)
98
+ {
99
+ r->coeffs[8*i + j] = (t1 & 0x1) - (t2 & 0x1);
100
+
101
+ t1 >>= 1;
102
+ t2 >>= 1;
103
+ }
104
+ }
105
+ }
106
+
107
+ /*************************************************
108
+ * Name: poly_sotp_encode
109
+ *
110
+ * Description: Encode a message deterministically using SOTP and a random,
111
+ with output polynomial close to centered binomial distribution
112
+ *
113
+ * Arguments: - poly *r: pointer to output polynomial
114
+ * - const uint8_t *msg: pointer to input message
115
+ * - const uint8_t *buf: pointer to input random
116
+ **************************************************/
117
+ void poly_sotp_encode(poly *r, const uint8_t msg[NTRUPLUS_N/8], const uint8_t buf[NTRUPLUS_N/4])
118
+ {
119
+ uint8_t tmp[NTRUPLUS_N/4];
120
+
121
+ for(int i = 0; i < NTRUPLUS_N/8; i++)
122
+ {
123
+ tmp[i] = buf[i]^msg[i];
124
+ }
125
+
126
+ for(int i = NTRUPLUS_N/8; i < NTRUPLUS_N/4; i++)
127
+ {
128
+ tmp[i] = buf[i];
129
+ }
130
+
131
+ poly_cbd1(r, tmp);
132
+ }
133
+
134
+ /*************************************************
135
+ * Name: poly_sotp_decode
136
+ *
137
+ * Description: Decode a message deterministically using SOTP_INV and a random
138
+ *
139
+ * Arguments: - uint8_t *msg: pointer to output message
140
+ * - const poly *a: pointer to iput polynomial
141
+ * - const uint8_t *buf: pointer to input random
142
+ *
143
+ * Returns 0 (success) or 1 (failure)
144
+ **************************************************/
145
+ int poly_sotp_decode(uint8_t msg[NTRUPLUS_N/8], const poly *a, const uint8_t buf[NTRUPLUS_N/4])
146
+ {
147
+ uint8_t t1, t2, t3;
148
+ uint16_t t4;
149
+ uint32_t r = 0;
150
+
151
+ for(size_t i = 0; i < NTRUPLUS_N / 8; i++)
152
+ {
153
+ t1 = buf[i ];
154
+ t2 = buf[i + NTRUPLUS_N / 8];
155
+ t3 = 0;
156
+
157
+ for(int j = 0; j < 8; j++)
158
+ {
159
+ t4 = t2 & 0x1;
160
+ t4 += a->coeffs[8*i + j];
161
+ r |= t4;
162
+ t4 = (t4 ^ t1) & 0x1;
163
+ t3 ^= (uint8_t)(t4 << j);
164
+
165
+ t1 >>= 1;
166
+ t2 >>= 1;
167
+ }
168
+
169
+ msg[i] = t3;
170
+ }
171
+
172
+ r = r >> 1;
173
+ r = (-(uint32_t)r) >> 31;
174
+
175
+ return r;
176
+ }
177
+
178
+ /*************************************************
179
+ * Name: poly_ntt
180
+ *
181
+ * Description: Computes number-theoretic transform (NTT)
182
+ *
183
+ * Arguments: - poly *r: pointer to input/output polynomial
184
+ **************************************************/
185
+ void poly_ntt(poly *r)
186
+ {
187
+ ntt(r->coeffs);
188
+ }
189
+
190
+ /*************************************************
191
+ * Name: poly_invntt
192
+ *
193
+ * Description: Computes inverse of number-theoretic transform (NTT)
194
+ *
195
+ * Arguments: - poly *r: pointer to input/output polynomial
196
+ **************************************************/
197
+ void poly_invntt(poly *r)
198
+ {
199
+ invntt(r->coeffs);
200
+ }
201
+
202
+ /*************************************************
203
+ * Name: poly_baseinv
204
+ *
205
+ * Description: Inversion of polynomial in NTT domain
206
+ *
207
+ * Arguments: - poly *r: pointer to output polynomial
208
+ * - const poly *a: pointer to input polynomial
209
+ *
210
+ * Returns: integer
211
+ **************************************************/
212
+ int poly_baseinv(poly *r, const poly *a)
213
+ {
214
+ for(int i = 0; i < NTRUPLUS_N/6; ++i)
215
+ {
216
+ if(baseinv(r->coeffs+6*i, a->coeffs+6*i, zetas[144+i]))
217
+ {
218
+ for (size_t j = 0; j < NTRUPLUS_N; ++j)
219
+ r->coeffs[j] = 0;
220
+
221
+ return 1;
222
+ }
223
+
224
+ if(baseinv(r->coeffs+6*i+3, a->coeffs+6*i+3, -zetas[144+i]))
225
+ {
226
+ for (size_t j = 0; j < NTRUPLUS_N; ++j)
227
+ r->coeffs[j] = 0;
228
+
229
+ return 1;
230
+ }
231
+ }
232
+
233
+ return 0;
234
+ }
235
+
236
+ /*************************************************
237
+ * Name: poly_basemul
238
+ *
239
+ * Description: Multiplication of two polynomials in NTT domain
240
+ *
241
+ * Arguments: - poly *r: pointer to output polynomial
242
+ * - const poly *a: pointer to first input polynomial
243
+ * - const poly *b: pointer to second input polynomial
244
+ **************************************************/
245
+ void poly_basemul(poly *r, const poly *a, const poly *b)
246
+ {
247
+ for(int i = 0; i < NTRUPLUS_N/6; i++)
248
+ {
249
+ basemul(r->coeffs+6*i, a->coeffs+6*i, b->coeffs+6*i, zetas[144+i]);
250
+ basemul(r->coeffs+6*i+3, a->coeffs+6*i+3, b->coeffs+6*i+3, -zetas[144+i]);
251
+ }
252
+ }
253
+
254
+ /*************************************************
255
+ * Name: poly_basemul_add
256
+ *
257
+ * Description: Multiplication then addition of three polynomials in NTT domain
258
+ *
259
+ * Arguments: - poly *r: pointer to output polynomial
260
+ * - const poly *a: pointer to first input polynomial
261
+ * - const poly *b: pointer to second input polynomial
262
+ * - const poly *c: pointer to third input polynomial
263
+ **************************************************/
264
+ void poly_basemul_add(poly *r, const poly *a, const poly *b, const poly *c)
265
+ {
266
+ for(int i = 0; i < NTRUPLUS_N/6; i++)
267
+ {
268
+ basemul_add(r->coeffs+6*i, a->coeffs+6*i, b->coeffs+6*i, c->coeffs+6*i, zetas[144+i]);
269
+ basemul_add(r->coeffs+6*i+3, a->coeffs+6*i+3, b->coeffs+6*i+3, c->coeffs+6*i+3, -zetas[144+i]);
270
+ }
271
+ }
272
+
273
+ /*************************************************
274
+ * Name: poly_sub
275
+ *
276
+ * Description: Subtract two polynomials; no modular reduction is performed
277
+ *
278
+ * Arguments: - poly *r: pointer to output polynomial
279
+ * - const poly *a: pointer to first input polynomial
280
+ * - const poly *b: pointer to second input polynomial
281
+ **************************************************/
282
+ void poly_sub(poly *r, const poly *a, const poly *b)
283
+ {
284
+ for(int i = 0; i < NTRUPLUS_N; ++i)
285
+ r->coeffs[i] = a->coeffs[i] - b->coeffs[i];
286
+ }
287
+
288
+ /*************************************************
289
+ * Name: poly_triple
290
+ *
291
+ * Description: Multiply polynomial by 3; no modular reduction is performed
292
+ *
293
+ * Arguments: - poly *r: pointer to output polynomial
294
+ * - const poly *a: pointer to input polynomial
295
+ **************************************************/
296
+ void poly_triple(poly *r, const poly *a)
297
+ {
298
+ for(int i = 0; i < NTRUPLUS_N; ++i)
299
+ r->coeffs[i] = 3*a->coeffs[i];
300
+ }
301
+
302
+ /*************************************************
303
+ * Name: poly_crepmod3
304
+ *
305
+ * Description: Compute modulus 3 operation to polynomial
306
+ *
307
+ * Arguments: - poly *r: pointer to output polynomial
308
+ * - const poly *a: pointer to input polynomial
309
+ **************************************************/
310
+ void poly_crepmod3(poly *r, const poly *a)
311
+ {
312
+ for(int i = 0; i < NTRUPLUS_N; i++)
313
+ r->coeffs[i] = crepmod3(a->coeffs[i]);
314
+ }
@@ -0,0 +1,31 @@
1
+ #ifndef POLY_H
2
+ #define POLY_H
3
+
4
+ #include <stdint.h>
5
+ #include "params.h"
6
+
7
+ /*
8
+ * Elements of R_q = Z_q[X]/(X^n - X^n/2 + 1). Represents polynomial
9
+ * coeffs[0] + X*coeffs[1] + X^2*xoeffs[2] + ... + X^{n-1}*coeffs[n-1]
10
+ */
11
+ typedef struct{
12
+ int16_t coeffs[NTRUPLUS_N];
13
+ } poly;
14
+
15
+ void poly_tobytes(uint8_t r[NTRUPLUS_POLYBYTES], const poly *a);
16
+ void poly_frombytes(poly *r, const uint8_t a[NTRUPLUS_POLYBYTES]);
17
+
18
+ void poly_cbd1(poly *r, const uint8_t buf[NTRUPLUS_N/4]);
19
+ void poly_sotp_encode(poly *r, const uint8_t msg[NTRUPLUS_N/8], const uint8_t buf[NTRUPLUS_N/4]);
20
+ int poly_sotp_decode(uint8_t msg[NTRUPLUS_N/8], const poly *a, const uint8_t buf[NTRUPLUS_N/4]);
21
+
22
+ void poly_ntt(poly *r);
23
+ void poly_invntt(poly *r);
24
+ int poly_baseinv(poly *r, const poly *a);
25
+ void poly_basemul(poly *r, const poly *a, const poly *b);
26
+ void poly_basemul_add(poly *r, const poly *a, const poly *b, const poly *c);
27
+ void poly_sub(poly *r, const poly *a, const poly *b);
28
+ void poly_triple(poly *r, const poly *a);
29
+ void poly_crepmod3(poly *r, const poly *a);
30
+
31
+ #endif
@@ -0,0 +1,9 @@
1
+ #ifndef RANDOMBYTES_H
2
+ #define RANDOMBYTES_H
3
+
4
+ #include <stddef.h>
5
+ #include <stdint.h>
6
+
7
+ int randombytes(uint8_t *out, size_t outlen);
8
+
9
+ #endif
@@ -0,0 +1,39 @@
1
+ #include <string.h>
2
+ #include "symmetric.h"
3
+ #include "fips202/fips202.h"
4
+
5
+ #define HASH_F_INBYTES (NTRUPLUS_POLYBYTES)
6
+ #define HASH_F_OUTBYTES (32)
7
+
8
+ #define HASH_G_INBYTES (NTRUPLUS_POLYBYTES)
9
+ #define HASH_G_OUTBYTES (NTRUPLUS_N / 4)
10
+
11
+ #define HASH_H_INBYTES (NTRUPLUS_N / 8 + NTRUPLUS_SYMBYTES)
12
+ #define HASH_H_OUTBYTES (NTRUPLUS_SSBYTES + NTRUPLUS_N / 4)
13
+
14
+ void hash_f(uint8_t *buf, const uint8_t *msg)
15
+ {
16
+ uint8_t data[1 + HASH_F_INBYTES];
17
+
18
+ data[0] = 0x00;
19
+ memcpy(data + 1, msg, HASH_F_INBYTES);
20
+ shake256(buf, HASH_F_OUTBYTES, data, HASH_F_INBYTES + 1);
21
+ }
22
+
23
+ void hash_g(uint8_t *buf, const uint8_t *msg)
24
+ {
25
+ uint8_t data[1 + HASH_G_INBYTES];
26
+
27
+ data[0] = 0x01;
28
+ memcpy(data + 1, msg, HASH_G_INBYTES);
29
+ shake256(buf, HASH_G_OUTBYTES, data, HASH_G_INBYTES + 1);
30
+ }
31
+
32
+ void hash_h(uint8_t *buf, const uint8_t *msg)
33
+ {
34
+ uint8_t data[1 + HASH_H_INBYTES];
35
+
36
+ data[0] = 0x02;
37
+ memcpy(data + 1, msg, HASH_H_INBYTES);
38
+ shake256(buf, HASH_H_OUTBYTES, data, HASH_H_INBYTES + 1);
39
+ }
@@ -0,0 +1,12 @@
1
+ #ifndef SYMMETRIC_H
2
+ #define SYMMETRIC_H
3
+
4
+ #include <stddef.h>
5
+ #include <stdint.h>
6
+ #include "params.h"
7
+
8
+ void hash_f(uint8_t *buf, const uint8_t *msg);
9
+ void hash_g(uint8_t *buf, const uint8_t *msg);
10
+ void hash_h(uint8_t *buf, const uint8_t *msg);
11
+
12
+ #endif /* SYMMETRIC_H */
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2026 Team SMAUG-T
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,37 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ #ifndef SMAUGT_API_H
4
+ #define SMAUGT_API_H
5
+
6
+ #include "params.h"
7
+
8
+ #include <stdint.h>
9
+
10
+ /* Export API in SUPERCOP naming scheme CRYPTO_xxx / crypto_kem_xxx */
11
+ #define CRYPTO_SECRETKEYBYTES SMAUGT_KEM_SECRETKEY_BYTES
12
+ #define CRYPTO_PUBLICKEYBYTES SMAUGT_PUBLICKEY_BYTES
13
+ #define CRYPTO_CIPHERTEXTBYTES SMAUGT_CIPHERTEXT_BYTES
14
+ #define CRYPTO_BYTES SMAUGT_SHARED_SECRETE_BYTES
15
+ #define CRYPTO_ALGNAME SMAUGT_CRYPTO_ALGNAME
16
+
17
+ #define crypto_kem_keypair_internal SMAUGT_NAMESPACE(keypair_internal)
18
+ #define crypto_kem_keypair SMAUGT_NAMESPACE(keypair)
19
+ #define crypto_kem_enc_internal SMAUGT_NAMESPACE(enc_internal)
20
+ #define crypto_kem_enc SMAUGT_NAMESPACE(enc)
21
+ #define crypto_kem_dec_internal SMAUGT_NAMESPACE(dec_internal)
22
+ #define crypto_kem_dec SMAUGT_NAMESPACE(dec)
23
+
24
+ int crypto_kem_keypair_internal(uint8_t *pk, uint8_t *sk,
25
+ uint8_t d[SMAUGT_T_BYTES],
26
+ uint8_t seed[SMAUGT_CRYPTO_BYTES]);
27
+ int crypto_kem_keypair(uint8_t *pk, uint8_t *sk);
28
+
29
+ int crypto_kem_enc_internal(uint8_t *ctxt, uint8_t *ss, const uint8_t *pk,
30
+ const uint8_t *mu);
31
+ int crypto_kem_enc(uint8_t *ctxt, uint8_t *ss, const uint8_t *pk);
32
+
33
+ int crypto_kem_dec_internal(uint8_t *ss, const uint8_t *ctxt,
34
+ const uint8_t *sk);
35
+ int crypto_kem_dec(uint8_t *ss, const uint8_t *ctxt, const uint8_t *sk);
36
+
37
+ #endif /* !SMAUGT_API_H */
@@ -0,0 +1,15 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ #ifndef SMAUGT_CBD_H
4
+ #define SMAUGT_CBD_H
5
+
6
+ #include "params.h"
7
+ #include "poly.h"
8
+
9
+ #include <stdint.h>
10
+
11
+ #define sp_cbd SMAUGT_NAMESPACE(sp_cbd)
12
+
13
+ void sp_cbd(poly *r, const uint8_t buf[SMAUGT_CBDSEED_BYTES]);
14
+
15
+ #endif /* !SMAUGT_CBD_H */
@@ -0,0 +1,45 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ #ifndef SMAUGT_CIPHERTEXT_H
4
+ #define SMAUGT_CIPHERTEXT_H
5
+
6
+ #include "params.h"
7
+ #include "poly.h"
8
+
9
+ #include <stdint.h>
10
+
11
+ #if SMAUGT_MODE == SMAUGT_MODE1
12
+ #define SMAUGT_RD_ADD 0x80 // 2^(15 - SMAUGT_LOG_P)
13
+ #define SMAUGT_RD_AND 0xff00 // 2^16 - 2^(16 - SMAUGT_LOG_P)
14
+ #define SMAUGT_RD_ADD2 0x0400 // 2^(15 - SMAUGT_LOG_P_PRIME)
15
+ #define SMAUGT_RD_AND2 0xf800 // 2^16 - 2^(16 - SMAUGT_LOG_P_PRIME)
16
+ #elif SMAUGT_MODE == SMAUGT_MODE3
17
+ #define SMAUGT_RD_ADD 0x40 // 2^(15 - SMAUGT_LOG_P)
18
+ #define SMAUGT_RD_AND 0xff80 // 2^16 - 2^(16 - SMAUGT_LOG_P)
19
+ #define SMAUGT_RD_ADD2 0x0800 // 2^(15 - SMAUGT_LOG_P_PRIME)
20
+ #define SMAUGT_RD_AND2 0xf000 // 2^16 - 2^(16 - SMAUGT_LOG_P_PRIME)
21
+ #elif SMAUGT_MODE == SMAUGT_MODE5
22
+ #define SMAUGT_RD_ADD 0x40 // 2^(15 - SMAUGT_LOG_P)
23
+ #define SMAUGT_RD_AND 0xff80 // 2^16 - 2^(16 - SMAUGT_LOG_P)
24
+ #define SMAUGT_RD_ADD2 0x0100 // 2^(15 - SMAUGT_LOG_P_PRIME)
25
+ #define SMAUGT_RD_AND2 0xfe00 // 2^16 - 2^(16 - SMAUGT_LOG_P_PRIME)
26
+ #elif SMAUGT_MODE == SMAUGT_MODET
27
+ #define SMAUGT_RD_ADD 0x80 // 2^(15 - SMAUGT_LOG_P)
28
+ #define SMAUGT_RD_AND 0xff00 // 2^16 - 2^(16 - SMAUGT_LOG_P)
29
+ #define SMAUGT_RD_ADD2 0x1000 // 2^(15 - SMAUGT_LOG_P_PRIME)
30
+ #define SMAUGT_RD_AND2 0xe000 // 2^16 - 2^(16 - SMAUGT_LOG_P_PRIME)
31
+ #endif
32
+
33
+ typedef struct Ciphertext {
34
+ polyvec c1;
35
+ poly c2;
36
+ } ciphertext;
37
+
38
+ #define computeC1 SMAUGT_NAMESPACE(computeC1)
39
+ #define computeC2 SMAUGT_NAMESPACE(computeC2)
40
+
41
+ void computeC1(polyvec *c1, const polyvec A[SMAUGT_K], const polyvec *r);
42
+ void computeC2(poly *c2, const uint8_t mu[SMAUGT_MSG_BYTES], const polyvec *b,
43
+ const polyvec *r);
44
+
45
+ #endif /* !SMAUGT_CIPHERTEXT_H */
@@ -0,0 +1,12 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ #ifndef SMAUGT_COMMON_H
4
+ #define SMAUGT_COMMON_H
5
+
6
+ #define SMAUGT_CONCAT_(x1, x2) x1##x2
7
+ #define SMAUGT_CONCAT(x1, x2) SMAUGT_CONCAT_(x1, x2)
8
+
9
+ #define SMAUGT_STR_(x) #x
10
+ #define SMAUGT_STR(x) SMAUGT_STR_(x)
11
+
12
+ #endif /* !SMAUGT_COMMON_H */
@@ -0,0 +1,48 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ #ifndef SMAUGT_CONFIG_H
4
+ #define SMAUGT_CONFIG_H
5
+
6
+ #include "common.h"
7
+
8
+ #define SMAUGT_MODE1 0
9
+ #define SMAUGT_MODE3 1
10
+ #define SMAUGT_MODE5 2
11
+ #define SMAUGT_MODET 3
12
+
13
+ #if !defined(SMAUGT_CONFIG_MODE)
14
+ #define SMAUGT_CONFIG_MODE SMAUGT_MODE1
15
+ #endif
16
+
17
+ #if (SMAUGT_CONFIG_MODE != SMAUGT_MODE1) && \
18
+ (SMAUGT_CONFIG_MODE != SMAUGT_MODE3) && \
19
+ (SMAUGT_CONFIG_MODE != SMAUGT_MODE5) && \
20
+ (SMAUGT_CONFIG_MODE != SMAUGT_MODET)
21
+ #error "Invalid SMAUGT_CONFIG_MODE"
22
+ #endif
23
+
24
+ #if !defined(SMAUGT_CONFIG_MODE_STRING)
25
+
26
+ #if SMAUGT_CONFIG_MODE == SMAUGT_MODE1
27
+ #define SMAUGT_CONFIG_MODE_STRING mode1
28
+ #elif SMAUGT_CONFIG_MODE == SMAUGT_MODE3
29
+ #define SMAUGT_CONFIG_MODE_STRING mode3
30
+ #elif SMAUGT_CONFIG_MODE == SMAUGT_MODE5
31
+ #define SMAUGT_CONFIG_MODE_STRING mode5
32
+ #elif SMAUGT_CONFIG_MODE == SMAUGT_MODET
33
+ #define SMAUGT_CONFIG_MODE_STRING modet
34
+ #endif
35
+
36
+ #endif
37
+
38
+ #if !defined(SMAUGT_CONFIG_NAMESPACE_PREFIX)
39
+ #define SMAUGT_CONFIG_NAMESPACE_PREFIX cryptolab_smaugt_
40
+ #endif
41
+
42
+ #if !defined(SMAUGT_CONFIG_NAMESPACE_PREFIX_MODE)
43
+ #define SMAUGT_ADD_MODE(s) SMAUGT_CONCAT(s, SMAUGT_CONFIG_MODE_STRING)
44
+ #define SMAUGT_CONFIG_NAMESPACE_PREFIX_MODE \
45
+ SMAUGT_CONCAT(SMAUGT_ADD_MODE(SMAUGT_CONFIG_NAMESPACE_PREFIX), _)
46
+ #endif
47
+
48
+ #endif /* !SMAUGT_CONFIG_H */
@@ -0,0 +1,28 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ #ifndef SMAUGT_DG_H
4
+ #define SMAUGT_DG_H
5
+
6
+ #include "params.h"
7
+ #include "poly.h"
8
+
9
+ #include <stdint.h>
10
+
11
+ // error distribution for LWE instances in public key generation:
12
+ // store probability table (CDF_TABLE)
13
+ // sample a single error from (SMAUGT_DG_RAND_BITS)-length of random binary
14
+ // string A discrete error distribution close to the discrete Gaussian
15
+ // distribution
16
+ #define SMAUGT_DG_RAND_BITS 10 // bits for RND + SIGN
17
+ #define SMAUGT_DG_SMAUGT_SLEN 2
18
+ #define SMAUGT_DG_SMAUGT_SEED_LEN \
19
+ (SMAUGT_DG_RAND_BITS * SMAUGT_N / 64) // 64-bit seed length
20
+
21
+ #define d_gaussian_poly SMAUGT_NAMESPACE(d_gaussian_poly)
22
+ #define d_gaussian SMAUGT_NAMESPACE(d_gaussian)
23
+
24
+ int d_gaussian_poly(poly *op, const uint8_t *seed);
25
+
26
+ void d_gaussian(polyvec *op, const uint8_t seed[SMAUGT_CRYPTO_BYTES]);
27
+
28
+ #endif /* !SMAUGT_DG_H */
@@ -0,0 +1,41 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ #ifndef SMAUGT_FIPS202_H
4
+ #define SMAUGT_FIPS202_H
5
+
6
+ #include <stddef.h>
7
+ #include <stdint.h>
8
+
9
+ #define SHAKE128_RATE 168
10
+ #define SHAKE256_RATE 136
11
+ #define SHA3_256_RATE 136
12
+ #define SHA3_512_RATE 72
13
+
14
+ #define SHA3_256_HashSize 32
15
+ #define SHA3_512_HashSize 64
16
+
17
+ typedef struct {
18
+ uint64_t s[25];
19
+ unsigned int pos;
20
+ } keccak_state;
21
+
22
+ void shake128_init(keccak_state *state);
23
+ void shake128_absorb(keccak_state *state, const uint8_t *in, size_t inlen);
24
+ void shake128_finalize(keccak_state *state);
25
+ void shake128_squeeze(uint8_t *out, size_t outlen, keccak_state *state);
26
+ void shake128_absorb_once(keccak_state *state, const uint8_t *in, size_t inlen);
27
+ void shake128_squeezeblocks(uint8_t *out, size_t nblocks, keccak_state *state);
28
+
29
+ void shake256_init(keccak_state *state);
30
+ void shake256_absorb(keccak_state *state, const uint8_t *in, size_t inlen);
31
+ void shake256_finalize(keccak_state *state);
32
+ void shake256_squeeze(uint8_t *out, size_t outlen, keccak_state *state);
33
+ void shake256_absorb_once(keccak_state *state, const uint8_t *in, size_t inlen);
34
+ void shake256_squeezeblocks(uint8_t *out, size_t nblocks, keccak_state *state);
35
+
36
+ void shake128(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen);
37
+ void shake256(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen);
38
+ void sha3_256(uint8_t h[32], const uint8_t *in, size_t inlen);
39
+ void sha3_512(uint8_t h[64], const uint8_t *in, size_t inlen);
40
+
41
+ #endif /* !SMAUGT_FIPS202_H */
@@ -0,0 +1,21 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ #ifndef SMAUGT_HASH_H
4
+ #define SMAUGT_HASH_H
5
+
6
+ #include "params.h"
7
+
8
+ #include <stddef.h>
9
+ #include <stdint.h>
10
+
11
+ #define hash_h(OUT, IN, INLEN) sha3_256(OUT, IN, INLEN)
12
+ #define hash_g(OUT, OUTLEN, IN1, INLEN1, IN2, INLEN2) \
13
+ shake256_absorb_twice_squeeze(OUT, OUTLEN, IN1, INLEN1, IN2, INLEN2)
14
+ #define shake256_absorb_twice_squeeze \
15
+ SMAUGT_NAMESPACE(shake256_absorb_twice_squeeze)
16
+
17
+ void shake256_absorb_twice_squeeze(uint8_t *out, size_t outlen,
18
+ const uint8_t *in1, size_t inlen1,
19
+ const uint8_t *in2, size_t inlen2);
20
+
21
+ #endif /* !SMAUGT_HASH_H */