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,75 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ #include "tree.h"
4
+ #include "hash.h"
5
+ #include <stddef.h>
6
+ #include <stdint.h>
7
+ #include <string.h>
8
+
9
+ void expand_tree(uint8_t nodes[2 * AIMER_N - 1][AIMER_SEED_SIZE],
10
+ const uint8_t salt[AIMER_SALT_SIZE], size_t rep_index,
11
+ const uint8_t seed[AIMER_SEED_SIZE])
12
+ {
13
+ size_t node_index;
14
+ hash_instance ctx;
15
+
16
+ memcpy(nodes[0], seed, AIMER_SEED_SIZE);
17
+ for (node_index = 1; node_index < AIMER_N; node_index++)
18
+ {
19
+ hash_init_prefix(&ctx, HASH_PREFIX_4);
20
+ hash_update(&ctx, salt, AIMER_SALT_SIZE);
21
+ hash_update(&ctx, (const uint8_t*)&rep_index, sizeof(uint8_t));
22
+ hash_update(&ctx, (const uint8_t*)&node_index, sizeof(uint8_t));
23
+ hash_update(&ctx, nodes[node_index - 1], AIMER_SEED_SIZE);
24
+ hash_final(&ctx);
25
+
26
+ hash_squeeze(&ctx, nodes[2 * node_index - 1], AIMER_SEED_SIZE << 1);
27
+ hash_ctx_release(&ctx);
28
+ }
29
+ }
30
+
31
+ void reveal_all_but(uint8_t reveal_path[AIMER_LOGN][AIMER_SEED_SIZE],
32
+ const uint8_t nodes[2 * AIMER_N - 1][AIMER_SEED_SIZE],
33
+ size_t cover_index)
34
+ {
35
+ size_t index = cover_index + AIMER_N;
36
+ for (size_t depth = 0; depth < AIMER_LOGN; depth++)
37
+ {
38
+ // index ^ 1 is sibling index
39
+ memcpy(reveal_path[depth], nodes[(index ^ 1) - 1], AIMER_SEED_SIZE);
40
+
41
+ // go to parent node
42
+ index >>= 1;
43
+ }
44
+ }
45
+
46
+ void reconstruct_tree(uint8_t nodes[2 * AIMER_N - 2][AIMER_SEED_SIZE],
47
+ const uint8_t salt[AIMER_SALT_SIZE],
48
+ const uint8_t reveal_path[AIMER_LOGN][AIMER_SEED_SIZE],
49
+ size_t rep_index, size_t cover_index)
50
+ {
51
+ size_t index, depth, path;
52
+ hash_instance ctx;
53
+
54
+ for (depth = 1; depth < AIMER_LOGN; depth++)
55
+ {
56
+ path = ((cover_index + AIMER_N) >> (AIMER_LOGN - depth)) ^ 1;
57
+ memcpy(nodes[path - 2], reveal_path[AIMER_LOGN - depth], AIMER_SEED_SIZE);
58
+
59
+ for (index = (1U << depth); index < (2U << depth); index++)
60
+ {
61
+ hash_init_prefix(&ctx, HASH_PREFIX_4);
62
+ hash_update(&ctx, salt, AIMER_SALT_SIZE);
63
+ hash_update(&ctx, (const uint8_t*)&rep_index, sizeof(uint8_t));
64
+ hash_update(&ctx, (const uint8_t*)&index, sizeof(uint8_t));
65
+ hash_update(&ctx, nodes[index - 2], AIMER_SEED_SIZE);
66
+ hash_final(&ctx);
67
+
68
+ hash_squeeze(&ctx, nodes[2 * index - 2], AIMER_SEED_SIZE << 1);
69
+ hash_ctx_release(&ctx);
70
+ }
71
+ }
72
+
73
+ path = ((cover_index + AIMER_N) >> (AIMER_LOGN - depth)) ^ 1;
74
+ memcpy(nodes[path - 2], reveal_path[AIMER_LOGN - depth], AIMER_SEED_SIZE);
75
+ }
@@ -0,0 +1,26 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ #ifndef TREE_H
4
+ #define TREE_H
5
+
6
+ #include "params.h"
7
+ #include <stddef.h>
8
+ #include <stdint.h>
9
+
10
+ #define expand_tree AIMER_NAMESPACE(expand_trees)
11
+ void expand_tree(uint8_t nodes[2 * AIMER_N - 1][AIMER_SEED_SIZE],
12
+ const uint8_t salt[AIMER_SALT_SIZE], size_t rep_index,
13
+ const uint8_t seed[AIMER_SEED_SIZE]);
14
+
15
+ #define reveal_all_but AIMER_NAMESPACE(reveal_all_but)
16
+ void reveal_all_but(uint8_t reveal_path[AIMER_LOGN][AIMER_SEED_SIZE],
17
+ const uint8_t nodes[2 * AIMER_N - 1][AIMER_SEED_SIZE],
18
+ size_t cover_index);
19
+
20
+ #define reconstruct_tree AIMER_NAMESPACE(reconstruct_tree)
21
+ void reconstruct_tree(uint8_t nodes[2 * AIMER_N - 2][AIMER_SEED_SIZE],
22
+ const uint8_t salt[AIMER_SALT_SIZE],
23
+ const uint8_t reveal_path[AIMER_LOGN][AIMER_SEED_SIZE],
24
+ size_t rep_index, size_t cover_index);
25
+
26
+ #endif // TREE_H
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2026 Team HAETAE
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,54 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ #ifndef HAETAE_API_H
4
+ #define HAETAE_API_H
5
+
6
+ #include "params.h"
7
+
8
+ #include <stddef.h>
9
+ #include <stdint.h>
10
+
11
+ #define CRYPTO_ALGNAME HAETAE_CRYPTO_ALGNAME
12
+ #define CRYPTO_SECRETKEYBYTES HAETAE_CRYPTO_SECRETKEYBYTES
13
+ #define CRYPTO_PUBLICKEYBYTES HAETAE_CRYPTO_PUBLICKEYBYTES
14
+ #define CRYPTO_BYTES HAETAE_CRYPTO_BYTES
15
+
16
+ #define crypto_sign_keypair HAETAE_NAMESPACE(keypair)
17
+ int crypto_sign_keypair(uint8_t *vk, uint8_t *sk);
18
+
19
+ #define crypto_sign_keypair_internal HAETAE_NAMESPACE(keypair_internal)
20
+ int crypto_sign_keypair_internal(uint8_t *vk, uint8_t *sk,
21
+ uint8_t seed[HAETAE_SEEDBYTES]);
22
+
23
+ #define crypto_sign_signature_internal HAETAE_NAMESPACE(signature_internal)
24
+ int crypto_sign_signature_internal(uint8_t *sig, size_t *siglen,
25
+ const uint8_t *m, size_t mlen,
26
+ const uint8_t *pre, size_t prelen,
27
+ const uint8_t rnd[HAETAE_SEEDBYTES],
28
+ const uint8_t *sk);
29
+
30
+ #define crypto_sign_signature HAETAE_NAMESPACE(signature)
31
+ int crypto_sign_signature(uint8_t *sig, size_t *siglen, const uint8_t *m,
32
+ size_t mlen, const uint8_t *ctx, size_t ctxlen,
33
+ const uint8_t *sk);
34
+
35
+ #define crypto_sign HAETAE_NAMESPACE(sign)
36
+ int crypto_sign(uint8_t *sm, size_t *smlen, const uint8_t *m, size_t mlen,
37
+ const uint8_t *ctx, size_t ctxlen, const uint8_t *sk);
38
+
39
+ #define crypto_sign_verify_internal HAETAE_NAMESPACE(verify_internal)
40
+ int crypto_sign_verify_internal(const uint8_t *sig, size_t siglen,
41
+ const uint8_t *m, size_t mlen,
42
+ const uint8_t *pre, size_t prelen,
43
+ const uint8_t *vk);
44
+
45
+ #define crypto_sign_verify HAETAE_NAMESPACE(verify)
46
+ int crypto_sign_verify(const uint8_t *sig, size_t siglen, const uint8_t *m,
47
+ size_t mlen, const uint8_t *ctx, size_t ctxlen,
48
+ const uint8_t *vk);
49
+
50
+ #define crypto_sign_open HAETAE_NAMESPACE(open)
51
+ int crypto_sign_open(uint8_t *m, size_t *mlen, const uint8_t *sm, size_t smlen,
52
+ const uint8_t *ctx, size_t ctxlen, const uint8_t *vk);
53
+
54
+ #endif // HAETAE_API_H
@@ -0,0 +1,12 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ #ifndef HAETAE_COMMON_H
4
+ #define HAETAE_COMMON_H
5
+
6
+ #define HAETAE_CONCAT_(x1, x2) x1##x2
7
+ #define HAETAE_CONCAT(x1, x2) HAETAE_CONCAT_(x1, x2)
8
+
9
+ #define HAETAE_STR_(x) #x
10
+ #define HAETAE_STR(x) HAETAE_STR_(x)
11
+
12
+ #endif /* !HAETAE_COMMON_H */
@@ -0,0 +1,42 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ #ifndef HAETAE_CONFIG_H
4
+ #define HAETAE_CONFIG_H
5
+
6
+ #include "common.h"
7
+
8
+ #define HAETAE_MODE2 0
9
+ #define HAETAE_MODE3 1
10
+ #define HAETAE_MODE5 2
11
+
12
+ #if !defined(HAETAE_CONFIG_MODE)
13
+ #define HAETAE_CONFIG_MODE HAETAE_MODE2
14
+ #endif
15
+
16
+ #if (HAETAE_CONFIG_MODE != HAETAE_MODE2) && \
17
+ (HAETAE_CONFIG_MODE != HAETAE_MODE3) && \
18
+ (HAETAE_CONFIG_MODE != HAETAE_MODE5)
19
+ #error "Invalid HAETAE_CONFIG_MODE"
20
+ #endif
21
+
22
+ #if !defined(HAETAE_CONFIG_MODE_STRING)
23
+ #if HAETAE_CONFIG_MODE == HAETAE_MODE2
24
+ #define HAETAE_CONFIG_MODE_STRING mode2
25
+ #elif HAETAE_CONFIG_MODE == HAETAE_MODE3
26
+ #define HAETAE_CONFIG_MODE_STRING mode3
27
+ #elif HAETAE_CONFIG_MODE == HAETAE_MODE5
28
+ #define HAETAE_CONFIG_MODE_STRING mode5
29
+ #endif
30
+ #endif
31
+
32
+ #if !defined(HAETAE_CONFIG_NAMESPACE_PREFIX)
33
+ #define HAETAE_CONFIG_NAMESPACE_PREFIX cryptolab_haetae_
34
+ #endif
35
+
36
+ #if !defined(HAETAE_CONFIG_NAMESPACE_PREFIX_MODE)
37
+ #define HAETAE_ADD_MODE(s) HAETAE_CONCAT(s, HAETAE_CONFIG_MODE_STRING)
38
+ #define HAETAE_CONFIG_NAMESPACE_PREFIX_MODE \
39
+ HAETAE_CONCAT(HAETAE_ADD_MODE(HAETAE_CONFIG_NAMESPACE_PREFIX), _)
40
+ #endif
41
+
42
+ #endif /* !HAETAE_CONFIG_H */
@@ -0,0 +1,19 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ #ifndef HAETAE_DECOMPOSE_H
4
+ #define HAETAE_DECOMPOSE_H
5
+
6
+ #include "params.h"
7
+
8
+ #include <stdint.h>
9
+
10
+ #define decompose_z1 HAETAE_NAMESPACE(decompose_z1)
11
+ void decompose_z1(int32_t *highbits, int32_t *lowbits, const int32_t r);
12
+
13
+ #define decompose_hint HAETAE_NAMESPACE(decompose_hint)
14
+ void decompose_hint(int32_t *highbits, const int32_t r);
15
+
16
+ #define decompose_vk HAETAE_NAMESPACE(decompose_vk)
17
+ int32_t decompose_vk(int32_t *a0, const int32_t a);
18
+
19
+ #endif /* !HAETAE_DECOMPOSE_H */
@@ -0,0 +1,20 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ #ifndef HAETAE_ENCODING_H
4
+ #define HAETAE_ENCODING_H
5
+
6
+ #include "params.h"
7
+
8
+ #include <stddef.h>
9
+ #include <stdint.h>
10
+
11
+ #define encode_h HAETAE_NAMESPACE(encode_h)
12
+ uint16_t encode_h(uint8_t *buf, const int32_t *h);
13
+ #define decode_h HAETAE_NAMESPACE(decode_h)
14
+ uint16_t decode_h(int32_t *h, const uint8_t *buf, uint16_t size_in);
15
+ #define encode_hb_z1 HAETAE_NAMESPACE(encode_hb_z1)
16
+ uint16_t encode_hb_z1(uint8_t *buf, const int32_t *hb_z1);
17
+ #define decode_hb_z1 HAETAE_NAMESPACE(decode_hb_z1)
18
+ uint16_t decode_hb_z1(int32_t *hb_z1, const uint8_t *buf, uint16_t size_in);
19
+
20
+ #endif /* !HAETAE_ENCODING_H */
@@ -0,0 +1,26 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ #ifndef HAETAE_FFT_H
4
+ #define HAETAE_FFT_H
5
+
6
+ #include "params.h"
7
+ #include "poly.h"
8
+
9
+ #include <stdint.h>
10
+
11
+ #define FFT_N 256
12
+ #define FFT_LOGN 8
13
+
14
+ typedef struct {
15
+ int32_t real;
16
+ int32_t imag;
17
+ } complex_fp32_16;
18
+
19
+ #define fft HAETAE_NAMESPACE(fft)
20
+ void fft(complex_fp32_16 data[FFT_N]);
21
+ #define fft_init_and_bitrev HAETAE_NAMESPACE(fft_init_and_bitrev)
22
+ void fft_init_and_bitrev(complex_fp32_16 r[FFT_N], const poly *x);
23
+ #define complex_fp_sqabs HAETAE_NAMESPACE(complex_fp_sqabs)
24
+ int32_t complex_fp_sqabs(complex_fp32_16 x);
25
+
26
+ #endif /* !HAETAE_FFT_H */
@@ -0,0 +1,40 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ #ifndef HAETAE_FIPS202_H
4
+ #define HAETAE_FIPS202_H
5
+
6
+ #include <stddef.h>
7
+ #include <stdint.h>
8
+
9
+ #define SHAKE128_RATE 168
10
+ #define SHAKE256_RATE 136
11
+ #define SHA3_256_RATE 136
12
+ #define SHA3_512_RATE 72
13
+
14
+ typedef struct {
15
+ uint64_t s[25];
16
+ unsigned int pos;
17
+ } keccak_state;
18
+
19
+ extern const uint64_t KeccakF_RoundConstants[];
20
+
21
+ void shake128_init(keccak_state *state);
22
+ void shake128_absorb(keccak_state *state, const uint8_t *in, size_t inlen);
23
+ void shake128_finalize(keccak_state *state);
24
+ void shake128_squeeze(uint8_t *out, size_t outlen, keccak_state *state);
25
+ void shake128_absorb_once(keccak_state *state, const uint8_t *in, size_t inlen);
26
+ void shake128_squeezeblocks(uint8_t *out, size_t nblocks, keccak_state *state);
27
+
28
+ void shake256_init(keccak_state *state);
29
+ void shake256_absorb(keccak_state *state, const uint8_t *in, size_t inlen);
30
+ void shake256_finalize(keccak_state *state);
31
+ void shake256_squeeze(uint8_t *out, size_t outlen, keccak_state *state);
32
+ void shake256_absorb_once(keccak_state *state, const uint8_t *in, size_t inlen);
33
+ void shake256_squeezeblocks(uint8_t *out, size_t nblocks, keccak_state *state);
34
+
35
+ void shake128(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen);
36
+ void shake256(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen);
37
+ void sha3_256(uint8_t h[32], const uint8_t *in, size_t inlen);
38
+ void sha3_512(uint8_t h[64], const uint8_t *in, size_t inlen);
39
+
40
+ #endif /* !HAETAE_FIPS202_H */
@@ -0,0 +1,125 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ #ifndef HAETAE_FIXPOINT_H
4
+ #define HAETAE_FIXPOINT_H
5
+
6
+ #include "params.h"
7
+
8
+ #include <stdint.h>
9
+ #include <stdlib.h>
10
+
11
+ #ifdef __SIZEOF_INT128__
12
+ __extension__ typedef __int128 int128;
13
+ __extension__ typedef unsigned __int128 uint128;
14
+ #endif
15
+
16
+ typedef struct {
17
+ uint64_t limb48[2];
18
+ } fp96_76;
19
+
20
+ #define fixpoint_square HAETAE_NAMESPACE(fixpoint_square)
21
+ void fixpoint_square(fp96_76 *sqx, const fp96_76 *x);
22
+
23
+ #define fixpoint_newton_invsqrt HAETAE_NAMESPACE(fixpoint_newton_invsqrt)
24
+ void fixpoint_newton_invsqrt(fp96_76 *invsqrtx, const fp96_76 *xhalf);
25
+
26
+ #define fixpoint_mul_rnd13 HAETAE_NAMESPACE(fixpoint_mul_rnd13)
27
+ int32_t fixpoint_mul_rnd13(const uint64_t x, const fp96_76 *y,
28
+ const uint8_t sign);
29
+
30
+ #define fixpoint_add HAETAE_NAMESPACE(fixpoint_add)
31
+ void fixpoint_add(fp96_76 *xy, const fp96_76 *x, const fp96_76 *y);
32
+
33
+ static inline void renormalize(fp96_76 *x) {
34
+ x->limb48[1] += x->limb48[0] >> 48;
35
+ x->limb48[0] &= (1ULL << 48) - 1;
36
+ }
37
+
38
+ static inline int64_t smulh48(int64_t a, uint64_t b) {
39
+ #ifndef __SIZEOF_INT128__
40
+ int64_t ah = a >> 24;
41
+ int64_t al = a - (ah << 24);
42
+ int64_t bl = b & ((1 << 24) - 1);
43
+ int64_t bh = b >> 24;
44
+
45
+ int64_t res = (al * bl) >> 24;
46
+ res += al * bh + ah * bl + (1 << 23); // rounding
47
+ res >>= 24;
48
+ return res + (ah * bh);
49
+ #else
50
+ return ((int128)a * (int128)b + (1ULL << 47)) >> 48; // rounding
51
+ #endif
52
+ }
53
+
54
+ static inline void mul64(uint64_t r[2], const uint64_t b, const uint64_t a) {
55
+ #ifndef __SIZEOF_INT128__
56
+ uint64_t al = a & ((1ULL << 32) - 1), bl = b & ((1ULL << 32) - 1),
57
+ ah = a >> 32, bh = b >> 32;
58
+ r[0] = a * b;
59
+ r[1] = ah * bl + al * bh + ((al * bl) >> 32);
60
+ r[1] >>= 32;
61
+ r[1] += ah * bh;
62
+ #else
63
+ uint128 res = ((uint128)a * (uint128)b);
64
+ r[0] = res;
65
+ r[1] = res >> 64;
66
+ #endif
67
+ }
68
+
69
+ static inline void sq64(uint64_t r[2], const uint64_t a) {
70
+ #ifndef __SIZEOF_INT128__
71
+ uint64_t al = a & ((1ULL << 32) - 1), ah = a >> 32;
72
+ r[0] = a * a;
73
+ r[1] = ah * al * 2 + ((al * al) >> 32);
74
+ r[1] >>= 32;
75
+ r[1] += ah * ah;
76
+ #else
77
+ uint128 res = ((uint128)a * (uint128)a);
78
+ r[0] = res;
79
+ r[1] = res >> 64;
80
+ #endif
81
+ }
82
+
83
+ static inline void mul48(uint64_t r[2], const uint64_t b, const uint64_t a) {
84
+ mul64(r, b, a);
85
+ r[1] <<= 16;
86
+ r[1] ^= r[0] >> 48;
87
+ r[0] &= (1ULL << 48) - 1;
88
+ }
89
+
90
+ static inline void mulacc48(uint64_t r[2], const uint64_t b, const uint64_t a) {
91
+ uint64_t tmp[2];
92
+ mul48(tmp, b, a);
93
+ r[0] += tmp[0];
94
+ r[1] += tmp[1];
95
+ }
96
+
97
+ // (a0 + a1*2^32)^2 = a0^2 + 2^33*a0*a1 + 2^64*a1^2
98
+ static inline void sq48(uint64_t r[2], const uint64_t a) {
99
+ sq64(r, a);
100
+
101
+ r[1] <<= 16;
102
+ r[1] ^= r[0] >> 48;
103
+ r[0] &= (1ULL << 48) - 1;
104
+ }
105
+
106
+ static inline void fixpoint_mul_high(fp96_76 *xy, const fp96_76 *x,
107
+ const uint64_t y) {
108
+ uint64_t tmp[2];
109
+ mul48(&xy->limb48[0], x->limb48[0], y); // implicitly shifted right by 48
110
+
111
+ mul48(tmp, x->limb48[1], y);
112
+ xy->limb48[1] += tmp[0];
113
+
114
+ // shift right by 28, rounding
115
+ xy->limb48[0] += 1UL << 27;
116
+ xy->limb48[0] >>= 28;
117
+ xy->limb48[0] += (xy->limb48[1] << 20) & ((1ULL << 48) - 1);
118
+ xy->limb48[1] >>= 28;
119
+
120
+ xy->limb48[1] += tmp[1] << 20;
121
+
122
+ renormalize(xy);
123
+ }
124
+
125
+ #endif /* !HAETAE_FIXPOINT_H */
@@ -0,0 +1,16 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ #ifndef HAETAE_NTT_H
4
+ #define HAETAE_NTT_H
5
+
6
+ #include "params.h"
7
+
8
+ #include <stdint.h>
9
+
10
+ #define ntt HAETAE_NAMESPACE(ntt)
11
+ void ntt(int32_t a[HAETAE_N]);
12
+
13
+ #define invntt_tomont HAETAE_NAMESPACE(invntt_tomont)
14
+ void invntt_tomont(int32_t a[HAETAE_N]);
15
+
16
+ #endif /* !HAETAE_NTT_H */
@@ -0,0 +1,58 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ #ifndef HAETAE_PACKING_H
4
+ #define HAETAE_PACKING_H
5
+
6
+ #include "params.h"
7
+ #include "polyvec.h"
8
+
9
+ #include <stdint.h>
10
+
11
+ #define pack_vk HAETAE_NAMESPACE(pack_vk)
12
+ void pack_vk(uint8_t vk[HAETAE_CRYPTO_PUBLICKEYBYTES], polyveck *b,
13
+ const uint8_t seed[HAETAE_SEEDBYTES]);
14
+ #define unpack_vk HAETAE_NAMESPACE(unpack_vk)
15
+ void unpack_vk(polyvecl A[HAETAE_K],
16
+ const uint8_t vk[HAETAE_CRYPTO_PUBLICKEYBYTES]);
17
+
18
+ #define pack_sk HAETAE_NAMESPACE(pack_sk)
19
+ void pack_sk(uint8_t sk[HAETAE_CRYPTO_SECRETKEYBYTES],
20
+ const uint8_t vk[HAETAE_CRYPTO_PUBLICKEYBYTES], const polyvecm *s0,
21
+ const polyveck *s1, const uint8_t key[HAETAE_SEEDBYTES]);
22
+ #define unpack_sk HAETAE_NAMESPACE(unpack_sk)
23
+ void unpack_sk(polyvecl A[HAETAE_K], polyvecm *s0, polyveck *s1, uint8_t *key,
24
+ const uint8_t sk[HAETAE_CRYPTO_SECRETKEYBYTES]);
25
+
26
+ #define pack_sig HAETAE_NAMESPACE(pack_sig)
27
+ int pack_sig(uint8_t sig[HAETAE_CRYPTO_BYTES], const poly *c,
28
+ const polyvecl *lowbits_z1, const polyvecl *highbits_z1,
29
+ const polyveck *h);
30
+ #define unpack_sig HAETAE_NAMESPACE(unpack_sig)
31
+ int unpack_sig(poly *c, polyvecl *lowbits_z1, polyvecl *highbits_z1,
32
+ polyveck *h, const uint8_t sig[HAETAE_CRYPTO_BYTES]);
33
+
34
+ #define pack_poly_q HAETAE_NAMESPACE(pack_poly_q)
35
+ void pack_poly_q(uint8_t *r, const poly *a);
36
+ #define unpack_poly_q HAETAE_NAMESPACE(unpack_poly_q)
37
+ void unpack_poly_q(poly *r, const uint8_t *a);
38
+
39
+ #define pack_poly_eta HAETAE_NAMESPACE(pack_poly_eta)
40
+ void pack_poly_eta(uint8_t *r, const poly *a);
41
+ #define unpack_poly_eta HAETAE_NAMESPACE(unpack_poly_eta)
42
+ void unpack_poly_eta(poly *r, const uint8_t *a);
43
+
44
+ #define pack_poly2_eta HAETAE_NAMESPACE(pack_poly2_eta)
45
+ void pack_poly2_eta(uint8_t *r, const poly *a);
46
+ #define unpack_poly2_eta HAETAE_NAMESPACE(unpack_poly2_eta)
47
+ void unpack_poly2_eta(poly *r, const uint8_t *a);
48
+
49
+ #define pack_vec_highbits HAETAE_NAMESPACE(pack_vec_highbits)
50
+ void pack_vec_highbits(uint8_t *buf, const polyveck *v);
51
+
52
+ #define pack_poly_highbits HAETAE_NAMESPACE(pack_poly_highbits)
53
+ void pack_poly_highbits(uint8_t *buf, const poly *a);
54
+
55
+ #define pack_poly_lsb HAETAE_NAMESPACE(pack_poly_lsb)
56
+ void pack_poly_lsb(uint8_t *buf, const poly *a);
57
+
58
+ #endif /* !HAETAE_PACKING_H */
@@ -0,0 +1,117 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ #ifndef HAETAE_PARAMS_H
4
+ #define HAETAE_PARAMS_H
5
+
6
+ #include "config.h"
7
+
8
+ #define HAETAE_SEEDBYTES 32
9
+ #define HAETAE_CRHBYTES 64
10
+ #define HAETAE_N 256
11
+
12
+ #define HAETAE_Q 64513
13
+ #define HAETAE_DQ (HAETAE_Q << 1) // 2Q
14
+
15
+ #define HAETAE_MODE HAETAE_CONFIG_MODE
16
+ #define HAETAE_NAMESPACE(s) \
17
+ HAETAE_CONCAT(HAETAE_CONFIG_NAMESPACE_PREFIX_MODE, s)
18
+ #define HAETAE_CRYPTO_ALGNAME \
19
+ HAETAE_STR(HAETAE_CONCAT(haetae_, HAETAE_CONFIG_MODE_STRING))
20
+
21
+ #if HAETAE_MODE == HAETAE_MODE2
22
+ #define HAETAE_K 2
23
+ #define HAETAE_L 4
24
+ #define HAETAE_TAU 58
25
+ #define HAETAE_B0 9846.02
26
+ #define HAETAE_B1 9838.98
27
+ #define HAETAE_B2 12777.52
28
+ #define HAETAE_GAMMA 48.858
29
+ #define HAETAE_LN 8192 // Large N
30
+ #define HAETAE_LNHALF 4096
31
+ #define HAETAE_LNBITS 13
32
+ #define HAETAE_SQNM 39.191835884530846 // \sqrt(n * m)
33
+ #define HAETAE_CRYPTO_BYTES 1474
34
+
35
+ #define HAETAE_BASE_ENC_HB_Z1 132
36
+ #define HAETAE_BASE_ENC_H 7
37
+
38
+ #define HAETAE_ALPHA_HINT 512
39
+ #define HAETAE_LOG_ALPHA_HINT 9
40
+
41
+ #define HAETAE_POLYQ_PACKEDBYTES 480 // 16bits * HAETAE_N / 8bits
42
+
43
+ #elif HAETAE_MODE == HAETAE_MODE3
44
+ #define HAETAE_K 3
45
+ #define HAETAE_L 6
46
+ #define HAETAE_TAU 80
47
+ #define HAETAE_B0 18314.98
48
+ #define HAETAE_B1 18307.70
49
+ #define HAETAE_B2 21906.65
50
+ #define HAETAE_GAMMA 57.707
51
+ #define HAETAE_LN 8192 // Large N
52
+ #define HAETAE_LNHALF 4096
53
+ #define HAETAE_LNBITS 13
54
+ #define HAETAE_SQNM 48.0
55
+ #define HAETAE_CRYPTO_BYTES 2349
56
+
57
+ #define HAETAE_BASE_ENC_HB_Z1 376
58
+ #define HAETAE_BASE_ENC_H 127
59
+
60
+ #define HAETAE_ALPHA_HINT 512
61
+ #define HAETAE_LOG_ALPHA_HINT 9
62
+
63
+ #define HAETAE_POLYQ_PACKEDBYTES 480 // 16bits * HAETAE_N / 8bits
64
+
65
+ #elif HAETAE_MODE == HAETAE_MODE5
66
+ #define HAETAE_K 4
67
+ #define HAETAE_L 7
68
+ #define HAETAE_TAU 128
69
+ #define HAETAE_B0 22343.66
70
+ #define HAETAE_B1 22334.95
71
+ #define HAETAE_B2 24441.49
72
+ #define HAETAE_GAMMA 55.13
73
+ #define HAETAE_LN 8192 // Large N
74
+ #define HAETAE_LNHALF 4096
75
+ #define HAETAE_LNBITS 13
76
+ #define HAETAE_SQNM 53.0659966456864
77
+ #define HAETAE_CRYPTO_BYTES 2948
78
+
79
+ #define HAETAE_BASE_ENC_HB_Z1 501
80
+ #define HAETAE_BASE_ENC_H 358
81
+
82
+ #define HAETAE_ALPHA_HINT 256
83
+ #define HAETAE_LOG_ALPHA_HINT 8
84
+
85
+ #define HAETAE_POLYQ_PACKEDBYTES 512 // 16bits * HAETAE_N / 8bits
86
+
87
+ #endif // HAETAE_MODE
88
+
89
+ #define HAETAE_HALF_ALPHA_HINT (HAETAE_ALPHA_HINT >> 1) // ALPHA / 2
90
+
91
+ #define HAETAE_B0SQ ((uint64_t)(HAETAE_B0 * HAETAE_B0))
92
+ #define HAETAE_B1SQ ((uint64_t)(HAETAE_B1 * HAETAE_B1))
93
+ #define HAETAE_B2SQ ((uint64_t)(HAETAE_B2 * HAETAE_B2))
94
+
95
+ #define HAETAE_M (HAETAE_L - 1)
96
+
97
+ #define HAETAE_ETA 1
98
+ #define HAETAE_POLYETA_PACKEDBYTES 64
99
+ #if (HAETAE_MODE == HAETAE_MODE2) || (HAETAE_MODE == HAETAE_MODE3)
100
+ #define HAETAE_POLY2ETA_PACKEDBYTES 96
101
+ #elif HAETAE_MODE == HAETAE_MODE5
102
+ #define HAETAE_POLY2ETA_PACKEDBYTES 64
103
+ #endif
104
+
105
+ #define HAETAE_POLYC_PACKEDBYTES 32 // 1bit * HAETAE_N / 8bits
106
+ #define HAETAE_POLY_HIGHBITS_PACKEDBYTES (HAETAE_N * 9 / 8)
107
+ #define HAETAE_POLYVECK_HIGHBITS_PACKEDBYTES \
108
+ (HAETAE_POLY_HIGHBITS_PACKEDBYTES * HAETAE_K)
109
+
110
+ #define HAETAE_CRYPTO_PUBLICKEYBYTES \
111
+ (HAETAE_SEEDBYTES + HAETAE_K * HAETAE_POLYQ_PACKEDBYTES) // seed + b
112
+ #define HAETAE_CRYPTO_SECRETKEYBYTES \
113
+ (HAETAE_CRYPTO_PUBLICKEYBYTES + HAETAE_M * HAETAE_POLYETA_PACKEDBYTES + \
114
+ HAETAE_K * HAETAE_POLY2ETA_PACKEDBYTES + \
115
+ HAETAE_SEEDBYTES) // vk + s + HAETAE_K
116
+
117
+ #endif /* !HAETAE_PARAMS_H */