pq_crypto 0.3.2 → 0.5.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 (328) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +56 -0
  3. data/CHANGELOG.md +62 -0
  4. data/GET_STARTED.md +366 -40
  5. data/README.md +76 -233
  6. data/SECURITY.md +107 -82
  7. data/ext/pqcrypto/extconf.rb +169 -87
  8. data/ext/pqcrypto/mldsa_api.h +1 -48
  9. data/ext/pqcrypto/mlkem_api.h +1 -18
  10. data/ext/pqcrypto/pq_externalmu.c +89 -204
  11. data/ext/pqcrypto/pqcrypto_native_api.h +129 -0
  12. data/ext/pqcrypto/pqcrypto_ruby_secure.c +484 -84
  13. data/ext/pqcrypto/pqcrypto_secure.c +203 -78
  14. data/ext/pqcrypto/pqcrypto_secure.h +53 -14
  15. data/ext/pqcrypto/pqcrypto_version.h +7 -0
  16. data/ext/pqcrypto/randombytes.h +9 -0
  17. data/ext/pqcrypto/vendor/.vendored +10 -5
  18. data/ext/pqcrypto/vendor/mldsa-native/BUILDING.md +105 -0
  19. data/ext/pqcrypto/vendor/mldsa-native/LICENSE +286 -0
  20. data/ext/pqcrypto/vendor/mldsa-native/META.yml +24 -0
  21. data/ext/pqcrypto/vendor/mldsa-native/README.md +221 -0
  22. data/ext/pqcrypto/vendor/mldsa-native/SECURITY.md +8 -0
  23. data/ext/pqcrypto/vendor/mldsa-native/mldsa/mldsa_native.c +721 -0
  24. data/ext/pqcrypto/vendor/mldsa-native/mldsa/mldsa_native.h +975 -0
  25. data/ext/pqcrypto/vendor/mldsa-native/mldsa/mldsa_native_asm.S +724 -0
  26. data/ext/pqcrypto/vendor/mldsa-native/mldsa/mldsa_native_config.h +723 -0
  27. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/cbmc.h +166 -0
  28. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/common.h +321 -0
  29. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/ct.c +21 -0
  30. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/ct.h +385 -0
  31. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/debug.c +73 -0
  32. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/debug.h +130 -0
  33. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/fips202.c +277 -0
  34. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/fips202.h +244 -0
  35. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/fips202x4.c +182 -0
  36. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/fips202x4.h +117 -0
  37. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/keccakf1600.c +438 -0
  38. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/keccakf1600.h +105 -0
  39. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/auto.h +71 -0
  40. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/fips202_native_aarch64.h +62 -0
  41. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x1_scalar_asm.S +376 -0
  42. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x1_v84a_asm.S +204 -0
  43. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x2_v84a_asm.S +259 -0
  44. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x4_v8a_scalar_hybrid_asm.S +1077 -0
  45. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x4_v8a_v84a_scalar_hybrid_asm.S +987 -0
  46. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccakf1600_round_constants.c +41 -0
  47. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/x1_scalar.h +26 -0
  48. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/x1_v84a.h +35 -0
  49. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/x2_v84a.h +37 -0
  50. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/x4_v8a_scalar.h +27 -0
  51. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/x4_v8a_v84a_scalar.h +36 -0
  52. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/api.h +69 -0
  53. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/README.md +10 -0
  54. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/mve.h +32 -0
  55. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/src/fips202_native_armv81m.h +20 -0
  56. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/src/keccak_f1600_x4_mve.S +638 -0
  57. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/src/keccak_f1600_x4_mve.c +136 -0
  58. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/src/keccakf1600_round_constants.c +52 -0
  59. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/auto.h +29 -0
  60. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/x86_64/src/KeccakP_1600_times4_SIMD256.c +488 -0
  61. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/x86_64/src/KeccakP_1600_times4_SIMD256.h +16 -0
  62. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/x86_64/xkcp.h +31 -0
  63. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/meta.h +247 -0
  64. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/aarch64_zetas.c +231 -0
  65. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/arith_native_aarch64.h +150 -0
  66. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/intt.S +753 -0
  67. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/mld_polyvecl_pointwise_acc_montgomery_l4.S +129 -0
  68. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/mld_polyvecl_pointwise_acc_montgomery_l5.S +145 -0
  69. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/mld_polyvecl_pointwise_acc_montgomery_l7.S +177 -0
  70. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/ntt.S +653 -0
  71. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/pointwise_montgomery.S +79 -0
  72. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/poly_caddq_asm.S +53 -0
  73. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/poly_chknorm_asm.S +55 -0
  74. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/poly_decompose_32_asm.S +85 -0
  75. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/poly_decompose_88_asm.S +85 -0
  76. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/poly_use_hint_32_asm.S +102 -0
  77. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/poly_use_hint_88_asm.S +110 -0
  78. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/polyz_unpack_17_asm.S +72 -0
  79. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/polyz_unpack_19_asm.S +69 -0
  80. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/polyz_unpack_table.c +40 -0
  81. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/rej_uniform_asm.S +189 -0
  82. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/rej_uniform_eta2_asm.S +135 -0
  83. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/rej_uniform_eta4_asm.S +128 -0
  84. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/rej_uniform_eta_table.c +543 -0
  85. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/rej_uniform_table.c +62 -0
  86. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/api.h +649 -0
  87. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/meta.h +23 -0
  88. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/meta.h +315 -0
  89. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/arith_native_x86_64.h +124 -0
  90. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/consts.c +157 -0
  91. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/consts.h +27 -0
  92. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/intt.S +2311 -0
  93. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/ntt.S +2383 -0
  94. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/nttunpack.S +239 -0
  95. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/pointwise.S +131 -0
  96. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/pointwise_acc_l4.S +139 -0
  97. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/pointwise_acc_l5.S +155 -0
  98. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/pointwise_acc_l7.S +187 -0
  99. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/poly_caddq_avx2.c +61 -0
  100. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/poly_chknorm_avx2.c +52 -0
  101. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/poly_decompose_32_avx2.c +155 -0
  102. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/poly_decompose_88_avx2.c +155 -0
  103. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/poly_use_hint_32_avx2.c +102 -0
  104. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/poly_use_hint_88_avx2.c +104 -0
  105. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/polyz_unpack_17_avx2.c +91 -0
  106. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/polyz_unpack_19_avx2.c +93 -0
  107. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/rej_uniform_avx2.c +126 -0
  108. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/rej_uniform_eta2_avx2.c +155 -0
  109. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/rej_uniform_eta4_avx2.c +139 -0
  110. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/rej_uniform_table.c +160 -0
  111. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/packing.c +293 -0
  112. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/packing.h +224 -0
  113. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/params.h +77 -0
  114. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/poly.c +991 -0
  115. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/poly.h +393 -0
  116. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/poly_kl.c +946 -0
  117. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/poly_kl.h +360 -0
  118. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/polyvec.c +877 -0
  119. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/polyvec.h +725 -0
  120. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/randombytes.h +26 -0
  121. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/reduce.h +139 -0
  122. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/rounding.h +249 -0
  123. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/sign.c +1511 -0
  124. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/sign.h +806 -0
  125. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/symmetric.h +68 -0
  126. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/sys.h +268 -0
  127. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/zetas.inc +55 -0
  128. data/ext/pqcrypto/vendor/mlkem-native/BUILDING.md +104 -0
  129. data/ext/pqcrypto/vendor/mlkem-native/LICENSE +294 -0
  130. data/ext/pqcrypto/vendor/mlkem-native/META.yml +30 -0
  131. data/ext/pqcrypto/vendor/mlkem-native/README.md +223 -0
  132. data/ext/pqcrypto/vendor/mlkem-native/RELEASE.md +86 -0
  133. data/ext/pqcrypto/vendor/mlkem-native/SECURITY.md +8 -0
  134. data/ext/pqcrypto/vendor/mlkem-native/mlkem/README.md +23 -0
  135. data/ext/pqcrypto/vendor/mlkem-native/mlkem/mlkem_native.c +660 -0
  136. data/ext/pqcrypto/vendor/mlkem-native/mlkem/mlkem_native.h +538 -0
  137. data/ext/pqcrypto/vendor/mlkem-native/mlkem/mlkem_native_asm.S +681 -0
  138. data/ext/pqcrypto/vendor/mlkem-native/mlkem/mlkem_native_config.h +709 -0
  139. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/cbmc.h +174 -0
  140. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/common.h +274 -0
  141. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/compress.c +717 -0
  142. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/compress.h +688 -0
  143. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/debug.c +64 -0
  144. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/debug.h +128 -0
  145. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/fips202.c +251 -0
  146. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/fips202.h +158 -0
  147. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/fips202x4.c +208 -0
  148. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/fips202x4.h +80 -0
  149. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/keccakf1600.c +463 -0
  150. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/keccakf1600.h +98 -0
  151. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/aarch64/auto.h +70 -0
  152. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/aarch64/src/fips202_native_aarch64.h +69 -0
  153. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/aarch64/src/keccak_f1600_x1_scalar_asm.S +375 -0
  154. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/aarch64/src/keccak_f1600_x1_v84a_asm.S +203 -0
  155. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/aarch64/src/keccak_f1600_x2_v84a_asm.S +258 -0
  156. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/aarch64/src/keccak_f1600_x4_v8a_scalar_hybrid_asm.S +1076 -0
  157. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/aarch64/src/keccak_f1600_x4_v8a_v84a_scalar_hybrid_asm.S +986 -0
  158. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/aarch64/src/keccakf1600_round_constants.c +46 -0
  159. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/aarch64/x1_scalar.h +25 -0
  160. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/aarch64/x1_v84a.h +34 -0
  161. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/aarch64/x2_v84a.h +35 -0
  162. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/aarch64/x4_v8a_scalar.h +26 -0
  163. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/aarch64/x4_v8a_v84a_scalar.h +35 -0
  164. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/api.h +117 -0
  165. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/armv81m/README.md +10 -0
  166. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/armv81m/mve.h +79 -0
  167. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/armv81m/src/fips202_native_armv81m.h +35 -0
  168. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/armv81m/src/keccak_f1600_x4_mve.S +667 -0
  169. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/armv81m/src/keccak_f1600_x4_mve.c +40 -0
  170. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/armv81m/src/keccakf1600_round_constants.c +51 -0
  171. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/armv81m/src/state_extract_bytes_x4_mve.S +290 -0
  172. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/armv81m/src/state_xor_bytes_x4_mve.S +314 -0
  173. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/auto.h +28 -0
  174. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/x86_64/keccak_f1600_x4_avx2.h +33 -0
  175. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/x86_64/src/fips202_native_x86_64.h +41 -0
  176. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/x86_64/src/keccak_f1600_x4_avx2.S +451 -0
  177. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/x86_64/src/keccakf1600_constants.c +51 -0
  178. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/indcpa.c +622 -0
  179. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/indcpa.h +156 -0
  180. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/kem.c +446 -0
  181. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/kem.h +326 -0
  182. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/aarch64/README.md +16 -0
  183. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/aarch64/meta.h +122 -0
  184. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/aarch64/src/aarch64_zetas.c +174 -0
  185. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/aarch64/src/arith_native_aarch64.h +177 -0
  186. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/aarch64/src/intt.S +628 -0
  187. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/aarch64/src/ntt.S +562 -0
  188. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/aarch64/src/poly_mulcache_compute_asm.S +127 -0
  189. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/aarch64/src/poly_reduce_asm.S +150 -0
  190. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/aarch64/src/poly_tobytes_asm.S +117 -0
  191. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/aarch64/src/poly_tomont_asm.S +98 -0
  192. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/aarch64/src/polyvec_basemul_acc_montgomery_cached_asm_k2.S +261 -0
  193. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/aarch64/src/polyvec_basemul_acc_montgomery_cached_asm_k3.S +314 -0
  194. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/aarch64/src/polyvec_basemul_acc_montgomery_cached_asm_k4.S +368 -0
  195. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/aarch64/src/rej_uniform_asm.S +226 -0
  196. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/aarch64/src/rej_uniform_table.c +542 -0
  197. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/api.h +637 -0
  198. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/meta.h +25 -0
  199. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/riscv64/README.md +11 -0
  200. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/riscv64/meta.h +128 -0
  201. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/riscv64/src/arith_native_riscv64.h +45 -0
  202. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/riscv64/src/rv64v_debug.c +81 -0
  203. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/riscv64/src/rv64v_debug.h +145 -0
  204. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/riscv64/src/rv64v_izetas.inc +27 -0
  205. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/riscv64/src/rv64v_poly.c +805 -0
  206. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/riscv64/src/rv64v_zetas.inc +27 -0
  207. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/riscv64/src/rv64v_zetas_basemul.inc +39 -0
  208. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/x86_64/README.md +4 -0
  209. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/x86_64/meta.h +304 -0
  210. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/x86_64/src/arith_native_x86_64.h +309 -0
  211. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/x86_64/src/compress_consts.c +94 -0
  212. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/x86_64/src/compress_consts.h +45 -0
  213. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/x86_64/src/consts.c +102 -0
  214. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/x86_64/src/consts.h +25 -0
  215. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/x86_64/src/intt.S +719 -0
  216. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/x86_64/src/mulcache_compute.S +90 -0
  217. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/x86_64/src/ntt.S +639 -0
  218. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/x86_64/src/nttfrombytes.S +193 -0
  219. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/x86_64/src/ntttobytes.S +181 -0
  220. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/x86_64/src/nttunpack.S +174 -0
  221. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/x86_64/src/poly_compress_d10.S +382 -0
  222. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/x86_64/src/poly_compress_d11.S +448 -0
  223. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/x86_64/src/poly_compress_d4.S +163 -0
  224. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/x86_64/src/poly_compress_d5.S +220 -0
  225. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/x86_64/src/poly_decompress_d10.S +228 -0
  226. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/x86_64/src/poly_decompress_d11.S +277 -0
  227. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/x86_64/src/poly_decompress_d4.S +180 -0
  228. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/x86_64/src/poly_decompress_d5.S +192 -0
  229. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/x86_64/src/polyvec_basemul_acc_montgomery_cached_asm_k2.S +502 -0
  230. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/x86_64/src/polyvec_basemul_acc_montgomery_cached_asm_k3.S +750 -0
  231. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/x86_64/src/polyvec_basemul_acc_montgomery_cached_asm_k4.S +998 -0
  232. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/x86_64/src/reduce.S +218 -0
  233. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/x86_64/src/rej_uniform_asm.S +103 -0
  234. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/x86_64/src/rej_uniform_table.c +544 -0
  235. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/x86_64/src/tomont.S +155 -0
  236. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/params.h +76 -0
  237. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/poly.c +572 -0
  238. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/poly.h +317 -0
  239. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/poly_k.c +502 -0
  240. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/poly_k.h +668 -0
  241. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/randombytes.h +60 -0
  242. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/sampling.c +362 -0
  243. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/sampling.h +118 -0
  244. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/symmetric.h +70 -0
  245. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/sys.h +260 -0
  246. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/verify.c +20 -0
  247. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/verify.h +464 -0
  248. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/zetas.inc +30 -0
  249. data/lib/pq_crypto/algorithm_registry.rb +200 -0
  250. data/lib/pq_crypto/hybrid_kem.rb +1 -12
  251. data/lib/pq_crypto/kem.rb +104 -13
  252. data/lib/pq_crypto/pkcs8.rb +387 -0
  253. data/lib/pq_crypto/serialization.rb +1 -14
  254. data/lib/pq_crypto/signature.rb +123 -17
  255. data/lib/pq_crypto/spki.rb +131 -0
  256. data/lib/pq_crypto/version.rb +1 -1
  257. data/lib/pq_crypto.rb +79 -20
  258. data/script/vendor_libs.rb +88 -155
  259. metadata +241 -73
  260. data/ext/pqcrypto/vendor/pqclean/common/aes.c +0 -639
  261. data/ext/pqcrypto/vendor/pqclean/common/aes.h +0 -64
  262. data/ext/pqcrypto/vendor/pqclean/common/compat.h +0 -73
  263. data/ext/pqcrypto/vendor/pqclean/common/crypto_declassify.h +0 -7
  264. data/ext/pqcrypto/vendor/pqclean/common/fips202.c +0 -928
  265. data/ext/pqcrypto/vendor/pqclean/common/fips202.h +0 -166
  266. data/ext/pqcrypto/vendor/pqclean/common/keccak2x/feat.S +0 -168
  267. data/ext/pqcrypto/vendor/pqclean/common/keccak2x/fips202x2.c +0 -684
  268. data/ext/pqcrypto/vendor/pqclean/common/keccak2x/fips202x2.h +0 -60
  269. data/ext/pqcrypto/vendor/pqclean/common/keccak4x/KeccakP-1600-times4-SIMD256.c +0 -1028
  270. data/ext/pqcrypto/vendor/pqclean/common/keccak4x/KeccakP-1600-times4-SnP.h +0 -50
  271. data/ext/pqcrypto/vendor/pqclean/common/keccak4x/KeccakP-1600-unrolling.macros +0 -198
  272. data/ext/pqcrypto/vendor/pqclean/common/keccak4x/Makefile +0 -8
  273. data/ext/pqcrypto/vendor/pqclean/common/keccak4x/Makefile.Microsoft_nmake +0 -8
  274. data/ext/pqcrypto/vendor/pqclean/common/keccak4x/SIMD256-config.h +0 -3
  275. data/ext/pqcrypto/vendor/pqclean/common/keccak4x/align.h +0 -34
  276. data/ext/pqcrypto/vendor/pqclean/common/keccak4x/brg_endian.h +0 -142
  277. data/ext/pqcrypto/vendor/pqclean/common/nistseedexpander.c +0 -101
  278. data/ext/pqcrypto/vendor/pqclean/common/nistseedexpander.h +0 -39
  279. data/ext/pqcrypto/vendor/pqclean/common/randombytes.c +0 -355
  280. data/ext/pqcrypto/vendor/pqclean/common/randombytes.h +0 -27
  281. data/ext/pqcrypto/vendor/pqclean/common/sha2.c +0 -769
  282. data/ext/pqcrypto/vendor/pqclean/common/sha2.h +0 -173
  283. data/ext/pqcrypto/vendor/pqclean/common/sp800-185.c +0 -156
  284. data/ext/pqcrypto/vendor/pqclean/common/sp800-185.h +0 -27
  285. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/LICENSE +0 -5
  286. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/Makefile +0 -19
  287. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/Makefile.Microsoft_nmake +0 -23
  288. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/api.h +0 -18
  289. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/cbd.c +0 -83
  290. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/cbd.h +0 -11
  291. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/indcpa.c +0 -327
  292. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/indcpa.h +0 -22
  293. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/kem.c +0 -164
  294. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/kem.h +0 -23
  295. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/ntt.c +0 -146
  296. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/ntt.h +0 -14
  297. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/params.h +0 -36
  298. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/poly.c +0 -299
  299. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/poly.h +0 -37
  300. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/polyvec.c +0 -188
  301. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/polyvec.h +0 -26
  302. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/reduce.c +0 -41
  303. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/reduce.h +0 -13
  304. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/symmetric-shake.c +0 -71
  305. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/symmetric.h +0 -30
  306. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/verify.c +0 -67
  307. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/verify.h +0 -13
  308. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/LICENSE +0 -5
  309. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/Makefile +0 -19
  310. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/Makefile.Microsoft_nmake +0 -23
  311. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/api.h +0 -50
  312. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/ntt.c +0 -98
  313. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/ntt.h +0 -10
  314. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/packing.c +0 -261
  315. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/packing.h +0 -31
  316. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/params.h +0 -44
  317. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/poly.c +0 -799
  318. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/poly.h +0 -52
  319. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/polyvec.c +0 -415
  320. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/polyvec.h +0 -65
  321. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/reduce.c +0 -69
  322. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/reduce.h +0 -17
  323. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/rounding.c +0 -92
  324. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/rounding.h +0 -14
  325. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/sign.c +0 -407
  326. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/sign.h +0 -47
  327. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/symmetric-shake.c +0 -26
  328. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/symmetric.h +0 -34
@@ -0,0 +1,60 @@
1
+ /*
2
+ * Copyright (c) The mlkem-native project authors
3
+ * SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
4
+ */
5
+ #ifndef MLK_RANDOMBYTES_H
6
+ #define MLK_RANDOMBYTES_H
7
+
8
+
9
+ #include "cbmc.h"
10
+ #include "common.h"
11
+
12
+ #if !defined(MLK_CONFIG_NO_RANDOMIZED_API)
13
+ #if !defined(MLK_CONFIG_CUSTOM_RANDOMBYTES)
14
+ /*************************************************
15
+ * Name: randombytes
16
+ *
17
+ * Description: Fill a buffer with cryptographically secure random bytes.
18
+ *
19
+ * mlkem-native does not provide an implementation of this
20
+ * function. It must be provided by the consumer.
21
+ *
22
+ * To use a custom random byte source with a different name
23
+ * or signature, set MLK_CONFIG_CUSTOM_RANDOMBYTES and define
24
+ * mlk_randombytes directly.
25
+ *
26
+ * Arguments: - uint8_t *out: pointer to output buffer
27
+ * - size_t outlen: number of random bytes to write
28
+ *
29
+ * Returns: 0 on success, non-zero on failure.
30
+ * On failure, top-level APIs return MLK_ERR_RNG_FAIL.
31
+ *
32
+ **************************************************/
33
+ int randombytes(uint8_t *out, size_t outlen);
34
+
35
+ /*************************************************
36
+ * Name: mlk_randombytes
37
+ *
38
+ * Description: Internal wrapper around randombytes().
39
+ *
40
+ * Fill a buffer with cryptographically secure random bytes.
41
+ *
42
+ * This function can be replaced by setting
43
+ * MLK_CONFIG_CUSTOM_RANDOMBYTES and defining mlk_randombytes
44
+ * directly.
45
+ *
46
+ * Arguments: - uint8_t *out: pointer to output buffer
47
+ * - size_t outlen: number of random bytes to write
48
+ *
49
+ * Returns: 0 on success, non-zero on failure.
50
+ * On failure, top-level APIs return MLK_ERR_RNG_FAIL.
51
+ *
52
+ **************************************************/
53
+ MLK_MUST_CHECK_RETURN_VALUE
54
+ static MLK_INLINE int mlk_randombytes(uint8_t *out, size_t outlen)
55
+ __contract__(
56
+ requires(memory_no_alias(out, outlen))
57
+ assigns(memory_slice(out, outlen))) { return randombytes(out, outlen); }
58
+ #endif /* !MLK_CONFIG_CUSTOM_RANDOMBYTES */
59
+ #endif /* !MLK_CONFIG_NO_RANDOMIZED_API */
60
+ #endif /* !MLK_RANDOMBYTES_H */
@@ -0,0 +1,362 @@
1
+ /*
2
+ * Copyright (c) The mlkem-native project authors
3
+ * SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
4
+ */
5
+
6
+ /* References
7
+ * ==========
8
+ *
9
+ * - [FIPS203]
10
+ * FIPS 203 Module-Lattice-Based Key-Encapsulation Mechanism Standard
11
+ * National Institute of Standards and Technology
12
+ * https://csrc.nist.gov/pubs/fips/203/final
13
+ *
14
+ * - [REF]
15
+ * CRYSTALS-Kyber C reference implementation
16
+ * Bos, Ducas, Kiltz, Lepoint, Lyubashevsky, Schanck, Schwabe, Seiler, Stehlé
17
+ * https://github.com/pq-crystals/kyber/tree/main/ref
18
+ */
19
+
20
+ #include "common.h"
21
+ #if !defined(MLK_CONFIG_MULTILEVEL_NO_SHARED)
22
+
23
+ #include "debug.h"
24
+ #include "sampling.h"
25
+ #include "symmetric.h"
26
+
27
+ /* Reference: `rej_uniform()` in the reference implementation @[REF].
28
+ * - Our signature differs from the reference implementation
29
+ * in that it adds the offset and always expects the base of the
30
+ * target buffer. This avoids shifting the buffer base in the
31
+ * caller, which appears tricky to reason about. */
32
+ MLK_STATIC_TESTABLE unsigned mlk_rej_uniform_c(int16_t *r, unsigned target,
33
+ unsigned offset,
34
+ const uint8_t *buf,
35
+ unsigned buflen)
36
+ __contract__(
37
+ requires(offset <= target && target <= 4096 && buflen <= 4096 && buflen % 3 == 0)
38
+ requires(memory_no_alias(r, sizeof(int16_t) * target))
39
+ requires(memory_no_alias(buf, buflen))
40
+ requires(array_bound(r, 0, offset, 0, MLKEM_Q))
41
+ assigns(memory_slice(r, sizeof(int16_t) * target))
42
+ ensures(offset <= return_value && return_value <= target)
43
+ ensures(array_bound(r, 0, return_value, 0, MLKEM_Q)))
44
+ {
45
+ unsigned ctr, pos;
46
+ int16_t val0, val1;
47
+
48
+ mlk_assert_bound(r, offset, 0, MLKEM_Q);
49
+
50
+ ctr = offset;
51
+ pos = 0;
52
+ /* pos + 3 cannot overflow due to the assumption buflen <= 4096 */
53
+ while (ctr < target && pos + 3 <= buflen)
54
+ __loop__(
55
+ invariant(offset <= ctr && ctr <= target && pos <= buflen)
56
+ invariant(array_bound(r, 0, ctr, 0, MLKEM_Q)))
57
+ {
58
+ val0 = ((buf[pos + 0] >> 0) | (buf[pos + 1] << 8)) & 0xFFF;
59
+ val1 = ((buf[pos + 1] >> 4) | (buf[pos + 2] << 4)) & 0xFFF;
60
+ pos += 3;
61
+
62
+ if (val0 < MLKEM_Q)
63
+ {
64
+ r[ctr++] = val0;
65
+ }
66
+ if (ctr < target && val1 < MLKEM_Q)
67
+ {
68
+ r[ctr++] = val1;
69
+ }
70
+ }
71
+
72
+ mlk_assert_bound(r, ctr, 0, MLKEM_Q);
73
+ return ctr;
74
+ }
75
+
76
+ /*************************************************
77
+ * Name: mlk_rej_uniform
78
+ *
79
+ * Description: Run rejection sampling on uniform random bytes to generate
80
+ * uniform random integers mod q
81
+ *
82
+ * Arguments: - int16_t *r: pointer to output buffer
83
+ * - unsigned target: requested number of 16-bit integers
84
+ * (uniform mod q).
85
+ * Must be <= 4096.
86
+ * - unsigned offset: number of 16-bit integers that have
87
+ * already been sampled.
88
+ * Must be <= target.
89
+ * - const uint8_t *buf: pointer to input buffer
90
+ * (assumed to be uniform random bytes)
91
+ * - unsigned buflen: length of input buffer in bytes
92
+ * Must be <= 4096.
93
+ * Must be a multiple of 3.
94
+ *
95
+ * Note: Strictly speaking, only a few values of buflen near UINT_MAX need
96
+ * excluding. The limit of 4096 is somewhat arbitrary but sufficient for all
97
+ * uses of this function. Similarly, the actual limit for target is UINT_MAX/2.
98
+ *
99
+ * Returns the new offset of sampled 16-bit integers, at most target,
100
+ * and at least the initial offset.
101
+ * If the new offset is strictly less than len, all of the input buffers
102
+ * is guaranteed to have been consumed. If it is equal to len, no information
103
+ * is provided on how many bytes of the input buffer have been consumed.
104
+ **************************************************/
105
+
106
+ /* Reference: `rej_uniform()` in the reference implementation @[REF].
107
+ * - Our signature differs from the reference implementation
108
+ * in that it adds the offset and always expects the base of the
109
+ * target buffer. This avoids shifting the buffer base in the
110
+ * caller, which appears tricky to reason about.
111
+ * - Optional fallback to native implementation. */
112
+ static unsigned mlk_rej_uniform(int16_t *r, unsigned target, unsigned offset,
113
+ const uint8_t *buf, unsigned buflen)
114
+ __contract__(
115
+ requires(offset <= target && target <= 4096 && buflen <= 4096 && buflen % 3 == 0)
116
+ requires(memory_no_alias(r, sizeof(int16_t) * target))
117
+ requires(memory_no_alias(buf, buflen))
118
+ requires(array_bound(r, 0, offset, 0, MLKEM_Q))
119
+ assigns(memory_slice(r, sizeof(int16_t) * target))
120
+ ensures(offset <= return_value && return_value <= target)
121
+ ensures(array_bound(r, 0, return_value, 0, MLKEM_Q))
122
+ )
123
+ {
124
+ #if defined(MLK_USE_NATIVE_REJ_UNIFORM)
125
+ if (offset == 0)
126
+ {
127
+ int ret;
128
+ ret = mlk_rej_uniform_native(r, target, buf, buflen);
129
+ if (ret != MLK_NATIVE_FUNC_FALLBACK)
130
+ {
131
+ unsigned res = (unsigned)ret;
132
+ mlk_assert_bound(r, res, 0, MLKEM_Q);
133
+ return res;
134
+ }
135
+ }
136
+ #endif /* MLK_USE_NATIVE_REJ_UNIFORM */
137
+
138
+ return mlk_rej_uniform_c(r, target, offset, buf, buflen);
139
+ }
140
+
141
+ #ifndef MLKEM_GEN_MATRIX_NBLOCKS
142
+ #define MLKEM_GEN_MATRIX_NBLOCKS \
143
+ ((12 * MLKEM_N / 8 * ((uint32_t)1 << 12) / MLKEM_Q + MLK_XOF_RATE) / \
144
+ MLK_XOF_RATE)
145
+ #endif
146
+
147
+ #if !defined(MLK_CONFIG_SERIAL_FIPS202_ONLY)
148
+ /* Reference: Does not exist in the reference implementation @[REF].
149
+ * - x4-batched version of `rej_uniform()` from the
150
+ * reference implementation, leveraging x4-batched Keccak-f1600. */
151
+ MLK_INTERNAL_API
152
+ void mlk_poly_rej_uniform_x4(mlk_poly *vec0, mlk_poly *vec1, mlk_poly *vec2,
153
+ mlk_poly *vec3,
154
+ uint8_t seed[4][MLK_ALIGN_UP(MLKEM_SYMBYTES + 2)])
155
+ {
156
+ /* Temporary buffers for XOF output before rejection sampling */
157
+ MLK_ALIGN uint8_t
158
+ buf[4][MLK_ALIGN_UP(MLKEM_GEN_MATRIX_NBLOCKS * MLK_XOF_RATE)];
159
+
160
+ /* Tracks the number of coefficients we have already sampled */
161
+ unsigned ctr[4];
162
+ mlk_xof_x4_ctx statex;
163
+ unsigned buflen;
164
+
165
+ mlk_xof_x4_init(&statex);
166
+ mlk_xof_x4_absorb(&statex, seed, MLKEM_SYMBYTES + 2);
167
+
168
+ /*
169
+ * Initially, squeeze heuristic number of MLKEM_GEN_MATRIX_NBLOCKS.
170
+ * This should generate the matrix entries with high probability.
171
+ */
172
+ mlk_xof_x4_squeezeblocks(buf, MLKEM_GEN_MATRIX_NBLOCKS, &statex);
173
+ buflen = MLKEM_GEN_MATRIX_NBLOCKS * MLK_XOF_RATE;
174
+ ctr[0] = mlk_rej_uniform(vec0->coeffs, MLKEM_N, 0, buf[0], buflen);
175
+ ctr[1] = mlk_rej_uniform(vec1->coeffs, MLKEM_N, 0, buf[1], buflen);
176
+ ctr[2] = mlk_rej_uniform(vec2->coeffs, MLKEM_N, 0, buf[2], buflen);
177
+ ctr[3] = mlk_rej_uniform(vec3->coeffs, MLKEM_N, 0, buf[3], buflen);
178
+
179
+ /*
180
+ * So long as not all matrix entries have been generated, squeeze
181
+ * one more block a time until we're done.
182
+ */
183
+ buflen = MLK_XOF_RATE;
184
+ while (ctr[0] < MLKEM_N || ctr[1] < MLKEM_N || ctr[2] < MLKEM_N ||
185
+ ctr[3] < MLKEM_N)
186
+ __loop__(
187
+ assigns(ctr, statex,
188
+ memory_slice(vec0, sizeof(mlk_poly)),
189
+ memory_slice(vec1, sizeof(mlk_poly)),
190
+ memory_slice(vec2, sizeof(mlk_poly)),
191
+ memory_slice(vec3, sizeof(mlk_poly)),
192
+ object_whole(buf))
193
+ invariant(ctr[0] <= MLKEM_N && ctr[1] <= MLKEM_N)
194
+ invariant(ctr[2] <= MLKEM_N && ctr[3] <= MLKEM_N)
195
+ invariant(array_bound(vec0->coeffs, 0, ctr[0], 0, MLKEM_Q))
196
+ invariant(array_bound(vec1->coeffs, 0, ctr[1], 0, MLKEM_Q))
197
+ invariant(array_bound(vec2->coeffs, 0, ctr[2], 0, MLKEM_Q))
198
+ invariant(array_bound(vec3->coeffs, 0, ctr[3], 0, MLKEM_Q)))
199
+ {
200
+ mlk_xof_x4_squeezeblocks(buf, 1, &statex);
201
+ ctr[0] = mlk_rej_uniform(vec0->coeffs, MLKEM_N, ctr[0], buf[0], buflen);
202
+ ctr[1] = mlk_rej_uniform(vec1->coeffs, MLKEM_N, ctr[1], buf[1], buflen);
203
+ ctr[2] = mlk_rej_uniform(vec2->coeffs, MLKEM_N, ctr[2], buf[2], buflen);
204
+ ctr[3] = mlk_rej_uniform(vec3->coeffs, MLKEM_N, ctr[3], buf[3], buflen);
205
+ }
206
+
207
+ mlk_xof_x4_release(&statex);
208
+
209
+ /* Specification: Partially implements
210
+ * @[FIPS203, Section 3.3, Destruction of intermediate values] */
211
+ mlk_zeroize(buf, sizeof(buf));
212
+ }
213
+ #endif /* !MLK_CONFIG_SERIAL_FIPS202_ONLY */
214
+
215
+ MLK_INTERNAL_API
216
+ void mlk_poly_rej_uniform(mlk_poly *entry, uint8_t seed[MLKEM_SYMBYTES + 2])
217
+ {
218
+ mlk_xof_ctx state;
219
+ MLK_ALIGN uint8_t buf[MLKEM_GEN_MATRIX_NBLOCKS * MLK_XOF_RATE];
220
+ unsigned ctr, buflen;
221
+
222
+ mlk_xof_init(&state);
223
+ mlk_xof_absorb(&state, seed, MLKEM_SYMBYTES + 2);
224
+
225
+ /* Initially, squeeze + sample heuristic number of MLKEM_GEN_MATRIX_NBLOCKS.
226
+ */
227
+ /* This should generate the matrix entry with high probability. */
228
+ mlk_xof_squeezeblocks(buf, MLKEM_GEN_MATRIX_NBLOCKS, &state);
229
+ buflen = MLKEM_GEN_MATRIX_NBLOCKS * MLK_XOF_RATE;
230
+ ctr = mlk_rej_uniform(entry->coeffs, MLKEM_N, 0, buf, buflen);
231
+
232
+ /* Squeeze + sample one more block a time until we're done */
233
+ buflen = MLK_XOF_RATE;
234
+ while (ctr < MLKEM_N)
235
+ __loop__(
236
+ assigns(ctr, state, memory_slice(entry, sizeof(mlk_poly)), object_whole(buf))
237
+ invariant(ctr <= MLKEM_N)
238
+ invariant(array_bound(entry->coeffs, 0, ctr, 0, MLKEM_Q)))
239
+ {
240
+ mlk_xof_squeezeblocks(buf, 1, &state);
241
+ ctr = mlk_rej_uniform(entry->coeffs, MLKEM_N, ctr, buf, buflen);
242
+ }
243
+
244
+ mlk_xof_release(&state);
245
+
246
+ /* Specification: Partially implements
247
+ * @[FIPS203, Section 3.3, Destruction of intermediate values] */
248
+ mlk_zeroize(buf, sizeof(buf));
249
+ }
250
+
251
+ /*************************************************
252
+ * Name: mlk_load32_littleendian
253
+ *
254
+ * Description: load 4 bytes into a 32-bit integer
255
+ * in little-endian order
256
+ *
257
+ * Arguments: - const uint8_t *x: pointer to input byte array
258
+ *
259
+ * Returns 32-bit unsigned integer loaded from x
260
+ *
261
+ **************************************************/
262
+
263
+ /* Reference: `load32_littleendian()` in the reference implementation @[REF]. */
264
+ static uint32_t mlk_load32_littleendian(const uint8_t x[4])
265
+ {
266
+ uint32_t r;
267
+ r = (uint32_t)x[0];
268
+ r |= (uint32_t)x[1] << 8;
269
+ r |= (uint32_t)x[2] << 16;
270
+ r |= (uint32_t)x[3] << 24;
271
+ return r;
272
+ }
273
+
274
+ /* Reference: `cbd2()` in the reference implementation @[REF]. */
275
+ MLK_INTERNAL_API
276
+ void mlk_poly_cbd2(mlk_poly *r, const uint8_t buf[2 * MLKEM_N / 4])
277
+ {
278
+ unsigned i;
279
+ for (i = 0; i < MLKEM_N / 8; i++)
280
+ __loop__(
281
+ invariant(i <= MLKEM_N / 8)
282
+ invariant(array_abs_bound(r->coeffs, 0, 8 * i, 3)))
283
+ {
284
+ unsigned j;
285
+ uint32_t t = mlk_load32_littleendian(buf + 4 * i);
286
+ uint32_t d = t & 0x55555555;
287
+ d += (t >> 1) & 0x55555555;
288
+
289
+ for (j = 0; j < 8; j++)
290
+ __loop__(
291
+ invariant(i <= MLKEM_N / 8 && j <= 8)
292
+ invariant(array_abs_bound(r->coeffs, 0, 8 * i + j, 3)))
293
+ {
294
+ const int16_t a = (d >> (4 * j + 0)) & 0x3;
295
+ const int16_t b = (d >> (4 * j + 2)) & 0x3;
296
+ r->coeffs[8 * i + j] = (int16_t)(a - b);
297
+ }
298
+ }
299
+ }
300
+
301
+ #if defined(MLK_CONFIG_MULTILEVEL_WITH_SHARED) || MLKEM_ETA1 == 3
302
+ /*************************************************
303
+ * Name: mlk_load24_littleendian
304
+ *
305
+ * Description: load 3 bytes into a 32-bit integer
306
+ * in little-endian order.
307
+ * This function is only needed for ML-KEM-512
308
+ *
309
+ * Arguments: - const uint8_t *x: pointer to input byte array
310
+ *
311
+ * Returns 32-bit unsigned integer loaded from x (most significant byte is zero)
312
+ *
313
+ **************************************************/
314
+
315
+ /* Reference: `load24_littleendian()` in the reference implementation @[REF]. */
316
+ static uint32_t mlk_load24_littleendian(const uint8_t x[3])
317
+ {
318
+ uint32_t r;
319
+ r = (uint32_t)x[0];
320
+ r |= (uint32_t)x[1] << 8;
321
+ r |= (uint32_t)x[2] << 16;
322
+ return r;
323
+ }
324
+
325
+ /* Reference: `cbd3()` in the reference implementation @[REF]. */
326
+ MLK_INTERNAL_API
327
+ void mlk_poly_cbd3(mlk_poly *r, const uint8_t buf[3 * MLKEM_N / 4])
328
+ {
329
+ unsigned i;
330
+ for (i = 0; i < MLKEM_N / 4; i++)
331
+ __loop__(
332
+ invariant(i <= MLKEM_N / 4)
333
+ invariant(array_abs_bound(r->coeffs, 0, 4 * i, 4)))
334
+ {
335
+ unsigned j;
336
+ const uint32_t t = mlk_load24_littleendian(buf + 3 * i);
337
+ uint32_t d = t & 0x00249249;
338
+ d += (t >> 1) & 0x00249249;
339
+ d += (t >> 2) & 0x00249249;
340
+
341
+ for (j = 0; j < 4; j++)
342
+ __loop__(
343
+ invariant(i <= MLKEM_N / 4 && j <= 4)
344
+ invariant(array_abs_bound(r->coeffs, 0, 4 * i + j, 4)))
345
+ {
346
+ const int16_t a = (d >> (6 * j + 0)) & 0x7;
347
+ const int16_t b = (d >> (6 * j + 3)) & 0x7;
348
+ r->coeffs[4 * i + j] = (int16_t)(a - b);
349
+ }
350
+ }
351
+ }
352
+ #endif /* MLK_CONFIG_MULTILEVEL_WITH_SHARED || MLKEM_ETA1 == 3 */
353
+
354
+ #else /* !MLK_CONFIG_MULTILEVEL_NO_SHARED */
355
+
356
+ MLK_EMPTY_CU(sampling)
357
+
358
+ #endif /* MLK_CONFIG_MULTILEVEL_NO_SHARED */
359
+
360
+ /* To facilitate single-compilation-unit (SCU) builds, undefine all macros.
361
+ * Don't modify by hand -- this is auto-generated by scripts/autogen. */
362
+ #undef MLKEM_GEN_MATRIX_NBLOCKS
@@ -0,0 +1,118 @@
1
+ /*
2
+ * Copyright (c) The mlkem-native project authors
3
+ * SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
4
+ */
5
+
6
+ /* References
7
+ * ==========
8
+ *
9
+ * - [FIPS203]
10
+ * FIPS 203 Module-Lattice-Based Key-Encapsulation Mechanism Standard
11
+ * National Institute of Standards and Technology
12
+ * https://csrc.nist.gov/pubs/fips/203/final
13
+ */
14
+
15
+ #ifndef MLK_SAMPLING_H
16
+ #define MLK_SAMPLING_H
17
+
18
+ #include "cbmc.h"
19
+ #include "common.h"
20
+ #include "poly.h"
21
+
22
+ #define mlk_poly_cbd2 MLK_NAMESPACE(poly_cbd2)
23
+ /*************************************************
24
+ * Name: mlk_poly_cbd2
25
+ *
26
+ * Description: Given an array of uniformly random bytes, compute
27
+ * polynomial with coefficients distributed according to
28
+ * a centered binomial distribution with parameter eta=2
29
+ *
30
+ * Arguments: - mlk_poly *r: pointer to output polynomial
31
+ * - const uint8_t *buf: pointer to input byte array
32
+ *
33
+ * Specification: Implements @[FIPS203, Algorithm 8, SamplePolyCBD_2]
34
+ *
35
+ **************************************************/
36
+ MLK_INTERNAL_API
37
+ void mlk_poly_cbd2(mlk_poly *r, const uint8_t buf[2 * MLKEM_N / 4]);
38
+
39
+ #if defined(MLK_CONFIG_MULTILEVEL_WITH_SHARED) || MLKEM_ETA1 == 3
40
+ #define mlk_poly_cbd3 MLK_NAMESPACE(poly_cbd3)
41
+ /*************************************************
42
+ * Name: mlk_poly_cbd3
43
+ *
44
+ * Description: Given an array of uniformly random bytes, compute
45
+ * polynomial with coefficients distributed according to
46
+ * a centered binomial distribution with parameter eta=3.
47
+ * This function is only needed for ML-KEM-512
48
+ *
49
+ * Arguments: - mlk_poly *r: pointer to output polynomial
50
+ * - const uint8_t *buf: pointer to input byte array
51
+ *
52
+ * Specification: Implements @[FIPS203, Algorithm 8, SamplePolyCBD_3]
53
+ *
54
+ **************************************************/
55
+ MLK_INTERNAL_API
56
+ void mlk_poly_cbd3(mlk_poly *r, const uint8_t buf[3 * MLKEM_N / 4]);
57
+ #endif /* MLK_CONFIG_MULTILEVEL_WITH_SHARED || MLKEM_ETA1 == 3 */
58
+
59
+ #if !defined(MLK_CONFIG_SERIAL_FIPS202_ONLY)
60
+ #define mlk_poly_rej_uniform_x4 MLK_NAMESPACE(poly_rej_uniform_x4)
61
+ /*************************************************
62
+ * Name: mlk_poly_rej_uniform_x4
63
+ *
64
+ * Description: Generate four polynomials using rejection sampling
65
+ * on (pseudo-)uniformly random bytes sampled from a seed.
66
+ *
67
+ * Arguments: - mlk_poly *vec0, *vec1, *vec2, *vec3:
68
+ * Pointers to 4 polynomials to be sampled.
69
+ * - uint8_t seed[4][MLK_ALIGN_UP(MLKEM_SYMBYTES + 2)]:
70
+ * Pointer consecutive array of seed buffers of size
71
+ * MLKEM_SYMBYTES + 2 each, plus padding for alignment.
72
+ *
73
+ * Specification: Implements @[FIPS203, Algorithm 7, SampleNTT]
74
+ *
75
+ **************************************************/
76
+ MLK_INTERNAL_API
77
+ void mlk_poly_rej_uniform_x4(mlk_poly *vec0, mlk_poly *vec1, mlk_poly *vec2,
78
+ mlk_poly *vec3,
79
+ uint8_t seed[4][MLK_ALIGN_UP(MLKEM_SYMBYTES + 2)])
80
+ __contract__(
81
+ requires(memory_no_alias(vec0, sizeof(mlk_poly)))
82
+ requires(memory_no_alias(vec1, sizeof(mlk_poly)))
83
+ requires(memory_no_alias(vec2, sizeof(mlk_poly)))
84
+ requires(memory_no_alias(vec3, sizeof(mlk_poly)))
85
+ requires(memory_no_alias(seed, 4 * MLK_ALIGN_UP(MLKEM_SYMBYTES + 2)))
86
+ assigns(memory_slice(vec0, sizeof(mlk_poly)))
87
+ assigns(memory_slice(vec1, sizeof(mlk_poly)))
88
+ assigns(memory_slice(vec2, sizeof(mlk_poly)))
89
+ assigns(memory_slice(vec3, sizeof(mlk_poly)))
90
+ ensures(array_bound(vec0->coeffs, 0, MLKEM_N, 0, MLKEM_Q))
91
+ ensures(array_bound(vec1->coeffs, 0, MLKEM_N, 0, MLKEM_Q))
92
+ ensures(array_bound(vec2->coeffs, 0, MLKEM_N, 0, MLKEM_Q))
93
+ ensures(array_bound(vec3->coeffs, 0, MLKEM_N, 0, MLKEM_Q)));
94
+ #endif /* !MLK_CONFIG_SERIAL_FIPS202_ONLY */
95
+
96
+ #define mlk_poly_rej_uniform MLK_NAMESPACE(poly_rej_uniform)
97
+ /*************************************************
98
+ * Name: mlk_poly_rej_uniform
99
+ *
100
+ * Description: Generate polynomial using rejection sampling
101
+ * on (pseudo-)uniformly random bytes sampled from a seed.
102
+ *
103
+ * Arguments: - mlk_poly *vec: Pointer to polynomial to be sampled.
104
+ * - uint8_t *seed: Pointer to seed buffer of size
105
+ * MLKEM_SYMBYTES + 2 each.
106
+ *
107
+ * Specification: Implements @[FIPS203, Algorithm 7, SampleNTT]
108
+ *
109
+ **************************************************/
110
+ MLK_INTERNAL_API
111
+ void mlk_poly_rej_uniform(mlk_poly *entry, uint8_t seed[MLKEM_SYMBYTES + 2])
112
+ __contract__(
113
+ requires(memory_no_alias(entry, sizeof(mlk_poly)))
114
+ requires(memory_no_alias(seed, MLKEM_SYMBYTES + 2))
115
+ assigns(memory_slice(entry, sizeof(mlk_poly)))
116
+ ensures(array_bound(entry->coeffs, 0, MLKEM_N, 0, MLKEM_Q)));
117
+
118
+ #endif /* !MLK_SAMPLING_H */
@@ -0,0 +1,70 @@
1
+ /*
2
+ * Copyright (c) The mlkem-native project authors
3
+ * SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
4
+ */
5
+
6
+ /* References
7
+ * ==========
8
+ *
9
+ * - [FIPS203]
10
+ * FIPS 203 Module-Lattice-Based Key-Encapsulation Mechanism Standard
11
+ * National Institute of Standards and Technology
12
+ * https://csrc.nist.gov/pubs/fips/203/final
13
+ */
14
+
15
+ #ifndef MLK_SYMMETRIC_H
16
+ #define MLK_SYMMETRIC_H
17
+
18
+
19
+ #include "cbmc.h"
20
+ #include "common.h"
21
+ #include MLK_FIPS202_HEADER_FILE
22
+ #if !defined(MLK_CONFIG_SERIAL_FIPS202_ONLY)
23
+ #include MLK_FIPS202X4_HEADER_FILE
24
+ #endif
25
+
26
+ /* Macros denoting FIPS 203 specific Hash functions */
27
+
28
+ /* Hash function H, @[FIPS203, Section 4.1, Eq (4.4)] */
29
+ #define mlk_hash_h(OUT, IN, INBYTES) mlk_sha3_256(OUT, IN, INBYTES)
30
+
31
+ /* Hash function G, @[FIPS203, Section 4.1, Eq (4.5)] */
32
+ #define mlk_hash_g(OUT, IN, INBYTES) mlk_sha3_512(OUT, IN, INBYTES)
33
+
34
+ /* Hash function J, @[FIPS203, Section 4.1, Eq (4.4)] */
35
+ #define mlk_hash_j(OUT, IN, INBYTES) \
36
+ mlk_shake256(OUT, MLKEM_SYMBYTES, IN, INBYTES)
37
+
38
+ /* PRF function, @[FIPS203, Section 4.1, Eq (4.3)]
39
+ * Referring to (eq 4.3), `OUT` is assumed to contain `s || b`. */
40
+ #define mlk_prf_eta(ETA, OUT, IN) \
41
+ mlk_shake256(OUT, (ETA) * MLKEM_N / 4, IN, MLKEM_SYMBYTES + 1)
42
+ #define mlk_prf_eta1(OUT, IN) mlk_prf_eta(MLKEM_ETA1, OUT, IN)
43
+ #define mlk_prf_eta2(OUT, IN) mlk_prf_eta(MLKEM_ETA2, OUT, IN)
44
+ #define mlk_prf_eta1_x4(OUT, IN) \
45
+ mlk_shake256x4((OUT)[0], (OUT)[1], (OUT)[2], (OUT)[3], \
46
+ (MLKEM_ETA1 * MLKEM_N / 4), (IN)[0], (IN)[1], (IN)[2], \
47
+ (IN)[3], MLKEM_SYMBYTES + 1)
48
+
49
+ /* XOF function, FIPS 203 4.1 */
50
+ #define mlk_xof_ctx mlk_shake128ctx
51
+ #define mlk_xof_x4_ctx mlk_shake128x4ctx
52
+ #define mlk_xof_init(CTX) mlk_shake128_init((CTX))
53
+ #define mlk_xof_absorb(CTX, IN, INBYTES) \
54
+ mlk_shake128_absorb_once((CTX), (IN), (INBYTES))
55
+ #define mlk_xof_squeezeblocks(BUF, NBLOCKS, CTX) \
56
+ mlk_shake128_squeezeblocks((BUF), (NBLOCKS), (CTX))
57
+ #define mlk_xof_release(CTX) mlk_shake128_release((CTX))
58
+
59
+ #define mlk_xof_x4_init(CTX) mlk_shake128x4_init((CTX))
60
+ #define mlk_xof_x4_absorb(CTX, IN, INBYTES) \
61
+ mlk_shake128x4_absorb_once((CTX), (IN)[0], (IN)[1], (IN)[2], (IN)[3], \
62
+ (INBYTES))
63
+ #define mlk_xof_x4_squeezeblocks(BUF, NBLOCKS, CTX) \
64
+ mlk_shake128x4_squeezeblocks((BUF)[0], (BUF)[1], (BUF)[2], (BUF)[3], \
65
+ (NBLOCKS), (CTX))
66
+ #define mlk_xof_x4_release(CTX) mlk_shake128x4_release((CTX))
67
+
68
+ #define MLK_XOF_RATE SHAKE128_RATE
69
+
70
+ #endif /* !MLK_SYMMETRIC_H */