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,464 @@
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_H
16
+ #define MLD_POLY_H
17
+
18
+ #include "cbmc.h"
19
+ #include "common.h"
20
+ #include "reduce.h"
21
+ #include "rounding.h"
22
+
23
+ /* Absolute exclusive upper bound for the output of fqmul */
24
+ #define MLD_FQMUL_BOUND ((5 * MLDSA_Q + 3) / 4)
25
+ /* Absolute exclusive upper bound for the output of the forward NTT */
26
+ #define MLD_NTT_BOUND (9 * MLD_FQMUL_BOUND)
27
+ /* Absolute exclusive upper bound for the output of the inverse NTT*/
28
+ #define MLD_INTT_BOUND MLDSA_Q
29
+
30
+ /**
31
+ * Element of R_q = Z_q[X]/(X^n + 1). Represents polynomial
32
+ * coeffs[0] + X*coeffs[1] + X^2*coeffs[2] + ... + X^{n-1}*coeffs[n-1].
33
+ */
34
+ typedef struct
35
+ {
36
+ int32_t coeffs[MLDSA_N]; /**< Polynomial coefficients. */
37
+ } MLD_ALIGN mld_poly;
38
+
39
+ #define mld_poly_reduce MLD_NAMESPACE(poly_reduce)
40
+ /**
41
+ * In-place reduction of all coefficients of polynomial to representative in
42
+ * [-MLD_REDUCE32_RANGE_MAX, MLD_REDUCE32_RANGE_MAX].
43
+ *
44
+ * @param[in,out] a Pointer to input/output polynomial.
45
+ */
46
+ MLD_INTERNAL_API
47
+ void mld_poly_reduce(mld_poly *a)
48
+ __contract__(
49
+ requires(memory_no_alias(a, sizeof(mld_poly)))
50
+ requires(array_bound(a->coeffs, 0, MLDSA_N, INT32_MIN, MLD_REDUCE32_DOMAIN_MAX))
51
+ assigns(memory_slice(a, sizeof(mld_poly)))
52
+ ensures(array_bound(a->coeffs, 0, MLDSA_N, -MLD_REDUCE32_RANGE_MAX, MLD_REDUCE32_RANGE_MAX))
53
+ );
54
+
55
+ #define mld_poly_caddq MLD_NAMESPACE(poly_caddq)
56
+ /**
57
+ * For all coefficients of in/out polynomial add MLDSA_Q if coefficient is
58
+ * negative.
59
+ *
60
+ * @param[in,out] a Pointer to input/output polynomial.
61
+ */
62
+ MLD_INTERNAL_API
63
+ void mld_poly_caddq(mld_poly *a)
64
+ __contract__(
65
+ requires(memory_no_alias(a, sizeof(mld_poly)))
66
+ requires(array_abs_bound(a->coeffs, 0, MLDSA_N, MLDSA_Q))
67
+ assigns(memory_slice(a, sizeof(mld_poly)))
68
+ ensures(array_bound(a->coeffs, 0, MLDSA_N, 0, MLDSA_Q))
69
+ );
70
+
71
+ #if !defined(MLD_CONFIG_NO_KEYPAIR_API) || !defined(MLD_CONFIG_NO_SIGN_API) || \
72
+ defined(MLD_CONFIG_REDUCE_RAM) || defined(MLD_UNIT_TEST)
73
+ #define mld_poly_add MLD_NAMESPACE(poly_add)
74
+ /**
75
+ * Add polynomials. No modular reduction is performed.
76
+ *
77
+ * @spec{Implements @[FIPS204, Algorithm 44, AddNTT] (coefficientwise
78
+ * polynomial addition; also used for addition in the normal domain).}
79
+ *
80
+ * @param[in,out] r Pointer to input-output polynomial to be added to.
81
+ * @param[in] b Pointer to input polynomial that should be added to r.
82
+ * Must be disjoint from r.
83
+ */
84
+
85
+ /*
86
+ * NOTE: The reference implementation uses a 3-argument poly_add.
87
+ * We specialize to the accumulator form to avoid reasoning about aliasing.
88
+ */
89
+ MLD_INTERNAL_API
90
+ void mld_poly_add(mld_poly *r, const mld_poly *b)
91
+ __contract__(
92
+ requires(memory_no_alias(b, sizeof(mld_poly)))
93
+ requires(memory_no_alias(r, sizeof(mld_poly)))
94
+ requires(forall(k0, 0, MLDSA_N, (int64_t) r->coeffs[k0] + b->coeffs[k0] < MLD_REDUCE32_DOMAIN_MAX))
95
+ requires(forall(k1, 0, MLDSA_N, (int64_t) r->coeffs[k1] + b->coeffs[k1] >= INT32_MIN))
96
+ assigns(memory_slice(r, sizeof(mld_poly)))
97
+ ensures(forall(k2, 0, MLDSA_N, r->coeffs[k2] == old(*r).coeffs[k2] + b->coeffs[k2]))
98
+ ensures(forall(k3, 0, MLDSA_N, r->coeffs[k3] < MLD_REDUCE32_DOMAIN_MAX))
99
+ ensures(forall(k4, 0, MLDSA_N, r->coeffs[k4] >= INT32_MIN))
100
+ );
101
+ #endif /* !MLD_CONFIG_NO_KEYPAIR_API || !MLD_CONFIG_NO_SIGN_API || \
102
+ MLD_CONFIG_REDUCE_RAM || MLD_UNIT_TEST */
103
+
104
+ #if !defined(MLD_CONFIG_NO_SIGN_API) || !defined(MLD_CONFIG_NO_VERIFY_API)
105
+ #define mld_poly_sub MLD_NAMESPACE(poly_sub)
106
+ /**
107
+ * Subtract polynomials. No modular reduction is performed.
108
+ *
109
+ * @param[in,out] r Pointer to input-output polynomial.
110
+ * @param[in] b Pointer to input polynomial that should be subtracted from
111
+ * r. Must be disjoint from r.
112
+ */
113
+ /*
114
+ * NOTE: The reference implementation uses a 3-argument poly_sub.
115
+ * We specialize to the accumulator form to avoid reasoning about aliasing.
116
+ */
117
+ MLD_INTERNAL_API
118
+ void mld_poly_sub(mld_poly *r, const mld_poly *b)
119
+ __contract__(
120
+ requires(memory_no_alias(b, sizeof(mld_poly)))
121
+ requires(memory_no_alias(r, sizeof(mld_poly)))
122
+ requires(array_abs_bound(r->coeffs, 0, MLDSA_N, MLDSA_Q))
123
+ requires(array_abs_bound(b->coeffs, 0, MLDSA_N, MLDSA_Q))
124
+ assigns(memory_slice(r, sizeof(mld_poly)))
125
+ ensures(array_bound(r->coeffs, 0, MLDSA_N, INT32_MIN, MLD_REDUCE32_DOMAIN_MAX))
126
+ );
127
+ #endif /* !MLD_CONFIG_NO_SIGN_API || !MLD_CONFIG_NO_VERIFY_API */
128
+
129
+ #if !defined(MLD_CONFIG_NO_VERIFY_API)
130
+ #define mld_poly_shiftl MLD_NAMESPACE(poly_shiftl)
131
+ /**
132
+ * Multiply polynomial by 2^MLDSA_D without modular reduction. Assumes input
133
+ * coefficients to be less than 2^{31-MLDSA_D} in absolute value.
134
+ *
135
+ * @param[in,out] a Pointer to input/output polynomial.
136
+ */
137
+ MLD_INTERNAL_API
138
+ void mld_poly_shiftl(mld_poly *a)
139
+ __contract__(
140
+ requires(memory_no_alias(a, sizeof(mld_poly)))
141
+ requires(array_bound(a->coeffs, 0, MLDSA_N, 0, 1 << 10))
142
+ assigns(memory_slice(a, sizeof(mld_poly)))
143
+ ensures(array_bound(a->coeffs, 0, MLDSA_N, 0, MLDSA_Q))
144
+ );
145
+ #endif /* !MLD_CONFIG_NO_VERIFY_API */
146
+
147
+ #define mld_poly_ntt MLD_NAMESPACE(poly_ntt)
148
+ /**
149
+ * In-place forward NTT. Output coefficients are bounded by MLD_NTT_BOUND in
150
+ * absolute value.
151
+ *
152
+ * @spec{Implements @[FIPS204, Algorithm 41, NTT].}
153
+ *
154
+ * @param[in,out] a Pointer to input/output polynomial.
155
+ */
156
+ MLD_INTERNAL_API
157
+ void mld_poly_ntt(mld_poly *a)
158
+ __contract__(
159
+ requires(memory_no_alias(a, sizeof(mld_poly)))
160
+ requires(array_abs_bound(a->coeffs, 0, MLDSA_N, MLDSA_Q))
161
+ assigns(memory_slice(a, sizeof(mld_poly)))
162
+ ensures(array_abs_bound(a->coeffs, 0, MLDSA_N, MLD_NTT_BOUND))
163
+ );
164
+
165
+
166
+ #define mld_poly_invntt_tomont MLD_NAMESPACE(poly_invntt_tomont)
167
+ /**
168
+ * In-place inverse NTT.
169
+ *
170
+ * Input coefficients need to be less than MLDSA_Q in absolute value and
171
+ * output coefficients are bounded by MLD_INTT_BOUND.
172
+ *
173
+ * @spec{Implements @[FIPS204, Algorithm 42, NTT^{-1}] up to scaling:
174
+ * The input is scaled by 2^{-32} as a result of the Montgomery base
175
+ * multiplication. The output is in normal domain. In other words, this
176
+ * function implements `NTT^{-1} o mult(2^32)`.}
177
+ *
178
+ * @param[in,out] a Pointer to input/output polynomial.
179
+ */
180
+ MLD_INTERNAL_API
181
+ void mld_poly_invntt_tomont(mld_poly *a)
182
+ __contract__(
183
+ requires(memory_no_alias(a, sizeof(mld_poly)))
184
+ requires(array_abs_bound(a->coeffs, 0, MLDSA_N, MLDSA_Q))
185
+ assigns(memory_slice(a, sizeof(mld_poly)))
186
+ ensures(array_abs_bound(a->coeffs, 0, MLDSA_N, MLD_INTT_BOUND))
187
+ );
188
+
189
+ #if !defined(MLD_CONFIG_NO_SIGN_API) || !defined(MLD_CONFIG_NO_VERIFY_API) || \
190
+ defined(MLD_CONFIG_REDUCE_RAM) || defined(MLD_UNIT_TEST)
191
+ #define mld_poly_pointwise_montgomery MLD_NAMESPACE(poly_pointwise_montgomery)
192
+ /**
193
+ * Pointwise multiplication of polynomials. Destructive in the first argument.
194
+ *
195
+ * @spec{Implements @[FIPS204, Algorithm 45, MultiplyNTT], up to scaling: The
196
+ * input is in normal domain, the output is scaled by 2^{-32} as a result of
197
+ * the use of Montgomery multiplication. In other words, this function
198
+ * implements `mult(2^{-32}) o MultiplyNTT`.}
199
+ *
200
+ * @param[in,out] a Pointer to first input/output polynomial. On entry, holds
201
+ * the first multiplicand; on exit, holds the product
202
+ * a * b * 2^{-32}.
203
+ * @param[in] b Pointer to second input polynomial.
204
+ */
205
+ MLD_INTERNAL_API
206
+ void mld_poly_pointwise_montgomery(mld_poly *a, const mld_poly *b)
207
+ __contract__(
208
+ requires(memory_no_alias(a, sizeof(mld_poly)))
209
+ requires(memory_no_alias(b, sizeof(mld_poly)))
210
+ requires(array_abs_bound(a->coeffs, 0, MLDSA_N, MLD_NTT_BOUND))
211
+ requires(array_abs_bound(b->coeffs, 0, MLDSA_N, MLD_NTT_BOUND))
212
+ assigns(memory_slice(a, sizeof(mld_poly)))
213
+ ensures(array_abs_bound(a->coeffs, 0, MLDSA_N, MLDSA_Q))
214
+ );
215
+ #endif /* !MLD_CONFIG_NO_SIGN_API || !MLD_CONFIG_NO_VERIFY_API || \
216
+ MLD_CONFIG_REDUCE_RAM || MLD_UNIT_TEST */
217
+
218
+ #if !defined(MLD_CONFIG_NO_KEYPAIR_API)
219
+ #define mld_poly_power2round MLD_NAMESPACE(poly_power2round)
220
+ /**
221
+ * For all coefficients c of the input polynomial, compute c0, c1 such that
222
+ * c mod MLDSA_Q = c1*2^MLDSA_D + c0 with -2^{MLDSA_D-1} < c0 <= 2^{MLDSA_D-1}.
223
+ * Assumes coefficients to be standard representatives.
224
+ *
225
+ * @param[out] a1 Pointer to output polynomial with coefficients c1.
226
+ * @param[out] a0 Pointer to output polynomial with coefficients c0; may alias
227
+ * the input polynomial a.
228
+ * @param[in] a Pointer to input polynomial.
229
+ */
230
+ MLD_INTERNAL_API
231
+ void mld_poly_power2round(mld_poly *a1, mld_poly *a0, const mld_poly *a)
232
+ __contract__(
233
+ requires(memory_no_alias(a0, sizeof(mld_poly)))
234
+ requires(memory_no_alias(a1, sizeof(mld_poly)))
235
+ /* The implementation does not require a0 == a, but the single call site
236
+ * aliases them and asserting equality simplifies the proof. */
237
+ requires(a0 == a)
238
+ requires(array_bound(a->coeffs, 0, MLDSA_N, 0, MLDSA_Q))
239
+ assigns(memory_slice(a1, sizeof(mld_poly)))
240
+ assigns(memory_slice(a0, sizeof(mld_poly)))
241
+ ensures(array_bound(a0->coeffs, 0, MLDSA_N, -(MLD_2_POW_D/2)+1, (MLD_2_POW_D/2)+1))
242
+ ensures(array_bound(a1->coeffs, 0, MLDSA_N, 0, ((MLDSA_Q - 1) / MLD_2_POW_D) + 1))
243
+ );
244
+ #endif /* !MLD_CONFIG_NO_KEYPAIR_API */
245
+
246
+ #define mld_poly_uniform MLD_NAMESPACE(poly_uniform)
247
+ /**
248
+ * Sample polynomial with uniformly random coefficients in [0, MLDSA_Q-1] by
249
+ * performing rejection sampling on the output stream of SHAKE128(seed|nonce).
250
+ *
251
+ * @spec{Implements @[FIPS204, Algorithm 30, RejNTTPoly].}
252
+ *
253
+ * @param[out] a Pointer to output polynomial.
254
+ * @param[in] seed Byte array with seed of length MLDSA_SEEDBYTES and the
255
+ * packed 2-byte nonce.
256
+ */
257
+ MLD_INTERNAL_API
258
+ void mld_poly_uniform(mld_poly *a, const uint8_t seed[MLDSA_SEEDBYTES + 2])
259
+ __contract__(
260
+ requires(memory_no_alias(a, sizeof(mld_poly)))
261
+ requires(memory_no_alias(seed, MLDSA_SEEDBYTES + 2))
262
+ assigns(memory_slice(a, sizeof(mld_poly)))
263
+ ensures(array_bound(a->coeffs, 0, MLDSA_N, 0, MLDSA_Q))
264
+ );
265
+
266
+ #if !defined(MLD_CONFIG_SERIAL_FIPS202_ONLY) && \
267
+ (!defined(MLD_CONFIG_REDUCE_RAM) || defined(MLD_UNIT_TEST))
268
+ #define mld_poly_uniform_4x MLD_NAMESPACE(poly_uniform_4x)
269
+ /**
270
+ * Generate four polynomials using rejection sampling on (pseudo-)uniformly
271
+ * random bytes sampled from a seed.
272
+ *
273
+ * @spec{Implements @[FIPS204, Algorithm 30, RejNTTPoly] (four-way batched).}
274
+ *
275
+ * @param[out] vec0 Pointer to first polynomial to be sampled.
276
+ * @param[out] vec1 Pointer to second polynomial to be sampled.
277
+ * @param[out] vec2 Pointer to third polynomial to be sampled.
278
+ * @param[out] vec3 Pointer to fourth polynomial to be sampled.
279
+ * @param[in] seed Pointer to consecutive array of seed buffers of size
280
+ * MLDSA_SEEDBYTES + 2 each, plus padding for alignment.
281
+ */
282
+ MLD_INTERNAL_API
283
+ void mld_poly_uniform_4x(mld_poly *vec0, mld_poly *vec1, mld_poly *vec2,
284
+ mld_poly *vec3,
285
+ uint8_t seed[4][MLD_ALIGN_UP(MLDSA_SEEDBYTES + 2)])
286
+ __contract__(
287
+ requires(memory_no_alias(vec0, sizeof(mld_poly)))
288
+ requires(memory_no_alias(vec1, sizeof(mld_poly)))
289
+ requires(memory_no_alias(vec2, sizeof(mld_poly)))
290
+ requires(memory_no_alias(vec3, sizeof(mld_poly)))
291
+ requires(memory_no_alias(seed, 4 * MLD_ALIGN_UP(MLDSA_SEEDBYTES + 2)))
292
+ assigns(memory_slice(vec0, sizeof(mld_poly)))
293
+ assigns(memory_slice(vec1, sizeof(mld_poly)))
294
+ assigns(memory_slice(vec2, sizeof(mld_poly)))
295
+ assigns(memory_slice(vec3, sizeof(mld_poly)))
296
+ ensures(array_bound(vec0->coeffs, 0, MLDSA_N, 0, MLDSA_Q))
297
+ ensures(array_bound(vec1->coeffs, 0, MLDSA_N, 0, MLDSA_Q))
298
+ ensures(array_bound(vec2->coeffs, 0, MLDSA_N, 0, MLDSA_Q))
299
+ ensures(array_bound(vec3->coeffs, 0, MLDSA_N, 0, MLDSA_Q))
300
+ );
301
+ #endif /* !MLD_CONFIG_SERIAL_FIPS202_ONLY && (!MLD_CONFIG_REDUCE_RAM || \
302
+ MLD_UNIT_TEST) */
303
+
304
+ #if !defined(MLD_CONFIG_NO_KEYPAIR_API)
305
+ #define mld_polyt1_pack MLD_NAMESPACE(polyt1_pack)
306
+ /**
307
+ * Bit-pack polynomial t1 with coefficients fitting in 10 bits. Input
308
+ * coefficients are assumed to be standard representatives.
309
+ *
310
+ * @spec{Implements @[FIPS204, Algorithm 16, SimpleBitPack].}
311
+ *
312
+ * @param[out] r Pointer to output byte array with at least
313
+ * MLDSA_POLYT1_PACKEDBYTES bytes.
314
+ * @param[in] a Pointer to input polynomial.
315
+ */
316
+ MLD_INTERNAL_API
317
+ void mld_polyt1_pack(uint8_t r[MLDSA_POLYT1_PACKEDBYTES], const mld_poly *a)
318
+ __contract__(
319
+ requires(memory_no_alias(r, MLDSA_POLYT1_PACKEDBYTES))
320
+ requires(memory_no_alias(a, sizeof(mld_poly)))
321
+ requires(array_bound(a->coeffs, 0, MLDSA_N, 0, 1 << 10))
322
+ assigns(memory_slice(r, MLDSA_POLYT1_PACKEDBYTES))
323
+ );
324
+ #endif /* !MLD_CONFIG_NO_KEYPAIR_API */
325
+
326
+ #if !defined(MLD_CONFIG_NO_VERIFY_API)
327
+ #define mld_polyt1_unpack MLD_NAMESPACE(polyt1_unpack)
328
+ /**
329
+ * Unpack polynomial t1 with 10-bit coefficients. Output coefficients are
330
+ * standard representatives.
331
+ *
332
+ * @spec{Implements @[FIPS204, Algorithm 18, SimpleBitUnpack].}
333
+ *
334
+ * @param[out] r Pointer to output polynomial.
335
+ * @param[in] a Byte array with bit-packed polynomial.
336
+ */
337
+ MLD_INTERNAL_API
338
+ void mld_polyt1_unpack(mld_poly *r, const uint8_t a[MLDSA_POLYT1_PACKEDBYTES])
339
+ __contract__(
340
+ requires(memory_no_alias(r, sizeof(mld_poly)))
341
+ requires(memory_no_alias(a, MLDSA_POLYT1_PACKEDBYTES))
342
+ assigns(memory_slice(r, sizeof(mld_poly)))
343
+ ensures(array_bound(r->coeffs, 0, MLDSA_N, 0, 1 << 10))
344
+ );
345
+ #endif /* !MLD_CONFIG_NO_VERIFY_API */
346
+
347
+ #if !defined(MLD_CONFIG_NO_KEYPAIR_API)
348
+ #define mld_polyt0_pack MLD_NAMESPACE(polyt0_pack)
349
+ /**
350
+ * Bit-pack polynomial t0 with coefficients in ]-2^{MLDSA_D-1}, 2^{MLDSA_D-1}].
351
+ *
352
+ * @spec{Implements @[FIPS204, Algorithm 17, BitPack].}
353
+ *
354
+ * @param[out] r Pointer to output byte array with at least
355
+ * MLDSA_POLYT0_PACKEDBYTES bytes.
356
+ * @param[in] a Pointer to input polynomial.
357
+ */
358
+ MLD_INTERNAL_API
359
+ void mld_polyt0_pack(uint8_t r[MLDSA_POLYT0_PACKEDBYTES], const mld_poly *a)
360
+ __contract__(
361
+ requires(memory_no_alias(r, MLDSA_POLYT0_PACKEDBYTES))
362
+ requires(memory_no_alias(a, sizeof(mld_poly)))
363
+ requires(array_bound(a->coeffs, 0, MLDSA_N, -(1<<(MLDSA_D-1)) + 1, (1<<(MLDSA_D-1)) + 1))
364
+ assigns(memory_slice(r, MLDSA_POLYT0_PACKEDBYTES))
365
+ );
366
+ #endif /* !MLD_CONFIG_NO_KEYPAIR_API */
367
+
368
+ #if !defined(MLD_CONFIG_NO_SIGN_API) || defined(MLD_UNIT_TEST)
369
+ #define mld_polyt0_unpack MLD_NAMESPACE(polyt0_unpack)
370
+ /**
371
+ * Unpack polynomial t0 with coefficients in ]-2^{MLDSA_D-1}, 2^{MLDSA_D-1}].
372
+ *
373
+ * @spec{Implements @[FIPS204, Algorithm 19, BitUnpack].}
374
+ *
375
+ * @param[out] r Pointer to output polynomial.
376
+ * @param[in] a Byte array with bit-packed polynomial.
377
+ */
378
+ MLD_INTERNAL_API
379
+ void mld_polyt0_unpack(mld_poly *r, const uint8_t a[MLDSA_POLYT0_PACKEDBYTES])
380
+ __contract__(
381
+ requires(memory_no_alias(r, sizeof(mld_poly)))
382
+ requires(memory_no_alias(a, MLDSA_POLYT0_PACKEDBYTES))
383
+ assigns(memory_slice(r, sizeof(mld_poly)))
384
+ ensures(array_bound(r->coeffs, 0, MLDSA_N, -(1<<(MLDSA_D-1)) + 1, (1<<(MLDSA_D-1)) + 1))
385
+ );
386
+ #endif /* !MLD_CONFIG_NO_SIGN_API || MLD_UNIT_TEST */
387
+
388
+ #define mld_poly_chknorm MLD_NAMESPACE(poly_chknorm)
389
+ /**
390
+ * Check infinity norm of polynomial against given bound. Assumes input
391
+ * coefficients were reduced by mld_reduce32().
392
+ *
393
+ * @spec{@[FIPS204] defines the infinity norm via signed canonical reduction
394
+ * (mod± MLDSA_Q) prior to applying the bounds check. However,
395
+ * `-B < (a mod± MLDSA_Q) < B` is equivalent to `-B < a < B` under the
396
+ * assumption that `B <= MLDSA_Q - MLD_REDUCE32_RANGE_MAX` (cf. the assertion in
397
+ * the code). Hence, this contract and implementation are correct without
398
+ * reduction.}
399
+ *
400
+ * @param[in] a Pointer to polynomial.
401
+ * @param B Norm bound.
402
+ *
403
+ * @return 0 if norm is strictly smaller than
404
+ * B <= (MLDSA_Q - MLD_REDUCE32_RANGE_MAX) and 0xFFFFFFFF otherwise.
405
+ */
406
+ MLD_INTERNAL_API
407
+ MLD_MUST_CHECK_RETURN_VALUE
408
+ uint32_t mld_poly_chknorm(const mld_poly *a, int32_t B)
409
+ __contract__(
410
+ requires(memory_no_alias(a, sizeof(mld_poly)))
411
+ requires(0 <= B && B <= MLDSA_Q - MLD_REDUCE32_RANGE_MAX)
412
+ requires(array_bound(a->coeffs, 0, MLDSA_N, -MLD_REDUCE32_RANGE_MAX, MLD_REDUCE32_RANGE_MAX))
413
+ ensures(return_value == 0 || return_value == 0xFFFFFFFF)
414
+ ensures((return_value == 0) == array_abs_bound(a->coeffs, 0, MLDSA_N, B))
415
+ );
416
+
417
+ #if !defined(MLD_CONFIG_NO_SIGN_API) || !defined(MLD_CONFIG_NO_VERIFY_API)
418
+ #if defined(MLD_CONFIG_MULTILEVEL_WITH_SHARED) || MLD_CONFIG_PARAMETER_SET == 44
419
+ #define mld_polyw1_pack_88 MLD_NAMESPACE(polyw1_pack_88)
420
+ /**
421
+ * Bit-pack polynomial w1, using 6 bits per coefficient.
422
+ * This is the variant for parameter sets with MLDSA_GAMMA2 = (MLDSA_Q-1)/88
423
+ * (ML-DSA-44), for which w1 coefficients lie in [0, 43].
424
+ *
425
+ * @param[out] r Pointer to output byte array (MLDSA_POLYW1_PACKEDBYTES_88).
426
+ * @param[in] a Pointer to input polynomial.
427
+ */
428
+ MLD_INTERNAL_API
429
+ void mld_polyw1_pack_88(uint8_t r[MLDSA_POLYW1_PACKEDBYTES_88],
430
+ const mld_poly *a)
431
+ __contract__(
432
+ requires(memory_no_alias(r, MLDSA_POLYW1_PACKEDBYTES_88))
433
+ requires(memory_no_alias(a, sizeof(mld_poly)))
434
+ requires(array_bound(a->coeffs, 0, MLDSA_N, 0, (MLDSA_Q-1)/(2*MLDSA_GAMMA2_88)))
435
+ assigns(memory_slice(r, MLDSA_POLYW1_PACKEDBYTES_88))
436
+ );
437
+ #endif /* MLD_CONFIG_MULTILEVEL_WITH_SHARED || MLD_CONFIG_PARAMETER_SET == 44 \
438
+ */
439
+
440
+ #if defined(MLD_CONFIG_MULTILEVEL_WITH_SHARED) || \
441
+ (MLD_CONFIG_PARAMETER_SET == 65 || MLD_CONFIG_PARAMETER_SET == 87)
442
+ #define mld_polyw1_pack_32 MLD_NAMESPACE(polyw1_pack_32)
443
+ /**
444
+ * Bit-pack polynomial w1, using 4 bits per coefficient.
445
+ * This is the variant for parameter sets with MLDSA_GAMMA2 = (MLDSA_Q-1)/32
446
+ * (ML-DSA-65 and ML-DSA-87), for which w1 coefficients lie in [0, 15].
447
+ *
448
+ * @param[out] r Pointer to output byte array (MLDSA_POLYW1_PACKEDBYTES_32).
449
+ * @param[in] a Pointer to input polynomial.
450
+ */
451
+ MLD_INTERNAL_API
452
+ void mld_polyw1_pack_32(uint8_t r[MLDSA_POLYW1_PACKEDBYTES_32],
453
+ const mld_poly *a)
454
+ __contract__(
455
+ requires(memory_no_alias(r, MLDSA_POLYW1_PACKEDBYTES_32))
456
+ requires(memory_no_alias(a, sizeof(mld_poly)))
457
+ requires(array_bound(a->coeffs, 0, MLDSA_N, 0, (MLDSA_Q-1)/(2*MLDSA_GAMMA2_32)))
458
+ assigns(memory_slice(r, MLDSA_POLYW1_PACKEDBYTES_32))
459
+ );
460
+ #endif /* MLD_CONFIG_MULTILEVEL_WITH_SHARED || MLD_CONFIG_PARAMETER_SET == 65 \
461
+ || MLD_CONFIG_PARAMETER_SET == 87 */
462
+ #endif /* !MLD_CONFIG_NO_SIGN_API || !MLD_CONFIG_NO_VERIFY_API */
463
+
464
+ #endif /* !MLD_POLY_H */