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,435 @@
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_POLYVEC_H
16
+ #define MLD_POLYVEC_H
17
+
18
+ #include "cbmc.h"
19
+ #include "common.h"
20
+ #include "poly.h"
21
+ #include "poly_kl.h"
22
+
23
+ /* Parameter set namespacing
24
+ * This is to facilitate building multiple instances
25
+ * of mldsa-native (e.g. with varying parameter sets)
26
+ * within a single compilation unit. */
27
+ #define mld_polyvecl MLD_ADD_PARAM_SET(mld_polyvecl)
28
+ #define mld_polyveck MLD_ADD_PARAM_SET(mld_polyveck)
29
+ /* End of parameter set namespacing */
30
+
31
+ /** Vector of MLDSA_L polynomials. */
32
+ typedef struct
33
+ {
34
+ mld_poly vec[MLDSA_L]; /**< Component polynomials. */
35
+ } mld_polyvecl;
36
+
37
+
38
+ #if !defined(MLD_CONFIG_NO_SIGN_API) && \
39
+ (!defined(MLD_CONFIG_REDUCE_RAM) || defined(MLD_UNIT_TEST))
40
+ #define mld_polyvecl_uniform_gamma1 MLD_NAMESPACE_KL(polyvecl_uniform_gamma1)
41
+ /**
42
+ * Sample vector of polynomials with uniformly random coefficients in
43
+ * [-(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1] by unpacking output stream of
44
+ * SHAKE256(seed|kappa+i) for component i.
45
+ *
46
+ * @spec{Implements @[FIPS204, Algorithm 34, ExpandMask].}
47
+ *
48
+ * @param[out] v Pointer to output vector.
49
+ * @param[in] seed Byte array with seed of length MLDSA_CRHBYTES.
50
+ * @param kappa Base counter; component i uses kappa + i.
51
+ */
52
+ MLD_INTERNAL_API
53
+ void mld_polyvecl_uniform_gamma1(mld_polyvecl *v,
54
+ const uint8_t seed[MLDSA_CRHBYTES],
55
+ uint16_t kappa)
56
+ __contract__(
57
+ requires(memory_no_alias(v, sizeof(mld_polyvecl)))
58
+ requires(memory_no_alias(seed, MLDSA_CRHBYTES))
59
+ requires(kappa <= MLD_MAX_KAPPA)
60
+ assigns(memory_slice(v, sizeof(mld_polyvecl)))
61
+ ensures(forall(k0, 0, MLDSA_L,
62
+ array_bound(v->vec[k0].coeffs, 0, MLDSA_N, -(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1 + 1)))
63
+ );
64
+ #endif /* !MLD_CONFIG_NO_SIGN_API && (!MLD_CONFIG_REDUCE_RAM || MLD_UNIT_TEST) \
65
+ */
66
+
67
+ #if !defined(MLD_CONFIG_NO_KEYPAIR_API) || \
68
+ !defined(MLD_CONFIG_NO_VERIFY_API) || \
69
+ (!defined(MLD_CONFIG_NO_SIGN_API) && \
70
+ (!defined(MLD_CONFIG_REDUCE_RAM) || defined(MLD_UNIT_TEST)))
71
+ #define mld_polyvecl_ntt MLD_NAMESPACE_KL(polyvecl_ntt)
72
+ /**
73
+ * Forward NTT of all polynomials in vector of length MLDSA_L. Output
74
+ * coefficients are bounded by MLD_NTT_BOUND in absolute value.
75
+ *
76
+ * @param[in,out] v Pointer to input/output vector.
77
+ */
78
+ MLD_INTERNAL_API
79
+ void mld_polyvecl_ntt(mld_polyvecl *v)
80
+ __contract__(
81
+ requires(memory_no_alias(v, sizeof(mld_polyvecl)))
82
+ requires(forall(k0, 0, MLDSA_L, array_abs_bound(v->vec[k0].coeffs, 0, MLDSA_N, MLDSA_Q)))
83
+ assigns(memory_slice(v, sizeof(mld_polyvecl)))
84
+ ensures(forall(k1, 0, MLDSA_L, array_abs_bound(v->vec[k1].coeffs, 0, MLDSA_N, MLD_NTT_BOUND)))
85
+ );
86
+ #endif /* !MLD_CONFIG_NO_KEYPAIR_API || !MLD_CONFIG_NO_VERIFY_API || \
87
+ (!MLD_CONFIG_NO_SIGN_API && (!MLD_CONFIG_REDUCE_RAM || \
88
+ MLD_UNIT_TEST)) */
89
+
90
+ #if !defined(MLD_CONFIG_REDUCE_RAM) || defined(MLD_UNIT_TEST)
91
+ #define mld_polyvecl_pointwise_acc_montgomery \
92
+ MLD_NAMESPACE_KL(polyvecl_pointwise_acc_montgomery)
93
+ /**
94
+ * Pointwise multiply vectors of polynomials of length MLDSA_L, multiply
95
+ * resulting vector by 2^{-32} and add (accumulate) polynomials in it.
96
+ * Input/output vectors are in NTT domain representation.
97
+ *
98
+ * The first input "u" must be the output of polyvec_matrix_expand() and so
99
+ * have coefficients in [0, MLDSA_Q-1] inclusive.
100
+ *
101
+ * The second input "v" is assumed to be output of an NTT, and hence must have
102
+ * coefficients bounded by [-(MLD_NTT_BOUND-1), MLD_NTT_BOUND-1] inclusive.
103
+ *
104
+ * @spec{Partially implements @[FIPS204, Algorithm 48, MatrixVectorNTT]
105
+ * (one output polynomial; multiply-accumulate of two NTT-domain vectors).}
106
+ *
107
+ * @param[out] w Output polynomial.
108
+ * @param[in] u Pointer to first input vector.
109
+ * @param[in] v Pointer to second input vector.
110
+ */
111
+ MLD_INTERNAL_API
112
+ void mld_polyvecl_pointwise_acc_montgomery(mld_poly *w, const mld_polyvecl *u,
113
+ const mld_polyvecl *v)
114
+ __contract__(
115
+ requires(memory_no_alias(w, sizeof(mld_poly)))
116
+ requires(memory_no_alias(u, sizeof(mld_polyvecl)))
117
+ requires(memory_no_alias(v, sizeof(mld_polyvecl)))
118
+ requires(forall(l0, 0, MLDSA_L,
119
+ array_bound(u->vec[l0].coeffs, 0, MLDSA_N, 0, MLDSA_Q)))
120
+ requires(forall(l1, 0, MLDSA_L,
121
+ array_abs_bound(v->vec[l1].coeffs, 0, MLDSA_N, MLD_NTT_BOUND)))
122
+ assigns(memory_slice(w, sizeof(mld_poly)))
123
+ ensures(array_abs_bound(w->coeffs, 0, MLDSA_N, MLDSA_Q))
124
+ );
125
+ #endif /* !MLD_CONFIG_REDUCE_RAM || MLD_UNIT_TEST */
126
+
127
+ #if !defined(MLD_CONFIG_NO_KEYPAIR_API) || !defined(MLD_CONFIG_NO_VERIFY_API)
128
+ #define mld_polyvecl_chknorm MLD_NAMESPACE_KL(polyvecl_chknorm)
129
+ /**
130
+ * Check infinity norm of polynomials in vector of length MLDSA_L. Assumes
131
+ * input mld_polyvecl to be reduced by polyvecl_reduce().
132
+ *
133
+ * @param[in] v Pointer to vector.
134
+ * @param B Norm bound.
135
+ *
136
+ * @return 0 if norm of all polynomials is strictly smaller than
137
+ * B <= (MLDSA_Q-1)/8 and 0xFFFFFFFF otherwise.
138
+ */
139
+ MLD_INTERNAL_API
140
+ MLD_MUST_CHECK_RETURN_VALUE
141
+ uint32_t mld_polyvecl_chknorm(const mld_polyvecl *v, int32_t B)
142
+ __contract__(
143
+ requires(memory_no_alias(v, sizeof(mld_polyvecl)))
144
+ requires(0 <= B && B <= (MLDSA_Q - 1) / 8)
145
+ requires(forall(k0, 0, MLDSA_L,
146
+ array_bound(v->vec[k0].coeffs, 0, MLDSA_N, -MLD_REDUCE32_RANGE_MAX, MLD_REDUCE32_RANGE_MAX)))
147
+ ensures(return_value == 0 || return_value == 0xFFFFFFFF)
148
+ ensures((return_value == 0) == forall(k1, 0, MLDSA_L, array_abs_bound(v->vec[k1].coeffs, 0, MLDSA_N, B)))
149
+ );
150
+ #endif /* !MLD_CONFIG_NO_KEYPAIR_API || !MLD_CONFIG_NO_VERIFY_API */
151
+
152
+ /** Vector of MLDSA_K polynomials. */
153
+ typedef struct
154
+ {
155
+ mld_poly vec[MLDSA_K]; /**< Component polynomials. */
156
+ } mld_polyveck;
157
+
158
+ #if (!defined(MLD_CONFIG_NO_SIGN_API) && defined(MLD_CONFIG_REDUCE_RAM)) || \
159
+ defined(MLD_UNIT_TEST)
160
+ #define mld_polyveck_reduce MLD_NAMESPACE_KL(polyveck_reduce)
161
+ /**
162
+ * Reduce coefficients of polynomials in vector of length MLDSA_K to
163
+ * representatives in [-MLD_REDUCE32_RANGE_MAX, MLD_REDUCE32_RANGE_MAX].
164
+ *
165
+ * @param[in,out] v Pointer to input/output vector.
166
+ */
167
+ MLD_INTERNAL_API
168
+ void mld_polyveck_reduce(mld_polyveck *v)
169
+ __contract__(
170
+ requires(memory_no_alias(v, sizeof(mld_polyveck)))
171
+ requires(forall(k0, 0, MLDSA_K,
172
+ array_bound(v->vec[k0].coeffs, 0, MLDSA_N, INT32_MIN, MLD_REDUCE32_DOMAIN_MAX)))
173
+ assigns(memory_slice(v, sizeof(mld_polyveck)))
174
+ ensures(forall(k1, 0, MLDSA_K,
175
+ array_bound(v->vec[k1].coeffs, 0, MLDSA_N, -MLD_REDUCE32_RANGE_MAX, MLD_REDUCE32_RANGE_MAX)))
176
+ );
177
+ #endif /* (!MLD_CONFIG_NO_SIGN_API && MLD_CONFIG_REDUCE_RAM) || MLD_UNIT_TEST \
178
+ */
179
+
180
+ #if !defined(MLD_CONFIG_NO_SIGN_API) || defined(MLD_UNIT_TEST)
181
+ #define mld_polyveck_caddq MLD_NAMESPACE_KL(polyveck_caddq)
182
+ /**
183
+ * For all coefficients of polynomials in vector of length MLDSA_K add MLDSA_Q
184
+ * if coefficient is negative.
185
+ *
186
+ * @param[in,out] v Pointer to input/output vector.
187
+ */
188
+ MLD_INTERNAL_API
189
+ void mld_polyveck_caddq(mld_polyveck *v)
190
+ __contract__(
191
+ requires(memory_no_alias(v, sizeof(mld_polyveck)))
192
+ requires(forall(k0, 0, MLDSA_K,
193
+ array_abs_bound(v->vec[k0].coeffs, 0, MLDSA_N, MLDSA_Q)))
194
+ assigns(memory_slice(v, sizeof(mld_polyveck)))
195
+ ensures(forall(k1, 0, MLDSA_K,
196
+ array_bound(v->vec[k1].coeffs, 0, MLDSA_N, 0, MLDSA_Q)))
197
+ );
198
+ #endif /* !MLD_CONFIG_NO_SIGN_API || MLD_UNIT_TEST */
199
+
200
+ #if (!defined(MLD_CONFIG_NO_SIGN_API) || defined(MLD_UNIT_TEST)) && \
201
+ (!defined(MLD_CONFIG_REDUCE_RAM) || defined(MLD_UNIT_TEST))
202
+ #define mld_polyveck_ntt MLD_NAMESPACE_KL(polyveck_ntt)
203
+ /**
204
+ * Forward NTT of all polynomials in vector of length MLDSA_K. Output
205
+ * coefficients are bounded by MLD_NTT_BOUND in absolute value.
206
+ *
207
+ * @param[in,out] v Pointer to input/output vector.
208
+ */
209
+ MLD_INTERNAL_API
210
+ void mld_polyveck_ntt(mld_polyveck *v)
211
+ __contract__(
212
+ requires(memory_no_alias(v, sizeof(mld_polyveck)))
213
+ requires(forall(k0, 0, MLDSA_K, array_abs_bound(v->vec[k0].coeffs, 0, MLDSA_N, MLDSA_Q)))
214
+ assigns(memory_slice(v, sizeof(mld_polyveck)))
215
+ ensures(forall(k1, 0, MLDSA_K, array_abs_bound(v->vec[k1].coeffs, 0, MLDSA_N, MLD_NTT_BOUND)))
216
+ );
217
+ #endif /* (!MLD_CONFIG_NO_SIGN_API || MLD_UNIT_TEST) && \
218
+ (!MLD_CONFIG_REDUCE_RAM || MLD_UNIT_TEST) */
219
+
220
+ #if !defined(MLD_CONFIG_NO_SIGN_API) || defined(MLD_UNIT_TEST)
221
+ #define mld_polyveck_invntt_tomont MLD_NAMESPACE_KL(polyveck_invntt_tomont)
222
+ /**
223
+ * Inverse NTT and multiplication by 2^{32} of polynomials in vector of
224
+ * length MLDSA_K.
225
+ *
226
+ * Input coefficients need to be less than MLDSA_Q, and output coefficients
227
+ * are bounded by MLD_INTT_BOUND.
228
+ *
229
+ * @param[in,out] v Pointer to input/output vector.
230
+ */
231
+ MLD_INTERNAL_API
232
+ void mld_polyveck_invntt_tomont(mld_polyveck *v)
233
+ __contract__(
234
+ requires(memory_no_alias(v, sizeof(mld_polyveck)))
235
+ requires(forall(k0, 0, MLDSA_K, array_abs_bound(v->vec[k0].coeffs, 0, MLDSA_N, MLDSA_Q)))
236
+ assigns(memory_slice(v, sizeof(mld_polyveck)))
237
+ ensures(forall(k1, 0, MLDSA_K, array_abs_bound(v->vec[k1].coeffs, 0, MLDSA_N, MLD_INTT_BOUND)))
238
+ );
239
+ #endif /* !MLD_CONFIG_NO_SIGN_API || MLD_UNIT_TEST */
240
+
241
+ #if !defined(MLD_CONFIG_NO_KEYPAIR_API)
242
+ #define mld_polyveck_chknorm MLD_NAMESPACE_KL(polyveck_chknorm)
243
+ /**
244
+ * Check infinity norm of polynomials in vector of length MLDSA_K. Assumes
245
+ * input mld_polyveck to be reduced by polyveck_reduce().
246
+ *
247
+ * @param[in] v Pointer to vector.
248
+ * @param B Norm bound.
249
+ *
250
+ * @return 0 if norm of all polynomials are strictly smaller than
251
+ * B <= (MLDSA_Q-1)/8 and 0xFFFFFFFF otherwise.
252
+ */
253
+ MLD_INTERNAL_API
254
+ MLD_MUST_CHECK_RETURN_VALUE
255
+ uint32_t mld_polyveck_chknorm(const mld_polyveck *v, int32_t B)
256
+ __contract__(
257
+ requires(memory_no_alias(v, sizeof(mld_polyveck)))
258
+ requires(0 <= B && B <= (MLDSA_Q - 1) / 8)
259
+ requires(forall(k0, 0, MLDSA_K,
260
+ array_bound(v->vec[k0].coeffs, 0, MLDSA_N,
261
+ -MLD_REDUCE32_RANGE_MAX, MLD_REDUCE32_RANGE_MAX)))
262
+ ensures(return_value == 0 || return_value == 0xFFFFFFFF)
263
+ ensures((return_value == 0) == forall(k1, 0, MLDSA_K, array_abs_bound(v->vec[k1].coeffs, 0, MLDSA_N, B)))
264
+ );
265
+
266
+ #endif /* !MLD_CONFIG_NO_KEYPAIR_API */
267
+
268
+ #if !defined(MLD_CONFIG_NO_SIGN_API)
269
+ #define mld_polyveck_decompose MLD_NAMESPACE_KL(polyveck_decompose)
270
+ /**
271
+ * For all coefficients a of polynomials in vector of length MLDSA_K, compute
272
+ * high and low bits a0, a1 such a mod^+ MLDSA_Q = a1*ALPHA + a0 with
273
+ * -ALPHA/2 < a0 <= ALPHA/2 except a1 = (MLDSA_Q-1)/ALPHA where we set
274
+ * a1 = 0 and -ALPHA/2 <= a0 = a mod MLDSA_Q - MLDSA_Q < 0. Assumes
275
+ * coefficients to be standard representatives.
276
+ *
277
+ * @reference{The reference implementation has the input polynomial as a
278
+ * separate argument that may be aliased with either of the outputs. Removing
279
+ * the aliasing eases CBMC proofs.}
280
+ *
281
+ * @param[out] v1 Pointer to output vector of polynomials with
282
+ * coefficients a1.
283
+ * @param[in,out] v0 Pointer to input/output vector of polynomials. Output
284
+ * polynomial has coefficients a0.
285
+ */
286
+ MLD_INTERNAL_API
287
+ void mld_polyveck_decompose(mld_polyveck *v1, mld_polyveck *v0)
288
+ __contract__(
289
+ requires(memory_no_alias(v1, sizeof(mld_polyveck)))
290
+ requires(memory_no_alias(v0, sizeof(mld_polyveck)))
291
+ requires(forall(k0, 0, MLDSA_K,
292
+ array_bound(v0->vec[k0].coeffs, 0, MLDSA_N, 0, MLDSA_Q)))
293
+ assigns(memory_slice(v1, sizeof(mld_polyveck)))
294
+ assigns(memory_slice(v0, sizeof(mld_polyveck)))
295
+ ensures(forall(k1, 0, MLDSA_K,
296
+ array_bound(v1->vec[k1].coeffs, 0, MLDSA_N, 0, (MLDSA_Q-1)/(2*MLDSA_GAMMA2))))
297
+ ensures(forall(k2, 0, MLDSA_K,
298
+ array_abs_bound(v0->vec[k2].coeffs, 0, MLDSA_N, MLDSA_GAMMA2+1)))
299
+ );
300
+ #endif /* !MLD_CONFIG_NO_SIGN_API */
301
+
302
+ #if !defined(MLD_CONFIG_NO_SIGN_API)
303
+ #define mld_polyveck_pack_w1 MLD_NAMESPACE_KL(polyveck_pack_w1)
304
+ /**
305
+ * Bit-pack polynomial vector w1 with coefficients in [0, 15] or [0, 43]. Input
306
+ * coefficients are assumed to be standard representatives.
307
+ *
308
+ * @spec{Implements @[FIPS204, Algorithm 28, w1Encode].}
309
+ *
310
+ * @param[out] r Pointer to output byte array with at least
311
+ * MLDSA_K * MLDSA_POLYW1_PACKEDBYTES bytes.
312
+ * @param[in] w1 Pointer to input polynomial vector.
313
+ */
314
+ MLD_INTERNAL_API
315
+ void mld_polyveck_pack_w1(uint8_t r[MLDSA_K * MLDSA_POLYW1_PACKEDBYTES],
316
+ const mld_polyveck *w1)
317
+ __contract__(
318
+ requires(memory_no_alias(r, MLDSA_K * MLDSA_POLYW1_PACKEDBYTES))
319
+ requires(memory_no_alias(w1, sizeof(mld_polyveck)))
320
+ requires(forall(k1, 0, MLDSA_K,
321
+ array_bound(w1->vec[k1].coeffs, 0, MLDSA_N, 0, (MLDSA_Q-1)/(2*MLDSA_GAMMA2))))
322
+ assigns(memory_slice(r, MLDSA_K * MLDSA_POLYW1_PACKEDBYTES))
323
+ );
324
+ #endif /* !MLD_CONFIG_NO_SIGN_API */
325
+
326
+ #if !defined(MLD_CONFIG_NO_KEYPAIR_API)
327
+ #define mld_polyveck_pack_eta MLD_NAMESPACE_KL(polyveck_pack_eta)
328
+ /**
329
+ * Bit-pack polynomial vector with coefficients in [-MLDSA_ETA, MLDSA_ETA].
330
+ *
331
+ * @param[out] r Pointer to output byte array with
332
+ * MLDSA_K * MLDSA_POLYETA_PACKEDBYTES bytes.
333
+ * @param[in] p Pointer to input polynomial vector.
334
+ */
335
+ MLD_INTERNAL_API
336
+ void mld_polyveck_pack_eta(uint8_t r[MLDSA_K * MLDSA_POLYETA_PACKEDBYTES],
337
+ const mld_polyveck *p)
338
+ __contract__(
339
+ requires(memory_no_alias(r, MLDSA_K * MLDSA_POLYETA_PACKEDBYTES))
340
+ requires(memory_no_alias(p, sizeof(mld_polyveck)))
341
+ requires(forall(k1, 0, MLDSA_K,
342
+ array_abs_bound(p->vec[k1].coeffs, 0, MLDSA_N, MLDSA_ETA + 1)))
343
+ assigns(memory_slice(r, MLDSA_K * MLDSA_POLYETA_PACKEDBYTES))
344
+ );
345
+
346
+ #define mld_polyvecl_pack_eta MLD_NAMESPACE_KL(polyvecl_pack_eta)
347
+ /**
348
+ * Bit-pack polynomial vector with coefficients in [-MLDSA_ETA, MLDSA_ETA].
349
+ *
350
+ * @param[out] r Pointer to output byte array with
351
+ * MLDSA_L * MLDSA_POLYETA_PACKEDBYTES bytes.
352
+ * @param[in] p Pointer to input polynomial vector.
353
+ */
354
+ MLD_INTERNAL_API
355
+ void mld_polyvecl_pack_eta(uint8_t r[MLDSA_L * MLDSA_POLYETA_PACKEDBYTES],
356
+ const mld_polyvecl *p)
357
+ __contract__(
358
+ requires(memory_no_alias(r, MLDSA_L * MLDSA_POLYETA_PACKEDBYTES))
359
+ requires(memory_no_alias(p, sizeof(mld_polyvecl)))
360
+ requires(forall(k1, 0, MLDSA_L,
361
+ array_abs_bound(p->vec[k1].coeffs, 0, MLDSA_N, MLDSA_ETA + 1)))
362
+ assigns(memory_slice(r, MLDSA_L * MLDSA_POLYETA_PACKEDBYTES))
363
+ );
364
+
365
+ #endif /* !MLD_CONFIG_NO_KEYPAIR_API */
366
+
367
+ #if !defined(MLD_CONFIG_NO_KEYPAIR_API) || \
368
+ (!defined(MLD_CONFIG_NO_SIGN_API) && \
369
+ (!defined(MLD_CONFIG_REDUCE_RAM) || defined(MLD_UNIT_TEST)))
370
+ #define mld_polyvecl_unpack_eta MLD_NAMESPACE_KL(polyvecl_unpack_eta)
371
+ /**
372
+ * Unpack polynomial vector with coefficients in [-MLDSA_ETA, MLDSA_ETA].
373
+ *
374
+ * @param[out] p Pointer to output polynomial vector.
375
+ * @param[in] r Input byte array with bit-packed polynomial vector.
376
+ */
377
+ MLD_INTERNAL_API
378
+ void mld_polyvecl_unpack_eta(
379
+ mld_polyvecl *p, const uint8_t r[MLDSA_L * MLDSA_POLYETA_PACKEDBYTES])
380
+ __contract__(
381
+ requires(memory_no_alias(r, MLDSA_L * MLDSA_POLYETA_PACKEDBYTES))
382
+ requires(memory_no_alias(p, sizeof(mld_polyvecl)))
383
+ assigns(memory_slice(p, sizeof(mld_polyvecl)))
384
+ ensures(forall(k1, 0, MLDSA_L,
385
+ array_bound(p->vec[k1].coeffs, 0, MLDSA_N, MLD_POLYETA_UNPACK_LOWER_BOUND, MLDSA_ETA + 1)))
386
+ );
387
+ #endif /* !MLD_CONFIG_NO_KEYPAIR_API || (!MLD_CONFIG_NO_SIGN_API && \
388
+ (!MLD_CONFIG_REDUCE_RAM || MLD_UNIT_TEST)) */
389
+
390
+ #if !defined(MLD_CONFIG_NO_VERIFY_API)
391
+ #define mld_polyvecl_unpack_z MLD_NAMESPACE_KL(polyvecl_unpack_z)
392
+ /**
393
+ * Unpack polynomial vector with coefficients in
394
+ * [-(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1].
395
+ *
396
+ * @param[out] z Pointer to output polynomial vector.
397
+ * @param[in] r Input byte array with bit-packed polynomial vector.
398
+ */
399
+ MLD_INTERNAL_API
400
+ void mld_polyvecl_unpack_z(mld_polyvecl *z,
401
+ const uint8_t r[MLDSA_L * MLDSA_POLYZ_PACKEDBYTES])
402
+ __contract__(
403
+ requires(memory_no_alias(r, MLDSA_L * MLDSA_POLYZ_PACKEDBYTES))
404
+ requires(memory_no_alias(z, sizeof(mld_polyvecl)))
405
+ assigns(memory_slice(z, sizeof(mld_polyvecl)))
406
+ ensures(forall(k1, 0, MLDSA_L,
407
+ array_bound(z->vec[k1].coeffs, 0, MLDSA_N, -(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1 + 1)))
408
+ );
409
+ #endif /* !MLD_CONFIG_NO_VERIFY_API */
410
+
411
+ #if !defined(MLD_CONFIG_NO_KEYPAIR_API) || \
412
+ (!defined(MLD_CONFIG_NO_SIGN_API) && \
413
+ (!defined(MLD_CONFIG_REDUCE_RAM) || defined(MLD_UNIT_TEST)))
414
+ #define mld_polyveck_unpack_eta MLD_NAMESPACE_KL(polyveck_unpack_eta)
415
+ /**
416
+ * Unpack polynomial vector with coefficients in [-MLDSA_ETA, MLDSA_ETA].
417
+ *
418
+ * @param[out] p Pointer to output polynomial vector.
419
+ * @param[in] r Input byte array with bit-packed polynomial vector.
420
+ */
421
+ MLD_INTERNAL_API
422
+ void mld_polyveck_unpack_eta(
423
+ mld_polyveck *p, const uint8_t r[MLDSA_K * MLDSA_POLYETA_PACKEDBYTES])
424
+ __contract__(
425
+ requires(memory_no_alias(r, MLDSA_K * MLDSA_POLYETA_PACKEDBYTES))
426
+ requires(memory_no_alias(p, sizeof(mld_polyveck)))
427
+ assigns(memory_slice(p, sizeof(mld_polyveck)))
428
+ ensures(forall(k1, 0, MLDSA_K,
429
+ array_bound(p->vec[k1].coeffs, 0, MLDSA_N, MLD_POLYETA_UNPACK_LOWER_BOUND, MLDSA_ETA + 1)))
430
+ );
431
+ #endif /* !MLD_CONFIG_NO_KEYPAIR_API || (!MLD_CONFIG_NO_SIGN_API && \
432
+ (!MLD_CONFIG_REDUCE_RAM || MLD_UNIT_TEST)) */
433
+
434
+
435
+ #endif /* !MLD_POLYVEC_H */