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,554 @@
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_MDOC_MDOC_HASH_H_
16
+ #define PRIVACY_PROOFS_ZK_LIB_CIRCUITS_MDOC_MDOC_HASH_H_
17
+
18
+ #include <cstddef>
19
+ #include <cstdint>
20
+ #include <vector>
21
+
22
+ #include "circuits/cbor_parser/cbor_byte_decoder.h"
23
+ #include "circuits/logic/bit_plucker.h"
24
+ #include "circuits/logic/memcmp.h"
25
+ #include "circuits/logic/routing.h"
26
+ #include "circuits/mdoc/mdoc_constants.h"
27
+ #include "circuits/sha/flatsha256_circuit.h"
28
+
29
+ namespace proofs {
30
+
31
+ static constexpr size_t kSHAPluckerBits = 4u;
32
+
33
+ // This class (only) verifies the hashing and pseudo-parsing of an mdoc.
34
+ // Specifically, it checks
35
+ // (a) the hash of the mdoc matches its mac.
36
+ // (b) the values dpk_{x,y} appear in approximate cbor form, and their macs
37
+ // match the expected values
38
+ // (c) validFrom, validUntil appear in approximate cbor form, and that
39
+ // validFrom <= now <= validUntil
40
+ // (d) For each expected attribute, there exists a preimage to a sha hash
41
+ // that appears in the mso, the preimage is approximately cbor formatted,
42
+ // and the preimage includes the expected attribute id and value.
43
+ template <class LogicCircuit, class Field>
44
+ class MdocHash {
45
+ using BitW = typename LogicCircuit::BitW;
46
+ using v8 = typename LogicCircuit::v8;
47
+ using v32 = typename LogicCircuit::v32;
48
+ using v64 = typename LogicCircuit::v64;
49
+ using v256 = typename LogicCircuit::v256;
50
+
51
+ using vind = typename LogicCircuit::template bitvec<kCborIndexBits>;
52
+
53
+ using Flatsha = FlatSHA256Circuit<LogicCircuit,
54
+ BitPlucker<LogicCircuit, kSHAPluckerBits>>;
55
+ using ShaBlockWitness = typename Flatsha::BlockWitness;
56
+ using sha_packed_v32 = typename Flatsha::packed_v32;
57
+
58
+ public:
59
+ // These structures mimic the similarly named structures in Witness, but
60
+ // their members are circuit wire objects instead of size_t.
61
+ struct OpenedAttribute {
62
+ v8 attr[32]; /* 32b representing attribute name in be. */
63
+ v8 v1[64]; /* 64b of attribute value */
64
+ // NOTE: although these arrays are sized {32,64}, other aspects of the
65
+ // system constrain the sum of the elementIdentifier and elementValue to be
66
+ // at most 56 bytes for a proof to succeed. We will maintain this API, but
67
+ // publish the constraint about the sum in the documentation.
68
+ v8 len; /* public length of the encoded attribute id */
69
+ v8 vlen; /* public length of the encoded attribute value */
70
+ void input(const LogicCircuit& lc) {
71
+ for (size_t j = 0; j < 32; ++j) {
72
+ attr[j] = lc.template vinput<8>();
73
+ }
74
+ for (size_t j = 0; j < 64; ++j) {
75
+ v1[j] = lc.template vinput<8>();
76
+ }
77
+ len = lc.template vinput<8>();
78
+ vlen = lc.template vinput<8>();
79
+ }
80
+ };
81
+ struct CborIndex {
82
+ vind k;
83
+ void input(const LogicCircuit& lc) {
84
+ k = lc.template vinput<kCborIndexBits>();
85
+ }
86
+ };
87
+
88
+ struct AttrShift {
89
+ vind offset;
90
+ vind len;
91
+ void input(const LogicCircuit& lc) {
92
+ offset = lc.template vinput<kCborIndexBits>();
93
+ len = lc.template vinput<kCborIndexBits>();
94
+ }
95
+ };
96
+
97
+ struct SaltedHash {
98
+ vind i1, i2, i3;
99
+ vind l[4];
100
+ v8 perm;
101
+ void input(const LogicCircuit& lc) {
102
+ i1 = lc.template vinput<kCborIndexBits>();
103
+ i2 = lc.template vinput<kCborIndexBits>();
104
+ i3 = lc.template vinput<kCborIndexBits>();
105
+ for (size_t j = 0; j < 4; ++j) {
106
+ l[j] = lc.template vinput<kCborIndexBits>();
107
+ }
108
+ perm = lc.template vinput<8>();
109
+ }
110
+ };
111
+
112
+ class Witness {
113
+ public:
114
+ v8 in_[64 * kMaxSHABlocks]; /* input bytes, 64 * MAX */
115
+
116
+ v8 nb_; /* index of sha block that contains the real hash */
117
+ ShaBlockWitness sig_sha_[kMaxSHABlocks];
118
+
119
+ std::vector<std::vector<ShaBlockWitness>> attr_sha_;
120
+ std::vector<std::vector<v8>> attrb_;
121
+
122
+ CborIndex valid_from_, valid_until_;
123
+ CborIndex dev_key_info_;
124
+ CborIndex value_digests_;
125
+ std::vector<CborIndex> attr_mso_;
126
+ std::vector<AttrShift> attr_ei_;
127
+ std::vector<AttrShift> attr_ev_;
128
+ std::vector<SaltedHash> salted_hashes_;
129
+ size_t num_attr_;
130
+
131
+ explicit Witness(size_t num_attr) {
132
+ num_attr_ = num_attr;
133
+ attr_mso_.resize(num_attr);
134
+ attr_ei_.resize(num_attr);
135
+ attr_ev_.resize(num_attr);
136
+ attr_sha_.resize(num_attr);
137
+ salted_hashes_.resize(num_attr);
138
+ for (size_t i = 0; i < num_attr; ++i) {
139
+ attr_sha_[i].resize(2);
140
+ }
141
+
142
+ attrb_.resize(num_attr);
143
+ }
144
+
145
+ void input(const LogicCircuit& lc) {
146
+ nb_ = lc.template vinput<8>();
147
+
148
+ // sha input init =========================
149
+ for (size_t i = 0; i + kCose1PrefixLen < 64 * kMaxSHABlocks; ++i) {
150
+ in_[i] = lc.template vinput<8>();
151
+ }
152
+ for (size_t j = 0; j < kMaxSHABlocks; j++) {
153
+ sig_sha_[j].input(lc);
154
+ }
155
+
156
+ valid_from_.input(lc);
157
+ valid_until_.input(lc);
158
+ dev_key_info_.input(lc);
159
+ value_digests_.input(lc);
160
+
161
+ // Attribute opening witnesses
162
+ for (size_t ai = 0; ai < num_attr_; ++ai) {
163
+ for (size_t i = 0; i < 64 * 2; ++i) {
164
+ attrb_[ai].push_back(lc.template vinput<8>());
165
+ }
166
+ for (size_t j = 0; j < 2; j++) {
167
+ attr_sha_[ai][j].input(lc);
168
+ }
169
+ attr_mso_[ai].input(lc);
170
+ attr_ei_[ai].input(lc);
171
+ attr_ev_[ai].input(lc);
172
+ salted_hashes_[ai].input(lc);
173
+ }
174
+ }
175
+ };
176
+
177
+ explicit MdocHash(const LogicCircuit& lc)
178
+ : lc_(lc), sha_(lc), r_(lc), cb_(lc) {}
179
+
180
+ void assert_valid_hash_mdoc(OpenedAttribute oa[/* NUM_ATTR */],
181
+ const v8 now[/*20*/], const v256& e,
182
+ const v256& dpkx, const v256& dpky,
183
+ const Witness& vw) const {
184
+ auto preimage = construct_signature_preimage(vw);
185
+ lc_.vassert_is_bit(vw.nb_);
186
+ lc_.vleq(vw.nb_, kMaxSHABlocks);
187
+ sha_.assert_message_hash(kMaxSHABlocks, vw.nb_, preimage.data(), e,
188
+ vw.sig_sha_);
189
+
190
+ // Find the length of the MSO in bytes. Use this to range check each index.
191
+ v64 len = sha_.find_len(kMaxSHABlocks, preimage.data(), vw.nb_);
192
+
193
+ // Shift a portion of the MSO into buf and check it.
194
+ const v8 zz = lc_.template vbit<8>(0); // cannot appear in strings
195
+ std::vector<v8> cmp_buf(kMaxMsoLen);
196
+ const Memcmp<LogicCircuit> CMP(lc_);
197
+
198
+ // In the shifting below, the +5 corresponds to the prefix
199
+ // D8 18 <len2> prefix of the mso that we want to skip parsing.
200
+ // The +2 corresponds to the length.
201
+
202
+ // validFrom <= now
203
+ check_index(vw.valid_from_.k, len);
204
+ r_.shift(vw.valid_from_.k, kValidFromLen + kDateLen, &cmp_buf[0],
205
+ kMaxMsoLen, vw.in_ + 5 + 2, zz, /*unroll=*/3);
206
+ assert_bytes_at(kValidFromLen, &cmp_buf[0], kValidFromCheck);
207
+ auto cmp = CMP.leq(kDateLen, &cmp_buf[kValidFromLen], &now[0]);
208
+ lc_.assert1(cmp);
209
+
210
+ // now <= validUntil
211
+ check_index(vw.valid_until_.k, len);
212
+ r_.shift(vw.valid_until_.k, kValidUntilLen + kDateLen, &cmp_buf[0],
213
+ kMaxMsoLen, vw.in_ + 5 + 2, zz, /*unroll=*/3);
214
+ assert_bytes_at(kValidUntilLen, &cmp_buf[0], kValidUntilCheck);
215
+ cmp = CMP.leq(kDateLen, &now[0], &cmp_buf[kValidUntilLen]);
216
+ lc_.assert1(cmp);
217
+
218
+ // DPK_{x,y}
219
+ check_index(vw.dev_key_info_.k, len);
220
+ r_.shift(vw.dev_key_info_.k, kDeviceKeyInfoLen + 3 + 32 + 32, &cmp_buf[0],
221
+ kMaxMsoLen, vw.in_ + 5 + 2, zz, /*unroll=*/3);
222
+ assert_bytes_at(kDeviceKeyInfoLen, &cmp_buf[0], kDeviceKeyInfoCheck);
223
+ uint8_t dpkyCheck[] = {0x22, 0x58, 0x20};
224
+ assert_bytes_at(sizeof(dpkyCheck), &cmp_buf[65], dpkyCheck);
225
+
226
+ assert_key(dpkx, &cmp_buf[kPkxInd]);
227
+ assert_key(dpky, &cmp_buf[kPkyInd]);
228
+
229
+ // Attributes parsing
230
+ // valueDigests, ignore byte 13 \in {A1,A2} representing map size.
231
+ check_index(vw.value_digests_.k, len);
232
+ r_.shift(vw.value_digests_.k, kValueDigestsLen, cmp_buf.data(), kMaxMsoLen,
233
+ vw.in_ + 5 + 2, zz, /*unroll=*/3);
234
+ assert_bytes_at(13, &cmp_buf[0], kValueDigestsCheck);
235
+
236
+ // Attributes: Equality of hash with MSO value.
237
+
238
+ for (size_t ai = 0; ai < vw.num_attr_; ++ai) {
239
+ // Recall that the MSO contains a hash value e2 where
240
+ // e2 = hash( cbor.bstr( IssuerSignedItem ))
241
+ // and IssuerSignedItem is a CBOR structure defined as:
242
+ // IssuerSignedItem = {
243
+ // "digestID" : uint,
244
+ // "random" : bstr,
245
+ // "elementIdentifier" : DataElementIdentifier,
246
+ // "elementValue" : DataElementValue };
247
+ // The order of the elements in the IssuerSignedItem is not specified.
248
+ //
249
+ // The constraints below verify the following:
250
+ // 1. the index attr_mso_[ai].k < len(mso)
251
+ // 2. a 32-byte cbor array e2 occurs at index attr_mso_[ai].k in mso
252
+ // -- attrb_[ai] is a SHA-256 length-padded 128-byte witness array
253
+ // 3. e2 = hash(attrb_[ai].data())
254
+ // 4. sl is the length of attrb_[ai] as per the SHA-256 padding rule
255
+ // 5. The attrb_[ai] array is well-formed cbor that includes the
256
+ // expected elementIdentifier, and elementValue fields as per the
257
+ // public oa[ai] argument.
258
+ check_index(vw.attr_mso_[ai].k, len);
259
+ r_.shift(vw.attr_mso_[ai].k, 2 + 32, &cmp_buf[0], kMaxMsoLen,
260
+ vw.in_ + 5 + 2, zz, /*unroll=*/3);
261
+
262
+ // 2. Basic CBOR check of the Tag
263
+ assert_bytes_at(2, &cmp_buf[0], kTag32);
264
+
265
+ v256 mm;
266
+ // The loop below accounts for endian and v256 vs v8 types.
267
+ for (size_t j = 0; j < 256; ++j) {
268
+ mm[j] = cmp_buf[2 + (255 - j) / 8][(j % 8)];
269
+ }
270
+ lc_.vassert_is_bit(mm);
271
+
272
+ // 3. Check the hash matches the value in the witness.
273
+ auto two = lc_.template vbit<8>(2);
274
+ sha_.assert_message_hash(2, two, vw.attrb_[ai].data(), mm,
275
+ vw.attr_sha_[ai].data());
276
+
277
+ // 4. Check the length of the witness.
278
+ v64 salted_len = sha_.find_len(2, vw.attrb_[ai].data(), two);
279
+
280
+ // 5. Check the attribute is well-formed and matches the public argument.
281
+ assert_attribute(128, vw.attrb_[ai].data(), vw.salted_hashes_[ai], oa[ai],
282
+ salted_len);
283
+ }
284
+ }
285
+
286
+ private:
287
+ std::vector<v8> construct_signature_preimage(const Witness& vw) const {
288
+ std::vector<v8> bbuf(64 * kMaxSHABlocks);
289
+ for (size_t i = 0; i < 64 * kMaxSHABlocks; ++i) {
290
+ if (i < kCose1PrefixLen) {
291
+ lc_.bits(8, bbuf[i].data(), kCose1Prefix[i]);
292
+ } else {
293
+ bbuf[i] = vw.in_[i - kCose1PrefixLen];
294
+ }
295
+ }
296
+ return bbuf;
297
+ }
298
+
299
+ vind extract_vind(const v64& len) const {
300
+ auto low = lc_.template slice<0, 3>(len);
301
+ auto mid = lc_.template slice<3, 3 + kCborIndexBits>(len);
302
+ auto hi = lc_.template slice<3 + kCborIndexBits, 64>(len);
303
+ // Because check_index is called on several indices, the following two
304
+ // checks will be called several times. However, the compiler removes
305
+ // redundant checks, and it is convenient to verify the ranges in one
306
+ // function. An alternative would be to verify the low 3 bits of len
307
+ // and upper 40+ bits of len are 0, and then rely on that invariant in
308
+ // this method, but that separates the logic, and this is easier to audit.
309
+ lc_.vassert0(low);
310
+ lc_.vassert0(hi);
311
+ return mid;
312
+ }
313
+
314
+ // Checks that the index is less than the length. The len is given as bits
315
+ // in a v64 in big endian order, and the index is given as a byte index.
316
+ void check_index(const vind& index, const v64& len) const {
317
+ lc_.vassert_is_bit(index);
318
+ auto mid = extract_vind(len);
319
+ lc_.assert1(lc_.vlt(index, mid));
320
+ }
321
+
322
+ void assert_bytes_at(size_t len, const v8 buf[/*>=len*/],
323
+ const uint8_t want[/*len*/]) const {
324
+ for (size_t i = 0; i < len; ++i) {
325
+ auto want_i = lc_.template vbit<8>(want[i]);
326
+ lc_.vassert_eq(buf[i], want_i);
327
+ }
328
+ }
329
+
330
+ void format_element(v8 buf[/*max*/], size_t max,
331
+ const uint8_t prefix[/*prefix_len*/], size_t prefix_len,
332
+ const v8 str[/*str_len*/], size_t str_len) const {
333
+ for (size_t i = 0; i < max; ++i) {
334
+ buf[i] = lc_.template vbit<8>(0);
335
+ }
336
+ for (size_t i = 0; i < prefix_len; ++i) {
337
+ buf[i] = lc_.template vbit<8>(prefix[i]);
338
+ }
339
+ for (size_t i = 0; i < str_len && prefix_len + i < max; ++i) {
340
+ buf[prefix_len + i] = str[i];
341
+ }
342
+ }
343
+
344
+ // This function verifies that the length of a cbor key-value pair in an array
345
+ // matches the claimed length.
346
+ // key_len: the length of the key with its header byte included.
347
+ // val_hdr_index: the index of the value's cbor header
348
+ // byte in the buffer.
349
+ // atom: true if the value is an atom, e.g., the digestID.
350
+ // false it is a string or array.
351
+ void check_cbor_length(const v8 buf[/*max*/], size_t max,
352
+ const vind& expected_len, size_t val_hdr_index,
353
+ bool atom = false) const {
354
+ // The length of a key-value field for a known key will be:
355
+ // len(encoding of key) + len(key) + len(header of value) + len(value)
356
+
357
+ auto cbor = cb_.decode_one_v8(buf[val_hdr_index]);
358
+ lc_.assert0(cbor.invalid);
359
+
360
+ vind l1 = lc_.template vbit<kCborIndexBits>(0), // len of value
361
+ l2 = lc_.template vbit<kCborIndexBits>(0); // len of header of value
362
+ vind one = lc_.template vbit<kCborIndexBits>(1),
363
+ two = lc_.template vbit<kCborIndexBits>(2);
364
+ if (!atom) {
365
+ // Mux the length of the value into l1.
366
+ // In this case, the value is either encoded in the last 5 bits of the
367
+ // header byte, or in the next byte. The array length is < 256 by
368
+ // external constraints, and thus its length is in 1 or 2 bytes.
369
+ for (size_t j = 0; j < 8; ++j) {
370
+ l1[j] = lc_.mux(cbor.length_plus_next_v8, buf[val_hdr_index + 1][j],
371
+ j < 5 ? buf[val_hdr_index][j] : lc_.bit(0));
372
+ }
373
+ lc_.vmux(cbor.length_plus_next_v8, l2, two, one);
374
+ } else {
375
+ // For atoms, the value length is zero. The header length is 1,2,3, or 5
376
+ // because the digestID is constrained to be < 2^31 [18013-5, 9.1.2.4].
377
+ lc_.assert0(cbor.count27);
378
+ l2[2] = cbor.count26;
379
+ l2[1] = lc_.lor(cbor.count24, cbor.count25);
380
+ l2[0] = lc_.lnot(cbor.count24);
381
+ }
382
+
383
+ // Compute the read length.
384
+ vind k_len = lc_.template vbit<kCborIndexBits>(val_hdr_index);
385
+ vind v_len = lc_.template vadd<kCborIndexBits>(l1, l2);
386
+ lc_.assert_sum(kCborIndexBits, expected_len.data(), k_len.data(),
387
+ v_len.data());
388
+ }
389
+
390
+ // assert_attribute checks that the bytes in buf correspond to a valid
391
+ // cbor structure that encodes the OpenedAttribute oa.
392
+ void assert_attribute(size_t max, const v8 buf[/*max*/], const SaltedHash& sh,
393
+ const OpenedAttribute& oa,
394
+ const v64& salted_len) const {
395
+ // Perform a cbor parsing of the buffer, which is expected to be
396
+ // a 4-element key-value array consisting of keys digestId, random,
397
+ // elementIdentifier, and elementValue. Then perform a check on the
398
+ // last two. The complexity of check stems from the fact that the 4 pairs
399
+ // can occur in any order.
400
+ // The prover provides the following witnesses in SaltedHash:
401
+ // -- (5, l0), (i1, l1), (i2, l2), (i3, l3)
402
+ // where li is the length of the i-th element in bytes.
403
+ // -- j7j6 j5j4 j3j2 j1j0 where ji \in {0,1}, and
404
+ // j1j0: specifies the index in the previous array of digestId
405
+ // j3j2: specifies the index in the previous array of random
406
+ // j5j4: specifies the index in the previous array of elementIdentifier
407
+ // j7j6: specifies the index in the previous array of elementValue
408
+
409
+ // Verify the cbor prefix for IssuerSignedItem.
410
+ static const uint8_t cbor_tag[] = {0xD8, 0x18, 0x58};
411
+ static const uint8_t cbor_array[] = {0xA4};
412
+ assert_bytes_at(3, buf, cbor_tag);
413
+ assert_bytes_at(1, &buf[4], cbor_array);
414
+
415
+ // Verify all of the indices and lengths are contiguous and consistent.
416
+ vind five = lc_.template vbit<kCborIndexBits>(5);
417
+ vind tot = extract_vind(salted_len);
418
+ lc_.assert_sum(kCborIndexBits, sh.i1.data(), five.data(), sh.l[0].data());
419
+ lc_.assert_sum(kCborIndexBits, sh.i2.data(), sh.i1.data(), sh.l[1].data());
420
+ lc_.assert_sum(kCborIndexBits, sh.i3.data(), sh.i2.data(), sh.l[2].data());
421
+ lc_.assert_sum(kCborIndexBits, tot.data(), sh.i3.data(), sh.l[3].data());
422
+
423
+ // Verify expected structure of the salted hash cbor array.
424
+ vind shift, len;
425
+ const size_t MAX_BUF = 119;
426
+ v8 got[MAX_BUF]; // Max buffer length for a 2-block SHA hash.
427
+ const v8 zz = lc_.template vbit<8>(0); // cannot appear in strings
428
+
429
+ // Shift each of the 4 fields into place, and perform consistency checks.
430
+ // "digestID" checks the cbor key and consistency with the total length.
431
+ mux_offset(0, shift, len, sh);
432
+ r_.shift(shift, MAX_BUF, got, max, buf, zz, 3);
433
+ assert_bytes_at(kDigestLen, &got[0], kDigestID);
434
+ check_cbor_length(got, MAX_BUF, len, 9, true);
435
+
436
+ // "random" checks the cbor key and consistency with the total length.
437
+ mux_offset(1, shift, len, sh);
438
+ r_.shift(shift, MAX_BUF, got, max, buf, zz, 3);
439
+ assert_bytes_at(kRandomLen, &got[0], kRandomID);
440
+ check_cbor_length(got, MAX_BUF, len, 7);
441
+
442
+ const size_t MAX_EI =
443
+ 1 + 17 + 32; // The 32/64 includes len of any headers.
444
+ const size_t MAX_EV = 1 + 12 + 64;
445
+ v8 want_ei[MAX_EI], want_ev[MAX_EV];
446
+ uint8_t ei_bytes[] = {0x60 + 17, 'e', 'l', 'e', 'm', 'e', 'n', 't', 'I',
447
+ 'd', 'e', 'n', 't', 'i', 'f', 'i', 'e', 'r'};
448
+ uint8_t ev_bytes[] = {0x60 + 12, 'e', 'l', 'e', 'm', 'e', 'n',
449
+ 't', 'V', 'a', 'l', 'u', 'e'};
450
+ format_element(want_ei, MAX_EI, ei_bytes, sizeof(ei_bytes), oa.attr, 32);
451
+ format_element(want_ev, MAX_EV, ev_bytes, sizeof(ev_bytes), oa.v1, 64);
452
+
453
+ // "elementIdentifier" checks the full cbor key-value, because it is public.
454
+ mux_offset(2, shift, len, sh);
455
+ r_.shift(shift, MAX_BUF, got, max, buf, zz, 3);
456
+ for (size_t j = 0; j < MAX_EI; ++j) {
457
+ auto ll = lc_.vlt(j, oa.len);
458
+ for (size_t i = 0; i < 8; ++i) {
459
+ auto same = lc_.eq(1, &got[j][i], &want_ei[j][i]);
460
+ lc_.assert_implies(ll, same);
461
+ }
462
+ }
463
+ v8 tmp;
464
+ std::copy(len.begin(), len.begin() + 8, tmp.begin()); // cast vind into v8.
465
+ lc_.vassert_eq(tmp, oa.len);
466
+
467
+ // "elementValue" checks the full cbor key-value, because it is public.
468
+ mux_offset(3, shift, len, sh);
469
+ r_.shift(shift, MAX_BUF, got, max, buf, zz, 3);
470
+ for (size_t j = 0; j < MAX_EV; ++j) {
471
+ auto ll = lc_.vlt(j, oa.vlen);
472
+ for (size_t i = 0; i < 8; ++i) {
473
+ auto same = lc_.eq(1, &got[j][i], &want_ev[j][i]);
474
+ lc_.assert_implies(ll, same);
475
+ }
476
+ }
477
+ std::copy(len.begin(), len.begin() + 8, tmp.begin()); // cast vind into v8.
478
+ lc_.vassert_eq(tmp, oa.vlen);
479
+ }
480
+
481
+ void mux_offset(size_t slot, vind& shift, vind& len,
482
+ const SaltedHash& sh) const {
483
+ vind t[2];
484
+ vind five = lc_.template vbit<kCborIndexBits>(5);
485
+ lc_.vmux(sh.perm[2 * slot + 1], t[0], sh.i2, five);
486
+ lc_.vmux(sh.perm[2 * slot + 1], t[1], sh.i3, sh.i1);
487
+ lc_.vmux(sh.perm[2 * slot], shift, t[1], t[0]);
488
+
489
+ lc_.vmux(sh.perm[2 * slot + 1], t[0], sh.l[2], sh.l[0]);
490
+ lc_.vmux(sh.perm[2 * slot + 1], t[1], sh.l[3], sh.l[1]);
491
+ lc_.vmux(sh.perm[2 * slot], len, t[1], t[0]);
492
+ }
493
+
494
+ // Asserts that the key is equal to the value in big-endian order in buf_be.
495
+ void assert_key(const v256& key, const v8 buf_be[/*32*/]) const {
496
+ v256 m;
497
+ for (size_t i = 0; i < 256; ++i) {
498
+ m[i] = buf_be[31 - (i / 8)][i % 8];
499
+ }
500
+ lc_.vassert_eq(m, key);
501
+ }
502
+
503
+ // The constants below define the prefix of each field that is verified
504
+ // in the MDOC. This string matching approach is substantially faster than
505
+ // parsing the MDOC into cbor, and its soundness analysis provides at least 96
506
+ // bits of static security. These constants differ from similarly named ones
507
+ // in mdoc_constants because they include header bytes; the mdoc_constants
508
+ // values are used for cbor parsing of the raw mdoc, whereas these are used by
509
+ // the circuit.
510
+
511
+ // 69 [text(9)] 76616C696446726F6D [validFrom] C0 [tag(0)] 74 [len 20]
512
+ static constexpr uint8_t kValidFromCheck[] = {
513
+ 0x69, 0x76, 0x61, 0x6C, 0x69, 0x64, 0x46, 0x72, 0x6F, 0x6D, 0xC0, 0x74};
514
+ static constexpr size_t kValidFromLen = sizeof(kValidFromCheck);
515
+
516
+ // 6A [text(10)] 76616C6964556E74696C [validUntil] C0 [tag(0)] 74
517
+ static constexpr uint8_t kValidUntilCheck[] = {0x6A, 0x76, 0x61, 0x6C, 0x69,
518
+ 0x64, 0x55, 0x6E, 0x74, 0x69,
519
+ 0x6C, 0xC0, 0x74};
520
+ static constexpr size_t kValidUntilLen = sizeof(kValidUntilCheck);
521
+
522
+ // 6D text(13) 6465766963654B6579496E666F "deviceKeyInfo"
523
+ // A1 map(1) 69 text(9) 6465766963654B6579 "deviceKey"
524
+ // A4 map(4) 01 02 20 01
525
+ // 21 negative(1) 58 20 bytes(32)
526
+ // <dpkx>
527
+ // 22 negative(2) 58 20 bytes(32)
528
+ // <dpky>
529
+ static constexpr uint8_t kDeviceKeyInfoCheck[] = {
530
+ 0x6D, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x4B, 0x65, 0x79, 0x49,
531
+ 0x6E, 0x66, 0x6F, 0xA1, 0x69, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65,
532
+ 0x4B, 0x65, 0x79, 0xA4, 0x01, 0x02, 0x20, 0x01, 0x21, 0x58, 0x20};
533
+ static constexpr size_t kDeviceKeyInfoLen = sizeof(kDeviceKeyInfoCheck);
534
+ static constexpr size_t kPkxInd = kDeviceKeyInfoLen;
535
+ static constexpr size_t kPkyInd = 68; /* 64 + 3 byte tag + 1*/
536
+
537
+ // 6C text(12) 76616C756544696765737473 "valueDigests" A{1,2} # map(1,2)
538
+ // 71 text(17) 6F72672E69736F2E31383031332E352E31 "org.iso.18013.5.1"
539
+ static constexpr uint8_t kValueDigestsCheck[] = {
540
+ 0x6C, 0x76, 0x61, 0x6C, 0x75, 0x65, 0x44,
541
+ 0x69, 0x67, 0x65, 0x73, 0x74, 0x73,
542
+ };
543
+ static constexpr size_t kValueDigestsLen = sizeof(kValueDigestsCheck);
544
+
545
+ static constexpr size_t kDateLen = 20;
546
+
547
+ const LogicCircuit& lc_;
548
+ Flatsha sha_;
549
+ Routing<LogicCircuit> r_;
550
+ CborByteDecoder<LogicCircuit> cb_;
551
+ };
552
+ } // namespace proofs
553
+
554
+ #endif // PRIVACY_PROOFS_ZK_LIB_CIRCUITS_MDOC_MDOC_HASH_H_