kpqc 0.1.1 → 0.2.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 (100) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +15 -0
  3. data/README.md +9 -6
  4. data/THIRD_PARTY_NOTICES.md +14 -47
  5. data/ext/kpqc/extconf_helper.rb +6 -3
  6. data/ext/kpqc/secure_clear.c +8 -0
  7. data/kpqc.gemspec +4 -2
  8. data/lib/kpqc/aimer.rb +6 -6
  9. data/lib/kpqc/version.rb +1 -1
  10. data/test/kat/aimer128f.rsp +4 -4
  11. data/test/kat/aimer128s.rsp +4 -4
  12. data/test/kat/aimer192f.rsp +4 -4
  13. data/test/kat/aimer192s.rsp +4 -4
  14. data/test/kat/aimer256f.rsp +4 -4
  15. data/test/kat/aimer256s.rsp +4 -4
  16. data/test/kat/haetae2.rsp +1 -1
  17. data/test/kat/haetae3.rsp +1 -1
  18. data/test/kat/haetae5.rsp +1 -1
  19. data/vendor/AIMer/LICENSE +1 -1
  20. data/vendor/AIMer/aim3.c +227 -0
  21. data/vendor/AIMer/aim3.h +63 -0
  22. data/vendor/AIMer/field.h +12 -4
  23. data/vendor/AIMer/field128.c +24 -381
  24. data/vendor/AIMer/field192.c +24 -400
  25. data/vendor/AIMer/field256.c +28 -447
  26. data/vendor/AIMer/field_common.c +98 -0
  27. data/vendor/AIMer/field_internal.h +218 -0
  28. data/vendor/AIMer/params/params-aimer-128f.h +8 -8
  29. data/vendor/AIMer/params/params-aimer-128s.h +8 -8
  30. data/vendor/AIMer/params/params-aimer-192f.h +8 -8
  31. data/vendor/AIMer/params/params-aimer-192s.h +8 -8
  32. data/vendor/AIMer/params/params-aimer-256f.h +8 -8
  33. data/vendor/AIMer/params/params-aimer-256s.h +8 -8
  34. data/vendor/AIMer/params.h +5 -0
  35. data/vendor/AIMer/sign.c +246 -167
  36. data/vendor/AIMer/sign.h +6 -6
  37. data/vendor/AIMer/tree.c +10 -4
  38. data/vendor/AIMer/tree.h +1 -1
  39. data/vendor/HAETAE/include/fixpoint.h +14 -14
  40. data/vendor/HAETAE/include/rans_byte.h +10 -9
  41. data/vendor/HAETAE/src/decompose.c +8 -4
  42. data/vendor/HAETAE/src/encoding.c +5 -5
  43. data/vendor/HAETAE/src/fft.c +1 -1
  44. data/vendor/HAETAE/src/fips202.c +26 -14
  45. data/vendor/HAETAE/src/fixpoint.c +21 -18
  46. data/vendor/HAETAE/src/ntt.c +2 -0
  47. data/vendor/HAETAE/src/packing.c +15 -15
  48. data/vendor/HAETAE/src/poly.c +30 -3
  49. data/vendor/HAETAE/src/polyfix.c +2 -2
  50. data/vendor/HAETAE/src/polymat.c +2 -2
  51. data/vendor/HAETAE/src/polyvec.c +7 -5
  52. data/vendor/HAETAE/src/reduce.c +2 -0
  53. data/vendor/HAETAE/src/sampler.c +231 -47
  54. data/vendor/HAETAE/src/sign.c +11 -10
  55. data/vendor/NTRUplus/NTRU+1152/api.h +27 -8
  56. data/vendor/NTRUplus/NTRU+1152/fips202/fips202.c +12 -269
  57. data/vendor/NTRUplus/NTRU+1152/fips202/fips202.h +4 -113
  58. data/vendor/NTRUplus/NTRU+1152/kem.c +110 -40
  59. data/vendor/NTRUplus/NTRU+1152/ntt.c +50 -54
  60. data/vendor/NTRUplus/NTRU+1152/poly.c +21 -13
  61. data/vendor/NTRUplus/NTRU+1152/poly.h +3 -3
  62. data/vendor/NTRUplus/NTRU+1152/randombytes.h +1 -0
  63. data/vendor/NTRUplus/NTRU+1152/symmetric.c +3 -9
  64. data/vendor/NTRUplus/NTRU+1152/symmetric.h +13 -3
  65. data/vendor/NTRUplus/NTRU+1152/util.h +15 -0
  66. data/vendor/NTRUplus/NTRU+768/api.h +27 -8
  67. data/vendor/NTRUplus/NTRU+768/fips202/fips202.c +12 -269
  68. data/vendor/NTRUplus/NTRU+768/fips202/fips202.h +4 -113
  69. data/vendor/NTRUplus/NTRU+768/kem.c +110 -40
  70. data/vendor/NTRUplus/NTRU+768/ntt.c +50 -54
  71. data/vendor/NTRUplus/NTRU+768/poly.c +23 -13
  72. data/vendor/NTRUplus/NTRU+768/poly.h +3 -3
  73. data/vendor/NTRUplus/NTRU+768/randombytes.h +1 -0
  74. data/vendor/NTRUplus/NTRU+768/symmetric.c +3 -9
  75. data/vendor/NTRUplus/NTRU+768/symmetric.h +13 -3
  76. data/vendor/NTRUplus/NTRU+768/util.h +15 -0
  77. data/vendor/NTRUplus/NTRU+864/api.h +27 -8
  78. data/vendor/NTRUplus/NTRU+864/fips202/fips202.c +12 -269
  79. data/vendor/NTRUplus/NTRU+864/fips202/fips202.h +4 -113
  80. data/vendor/NTRUplus/NTRU+864/kem.c +110 -40
  81. data/vendor/NTRUplus/NTRU+864/ntt.c +24 -28
  82. data/vendor/NTRUplus/NTRU+864/poly.c +26 -12
  83. data/vendor/NTRUplus/NTRU+864/poly.h +3 -3
  84. data/vendor/NTRUplus/NTRU+864/randombytes.h +1 -0
  85. data/vendor/NTRUplus/NTRU+864/symmetric.c +3 -9
  86. data/vendor/NTRUplus/NTRU+864/symmetric.h +13 -3
  87. data/vendor/NTRUplus/NTRU+864/util.h +15 -0
  88. data/vendor/SMAUG-T/src/cbd.c +4 -4
  89. data/vendor/SMAUG-T/src/ciphertext.c +2 -2
  90. data/vendor/SMAUG-T/src/dg.c +2 -2
  91. data/vendor/SMAUG-T/src/hwt.c +4 -4
  92. data/vendor/SMAUG-T/src/indcpa.c +5 -5
  93. data/vendor/SMAUG-T/src/kem.c +9 -7
  94. data/vendor/SMAUG-T/src/key.c +2 -2
  95. data/vendor/SMAUG-T/src/pack.c +8 -8
  96. metadata +16 -13
  97. data/vendor/AIMer/aim2.c +0 -163
  98. data/vendor/AIMer/aim2.h +0 -36
  99. data/vendor/AIMer/aim2_constant.h +0 -74
  100. data/vendor/SOURCES.json +0 -130
@@ -0,0 +1,227 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ #include "aim3.h"
4
+ #include "field.h"
5
+ #include "hash.h"
6
+ #include "params.h"
7
+ #include <stddef.h>
8
+ #include <stdint.h>
9
+ #include <stdlib.h>
10
+
11
+ static void squeeze_invertible_matrix(hash_instance *ctx,
12
+ gf mat[AIM3_NUM_BITS_FIELD])
13
+ {
14
+ uint8_t buf[AIM3_NUM_BYTES_FIELD];
15
+ uint64_t ormask, lmask, umask;
16
+
17
+ gf matrix_L[AIM3_NUM_BITS_FIELD];
18
+ gf matrix_U[AIM3_NUM_BITS_FIELD];
19
+ gf temp = {0,};
20
+
21
+ for (size_t row = 0; row < AIM3_NUM_BITS_FIELD; row++)
22
+ {
23
+ hash_squeeze(ctx, buf, AIM3_NUM_BYTES_FIELD);
24
+ gf_from_bytes(temp, buf);
25
+
26
+ ormask = ((uint64_t)1) << (row % 64);
27
+ lmask = ((uint64_t)-1) << (row % 64);
28
+ umask = ~lmask;
29
+
30
+ size_t inter = row / 64;
31
+ size_t col_word;
32
+ for (col_word = 0; col_word < inter; col_word++)
33
+ {
34
+ // L is zero, U is full
35
+ matrix_L[row][col_word] = 0;
36
+ matrix_U[row][col_word] = temp[col_word];
37
+ }
38
+ matrix_L[row][inter] = (temp[inter] & lmask) | ormask;
39
+ matrix_U[row][inter] = (temp[inter] & umask) | ormask;
40
+ for (col_word = inter + 1; col_word < AIM3_NUM_WORDS_FIELD; col_word++)
41
+ {
42
+ // L is full, U is zero
43
+ matrix_L[row][col_word] = temp[col_word];
44
+ matrix_U[row][col_word] = 0;
45
+ }
46
+ }
47
+
48
+ for (size_t i = 0; i < AIM3_NUM_BITS_FIELD; i++)
49
+ {
50
+ gf_mat_vec_mul(mat[i], matrix_U[i], (const gf *)matrix_L);
51
+ }
52
+ }
53
+
54
+ void aim3_generate_linear(aim_lin_t *lin, const uint8_t iv[AIM3_IV_SIZE])
55
+ {
56
+ hash_instance ctx;
57
+
58
+ hash_init(&ctx);
59
+ hash_update(&ctx, iv, AIM3_IV_SIZE);
60
+ hash_final(&ctx);
61
+
62
+ for (size_t ell = 0; ell < 2 * AIM3_NUM_INPUT_SBOX; ell++)
63
+ {
64
+ squeeze_invertible_matrix(&ctx, lin->mat_A[ell]);
65
+ }
66
+
67
+ for (size_t ell = 0; ell < AIM3_NUM_INPUT_SBOX + 1; ell++)
68
+ {
69
+ hash_squeeze(&ctx, (uint8_t *)lin->vec_b[ell], AIM3_NUM_BYTES_FIELD);
70
+ }
71
+ hash_ctx_release(&ctx);
72
+ }
73
+
74
+ int aim3_nozeroinpsb(uint8_t ct[AIM3_NUM_BYTES_FIELD],
75
+ const uint8_t pt[AIM3_NUM_BYTES_FIELD],
76
+ const uint8_t iv[AIM3_IV_SIZE])
77
+ {
78
+ aim_lin_t *lin = (aim_lin_t *)malloc(sizeof(aim_lin_t));
79
+ if (lin == NULL)
80
+ {
81
+ // allocation failure
82
+ return -2;
83
+ }
84
+
85
+ gf state[AIM3_NUM_INPUT_SBOX];
86
+ gf pt_gf = {0,}, ct_gf = {0,}, tmp = {0,};
87
+ gf_from_bytes(pt_gf, pt);
88
+
89
+ // generate random matrix
90
+ aim3_generate_linear(lin, iv);
91
+
92
+ for (size_t i = 0; i < AIM3_NUM_INPUT_SBOX; i++)
93
+ {
94
+ // linear component: affine layer
95
+ gf_mat_vec_mul(state[i], pt_gf, lin->mat_A[i]);
96
+ gf_add(state[i], state[i], lin->vec_b[i]);
97
+
98
+ // non-linear component: y = x^{2^e-1} + x^{-1} = x^{-1} * (x^2^e + 1)
99
+ if (gf_is0(state[i]))
100
+ {
101
+ free(lin);
102
+ return -1;
103
+ }
104
+ gf_inv(tmp, state[i]);
105
+ for (size_t e = 0; e < aim3_exponents[i]; e++)
106
+ {
107
+ gf_sqr(state[i], state[i]);
108
+ }
109
+ state[i][0] ^= 1;
110
+ gf_mul(state[i], state[i], tmp);
111
+
112
+ // linear component: affine layer
113
+ gf_mat_vec_mul(state[i], state[i],
114
+ (const gf *)lin->mat_A[i + AIM3_NUM_INPUT_SBOX]);
115
+ }
116
+
117
+ for (size_t i = 1; i < AIM3_NUM_INPUT_SBOX; i++)
118
+ {
119
+ gf_add(state[0], state[0], state[i]);
120
+ }
121
+ gf_add(state[0], state[0], lin->vec_b[AIM3_NUM_INPUT_SBOX]);
122
+
123
+ // non-linear component: y = x^{2^e-1} + x^{-1} = x^{-1} * (x^2^e + 1)
124
+ if (gf_is0(state[0]))
125
+ {
126
+ free(lin);
127
+ return -1;
128
+ }
129
+ gf_inv(tmp, state[0]);
130
+ for (size_t e = 0; e < aim3_exponents[AIM3_NUM_INPUT_SBOX]; e++)
131
+ {
132
+ gf_sqr(state[0], state[0]);
133
+ }
134
+ state[0][0] ^= 1;
135
+ gf_mul(state[0], state[0], tmp);
136
+
137
+ // linear component: feed-forward
138
+ gf_add(ct_gf, pt_gf, state[0]);
139
+
140
+ gf_to_bytes(ct, ct_gf);
141
+
142
+ free(lin);
143
+ return 0;
144
+ }
145
+
146
+ void aim3_sbox_outputs(gf sbox_outputs[AIM3_NUM_INPUT_SBOX+1],
147
+ const aim_lin_t* lin,
148
+ const gf pt, const gf ct)
149
+ {
150
+ gf tmp = {0,};
151
+ for (size_t i = 0; i < AIM3_NUM_INPUT_SBOX; i ++)
152
+ {
153
+ // linear component: affine layer
154
+ gf_mat_vec_mul(sbox_outputs[i], pt, lin->mat_A[i]);
155
+ gf_add(sbox_outputs[i], sbox_outputs[i], lin->vec_b[i]);
156
+
157
+ // non-linear component: y = x^{2^e-1} + x^{-1} = x^{-1} * (x^2^e + 1)
158
+ gf_inv(tmp, sbox_outputs[i]);
159
+ for (size_t e = 0; e < aim3_exponents[i]; e++)
160
+ {
161
+ gf_sqr(sbox_outputs[i], sbox_outputs[i]);
162
+ }
163
+ sbox_outputs[i][0] ^= 1;
164
+ gf_mul(sbox_outputs[i], sbox_outputs[i], tmp);
165
+ }
166
+ gf_add(sbox_outputs[AIMER_L], pt, ct);
167
+ }
168
+
169
+ void aim3_mpc(mult_chk_t *mult_chk, const aim_lin_t *lin,
170
+ const tape_t *tape, const gf ct_gf,
171
+ size_t party)
172
+ {
173
+ for (size_t i = 0; i < AIMER_L + 1; i++)
174
+ {
175
+ gf_copy(mult_chk->a_shares[i], tape->a_shares[i]);
176
+ }
177
+
178
+ // b_shares[i] = share of sbox outputs
179
+ for (size_t i = 0; i < AIMER_L; i++)
180
+ {
181
+ gf_copy(mult_chk->b_shares[i], tape->y_shares[i]);
182
+ }
183
+ gf_copy(mult_chk->b_shares[AIMER_L], tape->pt_share);
184
+ if (party == AIMER_N - 1)
185
+ {
186
+ gf_add(mult_chk->b_shares[AIMER_L], mult_chk->b_shares[AIMER_L], ct_gf);
187
+ }
188
+
189
+ gf_copy(mult_chk->c_share, tape->c_share);
190
+
191
+ // Set x_shares (= share of sbox inputs)
192
+ for (size_t i = 0; i < AIMER_L; i++)
193
+ {
194
+ gf_mat_vec_mul(mult_chk->x_shares[i], tape->pt_share, lin->mat_A[i]);
195
+ if (party == AIMER_N - 1)
196
+ {
197
+ gf_add(mult_chk->x_shares[i], mult_chk->x_shares[i], lin->vec_b[i]);
198
+ }
199
+ }
200
+
201
+ gf_set0(mult_chk->x_shares[AIMER_L]);
202
+ for (size_t ell = 0; ell < AIMER_L; ell++)
203
+ {
204
+ gf_mat_vec_mul_add(mult_chk->x_shares[AIMER_L], tape->y_shares[ell],
205
+ lin->mat_A[ell+AIMER_L]);
206
+ }
207
+ if (party == AIMER_N - 1)
208
+ {
209
+ gf_add(mult_chk->x_shares[AIMER_L], mult_chk->x_shares[AIMER_L],
210
+ lin->vec_b[AIMER_L]);
211
+ }
212
+
213
+ // Set z_shares: z[i] = x[i]^{2^e} + 1
214
+ // y = x^{-1} + x^{2^e-1} -> xy = x^{2^e} + 1
215
+ for (size_t ell = 0; ell < AIMER_L + 1; ell++)
216
+ {
217
+ gf_sqr(mult_chk->z_shares[ell], mult_chk->x_shares[ell]);
218
+ for (size_t i = 1; i < aim3_exponents[ell]; i++)
219
+ {
220
+ gf_sqr(mult_chk->z_shares[ell], mult_chk->z_shares[ell]);
221
+ }
222
+ if (party == AIMER_N - 1)
223
+ {
224
+ mult_chk->z_shares[ell][0] ^= 1;
225
+ }
226
+ }
227
+ }
@@ -0,0 +1,63 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ #ifndef AIM3_H
4
+ #define AIM3_H
5
+
6
+ #include "field.h"
7
+ #include "params.h"
8
+ #include "sign.h"
9
+ #include <stdint.h>
10
+
11
+ typedef struct mult_chk_t
12
+ {
13
+ gf x_shares[AIMER_L + 1];
14
+ gf z_shares[AIMER_L + 1];
15
+ gf a_shares[AIMER_L + 1];
16
+ gf b_shares[AIMER_L + 1]; // b_shares[i] = y_shares[i]
17
+ gf c_share;
18
+ } mult_chk_t;
19
+
20
+ typedef struct aim_lin_t
21
+ {
22
+ gf mat_A[2 * AIM3_NUM_INPUT_SBOX][AIM3_NUM_BITS_FIELD];
23
+ gf vec_b[AIM3_NUM_INPUT_SBOX + 1];
24
+ } aim_lin_t;
25
+
26
+ #if SECURITY_BITS == 128
27
+ static const size_t aim3_exponents[AIM3_NUM_INPUT_SBOX + 1] =
28
+ {
29
+ 3, 7, 11
30
+ };
31
+
32
+ #elif SECURITY_BITS == 192
33
+ static const size_t aim3_exponents[AIM3_NUM_INPUT_SBOX + 1] =
34
+ {
35
+ 11, 23, 47
36
+ };
37
+
38
+ #else
39
+ static const size_t aim3_exponents[AIM3_NUM_INPUT_SBOX + 1] =
40
+ {
41
+ 3, 7, 11, 19
42
+ };
43
+ #endif
44
+
45
+ #define aim3_generate_linear AIMER_NAMESPACE(aim3_generate_linear)
46
+ void aim3_generate_linear(aim_lin_t *lin, const uint8_t iv[AIM3_IV_SIZE]);
47
+
48
+ #define aim3_sbox_outputs AIMER_NAMESPACE(aim3_sbox_outputs)
49
+ void aim3_sbox_outputs(gf sbox_outputs[AIM3_NUM_INPUT_SBOX+1],
50
+ const aim_lin_t *lin,
51
+ const gf pt, const gf ct);
52
+
53
+ #define aim3_nozeroinpsb AIMER_NAMESPACE(aim3_nozeroinpsb)
54
+ int aim3_nozeroinpsb(uint8_t ct[AIM3_NUM_BYTES_FIELD],
55
+ const uint8_t pt[AIM3_NUM_BYTES_FIELD],
56
+ const uint8_t iv[AIM3_IV_SIZE]);
57
+
58
+ #define aim3_mpc AIMER_NAMESPACE(aim3_mpc)
59
+ void aim3_mpc(mult_chk_t *mult_chk, const aim_lin_t *lin,
60
+ const tape_t *tape, const gf ct_gf,
61
+ size_t party);
62
+
63
+ #endif // AIM3_H
data/vendor/AIMer/field.h CHANGED
@@ -4,9 +4,15 @@
4
4
  #define FIELD_H
5
5
 
6
6
  #include "params.h"
7
+ #include <stdbool.h>
7
8
  #include <stddef.h>
8
9
  #include <stdint.h>
9
10
 
11
+ #if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
12
+ __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
13
+ #error "AIMer reference implementation assumes a little-endian target"
14
+ #endif
15
+
10
16
  #if SECURITY_BITS == 128
11
17
  typedef uint64_t gf[2];
12
18
  #elif SECURITY_BITS == 192
@@ -17,6 +23,8 @@ typedef uint64_t gf[4];
17
23
 
18
24
  #define gf_set0 AIMER_NAMESPACE(gf_set0)
19
25
  void gf_set0(gf a);
26
+ #define gf_is0 AIMER_NAMESPACE(gf_is0)
27
+ bool gf_is0(const gf a);
20
28
  #define gf_copy AIMER_NAMESPACE(gf_copy)
21
29
  void gf_copy(gf out, const gf in);
22
30
  #define gf_to_bytes AIMER_NAMESPACE(gf_to_bytes)
@@ -32,12 +40,12 @@ void gf_mul(gf c, const gf a, const gf b);
32
40
  void gf_mul_add(gf c, const gf a, const gf b);
33
41
  #define gf_sqr AIMER_NAMESPACE(gf_sqr)
34
42
  void gf_sqr(gf c, const gf a);
35
- #define gf_exp AIMER_NAMESPACE(gf_exp)
36
- void gf_exp(gf out, const gf in, const uint64_t *exp);
43
+ #define gf_inv AIMER_NAMESPACE(gf_inv)
44
+ void gf_inv(gf c, const gf a);
37
45
 
38
46
  #define gf_mat_vec_mul AIMER_NAMESPACE(gf_mat_vec_mul)
39
- void gf_mat_vec_mul(gf c, const gf a, const gf b[AIM2_NUM_BITS_FIELD]);
47
+ void gf_mat_vec_mul(gf c, const gf a, const gf b[AIM3_NUM_BITS_FIELD]);
40
48
  #define gf_mat_vec_mul_add AIMER_NAMESPACE(gf_mat_vec_mul_add)
41
- void gf_mat_vec_mul_add(gf c, const gf a, const gf b[AIM2_NUM_BITS_FIELD]);
49
+ void gf_mat_vec_mul_add(gf c, const gf a, const gf b[AIM3_NUM_BITS_FIELD]);
42
50
 
43
51
  #endif // FIELD_H