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,318 @@
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
+ /*************************************************
61
+ * Name: poly_frombytes
62
+ *
63
+ * Description: De-serialization of a polynomial;
64
+ * inverse of poly_tobytes
65
+ *
66
+ * Arguments: - poly *r: pointer to output polynomial
67
+ * - const uint8_t *a: pointer to input byte array
68
+ * (of NTRUPLUS_POLYBYTES bytes)
69
+ **************************************************/
70
+ void poly_frombytes(poly *r, const uint8_t a[NTRUPLUS_POLYBYTES])
71
+ {
72
+ for(size_t i = 0; i < NTRUPLUS_N/2; i++)
73
+ {
74
+ r->coeffs[2*i] = ((a[3*i+0] >> 0) | ((uint16_t)a[3*i+1] << 8)) & 0xFFF;
75
+ r->coeffs[2*i+1] = ((a[3*i+1] >> 4) | ((uint16_t)a[3*i+2] << 4)) & 0xFFF;
76
+ }
77
+ }
78
+
79
+ /*************************************************
80
+ * Name: poly_cbd1
81
+ *
82
+ * Description: Sample a polynomial deterministically from a random,
83
+ * with output polynomial close to centered binomial distribution
84
+ *
85
+ * Arguments: - poly *r: pointer to output polynomial
86
+ * - const uint8_t *buf: pointer to input random
87
+ * (of length NTRUPLUS_N/4 bytes)
88
+ **************************************************/
89
+ void poly_cbd1(poly *r, const unsigned char buf[NTRUPLUS_N/4])
90
+ {
91
+ uint8_t t1, t2;
92
+
93
+ for(size_t i = 0; i < NTRUPLUS_N / 8; i++)
94
+ {
95
+ t1 = buf[i];
96
+ t2 = buf[i + NTRUPLUS_N / 8];
97
+
98
+ for(size_t j = 0; j < 8; j++)
99
+ {
100
+ r->coeffs[8*i + j] = (t1 & 0x1) - (t2 & 0x1);
101
+
102
+ t1 >>= 1;
103
+ t2 >>= 1;
104
+ }
105
+ }
106
+ }
107
+
108
+ /*************************************************
109
+ * Name: poly_sotp_encode
110
+ *
111
+ * Description: Encode a message deterministically using SOTP and a random,
112
+ with output polynomial close to centered binomial distribution
113
+ *
114
+ * Arguments: - poly *r: pointer to output polynomial
115
+ * - const uint8_t *msg: pointer to input message
116
+ * - const uint8_t *buf: pointer to input random
117
+ **************************************************/
118
+ void poly_sotp_encode(poly *r, const uint8_t msg[NTRUPLUS_N/8], const uint8_t buf[NTRUPLUS_N/4])
119
+ {
120
+ uint8_t tmp[NTRUPLUS_N/4];
121
+
122
+ for(int i = 0; i < NTRUPLUS_N/8; i++)
123
+ {
124
+ tmp[i] = buf[i]^msg[i];
125
+ }
126
+
127
+ for(int i = NTRUPLUS_N/8; i < NTRUPLUS_N/4; i++)
128
+ {
129
+ tmp[i] = buf[i];
130
+ }
131
+
132
+ poly_cbd1(r, tmp);
133
+ }
134
+
135
+ /*************************************************
136
+ * Name: poly_sotp_decode
137
+ *
138
+ * Description: Decode a message deterministically using SOTP_INV and a random
139
+ *
140
+ * Arguments: - uint8_t *msg: pointer to output message
141
+ * - const poly *a: pointer to iput polynomial
142
+ * - const uint8_t *buf: pointer to input random
143
+ *
144
+ * Returns 0 (success) or 1 (failure)
145
+ **************************************************/
146
+ int poly_sotp_decode(uint8_t msg[NTRUPLUS_N/8], const poly *a, const uint8_t buf[NTRUPLUS_N/4])
147
+ {
148
+ uint8_t t1, t2, t3;
149
+ uint16_t t4;
150
+ uint32_t r = 0;
151
+ uint8_t mask;
152
+
153
+ for(size_t i = 0; i < NTRUPLUS_N / 8; i++)
154
+ {
155
+ t1 = buf[i ];
156
+ t2 = buf[i + NTRUPLUS_N / 8];
157
+ t3 = 0;
158
+
159
+ for(size_t j = 0; j < 8; j++)
160
+ {
161
+ t4 = t2 & 0x1;
162
+ t4 += a->coeffs[8*i + j];
163
+ r |= t4;
164
+ t4 = (t4 ^ t1) & 0x1;
165
+ t3 ^= (uint8_t)(t4 << j);
166
+
167
+ t1 >>= 1;
168
+ t2 >>= 1;
169
+ }
170
+
171
+ msg[i] = t3;
172
+ }
173
+
174
+ r = r >> 1;
175
+ r = (-(uint32_t)r) >> 31;
176
+
177
+ mask = (uint8_t)(r - 1);
178
+
179
+ for (size_t i = 0; i < NTRUPLUS_N / 8; i++)
180
+ msg[i] &= mask;
181
+
182
+ return r;
183
+ }
184
+
185
+ /*************************************************
186
+ * Name: poly_ntt
187
+ *
188
+ * Description: Computes number-theoretic transform (NTT)
189
+ *
190
+ * Arguments: - poly *r: pointer to input/output polynomial
191
+ **************************************************/
192
+ void poly_ntt(poly *r)
193
+ {
194
+ ntt(r->coeffs);
195
+ }
196
+
197
+ /*************************************************
198
+ * Name: poly_invntt
199
+ *
200
+ * Description: Computes inverse of number-theoretic transform (NTT)
201
+ *
202
+ * Arguments: - poly *r: pointer to input/output polynomial
203
+ **************************************************/
204
+ void poly_invntt(poly *r)
205
+ {
206
+ invntt(r->coeffs);
207
+ }
208
+
209
+ /*************************************************
210
+ * Name: poly_baseinv
211
+ *
212
+ * Description: Inverse of polynomial in NTT domain
213
+ *
214
+ * Arguments: - poly *r: pointer to output polynomial
215
+ * - const poly *a: pointer to input polynomial
216
+ **************************************************/
217
+ int poly_baseinv(poly *r, const poly *a)
218
+ {
219
+ for(size_t i = 0; i < NTRUPLUS_N/8; ++i)
220
+ {
221
+ if(baseinv(r->coeffs + 8*i, a->coeffs + 8*i, zetas[144 + i]))
222
+ {
223
+ for (size_t j = 0; j < NTRUPLUS_N; ++j)
224
+ r->coeffs[j] = 0;
225
+
226
+ return 1;
227
+ }
228
+
229
+ if(baseinv(r->coeffs + 8*i + 4, a->coeffs + 8*i + 4, -zetas[144 + i]))
230
+ {
231
+ for (size_t j = 0; j < NTRUPLUS_N; ++j)
232
+ r->coeffs[j] = 0;
233
+
234
+ return 1;
235
+ }
236
+ }
237
+
238
+ return 0;
239
+ }
240
+
241
+ /*************************************************
242
+ * Name: poly_basemul
243
+ *
244
+ * Description: Multiplication of two polynomials in NTT domain
245
+ *
246
+ * Arguments: - poly *r: pointer to output polynomial
247
+ * - const poly *a: pointer to first input polynomial
248
+ * - const poly *b: pointer to second input polynomial
249
+ **************************************************/
250
+ void poly_basemul(poly *r, const poly *a, const poly *b)
251
+ {
252
+ for(int i = 0; i < NTRUPLUS_N/8; ++i)
253
+ {
254
+ basemul(r->coeffs + 8*i, a->coeffs + 8*i, b->coeffs + 8*i, zetas[144 + i]);
255
+ basemul(r->coeffs + 8*i + 4, a->coeffs + 8*i + 4, b->coeffs + 8*i + 4, -zetas[144 + i]);
256
+ }
257
+ }
258
+
259
+ /*************************************************
260
+ * Name: poly_basemul_add
261
+ *
262
+ * Description: Multiplication of two polynomials in NTT domain
263
+ *
264
+ * Arguments: - poly *r: pointer to output polynomial
265
+ * - const poly *a: pointer to first input polynomial
266
+ * - const poly *b: pointer to second input polynomial
267
+ **************************************************/
268
+ void poly_basemul_add(poly *r, const poly *a, const poly *b, const poly *c)
269
+ {
270
+ for(int i = 0; i < NTRUPLUS_N/8; ++i)
271
+ {
272
+ basemul_add(r->coeffs + 8*i, a->coeffs + 8*i, b->coeffs + 8*i, c->coeffs + 8*i, zetas[144 + i]);
273
+ basemul_add(r->coeffs + 8*i + 4, a->coeffs + 8*i + 4, b->coeffs + 8*i + 4, c->coeffs + 8*i + 4, -zetas[144 + i]);
274
+ }
275
+ }
276
+
277
+ /*************************************************
278
+ * Name: poly_sub
279
+ *
280
+ * Description: Subtract two polynomials; no modular reduction is performed
281
+ *
282
+ * Arguments: - poly *r: pointer to output polynomial
283
+ * - const poly *a: pointer to first input polynomial
284
+ * - const poly *b: pointer to second input polynomial
285
+ **************************************************/
286
+ void poly_sub(poly *r, const poly *a, const poly *b)
287
+ {
288
+ for(int i = 0; i < NTRUPLUS_N; ++i)
289
+ r->coeffs[i] = a->coeffs[i] - b->coeffs[i];
290
+ }
291
+
292
+ /*************************************************
293
+ * Name: poly_triple
294
+ *
295
+ * Description: Multiply polynomial by 3; no modular reduction is performed
296
+ *
297
+ * Arguments: - poly *r: pointer to output polynomial
298
+ * - const poly *a: pointer to input polynomial
299
+ **************************************************/
300
+ void poly_triple(poly *r, const poly *a)
301
+ {
302
+ for(int i = 0; i < NTRUPLUS_N; ++i)
303
+ r->coeffs[i] = 3*a->coeffs[i];
304
+ }
305
+
306
+ /*************************************************
307
+ * Name: poly_crepmod3
308
+ *
309
+ * Description: Compute modulus 3 operation to polynomial
310
+ *
311
+ * Arguments: - poly *r: pointer to output polynomial
312
+ * - const poly *a: pointer to input polynomial
313
+ **************************************************/
314
+ void poly_crepmod3(poly *r, const poly *a)
315
+ {
316
+ for(int i = 0; i < NTRUPLUS_N; i++)
317
+ r->coeffs[i] = crepmod3(a->coeffs[i]);
318
+ }
@@ -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