From d776b470014e80cbaf5fccd3317179d7191ba2bf Mon Sep 17 00:00:00 2001 From: Ho Date: Tue, 16 Jan 2024 15:34:57 +0800 Subject: [PATCH] [FIX] sha256 witness issue (#1081) (#1087) * reproduce phase issues in sha256_circuit * fix issues * fmt and clippy * fix gate issue --------- Co-authored-by: kunxian xia --- zkevm-circuits/src/sha256_circuit/circuit.rs | 23 ++++++++++-------- zkevm-circuits/src/sha256_circuit/test.rs | 25 +++++++++++--------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/zkevm-circuits/src/sha256_circuit/circuit.rs b/zkevm-circuits/src/sha256_circuit/circuit.rs index 8e8eebd8a4..3f20471f55 100644 --- a/zkevm-circuits/src/sha256_circuit/circuit.rs +++ b/zkevm-circuits/src/sha256_circuit/circuit.rs @@ -270,12 +270,12 @@ impl CircuitConfig { sha256_table: impl SHA256Table, spec_challenge: Expression, ) -> Self { - let helper = meta.advice_column(); // index 3 - let trans_byte = meta.advice_column(); // index 4 + let copied_data = meta.advice_column(); + let trans_byte = meta.advice_column(); let bytes_rlc = sha256_table.hashes_rlc(); let byte_counter = sha256_table.input_len(); - let copied_data = sha256_table.input_rlc(); + let helper = sha256_table.input_rlc(); let s_output = sha256_table.s_enable(); let s_final_block = sha256_table.is_effect(); @@ -290,6 +290,7 @@ impl CircuitConfig { let byte_range = meta.lookup_table_column(); let table16 = Table16Chip::configure(meta); + meta.enable_equality(helper); meta.enable_equality(copied_data); meta.enable_equality(bytes_rlc); meta.enable_equality(s_final_block); @@ -763,7 +764,7 @@ impl CircuitConfig { input_block.bytes_rlc.copy_advice( || "copy input rlc", &mut region, - self.copied_data, + self.helper, final_row, )?; input_block.byte_counter.copy_advice( @@ -779,12 +780,14 @@ impl CircuitConfig { final_row, )?; - region.assign_advice( - || "flush unused row", - self.trans_byte, - final_row, - || Value::known(Fr::zero()), - )?; + for col in [self.trans_byte, self.copied_data] { + region.assign_advice( + || "flush unused row", + col, + final_row, + || Value::known(Fr::zero()), + )?; + } region.assign_advice( || "flush unused row", diff --git a/zkevm-circuits/src/sha256_circuit/test.rs b/zkevm-circuits/src/sha256_circuit/test.rs index 1b0ce5b45d..9a85cc0e51 100644 --- a/zkevm-circuits/src/sha256_circuit/test.rs +++ b/zkevm-circuits/src/sha256_circuit/test.rs @@ -1,5 +1,5 @@ use halo2_proofs::{ - circuit::{Layouter, SimpleFloorPlanner, Value}, + circuit::{Layouter, SimpleFloorPlanner}, plonk::{create_proof, keygen_pk, keygen_vk, verify_proof, Circuit, ConstraintSystem, Error}, transcript::{Blake2bRead, Blake2bWrite, Challenge255}, }; @@ -7,9 +7,10 @@ use rand::rngs::OsRng; use super::{circuit::*, BLOCK_SIZE}; +use crate::util::Challenges; use halo2_proofs::{ halo2curves::bn256::{Bn256, Fr}, - plonk::{Advice, Any, Column, Expression, Fixed}, + plonk::{Advice, Any, Column, Fixed, SecondPhase}, poly::{ commitment::ParamsProver, kzg::{ @@ -29,7 +30,7 @@ struct MyCircuit { } impl Circuit for MyCircuit { - type Config = CircuitConfig; + type Config = (CircuitConfig, Challenges); type FloorPlanner = SimpleFloorPlanner; fn without_witnesses(&self) -> Self { @@ -59,23 +60,25 @@ impl Circuit for MyCircuit { let dev_table = DevTable { s_enable: meta.fixed_column(), - input_rlc: meta.advice_column(), input_len: meta.advice_column(), - hashes_rlc: meta.advice_column(), + input_rlc: meta.advice_column_in(SecondPhase), + hashes_rlc: meta.advice_column_in(SecondPhase), is_effect: meta.advice_column(), }; meta.enable_constant(dev_table.s_enable); - let chng = Expression::Constant(Fr::from(0x100u64)); - Self::Config::configure(meta, dev_table, chng) + let challenges = Challenges::construct(meta); + let chng = challenges.exprs(meta).keccak_input(); + (CircuitConfig::configure(meta, dev_table, chng), challenges) } fn synthesize( &self, - config: Self::Config, + (config, challenges): Self::Config, mut layouter: impl Layouter, ) -> Result<(), Error> { - let chng_v = Value::known(Fr::from(0x100u64)); + let challenges = challenges.values(&layouter); + let chng_v = challenges.keccak_input(); let mut hasher = Hasher::new(config, &mut layouter)?; for _ in 0..self.blocks { @@ -114,7 +117,7 @@ fn vk_stable() { ¶ms, &pk, &[circuit], - &[], + &[&[]], OsRng, &mut transcript, ) @@ -127,7 +130,7 @@ fn vk_stable() { ¶ms, &vk_from_empty, strategy, - &[], + &[&[]], &mut transcript, ) .unwrap();