diff --git a/Cargo.lock b/Cargo.lock index e62b7536..240b146b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -99,17 +99,6 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" -[[package]] -name = "aes" -version = "0.8.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0" -dependencies = [ - "cfg-if", - "cipher", - "cpufeatures", -] - [[package]] name = "ahash" version = "0.8.12" @@ -157,7 +146,7 @@ dependencies = [ "k256", "once_cell", "rand 0.8.5", - "secp256k1 0.30.0", + "secp256k1", "serde", "serde_json", "serde_with", @@ -1749,16 +1738,6 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c7f02d4ea65f2c1853089ffd8d2787bdbc63de2f0d29dedbcf8ccdfa0ccd4cf" -[[package]] -name = "base58ck" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c8d66485a3a2ea485c1913c4572ce0256067a5377ac8c75c4960e1cda98605f" -dependencies = [ - "bitcoin-internals", - "bitcoin_hashes", -] - [[package]] name = "base64" version = "0.21.7" @@ -1787,12 +1766,6 @@ version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "55248b47b0caf0546f7988906588779981c43bb1bc9d0c44087278f80cdb44ba" -[[package]] -name = "bech32" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d965446196e3b7decd44aa7ee49e31d630118f90ef12f97900f262eb915c951d" - [[package]] name = "big-int" version = "7.0.0" @@ -1945,38 +1918,6 @@ dependencies = [ "thiserror 2.0.17", ] -[[package]] -name = "binius-examples" -version = "0.1.0" -source = "git+https://github.com/IrreducibleOSS/binius64?rev=d8bfbd6#d8bfbd67e13b087315755f11593a33631350f294" -dependencies = [ - "anyhow", - "base64 0.22.1", - "binius-circuits", - "binius-core", - "binius-frontend", - "binius-prover", - "binius-transcript", - "binius-utils", - "binius-verifier", - "bitcoin", - "bitcoin_hashes", - "blake2", - "clap", - "digest 0.10.7", - "ethsign", - "hex", - "jwt-simple", - "peakmem-alloc", - "rand 0.9.2", - "sha2", - "sha3", - "tiny-keccak", - "tracing", - "tracing-profile", - "ureq", -] - [[package]] name = "binius-field" version = "0.1.0" @@ -2072,7 +2013,6 @@ dependencies = [ "generic-array 0.14.9", "itertools 0.14.0", "rayon", - "regex", "thiserror 2.0.17", "trait-set", ] @@ -2104,7 +2044,6 @@ dependencies = [ "anyhow", "binius-circuits", "binius-core", - "binius-examples", "binius-frontend", "binius-prover", "binius-utils", @@ -2118,12 +2057,6 @@ dependencies = [ "utils 0.1.0", ] -[[package]] -name = "binstring" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0669d5a35b64fdb5ab7fb19cae13148b6b5cbdf4b8247faf54ece47f699c8cef" - [[package]] name = "bit-set" version = "0.8.0" @@ -2163,44 +2096,12 @@ dependencies = [ "syn 2.0.107", ] -[[package]] -name = "bitcoin" -version = "0.32.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fda569d741b895131a88ee5589a467e73e9c4718e958ac9308e4f7dc44b6945" -dependencies = [ - "base58ck", - "bech32", - "bitcoin-internals", - "bitcoin-io", - "bitcoin-units", - "bitcoin_hashes", - "hex-conservative", - "hex_lit", - "secp256k1 0.29.1", -] - -[[package]] -name = "bitcoin-internals" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30bdbe14aa07b06e6cfeffc529a1f099e5fbe249524f8125358604df99a4bed2" - [[package]] name = "bitcoin-io" version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b47c4ab7a93edb0c7198c5535ed9b52b63095f4e9b45279c6736cec4b856baf" -[[package]] -name = "bitcoin-units" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5285c8bcaa25876d07f37e3d30c303f2609179716e11d688f51e8f1fe70063e2" -dependencies = [ - "bitcoin-internals", -] - [[package]] name = "bitcoin_hashes" version = "0.14.0" @@ -2678,16 +2579,6 @@ dependencies = [ "half", ] -[[package]] -name = "cipher" -version = "0.4.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" -dependencies = [ - "crypto-common", - "inout", -] - [[package]] name = "circom" version = "0.1.0" @@ -2838,17 +2729,6 @@ dependencies = [ "cc", ] -[[package]] -name = "coarsetime" -version = "0.1.36" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91849686042de1b41cd81490edc83afbcb0abe5a9b6f2c4114f23ce8cca1bcf4" -dependencies = [ - "libc", - "wasix", - "wasm-bindgen", -] - [[package]] name = "cobs" version = "0.3.0" @@ -3270,21 +3150,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "ct-codecs" -version = "1.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b10589d1a5e400d61f9f38f12f884cfd080ff345de8f17efda36fe0e4a02aa8" - -[[package]] -name = "ctr" -version = "0.9.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0369ee1ad671834580515889b80f2ea915f23b8be8d0daa4bbaf2ac5c7590835" -dependencies = [ - "cipher", -] - [[package]] name = "ctrlc" version = "3.5.0" @@ -3772,16 +3637,6 @@ dependencies = [ "spki", ] -[[package]] -name = "ed25519-compact" -version = "2.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e9b3460f44bea8cd47f45a0c70892f1eff856d97cd55358b2f73f663789f6190" -dependencies = [ - "ct-codecs", - "getrandom 0.2.16", -] - [[package]] name = "educe" version = "0.6.0" @@ -3821,7 +3676,6 @@ dependencies = [ "ff 0.13.1", "generic-array 0.14.9", "group 0.13.0", - "hkdf", "pem-rfc7468", "pkcs8", "rand_core 0.6.4", @@ -4116,35 +3970,6 @@ version = "1.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca81e6b4777c89fd810c25a4be2b1bd93ea034fbe58e6a75216a34c6b82c539b" -[[package]] -name = "ethsign" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d1939b589fc2cec84f2e254c521f3b23f1cc35390c2feb07d30389f8d1f99996" -dependencies = [ - "ethsign-crypto", - "rand 0.8.5", - "rustc-hex", - "secp256k1 0.27.0", - "serde", - "zeroize", -] - -[[package]] -name = "ethsign-crypto" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6676491caa7ea71aecae4189ec9ee2f70ae3faadd2f9b08d64e5fc852125423e" -dependencies = [ - "aes", - "ctr", - "hmac", - "pbkdf2", - "scrypt", - "sha2", - "tiny-keccak", -] - [[package]] name = "eventsource-stream" version = "0.2.3" @@ -5082,21 +4907,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bcaaec4551594c969335c98c903c1397853d4198408ea609190f420500f6be71" -[[package]] -name = "hex_lit" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3011d1213f159867b13cfd6ac92d2cd5f1345762c63be3554e84092d85a50bbd" - -[[package]] -name = "hkdf" -version = "0.12.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b5f8eb2ad728638ea2c7d47a21db23b7b58a72ed6a38256b8a1849f15fbbdf7" -dependencies = [ - "hmac", -] - [[package]] name = "hla" version = "0.1.0" @@ -5114,30 +4924,6 @@ dependencies = [ "digest 0.10.7", ] -[[package]] -name = "hmac-sha1-compact" -version = "1.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18492c9f6f9a560e0d346369b665ad2bdbc89fa9bceca75796584e79042694c3" - -[[package]] -name = "hmac-sha256" -version = "1.1.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad6880c8d4a9ebf39c6e8b77007ce223f646a4d21ce29d99f70cb16420545425" -dependencies = [ - "digest 0.10.7", -] - -[[package]] -name = "hmac-sha512" -version = "1.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e89e8d20b3799fa526152a5301a771eaaad80857f83e01b23216ceaafb2d9280" -dependencies = [ - "digest 0.10.7", -] - [[package]] name = "http" version = "0.2.12" @@ -5588,15 +5374,6 @@ dependencies = [ "web-time", ] -[[package]] -name = "inout" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01" -dependencies = [ - "generic-array 0.14.9", -] - [[package]] name = "inplace-vec-builder" version = "0.1.1" @@ -5982,32 +5759,6 @@ dependencies = [ "subtle", ] -[[package]] -name = "jwt-simple" -version = "0.12.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "731011e9647a71ff4f8474176ff6ce6e0d2de87a0173f15613af3a84c3e3401a" -dependencies = [ - "anyhow", - "binstring", - "blake2b_simd", - "coarsetime", - "ct-codecs", - "ed25519-compact", - "hmac-sha1-compact", - "hmac-sha256", - "hmac-sha512", - "k256", - "p256", - "p384", - "rand 0.8.5", - "serde", - "serde_json", - "superboring", - "thiserror 2.0.17", - "zeroize", -] - [[package]] name = "k256" version = "0.13.4" @@ -8964,18 +8715,6 @@ dependencies = [ "serde", ] -[[package]] -name = "p384" -version = "0.13.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe42f1670a52a47d448f14b6a5c61dd78fce51856e68edaa38f7ae3a46b8d6b6" -dependencies = [ - "ecdsa", - "elliptic-curve", - "primeorder", - "sha2", -] - [[package]] name = "pairing" version = "0.22.0" @@ -9056,17 +8795,6 @@ dependencies = [ "windows-link 0.2.1", ] -[[package]] -name = "password-hash" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "346f04948ba92c43e8469c1ee6736c7563d71012b17d40745260fe106aac2166" -dependencies = [ - "base64ct", - "rand_core 0.6.4", - "subtle", -] - [[package]] name = "pasta_curves" version = "0.4.1" @@ -9109,24 +8837,6 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df94ce210e5bc13cb6651479fa48d14f601d9858cfe0467f43ae157023b938d3" -[[package]] -name = "pbkdf2" -version = "0.12.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8ed6a7761f76e3b9f92dfb0a60a6a6477c61024b775147ff0973a02653abaf2" -dependencies = [ - "digest 0.10.7", - "hmac", - "password-hash", - "rayon", -] - -[[package]] -name = "peakmem-alloc" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56b5fee98ddc9ea7d6b82046765a913c804768cae8e2d75d827afaf640c373d4" - [[package]] name = "pem-rfc7468" version = "0.7.0" @@ -10625,7 +10335,6 @@ dependencies = [ "pkcs1", "pkcs8", "rand_core 0.6.4", - "sha2", "signature", "spki", "subtle", @@ -11010,15 +10719,6 @@ dependencies = [ "safe-regex-compiler", ] -[[package]] -name = "salsa20" -version = "0.10.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97a22f5af31f73a954c10289c93e8a50cc23d971e80ee446f1f6f7137a088213" -dependencies = [ - "cipher", -] - [[package]] name = "same-file" version = "1.0.6" @@ -11106,18 +10806,6 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" -[[package]] -name = "scrypt" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0516a385866c09368f0b5bcd1caff3366aace790fcd46e2bb032697bb172fd1f" -dependencies = [ - "password-hash", - "pbkdf2", - "salsa20", - "sha2", -] - [[package]] name = "sct" version = "0.7.1" @@ -11149,25 +10837,6 @@ dependencies = [ "zeroize", ] -[[package]] -name = "secp256k1" -version = "0.27.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25996b82292a7a57ed3508f052cfff8640d38d32018784acd714758b43da9c8f" -dependencies = [ - "secp256k1-sys 0.8.2", -] - -[[package]] -name = "secp256k1" -version = "0.29.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9465315bc9d4566e1724f0fffcbcc446268cb522e60f9a27bcded6b19c108113" -dependencies = [ - "bitcoin_hashes", - "secp256k1-sys 0.10.1", -] - [[package]] name = "secp256k1" version = "0.30.0" @@ -11176,19 +10845,10 @@ checksum = "b50c5943d326858130af85e049f2661ba3c78b26589b8ab98e65e80ae44a1252" dependencies = [ "bitcoin_hashes", "rand 0.8.5", - "secp256k1-sys 0.10.1", + "secp256k1-sys", "serde", ] -[[package]] -name = "secp256k1-sys" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4473013577ec77b4ee3668179ef1186df3146e2cf2d927bd200974c6fe60fd99" -dependencies = [ - "cc", -] - [[package]] name = "secp256k1-sys" version = "0.10.1" @@ -12389,19 +12049,6 @@ dependencies = [ "utils 0.1.0 (git+https://github.com/PolyhedraZK/Expander?branch=main)", ] -[[package]] -name = "superboring" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "515cce34a781d7250b8a65706e0f2a5b99236ea605cb235d4baed6685820478f" -dependencies = [ - "getrandom 0.2.16", - "hmac-sha256", - "hmac-sha512", - "rand 0.8.5", - "rsa", -] - [[package]] name = "supports-color" version = "3.0.2" @@ -13504,36 +13151,6 @@ version = "0.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6d49784317cd0d1ee7ec5c716dd598ec5b4483ea832a2dced265471cc0f690ae" -[[package]] -name = "ureq" -version = "3.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99ba1025f18a4a3fc3e9b48c868e9beb4f24f4b4b1a325bada26bd4119f46537" -dependencies = [ - "base64 0.22.1", - "flate2", - "log", - "percent-encoding", - "rustls 0.23.33", - "rustls-pemfile 2.2.0", - "rustls-pki-types", - "ureq-proto", - "utf-8", - "webpki-roots", -] - -[[package]] -name = "ureq-proto" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60b4531c118335662134346048ddb0e54cc86bd7e81866757873055f0e38f5d2" -dependencies = [ - "base64 0.22.1", - "http 1.3.1", - "httparse", - "log", -] - [[package]] name = "url" version = "2.5.7" @@ -13750,15 +13367,6 @@ dependencies = [ "wit-bindgen", ] -[[package]] -name = "wasix" -version = "0.12.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1fbb4ef9bbca0c1170e0b00dd28abc9e3b68669821600cad1caaed606583c6d" -dependencies = [ - "wasi 0.11.1+wasi-snapshot-preview1", -] - [[package]] name = "wasm-bindgen" version = "0.2.104" diff --git a/binius64/.cargo/config.toml b/binius64/.cargo/config.toml new file mode 100644 index 00000000..ddff4407 --- /dev/null +++ b/binius64/.cargo/config.toml @@ -0,0 +1,2 @@ +[build] +rustflags = ["-C", "target-cpu=native"] diff --git a/binius64/Cargo.toml b/binius64/Cargo.toml index 59153436..94b73469 100644 --- a/binius64/Cargo.toml +++ b/binius64/Cargo.toml @@ -17,7 +17,6 @@ sha2 = { workspace = true } tracing-profile = "0.10.3" binius-frontend = { git = "https://github.com/IrreducibleOSS/binius64", rev = "d8bfbd6" } binius-circuits = { git = "https://github.com/IrreducibleOSS/binius64", rev = "d8bfbd6" } -binius-examples = { git = "https://github.com/IrreducibleOSS/binius64", rev = "d8bfbd6" } binius-prover = { git = "https://github.com/IrreducibleOSS/binius64", rev = "d8bfbd6" } binius-verifier = { git = "https://github.com/IrreducibleOSS/binius64", rev = "d8bfbd6" } binius-core = { git = "https://github.com/IrreducibleOSS/binius64", rev = "d8bfbd6" } diff --git a/binius64/src/lib.rs b/binius64/src/lib.rs index d838be36..eef7794d 100644 --- a/binius64/src/lib.rs +++ b/binius64/src/lib.rs @@ -16,10 +16,8 @@ use binius_verifier::{ }; use sha2::digest::{Digest, FixedOutputReset, Output, core_api::BlockSizeUser}; -use binius_examples::{ - ExampleCircuit, StdProver, StdVerifier, - circuits::sha256::{Instance, Params, Sha256Example}, -}; +mod utils; +use utils::{CircuitTrait, Instance, Params, Sha256Circuit, StdProver, StdVerifier}; /// Setup the prover and verifier and use SHA256 for Merkle tree compression. /// Providing the `key_collection` skips expensive key collection building. @@ -46,7 +44,7 @@ pub fn prepare( StdVerifier, StdProver, ConstraintSystem, - Sha256Example, + Sha256Circuit, Circuit, usize, )> { @@ -61,7 +59,7 @@ pub fn prepare( // Build the circuit let mut builder = CircuitBuilder::new(); - let sha256_circuit = Sha256Example::build(params, &mut builder)?; + let sha256_circuit = Sha256Circuit::build(params, &mut builder)?; let compiled_circuit = builder.build(); // Set up prover and verifier @@ -83,7 +81,7 @@ pub fn prepare( pub fn prove( prover: &Prover, compiled_circuit: &Circuit, - sha256_circuit: &Sha256Example, + sha256_circuit: &Sha256Circuit, input_size: usize, ) -> Result<(Vec, Vec)> where diff --git a/binius64/src/utils.rs b/binius64/src/utils.rs new file mode 100644 index 00000000..68dc0330 --- /dev/null +++ b/binius64/src/utils.rs @@ -0,0 +1,217 @@ +use anyhow::{Result, ensure}; +use rand::{RngCore, SeedableRng, rngs::StdRng}; + +use binius_frontend::{CircuitBuilder, Wire, WitnessFiller}; +use binius_prover::{ + OptimalPackedB128, Prover, hash::parallel_compression::ParallelCompressionAdaptor, +}; +use binius_verifier::{ + Verifier, + hash::{StdCompression, StdDigest}, +}; + +use binius_circuits::sha256::Sha256; +use clap::Args; +use sha2::Digest; + +use std::array; + +// Reference: https://github.com/IrreducibleOSS/binius64/blob/main/prover/examples/src/circuits/sha256.rs +pub struct Sha256Circuit { + sha256_gadget: Sha256, +} + +impl CircuitTrait for Sha256Circuit { + type Params = Params; + type Instance = Instance; + + fn build(params: Params, builder: &mut CircuitBuilder) -> Result { + let max_len_bytes = determine_hash_max_bytes_from_args(params.max_len_bytes)?; + let max_len = max_len_bytes.div_ceil(8); + let len_bytes = if params.exact_len { + builder.add_constant_64(max_len_bytes as u64) + } else { + builder.add_witness() + }; + let sha256_gadget = mk_circuit(builder, max_len, len_bytes); + + Ok(Self { sha256_gadget }) + } + + fn populate_witness(&self, instance: Instance, w: &mut WitnessFiller) -> Result<()> { + // Step 1: Get raw message bytes + let raw_message = generate_message_bytes(instance.message_string, instance.message_len); + + // Step 2: Zero-pad to maximum length + let padded_message = zero_pad_message(raw_message, self.sha256_gadget.max_len_bytes())?; + + // Step 3: Compute digest using reference implementation + let digest = sha2::Sha256::digest(&padded_message); + + // Step 4: Populate witness values + self.sha256_gadget + .populate_len_bytes(w, padded_message.len()); + self.sha256_gadget.populate_message(w, &padded_message); + self.sha256_gadget.populate_digest(w, digest.into()); + + Ok(()) + } + + fn param_summary(params: &Self::Params) -> Option { + let base = format!( + "{}b", + params.max_len_bytes.unwrap_or(DEFAULT_HASH_MESSAGE_BYTES) + ); + if params.exact_len { + Some(format!("{}-exact", base)) + } else { + Some(base) + } + } +} + +// Reference: https://github.com/IrreducibleOSS/binius64/blob/main/prover/examples/src/circuits/sha256.rs +fn mk_circuit(b: &mut CircuitBuilder, max_len: usize, len_bytes: Wire) -> Sha256 { + let digest: [Wire; 4] = array::from_fn(|_| b.add_inout()); + let message = (0..max_len).map(|_| b.add_inout()).collect(); + Sha256::new(b, len_bytes, digest, message) +} + +// Reference: https://github.com/IrreducibleOSS/binius64/blob/main/prover/examples/src/circuits/sha256.rs +#[derive(Args, Debug, Clone)] +pub struct Params { + /// Maximum message length in bytes that the circuit can handle. + #[arg(long)] + pub max_len_bytes: Option, + + /// Build circuit for exact message length (makes length a compile-time constant instead of + /// runtime witness). + #[arg(long, default_value_t = false)] + pub exact_len: bool, +} + +// Reference: https://github.com/IrreducibleOSS/binius64/blob/main/prover/examples/src/circuits/sha256.rs +#[derive(Args, Debug, Clone)] +#[group(multiple = false)] +pub struct Instance { + /// Length of the randomly generated message, in bytes (defaults to 1024). + #[arg(long)] + pub message_len: Option, + + /// UTF-8 string to hash (if not provided, random bytes are generated) + #[arg(long)] + pub message_string: Option, +} + +// Reference: https://github.com/IrreducibleOSS/binius64/blob/main/prover/examples/src/lib.rs +pub type StdVerifier = Verifier; + +// Reference: https://github.com/IrreducibleOSS/binius64/blob/main/prover/examples/src/lib.rs +pub type StdProver = + Prover, StdDigest>; + +// Reference: https://github.com/IrreducibleOSS/binius64/blob/main/prover/examples/src/lib.rs +pub trait CircuitTrait: Sized { + /// Circuit parameters that affect the structure of the circuit. + /// These are typically compile-time constants or bounds. + type Params: clap::Args; + + /// Instance data used to populate the witness. + /// This represents the actual input values for a specific proof. + type Instance: clap::Args; + + /// Build the circuit with the given parameters. + /// + /// This method should: + /// - Add witnesses, constants, and constraints to the builder + /// - Store any wire references needed for witness population + /// - Return a Self instance that can later populate witness values + fn build(params: Self::Params, builder: &mut CircuitBuilder) -> Result; + + /// Populate witness values for a specific instance. + /// + /// This method should: + /// - Process the instance data (e.g., parse inputs, compute hashes) + /// - Fill all witness values using the provided filler + /// - Validate that instance data is compatible with circuit parameters + fn populate_witness(&self, instance: Self::Instance, filler: &mut WitnessFiller) -> Result<()>; + + /// Generate a concise parameter summary for perfetto trace filenames. + /// + /// This method should return a short string (5-10 chars max) that captures + /// the most important parameters for this circuit configuration. + /// Used to differentiate traces with different parameter settings. + /// + /// Format suggestions: + /// - Bytes: "2048b", "4096b" + /// - Counts: "10p" (permutations), "5s" (signatures) + /// + /// Returns None if no meaningful parameters to include in filename. + #[allow(dead_code)] + fn param_summary(params: &Self::Params) -> Option { + let _ = params; + None + } +} + +// Reference: https://github.com/IrreducibleOSS/binius64/blob/main/prover/examples/src/circuits/utils.rs +pub const DEFAULT_HASH_MESSAGE_BYTES: usize = 1024; + +// Reference: https://github.com/IrreducibleOSS/binius64/blob/main/prover/examples/src/circuits/utils.rs +pub const DEFAULT_RANDOM_SEED: u64 = 42; + +// Reference: https://github.com/IrreducibleOSS/binius64/blob/main/prover/examples/src/circuits/utils.rs +pub fn determine_hash_max_bytes_from_args(max_bytes_param: Option) -> Result { + let max_bytes = max_bytes_param.unwrap_or_else(|| { + let args: Vec = std::env::args().collect(); + let mut message_len = None; + let mut message_string = None; + + for i in 0..args.len() { + if args[i] == "--message-len" && i + 1 < args.len() { + message_len = args[i + 1].parse::().ok(); + } else if args[i] == "--message-string" && i + 1 < args.len() { + message_string = Some(args[i + 1].clone()); + } + } + + if let Some(msg_string) = message_string { + msg_string.len() + } else { + message_len.unwrap_or(DEFAULT_HASH_MESSAGE_BYTES) + } + }); + + ensure!(max_bytes > 0, "Message length must be positive"); + Ok(max_bytes) +} + +// Reference: https://github.com/IrreducibleOSS/binius64/blob/main/prover/examples/src/circuits/utils.rs +pub fn generate_message_bytes( + message_string: Option, + message_len: Option, +) -> Vec { + if let Some(message_string) = message_string { + message_string.as_bytes().to_vec() + } else { + let mut rng = StdRng::seed_from_u64(DEFAULT_RANDOM_SEED); + let len = message_len.unwrap_or(DEFAULT_HASH_MESSAGE_BYTES); + let mut message_bytes = vec![0u8; len]; + rng.fill_bytes(&mut message_bytes); + message_bytes + } +} + +// Reference: https://github.com/IrreducibleOSS/binius64/blob/main/prover/examples/src/circuits/utils.rs +pub fn zero_pad_message(message_bytes: Vec, max_len: usize) -> Result> { + ensure!( + message_bytes.len() <= max_len, + "Message length ({}) exceeds maximum ({})", + message_bytes.len(), + max_len + ); + + let mut padded = message_bytes; + padded.resize(max_len, 0); + Ok(padded) +}