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.
- checksums.yaml +7 -0
- data/CODE_OF_CONDUCT.md +10 -0
- data/LICENSE.txt +21 -0
- data/README.md +152 -0
- data/ext/longfellow/CMakeLists.txt +76 -0
- data/ext/longfellow/extconf.rb +77 -0
- data/lib/longfellow/attribute.rb +65 -0
- data/lib/longfellow/c.rb +105 -0
- data/lib/longfellow/errors.rb +78 -0
- data/lib/longfellow/version.rb +5 -0
- data/lib/longfellow/zk_spec.rb +40 -0
- data/lib/longfellow.rb +162 -0
- data/sig/longfellow.rbs +74 -0
- data/vendor/longfellow-zk/LICENSE +203 -0
- data/vendor/longfellow-zk/lib/algebra/blas.h +121 -0
- data/vendor/longfellow-zk/lib/algebra/bogorng.h +68 -0
- data/vendor/longfellow-zk/lib/algebra/compare.h +40 -0
- data/vendor/longfellow-zk/lib/algebra/convolution.h +219 -0
- data/vendor/longfellow-zk/lib/algebra/crt.cc +42 -0
- data/vendor/longfellow-zk/lib/algebra/crt.h +299 -0
- data/vendor/longfellow-zk/lib/algebra/crt_convolution.h +114 -0
- data/vendor/longfellow-zk/lib/algebra/crt_test.cc +371 -0
- data/vendor/longfellow-zk/lib/algebra/fft.h +104 -0
- data/vendor/longfellow-zk/lib/algebra/fft_interpolation.h +304 -0
- data/vendor/longfellow-zk/lib/algebra/fft_interpolation_test.cc +168 -0
- data/vendor/longfellow-zk/lib/algebra/fft_test.cc +257 -0
- data/vendor/longfellow-zk/lib/algebra/fp.h +59 -0
- data/vendor/longfellow-zk/lib/algebra/fp2.h +240 -0
- data/vendor/longfellow-zk/lib/algebra/fp24.h +342 -0
- data/vendor/longfellow-zk/lib/algebra/fp24_6.h +305 -0
- data/vendor/longfellow-zk/lib/algebra/fp24_6_test.cc +197 -0
- data/vendor/longfellow-zk/lib/algebra/fp2_test.cc +280 -0
- data/vendor/longfellow-zk/lib/algebra/fp_generic.h +533 -0
- data/vendor/longfellow-zk/lib/algebra/fp_p128.h +91 -0
- data/vendor/longfellow-zk/lib/algebra/fp_p256.h +68 -0
- data/vendor/longfellow-zk/lib/algebra/fp_p256k1.h +123 -0
- data/vendor/longfellow-zk/lib/algebra/fp_p384.h +65 -0
- data/vendor/longfellow-zk/lib/algebra/fp_p521.h +62 -0
- data/vendor/longfellow-zk/lib/algebra/fp_test.cc +522 -0
- data/vendor/longfellow-zk/lib/algebra/hash.h +39 -0
- data/vendor/longfellow-zk/lib/algebra/interpolation.h +117 -0
- data/vendor/longfellow-zk/lib/algebra/interpolation_test.cc +74 -0
- data/vendor/longfellow-zk/lib/algebra/limb.h +153 -0
- data/vendor/longfellow-zk/lib/algebra/limb_test.cc +75 -0
- data/vendor/longfellow-zk/lib/algebra/nat.cc +32 -0
- data/vendor/longfellow-zk/lib/algebra/nat.h +212 -0
- data/vendor/longfellow-zk/lib/algebra/nat_test.cc +183 -0
- data/vendor/longfellow-zk/lib/algebra/nussbaumer.h +400 -0
- data/vendor/longfellow-zk/lib/algebra/nussbaumer_test.cc +138 -0
- data/vendor/longfellow-zk/lib/algebra/nussbaumerfp2_test.cc +139 -0
- data/vendor/longfellow-zk/lib/algebra/permutations.h +79 -0
- data/vendor/longfellow-zk/lib/algebra/poly.h +240 -0
- data/vendor/longfellow-zk/lib/algebra/poly_test.cc +123 -0
- data/vendor/longfellow-zk/lib/algebra/reed_solomon.h +150 -0
- data/vendor/longfellow-zk/lib/algebra/reed_solomon_extension.h +108 -0
- data/vendor/longfellow-zk/lib/algebra/reed_solomon_extension_test.cc +76 -0
- data/vendor/longfellow-zk/lib/algebra/reed_solomon_test.cc +473 -0
- data/vendor/longfellow-zk/lib/algebra/rfft.h +400 -0
- data/vendor/longfellow-zk/lib/algebra/rfft_test.cc +102 -0
- data/vendor/longfellow-zk/lib/algebra/static_string.h +29 -0
- data/vendor/longfellow-zk/lib/algebra/sysdep.h +495 -0
- data/vendor/longfellow-zk/lib/algebra/sysdep_test.cc +41 -0
- data/vendor/longfellow-zk/lib/algebra/twiddle.h +59 -0
- data/vendor/longfellow-zk/lib/algebra/utility.h +86 -0
- data/vendor/longfellow-zk/lib/algebra/utility_test.cc +86 -0
- data/vendor/longfellow-zk/lib/arrays/affine.h +56 -0
- data/vendor/longfellow-zk/lib/arrays/affine_test.cc +220 -0
- data/vendor/longfellow-zk/lib/arrays/dense.h +210 -0
- data/vendor/longfellow-zk/lib/arrays/eq.h +75 -0
- data/vendor/longfellow-zk/lib/arrays/eqs.h +137 -0
- data/vendor/longfellow-zk/lib/arrays/eqs_test.cc +151 -0
- data/vendor/longfellow-zk/lib/arrays/sparse.h +192 -0
- data/vendor/longfellow-zk/lib/cbor/host_decoder.h +323 -0
- data/vendor/longfellow-zk/lib/cbor/host_decoder_test.cc +541 -0
- data/vendor/longfellow-zk/lib/circuits/cbor_parser/cbor.h +594 -0
- data/vendor/longfellow-zk/lib/circuits/cbor_parser/cbor_byte_decoder.h +150 -0
- data/vendor/longfellow-zk/lib/circuits/cbor_parser/cbor_byte_decoder_test.cc +147 -0
- data/vendor/longfellow-zk/lib/circuits/cbor_parser/cbor_constants.h +27 -0
- data/vendor/longfellow-zk/lib/circuits/cbor_parser/cbor_pluck.h +110 -0
- data/vendor/longfellow-zk/lib/circuits/cbor_parser/cbor_pluck_test.cc +55 -0
- data/vendor/longfellow-zk/lib/circuits/cbor_parser/cbor_test.cc +174 -0
- data/vendor/longfellow-zk/lib/circuits/cbor_parser/cbor_testing.h +98 -0
- data/vendor/longfellow-zk/lib/circuits/cbor_parser/cbor_witness.h +312 -0
- data/vendor/longfellow-zk/lib/circuits/cbor_parser/mso2_test.cc +662 -0
- data/vendor/longfellow-zk/lib/circuits/cbor_parser/mso_test.cc +485 -0
- data/vendor/longfellow-zk/lib/circuits/cbor_parser/scan.h +104 -0
- data/vendor/longfellow-zk/lib/circuits/cbor_parser/scan_test.cc +137 -0
- data/vendor/longfellow-zk/lib/circuits/cbor_parser_v2/cbor.h +640 -0
- data/vendor/longfellow-zk/lib/circuits/cbor_parser_v2/cbor_byte_decoder.h +150 -0
- data/vendor/longfellow-zk/lib/circuits/cbor_parser_v2/cbor_byte_decoder_test.cc +147 -0
- data/vendor/longfellow-zk/lib/circuits/cbor_parser_v2/cbor_constants.h +27 -0
- data/vendor/longfellow-zk/lib/circuits/cbor_parser_v2/cbor_testing.h +99 -0
- data/vendor/longfellow-zk/lib/circuits/cbor_parser_v2/cbor_witness.h +319 -0
- data/vendor/longfellow-zk/lib/circuits/cbor_parser_v2/lexer_test.cc +120 -0
- data/vendor/longfellow-zk/lib/circuits/cbor_parser_v2/mdoc_examples_test.cc +89 -0
- data/vendor/longfellow-zk/lib/circuits/cbor_parser_v2/parser_circuit_test.cc +506 -0
- data/vendor/longfellow-zk/lib/circuits/cbor_parser_v2/parser_size_test.cc +79 -0
- data/vendor/longfellow-zk/lib/circuits/cbor_parser_v2/parser_test.cc +473 -0
- data/vendor/longfellow-zk/lib/circuits/compiler/canonicalization_test.cc +185 -0
- data/vendor/longfellow-zk/lib/circuits/compiler/circuit_dump.h +65 -0
- data/vendor/longfellow-zk/lib/circuits/compiler/compiler.h +471 -0
- data/vendor/longfellow-zk/lib/circuits/compiler/compiler_test.cc +110 -0
- data/vendor/longfellow-zk/lib/circuits/compiler/node.h +176 -0
- data/vendor/longfellow-zk/lib/circuits/compiler/pdqhash.h +127 -0
- data/vendor/longfellow-zk/lib/circuits/compiler/schedule.h +435 -0
- data/vendor/longfellow-zk/lib/circuits/ecdsa/verify_circuit.h +371 -0
- data/vendor/longfellow-zk/lib/circuits/ecdsa/verify_external_test.cc +246 -0
- data/vendor/longfellow-zk/lib/circuits/ecdsa/verify_test.cc +587 -0
- data/vendor/longfellow-zk/lib/circuits/ecdsa/verify_witness.h +201 -0
- data/vendor/longfellow-zk/lib/circuits/logic/bit_adder.h +140 -0
- data/vendor/longfellow-zk/lib/circuits/logic/bit_adder_test.cc +64 -0
- data/vendor/longfellow-zk/lib/circuits/logic/bit_plucker.h +247 -0
- data/vendor/longfellow-zk/lib/circuits/logic/bit_plucker_constants.h +35 -0
- data/vendor/longfellow-zk/lib/circuits/logic/bit_plucker_encoder.h +72 -0
- data/vendor/longfellow-zk/lib/circuits/logic/bit_plucker_test.cc +183 -0
- data/vendor/longfellow-zk/lib/circuits/logic/compiler_backend.h +62 -0
- data/vendor/longfellow-zk/lib/circuits/logic/counter.h +171 -0
- data/vendor/longfellow-zk/lib/circuits/logic/counter_test.cc +102 -0
- data/vendor/longfellow-zk/lib/circuits/logic/evaluation_backend.h +94 -0
- data/vendor/longfellow-zk/lib/circuits/logic/logic.h +1232 -0
- data/vendor/longfellow-zk/lib/circuits/logic/logic_circuit_test.cc +310 -0
- data/vendor/longfellow-zk/lib/circuits/logic/logic_test.cc +521 -0
- data/vendor/longfellow-zk/lib/circuits/logic/memcmp.h +68 -0
- data/vendor/longfellow-zk/lib/circuits/logic/memcmp_test.cc +148 -0
- data/vendor/longfellow-zk/lib/circuits/logic/polynomial.h +94 -0
- data/vendor/longfellow-zk/lib/circuits/logic/polynomial_test.cc +62 -0
- data/vendor/longfellow-zk/lib/circuits/logic/routing.h +445 -0
- data/vendor/longfellow-zk/lib/circuits/logic/routing_test.cc +241 -0
- data/vendor/longfellow-zk/lib/circuits/logic/unary.h +55 -0
- data/vendor/longfellow-zk/lib/circuits/logic/unary_plucker.h +77 -0
- data/vendor/longfellow-zk/lib/circuits/logic/unary_plucker_constants.h +37 -0
- data/vendor/longfellow-zk/lib/circuits/logic/unary_plucker_test.cc +53 -0
- data/vendor/longfellow-zk/lib/circuits/logic/unary_size_test.cc +69 -0
- data/vendor/longfellow-zk/lib/circuits/logic/unary_test.cc +62 -0
- data/vendor/longfellow-zk/lib/circuits/mac/mac_circuit.h +193 -0
- data/vendor/longfellow-zk/lib/circuits/mac/mac_circuit_test.cc +223 -0
- data/vendor/longfellow-zk/lib/circuits/mac/mac_reference.h +72 -0
- data/vendor/longfellow-zk/lib/circuits/mac/mac_witness.h +94 -0
- data/vendor/longfellow-zk/lib/circuits/mdoc/circuit_maker.cc +242 -0
- data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_attribute_ids.h +311 -0
- data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_attribute_test.cc +64 -0
- data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_circuit_id.cc +85 -0
- data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_constants.h +85 -0
- data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_decompress.cc +41 -0
- data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_decompress.h +27 -0
- data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_examples.h +5232 -0
- data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_generate_circuit.cc +199 -0
- data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_hash.h +554 -0
- data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_signature.h +143 -0
- data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_signature_test.cc +444 -0
- data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_test_attributes.h +157 -0
- data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_witness.h +863 -0
- data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_zk.cc +693 -0
- data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_zk.h +216 -0
- data/vendor/longfellow-zk/lib/circuits/mdoc/mdoc_zk_test.cc +724 -0
- data/vendor/longfellow-zk/lib/circuits/mdoc/zk_spec.cc +100 -0
- data/vendor/longfellow-zk/lib/circuits/mdoc/zk_spec_test.cc +155 -0
- data/vendor/longfellow-zk/lib/circuits/sha/flatsha256_circuit.h +330 -0
- data/vendor/longfellow-zk/lib/circuits/sha/flatsha256_circuit_test.cc +607 -0
- data/vendor/longfellow-zk/lib/circuits/sha/flatsha256_io.h +26 -0
- data/vendor/longfellow-zk/lib/circuits/sha/flatsha256_witness.cc +163 -0
- data/vendor/longfellow-zk/lib/circuits/sha/flatsha256_witness.h +47 -0
- data/vendor/longfellow-zk/lib/circuits/sha/sha256_constants.cc +34 -0
- data/vendor/longfellow-zk/lib/circuits/sha/sha256_constants.h +27 -0
- data/vendor/longfellow-zk/lib/circuits/sha/sha256_test_values.h +389 -0
- data/vendor/longfellow-zk/lib/circuits/tests/anoncred/ptrcred.h +171 -0
- data/vendor/longfellow-zk/lib/circuits/tests/anoncred/small.h +218 -0
- data/vendor/longfellow-zk/lib/circuits/tests/anoncred/small_examples.h +118 -0
- data/vendor/longfellow-zk/lib/circuits/tests/anoncred/small_io.h +25 -0
- data/vendor/longfellow-zk/lib/circuits/tests/anoncred/small_test.cc +208 -0
- data/vendor/longfellow-zk/lib/circuits/tests/anoncred/small_witness.h +130 -0
- data/vendor/longfellow-zk/lib/circuits/tests/base64/decode.h +508 -0
- data/vendor/longfellow-zk/lib/circuits/tests/base64/decode_circuit_test.cc +95 -0
- data/vendor/longfellow-zk/lib/circuits/tests/base64/decode_test.cc +119 -0
- data/vendor/longfellow-zk/lib/circuits/tests/base64/decode_util.cc +47 -0
- data/vendor/longfellow-zk/lib/circuits/tests/base64/decode_util.h +29 -0
- data/vendor/longfellow-zk/lib/circuits/tests/ec/pk_circuit.h +231 -0
- data/vendor/longfellow-zk/lib/circuits/tests/ec/pk_circuit_test.cc +428 -0
- data/vendor/longfellow-zk/lib/circuits/tests/ec/pk_witness.h +102 -0
- data/vendor/longfellow-zk/lib/circuits/tests/jwt/jwt.h +190 -0
- data/vendor/longfellow-zk/lib/circuits/tests/jwt/jwt_constants.h +26 -0
- data/vendor/longfellow-zk/lib/circuits/tests/jwt/jwt_test.cc +559 -0
- data/vendor/longfellow-zk/lib/circuits/tests/jwt/jwt_witness.h +315 -0
- data/vendor/longfellow-zk/lib/circuits/tests/mdoc/mdoc_1f.h +411 -0
- data/vendor/longfellow-zk/lib/circuits/tests/mdoc/mdoc_1f_io.h +32 -0
- data/vendor/longfellow-zk/lib/circuits/tests/mdoc/mdoc_1f_test.cc +364 -0
- data/vendor/longfellow-zk/lib/circuits/tests/mdoc/mdoc_1f_witness.h +278 -0
- data/vendor/longfellow-zk/lib/circuits/tests/mdoc/mdoc_revocation.h +146 -0
- data/vendor/longfellow-zk/lib/circuits/tests/mdoc/mdoc_revocation_constants.h +25 -0
- data/vendor/longfellow-zk/lib/circuits/tests/mdoc/mdoc_revocation_test.cc +315 -0
- data/vendor/longfellow-zk/lib/circuits/tests/mdoc/mdoc_revocation_witness.h +136 -0
- data/vendor/longfellow-zk/lib/circuits/tests/pq/bitaddr/bitaddr.h +250 -0
- data/vendor/longfellow-zk/lib/circuits/tests/pq/bitaddr/bitaddr_test.cc +333 -0
- data/vendor/longfellow-zk/lib/circuits/tests/pq/bitaddr/bitaddr_witness.h +152 -0
- data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/ml_dsa_44.h +903 -0
- data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/ml_dsa_44_circuit_test.cc +274 -0
- data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/ml_dsa_44_eval_test.cc +440 -0
- data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/ml_dsa_44_examples.cc +8851 -0
- data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/ml_dsa_44_examples.h +93 -0
- data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/ml_dsa_44_types.cc +24 -0
- data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/ml_dsa_44_types.h +118 -0
- data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/ml_dsa_44_witness.h +453 -0
- data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/ml_dsa_44_witness_test.cc +49 -0
- data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/ml_dsa_ref.cc +458 -0
- data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/ml_dsa_ref.h +150 -0
- data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/ml_dsa_ref_test.cc +398 -0
- data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/ml_dsa_ref_test_vectors.inc +3618 -0
- data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/ml_dsa_ref_test_vectors_pkdecode.inc +689 -0
- data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/ml_dsa_ref_test_vectors_sigdecode.inc +1501 -0
- data/vendor/longfellow-zk/lib/circuits/tests/pq/ml_dsa/sigdecode_test_vectors.inc +540 -0
- data/vendor/longfellow-zk/lib/circuits/tests/ripemd/ripemd_circuit.h +394 -0
- data/vendor/longfellow-zk/lib/circuits/tests/ripemd/ripemd_circuit_test.cc +577 -0
- data/vendor/longfellow-zk/lib/circuits/tests/ripemd/ripemd_constants.h +90 -0
- data/vendor/longfellow-zk/lib/circuits/tests/ripemd/ripemd_witness.cc +174 -0
- data/vendor/longfellow-zk/lib/circuits/tests/ripemd/ripemd_witness.h +140 -0
- data/vendor/longfellow-zk/lib/circuits/tests/sha3/sha3_circuit.h +351 -0
- data/vendor/longfellow-zk/lib/circuits/tests/sha3/sha3_circuit_test.cc +466 -0
- data/vendor/longfellow-zk/lib/circuits/tests/sha3/sha3_reference.cc +207 -0
- data/vendor/longfellow-zk/lib/circuits/tests/sha3/sha3_reference.h +59 -0
- data/vendor/longfellow-zk/lib/circuits/tests/sha3/sha3_reference_test.cc +153 -0
- data/vendor/longfellow-zk/lib/circuits/tests/sha3/sha3_round_constants.cc +39 -0
- data/vendor/longfellow-zk/lib/circuits/tests/sha3/sha3_round_constants.h +29 -0
- data/vendor/longfellow-zk/lib/circuits/tests/sha3/sha3_slicing.h +31 -0
- data/vendor/longfellow-zk/lib/circuits/tests/sha3/sha3_witness.cc +83 -0
- data/vendor/longfellow-zk/lib/circuits/tests/sha3/sha3_witness.h +72 -0
- data/vendor/longfellow-zk/lib/circuits/tests/sha3/shake_test_vectors.h +477 -0
- data/vendor/longfellow-zk/lib/ec/elliptic_curve.h +596 -0
- data/vendor/longfellow-zk/lib/ec/elliptic_curve_test.cc +548 -0
- data/vendor/longfellow-zk/lib/ec/p256.cc +36 -0
- data/vendor/longfellow-zk/lib/ec/p256.h +60 -0
- data/vendor/longfellow-zk/lib/ec/p256k1.cc +34 -0
- data/vendor/longfellow-zk/lib/ec/p256k1.h +60 -0
- data/vendor/longfellow-zk/lib/gf2k/gf2_128.h +503 -0
- data/vendor/longfellow-zk/lib/gf2k/gf2_128_bench.cc +48 -0
- data/vendor/longfellow-zk/lib/gf2k/gf2_128_test.cc +416 -0
- data/vendor/longfellow-zk/lib/gf2k/gf2poly.h +74 -0
- data/vendor/longfellow-zk/lib/gf2k/lch14.h +242 -0
- data/vendor/longfellow-zk/lib/gf2k/lch14_bench.cc +75 -0
- data/vendor/longfellow-zk/lib/gf2k/lch14_reed_solomon.h +127 -0
- data/vendor/longfellow-zk/lib/gf2k/lch14_reed_solomon_test.cc +110 -0
- data/vendor/longfellow-zk/lib/gf2k/lch14_test.cc +246 -0
- data/vendor/longfellow-zk/lib/gf2k/sysdep.h +329 -0
- data/vendor/longfellow-zk/lib/ligero/ligero_param.h +449 -0
- data/vendor/longfellow-zk/lib/ligero/ligero_prover.h +354 -0
- data/vendor/longfellow-zk/lib/ligero/ligero_test.cc +136 -0
- data/vendor/longfellow-zk/lib/ligero/ligero_transcript.h +67 -0
- data/vendor/longfellow-zk/lib/ligero/ligero_verifier.h +272 -0
- data/vendor/longfellow-zk/lib/merkle/merkle_commitment.h +104 -0
- data/vendor/longfellow-zk/lib/merkle/merkle_tree.h +216 -0
- data/vendor/longfellow-zk/lib/merkle/merkle_tree_test.cc +240 -0
- data/vendor/longfellow-zk/lib/proto/circuit.h +354 -0
- data/vendor/longfellow-zk/lib/proto/circuit_test.cc +202 -0
- data/vendor/longfellow-zk/lib/random/random.h +119 -0
- data/vendor/longfellow-zk/lib/random/random_test.cc +189 -0
- data/vendor/longfellow-zk/lib/random/secure_random_engine.h +37 -0
- data/vendor/longfellow-zk/lib/random/transcript.h +193 -0
- data/vendor/longfellow-zk/lib/random/transcript_test.cc +344 -0
- data/vendor/longfellow-zk/lib/sumcheck/circuit.h +148 -0
- data/vendor/longfellow-zk/lib/sumcheck/circuit_id.h +71 -0
- data/vendor/longfellow-zk/lib/sumcheck/equad.h +126 -0
- data/vendor/longfellow-zk/lib/sumcheck/hquad.h +115 -0
- data/vendor/longfellow-zk/lib/sumcheck/prover.h +59 -0
- data/vendor/longfellow-zk/lib/sumcheck/prover_layers.h +362 -0
- data/vendor/longfellow-zk/lib/sumcheck/quad.h +227 -0
- data/vendor/longfellow-zk/lib/sumcheck/quad_builder.h +211 -0
- data/vendor/longfellow-zk/lib/sumcheck/quad_test.cc +169 -0
- data/vendor/longfellow-zk/lib/sumcheck/sumcheck_test.cc +324 -0
- data/vendor/longfellow-zk/lib/sumcheck/testing.h +69 -0
- data/vendor/longfellow-zk/lib/sumcheck/transcript_sumcheck.h +85 -0
- data/vendor/longfellow-zk/lib/sumcheck/verifier.h +84 -0
- data/vendor/longfellow-zk/lib/sumcheck/verifier_layers.h +221 -0
- data/vendor/longfellow-zk/lib/testing/test_main.cc +50 -0
- data/vendor/longfellow-zk/lib/util/ceildiv.h +164 -0
- data/vendor/longfellow-zk/lib/util/ceildiv_test.cc +152 -0
- data/vendor/longfellow-zk/lib/util/crc64.h +45 -0
- data/vendor/longfellow-zk/lib/util/crypto.cc +39 -0
- data/vendor/longfellow-zk/lib/util/crypto.h +108 -0
- data/vendor/longfellow-zk/lib/util/log.cc +110 -0
- data/vendor/longfellow-zk/lib/util/log.h +33 -0
- data/vendor/longfellow-zk/lib/util/panic.h +40 -0
- data/vendor/longfellow-zk/lib/util/readbuffer.h +67 -0
- data/vendor/longfellow-zk/lib/util/serialization.h +54 -0
- data/vendor/longfellow-zk/lib/zk/zk_common.h +455 -0
- data/vendor/longfellow-zk/lib/zk/zk_proof.h +378 -0
- data/vendor/longfellow-zk/lib/zk/zk_prover.h +202 -0
- data/vendor/longfellow-zk/lib/zk/zk_test.cc +340 -0
- data/vendor/longfellow-zk/lib/zk/zk_testing.h +154 -0
- data/vendor/longfellow-zk/lib/zk/zk_verifier.h +109 -0
- metadata +347 -0
|
@@ -0,0 +1,74 @@
|
|
|
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 "algebra/interpolation.h"
|
|
16
|
+
|
|
17
|
+
#include <cstddef>
|
|
18
|
+
|
|
19
|
+
#include "algebra/fp.h"
|
|
20
|
+
#include "algebra/poly.h"
|
|
21
|
+
#include "gtest/gtest.h"
|
|
22
|
+
|
|
23
|
+
namespace proofs {
|
|
24
|
+
namespace {
|
|
25
|
+
using Field = Fp<1>;
|
|
26
|
+
using Elt = typename Field::Elt;
|
|
27
|
+
static constexpr size_t N = 37;
|
|
28
|
+
|
|
29
|
+
using Interpolation = Interpolation<N, Field>;
|
|
30
|
+
using Poly = Poly<N, Field>;
|
|
31
|
+
const Field F("18446744073709551557");
|
|
32
|
+
|
|
33
|
+
TEST(Interpolation, Simple) {
|
|
34
|
+
Poly X, M;
|
|
35
|
+
|
|
36
|
+
// arbitrary points and coefficients
|
|
37
|
+
for (size_t i = 0; i < N; ++i) {
|
|
38
|
+
X[i] = F.of_scalar(i * i + 3 * i + 37);
|
|
39
|
+
M[i] = F.of_scalar(i * i * i + (i & 0xF) + (i ^ (i << 2)));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// lagrange basis
|
|
43
|
+
Poly L;
|
|
44
|
+
for (size_t i = 0; i < N; ++i) {
|
|
45
|
+
L[i] = Interpolation::eval_monomial(M, X[i], F);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// newton basis
|
|
49
|
+
auto Newton = Interpolation::newton_of_lagrange(L, X, F);
|
|
50
|
+
|
|
51
|
+
// evaluation in newton and monomial bases must agree
|
|
52
|
+
for (size_t i = 0; i < 1000; ++i) {
|
|
53
|
+
Elt x = F.of_scalar(i);
|
|
54
|
+
EXPECT_EQ(Interpolation::eval_newton(Newton, X, x, F),
|
|
55
|
+
Interpolation::eval_monomial(M, x, F));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
auto M2 = Interpolation::monomial_of_newton(Newton, X, F);
|
|
59
|
+
|
|
60
|
+
// monomial coefficients must agree
|
|
61
|
+
for (size_t i = 0; i < N; ++i) {
|
|
62
|
+
EXPECT_EQ(M[i], M2[i]);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
auto M3 = Interpolation::monomial_of_lagrange(L, X, F);
|
|
66
|
+
|
|
67
|
+
// monomial coefficients must agree
|
|
68
|
+
for (size_t i = 0; i < N; ++i) {
|
|
69
|
+
EXPECT_EQ(M[i], M3[i]);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
} // namespace
|
|
73
|
+
} // namespace proofs
|
|
74
|
+
|
|
@@ -0,0 +1,153 @@
|
|
|
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_ALGEBRA_LIMB_H_
|
|
16
|
+
#define PRIVACY_PROOFS_ZK_LIB_ALGEBRA_LIMB_H_
|
|
17
|
+
|
|
18
|
+
#include <array>
|
|
19
|
+
#include <cstddef>
|
|
20
|
+
#include <cstdint>
|
|
21
|
+
|
|
22
|
+
#include "util/serialization.h"
|
|
23
|
+
|
|
24
|
+
namespace proofs {
|
|
25
|
+
|
|
26
|
+
// Base class for representing bignum or bigpoly as arrays of
|
|
27
|
+
// machine-dependent "limbs". The serialization is in this
|
|
28
|
+
// class; arithmetic is in subclasses.
|
|
29
|
+
|
|
30
|
+
template <size_t W64>
|
|
31
|
+
class Limb {
|
|
32
|
+
public:
|
|
33
|
+
using T = Limb<W64>;
|
|
34
|
+
|
|
35
|
+
#if __WORDSIZE == 64
|
|
36
|
+
// Use the native word size as the limb size. However, changing
|
|
37
|
+
// limb_t to uint32_t is expected to work at least on x86_64, as a
|
|
38
|
+
// way to test 32-bit arithmetic without cross-compiling
|
|
39
|
+
using limb_t = uint64_t;
|
|
40
|
+
#else
|
|
41
|
+
using limb_t = uint32_t;
|
|
42
|
+
#endif
|
|
43
|
+
|
|
44
|
+
// sizes in bytes, bits, limbs, uint64_t
|
|
45
|
+
static constexpr size_t kBytes = 8 * W64;
|
|
46
|
+
static constexpr size_t kBits = 64 * W64;
|
|
47
|
+
static constexpr size_t kLimbs = kBytes / sizeof(limb_t);
|
|
48
|
+
static constexpr size_t kU64 = W64;
|
|
49
|
+
static constexpr size_t kBitsPerLimb = 8 * sizeof(limb_t);
|
|
50
|
+
|
|
51
|
+
// no rounding allowed
|
|
52
|
+
static_assert(kLimbs * sizeof(limb_t) == kBytes);
|
|
53
|
+
|
|
54
|
+
limb_t limb_[kLimbs];
|
|
55
|
+
|
|
56
|
+
Limb() = default; // uninitialized
|
|
57
|
+
explicit Limb(uint64_t x) : limb_{} { assign(limb_, 1, &x); }
|
|
58
|
+
|
|
59
|
+
explicit Limb(const std::array<uint64_t, kU64>& a) : limb_{} {
|
|
60
|
+
assign(limb_, kU64, &a[0]);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
std::array<uint64_t, kU64> u64() const {
|
|
64
|
+
std::array<uint64_t, kU64> a;
|
|
65
|
+
unassign(limb_, kU64, &a[0]);
|
|
66
|
+
return a;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
void to_bytes(uint8_t a[/* kBytes */]) const {
|
|
70
|
+
for (size_t i = 0; i < kLimbs; ++i) {
|
|
71
|
+
a = to_bytes(&limb_[i], a);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
bool operator==(const T& other) const {
|
|
76
|
+
for (size_t i = 0; i < kLimbs; ++i) {
|
|
77
|
+
if (limb_[i] != other.limb_[i]) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
bool operator!=(const T& other) const { return !(operator==(other)); }
|
|
84
|
+
|
|
85
|
+
// Shift right by z. Return the bits that fall off
|
|
86
|
+
// the edge.
|
|
87
|
+
limb_t shiftr(size_t z) {
|
|
88
|
+
limb_t c = 0;
|
|
89
|
+
for (size_t i = kLimbs; i-- > 0;) {
|
|
90
|
+
limb_t d = limb_[i];
|
|
91
|
+
limb_[i] = c | (d >> z);
|
|
92
|
+
c = d << (kBitsPerLimb - z);
|
|
93
|
+
}
|
|
94
|
+
return c;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Returns the pos-th bit in the representation of this nat.
|
|
98
|
+
limb_t bit(size_t pos) const {
|
|
99
|
+
size_t ind = pos / kBitsPerLimb;
|
|
100
|
+
if (ind < kLimbs) {
|
|
101
|
+
size_t off = pos % kBitsPerLimb;
|
|
102
|
+
return (limb_[ind] >> off) & 0x1u;
|
|
103
|
+
}
|
|
104
|
+
return 0;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
protected:
|
|
108
|
+
static void assign(uint64_t d[], size_t ns, const uint64_t s[/*ns*/]) {
|
|
109
|
+
for (size_t i = 0; i < ns; ++i) {
|
|
110
|
+
d[i] = s[i];
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
static void assign(uint32_t d[], size_t ns, const uint64_t s[/*ns*/]) {
|
|
115
|
+
for (size_t i = 0; i < ns; ++i) {
|
|
116
|
+
d[2 * i] = static_cast<uint32_t>(s[i]);
|
|
117
|
+
d[2 * i + 1] = static_cast<uint32_t>(s[i] >> 32);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
static void unassign(const uint64_t d[], size_t ns, uint64_t s[/*ns*/]) {
|
|
122
|
+
for (size_t i = 0; i < ns; ++i) {
|
|
123
|
+
s[i] = d[i];
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
static void unassign(const uint32_t d[], size_t ns, uint64_t s[/*ns*/]) {
|
|
128
|
+
for (size_t i = 0; i < ns; ++i) {
|
|
129
|
+
s[i] = d[2 * i] | (static_cast<uint64_t>(d[2 * i + 1]) << 32);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
static const uint8_t* of_bytes(uint64_t* r, const uint8_t* a) {
|
|
134
|
+
*r = u64_of_le(a);
|
|
135
|
+
return a + 8;
|
|
136
|
+
}
|
|
137
|
+
static const uint8_t* of_bytes(uint32_t* r, const uint8_t* a) {
|
|
138
|
+
*r = u32_of_le(a);
|
|
139
|
+
return a + 4;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
static uint8_t* to_bytes(const uint64_t* r, uint8_t* a) {
|
|
143
|
+
u64_to_le(a, *r);
|
|
144
|
+
return a + 8;
|
|
145
|
+
}
|
|
146
|
+
static uint8_t* to_bytes(const uint32_t* r, uint8_t* a) {
|
|
147
|
+
u32_to_le(a, *r);
|
|
148
|
+
return a + 4;
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
} // namespace proofs
|
|
152
|
+
|
|
153
|
+
#endif // PRIVACY_PROOFS_ZK_LIB_ALGEBRA_LIMB_H_
|
|
@@ -0,0 +1,75 @@
|
|
|
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 "algebra/limb.h"
|
|
16
|
+
|
|
17
|
+
#include <array>
|
|
18
|
+
#include <cstdint>
|
|
19
|
+
#include <cstdlib>
|
|
20
|
+
|
|
21
|
+
#include "gtest/gtest.h"
|
|
22
|
+
|
|
23
|
+
namespace proofs {
|
|
24
|
+
namespace {
|
|
25
|
+
TEST(Limb, Scalar) {
|
|
26
|
+
constexpr size_t W = 4;
|
|
27
|
+
Limb<W> k42 = Limb<W>(42);
|
|
28
|
+
EXPECT_EQ(k42, k42);
|
|
29
|
+
|
|
30
|
+
auto k42u64 = k42.u64();
|
|
31
|
+
EXPECT_EQ(k42u64[0], 42u);
|
|
32
|
+
for (size_t i = 1; i < 4; ++i) {
|
|
33
|
+
EXPECT_EQ(k42u64[i], 0u);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
uint8_t bytes[32];
|
|
37
|
+
k42.to_bytes(bytes);
|
|
38
|
+
EXPECT_EQ(bytes[0], 42);
|
|
39
|
+
for (size_t i = 1; i < 32; ++i) {
|
|
40
|
+
EXPECT_EQ(bytes[i], 0u);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
TEST(Limb, Array) {
|
|
45
|
+
constexpr size_t W = 4;
|
|
46
|
+
std::array<uint64_t, W> k = {
|
|
47
|
+
0x0706050403020100ull,
|
|
48
|
+
0x0f0e0d0c0b0a0908ull,
|
|
49
|
+
0x1716151413121110ull,
|
|
50
|
+
0x1f1e1d1c1b1a1918ull,
|
|
51
|
+
};
|
|
52
|
+
Limb<W> kk = Limb<W>(k);
|
|
53
|
+
EXPECT_EQ(kk, kk);
|
|
54
|
+
|
|
55
|
+
auto kku64 = kk.u64();
|
|
56
|
+
for (size_t i = 0; i < 4; ++i) {
|
|
57
|
+
EXPECT_EQ(kku64[i], k[i]);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
uint8_t bytes[32];
|
|
61
|
+
kk.to_bytes(bytes);
|
|
62
|
+
for (size_t i = 0; i < 32; ++i) {
|
|
63
|
+
EXPECT_EQ(bytes[i], i);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
kk.shiftr(8);
|
|
67
|
+
kk.to_bytes(bytes);
|
|
68
|
+
for (size_t i = 0; i < 31; ++i) {
|
|
69
|
+
EXPECT_EQ(bytes[i], i + 1);
|
|
70
|
+
}
|
|
71
|
+
EXPECT_EQ(bytes[31], 0u);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
} // namespace
|
|
75
|
+
} // namespace proofs
|
|
@@ -0,0 +1,32 @@
|
|
|
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 "algebra/nat.h"
|
|
16
|
+
|
|
17
|
+
#include "util/panic.h"
|
|
18
|
+
|
|
19
|
+
namespace proofs {
|
|
20
|
+
unsigned digit(char c) {
|
|
21
|
+
if (c >= '0' && c <= '9') {
|
|
22
|
+
return c - '0';
|
|
23
|
+
} else if (c >= 'a' && c <= 'f') {
|
|
24
|
+
return c - 'a' + 10;
|
|
25
|
+
} else if (c >= 'A' && c <= 'F') {
|
|
26
|
+
return c - 'A' + 10;
|
|
27
|
+
} else {
|
|
28
|
+
check(false, "malformed numeral in digit()");
|
|
29
|
+
return 0; // silence compiler warning
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
} // namespace proofs
|
|
@@ -0,0 +1,212 @@
|
|
|
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_ALGEBRA_NAT_H_
|
|
16
|
+
#define PRIVACY_PROOFS_ZK_LIB_ALGEBRA_NAT_H_
|
|
17
|
+
|
|
18
|
+
#include <array>
|
|
19
|
+
#include <cctype>
|
|
20
|
+
#include <cstddef>
|
|
21
|
+
#include <cstdint>
|
|
22
|
+
#include <optional>
|
|
23
|
+
|
|
24
|
+
#include "algebra/limb.h"
|
|
25
|
+
#include "algebra/static_string.h"
|
|
26
|
+
#include "algebra/sysdep.h"
|
|
27
|
+
#include "util/panic.h"
|
|
28
|
+
|
|
29
|
+
namespace proofs {
|
|
30
|
+
|
|
31
|
+
// return a^-1 mod 2^L where L is the number of bits in limb_t
|
|
32
|
+
template <class limb_t>
|
|
33
|
+
static limb_t inv_mod_b(limb_t a) {
|
|
34
|
+
// Let v=1-a. We have 1/a=1/(1-v)=1+v+v^2+..., or
|
|
35
|
+
// 1/a=(1+v)(1+v^2)(1+v^4)... At some point v^(2^k) becomes 0 mod
|
|
36
|
+
// 2^L because v is even.
|
|
37
|
+
|
|
38
|
+
// A more complicated variant of this idea appears in Dumas,
|
|
39
|
+
// J.G. "On Newton–Raphson Iteration for Multiplicative Inverses
|
|
40
|
+
// Modulo Prime Powers", Algorithm 3, where they use v'=a-1
|
|
41
|
+
// instead of v=1-a, and so the first term needs to be handled
|
|
42
|
+
// separately as 2-a instead of 1+v, breaking the uniformity of
|
|
43
|
+
// the algorithm. The sign difference disappears after the first
|
|
44
|
+
// squaring.
|
|
45
|
+
check((a & 1) != 0, "even A in inv_mod_b()");
|
|
46
|
+
|
|
47
|
+
limb_t v = 1u - a;
|
|
48
|
+
limb_t u = 1u;
|
|
49
|
+
while (v != 0) {
|
|
50
|
+
u *= (1u + v);
|
|
51
|
+
v *= v;
|
|
52
|
+
}
|
|
53
|
+
return u;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// This function should only be called on static input known at compile time.
|
|
57
|
+
unsigned digit(char c);
|
|
58
|
+
|
|
59
|
+
template <size_t W64>
|
|
60
|
+
class Nat : public Limb<W64> {
|
|
61
|
+
public:
|
|
62
|
+
using Super = Limb<W64>;
|
|
63
|
+
using T = Nat<W64>;
|
|
64
|
+
using limb_t = typename Super::limb_t;
|
|
65
|
+
using Super::kLimbs;
|
|
66
|
+
using Super::kU64;
|
|
67
|
+
using Super::limb_;
|
|
68
|
+
|
|
69
|
+
// Maximum length for an untrusted string, 2^64 ~ 20 decimal digits.
|
|
70
|
+
static constexpr size_t kMaxStringLen = 20 * W64 + 1;
|
|
71
|
+
|
|
72
|
+
Nat() = default; // uninitialized
|
|
73
|
+
explicit Nat(uint64_t x) : Super(x) {}
|
|
74
|
+
|
|
75
|
+
explicit Nat(const std::array<uint64_t, kU64>& a) : Super(a) {}
|
|
76
|
+
|
|
77
|
+
// Pre-condition: the caller of this function must check that the string
|
|
78
|
+
// s is either a valid base-10 or base-16 representation of a natural number
|
|
79
|
+
// that does not overflow the representation.
|
|
80
|
+
// In our current implementation, this method is only used on static strings.
|
|
81
|
+
explicit Nat(const StaticString& ss) : Super(0) {
|
|
82
|
+
limb_t base = 10u;
|
|
83
|
+
const char* s = ss.as_pointer;
|
|
84
|
+
if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {
|
|
85
|
+
s += 2;
|
|
86
|
+
base = 16u;
|
|
87
|
+
}
|
|
88
|
+
for (; *s; s++) {
|
|
89
|
+
T d(digit(*s));
|
|
90
|
+
bool ok = muls(limb_, base);
|
|
91
|
+
check(ok, "overflow in nat(const char *s)");
|
|
92
|
+
limb_t ah = add_limb(kLimbs, limb_, d.limb_);
|
|
93
|
+
check(ah == 0, "overflow in nat(const char *s)");
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
template <size_t LEN>
|
|
98
|
+
explicit Nat(const char (&p)[LEN]) : Nat(StaticString(p)) {}
|
|
99
|
+
|
|
100
|
+
// Interpret A[] as a little-endian nat
|
|
101
|
+
static T of_bytes(const uint8_t a[/* kBytes */]) {
|
|
102
|
+
T r;
|
|
103
|
+
for (size_t i = 0; i < kLimbs; ++i) {
|
|
104
|
+
a = Super::of_bytes(&r.limb_[i], a);
|
|
105
|
+
}
|
|
106
|
+
return r;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Interpret A[] as a little-endian nat, masking the top bits to
|
|
110
|
+
// return a value in the range [0, 2^nbits - 1].
|
|
111
|
+
static T of_bytes(const uint8_t a[], size_t nbits) {
|
|
112
|
+
T r;
|
|
113
|
+
for (size_t i = 0; i < kLimbs; ++i) {
|
|
114
|
+
a = Super::of_bytes(&r.limb_[i], a);
|
|
115
|
+
if (nbits >= Super::kBitsPerLimb) {
|
|
116
|
+
nbits -= Super::kBitsPerLimb;
|
|
117
|
+
} else {
|
|
118
|
+
r.limb_[i] &= (limb_t{1} << nbits) - 1;
|
|
119
|
+
nbits = 0;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return r;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
static std::optional<unsigned> safe_digit(char c, limb_t base) {
|
|
126
|
+
c = tolower(c);
|
|
127
|
+
if (c >= '0' && c <= '9') {
|
|
128
|
+
return c - '0';
|
|
129
|
+
} else if (base == 16u && c >= 'a' && c <= 'f') {
|
|
130
|
+
return c - 'a' + 10;
|
|
131
|
+
}
|
|
132
|
+
return std::nullopt;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
static std::optional<T> of_untrusted_string(const char* s) {
|
|
136
|
+
T r(0);
|
|
137
|
+
limb_t base = 10u;
|
|
138
|
+
if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {
|
|
139
|
+
s += 2;
|
|
140
|
+
base = 16u;
|
|
141
|
+
}
|
|
142
|
+
const char* p = s;
|
|
143
|
+
for (size_t len = 0; len < kMaxStringLen && *p; ++len, ++p) {
|
|
144
|
+
auto d = safe_digit(*p, base);
|
|
145
|
+
if (!d.has_value()) {
|
|
146
|
+
return std::nullopt;
|
|
147
|
+
}
|
|
148
|
+
T td(d.value());
|
|
149
|
+
if (!muls(r.limb_, base)) {
|
|
150
|
+
return std::nullopt;
|
|
151
|
+
}
|
|
152
|
+
limb_t ah = add_limb(kLimbs, r.limb_, td.limb_);
|
|
153
|
+
if (ah != 0) {
|
|
154
|
+
return std::nullopt;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
// If the loop terminates due to the length limit, then the string is not
|
|
158
|
+
// a valid base-10 or base-16 representation of a natural number.
|
|
159
|
+
if (*p) {
|
|
160
|
+
return std::nullopt;
|
|
161
|
+
}
|
|
162
|
+
return r;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
bool operator<(const T& y) const {
|
|
166
|
+
T b = *this;
|
|
167
|
+
limb_t bh = sub_limb(kLimbs, b.limb_, y.limb_);
|
|
168
|
+
return (bh != 0);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
T& add(const T& y) {
|
|
172
|
+
(void)add_limb(kLimbs, limb_, y.limb_);
|
|
173
|
+
return *this;
|
|
174
|
+
}
|
|
175
|
+
T& sub(const T& y) {
|
|
176
|
+
(void)sub_limb(kLimbs, limb_, y.limb_);
|
|
177
|
+
return *this;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// *this += x * y
|
|
181
|
+
template <size_t WX, size_t WY>
|
|
182
|
+
T& mac(const Nat<WX>& x, const Nat<WY>& y) {
|
|
183
|
+
constexpr size_t kLimbsX = Nat<WX>::kLimbs;
|
|
184
|
+
constexpr size_t kLimbsY = Nat<WY>::kLimbs;
|
|
185
|
+
static_assert(kLimbs >= kLimbsX + kLimbsY);
|
|
186
|
+
if (WX > WY) {
|
|
187
|
+
return mac(y, x);
|
|
188
|
+
} else {
|
|
189
|
+
// WX <= WY, outer loop on WX
|
|
190
|
+
for (size_t i = 0; i < kLimbsX; ++i) {
|
|
191
|
+
limb_t l[kLimbsY], h[kLimbsY];
|
|
192
|
+
mulhl(kLimbsY, l, h, x.limb_[i], y.limb_);
|
|
193
|
+
accum(kLimbs - i, limb_ + i, kLimbsY, l);
|
|
194
|
+
accum(kLimbs - i - 1, limb_ + i + 1, kLimbsY, h);
|
|
195
|
+
}
|
|
196
|
+
return *this;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
private:
|
|
201
|
+
// b *= a, returns false if overflow occurred.
|
|
202
|
+
static bool muls(limb_t b[kLimbs], limb_t a) {
|
|
203
|
+
limb_t h[kLimbs];
|
|
204
|
+
mulhl(kLimbs, b, h, a, b);
|
|
205
|
+
limb_t bh = addh(kLimbs, b, h);
|
|
206
|
+
return bh == 0;
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
} // namespace proofs
|
|
211
|
+
|
|
212
|
+
#endif // PRIVACY_PROOFS_ZK_LIB_ALGEBRA_NAT_H_
|