longfellow 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 (289) hide show
  1. checksums.yaml +7 -0
  2. data/CODE_OF_CONDUCT.md +10 -0
  3. data/LICENSE.txt +21 -0
  4. data/README.md +152 -0
  5. data/ext/longfellow/CMakeLists.txt +76 -0
  6. data/ext/longfellow/extconf.rb +77 -0
  7. data/lib/longfellow/attribute.rb +65 -0
  8. data/lib/longfellow/c.rb +105 -0
  9. data/lib/longfellow/errors.rb +78 -0
  10. data/lib/longfellow/version.rb +5 -0
  11. data/lib/longfellow/zk_spec.rb +40 -0
  12. data/lib/longfellow.rb +162 -0
  13. data/sig/longfellow.rbs +74 -0
  14. data/vendor/longfellow-zk/LICENSE +203 -0
  15. data/vendor/longfellow-zk/lib/algebra/blas.h +121 -0
  16. data/vendor/longfellow-zk/lib/algebra/bogorng.h +68 -0
  17. data/vendor/longfellow-zk/lib/algebra/compare.h +40 -0
  18. data/vendor/longfellow-zk/lib/algebra/convolution.h +219 -0
  19. data/vendor/longfellow-zk/lib/algebra/crt.cc +42 -0
  20. data/vendor/longfellow-zk/lib/algebra/crt.h +299 -0
  21. data/vendor/longfellow-zk/lib/algebra/crt_convolution.h +114 -0
  22. data/vendor/longfellow-zk/lib/algebra/crt_test.cc +371 -0
  23. data/vendor/longfellow-zk/lib/algebra/fft.h +104 -0
  24. data/vendor/longfellow-zk/lib/algebra/fft_interpolation.h +304 -0
  25. data/vendor/longfellow-zk/lib/algebra/fft_interpolation_test.cc +168 -0
  26. data/vendor/longfellow-zk/lib/algebra/fft_test.cc +257 -0
  27. data/vendor/longfellow-zk/lib/algebra/fp.h +59 -0
  28. data/vendor/longfellow-zk/lib/algebra/fp2.h +240 -0
  29. data/vendor/longfellow-zk/lib/algebra/fp24.h +342 -0
  30. data/vendor/longfellow-zk/lib/algebra/fp24_6.h +305 -0
  31. data/vendor/longfellow-zk/lib/algebra/fp24_6_test.cc +197 -0
  32. data/vendor/longfellow-zk/lib/algebra/fp2_test.cc +280 -0
  33. data/vendor/longfellow-zk/lib/algebra/fp_generic.h +533 -0
  34. data/vendor/longfellow-zk/lib/algebra/fp_p128.h +91 -0
  35. data/vendor/longfellow-zk/lib/algebra/fp_p256.h +68 -0
  36. data/vendor/longfellow-zk/lib/algebra/fp_p256k1.h +123 -0
  37. data/vendor/longfellow-zk/lib/algebra/fp_p384.h +65 -0
  38. data/vendor/longfellow-zk/lib/algebra/fp_p521.h +62 -0
  39. data/vendor/longfellow-zk/lib/algebra/fp_test.cc +522 -0
  40. data/vendor/longfellow-zk/lib/algebra/hash.h +39 -0
  41. data/vendor/longfellow-zk/lib/algebra/interpolation.h +117 -0
  42. data/vendor/longfellow-zk/lib/algebra/interpolation_test.cc +74 -0
  43. data/vendor/longfellow-zk/lib/algebra/limb.h +153 -0
  44. data/vendor/longfellow-zk/lib/algebra/limb_test.cc +75 -0
  45. data/vendor/longfellow-zk/lib/algebra/nat.cc +32 -0
  46. data/vendor/longfellow-zk/lib/algebra/nat.h +212 -0
  47. data/vendor/longfellow-zk/lib/algebra/nat_test.cc +183 -0
  48. data/vendor/longfellow-zk/lib/algebra/nussbaumer.h +400 -0
  49. data/vendor/longfellow-zk/lib/algebra/nussbaumer_test.cc +138 -0
  50. data/vendor/longfellow-zk/lib/algebra/nussbaumerfp2_test.cc +139 -0
  51. data/vendor/longfellow-zk/lib/algebra/permutations.h +79 -0
  52. data/vendor/longfellow-zk/lib/algebra/poly.h +240 -0
  53. data/vendor/longfellow-zk/lib/algebra/poly_test.cc +123 -0
  54. data/vendor/longfellow-zk/lib/algebra/reed_solomon.h +150 -0
  55. data/vendor/longfellow-zk/lib/algebra/reed_solomon_extension.h +108 -0
  56. data/vendor/longfellow-zk/lib/algebra/reed_solomon_extension_test.cc +76 -0
  57. data/vendor/longfellow-zk/lib/algebra/reed_solomon_test.cc +473 -0
  58. data/vendor/longfellow-zk/lib/algebra/rfft.h +400 -0
  59. data/vendor/longfellow-zk/lib/algebra/rfft_test.cc +102 -0
  60. data/vendor/longfellow-zk/lib/algebra/static_string.h +29 -0
  61. data/vendor/longfellow-zk/lib/algebra/sysdep.h +495 -0
  62. data/vendor/longfellow-zk/lib/algebra/sysdep_test.cc +41 -0
  63. data/vendor/longfellow-zk/lib/algebra/twiddle.h +59 -0
  64. data/vendor/longfellow-zk/lib/algebra/utility.h +86 -0
  65. data/vendor/longfellow-zk/lib/algebra/utility_test.cc +86 -0
  66. data/vendor/longfellow-zk/lib/arrays/affine.h +56 -0
  67. data/vendor/longfellow-zk/lib/arrays/affine_test.cc +220 -0
  68. data/vendor/longfellow-zk/lib/arrays/dense.h +210 -0
  69. data/vendor/longfellow-zk/lib/arrays/eq.h +75 -0
  70. data/vendor/longfellow-zk/lib/arrays/eqs.h +137 -0
  71. data/vendor/longfellow-zk/lib/arrays/eqs_test.cc +151 -0
  72. data/vendor/longfellow-zk/lib/arrays/sparse.h +192 -0
  73. data/vendor/longfellow-zk/lib/cbor/host_decoder.h +323 -0
  74. data/vendor/longfellow-zk/lib/cbor/host_decoder_test.cc +541 -0
  75. data/vendor/longfellow-zk/lib/circuits/cbor_parser/cbor.h +594 -0
  76. data/vendor/longfellow-zk/lib/circuits/cbor_parser/cbor_byte_decoder.h +150 -0
  77. data/vendor/longfellow-zk/lib/circuits/cbor_parser/cbor_byte_decoder_test.cc +147 -0
  78. data/vendor/longfellow-zk/lib/circuits/cbor_parser/cbor_constants.h +27 -0
  79. data/vendor/longfellow-zk/lib/circuits/cbor_parser/cbor_pluck.h +110 -0
  80. data/vendor/longfellow-zk/lib/circuits/cbor_parser/cbor_pluck_test.cc +55 -0
  81. data/vendor/longfellow-zk/lib/circuits/cbor_parser/cbor_test.cc +174 -0
  82. data/vendor/longfellow-zk/lib/circuits/cbor_parser/cbor_testing.h +98 -0
  83. data/vendor/longfellow-zk/lib/circuits/cbor_parser/cbor_witness.h +312 -0
  84. data/vendor/longfellow-zk/lib/circuits/cbor_parser/mso2_test.cc +662 -0
  85. data/vendor/longfellow-zk/lib/circuits/cbor_parser/mso_test.cc +485 -0
  86. data/vendor/longfellow-zk/lib/circuits/cbor_parser/scan.h +104 -0
  87. data/vendor/longfellow-zk/lib/circuits/cbor_parser/scan_test.cc +137 -0
  88. data/vendor/longfellow-zk/lib/circuits/cbor_parser_v2/cbor.h +640 -0
  89. data/vendor/longfellow-zk/lib/circuits/cbor_parser_v2/cbor_byte_decoder.h +150 -0
  90. data/vendor/longfellow-zk/lib/circuits/cbor_parser_v2/cbor_byte_decoder_test.cc +147 -0
  91. data/vendor/longfellow-zk/lib/circuits/cbor_parser_v2/cbor_constants.h +27 -0
  92. data/vendor/longfellow-zk/lib/circuits/cbor_parser_v2/cbor_testing.h +99 -0
  93. data/vendor/longfellow-zk/lib/circuits/cbor_parser_v2/cbor_witness.h +319 -0
  94. data/vendor/longfellow-zk/lib/circuits/cbor_parser_v2/lexer_test.cc +120 -0
  95. data/vendor/longfellow-zk/lib/circuits/cbor_parser_v2/mdoc_examples_test.cc +89 -0
  96. data/vendor/longfellow-zk/lib/circuits/cbor_parser_v2/parser_circuit_test.cc +506 -0
  97. data/vendor/longfellow-zk/lib/circuits/cbor_parser_v2/parser_size_test.cc +79 -0
  98. data/vendor/longfellow-zk/lib/circuits/cbor_parser_v2/parser_test.cc +473 -0
  99. data/vendor/longfellow-zk/lib/circuits/compiler/canonicalization_test.cc +185 -0
  100. data/vendor/longfellow-zk/lib/circuits/compiler/circuit_dump.h +65 -0
  101. data/vendor/longfellow-zk/lib/circuits/compiler/compiler.h +471 -0
  102. data/vendor/longfellow-zk/lib/circuits/compiler/compiler_test.cc +110 -0
  103. data/vendor/longfellow-zk/lib/circuits/compiler/node.h +176 -0
  104. data/vendor/longfellow-zk/lib/circuits/compiler/pdqhash.h +127 -0
  105. data/vendor/longfellow-zk/lib/circuits/compiler/schedule.h +435 -0
  106. data/vendor/longfellow-zk/lib/circuits/ecdsa/verify_circuit.h +371 -0
  107. data/vendor/longfellow-zk/lib/circuits/ecdsa/verify_external_test.cc +246 -0
  108. data/vendor/longfellow-zk/lib/circuits/ecdsa/verify_test.cc +587 -0
  109. data/vendor/longfellow-zk/lib/circuits/ecdsa/verify_witness.h +201 -0
  110. data/vendor/longfellow-zk/lib/circuits/logic/bit_adder.h +140 -0
  111. data/vendor/longfellow-zk/lib/circuits/logic/bit_adder_test.cc +64 -0
  112. data/vendor/longfellow-zk/lib/circuits/logic/bit_plucker.h +247 -0
  113. data/vendor/longfellow-zk/lib/circuits/logic/bit_plucker_constants.h +35 -0
  114. data/vendor/longfellow-zk/lib/circuits/logic/bit_plucker_encoder.h +72 -0
  115. data/vendor/longfellow-zk/lib/circuits/logic/bit_plucker_test.cc +183 -0
  116. data/vendor/longfellow-zk/lib/circuits/logic/compiler_backend.h +62 -0
  117. data/vendor/longfellow-zk/lib/circuits/logic/counter.h +171 -0
  118. data/vendor/longfellow-zk/lib/circuits/logic/counter_test.cc +102 -0
  119. data/vendor/longfellow-zk/lib/circuits/logic/evaluation_backend.h +94 -0
  120. data/vendor/longfellow-zk/lib/circuits/logic/logic.h +1232 -0
  121. data/vendor/longfellow-zk/lib/circuits/logic/logic_circuit_test.cc +310 -0
  122. data/vendor/longfellow-zk/lib/circuits/logic/logic_test.cc +521 -0
  123. data/vendor/longfellow-zk/lib/circuits/logic/memcmp.h +68 -0
  124. data/vendor/longfellow-zk/lib/circuits/logic/memcmp_test.cc +148 -0
  125. data/vendor/longfellow-zk/lib/circuits/logic/polynomial.h +94 -0
  126. data/vendor/longfellow-zk/lib/circuits/logic/polynomial_test.cc +62 -0
  127. data/vendor/longfellow-zk/lib/circuits/logic/routing.h +445 -0
  128. data/vendor/longfellow-zk/lib/circuits/logic/routing_test.cc +241 -0
  129. data/vendor/longfellow-zk/lib/circuits/logic/unary.h +55 -0
  130. data/vendor/longfellow-zk/lib/circuits/logic/unary_plucker.h +77 -0
  131. data/vendor/longfellow-zk/lib/circuits/logic/unary_plucker_constants.h +37 -0
  132. data/vendor/longfellow-zk/lib/circuits/logic/unary_plucker_test.cc +53 -0
  133. data/vendor/longfellow-zk/lib/circuits/logic/unary_size_test.cc +69 -0
  134. data/vendor/longfellow-zk/lib/circuits/logic/unary_test.cc +62 -0
  135. data/vendor/longfellow-zk/lib/circuits/mac/mac_circuit.h +193 -0
  136. data/vendor/longfellow-zk/lib/circuits/mac/mac_circuit_test.cc +223 -0
  137. data/vendor/longfellow-zk/lib/circuits/mac/mac_reference.h +72 -0
  138. data/vendor/longfellow-zk/lib/circuits/mac/mac_witness.h +94 -0
  139. data/vendor/longfellow-zk/lib/circuits/mdoc/circuit_maker.cc +242 -0
  140. data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_attribute_ids.h +311 -0
  141. data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_attribute_test.cc +64 -0
  142. data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_circuit_id.cc +85 -0
  143. data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_constants.h +85 -0
  144. data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_decompress.cc +41 -0
  145. data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_decompress.h +27 -0
  146. data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_examples.h +5232 -0
  147. data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_generate_circuit.cc +199 -0
  148. data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_hash.h +554 -0
  149. data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_signature.h +143 -0
  150. data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_signature_test.cc +444 -0
  151. data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_test_attributes.h +157 -0
  152. data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_witness.h +863 -0
  153. data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_zk.cc +693 -0
  154. data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_zk.h +216 -0
  155. data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_zk_test.cc +724 -0
  156. data/vendor/longfellow-zk/lib/circuits/mdoc/zk_spec.cc +100 -0
  157. data/vendor/longfellow-zk/lib/circuits/mdoc/zk_spec_test.cc +155 -0
  158. data/vendor/longfellow-zk/lib/circuits/sha/flatsha256_circuit.h +330 -0
  159. data/vendor/longfellow-zk/lib/circuits/sha/flatsha256_circuit_test.cc +607 -0
  160. data/vendor/longfellow-zk/lib/circuits/sha/flatsha256_io.h +26 -0
  161. data/vendor/longfellow-zk/lib/circuits/sha/flatsha256_witness.cc +163 -0
  162. data/vendor/longfellow-zk/lib/circuits/sha/flatsha256_witness.h +47 -0
  163. data/vendor/longfellow-zk/lib/circuits/sha/sha256_constants.cc +34 -0
  164. data/vendor/longfellow-zk/lib/circuits/sha/sha256_constants.h +27 -0
  165. data/vendor/longfellow-zk/lib/circuits/sha/sha256_test_values.h +389 -0
  166. data/vendor/longfellow-zk/lib/circuits/tests/anoncred/ptrcred.h +171 -0
  167. data/vendor/longfellow-zk/lib/circuits/tests/anoncred/small.h +218 -0
  168. data/vendor/longfellow-zk/lib/circuits/tests/anoncred/small_examples.h +118 -0
  169. data/vendor/longfellow-zk/lib/circuits/tests/anoncred/small_io.h +25 -0
  170. data/vendor/longfellow-zk/lib/circuits/tests/anoncred/small_test.cc +208 -0
  171. data/vendor/longfellow-zk/lib/circuits/tests/anoncred/small_witness.h +130 -0
  172. data/vendor/longfellow-zk/lib/circuits/tests/base64/decode.h +508 -0
  173. data/vendor/longfellow-zk/lib/circuits/tests/base64/decode_circuit_test.cc +95 -0
  174. data/vendor/longfellow-zk/lib/circuits/tests/base64/decode_test.cc +119 -0
  175. data/vendor/longfellow-zk/lib/circuits/tests/base64/decode_util.cc +47 -0
  176. data/vendor/longfellow-zk/lib/circuits/tests/base64/decode_util.h +29 -0
  177. data/vendor/longfellow-zk/lib/circuits/tests/ec/pk_circuit.h +231 -0
  178. data/vendor/longfellow-zk/lib/circuits/tests/ec/pk_circuit_test.cc +428 -0
  179. data/vendor/longfellow-zk/lib/circuits/tests/ec/pk_witness.h +102 -0
  180. data/vendor/longfellow-zk/lib/circuits/tests/jwt/jwt.h +190 -0
  181. data/vendor/longfellow-zk/lib/circuits/tests/jwt/jwt_constants.h +26 -0
  182. data/vendor/longfellow-zk/lib/circuits/tests/jwt/jwt_test.cc +559 -0
  183. data/vendor/longfellow-zk/lib/circuits/tests/jwt/jwt_witness.h +315 -0
  184. data/vendor/longfellow-zk/lib/circuits/tests/mdoc/mdoc_1f.h +411 -0
  185. data/vendor/longfellow-zk/lib/circuits/tests/mdoc/mdoc_1f_io.h +32 -0
  186. data/vendor/longfellow-zk/lib/circuits/tests/mdoc/mdoc_1f_test.cc +364 -0
  187. data/vendor/longfellow-zk/lib/circuits/tests/mdoc/mdoc_1f_witness.h +278 -0
  188. data/vendor/longfellow-zk/lib/circuits/tests/mdoc/mdoc_revocation.h +146 -0
  189. data/vendor/longfellow-zk/lib/circuits/tests/mdoc/mdoc_revocation_constants.h +25 -0
  190. data/vendor/longfellow-zk/lib/circuits/tests/mdoc/mdoc_revocation_test.cc +315 -0
  191. data/vendor/longfellow-zk/lib/circuits/tests/mdoc/mdoc_revocation_witness.h +136 -0
  192. data/vendor/longfellow-zk/lib/circuits/tests/pq/bitaddr/bitaddr.h +250 -0
  193. data/vendor/longfellow-zk/lib/circuits/tests/pq/bitaddr/bitaddr_test.cc +333 -0
  194. data/vendor/longfellow-zk/lib/circuits/tests/pq/bitaddr/bitaddr_witness.h +152 -0
  195. data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/ml_dsa_44.h +903 -0
  196. data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/ml_dsa_44_circuit_test.cc +274 -0
  197. data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/ml_dsa_44_eval_test.cc +440 -0
  198. data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/ml_dsa_44_examples.cc +8851 -0
  199. data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/ml_dsa_44_examples.h +93 -0
  200. data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/ml_dsa_44_types.cc +24 -0
  201. data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/ml_dsa_44_types.h +118 -0
  202. data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/ml_dsa_44_witness.h +453 -0
  203. data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/ml_dsa_44_witness_test.cc +49 -0
  204. data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/ml_dsa_ref.cc +458 -0
  205. data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/ml_dsa_ref.h +150 -0
  206. data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/ml_dsa_ref_test.cc +398 -0
  207. data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/ml_dsa_ref_test_vectors.inc +3618 -0
  208. data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/ml_dsa_ref_test_vectors_pkdecode.inc +689 -0
  209. data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/ml_dsa_ref_test_vectors_sigdecode.inc +1501 -0
  210. data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/sigdecode_test_vectors.inc +540 -0
  211. data/vendor/longfellow-zk/lib/circuits/tests/ripemd/ripemd_circuit.h +394 -0
  212. data/vendor/longfellow-zk/lib/circuits/tests/ripemd/ripemd_circuit_test.cc +577 -0
  213. data/vendor/longfellow-zk/lib/circuits/tests/ripemd/ripemd_constants.h +90 -0
  214. data/vendor/longfellow-zk/lib/circuits/tests/ripemd/ripemd_witness.cc +174 -0
  215. data/vendor/longfellow-zk/lib/circuits/tests/ripemd/ripemd_witness.h +140 -0
  216. data/vendor/longfellow-zk/lib/circuits/tests/sha3/sha3_circuit.h +351 -0
  217. data/vendor/longfellow-zk/lib/circuits/tests/sha3/sha3_circuit_test.cc +466 -0
  218. data/vendor/longfellow-zk/lib/circuits/tests/sha3/sha3_reference.cc +207 -0
  219. data/vendor/longfellow-zk/lib/circuits/tests/sha3/sha3_reference.h +59 -0
  220. data/vendor/longfellow-zk/lib/circuits/tests/sha3/sha3_reference_test.cc +153 -0
  221. data/vendor/longfellow-zk/lib/circuits/tests/sha3/sha3_round_constants.cc +39 -0
  222. data/vendor/longfellow-zk/lib/circuits/tests/sha3/sha3_round_constants.h +29 -0
  223. data/vendor/longfellow-zk/lib/circuits/tests/sha3/sha3_slicing.h +31 -0
  224. data/vendor/longfellow-zk/lib/circuits/tests/sha3/sha3_witness.cc +83 -0
  225. data/vendor/longfellow-zk/lib/circuits/tests/sha3/sha3_witness.h +72 -0
  226. data/vendor/longfellow-zk/lib/circuits/tests/sha3/shake_test_vectors.h +477 -0
  227. data/vendor/longfellow-zk/lib/ec/elliptic_curve.h +596 -0
  228. data/vendor/longfellow-zk/lib/ec/elliptic_curve_test.cc +548 -0
  229. data/vendor/longfellow-zk/lib/ec/p256.cc +36 -0
  230. data/vendor/longfellow-zk/lib/ec/p256.h +60 -0
  231. data/vendor/longfellow-zk/lib/ec/p256k1.cc +34 -0
  232. data/vendor/longfellow-zk/lib/ec/p256k1.h +60 -0
  233. data/vendor/longfellow-zk/lib/gf2k/gf2_128.h +503 -0
  234. data/vendor/longfellow-zk/lib/gf2k/gf2_128_bench.cc +48 -0
  235. data/vendor/longfellow-zk/lib/gf2k/gf2_128_test.cc +416 -0
  236. data/vendor/longfellow-zk/lib/gf2k/gf2poly.h +74 -0
  237. data/vendor/longfellow-zk/lib/gf2k/lch14.h +242 -0
  238. data/vendor/longfellow-zk/lib/gf2k/lch14_bench.cc +75 -0
  239. data/vendor/longfellow-zk/lib/gf2k/lch14_reed_solomon.h +127 -0
  240. data/vendor/longfellow-zk/lib/gf2k/lch14_reed_solomon_test.cc +110 -0
  241. data/vendor/longfellow-zk/lib/gf2k/lch14_test.cc +246 -0
  242. data/vendor/longfellow-zk/lib/gf2k/sysdep.h +329 -0
  243. data/vendor/longfellow-zk/lib/ligero/ligero_param.h +449 -0
  244. data/vendor/longfellow-zk/lib/ligero/ligero_prover.h +354 -0
  245. data/vendor/longfellow-zk/lib/ligero/ligero_test.cc +136 -0
  246. data/vendor/longfellow-zk/lib/ligero/ligero_transcript.h +67 -0
  247. data/vendor/longfellow-zk/lib/ligero/ligero_verifier.h +272 -0
  248. data/vendor/longfellow-zk/lib/merkle/merkle_commitment.h +104 -0
  249. data/vendor/longfellow-zk/lib/merkle/merkle_tree.h +216 -0
  250. data/vendor/longfellow-zk/lib/merkle/merkle_tree_test.cc +240 -0
  251. data/vendor/longfellow-zk/lib/proto/circuit.h +354 -0
  252. data/vendor/longfellow-zk/lib/proto/circuit_test.cc +202 -0
  253. data/vendor/longfellow-zk/lib/random/random.h +119 -0
  254. data/vendor/longfellow-zk/lib/random/random_test.cc +189 -0
  255. data/vendor/longfellow-zk/lib/random/secure_random_engine.h +37 -0
  256. data/vendor/longfellow-zk/lib/random/transcript.h +193 -0
  257. data/vendor/longfellow-zk/lib/random/transcript_test.cc +344 -0
  258. data/vendor/longfellow-zk/lib/sumcheck/circuit.h +148 -0
  259. data/vendor/longfellow-zk/lib/sumcheck/circuit_id.h +71 -0
  260. data/vendor/longfellow-zk/lib/sumcheck/equad.h +126 -0
  261. data/vendor/longfellow-zk/lib/sumcheck/hquad.h +115 -0
  262. data/vendor/longfellow-zk/lib/sumcheck/prover.h +59 -0
  263. data/vendor/longfellow-zk/lib/sumcheck/prover_layers.h +362 -0
  264. data/vendor/longfellow-zk/lib/sumcheck/quad.h +227 -0
  265. data/vendor/longfellow-zk/lib/sumcheck/quad_builder.h +211 -0
  266. data/vendor/longfellow-zk/lib/sumcheck/quad_test.cc +169 -0
  267. data/vendor/longfellow-zk/lib/sumcheck/sumcheck_test.cc +324 -0
  268. data/vendor/longfellow-zk/lib/sumcheck/testing.h +69 -0
  269. data/vendor/longfellow-zk/lib/sumcheck/transcript_sumcheck.h +85 -0
  270. data/vendor/longfellow-zk/lib/sumcheck/verifier.h +84 -0
  271. data/vendor/longfellow-zk/lib/sumcheck/verifier_layers.h +221 -0
  272. data/vendor/longfellow-zk/lib/testing/test_main.cc +50 -0
  273. data/vendor/longfellow-zk/lib/util/ceildiv.h +164 -0
  274. data/vendor/longfellow-zk/lib/util/ceildiv_test.cc +152 -0
  275. data/vendor/longfellow-zk/lib/util/crc64.h +45 -0
  276. data/vendor/longfellow-zk/lib/util/crypto.cc +39 -0
  277. data/vendor/longfellow-zk/lib/util/crypto.h +108 -0
  278. data/vendor/longfellow-zk/lib/util/log.cc +110 -0
  279. data/vendor/longfellow-zk/lib/util/log.h +33 -0
  280. data/vendor/longfellow-zk/lib/util/panic.h +40 -0
  281. data/vendor/longfellow-zk/lib/util/readbuffer.h +67 -0
  282. data/vendor/longfellow-zk/lib/util/serialization.h +54 -0
  283. data/vendor/longfellow-zk/lib/zk/zk_common.h +455 -0
  284. data/vendor/longfellow-zk/lib/zk/zk_proof.h +378 -0
  285. data/vendor/longfellow-zk/lib/zk/zk_prover.h +202 -0
  286. data/vendor/longfellow-zk/lib/zk/zk_test.cc +340 -0
  287. data/vendor/longfellow-zk/lib/zk/zk_testing.h +154 -0
  288. data/vendor/longfellow-zk/lib/zk/zk_verifier.h +109 -0
  289. metadata +347 -0
@@ -0,0 +1,93 @@
1
+ // Copyright 2026 Google LLC.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ #ifndef PRIVACY_PROOFS_ZK_LIB_CIRCUITS_TESTS_PQ_ML_DSA_ML_DSA_44_EXAMPLES_H_
16
+ #define PRIVACY_PROOFS_ZK_LIB_CIRCUITS_TESTS_PQ_ML_DSA_ML_DSA_44_EXAMPLES_H_
17
+
18
+ #include <cstdint>
19
+ #include <vector>
20
+
21
+ namespace proofs {
22
+ namespace ml_dsa {
23
+
24
+ // This file defines test vectors for various steps of the ML-DSA-44
25
+ // verification algorithm. Because these objects are cumbersome, many
26
+ // are defined in .inc files and included in the corresponding .cc file.
27
+ // This header file defines the structures used to hold these test vectors.
28
+
29
+ struct MlDsa44SignatureExample {
30
+ std::vector<uint8_t> msg;
31
+ std::vector<uint8_t> pkey;
32
+ std::vector<uint8_t> ctx;
33
+ std::vector<uint8_t> mu;
34
+ std::vector<uint8_t> sig;
35
+ };
36
+
37
+ std::vector<MlDsa44SignatureExample> GetMlDsa44Examples();
38
+
39
+ std::vector<MlDsa44SignatureExample> GetMlDsa44FailExamples();
40
+
41
+ struct UseHintTestCase {
42
+ bool h;
43
+ int32_t r;
44
+ uint32_t expected;
45
+ };
46
+
47
+ std::vector<UseHintTestCase> GetUseHintTestCases();
48
+
49
+ extern const uint64_t kExpectedExpandAVectors[4][4][256];
50
+
51
+ struct MlDsa44ByteInputOutput {
52
+ std::vector<uint8_t> in;
53
+ std::vector<uint32_t> out;
54
+ };
55
+
56
+ std::vector<MlDsa44ByteInputOutput> GetSampleInBallTests();
57
+
58
+ struct MlDsa44PkDecodeTest {
59
+ std::vector<uint8_t> in;
60
+ uint8_t rho[32];
61
+ uint64_t t1[4][256];
62
+ uint8_t tr[64];
63
+ };
64
+
65
+ std::vector<MlDsa44PkDecodeTest> GetPkDecodeTests();
66
+
67
+ struct MlDsa44SigDecodeTest {
68
+ std::vector<uint8_t> in;
69
+ uint8_t c_tilde[32];
70
+ uint64_t z[4][256];
71
+ bool h[4][256];
72
+ };
73
+
74
+ std::vector<MlDsa44SigDecodeTest> GetSigDecodeTests();
75
+
76
+ struct MlDsa44W1EncodeTests {
77
+ int32_t in[4][256];
78
+ std::vector<uint8_t> out;
79
+ };
80
+
81
+ std::vector<MlDsa44W1EncodeTests> GetW1EncodeTests();
82
+
83
+ struct MlDsa44NTTTest {
84
+ std::vector<uint32_t> in;
85
+ std::vector<uint32_t> out;
86
+ };
87
+
88
+ std::vector<MlDsa44NTTTest> GetNTTTests();
89
+
90
+ } // namespace ml_dsa
91
+ } // namespace proofs
92
+
93
+ #endif // PRIVACY_PROOFS_ZK_LIB_CIRCUITS_TESTS_PQ_ML_DSA_ML_DSA_44_EXAMPLES_H_
@@ -0,0 +1,24 @@
1
+ // Copyright 2026 Google LLC.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ #include "circuits/tests/pq/ml_dsa/ml_dsa_44_types.h"
16
+
17
+ #include "algebra/fp24.h"
18
+ namespace proofs {
19
+ namespace ml_dsa {
20
+
21
+ const Fp24 Fq = Fp24(Q);
22
+
23
+ }
24
+ } // namespace proofs
@@ -0,0 +1,118 @@
1
+ // Copyright 2026 Google LLC.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ #ifndef PRIVACY_PROOFS_ZK_LIB_CIRCUITS_TESTS_PQ_ML_DSA_ML_DSA_44_TYPES_H_
16
+ #define PRIVACY_PROOFS_ZK_LIB_CIRCUITS_TESTS_PQ_ML_DSA_ML_DSA_44_TYPES_H_
17
+
18
+ #include <array>
19
+ #include <cstddef>
20
+ #include <cstdint>
21
+
22
+ #include "algebra/fp24.h"
23
+ namespace proofs {
24
+ namespace ml_dsa {
25
+
26
+ // ----------------------------------------------------------------------------
27
+ //
28
+ // !!!!! DO NOT USE IN PRODUCTION !!!!!
29
+ //
30
+ // This ML-DSA circuit is an experimental implementation for research purposes.
31
+ // It has not been fully vetted and is not recommended for production use cases
32
+ // at this time.
33
+ //
34
+ // ML-DSA is specified in
35
+ //
36
+ // FIPS 204
37
+ // Federal Information Processing Standards Publication
38
+ // Module-Lattice-Based Digital
39
+ // Signature Standard
40
+ //
41
+ // https://nvlpubs.nist.gov/nistPubs/fips/nist.fips.204.pdf
42
+ //
43
+ // ----------------------------------------------------------------------------
44
+
45
+ // q: 2^23 - 2^13 + 1 = 8380417.
46
+ static constexpr uint32_t Q = 8380417;
47
+ using Elt = Fp24::Elt;
48
+ static constexpr size_t N = 256;
49
+ extern const Fp24 Fq;
50
+
51
+ // The ML-DSA 44 algorithm is specified in
52
+ // https://nvlpubs.nist.gov/nistPubs/fips/nist.fips.204.pdf.
53
+ // For "44", the parameters from Section 4, page 15 are:
54
+ static constexpr uint64_t ZETA = 1753; // a 512-th root of unity in F_q
55
+ static constexpr uint64_t D = 13; // number of bits dropped from t
56
+ static constexpr uint64_t TAU = 39; // number of ±1 in c
57
+ static constexpr uint64_t GAMMA_1 = 131072; // coefficient range of y: 2^17
58
+ static constexpr uint64_t GAMMA_2 = 95232; // low order rounding: (q-1)/88
59
+ static constexpr uint64_t K = 4; // Dimensions of A = k x l
60
+ static constexpr uint64_t L = 4; // Dimensions of A = k x l
61
+ static constexpr uint64_t ETA = 2; // Private key range
62
+ static constexpr uint64_t BETA = 78; // \tau * \eta
63
+ static constexpr uint64_t OMEGA = 80; // Max number of ones in hint
64
+ static constexpr uint64_t C_TILDE_BYTES = 32;
65
+
66
+ // Derived parameters
67
+ static constexpr size_t PK_SIZE = 32 + 32 * K * 10;
68
+
69
+ // Define ring R_q = R_q[x]/(x^256 + 1).
70
+ using Rq = std::array<Elt, N>;
71
+ using RqK = std::array<Rq, K>;
72
+ using RqL = std::array<Rq, L>;
73
+ using MatrixA = std::array<RqL, K>;
74
+
75
+ static constexpr uint64_t kZetas[256] = {
76
+ 1u, 4808194u, 3765607u, 3761513u, 5178923u, 5496691u, 5234739u,
77
+ 5178987u, 7778734u, 3542485u, 2682288u, 2129892u, 3764867u, 7375178u,
78
+ 557458u, 7159240u, 5010068u, 4317364u, 2663378u, 6705802u, 4855975u,
79
+ 7946292u, 676590u, 7044481u, 5152541u, 1714295u, 2453983u, 1460718u,
80
+ 7737789u, 4795319u, 2815639u, 2283733u, 3602218u, 3182878u, 2740543u,
81
+ 4793971u, 5269599u, 2101410u, 3704823u, 1159875u, 394148u, 928749u,
82
+ 1095468u, 4874037u, 2071829u, 4361428u, 3241972u, 2156050u, 3415069u,
83
+ 1759347u, 7562881u, 4805951u, 3756790u, 6444618u, 6663429u, 4430364u,
84
+ 5483103u, 3192354u, 556856u, 3870317u, 2917338u, 1853806u, 3345963u,
85
+ 1858416u, 3073009u, 1277625u, 5744944u, 3852015u, 4183372u, 5157610u,
86
+ 5258977u, 8106357u, 2508980u, 2028118u, 1937570u, 4564692u, 2811291u,
87
+ 5396636u, 7270901u, 4158088u, 1528066u, 482649u, 1148858u, 5418153u,
88
+ 7814814u, 169688u, 2462444u, 5046034u, 4213992u, 4892034u, 1987814u,
89
+ 5183169u, 1736313u, 235407u, 5130263u, 3258457u, 5801164u, 1787943u,
90
+ 5989328u, 6125690u, 3482206u, 4197502u, 7080401u, 6018354u, 7062739u,
91
+ 2461387u, 3035980u, 621164u, 3901472u, 7153756u, 2925816u, 3374250u,
92
+ 1356448u, 5604662u, 2683270u, 5601629u, 4912752u, 2312838u, 7727142u,
93
+ 7921254u, 348812u, 8052569u, 1011223u, 6026202u, 4561790u, 6458164u,
94
+ 6143691u, 1744507u, 1753u, 6444997u, 5720892u, 6924527u, 2660408u,
95
+ 6600190u, 8321269u, 2772600u, 1182243u, 87208u, 636927u, 4415111u,
96
+ 4423672u, 6084020u, 5095502u, 4663471u, 8352605u, 822541u, 1009365u,
97
+ 5926272u, 6400920u, 1596822u, 4423473u, 4620952u, 6695264u, 4969849u,
98
+ 2678278u, 4611469u, 4829411u, 635956u, 8129971u, 5925040u, 4234153u,
99
+ 6607829u, 2192938u, 6653329u, 2387513u, 4768667u, 8111961u, 5199961u,
100
+ 3747250u, 2296099u, 1239911u, 4541938u, 3195676u, 2642980u, 1254190u,
101
+ 8368000u, 2998219u, 141835u, 8291116u, 2513018u, 7025525u, 613238u,
102
+ 7070156u, 6161950u, 7921677u, 6458423u, 4040196u, 4908348u, 2039144u,
103
+ 6500539u, 7561656u, 6201452u, 6757063u, 2105286u, 6006015u, 6346610u,
104
+ 586241u, 7200804u, 527981u, 5637006u, 6903432u, 1994046u, 2491325u,
105
+ 6987258u, 507927u, 7192532u, 7655613u, 6545891u, 5346675u, 8041997u,
106
+ 2647994u, 3009748u, 5767564u, 4148469u, 749577u, 4357667u, 3980599u,
107
+ 2569011u, 6764887u, 1723229u, 1665318u, 2028038u, 1163598u, 5011144u,
108
+ 3994671u, 8368538u, 7009900u, 3020393u, 3363542u, 214880u, 545376u,
109
+ 7609976u, 3105558u, 7277073u, 508145u, 7826699u, 860144u, 3430436u,
110
+ 140244u, 6866265u, 6195333u, 3123762u, 2358373u, 6187330u, 5365997u,
111
+ 6663603u, 2926054u, 7987710u, 8077412u, 3531229u, 4405932u, 4606686u,
112
+ 1900052u, 7598542u, 1054478u, 7648983u,
113
+ };
114
+
115
+ } // namespace ml_dsa
116
+ } // namespace proofs
117
+
118
+ #endif // PRIVACY_PROOFS_ZK_LIB_CIRCUITS_TESTS_PQ_ML_DSA_ML_DSA_44_TYPES_H_
@@ -0,0 +1,453 @@
1
+ // Copyright 2026 Google LLC.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ #ifndef PRIVACY_PROOFS_ZK_LIB_CIRCUITS_TESTS_PQ_ML_DSA_ML_DSA_44_WITNESS_H_
16
+ #define PRIVACY_PROOFS_ZK_LIB_CIRCUITS_TESTS_PQ_ML_DSA_ML_DSA_44_WITNESS_H_
17
+
18
+ #include <algorithm>
19
+ #include <array>
20
+ #include <cstddef>
21
+ #include <cstdint>
22
+ #include <vector>
23
+
24
+ #include "algebra/fp24.h"
25
+ #include "arrays/dense.h"
26
+ #include "circuits/tests/pq/ml_dsa/ml_dsa_44_types.h"
27
+ #include "circuits/tests/pq/ml_dsa/ml_dsa_ref.h"
28
+ #include "circuits/tests/sha3/sha3_witness.h"
29
+
30
+ namespace proofs {
31
+
32
+ class ml_dsa_44_witness {
33
+ public:
34
+ std::array<uint8_t, 64> tr_;
35
+ std::array<uint8_t, ml_dsa::C_TILDE_BYTES> c_tilde_;
36
+ std::array<uint8_t, 64> mu_;
37
+ std::array<uint8_t, ml_dsa::K * 192> w1_tilde_;
38
+ std::vector<Sha3Witness::BlockWitness> mu_bws_;
39
+ std::array<uint8_t, ml_dsa::C_TILDE_BYTES> c_prime_tilde_;
40
+ std::vector<Sha3Witness::BlockWitness> c_prime_tilde_bws_;
41
+ std::array<std::array<uint64_t, ml_dsa::N>, ml_dsa::L> z_bits_;
42
+
43
+ Sha3Witness::BlockWitness shake_bws_;
44
+
45
+ std::array<uint8_t, ml_dsa::TAU> j_vals_;
46
+ std::array<uint16_t, ml_dsa::TAU> j_k_indices_;
47
+ std::array<std::array<uint8_t, ml_dsa::TAU>, ml_dsa::TAU> position_trace_{};
48
+ ml_dsa::Rq c_coeffs_; // Polynomial c in domain
49
+
50
+ static int64_t SymmetricReduce(int64_t delta) {
51
+ delta = delta % static_cast<int64_t>(ml_dsa::Q);
52
+ if (delta > static_cast<int64_t>(ml_dsa::Q) / 2) {
53
+ delta -= ml_dsa::Q;
54
+ }
55
+ return delta;
56
+ }
57
+
58
+ // Derived NTT values
59
+ ml_dsa::RqL nttz_;
60
+ ml_dsa::Rq nttc_; // Single poly
61
+ ml_dsa::RqK nttt1_;
62
+ ml_dsa::RqK w_prime_approx_;
63
+ std::array<std::array<int32_t, ml_dsa::N>, ml_dsa::K> w1_;
64
+ std::array<std::array<uint64_t, ml_dsa::N>, ml_dsa::K> hint_aux_bits_;
65
+ std::array<std::array<int32_t, ml_dsa::N>, ml_dsa::K> w_prime_1_;
66
+ std::array<std::array<uint64_t, ml_dsa::N>, ml_dsa::K> w_prime_1_bits_;
67
+
68
+ // Public inputs or derived values
69
+ std::array<uint8_t, 32> rho_;
70
+ ml_dsa::RqK t1_;
71
+
72
+ const Fp24& f_ = ml_dsa::Fq;
73
+
74
+ ml_dsa::PublicKey ref_pk_;
75
+ ml_dsa::Signature ref_sig_;
76
+ std::vector<uint8_t> msg_;
77
+
78
+ ml_dsa::Rq eval_ntt(const ml_dsa::Rq& p_in) const {
79
+ ml_dsa::Rq p = p_in;
80
+ int k = 1;
81
+ int length = ml_dsa::N / 2;
82
+ while (length > 0) {
83
+ for (int start = 0; start < ml_dsa::N; start += 2 * length) {
84
+ auto zeta = f_.of_scalar(ml_dsa::kZetas[k]);
85
+ k++;
86
+ for (int j = start; j < start + length; ++j) {
87
+ auto t = zeta;
88
+ f_.mul(t, p[j + length]); // t = zeta * p[j+len]
89
+ p[j + length] = p[j];
90
+ f_.sub(p[j + length], t); // p[j+len] = p[j] - t
91
+ f_.add(p[j], t); // p[j] = p[j] + t
92
+ }
93
+ }
94
+ length /= 2;
95
+ }
96
+ return p;
97
+ }
98
+
99
+ ml_dsa::Rq eval_inverse_ntt(const ml_dsa::Rq& p_in) const {
100
+ ml_dsa::Rq p = p_in;
101
+ int k = 256;
102
+ int length = 1;
103
+ while (length < ml_dsa::N) {
104
+ for (int start = 0; start < ml_dsa::N; start += 2 * length) {
105
+ k--;
106
+ auto neg_zeta = f_.of_scalar(ml_dsa::kZetas[k]);
107
+ f_.neg(neg_zeta); // Revert to negative zeta
108
+ for (int j = start; j < start + length; ++j) {
109
+ auto t = p[j];
110
+ f_.add(p[j], p[j + length]); // p[j] = p[j] + p[j+len]
111
+ f_.sub(t,
112
+ p[j + length]); // t = p[j] - p[j+len] (using original p[j])
113
+ f_.mul(t, neg_zeta);
114
+ p[j + length] = t;
115
+ }
116
+ }
117
+ length *= 2;
118
+ }
119
+ auto inv_256 = f_.of_scalar_field(8347681u); // 256^-1 mod q
120
+ for (size_t i = 0; i < ml_dsa::N; ++i) {
121
+ f_.mul(p[i], inv_256);
122
+ }
123
+ return p;
124
+ }
125
+
126
+ template <typename Field>
127
+ void fill_pk(DenseFiller<Field>& filler, const Field& f) const {
128
+ // 1. Pk
129
+ for (size_t i = 0; i < ml_dsa::K; ++i) {
130
+ for (size_t j = 0; j < ml_dsa::L; ++j) {
131
+ for (size_t k = 0; k < ml_dsa::N; ++k) {
132
+ filler.push_back(f.of_scalar(ref_pk_.a_hat[i][j][k]));
133
+ }
134
+ }
135
+ }
136
+ for (size_t i = 0; i < ml_dsa::K; ++i) {
137
+ for (size_t k = 0; k < ml_dsa::N; ++k) {
138
+ filler.push_back(f.of_scalar(nttt1_[i][k]));
139
+ }
140
+ }
141
+ for (size_t i = 0; i < 64; ++i) {
142
+ filler.push_back(tr_[i], 8, f);
143
+ }
144
+ }
145
+
146
+ template <typename Field>
147
+ void fill_witness(DenseFiller<Field>& filler, const Field& f) const {
148
+ fill_pk(filler, f);
149
+
150
+ // 2. Sig
151
+ for (size_t i = 0; i < 32; ++i) {
152
+ filler.push_back(c_tilde_[i], 8, f);
153
+ }
154
+ for (size_t i = 0; i < ml_dsa::L; ++i) {
155
+ for (size_t k = 0; k < ml_dsa::N; ++k) {
156
+ filler.push_back(f.of_scalar(ref_sig_.z[i][k]));
157
+ }
158
+ }
159
+ for (size_t i = 0; i < ml_dsa::L; ++i) {
160
+ for (size_t j = 0; j < ml_dsa::N; ++j) {
161
+ filler.push_back(z_bits_[i][j], 19, f);
162
+ }
163
+ }
164
+ for (size_t i = 0; i < ml_dsa::K; ++i) {
165
+ for (size_t k = 0; k < ml_dsa::N; ++k) {
166
+ filler.push_back(ref_sig_.h[i][k] ? f.one() : f.zero());
167
+ }
168
+ }
169
+
170
+ // 3. Witness
171
+ for (size_t i = 0; i < ml_dsa::TAU; ++i) {
172
+ filler.push_back(j_vals_[i], 8, f);
173
+ filler.push_back(j_k_indices_[i], 16, f);
174
+ }
175
+
176
+ Sha3Witness::fill_witness(filler, shake_bws_, f);
177
+
178
+ for (size_t s = 0; s < ml_dsa::TAU; ++s) {
179
+ for (size_t k = 0; k <= s; ++k) {
180
+ filler.push_back(position_trace_[s][k], 8, f);
181
+ }
182
+ }
183
+
184
+ for (size_t k = 0; k < ml_dsa::N; ++k)
185
+ filler.push_back(f.of_scalar(c_coeffs_[k]));
186
+
187
+ for (size_t i = 0; i < ml_dsa::K; ++i) {
188
+ for (size_t k = 0; k < ml_dsa::N; ++k)
189
+ filler.push_back(f.of_scalar(w_prime_approx_[i][k]));
190
+ for (size_t k = 0; k < ml_dsa::N; ++k) {
191
+ int32_t val = w1_[i][k];
192
+ if (val < 0) val += ml_dsa::Q;
193
+ filler.push_back(f.of_scalar(val));
194
+ }
195
+ for (size_t j = 0; j < ml_dsa::N; ++j)
196
+ filler.push_back(hint_aux_bits_[i][j], 19, f);
197
+
198
+ for (size_t k = 0; k < ml_dsa::N; ++k)
199
+ filler.push_back(f.of_scalar(w_prime_1_[i][k]));
200
+ for (size_t j = 0; j < ml_dsa::N; ++j)
201
+ filler.push_back(w_prime_1_bits_[i][j], 6, f);
202
+ }
203
+
204
+ for (size_t i = 0; i < ml_dsa::L; ++i) {
205
+ for (size_t k = 0; k < ml_dsa::N; ++k)
206
+ filler.push_back(f.of_scalar(nttz_[i][k]));
207
+ }
208
+ for (size_t k = 0; k < ml_dsa::N; ++k)
209
+ filler.push_back(f.of_scalar(nttc_[k]));
210
+
211
+ for (size_t i = 0; i < w1_tilde_.size(); ++i)
212
+ filler.push_back(w1_tilde_[i], 8, f);
213
+
214
+ for (size_t i = 0; i < 7; ++i) {
215
+ Sha3Witness::fill_witness(filler, c_prime_tilde_bws_[i], f);
216
+ }
217
+ }
218
+
219
+ bool compute_witness(const std::vector<uint8_t>& pk,
220
+ const std::vector<uint8_t>& sig,
221
+ const std::vector<uint8_t>& msg,
222
+ const std::vector<uint8_t>& ctx) {
223
+ // 1. Decode Pk
224
+ auto ref_pk = ml_dsa::pkDecode(pk);
225
+ ref_pk_ = ref_pk;
226
+ std::copy(pk.begin(), pk.begin() + 32, rho_.begin());
227
+ std::copy(ref_pk.tr.begin(), ref_pk.tr.end(), tr_.begin());
228
+
229
+ for (size_t i = 0; i < ml_dsa::K; ++i) {
230
+ t1_[i] = ref_pk.t1[i]; // Copy Rq directly
231
+ }
232
+
233
+ // 2. Decode Sig
234
+ auto maybe_ref_sig = ml_dsa::sigDecode(sig);
235
+ if (!maybe_ref_sig.has_value()) return false;
236
+ auto ref_sig = maybe_ref_sig.value();
237
+ std::copy(ref_sig.c_tilde.begin(), ref_sig.c_tilde.end(), c_tilde_.begin());
238
+ ref_sig_ = ref_sig;
239
+ msg_ = msg;
240
+
241
+ // Z and H handling
242
+
243
+ for (size_t i = 0; i < ml_dsa::L; ++i) {
244
+ ml_dsa::Rq z_poly = ref_sig.z[i];
245
+
246
+ for (size_t j = 0; j < ml_dsa::N; ++j) {
247
+ // Shift z to be positive in [0, 2*(GAMMA1 - BETA)] range for
248
+ // interaction with assert_infty_norm. We want to check ||z||_oo <
249
+ // GAMMA1 - BETA. The check in the circuit expects the witness bits to
250
+ // represent z + (GAMMA1 - BETA). Since z is in [-(Q-1)/2, (Q-1)/2]
251
+ // (conceptually) but stored as [0, Q), we first normalize it to signed
252
+ // int32, then shift.
253
+
254
+ int32_t val =
255
+ static_cast<int32_t>(f_.from_montgomery(z_poly[j]).limb_[0]);
256
+ if (val > (ml_dsa::Q / 2)) {
257
+ val -= ml_dsa::Q;
258
+ }
259
+
260
+ // val is now in range roughly [-GAMMA1, GAMMA1] if valid.
261
+ // We compute shifted = val + (GAMMA1 - BETA).
262
+ // The bound is GAMMA1 - BETA.
263
+ // If |val| < bound, then -bound < val < bound
264
+ // => 0 < val + bound < 2*bound.
265
+ // So shifted value should be in [0, 2*bound].
266
+ // Actually [1, 2*bound-1] strictly if we want strict <.
267
+ // The circuit uses 19 bits.
268
+ int32_t bound = ml_dsa::GAMMA_1 - ml_dsa::BETA;
269
+ int32_t shifted = val + bound;
270
+
271
+ // We store it as 64-bit primarily to match the vector type, but it fits
272
+ // in 19 bits.
273
+ z_bits_[i][j] = static_cast<uint64_t>(shifted);
274
+ }
275
+ nttz_[i] = eval_ntt(z_poly);
276
+ }
277
+
278
+ // 3. SampleInBall logic
279
+ std::vector<uint8_t> shake_input(c_tilde_.begin(),
280
+ c_tilde_.end()); // 32 bytes
281
+ c_coeffs_ = ml_dsa::SampleInBall(c_tilde_);
282
+
283
+ // Flattened c for NTT
284
+ nttc_ = eval_ntt(c_coeffs_);
285
+
286
+ // witness logic for SampleInBall (SHAKE blocks)
287
+ std::vector<Sha3Witness::BlockWitness> temp_bws;
288
+ Sha3Witness::compute_witness_shake256(shake_input, 136, temp_bws);
289
+ shake_bws_ = temp_bws[0];
290
+
291
+ // Manual rejecting sampling witness
292
+ std::array<uint8_t, 136> hash_out;
293
+ ml_dsa::H(shake_input, hash_out);
294
+
295
+ int count = 0;
296
+ size_t out_idx = 8;
297
+ for (int i = 256 - ml_dsa::TAU; i < 256; ++i) {
298
+ uint8_t j;
299
+ do {
300
+ j = hash_out[out_idx++];
301
+ } while (j > i);
302
+ j_vals_[count] = j;
303
+ j_k_indices_[count] = out_idx - 1;
304
+ count++;
305
+ }
306
+
307
+ // Compute position trace
308
+ std::vector<uint8_t> current_pos;
309
+ current_pos.reserve(ml_dsa::TAU);
310
+
311
+ for (size_t s = 0; s < ml_dsa::TAU; ++s) {
312
+ uint8_t j = j_vals_[s];
313
+ uint8_t i = 256 - ml_dsa::TAU + s;
314
+
315
+ // If j is occupied, move it to i
316
+ for (auto& p : current_pos) {
317
+ if (p == j) {
318
+ p = i;
319
+ break; // Should only be one match if logic is correct
320
+ }
321
+ }
322
+ // New coefficient is at j
323
+ current_pos.push_back(j);
324
+ std::copy(current_pos.begin(), current_pos.end(),
325
+ position_trace_[s].begin());
326
+ }
327
+
328
+ // nttt1
329
+ auto& f_ = ml_dsa::Fq;
330
+ auto scale_factor = f_.of_scalar(1 << 13);
331
+ for (size_t i = 0; i < ml_dsa::K; ++i) {
332
+ ml_dsa::Rq t1_scaled = ref_pk.t1[i];
333
+ for (size_t j = 0; j < ml_dsa::N; ++j) {
334
+ f_.mul(t1_scaled[j], scale_factor); // in-place
335
+ }
336
+ nttt1_[i] = eval_ntt(t1_scaled);
337
+ }
338
+
339
+ // w_prime_approx
340
+ for (size_t i = 0; i < ml_dsa::K; ++i) {
341
+ ml_dsa::Rq diff;
342
+ for (size_t k = 0; k < ml_dsa::N; ++k) {
343
+ auto az = f_.zero();
344
+ // Az = sum(A[i][j] * z[j])
345
+ for (size_t j = 0; j < ml_dsa::L; ++j) {
346
+ auto term = ref_pk.a_hat[i][j][k];
347
+ f_.mul(term, nttz_[j][k]); // in-place term *= nttz
348
+ f_.add(az, term); // in-place az += term
349
+ }
350
+ auto ct1 = nttc_[k];
351
+ f_.mul(ct1, nttt1_[i][k]); // in-place ct1 *= nttt1
352
+
353
+ diff[k] = az;
354
+ f_.sub(diff[k], ct1); // in-place diff[k] -= ct1
355
+ }
356
+
357
+ w_prime_approx_[i] = eval_inverse_ntt(diff);
358
+ }
359
+
360
+ // 6. Compute Decompose and UseHint witnesses
361
+
362
+ // Using string conversion to handle field elements safely
363
+ for (size_t i = 0; i < ml_dsa::K; ++i) {
364
+ for (size_t k = 0; k < ml_dsa::N; ++k) {
365
+ // Elt to int32 conversion
366
+ auto n = f_.from_montgomery(w_prime_approx_[i][k]).limb_[0];
367
+ int32_t val = static_cast<int32_t>(n);
368
+
369
+ auto [r1, r0] = ml_dsa::Decompose(val);
370
+
371
+ bool h_bit = ref_sig.h[i][k];
372
+ w_prime_1_[i][k] = ml_dsa::UseHint(h_bit, val);
373
+
374
+ // Calculate unreduced w1 based on hint
375
+ int32_t w1_raw = r1;
376
+ if (h_bit && r0 > 0)
377
+ w1_raw = r1 + 1;
378
+ else if (h_bit && r0 <= 0)
379
+ w1_raw = r1 - 1;
380
+
381
+ w1_[i][k] = r1;
382
+
383
+ // Populate bit witnesses with normalization logic
384
+ auto normalize = [&](int64_t x) {
385
+ int64_t v = x % static_cast<int64_t>(ml_dsa::Q);
386
+ if (v < 0) v += ml_dsa::Q;
387
+ return static_cast<uint64_t>(v);
388
+ };
389
+
390
+ // 18-bit Range Check interval shifting logic
391
+ int64_t gamma2 = static_cast<int64_t>(ml_dsa::GAMMA_2);
392
+ int64_t delta =
393
+ static_cast<int64_t>(val) - static_cast<int64_t>(r1) * (2 * gamma2);
394
+
395
+ // Symmetrically reduce modulo Q to get true remainder in Z_Q
396
+ delta = SymmetricReduce(delta);
397
+
398
+ uint64_t R = delta + gamma2 - 1;
399
+ uint64_t s = (delta > 0) ? 0 : 1;
400
+
401
+ uint64_t aux_bits = R | (s << 18);
402
+ hint_aux_bits_[i][k] = normalize(aux_bits);
403
+
404
+ w_prime_1_bits_[i][k] = normalize(w_prime_1_[i][k]);
405
+ }
406
+ }
407
+
408
+ // 7. Compute w1Encode (needed for mu)
409
+ std::array<ml_dsa::Rq, ml_dsa::K> w1_polys;
410
+ for (size_t i = 0; i < ml_dsa::K; ++i) {
411
+ for (size_t j = 0; j < ml_dsa::N; ++j) {
412
+ w1_polys[i][j] = f_.of_scalar(w_prime_1_[i][j]);
413
+ }
414
+ }
415
+ w1_tilde_ = ml_dsa::w1Encode(w1_polys);
416
+
417
+ // 8. Compute mu = H(tr || M', 64)
418
+ // Concat tr and msg_prime
419
+ std::vector<uint8_t> mu_input(tr_.begin(), tr_.end());
420
+ mu_input.push_back(0); // domain separator
421
+ mu_input.push_back(static_cast<uint8_t>(ctx.size()));
422
+ mu_input.insert(mu_input.end(), ctx.begin(), ctx.end());
423
+ mu_input.insert(mu_input.end(), msg.begin(), msg.end());
424
+
425
+ std::array<uint8_t, 64> mu_out;
426
+ ml_dsa::H(mu_input, mu_out);
427
+ std::copy(mu_out.begin(), mu_out.end(), mu_.begin());
428
+ Sha3Witness::compute_witness_shake256(mu_input, 64, mu_bws_);
429
+
430
+ // 9. c_prime_tilde = H(mu || w1_tilde, 32)
431
+ std::vector<uint8_t> c_prime_tilde_input(mu_out.begin(), mu_out.end());
432
+ c_prime_tilde_input.insert(c_prime_tilde_input.end(), w1_tilde_.begin(),
433
+ w1_tilde_.end());
434
+
435
+ std::array<uint8_t, 32> c_prime_tilde_vec;
436
+ ml_dsa::H(c_prime_tilde_input, c_prime_tilde_vec);
437
+ std::copy(c_prime_tilde_vec.begin(), c_prime_tilde_vec.end(),
438
+ c_prime_tilde_.begin());
439
+
440
+ Sha3Witness::compute_witness_shake256(c_prime_tilde_input, 32,
441
+ c_prime_tilde_bws_);
442
+
443
+ if (c_tilde_ != c_prime_tilde_) {
444
+ return false;
445
+ }
446
+
447
+ return true;
448
+ }
449
+ };
450
+
451
+ } // namespace proofs
452
+
453
+ #endif // PRIVACY_PROOFS_ZK_LIB_CIRCUITS_TESTS_PQ_ML_DSA_ML_DSA_44_WITNESS_H_