diff --git a/.gitignore b/.gitignore index eb915932..e0b57497 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,6 @@ Cargo.lock /halo2_ecc/src/bn254/data/ /halo2_ecc/src/secp256k1/data/ + +*.srs +*.csv diff --git a/Cargo.toml b/Cargo.toml index b2d3ab72..372c6c4f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,9 @@ [workspace] -members = ["halo2-base", "halo2-ecc", "hashes/zkevm"] +members = [ + "halo2-base", + "halo2-ecc", + "hashes/zkevm" + ] resolver = "2" [profile.dev] @@ -39,3 +43,6 @@ debug = true [patch."https://github.com/axiom-crypto/halo2-lib.git"] halo2-base = { path = "../halo2-lib/halo2-base" } halo2-ecc = { path = "../halo2-lib/halo2-ecc" } + +[patch."https://github.com/privacy-scaling-explorations/halo2.git"] +halo2_proofs = { git = "https://github.com/scroll-tech/halo2.git", branch = "sync-halo2-lib-0.4.0" } diff --git a/halo2-base/Cargo.toml b/halo2-base/Cargo.toml index 542b98ad..02e54535 100644 --- a/halo2-base/Cargo.toml +++ b/halo2-base/Cargo.toml @@ -18,13 +18,14 @@ getset = "0.1.2" ark-std = { version = "0.3.0", features = ["print-trace"], optional = true } # Use Axiom's custom halo2 monorepo for faster proving when feature = "halo2-axiom" is on -halo2_proofs_axiom = { git = "https://github.com/axiom-crypto/halo2.git", package = "halo2_proofs", optional = true } +halo2_proofs_axiom = { git = "https://github.com/axiom-crypto/halo2.git", package = "halo2-axiom", optional = true } # Use PSE halo2 and halo2curves for compatibility when feature = "halo2-pse" is on halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2.git", rev = "7a21656", optional = true } +# halo2_proofs = { git = "https://github.com/scroll-tech/halo2.git", branch = "sync-upstream-1007", optional = true } # This is Scroll's audited poseidon circuit. We only use it for the Native Poseidon spec. We do not use the halo2 circuit at all (and it wouldn't even work because the halo2_proofs tag is not compatbile). # We forked it to upgrade to ff v0.13 and removed the circuit module -poseidon-rs = { git = "https://github.com/axiom-crypto/poseidon-circuit.git", rev = "1aee4a1" } +poseidon-circuit = { git = "https://github.com/scroll-tech/poseidon-circuit.git", branch = "sync-halo2-lib-0.4.0" } # plotting circuit layout plotters = { version = "0.3.0", optional = true } tabbycat = { version = "0.1", features = ["attributes"], optional = true } @@ -43,7 +44,7 @@ test-log = "0.2.12" env_logger = "0.10.0" proptest = "1.1.0" # native poseidon for testing -pse-poseidon = { git = "https://github.com/axiom-crypto/pse-poseidon.git" } +poseidon = { git = "https://github.com/scroll-tech/poseidon.git", branch = "sync-halo2-lib-0.4.0" } # memory allocation [target.'cfg(not(target_env = "msvc"))'.dependencies] @@ -52,13 +53,15 @@ jemallocator = { version = "0.5", optional = true } mimalloc = { version = "0.1", default-features = false, optional = true } [features] -default = ["halo2-axiom", "display", "test-utils"] +default = ["halo2-pse", "test-utils"] +# default = ["halo2-pse", "display", "test-utils"] asm = ["halo2_proofs_axiom?/asm"] dev-graph = [ "halo2_proofs?/dev-graph", "halo2_proofs_axiom?/dev-graph", "plotters", ] +# halo2-pse = [] halo2-pse = ["halo2_proofs/circuit-params"] halo2-axiom = ["halo2_proofs_axiom"] display = [] diff --git a/halo2-base/proptest-regressions/gates/tests/prop_test.txt b/halo2-base/proptest-regressions/gates/tests/prop_test.txt deleted file mode 100644 index aa4e1000..00000000 --- a/halo2-base/proptest-regressions/gates/tests/prop_test.txt +++ /dev/null @@ -1,11 +0,0 @@ -# Seeds for failure cases proptest has generated in the past. It is -# automatically read and these particular cases re-run before any -# novel cases are generated. -# -# It is recommended to check this file in to source control so that -# everyone who runs the test benefits from these saved cases. -cc 8489bbcc3439950355c90ecbc92546a66e4b57eae0a3856e7a4ccb59bf74b4ce # shrinks to k = 1, len = 1, idx = 0, witness_vals = [0x0000000000000000000000000000000000000000000000000000000000000000] -cc b18c4f5e502fe36dbc2471f89a6ffb389beaf473b280e844936298ab1cf9b74e # shrinks to (k, len, idx, witness_vals) = (8, 2, 1, [0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000001]) -cc 4528fb02e7227f85116c2a16aef251b9c3b6d9c340ddb50b936c2140d7856cc4 # shrinks to inputs = ([], []) -cc 79bfe42c93b5962a38b2f831f1dd438d8381a24a6ce15bfb89a8562ce9af0a2d # shrinks to (k, len, idx, witness_vals) = (8, 62, 0, [0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000]) -cc d0e10a06108cb58995a8ae77a91b299fb6230e9e6220121c48f2488e5d199e82 # shrinks to input = (0x000000000000000000000000000000000000000000000000070a95cb0607bef9, 4096) diff --git a/halo2-base/src/gates/circuit/builder.rs b/halo2-base/src/gates/circuit/builder.rs index 980abee9..a0444a22 100644 --- a/halo2-base/src/gates/circuit/builder.rs +++ b/halo2-base/src/gates/circuit/builder.rs @@ -366,6 +366,7 @@ impl BaseCircuitBuilder { } /// Basic statistics +#[derive(Debug)] pub struct RangeStatistics { /// Number of advice cells for the basic gate and total constants used pub gate: GateStatistics, diff --git a/halo2-base/src/gates/circuit/mod.rs b/halo2-base/src/gates/circuit/mod.rs index 46dec873..44fd139b 100644 --- a/halo2-base/src/gates/circuit/mod.rs +++ b/halo2-base/src/gates/circuit/mod.rs @@ -137,6 +137,113 @@ impl BaseConfig { } } +impl BaseCircuitBuilder { + /// Performs the actual computation on the circuit (e.g., witness generation), populating the lookup table and filling in all the advice values for a particular proof. + pub fn synthesize_ref_layouter_phase_0( + &self, + config: BaseConfig, + layouter: &mut impl Layouter, + ) -> Result<(), Error> { + // only load lookup table if we are actually doing lookups + if let MaybeRangeConfig::WithRange(config) = &config.base { + config.load_lookup_table(layouter).expect("load lookup table should not fail"); + } + // FirstPhase (phase 0) + layouter + .assign_region( + || "base phase 0", + |mut region| { + let usable_rows = config.gate().max_rows; + self.core.phase_manager[0].assign_raw( + &(config.gate().basic_gates[0].clone(), usable_rows), + &mut region, + ); + + // Only assign cells to lookup if we're sure we're doing range lookups + if let MaybeRangeConfig::WithRange(config) = &config.base { + self.assign_lookups_in_phase(config, &mut region, 0); + } + Ok(()) + }, + ) + .unwrap(); + + Ok(()) + } + + /// Performs the actual computation on the circuit (e.g., witness generation), populating the lookup table and filling in all the advice values for a particular proof. + pub fn synthesize_ref_layouter_phase_1( + &self, + config: BaseConfig, + layouter: &mut impl Layouter, + ) -> Result<(), Error> { + // SecondPhase (phase 1) + layouter + .assign_region( + || "base phase 1", + |mut region| { + let usable_rows = config.gate().max_rows; + if self.core.phase_manager.len() > 1 { + self.core.phase_manager[1].assign_raw( + &(config.gate().basic_gates[1].clone(), usable_rows), + &mut region, + ); + } + + // Only assign cells to lookup if we're sure we're doing range lookups + if let MaybeRangeConfig::WithRange(config) = &config.base { + if self.core.phase_manager.len() > 1 { + self.assign_lookups_in_phase(config, &mut region, 1); + } + } + + Ok(()) + }, + ) + .unwrap(); + + Ok(()) + } + + /// Performs the actual computation on the circuit (e.g., witness generation), populating the lookup table and filling in all the advice values for a particular proof. + pub fn synthesize_ref_layouter_final( + &self, + config: BaseConfig, + layouter: &mut impl Layouter, + with_instances: bool, + ) -> Result<(), Error> { + // finalize + layouter + .assign_region( + || "constants assignments + copy constraints", + |mut region| { + // Impose equality constraints + if !self.core.witness_gen_only() { + self.core.copy_manager.assign_raw(config.constants(), &mut region); + } + Ok(()) + }, + ) + .unwrap(); + if with_instances { + self.assign_instances(&config.instance, layouter.namespace(|| "expose instances")); + } + Ok(()) + } + + /// Performs the actual computation on the circuit (e.g., witness generation), populating the lookup table and filling in all the advice values for a particular proof. + pub fn synthesize_ref_layouter( + &self, + config: BaseConfig, + layouter: &mut impl Layouter, + ) -> Result<(), Error> { + self.synthesize_ref_layouter_phase_0(config.clone(), layouter)?; + self.synthesize_ref_layouter_phase_1(config.clone(), layouter)?; + self.synthesize_ref_layouter_final(config, layouter, true)?; + Ok(()) + } +} + impl Circuit for BaseCircuitBuilder { type Config = BaseConfig; type FloorPlanner = SimpleFloorPlanner; @@ -166,35 +273,7 @@ impl Circuit for BaseCircuitBuilder { config: Self::Config, mut layouter: impl Layouter, ) -> Result<(), Error> { - // only load lookup table if we are actually doing lookups - if let MaybeRangeConfig::WithRange(config) = &config.base { - config.load_lookup_table(&mut layouter).expect("load lookup table should not fail"); - } - // Only FirstPhase (phase 0) - layouter - .assign_region( - || "BaseCircuitBuilder generated circuit", - |mut region| { - let usable_rows = config.gate().max_rows; - self.core.phase_manager[0].assign_raw( - &(config.gate().basic_gates[0].clone(), usable_rows), - &mut region, - ); - // Only assign cells to lookup if we're sure we're doing range lookups - if let MaybeRangeConfig::WithRange(config) = &config.base { - self.assign_lookups_in_phase(config, &mut region, 0); - } - // Impose equality constraints - if !self.core.witness_gen_only() { - self.core.copy_manager.assign_raw(config.constants(), &mut region); - } - Ok(()) - }, - ) - .unwrap(); - - self.assign_instances(&config.instance, layouter.namespace(|| "expose")); - Ok(()) + self.synthesize_ref_layouter(config, &mut layouter) } } diff --git a/halo2-base/src/gates/flex_gate/threads/multi_phase.rs b/halo2-base/src/gates/flex_gate/threads/multi_phase.rs index 40ce5103..838d8993 100644 --- a/halo2-base/src/gates/flex_gate/threads/multi_phase.rs +++ b/halo2-base/src/gates/flex_gate/threads/multi_phase.rs @@ -154,6 +154,7 @@ impl MultiPhaseCoreManager { } /// Basic statistics +#[derive(Debug)] pub struct GateStatistics { /// Total advice cell count per phase pub total_advice_per_phase: Vec, diff --git a/halo2-base/src/poseidon/hasher/spec.rs b/halo2-base/src/poseidon/hasher/spec.rs index e0a0d2c9..9df94a3c 100644 --- a/halo2-base/src/poseidon/hasher/spec.rs +++ b/halo2-base/src/poseidon/hasher/spec.rs @@ -4,7 +4,7 @@ use crate::{ }; use getset::{CopyGetters, Getters}; -use poseidon_rs::poseidon::primitives::Spec as PoseidonSpec; // trait +use poseidon_circuit::poseidon::primitives::Spec as PoseidonSpec; // trait use std::marker::PhantomData; // struct so we can use PoseidonSpec trait to generate round constants and MDS matrix @@ -21,7 +21,7 @@ pub(crate) struct Poseidon128Pow5Gen< } impl< - F: PrimeField, + F: FromUniformBytes<64> + Ord, const T: usize, const RATE: usize, const R_F: usize, diff --git a/halo2-base/src/poseidon/hasher/tests/compatibility.rs b/halo2-base/src/poseidon/hasher/tests/compatibility.rs index 74e40531..fc91b7f3 100644 --- a/halo2-base/src/poseidon/hasher/tests/compatibility.rs +++ b/halo2-base/src/poseidon/hasher/tests/compatibility.rs @@ -6,7 +6,7 @@ use crate::{ poseidon::hasher::PoseidonSponge, utils::ScalarField, }; -use pse_poseidon::Poseidon; +use poseidon::Poseidon; use rand::Rng; // make interleaved calls to absorb and squeeze elements and diff --git a/halo2-base/src/poseidon/hasher/tests/hasher.rs b/halo2-base/src/poseidon/hasher/tests/hasher.rs index fba101cc..67cdc707 100644 --- a/halo2-base/src/poseidon/hasher/tests/hasher.rs +++ b/halo2-base/src/poseidon/hasher/tests/hasher.rs @@ -10,7 +10,7 @@ use crate::{ Context, }; use itertools::Itertools; -use pse_poseidon::Poseidon; +use poseidon::Poseidon; use rand::Rng; #[derive(Clone)] diff --git a/halo2-ecc/Cargo.toml b/halo2-ecc/Cargo.toml index 7692ef73..47ad51a5 100644 --- a/halo2-ecc/Cargo.toml +++ b/halo2-ecc/Cargo.toml @@ -32,7 +32,7 @@ test-log = "0.2.12" env_logger = "0.10.0" [features] -default = ["jemallocator", "halo2-axiom", "display"] +default = ["jemallocator", "halo2-pse", "display"] dev-graph = ["halo2-base/dev-graph"] display = ["halo2-base/display"] asm = ["halo2-base/asm"]