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,233 @@
1
+ /*
2
+ * Copyright (c) The mldsa-native project authors
3
+ * SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
4
+ */
5
+
6
+ #ifndef MLD_CBMC_H
7
+ #define MLD_CBMC_H
8
+
9
+ /***************************************************
10
+ * Basic replacements for __CPROVER_XXX contracts
11
+ ***************************************************/
12
+ /*
13
+ * The `__contract__` / `__loop__` annotation macros use a
14
+ * leading-double-underscore spelling in line with other CBMC macros.
15
+ * clang-tidy flags these as reserved identifiers; we suppress the diagnostic
16
+ * at each definition site (NOLINT) rather than disabling the check globally,
17
+ * so it stays active for the rest of the tree.
18
+ */
19
+ #ifndef CBMC
20
+
21
+ /* clang-format off */
22
+ #define __contract__(x) /* NOLINT(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
23
+ #define __loop__(x) /* NOLINT(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
24
+ /* clang-format on */
25
+ #define cassert(x)
26
+
27
+ #else /* !CBMC */
28
+
29
+
30
+ /* clang-format off */
31
+ #define __contract__(x) x /* NOLINT(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
32
+ #define __loop__(x) x /* NOLINT(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
33
+ /* clang-format on */
34
+
35
+ /* Conditionally expand to __VA_ARGS__ depending on MLD_CONFIG_REDUCE_RAM. */
36
+ #if defined(MLD_CONFIG_REDUCE_RAM)
37
+ #define MLD_IF_REDUCE_RAM(...) __VA_ARGS__
38
+ #define MLD_IF_NOT_REDUCE_RAM(...)
39
+ #else
40
+ #define MLD_IF_REDUCE_RAM(...)
41
+ #define MLD_IF_NOT_REDUCE_RAM(...) __VA_ARGS__
42
+ #endif
43
+
44
+ /* https://diffblue.github.io/cbmc/contracts-assigns.html */
45
+ #define assigns(...) __CPROVER_assigns(__VA_ARGS__)
46
+
47
+ /* https://diffblue.github.io/cbmc/contracts-requires-ensures.html */
48
+ #define requires(...) __CPROVER_requires(__VA_ARGS__)
49
+ #define ensures(...) __CPROVER_ensures(__VA_ARGS__)
50
+ /* https://diffblue.github.io/cbmc/contracts-loops.html */
51
+ #define invariant(...) __CPROVER_loop_invariant(__VA_ARGS__)
52
+ #define decreases(...) __CPROVER_decreases(__VA_ARGS__)
53
+ /* cassert to avoid confusion with in-built assert */
54
+ #define cassert(x) __CPROVER_assert(x, "cbmc assertion failed")
55
+ #define assume(...) __CPROVER_assume(__VA_ARGS__)
56
+
57
+ /***************************************************
58
+ * Macros for "expression" forms that may appear
59
+ * _inside_ top-level contracts.
60
+ ***************************************************/
61
+
62
+ /*
63
+ * function return value - useful inside ensures
64
+ * https://diffblue.github.io/cbmc/contracts-functions.html
65
+ */
66
+ #define return_value (__CPROVER_return_value)
67
+
68
+ /*
69
+ * assigns l-value targets
70
+ * https://diffblue.github.io/cbmc/contracts-assigns.html
71
+ */
72
+ #define object_whole(...) __CPROVER_object_whole(__VA_ARGS__)
73
+ #define memory_slice(...) __CPROVER_object_upto(__VA_ARGS__)
74
+ #define same_object(...) __CPROVER_same_object(__VA_ARGS__)
75
+
76
+ /*
77
+ * Pointer-related predicates
78
+ * https://diffblue.github.io/cbmc/contracts-memory-predicates.html
79
+ */
80
+ #define memory_no_alias(...) __CPROVER_is_fresh(__VA_ARGS__)
81
+ #define readable(...) __CPROVER_r_ok(__VA_ARGS__)
82
+ #define writeable(...) __CPROVER_w_ok(__VA_ARGS__)
83
+
84
+ /* Maximum supported buffer size
85
+ *
86
+ * Larger buffers may be supported, but due to internal modeling constraints
87
+ * in CBMC, the proofs of memory- and type-safety won't be able to run.
88
+ *
89
+ * If you find yourself in need for a buffer size larger than this,
90
+ * please contact the maintainers, so we can prioritize work to relax
91
+ * this somewhat artificial bound.
92
+ */
93
+ #define MLD_MAX_BUFFER_SIZE (SIZE_MAX >> 12)
94
+
95
+
96
+ /*
97
+ * History variables
98
+ * https://diffblue.github.io/cbmc/contracts-history-variables.html
99
+ */
100
+ #define old(...) __CPROVER_old(__VA_ARGS__)
101
+ #define loop_entry(...) __CPROVER_loop_entry(__VA_ARGS__)
102
+
103
+ /*
104
+ * Quantifiers
105
+ * Note that the range on qvar is _exclusive_ between qvar_lb .. qvar_ub
106
+ * https://diffblue.github.io/cbmc/contracts-quantifiers.html
107
+ *
108
+ * The quantified variable is declared as uint32_t, so these macros
109
+ * quantify only over indices in [0, UINT32_MAX). Bounds larger than
110
+ * UINT32_MAX (4 GiB) are NOT supported: the explicit (uint32_t) casts
111
+ * on the bounds will trigger CBMC's conversion check if a wider bound
112
+ * (e.g. a size_t > UINT32_MAX) is passed.
113
+ *
114
+ * Quantifying over size_t (64-bit) was found to blow up SMT proof
115
+ * times, so we deliberately keep the index width at 32 bits. Callers
116
+ * dealing with size_t-typed buffers must add an explicit
117
+ * requires(len <= UINT32_MAX)
118
+ * precondition.
119
+ */
120
+
121
+ /*
122
+ * Prevent clang-format from corrupting CBMC's special ==> operator
123
+ */
124
+ /* clang-format off */
125
+ #define forall(qvar, qvar_lb, qvar_ub, predicate) \
126
+ __CPROVER_forall \
127
+ { \
128
+ uint32_t qvar; \
129
+ ((uint32_t) (qvar_lb) <= (qvar) && (qvar) < (uint32_t) (qvar_ub)) ==> (predicate) \
130
+ }
131
+
132
+ #define exists(qvar, qvar_lb, qvar_ub, predicate) \
133
+ __CPROVER_exists \
134
+ { \
135
+ uint32_t qvar; \
136
+ ((uint32_t) (qvar_lb) <= (qvar) && (qvar) < (uint32_t) (qvar_ub)) && (predicate) \
137
+ }
138
+ /* clang-format on */
139
+
140
+ /***************************************************
141
+ * Convenience macros for common contract patterns
142
+ ***************************************************/
143
+ /*
144
+ * Prevent clang-format from corrupting CBMC's special ==> operator
145
+ */
146
+ /* clang-format off */
147
+ #define CBMC_CONCAT_(left, right) left##right
148
+ #define CBMC_CONCAT(left, right) CBMC_CONCAT_(left, right)
149
+
150
+ #define array_bound_core(qvar, qvar_lb, qvar_ub, array_var, \
151
+ value_lb, value_ub) \
152
+ __CPROVER_forall \
153
+ { \
154
+ uint32_t qvar; \
155
+ ((uint32_t) (qvar_lb) <= (qvar) && (qvar) < (uint32_t) (qvar_ub)) ==> \
156
+ (((int)(value_lb) <= ((array_var)[(qvar)])) && \
157
+ (((array_var)[(qvar)]) < (int)(value_ub))) \
158
+ }
159
+
160
+ #define array_bound(array_var, qvar_lb, qvar_ub, value_lb, value_ub) \
161
+ array_bound_core(CBMC_CONCAT(_cbmc_idx, __COUNTER__), (qvar_lb), \
162
+ (qvar_ub), (array_var), (value_lb), (value_ub))
163
+
164
+ #define array_unchanged_core(qvar, qvar_lb, qvar_ub, array_var) \
165
+ __CPROVER_forall \
166
+ { \
167
+ uint32_t qvar; \
168
+ ((uint32_t) (qvar_lb) <= (qvar) && (qvar) < (uint32_t) (qvar_ub)) ==> \
169
+ ((array_var)[(qvar)]) == (old(* (int32_t (*)[(qvar_ub)])(array_var)))[(qvar)] \
170
+ }
171
+
172
+ #define array_unchanged(array_var, N) \
173
+ array_unchanged_core(CBMC_CONCAT(_cbmc_idx, __COUNTER__), 0, (N), (array_var))
174
+
175
+ #define array_unchanged_u64_core(qvar, qvar_lb, qvar_ub, array_var) \
176
+ __CPROVER_forall \
177
+ { \
178
+ uint32_t qvar; \
179
+ ((uint32_t) (qvar_lb) <= (qvar) && (qvar) < (uint32_t) (qvar_ub)) ==> \
180
+ ((array_var)[(qvar)]) == (old(* (uint64_t (*)[(qvar_ub)])(array_var)))[(qvar)] \
181
+ }
182
+
183
+ #define array_unchanged_u64(array_var, N) \
184
+ array_unchanged_u64_core(CBMC_CONCAT(_cbmc_idx, __COUNTER__), 0, (N), (array_var))
185
+
186
+ #define array_unchanged_u8_core(qvar, qvar_lb, qvar_ub, array_var) \
187
+ __CPROVER_forall \
188
+ { \
189
+ uint32_t qvar; \
190
+ ((uint32_t) (qvar_lb) <= (qvar) && (qvar) < (uint32_t) (qvar_ub)) ==> \
191
+ ((array_var)[(qvar)]) == (old(* (uint8_t (*)[(qvar_ub)])(array_var)))[(qvar)] \
192
+ }
193
+
194
+ #define array_unchanged_u8(array_var, N) \
195
+ array_unchanged_u8_core(CBMC_CONCAT(_cbmc_idx, __COUNTER__), 0, (N), (array_var))
196
+
197
+ #define array_zeroized_u8_core(qvar, qvar_lb, qvar_ub, array_var) \
198
+ __CPROVER_forall \
199
+ { \
200
+ uint32_t qvar; \
201
+ ((uint32_t) (qvar_lb) <= (qvar) && (qvar) < (uint32_t) (qvar_ub)) ==> \
202
+ ((array_var)[(qvar)]) == 0 \
203
+ }
204
+
205
+ #define array_zeroized_u8(array_var, N) \
206
+ array_zeroized_u8_core(CBMC_CONCAT(_cbmc_idx, __COUNTER__), 0, (N), (array_var))
207
+ /* clang-format on */
208
+
209
+ /*
210
+ * Output-buffer discipline on failure, as documented in API-CONVENTIONS.md:
211
+ * when a function fails, each caller-owned output buffer is left either
212
+ * fully unchanged or fully zeroized -- never holding partially computed or
213
+ * stale data that could be mistaken for a valid result.
214
+ *
215
+ * Note the disjunction is over the buffer as a whole: it is not enough for
216
+ * each byte to be individually either unchanged or zero.
217
+ */
218
+ #define array_unchanged_or_zeroized_u8(array_var, N) \
219
+ (array_unchanged_u8((array_var), (N)) || array_zeroized_u8((array_var), (N)))
220
+
221
+ /* Wrapper around array_bound operating on absolute values.
222
+ *
223
+ * The absolute value bound `k` is exclusive.
224
+ *
225
+ * Note that since the lower bound in array_bound is inclusive, we have to
226
+ * raise it by 1 here.
227
+ */
228
+ #define array_abs_bound(arr, lb, ub, k) \
229
+ array_bound((arr), (lb), (ub), -((int)(k)) + 1, (k))
230
+
231
+ #endif /* CBMC */
232
+
233
+ #endif /* !MLD_CBMC_H */
@@ -0,0 +1,301 @@
1
+ /*
2
+ * Copyright (c) The mldsa-native project authors
3
+ * Copyright (c) The mlkem-native project authors
4
+ * SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
5
+ */
6
+ #ifndef MLD_COMMON_H
7
+ #define MLD_COMMON_H
8
+
9
+ #ifndef __ASSEMBLER__
10
+ #include <stdint.h>
11
+ #endif
12
+
13
+
14
+ #define MLD_BUILD_INTERNAL
15
+
16
+ #if defined(MLD_CONFIG_FILE)
17
+ #include MLD_CONFIG_FILE
18
+ #else
19
+ #include "mldsa_native_config.h"
20
+ #endif
21
+
22
+ #include "params.h"
23
+ #include "sys.h"
24
+
25
+ /* Internal and public API have external linkage by default, but
26
+ * this can be overwritten by the user, e.g. for single-CU builds. */
27
+ #if !defined(MLD_CONFIG_INTERNAL_API_QUALIFIER)
28
+ #define MLD_INTERNAL_API
29
+ #define MLD_INTERNAL_DATA_DECLARATION extern
30
+ #define MLD_INTERNAL_DATA_DEFINITION
31
+ #else
32
+ #define MLD_INTERNAL_API MLD_CONFIG_INTERNAL_API_QUALIFIER
33
+ #define MLD_INTERNAL_DATA_DECLARATION MLD_CONFIG_INTERNAL_API_QUALIFIER
34
+ #define MLD_INTERNAL_DATA_DEFINITION MLD_CONFIG_INTERNAL_API_QUALIFIER
35
+ #endif
36
+
37
+ #if !defined(MLD_CONFIG_EXTERNAL_API_QUALIFIER)
38
+ #define MLD_EXTERNAL_API
39
+ #else
40
+ #define MLD_EXTERNAL_API MLD_CONFIG_EXTERNAL_API_QUALIFIER
41
+ #endif
42
+
43
+ #if defined(MLD_CONFIG_MULTILEVEL_NO_SHARED) || \
44
+ defined(MLD_CONFIG_MULTILEVEL_WITH_SHARED)
45
+ #define MLD_MULTILEVEL_BUILD
46
+ #endif
47
+
48
+ #define MLD_CONCAT_(x1, x2) x1##x2
49
+ #define MLD_CONCAT(x1, x2) MLD_CONCAT_(x1, x2)
50
+
51
+ #if defined(MLD_MULTILEVEL_BUILD)
52
+ #define MLD_ADD_PARAM_SET(s) MLD_CONCAT(s, MLD_CONFIG_PARAMETER_SET)
53
+ #else
54
+ #define MLD_ADD_PARAM_SET(s) s
55
+ #endif
56
+
57
+ #define MLD_NAMESPACE_PREFIX MLD_CONCAT(MLD_CONFIG_NAMESPACE_PREFIX, _)
58
+ #define MLD_NAMESPACE_PREFIX_KL \
59
+ MLD_CONCAT(MLD_ADD_PARAM_SET(MLD_CONFIG_NAMESPACE_PREFIX), _)
60
+
61
+ /* Functions are prefixed by MLD_CONFIG_NAMESPACE_PREFIX.
62
+ *
63
+ * If multiple parameter sets are used, functions depending on the parameter
64
+ * set are additionally prefixed with 44/65/87. See mldsa_native_config.h.
65
+ *
66
+ * Example: If MLD_CONFIG_NAMESPACE_PREFIX is PQCP_MLDSA_NATIVE, then
67
+ * MLD_NAMESPACE_KL(keypair) becomes PQCP_MLDSA_NATIVE44_keypair/
68
+ * PQCP_MLDSA_NATIVE65_keypair/PQCP_MLDSA_NATIVE87_keypair.
69
+ */
70
+ #define MLD_NAMESPACE(s) MLD_CONCAT(MLD_NAMESPACE_PREFIX, s)
71
+ #define MLD_NAMESPACE_KL(s) MLD_CONCAT(MLD_NAMESPACE_PREFIX_KL, s)
72
+
73
+ /* On Apple platforms, we need to emit leading underscore
74
+ * in front of assembly symbols. We thus introduce a separate
75
+ * namespace wrapper for ASM symbols. */
76
+ #if !defined(__APPLE__)
77
+ #define MLD_ASM_NAMESPACE(sym) MLD_NAMESPACE(sym)
78
+ #else
79
+ #define MLD_ASM_NAMESPACE(sym) MLD_CONCAT(_, MLD_NAMESPACE(sym))
80
+ #endif
81
+
82
+ /*
83
+ * On X86_64 if control-flow protections (CET) are enabled (through
84
+ * -fcf-protection=), we add an endbr64 instruction at every global function
85
+ * label. See sys.h for more details
86
+ */
87
+ #if defined(MLD_SYS_X86_64)
88
+ #define MLD_ASM_FN_SYMBOL(sym) MLD_ASM_NAMESPACE(sym) : MLD_CET_ENDBR
89
+ #elif defined(MLD_SYS_ARMV81M_MVE)
90
+ /* clang-format off */
91
+ #define MLD_ASM_FN_SYMBOL(sym) \
92
+ .type MLD_ASM_NAMESPACE(sym), %function; \
93
+ MLD_ASM_NAMESPACE(sym) :
94
+ /* clang-format on */
95
+ #else /* !MLD_SYS_X86_64 && MLD_SYS_ARMV81M_MVE */
96
+ #define MLD_ASM_FN_SYMBOL(sym) MLD_ASM_NAMESPACE(sym) :
97
+ #endif /* !MLD_SYS_X86_64 && !MLD_SYS_ARMV81M_MVE */
98
+
99
+ /*
100
+ * Output the size of an assembly function.
101
+ */
102
+ #if defined(__ELF__)
103
+ #define MLD_ASM_FN_SIZE(sym) \
104
+ .size MLD_ASM_NAMESPACE(sym), .- MLD_ASM_NAMESPACE(sym)
105
+ #else
106
+ #define MLD_ASM_FN_SIZE(sym)
107
+ #endif
108
+
109
+ /* We aim to simplify the user's life by supporting builds where
110
+ * all source files are included, even those that are not needed.
111
+ * Those files are appropriately guarded and will be empty when unneeded.
112
+ * The following is to avoid compilers complaining about this. */
113
+ #define MLD_EMPTY_CU(s) extern int MLD_NAMESPACE_KL(empty_cu_##s);
114
+
115
+ /* MLD_CONFIG_NO_ASM takes precedence over MLD_USE_NATIVE_XXX */
116
+ #if defined(MLD_CONFIG_NO_ASM)
117
+ #undef MLD_CONFIG_USE_NATIVE_BACKEND_ARITH
118
+ #undef MLD_CONFIG_USE_NATIVE_BACKEND_FIPS202
119
+ #endif
120
+
121
+ #if defined(MLD_CONFIG_USE_NATIVE_BACKEND_ARITH) && \
122
+ !defined(MLD_CONFIG_ARITH_BACKEND_FILE)
123
+ #error Bad configuration: MLD_CONFIG_USE_NATIVE_BACKEND_ARITH is set, but MLD_CONFIG_ARITH_BACKEND_FILE is not.
124
+ #endif
125
+
126
+ #if defined(MLD_CONFIG_USE_NATIVE_BACKEND_FIPS202) && \
127
+ !defined(MLD_CONFIG_FIPS202_BACKEND_FILE)
128
+ #error Bad configuration: MLD_CONFIG_USE_NATIVE_BACKEND_FIPS202 is set, but MLD_CONFIG_FIPS202_BACKEND_FILE is not.
129
+ #endif
130
+
131
+ #if defined(MLD_CONFIG_NO_RANDOMIZED_API) && defined(MLD_CONFIG_KEYGEN_PCT)
132
+ #error Bad configuration: MLD_CONFIG_NO_RANDOMIZED_API is incompatible with MLD_CONFIG_KEYGEN_PCT as the current PCT implementation requires signature()
133
+ #endif
134
+
135
+ #if defined(MLD_CONFIG_NO_SIGN_API) && defined(MLD_CONFIG_KEYGEN_PCT)
136
+ #error Bad configuration: MLD_CONFIG_NO_SIGN_API is incompatible with MLD_CONFIG_KEYGEN_PCT as the current PCT implementation requires signature()
137
+ #endif
138
+
139
+ #if defined(MLD_CONFIG_NO_VERIFY_API) && defined(MLD_CONFIG_KEYGEN_PCT)
140
+ #error Bad configuration: MLD_CONFIG_NO_VERIFY_API is incompatible with MLD_CONFIG_KEYGEN_PCT as the current PCT implementation requires verify()
141
+ #endif
142
+
143
+ #if defined(MLD_CONFIG_CORE_API_ONLY) && defined(MLD_CONFIG_KEYGEN_PCT)
144
+ #error Bad configuration: MLD_CONFIG_CORE_API_ONLY is incompatible with MLD_CONFIG_KEYGEN_PCT as the current PCT implementation requires signature() and verify()
145
+ #endif
146
+
147
+ #if defined(MLD_CONFIG_USE_NATIVE_BACKEND_ARITH)
148
+ #include MLD_CONFIG_ARITH_BACKEND_FILE
149
+ /* Include to enforce consistency of API and implementation,
150
+ * and conduct sanity checks on the backend.
151
+ *
152
+ * Keep this _after_ the inclusion of the backend; otherwise,
153
+ * the sanity checks won't have an effect. */
154
+ #if defined(MLD_CHECK_APIS) && !defined(__ASSEMBLER__)
155
+ #include "native/api.h"
156
+ #endif
157
+ #endif /* MLD_CONFIG_USE_NATIVE_BACKEND_ARITH */
158
+
159
+ #if defined(MLD_CONFIG_USE_NATIVE_BACKEND_FIPS202)
160
+ #include MLD_CONFIG_FIPS202_BACKEND_FILE
161
+ /* Include to enforce consistency of API and implementation,
162
+ * and conduct sanity checks on the backend.
163
+ *
164
+ * Keep this _after_ the inclusion of the backend; otherwise,
165
+ * the sanity checks won't have an effect. */
166
+ #if defined(MLD_CHECK_APIS) && !defined(__ASSEMBLER__)
167
+ #include "fips202/native/api.h"
168
+ #endif
169
+ #endif /* MLD_CONFIG_USE_NATIVE_BACKEND_FIPS202 */
170
+
171
+ #if !defined(MLD_CONFIG_FIPS202_CUSTOM_HEADER)
172
+ #define MLD_FIPS202_HEADER_FILE "fips202/fips202.h"
173
+ #else
174
+ #define MLD_FIPS202_HEADER_FILE MLD_CONFIG_FIPS202_CUSTOM_HEADER
175
+ #endif
176
+
177
+ #if !defined(MLD_CONFIG_FIPS202X4_CUSTOM_HEADER)
178
+ #define MLD_FIPS202X4_HEADER_FILE "fips202/fips202x4.h"
179
+ #else
180
+ #define MLD_FIPS202X4_HEADER_FILE MLD_CONFIG_FIPS202X4_CUSTOM_HEADER
181
+ #endif
182
+
183
+ /* Standard library function replacements */
184
+ #if !defined(__ASSEMBLER__)
185
+ #if !defined(MLD_CONFIG_CUSTOM_MEMCPY)
186
+ #include <string.h>
187
+ #define mld_memcpy memcpy
188
+ #endif
189
+
190
+ #if !defined(MLD_CONFIG_CUSTOM_MEMSET)
191
+ #include <string.h>
192
+ #define mld_memset memset
193
+ #endif
194
+
195
+ /* Allocation macros for large local structures
196
+ *
197
+ * MLD_ALLOC(v, T, N) declares T *v and attempts to point it to an T[N]
198
+ * MLD_FREE(v, T, N) zeroizes and frees the allocation
199
+ *
200
+ * Default implementation uses stack allocation.
201
+ * Can be overridden by setting the config option MLD_CONFIG_CUSTOM_ALLOC_FREE
202
+ * and defining MLD_CUSTOM_ALLOC and MLD_CUSTOM_FREE.
203
+ */
204
+ #if defined(MLD_CONFIG_CUSTOM_ALLOC_FREE) != \
205
+ (defined(MLD_CUSTOM_ALLOC) && defined(MLD_CUSTOM_FREE))
206
+ #error Bad configuration: MLD_CONFIG_CUSTOM_ALLOC_FREE must be set together with MLD_CUSTOM_ALLOC and MLD_CUSTOM_FREE
207
+ #endif
208
+
209
+ /* Context-parameter machinery (MLD_CONTEXT_PARAMETERS_n and related config
210
+ * checks). Kept in a separate, level-generic header for readability; included
211
+ * here so it is available to the allocation macros below and to all consumers
212
+ * of common.h. */
213
+ #include "context.h"
214
+
215
+ #if !defined(MLD_CONFIG_CUSTOM_ALLOC_FREE)
216
+ /* Default: stack allocation */
217
+
218
+ /* This is a declaration macro, not an expression macro: T is a type and v is
219
+ * a declarator, neither of which can be wrapped in parentheses. The
220
+ * bugprone-macro-parentheses diagnostic is therefore a false positive here. */
221
+ #define MLD_ALLOC(v, T, N, context) \
222
+ MLD_ALIGN T mld_alloc_##v[N]; \
223
+ T *v = mld_alloc_##v /* NOLINT(bugprone-macro-parentheses) */
224
+
225
+ /* The MLD_FREE macro body references mld_zeroize(), which is declared in
226
+ * ct.h. We deliberately do NOT include ct.h here: doing so would create a
227
+ * circular dependency (ct.h includes common.h), and common.h itself never
228
+ * calls mld_zeroize() -- only the macro expansion does. Each translation
229
+ * unit that uses MLD_FREE therefore includes ct.h directly. */
230
+ #define MLD_FREE(v, T, N, context) \
231
+ do \
232
+ { \
233
+ MLD_CONTEXT_UNUSED(context); \
234
+ mld_zeroize(mld_alloc_##v, sizeof(mld_alloc_##v)); \
235
+ (v) = NULL; \
236
+ } while (0)
237
+
238
+ #else /* !MLD_CONFIG_CUSTOM_ALLOC_FREE */
239
+
240
+ /* Custom allocation */
241
+
242
+ /*
243
+ * The indirection here is necessary to use MLD_CONTEXT_PARAMETERS_3 here.
244
+ */
245
+ #define MLD_APPLY(f, args) f args
246
+
247
+ #define MLD_ALLOC(v, T, N, context) \
248
+ MLD_APPLY(MLD_CUSTOM_ALLOC, MLD_CONTEXT_PARAMETERS_3(v, T, N, context))
249
+
250
+ #define MLD_FREE(v, T, N, context) \
251
+ do \
252
+ { \
253
+ if (v != NULL) \
254
+ { \
255
+ mld_zeroize(v, sizeof(T) * (N)); \
256
+ MLD_APPLY(MLD_CUSTOM_FREE, MLD_CONTEXT_PARAMETERS_3(v, T, N, context)); \
257
+ v = NULL; \
258
+ } \
259
+ } while (0)
260
+
261
+ #endif /* MLD_CONFIG_CUSTOM_ALLOC_FREE */
262
+
263
+ /****************************** Error codes ***********************************/
264
+
265
+ /* Generic failure condition, reserved for failures not covered by a more
266
+ * specific error code. */
267
+ #define MLD_ERR_FAIL (-1)
268
+ /* An allocation failed. This can only happen if MLD_CONFIG_CUSTOM_ALLOC_FREE
269
+ * is defined and the provided MLD_CUSTOM_ALLOC can fail. */
270
+ #define MLD_ERR_OUT_OF_MEMORY (-2)
271
+ /* An RNG failure occurred. Might be due to insufficient entropy or
272
+ * system misconfiguration. */
273
+ #define MLD_ERR_RNG_FAIL (-3)
274
+ /* The signing rejection-sampling loop exceeded
275
+ * MLD_CONFIG_MAX_SIGNING_ATTEMPTS iterations without producing a valid
276
+ * signature. With a FIPS 204 Appendix C compliant bound (>= 821) this
277
+ * has probability < 2^-256. */
278
+ #define MLD_ERR_SIGN_ATTEMPTS_EXHAUSTED (-4)
279
+ /* Signing was paused before completing, at the request of a caller-provided
280
+ * MLD_CONFIG_SIGN_HOOK_ATTEMPT hook (see mldsa_native_config.h). The caller
281
+ * resumes by re-invoking signing with the same inputs; the attempt hook,
282
+ * together with MLD_CONFIG_SIGN_HOOK_RESUME, decides where to continue. */
283
+ #define MLD_ERR_SIGNING_PAUSED (-5)
284
+ /* Signature verification failed: the signature is not valid for the given
285
+ * message and public key. Returned by the verification API. */
286
+ #define MLD_ERR_INVALID_SIGNATURE (-6)
287
+ /* Secret key validation failed: the secret key is malformed or internally
288
+ * inconsistent. Returned by pk_from_sk. */
289
+ #define MLD_ERR_INVALID_KEY (-7)
290
+ /* The Pairwise Consistency Test failed. Only possible when
291
+ * MLD_CONFIG_KEYGEN_PCT is enabled; signals that the freshly generated key
292
+ * pair failed its sign/verify self-test. */
293
+ #define MLD_ERR_PCT_FAIL (-8)
294
+ /* An argument was invalid, e.g. an unsupported pre-hash algorithm or a context
295
+ * string longer than 255 bytes. */
296
+ #define MLD_ERR_INVALID_ARG (-9)
297
+
298
+
299
+ #endif /* !__ASSEMBLER__ */
300
+
301
+ #endif /* !MLD_COMMON_H */
@@ -0,0 +1,152 @@
1
+ /*
2
+ * Copyright (c) The mldsa-native project authors
3
+ * SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
4
+ */
5
+ #ifndef MLD_CONTEXT_H
6
+ #define MLD_CONTEXT_H
7
+
8
+ /* This header is included by common.h once the configuration has been pulled
9
+ * in; it is not meant to be included directly. */
10
+ #if !defined(__ASSEMBLER__)
11
+
12
+ #include <stdint.h>
13
+ #include "cbmc.h"
14
+ #include "sys.h"
15
+
16
+ /*
17
+ * If the integration wants to provide a context parameter for use in
18
+ * platform-specific hooks, then it should define this parameter.
19
+ *
20
+ * The MLD_CONTEXT_PARAMETERS_n macros are intended to be used with macros
21
+ * defining the function names and expand to either pass or discard the context
22
+ * argument as required by the current build. If there is no context parameter
23
+ * requested then these are removed from the prototypes and from all calls.
24
+ */
25
+ #ifdef MLD_CONFIG_CONTEXT_PARAMETER
26
+ #define MLD_CONTEXT_PARAMETERS_0(context) (context)
27
+ #define MLD_CONTEXT_PARAMETERS_1(arg0, context) (arg0, context)
28
+ #define MLD_CONTEXT_PARAMETERS_2(arg0, arg1, context) (arg0, arg1, context)
29
+ #define MLD_CONTEXT_PARAMETERS_3(arg0, arg1, arg2, context) \
30
+ (arg0, arg1, arg2, context)
31
+ #define MLD_CONTEXT_PARAMETERS_4(arg0, arg1, arg2, arg3, context) \
32
+ (arg0, arg1, arg2, arg3, context)
33
+ #define MLD_CONTEXT_PARAMETERS_5(arg0, arg1, arg2, arg3, arg4, context) \
34
+ (arg0, arg1, arg2, arg3, arg4, context)
35
+ #define MLD_CONTEXT_PARAMETERS_6(arg0, arg1, arg2, arg3, arg4, arg5, context) \
36
+ (arg0, arg1, arg2, arg3, arg4, arg5, context)
37
+ #define MLD_CONTEXT_PARAMETERS_7(arg0, arg1, arg2, arg3, arg4, arg5, arg6, \
38
+ context) \
39
+ (arg0, arg1, arg2, arg3, arg4, arg5, arg6, context)
40
+ #define MLD_CONTEXT_PARAMETERS_8(arg0, arg1, arg2, arg3, arg4, arg5, arg6, \
41
+ arg7, context) \
42
+ (arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, context)
43
+ #define MLD_CONTEXT_PARAMETERS_9(arg0, arg1, arg2, arg3, arg4, arg5, arg6, \
44
+ arg7, arg8, context) \
45
+ (arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, context)
46
+ #else /* MLD_CONFIG_CONTEXT_PARAMETER */
47
+ #define MLD_CONTEXT_PARAMETERS_0(context) ()
48
+ #define MLD_CONTEXT_PARAMETERS_1(arg0, context) (arg0)
49
+ #define MLD_CONTEXT_PARAMETERS_2(arg0, arg1, context) (arg0, arg1)
50
+ #define MLD_CONTEXT_PARAMETERS_3(arg0, arg1, arg2, context) (arg0, arg1, arg2)
51
+ #define MLD_CONTEXT_PARAMETERS_4(arg0, arg1, arg2, arg3, context) \
52
+ (arg0, arg1, arg2, arg3)
53
+ #define MLD_CONTEXT_PARAMETERS_5(arg0, arg1, arg2, arg3, arg4, context) \
54
+ (arg0, arg1, arg2, arg3, arg4)
55
+ #define MLD_CONTEXT_PARAMETERS_6(arg0, arg1, arg2, arg3, arg4, arg5, context) \
56
+ (arg0, arg1, arg2, arg3, arg4, arg5)
57
+ #define MLD_CONTEXT_PARAMETERS_7(arg0, arg1, arg2, arg3, arg4, arg5, arg6, \
58
+ context) \
59
+ (arg0, arg1, arg2, arg3, arg4, arg5, arg6)
60
+ #define MLD_CONTEXT_PARAMETERS_8(arg0, arg1, arg2, arg3, arg4, arg5, arg6, \
61
+ arg7, context) \
62
+ (arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7)
63
+ #define MLD_CONTEXT_PARAMETERS_9(arg0, arg1, arg2, arg3, arg4, arg5, arg6, \
64
+ arg7, arg8, context) \
65
+ (arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
66
+ #endif /* !MLD_CONFIG_CONTEXT_PARAMETER */
67
+
68
+ /* Consume a context parameter carried only for the integration's benefit,
69
+ * avoiding -Wunused-parameter; expands to nothing when no context is
70
+ * configured. */
71
+ #if defined(MLD_CONFIG_CONTEXT_PARAMETER)
72
+ #define MLD_CONTEXT_UNUSED(context) ((void)(context))
73
+ #else
74
+ #define MLD_CONTEXT_UNUSED(context) ((void)0)
75
+ #endif
76
+
77
+ #if defined(MLD_CONFIG_CONTEXT_PARAMETER_TYPE) != \
78
+ defined(MLD_CONFIG_CONTEXT_PARAMETER)
79
+ #error MLD_CONFIG_CONTEXT_PARAMETER_TYPE must be defined if and only if MLD_CONFIG_CONTEXT_PARAMETER is defined
80
+ #endif
81
+
82
+ /* The signing hooks tie into the rejection-sampling loop. A pausing attempt
83
+ * hook only reproduces the uninterrupted signature if the randomness is fixed
84
+ * across calls, and thus requires the deterministic API.
85
+ * For now we impose that requirement on all three hooks uniformly: enabling any
86
+ * of them requires MLD_CONFIG_NO_RANDOMIZED_API. This also rules out
87
+ * MLD_CONFIG_KEYGEN_PCT (whose PCT needs the randomized signature(), see
88
+ * common.h).
89
+ *
90
+ * A logging-only use (attempt always returns 0; resume/finish merely observe)
91
+ * would be safe with the randomized API too, but the restriction is applied
92
+ * uniformly for now. */
93
+ #if (defined(MLD_CONFIG_SIGN_HOOK_RESUME) || \
94
+ defined(MLD_CONFIG_SIGN_HOOK_ATTEMPT) || \
95
+ defined(MLD_CONFIG_SIGN_HOOK_FINISH)) && \
96
+ !defined(MLD_CONFIG_NO_RANDOMIZED_API)
97
+ #error Signing hooks (MLD_CONFIG_SIGN_HOOK_RESUME / _ATTEMPT / _FINISH) require MLD_CONFIG_NO_RANDOMIZED_API
98
+ #endif /* (MLD_CONFIG_SIGN_HOOK_RESUME || MLD_CONFIG_SIGN_HOOK_ATTEMPT || \
99
+ MLD_CONFIG_SIGN_HOOK_FINISH) && !MLD_CONFIG_NO_RANDOMIZED_API */
100
+
101
+ /* Signing hooks (MLD_CONFIG_SIGN_HOOK_RESUME / _ATTEMPT / _FINISH; documented
102
+ * in mldsa_native_config.h). The following macros route the call sites to
103
+ * mld_sign_hook_*, appending or dropping the context argument; each unset hook
104
+ * uses the dummy below. */
105
+ #define mld_sign_resume mld_sign_hook_resume MLD_CONTEXT_PARAMETERS_0
106
+ #define mld_sign_attempt mld_sign_hook_attempt MLD_CONTEXT_PARAMETERS_1
107
+ #define mld_sign_finish mld_sign_hook_finish MLD_CONTEXT_PARAMETERS_1
108
+
109
+ /* We don't use mld_sign_resume here because MLD_CONTEXT_PARAMETERS_0 is
110
+ * unsuitable for function declarations: it misses `void` as the placeholder
111
+ * argument. */
112
+ #if !defined(MLD_CONFIG_SIGN_HOOK_RESUME)
113
+ MLD_MUST_CHECK_RETURN_VALUE
114
+ static MLD_INLINE uint16_t mld_sign_hook_resume(
115
+ #if defined(MLD_CONFIG_CONTEXT_PARAMETER)
116
+ MLD_CONFIG_CONTEXT_PARAMETER_TYPE context
117
+ #else
118
+ void
119
+ #endif
120
+ )
121
+ __contract__(assigns() ensures(1))
122
+ {
123
+ MLD_CONTEXT_UNUSED(context);
124
+ return 0;
125
+ }
126
+ #endif /* !MLD_CONFIG_SIGN_HOOK_RESUME */
127
+
128
+ #if !defined(MLD_CONFIG_SIGN_HOOK_ATTEMPT)
129
+ MLD_MUST_CHECK_RETURN_VALUE
130
+ static MLD_INLINE int mld_sign_attempt(
131
+ uint16_t attempt, MLD_CONFIG_CONTEXT_PARAMETER_TYPE context)
132
+ __contract__(assigns() ensures(1))
133
+ {
134
+ ((void)attempt);
135
+ MLD_CONTEXT_UNUSED(context);
136
+ return 0;
137
+ }
138
+ #endif /* !MLD_CONFIG_SIGN_HOOK_ATTEMPT */
139
+
140
+ #if !defined(MLD_CONFIG_SIGN_HOOK_FINISH)
141
+ static MLD_INLINE void mld_sign_finish(
142
+ uint16_t attempt, MLD_CONFIG_CONTEXT_PARAMETER_TYPE context)
143
+ __contract__(assigns() ensures(1))
144
+ {
145
+ ((void)attempt);
146
+ MLD_CONTEXT_UNUSED(context);
147
+ }
148
+ #endif /* !MLD_CONFIG_SIGN_HOOK_FINISH */
149
+
150
+ #endif /* !__ASSEMBLER__ */
151
+
152
+ #endif /* !MLD_CONTEXT_H */