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,68 @@
1
+ /*
2
+ * Copyright (c) The mldsa-native project authors
3
+ * SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
4
+ */
5
+ #ifndef MLD_SYMMETRIC_H
6
+ #define MLD_SYMMETRIC_H
7
+
8
+ #include "cbmc.h"
9
+ #include "common.h"
10
+
11
+ #include MLD_FIPS202_HEADER_FILE
12
+ #if !defined(MLD_CONFIG_SERIAL_FIPS202_ONLY)
13
+ #include MLD_FIPS202X4_HEADER_FILE
14
+ #endif
15
+
16
+ #define MLD_STREAM128_BLOCKBYTES SHAKE128_RATE
17
+ #define MLD_STREAM256_BLOCKBYTES SHAKE256_RATE
18
+
19
+ #define mld_xof256_ctx mld_shake256ctx
20
+ #define mld_xof256_init(CTX) mld_shake256_init(CTX)
21
+
22
+ #define mld_xof256_absorb_once(CTX, IN, INBYTES) \
23
+ do \
24
+ { \
25
+ mld_shake256_absorb(CTX, IN, INBYTES); \
26
+ mld_shake256_finalize(CTX); \
27
+ } while (0)
28
+
29
+
30
+ #define mld_xof256_release(CTX) mld_shake256_release(CTX)
31
+ #define mld_xof256_squeezeblocks(OUT, OUTBLOCKS, STATE) \
32
+ mld_shake256_squeeze(OUT, (OUTBLOCKS) * SHAKE256_RATE, STATE)
33
+
34
+ #define mld_xof128_ctx mld_shake128ctx
35
+ #define mld_xof128_init(CTX) mld_shake128_init(CTX)
36
+
37
+ #define mld_xof128_absorb_once(CTX, IN, INBYTES) \
38
+ do \
39
+ { \
40
+ mld_shake128_absorb(CTX, IN, INBYTES); \
41
+ mld_shake128_finalize(CTX); \
42
+ } while (0)
43
+
44
+ #define mld_xof128_release(CTX) mld_shake128_release(CTX)
45
+ #define mld_xof128_squeezeblocks(OUT, OUTBLOCKS, STATE) \
46
+ mld_shake128_squeeze(OUT, (OUTBLOCKS) * SHAKE128_RATE, STATE)
47
+
48
+ #define mld_xof256_x4_ctx mld_shake256x4ctx
49
+ #define mld_xof256_x4_init(CTX) mld_shake256x4_init((CTX))
50
+ #define mld_xof256_x4_absorb(CTX, IN, INBYTES) \
51
+ mld_shake256x4_absorb_once((CTX), (IN)[0], (IN)[1], (IN)[2], (IN)[3], \
52
+ (INBYTES))
53
+ #define mld_xof256_x4_squeezeblocks(BUF, NBLOCKS, CTX) \
54
+ mld_shake256x4_squeezeblocks((BUF)[0], (BUF)[1], (BUF)[2], (BUF)[3], \
55
+ (NBLOCKS), (CTX))
56
+ #define mld_xof256_x4_release(CTX) mld_shake256x4_release((CTX))
57
+
58
+ #define mld_xof128_x4_ctx mld_shake128x4ctx
59
+ #define mld_xof128_x4_init(CTX) mld_shake128x4_init((CTX))
60
+ #define mld_xof128_x4_absorb(CTX, IN, INBYTES) \
61
+ mld_shake128x4_absorb_once((CTX), (IN)[0], (IN)[1], (IN)[2], (IN)[3], \
62
+ (INBYTES))
63
+ #define mld_xof128_x4_squeezeblocks(BUF, NBLOCKS, CTX) \
64
+ mld_shake128x4_squeezeblocks((BUF)[0], (BUF)[1], (BUF)[2], (BUF)[3], \
65
+ (NBLOCKS), (CTX))
66
+ #define mld_xof128_x4_release(CTX) mld_shake128x4_release((CTX))
67
+
68
+ #endif /* !MLD_SYMMETRIC_H */
@@ -0,0 +1,327 @@
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_SYS_H
8
+ #define MLD_SYS_H
9
+
10
+ #if !defined(MLD_CONFIG_NO_ASM) && (defined(__GNUC__) || defined(__clang__))
11
+ #define MLD_HAVE_INLINE_ASM
12
+ #endif
13
+
14
+ /* Try to find endianness, if not forced through CFLAGS already */
15
+ #if !defined(MLD_SYS_LITTLE_ENDIAN) && !defined(MLD_SYS_BIG_ENDIAN)
16
+ #if defined(__BYTE_ORDER__)
17
+ #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
18
+ #define MLD_SYS_LITTLE_ENDIAN
19
+ #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
20
+ #define MLD_SYS_BIG_ENDIAN
21
+ #else
22
+ #error "__BYTE_ORDER__ defined, but don't recognize value."
23
+ #endif
24
+ #endif /* __BYTE_ORDER__ */
25
+
26
+ /* MSVC does not define __BYTE_ORDER__. However, MSVC only supports
27
+ * little endian x86, x86_64, and AArch64. It is, hence, safe to assume
28
+ * little endian. */
29
+ #if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_AMD64) || \
30
+ defined(_M_IX86) || defined(_M_ARM64))
31
+ #define MLD_SYS_LITTLE_ENDIAN
32
+ #endif
33
+
34
+ #endif /* !MLD_SYS_LITTLE_ENDIAN && !MLD_SYS_BIG_ENDIAN */
35
+
36
+ /* Check if we're running on an AArch64 little endian system. _M_ARM64 is set by
37
+ * MSVC. */
38
+ #if defined(__AARCH64EL__) || defined(_M_ARM64)
39
+ #define MLD_SYS_AARCH64
40
+ #endif
41
+
42
+ /* Check if the AArch64 compilation target supports NEON (Advanced SIMD).
43
+ *
44
+ * Some compilers also define __ARM_NEON__, but __ARM_NEON is the most reliable
45
+ * signal. Specifically, clang on Apple appears to keep __ARM_NEON__ set even if
46
+ * -march=armv8-a+nosimd is set.
47
+ *
48
+ * gcc 4.8 -- the first gcc version introducing Neon support -- sets neither
49
+ * __ARM_NEON nor __ARM_NEON__; in fact, there is no preprocessor signal that
50
+ * Neon is enabled. If you use gcc 4.8 and need Neon, you should set
51
+ * MLD_SYS_AARCH64_NEON manually. gcc 4.9 onwards do set __ARM_NEON.
52
+ */
53
+ #if defined(MLD_SYS_AARCH64) && defined(__ARM_NEON)
54
+ #define MLD_SYS_AARCH64_NEON
55
+ #endif
56
+
57
+ /* Check if we're running on an AArch64 big endian system. */
58
+ #if defined(__AARCH64EB__)
59
+ #define MLD_SYS_AARCH64_EB
60
+ #endif
61
+
62
+ /* Check if we're running on an Armv8.1-M system with MVE */
63
+ #if defined(__ARM_ARCH_8_1M_MAIN__) || defined(__ARM_FEATURE_MVE)
64
+ #define MLD_SYS_ARMV81M_MVE
65
+ #endif
66
+
67
+ /* Check if we're running on an x86_64 system. */
68
+ #if defined(__x86_64__) || defined(_M_X64) || defined(_M_AMD64)
69
+ #define MLD_SYS_X86_64
70
+ #if defined(__AVX2__)
71
+ #define MLD_SYS_X86_64_AVX2
72
+ #endif
73
+ #endif /* __x86_64__ || _M_X64 || _M_AMD64 */
74
+
75
+ #if defined(MLD_SYS_LITTLE_ENDIAN) && defined(__powerpc64__)
76
+ #define MLD_SYS_PPC64LE
77
+ #endif
78
+
79
+ #if defined(__riscv) && defined(__riscv_xlen) && __riscv_xlen == 64
80
+ #define MLD_SYS_RISCV64
81
+ #endif
82
+
83
+ #if defined(MLD_SYS_RISCV64) && defined(__riscv_vector) && \
84
+ defined(__riscv_v_intrinsic)
85
+ #define MLD_SYS_RISCV64_RVV
86
+ #endif
87
+
88
+ #if defined(__riscv) && defined(__riscv_xlen) && __riscv_xlen == 32
89
+ #define MLD_SYS_RISCV32
90
+ #endif
91
+
92
+ #if defined(_WIN64) || defined(_WIN32)
93
+ #define MLD_SYS_WINDOWS
94
+ #endif
95
+
96
+ #if defined(__linux__)
97
+ #define MLD_SYS_LINUX
98
+ #endif
99
+
100
+ #if defined(__APPLE__)
101
+ #define MLD_SYS_APPLE
102
+ #endif
103
+
104
+ /* If MLD_FORCE_AARCH64 is set, assert that we're indeed on an AArch64 system.
105
+ */
106
+ #if defined(MLD_FORCE_AARCH64) && !defined(MLD_SYS_AARCH64)
107
+ #error "MLD_FORCE_AARCH64 is set, but we don't seem to be on an AArch64 system."
108
+ #endif
109
+
110
+ /* If MLD_FORCE_AARCH64_EB is set, assert that we're indeed on a big endian
111
+ * AArch64 system. */
112
+ #if defined(MLD_FORCE_AARCH64_EB) && !defined(MLD_SYS_AARCH64_EB)
113
+ #error \
114
+ "MLD_FORCE_AARCH64_EB is set, but we don't seem to be on an AArch64 system."
115
+ #endif
116
+
117
+ /* If MLD_FORCE_X86_64 is set, assert that we're indeed on an X86_64 system. */
118
+ #if defined(MLD_FORCE_X86_64) && !defined(MLD_SYS_X86_64)
119
+ #error "MLD_FORCE_X86_64 is set, but we don't seem to be on an X86_64 system."
120
+ #endif
121
+
122
+ #if defined(MLD_FORCE_PPC64LE) && !defined(MLD_SYS_PPC64LE)
123
+ #error "MLD_FORCE_PPC64LE is set, but we don't seem to be on a PPC64LE system."
124
+ #endif
125
+
126
+ #if defined(MLD_FORCE_RISCV64) && !defined(MLD_SYS_RISCV64)
127
+ #error "MLD_FORCE_RISCV64 is set, but we don't seem to be on a RISCV64 system."
128
+ #endif
129
+
130
+ #if defined(MLD_FORCE_RISCV32) && !defined(MLD_SYS_RISCV32)
131
+ #error "MLD_FORCE_RISCV32 is set, but we don't seem to be on a RISCV32 system."
132
+ #endif
133
+
134
+ /*
135
+ * MLD_INLINE: Hint for inlining.
136
+ * - MSVC: __inline
137
+ * - C99+: inline
138
+ * - GCC/Clang C90: __attribute__((unused)) to silence warnings
139
+ * - Other C90: empty
140
+ */
141
+ #if !defined(MLD_INLINE)
142
+ #if defined(_MSC_VER)
143
+ #define MLD_INLINE __inline
144
+ #elif defined(inline) || \
145
+ (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
146
+ #define MLD_INLINE inline
147
+ #elif defined(__GNUC__) || defined(__clang__)
148
+ #define MLD_INLINE __attribute__((unused))
149
+ #else
150
+ #define MLD_INLINE
151
+ #endif
152
+ #endif /* !MLD_INLINE */
153
+
154
+ /*
155
+ * MLD_ALWAYS_INLINE: Force inlining.
156
+ * - MSVC: __forceinline
157
+ * - GCC/Clang C99+: MLD_INLINE __attribute__((always_inline))
158
+ * - Other: MLD_INLINE (no forced inlining)
159
+ */
160
+ #if !defined(MLD_ALWAYS_INLINE)
161
+ #if defined(_MSC_VER)
162
+ #define MLD_ALWAYS_INLINE __forceinline
163
+ #elif (defined(__GNUC__) || defined(__clang__)) && \
164
+ (defined(inline) || \
165
+ (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L))
166
+ #define MLD_ALWAYS_INLINE MLD_INLINE __attribute__((always_inline))
167
+ #else
168
+ #define MLD_ALWAYS_INLINE MLD_INLINE
169
+ #endif
170
+ #endif /* !MLD_ALWAYS_INLINE */
171
+
172
+ /*
173
+ * MLD_NOINLINE: Prevent inlining.
174
+ * - MSVC: __declspec(noinline)
175
+ * - GCC/Clang: __attribute__((noinline))
176
+ * - Other: empty
177
+ */
178
+ #if !defined(MLD_NOINLINE)
179
+ #if defined(_MSC_VER)
180
+ #define MLD_NOINLINE __declspec(noinline)
181
+ #elif defined(__GNUC__) || defined(__clang__)
182
+ #define MLD_NOINLINE __attribute__((noinline))
183
+ #else
184
+ #define MLD_NOINLINE
185
+ #endif
186
+ #endif /* !MLD_NOINLINE */
187
+
188
+ #ifndef MLD_STATIC_TESTABLE
189
+ #define MLD_STATIC_TESTABLE static
190
+ #endif
191
+
192
+ /*
193
+ * C90 does not have the restrict compiler directive yet.
194
+ * We don't use it in C90 builds.
195
+ */
196
+ #if !defined(restrict)
197
+ #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
198
+ #define MLD_RESTRICT restrict
199
+ #else
200
+ #define MLD_RESTRICT
201
+ #endif
202
+
203
+ #else /* !restrict */
204
+
205
+ #define MLD_RESTRICT restrict
206
+ #endif /* restrict */
207
+
208
+ #define MLD_DEFAULT_ALIGN 32
209
+ #define MLD_ALIGN_UP(N) \
210
+ ((((N) + (MLD_DEFAULT_ALIGN - 1)) / MLD_DEFAULT_ALIGN) * MLD_DEFAULT_ALIGN)
211
+ #if defined(__GNUC__)
212
+ #define MLD_ALIGN __attribute__((aligned(MLD_DEFAULT_ALIGN)))
213
+ #elif defined(_MSC_VER)
214
+ #define MLD_ALIGN __declspec(align(MLD_DEFAULT_ALIGN))
215
+ #else
216
+ #define MLD_ALIGN /* No known support for alignment constraints */
217
+ #endif
218
+
219
+
220
+ /* New X86_64 CPUs support control-flow protection using the CET instructions.
221
+ * When enabled (through -fcf-protection=), all compilation units (including
222
+ * empty ones) need to support CET for this to work.
223
+ * For assembly, this means that source files need to signal support for
224
+ * CET by setting the appropriate note.gnu.property section.
225
+ * This can be achieved by including the <cet.h> header in all assembly file.
226
+ * This file also provides the _CET_ENDBR macro which needs to be placed at
227
+ * every potential target of an indirect branch.
228
+ * If CET is enabled _CET_ENDBR maps to the endbr64 instruction, otherwise
229
+ * it is empty.
230
+ * In case the compiler does not support CET (e.g., <gcc8, <clang11),
231
+ * the __CET__ macro is not set and we default to nothing.
232
+ * Note that we only issue _CET_ENDBR instructions through the MLD_ASM_FN_SYMBOL
233
+ * macro as the global symbols are the only possible targets of indirect
234
+ * branches in our code.
235
+ */
236
+ #if defined(MLD_SYS_X86_64)
237
+ #if defined(__CET__)
238
+ #include <cet.h>
239
+ #define MLD_CET_ENDBR _CET_ENDBR
240
+ #else
241
+ #define MLD_CET_ENDBR
242
+ #endif
243
+ #endif /* MLD_SYS_X86_64 */
244
+
245
+ #if defined(MLD_CONFIG_CT_TESTING_ENABLED) && !defined(__ASSEMBLER__)
246
+ #include <valgrind/memcheck.h>
247
+ #define MLD_CT_TESTING_SECRET(ptr, len) \
248
+ VALGRIND_MAKE_MEM_UNDEFINED((ptr), (len))
249
+ #define MLD_CT_TESTING_DECLASSIFY(ptr, len) \
250
+ VALGRIND_MAKE_MEM_DEFINED((ptr), (len))
251
+ #else /* MLD_CONFIG_CT_TESTING_ENABLED && !__ASSEMBLER__ */
252
+ #define MLD_CT_TESTING_SECRET(ptr, len) \
253
+ do \
254
+ { \
255
+ } while (0)
256
+ #define MLD_CT_TESTING_DECLASSIFY(ptr, len) \
257
+ do \
258
+ { \
259
+ } while (0)
260
+ #endif /* !(MLD_CONFIG_CT_TESTING_ENABLED && !__ASSEMBLER__) */
261
+
262
+ #if defined(__GNUC__) || defined(__clang__)
263
+ #define MLD_MUST_CHECK_RETURN_VALUE __attribute__((warn_unused_result))
264
+ #else
265
+ #define MLD_MUST_CHECK_RETURN_VALUE
266
+ #endif
267
+
268
+ /* The x86_64 assembly backend uses the SysV calling convention. On Windows,
269
+ * where the Microsoft x64 calling convention is the default, it can still be
270
+ * used with compilers that allow choosing the calling convention per
271
+ * function: GCC and Clang support __attribute__((sysv_abi)), which makes
272
+ * calls to the annotated function follow the SysV calling convention.
273
+ *
274
+ * MLD_SYSV_ABI_SUPPORTED signals that the toolchain can call SysV assembly
275
+ * routines; the x86_64 assembly backend is only enabled if it is defined.
276
+ * MLD_SYSV_ABI is the attribute carried by declarations of x86_64 assembly
277
+ * routines. Both macros can be set externally for toolchains offering an
278
+ * equivalent mechanism that is not recognized here. */
279
+ #if defined(MLD_SYS_X86_64) && !defined(MLD_SYSV_ABI_SUPPORTED)
280
+ #if !defined(MLD_SYS_WINDOWS) || defined(__GNUC__) || defined(__clang__)
281
+ #define MLD_SYSV_ABI_SUPPORTED
282
+ #endif
283
+ #endif
284
+
285
+ #if !defined(MLD_SYSV_ABI)
286
+ #if defined(MLD_SYS_WINDOWS) && defined(MLD_SYSV_ABI_SUPPORTED)
287
+ #define MLD_SYSV_ABI __attribute__((sysv_abi))
288
+ #else
289
+ #define MLD_SYSV_ABI
290
+ #endif
291
+ #endif /* !MLD_SYSV_ABI */
292
+
293
+ #if !defined(__ASSEMBLER__)
294
+ /* System capability enumeration */
295
+ typedef enum
296
+ {
297
+ /* x86_64 */
298
+ MLD_SYS_CAP_X86_64_AVX2,
299
+ /* AArch64 */
300
+ MLD_SYS_CAP_AARCH64_NEON,
301
+ MLD_SYS_CAP_AARCH64_SHA3,
302
+ /* Armv8.1-M */
303
+ MLD_SYS_CAP_ARMV81M_MVE
304
+ } mld_sys_cap;
305
+
306
+ #if !defined(MLD_CONFIG_CUSTOM_CAPABILITY_FUNC)
307
+ #include "cbmc.h"
308
+
309
+ MLD_MUST_CHECK_RETURN_VALUE
310
+ static MLD_INLINE int mld_sys_check_capability(mld_sys_cap cap)
311
+ __contract__(
312
+ ensures(return_value == 0 || return_value == 1)
313
+ )
314
+ {
315
+ /* By default, we rely on compile-time feature detection/specification:
316
+ * If a feature is enabled at compile-time, we assume it is supported by
317
+ * the host that the resulting library/binary will be built on.
318
+ * If this assumption is not true, you MUST overwrite this function.
319
+ * See the documentation of MLD_CONFIG_CUSTOM_CAPABILITY_FUNC in
320
+ * mldsa_native_config.h for more information. */
321
+ (void)cap;
322
+ return 1;
323
+ }
324
+ #endif /* !MLD_CONFIG_CUSTOM_CAPABILITY_FUNC */
325
+ #endif /* !__ASSEMBLER__ */
326
+
327
+ #endif /* !MLD_SYS_H */
@@ -0,0 +1,55 @@
1
+ /*
2
+ * Copyright (c) The mldsa-native project authors
3
+ * SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
4
+ */
5
+
6
+ /*
7
+ * WARNING: This file is auto-generated from scripts/autogen
8
+ * in the mldsa-native repository.
9
+ * Do not modify it directly.
10
+ */
11
+
12
+
13
+ /*
14
+ * Table of zeta values used in the reference NTT and inverse NTT.
15
+ * See autogen for details.
16
+ */
17
+ static const int32_t mld_zetas[MLDSA_N] = {
18
+ 0, 25847, -2608894, -518909, 237124, -777960, -876248,
19
+ 466468, 1826347, 2353451, -359251, -2091905, 3119733, -2884855,
20
+ 3111497, 2680103, 2725464, 1024112, -1079900, 3585928, -549488,
21
+ -1119584, 2619752, -2108549, -2118186, -3859737, -1399561, -3277672,
22
+ 1757237, -19422, 4010497, 280005, 2706023, 95776, 3077325,
23
+ 3530437, -1661693, -3592148, -2537516, 3915439, -3861115, -3043716,
24
+ 3574422, -2867647, 3539968, -300467, 2348700, -539299, -1699267,
25
+ -1643818, 3505694, -3821735, 3507263, -2140649, -1600420, 3699596,
26
+ 811944, 531354, 954230, 3881043, 3900724, -2556880, 2071892,
27
+ -2797779, -3930395, -1528703, -3677745, -3041255, -1452451, 3475950,
28
+ 2176455, -1585221, -1257611, 1939314, -4083598, -1000202, -3190144,
29
+ -3157330, -3632928, 126922, 3412210, -983419, 2147896, 2715295,
30
+ -2967645, -3693493, -411027, -2477047, -671102, -1228525, -22981,
31
+ -1308169, -381987, 1349076, 1852771, -1430430, -3343383, 264944,
32
+ 508951, 3097992, 44288, -1100098, 904516, 3958618, -3724342,
33
+ -8578, 1653064, -3249728, 2389356, -210977, 759969, -1316856,
34
+ 189548, -3553272, 3159746, -1851402, -2409325, -177440, 1315589,
35
+ 1341330, 1285669, -1584928, -812732, -1439742, -3019102, -3881060,
36
+ -3628969, 3839961, 2091667, 3407706, 2316500, 3817976, -3342478,
37
+ 2244091, -2446433, -3562462, 266997, 2434439, -1235728, 3513181,
38
+ -3520352, -3759364, -1197226, -3193378, 900702, 1859098, 909542,
39
+ 819034, 495491, -1613174, -43260, -522500, -655327, -3122442,
40
+ 2031748, 3207046, -3556995, -525098, -768622, -3595838, 342297,
41
+ 286988, -2437823, 4108315, 3437287, -3342277, 1735879, 203044,
42
+ 2842341, 2691481, -2590150, 1265009, 4055324, 1247620, 2486353,
43
+ 1595974, -3767016, 1250494, 2635921, -3548272, -2994039, 1869119,
44
+ 1903435, -1050970, -1333058, 1237275, -3318210, -1430225, -451100,
45
+ 1312455, 3306115, -1962642, -1279661, 1917081, -2546312, -1374803,
46
+ 1500165, 777191, 2235880, 3406031, -542412, -2831860, -1671176,
47
+ -1846953, -2584293, -3724270, 594136, -3776993, -2013608, 2432395,
48
+ 2454455, -164721, 1957272, 3369112, 185531, -1207385, -3183426,
49
+ 162844, 1616392, 3014001, 810149, 1652634, -3694233, -1799107,
50
+ -3038916, 3523897, 3866901, 269760, 2213111, -975884, 1717735,
51
+ 472078, -426683, 1723600, -1803090, 1910376, -1667432, -1104333,
52
+ -260646, -3833893, -2939036, -2235985, -420899, -2286327, 183443,
53
+ -976891, 1612842, -3545687, -554416, 3919660, -48306, -1362209,
54
+ 3937738, 1400424, -846154, 1976782,
55
+ };
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MLDSA
4
+ # ML-DSA parameter set metadata.
5
+ class ParameterSet
6
+ # @return [Integer] mldsa-native numeric identifier
7
+ attr_reader :identifier
8
+
9
+ # @return [Integer] private seed size in bytes
10
+ attr_reader :seed_size
11
+
12
+ # @return [Integer] expanded private key size in bytes
13
+ attr_reader :private_key_size
14
+
15
+ # @return [Integer] public key size in bytes
16
+ attr_reader :public_key_size
17
+
18
+ # @return [Integer] signature size in bytes
19
+ attr_reader :signature_size
20
+
21
+ # @return [Integer] external message representative size in bytes
22
+ attr_reader :mu_size
23
+
24
+ # @return [Integer] hedging randomness size in bytes
25
+ attr_reader :random_size
26
+
27
+ # @api private
28
+ def initialize(identifier:, private_key_size:, public_key_size:, signature_size:)
29
+ @identifier = identifier
30
+ @seed_size = MLDSA::SEED_SIZE
31
+ @private_key_size = private_key_size
32
+ @public_key_size = public_key_size
33
+ @signature_size = signature_size
34
+ @mu_size = MLDSA::MU_SIZE
35
+ @random_size = MLDSA::RANDOM_SIZE
36
+ end
37
+
38
+ # Compare parameter sets by their metadata.
39
+ #
40
+ # @param other [Object] object to compare
41
+ # @return [Boolean]
42
+ def ==(other)
43
+ other.is_a?(self.class) &&
44
+ identifier == other.identifier &&
45
+ seed_size == other.seed_size &&
46
+ private_key_size == other.private_key_size &&
47
+ public_key_size == other.public_key_size &&
48
+ signature_size == other.signature_size &&
49
+ mu_size == other.mu_size &&
50
+ random_size == other.random_size
51
+ end
52
+
53
+ # @see #==
54
+ def eql?(other)
55
+ self == other
56
+ end
57
+
58
+ # @return [Integer]
59
+ def hash
60
+ [
61
+ self.class,
62
+ identifier,
63
+ seed_size,
64
+ private_key_size,
65
+ public_key_size,
66
+ signature_size,
67
+ mu_size,
68
+ random_size
69
+ ].hash
70
+ end
71
+
72
+ # @return [String]
73
+ def to_s
74
+ "ML-DSA-#{identifier}"
75
+ end
76
+
77
+ # @return [String]
78
+ def inspect
79
+ "#<#{self.class}:#{object_id} #{self}>"
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,147 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MLDSA
4
+ # Private key for producing ML-DSA signatures.
5
+ class SigningKey
6
+ # @return [MLDSA::VerifyKey] public key corresponding to this signing key
7
+ attr_reader :verify_key
8
+
9
+ # @return [MLDSA::ParameterSet] ML-DSA parameter set for this key
10
+ attr_reader :parameter_set
11
+
12
+ # Generate a random ML-DSA signing key for the given parameter set.
13
+ #
14
+ # @param parameter_set [MLDSA::ParameterSet] one of {MLDSA::ML_DSA_44}, {MLDSA::ML_DSA_65}, or {MLDSA::ML_DSA_87}
15
+ # @return [MLDSA::SigningKey]
16
+ # @raise [ArgumentError] if the parameter set is not supported
17
+ def self.generate(parameter_set)
18
+ parameter_set = MLDSA.validate_parameter_set(parameter_set)
19
+ new SecureRandom.random_bytes(parameter_set.seed_size), parameter_set
20
+ end
21
+
22
+ # Import a signing key from an expanded private key.
23
+ #
24
+ # The private seed is not recoverable from keys imported this way.
25
+ #
26
+ # @param private_key [String] serialized expanded private key bytes
27
+ # @param parameter_set [MLDSA::ParameterSet] one of {MLDSA::ML_DSA_44}, {MLDSA::ML_DSA_65}, or {MLDSA::ML_DSA_87}
28
+ # @return [MLDSA::SigningKey]
29
+ # @raise [ArgumentError] if the parameter set is unsupported, the key has the wrong size, or the key is invalid
30
+ # @raise [TypeError] if private_key is not a String
31
+ def self.from_private_key(private_key, parameter_set)
32
+ parameter_set = MLDSA.validate_parameter_set(parameter_set)
33
+ MLDSA.validate_bytes(private_key, parameter_set.private_key_size, "private key")
34
+ private_key = MLDSA.copy_bytes(private_key)
35
+
36
+ public_key = Native.public_key_from_private_key(parameter_set.identifier, private_key)
37
+ new_from_private_key(private_key, public_key, parameter_set)
38
+ end
39
+
40
+ # Import a signing key from a private seed.
41
+ #
42
+ # @param seed [String] {MLDSA::SEED_SIZE}-byte private seed
43
+ # @param parameter_set [MLDSA::ParameterSet] one of {MLDSA::ML_DSA_44}, {MLDSA::ML_DSA_65}, or {MLDSA::ML_DSA_87}
44
+ # @param public_key [String, nil] optional public key expected to match the seed
45
+ # @raise [ArgumentError] if the parameter set is unsupported, seed has the wrong size, public_key has the wrong size, or public_key does not match the seed
46
+ # @raise [TypeError] if seed or public_key is not a String
47
+ def initialize(seed, parameter_set, public_key: nil)
48
+ parameter_set = MLDSA.validate_parameter_set(parameter_set)
49
+ MLDSA.validate_bytes(seed, parameter_set.seed_size, "private seed")
50
+ MLDSA.validate_bytes(public_key, parameter_set.public_key_size, "public key") if public_key
51
+
52
+ @parameter_set = parameter_set
53
+ @seed = MLDSA.copy_bytes(seed)
54
+ private_key, derived_public_key = Native.keypair_from_seed(parameter_set.identifier, @seed)
55
+
56
+ raise ArgumentError, "public key does not match private seed" if public_key && public_key != derived_public_key
57
+
58
+ @private_key = MLDSA.copy_bytes(private_key)
59
+ @verify_key = VerifyKey.new(derived_public_key, parameter_set)
60
+ end
61
+
62
+ # Export the private seed.
63
+ #
64
+ # @return [String] mutable copy of the private seed
65
+ # @raise [MLDSA::SeedUnavailableError] if this key was imported from an expanded private key
66
+ def seed
67
+ return @seed.dup if @seed
68
+
69
+ raise SeedUnavailableError, "private seed is unavailable for keys imported from a private key"
70
+ end
71
+
72
+ # @see #seed
73
+ alias private_seed seed
74
+
75
+ # Export the expanded private key.
76
+ #
77
+ # @return [String] mutable copy of the expanded private key
78
+ def private_key
79
+ @private_key.dup
80
+ end
81
+
82
+ # Sign a message using hedged ML-DSA.
83
+ #
84
+ # @param message [String] message to sign
85
+ # @param context [String] optional application context, at most 255 bytes
86
+ # @return [String] signature bytes
87
+ # @raise [ArgumentError] if context is too large
88
+ # @raise [TypeError] if context is not a String
89
+ def sign(message, context: "")
90
+ MLDSA.validate_context(context)
91
+ Native.sign(parameter_set.identifier, @private_key, message, context, SecureRandom.random_bytes(parameter_set.random_size))
92
+ end
93
+
94
+ # Sign a precomputed external message representative.
95
+ #
96
+ # This method does not compute `mu`; callers must provide the exact
97
+ # {MLDSA::MU_SIZE}-byte value.
98
+ #
99
+ # @param mu [String] precomputed external message representative
100
+ # @return [String] signature bytes
101
+ # @raise [ArgumentError] if mu has the wrong size
102
+ # @raise [TypeError] if mu is not a String
103
+ def sign_mu(mu)
104
+ MLDSA.validate_bytes(mu, parameter_set.mu_size, "mu")
105
+ Native.sign_mu(parameter_set.identifier, @private_key, mu, SecureRandom.random_bytes(parameter_set.random_size))
106
+ end
107
+
108
+ # Export the expanded private key.
109
+ #
110
+ # @return [String] mutable copy of the expanded private key
111
+ def private_key_bytes
112
+ private_key
113
+ end
114
+
115
+ # @see #private_key_bytes
116
+ alias to_private_key private_key_bytes
117
+
118
+ # Export the private seed.
119
+ #
120
+ # @return [String] mutable copy of the private seed
121
+ # @raise [MLDSA::SeedUnavailableError] if this key was imported from an expanded private key
122
+ def to_bytes
123
+ seed
124
+ end
125
+
126
+ # Redacted string representation.
127
+ #
128
+ # @return [String]
129
+ def to_s
130
+ "#<#{self.class}:#{object_id} parameter_set=#{parameter_set}>"
131
+ end
132
+
133
+ # @see #to_s
134
+ alias inspect to_s
135
+
136
+ # @api private
137
+ def self.new_from_private_key(private_key, public_key, parameter_set)
138
+ allocate.tap do |key|
139
+ key.instance_variable_set(:@parameter_set, parameter_set)
140
+ key.instance_variable_set(:@seed, nil)
141
+ key.instance_variable_set(:@private_key, MLDSA.copy_bytes(private_key))
142
+ key.instance_variable_set(:@verify_key, VerifyKey.new(public_key, parameter_set))
143
+ end
144
+ end
145
+ private_class_method :new_from_private_key
146
+ end
147
+ end