From effc694b8d9256f6a0f76ed7be78370a4784b8c5 Mon Sep 17 00:00:00 2001 From: zhenfei Date: Mon, 16 Oct 2023 15:07:35 -0400 Subject: [PATCH 1/9] refactor: use updated ff/halo2/poseidon --- .gitignore | 3 +++ Cargo.toml | 3 +++ halo2-base/Cargo.toml | 8 +++++--- halo2-base/src/poseidon/hasher/spec.rs | 4 ++-- halo2-base/src/poseidon/hasher/tests/compatibility.rs | 2 +- halo2-base/src/poseidon/hasher/tests/hasher.rs | 2 +- 6 files changed, 15 insertions(+), 7 deletions(-) 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..7a8f626c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,3 +39,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..8402ff22 100644 --- a/halo2-base/Cargo.toml +++ b/halo2-base/Cargo.toml @@ -21,10 +21,11 @@ ark-std = { version = "0.3.0", features = ["print-trace"], optional = true } halo2_proofs_axiom = { git = "https://github.com/axiom-crypto/halo2.git", package = "halo2_proofs", 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,14 @@ 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", "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/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)] From aa380a4116fefd57db3ce5f1b84e1dd4f7444c34 Mon Sep 17 00:00:00 2001 From: zhenfei Date: Mon, 30 Oct 2023 12:27:55 -0400 Subject: [PATCH 2/9] update links --- halo2-ecc/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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"] From 2024735b693fa69b0797b754b41b03cef734106d Mon Sep 17 00:00:00 2001 From: zhenfei Date: Mon, 6 Nov 2023 15:15:23 -0500 Subject: [PATCH 3/9] api to pass layouter by reference --- halo2-base/src/gates/circuit/mod.rs | 65 +++++++++++++++++------------ 1 file changed, 38 insertions(+), 27 deletions(-) diff --git a/halo2-base/src/gates/circuit/mod.rs b/halo2-base/src/gates/circuit/mod.rs index 46dec873..d6f78d96 100644 --- a/halo2-base/src/gates/circuit/mod.rs +++ b/halo2-base/src/gates/circuit/mod.rs @@ -137,38 +137,16 @@ impl BaseConfig { } } -impl Circuit for BaseCircuitBuilder { - type Config = BaseConfig; - type FloorPlanner = SimpleFloorPlanner; - type Params = BaseCircuitParams; - - fn params(&self) -> Self::Params { - self.config_params.clone() - } - - /// Creates a new instance of the [RangeCircuitBuilder] without witnesses by setting the witness_gen_only flag to false - fn without_witnesses(&self) -> Self { - unimplemented!() - } - - /// Configures a new circuit using [`BaseConfigParams`] - fn configure_with_params(meta: &mut ConstraintSystem, params: Self::Params) -> Self::Config { - BaseConfig::configure(meta, params) - } - - fn configure(_: &mut ConstraintSystem) -> Self::Config { - unreachable!("You must use configure_with_params"); - } - +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. - fn synthesize( + pub fn synthesize_ref_layouter( &self, - config: Self::Config, - mut layouter: impl Layouter, + 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(&mut layouter).expect("load lookup table should not fail"); + config.load_lookup_table(layouter).expect("load lookup table should not fail"); } // Only FirstPhase (phase 0) layouter @@ -198,6 +176,39 @@ impl Circuit for BaseCircuitBuilder { } } +impl Circuit for BaseCircuitBuilder { + type Config = BaseConfig; + type FloorPlanner = SimpleFloorPlanner; + type Params = BaseCircuitParams; + + fn params(&self) -> Self::Params { + self.config_params.clone() + } + + /// Creates a new instance of the [RangeCircuitBuilder] without witnesses by setting the witness_gen_only flag to false + fn without_witnesses(&self) -> Self { + unimplemented!() + } + + /// Configures a new circuit using [`BaseConfigParams`] + fn configure_with_params(meta: &mut ConstraintSystem, params: Self::Params) -> Self::Config { + BaseConfig::configure(meta, params) + } + + fn configure(_: &mut ConstraintSystem) -> Self::Config { + unreachable!("You must use configure_with_params"); + } + + /// 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. + fn synthesize( + &self, + config: Self::Config, + mut layouter: impl Layouter, + ) -> Result<(), Error> { + self.synthesize_ref_layouter(config, &mut layouter) + } +} + /// Defines stage of circuit building. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum CircuitBuilderStage { From f2cd0ea2f86364c56d5e2f99df519db63721e796 Mon Sep 17 00:00:00 2001 From: zhenfei Date: Mon, 6 Nov 2023 21:26:29 -0500 Subject: [PATCH 4/9] second phase assignment --- halo2-base/src/gates/circuit/mod.rs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/halo2-base/src/gates/circuit/mod.rs b/halo2-base/src/gates/circuit/mod.rs index d6f78d96..8202c0a7 100644 --- a/halo2-base/src/gates/circuit/mod.rs +++ b/halo2-base/src/gates/circuit/mod.rs @@ -148,7 +148,7 @@ impl BaseCircuitBuilder { if let MaybeRangeConfig::WithRange(config) = &config.base { config.load_lookup_table(layouter).expect("load lookup table should not fail"); } - // Only FirstPhase (phase 0) + // FirstPhase (phase 0) layouter .assign_region( || "BaseCircuitBuilder generated circuit", @@ -170,7 +170,28 @@ impl BaseCircuitBuilder { }, ) .unwrap(); - + // SecondPhase (phase 1) + layouter + .assign_region( + || "BaseCircuitBuilder generated circuit", + |mut region| { + let usable_rows = config.gate().max_rows; + 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 { + self.assign_lookups_in_phase(config, &mut region, 1); + } + // 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(()) } From 22d7512d443aeeb782a905af26742ec2169ecdfa Mon Sep 17 00:00:00 2001 From: zhenfei Date: Mon, 6 Nov 2023 21:48:17 -0500 Subject: [PATCH 5/9] allow for debugging statistics --- halo2-base/src/gates/circuit/builder.rs | 1 + halo2-base/src/gates/flex_gate/threads/multi_phase.rs | 1 + 2 files changed, 2 insertions(+) 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/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, From b0d8b6e78754a717bfa6fc9320c76ec34c955ab5 Mon Sep 17 00:00:00 2001 From: zhenfei Date: Mon, 6 Nov 2023 22:01:01 -0500 Subject: [PATCH 6/9] two phases --- halo2-base/src/gates/circuit/mod.rs | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/halo2-base/src/gates/circuit/mod.rs b/halo2-base/src/gates/circuit/mod.rs index 8202c0a7..8ef7133e 100644 --- a/halo2-base/src/gates/circuit/mod.rs +++ b/halo2-base/src/gates/circuit/mod.rs @@ -148,7 +148,7 @@ impl BaseCircuitBuilder { if let MaybeRangeConfig::WithRange(config) = &config.base { config.load_lookup_table(layouter).expect("load lookup table should not fail"); } - // FirstPhase (phase 0) + // FirstPhase and SecondPhase (phase 0/1) layouter .assign_region( || "BaseCircuitBuilder generated circuit", @@ -158,30 +158,13 @@ impl BaseCircuitBuilder { &(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(); - // SecondPhase (phase 1) - layouter - .assign_region( - || "BaseCircuitBuilder generated circuit", - |mut region| { - let usable_rows = config.gate().max_rows; 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 { + self.assign_lookups_in_phase(config, &mut region, 0); self.assign_lookups_in_phase(config, &mut region, 1); } // Impose equality constraints From ede59c90ca634481f845b6098404b6e0658ea3df Mon Sep 17 00:00:00 2001 From: zhenfei Date: Mon, 6 Nov 2023 22:25:51 -0500 Subject: [PATCH 7/9] dynamic number of phases --- halo2-base/src/gates/circuit/mod.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/halo2-base/src/gates/circuit/mod.rs b/halo2-base/src/gates/circuit/mod.rs index 8ef7133e..9bbba970 100644 --- a/halo2-base/src/gates/circuit/mod.rs +++ b/halo2-base/src/gates/circuit/mod.rs @@ -158,14 +158,19 @@ impl BaseCircuitBuilder { &(config.gate().basic_gates[0].clone(), usable_rows), &mut region, ); - self.core.phase_manager[1].assign_raw( - &(config.gate().basic_gates[1].clone(), usable_rows), - &mut region, - ); + 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 { self.assign_lookups_in_phase(config, &mut region, 0); - self.assign_lookups_in_phase(config, &mut region, 1); + if self.core.phase_manager.len() > 1 { + self.assign_lookups_in_phase(config, &mut region, 1); + } } // Impose equality constraints if !self.core.witness_gen_only() { From 3e807cc1e406e6b9fef81e431ab7fead07c6fd09 Mon Sep 17 00:00:00 2001 From: zhenfei Date: Mon, 27 Nov 2023 18:44:23 -0500 Subject: [PATCH 8/9] wip --- Cargo.toml | 6 +- halo2-base/Cargo.toml | 5 +- .../gates/tests/prop_test.txt | 11 --- halo2-base/src/gates/circuit/mod.rs | 82 +++++++++++++++++-- .../src/virtual_region/copy_constraints.rs | 1 + 5 files changed, 83 insertions(+), 22 deletions(-) delete mode 100644 halo2-base/proptest-regressions/gates/tests/prop_test.txt diff --git a/Cargo.toml b/Cargo.toml index 7a8f626c..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] diff --git a/halo2-base/Cargo.toml b/halo2-base/Cargo.toml index 8402ff22..02e54535 100644 --- a/halo2-base/Cargo.toml +++ b/halo2-base/Cargo.toml @@ -18,7 +18,7 @@ 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 } @@ -53,7 +53,8 @@ jemallocator = { version = "0.5", optional = true } mimalloc = { version = "0.1", default-features = false, optional = true } [features] -default = ["halo2-pse", "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", 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/mod.rs b/halo2-base/src/gates/circuit/mod.rs index 9bbba970..df042b5d 100644 --- a/halo2-base/src/gates/circuit/mod.rs +++ b/halo2-base/src/gates/circuit/mod.rs @@ -139,7 +139,7 @@ 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( + pub fn synthesize_ref_layouter_phase_0( &self, config: BaseConfig, layouter: &mut impl Layouter, @@ -148,16 +148,45 @@ impl BaseCircuitBuilder { if let MaybeRangeConfig::WithRange(config) = &config.base { config.load_lookup_table(layouter).expect("load lookup table should not fail"); } - // FirstPhase and SecondPhase (phase 0/1) + // FirstPhase (phase 0) layouter .assign_region( - || "BaseCircuitBuilder generated circuit", + || "base phase 0", |mut region| { let usable_rows = config.gate().max_rows; + println!("region before phase 0: {:?}", region); 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); + } + println!("region after phase 0: {:?}", region); + 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; + println!("region before phase 1: {:?}", region); + println!("base gate len: {:?}", config.gate().basic_gates.len()); if self.core.phase_manager.len() > 1 { self.core.phase_manager[1].assign_raw( &(config.gate().basic_gates[1].clone(), usable_rows), @@ -167,20 +196,57 @@ impl BaseCircuitBuilder { // 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); - if self.core.phase_manager.len() > 1 { - self.assign_lookups_in_phase(config, &mut region, 1); - } + // if self.core.phase_manager.len() > 1 { + self.assign_lookups_in_phase(config, &mut region, 1); + // } } + println!("region after phase 1: {:?}", region); + + 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, + ) -> Result<(), Error> { + // finalize + layouter + .assign_region( + || "constants assignments + copy constraints", + |mut region| { + println!("region before final phase: {:?}", region); + // Impose equality constraints if !self.core.witness_gen_only() { self.core.copy_manager.assign_raw(config.constants(), &mut region); } + + println!("region after final phase: {:?}", region); Ok(()) }, ) .unwrap(); - self.assign_instances(&config.instance, layouter.namespace(|| "expose")); + + 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)?; Ok(()) } } diff --git a/halo2-base/src/virtual_region/copy_constraints.rs b/halo2-base/src/virtual_region/copy_constraints.rs index d9fe6742..bae0bd92 100644 --- a/halo2-base/src/virtual_region/copy_constraints.rs +++ b/halo2-base/src/virtual_region/copy_constraints.rs @@ -114,6 +114,7 @@ impl Drop for CopyConstraintManager { if !self.advice_equalities.is_empty() { dbg!("WARNING: advice_equalities not empty"); } + println!("const equality: {:?}", self.constant_equalities); if !self.constant_equalities.is_empty() { dbg!("WARNING: constant_equalities not empty"); } From 276991de8b24daffb22b966f2757772efe3da1fa Mon Sep 17 00:00:00 2001 From: zhenfei Date: Mon, 4 Dec 2023 13:52:37 -0500 Subject: [PATCH 9/9] clean up --- halo2-base/src/gates/circuit/mod.rs | 25 +++++++------------ .../src/virtual_region/copy_constraints.rs | 1 - 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/halo2-base/src/gates/circuit/mod.rs b/halo2-base/src/gates/circuit/mod.rs index df042b5d..44fd139b 100644 --- a/halo2-base/src/gates/circuit/mod.rs +++ b/halo2-base/src/gates/circuit/mod.rs @@ -154,7 +154,6 @@ impl BaseCircuitBuilder { || "base phase 0", |mut region| { let usable_rows = config.gate().max_rows; - println!("region before phase 0: {:?}", region); self.core.phase_manager[0].assign_raw( &(config.gate().basic_gates[0].clone(), usable_rows), &mut region, @@ -164,7 +163,6 @@ impl BaseCircuitBuilder { if let MaybeRangeConfig::WithRange(config) = &config.base { self.assign_lookups_in_phase(config, &mut region, 0); } - println!("region after phase 0: {:?}", region); Ok(()) }, ) @@ -185,8 +183,6 @@ impl BaseCircuitBuilder { || "base phase 1", |mut region| { let usable_rows = config.gate().max_rows; - println!("region before phase 1: {:?}", region); - println!("base gate len: {:?}", config.gate().basic_gates.len()); if self.core.phase_manager.len() > 1 { self.core.phase_manager[1].assign_raw( &(config.gate().basic_gates[1].clone(), usable_rows), @@ -196,11 +192,10 @@ impl BaseCircuitBuilder { // 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); - // } + if self.core.phase_manager.len() > 1 { + self.assign_lookups_in_phase(config, &mut region, 1); + } } - println!("region after phase 1: {:?}", region); Ok(()) }, @@ -215,26 +210,24 @@ impl BaseCircuitBuilder { &self, config: BaseConfig, layouter: &mut impl Layouter, + with_instances: bool, ) -> Result<(), Error> { // finalize layouter .assign_region( || "constants assignments + copy constraints", |mut region| { - println!("region before final phase: {:?}", region); - // Impose equality constraints if !self.core.witness_gen_only() { self.core.copy_manager.assign_raw(config.constants(), &mut region); } - - println!("region after final phase: {:?}", region); Ok(()) }, ) .unwrap(); - - self.assign_instances(&config.instance, layouter.namespace(|| "expose instances")); + if with_instances { + self.assign_instances(&config.instance, layouter.namespace(|| "expose instances")); + } Ok(()) } @@ -245,8 +238,8 @@ impl BaseCircuitBuilder { 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)?; + self.synthesize_ref_layouter_phase_1(config.clone(), layouter)?; + self.synthesize_ref_layouter_final(config, layouter, true)?; Ok(()) } } diff --git a/halo2-base/src/virtual_region/copy_constraints.rs b/halo2-base/src/virtual_region/copy_constraints.rs index bae0bd92..d9fe6742 100644 --- a/halo2-base/src/virtual_region/copy_constraints.rs +++ b/halo2-base/src/virtual_region/copy_constraints.rs @@ -114,7 +114,6 @@ impl Drop for CopyConstraintManager { if !self.advice_equalities.is_empty() { dbg!("WARNING: advice_equalities not empty"); } - println!("const equality: {:?}", self.constant_equalities); if !self.constant_equalities.is_empty() { dbg!("WARNING: constant_equalities not empty"); }