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,21 @@
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
+ #include "ct.h"
7
+
8
+ #if !defined(MLD_USE_ASM_VALUE_BARRIER) && \
9
+ !defined(MLD_CONFIG_MULTILEVEL_NO_SHARED)
10
+ /*
11
+ * Masking value used in constant-time functions from
12
+ * ct.h to block the compiler's range analysis and
13
+ * thereby reduce the risk of compiler-introduced branches.
14
+ */
15
+ volatile uint64_t mld_ct_opt_blocker_u64 = 0;
16
+
17
+ #else /* !MLD_USE_ASM_VALUE_BARRIER && !MLD_CONFIG_MULTILEVEL_NO_SHARED */
18
+
19
+ MLD_EMPTY_CU(ct)
20
+
21
+ #endif /* !(!MLD_USE_ASM_VALUE_BARRIER && !MLD_CONFIG_MULTILEVEL_NO_SHARED) */
@@ -0,0 +1,373 @@
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
+ /* References
8
+ * ==========
9
+ *
10
+ * - [FIPS204]
11
+ * FIPS 204 Module-Lattice-Based Digital Signature Standard
12
+ * National Institute of Standards and Technology
13
+ * https://csrc.nist.gov/pubs/fips/204/final
14
+ *
15
+ * - [libmceliece]
16
+ * libmceliece implementation of Classic McEliece
17
+ * Bernstein, Chou
18
+ * https://lib.mceliece.org/
19
+ *
20
+ * - [optblocker]
21
+ * PQC forum post on opt-blockers using volatile globals
22
+ * Daniel J. Bernstein
23
+ * https://groups.google.com/a/list.nist.gov/g/pqc-forum/c/hqbtIGFKIpU/m/H14H0wOlBgAJ
24
+ */
25
+
26
+ #ifndef MLD_CT_H
27
+ #define MLD_CT_H
28
+
29
+ #include "cbmc.h"
30
+ #include "common.h"
31
+
32
+ /* Constant-time comparisons and conditional operations
33
+
34
+ We reduce the risk for compilation into variable-time code
35
+ through the use of 'value barriers'.
36
+
37
+ Functionally, a value barrier is a no-op. To the compiler, however,
38
+ it constitutes an arbitrary modification of its input, and therefore
39
+ harden's value propagation and range analysis.
40
+
41
+ We consider two approaches to implement a value barrier:
42
+ - An empty inline asm block which marks the target value as clobbered.
43
+ - XOR'ing with the value of a volatile global that's set to 0;
44
+ see @[optblocker] for a discussion of this idea, and
45
+ @[libmceliece, inttypes/crypto_intN.h] for an implementation.
46
+
47
+ The first approach is cheap because it only prevents the compiler
48
+ from reasoning about the value of the variable past the barrier,
49
+ but does not directly generate additional instructions.
50
+
51
+ The second approach generates redundant loads and XOR operations
52
+ and therefore comes at a higher runtime cost. However, it appears
53
+ more robust towards optimization, as compilers should never drop
54
+ a volatile load.
55
+
56
+ We use the empty-ASM value barrier for GCC and clang, and fall
57
+ back to the global volatile barrier otherwise.
58
+
59
+ The global value barrier can be forced by setting
60
+ MLD_CONFIG_NO_ASM_VALUE_BARRIER.
61
+
62
+ */
63
+
64
+ #if defined(MLD_HAVE_INLINE_ASM) && !defined(MLD_CONFIG_NO_ASM_VALUE_BARRIER)
65
+ #define MLD_USE_ASM_VALUE_BARRIER
66
+ #endif
67
+
68
+
69
+ #if !defined(MLD_USE_ASM_VALUE_BARRIER)
70
+ /*
71
+ * Declaration of global volatile that the global value barrier
72
+ * is loading from and masking with.
73
+ */
74
+ #define mld_ct_opt_blocker_u64 MLD_NAMESPACE(ct_opt_blocker_u64)
75
+ extern volatile uint64_t mld_ct_opt_blocker_u64;
76
+
77
+
78
+ /* Helper functions for obtaining global masks of various sizes */
79
+
80
+ /* This contract is not proved but treated as an axiom.
81
+ *
82
+ * Its validity relies on the assumption that the global opt-blocker
83
+ * constant mld_ct_opt_blocker_u64 is not modified.
84
+ */
85
+ MLD_MUST_CHECK_RETURN_VALUE
86
+ static MLD_INLINE uint64_t mld_ct_get_optblocker_u64(void)
87
+ __contract__(ensures(return_value == 0)) { return mld_ct_opt_blocker_u64; }
88
+
89
+ MLD_MUST_CHECK_RETURN_VALUE
90
+ static MLD_INLINE int64_t mld_ct_get_optblocker_i64(void)
91
+ __contract__(ensures(return_value == 0)) { return (int64_t)mld_ct_get_optblocker_u64(); }
92
+
93
+ MLD_MUST_CHECK_RETURN_VALUE
94
+ static MLD_INLINE uint32_t mld_ct_get_optblocker_u32(void)
95
+ __contract__(ensures(return_value == 0)) { return (uint32_t)mld_ct_get_optblocker_u64(); }
96
+
97
+ MLD_MUST_CHECK_RETURN_VALUE
98
+ static MLD_INLINE uint8_t mld_ct_get_optblocker_u8(void)
99
+ __contract__(ensures(return_value == 0)) { return (uint8_t)mld_ct_get_optblocker_u64(); }
100
+
101
+ /* Opt-blocker based implementation of value barriers */
102
+ MLD_MUST_CHECK_RETURN_VALUE
103
+ static MLD_INLINE int64_t mld_value_barrier_i64(int64_t b)
104
+ __contract__(ensures(return_value == b)) { return (b ^ mld_ct_get_optblocker_i64()); }
105
+
106
+ MLD_MUST_CHECK_RETURN_VALUE
107
+ static MLD_INLINE uint32_t mld_value_barrier_u32(uint32_t b)
108
+ __contract__(ensures(return_value == b)) { return (b ^ mld_ct_get_optblocker_u32()); }
109
+
110
+ MLD_MUST_CHECK_RETURN_VALUE
111
+ static MLD_INLINE uint8_t mld_value_barrier_u8(uint8_t b)
112
+ __contract__(ensures(return_value == b)) { return (b ^ mld_ct_get_optblocker_u8()); }
113
+
114
+
115
+ #else /* !MLD_USE_ASM_VALUE_BARRIER */
116
+ MLD_MUST_CHECK_RETURN_VALUE
117
+ static MLD_INLINE int64_t mld_value_barrier_i64(int64_t b)
118
+ __contract__(ensures(return_value == b))
119
+ {
120
+ __asm__ volatile("" : "+r"(b));
121
+ return b;
122
+ }
123
+
124
+ MLD_MUST_CHECK_RETURN_VALUE
125
+ static MLD_INLINE uint32_t mld_value_barrier_u32(uint32_t b)
126
+ __contract__(ensures(return_value == b))
127
+ {
128
+ __asm__ volatile("" : "+r"(b));
129
+ return b;
130
+ }
131
+
132
+ MLD_MUST_CHECK_RETURN_VALUE
133
+ static MLD_INLINE uint8_t mld_value_barrier_u8(uint8_t b)
134
+ __contract__(ensures(return_value == b))
135
+ {
136
+ __asm__ volatile("" : "+r"(b));
137
+ return b;
138
+ }
139
+ #endif /* MLD_USE_ASM_VALUE_BARRIER */
140
+
141
+ #ifdef CBMC
142
+ #pragma CPROVER check push
143
+ #pragma CPROVER check disable "conversion"
144
+ #endif
145
+
146
+ /**
147
+ * Cast uint32 value to int32.
148
+ *
149
+ * @param x Input value.
150
+ *
151
+ * @return For uint32_t x, the unique y in int32_t so that x == y mod 2^32.
152
+ * Concretely:
153
+ * - x < 2^31: returns x
154
+ * - x >= 2^31: returns x - 2^32
155
+ */
156
+ MLD_MUST_CHECK_RETURN_VALUE
157
+ static MLD_ALWAYS_INLINE int32_t mld_cast_uint32_to_int32(uint32_t x)
158
+ {
159
+ /*
160
+ * PORTABILITY: This relies on uint32_t -> int32_t
161
+ * being implemented as the inverse of int32_t -> uint32_t,
162
+ * which is implementation-defined (C99 6.3.1.3 (3))
163
+ * CBMC (correctly) fails to prove this conversion is OK,
164
+ * so we have to suppress that check here
165
+ */
166
+ return (int32_t)x;
167
+ }
168
+
169
+ #ifdef CBMC
170
+ #pragma CPROVER check pop
171
+ #endif
172
+
173
+
174
+ /**
175
+ * Cast int64 value to uint32 as per C standard.
176
+ *
177
+ * @param x Input value.
178
+ *
179
+ * @return For int64_t x, the unique y in uint32_t so that x == y mod 2^32.
180
+ */
181
+ MLD_MUST_CHECK_RETURN_VALUE
182
+ static MLD_ALWAYS_INLINE uint32_t mld_cast_int64_to_uint32(int64_t x)
183
+ {
184
+ return (uint32_t)(x & (int64_t)UINT32_MAX);
185
+ }
186
+
187
+ /**
188
+ * Cast int32 value to uint32 as per C standard.
189
+ *
190
+ * @param x Input value.
191
+ *
192
+ * @return For int32_t x, the unique y in uint32_t so that x == y mod 2^32.
193
+ */
194
+ MLD_MUST_CHECK_RETURN_VALUE
195
+ static MLD_ALWAYS_INLINE uint32_t mld_cast_int32_to_uint32(int32_t x)
196
+ {
197
+ return mld_cast_int64_to_uint32((int64_t)x);
198
+ }
199
+
200
+ /**
201
+ * Functionally equivalent to cond ? a : b, but implemented with guards against
202
+ * compiler-introduced branches.
203
+ *
204
+ * @param a First alternative.
205
+ * @param b Second alternative.
206
+ * @param cond Condition variable.
207
+ *
208
+ * @return a if cond is 0xFFFFFFFF, b if cond is 0.
209
+ */
210
+ MLD_MUST_CHECK_RETURN_VALUE
211
+ static MLD_INLINE int32_t mld_ct_sel_int32(int32_t a, int32_t b, uint32_t cond)
212
+ __contract__(
213
+ requires(cond == 0x0 || cond == 0xFFFFFFFF)
214
+ ensures(return_value == (cond ? a : b))
215
+ )
216
+ {
217
+ uint32_t au = mld_cast_int32_to_uint32(a);
218
+ uint32_t bu = mld_cast_int32_to_uint32(b);
219
+ uint32_t res = bu ^ (mld_value_barrier_u32(cond) & (au ^ bu));
220
+ return mld_cast_uint32_to_int32(res);
221
+ }
222
+
223
+ /**
224
+ * Return 0 if input is zero, and -1 otherwise.
225
+ *
226
+ * @param x Value to be converted into a mask.
227
+ */
228
+ MLD_MUST_CHECK_RETURN_VALUE
229
+ static MLD_INLINE uint32_t mld_ct_cmask_nonzero_u32(uint32_t x)
230
+ __contract__(ensures(return_value == ((x == 0) ? 0 : 0xFFFFFFFF)))
231
+ {
232
+ int64_t tmp = mld_value_barrier_i64(-((int64_t)x));
233
+ /*
234
+ * PORTABILITY: Right-shift on a signed integer is
235
+ * implementation-defined for negative left argument.
236
+ * Here, we assume it's sign-preserving "arithmetic" shift right.
237
+ * See (C99 6.5.7 (5))
238
+ */
239
+ tmp >>= 32;
240
+ return mld_cast_int64_to_uint32(tmp);
241
+ }
242
+
243
+ /**
244
+ * Return 0 if input is zero, and -1 otherwise.
245
+ *
246
+ * @param x Value to be converted into a mask.
247
+ */
248
+ MLD_MUST_CHECK_RETURN_VALUE
249
+ static MLD_INLINE uint8_t mld_ct_cmask_nonzero_u8(uint8_t x)
250
+ __contract__(ensures(return_value == ((x == 0) ? 0 : 0xFF)))
251
+ {
252
+ uint32_t mask = mld_ct_cmask_nonzero_u32((uint32_t)x);
253
+ return (uint8_t)(mask & 0xFF);
254
+ }
255
+
256
+ /**
257
+ * Return 0 if input is non-negative, and -1 otherwise.
258
+ *
259
+ * @param x Value to be converted into a mask.
260
+ */
261
+ MLD_MUST_CHECK_RETURN_VALUE
262
+ static MLD_INLINE uint32_t mld_ct_cmask_neg_i32(int32_t x)
263
+ __contract__(
264
+ ensures(return_value == ((x < 0) ? 0xFFFFFFFF : 0))
265
+ )
266
+ {
267
+ int64_t tmp = mld_value_barrier_i64((int64_t)x);
268
+ /*
269
+ * PORTABILITY: Right-shift on a signed integer is
270
+ * implementation-defined for negative left argument.
271
+ * Here, we assume it's sign-preserving "arithmetic" shift right.
272
+ * See (C99 6.5.7 (5))
273
+ */
274
+ tmp >>= 31;
275
+ return mld_cast_int64_to_uint32(tmp);
276
+ }
277
+
278
+ /**
279
+ * Return -x if x<0, x otherwise.
280
+ *
281
+ * @param x Input value.
282
+ */
283
+ MLD_MUST_CHECK_RETURN_VALUE
284
+ static MLD_INLINE int32_t mld_ct_abs_i32(int32_t x)
285
+ __contract__(
286
+ requires(x >= -INT32_MAX)
287
+ ensures(return_value == ((x < 0) ? -x : x))
288
+ )
289
+ {
290
+ return mld_ct_sel_int32(-x, x, mld_ct_cmask_neg_i32(x));
291
+ }
292
+
293
+ /**
294
+ * Compare two arrays for equality in constant time.
295
+ *
296
+ * @param[in] a Pointer to first byte array.
297
+ * @param[in] b Pointer to second byte array.
298
+ * @param len Length of the byte arrays, upper-bounded to UINT16_MAX to
299
+ * control proof complexity only.
300
+ *
301
+ * @return 0 if the byte arrays are equal, 0xFF otherwise.
302
+ */
303
+ MLD_MUST_CHECK_RETURN_VALUE
304
+ static MLD_INLINE uint8_t mld_ct_memcmp(const uint8_t *a, const uint8_t *b,
305
+ const size_t len)
306
+ __contract__(
307
+ requires(len <= UINT16_MAX)
308
+ requires(memory_no_alias(a, len))
309
+ requires(memory_no_alias(b, len))
310
+ ensures((return_value == 0) || (return_value == 0xFF))
311
+ ensures((return_value == 0) == forall(i, 0, len, (a[i] == b[i]))))
312
+ {
313
+ uint8_t r = 0, s = 0;
314
+ unsigned i;
315
+
316
+ for (i = 0; i < len; i++)
317
+ __loop__(
318
+ invariant(i <= len)
319
+ invariant((r == 0) == (forall(k, 0, i, (a[k] == b[k]))))
320
+ decreases(len - i))
321
+ {
322
+ r |= a[i] ^ b[i];
323
+ /* s is useless, but prevents the loop from being aborted once r=0xff. */
324
+ s ^= a[i] ^ b[i];
325
+ }
326
+
327
+ /*
328
+ * - Convert r into a mask; this may not be necessary, but is an additional
329
+ * safeguard
330
+ * towards leaking information about a and b.
331
+ * - XOR twice with s, separated by a value barrier, to prevent the compile
332
+ * from dropping the s computation in the loop.
333
+ */
334
+ return (mld_value_barrier_u8(mld_ct_cmask_nonzero_u8(r) ^ s) ^ s);
335
+ }
336
+
337
+ /**
338
+ * Force-zeroize a buffer.
339
+ *
340
+ * @[FIPS204, Section 3.6.3] Destruction of intermediate values.
341
+ *
342
+ * @param[out] ptr Pointer to buffer to be zeroed.
343
+ * @param len Amount of bytes to be zeroed.
344
+ */
345
+ #if !defined(MLD_CONFIG_CUSTOM_ZEROIZE)
346
+ #if defined(MLD_SYS_WINDOWS)
347
+ #include <windows.h>
348
+ #elif !defined(MLD_HAVE_INLINE_ASM)
349
+ #error No plausibly-secure implementation of mld_zeroize available. Please provide your own using MLD_CONFIG_CUSTOM_ZEROIZE.
350
+ #endif
351
+
352
+ static MLD_INLINE void mld_zeroize(void *ptr, size_t len)
353
+ __contract__(
354
+ requires(len <= UINT32_MAX)
355
+ requires(memory_no_alias(ptr, len))
356
+ assigns(memory_slice(ptr, len))
357
+ ensures(array_zeroized_u8((uint8_t *)ptr, len)))
358
+ {
359
+ #if defined(MLD_SYS_WINDOWS)
360
+ SecureZeroMemory(ptr, len);
361
+ #else
362
+ mld_memset(ptr, 0, len);
363
+ /* This follows OpenSSL and seems sufficient to prevent the compiler
364
+ * from optimizing away the memset.
365
+ *
366
+ * If there was a reliable way to detect availability of memset_s(),
367
+ * that would be preferred. */
368
+ __asm__ __volatile__("" : : "r"(ptr) : "memory");
369
+ #endif /* !MLD_SYS_WINDOWS */
370
+ }
371
+ #endif /* !MLD_CONFIG_CUSTOM_ZEROIZE */
372
+
373
+ #endif /* !MLD_CT_H */
@@ -0,0 +1,75 @@
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
+ /* NOTE: You can remove this file unless you compile with MLDSA_DEBUG. */
8
+
9
+ #include "common.h"
10
+
11
+ #if !defined(MLD_CONFIG_MULTILEVEL_NO_SHARED)
12
+
13
+ #if defined(MLDSA_DEBUG)
14
+
15
+ #include <inttypes.h>
16
+ #include <stdio.h>
17
+ #include <stdlib.h>
18
+ #include "debug.h"
19
+
20
+ #define MLD_DEBUG_ERROR_HEADER "[ERROR:%s:%04d] "
21
+
22
+ MLD_INTERNAL_API
23
+ void mld_debug_check_assert(const char *file, int line, const int val)
24
+ {
25
+ if (val == 0)
26
+ {
27
+ fprintf(stderr, MLD_DEBUG_ERROR_HEADER "Assertion failed (value %d)\n",
28
+ file, line, val);
29
+ exit(1);
30
+ }
31
+ }
32
+
33
+ MLD_INTERNAL_API
34
+ void mld_debug_check_bounds(const char *file, int line, const int32_t *ptr,
35
+ unsigned len, int64_t lower_bound_exclusive,
36
+ int64_t upper_bound_exclusive)
37
+ {
38
+ int err = 0;
39
+ unsigned i;
40
+ for (i = 0; i < len; i++)
41
+ {
42
+ int32_t val = ptr[i];
43
+ if (!(val > lower_bound_exclusive && val < upper_bound_exclusive))
44
+ {
45
+ fprintf(stderr,
46
+ MLD_DEBUG_ERROR_HEADER
47
+ "Bounds assertion failed: Index %u, value %d out of bounds "
48
+ "(%" PRId64 ",%" PRId64 ")\n",
49
+ file, line, i, (int)val, lower_bound_exclusive,
50
+ upper_bound_exclusive);
51
+ err = 1;
52
+ }
53
+ }
54
+
55
+ if (err == 1)
56
+ {
57
+ exit(1);
58
+ }
59
+ }
60
+
61
+ #else /* MLDSA_DEBUG */
62
+
63
+ MLD_EMPTY_CU(debug)
64
+
65
+ #endif /* !MLDSA_DEBUG */
66
+
67
+ #else /* !MLD_CONFIG_MULTILEVEL_NO_SHARED */
68
+
69
+ MLD_EMPTY_CU(debug)
70
+
71
+ #endif /* MLD_CONFIG_MULTILEVEL_NO_SHARED */
72
+
73
+ /* To facilitate single-compilation-unit (SCU) builds, undefine all macros.
74
+ * Don't modify by hand -- this is auto-generated by scripts/autogen. */
75
+ #undef MLD_DEBUG_ERROR_HEADER
@@ -0,0 +1,125 @@
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
+ #ifndef MLD_DEBUG_H
7
+ #define MLD_DEBUG_H
8
+ #include "common.h"
9
+
10
+ #if defined(MLDSA_DEBUG)
11
+
12
+ /**
13
+ * Check debug assertion.
14
+ *
15
+ * Prints an error message to stderr and calls exit(1) if not.
16
+ *
17
+ * @param file Filename.
18
+ * @param line Line number.
19
+ * @param val Value asserted to be non-zero.
20
+ */
21
+ #define mld_debug_check_assert MLD_NAMESPACE(mldsa_debug_assert)
22
+ MLD_INTERNAL_API
23
+ void mld_debug_check_assert(const char *file, int line, const int val);
24
+
25
+ /**
26
+ * Check whether values in an array of int32_t are within specified bounds.
27
+ *
28
+ * Prints an error message to stderr and calls exit(1) if not.
29
+ *
30
+ * @param file Filename.
31
+ * @param line Line number.
32
+ * @param[in] ptr Base of array to be checked.
33
+ * @param len Number of int32_t in ptr.
34
+ * @param lower_bound_exclusive Exclusive lower bound.
35
+ * @param upper_bound_exclusive Exclusive upper bound.
36
+ */
37
+ #define mld_debug_check_bounds MLD_NAMESPACE(mldsa_debug_check_bounds)
38
+ MLD_INTERNAL_API
39
+ void mld_debug_check_bounds(const char *file, int line, const int32_t *ptr,
40
+ unsigned len, int64_t lower_bound_exclusive,
41
+ int64_t upper_bound_exclusive);
42
+
43
+ /* Check assertion, calling exit() upon failure
44
+ *
45
+ * val: Value that's asserted to be non-zero
46
+ */
47
+ #define mld_assert(val) mld_debug_check_assert(__FILE__, __LINE__, (val))
48
+
49
+ /* Check bounds in array of int32_t's
50
+ * ptr: Base of int32_t array; will be explicitly cast to int32_t*,
51
+ * so you may pass a byte-compatible type such as mld_poly or mld_polyvec.
52
+ * len: Number of int32_t in array
53
+ * value_lb: Inclusive lower value bound
54
+ * value_ub: Exclusive upper value bound */
55
+ #define mld_assert_bound(ptr, len, value_lb, value_ub) \
56
+ mld_debug_check_bounds(__FILE__, __LINE__, (const int32_t *)(ptr), (len), \
57
+ ((int64_t)(value_lb)) - 1, (value_ub))
58
+
59
+ /* Check absolute bounds in array of int32_t's
60
+ * ptr: Base of array, expression of type int32_t*
61
+ * len: Number of int32_t in array
62
+ * value_abs_bd: Exclusive absolute upper bound */
63
+ #define mld_assert_abs_bound(ptr, len, value_abs_bd) \
64
+ mld_assert_bound((ptr), (len), (-((int64_t)(value_abs_bd)) + 1), \
65
+ (value_abs_bd))
66
+
67
+ /* Version of bounds assertions for 2-dimensional arrays */
68
+ #define mld_assert_bound_2d(ptr, len0, len1, value_lb, value_ub) \
69
+ mld_assert_bound((ptr), ((len0) * (len1)), (value_lb), (value_ub))
70
+
71
+ #define mld_assert_abs_bound_2d(ptr, len0, len1, value_abs_bd) \
72
+ mld_assert_abs_bound((ptr), ((len0) * (len1)), (value_abs_bd))
73
+
74
+ /* When running CBMC, convert debug assertions into proof obligations */
75
+ #elif defined(CBMC)
76
+ #include "cbmc.h"
77
+
78
+ #define mld_assert(val) cassert(val)
79
+
80
+ #define mld_assert_bound(ptr, len, value_lb, value_ub) \
81
+ cassert(array_bound(((int32_t *)(ptr)), 0, (len), (value_lb), (value_ub)))
82
+
83
+ #define mld_assert_abs_bound(ptr, len, value_abs_bd) \
84
+ cassert(array_abs_bound(((int32_t *)(ptr)), 0, (len), (value_abs_bd)))
85
+
86
+ /* Because of https://github.com/diffblue/cbmc/issues/8570, we can't
87
+ * just use a single flattened array_bound(...) here. */
88
+ #define mld_assert_bound_2d(ptr, M, N, value_lb, value_ub) \
89
+ cassert(forall(kN, 0, (M), \
90
+ array_bound(&((int32_t (*)[(N)])(ptr))[kN][0], 0, (N), \
91
+ (value_lb), (value_ub))))
92
+
93
+ #define mld_assert_abs_bound_2d(ptr, M, N, value_abs_bd) \
94
+ cassert(forall(kN, 0, (M), \
95
+ array_abs_bound(&((int32_t (*)[(N)])(ptr))[kN][0], 0, (N), \
96
+ (value_abs_bd))))
97
+
98
+ #else /* !MLDSA_DEBUG && CBMC */
99
+
100
+ #define mld_assert(val) \
101
+ do \
102
+ { \
103
+ } while (0)
104
+ #define mld_assert_bound(ptr, len, value_lb, value_ub) \
105
+ do \
106
+ { \
107
+ } while (0)
108
+ #define mld_assert_abs_bound(ptr, len, value_abs_bd) \
109
+ do \
110
+ { \
111
+ } while (0)
112
+
113
+ #define mld_assert_bound_2d(ptr, len0, len1, value_lb, value_ub) \
114
+ do \
115
+ { \
116
+ } while (0)
117
+
118
+ #define mld_assert_abs_bound_2d(ptr, len0, len1, value_abs_bd) \
119
+ do \
120
+ { \
121
+ } while (0)
122
+
123
+
124
+ #endif /* !MLDSA_DEBUG && !CBMC */
125
+ #endif /* !MLD_DEBUG_H */