pq_crypto 0.6.0 → 0.6.2

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 (154) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +12 -0
  3. data/SECURITY.md +7 -0
  4. data/ext/pqcrypto/extconf.rb +2 -0
  5. data/ext/pqcrypto/pqcrypto_ruby_secure.c +139 -0
  6. data/ext/pqcrypto/pqcrypto_secure.c +532 -0
  7. data/ext/pqcrypto/pqcrypto_secure.h +20 -0
  8. data/ext/pqcrypto/pqcrypto_version.h +1 -1
  9. data/ext/pqcrypto/vendor/.vendored +4 -4
  10. data/ext/pqcrypto/vendor/mldsa-native/README.md +23 -10
  11. data/ext/pqcrypto/vendor/mldsa-native/mldsa/README.md +23 -0
  12. data/ext/pqcrypto/vendor/mldsa-native/mldsa/mldsa_native.c +114 -58
  13. data/ext/pqcrypto/vendor/mldsa-native/mldsa/mldsa_native.h +498 -461
  14. data/ext/pqcrypto/vendor/mldsa-native/mldsa/mldsa_native_asm.S +145 -85
  15. data/ext/pqcrypto/vendor/mldsa-native/mldsa/mldsa_native_config.h +456 -422
  16. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/cbmc.h +47 -25
  17. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/common.h +26 -14
  18. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/ct.h +56 -81
  19. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/debug.h +17 -24
  20. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/fips202.c +33 -40
  21. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/fips202.h +67 -87
  22. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/fips202x4.c +19 -14
  23. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/fips202x4.h +13 -5
  24. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/keccakf1600.c +84 -10
  25. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/keccakf1600.h +10 -5
  26. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/auto.h +6 -0
  27. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/fips202_native_aarch64.h +22 -15
  28. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x1_scalar_aarch64_asm.S +376 -0
  29. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x1_v84a_aarch64_asm.S +204 -0
  30. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x2_v84a_aarch64_asm.S +259 -0
  31. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x4_v8a_scalar_hybrid_aarch64_asm.S +1077 -0
  32. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x4_v8a_v84a_scalar_hybrid_aarch64_asm.S +987 -0
  33. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccakf1600_round_constants.c +16 -10
  34. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/x1_scalar.h +2 -1
  35. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/x1_v84a.h +1 -1
  36. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/x2_v84a.h +4 -2
  37. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/x4_v8a_scalar.h +2 -2
  38. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/x4_v8a_v84a_scalar.h +1 -1
  39. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/api.h +60 -0
  40. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/mve.h +48 -0
  41. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/src/fips202_native_armv81m.h +18 -1
  42. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/src/keccak_f1600_x4_mve.S +658 -582
  43. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/src/keccak_f1600_x4_mve.c +5 -100
  44. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/src/keccakf1600_round_constants.c +26 -25
  45. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/src/state_extract_bytes_x4_mve.S +334 -0
  46. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/src/state_xor_bytes_x4_mve.S +355 -0
  47. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/auto.h +8 -3
  48. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/x86_64/{xkcp.h → keccak_f1600_x4_avx2.h} +11 -8
  49. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/x86_64/src/fips202_native_x86_64.h +44 -0
  50. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/x86_64/src/keccak_f1600_x4_avx2_asm.S +454 -0
  51. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/x86_64/src/keccakf1600_constants.c +52 -0
  52. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/meta.h +37 -28
  53. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/aarch64_zetas.c +213 -196
  54. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/arith_native_aarch64.h +248 -64
  55. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/intt_aarch64_asm.S +753 -0
  56. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/mld_polyvecl_pointwise_acc_montgomery_l4_aarch64_asm.S +129 -0
  57. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/mld_polyvecl_pointwise_acc_montgomery_l5_aarch64_asm.S +145 -0
  58. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/mld_polyvecl_pointwise_acc_montgomery_l7_aarch64_asm.S +177 -0
  59. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/ntt_aarch64_asm.S +653 -0
  60. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/pointwise_montgomery_aarch64_asm.S +84 -0
  61. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/poly_caddq_aarch64_asm.S +53 -0
  62. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/poly_chknorm_aarch64_asm.S +55 -0
  63. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/poly_decompose_32_aarch64_asm.S +86 -0
  64. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/poly_decompose_88_aarch64_asm.S +86 -0
  65. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/poly_use_hint_32_aarch64_asm.S +103 -0
  66. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/poly_use_hint_88_aarch64_asm.S +111 -0
  67. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/polyz_unpack_17_aarch64_asm.S +75 -0
  68. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/polyz_unpack_19_aarch64_asm.S +72 -0
  69. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/polyz_unpack_table.c +23 -11
  70. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/rej_uniform_aarch64_asm.S +189 -0
  71. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/rej_uniform_eta2_aarch64_asm.S +137 -0
  72. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/rej_uniform_eta4_aarch64_asm.S +130 -0
  73. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/rej_uniform_eta_table.c +520 -516
  74. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/rej_uniform_table.c +34 -33
  75. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/api.h +202 -242
  76. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/meta.h +25 -17
  77. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/arith_native_x86_64.h +112 -28
  78. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/consts.c +1 -1
  79. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/consts.h +1 -1
  80. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/intt_avx2_asm.S +2311 -0
  81. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/ntt_avx2_asm.S +2383 -0
  82. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/nttunpack_avx2_asm.S +238 -0
  83. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/pointwise_acc_l4_avx2_asm.S +139 -0
  84. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/pointwise_acc_l5_avx2_asm.S +155 -0
  85. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/pointwise_acc_l7_avx2_asm.S +187 -0
  86. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/pointwise_avx2_asm.S +130 -0
  87. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/poly_caddq_avx2_asm.S +190 -0
  88. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/poly_decompose_32_avx2.c +6 -4
  89. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/poly_decompose_88_avx2.c +6 -4
  90. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/poly_use_hint_32_avx2.c +9 -8
  91. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/poly_use_hint_88_avx2.c +10 -9
  92. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/polyz_unpack_17_avx2.c +8 -5
  93. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/polyz_unpack_19_avx2.c +8 -5
  94. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/rej_uniform_eta2_avx2.c +6 -4
  95. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/rej_uniform_eta4_avx2.c +6 -4
  96. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/rej_uniform_table.c +130 -129
  97. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/packing.c +109 -180
  98. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/packing.h +169 -150
  99. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/poly.c +56 -40
  100. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/poly.h +149 -164
  101. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/poly_kl.c +52 -57
  102. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/poly_kl.h +132 -167
  103. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/polyvec.c +57 -424
  104. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/polyvec.h +167 -474
  105. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/polyvec_lazy.c +308 -0
  106. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/polyvec_lazy.h +653 -0
  107. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/reduce.h +22 -29
  108. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/rounding.h +37 -43
  109. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/sign.c +511 -367
  110. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/sign.h +456 -417
  111. data/lib/pq_crypto/hybrid_kem.rb +1 -1
  112. data/lib/pq_crypto/internal.rb +23 -0
  113. data/lib/pq_crypto/kem.rb +27 -34
  114. data/lib/pq_crypto/pkcs8/der.rb +68 -0
  115. data/lib/pq_crypto/pkcs8/private_key_choice.rb +186 -0
  116. data/lib/pq_crypto/pkcs8.rb +51 -468
  117. data/lib/pq_crypto/serialization.rb +19 -29
  118. data/lib/pq_crypto/signature.rb +28 -35
  119. data/lib/pq_crypto/version.rb +1 -1
  120. data/lib/pq_crypto.rb +10 -0
  121. data/script/vendor_libs.rb +3 -3
  122. metadata +44 -35
  123. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x1_scalar_asm.S +0 -376
  124. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x1_v84a_asm.S +0 -204
  125. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x2_v84a_asm.S +0 -259
  126. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x4_v8a_scalar_hybrid_asm.S +0 -1077
  127. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x4_v8a_v84a_scalar_hybrid_asm.S +0 -987
  128. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/x86_64/src/KeccakP_1600_times4_SIMD256.c +0 -488
  129. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/x86_64/src/KeccakP_1600_times4_SIMD256.h +0 -16
  130. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/intt.S +0 -753
  131. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/mld_polyvecl_pointwise_acc_montgomery_l4.S +0 -129
  132. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/mld_polyvecl_pointwise_acc_montgomery_l5.S +0 -145
  133. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/mld_polyvecl_pointwise_acc_montgomery_l7.S +0 -177
  134. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/ntt.S +0 -653
  135. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/pointwise_montgomery.S +0 -79
  136. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/poly_caddq_asm.S +0 -53
  137. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/poly_chknorm_asm.S +0 -55
  138. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/poly_decompose_32_asm.S +0 -85
  139. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/poly_decompose_88_asm.S +0 -85
  140. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/poly_use_hint_32_asm.S +0 -102
  141. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/poly_use_hint_88_asm.S +0 -110
  142. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/polyz_unpack_17_asm.S +0 -72
  143. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/polyz_unpack_19_asm.S +0 -69
  144. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/rej_uniform_asm.S +0 -189
  145. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/rej_uniform_eta2_asm.S +0 -135
  146. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/rej_uniform_eta4_asm.S +0 -128
  147. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/intt.S +0 -2311
  148. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/ntt.S +0 -2383
  149. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/nttunpack.S +0 -239
  150. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/pointwise.S +0 -131
  151. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/pointwise_acc_l4.S +0 -139
  152. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/pointwise_acc_l5.S +0 -155
  153. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/pointwise_acc_l7.S +0 -187
  154. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/poly_caddq_avx2.c +0 -61
@@ -8,54 +8,38 @@
8
8
  #include "packing.h"
9
9
  #include "poly.h"
10
10
  #include "polyvec.h"
11
+ #include "rounding.h"
11
12
 
12
13
  /* Parameter set namespacing
13
14
  * This is to facilitate building multiple instances
14
15
  * of mldsa-native (e.g. with varying parameter sets)
15
16
  * within a single compilation unit. */
16
- #define mld_unpack_hints MLD_ADD_PARAM_SET(mld_unpack_hints)
17
17
  /* End of parameter set namespacing */
18
18
 
19
+ #if !defined(MLD_CONFIG_NO_VERIFY_API)
19
20
  MLD_INTERNAL_API
20
- void mld_pack_pk(uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES],
21
- const uint8_t rho[MLDSA_SEEDBYTES], const mld_polyveck *t1)
21
+ void mld_unpack_pk_t1(mld_poly *t1,
22
+ const uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES],
23
+ unsigned int i)
22
24
  {
23
- unsigned int i;
24
-
25
- mld_memcpy(pk, rho, MLDSA_SEEDBYTES);
26
- for (i = 0; i < MLDSA_K; ++i)
27
- __loop__(
28
- assigns(i, memory_slice(pk, MLDSA_CRYPTO_PUBLICKEYBYTES))
29
- invariant(i <= MLDSA_K)
30
- decreases(MLDSA_K - i)
31
- )
32
- {
33
- mld_polyt1_pack(pk + MLDSA_SEEDBYTES + i * MLDSA_POLYT1_PACKEDBYTES,
34
- &t1->vec[i]);
35
- }
25
+ mld_polyt1_unpack(t1, pk + MLDSA_SEEDBYTES + i * MLDSA_POLYT1_PACKEDBYTES);
36
26
  }
27
+ #endif /* !MLD_CONFIG_NO_VERIFY_API */
37
28
 
29
+ #if !defined(MLD_CONFIG_NO_KEYPAIR_API)
38
30
  MLD_INTERNAL_API
39
- void mld_unpack_pk(uint8_t rho[MLDSA_SEEDBYTES], mld_polyveck *t1,
40
- const uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES])
31
+ void mld_pack_sk_s1(uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES],
32
+ const mld_polyvecl *s1)
41
33
  {
42
- unsigned int i;
43
-
44
- mld_memcpy(rho, pk, MLDSA_SEEDBYTES);
45
- pk += MLDSA_SEEDBYTES;
46
-
47
- for (i = 0; i < MLDSA_K; ++i)
48
- {
49
- mld_polyt1_unpack(&t1->vec[i], pk + i * MLDSA_POLYT1_PACKEDBYTES);
50
- }
34
+ mld_polyvecl_pack_eta(sk + 2 * MLDSA_SEEDBYTES + MLDSA_TRBYTES, s1);
51
35
  }
52
36
 
53
37
  MLD_INTERNAL_API
54
- void mld_pack_sk(uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES],
55
- const uint8_t rho[MLDSA_SEEDBYTES],
56
- const uint8_t tr[MLDSA_TRBYTES],
57
- const uint8_t key[MLDSA_SEEDBYTES], const mld_polyveck *t0,
58
- const mld_polyvecl *s1, const mld_polyveck *s2)
38
+ void mld_pack_sk_rho_key_tr_s2(uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES],
39
+ const uint8_t rho[MLDSA_SEEDBYTES],
40
+ const uint8_t tr[MLDSA_TRBYTES],
41
+ const uint8_t key[MLDSA_SEEDBYTES],
42
+ const mld_polyveck *s2)
59
43
  {
60
44
  mld_memcpy(sk, rho, MLDSA_SEEDBYTES);
61
45
  sk += MLDSA_SEEDBYTES;
@@ -66,19 +50,19 @@ void mld_pack_sk(uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES],
66
50
  mld_memcpy(sk, tr, MLDSA_TRBYTES);
67
51
  sk += MLDSA_TRBYTES;
68
52
 
69
- mld_polyvecl_pack_eta(sk, s1);
53
+ /* s1 already packed via mld_pack_sk_s1 */
70
54
  sk += MLDSA_L * MLDSA_POLYETA_PACKEDBYTES;
71
55
 
72
56
  mld_polyveck_pack_eta(sk, s2);
73
- sk += MLDSA_K * MLDSA_POLYETA_PACKEDBYTES;
74
-
75
- mld_polyveck_pack_t0(sk, t0);
57
+ /* t0 already packed via mld_compute_pack_t0_t1 */
76
58
  }
59
+ #endif /* !MLD_CONFIG_NO_KEYPAIR_API */
77
60
 
61
+ #if !defined(MLD_CONFIG_NO_SIGN_API)
78
62
  MLD_INTERNAL_API
79
63
  void mld_unpack_sk(uint8_t rho[MLDSA_SEEDBYTES], uint8_t tr[MLDSA_TRBYTES],
80
- uint8_t key[MLDSA_SEEDBYTES], mld_polyveck *t0,
81
- mld_polyvecl *s1, mld_polyveck *s2,
64
+ uint8_t key[MLDSA_SEEDBYTES], mld_sk_t0hat *t0,
65
+ mld_sk_s1hat *s1, mld_sk_s2hat *s2,
82
66
  const uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES])
83
67
  {
84
68
  mld_memcpy(rho, sk, MLDSA_SEEDBYTES);
@@ -90,85 +74,83 @@ void mld_unpack_sk(uint8_t rho[MLDSA_SEEDBYTES], uint8_t tr[MLDSA_TRBYTES],
90
74
  mld_memcpy(tr, sk, MLDSA_TRBYTES);
91
75
  sk += MLDSA_TRBYTES;
92
76
 
93
- mld_polyvecl_unpack_eta(s1, sk);
77
+ mld_unpack_sk_s1hat(s1, sk);
94
78
  sk += MLDSA_L * MLDSA_POLYETA_PACKEDBYTES;
95
79
 
96
- mld_polyveck_unpack_eta(s2, sk);
80
+ mld_unpack_sk_s2hat(s2, sk);
97
81
  sk += MLDSA_K * MLDSA_POLYETA_PACKEDBYTES;
98
82
 
99
- mld_polyveck_unpack_t0(t0, sk);
83
+ mld_unpack_sk_t0hat(t0, sk);
100
84
  }
101
85
 
102
86
  MLD_INTERNAL_API
103
- void mld_pack_sig_c_h(uint8_t sig[MLDSA_CRYPTO_BYTES],
104
- const uint8_t c[MLDSA_CTILDEBYTES], const mld_polyveck *h,
105
- const unsigned int number_of_hints)
87
+ void mld_pack_sig_c(uint8_t sig[MLDSA_CRYPTO_BYTES],
88
+ const uint8_t c[MLDSA_CTILDEBYTES])
106
89
  {
107
- unsigned int i, j, k;
108
-
109
90
  mld_memcpy(sig, c, MLDSA_CTILDEBYTES);
110
- sig += MLDSA_CTILDEBYTES;
111
-
112
- /* skip z component - packed via mld_pack_sig_z */
113
- sig += MLDSA_L * MLDSA_POLYZ_PACKEDBYTES;
91
+ }
114
92
 
115
- /* Encode hints h */
93
+ MLD_INTERNAL_API
94
+ int mld_pack_sig_h(uint8_t sig[MLDSA_CRYPTO_BYTES], const mld_polyveck *w0,
95
+ const mld_polyveck *w1)
96
+ {
97
+ unsigned int j, k, n;
116
98
 
117
- /* The final section of sig[] is MLDSA_POLYVECH_PACKEDBYTES long, where
118
- * MLDSA_POLYVECH_PACKEDBYTES = MLDSA_OMEGA + MLDSA_K
99
+ /* The hint section of sig[] is MLDSA_POLYVECH_PACKEDBYTES long, where
100
+ * MLDSA_POLYVECH_PACKEDBYTES = MLDSA_OMEGA + MLDSA_K.
119
101
  *
120
102
  * The first OMEGA bytes record the index numbers of the coefficients
121
- * that are not equal to 0
103
+ * that are not equal to 0.
122
104
  *
123
105
  * The final K bytes record a running tally of the number of hints
124
- * coming from each of the K polynomials in h.
125
- *
126
- * The pre-condition tells us that number_of_hints <= OMEGA, so some
127
- * bytes may not be written, so we initialize all of them to zero
128
- * to start.
129
- */
130
- mld_memset(sig, 0, MLDSA_POLYVECH_PACKEDBYTES);
106
+ * coming from each of the K polynomials. */
107
+ uint8_t *sig_h = sig + MLDSA_CTILDEBYTES + MLDSA_L * MLDSA_POLYZ_PACKEDBYTES;
108
+
109
+ mld_memset(sig_h, 0, MLDSA_POLYVECH_PACKEDBYTES);
110
+ n = 0;
131
111
 
132
- k = 0;
133
- /* For each polynomial in h... */
134
- for (i = 0; i < MLDSA_K; ++i)
112
+ /* For each coefficient of each polynomial, compute its hint bit and, if
113
+ * non-zero, record the index in the hint section of sig. If recording the
114
+ * hint would overflow the OMEGA-sized index array, abort early and return
115
+ * MLD_ERR_FAIL. The caller is expected to reject the signature in that case.
116
+ *
117
+ * Constant time: At this point w0/w1 are public (see comment in sign.c
118
+ * before the call), so a data-dependent early return is fine. */
119
+ for (k = 0; k < MLDSA_K; k++)
135
120
  __loop__(
136
- assigns(i, j, k, memory_slice(sig, MLDSA_POLYVECH_PACKEDBYTES))
137
- invariant(i <= MLDSA_K)
138
- invariant(k <= number_of_hints)
139
- invariant(number_of_hints <= MLDSA_OMEGA)
140
- decreases(MLDSA_K - i)
121
+ assigns(k, j, n, memory_slice(sig_h, MLDSA_POLYVECH_PACKEDBYTES))
122
+ invariant(k <= MLDSA_K && n <= MLDSA_OMEGA)
123
+ decreases(MLDSA_K - k)
141
124
  )
142
125
  {
143
- /* For each coefficient in that polynomial, record it as as hint */
144
- /* if its value is not zero */
145
- for (j = 0; j < MLDSA_N; ++j)
126
+ for (j = 0; j < MLDSA_N; j++)
146
127
  __loop__(
147
- assigns(j, k, memory_slice(sig, MLDSA_POLYVECH_PACKEDBYTES))
148
- invariant(i <= MLDSA_K)
149
- invariant(j <= MLDSA_N)
150
- invariant(k <= number_of_hints)
151
- invariant(number_of_hints <= MLDSA_OMEGA)
128
+ assigns(j, n, memory_slice(sig_h, MLDSA_POLYVECH_PACKEDBYTES))
129
+ invariant(j <= MLDSA_N && n <= MLDSA_OMEGA)
152
130
  decreases(MLDSA_N - j)
153
131
  )
154
132
  {
155
- /* The reference implementation implicitly relies on the total */
156
- /* number of hints being less than OMEGA, assuming h is valid. */
157
- /* In mldsa-native, we check this explicitly to ease proof of */
158
- /* type safety. */
159
- if (h->vec[i].coeffs[j] != 0 && k < number_of_hints)
133
+ const unsigned int hint_bit =
134
+ mld_make_hint(w0->vec[k].coeffs[j], w1->vec[k].coeffs[j]);
135
+ if (hint_bit)
160
136
  {
161
- /* The enclosing if condition AND the loop invariant infer */
162
- /* that k < MLDSA_OMEGA, so writing to sig[k] is safe and k */
163
- /* can be incremented. */
164
- sig[k++] = (uint8_t)j;
137
+ if (n == MLDSA_OMEGA)
138
+ {
139
+ return MLD_ERR_FAIL;
140
+ }
141
+ /* Safety: branch above ensures n < MLDSA_OMEGA so n is a valid index
142
+ * into the OMEGA-sized index array; j < MLDSA_N <= 256 fits in
143
+ * uint8_t. */
144
+ sig_h[n] = (uint8_t)j;
145
+ n++;
165
146
  }
166
147
  }
167
- /* Having recorded all the hints for this polynomial, also */
168
- /* record the running tally into the correct "slot" for that */
169
- /* coefficient in the final K bytes */
170
- sig[MLDSA_OMEGA + i] = (uint8_t)k;
148
+ /* Record the running tally into the correct slot for this polynomial.
149
+ * Safety: k < MLDSA_K, so MLDSA_OMEGA + k is a valid index into the
150
+ * K-byte tally tail; n <= MLDSA_OMEGA fits in uint8_t. */
151
+ sig_h[MLDSA_OMEGA + k] = (uint8_t)n;
171
152
  }
153
+ return 0;
172
154
  }
173
155
 
174
156
  MLD_INTERNAL_API
@@ -179,115 +161,62 @@ void mld_pack_sig_z(uint8_t sig[MLDSA_CRYPTO_BYTES], const mld_poly *zi,
179
161
  sig += i * MLDSA_POLYZ_PACKEDBYTES;
180
162
  mld_polyz_pack(sig, zi);
181
163
  }
164
+ #endif /* !MLD_CONFIG_NO_SIGN_API */
182
165
 
183
- /*************************************************
184
- * Name: mld_unpack_hints
185
- *
186
- * Description: Unpack raw hint bytes into a polyveck
187
- * struct
188
- *
189
- * Arguments: - mld_polyveck *h: pointer to output hint vector h
190
- * - const uint8_t packed_hints[MLDSA_POLYVECH_PACKEDBYTES]:
191
- * raw hint bytes
192
- *
193
- * Returns 1 in case of malformed hints; otherwise 0.
194
- **************************************************/
195
- static int mld_unpack_hints(
196
- mld_polyveck *h, const uint8_t packed_hints[MLDSA_POLYVECH_PACKEDBYTES])
197
- __contract__(
198
- requires(memory_no_alias(packed_hints, MLDSA_POLYVECH_PACKEDBYTES))
199
- requires(memory_no_alias(h, sizeof(mld_polyveck)))
200
- assigns(memory_slice(h, sizeof(mld_polyveck)))
201
- /* All returned coefficients are either 0 or 1 */
202
- ensures(forall(k1, 0, MLDSA_K,
203
- array_bound(h->vec[k1].coeffs, 0, MLDSA_N, 0, 2)))
204
- ensures(return_value >= 0 && return_value <= 1)
205
- )
166
+ #if !defined(MLD_CONFIG_NO_VERIFY_API)
167
+ MLD_INTERNAL_API
168
+ int mld_sig_unpack_hints(mld_poly *h, const uint8_t sig[MLDSA_CRYPTO_BYTES],
169
+ unsigned int i)
206
170
  {
207
- unsigned int i, j;
208
- unsigned int old_hint_count;
171
+ const uint8_t *packed_hints =
172
+ sig + MLDSA_CTILDEBYTES + MLDSA_L * MLDSA_POLYZ_PACKEDBYTES;
173
+ const unsigned int old_hint_count =
174
+ (i == 0) ? 0 : packed_hints[MLDSA_OMEGA + i - 1];
175
+ const unsigned int new_hint_count = packed_hints[MLDSA_OMEGA + i];
176
+ unsigned int j;
177
+
178
+ if (new_hint_count < old_hint_count || new_hint_count > MLDSA_OMEGA)
179
+ {
180
+ return MLD_ERR_FAIL;
181
+ }
209
182
 
210
- /* Set all coefficients of all polynomials to 0. */
211
- /* Only those that are actually non-zero hints will */
212
- /* be overwritten below. */
213
- mld_memset(h, 0, sizeof(mld_polyveck));
183
+ mld_memset(h, 0, sizeof(mld_poly));
214
184
 
215
- old_hint_count = 0;
216
- for (i = 0; i < MLDSA_K; ++i)
185
+ for (j = old_hint_count; j < new_hint_count; ++j)
217
186
  __loop__(
218
- invariant(i <= MLDSA_K)
219
- /* Maintain the post-condition */
220
- invariant(forall(k1, 0, MLDSA_K, array_bound(h->vec[k1].coeffs, 0, MLDSA_N, 0, 2)))
221
- decreases(MLDSA_K - i)
187
+ invariant(j >= old_hint_count && j <= new_hint_count &&
188
+ new_hint_count <= MLDSA_OMEGA)
189
+ invariant(array_bound(h->coeffs, 0, MLDSA_N, 0, 2))
190
+ decreases(new_hint_count - j)
222
191
  )
223
192
  {
224
- /* Grab the hint count for the i'th polynomial */
225
- const unsigned int new_hint_count = packed_hints[MLDSA_OMEGA + i];
226
-
227
- /* new_hint_count must increase or stay the same, but also remain */
228
- /* less than or equal to MLDSA_OMEGA */
229
- if (new_hint_count < old_hint_count || new_hint_count > MLDSA_OMEGA)
193
+ if (j > old_hint_count && packed_hints[j] <= packed_hints[j - 1])
230
194
  {
231
- /* Error - new_hint_count is invalid */
232
- return 1;
195
+ return MLD_ERR_FAIL;
233
196
  }
197
+ /* Safety: packed_hints[j] is uint8_t (<= 255) and MLDSA_N == 256. */
198
+ h->coeffs[packed_hints[j]] = 1;
199
+ }
234
200
 
235
- /* If new_hint_count == old_hint_count, then this polynomial has */
236
- /* zero hints, so this loop executes zero times and we move */
237
- /* straight on to the next polynomial. */
238
- for (j = old_hint_count; j < new_hint_count; ++j)
201
+ /* On the last row, also verify that the trailing index slots are zero. */
202
+ if (i == MLDSA_K - 1)
203
+ {
204
+ for (j = new_hint_count; j < MLDSA_OMEGA; ++j)
239
205
  __loop__(
240
- invariant(i <= MLDSA_K)
241
- /* Maintain the post-condition */
242
- invariant(j <= new_hint_count && new_hint_count <= MLDSA_OMEGA)
243
- invariant(forall(k1, 0, MLDSA_K, array_bound(h->vec[k1].coeffs, 0, MLDSA_N, 0, 2)))
244
- decreases(new_hint_count - j)
245
- )
206
+ invariant(j <= MLDSA_OMEGA)
207
+ decreases(MLDSA_OMEGA - j)
208
+ )
246
209
  {
247
- const uint8_t this_hint_index = packed_hints[j];
248
-
249
- /* Coefficients must be ordered for strong unforgeability */
250
- if (j > old_hint_count && this_hint_index <= packed_hints[j - 1])
210
+ if (packed_hints[j] != 0)
251
211
  {
252
- return 1;
212
+ return MLD_ERR_FAIL;
253
213
  }
254
- h->vec[i].coeffs[this_hint_index] = 1;
255
- }
256
-
257
- old_hint_count = new_hint_count;
258
- }
259
-
260
- /* Extra indices must be zero for strong unforgeability */
261
- for (j = old_hint_count; j < MLDSA_OMEGA; ++j)
262
- __loop__(
263
- invariant(j <= MLDSA_OMEGA)
264
- /* Maintain the post-condition */
265
- invariant(forall(k1, 0, MLDSA_K, array_bound(h->vec[k1].coeffs, 0, MLDSA_N, 0, 2)))
266
- decreases(MLDSA_OMEGA - j)
267
- )
268
- {
269
- if (packed_hints[j] != 0)
270
- {
271
- return 1;
272
214
  }
273
215
  }
274
216
 
275
217
  return 0;
276
218
  }
277
-
278
- MLD_INTERNAL_API
279
- int mld_unpack_sig(uint8_t c[MLDSA_CTILDEBYTES], mld_polyvecl *z,
280
- mld_polyveck *h, const uint8_t sig[MLDSA_CRYPTO_BYTES])
281
- {
282
- mld_memcpy(c, sig, MLDSA_CTILDEBYTES);
283
- sig += MLDSA_CTILDEBYTES;
284
-
285
- mld_polyvecl_unpack_z(z, sig);
286
- sig += MLDSA_L * MLDSA_POLYZ_PACKEDBYTES;
287
-
288
- return mld_unpack_hints(h, sig);
289
- }
219
+ #endif /* !MLD_CONFIG_NO_VERIFY_API */
290
220
 
291
221
  /* To facilitate single-compilation-unit (SCU) builds, undefine all macros.
292
222
  * Don't modify by hand -- this is auto-generated by scripts/autogen. */
293
- #undef mld_unpack_hints