kpqc 0.1.0 → 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 (101) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +15 -0
  3. data/Gemfile +4 -0
  4. data/README.md +9 -6
  5. data/THIRD_PARTY_NOTICES.md +14 -47
  6. data/ext/kpqc/extconf_helper.rb +6 -3
  7. data/ext/kpqc/secure_clear.c +8 -0
  8. data/kpqc.gemspec +4 -5
  9. data/lib/kpqc/aimer.rb +6 -6
  10. data/lib/kpqc/version.rb +1 -2
  11. data/test/kat/aimer128f.rsp +4 -4
  12. data/test/kat/aimer128s.rsp +4 -4
  13. data/test/kat/aimer192f.rsp +4 -4
  14. data/test/kat/aimer192s.rsp +4 -4
  15. data/test/kat/aimer256f.rsp +4 -4
  16. data/test/kat/aimer256s.rsp +4 -4
  17. data/test/kat/haetae2.rsp +1 -1
  18. data/test/kat/haetae3.rsp +1 -1
  19. data/test/kat/haetae5.rsp +1 -1
  20. data/vendor/AIMer/LICENSE +1 -1
  21. data/vendor/AIMer/aim3.c +227 -0
  22. data/vendor/AIMer/aim3.h +63 -0
  23. data/vendor/AIMer/field.h +12 -4
  24. data/vendor/AIMer/field128.c +24 -381
  25. data/vendor/AIMer/field192.c +24 -400
  26. data/vendor/AIMer/field256.c +28 -447
  27. data/vendor/AIMer/field_common.c +98 -0
  28. data/vendor/AIMer/field_internal.h +218 -0
  29. data/vendor/AIMer/params/params-aimer-128f.h +8 -8
  30. data/vendor/AIMer/params/params-aimer-128s.h +8 -8
  31. data/vendor/AIMer/params/params-aimer-192f.h +8 -8
  32. data/vendor/AIMer/params/params-aimer-192s.h +8 -8
  33. data/vendor/AIMer/params/params-aimer-256f.h +8 -8
  34. data/vendor/AIMer/params/params-aimer-256s.h +8 -8
  35. data/vendor/AIMer/params.h +5 -0
  36. data/vendor/AIMer/sign.c +246 -167
  37. data/vendor/AIMer/sign.h +6 -6
  38. data/vendor/AIMer/tree.c +10 -4
  39. data/vendor/AIMer/tree.h +1 -1
  40. data/vendor/HAETAE/include/fixpoint.h +14 -14
  41. data/vendor/HAETAE/include/rans_byte.h +10 -9
  42. data/vendor/HAETAE/src/decompose.c +8 -4
  43. data/vendor/HAETAE/src/encoding.c +5 -5
  44. data/vendor/HAETAE/src/fft.c +1 -1
  45. data/vendor/HAETAE/src/fips202.c +26 -14
  46. data/vendor/HAETAE/src/fixpoint.c +21 -18
  47. data/vendor/HAETAE/src/ntt.c +2 -0
  48. data/vendor/HAETAE/src/packing.c +15 -15
  49. data/vendor/HAETAE/src/poly.c +30 -3
  50. data/vendor/HAETAE/src/polyfix.c +2 -2
  51. data/vendor/HAETAE/src/polymat.c +2 -2
  52. data/vendor/HAETAE/src/polyvec.c +7 -5
  53. data/vendor/HAETAE/src/reduce.c +2 -0
  54. data/vendor/HAETAE/src/sampler.c +231 -47
  55. data/vendor/HAETAE/src/sign.c +11 -10
  56. data/vendor/NTRUplus/NTRU+1152/api.h +27 -8
  57. data/vendor/NTRUplus/NTRU+1152/fips202/fips202.c +12 -269
  58. data/vendor/NTRUplus/NTRU+1152/fips202/fips202.h +4 -113
  59. data/vendor/NTRUplus/NTRU+1152/kem.c +110 -40
  60. data/vendor/NTRUplus/NTRU+1152/ntt.c +50 -54
  61. data/vendor/NTRUplus/NTRU+1152/poly.c +21 -13
  62. data/vendor/NTRUplus/NTRU+1152/poly.h +3 -3
  63. data/vendor/NTRUplus/NTRU+1152/randombytes.h +1 -0
  64. data/vendor/NTRUplus/NTRU+1152/symmetric.c +3 -9
  65. data/vendor/NTRUplus/NTRU+1152/symmetric.h +13 -3
  66. data/vendor/NTRUplus/NTRU+1152/util.h +15 -0
  67. data/vendor/NTRUplus/NTRU+768/api.h +27 -8
  68. data/vendor/NTRUplus/NTRU+768/fips202/fips202.c +12 -269
  69. data/vendor/NTRUplus/NTRU+768/fips202/fips202.h +4 -113
  70. data/vendor/NTRUplus/NTRU+768/kem.c +110 -40
  71. data/vendor/NTRUplus/NTRU+768/ntt.c +50 -54
  72. data/vendor/NTRUplus/NTRU+768/poly.c +23 -13
  73. data/vendor/NTRUplus/NTRU+768/poly.h +3 -3
  74. data/vendor/NTRUplus/NTRU+768/randombytes.h +1 -0
  75. data/vendor/NTRUplus/NTRU+768/symmetric.c +3 -9
  76. data/vendor/NTRUplus/NTRU+768/symmetric.h +13 -3
  77. data/vendor/NTRUplus/NTRU+768/util.h +15 -0
  78. data/vendor/NTRUplus/NTRU+864/api.h +27 -8
  79. data/vendor/NTRUplus/NTRU+864/fips202/fips202.c +12 -269
  80. data/vendor/NTRUplus/NTRU+864/fips202/fips202.h +4 -113
  81. data/vendor/NTRUplus/NTRU+864/kem.c +110 -40
  82. data/vendor/NTRUplus/NTRU+864/ntt.c +24 -28
  83. data/vendor/NTRUplus/NTRU+864/poly.c +26 -12
  84. data/vendor/NTRUplus/NTRU+864/poly.h +3 -3
  85. data/vendor/NTRUplus/NTRU+864/randombytes.h +1 -0
  86. data/vendor/NTRUplus/NTRU+864/symmetric.c +3 -9
  87. data/vendor/NTRUplus/NTRU+864/symmetric.h +13 -3
  88. data/vendor/NTRUplus/NTRU+864/util.h +15 -0
  89. data/vendor/SMAUG-T/src/cbd.c +4 -4
  90. data/vendor/SMAUG-T/src/ciphertext.c +2 -2
  91. data/vendor/SMAUG-T/src/dg.c +2 -2
  92. data/vendor/SMAUG-T/src/hwt.c +4 -4
  93. data/vendor/SMAUG-T/src/indcpa.c +5 -5
  94. data/vendor/SMAUG-T/src/kem.c +9 -7
  95. data/vendor/SMAUG-T/src/key.c +2 -2
  96. data/vendor/SMAUG-T/src/pack.c +8 -8
  97. metadata +17 -42
  98. data/vendor/AIMer/aim2.c +0 -163
  99. data/vendor/AIMer/aim2.h +0 -36
  100. data/vendor/AIMer/aim2_constant.h +0 -74
  101. data/vendor/SOURCES.json +0 -130
data/vendor/AIMer/sign.h CHANGED
@@ -14,8 +14,8 @@
14
14
  typedef struct tape_t
15
15
  {
16
16
  gf pt_share;
17
- gf t_shares[AIMER_L];
18
- gf a_share;
17
+ gf y_shares[AIMER_L];
18
+ gf a_shares[AIMER_L + 1];
19
19
  gf c_share;
20
20
  } tape_t;
21
21
 
@@ -23,10 +23,10 @@ typedef struct proof_t
23
23
  {
24
24
  uint8_t reveal_path[AIMER_LOGN][AIMER_SEED_SIZE];
25
25
  uint8_t missing_commitment[AIMER_COMMIT_SIZE];
26
- uint8_t delta_pt_bytes[AIM2_NUM_BYTES_FIELD];
27
- uint8_t delta_ts_bytes[AIMER_L][AIM2_NUM_BYTES_FIELD];
28
- uint8_t delta_c_bytes[AIM2_NUM_BYTES_FIELD];
29
- uint8_t missing_alpha_share_bytes[AIM2_NUM_BYTES_FIELD];
26
+ uint8_t delta_pt_bytes[AIM3_NUM_BYTES_FIELD];
27
+ uint8_t delta_ys_bytes[AIMER_L][AIM3_NUM_BYTES_FIELD];
28
+ uint8_t delta_c_bytes[AIM3_NUM_BYTES_FIELD];
29
+ uint8_t missing_alpha_share_bytes[AIMER_L + 1][AIM3_NUM_BYTES_FIELD];
30
30
  } proof_t;
31
31
 
32
32
  typedef struct signature_t
data/vendor/AIMer/tree.c CHANGED
@@ -12,14 +12,17 @@ void expand_tree(uint8_t nodes[2 * AIMER_N - 1][AIMER_SEED_SIZE],
12
12
  {
13
13
  size_t node_index;
14
14
  hash_instance ctx;
15
+ uint8_t rep_byte = (uint8_t)rep_index;
15
16
 
16
17
  memcpy(nodes[0], seed, AIMER_SEED_SIZE);
17
18
  for (node_index = 1; node_index < AIMER_N; node_index++)
18
19
  {
20
+ uint8_t node_byte = (uint8_t)node_index;
21
+
19
22
  hash_init_prefix(&ctx, HASH_PREFIX_4);
20
23
  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));
24
+ hash_update(&ctx, &rep_byte, sizeof(rep_byte));
25
+ hash_update(&ctx, &node_byte, sizeof(node_byte));
23
26
  hash_update(&ctx, nodes[node_index - 1], AIMER_SEED_SIZE);
24
27
  hash_final(&ctx);
25
28
 
@@ -50,6 +53,7 @@ void reconstruct_tree(uint8_t nodes[2 * AIMER_N - 2][AIMER_SEED_SIZE],
50
53
  {
51
54
  size_t index, depth, path;
52
55
  hash_instance ctx;
56
+ uint8_t rep_byte = (uint8_t)rep_index;
53
57
 
54
58
  for (depth = 1; depth < AIMER_LOGN; depth++)
55
59
  {
@@ -58,10 +62,12 @@ void reconstruct_tree(uint8_t nodes[2 * AIMER_N - 2][AIMER_SEED_SIZE],
58
62
 
59
63
  for (index = (1U << depth); index < (2U << depth); index++)
60
64
  {
65
+ uint8_t index_byte = (uint8_t)index;
66
+
61
67
  hash_init_prefix(&ctx, HASH_PREFIX_4);
62
68
  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));
69
+ hash_update(&ctx, &rep_byte, sizeof(rep_byte));
70
+ hash_update(&ctx, &index_byte, sizeof(index_byte));
65
71
  hash_update(&ctx, nodes[index - 2], AIMER_SEED_SIZE);
66
72
  hash_final(&ctx);
67
73
 
data/vendor/AIMer/tree.h CHANGED
@@ -7,7 +7,7 @@
7
7
  #include <stddef.h>
8
8
  #include <stdint.h>
9
9
 
10
- #define expand_tree AIMER_NAMESPACE(expand_trees)
10
+ #define expand_tree AIMER_NAMESPACE(expand_tree)
11
11
  void expand_tree(uint8_t nodes[2 * AIMER_N - 1][AIMER_SEED_SIZE],
12
12
  const uint8_t salt[AIMER_SALT_SIZE], size_t rep_index,
13
13
  const uint8_t seed[AIMER_SEED_SIZE]);
@@ -32,29 +32,29 @@ void fixpoint_add(fp96_76 *xy, const fp96_76 *x, const fp96_76 *y);
32
32
 
33
33
  static inline void renormalize(fp96_76 *x) {
34
34
  x->limb48[1] += x->limb48[0] >> 48;
35
- x->limb48[0] &= (1ULL << 48) - 1;
35
+ x->limb48[0] &= (UINT64_C(1) << 48) - 1;
36
36
  }
37
37
 
38
38
  static inline int64_t smulh48(int64_t a, uint64_t b) {
39
- #ifndef __SIZEOF_INT128__
39
+ #ifdef __SIZEOF_INT128__
40
+ return (((int128)a * (int128)b) + ((int128)1 << 48) - 1) >> 48;
41
+ #else
40
42
  int64_t ah = a >> 24;
41
43
  int64_t al = a - (ah << 24);
42
44
  int64_t bl = b & ((1 << 24) - 1);
43
45
  int64_t bh = b >> 24;
44
46
 
45
- int64_t res = (al * bl) >> 24;
46
- res += al * bh + ah * bl + (1 << 23); // rounding
47
- res >>= 24;
47
+ int64_t res = (al * bl + ((1 << 24) - 1)) >> 24; // ceiling in low word
48
+ res += al * bh + ah * bl;
49
+ res = (res + ((1 << 24) - 1)) >> 24; // ceiling in high word
48
50
  return res + (ah * bh);
49
- #else
50
- return ((int128)a * (int128)b + (1ULL << 47)) >> 48; // rounding
51
51
  #endif
52
52
  }
53
53
 
54
54
  static inline void mul64(uint64_t r[2], const uint64_t b, const uint64_t a) {
55
55
  #ifndef __SIZEOF_INT128__
56
- uint64_t al = a & ((1ULL << 32) - 1), bl = b & ((1ULL << 32) - 1),
57
- ah = a >> 32, bh = b >> 32;
56
+ uint64_t al = a & ((UINT64_C(1) << 32) - 1),
57
+ bl = b & ((UINT64_C(1) << 32) - 1), ah = a >> 32, bh = b >> 32;
58
58
  r[0] = a * b;
59
59
  r[1] = ah * bl + al * bh + ((al * bl) >> 32);
60
60
  r[1] >>= 32;
@@ -68,7 +68,7 @@ static inline void mul64(uint64_t r[2], const uint64_t b, const uint64_t a) {
68
68
 
69
69
  static inline void sq64(uint64_t r[2], const uint64_t a) {
70
70
  #ifndef __SIZEOF_INT128__
71
- uint64_t al = a & ((1ULL << 32) - 1), ah = a >> 32;
71
+ uint64_t al = a & ((UINT64_C(1) << 32) - 1), ah = a >> 32;
72
72
  r[0] = a * a;
73
73
  r[1] = ah * al * 2 + ((al * al) >> 32);
74
74
  r[1] >>= 32;
@@ -84,7 +84,7 @@ static inline void mul48(uint64_t r[2], const uint64_t b, const uint64_t a) {
84
84
  mul64(r, b, a);
85
85
  r[1] <<= 16;
86
86
  r[1] ^= r[0] >> 48;
87
- r[0] &= (1ULL << 48) - 1;
87
+ r[0] &= (UINT64_C(1) << 48) - 1;
88
88
  }
89
89
 
90
90
  static inline void mulacc48(uint64_t r[2], const uint64_t b, const uint64_t a) {
@@ -100,7 +100,7 @@ static inline void sq48(uint64_t r[2], const uint64_t a) {
100
100
 
101
101
  r[1] <<= 16;
102
102
  r[1] ^= r[0] >> 48;
103
- r[0] &= (1ULL << 48) - 1;
103
+ r[0] &= (UINT64_C(1) << 48) - 1;
104
104
  }
105
105
 
106
106
  static inline void fixpoint_mul_high(fp96_76 *xy, const fp96_76 *x,
@@ -112,9 +112,9 @@ static inline void fixpoint_mul_high(fp96_76 *xy, const fp96_76 *x,
112
112
  xy->limb48[1] += tmp[0];
113
113
 
114
114
  // shift right by 28, rounding
115
- xy->limb48[0] += 1UL << 27;
115
+ xy->limb48[0] += UINT64_C(1) << 27;
116
116
  xy->limb48[0] >>= 28;
117
- xy->limb48[0] += (xy->limb48[1] << 20) & ((1ULL << 48) - 1);
117
+ xy->limb48[0] += (xy->limb48[1] << 20) & ((UINT64_C(1) << 48) - 1);
118
118
  xy->limb48[1] >>= 28;
119
119
 
120
120
  xy->limb48[1] += tmp[1] << 20;
@@ -48,7 +48,8 @@
48
48
  // Between this and our byte-aligned emission, we use 31 (not 32!) bits.
49
49
  // This is done intentionally because exact reciprocals for 31-bit uints
50
50
  // fit in 32-bit uints: this permits some optimizations during encoding.
51
- #define RANS_BYTE_L (1u << 23) // lower bound of our normalization interval
51
+ #define RANS_BYTE_L \
52
+ (UINT32_C(1) << 23) // lower bound of our normalization interval
52
53
 
53
54
  // State for a rANS encoder. Yep, that's all there is to it.
54
55
  typedef uint32_t RansState;
@@ -123,7 +124,7 @@ static inline int RansDecInit(RansState *r, uint8_t **pptr) {
123
124
 
124
125
  // Returns the current cumulative frequency (map it to a symbol yourself!)
125
126
  static inline uint32_t RansDecGet(RansState *r, uint32_t scale_bits) {
126
- return *r & ((1u << scale_bits) - 1);
127
+ return *r & ((UINT32_C(1) << scale_bits) - 1);
127
128
  }
128
129
 
129
130
  // Advances in the bit stream by "popping" a single symbol with range start
@@ -132,7 +133,7 @@ static inline uint32_t RansDecGet(RansState *r, uint32_t scale_bits) {
132
133
  static inline void RansDecAdvance(RansState *r, uint8_t **pptr,
133
134
  const uint8_t *end, uint32_t start,
134
135
  uint32_t freq, uint32_t scale_bits) {
135
- uint32_t mask = (1u << scale_bits) - 1;
136
+ uint32_t mask = (UINT32_C(1) << scale_bits) - 1;
136
137
 
137
138
  // s, x = D(x)
138
139
  uint32_t x = *r;
@@ -176,8 +177,8 @@ typedef struct {
176
177
  static inline void RansEncSymbolInit(RansEncSymbol *s, uint32_t start,
177
178
  uint32_t freq, uint32_t scale_bits) {
178
179
  RansAssert(scale_bits <= 16);
179
- RansAssert(start <= (1u << scale_bits));
180
- RansAssert(freq <= (1u << scale_bits) - start);
180
+ RansAssert(start <= (UINT32_C(1) << scale_bits));
181
+ RansAssert(freq <= (UINT32_C(1) << scale_bits) - start);
181
182
 
182
183
  // Say M := 1 << scale_bits.
183
184
  //
@@ -225,17 +226,17 @@ static inline void RansEncSymbolInit(RansEncSymbol *s, uint32_t start,
225
226
  //
226
227
  // so we have start = bias + 1 - M, or equivalently
227
228
  // bias = start + M - 1.
228
- s->rcp_freq = ~0u;
229
+ s->rcp_freq = ~UINT32_C(0);
229
230
  s->rcp_shift = 0;
230
231
  s->bias = start + (1 << scale_bits) - 1;
231
232
  } else {
232
233
  // Alverson, "Integer Division using reciprocals"
233
234
  // shift=ceil(log2(freq))
234
235
  uint32_t shift = 0;
235
- while (freq > (1u << shift))
236
+ while (freq > (UINT32_C(1) << shift))
236
237
  shift++;
237
238
 
238
- s->rcp_freq = (uint32_t)(((1ull << (shift + 31)) + freq - 1) / freq);
239
+ s->rcp_freq = (uint32_t)(((UINT64_C(1) << (shift + 31)) + freq - 1) / freq);
239
240
  s->rcp_shift = shift - 1;
240
241
 
241
242
  // With these values, 'q' is the correct quotient, so we
@@ -295,7 +296,7 @@ static inline void RansDecAdvanceSymbol(RansState *r, uint8_t **pptr,
295
296
  // scale_bits". No renormalization or output happens.
296
297
  static inline void RansDecAdvanceStep(RansState *r, uint32_t start,
297
298
  uint32_t freq, uint32_t scale_bits) {
298
- uint32_t mask = (1u << scale_bits) - 1;
299
+ uint32_t mask = (UINT32_C(1) << scale_bits) - 1;
299
300
 
300
301
  // s, x = D(x)
301
302
  uint32_t x = *r;
@@ -9,14 +9,18 @@
9
9
  * Name: decompose_z1
10
10
  *
11
11
  * Description: For finite field element r, compute high and lowbits
12
- * hb, lb such that r = hb * b + lb with -b/4 < lb <= b/4.
12
+ * hb, lb such that r = hb * b + lb with -b/2 <= lb < b/2,
13
+ * for b = alpha = 256.
13
14
  *
14
15
  * Arguments: - int32_t r: input element
15
16
  * - int32_t *lowbits: pointer to output element lb
16
17
  * - int32_t *highbits: pointer to output element hb
18
+ *
19
+ * Specification: Implements @[Algorithm 20, HighBits] and
20
+ * @[Algorithm 21, LowBits] with alpha = 256
17
21
  **************************************************/
18
22
  void decompose_z1(int32_t *highbits, int32_t *lowbits, const int32_t r) {
19
- const int alpha = 256; // Algorithm parameter.
23
+ const int alpha = 256; // TODO magic numbers!
20
24
  const int log_alpha = 8;
21
25
 
22
26
  int32_t lb, center;
@@ -38,7 +42,7 @@ void decompose_z1(int32_t *highbits, int32_t *lowbits, const int32_t r) {
38
42
  * Arguments: - int32_t r: input element
39
43
  * - int32_t *highbits: pointer to output element hb
40
44
  *
41
- * Specification: Implements Algorithm 20, DecomposeHint.
45
+ * Specification: Implements @[Algorithm 22, DecomposeHint]
42
46
  **************************************************/
43
47
 
44
48
  void decompose_hint(int32_t *highbits, const int32_t r) {
@@ -64,7 +68,7 @@ void decompose_hint(int32_t *highbits, const int32_t r) {
64
68
  *
65
69
  * Returns: - a1
66
70
  *
67
- * Specification: Implements Algorithm 17, DecomposeVK.
71
+ * Specification: Implements @[Algorithm 19, DecomposeVK]
68
72
  **************************************************/
69
73
  int32_t decompose_vk(int32_t *a0, const int32_t a) {
70
74
  *a0 = a & 1;
@@ -9,7 +9,7 @@
9
9
  #include <string.h>
10
10
 
11
11
  #define SCALE_BITS 10
12
- #define SCALE (1u << SCALE_BITS)
12
+ #define SCALE (UINT32_C(1) << SCALE_BITS)
13
13
 
14
14
  #if HAETAE_MODE == HAETAE_MODE2
15
15
  #define M_H 13
@@ -522,7 +522,7 @@ static uint16_t symbol_hb_z1[SCALE] = {
522
522
  * Arguments: - uint8_t *buf: pointer to output buffer
523
523
  * - const int32_t *h: pointer to polynomial vector h
524
524
  *
525
- * Specification: Implements Algorithm 38, EncodeH.
525
+ * Specification: Implements @[Algorithm 44, EncodeH]
526
526
  **************************************************/
527
527
  uint16_t encode_h(uint8_t *buf, const int32_t *h) {
528
528
  size_t size_encoded;
@@ -570,7 +570,7 @@ uint16_t encode_h(uint8_t *buf, const int32_t *h) {
570
570
  * Arguments: - int32_t *h: pointer to polynomial vector h
571
571
  * - uint8_t *buf: pointer to output buffer
572
572
  *
573
- * Specification: Implements Algorithm 39, DecodeH.
573
+ * Specification: Implements @[Algorithm 45, DecodeH]
574
574
  **************************************************/
575
575
  uint16_t decode_h(int32_t *h, const uint8_t *buf, uint16_t size_in) {
576
576
  size_t size_used;
@@ -616,7 +616,7 @@ uint16_t decode_h(int32_t *h, const uint8_t *buf, uint16_t size_in) {
616
616
  * Arguments: - uint8_t *buf: pointer to output buffer
617
617
  * - const int32_t *hb_z1: pointer to polynomial vector
618
618
  *
619
- * Specification: Implements Algorithm 40, EncodeHBz1.
619
+ * Specification: Implements @[Algorithm 46, EncodeHBz1]
620
620
  **************************************************/
621
621
  uint16_t encode_hb_z1(uint8_t *buf, const int32_t *hb_z1) {
622
622
  size_t size_encoded;
@@ -661,7 +661,7 @@ uint16_t encode_hb_z1(uint8_t *buf, const int32_t *hb_z1) {
661
661
  * Arguments: - int32_t *hb_z1: pointer to polynomial vector HighBits(z1)
662
662
  * - uint8_t *buf: pointer to output buffer
663
663
  *
664
- * Specification: Implements Algorithm 41, DecodeHBz1.
664
+ * Specification: Implements @[Algorithm 47, DecodeHBz1]
665
665
  **************************************************/
666
666
  uint16_t decode_hb_z1(int32_t *hb_z1, const uint8_t *buf, uint16_t size_in) {
667
667
  size_t size_used;
@@ -208,7 +208,7 @@ int32_t complex_fp_sqabs(complex_fp32_16 x) {
208
208
  *
209
209
  * Arguments: - complex_fp32_16 data[FFT_N]
210
210
  *
211
- * Specification: Implements Algorithm 43, FFT.
211
+ * Specification: Implements @[Algorithm 49, FFT]
212
212
  **************************************************/
213
213
  void fft(complex_fp32_16 data[FFT_N]) {
214
214
  unsigned int r, m, md2, n, k, even, odd, twid;
@@ -48,18 +48,30 @@ static void store64(uint8_t x[8], uint64_t u) {
48
48
 
49
49
  /* Keccak round constants */
50
50
  const uint64_t KeccakF_RoundConstants[NROUNDS] = {
51
- (uint64_t)0x0000000000000001ULL, (uint64_t)0x0000000000008082ULL,
52
- (uint64_t)0x800000000000808aULL, (uint64_t)0x8000000080008000ULL,
53
- (uint64_t)0x000000000000808bULL, (uint64_t)0x0000000080000001ULL,
54
- (uint64_t)0x8000000080008081ULL, (uint64_t)0x8000000000008009ULL,
55
- (uint64_t)0x000000000000008aULL, (uint64_t)0x0000000000000088ULL,
56
- (uint64_t)0x0000000080008009ULL, (uint64_t)0x000000008000000aULL,
57
- (uint64_t)0x000000008000808bULL, (uint64_t)0x800000000000008bULL,
58
- (uint64_t)0x8000000000008089ULL, (uint64_t)0x8000000000008003ULL,
59
- (uint64_t)0x8000000000008002ULL, (uint64_t)0x8000000000000080ULL,
60
- (uint64_t)0x000000000000800aULL, (uint64_t)0x800000008000000aULL,
61
- (uint64_t)0x8000000080008081ULL, (uint64_t)0x8000000000008080ULL,
62
- (uint64_t)0x0000000080000001ULL, (uint64_t)0x8000000080008008ULL};
51
+ (uint64_t)UINT64_C(0x0000000000000001),
52
+ (uint64_t)UINT64_C(0x0000000000008082),
53
+ (uint64_t)UINT64_C(0x800000000000808a),
54
+ (uint64_t)UINT64_C(0x8000000080008000),
55
+ (uint64_t)UINT64_C(0x000000000000808b),
56
+ (uint64_t)UINT64_C(0x0000000080000001),
57
+ (uint64_t)UINT64_C(0x8000000080008081),
58
+ (uint64_t)UINT64_C(0x8000000000008009),
59
+ (uint64_t)UINT64_C(0x000000000000008a),
60
+ (uint64_t)UINT64_C(0x0000000000000088),
61
+ (uint64_t)UINT64_C(0x0000000080008009),
62
+ (uint64_t)UINT64_C(0x000000008000000a),
63
+ (uint64_t)UINT64_C(0x000000008000808b),
64
+ (uint64_t)UINT64_C(0x800000000000008b),
65
+ (uint64_t)UINT64_C(0x8000000000008089),
66
+ (uint64_t)UINT64_C(0x8000000000008003),
67
+ (uint64_t)UINT64_C(0x8000000000008002),
68
+ (uint64_t)UINT64_C(0x8000000000000080),
69
+ (uint64_t)UINT64_C(0x000000000000800a),
70
+ (uint64_t)UINT64_C(0x800000008000000a),
71
+ (uint64_t)UINT64_C(0x8000000080008081),
72
+ (uint64_t)UINT64_C(0x8000000000008080),
73
+ (uint64_t)UINT64_C(0x0000000080000001),
74
+ (uint64_t)UINT64_C(0x8000000080008008)};
63
75
 
64
76
  /*************************************************
65
77
  * Name: KeccakF1600_StatePermute
@@ -389,7 +401,7 @@ static unsigned int keccak_absorb(uint64_t s[25], unsigned int pos,
389
401
  static void keccak_finalize(uint64_t s[25], unsigned int pos, unsigned int r,
390
402
  uint8_t p) {
391
403
  s[pos / 8] ^= (uint64_t)p << 8 * (pos % 8);
392
- s[r / 8 - 1] ^= 1ULL << 63;
404
+ s[r / 8 - 1] ^= UINT64_C(1) << 63;
393
405
  }
394
406
 
395
407
  /*************************************************
@@ -458,7 +470,7 @@ static void keccak_absorb_once(uint64_t s[25], unsigned int r,
458
470
  s[i / 8] ^= (uint64_t)in[i] << 8 * (i % 8);
459
471
 
460
472
  s[i / 8] ^= (uint64_t)p << 8 * (i % 8);
461
- s[(r - 1) / 8] ^= 1ULL << 63;
473
+ s[(r - 1) / 8] ^= UINT64_C(1) << 63;
462
474
  }
463
475
 
464
476
  /*************************************************
@@ -7,14 +7,14 @@
7
7
  #include <stdlib.h>
8
8
 
9
9
  static void __cneg(fp96_76 *x, const uint8_t sign) {
10
- x->limb48[0] ^= (-(int64_t)sign) & ((1ULL << 48) - 1);
10
+ x->limb48[0] ^= (-(int64_t)sign) & ((UINT64_C(1) << 48) - 1);
11
11
  x->limb48[1] ^= -(int64_t)sign;
12
12
  x->limb48[0] += sign;
13
13
  renormalize(x);
14
14
  }
15
15
 
16
16
  static void __copy_cneg(fp96_76 *y, const fp96_76 *x, const uint8_t sign) {
17
- y->limb48[0] = ((-(int64_t)sign) & ((1ULL << 48) - 1)) ^ x->limb48[0];
17
+ y->limb48[0] = ((-(int64_t)sign) & ((UINT64_C(1) << 48) - 1)) ^ x->limb48[0];
18
18
  ;
19
19
  y->limb48[1] = x->limb48[1] ^ (-(int64_t)sign);
20
20
  y->limb48[0] += sign;
@@ -34,13 +34,13 @@ static void fixpoint_mul(fp96_76 *xy, const fp96_76 *x, const fp96_76 *y) {
34
34
  mulacc48(&xy->limb48[0], x->limb48[1], y->limb48[0]);
35
35
 
36
36
  // shift right by 28, rounding
37
- xy->limb48[0] += 1UL << 27;
37
+ xy->limb48[0] += UINT64_C(1) << 27;
38
38
  xy->limb48[0] >>= 28;
39
- xy->limb48[0] += (xy->limb48[1] << 20) & ((1ULL << 48) - 1);
39
+ xy->limb48[0] += (xy->limb48[1] << 20) & ((UINT64_C(1) << 48) - 1);
40
40
  xy->limb48[1] >>= 28;
41
41
 
42
42
  mul64(tmp, x->limb48[1], y->limb48[1]);
43
- xy->limb48[0] += (tmp[0] << 20) & ((1ULL << 48) - 1);
43
+ xy->limb48[0] += (tmp[0] << 20) & ((UINT64_C(1) << 48) - 1);
44
44
  xy->limb48[1] += (tmp[0] >> 28) + (tmp[1] << 36);
45
45
 
46
46
  renormalize(xy);
@@ -62,7 +62,7 @@ static void fixpoint_sub(fp96_76 *xminy, const fp96_76 *x, const fp96_76 *y) {
62
62
 
63
63
  static void fixpoint_sub_from_threehalves(fp96_76 *x) {
64
64
  __cneg(x, 1);
65
- x->limb48[1] += 3ULL << 27; // left shift by 28 would be "3"
65
+ x->limb48[1] += UINT64_C(3) << 27; // left shift by 28 would be "3"
66
66
  renormalize(x);
67
67
  }
68
68
 
@@ -71,7 +71,7 @@ void fixpoint_square(fp96_76 *sqx, const fp96_76 *x) {
71
71
  sq48(&sqx->limb48[0], x->limb48[0]);
72
72
 
73
73
  // shift right by 48, rounding
74
- // sqx->limb48[0] += 1ULL << 47;
74
+ // sqx->limb48[0] += UINT64_C(1) << 47;
75
75
  sqx->limb48[0] >>= 48;
76
76
  sqx->limb48[0] += sqx->limb48[1];
77
77
 
@@ -81,13 +81,13 @@ void fixpoint_square(fp96_76 *sqx, const fp96_76 *x) {
81
81
  sqx->limb48[1] = tmp[1] << 1;
82
82
 
83
83
  // shift right by 28, rounding
84
- // sqx->limb48[0] += 1ULL << 27;
84
+ // sqx->limb48[0] += UINT64_C(1) << 27;
85
85
  sqx->limb48[0] >>= 28;
86
- sqx->limb48[0] += (sqx->limb48[1] << 20) & ((1ULL << 48) - 1);
86
+ sqx->limb48[0] += (sqx->limb48[1] << 20) & ((UINT64_C(1) << 48) - 1);
87
87
  sqx->limb48[1] >>= 28;
88
88
 
89
89
  sq64(tmp, x->limb48[1]);
90
- sqx->limb48[0] += (tmp[0] << 20) & ((1ULL << 48) - 1);
90
+ sqx->limb48[0] += (tmp[0] << 20) & ((UINT64_C(1) << 48) - 1);
91
91
  sqx->limb48[1] += (tmp[0] >> 28) + (tmp[1] << 36);
92
92
 
93
93
  renormalize(sqx);
@@ -96,17 +96,20 @@ void fixpoint_square(fp96_76 *sqx, const fp96_76 *x) {
96
96
  // start_cube = hex(round(2^64/(sqrt((K + L)*N + 2)^3)))
97
97
  // start_times_threehalfs = hex(round(2^64 * 2/(3 * sqrt((K + L)*N + 2))))
98
98
  #if HAETAE_MODE == HAETAE_MODE2
99
- const fp96_76 start_cube = {.limb48 = {0x770077e2e41aULL, 0x1162ULL}};
99
+ const fp96_76 start_cube = {
100
+ .limb48 = {UINT64_C(0x770077e2e41a), UINT64_C(0x1162)}};
100
101
  const fp96_76 start_times_threehalves = {
101
- .limb48 = {0x693861ad937bULL, 0x9caa56ULL}};
102
+ .limb48 = {UINT64_C(0x693861ad937b), UINT64_C(0x9caa56)}};
102
103
  #elif HAETAE_MODE == HAETAE_MODE3
103
- const fp96_76 start_cube = {.limb48 = {0x1a2935cfae68ULL, 0x978ULL}};
104
+ const fp96_76 start_cube = {
105
+ .limb48 = {UINT64_C(0x1a2935cfae68), UINT64_C(0x978)}};
104
106
  const fp96_76 start_times_threehalves = {
105
- .limb48 = {0x7ad215218533ULL, 0x7ff1c9ULL}};
107
+ .limb48 = {UINT64_C(0x7ad215218533), UINT64_C(0x7ff1c9)}};
106
108
  #elif HAETAE_MODE == HAETAE_MODE5
107
- const fp96_76 start_cube = {.limb48 = {0x700ff3e8890dULL, 0x702ULL}};
109
+ const fp96_76 start_cube = {
110
+ .limb48 = {UINT64_C(0x700ff3e8890d), UINT64_C(0x702)}};
108
111
  const fp96_76 start_times_threehalves = {
109
- .limb48 = {0x5768588eed31ULL, 0x73bd40ULL}};
112
+ .limb48 = {UINT64_C(0x5768588eed31), UINT64_C(0x73bd40)}};
110
113
  #endif
111
114
 
112
115
  // implements Newton's method
@@ -131,9 +134,9 @@ int32_t fixpoint_mul_rnd13(const uint64_t x, const fp96_76 *y,
131
134
  int64_t res;
132
135
  fp96_76 tmp, xx;
133
136
  xx.limb48[1] = x >> 32;
134
- xx.limb48[0] = (x & ((1ULL << 32) - 1)) << 16;
137
+ xx.limb48[0] = (x & ((UINT64_C(1) << 32) - 1)) << 16;
135
138
  fixpoint_mul(&tmp, &xx, y);
136
- res = (tmp.limb48[1] + (1UL << 14)) >> 15; // rounding
139
+ res = (tmp.limb48[1] + (UINT64_C(1) << 14)) >> 15; // rounding
137
140
  return (1 - 2 * (int32_t)sign) * res;
138
141
  }
139
142
 
@@ -45,6 +45,8 @@ static const int32_t zetas[HAETAE_N] = {
45
45
  * order.
46
46
  *
47
47
  * Arguments: - uint32_t p[HAETAE_N]: input/output coefficient array
48
+ *
49
+ * Specification: Implements @[Algorithm 2, NTT]
48
50
  **************************************************/
49
51
  void ntt(int32_t a[HAETAE_N]) {
50
52
  unsigned int len, start, j, k;
@@ -19,7 +19,7 @@
19
19
  * containg b
20
20
  * - const uint8_t seed[]: seed for A'
21
21
  *
22
- * Specification: Implements Algorithm 21, PackVK.
22
+ * Specification: Implements @[Algorithm 23, PackVK]
23
23
  **************************************************/
24
24
  void pack_vk(uint8_t vk[HAETAE_CRYPTO_PUBLICKEYBYTES], polyveck *b,
25
25
  const uint8_t seed[HAETAE_SEEDBYTES]) {
@@ -42,7 +42,7 @@ void pack_vk(uint8_t vk[HAETAE_CRYPTO_PUBLICKEYBYTES], polyveck *b,
42
42
  * - polyveck *b: polynomial vector of length HAETAE_K containg b
43
43
  * - const uint8_t vk[]: output byte array
44
44
  *
45
- * Specification: Implements Algorithm 22, UnpackVK.
45
+ * Specification: Implements @[Algorithm 24, UnpackVK]
46
46
  **************************************************/
47
47
  void unpack_vk(polyvecl A[HAETAE_K],
48
48
  const uint8_t vk[HAETAE_CRYPTO_PUBLICKEYBYTES]) {
@@ -84,7 +84,7 @@ void unpack_vk(polyvecl A[HAETAE_K],
84
84
  * starting at offset 1)
85
85
  * - const polyveck *s1: polyveck pointer containing s1
86
86
  *
87
- * Specification: Implements Algorithm 23, PackSK.
87
+ * Specification: Implements @[Algorithm 25, PackSK]
88
88
  **************************************************/
89
89
  void pack_sk(uint8_t sk[HAETAE_CRYPTO_SECRETKEYBYTES],
90
90
  const uint8_t vk[HAETAE_CRYPTO_PUBLICKEYBYTES], const polyvecm *s0,
@@ -121,7 +121,7 @@ void pack_sk(uint8_t sk[HAETAE_CRYPTO_SECRETKEYBYTES],
121
121
  * - polyveck s1: output polyveck pointer for s1
122
122
  * - const uint8_t sk[]: byte array containing bit-packed sk
123
123
  *
124
- * Specification: Implements Algorithm 24, UnpackSK.
124
+ * Specification: Implements @[Algorithm 26, UnPackSK]
125
125
  **************************************************/
126
126
  void unpack_sk(polyvecl A[HAETAE_K], polyvecm *s0, polyveck *s1, uint8_t *key,
127
127
  const uint8_t sk[HAETAE_CRYPTO_SECRETKEYBYTES]) {
@@ -162,7 +162,7 @@ void unpack_sk(polyvecl A[HAETAE_K], polyvecm *s0, polyveck *s1, uint8_t *key,
162
162
  * - const polyveck *h: pointer t vector h of length HAETAE_K
163
163
  * Returns 1 in case the signature packing failed; otherwise 0.
164
164
  *
165
- * Specification: Implements Algorithm 25, PackSig.
165
+ * Specification: Implements @[Algorithm 27, PackSIG]
166
166
  **************************************************/
167
167
  int pack_sig(uint8_t sig[HAETAE_CRYPTO_BYTES], const poly *c,
168
168
  const polyvecl *lowbits_z1, const polyvecl *highbits_z1,
@@ -237,7 +237,7 @@ int pack_sig(uint8_t sig[HAETAE_CRYPTO_BYTES], const poly *c,
237
237
  *
238
238
  * Returns 1 in case of malformed signature; otherwise 0.
239
239
  *
240
- * Specification: Implements Algorithm 26, UnpackSig.
240
+ * Specification: Implements @[Algorithm 28, UnpackSIG]
241
241
  **************************************************/
242
242
  int unpack_sig(poly *c, polyvecl *lowbits_z1, polyvecl *highbits_z1,
243
243
  polyveck *h, const uint8_t sig[HAETAE_CRYPTO_BYTES]) {
@@ -293,7 +293,7 @@ int unpack_sig(poly *c, polyvecl *lowbits_z1, polyvecl *highbits_z1,
293
293
  * HAETAE_POLYQ_PACKEDBYTES bytes
294
294
  * - const poly *a: pointer to input polynomial
295
295
  *
296
- * Specification: Implements Algorithm 27, PackPolyQ.
296
+ * Specification: Implements @[Algorithm 29, PackPolyQ]
297
297
  **************************************************/
298
298
  void pack_poly_q(uint8_t *r, const poly *a) {
299
299
  unsigned int i;
@@ -343,7 +343,7 @@ void pack_poly_q(uint8_t *r, const poly *a) {
343
343
  * Arguments: - poly *r: pointer to output polynomial
344
344
  * - const uint8_t *a: byte array with bit-packed polynomial
345
345
  *
346
- * Specification: Implements Algorithm 28, UnpackPolyQ.
346
+ * Specification: Implements @[Algorithm 30, UnpackPolyQ]
347
347
  **************************************************/
348
348
  void unpack_poly_q(poly *r, const uint8_t *a) {
349
349
  unsigned int i;
@@ -395,7 +395,7 @@ void unpack_poly_q(poly *r, const uint8_t *a) {
395
395
  * HAETAE_POLYETA_PACKEDBYTES bytes
396
396
  * - const poly *a: pointer to input polynomial
397
397
  *
398
- * Specification: Implements Algorithm 29, PackPolyEta.
398
+ * Specification: Implements @[Algorithm 31, PackPolyEta]
399
399
  **************************************************/
400
400
  void pack_poly_eta(uint8_t *r, const poly *a) {
401
401
  unsigned int i;
@@ -421,7 +421,7 @@ void pack_poly_eta(uint8_t *r, const poly *a) {
421
421
  * Arguments: - poly *r: pointer to output polynomial
422
422
  * - const uint8_t *a: byte array with bit-packed polynomial
423
423
  *
424
- * Specification: Implements Algorithm 30, UnpackPolyEta.
424
+ * Specification: Implements @[Algorithm 32, UnpackPolyEta]
425
425
  **************************************************/
426
426
  void unpack_poly_eta(poly *r, const uint8_t *a) {
427
427
  unsigned int i;
@@ -454,7 +454,7 @@ void unpack_poly_eta(poly *r, const uint8_t *a) {
454
454
  * HAETAE_POLYETA_PACKEDBYTES bytes
455
455
  * - const poly *a: pointer to input polynomial
456
456
  *
457
- * Specification: Implements Algorithm 31, PackPoly2Eta.
457
+ * Specification: Implements @[Algorithm 33, PackPoly2Eta]
458
458
  **************************************************/
459
459
  void pack_poly2_eta(uint8_t *r, const poly *a) {
460
460
  unsigned int i;
@@ -483,7 +483,7 @@ void pack_poly2_eta(uint8_t *r, const poly *a) {
483
483
  * Arguments: - poly *r: pointer to output polynomial
484
484
  * - const uint8_t *a: byte array with bit-packed polynomial
485
485
  *
486
- * Specification: Implements Algorithm 32, UnpackPoly2Eta.
486
+ * Specification: Implements @[Algorithm 34, UnpackPoly2Eta]
487
487
  **************************************************/
488
488
  void unpack_poly2_eta(poly *r, const uint8_t *a) {
489
489
  unsigned int i;
@@ -516,7 +516,7 @@ void unpack_poly2_eta(poly *r, const uint8_t *a) {
516
516
  * Arguments: - uint8_t *buf: pointer to output bytes array
517
517
  * - const polyveck *v: a vector of polynomials
518
518
  *
519
- * Specification: Implements Algorithm 33, PackVecHighBits.
519
+ * Specification: Implements @[Algorithm 35, PackVecHighBits]
520
520
  **************************************************/
521
521
  void pack_vec_highbits(uint8_t *buf, const polyveck *v) {
522
522
  unsigned int i;
@@ -533,7 +533,7 @@ void pack_vec_highbits(uint8_t *buf, const polyveck *v) {
533
533
  * Arguments: - uint8_t *buf: pointer to output bytes array
534
534
  * - const poly *v: a polynomial
535
535
  *
536
- * Specification: Implements Algorithm 34, PackPolyHighBits.
536
+ * Specification: Implements @[Algorithm 36, PackPolyHighBits]
537
537
  **************************************************/
538
538
  void pack_poly_highbits(uint8_t *buf, const poly *a) {
539
539
  unsigned int i;
@@ -582,7 +582,7 @@ void pack_poly_highbits(uint8_t *buf, const poly *a) {
582
582
  * Arguments: - uint8_t *buf: pointer to output bytes array
583
583
  * - const poly *a: a polynomial
584
584
  *
585
- * Specification: Implements Algorithm 35, PackPolyLsb.
585
+ * Specification: Implements @[Algorithm 37, PackPolyLsb]
586
586
  **************************************************/
587
587
  void pack_poly_lsb(uint8_t *buf, const poly *a) {
588
588
  unsigned int i;