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,320 @@
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
+ uint8_t mask;
151
+
152
+ for(size_t i = 0; i < NTRUPLUS_N / 8; i++)
153
+ {
154
+ t1 = buf[i ];
155
+ t2 = buf[i + NTRUPLUS_N / 8];
156
+ t3 = 0;
157
+
158
+ for(size_t j = 0; j < 8; j++)
159
+ {
160
+ t4 = t2 & 0x1;
161
+ t4 += a->coeffs[8*i + j];
162
+ r |= t4;
163
+ t4 = (t4 ^ t1) & 0x1;
164
+ t3 ^= (uint8_t)(t4 << j);
165
+
166
+ t1 >>= 1;
167
+ t2 >>= 1;
168
+ }
169
+
170
+ msg[i] = t3;
171
+ }
172
+
173
+ r = r >> 1;
174
+ r = (-(uint32_t)r) >> 31;
175
+
176
+ mask = (uint8_t)(r - 1);
177
+
178
+ for (size_t i = 0; i < NTRUPLUS_N / 8; i++)
179
+ msg[i] &= mask;
180
+
181
+ return r;
182
+ }
183
+
184
+ /*************************************************
185
+ * Name: poly_ntt
186
+ *
187
+ * Description: Computes number-theoretic transform (NTT)
188
+ *
189
+ * Arguments: - poly *r: pointer to input/output polynomial
190
+ **************************************************/
191
+ void poly_ntt(poly *r)
192
+ {
193
+ ntt(r->coeffs);
194
+ }
195
+
196
+ /*************************************************
197
+ * Name: poly_invntt
198
+ *
199
+ * Description: Computes inverse of number-theoretic transform (NTT)
200
+ *
201
+ * Arguments: - poly *r: pointer to input/output polynomial
202
+ **************************************************/
203
+ void poly_invntt(poly *r)
204
+ {
205
+ invntt(r->coeffs);
206
+ }
207
+
208
+ /*************************************************
209
+ * Name: poly_baseinv
210
+ *
211
+ * Description: Inversion of polynomial in NTT domain
212
+ *
213
+ * Arguments: - poly *r: pointer to output polynomial
214
+ * - const poly *a: pointer to input polynomial
215
+ *
216
+ * Returns: integer
217
+ **************************************************/
218
+ int poly_baseinv(poly *r, const poly *a)
219
+ {
220
+ for(size_t i = 0; i < NTRUPLUS_N/8; ++i)
221
+ {
222
+ if(baseinv(r->coeffs + 8*i, a->coeffs + 8*i, zetas[96 + i]))
223
+ {
224
+ for (size_t j = 0; j < NTRUPLUS_N; ++j)
225
+ r->coeffs[j] = 0;
226
+
227
+ return 1;
228
+ }
229
+
230
+ if(baseinv(r->coeffs + 8*i + 4, a->coeffs + 8*i + 4, -zetas[96 + i]))
231
+ {
232
+ for (size_t j = 0; j < NTRUPLUS_N; ++j)
233
+ r->coeffs[j] = 0;
234
+
235
+ return 1;
236
+ }
237
+ }
238
+
239
+ return 0;
240
+ }
241
+
242
+ /*************************************************
243
+ * Name: poly_basemul
244
+ *
245
+ * Description: Multiplication of two polynomials in NTT domain
246
+ *
247
+ * Arguments: - poly *r: pointer to output polynomial
248
+ * - const poly *a: pointer to first input polynomial
249
+ * - const poly *b: pointer to second input polynomial
250
+ **************************************************/
251
+ void poly_basemul(poly *r, const poly *a, const poly *b)
252
+ {
253
+ for(int i = 0; i < NTRUPLUS_N/8; ++i)
254
+ {
255
+ basemul(r->coeffs + 8*i, a->coeffs + 8*i, b->coeffs + 8*i, zetas[96 + i]);
256
+ basemul(r->coeffs + 8*i + 4, a->coeffs + 8*i + 4, b->coeffs + 8*i + 4, -zetas[96 + i]);
257
+ }
258
+ }
259
+
260
+ /*************************************************
261
+ * Name: poly_basemul_add
262
+ *
263
+ * Description: Multiplication then addition of three polynomials in NTT domain
264
+ *
265
+ * Arguments: - poly *r: pointer to output polynomial
266
+ * - const poly *a: pointer to first input polynomial
267
+ * - const poly *b: pointer to second input polynomial
268
+ * - const poly *c: pointer to third input polynomial
269
+ **************************************************/
270
+ void poly_basemul_add(poly *r, const poly *a, const poly *b, const poly *c)
271
+ {
272
+ for(int i = 0; i < NTRUPLUS_N/8; ++i)
273
+ {
274
+ basemul_add(r->coeffs + 8*i, a->coeffs + 8*i, b->coeffs + 8*i, c->coeffs + 8*i, zetas[96 + i]);
275
+ basemul_add(r->coeffs + 8*i + 4, a->coeffs + 8*i + 4, b->coeffs + 8*i + 4, c->coeffs + 8*i + 4, -zetas[96 + i]);
276
+ }
277
+ }
278
+
279
+ /*************************************************
280
+ * Name: poly_sub
281
+ *
282
+ * Description: Subtract two polynomials; no modular reduction is performed
283
+ *
284
+ * Arguments: - poly *r: pointer to output polynomial
285
+ * - const poly *a: pointer to first input polynomial
286
+ * - const poly *b: pointer to second input polynomial
287
+ **************************************************/
288
+ void poly_sub(poly *r, const poly *a, const poly *b)
289
+ {
290
+ for(int i = 0; i < NTRUPLUS_N; ++i)
291
+ r->coeffs[i] = a->coeffs[i] - b->coeffs[i];
292
+ }
293
+
294
+ /*************************************************
295
+ * Name: poly_triple
296
+ *
297
+ * Description: Multiply polynomial by 3; no modular reduction is performed
298
+ *
299
+ * Arguments: - poly *r: pointer to output polynomial
300
+ * - const poly *a: pointer to input polynomial
301
+ **************************************************/
302
+ void poly_triple(poly *r, const poly *a)
303
+ {
304
+ for(int i = 0; i < NTRUPLUS_N; ++i)
305
+ r->coeffs[i] = 3*a->coeffs[i];
306
+ }
307
+
308
+ /*************************************************
309
+ * Name: poly_crepmod3
310
+ *
311
+ * Description: Compute modulus 3 operation to polynomial
312
+ *
313
+ * Arguments: - poly *r: pointer to output polynomial
314
+ * - const poly *a: pointer to input polynomial
315
+ **************************************************/
316
+ void poly_crepmod3(poly *r, const poly *a)
317
+ {
318
+ for(int i = 0; i < NTRUPLUS_N; i++)
319
+ r->coeffs[i] = crepmod3(a->coeffs[i]);
320
+ }
@@ -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,24 @@
1
+ #ifndef API_H
2
+ #define API_H
3
+
4
+ #include "params.h"
5
+
6
+ #define CRYPTO_SECRETKEYBYTES NTRUPLUS_SECRETKEYBYTES
7
+ #define CRYPTO_PUBLICKEYBYTES NTRUPLUS_PUBLICKEYBYTES
8
+ #define CRYPTO_CIPHERTEXTBYTES NTRUPLUS_CIPHERTEXTBYTES
9
+ #define CRYPTO_BYTES NTRUPLUS_SSBYTES
10
+
11
+ #define CRYPTO_ALGNAME NTRUPLUS_ALGNAME
12
+
13
+ int crypto_kem_keypair(unsigned char *pk,
14
+ unsigned char *sk);
15
+
16
+ int crypto_kem_enc(unsigned char *ct,
17
+ unsigned char *ss,
18
+ const unsigned char *pk);
19
+
20
+ int crypto_kem_dec(unsigned char *ss,
21
+ const unsigned char *ct,
22
+ const unsigned char *sk);
23
+
24
+ #endif