@pinkparrot/qsafe-mayo-wasm 0.0.3

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 (97) hide show
  1. package/.gitmodules +3 -0
  2. package/.vscode/launch.json +12 -0
  3. package/LICENSE +201 -0
  4. package/bridge/mayo1_bridge.c +26 -0
  5. package/bridge/mayo2_bridge.c +26 -0
  6. package/bridge/randombytes_inject.c +44 -0
  7. package/build_mayo1.ps1 +36 -0
  8. package/build_mayo2.ps1 +36 -0
  9. package/dist/mayo.browser.min.js +216 -0
  10. package/dist/mayo1.js +0 -0
  11. package/dist/mayo2.js +0 -0
  12. package/dist/mayo_api.js +139 -0
  13. package/dist/package.json +1 -0
  14. package/gitignore +2 -0
  15. package/index.mjs +1 -0
  16. package/mayo-c/.astylerc +16 -0
  17. package/mayo-c/.cmake/flags.cmake +45 -0
  18. package/mayo-c/.cmake/sanitizers.cmake +81 -0
  19. package/mayo-c/.cmake/target.cmake +71 -0
  20. package/mayo-c/.github/workflows/ci_clang.yml +61 -0
  21. package/mayo-c/.github/workflows/ci_gcc.yml +60 -0
  22. package/mayo-c/.github/workflows/cmake.yml +160 -0
  23. package/mayo-c/.github/workflows/macos_m1.yml +68 -0
  24. package/mayo-c/CMakeLists.txt +35 -0
  25. package/mayo-c/KAT/PQCsignKAT_24_MAYO_1.req +900 -0
  26. package/mayo-c/KAT/PQCsignKAT_24_MAYO_1.rsp +902 -0
  27. package/mayo-c/KAT/PQCsignKAT_24_MAYO_2.req +900 -0
  28. package/mayo-c/KAT/PQCsignKAT_24_MAYO_2.rsp +902 -0
  29. package/mayo-c/KAT/PQCsignKAT_32_MAYO_3.req +900 -0
  30. package/mayo-c/KAT/PQCsignKAT_32_MAYO_3.rsp +902 -0
  31. package/mayo-c/KAT/PQCsignKAT_40_MAYO_5.req +900 -0
  32. package/mayo-c/KAT/PQCsignKAT_40_MAYO_5.rsp +902 -0
  33. package/mayo-c/LICENSE +202 -0
  34. package/mayo-c/META/MAYO-1_META.yml +52 -0
  35. package/mayo-c/META/MAYO-2_META.yml +52 -0
  36. package/mayo-c/META/MAYO-3_META.yml +52 -0
  37. package/mayo-c/META/MAYO-5_META.yml +52 -0
  38. package/mayo-c/NOTICE +13 -0
  39. package/mayo-c/README.md +183 -0
  40. package/mayo-c/apps/CMakeLists.txt +31 -0
  41. package/mayo-c/apps/PQCgenKAT_sign.c +281 -0
  42. package/mayo-c/apps/example.c +151 -0
  43. package/mayo-c/apps/example_nistapi.c +124 -0
  44. package/mayo-c/include/mayo.h +442 -0
  45. package/mayo-c/include/mem.h +25 -0
  46. package/mayo-c/include/randombytes.h +31 -0
  47. package/mayo-c/scripts/contstants.py +141 -0
  48. package/mayo-c/scripts/find_irred_poly.sage +39 -0
  49. package/mayo-c/src/AVX2/arithmetic_common.h +159 -0
  50. package/mayo-c/src/AVX2/echelon_form.h +91 -0
  51. package/mayo-c/src/AVX2/echelon_form_loop.h +58 -0
  52. package/mayo-c/src/AVX2/shuffle_arithmetic.h +442 -0
  53. package/mayo-c/src/CMakeLists.txt +98 -0
  54. package/mayo-c/src/arithmetic.c +128 -0
  55. package/mayo-c/src/arithmetic.h +124 -0
  56. package/mayo-c/src/common/aes128ctr.c +293 -0
  57. package/mayo-c/src/common/aes_c.c +741 -0
  58. package/mayo-c/src/common/aes_ctr.h +32 -0
  59. package/mayo-c/src/common/aes_neon.c +201 -0
  60. package/mayo-c/src/common/debug_bench_tools.h +69 -0
  61. package/mayo-c/src/common/fips202.c +1093 -0
  62. package/mayo-c/src/common/fips202.h +12 -0
  63. package/mayo-c/src/common/mem.c +19 -0
  64. package/mayo-c/src/common/randombytes_ctrdrbg.c +141 -0
  65. package/mayo-c/src/common/randombytes_system.c +399 -0
  66. package/mayo-c/src/generic/arithmetic_dynamic.h +68 -0
  67. package/mayo-c/src/generic/arithmetic_fixed.h +84 -0
  68. package/mayo-c/src/generic/echelon_form.h +152 -0
  69. package/mayo-c/src/generic/ef_inner_loop.h +56 -0
  70. package/mayo-c/src/generic/generic_arithmetic.h +294 -0
  71. package/mayo-c/src/mayo.c +675 -0
  72. package/mayo-c/src/mayo_1/api.c +46 -0
  73. package/mayo-c/src/mayo_1/api.h +43 -0
  74. package/mayo-c/src/mayo_2/api.c +46 -0
  75. package/mayo-c/src/mayo_2/api.h +43 -0
  76. package/mayo-c/src/mayo_3/api.c +46 -0
  77. package/mayo-c/src/mayo_3/api.h +43 -0
  78. package/mayo-c/src/mayo_5/api.c +46 -0
  79. package/mayo-c/src/mayo_5/api.h +43 -0
  80. package/mayo-c/src/neon/arithmetic_common.h +132 -0
  81. package/mayo-c/src/neon/echelon_form.h +55 -0
  82. package/mayo-c/src/neon/echelon_form_loop.h +58 -0
  83. package/mayo-c/src/neon/shuffle_arithmetic.h +462 -0
  84. package/mayo-c/src/params.c +42 -0
  85. package/mayo-c/src/simple_arithmetic.h +138 -0
  86. package/mayo-c/test/CMakeLists.txt +51 -0
  87. package/mayo-c/test/bench.c +166 -0
  88. package/mayo-c/test/m1cycles.c +155 -0
  89. package/mayo-c/test/m1cycles.h +13 -0
  90. package/mayo-c/test/test_kat.c +271 -0
  91. package/mayo-c/test/test_mayo.c +139 -0
  92. package/mayo-c/test/test_sample_solution.c +75 -0
  93. package/mayo-c/test/test_various.c +680 -0
  94. package/package.json +39 -0
  95. package/publish.bat +22 -0
  96. package/readme.md +80 -0
  97. package/test/test.mjs +42 -0
@@ -0,0 +1,442 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+
3
+ #ifndef MAYO_H
4
+ #define MAYO_H
5
+
6
+ #include <stdint.h>
7
+ #include <stdlib.h>
8
+
9
+ #define F_TAIL_LEN 4
10
+ #define F_TAIL_64 \
11
+ { 8, 0, 2, 8 } // f(z) = z^64 + x^3*z^3 + x*z^2 + x^3
12
+ #define F_TAIL_78 \
13
+ { 8, 1, 1, 0 } // f(z) = z^78 + z^2 + z + x^3
14
+ #define F_TAIL_108 \
15
+ { 8, 0, 1, 7 } // f(z) = z^108 + (x^2 + x + 1)*z^3 + z^2 + x^3
16
+ #define F_TAIL_142 \
17
+ { 4, 0, 8, 1 } // f(z) = z^142 + z^3 + x^3*z^2 + x^2
18
+
19
+ #define MAYO_1_name "MAYO_1"
20
+ #define MAYO_1_n 86
21
+ #define MAYO_1_m 78
22
+ #define MAYO_1_m_vec_limbs 5
23
+ #define MAYO_1_o 8
24
+ #define MAYO_1_v (MAYO_1_n - MAYO_1_o)
25
+ #define MAYO_1_A_cols (MAYO_1_k * MAYO_1_o + 1)
26
+ #define MAYO_1_k 10
27
+ #define MAYO_1_q 16
28
+ #define MAYO_1_m_bytes 39
29
+ #define MAYO_1_O_bytes 312
30
+ #define MAYO_1_v_bytes 39
31
+ #define MAYO_1_r_bytes 40
32
+ #define MAYO_1_P1_bytes 120159
33
+ #define MAYO_1_P2_bytes 24336
34
+ #define MAYO_1_P3_bytes 1404
35
+ #define MAYO_1_csk_bytes 24
36
+ #define MAYO_1_cpk_bytes 1420
37
+ #define MAYO_1_sig_bytes 454
38
+ #define MAYO_1_f_tail F_TAIL_78
39
+ #define MAYO_1_f_tail_arr f_tail_78
40
+ #define MAYO_1_salt_bytes 24
41
+ #define MAYO_1_digest_bytes 32
42
+ #define MAYO_1_pk_seed_bytes 16
43
+ #define MAYO_1_sk_seed_bytes 24
44
+
45
+ #define MAYO_2_name "MAYO_2"
46
+ #define MAYO_2_n 81
47
+ #define MAYO_2_m 64
48
+ #define MAYO_2_m_vec_limbs 4
49
+ #define MAYO_2_o 17
50
+ #define MAYO_2_v (MAYO_2_n - MAYO_2_o)
51
+ #define MAYO_2_A_cols (MAYO_2_k * MAYO_2_o + 1)
52
+ #define MAYO_2_k 4
53
+ #define MAYO_2_q 16
54
+ #define MAYO_2_m_bytes 32
55
+ #define MAYO_2_O_bytes 544
56
+ #define MAYO_2_v_bytes 32
57
+ #define MAYO_2_r_bytes 34
58
+ #define MAYO_2_P1_bytes 66560
59
+ #define MAYO_2_P2_bytes 34816
60
+ #define MAYO_2_P3_bytes 4896
61
+ #define MAYO_2_csk_bytes 24
62
+ #define MAYO_2_cpk_bytes 4912
63
+ #define MAYO_2_sig_bytes 186
64
+ #define MAYO_2_f_tail F_TAIL_64
65
+ #define MAYO_2_f_tail_arr f_tail_64
66
+ #define MAYO_2_salt_bytes 24
67
+ #define MAYO_2_digest_bytes 32
68
+ #define MAYO_2_pk_seed_bytes 16
69
+ #define MAYO_2_sk_seed_bytes 24
70
+
71
+ #define MAYO_3_name "MAYO_3"
72
+ #define MAYO_3_n 118
73
+ #define MAYO_3_m 108
74
+ #define MAYO_3_m_vec_limbs 7
75
+ #define MAYO_3_o 10
76
+ #define MAYO_3_v (MAYO_3_n - MAYO_3_o)
77
+ #define MAYO_3_A_cols (MAYO_3_k * MAYO_3_o + 1)
78
+ #define MAYO_3_k 11
79
+ #define MAYO_3_q 16
80
+ #define MAYO_3_m_bytes 54
81
+ #define MAYO_3_O_bytes 540
82
+ #define MAYO_3_v_bytes 54
83
+ #define MAYO_3_r_bytes 55
84
+ #define MAYO_3_P1_bytes 317844
85
+ #define MAYO_3_P2_bytes 58320
86
+ #define MAYO_3_P3_bytes 2970
87
+ #define MAYO_3_csk_bytes 32
88
+ #define MAYO_3_cpk_bytes 2986
89
+ #define MAYO_3_sig_bytes 681
90
+ #define MAYO_3_f_tail F_TAIL_108
91
+ #define MAYO_3_f_tail_arr f_tail_108
92
+ #define MAYO_3_salt_bytes 32
93
+ #define MAYO_3_digest_bytes 48
94
+ #define MAYO_3_pk_seed_bytes 16
95
+ #define MAYO_3_sk_seed_bytes 32
96
+
97
+ #define MAYO_5_name "MAYO_5"
98
+ #define MAYO_5_n 154
99
+ #define MAYO_5_m 142
100
+ #define MAYO_5_m_vec_limbs 9
101
+ #define MAYO_5_o 12
102
+ #define MAYO_5_v (MAYO_5_n - MAYO_5_o)
103
+ #define MAYO_5_A_cols (MAYO_5_k * MAYO_5_o + 1)
104
+ #define MAYO_5_k 12
105
+ #define MAYO_5_q 16
106
+ #define MAYO_5_m_bytes 71
107
+ #define MAYO_5_O_bytes 852
108
+ #define MAYO_5_v_bytes 71
109
+ #define MAYO_5_r_bytes 72
110
+ #define MAYO_5_P1_bytes 720863
111
+ #define MAYO_5_P2_bytes 120984
112
+ #define MAYO_5_P3_bytes 5538
113
+ #define MAYO_5_csk_bytes 40
114
+ #define MAYO_5_cpk_bytes 5554
115
+ #define MAYO_5_sig_bytes 964
116
+ #define MAYO_5_f_tail F_TAIL_142
117
+ #define MAYO_5_f_tail_arr f_tail_142
118
+ #define MAYO_5_salt_bytes 40
119
+ #define MAYO_5_digest_bytes 64
120
+ #define MAYO_5_pk_seed_bytes 16
121
+ #define MAYO_5_sk_seed_bytes 40
122
+
123
+ #define PARAM_JOIN2_(a, b) a##_##b
124
+ #define PARAM_JOIN2(a, b) PARAM_JOIN2_(a, b)
125
+ #define PARAM_NAME(end) PARAM_JOIN2(MAYO_VARIANT, end)
126
+
127
+ #if defined(MAYO_VARIANT)
128
+ #define PARAM_JOIN3_(a, b, c) pqmayo_##a##_##b##_##c
129
+ #define PARAM_JOIN3(a, b, c) PARAM_JOIN3_(a, b, c)
130
+ #define PARAM_NAME3(end, s) PARAM_JOIN3(MAYO_VARIANT, end, s)
131
+
132
+ #if defined(MAYO_BUILD_TYPE_REF)
133
+ #define MAYO_NAMESPACE(s) PARAM_NAME3(ref, s)
134
+ #elif defined(MAYO_BUILD_TYPE_OPT)
135
+ #define MAYO_NAMESPACE(s) PARAM_NAME3(opt, s)
136
+ #elif defined(MAYO_BUILD_TYPE_AVX2)
137
+ #define MAYO_NAMESPACE(s) PARAM_NAME3(avx2, s)
138
+ #elif defined(MAYO_BUILD_TYPE_NEON)
139
+ #define MAYO_NAMESPACE(s) PARAM_NAME3(neon, s)
140
+ #else
141
+ #error "Build type not known"
142
+ #endif
143
+
144
+ #else
145
+ #define MAYO_NAMESPACE(s) s
146
+ #endif
147
+
148
+ #ifdef ENABLE_PARAMS_DYNAMIC
149
+ #define NAME_MAX mayo5
150
+ #define N_MAX 154
151
+ #define M_MAX 142
152
+ #define O_MAX 17
153
+ #define K_MAX 12
154
+ #define Q_MAX 16
155
+ #define PK_SEED_BYTES_MAX 16
156
+ #define SK_SEED_BYTES_MAX 40
157
+ #define SALT_BYTES_MAX 40
158
+ #define DIGEST_BYTES_MAX 64
159
+ #define V_MAX 142
160
+ #define O_BYTES_MAX 852
161
+ #define V_BYTES_MAX 71
162
+ #define R_BYTES_MAX 72
163
+ #define P1_BYTES_MAX 720863
164
+ #define P2_BYTES_MAX 120984
165
+ #define P3_BYTES_MAX 5538
166
+ #define SIG_BYTES_MAX 964
167
+ #define CPK_BYTES_MAX 5554
168
+ #define CSK_BYTES_MAX 40
169
+ #define M_BYTES_MAX 71
170
+ #define M_VEC_LIMBS_MAX 9
171
+ #elif defined(MAYO_VARIANT)
172
+ #define M_MAX PARAM_NAME(m)
173
+ #define M_VEC_LIMBS_MAX PARAM_NAME(m_vec_limbs)
174
+ #define N_MAX PARAM_NAME(n)
175
+ #define O_MAX PARAM_NAME(o)
176
+ #define V_MAX PARAM_NAME(v)
177
+ #define K_MAX PARAM_NAME(k)
178
+ #define Q_MAX PARAM_NAME(q)
179
+ #define M_BYTES_MAX PARAM_NAME(m_bytes)
180
+ #define O_BYTES_MAX PARAM_NAME(O_bytes)
181
+ #define V_BYTES_MAX PARAM_NAME(v_bytes)
182
+ #define R_BYTES_MAX PARAM_NAME(r_bytes)
183
+ #define P1_BYTES_MAX PARAM_NAME(P1_bytes)
184
+ #define P2_BYTES_MAX PARAM_NAME(P2_bytes)
185
+ #define P3_BYTES_MAX PARAM_NAME(P3_bytes)
186
+ #define SIG_BYTES_MAX PARAM_NAME(sig_bytes)
187
+ #define CSK_BYTES_MAX PARAM_NAME(csk_bytes)
188
+ #define CPK_BYTES_MAX PARAM_NAME(cpk_bytes)
189
+ #define SALT_BYTES_MAX PARAM_NAME(salt_bytes)
190
+ #define DIGEST_BYTES_MAX PARAM_NAME(digest_bytes)
191
+ #define PK_SEED_BYTES_MAX PARAM_NAME(pk_seed_bytes)
192
+ #define SK_SEED_BYTES_MAX SALT_BYTES_MAX
193
+ #else
194
+ #error "Parameter not specified"
195
+ #endif
196
+
197
+ #define P1_LIMBS_MAX (V_MAX*(V_MAX+1)/2*M_VEC_LIMBS_MAX)
198
+ #define P2_LIMBS_MAX (V_MAX*O_MAX*M_VEC_LIMBS_MAX)
199
+ #define P3_LIMBS_MAX (O_MAX*(O_MAX+1)/2*M_VEC_LIMBS_MAX)
200
+
201
+ #ifdef ENABLE_PARAMS_DYNAMIC
202
+ #define PARAM_name(p) (p->name)
203
+ #define PARAM_m(p) (p->m)
204
+ #define PARAM_m_vec_limbs(p) (p->m_vec_limbs)
205
+ #define PARAM_n(p) (p->n)
206
+ #define PARAM_o(p) (p->o)
207
+ #define PARAM_v(p) (p->n - p->o)
208
+ #define PARAM_A_cols(p) (p->k * p->o + 1)
209
+ #define PARAM_k(p) (p->k)
210
+ #define PARAM_q(p) (p->q)
211
+ #define PARAM_m_bytes(p) (p->m_bytes)
212
+ #define PARAM_O_bytes(p) (p->O_bytes)
213
+ #define PARAM_v_bytes(p) (p->v_bytes)
214
+ #define PARAM_r_bytes(p) (p->r_bytes)
215
+ #define PARAM_P1_bytes(p) (p->P1_bytes)
216
+ #define PARAM_P2_bytes(p) (p->P2_bytes)
217
+ #define PARAM_P3_bytes(p) (p->P3_bytes)
218
+ #define PARAM_csk_bytes(p) (p->csk_bytes)
219
+ #define PARAM_cpk_bytes(p) (p->cpk_bytes)
220
+ #define PARAM_sig_bytes(p) (p->sig_bytes)
221
+ #define PARAM_f_tail(p) (p->f_tail)
222
+ #define PARAM_salt_bytes(p) (p->salt_bytes)
223
+ #define PARAM_sk_seed_bytes(p) (p->sk_seed_bytes)
224
+ #define PARAM_digest_bytes(p) (p->digest_bytes)
225
+ #define PARAM_pk_seed_bytes(p) (p->pk_seed_bytes)
226
+ #elif defined(MAYO_VARIANT)
227
+ #define PARAM_name(p) PARAM_NAME(name)
228
+ #define PARAM_m(p) PARAM_NAME(m)
229
+ #define PARAM_m_vec_limbs(p) PARAM_NAME(m_vec_limbs)
230
+ #define PARAM_n(p) PARAM_NAME(n)
231
+ #define PARAM_o(p) PARAM_NAME(o)
232
+ #define PARAM_v(p) PARAM_NAME(v)
233
+ #define PARAM_A_cols(p) PARAM_NAME(A_cols)
234
+ #define PARAM_k(p) PARAM_NAME(k)
235
+ #define PARAM_q(p) PARAM_NAME(q)
236
+ #define PARAM_m_bytes(p) PARAM_NAME(m_bytes)
237
+ #define PARAM_O_bytes(p) PARAM_NAME(O_bytes)
238
+ #define PARAM_v_bytes(p) PARAM_NAME(v_bytes)
239
+ #define PARAM_r_bytes(p) PARAM_NAME(r_bytes)
240
+ #define PARAM_P1_bytes(p) PARAM_NAME(P1_bytes)
241
+ #define PARAM_P2_bytes(p) PARAM_NAME(P2_bytes)
242
+ #define PARAM_P3_bytes(p) PARAM_NAME(P3_bytes)
243
+ #define PARAM_csk_bytes(p) PARAM_NAME(csk_bytes)
244
+ #define PARAM_cpk_bytes(p) PARAM_NAME(cpk_bytes)
245
+ #define PARAM_epk_bytes(p) PARAM_NAME(epk_bytes)
246
+ #define PARAM_sig_bytes(p) PARAM_NAME(sig_bytes)
247
+ static const unsigned char f_tail[] = PARAM_NAME(f_tail);
248
+ #define PARAM_salt_bytes(p) PARAM_NAME(salt_bytes)
249
+ #define PARAM_sk_seed_bytes(p) PARAM_NAME(sk_seed_bytes)
250
+ #define PARAM_digest_bytes(p) PARAM_NAME(digest_bytes)
251
+ #define PARAM_pk_seed_bytes(p) PARAM_NAME(pk_seed_bytes)
252
+ #define PARAM_f_tail(p) f_tail
253
+ #else
254
+ #error "Parameter not specified"
255
+ #endif
256
+
257
+ #define PARAM_P1_limbs(p) (PARAM_v(p)*(PARAM_v(p)+1)/2*PARAM_m_vec_limbs(p))
258
+ #define PARAM_P2_limbs(p) (PARAM_v(p)*PARAM_o(p)*PARAM_m_vec_limbs(p))
259
+ #define PARAM_P3_limbs(p) (PARAM_o(p)*(PARAM_o(p)+1)/2*PARAM_m_vec_limbs(p))
260
+ #define PARAM_EPK_limbs(p) (PARAM_P1_limbs(p) + PARAM_P2_limbs(p) + PARAM_P3_limbs(p))
261
+
262
+ /**
263
+ * Struct defining MAYO parameters
264
+ */
265
+ typedef struct {
266
+ int m;
267
+ int n;
268
+ int o;
269
+ int k;
270
+ int q;
271
+ const unsigned char *f_tail;
272
+ int m_bytes;
273
+ int O_bytes;
274
+ int v_bytes;
275
+ int r_bytes;
276
+ int R_bytes;
277
+ int P1_bytes;
278
+ int P2_bytes;
279
+ int P3_bytes;
280
+ int csk_bytes;
281
+ int cpk_bytes;
282
+ int sig_bytes;
283
+ int salt_bytes;
284
+ int sk_seed_bytes;
285
+ int digest_bytes;
286
+ int pk_seed_bytes;
287
+ int m_vec_limbs;
288
+ const char *name;
289
+ } mayo_params_t;
290
+
291
+ typedef struct sk_t {
292
+ uint64_t p[P1_LIMBS_MAX + P2_LIMBS_MAX];
293
+ uint8_t O[V_MAX*O_MAX];
294
+ } sk_t;
295
+
296
+ typedef struct pk_t {
297
+ uint64_t p[P1_LIMBS_MAX + P2_LIMBS_MAX + P3_LIMBS_MAX];
298
+ } pk_t;
299
+
300
+ /**
301
+ * MAYO parameter sets
302
+ */
303
+ #ifdef ENABLE_PARAMS_DYNAMIC
304
+ extern const mayo_params_t MAYO_1;
305
+ extern const mayo_params_t MAYO_2;
306
+ extern const mayo_params_t MAYO_3;
307
+ extern const mayo_params_t MAYO_5;
308
+ #endif
309
+
310
+ /**
311
+ * Status codes
312
+ */
313
+ #define MAYO_OK 0
314
+ #define MAYO_ERR 1
315
+
316
+ /**
317
+ * Mayo keypair generation.
318
+ *
319
+ * The implementation corresponds to Mayo.CompactKeyGen() in the Mayo spec.
320
+ * The caller is responsible to allocate sufficient memory to hold pk and sk.
321
+ *
322
+ * @param[in] p Mayo parameter set
323
+ * @param[out] pk Mayo public key
324
+ * @param[out] sk Mayo secret key
325
+ * @return int status code
326
+ */
327
+ #define mayo_keypair MAYO_NAMESPACE(mayo_keypair)
328
+ int mayo_keypair(const mayo_params_t *p, unsigned char *pk, unsigned char *sk);
329
+
330
+ #define mayo_sign_signature MAYO_NAMESPACE(mayo_sign_signature)
331
+ int mayo_sign_signature(const mayo_params_t *p, unsigned char *sig,
332
+ size_t *siglen, const unsigned char *m,
333
+ size_t mlen, const unsigned char *csk);
334
+
335
+ /**
336
+ * MAYO signature generation.
337
+ *
338
+ * The implementation performs Mayo.expandSK() + Mayo.sign() in the Mayo spec.
339
+ * Keys provided is a compacted secret keys.
340
+ * The caller is responsible to allocate sufficient memory to hold sm.
341
+ *
342
+ * @param[in] p Mayo parameter set
343
+ * @param[out] sm Signature concatenated with message
344
+ * @param[out] smlen Pointer to the length of sm
345
+ * @param[in] m Message to be signed
346
+ * @param[in] mlen Message length
347
+ * @param[in] sk Compacted secret key
348
+ * @return int status code
349
+ */
350
+ #define mayo_sign MAYO_NAMESPACE(mayo_sign)
351
+ int mayo_sign(const mayo_params_t *p, unsigned char *sm,
352
+ size_t *smlen, const unsigned char *m,
353
+ size_t mlen, const unsigned char *sk);
354
+
355
+ /**
356
+ * Mayo open signature.
357
+ *
358
+ * The implementation performs Mayo.verify(). If the signature verification succeeded, the original message is stored in m.
359
+ * Keys provided is a compact public key.
360
+ * The caller is responsible to allocate sufficient memory to hold m.
361
+ *
362
+ * @param[in] p Mayo parameter set
363
+ * @param[out] m Message stored if verification succeeds
364
+ * @param[out] mlen Pointer to the length of m
365
+ * @param[in] sm Signature concatenated with message
366
+ * @param[in] smlen Length of sm
367
+ * @param[in] pk Compacted public key
368
+ * @return int status code
369
+ */
370
+ #define mayo_open MAYO_NAMESPACE(mayo_open)
371
+ int mayo_open(const mayo_params_t *p, unsigned char *m,
372
+ size_t *mlen, const unsigned char *sm,
373
+ size_t smlen, const unsigned char *pk);
374
+
375
+ /**
376
+ * Mayo compact keypair generation.
377
+ *
378
+ * The implementation corresponds to Mayo.CompactKeyGen() in the Mayo spec.
379
+ * The caller is responsible to allocate sufficient memory to hold pk and sk.
380
+ *
381
+ * outputs a pair (csk, cpk) \in B^{csk_bytes} x B^{cpk_bytes}, where csk and
382
+ * cpk are compact representations of a Mayo secret key and public key
383
+ *
384
+ * @param[in] p Mayo parameter set
385
+ * @param[out] cpk Mayo compacted public key
386
+ * @param[out] csk Mayo compacted secret key
387
+ * @return int status code
388
+ */
389
+ #define mayo_keypair_compact MAYO_NAMESPACE(mayo_keypair_compact)
390
+ int mayo_keypair_compact(const mayo_params_t *p, unsigned char *cpk,
391
+ unsigned char *csk);
392
+
393
+ /**
394
+ * Mayo expand public key.
395
+ *
396
+ * The implementation corresponds to Mayo.expandPK() in the Mayo spec.
397
+ * The caller is responsible to allocate sufficient memory to hold epk.
398
+ *
399
+ * @param[in] p Mayo parameter set
400
+ * @param[in] cpk Compacted public key.
401
+ * @param[out] epk Expanded public key.
402
+ * @return int return code
403
+ */
404
+ #define mayo_expand_pk MAYO_NAMESPACE(mayo_expand_pk)
405
+ int mayo_expand_pk(const mayo_params_t *p, const unsigned char *cpk,
406
+ uint64_t *epk);
407
+
408
+ /**
409
+ * Mayo expand secret key.
410
+ *
411
+ * The implementation corresponds to Mayo.expandSK() in the Mayo spec.
412
+ * The caller is responsible to allocate sufficient memory to hold esk.
413
+ *
414
+ * @param[in] p Mayo parameter set
415
+ * @param[in] csk Compacted secret key.
416
+ * @param[out] esk Expanded secret key.
417
+ * @return int return code
418
+ */
419
+ #define mayo_expand_sk MAYO_NAMESPACE(mayo_expand_sk)
420
+ int mayo_expand_sk(const mayo_params_t *p, const unsigned char *csk,
421
+ sk_t *esk);
422
+
423
+ /**
424
+ * Mayo verify signature.
425
+ *
426
+ * The implementation performs Mayo.verify(). If the signature verification succeeded, returns 0, otherwise 1.
427
+ * Keys provided is a compact public key.
428
+ *
429
+ * @param[in] p Mayo parameter set
430
+ * @param[out] m Message stored if verification succeeds
431
+ * @param[out] mlen Pointer to the length of m
432
+ * @param[in] sig Signature
433
+ * @param[in] pk Compacted public key
434
+ * @return int 0 if verification succeeded, 1 otherwise.
435
+ */
436
+ #define mayo_verify MAYO_NAMESPACE(mayo_verify)
437
+ int mayo_verify(const mayo_params_t *p, const unsigned char *m,
438
+ size_t mlen, const unsigned char *sig,
439
+ const unsigned char *pk);
440
+
441
+ #endif
442
+
@@ -0,0 +1,25 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+
3
+ #ifndef MEM_H
4
+ #define MEM_H
5
+ #include <stddef.h>
6
+ #include <stdint.h>
7
+
8
+ /**
9
+ * Clears and frees allocated memory.
10
+ *
11
+ * @param[out] mem Memory to be cleared and freed.
12
+ * @param size Size of memory to be cleared and freed.
13
+ */
14
+ void mayo_secure_free(void *mem, size_t size);
15
+
16
+ /**
17
+ * Clears memory.
18
+ *
19
+ * @param[out] mem Memory to be cleared.
20
+ * @param size Size of memory to be cleared.
21
+ */
22
+ void mayo_secure_clear(void *mem, size_t size);
23
+
24
+ #endif
25
+
@@ -0,0 +1,31 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+
3
+ #ifndef randombytes_h
4
+ #define randombytes_h
5
+
6
+ #include <stdlib.h>
7
+
8
+ /**
9
+ * Randombytes initialization.
10
+ * Initialization may be needed for some random number generators (e.g. CTR-DRBG).
11
+ *
12
+ * @param[in] entropy_input 48 bytes entropy input
13
+ * @param[in] personalization_string Personalization string
14
+ * @param[in] security_strength Security string
15
+ */
16
+ void randombytes_init(unsigned char *entropy_input,
17
+ unsigned char *personalization_string,
18
+ int security_strength);
19
+
20
+ /**
21
+ * Random byte generation.
22
+ * The caller is responsible to allocate sufficient memory to hold x.
23
+ *
24
+ * @param[out] x Memory to hold the random bytes.
25
+ * @param[in] xlen Number of random bytes to be generated
26
+ * @return int 0 on success, -1 otherwise
27
+ */
28
+ int randombytes(unsigned char *x, size_t xlen);
29
+
30
+ #endif /* randombytes_h */
31
+
@@ -0,0 +1,141 @@
1
+ # SPDX-License-Identifier: Apache-2.0
2
+
3
+ # Script go generate constants for the parameter sets.
4
+
5
+ import math
6
+
7
+ DEFAULT_PARAMETERS = {
8
+ "MAYO_1": {
9
+ "name": "mayo1",
10
+ "n": 86,
11
+ "m": 78,
12
+ "o": 8,
13
+ "k": 10,
14
+ "q": 16,
15
+ "pk_seed_bytes": 16,
16
+ "sk_seed_bytes": 24,
17
+ "salt_bytes": 24,
18
+ "digest_bytes": 32
19
+ },
20
+ "MAYO_2": {
21
+ "name": "mayo2",
22
+ "n": 81,
23
+ "m": 64,
24
+ "o": 17,
25
+ "k": 4,
26
+ "q": 16,
27
+ "pk_seed_bytes": 16,
28
+ "sk_seed_bytes": 24,
29
+ "salt_bytes": 24,
30
+ "digest_bytes": 32
31
+ },
32
+ "MAYO_3": {
33
+ "name": "mayo3",
34
+ "n": 118,
35
+ "m": 108,
36
+ "o": 10,
37
+ "k": 11,
38
+ "q": 16,
39
+ "pk_seed_bytes": 16,
40
+ "sk_seed_bytes": 32,
41
+ "salt_bytes": 32,
42
+ "digest_bytes": 48
43
+ },
44
+ "MAYO_5": {
45
+ "name": "mayo5",
46
+ "n": 154,
47
+ "m": 142,
48
+ "o": 12,
49
+ "k": 12,
50
+ "q": 16,
51
+ "pk_seed_bytes": 16,
52
+ "sk_seed_bytes": 40,
53
+ "salt_bytes": 40,
54
+ "digest_bytes": 64
55
+ },
56
+ }
57
+
58
+ for param in DEFAULT_PARAMETERS:
59
+ n = DEFAULT_PARAMETERS[param]["n"]
60
+ m = DEFAULT_PARAMETERS[param]["m"]
61
+ o = DEFAULT_PARAMETERS[param]["o"]
62
+ k = DEFAULT_PARAMETERS[param]["k"]
63
+ q = DEFAULT_PARAMETERS[param]["q"]
64
+ pk_seed_bytes = DEFAULT_PARAMETERS[param]["pk_seed_bytes"]
65
+ sk_seed_bytes = DEFAULT_PARAMETERS[param]["sk_seed_bytes"]
66
+ salt_bytes = DEFAULT_PARAMETERS[param]["salt_bytes"]
67
+ digest_bytes = DEFAULT_PARAMETERS[param]["digest_bytes"]
68
+
69
+ v = n - o
70
+ q_bytes = (math.log(q, 2)/8)
71
+ m_bytes = math.ceil(q_bytes*m)
72
+ O_bytes = math.ceil((n - o)*o*q_bytes)
73
+ v_bytes = math.ceil((n - o)*q_bytes)
74
+ r_bytes = math.ceil(k*o*q_bytes)
75
+ P1_bytes = math.ceil(m*math.comb((n-o+1), 2)*q_bytes)
76
+ P2_bytes = math.ceil(m*(n - o)*o*q_bytes)
77
+ P3_bytes = math.ceil(m*math.comb((o+1), 2)*q_bytes)
78
+ m_vec_limbs = math.ceil(m/16)
79
+
80
+ R_bytes = salt_bytes
81
+ sig_bytes = math.ceil(k * n * q_bytes) + salt_bytes
82
+ epk_bytes = P1_bytes + P2_bytes + P3_bytes
83
+ cpk_bytes = P3_bytes + pk_seed_bytes
84
+ csk_bytes = sk_seed_bytes
85
+ esk_bytes = sk_seed_bytes + O_bytes + P1_bytes + P2_bytes
86
+
87
+ DEFAULT_PARAMETERS[param]["v"] = v
88
+ DEFAULT_PARAMETERS[param]["O_bytes"] = O_bytes
89
+ DEFAULT_PARAMETERS[param]["v_bytes"] = v_bytes
90
+ DEFAULT_PARAMETERS[param]["r_bytes"] = r_bytes
91
+ DEFAULT_PARAMETERS[param]["P1_bytes"] = P1_bytes
92
+ DEFAULT_PARAMETERS[param]["P2_bytes"] = P2_bytes
93
+ DEFAULT_PARAMETERS[param]["P3_bytes"] = P3_bytes
94
+ DEFAULT_PARAMETERS[param]["sig_bytes"] = sig_bytes
95
+ DEFAULT_PARAMETERS[param]["cpk_bytes"] = cpk_bytes
96
+ DEFAULT_PARAMETERS[param]["csk_bytes"] = csk_bytes
97
+ DEFAULT_PARAMETERS[param]["m_bytes"] = m_bytes
98
+ DEFAULT_PARAMETERS[param]["pk_seed_bytes"] = pk_seed_bytes
99
+ DEFAULT_PARAMETERS[param]["sk_seed_bytes"] = sk_seed_bytes
100
+ DEFAULT_PARAMETERS[param]["salt_bytes"] = salt_bytes
101
+ DEFAULT_PARAMETERS[param]["digest_bytes"] = digest_bytes
102
+ DEFAULT_PARAMETERS[param]["m_vec_limbs"] = m_vec_limbs
103
+
104
+ print("#define " + param + "_n " + str(n))
105
+ print("#define " + param + "_m " + str(m))
106
+ print("#define " + param + "_m_vec_limbs " + str(m_vec_limbs))
107
+ print("#define " + param + "_o " + str(o))
108
+ print("#define " + param + "_v " + str(v))
109
+ print("#define " + param + "_A_cols (" + param + "_k * " + param + "_o + 1)")
110
+ print("#define " + param + "_k " + str(k))
111
+ print("#define " + param + "_q " + str(q))
112
+ print("#define " + param + "_m_bytes " + str(m_bytes))
113
+ print("#define " + param + "_O_bytes " + str(O_bytes))
114
+ print("#define " + param + "_v_bytes " + str(v_bytes))
115
+ print("#define " + param + "_r_bytes " + str(r_bytes))
116
+ print("#define " + param + "_P1_bytes " + str(P1_bytes))
117
+ print("#define " + param + "_P2_bytes " + str(P2_bytes))
118
+ print("#define " + param + "_P3_bytes " + str(P3_bytes))
119
+ print("#define " + param + "_csk_bytes " + str(csk_bytes))
120
+ print("#define " + param + "_cpk_bytes " + str(cpk_bytes))
121
+ print("#define " + param + "_sig_bytes " + str(sig_bytes))
122
+ print("#define " + param + "_f_tail F_TAIL_" + str(m))
123
+ print("#define " + param + "_f_tail_arr f_tail_" + str(m))
124
+ print("#define " + param + "_salt_bytes " + str(salt_bytes))
125
+ print("#define " + param + "_digest_bytes " + str(digest_bytes))
126
+ print("#define " + param + "_pk_seed_bytes " + str(pk_seed_bytes))
127
+ print("#define " + param + "_sk_seed_bytes " + str(sk_seed_bytes))
128
+
129
+
130
+
131
+ print("\n")
132
+
133
+ maxvals = {}
134
+
135
+ for param in DEFAULT_PARAMETERS:
136
+ for paramval in DEFAULT_PARAMETERS[param]:
137
+ if paramval not in maxvals or maxvals[paramval] < DEFAULT_PARAMETERS[param][paramval]:
138
+ maxvals[paramval] = DEFAULT_PARAMETERS[param][paramval]
139
+
140
+ for max in maxvals:
141
+ print("#define " + max.upper() + "_MAX " + str(maxvals[max]))
@@ -0,0 +1,39 @@
1
+
2
+ import itertools
3
+
4
+ K.<x> = GF(16)
5
+
6
+ coefs = [0,1,x,x**2,x**3]
7
+
8
+ score = {}
9
+ for k in K:
10
+ score[k] = 0;
11
+
12
+ for k in coefs:
13
+ score[k] = 1
14
+
15
+ score[0] = 4
16
+ score[1] = 2
17
+
18
+ PR.<z> = PolynomialRing(K)
19
+
20
+ for m in [78,64,108,142]:
21
+
22
+ tries = 0
23
+ best_score = -1
24
+ for tail in itertools.product(K, repeat = 4):
25
+ P = tail[0] + tail[1]*z + tail[2]*z**2 + tail[3]*z**3 + z**m
26
+
27
+ s= sum([score[k] for k in tail])
28
+ if tail[-1] == 0:
29
+ s += 2
30
+ if s <= best_score:
31
+ continue
32
+
33
+ tries += 1
34
+
35
+ if P.is_irreducible():
36
+ best_score = s
37
+ print("found: ", P, " score: ", best_score)
38
+
39
+ print("tries:", tries)