mldsa_gh 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 (128) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +88 -0
  4. data/ext/mldsa_gh_native/extconf.rb +14 -0
  5. data/ext/mldsa_gh_native/mldsa_gh_native.c +222 -0
  6. data/ext/mldsa_gh_native/mldsa_gh_native_all.c +24 -0
  7. data/ext/mldsa_gh_native/mldsa_gh_native_all.h +26 -0
  8. data/ext/mldsa_gh_native/vendor/mldsa-native/BUILDING.md +108 -0
  9. data/ext/mldsa_gh_native/vendor/mldsa-native/LICENSE +305 -0
  10. data/ext/mldsa_gh_native/vendor/mldsa-native/README.md +247 -0
  11. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/README.md +23 -0
  12. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/mldsa_native.c +803 -0
  13. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/mldsa_native.h +956 -0
  14. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/mldsa_native_asm.S +830 -0
  15. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/mldsa_native_config.h +855 -0
  16. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/cbmc.h +233 -0
  17. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/common.h +301 -0
  18. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/context.h +152 -0
  19. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/ct.c +21 -0
  20. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/ct.h +373 -0
  21. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/debug.c +75 -0
  22. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/debug.h +125 -0
  23. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/fips202.c +270 -0
  24. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/fips202.h +224 -0
  25. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/fips202x4.c +187 -0
  26. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/fips202x4.h +125 -0
  27. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/keccakf1600.c +510 -0
  28. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/keccakf1600.h +110 -0
  29. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/auto.h +85 -0
  30. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/fips202_native_aarch64.h +69 -0
  31. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x1_scalar_aarch64_asm.S +378 -0
  32. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x1_v84a_aarch64_asm.S +207 -0
  33. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x2_v84a_aarch64_asm.S +262 -0
  34. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x4_v8a_scalar_hybrid_aarch64_asm.S +1080 -0
  35. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x4_v8a_v84a_scalar_hybrid_aarch64_asm.S +990 -0
  36. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccakf1600_round_constants.c +47 -0
  37. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/x1_scalar.h +27 -0
  38. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/x1_v84a.h +36 -0
  39. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/x2_v84a.h +40 -0
  40. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/x4_v8a_scalar.h +32 -0
  41. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/x4_v8a_v84a_scalar.h +37 -0
  42. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/native/api.h +129 -0
  43. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/README.md +10 -0
  44. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/mve.h +67 -0
  45. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/src/fips202_native_armv81m.h +37 -0
  46. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/src/keccak_f1600_x4_mve.S +717 -0
  47. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/src/keccak_f1600_x4_mve.c +42 -0
  48. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/src/keccak_f1600_x4_state_extract_bytes_mve.S +334 -0
  49. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/src/keccak_f1600_x4_state_xor_bytes_mve.S +355 -0
  50. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/src/keccakf1600_round_constants.c +53 -0
  51. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/native/auto.h +35 -0
  52. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/native/x86_64/keccak_f1600_x4_avx2.h +34 -0
  53. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/native/x86_64/src/fips202_native_x86_64.h +45 -0
  54. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/native/x86_64/src/keccak_f1600_x4_avx2_asm.S +488 -0
  55. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/fips202/native/x86_64/src/keccakf1600_constants.c +52 -0
  56. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/aarch64/meta.h +314 -0
  57. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/aarch64/src/aarch64_zetas.c +248 -0
  58. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/aarch64/src/arith_native_aarch64.h +367 -0
  59. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/aarch64/src/mldsa_intt_aarch64_asm.S +786 -0
  60. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/aarch64/src/mldsa_ntt_aarch64_asm.S +686 -0
  61. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/aarch64/src/mldsa_pointwise_montgomery_aarch64_asm.S +106 -0
  62. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/aarch64/src/mldsa_poly_caddq_aarch64_asm.S +69 -0
  63. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/aarch64/src/mldsa_poly_chknorm_aarch64_asm.S +76 -0
  64. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/aarch64/src/mldsa_poly_decompose_32_aarch64_asm.S +108 -0
  65. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/aarch64/src/mldsa_poly_decompose_88_aarch64_asm.S +108 -0
  66. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/aarch64/src/mldsa_poly_use_hint_32_aarch64_asm.S +125 -0
  67. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/aarch64/src/mldsa_poly_use_hint_88_aarch64_asm.S +133 -0
  68. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/aarch64/src/mldsa_polyvecl_pointwise_acc_montgomery_l4_aarch64_asm.S +157 -0
  69. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/aarch64/src/mldsa_polyvecl_pointwise_acc_montgomery_l5_aarch64_asm.S +173 -0
  70. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/aarch64/src/mldsa_polyvecl_pointwise_acc_montgomery_l7_aarch64_asm.S +205 -0
  71. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/aarch64/src/mldsa_polyz_unpack_17_aarch64_asm.S +103 -0
  72. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/aarch64/src/mldsa_polyz_unpack_19_aarch64_asm.S +100 -0
  73. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/aarch64/src/mldsa_rej_uniform_aarch64_asm.S +222 -0
  74. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/aarch64/src/mldsa_rej_uniform_eta2_aarch64_asm.S +170 -0
  75. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/aarch64/src/mldsa_rej_uniform_eta4_aarch64_asm.S +163 -0
  76. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/aarch64/src/polyz_unpack_table.c +52 -0
  77. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/aarch64/src/rej_uniform_eta_table.c +547 -0
  78. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/aarch64/src/rej_uniform_table.c +63 -0
  79. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/api.h +617 -0
  80. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/meta.h +24 -0
  81. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/x86_64/meta.h +323 -0
  82. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/x86_64/src/arith_native_x86_64.h +330 -0
  83. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/x86_64/src/consts.c +157 -0
  84. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/x86_64/src/consts.h +27 -0
  85. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/x86_64/src/mldsa_intt_avx2_asm.S +2333 -0
  86. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/x86_64/src/mldsa_ntt_avx2_asm.S +2405 -0
  87. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/x86_64/src/mldsa_nttunpack_avx2_asm.S +254 -0
  88. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/x86_64/src/mldsa_pointwise_acc_l4_avx2_asm.S +173 -0
  89. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/x86_64/src/mldsa_pointwise_acc_l5_avx2_asm.S +189 -0
  90. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/x86_64/src/mldsa_pointwise_acc_l7_avx2_asm.S +221 -0
  91. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/x86_64/src/mldsa_pointwise_avx2_asm.S +158 -0
  92. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/x86_64/src/mldsa_poly_caddq_avx2_asm.S +199 -0
  93. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/x86_64/src/mldsa_poly_chknorm_avx2_asm.S +176 -0
  94. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/x86_64/src/mldsa_poly_decompose_32_avx2_asm.S +490 -0
  95. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/x86_64/src/mldsa_poly_decompose_88_avx2_asm.S +489 -0
  96. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/x86_64/src/mldsa_poly_use_hint_32_avx2_asm.S +123 -0
  97. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/x86_64/src/mldsa_poly_use_hint_88_avx2_asm.S +125 -0
  98. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/x86_64/src/mldsa_polyz_unpack_17_avx2_asm.S +355 -0
  99. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/x86_64/src/mldsa_polyz_unpack_19_avx2_asm.S +355 -0
  100. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/x86_64/src/mldsa_rej_uniform_avx2_asm.S +132 -0
  101. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/x86_64/src/mldsa_rej_uniform_eta2_avx2_asm.S +205 -0
  102. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/x86_64/src/mldsa_rej_uniform_eta4_avx2_asm.S +176 -0
  103. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/native/x86_64/src/rej_uniform_table.c +161 -0
  104. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/packing.c +213 -0
  105. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/packing.h +277 -0
  106. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/params.h +153 -0
  107. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/poly.c +1066 -0
  108. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/poly.h +464 -0
  109. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/poly_kl.c +910 -0
  110. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/poly_kl.h +367 -0
  111. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/polyvec.c +509 -0
  112. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/polyvec.h +435 -0
  113. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/polyvec_lazy.c +311 -0
  114. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/polyvec_lazy.h +652 -0
  115. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/randombytes.h +26 -0
  116. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/reduce.h +144 -0
  117. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/rounding.h +265 -0
  118. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/sign.c +1720 -0
  119. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/sign.h +850 -0
  120. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/symmetric.h +68 -0
  121. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/sys.h +327 -0
  122. data/ext/mldsa_gh_native/vendor/mldsa-native/mldsa/src/zetas.inc +55 -0
  123. data/lib/mldsa/parameter_set.rb +82 -0
  124. data/lib/mldsa/signing_key.rb +147 -0
  125. data/lib/mldsa/verify_key.rb +72 -0
  126. data/lib/mldsa/version.rb +7 -0
  127. data/lib/mldsa_gh.rb +93 -0
  128. metadata +225 -0
@@ -0,0 +1,367 @@
1
+ /*
2
+ * Copyright (c) The mldsa-native project authors
3
+ * SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
4
+ */
5
+
6
+ /* References
7
+ * ==========
8
+ *
9
+ * - [FIPS204]
10
+ * FIPS 204 Module-Lattice-Based Digital Signature Standard
11
+ * National Institute of Standards and Technology
12
+ * https://csrc.nist.gov/pubs/fips/204/final
13
+ */
14
+
15
+ #ifndef MLD_POLY_KL_H
16
+ #define MLD_POLY_KL_H
17
+
18
+ #include "cbmc.h"
19
+ #include "common.h"
20
+ #include "poly.h"
21
+
22
+ #if !defined(MLD_CONFIG_NO_SIGN_API)
23
+ #define mld_poly_decompose MLD_NAMESPACE_KL(poly_decompose)
24
+ /**
25
+ * For all coefficients c of the input polynomial, compute high and low bits
26
+ * c0, c1 such c mod MLDSA_Q = c1*ALPHA + c0 with -ALPHA/2 < c0 <= ALPHA/2
27
+ * except c1 = (MLDSA_Q-1)/ALPHA where we set c1 = 0 and
28
+ * -ALPHA/2 <= c0 = c mod MLDSA_Q - MLDSA_Q < 0. Assumes coefficients to be
29
+ * standard representatives.
30
+ *
31
+ * @reference{The reference implementation has the input polynomial as a
32
+ * separate argument that may be aliased with either of the outputs. Removing
33
+ * the aliasing eases CBMC proofs.}
34
+ *
35
+ * @param[out] a1 Pointer to output polynomial with coefficients c1.
36
+ * @param[in,out] a0 Pointer to input/output polynomial. Output polynomial has
37
+ * coefficients c0.
38
+ */
39
+ MLD_INTERNAL_API
40
+ void mld_poly_decompose(mld_poly *a1, mld_poly *a0)
41
+ __contract__(
42
+ requires(memory_no_alias(a1, sizeof(mld_poly)))
43
+ requires(memory_no_alias(a0, sizeof(mld_poly)))
44
+ requires(array_bound(a0->coeffs, 0, MLDSA_N, 0, MLDSA_Q))
45
+ assigns(memory_slice(a1, sizeof(mld_poly)))
46
+ assigns(memory_slice(a0, sizeof(mld_poly)))
47
+ ensures(array_bound(a1->coeffs, 0, MLDSA_N, 0, (MLDSA_Q-1)/(2*MLDSA_GAMMA2)))
48
+ ensures(array_abs_bound(a0->coeffs, 0, MLDSA_N, MLDSA_GAMMA2+1))
49
+ );
50
+
51
+ #endif /* !MLD_CONFIG_NO_SIGN_API */
52
+
53
+ #if !defined(MLD_CONFIG_NO_VERIFY_API)
54
+ #define mld_poly_use_hint MLD_NAMESPACE_KL(poly_use_hint)
55
+ /**
56
+ * Use hint polynomial h to correct the high bits of a in-place.
57
+ *
58
+ * @param[in,out] a Input/output polynomial.
59
+ * @param[in] h Hint polynomial.
60
+ */
61
+ MLD_INTERNAL_API
62
+ void mld_poly_use_hint(mld_poly *a, const mld_poly *h)
63
+ __contract__(
64
+ requires(memory_no_alias(a, sizeof(mld_poly)))
65
+ requires(memory_no_alias(h, sizeof(mld_poly)))
66
+ requires(array_bound(a->coeffs, 0, MLDSA_N, 0, MLDSA_Q))
67
+ requires(array_bound(h->coeffs, 0, MLDSA_N, 0, 2))
68
+ assigns(memory_slice(a, sizeof(mld_poly)))
69
+ ensures(array_bound(a->coeffs, 0, MLDSA_N, 0, (MLDSA_Q-1)/(2*MLDSA_GAMMA2)))
70
+ );
71
+ #endif /* !MLD_CONFIG_NO_VERIFY_API */
72
+
73
+ #if !defined(MLD_CONFIG_NO_KEYPAIR_API)
74
+ #if !defined(MLD_CONFIG_SERIAL_FIPS202_ONLY)
75
+ #define mld_poly_uniform_eta_4x MLD_NAMESPACE_KL(poly_uniform_eta_4x)
76
+ /**
77
+ * Sample four polynomials with uniformly random coefficients in
78
+ * [-MLDSA_ETA, MLDSA_ETA] by performing rejection sampling on the output
79
+ * stream from SHAKE256(seed|nonce_i).
80
+ *
81
+ * @spec{Implements @[FIPS204, Algorithm 31, RejBoundedPoly] (four-way
82
+ * batched).}
83
+ *
84
+ * @param[out] r0 Pointer to first output polynomial.
85
+ * @param[out] r1 Pointer to second output polynomial.
86
+ * @param[out] r2 Pointer to third output polynomial.
87
+ * @param[out] r3 Pointer to fourth output polynomial.
88
+ * @param[in] seed Byte array with seed of length MLDSA_CRHBYTES.
89
+ * @param nonce0 First nonce.
90
+ * @param nonce1 Second nonce.
91
+ * @param nonce2 Third nonce.
92
+ * @param nonce3 Fourth nonce.
93
+ */
94
+ MLD_INTERNAL_API
95
+ void mld_poly_uniform_eta_4x(mld_poly *r0, mld_poly *r1, mld_poly *r2,
96
+ mld_poly *r3, const uint8_t seed[MLDSA_CRHBYTES],
97
+ uint8_t nonce0, uint8_t nonce1, uint8_t nonce2,
98
+ uint8_t nonce3)
99
+ __contract__(
100
+ requires(memory_no_alias(r0, sizeof(mld_poly)))
101
+ requires(memory_no_alias(r1, sizeof(mld_poly)))
102
+ requires(memory_no_alias(r2, sizeof(mld_poly)))
103
+ requires(memory_no_alias(r3, sizeof(mld_poly)))
104
+ requires(memory_no_alias(seed, MLDSA_CRHBYTES))
105
+ assigns(memory_slice(r0, sizeof(mld_poly)))
106
+ assigns(memory_slice(r1, sizeof(mld_poly)))
107
+ assigns(memory_slice(r2, sizeof(mld_poly)))
108
+ assigns(memory_slice(r3, sizeof(mld_poly)))
109
+ ensures(array_abs_bound(r0->coeffs, 0, MLDSA_N, MLDSA_ETA + 1))
110
+ ensures(array_abs_bound(r1->coeffs, 0, MLDSA_N, MLDSA_ETA + 1))
111
+ ensures(array_abs_bound(r2->coeffs, 0, MLDSA_N, MLDSA_ETA + 1))
112
+ ensures(array_abs_bound(r3->coeffs, 0, MLDSA_N, MLDSA_ETA + 1))
113
+ );
114
+ #endif /* !MLD_CONFIG_SERIAL_FIPS202_ONLY */
115
+
116
+ #if defined(MLD_CONFIG_SERIAL_FIPS202_ONLY)
117
+ #define mld_poly_uniform_eta MLD_NAMESPACE_KL(poly_uniform_eta)
118
+ /**
119
+ * Sample polynomial with uniformly random coefficients in
120
+ * [-MLDSA_ETA, MLDSA_ETA] by performing rejection sampling on the output
121
+ * stream from SHAKE256(seed|nonce).
122
+ *
123
+ * @spec{Implements @[FIPS204, Algorithm 31, RejBoundedPoly].}
124
+ *
125
+ * @param[out] r Pointer to output polynomial.
126
+ * @param[in] seed Byte array with seed of length MLDSA_CRHBYTES.
127
+ * @param nonce Nonce.
128
+ */
129
+ MLD_INTERNAL_API
130
+ void mld_poly_uniform_eta(mld_poly *r, const uint8_t seed[MLDSA_CRHBYTES],
131
+ uint8_t nonce)
132
+ __contract__(
133
+ requires(memory_no_alias(r, sizeof(mld_poly)))
134
+ requires(memory_no_alias(seed, MLDSA_CRHBYTES))
135
+ assigns(memory_slice(r, sizeof(mld_poly)))
136
+ ensures(array_abs_bound(r->coeffs, 0, MLDSA_N, MLDSA_ETA + 1))
137
+ );
138
+ #endif /* MLD_CONFIG_SERIAL_FIPS202_ONLY */
139
+ #endif /* !MLD_CONFIG_NO_KEYPAIR_API */
140
+
141
+ #if !defined(MLD_CONFIG_NO_SIGN_API)
142
+ #if MLD_CONFIG_PARAMETER_SET == 65 || \
143
+ defined(MLD_CONFIG_SERIAL_FIPS202_ONLY) || \
144
+ defined(MLD_CONFIG_REDUCE_RAM) || defined(MLD_UNIT_TEST)
145
+ #define mld_poly_uniform_gamma1 MLD_NAMESPACE_KL(poly_uniform_gamma1)
146
+ /**
147
+ * Sample polynomial with uniformly random coefficients in
148
+ * [-(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1] by unpacking output stream of
149
+ * SHAKE256(seed|nonce).
150
+ *
151
+ * @spec{Partially implements @[FIPS204, Algorithm 34, ExpandMask] (one
152
+ * polynomial, i.e. the loop body of lines 3-5).}
153
+ *
154
+ * @param[out] a Pointer to output polynomial.
155
+ * @param[in] seed Byte array with seed of length MLDSA_CRHBYTES.
156
+ * @param nonce 16-bit nonce.
157
+ */
158
+ MLD_INTERNAL_API
159
+ void mld_poly_uniform_gamma1(mld_poly *a, const uint8_t seed[MLDSA_CRHBYTES],
160
+ uint16_t nonce)
161
+ __contract__(
162
+ requires(memory_no_alias(a, sizeof(mld_poly)))
163
+ requires(memory_no_alias(seed, MLDSA_CRHBYTES))
164
+ assigns(memory_slice(a, sizeof(mld_poly)))
165
+ ensures(array_bound(a->coeffs, 0, MLDSA_N, -(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1 + 1))
166
+ );
167
+ #endif /* MLD_CONFIG_PARAMETER_SET == 65 || MLD_CONFIG_SERIAL_FIPS202_ONLY || \
168
+ MLD_CONFIG_REDUCE_RAM || MLD_UNIT_TEST */
169
+
170
+ #if !defined(MLD_CONFIG_SERIAL_FIPS202_ONLY) && \
171
+ (!defined(MLD_CONFIG_REDUCE_RAM) || defined(MLD_UNIT_TEST))
172
+ #define mld_poly_uniform_gamma1_4x MLD_NAMESPACE_KL(poly_uniform_gamma1_4x)
173
+ /**
174
+ * Sample four polynomials with uniformly random coefficients in
175
+ * [-(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1] by unpacking output streams of
176
+ * SHAKE256(seed|nonce_i).
177
+ *
178
+ * @spec{Partially implements @[FIPS204, Algorithm 34, ExpandMask] (four-way
179
+ * batched, i.e. four iterations of the loop body of lines 3-5).}
180
+ *
181
+ * @param[out] r0 Pointer to first output polynomial.
182
+ * @param[out] r1 Pointer to second output polynomial.
183
+ * @param[out] r2 Pointer to third output polynomial.
184
+ * @param[out] r3 Pointer to fourth output polynomial.
185
+ * @param[in] seed Byte array with seed of length MLDSA_CRHBYTES.
186
+ * @param nonce0 First 16-bit nonce.
187
+ * @param nonce1 Second 16-bit nonce.
188
+ * @param nonce2 Third 16-bit nonce.
189
+ * @param nonce3 Fourth 16-bit nonce.
190
+ */
191
+ MLD_INTERNAL_API
192
+ void mld_poly_uniform_gamma1_4x(mld_poly *r0, mld_poly *r1, mld_poly *r2,
193
+ mld_poly *r3,
194
+ const uint8_t seed[MLDSA_CRHBYTES],
195
+ uint16_t nonce0, uint16_t nonce1,
196
+ uint16_t nonce2, uint16_t nonce3)
197
+ __contract__(
198
+ requires(memory_no_alias(r0, sizeof(mld_poly)))
199
+ requires(memory_no_alias(r1, sizeof(mld_poly)))
200
+ requires(memory_no_alias(r2, sizeof(mld_poly)))
201
+ requires(memory_no_alias(r3, sizeof(mld_poly)))
202
+ requires(memory_no_alias(seed, MLDSA_CRHBYTES))
203
+ assigns(memory_slice(r0, sizeof(mld_poly)))
204
+ assigns(memory_slice(r1, sizeof(mld_poly)))
205
+ assigns(memory_slice(r2, sizeof(mld_poly)))
206
+ assigns(memory_slice(r3, sizeof(mld_poly)))
207
+ ensures(array_bound(r0->coeffs, 0, MLDSA_N, -(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1 + 1))
208
+ ensures(array_bound(r1->coeffs, 0, MLDSA_N, -(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1 + 1))
209
+ ensures(array_bound(r2->coeffs, 0, MLDSA_N, -(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1 + 1))
210
+ ensures(array_bound(r3->coeffs, 0, MLDSA_N, -(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1 + 1))
211
+ );
212
+ #endif /* !MLD_CONFIG_SERIAL_FIPS202_ONLY && (!MLD_CONFIG_REDUCE_RAM || \
213
+ MLD_UNIT_TEST) */
214
+ #endif /* !MLD_CONFIG_NO_SIGN_API */
215
+
216
+ #if !defined(MLD_CONFIG_NO_SIGN_API) || !defined(MLD_CONFIG_NO_VERIFY_API)
217
+ #define mld_poly_challenge MLD_NAMESPACE_KL(poly_challenge)
218
+ /**
219
+ * Samples polynomial with MLDSA_TAU nonzero coefficients in {-1, 1} using the
220
+ * output stream of SHAKE256(seed).
221
+ *
222
+ * @spec{Implements @[FIPS204, Algorithm 29, SampleInBall].}
223
+ *
224
+ * @param[out] c Pointer to output polynomial.
225
+ * @param[in] seed Byte array containing seed of length MLDSA_CTILDEBYTES.
226
+ */
227
+ MLD_INTERNAL_API
228
+ void mld_poly_challenge(mld_poly *c, const uint8_t seed[MLDSA_CTILDEBYTES])
229
+ __contract__(
230
+ requires(memory_no_alias(c, sizeof(mld_poly)))
231
+ requires(memory_no_alias(seed, MLDSA_CTILDEBYTES))
232
+ assigns(memory_slice(c, sizeof(mld_poly)))
233
+ /* All coefficients of c are -1, 0 or +1 */
234
+ ensures(array_bound(c->coeffs, 0, MLDSA_N, -1, 2))
235
+ );
236
+ #endif /* !MLD_CONFIG_NO_SIGN_API || !MLD_CONFIG_NO_VERIFY_API */
237
+
238
+ #if !defined(MLD_CONFIG_NO_KEYPAIR_API)
239
+ #define mld_polyeta_pack MLD_NAMESPACE_KL(polyeta_pack)
240
+ /**
241
+ * Bit-pack polynomial with coefficients in [-MLDSA_ETA, MLDSA_ETA].
242
+ *
243
+ * @spec{Implements @[FIPS204, Algorithm 17, BitPack].}
244
+ *
245
+ * @param[out] r Pointer to output byte array with at least
246
+ * MLDSA_POLYETA_PACKEDBYTES bytes.
247
+ * @param[in] a Pointer to input polynomial.
248
+ */
249
+ MLD_INTERNAL_API
250
+ void mld_polyeta_pack(uint8_t r[MLDSA_POLYETA_PACKEDBYTES], const mld_poly *a)
251
+ __contract__(
252
+ requires(memory_no_alias(r, MLDSA_POLYETA_PACKEDBYTES))
253
+ requires(memory_no_alias(a, sizeof(mld_poly)))
254
+ requires(array_abs_bound(a->coeffs, 0, MLDSA_N, MLDSA_ETA + 1))
255
+ assigns(memory_slice(r, MLDSA_POLYETA_PACKEDBYTES))
256
+ );
257
+ #endif /* !MLD_CONFIG_NO_KEYPAIR_API */
258
+
259
+ #if !defined(MLD_CONFIG_NO_KEYPAIR_API) || !defined(MLD_CONFIG_NO_SIGN_API)
260
+ /*
261
+ * polyeta_unpack produces coefficients in [-MLDSA_ETA, MLDSA_ETA] for
262
+ * well-formed inputs (i.e., those produced by polyeta_pack).
263
+ * However, when passed an arbitrary byte array, it may produce smaller values,
264
+ * i.e., values in [MLD_POLYETA_UNPACK_LOWER_BOUND, MLDSA_ETA].
265
+ * Even though this should never happen, we use use the bound for arbitrary
266
+ * inputs in the CBMC proofs.
267
+ */
268
+ #if MLDSA_ETA == 2
269
+ #define MLD_POLYETA_UNPACK_LOWER_BOUND (-5)
270
+ #elif MLDSA_ETA == 4
271
+ #define MLD_POLYETA_UNPACK_LOWER_BOUND (-11)
272
+ #else
273
+ #error "Invalid value of MLDSA_ETA"
274
+ #endif
275
+
276
+ #define mld_polyeta_unpack MLD_NAMESPACE_KL(polyeta_unpack)
277
+ /**
278
+ * Unpack polynomial with coefficients in [-MLDSA_ETA, MLDSA_ETA].
279
+ *
280
+ * @spec{Implements @[FIPS204, Algorithm 19, BitUnpack].}
281
+ *
282
+ * @param[out] r Pointer to output polynomial.
283
+ * @param[in] a Byte array with bit-packed polynomial.
284
+ */
285
+ MLD_INTERNAL_API
286
+ void mld_polyeta_unpack(mld_poly *r, const uint8_t a[MLDSA_POLYETA_PACKEDBYTES])
287
+ __contract__(
288
+ requires(memory_no_alias(r, sizeof(mld_poly)))
289
+ requires(memory_no_alias(a, MLDSA_POLYETA_PACKEDBYTES))
290
+ assigns(memory_slice(r, sizeof(mld_poly)))
291
+ ensures(array_bound(r->coeffs, 0, MLDSA_N, MLD_POLYETA_UNPACK_LOWER_BOUND, MLDSA_ETA + 1))
292
+ );
293
+ #endif /* !MLD_CONFIG_NO_KEYPAIR_API || !MLD_CONFIG_NO_SIGN_API */
294
+
295
+ #if !defined(MLD_CONFIG_NO_SIGN_API)
296
+ #define mld_polyz_pack MLD_NAMESPACE_KL(polyz_pack)
297
+ /**
298
+ * Bit-pack polynomial with coefficients in
299
+ * [-(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1].
300
+ *
301
+ * @spec{Implements @[FIPS204, Algorithm 17, BitPack].}
302
+ *
303
+ * @param[out] r Pointer to output byte array with at least
304
+ * MLDSA_POLYZ_PACKEDBYTES bytes.
305
+ * @param[in] a Pointer to input polynomial.
306
+ */
307
+ MLD_INTERNAL_API
308
+ void mld_polyz_pack(uint8_t r[MLDSA_POLYZ_PACKEDBYTES], const mld_poly *a)
309
+ __contract__(
310
+ requires(memory_no_alias(r, MLDSA_POLYZ_PACKEDBYTES))
311
+ requires(memory_no_alias(a, sizeof(mld_poly)))
312
+ requires(array_bound(a->coeffs, 0, MLDSA_N, -(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1 + 1))
313
+ assigns(memory_slice(r, MLDSA_POLYZ_PACKEDBYTES))
314
+ );
315
+ #endif /* !MLD_CONFIG_NO_SIGN_API */
316
+
317
+ #if !defined(MLD_CONFIG_NO_SIGN_API) || !defined(MLD_CONFIG_NO_VERIFY_API)
318
+ #define mld_polyz_unpack MLD_NAMESPACE_KL(polyz_unpack)
319
+ /**
320
+ * Unpack polynomial z with coefficients in
321
+ * [-(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1].
322
+ *
323
+ * @spec{Implements @[FIPS204, Algorithm 19, BitUnpack].}
324
+ *
325
+ * @param[out] r Pointer to output polynomial.
326
+ * @param[in] a Byte array with bit-packed polynomial.
327
+ */
328
+ MLD_INTERNAL_API
329
+ void mld_polyz_unpack(mld_poly *r, const uint8_t a[MLDSA_POLYZ_PACKEDBYTES])
330
+ __contract__(
331
+ requires(memory_no_alias(r, sizeof(mld_poly)))
332
+ requires(memory_no_alias(a, MLDSA_POLYZ_PACKEDBYTES))
333
+ assigns(memory_slice(r, sizeof(mld_poly)))
334
+ ensures(array_bound(r->coeffs, 0, MLDSA_N, -(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1 + 1))
335
+ );
336
+
337
+ #define mld_polyw1_pack MLD_NAMESPACE_KL(polyw1_pack)
338
+ /**
339
+ * Bit-pack polynomial w1. Input coefficients must be in
340
+ * [0, (MLDSA_Q-1)/(2*MLDSA_GAMMA2)), i.e. [0, 43] for ML-DSA-44 and [0, 15]
341
+ * for ML-DSA-65/87. Dispatches to the value-specialized variant for the
342
+ * selected parameter set.
343
+ *
344
+ * @spec{Implements @[FIPS204, Algorithm 16, SimpleBitPack].}
345
+ *
346
+ * @param[out] r Pointer to output byte array with at least
347
+ * MLDSA_POLYW1_PACKEDBYTES bytes.
348
+ * @param[in] a Pointer to input polynomial.
349
+ */
350
+ static MLD_INLINE void mld_polyw1_pack(uint8_t r[MLDSA_POLYW1_PACKEDBYTES],
351
+ const mld_poly *a)
352
+ __contract__(
353
+ requires(memory_no_alias(r, MLDSA_POLYW1_PACKEDBYTES))
354
+ requires(memory_no_alias(a, sizeof(mld_poly)))
355
+ requires(array_bound(a->coeffs, 0, MLDSA_N, 0, (MLDSA_Q-1)/(2*MLDSA_GAMMA2)))
356
+ assigns(memory_slice(r, MLDSA_POLYW1_PACKEDBYTES))
357
+ )
358
+ {
359
+ #if MLD_CONFIG_PARAMETER_SET == 44
360
+ mld_polyw1_pack_88(r, a);
361
+ #else
362
+ mld_polyw1_pack_32(r, a);
363
+ #endif
364
+ }
365
+ #endif /* !MLD_CONFIG_NO_SIGN_API || !MLD_CONFIG_NO_VERIFY_API */
366
+
367
+ #endif /* !MLD_POLY_KL_H */