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,617 @@
1
+ /*
2
+ * Copyright (c) The mlkem-native project authors
3
+ * Copyright (c) The mldsa-native project authors
4
+ * SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
5
+ */
6
+
7
+ #ifndef MLD_NATIVE_API_H
8
+ #define MLD_NATIVE_API_H
9
+ /*
10
+ * Native arithmetic interface
11
+ *
12
+ * This header is primarily for documentation purposes.
13
+ * It should not be included by backend implementations.
14
+ *
15
+ * To ensure consistency with backends, the header will be
16
+ * included automatically after inclusion of the active
17
+ * backend, to ensure consistency of function signatures,
18
+ * and run sanity checks.
19
+ */
20
+
21
+ #include "../cbmc.h"
22
+ #include "../common.h"
23
+
24
+ /* Backends must return MLD_NATIVE_FUNC_SUCCESS upon success. */
25
+ #define MLD_NATIVE_FUNC_SUCCESS (0)
26
+ /* Backends may return MLD_NATIVE_FUNC_FALLBACK to signal to the frontend that
27
+ * the target/parameters are unsupported; typically, this would be because of
28
+ * dependencies on CPU features not detected on the host CPU. In this case,
29
+ * the frontend falls back to the default C implementation.
30
+ *
31
+ * IMPORTANT: Backend implementations must ensure that the decision of whether
32
+ * to fallback (return MLD_NATIVE_FUNC_FALLBACK) or not must never depend on
33
+ * the input data itself. Fallback decisions may only depend on system
34
+ * capabilities (e.g., CPU features) and, where present, length information.
35
+ * This requirement applies to all backend functions to maintain constant-time
36
+ * properties.
37
+ */
38
+ #define MLD_NATIVE_FUNC_FALLBACK (-1)
39
+
40
+ /* Absolute exclusive upper bound for the output of fqmul.
41
+ *
42
+ * NOTE: This is the same bound as in poly.h and has to be kept
43
+ * in sync. */
44
+ #define MLD_FQMUL_BOUND ((5 * MLDSA_Q + 3) / 4)
45
+
46
+ /* Bound on absolute value of coefficients after NTT.
47
+ *
48
+ * NOTE: This is the same bound as in poly.h and has to be kept
49
+ * in sync. */
50
+ #define MLD_NTT_BOUND (9 * MLD_FQMUL_BOUND)
51
+
52
+ /* Absolute exclusive upper bound for the output of the inverse NTT
53
+ *
54
+ * NOTE: This is the same bound as in poly.h and has to be kept
55
+ * in sync. */
56
+ #define MLD_INTT_BOUND MLDSA_Q
57
+
58
+ /* Absolute bound for range of mld_reduce32()
59
+ *
60
+ * NOTE: This is the same bound as in reduce.h and has to be kept
61
+ * in sync. */
62
+ /* check-magic: 6283009 == (MLD_REDUCE32_DOMAIN_MAX - 255 * MLDSA_Q + 1) */
63
+ #define MLD_REDUCE32_RANGE_MAX 6283009
64
+ /*
65
+ * This is the C<->native interface allowing for the drop-in of
66
+ * native code for performance-critical arithmetic components of ML-DSA.
67
+ *
68
+ * A _backend_ is a specific implementation of (part of) this interface.
69
+ *
70
+ * To add a function to a backend, define MLD_USE_NATIVE_XXX and
71
+ * implement `static inline xxx(...)` in the profile header.
72
+ */
73
+
74
+ /*
75
+ * Those functions are meant to be trivial wrappers around the chosen native
76
+ * implementation. The are static inline to avoid unnecessary calls.
77
+ * The macro before each declaration controls whether a native
78
+ * implementation is present.
79
+ */
80
+
81
+ #if defined(MLD_USE_NATIVE_NTT)
82
+ /**
83
+ * Computes negacyclic number-theoretic transform (NTT) of a polynomial
84
+ * in place.
85
+ *
86
+ * The input polynomial is assumed to be in normal order. The output
87
+ * polynomial is in bitreversed order.
88
+ *
89
+ * @param[in,out] p Pointer to in/output polynomial.
90
+ */
91
+ MLD_MUST_CHECK_RETURN_VALUE
92
+ static MLD_INLINE int mld_ntt_native(int32_t p[MLDSA_N])
93
+ __contract__(
94
+ requires(memory_no_alias(p, sizeof(int32_t) * MLDSA_N))
95
+ requires(array_abs_bound(p, 0, MLDSA_N, MLDSA_Q))
96
+ assigns(memory_slice(p, sizeof(int32_t) * MLDSA_N))
97
+ ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == MLD_NATIVE_FUNC_SUCCESS)
98
+ ensures((return_value == MLD_NATIVE_FUNC_SUCCESS) ==> array_abs_bound(p, 0, MLDSA_N, MLD_NTT_BOUND))
99
+ ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_abs_bound(p, 0, MLDSA_N, MLDSA_Q))
100
+ ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_unchanged(p, MLDSA_N))
101
+ );
102
+ #endif /* MLD_USE_NATIVE_NTT */
103
+
104
+
105
+ #if defined(MLD_USE_NATIVE_NTT_CUSTOM_ORDER)
106
+ /*
107
+ * This must only be set if NTT and INTT have native implementations
108
+ * that are adapted to the custom order.
109
+ */
110
+ #if !defined(MLD_USE_NATIVE_NTT) || !defined(MLD_USE_NATIVE_INTT)
111
+ #error \
112
+ "Invalid native profile: MLD_USE_NATIVE_NTT_CUSTOM_ORDER can only be \
113
+ set if there are native implementations for NTT and INTT."
114
+ #endif
115
+
116
+ /**
117
+ * When MLD_USE_NATIVE_NTT_CUSTOM_ORDER is defined, convert a polynomial in
118
+ * NTT domain from bitreversed order to the custom order output by the native
119
+ * NTT.
120
+ *
121
+ * This must only be defined if there is native code for both the NTT and
122
+ * INTT.
123
+ *
124
+ * @param[in,out] p Pointer to in/output polynomial.
125
+ */
126
+ static MLD_INLINE void mld_poly_permute_bitrev_to_custom(int32_t p[MLDSA_N])
127
+ __contract__(
128
+ /* We don't specify that this should be a permutation, but only
129
+ * that it does not change the bound established at the end of
130
+ * mld_polyvec_matrix_expand.
131
+ */
132
+ requires(memory_no_alias(p, sizeof(int32_t) * MLDSA_N))
133
+ requires(array_bound(p, 0, MLDSA_N, 0, MLDSA_Q))
134
+ assigns(memory_slice(p, sizeof(int32_t) * MLDSA_N))
135
+ ensures(array_bound(p, 0, MLDSA_N, 0, MLDSA_Q)));
136
+ #endif /* MLD_USE_NATIVE_NTT_CUSTOM_ORDER */
137
+
138
+
139
+ #if defined(MLD_USE_NATIVE_INTT)
140
+ /**
141
+ * Computes inverse of negacyclic number-theoretic transform (NTT) of a
142
+ * polynomial in place.
143
+ *
144
+ * The input polynomial is in bitreversed order. The output polynomial is
145
+ * assumed to be in normal order.
146
+ *
147
+ * @param[in,out] p Pointer to in/output polynomial.
148
+ */
149
+ MLD_MUST_CHECK_RETURN_VALUE
150
+ static MLD_INLINE int mld_intt_native(int32_t p[MLDSA_N])
151
+ __contract__(
152
+ requires(memory_no_alias(p, sizeof(int32_t) * MLDSA_N))
153
+ requires(array_abs_bound(p, 0, MLDSA_N, MLDSA_Q))
154
+ assigns(memory_slice(p, sizeof(int32_t) * MLDSA_N))
155
+ ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == MLD_NATIVE_FUNC_SUCCESS)
156
+ ensures((return_value == MLD_NATIVE_FUNC_SUCCESS) ==> array_abs_bound(p, 0, MLDSA_N, MLD_INTT_BOUND))
157
+ ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_abs_bound(p, 0, MLDSA_N, MLDSA_Q))
158
+ ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_unchanged(p, MLDSA_N))
159
+ );
160
+ #endif /* MLD_USE_NATIVE_INTT */
161
+
162
+ #if defined(MLD_USE_NATIVE_REJ_UNIFORM)
163
+ /**
164
+ * Run rejection sampling on uniform random bytes to generate uniform random
165
+ * integers in [0, MLDSA_Q-1].
166
+ *
167
+ * @param[out] r Pointer to output buffer.
168
+ * @param len Requested number of 32-bit integers (uniform mod
169
+ * MLDSA_Q).
170
+ * @param[in] buf Pointer to input buffer (assumed to be uniform random
171
+ * bytes).
172
+ * @param buflen Length of input buffer in bytes.
173
+ *
174
+ * @return - MLD_NATIVE_FUNC_FALLBACK if the native implementation does not
175
+ * support the input lengths.
176
+ * - Otherwise, the non-negative number of sampled 32-bit integers
177
+ * (at most len).
178
+ */
179
+ MLD_MUST_CHECK_RETURN_VALUE
180
+ static MLD_INLINE int mld_rej_uniform_native(int32_t *r, unsigned len,
181
+ const uint8_t *buf,
182
+ unsigned buflen)
183
+ __contract__(
184
+ requires(len <= MLDSA_N)
185
+ requires(buflen <= ( 5 * 168) && buflen % 3 == 0)
186
+ requires(memory_no_alias(r, sizeof(int32_t) * len))
187
+ requires(memory_no_alias(buf, buflen))
188
+ assigns(memory_slice(r, sizeof(int32_t) * len))
189
+ ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || (0 <= return_value && return_value <= len))
190
+ ensures((return_value != MLD_NATIVE_FUNC_FALLBACK) ==> array_bound(r, 0, (unsigned) return_value, 0, MLDSA_Q))
191
+ );
192
+ #endif /* MLD_USE_NATIVE_REJ_UNIFORM */
193
+
194
+ #if !defined(MLD_CONFIG_NO_KEYPAIR_API)
195
+ #if defined(MLD_USE_NATIVE_REJ_UNIFORM_ETA2)
196
+ #if defined(MLD_CONFIG_MULTILEVEL_WITH_SHARED) || MLDSA_ETA == 2
197
+ /**
198
+ * Run rejection sampling on uniform random bytes to generate uniform random
199
+ * integers in [-2, +2].
200
+ *
201
+ * @param[out] r Pointer to output buffer.
202
+ * @param len Requested number of 32-bit integers (uniform in
203
+ * [-2, +2]).
204
+ * @param[in] buf Pointer to input buffer (assumed to be uniform random
205
+ * bytes).
206
+ * @param buflen Length of input buffer in bytes.
207
+ *
208
+ * @return - MLD_NATIVE_FUNC_FALLBACK if the native implementation does not
209
+ * support the input lengths.
210
+ * - Otherwise, the non-negative number of sampled 32-bit integers
211
+ * (at most len).
212
+ */
213
+ MLD_MUST_CHECK_RETURN_VALUE
214
+ static MLD_INLINE int mld_rej_uniform_eta2_native(int32_t *r, unsigned len,
215
+ const uint8_t *buf,
216
+ unsigned buflen)
217
+ __contract__(
218
+ requires(len <= MLDSA_N)
219
+ requires(buflen <= (2 * 136))
220
+ requires(memory_no_alias(r, sizeof(int32_t) * len))
221
+ requires(memory_no_alias(buf, buflen))
222
+ assigns(memory_slice(r, sizeof(int32_t) * len))
223
+ ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || (0 <= return_value && return_value <= len))
224
+ /* check-magic: 3 == 2 + 1 (decl gated on MLDSA_ETA == 2) */
225
+ ensures((return_value != MLD_NATIVE_FUNC_FALLBACK) ==> (array_abs_bound(r, 0, return_value, 3)))
226
+ );
227
+ #endif /* MLD_CONFIG_MULTILEVEL_WITH_SHARED || MLDSA_ETA == 2 */
228
+ #endif /* MLD_USE_NATIVE_REJ_UNIFORM_ETA2 */
229
+
230
+ #if defined(MLD_USE_NATIVE_REJ_UNIFORM_ETA4)
231
+ #if defined(MLD_CONFIG_MULTILEVEL_WITH_SHARED) || MLDSA_ETA == 4
232
+ /**
233
+ * Run rejection sampling on uniform random bytes to generate uniform random
234
+ * integers in [-4, +4].
235
+ *
236
+ * @param[out] r Pointer to output buffer.
237
+ * @param len Requested number of 32-bit integers (uniform in
238
+ * [-4, +4]).
239
+ * @param[in] buf Pointer to input buffer (assumed to be uniform random
240
+ * bytes).
241
+ * @param buflen Length of input buffer in bytes.
242
+ *
243
+ * @return - MLD_NATIVE_FUNC_FALLBACK if the native implementation does not
244
+ * support the input lengths.
245
+ * - Otherwise, the non-negative number of sampled 32-bit integers
246
+ * (at most len).
247
+ */
248
+ MLD_MUST_CHECK_RETURN_VALUE
249
+ static MLD_INLINE int mld_rej_uniform_eta4_native(int32_t *r, unsigned len,
250
+ const uint8_t *buf,
251
+ unsigned buflen)
252
+ __contract__(
253
+ requires(len <= MLDSA_N)
254
+ requires(buflen <= (2 * 136))
255
+ requires(memory_no_alias(r, sizeof(int32_t) * len))
256
+ requires(memory_no_alias(buf, buflen))
257
+ assigns(memory_slice(r, sizeof(int32_t) * len))
258
+ ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || (0 <= return_value && return_value <= len))
259
+ /* check-magic: 5 == 4 + 1 (decl gated on MLDSA_ETA == 4) */
260
+ ensures((return_value != MLD_NATIVE_FUNC_FALLBACK) ==> (array_abs_bound(r, 0, return_value, 5)))
261
+ );
262
+ #endif /* MLD_CONFIG_MULTILEVEL_WITH_SHARED || MLDSA_ETA == 4 */
263
+ #endif /* MLD_USE_NATIVE_REJ_UNIFORM_ETA4 */
264
+ #endif /* !MLD_CONFIG_NO_KEYPAIR_API */
265
+
266
+ #if !defined(MLD_CONFIG_NO_SIGN_API)
267
+ #if defined(MLD_USE_NATIVE_POLY_DECOMPOSE_32)
268
+ #if defined(MLD_CONFIG_MULTILEVEL_WITH_SHARED) || \
269
+ (MLD_CONFIG_PARAMETER_SET == 65 || MLD_CONFIG_PARAMETER_SET == 87)
270
+ /**
271
+ * Native implementation of poly_decompose for GAMMA2 = (MLDSA_Q-1)/32.
272
+ *
273
+ * For all coefficients c of the input polynomial, compute high and low bits
274
+ * c0, c1 such c mod MLDSA_Q = c1*(2*GAMMA2) + c0 with
275
+ * -(2*GAMMA2)/2 < c0 <= (2*GAMMA2)/2 except c1 = (MLDSA_Q-1)/(2*GAMMA2) where
276
+ * we set c1 = 0 and -(2*GAMMA2)/2 <= c0 = c mod MLDSA_Q - MLDSA_Q < 0.
277
+ * Assumes coefficients to be standard representatives.
278
+ *
279
+ * @param[out] a1 Output polynomial with coefficients c1.
280
+ * @param[in,out] a0 Input/output polynomial. Output has coefficients c0.
281
+ */
282
+ MLD_MUST_CHECK_RETURN_VALUE
283
+ static MLD_INLINE int mld_poly_decompose_32_native(int32_t *a1, int32_t *a0)
284
+ __contract__(
285
+ requires(memory_no_alias(a1, sizeof(int32_t) * MLDSA_N))
286
+ requires(memory_no_alias(a0, sizeof(int32_t) * MLDSA_N))
287
+ requires(array_bound(a0, 0, MLDSA_N, 0, MLDSA_Q))
288
+ assigns(memory_slice(a1, sizeof(int32_t) * MLDSA_N))
289
+ assigns(memory_slice(a0, sizeof(int32_t) * MLDSA_N))
290
+ ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == MLD_NATIVE_FUNC_SUCCESS)
291
+ ensures((return_value == MLD_NATIVE_FUNC_SUCCESS) ==> array_bound(a1, 0, MLDSA_N, 0, (MLDSA_Q-1)/(2*MLDSA_GAMMA2)))
292
+ ensures((return_value == MLD_NATIVE_FUNC_SUCCESS) ==> array_abs_bound(a0, 0, MLDSA_N, MLDSA_GAMMA2+1))
293
+ ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_bound(a0, 0, MLDSA_N, 0, MLDSA_Q))
294
+ ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_unchanged(a0, MLDSA_N))
295
+ );
296
+ #endif /* MLD_CONFIG_MULTILEVEL_WITH_SHARED || MLD_CONFIG_PARAMETER_SET == 65 \
297
+ || MLD_CONFIG_PARAMETER_SET == 87 */
298
+ #endif /* MLD_USE_NATIVE_POLY_DECOMPOSE_32 */
299
+
300
+ #if defined(MLD_USE_NATIVE_POLY_DECOMPOSE_88)
301
+ #if defined(MLD_CONFIG_MULTILEVEL_WITH_SHARED) || MLD_CONFIG_PARAMETER_SET == 44
302
+ /**
303
+ * Native implementation of poly_decompose for GAMMA2 = (MLDSA_Q-1)/88.
304
+ *
305
+ * For all coefficients c of the input polynomial, compute high and low bits
306
+ * c0, c1 such c mod MLDSA_Q = c1*(2*GAMMA2) + c0 with
307
+ * -(2*GAMMA2)/2 < c0 <= (2*GAMMA2)/2 except c1 = (MLDSA_Q-1)/(2*GAMMA2) where
308
+ * we set c1 = 0 and -(2*GAMMA2)/2 <= c0 = c mod MLDSA_Q - MLDSA_Q < 0.
309
+ * Assumes coefficients to be standard representatives.
310
+ *
311
+ * @param[out] a1 Output polynomial with coefficients c1.
312
+ * @param[in,out] a0 Input/output polynomial. Output has coefficients c0.
313
+ */
314
+ MLD_MUST_CHECK_RETURN_VALUE
315
+ static MLD_INLINE int mld_poly_decompose_88_native(int32_t *a1, int32_t *a0)
316
+ __contract__(
317
+ requires(memory_no_alias(a1, sizeof(int32_t) * MLDSA_N))
318
+ requires(memory_no_alias(a0, sizeof(int32_t) * MLDSA_N))
319
+ requires(array_bound(a0, 0, MLDSA_N, 0, MLDSA_Q))
320
+ assigns(memory_slice(a1, sizeof(int32_t) * MLDSA_N))
321
+ assigns(memory_slice(a0, sizeof(int32_t) * MLDSA_N))
322
+ ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == MLD_NATIVE_FUNC_SUCCESS)
323
+ ensures((return_value == MLD_NATIVE_FUNC_SUCCESS) ==> array_bound(a1, 0, MLDSA_N, 0, (MLDSA_Q-1)/(2*MLDSA_GAMMA2)))
324
+ ensures((return_value == MLD_NATIVE_FUNC_SUCCESS) ==> array_abs_bound(a0, 0, MLDSA_N, MLDSA_GAMMA2+1))
325
+ ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_bound(a0, 0, MLDSA_N, 0, MLDSA_Q))
326
+ ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_unchanged(a0, MLDSA_N))
327
+ );
328
+ #endif /* MLD_CONFIG_MULTILEVEL_WITH_SHARED || MLD_CONFIG_PARAMETER_SET == 44 \
329
+ */
330
+ #endif /* MLD_USE_NATIVE_POLY_DECOMPOSE_88 */
331
+ #endif /* !MLD_CONFIG_NO_SIGN_API */
332
+
333
+ #if defined(MLD_USE_NATIVE_POLY_CADDQ)
334
+ /**
335
+ * For all coefficients of in/out polynomial add Q if coefficient is negative.
336
+ *
337
+ * @param[in,out] a Pointer to input/output polynomial.
338
+ */
339
+ MLD_MUST_CHECK_RETURN_VALUE
340
+ static MLD_INLINE int mld_poly_caddq_native(int32_t a[MLDSA_N])
341
+ __contract__(
342
+ requires(memory_no_alias(a, sizeof(int32_t) * MLDSA_N))
343
+ requires(array_abs_bound(a, 0, MLDSA_N, MLDSA_Q))
344
+ assigns(memory_slice(a, sizeof(int32_t) * MLDSA_N))
345
+ ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == MLD_NATIVE_FUNC_SUCCESS)
346
+ ensures((return_value == MLD_NATIVE_FUNC_SUCCESS) ==> array_bound(a, 0, MLDSA_N, 0, MLDSA_Q))
347
+ ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_abs_bound(a, 0, MLDSA_N, MLDSA_Q))
348
+ ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_unchanged(a, MLDSA_N))
349
+ );
350
+ #endif /* MLD_USE_NATIVE_POLY_CADDQ */
351
+
352
+ #if !defined(MLD_CONFIG_NO_VERIFY_API)
353
+ #if defined(MLD_USE_NATIVE_POLY_USE_HINT_32)
354
+ #if defined(MLD_CONFIG_MULTILEVEL_WITH_SHARED) || \
355
+ (MLD_CONFIG_PARAMETER_SET == 65 || MLD_CONFIG_PARAMETER_SET == 87)
356
+ /**
357
+ * Native implementation of poly_use_hint for GAMMA2 = (MLDSA_Q-1)/32.
358
+ *
359
+ * Use hint h to correct the high bits of a in-place.
360
+ *
361
+ * @param[in,out] a Input/output polynomial.
362
+ * @param[in] h Hint polynomial.
363
+ */
364
+ MLD_MUST_CHECK_RETURN_VALUE
365
+ static MLD_INLINE int mld_poly_use_hint_32_native(int32_t *a, const int32_t *h)
366
+ __contract__(
367
+ requires(memory_no_alias(a, sizeof(int32_t) * MLDSA_N))
368
+ requires(memory_no_alias(h, sizeof(int32_t) * MLDSA_N))
369
+ requires(array_bound(a, 0, MLDSA_N, 0, MLDSA_Q))
370
+ requires(array_bound(h, 0, MLDSA_N, 0, 2))
371
+ assigns(memory_slice(a, sizeof(int32_t) * MLDSA_N))
372
+ ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == MLD_NATIVE_FUNC_SUCCESS)
373
+ ensures((return_value == MLD_NATIVE_FUNC_SUCCESS) ==> array_bound(a, 0, MLDSA_N, 0, (MLDSA_Q-1)/(2*MLDSA_GAMMA2)))
374
+ ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_unchanged(a, MLDSA_N))
375
+ );
376
+ #endif /* MLD_CONFIG_MULTILEVEL_WITH_SHARED || MLD_CONFIG_PARAMETER_SET == 65 \
377
+ || MLD_CONFIG_PARAMETER_SET == 87 */
378
+ #endif /* MLD_USE_NATIVE_POLY_USE_HINT_32 */
379
+
380
+ #if defined(MLD_USE_NATIVE_POLY_USE_HINT_88)
381
+ #if defined(MLD_CONFIG_MULTILEVEL_WITH_SHARED) || MLD_CONFIG_PARAMETER_SET == 44
382
+ /**
383
+ * Native implementation of poly_use_hint for GAMMA2 = (MLDSA_Q-1)/88.
384
+ *
385
+ * Use hint h to correct the high bits of a in-place.
386
+ *
387
+ * @param[in,out] a Input/output polynomial.
388
+ * @param[in] h Hint polynomial.
389
+ */
390
+ MLD_MUST_CHECK_RETURN_VALUE
391
+ static MLD_INLINE int mld_poly_use_hint_88_native(int32_t *a, const int32_t *h)
392
+ __contract__(
393
+ requires(memory_no_alias(a, sizeof(int32_t) * MLDSA_N))
394
+ requires(memory_no_alias(h, sizeof(int32_t) * MLDSA_N))
395
+ requires(array_bound(a, 0, MLDSA_N, 0, MLDSA_Q))
396
+ requires(array_bound(h, 0, MLDSA_N, 0, 2))
397
+ assigns(memory_slice(a, sizeof(int32_t) * MLDSA_N))
398
+ ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == MLD_NATIVE_FUNC_SUCCESS)
399
+ ensures((return_value == MLD_NATIVE_FUNC_SUCCESS) ==> array_bound(a, 0, MLDSA_N, 0, (MLDSA_Q-1)/(2*MLDSA_GAMMA2)))
400
+ ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_unchanged(a, MLDSA_N))
401
+ );
402
+ #endif /* MLD_CONFIG_MULTILEVEL_WITH_SHARED || MLD_CONFIG_PARAMETER_SET == 44 \
403
+ */
404
+ #endif /* MLD_USE_NATIVE_POLY_USE_HINT_88 */
405
+ #endif /* !MLD_CONFIG_NO_VERIFY_API */
406
+
407
+ #if defined(MLD_USE_NATIVE_POLY_CHKNORM)
408
+ /**
409
+ * Check infinity norm of polynomial against given bound. Assumes input
410
+ * coefficients were reduced by mld_reduce32().
411
+ *
412
+ * @param[in] a Pointer to polynomial.
413
+ * @param B Norm bound, which must be in the range
414
+ * 0 .. MLDSA_Q - MLD_REDUCE32_RANGE_MAX inclusive.
415
+ *
416
+ * @return - MLD_NATIVE_FUNC_FALLBACK if the target CPU cannot support a
417
+ * native implementation of this function.
418
+ * - MLD_NATIVE_FUNC_SUCCESS if the infinity norm is strictly smaller
419
+ * than B.
420
+ * - 1 otherwise.
421
+ */
422
+ MLD_MUST_CHECK_RETURN_VALUE
423
+ static MLD_INLINE int mld_poly_chknorm_native(const int32_t *a, int32_t B)
424
+ __contract__(
425
+ requires(memory_no_alias(a, sizeof(int32_t) * MLDSA_N))
426
+ requires(0 <= B && B <= MLDSA_Q - MLD_REDUCE32_RANGE_MAX)
427
+ requires(array_bound(a, 0, MLDSA_N, -MLD_REDUCE32_RANGE_MAX, MLD_REDUCE32_RANGE_MAX))
428
+ ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == 0 ||
429
+ return_value == 1)
430
+ ensures((return_value != MLD_NATIVE_FUNC_FALLBACK) ==>
431
+ ((return_value == 0) == array_abs_bound(a, 0, MLDSA_N, B)))
432
+ );
433
+ #endif /* MLD_USE_NATIVE_POLY_CHKNORM */
434
+
435
+ #if !defined(MLD_CONFIG_NO_SIGN_API) || !defined(MLD_CONFIG_NO_VERIFY_API)
436
+ #if defined(MLD_USE_NATIVE_POLYZ_UNPACK_17)
437
+ #if defined(MLD_CONFIG_MULTILEVEL_WITH_SHARED) || MLD_CONFIG_PARAMETER_SET == 44
438
+ /**
439
+ * Native implementation of polyz_unpack for GAMMA1 = 2^17.
440
+ *
441
+ * Unpack polynomial z with coefficients in
442
+ * [-(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1].
443
+ *
444
+ * @param[out] r Pointer to output polynomial.
445
+ * @param[in] a Byte array with bit-packed polynomial.
446
+ */
447
+ MLD_MUST_CHECK_RETURN_VALUE
448
+ static MLD_INLINE int mld_polyz_unpack_17_native(int32_t *r, const uint8_t *a)
449
+ __contract__(
450
+ requires(memory_no_alias(r, sizeof(int32_t) * MLDSA_N))
451
+ requires(memory_no_alias(a, MLDSA_POLYZ_PACKEDBYTES))
452
+ assigns(memory_slice(r, sizeof(int32_t) * MLDSA_N))
453
+ ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == MLD_NATIVE_FUNC_SUCCESS)
454
+ ensures((return_value == MLD_NATIVE_FUNC_SUCCESS) ==> array_bound(r, 0, MLDSA_N, -(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1 + 1))
455
+ ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_unchanged(r, MLDSA_N))
456
+ );
457
+ #endif /* MLD_CONFIG_MULTILEVEL_WITH_SHARED || MLD_CONFIG_PARAMETER_SET == 44 \
458
+ */
459
+ #endif /* MLD_USE_NATIVE_POLYZ_UNPACK_17 */
460
+
461
+ #if defined(MLD_USE_NATIVE_POLYZ_UNPACK_19)
462
+ #if defined(MLD_CONFIG_MULTILEVEL_WITH_SHARED) || \
463
+ (MLD_CONFIG_PARAMETER_SET == 65 || MLD_CONFIG_PARAMETER_SET == 87)
464
+ /**
465
+ * Native implementation of polyz_unpack for GAMMA1 = 2^19.
466
+ *
467
+ * Unpack polynomial z with coefficients in
468
+ * [-(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1].
469
+ *
470
+ * @param[out] r Pointer to output polynomial.
471
+ * @param[in] a Byte array with bit-packed polynomial.
472
+ */
473
+ MLD_MUST_CHECK_RETURN_VALUE
474
+ static MLD_INLINE int mld_polyz_unpack_19_native(int32_t *r, const uint8_t *a)
475
+ __contract__(
476
+ requires(memory_no_alias(r, sizeof(int32_t) * MLDSA_N))
477
+ requires(memory_no_alias(a, MLDSA_POLYZ_PACKEDBYTES))
478
+ assigns(memory_slice(r, sizeof(int32_t) * MLDSA_N))
479
+ ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == MLD_NATIVE_FUNC_SUCCESS)
480
+ ensures((return_value == MLD_NATIVE_FUNC_SUCCESS) ==> array_bound(r, 0, MLDSA_N, -(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1 + 1))
481
+ ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_unchanged(r, MLDSA_N))
482
+ );
483
+ #endif /* MLD_CONFIG_MULTILEVEL_WITH_SHARED || MLD_CONFIG_PARAMETER_SET == 65 \
484
+ || MLD_CONFIG_PARAMETER_SET == 87 */
485
+ #endif /* MLD_USE_NATIVE_POLYZ_UNPACK_19 */
486
+ #endif /* !MLD_CONFIG_NO_SIGN_API || !MLD_CONFIG_NO_VERIFY_API */
487
+
488
+ #if !defined(MLD_CONFIG_NO_SIGN_API) || !defined(MLD_CONFIG_NO_VERIFY_API) || \
489
+ defined(MLD_CONFIG_REDUCE_RAM) || defined(MLD_UNIT_TEST)
490
+ #if defined(MLD_USE_NATIVE_POINTWISE_MONTGOMERY)
491
+ /**
492
+ * Pointwise multiplication of polynomials in NTT domain with Montgomery
493
+ * reduction. Destructive in the first argument.
494
+ *
495
+ * Computes a[i] = a[i] * b[i] * R^(-1) mod MLDSA_Q for all i, where R = 2^32.
496
+ *
497
+ * @param[in,out] a First input/output polynomial.
498
+ * @param[in] b Second input polynomial.
499
+ */
500
+ MLD_MUST_CHECK_RETURN_VALUE
501
+ static MLD_INLINE int mld_poly_pointwise_montgomery_native(
502
+ int32_t a[MLDSA_N], const int32_t b[MLDSA_N])
503
+ __contract__(
504
+ requires(memory_no_alias(a, sizeof(int32_t) * MLDSA_N))
505
+ requires(memory_no_alias(b, sizeof(int32_t) * MLDSA_N))
506
+ requires(array_abs_bound(a, 0, MLDSA_N, MLD_NTT_BOUND))
507
+ requires(array_abs_bound(b, 0, MLDSA_N, MLD_NTT_BOUND))
508
+ assigns(memory_slice(a, sizeof(int32_t) * MLDSA_N))
509
+ ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == MLD_NATIVE_FUNC_SUCCESS)
510
+ ensures((return_value == MLD_NATIVE_FUNC_SUCCESS) ==> array_abs_bound(a, 0, MLDSA_N, MLDSA_Q))
511
+ ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_abs_bound(a, 0, MLDSA_N, MLD_NTT_BOUND))
512
+ ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_abs_bound(b, 0, MLDSA_N, MLD_NTT_BOUND))
513
+ );
514
+ #endif /* MLD_USE_NATIVE_POINTWISE_MONTGOMERY */
515
+ #endif /* !MLD_CONFIG_NO_SIGN_API || !MLD_CONFIG_NO_VERIFY_API || \
516
+ MLD_CONFIG_REDUCE_RAM || MLD_UNIT_TEST */
517
+
518
+ #if defined(MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L4)
519
+ #if defined(MLD_CONFIG_MULTILEVEL_WITH_SHARED) || MLDSA_L == 4
520
+ /**
521
+ * Native implementation of polyvecl_pointwise_acc_montgomery for MLDSA_L = 4.
522
+ *
523
+ * Pointwise multiply vectors of polynomials of length MLDSA_L, multiply
524
+ * resulting vector by 2^{-32} and add (accumulate) polynomials in it.
525
+ * Input/output vectors are in NTT domain representation.
526
+ *
527
+ * @param[out] w Output polynomial.
528
+ * @param[in] u First input vector.
529
+ * @param[in] v Second input vector.
530
+ */
531
+ MLD_MUST_CHECK_RETURN_VALUE
532
+ static MLD_INLINE int mld_polyvecl_pointwise_acc_montgomery_l4_native(
533
+ int32_t w[MLDSA_N], const int32_t u[4][MLDSA_N],
534
+ const int32_t v[4][MLDSA_N])
535
+ __contract__(
536
+ requires(memory_no_alias(w, sizeof(int32_t) * MLDSA_N))
537
+ requires(memory_no_alias(u, sizeof(int32_t) * 4 * MLDSA_N))
538
+ requires(memory_no_alias(v, sizeof(int32_t) * 4 * MLDSA_N))
539
+ requires(forall(l0, 0, 4,
540
+ array_bound(u[l0], 0, MLDSA_N, 0, MLDSA_Q)))
541
+ requires(forall(l1, 0, 4,
542
+ array_abs_bound(v[l1], 0, MLDSA_N, MLD_NTT_BOUND)))
543
+ assigns(memory_slice(w, sizeof(int32_t) * MLDSA_N))
544
+ ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == MLD_NATIVE_FUNC_SUCCESS)
545
+ ensures((return_value == MLD_NATIVE_FUNC_SUCCESS) ==> array_abs_bound(w, 0, MLDSA_N, MLDSA_Q))
546
+ ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_unchanged(w, MLDSA_N))
547
+ );
548
+ #endif /* MLD_CONFIG_MULTILEVEL_WITH_SHARED || MLDSA_L == 4 */
549
+ #endif /* MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L4 */
550
+
551
+ #if defined(MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L5)
552
+ #if defined(MLD_CONFIG_MULTILEVEL_WITH_SHARED) || MLDSA_L == 5
553
+ /**
554
+ * Native implementation of polyvecl_pointwise_acc_montgomery for MLDSA_L = 5.
555
+ *
556
+ * Pointwise multiply vectors of polynomials of length MLDSA_L, multiply
557
+ * resulting vector by 2^{-32} and add (accumulate) polynomials in it.
558
+ * Input/output vectors are in NTT domain representation.
559
+ *
560
+ * @param[out] w Output polynomial.
561
+ * @param[in] u First input vector.
562
+ * @param[in] v Second input vector.
563
+ */
564
+ MLD_MUST_CHECK_RETURN_VALUE
565
+ static MLD_INLINE int mld_polyvecl_pointwise_acc_montgomery_l5_native(
566
+ int32_t w[MLDSA_N], const int32_t u[5][MLDSA_N],
567
+ const int32_t v[5][MLDSA_N])
568
+ __contract__(
569
+ requires(memory_no_alias(w, sizeof(int32_t) * MLDSA_N))
570
+ requires(memory_no_alias(u, sizeof(int32_t) * 5 * MLDSA_N))
571
+ requires(memory_no_alias(v, sizeof(int32_t) * 5 * MLDSA_N))
572
+ requires(forall(l0, 0, 5,
573
+ array_bound(u[l0], 0, MLDSA_N, 0, MLDSA_Q)))
574
+ requires(forall(l1, 0, 5,
575
+ array_abs_bound(v[l1], 0, MLDSA_N, MLD_NTT_BOUND)))
576
+ assigns(memory_slice(w, sizeof(int32_t) * MLDSA_N))
577
+ ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == MLD_NATIVE_FUNC_SUCCESS)
578
+ ensures((return_value == MLD_NATIVE_FUNC_SUCCESS) ==> array_abs_bound(w, 0, MLDSA_N, MLDSA_Q))
579
+ ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_unchanged(w, MLDSA_N))
580
+ );
581
+ #endif /* MLD_CONFIG_MULTILEVEL_WITH_SHARED || MLDSA_L == 5 */
582
+ #endif /* MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L5 */
583
+
584
+ #if defined(MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L7)
585
+ #if defined(MLD_CONFIG_MULTILEVEL_WITH_SHARED) || MLDSA_L == 7
586
+ /**
587
+ * Native implementation of polyvecl_pointwise_acc_montgomery for MLDSA_L = 7.
588
+ *
589
+ * Pointwise multiply vectors of polynomials of length MLDSA_L, multiply
590
+ * resulting vector by 2^{-32} and add (accumulate) polynomials in it.
591
+ * Input/output vectors are in NTT domain representation.
592
+ *
593
+ * @param[out] w Output polynomial.
594
+ * @param[in] u First input vector.
595
+ * @param[in] v Second input vector.
596
+ */
597
+ MLD_MUST_CHECK_RETURN_VALUE
598
+ static MLD_INLINE int mld_polyvecl_pointwise_acc_montgomery_l7_native(
599
+ int32_t w[MLDSA_N], const int32_t u[7][MLDSA_N],
600
+ const int32_t v[7][MLDSA_N])
601
+ __contract__(
602
+ requires(memory_no_alias(w, sizeof(int32_t) * MLDSA_N))
603
+ requires(memory_no_alias(u, sizeof(int32_t) * 7 * MLDSA_N))
604
+ requires(memory_no_alias(v, sizeof(int32_t) * 7 * MLDSA_N))
605
+ requires(forall(l0, 0, 7,
606
+ array_bound(u[l0], 0, MLDSA_N, 0, MLDSA_Q)))
607
+ requires(forall(l1, 0, 7,
608
+ array_abs_bound(v[l1], 0, MLDSA_N, MLD_NTT_BOUND)))
609
+ assigns(memory_slice(w, sizeof(int32_t) * MLDSA_N))
610
+ ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == MLD_NATIVE_FUNC_SUCCESS)
611
+ ensures((return_value == MLD_NATIVE_FUNC_SUCCESS) ==> array_abs_bound(w, 0, MLDSA_N, MLDSA_Q))
612
+ ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_unchanged(w, MLDSA_N))
613
+ );
614
+ #endif /* MLD_CONFIG_MULTILEVEL_WITH_SHARED || MLDSA_L == 7 */
615
+ #endif /* MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L7 */
616
+
617
+ #endif /* !MLD_NATIVE_API_H */
@@ -0,0 +1,24 @@
1
+ /*
2
+ * Copyright (c) The mlkem-native project authors
3
+ * Copyright (c) The mldsa-native project authors
4
+ * SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
5
+ */
6
+
7
+ #ifndef MLD_NATIVE_META_H
8
+ #define MLD_NATIVE_META_H
9
+
10
+ /*
11
+ * Default arithmetic backend
12
+ */
13
+ #include "../sys.h"
14
+
15
+ #ifdef MLD_SYS_AARCH64_NEON
16
+ #include "aarch64/meta.h"
17
+ #endif
18
+
19
+ /* The x86_64 backend requires toolchain support for the SysV ABI */
20
+ #if defined(MLD_SYS_X86_64_AVX2) && defined(MLD_SYSV_ABI_SUPPORTED)
21
+ #include "x86_64/meta.h"
22
+ #endif
23
+
24
+ #endif /* !MLD_NATIVE_META_H */