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,100 @@
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 <stdint.h>
16
+
17
+ #include <cstring>
18
+
19
+ #include "circuits/mdoc/mdoc_zk.h"
20
+
21
+ extern "C" {
22
+ // This is a hardcoded list of all the ZK specifications supported by this
23
+ // library. Every time a new breaking change is introduced in either the circuit
24
+ // format or its interpretation, a new version must be added here.
25
+ // It is possible to remove old versions, if we're sure that they are not used
26
+ // by either provers of verifiers in the wild.
27
+ //
28
+ // The format is:
29
+ // {
30
+ // - system - The ZK system name and version ("longfellow-libzk-v*" for Google
31
+ // library).
32
+ // - circuit_hash - SHA265 hash of the output of generate_circuit() function,
33
+ // the circuit in compressed format. It's converted to a hex string. Every
34
+ // time the circuit changes, the hash must be manually calculated and a new
35
+ // ZKSpec added to this list.
36
+ // - num_attributes. number of attributes the circuit supports,
37
+ // - version. version of the ZK specification
38
+ //. - block_enc_hash. block_enc parameter for the ZK proof of the hash
39
+ // component. Version 3 uses the legacy-computed value, v4 uses optimized
40
+ // values.
41
+ // - block_enc_sig. block_enc parameter for the ZK proof of the signature
42
+ // component.
43
+ // }
44
+
45
+ const ZkSpecStruct kZkSpecs[kNumZkSpecs] = {
46
+ // Circuits produced 2026-01-09
47
+ {"longfellow-libzk-v1",
48
+ "8d079211715200ff06c5109639245502bfe94aa869908d31176aae4016182121", 1, 7,
49
+ 4151, 4096},
50
+ {"longfellow-libzk-v1",
51
+ "6a5810683e62b6d7766ebd0d7ca72518a2b8325418142adcadb10d51dbbcd5ad", 2, 7,
52
+ 4265, 4096},
53
+ {"longfellow-libzk-v1",
54
+ "8ee4849ae1293ae6fe5f9082ce3e5e15c4f198f2998c682fa1b727237d6d252f", 3, 7,
55
+ 4307, 4096},
56
+ {"longfellow-libzk-v1",
57
+ "5aebdaaafe17296a3ef3ca6c80c6e7505e09291897c39700410a365fb278e460", 4, 7,
58
+ 4415, 4096},
59
+ // Circuits produced on 2025-10-10
60
+ {"longfellow-libzk-v1",
61
+ "137e5a75ce72735a37c8a72da1a8a0a5df8d13365c2ae3d2c2bd6a0e7197c7c6", 1, 6,
62
+ 4096, 2945},
63
+ {"longfellow-libzk-v1",
64
+ "b4bb6f01b7043f4f51d8302a30b36e3d4d2d0efc3c24557ab9212ad524a9764e", 2, 6,
65
+ 4025, 2945},
66
+ {"longfellow-libzk-v1",
67
+ "b2211223b954b34a1081e3fbf71b8ea2de28efc888b4be510f532d6ba76c2010", 3, 6,
68
+ 4121, 2945},
69
+ {"longfellow-libzk-v1",
70
+ "c70b5f44a1365c53847eb8948ad5b4fdc224251a2bc02d958c84c862823c49d6", 4, 6,
71
+ 4283, 2945},
72
+ // Circuits produced on 2025-08-21
73
+ {"longfellow-libzk-v1",
74
+ "f88a39e561ec0be02bb3dfe38fb609ad154e98decbbe632887d850fc612fea6f", 1, 5,
75
+ 4096, 2945},
76
+ {"longfellow-libzk-v1",
77
+ "f51b7248b364462854d306326abded169854697d752d3bb6d9a9446ff7605ddb", 2, 5,
78
+ 4025, 2945},
79
+ {"longfellow-libzk-v1",
80
+ "c27195e03e22c9ab4efe9e1dabd2c33aa8b2429cc4e86410c6f12542d3c5e0a1", 3, 5,
81
+ 4121, 2945},
82
+ {"longfellow-libzk-v1",
83
+ "fa5fadfb2a916d3b71144e9b412eff78f71fd6a6d4607eac10de66b195868b7a", 4, 5,
84
+ 4283, 2945},
85
+
86
+ };
87
+
88
+ const ZkSpecStruct* find_zk_spec(const char* system_name,
89
+ const char* circuit_hash) {
90
+ for (size_t i = 0; i < kNumZkSpecs; ++i) {
91
+ const ZkSpecStruct& zk_spec = kZkSpecs[i];
92
+ if (strcmp(zk_spec.system, system_name) == 0 &&
93
+ strcmp(zk_spec.circuit_hash, circuit_hash) == 0) {
94
+ return &zk_spec;
95
+ }
96
+ }
97
+ return nullptr;
98
+ }
99
+
100
+ } // extern "C"
@@ -0,0 +1,155 @@
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 <sys/types.h>
16
+
17
+ #include <cstddef>
18
+ #include <cstdint>
19
+ #include <cstdio>
20
+ #include <cstdlib>
21
+ #include <cstring>
22
+ #include <string>
23
+
24
+ #include "file/base/helpers.h"
25
+ #include "file/base/options.h"
26
+ #include "file/base/path.h"
27
+ #include "circuits/mdoc/mdoc_examples.h"
28
+ #include "circuits/mdoc/mdoc_test_attributes.h"
29
+ #include "circuits/mdoc/mdoc_zk.h"
30
+ #include "util/crypto.h"
31
+ #include "util/log.h"
32
+ #include "testing/base/public/gmock.h"
33
+ #include "gtest/gtest.h"
34
+
35
+ namespace proofs {
36
+ namespace {
37
+
38
+ TEST(ZkSpecTest, FindZkSpec) {
39
+ const ZkSpecStruct& zk_spec = kZkSpecs[0];
40
+
41
+ const ZkSpecStruct* found_zk_spec =
42
+ find_zk_spec("longfellow-libzk-v1", zk_spec.circuit_hash);
43
+ EXPECT_NE(found_zk_spec, nullptr);
44
+ EXPECT_EQ(found_zk_spec->system, zk_spec.system);
45
+ EXPECT_EQ(found_zk_spec->circuit_hash, zk_spec.circuit_hash);
46
+ EXPECT_EQ(found_zk_spec->num_attributes, zk_spec.num_attributes);
47
+ EXPECT_EQ(found_zk_spec->version, zk_spec.version);
48
+ }
49
+
50
+ TEST(ZkSpecTest, ReturnNullptrIfNoMatchingZkSpecFound) {
51
+ const ZkSpecStruct* zk_spec = find_zk_spec(
52
+ "longfellow-libzk-v1",
53
+ "1234567890123456789012345678901234567890123456789012345678901234");
54
+ EXPECT_EQ(zk_spec, nullptr);
55
+ }
56
+
57
+ void test_circuit_hash(size_t num_attributes) {
58
+ // Find the latest version of the circuit for the given number of attributes.
59
+ const ZkSpecStruct* zk_spec = nullptr;
60
+ for (int i = 0; i < kNumZkSpecs; ++i) {
61
+ if (kZkSpecs[i].num_attributes == num_attributes) {
62
+ if (zk_spec == nullptr || kZkSpecs[i].version > zk_spec->version) {
63
+ zk_spec = &kZkSpecs[i];
64
+ }
65
+ }
66
+ }
67
+
68
+ uint8_t* circuit;
69
+ size_t circuit_len;
70
+ auto ret = generate_circuit(zk_spec, &circuit, &circuit_len);
71
+ EXPECT_EQ(ret, CIRCUIT_GENERATION_SUCCESS);
72
+
73
+ uint8_t cid[kSHA256DigestSize];
74
+ EXPECT_TRUE(circuit_id(cid, circuit, circuit_len, zk_spec));
75
+
76
+ char buf[kSHA256DigestSize * 2 + 1] = {};
77
+ hex_to_str(buf, cid, kSHA256DigestSize);
78
+ log(INFO, "circuit hash %d attr:: %s", num_attributes, buf);
79
+
80
+ bool found = false;
81
+ for (size_t k = 0; k < kNumZkSpecs; ++k) {
82
+ if (strcmp(kZkSpecs[k].circuit_hash, buf) == 0) {
83
+ found = true;
84
+ break;
85
+ }
86
+ }
87
+ // Must use free because generate_circuit is a pure C library that allocates
88
+ // with malloc.
89
+ free(circuit);
90
+ EXPECT_TRUE(found);
91
+ }
92
+
93
+ // These tests ensure that the current circuit hash for 1--4 attributes is
94
+ // included in the zk_spec data structure.
95
+ // They are defined separately so that they can run in parallel.
96
+ // They can be run using
97
+ // blaze test -c opt --test_output=streamed \
98
+ // //circuits/mdoc:zk_spec_test
99
+ // in order to print out the new circuit hashes.
100
+ TEST(ZkSpecTest, CorrectSpecFor1Attribute) { test_circuit_hash(1); }
101
+
102
+ TEST(ZkSpecTest, CorrectSpecFor2Attributes) { test_circuit_hash(2); }
103
+
104
+ TEST(ZkSpecTest, CorrectSpecFor3Attributes) { test_circuit_hash(3); }
105
+
106
+ TEST(ZkSpecTest, CorrectSpecFor4Attributes) { test_circuit_hash(4); }
107
+
108
+ void test_proof_creation_and_verification(const ZkSpecStruct& zk_spec) {
109
+ // Read the circuit file from circuits/hash.
110
+ auto cp = file::JoinPath("circuits/mdoc/circuits/",
111
+ zk_spec.circuit_hash);
112
+ std::string circuit_bytes;
113
+ EXPECT_OK(file::GetContents(cp, &circuit_bytes, file::Defaults()));
114
+
115
+ const MdocTests* test = &mdoc_tests[3]; /* Sprind example w/4 attributes */
116
+ RequestedAttribute claims[4] = {test::age_over_18,
117
+ test::familyname_mustermann,
118
+ test::birthdate_1971_09_01, test::height_175};
119
+
120
+ uint8_t* zkproof;
121
+ size_t proof_len;
122
+
123
+ {
124
+ log(INFO, "starting prover");
125
+ MdocProverErrorCode ret = run_mdoc_prover(
126
+ (uint8_t*)circuit_bytes.data(), circuit_bytes.size(), test->mdoc,
127
+ test->mdoc_size, test->pkx.as_pointer, test->pky.as_pointer,
128
+ test->transcript, test->transcript_size, claims, zk_spec.num_attributes,
129
+ (const char*)test->now, &zkproof, &proof_len, &zk_spec);
130
+ EXPECT_EQ(ret, MDOC_PROVER_SUCCESS);
131
+ }
132
+ {
133
+ log(INFO, "starting verifier");
134
+ MdocVerifierErrorCode ret = run_mdoc_verifier(
135
+ (uint8_t*)circuit_bytes.data(), circuit_bytes.size(),
136
+ test->pkx.as_pointer, test->pky.as_pointer, test->transcript,
137
+ test->transcript_size, claims, zk_spec.num_attributes,
138
+ (const char*)test->now, zkproof, proof_len, test->doc_type, &zk_spec);
139
+ EXPECT_EQ(ret, MDOC_VERIFIER_SUCCESS);
140
+ free(zkproof);
141
+ }
142
+ }
143
+
144
+ // Test proof creation and verification against all supported circuits.
145
+ TEST(ZkSpecTest, ProofCreationAndVerification) {
146
+ for (size_t k = 0; k < kNumZkSpecs; ++k) {
147
+ const ZkSpecStruct& zk_spec = kZkSpecs[k];
148
+ log(INFO, "Testing circuit hash %s, %d attributes", zk_spec.circuit_hash,
149
+ zk_spec.num_attributes);
150
+ test_proof_creation_and_verification(zk_spec);
151
+ }
152
+ }
153
+
154
+ } // namespace
155
+ } // namespace proofs
@@ -0,0 +1,330 @@
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_SHA_FLATSHA256_CIRCUIT_H_
16
+ #define PRIVACY_PROOFS_ZK_LIB_CIRCUITS_SHA_FLATSHA256_CIRCUIT_H_
17
+
18
+ #include <stddef.h>
19
+
20
+ #include <cstdint>
21
+ #include <vector>
22
+
23
+ #include "circuits/logic/bit_adder.h"
24
+ #include "circuits/sha/sha256_constants.h"
25
+
26
+ namespace proofs {
27
+ // FlatSHA256Circuit
28
+ //
29
+ // Implements SHA256 hash function as an arithmetic circuit over the field F.
30
+ // The circuit is flattened, meaning that the SHA round function has been
31
+ // repeated in parallel instead of sequentially. As a result, the prover must
32
+ // provide the intermediate round values as witnesses.
33
+ //
34
+ // This package does not have any external dependencies on a SHA256 library.
35
+ //
36
+ // There are two versions of this function, one with standard bit inputs, and
37
+ // another with packed bit inputs. The later reduces the number of inputs at
38
+ // the cost of increasing the depth and number of wires. For example, the
39
+ // following shows the difference with pack parameter 2.
40
+ //
41
+ // FlatSHA256_Circuit.assert_transform_block
42
+ // depth: 7 wires: 38029 in: 6657 out:128 use:30897 ovh:7132 t:166468 cse:9703
43
+ // notn:113744
44
+ //
45
+ // FlatSHA256_Circuit.assert_transform_block_packed
46
+ // depth: 9 wires: 65735 in: 3585 out:128 use:55486 ovh:10249 t:214653
47
+ // cse:28135 notn:151504
48
+ //
49
+ //
50
+ template <class Logic, class BitPlucker>
51
+ class FlatSHA256Circuit {
52
+ public:
53
+ using BitW = typename Logic::BitW;
54
+ using v8 = typename Logic::v8;
55
+ using v256 = typename Logic::v256;
56
+ using v32 = typename Logic::v32;
57
+ using v64 = typename Logic::v64;
58
+ using EltW = typename Logic::EltW;
59
+ using Field = typename Logic::Field;
60
+ using packed_v32 = typename BitPlucker::packed_v32;
61
+
62
+ const Logic& l_;
63
+ BitPlucker bp_; /* public, so caller can encode input */
64
+
65
+ static packed_v32 packed_input(const Logic& lc) {
66
+ return BitPlucker::template packed_input<packed_v32>(lc);
67
+ }
68
+
69
+ struct BlockWitness {
70
+ packed_v32 outw[48];
71
+ packed_v32 oute[64];
72
+ packed_v32 outa[64];
73
+ packed_v32 h1[8];
74
+
75
+ void input(const Logic& lc) {
76
+ for (size_t k = 0; k < 48; ++k) {
77
+ outw[k] = packed_input(lc);
78
+ }
79
+ for (size_t k = 0; k < 64; ++k) {
80
+ oute[k] = packed_input(lc);
81
+ outa[k] = packed_input(lc);
82
+ }
83
+ for (size_t k = 0; k < 8; ++k) {
84
+ h1[k] = packed_input(lc);
85
+ }
86
+ }
87
+ };
88
+
89
+ explicit FlatSHA256Circuit(const Logic& l) : l_(l), bp_(l_) {}
90
+
91
+ void assert_transform_block(const v32 in[16], const v32 H0[8],
92
+ const v32 outw[48], const v32 oute[64],
93
+ const v32 outa[64], const v32 H1[8]) const {
94
+ const Logic& L = l_; // shorthand
95
+ BitAdder<Logic, 32> BA(L);
96
+
97
+ std::vector<v32> w(64);
98
+ for (size_t i = 0; i < 16; ++i) {
99
+ w[i] = in[i];
100
+ }
101
+
102
+ for (size_t i = 16; i < 64; ++i) {
103
+ w[i] = outw[i - 16];
104
+ BA.assert_eqmod(
105
+ w[i],
106
+ BA.add({sigma1(w[i - 2]), w[i - 7], sigma0(w[i - 15]), w[i - 16]}),
107
+ 4);
108
+ }
109
+
110
+ v32 a(H0[0]);
111
+ v32 b(H0[1]);
112
+ v32 c(H0[2]);
113
+ v32 d(H0[3]);
114
+ v32 e(H0[4]);
115
+ v32 f(H0[5]);
116
+ v32 g(H0[6]);
117
+ v32 h(H0[7]);
118
+
119
+ for (size_t t = 0; t < 64; ++t) {
120
+ EltW t1 = BA.add(
121
+ {h, Sigma1(e), L.vCh(e, f, g), L.vbit32(kSha256Round[t]), w[t]});
122
+ EltW sigma0 = BA.as_field_element(Sigma0(a));
123
+ EltW vmaj = BA.as_field_element(L.vMaj(a, b, c));
124
+ EltW t2 = BA.add(sigma0, vmaj);
125
+
126
+ h = g;
127
+ g = f;
128
+ f = e;
129
+ e = oute[t];
130
+ EltW ed = BA.as_field_element(d);
131
+ BA.assert_eqmod(e, BA.add(t1, ed), 6);
132
+ d = c;
133
+ c = b;
134
+ b = a;
135
+ a = outa[t];
136
+ BA.assert_eqmod(a, BA.add(t1, t2), 7);
137
+ }
138
+
139
+ BA.assert_eqmod(H1[0], BA.add(H0[0], a), 2);
140
+ BA.assert_eqmod(H1[1], BA.add(H0[1], b), 2);
141
+ BA.assert_eqmod(H1[2], BA.add(H0[2], c), 2);
142
+ BA.assert_eqmod(H1[3], BA.add(H0[3], d), 2);
143
+ BA.assert_eqmod(H1[4], BA.add(H0[4], e), 2);
144
+ BA.assert_eqmod(H1[5], BA.add(H0[5], f), 2);
145
+ BA.assert_eqmod(H1[6], BA.add(H0[6], g), 2);
146
+ BA.assert_eqmod(H1[7], BA.add(H0[7], h), 2);
147
+ }
148
+
149
+ // Packed API.
150
+ // H0 not packed, all others packed
151
+ void assert_transform_block(const v32 in[16], const v32 H0[8],
152
+ const packed_v32 poutw[48],
153
+ const packed_v32 poute[64],
154
+ const packed_v32 pouta[64],
155
+ const packed_v32 pH1[8]) const {
156
+ std::vector<v32> H1(8);
157
+ std::vector<v32> outw(48);
158
+ std::vector<v32> oute(64), outa(64);
159
+ for (size_t i = 0; i < 8; ++i) {
160
+ H1[i] = bp_.unpack_v32(pH1[i]);
161
+ }
162
+ for (size_t i = 0; i < 48; ++i) {
163
+ outw[i] = bp_.unpack_v32(poutw[i]);
164
+ }
165
+ for (size_t i = 0; i < 64; ++i) {
166
+ oute[i] = bp_.unpack_v32(poute[i]);
167
+ outa[i] = bp_.unpack_v32(pouta[i]);
168
+ }
169
+ assert_transform_block(in, H0, outw.data(), oute.data(), outa.data(),
170
+ H1.data());
171
+ }
172
+
173
+ // all packed
174
+ void assert_transform_block(const v32 in[16], const packed_v32 pH0[8],
175
+ const packed_v32 poutw[48],
176
+ const packed_v32 poute[64],
177
+ const packed_v32 pouta[64],
178
+ const packed_v32 pH1[8]) const {
179
+ std::vector<v32> H0(8);
180
+ for (size_t i = 0; i < 8; ++i) {
181
+ H0[i] = bp_.unpack_v32(pH0[i]);
182
+ }
183
+ assert_transform_block(in, H0.data(), poutw, poute, pouta, pH1);
184
+ }
185
+
186
+ /* This method checks that the block witness corresponds to the iterated
187
+ computation of the sha block transform on the input.
188
+ */
189
+ void assert_message(size_t max, const v8& nb, const v8 in[/* 64*max */],
190
+ const BlockWitness bw[/*max*/]) const {
191
+ const Logic& L = l_; // shorthand
192
+ const packed_v32* H = nullptr;
193
+ std::vector<v32> tmp(16);
194
+
195
+ for (size_t b = 0; b < max; ++b) {
196
+ const v8* inb = &in[64 * b];
197
+ for (size_t i = 0; i < 16; ++i) {
198
+ // big-endian mapping of v8[4] into v32. The first
199
+ // argument of vappend() is the LSB, and thus +3 is
200
+ // the LSB and +0 is the MSB, hence big-endian.
201
+ tmp[i] = L.vappend(L.vappend(inb[4 * i + 3], inb[4 * i + 2]),
202
+ L.vappend(inb[4 * i + 1], inb[4 * i + 0]));
203
+ }
204
+ if (b == 0) {
205
+ v32 H0[8];
206
+ initial_context(H0);
207
+ assert_transform_block(tmp.data(), H0, bw[b].outw, bw[b].oute,
208
+ bw[b].outa, bw[b].h1);
209
+ } else {
210
+ assert_transform_block(tmp.data(), H, bw[b].outw, bw[b].oute,
211
+ bw[b].outa, bw[b].h1);
212
+ }
213
+ H = bw[b].h1;
214
+ }
215
+ assert_zero_padding(max, nb, in);
216
+ }
217
+
218
+ /* This method checks if H(in) == target. The method requires that in[]
219
+ contains exactly nb*64 bytes and has been padded according to the SHA256
220
+ specification.
221
+ */
222
+ void assert_message_hash(size_t max, const v8& nb, const v8 in[/* 64*max */],
223
+ const v256& target,
224
+ const BlockWitness bw[/*max*/]) const {
225
+ assert_message(max, nb, in, bw);
226
+ assert_hash(max, target, nb, bw);
227
+ }
228
+
229
+ // Verifies that the nb_th element of the block witness is equal to e.
230
+ // The block witness keeps track of the intermediate output of each
231
+ // block transform. Therefore, this method can be used to verify that the
232
+ // prover knows a preimage that hashes to the desired e.
233
+ void assert_hash(size_t max, const v256& e, const v8& nb,
234
+ const BlockWitness bw[/*max*/]) const {
235
+ packed_v32 x[8];
236
+ for (size_t b = 0; b < max; ++b) {
237
+ auto bt = l_.veq(nb, b + 1); /* b is zero-indexed */
238
+ auto ebt = l_.eval(bt);
239
+ for (size_t i = 0; i < 8; ++i) {
240
+ for (size_t k = 0; k < bp_.kNv32Elts; ++k) {
241
+ if (b == 0) {
242
+ x[i][k] = l_.mul(ebt, bw[b].h1[i][k]);
243
+ } else {
244
+ auto maybe_sha = l_.mul(ebt, bw[b].h1[i][k]);
245
+ x[i][k] = l_.add(x[i][k], maybe_sha);
246
+ }
247
+ }
248
+ }
249
+ }
250
+
251
+ // Unpack the hash into a v256 in reverse byte-order.
252
+ v256 mm;
253
+ for (size_t j = 0; j < 8; ++j) {
254
+ auto hj = bp_.unpack_v32(x[j]);
255
+ for (size_t k = 0; k < 32; ++k) {
256
+ mm[((7 - j) * 32 + k)] = hj[k];
257
+ }
258
+ }
259
+ l_.vassert_eq(mm, e);
260
+ }
261
+
262
+ // Checks that the padding bytes of the input, i.e., any bytes that are
263
+ // not part of the SHA blocks that contain the mdoc, are zero.
264
+ void assert_zero_padding(size_t max, const v8& nb,
265
+ const v8 in[/*64 * max*/]) const {
266
+ for (size_t i = 0; i < max; ++i) {
267
+ auto wantzero = l_.vleq(nb, i); // If nb <= i, block should be 0.
268
+ for (size_t j = 0; j < 64; ++j) {
269
+ size_t ind = i * 64 + j;
270
+ auto zero = l_.veq(in[ind], 0);
271
+ l_.assert_implies(wantzero, zero);
272
+ }
273
+ }
274
+ }
275
+
276
+ // This function extracts the length of the message in bytes from the SHA
277
+ // block that is verified and also performs other sanity checks on the length.
278
+ // The length in bits is stored in the last 8 bytes of the nb_th SHA block.
279
+ v64 find_len(size_t max, const v8 in[/*64*max*/], const v8& nb) const {
280
+ v64 len = l_.template vbit<64>(0);
281
+ for (size_t i = 0; i < max; ++i) {
282
+ auto isblk = l_.veq(nb, i + 1); // If nb == i, i is zero-indexed.
283
+ size_t ind = i * 64 + 63;
284
+ for (size_t j = 0; j < 64; ++j) { /* this loop is over bits */
285
+ len[j] =
286
+ l_.lor_exclusive(len[j], l_.land(isblk, in[ind - j / 8][j % 8]));
287
+ }
288
+ }
289
+ l_.vassert_is_bit(len);
290
+ return len;
291
+ }
292
+
293
+ private:
294
+ void initial_context(v32 H[8]) const {
295
+ static const uint64_t initial[8] = {0x6a09e667u, 0xbb67ae85u, 0x3c6ef372u,
296
+ 0xa54ff53au, 0x510e527fu, 0x9b05688cu,
297
+ 0x1f83d9abu, 0x5be0cd19u};
298
+ for (size_t i = 0; i < 8; i++) {
299
+ H[i] = l_.template vbit<32>(initial[i]);
300
+ }
301
+ }
302
+
303
+ v32 Sigma0(const v32& x) const {
304
+ auto x2 = l_.vrotr(x, 2);
305
+ auto x13 = l_.vrotr(x, 13);
306
+ return l_.vxor3(x2, x13, l_.vrotr(x, 22));
307
+ }
308
+
309
+ v32 Sigma1(const v32& x) const {
310
+ auto x6 = l_.vrotr(x, 6);
311
+ auto x11 = l_.vrotr(x, 11);
312
+ return l_.vxor3(x6, x11, l_.vrotr(x, 25));
313
+ }
314
+
315
+ v32 sigma0(const v32& x) const {
316
+ auto x7 = l_.vrotr(x, 7);
317
+ auto x18 = l_.vrotr(x, 18);
318
+ return l_.vxor3(x7, x18, l_.vshr(x, 3));
319
+ }
320
+
321
+ v32 sigma1(const v32& x) const {
322
+ auto x17 = l_.vrotr(x, 17);
323
+ auto x19 = l_.vrotr(x, 19);
324
+ return l_.vxor3(x17, x19, l_.vshr(x, 10));
325
+ }
326
+ };
327
+
328
+ } // namespace proofs
329
+
330
+ #endif // PRIVACY_PROOFS_ZK_LIB_CIRCUITS_SHA_FLATSHA256_CIRCUIT_H_