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,383 @@
1
+ #include <stdint.h>
2
+ #include "params.h"
3
+ #include "ntt.h"
4
+
5
+ #define NTRUPLUS_R -147 // R = 2^16 mod q
6
+ #define NTRUPLUS_RINV -682 // (R)^(-1) mod q
7
+ #define NTRUPLUS_RSQ 867 // (R^2) mod q
8
+ #define NTRUPLUS_QINV 12929 // (q)^(-1) mod (2^16)
9
+
10
+ #define NTRUPLUS_OMEGA -886 // (omega * R) mod q
11
+ #define NTRUPLUS_ZMINUSZ5INV -1665 // (z - z^5)^(-1) * R mod q
12
+ // where z = zeta^((n/d)/6)
13
+
14
+ #define NTRUPLUS_NINV -1693 // (n/d)^(-1) * R mod q
15
+ #define NTRUPLUS_2NINV 71 // 2 * (n/d)^(-1) * R mod q
16
+
17
+ // zetas: Montgomery-form twiddle factors
18
+ const int16_t zetas[288] = {
19
+ -147, -1033, -1265, 708, 460, 1265, -467, 727,
20
+ 556, 1307, -773, -161, 1200, -1612, 570, 1529,
21
+ 1135, -556, 1120, 298, -822, -1556, -93, 1463,
22
+ 532, -377, -909, 58, -392, -450, 1722, 1236,
23
+ -486, -491, -1569, -1078, 36, 1289, -1443, 1628,
24
+ 1664, -725, -952, 99, -1020, 353, -599, 1119,
25
+ 592, 839, 1622, 652, 1244, -783, -1085, -726,
26
+ 566, -284, -1369, -1292, 268, -391, 781, -172,
27
+ 96, -1172, 211, 737, 473, -445, -234, 264,
28
+ -1536, 1467, -676, -1542, -170, 635, -705, -1332,
29
+ -658, 831, -1712, 1311, 1488, -881, 1087, -1315,
30
+ 1245, -75, 791, -6, -875, -697, -70, -1162,
31
+ 287, -767, -945, 1598, -882, 1261, 206, 654,
32
+ -1421, -81, 716, -1251, 838, -1300, 1035, -104,
33
+ 966, -558, -61, -1704, 404, -899, 862, -1593,
34
+ -1460, -37, 1266, 965, -1584, -1404, -265, -942,
35
+ 905, 1195, -619, 787, 118, 576, 286, -1475,
36
+ -194, 928, 1229, -1032, 1608, 1111, -1669, 642,
37
+ -1323, 163, 309, 981, -557, -258, 232, -1680,
38
+ -1657, -1233, 144, 1699, 311, -1060, 578, 1298,
39
+ -403, 1607, 1074, -148, 447, -1568, 1142, -402,
40
+ -1412, -623, 855, 365, -98, -244, 407, 1225,
41
+ 416, 683, -105, 1714, -1019, 1061, 1163, 638,
42
+ 798, 1493, -351, 396, -542, -9, 1616, -139,
43
+ -987, -482, 889, 238, -1513, 466, -1089, -101,
44
+ 849, -426, 1589, 1487, 671, 1459, -776, 255,
45
+ -1014, 1144, 472, -1153, -325, 1519, -26, -1123,
46
+ 324, 1230, 1547, -593, -428, 1192, 1072, -1564,
47
+ 688, -333, 1023, -1686, 841, 824, -71, 1587,
48
+ 522, -323, 1148, 389, 1231, 384, 1343, 169,
49
+ 628, -1329, -1056, -936, 24, -293, 1523, -300,
50
+ -1654, 891, -962, -67, 179, -1177, 844, -509,
51
+ -1677, -1565, -549, -1508, 1191, -280, -43, 669,
52
+ -746, 753, 770, -1046, 1711, 1438, 690, 1083,
53
+ 1062, 1727, -883, 553, 1670, 66, 825, -133,
54
+ -1586, 637, -680, -917, 644, -372, -1193, -1136
55
+ };
56
+
57
+ /*************************************************
58
+ * Name: montgomery_reduce
59
+ *
60
+ * Description: Montgomery reduction; given a 32-bit integer a, computes
61
+ * a 16-bit integer congruent to a * R^-1 mod q,
62
+ * where R = 2^16.
63
+ *
64
+ * Arguments: - int32_t a: input integer to be reduced;
65
+ * must lie in {-q*2^15, ..., q*2^15-1}
66
+ *
67
+ * Returns: an integer in {-q+1, ..., q-1} congruent to
68
+ * a * R^-1 mod q.
69
+ **************************************************/
70
+ static inline int16_t montgomery_reduce(int32_t a)
71
+ {
72
+ int16_t t;
73
+
74
+ t = (int16_t)a * NTRUPLUS_QINV;
75
+ t = (a - (int32_t)t * NTRUPLUS_Q) >> 16;
76
+ return t;
77
+ }
78
+
79
+ /*************************************************
80
+ * Name: barrett_reduce
81
+ *
82
+ * Description: Barrett reduction; given a 16-bit integer a, computes a
83
+ * centered representative congruent to a mod q in
84
+ * {-(q+1)/2, ..., (q+1)/2}.
85
+ *
86
+ * Arguments: - int16_t a: input integer to be reduced
87
+ *
88
+ * Returns: integer in {-(q+1)/2, ..., (q+1)/2} congruent to a mod q.
89
+ **************************************************/
90
+ static inline int16_t barrett_reduce(int16_t a)
91
+ {
92
+ int16_t t;
93
+ const int16_t v = ((1<<26) + NTRUPLUS_Q/2) / NTRUPLUS_Q;
94
+
95
+ t = ((int32_t)v*a + (1<<25)) >> 26;
96
+ t *= NTRUPLUS_Q;
97
+ return a - t;
98
+ }
99
+
100
+ /*************************************************
101
+ * Name: fqmul
102
+ *
103
+ * Description: Multiplication followed by Montgomery reduction.
104
+ *
105
+ * Arguments: - int16_t a: first factor
106
+ * - int16_t b: second factor
107
+ *
108
+ * Returns: 16-bit integer congruent to a*b*R^-1 mod q.
109
+ **************************************************/
110
+ static inline int16_t fqmul(int16_t a, int16_t b)
111
+ {
112
+ return montgomery_reduce((int32_t)a * b);
113
+ }
114
+
115
+ /*************************************************
116
+ * Name: fqinv
117
+ *
118
+ * Description: Computes the multiplicative inverse of a value in the
119
+ * finite field Z_q.
120
+ *
121
+ * Arguments: - int16_t a: input value a mod q
122
+ *
123
+ * Returns: 16-bit integer congruent to a^{-1} mod q.
124
+ **************************************************/
125
+ static inline int16_t fqinv(int16_t a)
126
+ {
127
+ int16_t t1, t2, t3;
128
+
129
+ t1 = fqmul(a, a); // 10
130
+ t2 = fqmul(t1, t1); // 100
131
+ t2 = fqmul(t2, t2); // 1000
132
+ t3 = fqmul(t2, t2); // 10000
133
+
134
+ t1 = fqmul(t1, t2); // 1010
135
+
136
+ t2 = fqmul(t1, t3); // 11010
137
+ t2 = fqmul(t2, t2); // 110100
138
+ t2 = fqmul(t2, a); // 110101
139
+
140
+ t1 = fqmul(t1, t2); // 111111
141
+
142
+ t2 = fqmul(t2, t2); // 1101010
143
+ t2 = fqmul(t2, t2); // 11010100
144
+ t2 = fqmul(t2, t2); // 110101000
145
+ t2 = fqmul(t2, t2); // 1101010000
146
+ t2 = fqmul(t2, t2); // 11010100000
147
+ t2 = fqmul(t2, t2); // 110101000000
148
+ t2 = fqmul(t2, t1); // 110101111111
149
+
150
+ t2 = fqmul(NTRUPLUS_RINV, t2);
151
+
152
+ return t2;
153
+ }
154
+
155
+ /*************************************************
156
+ * Name: ntt
157
+ *
158
+ * Description: Number-theoretic transform (NTT) in R_q. Transforms the
159
+ * coefficient representation of r into a representation
160
+ * where each block of 3 coefficients corresponds to an
161
+ * element of Zq[X]/(X^3 - zeta_i).
162
+ *
163
+ * Arguments: - int16_t r[NTRUPLUS_N]: pointer to input/output vector;
164
+ * input coefficients of r in R_q,
165
+ * output NTT representation in the
166
+ * product ring Zq[X]/(X^3 - zeta_i)
167
+ *
168
+ * Returns: none.
169
+ **************************************************/
170
+ void ntt(int16_t r[NTRUPLUS_N])
171
+ {
172
+ int16_t t1, t2, t3;
173
+ int16_t zeta1, zeta2;
174
+
175
+ int k = 1;
176
+
177
+ zeta1 = zetas[k++];
178
+
179
+ for (int i = 0; i < NTRUPLUS_N/2; i++)
180
+ {
181
+ t1 = fqmul(zeta1, r[i + NTRUPLUS_N/2]);
182
+
183
+ r[i + NTRUPLUS_N/2] = r[i] + r[i + NTRUPLUS_N/2] - t1;
184
+ r[i ] = r[i] + t1;
185
+ }
186
+
187
+ for (int step = NTRUPLUS_N/6; step >= 48; step = step/3)
188
+ {
189
+ for (int start = 0; start < NTRUPLUS_N; start += 3*step)
190
+ {
191
+ zeta1 = zetas[k++];
192
+ zeta2 = zetas[k++];
193
+
194
+ for (int i = start; i < start + step; i++)
195
+ {
196
+ t1 = fqmul(zeta1, r[i + step]);
197
+ t2 = fqmul(zeta2, r[i + 2*step]);
198
+ t3 = fqmul(NTRUPLUS_OMEGA, t1 - t2);
199
+
200
+ r[i + 2*step] = r[i] - t1 - t3;
201
+ r[i + step] = r[i] - t2 + t3;
202
+ r[i ] = r[i] + t1 + t2;
203
+ }
204
+ }
205
+ }
206
+
207
+ for (int step = 24; step >= 3; step >>= 1)
208
+ {
209
+ for (int start = 0; start < NTRUPLUS_N; start += (step << 1))
210
+ {
211
+ zeta1 = zetas[k++];
212
+
213
+ for (int i = start; i < start + step; i++)
214
+ {
215
+ t1 = fqmul(zeta1, r[i + step]);
216
+
217
+ r[i + step] = barrett_reduce(r[i] - t1);
218
+ r[i ] = barrett_reduce(r[i] + t1);
219
+ }
220
+ }
221
+ }
222
+ }
223
+
224
+ /*************************************************
225
+ * Name: invntt
226
+ *
227
+ * Description: Inverse number-theoretic transform (NTT) in R_q. Transforms
228
+ * the NTT representation in r, where each block of 3
229
+ * coefficients corresponds to an element of Zq[X]/(X^3 - zeta_i),
230
+ * back to the coefficient representation in R_q.
231
+ *
232
+ * Arguments: - int16_t r[NTRUPLUS_N]: pointer to input/output vector;
233
+ * input NTT representation in the
234
+ * product ring Zq[X]/(X^3 - zeta_i),
235
+ * output coefficient representation
236
+ * in R_q
237
+ *
238
+ * Returns: none.
239
+ **************************************************/
240
+ void invntt(int16_t r[NTRUPLUS_N])
241
+ {
242
+ int16_t t1,t2,t3;
243
+ int16_t zeta1,zeta2;
244
+ int k = 287;
245
+
246
+ for (int step = 3; step <= 24; step <<= 1)
247
+ {
248
+ for (int start = 0; start < NTRUPLUS_N; start += (step << 1))
249
+ {
250
+ zeta1 = zetas[k--];
251
+
252
+ for (int i = start; i < start + step; i++)
253
+ {
254
+ t1 = r[i + step];
255
+
256
+ r[i + step] = fqmul(zeta1, t1 - r[i]);
257
+ r[i ] = barrett_reduce(r[i] + t1);
258
+ }
259
+ }
260
+ }
261
+
262
+ for (int step = 48; step <= NTRUPLUS_N/6; step = 3*step)
263
+ {
264
+ for (int start = 0; start < NTRUPLUS_N; start += 3*step)
265
+ {
266
+ zeta2 = zetas[k--];
267
+ zeta1 = zetas[k--];
268
+
269
+ for (int i = start; i < start + step; i++)
270
+ {
271
+ t1 = fqmul(NTRUPLUS_OMEGA, r[i + step] - r[i]);
272
+ t2 = fqmul(zeta1, r[i + 2*step] - r[i] + t1);
273
+ t3 = fqmul(zeta2, r[i + 2*step] - r[i + step] - t1);
274
+
275
+ r[i ] = barrett_reduce(r[i] + r[i + step] + r[i + 2*step]);
276
+ r[i + step] = t2;
277
+ r[i + 2*step] = t3;
278
+ }
279
+ }
280
+ }
281
+
282
+ for (int i = 0; i < NTRUPLUS_N/2; i++)
283
+ {
284
+ t1 = r[i] + r[i + NTRUPLUS_N/2];
285
+ t2 = fqmul(NTRUPLUS_ZMINUSZ5INV, r[i] - r[i + NTRUPLUS_N/2]);
286
+
287
+ r[i ] = fqmul(NTRUPLUS_NINV, t1 - t2);
288
+ r[i + NTRUPLUS_N/2] = fqmul(NTRUPLUS_2NINV, t2);
289
+ }
290
+ }
291
+
292
+ /*************************************************
293
+ * Name: baseinv
294
+ *
295
+ * Description: Inversion of a polynomial in Zq[X]/(X^3 - zeta), used as
296
+ * a building block for inversion of elements in R_q in the
297
+ * NTT domain.
298
+ *
299
+ * Arguments: - int16_t r[3]: pointer to the output polynomial
300
+ * - const int16_t a[3]: pointer to the input polynomial
301
+ * - const int16_t zeta: parameter defining X^3 - zeta
302
+ *
303
+ * Returns: 0 if a is invertible, 1 otherwise.
304
+ **************************************************/
305
+ int baseinv(int16_t r[3], const int16_t a[3], int16_t zeta)
306
+ {
307
+ int16_t t;
308
+
309
+ r[0] = montgomery_reduce(a[1]*a[2]); // R^-1
310
+ r[1] = montgomery_reduce(a[2]*a[2]); // R^-1
311
+ r[2] = montgomery_reduce(a[1]*a[1]-a[0]*a[2]); // R^-1
312
+ r[0] = montgomery_reduce(a[0]*a[0]-r[0]*zeta); // R^-1
313
+ r[1] = montgomery_reduce(r[1]*zeta-a[0]*a[1]); // R^-1
314
+
315
+ t = montgomery_reduce(r[2]*a[1]+r[1]*a[2]); // R^-2
316
+ t = montgomery_reduce(t*zeta+r[0]*a[0]); // R^-2
317
+
318
+ if(t == 0) return 1;
319
+
320
+ t = fqinv(t); // R^4
321
+
322
+ r[0] = montgomery_reduce(r[0]*t); // R^0
323
+ r[1] = montgomery_reduce(r[1]*t); // R^0
324
+ r[2] = montgomery_reduce(r[2]*t); // R^0
325
+
326
+ return 0;
327
+ }
328
+
329
+ /*************************************************
330
+ * Name: basemul
331
+ *
332
+ * Description: Multiplication of polynomials in Zq[X]/(X^3 - zeta),
333
+ * used for multiplication of elements in R_q in the NTT domain.
334
+ *
335
+ * Arguments: - int16_t r[3]: pointer to the output polynomial
336
+ * - const int16_t a[3]: pointer to the first factor
337
+ * - const int16_t b[3]: pointer to the second factor
338
+ * - const int16_t zeta: parameter defining X^3 - zeta
339
+ *
340
+ * Returns: none.
341
+ **************************************************/
342
+ void basemul(int16_t r[3], const int16_t a[3], const int16_t b[3], int16_t zeta)
343
+ {
344
+ r[0] = montgomery_reduce(a[2]*b[1]+a[1]*b[2]); // R^-1
345
+ r[1] = montgomery_reduce(a[2]*b[2]); // R^-1
346
+
347
+ r[0] = montgomery_reduce(r[0]*zeta+a[0]*b[0]); // R^-1
348
+ r[1] = montgomery_reduce(r[1]*zeta+a[0]*b[1]+a[1]*b[0]); // R^-1
349
+ r[2] = montgomery_reduce(a[2]*b[0]+a[1]*b[1]+a[0]*b[2]); // R^-1
350
+
351
+ r[0] = montgomery_reduce(r[0]*NTRUPLUS_RSQ); // R^0
352
+ r[1] = montgomery_reduce(r[1]*NTRUPLUS_RSQ); // R^0
353
+ r[2] = montgomery_reduce(r[2]*NTRUPLUS_RSQ); // R^0
354
+ }
355
+
356
+ /*************************************************
357
+ * Name: basemul_add
358
+ *
359
+ * Description: Multiplication then addition of polynomials in
360
+ * Zq[X]/(X^3 - zeta), used for multiplication of
361
+ * elements in R_q in the NTT domain.
362
+ *
363
+ * Arguments: - int16_t r[3]: pointer to the output polynomial
364
+ * - const int16_t a[3]: pointer to the first factor
365
+ * - const int16_t b[3]: pointer to the second factor
366
+ * - const int16_t c[3]: pointer to the third factor
367
+ * - const int16_t zeta: parameter defining X^3 - zeta
368
+ *
369
+ * Returns: none.
370
+ **************************************************/
371
+ void basemul_add(int16_t r[3], const int16_t a[3], const int16_t b[3], const int16_t c[3], int16_t zeta)
372
+ {
373
+ r[0] = montgomery_reduce(a[2]*b[1]+a[1]*b[2]); // R^-1
374
+ r[1] = montgomery_reduce(a[2]*b[2]); // R^-1
375
+
376
+ r[0] = montgomery_reduce(r[0]*zeta+a[0]*b[0]); // R^-1
377
+ r[1] = montgomery_reduce(r[1]*zeta+a[0]*b[1]+a[1]*b[0]); // R^-1
378
+ r[2] = montgomery_reduce(a[2]*b[0]+a[1]*b[1]+a[0]*b[2]); // R^-1
379
+
380
+ r[0] = montgomery_reduce(c[0]*NTRUPLUS_R + r[0]*NTRUPLUS_RSQ); // R^0
381
+ r[1] = montgomery_reduce(c[1]*NTRUPLUS_R + r[1]*NTRUPLUS_RSQ); // R^0
382
+ r[2] = montgomery_reduce(c[2]*NTRUPLUS_R + r[2]*NTRUPLUS_RSQ); // R^0
383
+ }
@@ -0,0 +1,16 @@
1
+ #ifndef NTT_H
2
+ #define NTT_H
3
+
4
+ #include <stdint.h>
5
+ #include "params.h"
6
+
7
+ extern const int16_t zetas[288];
8
+
9
+ void ntt(int16_t r[NTRUPLUS_N]);
10
+ void invntt(int16_t r[NTRUPLUS_N]);
11
+
12
+ int baseinv(int16_t r[3], const int16_t a[3], int16_t zeta);
13
+ void basemul(int16_t r[3], const int16_t a[3], const int16_t b[3], int16_t zeta);
14
+ void basemul_add(int16_t r[3], const int16_t a[3], const int16_t b[3], const int16_t c[3], int16_t zeta);
15
+
16
+ #endif
@@ -0,0 +1,18 @@
1
+ #ifndef PARAMS_H
2
+ #define PARAMS_H
3
+
4
+ #define NTRUPLUS_ALGNAME "NTRU+864"
5
+
6
+ #define NTRUPLUS_N 864
7
+ #define NTRUPLUS_Q 3457
8
+ #define NTRUPLUS_D 3
9
+
10
+ #define NTRUPLUS_SYMBYTES 32 /* size in bytes of hashes, and seeds */
11
+ #define NTRUPLUS_SSBYTES 32 /* size in bytes of shared key */
12
+ #define NTRUPLUS_POLYBYTES 1296
13
+
14
+ #define NTRUPLUS_PUBLICKEYBYTES NTRUPLUS_POLYBYTES
15
+ #define NTRUPLUS_SECRETKEYBYTES ((NTRUPLUS_POLYBYTES << 1) + NTRUPLUS_SYMBYTES)
16
+ #define NTRUPLUS_CIPHERTEXTBYTES NTRUPLUS_POLYBYTES
17
+
18
+ #endif