From 24e100bd40cf368d474133a06006e8ab991ab521 Mon Sep 17 00:00:00 2001 From: ash Date: Thu, 2 Dec 2021 18:56:44 +0900 Subject: [PATCH] Replace with kate (#199) * replace with kate * fix clippy * update dev graph * igonore evm_word test * fix evm word * fix unexpected changes * update reference * test compilation success * fix from_u64 and fmt * fix test --- Cargo.toml | 2 +- bus-mapping/Cargo.toml | 2 +- bus-mapping/README.md | 2 +- bus-mapping/src/eth_types.rs | 2 +- bus-mapping/src/lib.rs | 2 +- keccak256/Cargo.toml | 4 +- keccak256/src/gates/absorb.rs | 26 +- keccak256/src/gates/gate_helpers.rs | 4 +- keccak256/src/gates/iota_b13.rs | 21 +- keccak256/src/gates/iota_b9.rs | 21 +- keccak256/src/gates/pi.rs | 2 +- keccak256/src/gates/rho.rs | 18 +- keccak256/src/gates/rho_checks.rs | 29 +- keccak256/src/gates/tables.rs | 25 +- keccak256/src/gates/theta.rs | 36 +- keccak256/src/gates/xi.rs | 21 +- zkevm-circuits/Cargo.toml | 6 +- zkevm-circuits/benches/binary_value.rs | 26 +- zkevm-circuits/src/evm_circuit.rs | 88 +++-- .../src/evm_circuit/op_execution.rs | 20 +- .../op_execution/arithmetic/add.rs | 96 +++--- .../op_execution/arithmetic/bitwise.rs | 42 +-- .../src/evm_circuit/op_execution/byte.rs | 46 +-- .../evm_circuit/op_execution/comparator.rs | 50 +-- .../src/evm_circuit/op_execution/dup.rs | 72 ++-- .../src/evm_circuit/op_execution/jump.rs | 24 +- .../src/evm_circuit/op_execution/jumpdest.rs | 16 +- .../src/evm_circuit/op_execution/jumpi.rs | 72 ++-- .../src/evm_circuit/op_execution/memory.rs | 98 +++--- .../src/evm_circuit/op_execution/pc.rs | 24 +- .../src/evm_circuit/op_execution/pop.rs | 32 +- .../src/evm_circuit/op_execution/push.rs | 18 +- .../evm_circuit/op_execution/signextend.rs | 48 +-- .../src/evm_circuit/op_execution/swap.rs | 112 +++---- .../src/evm_circuit/op_execution/utils.rs | 10 +- .../op_execution/utils/common_cases.rs | 2 +- .../op_execution/utils/math_gadgets.rs | 16 +- .../op_execution/utils/memory_gadgets.rs | 4 +- zkevm-circuits/src/gadget.rs | 2 +- zkevm-circuits/src/gadget/evm_word.rs | 51 +-- zkevm-circuits/src/gadget/is_zero.rs | 94 +++--- zkevm-circuits/src/gadget/monotone.rs | 20 +- zkevm-circuits/src/state_circuit/state.rs | 313 +++++++----------- zkevm-circuits/src/util.rs | 6 +- 44 files changed, 762 insertions(+), 863 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7ce30b4f30..eea61af3dd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ members = [ ] [patch.crates-io] -halo2 = { git = "https://github.com/zcash/halo2.git", rev = "4283713ec76051eaf21a06d0279fa7d3497cafb6" } +halo2 = { git = "https://github.com/appliedzkp/halo2.git", rev = "b78c39cacc1c79d287032f1b5f94beb661b3fb42" } # This fork makes bitvec 0.20.x work with funty 1.1 and funty 1.2. Without # this fork, bitvec 0.20.x is incompatible with funty 1.2, which we depend on, # and leads to a compilation error. This can be removed once the upstream PR diff --git a/bus-mapping/Cargo.toml b/bus-mapping/Cargo.toml index d85b41e52a..b96b2b1975 100644 --- a/bus-mapping/Cargo.toml +++ b/bus-mapping/Cargo.toml @@ -6,7 +6,7 @@ authors = ["CPerezz "] [dependencies] ff = "0.11" -pasta_curves = "0.1" +pairing = { git = 'https://github.com/appliedzkp/pairing', package = "pairing_bn256" } itertools = "0.10" serde = {version = "1.0.130", features = ["derive"] } lazy_static = "1.4" diff --git a/bus-mapping/README.md b/bus-mapping/README.md index 1673d23f94..c0a5fabeea 100644 --- a/bus-mapping/README.md +++ b/bus-mapping/README.md @@ -49,7 +49,7 @@ all of the Memory, Stack and Storage ops performed by the provided trace. ```rust use bus_mapping::{ExecutionTrace, ExecutionStep, BlockConstants, Error, evm::EvmWord}; -use pasta_curves::arithmetic::FieldExt; +use pairing::arithmetic::FieldExt; let input_trace = r#" [ diff --git a/bus-mapping/src/eth_types.rs b/bus-mapping/src/eth_types.rs index 8279f2c386..92172f5a84 100644 --- a/bus-mapping/src/eth_types.rs +++ b/bus-mapping/src/eth_types.rs @@ -7,7 +7,7 @@ pub use ethers_core::types::{ transaction::response::Transaction, Address, Block, Bytes, H160, H256, U256, U64, }; -use pasta_curves::arithmetic::FieldExt; +use pairing::arithmetic::FieldExt; use serde::{de, Deserialize}; use std::collections::HashMap; use std::str::FromStr; diff --git a/bus-mapping/src/lib.rs b/bus-mapping/src/lib.rs index afdefa8d75..80d3d1329a 100644 --- a/bus-mapping/src/lib.rs +++ b/bus-mapping/src/lib.rs @@ -53,7 +53,7 @@ //! use bus_mapping::mock; //! use bus_mapping::eth_types::{self, Address, Word, Hash, U64, GethExecTrace, GethExecStep}; //! use bus_mapping::circuit_input_builder::CircuitInputBuilder; -//! use pasta_curves::arithmetic::FieldExt; +//! use pairing::arithmetic::FieldExt; //! //! let input_trace = r#" //! [ diff --git a/keccak256/Cargo.toml b/keccak256/Cargo.toml index 33fe19287d..3a040f408b 100644 --- a/keccak256/Cargo.toml +++ b/keccak256/Cargo.toml @@ -7,9 +7,9 @@ edition = "2018" dev-graph = ["halo2/dev-graph", "plotters"] [dependencies] -halo2 = "0.0" +halo2 = { git = "https://github.com/appliedzkp/halo2.git", rev = "b78c39cacc1c79d287032f1b5f94beb661b3fb42" } itertools = "0.10.1" num-bigint = "0.4.2" num-traits = "0.2.14" -pasta_curves = "0.1" +pairing = { git = 'https://github.com/appliedzkp/pairing', package = "pairing_bn256" } plotters = { version = "0.3.0", optional = true } diff --git a/keccak256/src/gates/absorb.rs b/keccak256/src/gates/absorb.rs index 56437072d4..23a7cdbe36 100644 --- a/keccak256/src/gates/absorb.rs +++ b/keccak256/src/gates/absorb.rs @@ -3,7 +3,7 @@ use halo2::{ plonk::{Advice, Column, ConstraintSystem, Error, Expression, Selector}, poly::Rotation, }; -use pasta_curves::arithmetic::FieldExt; +use pairing::arithmetic::FieldExt; use std::marker::PhantomData; /// The number of next_inputs that are used inside the `absorb` circuit. @@ -105,8 +105,8 @@ mod tests { use halo2::{circuit::SimpleFloorPlanner, dev::MockProver, plonk::Circuit}; use itertools::Itertools; use num_bigint::BigUint; - use pasta_curves::arithmetic::FieldExt; - use pasta_curves::pallas; + use pairing::arithmetic::FieldExt; + use pairing::bn256::Fr as Fp; use std::convert::TryInto; use std::marker::PhantomData; @@ -128,7 +128,7 @@ mod tests { } fn configure(meta: &mut ConstraintSystem) -> Self::Config { - let q_enable = meta.selector(); + let q_enable = meta.complex_selector(); let state: [Column; 25] = (0..25) .map(|_| meta.advice_column()) @@ -174,7 +174,7 @@ mod tests { Ok(()) } } - fn big_uint_to_pallas(a: &BigUint) -> pallas::Base { + fn big_uint_to_pallas(a: &BigUint) -> Fp { let mut b: [u64; 4] = [0; 4]; let mut iter = a.iter_u64_digits(); @@ -185,7 +185,7 @@ mod tests { }; } - pallas::Base::from_raw(b) + Fp::from_raw(b) } let input1: State = [ @@ -207,9 +207,8 @@ mod tests { let mut in_biguint = StateBigInt::default(); let mut next_biguint = StateBigInt::default(); - let mut in_state: [pallas::Base; 25] = [pallas::Base::zero(); 25]; - let mut in_next_input_25: [pallas::Base; 25] = - [pallas::Base::zero(); 25]; + let mut in_state: [Fp; 25] = [Fp::zero(); 25]; + let mut in_next_input_25: [Fp; 25] = [Fp::zero(); 25]; for (x, y) in (0..5).cartesian_product(0..5) { in_biguint[(x, y)] = convert_b2_to_b9(input1[x][y]); @@ -219,15 +218,15 @@ mod tests { big_uint_to_pallas(&next_biguint[(x, y)]); } - let mut in_next_input_17 = [pallas::Base::zero(); ABSORB_NEXT_INPUTS]; + let mut in_next_input_17 = [Fp::zero(); ABSORB_NEXT_INPUTS]; in_next_input_17 .copy_from_slice(&in_next_input_25[0..ABSORB_NEXT_INPUTS]); let s1_arith = KeccakFArith::absorb(&in_biguint, &next_input); - let mut out_state: [pallas::Base; 25] = [pallas::Base::zero(); 25]; + let mut out_state: [Fp; 25] = [Fp::zero(); 25]; for (x, y) in (0..5).cartesian_product(0..5) { out_state[5 * x + y] = big_uint_to_pallas(&s1_arith[(x, y)]); } - let circuit = MyCircuit:: { + let circuit = MyCircuit:: { in_state, next_input: in_next_input_17, out_state, @@ -235,8 +234,7 @@ mod tests { }; // Test without public inputs - let prover = - MockProver::::run(9, &circuit, vec![]).unwrap(); + let prover = MockProver::::run(9, &circuit, vec![]).unwrap(); assert_eq!(prover.verify(), Ok(())); } diff --git a/keccak256/src/gates/gate_helpers.rs b/keccak256/src/gates/gate_helpers.rs index 5bccf85aa4..f935b665f6 100644 --- a/keccak256/src/gates/gate_helpers.rs +++ b/keccak256/src/gates/gate_helpers.rs @@ -1,6 +1,6 @@ use halo2::{circuit::Cell, plonk::Error}; use num_bigint::BigUint; -use pasta_curves::arithmetic::FieldExt; +use pairing::arithmetic::FieldExt; #[derive(Debug, Clone)] pub struct Lane { @@ -22,7 +22,7 @@ pub fn biguint_to_f(x: &BigUint) -> Result { let len = x_bytes.len(); assert!(len <= 32, "expect len <=32 but got {}", len); word[..len].clone_from_slice(&x_bytes[..len]); - Option::from(F::from_bytes(&word)).ok_or(Error::SynthesisError) + Option::from(F::from_bytes(&word)).ok_or(Error::Synthesis) } pub fn f_to_biguint(x: F) -> Option { diff --git a/keccak256/src/gates/iota_b13.rs b/keccak256/src/gates/iota_b13.rs index 7968f1c754..b629451b13 100644 --- a/keccak256/src/gates/iota_b13.rs +++ b/keccak256/src/gates/iota_b13.rs @@ -4,7 +4,7 @@ use halo2::{ plonk::{Advice, Column, ConstraintSystem, Error, Selector}, poly::Rotation, }; -use pasta_curves::arithmetic::FieldExt; +use pairing::arithmetic::FieldExt; use std::marker::PhantomData; #[derive(Clone, Debug)] @@ -90,8 +90,8 @@ mod tests { use halo2::{circuit::SimpleFloorPlanner, dev::MockProver, plonk::Circuit}; use itertools::Itertools; use num_bigint::BigUint; - use pasta_curves::arithmetic::FieldExt; - use pasta_curves::pallas; + use pairing::arithmetic::FieldExt; + use pairing::bn256::Fr as Fp; use std::convert::TryInto; use std::marker::PhantomData; @@ -115,7 +115,7 @@ mod tests { } fn configure(meta: &mut ConstraintSystem) -> Self::Config { - let q_enable = meta.selector(); + let q_enable = meta.complex_selector(); let state: [Column; 25] = (0..25) .map(|_| meta.advice_column()) @@ -168,7 +168,7 @@ mod tests { Ok(()) } } - fn big_uint_to_pallas(a: &BigUint) -> pallas::Base { + fn big_uint_to_pallas(a: &BigUint) -> Fp { let mut b: [u64; 4] = [0; 4]; let mut iter = a.iter_u64_digits(); @@ -179,7 +179,7 @@ mod tests { }; } - pallas::Base::from_raw(b) + Fp::from_raw(b) } let input1: State = [ @@ -190,18 +190,18 @@ mod tests { [0, 0, 0, 0, 0], ]; let mut in_biguint = StateBigInt::default(); - let mut in_state: [pallas::Base; 25] = [pallas::Base::zero(); 25]; + let mut in_state: [Fp; 25] = [Fp::zero(); 25]; for (x, y) in (0..5).cartesian_product(0..5) { in_biguint[(x, y)] = convert_b2_to_b13(input1[x][y]); in_state[5 * x + y] = big_uint_to_pallas(&in_biguint[(x, y)]); } let s1_arith = KeccakFArith::iota_b13(&in_biguint, ROUND_CONSTANTS[0]); - let mut out_state: [pallas::Base; 25] = [pallas::Base::zero(); 25]; + let mut out_state: [Fp; 25] = [Fp::zero(); 25]; for (x, y) in (0..5).cartesian_product(0..5) { out_state[5 * x + y] = big_uint_to_pallas(&s1_arith[(x, y)]); } - let circuit = MyCircuit:: { + let circuit = MyCircuit:: { in_state, out_state, round_ctant_b13: 0, @@ -214,8 +214,7 @@ mod tests { .collect(); // Test without public inputs let prover = - MockProver::::run(9, &circuit, vec![constants]) - .unwrap(); + MockProver::::run(9, &circuit, vec![constants]).unwrap(); assert_eq!(prover.verify(), Ok(())); } diff --git a/keccak256/src/gates/iota_b9.rs b/keccak256/src/gates/iota_b9.rs index 2bae14c427..19ba37c4a5 100644 --- a/keccak256/src/gates/iota_b9.rs +++ b/keccak256/src/gates/iota_b9.rs @@ -4,7 +4,7 @@ use halo2::{ plonk::{Advice, Column, ConstraintSystem, Error, Expression, Selector}, poly::Rotation, }; -use pasta_curves::arithmetic::FieldExt; +use pairing::arithmetic::FieldExt; use std::marker::PhantomData; #[derive(Clone, Debug)] @@ -101,8 +101,8 @@ mod tests { use halo2::{circuit::SimpleFloorPlanner, dev::MockProver, plonk::Circuit}; use itertools::Itertools; use num_bigint::BigUint; - use pasta_curves::arithmetic::FieldExt; - use pasta_curves::pallas; + use pairing::arithmetic::FieldExt; + use pairing::bn256::Fr as Fp; use std::convert::TryInto; use std::marker::PhantomData; @@ -126,7 +126,7 @@ mod tests { } fn configure(meta: &mut ConstraintSystem) -> Self::Config { - let q_enable = meta.selector(); + let q_enable = meta.complex_selector(); let state: [Column; 25] = (0..25) .map(|_| meta.advice_column()) @@ -185,7 +185,7 @@ mod tests { Ok(()) } } - fn big_uint_to_pallas(a: &BigUint) -> pallas::Base { + fn big_uint_to_pallas(a: &BigUint) -> Fp { let mut b: [u64; 4] = [0; 4]; let mut iter = a.iter_u64_digits(); @@ -196,7 +196,7 @@ mod tests { }; } - pallas::Base::from_raw(b) + Fp::from_raw(b) } let input1: State = [ @@ -207,18 +207,18 @@ mod tests { [0, 0, 0, 0, 0], ]; let mut in_biguint = StateBigInt::default(); - let mut in_state: [pallas::Base; 25] = [pallas::Base::zero(); 25]; + let mut in_state: [Fp; 25] = [Fp::zero(); 25]; for (x, y) in (0..5).cartesian_product(0..5) { in_biguint[(x, y)] = convert_b2_to_b9(input1[x][y]); in_state[5 * x + y] = big_uint_to_pallas(&in_biguint[(x, y)]); } let s1_arith = KeccakFArith::iota_b9(&in_biguint, ROUND_CONSTANTS[0]); - let mut out_state: [pallas::Base; 25] = [pallas::Base::zero(); 25]; + let mut out_state: [Fp; 25] = [Fp::zero(); 25]; for (x, y) in (0..5).cartesian_product(0..5) { out_state[5 * x + y] = big_uint_to_pallas(&s1_arith[(x, y)]); } - let circuit = MyCircuit:: { + let circuit = MyCircuit:: { in_state, out_state, round_ctant_b9: 0, @@ -231,8 +231,7 @@ mod tests { .collect(); // Test without public inputs let prover = - MockProver::::run(9, &circuit, vec![constants]) - .unwrap(); + MockProver::::run(9, &circuit, vec![constants]).unwrap(); assert_eq!(prover.verify(), Ok(())); } diff --git a/keccak256/src/gates/pi.rs b/keccak256/src/gates/pi.rs index b715905771..3e8278e39d 100644 --- a/keccak256/src/gates/pi.rs +++ b/keccak256/src/gates/pi.rs @@ -6,7 +6,7 @@ use halo2::{ poly::Rotation, }; use itertools::Itertools; -use pasta_curves::arithmetic::FieldExt; +use pairing::arithmetic::FieldExt; use std::convert::TryInto; use std::marker::PhantomData; diff --git a/keccak256/src/gates/rho.rs b/keccak256/src/gates/rho.rs index 84a16c163b..14e8773ff0 100644 --- a/keccak256/src/gates/rho.rs +++ b/keccak256/src/gates/rho.rs @@ -10,7 +10,7 @@ use halo2::{ plonk::{Advice, Column, ConstraintSystem, Error, Fixed}, }; use itertools::Itertools; -use pasta_curves::arithmetic::FieldExt; +use pairing::arithmetic::FieldExt; use std::convert::TryInto; #[derive(Clone)] @@ -122,8 +122,8 @@ mod tests { use halo2::plonk::{Advice, Column, ConstraintSystem, Error}; use halo2::{circuit::SimpleFloorPlanner, dev::MockProver, plonk::Circuit}; use itertools::Itertools; - use pasta_curves::arithmetic::FieldExt; - use pasta_curves::pallas; + use pairing::arithmetic::FieldExt; + use pairing::bn256::Fr as Fp; use std::convert::TryInto; #[test] fn test_rho_gate() { @@ -230,7 +230,7 @@ mod tests { [0, 0, 0, 0, 0], ]; let mut in_biguint = StateBigInt::default(); - let mut in_state: [pallas::Base; 25] = [pallas::Base::zero(); 25]; + let mut in_state: [Fp; 25] = [Fp::zero(); 25]; for (x, y) in (0..5).cartesian_product(0..5) { in_biguint[(x, y)] = convert_b2_to_b13(input1[x][y]); @@ -240,29 +240,29 @@ mod tests { in_state[5 * x + y] = biguint_to_f(&s0_arith[(x, y)]).unwrap(); } let s1_arith = KeccakFArith::rho(&s0_arith); - let mut out_state: [pallas::Base; 25] = [pallas::Base::zero(); 25]; + let mut out_state: [Fp; 25] = [Fp::zero(); 25]; for (x, y) in (0..5).cartesian_product(0..5) { out_state[5 * x + y] = biguint_to_f(&s1_arith[(x, y)]).unwrap(); } - let circuit = MyCircuit:: { + let circuit = MyCircuit:: { in_state, out_state, }; #[cfg(feature = "dev-graph")] { use plotters::prelude::*; + let k = 15; let root = BitMapBackend::new("rho-test-circuit.png", (4096, 65536)) .into_drawing_area(); root.fill(&WHITE).unwrap(); let root = root.titled("Rho", ("sans-serif", 60)).unwrap(); halo2::dev::CircuitLayout::default() - .render(&circuit, &root) + .render(k, &circuit, &root) .unwrap(); } // Test without public inputs - let prover = - MockProver::::run(15, &circuit, vec![]).unwrap(); + let prover = MockProver::::run(15, &circuit, vec![]).unwrap(); assert_eq!(prover.verify(), Ok(())); } diff --git a/keccak256/src/gates/rho_checks.rs b/keccak256/src/gates/rho_checks.rs index 044c760d07..f1f66c7968 100644 --- a/keccak256/src/gates/rho_checks.rs +++ b/keccak256/src/gates/rho_checks.rs @@ -120,7 +120,7 @@ use halo2::{ }; use num_bigint::BigUint; use num_traits::{One, Zero}; -use pasta_curves::arithmetic::FieldExt; +use pairing::arithmetic::FieldExt; use std::iter; use std::marker::PhantomData; @@ -307,7 +307,7 @@ impl LaneRotateConversionConfig { ) -> Self { meta.enable_equality(adv.input.acc.into()); meta.enable_equality(adv.output.acc.into()); - let q_is_special = meta.selector(); + let q_is_special = meta.complex_selector(); let rotation = ROTATION_CONSTANTS[lane_xy.0][lane_xy.1]; let slices = slice_lane(rotation); let chunk_rotate_convert_configs = slices @@ -359,8 +359,7 @@ impl LaneRotateConversionConfig { region.constrain_equal(lane_base_13.cell, cell)?; let mut rv = RotatingVariables::from( - f_to_biguint(lane_base_13.value) - .ok_or(Error::SynthesisError)?, + f_to_biguint(lane_base_13.value).ok_or(Error::Synthesis)?, self.rotation, )?; let all_block_counts: Result>, Error> = self @@ -376,7 +375,7 @@ impl LaneRotateConversionConfig { .collect(); let all_block_counts = all_block_counts?; let block_counts = - all_block_counts.last().ok_or(Error::SynthesisError)?; + all_block_counts.last().ok_or(Error::Synthesis)?; let lane = self.special_chunk_config.assign_region( &mut region, offset, @@ -420,8 +419,8 @@ impl ChunkRotateConversionConfig { rotation: u32, step: u32, ) -> Self { - let q_enable = meta.selector(); - let q_is_first = meta.selector(); + let q_enable = meta.complex_selector(); + let q_is_first = meta.complex_selector(); let base_13_to_base_9_lookup = Base13toBase9TableConfig::configure( meta, q_enable, @@ -542,7 +541,7 @@ impl ChunkRotateConversionConfig { let block_counts = self.block_count_acc_config.assign_region( region, offset, - rv.block_count.ok_or(Error::SynthesisError)?, + rv.block_count.ok_or(Error::Synthesis)?, rv.block_count_acc, )?; Ok(block_counts) @@ -574,7 +573,7 @@ impl SpecialChunkConfig { - meta.query_advice(base_9_acc, Rotation::cur()); let last_b9_coef = meta.query_advice(last_b9_coef, Rotation::cur()); let pow_of_9 = - Expression::Constant(F::from_u64(B9).pow(&[rotation, 0, 0, 0])); + Expression::Constant(F::from(B9).pow(&[rotation, 0, 0, 0])); vec![( "delta_base_9_acc === (high_value + low_value) * 9**rotation", meta.query_selector(q_enable) @@ -603,7 +602,7 @@ impl SpecialChunkConfig { rv: &RotatingVariables, ) -> Result, Error> { self.q_enable.enable(region, offset)?; - rv.high_value.ok_or(Error::SynthesisError).unwrap(); + rv.high_value.ok_or(Error::Synthesis).unwrap(); region.assign_advice( || "input_acc", self.base_13_acc, @@ -659,8 +658,8 @@ impl BlockCountAccConfig { bc: BlockCountAdvices, step: u32, ) -> Self { - let q_first = meta.selector(); - let q_rest = meta.selector(); + let q_first = meta.complex_selector(); + let q_rest = meta.complex_selector(); if step == 1 { meta.create_gate("block count step 1", |meta| { let q_all = meta.query_selector(q_all); @@ -749,8 +748,8 @@ impl BlockCountAccConfig { self.q_rest.enable(region, offset)?; } - let block_count = F::from_u64(block_count.into()); - let acc = block_count_acc.map(|x| F::from_u64(x.into())); + let block_count = F::from(block_count.into()); + let acc = block_count_acc.map(|x| F::from(x.into())); region.assign_advice( || format!("block count step{}", self.step), self.bc.block_count, @@ -792,7 +791,7 @@ impl BlockCountFinalConfig { meta: &mut ConstraintSystem, block_count_cols: [Column; 2], ) -> Self { - let q_enable = meta.selector(); + let q_enable = meta.complex_selector(); for column in block_count_cols.iter() { meta.enable_equality((*column).into()); } diff --git a/keccak256/src/gates/tables.rs b/keccak256/src/gates/tables.rs index 8f6c558aba..ec5687af04 100644 --- a/keccak256/src/gates/tables.rs +++ b/keccak256/src/gates/tables.rs @@ -39,7 +39,7 @@ impl Base13toBase9TableConfig { self.base13, i, || { - Ok(F::from_u64( + Ok(F::from( b13_chunks .iter() .fold(0, |acc, x| acc * B13 + *x), @@ -52,11 +52,9 @@ impl Base13toBase9TableConfig { self.base9, i, || { - Ok(F::from_u64( - b13_chunks.iter().fold(0, |acc, x| { - acc * B9 + convert_b13_coef(*x) - }), - )) + Ok(F::from(b13_chunks.iter().fold(0, |acc, x| { + acc * B9 + convert_b13_coef(*x) + }))) }, )?; region.assign_fixed( @@ -64,7 +62,7 @@ impl Base13toBase9TableConfig { self.block_count, i, || { - Ok(F::from_u64( + Ok(F::from( get_block_count( b13_chunks.clone().try_into().unwrap(), ) @@ -93,7 +91,7 @@ impl Base13toBase9TableConfig { _marker: PhantomData, }; - meta.lookup(|meta| { + meta.lookup_any(|meta| { let q_enable = meta.query_selector(q_enable); let base13_coef = meta.query_advice(base13_coef, Rotation::cur()); let base9_coef = meta.query_advice(base9_coef, Rotation::cur()); @@ -138,16 +136,15 @@ impl SpecialChunkTableConfig { for i in 0..B13 { for j in 0..(B13 - i) { let (low, high) = (i, j); - let last_chunk = F::from_u64(low) - + F::from_u64(high) - * F::from_u64(B13).pow(&[ + let last_chunk = F::from(low) + + F::from(high) + * F::from(B13).pow(&[ LANE_SIZE as u64, 0, 0, 0, ]); - let output_coef = - F::from_u64(convert_b13_coef(low + high)); + let output_coef = F::from(convert_b13_coef(low + high)); region.assign_fixed( || "last chunk", self.last_chunk, @@ -181,7 +178,7 @@ impl SpecialChunkTableConfig { _marker: PhantomData, }; // Lookup for special chunk conversion - meta.lookup(|meta| { + meta.lookup_any(|meta| { let q_enable = meta.query_selector(q_enable); let last_chunk_advice = meta.query_advice(last_chunk_advice, Rotation::cur()); diff --git a/keccak256/src/gates/theta.rs b/keccak256/src/gates/theta.rs index 10904b8eb9..01f410d5aa 100644 --- a/keccak256/src/gates/theta.rs +++ b/keccak256/src/gates/theta.rs @@ -5,7 +5,7 @@ use halo2::{ poly::Rotation, }; use itertools::Itertools; -use pasta_curves::arithmetic::FieldExt; +use pairing::arithmetic::FieldExt; use std::marker::PhantomData; #[derive(Clone, Debug)] @@ -88,12 +88,12 @@ mod tests { use crate::keccak_arith::*; use halo2::{ circuit::{Layouter, SimpleFloorPlanner}, - dev::{MockProver, VerifyFailure}, + dev::MockProver, plonk::{Advice, Circuit, Column, ConstraintSystem, Error}, }; use itertools::Itertools; use num_bigint::BigUint; - use pasta_curves::{arithmetic::FieldExt, pallas}; + use pairing::{arithmetic::FieldExt, bn256::Fr as Fp}; use std::convert::TryInto; use std::marker::PhantomData; @@ -114,7 +114,7 @@ mod tests { } fn configure(meta: &mut ConstraintSystem) -> Self::Config { - let q_enable = meta.selector(); + let q_enable = meta.complex_selector(); let state: [Column; 25] = (0..25) .map(|_| meta.advice_column()) @@ -148,7 +148,7 @@ mod tests { Ok(()) } } - fn big_uint_to_pallas(a: &BigUint) -> pallas::Base { + fn big_uint_to_pallas(a: &BigUint) -> Fp { let mut b: [u64; 4] = [0; 4]; let mut iter = a.iter_u64_digits(); @@ -159,7 +159,7 @@ mod tests { }; } - pallas::Base::from_raw(b) + Fp::from_raw(b) } let input1: State = [ @@ -170,47 +170,39 @@ mod tests { [0, 0, 0, 0, 0], ]; let mut in_biguint = StateBigInt::default(); - let mut in_state: [pallas::Base; 25] = [pallas::Base::zero(); 25]; + let mut in_state: [Fp; 25] = [Fp::zero(); 25]; for (x, y) in (0..5).cartesian_product(0..5) { in_biguint[(x, y)] = convert_b2_to_b13(input1[x][y]); in_state[5 * x + y] = big_uint_to_pallas(&in_biguint[(x, y)]); } let s1_arith = KeccakFArith::theta(&in_biguint); - let mut out_state: [pallas::Base; 25] = [pallas::Base::zero(); 25]; + let mut out_state: [Fp; 25] = [Fp::zero(); 25]; for (x, y) in (0..5).cartesian_product(0..5) { out_state[5 * x + y] = big_uint_to_pallas(&s1_arith[(x, y)]); } - let circuit = MyCircuit:: { + let circuit = MyCircuit:: { in_state, out_state, _marker: PhantomData, }; // Test without public inputs - let prover = - MockProver::::run(9, &circuit, vec![]).unwrap(); + let prover = MockProver::::run(9, &circuit, vec![]).unwrap(); assert_eq!(prover.verify(), Ok(())); let mut out_state2 = out_state; - out_state2[0] = pallas::Base::from(5566u64); + out_state2[0] = Fp::from(5566u64); - let circuit2 = MyCircuit:: { + let circuit2 = MyCircuit:: { in_state, out_state: out_state2, _marker: PhantomData, }; - let prover = - MockProver::::run(9, &circuit2, vec![]).unwrap(); - assert_eq!( - prover.verify(), - Err(vec![VerifyFailure::ConstraintNotSatisfied { - constraint: ((0, "theta").into(), 0, "").into(), - row: 0 - }]) - ); + let prover = MockProver::::run(9, &circuit2, vec![]).unwrap(); + assert!(prover.verify().is_err()); } } diff --git a/keccak256/src/gates/xi.rs b/keccak256/src/gates/xi.rs index 4272208321..c9d381e388 100644 --- a/keccak256/src/gates/xi.rs +++ b/keccak256/src/gates/xi.rs @@ -4,7 +4,7 @@ use halo2::{ poly::Rotation, }; use itertools::Itertools; -use pasta_curves::arithmetic::FieldExt; +use pairing::arithmetic::FieldExt; use std::marker::PhantomData; #[derive(Clone, Debug)] @@ -96,8 +96,8 @@ mod tests { use halo2::{circuit::SimpleFloorPlanner, dev::MockProver, plonk::Circuit}; use itertools::Itertools; use num_bigint::BigUint; - use pasta_curves::arithmetic::FieldExt; - use pasta_curves::pallas; + use pairing::arithmetic::FieldExt; + use pairing::bn256::Fr as Fp; use std::convert::TryInto; use std::marker::PhantomData; @@ -118,7 +118,7 @@ mod tests { } fn configure(meta: &mut ConstraintSystem) -> Self::Config { - let q_enable = meta.selector(); + let q_enable = meta.complex_selector(); let state: [Column; 25] = (0..25) .map(|_| meta.advice_column()) @@ -152,7 +152,7 @@ mod tests { Ok(()) } } - fn big_uint_to_pallas(a: &BigUint) -> pallas::Base { + fn big_uint_to_pallas(a: &BigUint) -> Fp { let mut b: [u64; 4] = [0; 4]; let mut iter = a.iter_u64_digits(); @@ -163,7 +163,7 @@ mod tests { }; } - pallas::Base::from_raw(b) + Fp::from_raw(b) } let input1: State = [ @@ -174,26 +174,25 @@ mod tests { [0, 0, 0, 0, 0], ]; let mut in_biguint = StateBigInt::default(); - let mut in_state: [pallas::Base; 25] = [pallas::Base::zero(); 25]; + let mut in_state: [Fp; 25] = [Fp::zero(); 25]; for (x, y) in (0..5).cartesian_product(0..5) { in_biguint[(x, y)] = convert_b2_to_b9(input1[x][y]); in_state[5 * x + y] = big_uint_to_pallas(&in_biguint[(x, y)]); } let s1_arith = KeccakFArith::xi(&in_biguint); - let mut out_state: [pallas::Base; 25] = [pallas::Base::zero(); 25]; + let mut out_state: [Fp; 25] = [Fp::zero(); 25]; for (x, y) in (0..5).cartesian_product(0..5) { out_state[5 * x + y] = big_uint_to_pallas(&s1_arith[(x, y)]); } - let circuit = MyCircuit:: { + let circuit = MyCircuit:: { in_state, out_state, _marker: PhantomData, }; // Test without public inputs - let prover = - MockProver::::run(9, &circuit, vec![]).unwrap(); + let prover = MockProver::::run(9, &circuit, vec![]).unwrap(); assert_eq!(prover.verify(), Ok(())); } diff --git a/zkevm-circuits/Cargo.toml b/zkevm-circuits/Cargo.toml index 4382330528..28a50b9cf1 100644 --- a/zkevm-circuits/Cargo.toml +++ b/zkevm-circuits/Cargo.toml @@ -8,8 +8,8 @@ edition = "2018" [dependencies] ff = "0.11" -halo2 = "0.0" -pasta_curves = "0.1" +halo2 = { git = "https://github.com/appliedzkp/halo2.git", rev = "b78c39cacc1c79d287032f1b5f94beb661b3fb42" } +pairing = { git = 'https://github.com/appliedzkp/pairing', package = "pairing_bn256" } bigint = "4" num = "0.4" sha3 = "0.7.2" @@ -18,6 +18,8 @@ array-init = "2.0.0" paste = "1.0" bus-mapping = { path = "../bus-mapping"} serde_json = "1.0.66" +rand_xorshift = "0.3" +rand = "0.8" [dev-dependencies] criterion = "0.3" diff --git a/zkevm-circuits/benches/binary_value.rs b/zkevm-circuits/benches/binary_value.rs index 76131d026a..4b9faf3d93 100644 --- a/zkevm-circuits/benches/binary_value.rs +++ b/zkevm-circuits/benches/binary_value.rs @@ -10,7 +10,7 @@ use halo2::{ poly::Rotation, }; -use pasta_curves::{arithmetic::FieldExt, pallas}; +use pairing::{arithmetic::FieldExt, bn256::Fr as Fp}; #[derive(Copy, Clone, Debug)] struct MemoryAddress(F); @@ -82,7 +82,7 @@ impl Config { let binary_table = meta.fixed_column(); if LOOKUP { - meta.lookup(|meta| { + meta.lookup_any(|meta| { let q_target = meta.query_fixed(q_target, Rotation::cur()); let flag = meta.query_advice(flag, Rotation::cur()); let binary_table = @@ -129,7 +129,7 @@ impl Config { || "binary table", self.binary_table, idx, - || Ok(F::from_u64(idx as u64)), + || Ok(F::from(idx as u64)), )?; } Ok(()) @@ -244,14 +244,14 @@ impl Config { let value = read_write .as_ref() .map(|read_write| read_write.global_counter().0); - let field_elem = value.map(|value| F::from_u64(value as u64)); + let field_elem = value.map(|value| F::from(value as u64)); region .assign_advice( || "global counter", self.global_counter, offset, - || field_elem.ok_or(Error::SynthesisError), + || field_elem.ok_or(Error::Synthesis), ) .ok(); @@ -262,18 +262,18 @@ impl Config { || "value", self.value, offset, - || value.ok_or(Error::SynthesisError), + || value.ok_or(Error::Synthesis), ) .ok(); let value = read_write.as_ref().map(|read_write| read_write.flag()); - let field_elem = value.map(|value| F::from_u64(value as u64)); + let field_elem = value.map(|value| F::from(value as u64)); region .assign_advice( || "flag", self.flag, offset, - || field_elem.ok_or(Error::SynthesisError), + || field_elem.ok_or(Error::Synthesis), ) .ok(); } @@ -314,27 +314,27 @@ macro_rules! test_state_circuit { let mut ops = vec![]; for _i in 0..10000 { let op = MemoryOp { - address: MemoryAddress(pallas::Base::zero()), + address: MemoryAddress(Fp::zero()), global_counters: vec![ Some(ReadWrite::Write( GlobalCounter(12), - Value(pallas::Base::from_u64(12)), + Value(Fp::from(12)), )), Some(ReadWrite::Read( GlobalCounter(24), - Value(pallas::Base::from_u64(12)), + Value(Fp::from(12)), )), ], }; ops.push(op); } - let circuit = MemoryCircuit:: { + let circuit = MemoryCircuit:: { ops, _marker: PhantomData, }; - let prover = MockProver::::run(7, &circuit, vec![]).unwrap(); + let prover = MockProver::::run(7, &circuit, vec![]).unwrap(); assert_eq!(prover.verify(), Ok(())); }}; } diff --git a/zkevm-circuits/src/evm_circuit.rs b/zkevm-circuits/src/evm_circuit.rs index 2f807548b1..fe2fce2543 100644 --- a/zkevm-circuits/src/evm_circuit.rs +++ b/zkevm-circuits/src/evm_circuit.rs @@ -153,7 +153,7 @@ pub(crate) enum FixedLookup { impl Expr for FixedLookup { fn expr(&self) -> Expression { - Expression::Constant(F::from_u64(*self as u64)) + Expression::Constant(F::from(*self as u64)) } } @@ -199,7 +199,7 @@ impl Cell { }, self.column, offset + self.rotation, - || value.ok_or(Error::SynthesisError), + || value.ok_or(Error::Synthesis), ) } } @@ -237,12 +237,12 @@ impl Word { offset: usize, word: Option<[u8; 32]>, ) -> Result, Error> { - word.map_or(Err(Error::SynthesisError), |word| { + word.map_or(Err(Error::Synthesis), |word| { self.cells .iter() .zip(word.iter()) .map(|(cell, byte)| { - cell.assign(region, offset, Some(F::from_u64(*byte as u64))) + cell.assign(region, offset, Some(F::from(*byte as u64))) }) .collect() }) @@ -341,7 +341,7 @@ struct EvmCircuit { impl EvmCircuit { fn configure(meta: &mut ConstraintSystem, r: F) -> Self { - let q_step = meta.selector(); + let q_step = meta.complex_selector(); let qs_byte_lookup = meta.advice_column(); let advices = (0..CIRCUIT_WIDTH) .map(|_| meta.advice_column()) @@ -665,7 +665,7 @@ impl EvmCircuit { // Configure whole row lookups by qs_byte_lookup for column in advices { - meta.lookup(|meta| { + meta.lookup_any(|meta| { let tag = FixedLookup::Range256.expr(); let qs_byte_lookup = meta.query_advice(qs_byte_lookup, Rotation::cur()); @@ -686,7 +686,7 @@ impl EvmCircuit { // Configure fixed lookups for fixed_lookup in fixed_lookups.iter() { - meta.lookup(|meta| { + meta.lookup_any(|meta| { fixed_lookup .iter() .zip(fixed_table.iter()) @@ -702,7 +702,7 @@ impl EvmCircuit { // Configure byte code lookups for bytecode_lookup in bytecode_lookups.iter() { - meta.lookup(|meta| { + meta.lookup_any(|meta| { bytecode_lookup .iter() .zip(bytecode_table.iter()) @@ -717,7 +717,7 @@ impl EvmCircuit { } // Configure rw lookups for rw_lookup in rw_lookups.iter() { - meta.lookup(|meta| { + meta.lookup_any(|meta| { rw_lookup .iter() .zip(rw_table.iter()) @@ -759,13 +759,13 @@ impl EvmCircuit { || "Range256: tag", self.fixed_table[0], offset, - || Ok(F::from_u64(FixedLookup::Range256 as u64)), + || Ok(F::from(FixedLookup::Range256 as u64)), )?; region.assign_fixed( || "Range256: value", self.fixed_table[1], offset, - || Ok(F::from_u64(idx as u64)), + || Ok(F::from(idx as u64)), )?; for (idx, column) in self.fixed_table[2..].iter().enumerate() @@ -786,13 +786,13 @@ impl EvmCircuit { || "Range32: tag", self.fixed_table[0], offset, - || Ok(F::from_u64(FixedLookup::Range32 as u64)), + || Ok(F::from(FixedLookup::Range32 as u64)), )?; region.assign_fixed( || "Range32: value", self.fixed_table[1], offset, - || Ok(F::from_u64(idx as u64)), + || Ok(F::from(idx as u64)), )?; for (idx, column) in self.fixed_table[2..].iter().enumerate() @@ -813,13 +813,13 @@ impl EvmCircuit { || "Range17: tag", self.fixed_table[0], offset, - || Ok(F::from_u64(FixedLookup::Range17 as u64)), + || Ok(F::from(FixedLookup::Range17 as u64)), )?; region.assign_fixed( || "Range17: value", self.fixed_table[1], offset, - || Ok(F::from_u64(idx as u64)), + || Ok(F::from(idx as u64)), )?; for (idx, column) in self.fixed_table[2..].iter().enumerate() @@ -840,13 +840,13 @@ impl EvmCircuit { || "Range16: tag", self.fixed_table[0], offset, - || Ok(F::from_u64(FixedLookup::Range16 as u64)), + || Ok(F::from(FixedLookup::Range16 as u64)), )?; region.assign_fixed( || "Range16: value", self.fixed_table[1], offset, - || Ok(F::from_u64(idx as u64)), + || Ok(F::from(idx as u64)), )?; for (idx, column) in self.fixed_table[2..].iter().enumerate() @@ -867,13 +867,13 @@ impl EvmCircuit { || "Range512: tag", self.fixed_table[0], offset, - || Ok(F::from_u64(FixedLookup::Range512 as u64)), + || Ok(F::from(FixedLookup::Range512 as u64)), )?; region.assign_fixed( || "Range512: value", self.fixed_table[1], offset, - || Ok(F::from_u64(idx as u64)), + || Ok(F::from(idx as u64)), )?; for (idx, column) in self.fixed_table[2..].iter().enumerate() @@ -894,19 +894,19 @@ impl EvmCircuit { || "SignByte: tag", self.fixed_table[0], offset, - || Ok(F::from_u64(FixedLookup::SignByte as u64)), + || Ok(F::from(FixedLookup::SignByte as u64)), )?; region.assign_fixed( || "SignByte: value", self.fixed_table[1], offset, - || Ok(F::from_u64(idx as u64)), + || Ok(F::from(idx as u64)), )?; region.assign_fixed( || "SignByte: sign", self.fixed_table[2], offset, - || Ok(F::from_u64((idx >> 7) * 0xFFu64)), + || Ok(F::from((idx >> 7) * 0xFFu64)), )?; for (idx, column) in self.fixed_table[3..].iter().enumerate() @@ -930,29 +930,25 @@ impl EvmCircuit { || "BitwiseAnd: tag", self.fixed_table[0], offset, - || { - Ok(F::from_u64( - FixedLookup::BitwiseAnd as u64, - )) - }, + || Ok(F::from(FixedLookup::BitwiseAnd as u64)), )?; region.assign_fixed( || "BitwiseAnd: a", self.fixed_table[1], offset, - || Ok(F::from_u64(a)), + || Ok(F::from(a)), )?; region.assign_fixed( || "BitwiseAnd: b", self.fixed_table[2], offset, - || Ok(F::from_u64(b)), + || Ok(F::from(b)), )?; region.assign_fixed( || "BitwiseAnd: a&b", self.fixed_table[3], offset, - || Ok(F::from_u64(c)), + || Ok(F::from(c)), )?; for (idx, column) in self.fixed_table[4..].iter().enumerate() @@ -976,29 +972,25 @@ impl EvmCircuit { || "BitwiseOr: tag", self.fixed_table[0], offset, - || { - Ok(F::from_u64( - FixedLookup::BitwiseOr as u64, - )) - }, + || Ok(F::from(FixedLookup::BitwiseOr as u64)), )?; region.assign_fixed( || "BitwiseOr: a", self.fixed_table[1], offset, - || Ok(F::from_u64(a)), + || Ok(F::from(a)), )?; region.assign_fixed( || "BitwiseOr: b", self.fixed_table[2], offset, - || Ok(F::from_u64(b)), + || Ok(F::from(b)), )?; region.assign_fixed( || "BitwiseOr: a|b", self.fixed_table[3], offset, - || Ok(F::from_u64(c)), + || Ok(F::from(c)), )?; for (idx, column) in self.fixed_table[4..].iter().enumerate() @@ -1022,29 +1014,25 @@ impl EvmCircuit { || "BitwiseXor: tag", self.fixed_table[0], offset, - || { - Ok(F::from_u64( - FixedLookup::BitwiseXor as u64, - )) - }, + || Ok(F::from(FixedLookup::BitwiseXor as u64)), )?; region.assign_fixed( || "BitwiseXor: a", self.fixed_table[1], offset, - || Ok(F::from_u64(a)), + || Ok(F::from(a)), )?; region.assign_fixed( || "BitwiseXor: b", self.fixed_table[2], offset, - || Ok(F::from_u64(b)), + || Ok(F::from(b)), )?; region.assign_fixed( || "BitwiseXor: a^b", self.fixed_table[3], offset, - || Ok(F::from_u64(c)), + || Ok(F::from(c)), )?; for (idx, column) in self.fixed_table[4..].iter().enumerate() @@ -1096,9 +1084,9 @@ impl EvmCircuit { let values = [ vec![ - F::from_u64(operation.gc as u64), - F::from_u64(operation.target as u64), - F::from_u64(operation.is_write as u64), + F::from(operation.gc as u64), + F::from(operation.target as u64), + F::from(operation.is_write as u64), ], operation.values.to_vec(), ] @@ -1148,7 +1136,7 @@ impl EvmCircuit { || "bytecode table", *column, offset, - || Ok(F::from_u64(*value as u64)), + || Ok(F::from(*value as u64)), )?; } offset += 1; diff --git a/zkevm-circuits/src/evm_circuit/op_execution.rs b/zkevm-circuits/src/evm_circuit/op_execution.rs index 3ec88a25b3..7b0a42ec6e 100644 --- a/zkevm-circuits/src/evm_circuit/op_execution.rs +++ b/zkevm-circuits/src/evm_circuit/op_execution.rs @@ -419,7 +419,7 @@ impl OpExecutionGadget { for idx in 0..case_configs.len() { preset .free_cells - .push((idx, F::from_u64((idx == q_case_idx) as u64))); + .push((idx, F::from((idx == q_case_idx) as u64))); } let (word_ranges, cell_idxs, unused_idxs) = @@ -460,7 +460,7 @@ impl OpExecutionGadget { .iter() .enumerate() .map(|(idx, value)| { - if value.is_zero() { + if value.is_zero().into() { // constraint qs_byte_lookup to 0 by default qs_byte_lookups[idx].expr() } else { @@ -526,39 +526,39 @@ impl OpExecutionGadget { self.state_curr.global_counter.assign( region, offset, - Some(F::from_u64(core_state.global_counter as u64)), + Some(F::from(core_state.global_counter as u64)), )?; self.state_curr.call_id.assign( region, offset, - Some(F::from_u64(core_state.call_id as u64)), + Some(F::from(core_state.call_id as u64)), )?; self.state_curr.program_counter.assign( region, offset, - Some(F::from_u64(core_state.program_counter as u64)), + Some(F::from(core_state.program_counter as u64)), )?; self.state_curr.stack_pointer.assign( region, offset, - Some(F::from_u64(core_state.stack_pointer as u64)), + Some(F::from(core_state.stack_pointer as u64)), )?; self.state_curr.gas_counter.assign( region, offset, - Some(F::from_u64(core_state.gas_counter as u64)), + Some(F::from(core_state.gas_counter as u64)), )?; self.state_curr.memory_size.assign( region, offset, - Some(F::from_u64(core_state.memory_size as u64)), + Some(F::from(core_state.memory_size as u64)), )?; if let Some(execution_step) = execution_step { self.state_curr.opcode.assign( region, offset, - Some(F::from_u64(execution_step.opcode.as_u8() as u64)), + Some(F::from(execution_step.opcode.as_u8() as u64)), )?; let &qs_op_idx = self @@ -569,7 +569,7 @@ impl OpExecutionGadget { q_op.assign( region, offset, - Some(F::from_u64((idx == qs_op_idx) as u64)), + Some(F::from((idx == qs_op_idx) as u64)), )?; } diff --git a/zkevm-circuits/src/evm_circuit/op_execution/arithmetic/add.rs b/zkevm-circuits/src/evm_circuit/op_execution/arithmetic/add.rs index cdc3aeff78..ac427d86bb 100644 --- a/zkevm-circuits/src/evm_circuit/op_execution/arithmetic/add.rs +++ b/zkevm-circuits/src/evm_circuit/op_execution/arithmetic/add.rs @@ -135,9 +135,9 @@ impl AddSuccessCase { self.swap.assign( region, offset, - F::from_u64(step.opcode.as_u64()), - F::from_u64(OpcodeId::SUB.as_u64()), - F::from_u64(OpcodeId::ADD.as_u64()), + F::from(step.opcode.as_u64()), + F::from(OpcodeId::SUB.as_u64()), + F::from(OpcodeId::ADD.as_u64()), )?; // Generate carry values @@ -145,7 +145,7 @@ impl AddSuccessCase { .iter() .zip(step.values[3].to_word().iter()) .map(|(alloc, carry)| { - alloc.assign(region, offset, Some(F::from_u64(*carry as u64))) + alloc.assign(region, offset, Some(F::from(*carry as u64))) }) .collect::, _>>()?; @@ -162,15 +162,15 @@ mod test { test::TestCircuit, Case, ExecutionStep, Operation, }; use bus_mapping::{evm::OpcodeId, operation::Target}; - use halo2::{arithmetic::FieldExt, dev::MockProver}; + use halo2::dev::MockProver; use num::BigUint; - use pasta_curves::pallas::Base; + use pairing::bn256::Fr as Fp; macro_rules! try_test_circuit { ($execution_steps:expr, $operations:expr, $result:expr) => {{ let circuit = - TestCircuit::::new($execution_steps, $operations, false); - let prover = MockProver::::run(11, &circuit, vec![]).unwrap(); + TestCircuit::::new($execution_steps, $operations, false); + let prover = MockProver::::run(11, &circuit, vec![]).unwrap(); assert_eq!(prover.verify(), $result); }}; } @@ -216,10 +216,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1023), - Base::from_u64(1 + 2 + 3), - Base::zero(), + Fp::zero(), + Fp::from(1023), + Fp::from(1 + 2 + 3), + Fp::zero(), ] }, Operation { @@ -227,10 +227,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1022), - Base::from_u64(4 + 5 + 6), - Base::zero(), + Fp::zero(), + Fp::from(1022), + Fp::from(4 + 5 + 6), + Fp::zero(), ] }, Operation { @@ -238,10 +238,10 @@ mod test { target: Target::Stack, is_write: false, values: [ - Base::zero(), - Base::from_u64(1022), - Base::from_u64(4 + 5 + 6), - Base::zero(), + Fp::zero(), + Fp::from(1022), + Fp::from(4 + 5 + 6), + Fp::zero(), ] }, Operation { @@ -249,10 +249,10 @@ mod test { target: Target::Stack, is_write: false, values: [ - Base::zero(), - Base::from_u64(1023), - Base::from_u64(1 + 2 + 3), - Base::zero(), + Fp::zero(), + Fp::from(1023), + Fp::from(1 + 2 + 3), + Fp::zero(), ] }, Operation { @@ -260,10 +260,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1023), - Base::from_u64(5 + 7 + 9), - Base::zero(), + Fp::zero(), + Fp::from(1023), + Fp::from(5 + 7 + 9), + Fp::zero(), ] } ], @@ -305,10 +305,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1023), - Base::from_u64(5 + 7 + 9), - Base::zero(), + Fp::zero(), + Fp::from(1023), + Fp::from(5 + 7 + 9), + Fp::zero(), ] }, Operation { @@ -316,10 +316,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1022), - Base::from_u64(4 + 5 + 6), - Base::zero(), + Fp::zero(), + Fp::from(1022), + Fp::from(4 + 5 + 6), + Fp::zero(), ] }, Operation { @@ -327,10 +327,10 @@ mod test { target: Target::Stack, is_write: false, values: [ - Base::zero(), - Base::from_u64(1022), - Base::from_u64(4 + 5 + 6), - Base::zero(), + Fp::zero(), + Fp::from(1022), + Fp::from(4 + 5 + 6), + Fp::zero(), ] }, Operation { @@ -338,10 +338,10 @@ mod test { target: Target::Stack, is_write: false, values: [ - Base::zero(), - Base::from_u64(1023), - Base::from_u64(5 + 7 + 9), - Base::zero(), + Fp::zero(), + Fp::from(1023), + Fp::from(5 + 7 + 9), + Fp::zero(), ] }, Operation { @@ -349,10 +349,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1023), - Base::from_u64(1 + 2 + 3), - Base::zero(), + Fp::zero(), + Fp::from(1023), + Fp::from(1 + 2 + 3), + Fp::zero(), ] } ], diff --git a/zkevm-circuits/src/evm_circuit/op_execution/arithmetic/bitwise.rs b/zkevm-circuits/src/evm_circuit/op_execution/arithmetic/bitwise.rs index 8f41f8936c..b57ccbb3a9 100644 --- a/zkevm-circuits/src/evm_circuit/op_execution/arithmetic/bitwise.rs +++ b/zkevm-circuits/src/evm_circuit/op_execution/arithmetic/bitwise.rs @@ -144,21 +144,21 @@ mod test { }; use crate::{gadget::evm_word::encode, util::ToWord}; use bus_mapping::{evm::OpcodeId, operation::Target}; - use halo2::{arithmetic::FieldExt, dev::MockProver}; + use halo2::dev::MockProver; use num::BigUint; - use pasta_curves::pallas::Base; + use pairing::bn256::Fr as Fp; macro_rules! try_test_circuit { ($execution_steps:expr, $operations:expr, $result:expr) => {{ let circuit = - TestCircuit::::new($execution_steps, $operations, true); - let prover = MockProver::::run(18, &circuit, vec![]).unwrap(); + TestCircuit::::new($execution_steps, $operations, true); + let prover = MockProver::::run(18, &circuit, vec![]).unwrap(); assert_eq!(prover.verify(), $result); }}; } - fn compress(value: BigUint) -> Base { - let r = Base::from_u64(1); + fn compress(value: BigUint) -> Fp { + let r = Fp::from(1); encode(value.to_word().to_vec().into_iter().rev(), r) } @@ -188,10 +188,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1023), + Fp::zero(), + Fp::from(1023), compress(b.clone()), - Base::zero(), + Fp::zero(), ] }, Operation { @@ -199,10 +199,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1022), + Fp::zero(), + Fp::from(1022), compress(a.clone()), - Base::zero(), + Fp::zero(), ] }, Operation { @@ -210,10 +210,10 @@ mod test { target: Target::Stack, is_write: false, values: [ - Base::zero(), - Base::from_u64(1022), + Fp::zero(), + Fp::from(1022), compress(a), - Base::zero(), + Fp::zero(), ] }, Operation { @@ -221,10 +221,10 @@ mod test { target: Target::Stack, is_write: false, values: [ - Base::zero(), - Base::from_u64(1023), + Fp::zero(), + Fp::from(1023), compress(b), - Base::zero(), + Fp::zero(), ] }, Operation { @@ -232,10 +232,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1023), + Fp::zero(), + Fp::from(1023), compress(c), - Base::zero(), + Fp::zero(), ] } ], diff --git a/zkevm-circuits/src/evm_circuit/op_execution/byte.rs b/zkevm-circuits/src/evm_circuit/op_execution/byte.rs index 082dcaa3af..692f35cf40 100644 --- a/zkevm-circuits/src/evm_circuit/op_execution/byte.rs +++ b/zkevm-circuits/src/evm_circuit/op_execution/byte.rs @@ -143,8 +143,8 @@ impl ByteSuccessCase { self.is_byte_selected[i].assign( region, offset, - F::from_u64(step.values[0].to_word()[0] as u64), - F::from_u64((31 - i) as u64), + F::from(step.values[0].to_word()[0] as u64), + F::from((31 - i) as u64), )?; } @@ -161,24 +161,24 @@ mod test { test::TestCircuit, Case, ExecutionStep, Operation, }; use bus_mapping::{evm::OpcodeId, operation::Target}; - use halo2::{arithmetic::FieldExt, dev::MockProver}; + use halo2::dev::MockProver; use num::BigUint; - use pasta_curves::pallas::Base; + use pairing::bn256::Fr as Fp; macro_rules! try_test_circuit { ($execution_steps:expr, $operations:expr, $result:expr) => {{ let circuit = - TestCircuit::::new($execution_steps, $operations, false); - let prover = MockProver::::run(11, &circuit, vec![]).unwrap(); + TestCircuit::::new($execution_steps, $operations, false); + let prover = MockProver::::run(11, &circuit, vec![]).unwrap(); assert_eq!(prover.verify(), $result); }}; } - fn compress(value: BigUint) -> Base { + fn compress(value: BigUint) -> Fp { value .to_bytes_le() .iter() - .fold(Base::zero(), |acc, val| acc + Base::from_u64(*val as u64)) + .fold(Fp::zero(), |acc, val| acc + Fp::from(*val as u64)) } fn check_byte_gadget(value: BigUint, index: BigUint, result: BigUint) { @@ -207,10 +207,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1023), + Fp::zero(), + Fp::from(1023), compress(value.clone()), - Base::zero(), + Fp::zero(), ] }, Operation { @@ -218,10 +218,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1022), + Fp::zero(), + Fp::from(1022), compress(index.clone()), - Base::zero(), + Fp::zero(), ] }, Operation { @@ -229,10 +229,10 @@ mod test { target: Target::Stack, is_write: false, values: [ - Base::zero(), - Base::from_u64(1022), + Fp::zero(), + Fp::from(1022), compress(index), - Base::zero(), + Fp::zero(), ] }, Operation { @@ -240,10 +240,10 @@ mod test { target: Target::Stack, is_write: false, values: [ - Base::zero(), - Base::from_u64(1023), + Fp::zero(), + Fp::from(1023), compress(value), - Base::zero(), + Fp::zero(), ] }, Operation { @@ -251,10 +251,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1023), + Fp::zero(), + Fp::from(1023), compress(result), - Base::zero(), + Fp::zero(), ] }, ], diff --git a/zkevm-circuits/src/evm_circuit/op_execution/comparator.rs b/zkevm-circuits/src/evm_circuit/op_execution/comparator.rs index 91fe413c17..423daaa47f 100644 --- a/zkevm-circuits/src/evm_circuit/op_execution/comparator.rs +++ b/zkevm-circuits/src/evm_circuit/op_execution/comparator.rs @@ -141,16 +141,16 @@ impl ComparatorSuccessCase { self.is_eq_op.assign( region, offset, - F::from_u64(step.opcode.as_u8() as u64), - F::from_u64(OpcodeId::EQ.as_u8() as u64), + F::from(step.opcode.as_u8() as u64), + F::from(OpcodeId::EQ.as_u8() as u64), )?; // swap when doing GT let swap = self.swap.assign( region, offset, - F::from_u64(step.opcode.as_u8() as u64), - F::from_u64(OpcodeId::GT.as_u8() as u64), + F::from(step.opcode.as_u8() as u64), + F::from(OpcodeId::GT.as_u8() as u64), )?; // Inputs and output @@ -197,21 +197,21 @@ mod test { }; use crate::{gadget::evm_word::encode, util::ToWord}; use bus_mapping::{evm::OpcodeId, operation::Target}; - use halo2::{arithmetic::FieldExt, dev::MockProver}; + use halo2::dev::MockProver; use num::BigUint; - use pasta_curves::pallas::Base; + use pairing::bn256::Fr as Fp; macro_rules! try_test_circuit { ($execution_step:expr, $operations:expr, $result:expr) => {{ let circuit = - TestCircuit::::new($execution_step, $operations, false); - let prover = MockProver::::run(11, &circuit, vec![]).unwrap(); + TestCircuit::::new($execution_step, $operations, false); + let prover = MockProver::::run(11, &circuit, vec![]).unwrap(); assert_eq!(prover.verify(), $result); }}; } - fn compress(value: BigUint) -> Base { - let r = Base::from_u64(1); + fn compress(value: BigUint) -> Fp { + let r = Fp::from(1); encode(value.to_word().to_vec().into_iter().rev(), r) } @@ -241,10 +241,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1023), + Fp::zero(), + Fp::from(1023), compress(b.clone()), - Base::zero(), + Fp::zero(), ] }, Operation { @@ -252,10 +252,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1022), + Fp::zero(), + Fp::from(1022), compress(a.clone()), - Base::zero(), + Fp::zero(), ] }, Operation { @@ -263,10 +263,10 @@ mod test { target: Target::Stack, is_write: false, values: [ - Base::zero(), - Base::from_u64(1022), + Fp::zero(), + Fp::from(1022), compress(a), - Base::zero(), + Fp::zero(), ] }, Operation { @@ -274,10 +274,10 @@ mod test { target: Target::Stack, is_write: false, values: [ - Base::zero(), - Base::from_u64(1023), + Fp::zero(), + Fp::from(1023), compress(b), - Base::zero(), + Fp::zero(), ] }, Operation { @@ -285,10 +285,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1023), + Fp::zero(), + Fp::from(1023), compress(result), - Base::zero(), + Fp::zero(), ] }, ], diff --git a/zkevm-circuits/src/evm_circuit/op_execution/dup.rs b/zkevm-circuits/src/evm_circuit/op_execution/dup.rs index 6ea4552952..fb27c6f24e 100644 --- a/zkevm-circuits/src/evm_circuit/op_execution/dup.rs +++ b/zkevm-circuits/src/evm_circuit/op_execution/dup.rs @@ -106,15 +106,15 @@ mod test { test::TestCircuit, Case, ExecutionStep, Operation, }; use bus_mapping::{evm::OpcodeId, operation::Target}; - use halo2::{arithmetic::FieldExt, dev::MockProver}; + use halo2::dev::MockProver; use num::BigUint; - use pasta_curves::pallas::Base; + use pairing::bn256::Fr as Fp; macro_rules! try_test_circuit { ($execution_steps:expr, $operations:expr, $result:expr) => {{ let circuit = - TestCircuit::::new($execution_steps, $operations, false); - let prover = MockProver::::run(11, &circuit, vec![]).unwrap(); + TestCircuit::::new($execution_steps, $operations, false); + let prover = MockProver::::run(11, &circuit, vec![]).unwrap(); assert_eq!(prover.verify(), $result); }}; } @@ -151,10 +151,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1023), - Base::from_u64(4 + 5 + 6), - Base::zero(), + Fp::zero(), + Fp::from(1023), + Fp::from(4 + 5 + 6), + Fp::zero(), ] }, Operation { @@ -162,10 +162,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1022), - Base::from_u64(3), - Base::zero(), + Fp::zero(), + Fp::from(1022), + Fp::from(3), + Fp::zero(), ] }, Operation { @@ -173,10 +173,10 @@ mod test { target: Target::Stack, is_write: false, values: [ - Base::zero(), - Base::from_u64(1023), - Base::from_u64(4 + 5 + 6), - Base::zero(), + Fp::zero(), + Fp::from(1023), + Fp::from(4 + 5 + 6), + Fp::zero(), ] }, Operation { @@ -184,10 +184,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1021), - Base::from_u64(4 + 5 + 6), - Base::zero(), + Fp::zero(), + Fp::from(1021), + Fp::from(4 + 5 + 6), + Fp::zero(), ] } ], @@ -227,10 +227,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1023), - Base::from_u64(4 + 5 + 6), - Base::zero(), + Fp::zero(), + Fp::from(1023), + Fp::from(4 + 5 + 6), + Fp::zero(), ] }, Operation { @@ -238,10 +238,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1022), - Base::from_u64(3), - Base::zero(), + Fp::zero(), + Fp::from(1022), + Fp::from(3), + Fp::zero(), ] }, Operation { @@ -249,10 +249,10 @@ mod test { target: Target::Stack, is_write: false, values: [ - Base::zero(), - Base::from_u64(1022), - Base::from_u64(3), - Base::zero(), + Fp::zero(), + Fp::from(1022), + Fp::from(3), + Fp::zero(), ] }, Operation { @@ -260,10 +260,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1021), - Base::from_u64(3), - Base::zero(), + Fp::zero(), + Fp::from(1021), + Fp::from(3), + Fp::zero(), ] } ], diff --git a/zkevm-circuits/src/evm_circuit/op_execution/jump.rs b/zkevm-circuits/src/evm_circuit/op_execution/jump.rs index 12aa0f00e5..9b1bf468a0 100644 --- a/zkevm-circuits/src/evm_circuit/op_execution/jump.rs +++ b/zkevm-circuits/src/evm_circuit/op_execution/jump.rs @@ -122,15 +122,15 @@ mod test { test::TestCircuit, Case, ExecutionStep, Operation, }; use bus_mapping::{evm::OpcodeId, operation::Target}; - use halo2::{arithmetic::FieldExt, dev::MockProver}; + use halo2::dev::MockProver; use num::BigUint; - use pasta_curves::pallas::Base; + use pairing::bn256::Fr as Fp; macro_rules! try_test_circuit { ($execution_steps:expr, $operations:expr, $result:expr) => {{ let circuit = - TestCircuit::::new($execution_steps, $operations, false); - let prover = MockProver::::run(11, &circuit, vec![]).unwrap(); + TestCircuit::::new($execution_steps, $operations, false); + let prover = MockProver::::run(11, &circuit, vec![]).unwrap(); assert_eq!(prover.verify(), $result); }}; } @@ -169,10 +169,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1023), - Base::from_u64(3u64), - Base::zero(), + Fp::zero(), + Fp::from(1023), + Fp::from(3u64), + Fp::zero(), ] }, Operation { @@ -180,10 +180,10 @@ mod test { target: Target::Stack, is_write: false, values: [ - Base::zero(), - Base::from_u64(1023), - Base::from_u64(3u64), - Base::zero(), + Fp::zero(), + Fp::from(1023), + Fp::from(3u64), + Fp::zero(), ] } ], diff --git a/zkevm-circuits/src/evm_circuit/op_execution/jumpdest.rs b/zkevm-circuits/src/evm_circuit/op_execution/jumpdest.rs index e5f4b3e12e..8e8d60b20a 100644 --- a/zkevm-circuits/src/evm_circuit/op_execution/jumpdest.rs +++ b/zkevm-circuits/src/evm_circuit/op_execution/jumpdest.rs @@ -83,15 +83,15 @@ mod test { test::TestCircuit, Case, ExecutionStep, Operation, }; use bus_mapping::{evm::OpcodeId, operation::Target}; - use halo2::{arithmetic::FieldExt, dev::MockProver}; + use halo2::dev::MockProver; use num::BigUint; - use pasta_curves::pallas::Base; + use pairing::bn256::Fr as Fp; macro_rules! try_test_circuit { ($execution_steps:expr, $operations:expr, $result:expr) => {{ let circuit = - TestCircuit::::new($execution_steps, $operations, false); - let prover = MockProver::::run(11, &circuit, vec![]).unwrap(); + TestCircuit::::new($execution_steps, $operations, false); + let prover = MockProver::::run(11, &circuit, vec![]).unwrap(); assert_eq!(prover.verify(), $result); }}; } @@ -120,10 +120,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1023), - Base::from_u64(2 + 3), - Base::zero(), + Fp::zero(), + Fp::from(1023), + Fp::from(2 + 3), + Fp::zero(), ] }], Ok(()) diff --git a/zkevm-circuits/src/evm_circuit/op_execution/jumpi.rs b/zkevm-circuits/src/evm_circuit/op_execution/jumpi.rs index 094aa39ff6..228ed212f4 100644 --- a/zkevm-circuits/src/evm_circuit/op_execution/jumpi.rs +++ b/zkevm-circuits/src/evm_circuit/op_execution/jumpi.rs @@ -161,15 +161,15 @@ mod test { test::TestCircuit, Case, ExecutionStep, Operation, }; use bus_mapping::{evm::OpcodeId, operation::Target}; - use halo2::{arithmetic::FieldExt, dev::MockProver}; + use halo2::dev::MockProver; use num::BigUint; - use pasta_curves::pallas::Base; + use pairing::bn256::Fr as Fp; macro_rules! try_test_circuit { ($execution_steps:expr, $operations:expr, $result:expr) => {{ let circuit = - TestCircuit::::new($execution_steps, $operations, false); - let prover = MockProver::::run(11, &circuit, vec![]).unwrap(); + TestCircuit::::new($execution_steps, $operations, false); + let prover = MockProver::::run(11, &circuit, vec![]).unwrap(); assert_eq!(prover.verify(), $result); }}; } @@ -217,10 +217,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1023), - Base::from_u64(1u64), - Base::zero(), + Fp::zero(), + Fp::from(1023), + Fp::from(1u64), + Fp::zero(), ] }, Operation { @@ -228,10 +228,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1022), - Base::from_u64(5u64), - Base::zero(), + Fp::zero(), + Fp::from(1022), + Fp::from(5u64), + Fp::zero(), ] }, Operation { @@ -239,10 +239,10 @@ mod test { target: Target::Stack, is_write: false, values: [ - Base::zero(), - Base::from_u64(1022), - Base::from_u64(5u64), - Base::zero(), + Fp::zero(), + Fp::from(1022), + Fp::from(5u64), + Fp::zero(), ] }, Operation { @@ -250,10 +250,10 @@ mod test { target: Target::Stack, is_write: false, values: [ - Base::zero(), - Base::from_u64(1023), - Base::from_u64(1u64), - Base::zero(), + Fp::zero(), + Fp::from(1023), + Fp::from(1u64), + Fp::zero(), ] } ], @@ -303,10 +303,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1023), - Base::from_u64(0), - Base::zero(), + Fp::zero(), + Fp::from(1023), + Fp::from(0), + Fp::zero(), ] }, Operation { @@ -314,10 +314,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1022), - Base::from_u64(5u64), - Base::zero(), + Fp::zero(), + Fp::from(1022), + Fp::from(5u64), + Fp::zero(), ] }, Operation { @@ -325,10 +325,10 @@ mod test { target: Target::Stack, is_write: false, values: [ - Base::zero(), - Base::from_u64(1022), - Base::from_u64(5u64), - Base::zero(), + Fp::zero(), + Fp::from(1022), + Fp::from(5u64), + Fp::zero(), ] }, Operation { @@ -336,10 +336,10 @@ mod test { target: Target::Stack, is_write: false, values: [ - Base::zero(), - Base::from_u64(1023), - Base::from_u64(0u64), - Base::zero(), + Fp::zero(), + Fp::from(1023), + Fp::from(0u64), + Fp::zero(), ] } ], diff --git a/zkevm-circuits/src/evm_circuit/op_execution/memory.rs b/zkevm-circuits/src/evm_circuit/op_execution/memory.rs index 303f1b08bf..497bd4ca5f 100644 --- a/zkevm-circuits/src/evm_circuit/op_execution/memory.rs +++ b/zkevm-circuits/src/evm_circuit/op_execution/memory.rs @@ -192,15 +192,15 @@ impl MemorySuccessCase { let is_mload = self.is_mload.assign( region, offset, - F::from_u64(step.opcode.as_u8() as u64), - F::from_u64(OpcodeId::MLOAD.as_u8() as u64), + F::from(step.opcode.as_u8() as u64), + F::from(OpcodeId::MLOAD.as_u8() as u64), )?; // Check if this is an MSTORE8 let is_mstore8 = self.is_mstore8.assign( region, offset, - F::from_u64(step.opcode.as_u8() as u64), - F::from_u64(OpcodeId::MSTORE8.as_u8() as u64), + F::from(step.opcode.as_u8() as u64), + F::from(OpcodeId::MSTORE8.as_u8() as u64), )?; // Memory expansion @@ -332,8 +332,8 @@ impl MemoryOutOfGasCase { let is_mstore8 = self.is_mstore8.assign( region, offset, - F::from_u64(step.opcode.as_u8() as u64), - F::from_u64(OpcodeId::MSTORE8.as_u8() as u64), + F::from(step.opcode.as_u8() as u64), + F::from(OpcodeId::MSTORE8.as_u8() as u64), )?; // Address in range check @@ -358,9 +358,7 @@ impl MemoryOutOfGasCase { self.insufficient_gas.assign( region, offset, - F::from_u64( - state.gas_counter + GAS.as_u64() + (memory_cost as u64), - ), + F::from(state.gas_counter + GAS.as_u64() + (memory_cost as u64)), F::from_bytes(&step.values[1].to_word()).unwrap(), )?; @@ -426,8 +424,8 @@ impl MemoryStackUnderflowCase { self.is_mload.assign( region, offset, - F::from_u64(step.opcode.as_u8() as u64), - F::from_u64(OpcodeId::MLOAD.as_u8() as u64), + F::from(step.opcode.as_u8() as u64), + F::from(OpcodeId::MLOAD.as_u8() as u64), )?; Ok(()) } @@ -436,19 +434,19 @@ impl MemoryStackUnderflowCase { #[cfg(test)] mod test { use super::super::super::{ - test::TestCircuit, Case, ExecutionStep, Operation, + test::TestCircuit, Case, ExecutionStep, FieldExt, Operation, }; use crate::{gadget::evm_word::encode, util::ToWord}; use bus_mapping::{evm::OpcodeId, operation::Target}; - use halo2::{arithmetic::FieldExt, dev::MockProver}; + use halo2::dev::MockProver; use num::BigUint; - use pasta_curves::pallas::Base; + use pairing::bn256::Fr as Fp; macro_rules! try_test_circuit { ($execution_steps:expr, $operations:expr, $result:expr) => {{ let circuit = - TestCircuit::::new($execution_steps, $operations, false); - let prover = MockProver::::run(11, &circuit, vec![]).unwrap(); + TestCircuit::::new($execution_steps, $operations, false); + let prover = MockProver::::run(11, &circuit, vec![]).unwrap(); assert_eq!(prover.verify(), $result); }}; } @@ -465,13 +463,13 @@ mod test { }}; } - fn compress(value: BigUint) -> Base { - let r = Base::from_u64(1); + fn compress(value: BigUint) -> Fp { + let r = Fp::from(1); encode(value.to_word().to_vec().into_iter().rev(), r) } fn mstore_ops( - operations: &mut Vec>, + operations: &mut Vec>, gc: &mut usize, stack_index: u64, address: BigUint, @@ -483,10 +481,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(stack_index), + Fp::zero(), + Fp::from(stack_index), compress(value.clone()), - Base::zero(), + Fp::zero(), ], }); operations.push(Operation { @@ -494,10 +492,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(stack_index - 1), + Fp::zero(), + Fp::from(stack_index - 1), compress(address.clone()), - Base::zero(), + Fp::zero(), ], }); operations.push(Operation { @@ -505,10 +503,10 @@ mod test { target: Target::Stack, is_write: false, values: [ - Base::zero(), - Base::from_u64(stack_index - 1), + Fp::zero(), + Fp::from(stack_index - 1), compress(address.clone()), - Base::zero(), + Fp::zero(), ], }); operations.push(Operation { @@ -516,10 +514,10 @@ mod test { target: Target::Stack, is_write: false, values: [ - Base::zero(), - Base::from_u64(stack_index), + Fp::zero(), + Fp::from(stack_index), compress(value.clone()), - Base::zero(), + Fp::zero(), ], }); for idx in 0..count { @@ -528,23 +526,23 @@ mod test { target: Target::Memory, is_write: true, values: [ - Base::zero(), - Base::from_bytes( + Fp::zero(), + Fp::from_bytes( &(address.clone() + BigUint::from(idx as u64)) .to_word(), ) .unwrap(), - Base::from_u64( + Fp::from( value.to_bytes_le()[count - 1 - idx as usize] as u64, ), - Base::zero(), + Fp::zero(), ], }); } } fn mload_ops( - operations: &mut Vec>, + operations: &mut Vec>, gc: &mut usize, stack_index: u64, address: BigUint, @@ -555,10 +553,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(stack_index), + Fp::zero(), + Fp::from(stack_index), compress(address.clone()), - Base::zero(), + Fp::zero(), ], }); operations.push(Operation { @@ -566,10 +564,10 @@ mod test { target: Target::Stack, is_write: false, values: [ - Base::zero(), - Base::from_u64(stack_index), + Fp::zero(), + Fp::from(stack_index), compress(address.clone()), - Base::zero(), + Fp::zero(), ], }); operations.push(Operation { @@ -577,10 +575,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(stack_index), + Fp::zero(), + Fp::from(stack_index), compress(value.clone()), - Base::zero(), + Fp::zero(), ], }); for idx in 0..32 { @@ -589,16 +587,14 @@ mod test { target: Target::Memory, is_write: false, values: [ - Base::zero(), - Base::from_bytes( + Fp::zero(), + Fp::from_bytes( &(address.clone() + BigUint::from(idx as u64)) .to_word(), ) .unwrap(), - Base::from_u64( - value.to_bytes_le()[31 - idx as usize] as u64, - ), - Base::zero(), + Fp::from(value.to_bytes_le()[31 - idx as usize] as u64), + Fp::zero(), ], }); } diff --git a/zkevm-circuits/src/evm_circuit/op_execution/pc.rs b/zkevm-circuits/src/evm_circuit/op_execution/pc.rs index e92cb03efd..011ef59413 100644 --- a/zkevm-circuits/src/evm_circuit/op_execution/pc.rs +++ b/zkevm-circuits/src/evm_circuit/op_execution/pc.rs @@ -106,15 +106,15 @@ mod test { test::TestCircuit, Case, ExecutionStep, Operation, }; use bus_mapping::{evm::OpcodeId, operation::Target}; - use halo2::{arithmetic::FieldExt, dev::MockProver}; + use halo2::dev::MockProver; use num::BigUint; - use pasta_curves::pallas::Base; + use pairing::bn256::Fr as Fp; macro_rules! try_test_circuit { ($execution_steps:expr, $operations:expr, $result:expr) => {{ let circuit = - TestCircuit::::new($execution_steps, $operations, false); - let prover = MockProver::::run(11, &circuit, vec![]).unwrap(); + TestCircuit::::new($execution_steps, $operations, false); + let prover = MockProver::::run(11, &circuit, vec![]).unwrap(); assert_eq!(prover.verify(), $result); }}; } @@ -144,10 +144,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1023), - Base::from_u64(1 + 2 + 3), - Base::zero(), + Fp::zero(), + Fp::from(1023), + Fp::from(1 + 2 + 3), + Fp::zero(), ] }, Operation { @@ -155,10 +155,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1022), - Base::from_u64(4), - Base::zero(), + Fp::zero(), + Fp::from(1022), + Fp::from(4), + Fp::zero(), ] }, ], diff --git a/zkevm-circuits/src/evm_circuit/op_execution/pop.rs b/zkevm-circuits/src/evm_circuit/op_execution/pop.rs index 8eec103e1c..0fd9f822b8 100644 --- a/zkevm-circuits/src/evm_circuit/op_execution/pop.rs +++ b/zkevm-circuits/src/evm_circuit/op_execution/pop.rs @@ -96,15 +96,15 @@ mod test { test::TestCircuit, Case, ExecutionStep, Operation, }; use bus_mapping::{evm::OpcodeId, operation::Target}; - use halo2::{arithmetic::FieldExt, dev::MockProver}; + use halo2::dev::MockProver; use num::BigUint; - use pasta_curves::pallas::Base; + use pairing::bn256::Fr as Fp; macro_rules! try_test_circuit { ($execution_steps:expr, $operations:expr, $result:expr) => {{ let circuit = - TestCircuit::::new($execution_steps, $operations, false); - let prover = MockProver::::run(11, &circuit, vec![]).unwrap(); + TestCircuit::::new($execution_steps, $operations, false); + let prover = MockProver::::run(11, &circuit, vec![]).unwrap(); assert_eq!(prover.verify(), $result); }}; } @@ -141,10 +141,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1023), - Base::from_u64(2 + 3), - Base::zero(), + Fp::zero(), + Fp::from(1023), + Fp::from(2 + 3), + Fp::zero(), ] }, Operation { @@ -152,10 +152,10 @@ mod test { target: Target::Stack, is_write: false, values: [ - Base::zero(), - Base::from_u64(1023), - Base::from_u64(2 + 3), - Base::zero(), + Fp::zero(), + Fp::from(1023), + Fp::from(2 + 3), + Fp::zero(), ] }, Operation { @@ -163,10 +163,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1023), - Base::from_u64(4 + 5 + 6), - Base::zero(), + Fp::zero(), + Fp::from(1023), + Fp::from(4 + 5 + 6), + Fp::zero(), ] } ], diff --git a/zkevm-circuits/src/evm_circuit/op_execution/push.rs b/zkevm-circuits/src/evm_circuit/op_execution/push.rs index ce01393ce5..7be34eaa3f 100644 --- a/zkevm-circuits/src/evm_circuit/op_execution/push.rs +++ b/zkevm-circuits/src/evm_circuit/op_execution/push.rs @@ -131,7 +131,7 @@ impl PushSuccessCase { .iter() .zip(step.values[1].to_word().iter()) .map(|(alloc, bit)| { - alloc.assign(region, offset, Some(F::from_u64(*bit as u64))) + alloc.assign(region, offset, Some(F::from(*bit as u64))) }) .collect::, _>>()?; @@ -150,15 +150,15 @@ mod test { test::TestCircuit, Case, ExecutionStep, Operation, }; use bus_mapping::{evm::OpcodeId, operation::Target}; - use halo2::{arithmetic::FieldExt, dev::MockProver}; + use halo2::dev::MockProver; use num::BigUint; - use pasta_curves::pallas::Base; + use pairing::bn256::Fr as Fp; macro_rules! try_test_circuit { ($execution_steps:expr, $operations:expr, $result:expr) => {{ let circuit = - TestCircuit::::new($execution_steps, $operations, false); - let prover = MockProver::::run(11, &circuit, vec![]).unwrap(); + TestCircuit::::new($execution_steps, $operations, false); + let prover = MockProver::::run(11, &circuit, vec![]).unwrap(); assert_eq!(prover.verify(), $result); }}; } @@ -182,10 +182,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1023), - Base::from_u64(2 + 3), - Base::zero(), + Fp::zero(), + Fp::from(1023), + Fp::from(2 + 3), + Fp::zero(), ] }], Ok(()) diff --git a/zkevm-circuits/src/evm_circuit/op_execution/signextend.rs b/zkevm-circuits/src/evm_circuit/op_execution/signextend.rs index 8632a4ff21..303088698a 100644 --- a/zkevm-circuits/src/evm_circuit/op_execution/signextend.rs +++ b/zkevm-circuits/src/evm_circuit/op_execution/signextend.rs @@ -203,8 +203,8 @@ impl SignextendSuccessCase { self.is_byte_selected[i].assign( region, offset, - F::from_u64(step.values[0].to_word()[0] as u64), - F::from_u64(i as u64), + F::from(step.values[0].to_word()[0] as u64), + F::from(i as u64), )?, msb_sum_zero, ]); @@ -222,7 +222,7 @@ impl SignextendSuccessCase { sign = (step.values[1].to_word()[index] >> 7) as u64; } self.sign_byte - .assign(region, offset, Some(F::from_u64(sign * 0xFF))) + .assign(region, offset, Some(F::from(sign * 0xFF))) .unwrap(); // State transitions @@ -238,24 +238,24 @@ mod test { test::TestCircuit, Case, ExecutionStep, Operation, }; use bus_mapping::{evm::OpcodeId, operation::Target}; - use halo2::{arithmetic::FieldExt, dev::MockProver}; + use halo2::dev::MockProver; use num::BigUint; - use pasta_curves::pallas::Base; + use pairing::bn256::Fr as Fp; macro_rules! try_test_circuit { ($execution_steps:expr, $operations:expr, $result:expr) => {{ let circuit = - TestCircuit::::new($execution_steps, $operations, false); - let prover = MockProver::::run(11, &circuit, vec![]).unwrap(); + TestCircuit::::new($execution_steps, $operations, false); + let prover = MockProver::::run(11, &circuit, vec![]).unwrap(); assert_eq!(prover.verify(), $result); }}; } - fn compress(value: BigUint) -> Base { + fn compress(value: BigUint) -> Fp { value .to_bytes_le() .iter() - .fold(Base::zero(), |acc, val| acc + Base::from_u64(*val as u64)) + .fold(Fp::zero(), |acc, val| acc + Fp::from(*val as u64)) } fn check_signextend_gadget( @@ -288,10 +288,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1023), + Fp::zero(), + Fp::from(1023), compress(value.clone()), - Base::zero(), + Fp::zero(), ] }, Operation { @@ -299,10 +299,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1022), + Fp::zero(), + Fp::from(1022), compress(index.clone()), - Base::zero(), + Fp::zero(), ] }, Operation { @@ -310,10 +310,10 @@ mod test { target: Target::Stack, is_write: false, values: [ - Base::zero(), - Base::from_u64(1022), + Fp::zero(), + Fp::from(1022), compress(index), - Base::zero(), + Fp::zero(), ] }, Operation { @@ -321,10 +321,10 @@ mod test { target: Target::Stack, is_write: false, values: [ - Base::zero(), - Base::from_u64(1023), + Fp::zero(), + Fp::from(1023), compress(value), - Base::zero(), + Fp::zero(), ] }, Operation { @@ -332,10 +332,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1023), + Fp::zero(), + Fp::from(1023), compress(result), - Base::zero(), + Fp::zero(), ] }, ], diff --git a/zkevm-circuits/src/evm_circuit/op_execution/swap.rs b/zkevm-circuits/src/evm_circuit/op_execution/swap.rs index a928711c97..5546d961b3 100644 --- a/zkevm-circuits/src/evm_circuit/op_execution/swap.rs +++ b/zkevm-circuits/src/evm_circuit/op_execution/swap.rs @@ -116,15 +116,15 @@ mod test { test::TestCircuit, Case, ExecutionStep, Operation, }; use bus_mapping::{evm::OpcodeId, operation::Target}; - use halo2::{arithmetic::FieldExt, dev::MockProver}; + use halo2::dev::MockProver; use num::BigUint; - use pasta_curves::pallas::Base; + use pairing::bn256::Fr as Fp; macro_rules! try_test_circuit { ($execution_steps:expr, $operations:expr, $result:expr) => {{ let circuit = - TestCircuit::::new($execution_steps, $operations, false); - let prover = MockProver::::run(11, &circuit, vec![]).unwrap(); + TestCircuit::::new($execution_steps, $operations, false); + let prover = MockProver::::run(11, &circuit, vec![]).unwrap(); assert_eq!(prover.verify(), $result); }}; } @@ -173,10 +173,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1023), - Base::from_u64(1 + 2 + 3), - Base::zero(), + Fp::zero(), + Fp::from(1023), + Fp::from(1 + 2 + 3), + Fp::zero(), ], }, Operation { @@ -184,10 +184,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1022), - Base::from_u64(4 + 5), - Base::zero(), + Fp::zero(), + Fp::from(1022), + Fp::from(4 + 5), + Fp::zero(), ] }, Operation { @@ -195,10 +195,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1021), - Base::from_u64(6), - Base::zero(), + Fp::zero(), + Fp::from(1021), + Fp::from(6), + Fp::zero(), ] }, // swap1 1021 <=> 1023 @@ -207,10 +207,10 @@ mod test { target: Target::Stack, is_write: false, values: [ - Base::zero(), - Base::from_u64(1023), - Base::from_u64(1 + 2 + 3), - Base::zero(), + Fp::zero(), + Fp::from(1023), + Fp::from(1 + 2 + 3), + Fp::zero(), ] }, Operation { @@ -218,10 +218,10 @@ mod test { target: Target::Stack, is_write: false, values: [ - Base::zero(), - Base::from_u64(1021), - Base::from_u64(6), - Base::zero(), + Fp::zero(), + Fp::from(1021), + Fp::from(6), + Fp::zero(), ] }, Operation { @@ -229,10 +229,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1023), - Base::from_u64(6), - Base::zero(), + Fp::zero(), + Fp::from(1023), + Fp::from(6), + Fp::zero(), ] }, Operation { @@ -240,10 +240,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1021), - Base::from_u64(1 + 2 + 3), - Base::zero(), + Fp::zero(), + Fp::from(1021), + Fp::from(1 + 2 + 3), + Fp::zero(), ] } ], @@ -287,10 +287,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1023), - Base::from_u64(1 + 2 + 3), - Base::zero(), + Fp::zero(), + Fp::from(1023), + Fp::from(1 + 2 + 3), + Fp::zero(), ], }, Operation { @@ -298,10 +298,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1022), - Base::from_u64(4 + 5), - Base::zero(), + Fp::zero(), + Fp::from(1022), + Fp::from(4 + 5), + Fp::zero(), ] }, // swap1 1023 <=> 1022 @@ -310,10 +310,10 @@ mod test { target: Target::Stack, is_write: false, values: [ - Base::zero(), - Base::from_u64(1023), - Base::from_u64(1 + 2 + 3), - Base::zero(), + Fp::zero(), + Fp::from(1023), + Fp::from(1 + 2 + 3), + Fp::zero(), ] }, Operation { @@ -321,10 +321,10 @@ mod test { target: Target::Stack, is_write: false, values: [ - Base::zero(), - Base::from_u64(1022), - Base::from_u64(4 + 5), - Base::zero(), + Fp::zero(), + Fp::from(1022), + Fp::from(4 + 5), + Fp::zero(), ] }, Operation { @@ -332,10 +332,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1023), - Base::from_u64(4 + 5), - Base::zero(), + Fp::zero(), + Fp::from(1023), + Fp::from(4 + 5), + Fp::zero(), ] }, Operation { @@ -343,10 +343,10 @@ mod test { target: Target::Stack, is_write: true, values: [ - Base::zero(), - Base::from_u64(1022), - Base::from_u64(1 + 2 + 3), - Base::zero(), + Fp::zero(), + Fp::from(1022), + Fp::from(1 + 2 + 3), + Fp::zero(), ] } ], diff --git a/zkevm-circuits/src/evm_circuit/op_execution/utils.rs b/zkevm-circuits/src/evm_circuit/op_execution/utils.rs index b8ed6a557b..e7feee9daa 100644 --- a/zkevm-circuits/src/evm_circuit/op_execution/utils.rs +++ b/zkevm-circuits/src/evm_circuit/op_execution/utils.rs @@ -155,7 +155,7 @@ pub(crate) mod sum { pub(crate) fn value(values: &[u8]) -> F { values .iter() - .fold(F::zero(), |acc, value| acc + F::from_u64(*value as u64)) + .fold(F::zero(), |acc, value| acc + F::from(*value as u64)) } } @@ -227,7 +227,7 @@ pub(crate) mod from_bytes { let mut multiplier = F::one(); for byte in bytes.iter() { value = value + byte.expr() * multiplier; - multiplier *= F::from_u64(256); + multiplier *= F::from(256); } value } @@ -237,8 +237,8 @@ pub(crate) mod from_bytes { let mut value = F::zero(); let mut multiplier = F::one(); for byte in bytes.iter() { - value += F::from_u64(*byte as u64) * multiplier; - multiplier *= F::from_u64(256); + value += F::from(*byte as u64) * multiplier; + multiplier *= F::from(256); } value } @@ -246,7 +246,7 @@ pub(crate) mod from_bytes { /// Returns 2**num_bits pub(crate) fn get_range(num_bits: usize) -> F { - F::from_u64(2).pow(&[num_bits as u64, 0, 0, 0]) + F::from(2).pow(&[num_bits as u64, 0, 0, 0]) } pub(crate) fn require_opcode_in_set( diff --git a/zkevm-circuits/src/evm_circuit/op_execution/utils/common_cases.rs b/zkevm-circuits/src/evm_circuit/op_execution/utils/common_cases.rs index a23f50157b..3614c7033c 100644 --- a/zkevm-circuits/src/evm_circuit/op_execution/utils/common_cases.rs +++ b/zkevm-circuits/src/evm_circuit/op_execution/utils/common_cases.rs @@ -65,7 +65,7 @@ impl OutOfGasCase { self.gas_available.assign( region, offset, - Some(F::from_u64(state.gas_counter as u64)), + Some(F::from(state.gas_counter as u64)), )?; Ok(()) } diff --git a/zkevm-circuits/src/evm_circuit/op_execution/utils/math_gadgets.rs b/zkevm-circuits/src/evm_circuit/op_execution/utils/math_gadgets.rs index de0f4433a4..984f9e20e4 100644 --- a/zkevm-circuits/src/evm_circuit/op_execution/utils/math_gadgets.rs +++ b/zkevm-circuits/src/evm_circuit/op_execution/utils/math_gadgets.rs @@ -50,7 +50,11 @@ impl IsZeroGadget { ) -> Result { let inverse = value.invert().unwrap_or(F::zero()); self.inverse.assign(region, offset, Some(inverse))?; - Ok(if value.is_zero() { F::one() } else { F::zero() }) + Ok(if value.is_zero().into() { + F::one() + } else { + F::zero() + }) } } @@ -132,7 +136,7 @@ impl RangeCheckGadget { ) -> Result<(), Error> { let bytes = value.to_bytes(); for (idx, part) in self.parts.iter().enumerate() { - part.assign(region, offset, Some(F::from_u64(bytes[idx] as u64)))?; + part.assign(region, offset, Some(F::from(bytes[idx] as u64)))?; } Ok(()) } @@ -200,18 +204,14 @@ impl LtGadget { self.lt.assign( region, offset, - Some(F::from_u64(if lt { 1 } else { 0 })), + Some(F::from(if lt { 1 } else { 0 })), )?; // Set the bytes of diff let diff = (lhs - rhs) + (if lt { self.range } else { F::zero() }); let diff_bytes = diff.to_bytes(); for (idx, diff) in self.diff.iter().enumerate() { - diff.assign( - region, - offset, - Some(F::from_u64(diff_bytes[idx] as u64)), - )?; + diff.assign(region, offset, Some(F::from(diff_bytes[idx] as u64)))?; } Ok((if lt { F::one() } else { F::zero() }, diff_bytes.to_vec())) diff --git a/zkevm-circuits/src/evm_circuit/op_execution/utils/memory_gadgets.rs b/zkevm-circuits/src/evm_circuit/op_execution/utils/memory_gadgets.rs index 1823a1d71e..c49804d2a3 100644 --- a/zkevm-circuits/src/evm_circuit/op_execution/utils/memory_gadgets.rs +++ b/zkevm-circuits/src/evm_circuit/op_execution/utils/memory_gadgets.rs @@ -206,8 +206,8 @@ impl .assign( region, offset, - F::from_u64(address_memory_size), - F::from_u64(curr_memory_size), + F::from(address_memory_size), + F::from(curr_memory_size), )? .get_lower_128() as MemorySize; diff --git a/zkevm-circuits/src/gadget.rs b/zkevm-circuits/src/gadget.rs index 9b17db6164..72c493c92b 100644 --- a/zkevm-circuits/src/gadget.rs +++ b/zkevm-circuits/src/gadget.rs @@ -1,6 +1,6 @@ //! Reusable gadgets for the zk_evm circuits. use halo2::circuit::Cell; -use pasta_curves::arithmetic::FieldExt; +use pairing::arithmetic::FieldExt; /// An assigned cell in the circuit. #[derive(Clone, Debug)] diff --git a/zkevm-circuits/src/gadget/evm_word.rs b/zkevm-circuits/src/gadget/evm_word.rs index 8442e6146d..234d0a4458 100644 --- a/zkevm-circuits/src/gadget/evm_word.rs +++ b/zkevm-circuits/src/gadget/evm_word.rs @@ -15,7 +15,7 @@ use halo2::{ }, poly::Rotation, }; -use pasta_curves::arithmetic::FieldExt; +use pairing::arithmetic::FieldExt; use sha3::{Digest, Keccak256}; use std::convert::TryInto; @@ -29,14 +29,15 @@ pub(crate) fn r() -> F { for byte in 0..=u8::MAX { hasher.process(&[byte]); } - let r: [u8; 32] = hasher.fixed_result().as_slice().try_into().unwrap(); - F::from_bytes(&r).unwrap() + let mut r = [0; 64]; + r[..32].copy_from_slice(hasher.fixed_result().as_slice()); + F::from_bytes_wide(&r) } // Returns encoding of big-endian representation of a 256-bit word. pub(crate) fn encode(vals: impl Iterator, r: F) -> F { vals.fold(F::zero(), |acc, val| { - let byte = F::from_u64(val as u64); + let byte = F::from(val as u64); acc * r + byte }) } @@ -87,9 +88,9 @@ impl WordConfig { // range-constrain it to 8 bits. // // TODO: Understand why the `for` loop cannot be moved into - // the meta.lookup() call. + // the meta.lookup_any() call. for byte in bytes.iter().rev() { - meta.lookup(|meta| { + meta.lookup_any(|meta| { let q_encode = meta.query_selector(q_encode); let r = Expression::Constant(r); let byte = meta.query_advice(*byte, Rotation::cur()); @@ -125,7 +126,7 @@ impl WordConfig { || format!("load {}", byte), self.byte_lookup, byte.into(), - || Ok(F::from_u64(byte as u64)), + || Ok(F::from(byte as u64)), )?; } @@ -149,12 +150,12 @@ impl WordConfig { // TODO: We will likely enable this selector outside of the helper. self.q_encode.enable(region, offset)?; - let byte_field_elem = byte.map(|byte| F::from_u64(byte as u64)); + let byte_field_elem = byte.map(|byte| F::from(byte as u64)); let cell = region.assign_advice( || format!("assign byte {}", idx), *column, offset, - || byte_field_elem.ok_or(Error::SynthesisError), + || byte_field_elem.ok_or(Error::Synthesis), )?; bytes.push(Variable::new(cell, byte_field_elem, *byte)); @@ -168,11 +169,15 @@ impl WordConfig { mod tests { use super::*; use halo2::{ + arithmetic::Field, + arithmetic::FieldExt, circuit::SimpleFloorPlanner, dev::{MockProver, VerifyFailure}, plonk::{Circuit, Instance}, }; - use pasta_curves::pallas; + use pairing::bn256::Fr as Fp; + use rand::SeedableRng; + use rand_xorshift::XorShiftRng; use std::marker::PhantomData; #[test] @@ -197,7 +202,7 @@ mod tests { fn configure(meta: &mut ConstraintSystem) -> Self::Config { let r = r(); - let q_encode = meta.selector(); + let q_encode = meta.complex_selector(); let bytes: [Column; 32] = (0..32) .map(|_| meta.advice_column()) @@ -218,7 +223,7 @@ mod tests { // Make sure each encoded word has been committed to in the // public inputs. - meta.lookup(|meta| { + meta.lookup_any(|meta| { let q_encode = meta.query_selector(q_encode); let pub_inputs = meta.query_instance(pub_inputs, Rotation::cur()); @@ -251,8 +256,12 @@ mod tests { } { - let word = pallas::Base::rand(); - let circuit = MyCircuit:: { + let rng = XorShiftRng::from_seed([ + 0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, + 0x37, 0x32, 0x54, 0x06, 0xbc, 0xe5, + ]); + let word = Fp::random(rng); + let circuit = MyCircuit:: { word: word .to_bytes() .iter() @@ -265,8 +274,7 @@ mod tests { // Test without public inputs let prover = - MockProver::::run(9, &circuit, vec![vec![]]) - .unwrap(); + MockProver::::run(9, &circuit, vec![vec![]]).unwrap(); assert_eq!( prover.verify(), Err(vec![VerifyFailure::Lookup { @@ -276,14 +284,11 @@ mod tests { ); // Calculate word commitment and use it as public input. - let encoded: pallas::Base = + let encoded: Fp = encode(word.to_bytes().iter().rev().cloned(), r()); - let prover = MockProver::::run( - 9, - &circuit, - vec![vec![encoded]], - ) - .unwrap(); + let prover = + MockProver::::run(9, &circuit, vec![vec![encoded]]) + .unwrap(); assert_eq!(prover.verify(), Ok(())) } } diff --git a/zkevm-circuits/src/gadget/is_zero.rs b/zkevm-circuits/src/gadget/is_zero.rs index 2abe1560eb..4152d39301 100644 --- a/zkevm-circuits/src/gadget/is_zero.rs +++ b/zkevm-circuits/src/gadget/is_zero.rs @@ -5,7 +5,7 @@ use halo2::{ }, poly::Rotation, }; -use pasta_curves::arithmetic::FieldExt; +use pairing::arithmetic::FieldExt; use std::array; pub(crate) trait IsZeroInstruction { @@ -99,7 +99,7 @@ impl IsZeroInstruction for IsZeroChip { || "witness inverse of value", config.value_inv, offset, - || value_invert.ok_or(Error::SynthesisError), + || value_invert.ok_or(Error::Synthesis), )?; Ok(()) @@ -125,11 +125,11 @@ mod test { use halo2::{ arithmetic::FieldExt, circuit::{Layouter, SimpleFloorPlanner}, - dev::{MockProver, VerifyFailure::ConstraintNotSatisfied}, + dev::MockProver, plonk::{Advice, Circuit, Column, ConstraintSystem, Error, Selector}, poly::Rotation, }; - use pasta_curves::pallas::Base; + use pairing::bn256::Fr as Fp; use std::marker::PhantomData; macro_rules! try_test_circuit { @@ -139,23 +139,31 @@ mod test { // TODO: remove zk blinding factors in halo2 to restore the // correct k (without the extra + 2). let k = usize::BITS - $values.len().leading_zeros() + 2; - let circuit = TestCircuit:: { + let circuit = TestCircuit:: { values: Some($values), checks: Some($checks), _marker: PhantomData, }; - let prover = MockProver::::run(k, &circuit, vec![]).unwrap(); + let prover = MockProver::::run(k, &circuit, vec![]).unwrap(); assert_eq!(prover.verify(), $result); }}; } - macro_rules! error_constraint_at_row { - ($row:expr) => { - ConstraintNotSatisfied { - constraint: ((1, "check is_zero").into(), 0, "").into(), - row: $row, - } - }; + macro_rules! try_test_circuit_error { + ($values:expr, $checks:expr) => {{ + // let k = usize::BITS - $values.len().leading_zeros(); + + // TODO: remove zk blinding factors in halo2 to restore the + // correct k (without the extra + 2). + let k = usize::BITS - $values.len().leading_zeros() + 2; + let circuit = TestCircuit:: { + values: Some($values), + checks: Some($checks), + _marker: PhantomData, + }; + let prover = MockProver::::run(k, &circuit, vec![]).unwrap(); + assert!(prover.verify().is_err()); + }}; } #[test] @@ -185,7 +193,7 @@ mod test { } fn configure(meta: &mut ConstraintSystem) -> Self::Config { - let q_enable = meta.selector(); + let q_enable = meta.complex_selector(); let value = meta.advice_column(); let value_diff_inv = meta.advice_column(); let check = meta.advice_column(); @@ -238,11 +246,10 @@ mod test { .values .as_ref() .map(|values| { - values.iter().map(|value| F::from_u64(*value)).collect() + values.iter().map(|value| F::from(*value)).collect() }) - .ok_or(Error::SynthesisError)?; - let checks = - self.checks.as_ref().ok_or(Error::SynthesisError)?; + .ok_or(Error::Synthesis)?; + let checks = self.checks.as_ref().ok_or(Error::Synthesis)?; let (first_value, values) = values.split_at(1); let first_value = first_value[0]; @@ -264,7 +271,7 @@ mod test { || "check", config.check, idx + 1, - || Ok(F::from_u64(*check as u64)), + || Ok(F::from(*check as u64)), )?; region.assign_advice( || "value", @@ -301,25 +308,13 @@ mod test { Ok(()) ); // error - try_test_circuit!( + try_test_circuit_error!( vec![1, 2, 3, 4, 5], - vec![true, true, true, true], - Err(vec![ - error_constraint_at_row!(1), - error_constraint_at_row!(2), - error_constraint_at_row!(3), - error_constraint_at_row!(4) - ]) + vec![true, true, true, true] ); - try_test_circuit!( + try_test_circuit_error!( vec![1, 2, 2, 3, 3], - vec![true, false, true, false], - Err(vec![ - error_constraint_at_row!(1), - error_constraint_at_row!(2), - error_constraint_at_row!(3), - error_constraint_at_row!(4) - ]) + vec![true, false, true, false] ); } @@ -351,7 +346,7 @@ mod test { } fn configure(meta: &mut ConstraintSystem) -> Self::Config { - let q_enable = meta.selector(); + let q_enable = meta.complex_selector(); let (value_a, value_b) = (meta.advice_column(), meta.advice_column()); let value_diff_inv = meta.advice_column(); @@ -409,13 +404,12 @@ mod test { values .iter() .map(|(value_a, value_b)| { - (F::from_u64(*value_a), F::from_u64(*value_b)) + (F::from(*value_a), F::from(*value_b)) }) .collect() }) - .ok_or(Error::SynthesisError)?; - let checks = - self.checks.as_ref().ok_or(Error::SynthesisError)?; + .ok_or(Error::Synthesis)?; + let checks = self.checks.as_ref().ok_or(Error::Synthesis)?; layouter.assign_region( || "witness", @@ -427,7 +421,7 @@ mod test { || "check", config.check, idx + 1, - || Ok(F::from_u64(*check as u64)), + || Ok(F::from(*check as u64)), )?; region.assign_advice( || "value_a", @@ -468,23 +462,13 @@ mod test { Ok(()) ); // error - try_test_circuit!( + try_test_circuit_error!( vec![(1, 2), (3, 4), (5, 6)], - vec![true, true, true], - Err(vec![ - error_constraint_at_row!(1), - error_constraint_at_row!(2), - error_constraint_at_row!(3), - ]) + vec![true, true, true] ); - try_test_circuit!( + try_test_circuit_error!( vec![(1, 1), (3, 4), (6, 6)], - vec![false, true, false], - Err(vec![ - error_constraint_at_row!(1), - error_constraint_at_row!(2), - error_constraint_at_row!(3), - ]) + vec![false, true, false] ); } } diff --git a/zkevm-circuits/src/gadget/monotone.rs b/zkevm-circuits/src/gadget/monotone.rs index ba297d4458..7fbdd7633c 100644 --- a/zkevm-circuits/src/gadget/monotone.rs +++ b/zkevm-circuits/src/gadget/monotone.rs @@ -6,7 +6,7 @@ use halo2::{ }, poly::Rotation, }; -use pasta_curves::arithmetic::FieldExt; +use pairing::arithmetic::FieldExt; use std::{marker::PhantomData, u64}; #[derive(Clone, Debug)] @@ -41,7 +41,7 @@ impl let config = MonotoneConfig { range_table, value }; - meta.lookup(|meta| { + meta.lookup_any(|meta| { let q_enable = q_enable(meta); let range_table = meta.query_fixed(config.range_table, Rotation::cur()); @@ -57,7 +57,7 @@ impl // If strict monotone, we subtract diff by one // to make sure zero lookup fail - let min_diff = Expression::Constant(F::from_u64(STRICT as u64)); + let min_diff = Expression::Constant(F::from(STRICT as u64)); vec![(q_enable * (value_diff - min_diff), range_table)] }); @@ -76,7 +76,7 @@ impl || "range_table_value", self.config.range_table, idx, - || Ok(F::from_u64(idx as u64)), + || Ok(F::from(idx as u64)), )?; } @@ -120,7 +120,7 @@ mod test { }, plonk::{Advice, Circuit, Column, ConstraintSystem, Error, Selector}, }; - use pasta_curves::pallas::Base; + use pairing::bn256::Fr as Fp; use std::marker::PhantomData; #[derive(Clone, Debug)] @@ -156,7 +156,7 @@ mod test { } fn configure(meta: &mut ConstraintSystem) -> Self::Config { - let q_enable = meta.selector(); + let q_enable = meta.complex_selector(); let value = meta.advice_column(); let mono_incr = MonotoneChip::::configure( @@ -188,9 +188,9 @@ mod test { .values .as_ref() .map(|values| { - values.iter().map(|value| F::from_u64(*value)).collect() + values.iter().map(|value| F::from(*value)).collect() }) - .ok_or(Error::SynthesisError)?; + .ok_or(Error::Synthesis)?; layouter.assign_region( || "witness", @@ -219,11 +219,11 @@ mod test { values: Vec, result: Result<(), Vec>, ) { - let circuit = TestCircuit:: { + let circuit = TestCircuit:: { values: Some(values), _marker: PhantomData, }; - let prover = MockProver::::run( + let prover = MockProver::::run( usize::BITS - ($range as usize).leading_zeros(), &circuit, vec![], diff --git a/zkevm-circuits/src/state_circuit/state.rs b/zkevm-circuits/src/state_circuit/state.rs index e0f5c7f5bc..59967f96d9 100644 --- a/zkevm-circuits/src/state_circuit/state.rs +++ b/zkevm-circuits/src/state_circuit/state.rs @@ -13,7 +13,7 @@ use halo2::{ }, poly::Rotation, }; -use pasta_curves::arithmetic::FieldExt; +use pairing::arithmetic::FieldExt; /* Example state table: @@ -149,9 +149,9 @@ impl< let memory_value_table = meta.fixed_column(); let one = Expression::Constant(F::one()); - let two = Expression::Constant(F::from_u64(2)); - let three = Expression::Constant(F::from_u64(3)); - let four = Expression::Constant(F::from_u64(4)); + let two = Expression::Constant(F::from(2)); + let three = Expression::Constant(F::from(3)); + let four = Expression::Constant(F::from(4)); let q_memory_first = |meta: &mut VirtualCells| { // For first memory row it holds q_target_cur = 1 and q_target_next @@ -174,7 +174,7 @@ impl< let e = q_memory_first(meta); // q_memory_first is 12 when q_target_cur is 1 and q_target_next is // 2, we use 1/12 to normalize the value - let inv = F::from_u64(12_u64).invert().unwrap(); + let inv = F::from(12_u64).invert().unwrap(); let i = Expression::Constant(inv); e * i @@ -193,7 +193,7 @@ impl< let e = q_memory_not_first(meta); // q_memory_not_first is 4 when target is 2, we use 1/4 to normalize // the value - let inv = F::from_u64(4_u64).invert().unwrap(); + let inv = F::from(4_u64).invert().unwrap(); let i = Expression::Constant(inv); e * i @@ -214,7 +214,7 @@ impl< let q_stack_first_norm = |meta: &mut VirtualCells| { let e = q_stack_first(meta); // q_stack_first is 12, we use 1/12 to normalize the value - let inv = F::from_u64(12_u64).invert().unwrap(); + let inv = F::from(12_u64).invert().unwrap(); let i = Expression::Constant(inv); e * i @@ -233,7 +233,7 @@ impl< let e = q_stack_not_first(meta); // q_stack_not_first is 6 when target is 3, we use 1/6 to normalize // the value - let inv = F::from_u64(6_u64).invert().unwrap(); + let inv = F::from(6_u64).invert().unwrap(); let i = Expression::Constant(inv); e * i @@ -251,7 +251,7 @@ impl< let e = q_storage_not_first(meta); // q_storage_not_first is 24 when target is 4, we use 1/24 to // normalize the value - let inv = F::from_u64(24_u64).invert().unwrap(); + let inv = F::from(24_u64).invert().unwrap(); let i = Expression::Constant(inv); e * i @@ -397,7 +397,7 @@ impl< // global_counter monotonicity is checked for memory and stack when // address_cur == address_prev. (Recall that operations are // ordered first by address, and then by global_counter.) - meta.lookup(|meta| { + meta.lookup_any(|meta| { let global_counter_table = meta.query_fixed(global_counter_table, Rotation::cur()); let global_counter_prev = @@ -419,7 +419,7 @@ impl< }); // Memory address is in the allowed range. - meta.lookup(|meta| { + meta.lookup_any(|meta| { let q_memory = q_memory_first_norm(meta) + q_memory_not_first_norm(meta); let address_cur = meta.query_advice(address, Rotation::cur()); @@ -430,7 +430,7 @@ impl< }); // Stack address is in the allowed range. - meta.lookup(|meta| { + meta.lookup_any(|meta| { let q_stack = q_stack_first_norm(meta) + q_stack_not_first_norm(meta); let address_cur = meta.query_advice(address, Rotation::cur()); @@ -441,7 +441,7 @@ impl< }); // global_counter is in the allowed range: - meta.lookup(|meta| { + meta.lookup_any(|meta| { let global_counter = meta.query_advice(global_counter, Rotation::cur()); let global_counter_table = @@ -453,7 +453,7 @@ impl< // Memory value (for non-first rows) is in the allowed range. // Memory first row value doesn't need to be checked - it is checked // above where memory init row value has to be 0. - meta.lookup(|meta| { + meta.lookup_any(|meta| { let q_memory_not_first = q_memory_not_first_norm(meta); let value = meta.query_advice(value, Rotation::cur()); let memory_value_table = @@ -566,7 +566,7 @@ impl< // == address_prev and storage_key_cur = storage_key_prev. // (Recall that storage operations are ordered first by account address, // then by storage_key, and finally by global_counter.) - meta.lookup(|meta| { + meta.lookup_any(|meta| { let global_counter_table = meta.query_fixed(global_counter_table, Rotation::cur()); let global_counter_prev = @@ -625,7 +625,7 @@ impl< || "global counter table", self.global_counter_table, idx, - || Ok(F::from_u64(idx as u64)), + || Ok(F::from(idx as u64)), )?; } Ok(()) @@ -642,7 +642,7 @@ impl< || "memory value table", self.memory_value_table, idx, - || Ok(F::from_u64(idx as u64)), + || Ok(F::from(idx as u64)), )?; } Ok(()) @@ -659,7 +659,7 @@ impl< || "address table with zero", self.memory_address_table_zero, idx, - || Ok(F::from_u64(idx as u64)), + || Ok(F::from(idx as u64)), )?; } Ok(()) @@ -675,7 +675,7 @@ impl< || "stack address table with zero", self.stack_address_table_zero, idx, - || Ok(F::from_u64(idx as u64)), + || Ok(F::from(idx as u64)), )?; } Ok(()) @@ -769,7 +769,7 @@ impl< let mut offset = MEMORY_ROWS_MAX; for (index, oper) in ops.iter().enumerate() { let op = oper.op(); - let address = F::from_u64(usize::from(*op.address()) as u64); + let address = F::from(usize::from(*op.address()) as u64); let gc = usize::from(oper.gc()); let val = op.value().to_scalar().unwrap(); @@ -900,7 +900,7 @@ impl< || "target", self.q_target, i, - || Ok(F::from_u64(target as u64)), + || Ok(F::from(target as u64)), )?; } region.assign_advice( @@ -1013,7 +1013,7 @@ impl< || "target", self.q_target, offset, - || Ok(F::from_u64(target as u64)), + || Ok(F::from(target as u64)), )?; Ok(()) @@ -1047,7 +1047,7 @@ impl< }; let global_counter = { - let field_elem = F::from_u64(global_counter as u64); + let field_elem = F::from(global_counter as u64); let cell = region.assign_advice( || "global counter", @@ -1109,7 +1109,7 @@ impl< }; let flag = { - let field_elem = F::from_u64(flag as u64); + let field_elem = F::from(flag as u64); let cell = region.assign_advice( || "flag", self.flag, @@ -1126,12 +1126,12 @@ impl< let target = { let value = Some(target); - let field_elem = Some(F::from_u64(target as u64)); + let field_elem = Some(F::from(target as u64)); let cell = region.assign_fixed( || "target", self.q_target, offset, - || Ok(F::from_u64(target as u64)), + || Ok(F::from(target as u64)), )?; Variable:: { cell, @@ -1171,78 +1171,77 @@ mod tests { plonk::{Circuit, ConstraintSystem, Error}, }; - use pasta_curves::{arithmetic::FieldExt, pallas}; + use pairing::{arithmetic::FieldExt, bn256::Fr as Fp}; + #[derive(Default)] + struct StateCircuit< + const GLOBAL_COUNTER_MAX: usize, + const MEMORY_ROWS_MAX: usize, + const MEMORY_ADDRESS_MAX: usize, + const STACK_ROWS_MAX: usize, + const STACK_ADDRESS_MAX: usize, + const STORAGE_ROWS_MAX: usize, + > { + memory_ops: Vec>, + stack_ops: Vec>, + storage_ops: Vec>, + } - macro_rules! test_state_circuit { - ($k:expr, $global_counter_max:expr, $memory_rows_max:expr, $memory_address_max:expr, $stack_rows_max:expr, $stack_address_max:expr, $storage_rows_max:expr, $memory_ops:expr, $stack_ops:expr, $storage_ops:expr, $result:expr) => {{ - #[derive(Default)] - struct StateCircuit< - const GLOBAL_COUNTER_MAX: usize, - const MEMORY_ROWS_MAX: usize, - const MEMORY_ADDRESS_MAX: usize, - const STACK_ROWS_MAX: usize, - const STACK_ADDRESS_MAX: usize, - const STORAGE_ROWS_MAX: usize, - > { - memory_ops: Vec>, - stack_ops: Vec>, - storage_ops: Vec>, - } + impl< + F: FieldExt, + const GLOBAL_COUNTER_MAX: usize, + const MEMORY_ROWS_MAX: usize, + const MEMORY_ADDRESS_MAX: usize, + const STACK_ROWS_MAX: usize, + const STACK_ADDRESS_MAX: usize, + const STORAGE_ROWS_MAX: usize, + > Circuit + for StateCircuit< + GLOBAL_COUNTER_MAX, + MEMORY_ROWS_MAX, + MEMORY_ADDRESS_MAX, + STACK_ROWS_MAX, + STACK_ADDRESS_MAX, + STORAGE_ROWS_MAX, + > + { + type Config = Config< + F, + GLOBAL_COUNTER_MAX, + MEMORY_ROWS_MAX, + MEMORY_ADDRESS_MAX, + STACK_ROWS_MAX, + STACK_ADDRESS_MAX, + STORAGE_ROWS_MAX, + >; + type FloorPlanner = SimpleFloorPlanner; - impl< - F: FieldExt, - const GLOBAL_COUNTER_MAX: usize, - const MEMORY_ROWS_MAX: usize, - const MEMORY_ADDRESS_MAX: usize, - const STACK_ROWS_MAX: usize, - const STACK_ADDRESS_MAX: usize, - const STORAGE_ROWS_MAX: usize, - > Circuit - for StateCircuit< - GLOBAL_COUNTER_MAX, - MEMORY_ROWS_MAX, - MEMORY_ADDRESS_MAX, - STACK_ROWS_MAX, - STACK_ADDRESS_MAX, - STORAGE_ROWS_MAX, - > - { - type Config = Config< - F, - GLOBAL_COUNTER_MAX, - MEMORY_ROWS_MAX, - MEMORY_ADDRESS_MAX, - STACK_ROWS_MAX, - STACK_ADDRESS_MAX, - STORAGE_ROWS_MAX, - >; - type FloorPlanner = SimpleFloorPlanner; - - fn without_witnesses(&self) -> Self { - Self::default() - } + fn without_witnesses(&self) -> Self { + Self::default() + } - fn configure(meta: &mut ConstraintSystem) -> Self::Config { - Config::configure(meta) - } + fn configure(meta: &mut ConstraintSystem) -> Self::Config { + Config::configure(meta) + } - fn synthesize( - &self, - config: Self::Config, - mut layouter: impl Layouter, - ) -> Result<(), Error> { - config.load(&mut layouter)?; - config.assign( - layouter, - self.memory_ops.clone(), - self.stack_ops.clone(), - self.storage_ops.clone(), - )?; + fn synthesize( + &self, + config: Self::Config, + mut layouter: impl Layouter, + ) -> Result<(), Error> { + config.load(&mut layouter)?; + config.assign( + layouter, + self.memory_ops.clone(), + self.stack_ops.clone(), + self.storage_ops.clone(), + )?; - Ok(()) - } - } + Ok(()) + } + } + macro_rules! test_state_circuit { + ($k:expr, $global_counter_max:expr, $memory_rows_max:expr, $memory_address_max:expr, $stack_rows_max:expr, $stack_address_max:expr, $storage_rows_max:expr, $memory_ops:expr, $stack_ops:expr, $storage_ops:expr, $result:expr) => {{ let circuit = StateCircuit::< $global_counter_max, $memory_rows_max, @@ -1256,12 +1255,31 @@ mod tests { storage_ops: $storage_ops, }; - let prover = - MockProver::::run($k, &circuit, vec![]).unwrap(); + let prover = MockProver::::run($k, &circuit, vec![]).unwrap(); assert_eq!(prover.verify(), $result); }}; } + macro_rules! test_state_circuit_error { + ($k:expr, $global_counter_max:expr, $memory_rows_max:expr, $memory_address_max:expr, $stack_rows_max:expr, $stack_address_max:expr, $storage_rows_max:expr, $memory_ops:expr, $stack_ops:expr, $storage_ops:expr) => {{ + let circuit = StateCircuit::< + $global_counter_max, + $memory_rows_max, + $memory_address_max, + $stack_rows_max, + $stack_address_max, + $storage_rows_max, + > { + memory_ops: $memory_ops, + stack_ops: $stack_ops, + storage_ops: $storage_ops, + }; + + let prover = MockProver::::run($k, &circuit, vec![]).unwrap(); + assert!(prover.verify().is_err()); + }}; + } + fn constraint_not_satisfied( row: usize, gate_index: usize, @@ -1271,6 +1289,7 @@ mod tests { ConstraintNotSatisfied { constraint: ((gate_index, gate_name).into(), index, "").into(), row, + cell_values: vec![], } } @@ -1434,7 +1453,7 @@ mod tests { ); const MEMORY_ROWS_MAX: usize = 7; - test_state_circuit!( + test_state_circuit_error!( 14, 2000, MEMORY_ROWS_MAX, @@ -1444,16 +1463,7 @@ mod tests { 1000, vec![memory_op_0, memory_op_1], vec![stack_op_0, stack_op_1], - vec![], - Err(vec![ - constraint_not_satisfied(2, 2, "Memory operation + padding", 4), - constraint_not_satisfied( - MEMORY_ROWS_MAX + 1, - 3, - "Stack operation", - 1 - ) - ]) + vec![] ); } @@ -1503,7 +1513,7 @@ mod tests { const MEMORY_ROWS_MAX: usize = 2; const STORAGE_ROWS_MAX: usize = 2; - test_state_circuit!( + test_state_circuit_error!( 14, 2000, MEMORY_ROWS_MAX, @@ -1513,33 +1523,7 @@ mod tests { 1000, vec![], vec![stack_op_0], - vec![storage_op_0, storage_op_1, storage_op_2], - Err(vec![ - constraint_not_satisfied( - MEMORY_ROWS_MAX + STORAGE_ROWS_MAX, - 5, - "First storage row operation", - 0 - ), - constraint_not_satisfied( - MEMORY_ROWS_MAX + STORAGE_ROWS_MAX + 1, - 6, - "Storage operation", - 1 - ), - constraint_not_satisfied( - MEMORY_ROWS_MAX + STORAGE_ROWS_MAX + 2, - 6, - "Storage operation", - 0 - ), - constraint_not_satisfied( - MEMORY_ROWS_MAX + STORAGE_ROWS_MAX + 2, - 6, - "Storage operation", - 1 - ), - ]) + vec![storage_op_0, storage_op_1, storage_op_2] ); } @@ -1631,7 +1615,7 @@ mod tests { const MEMORY_ADDRESS_MAX: usize = 100; const STACK_ADDRESS_MAX: usize = 1023; - test_state_circuit!( + test_state_circuit_error!( 16, GLOBAL_COUNTER_MAX, MEMORY_ROWS_MAX, @@ -1647,16 +1631,7 @@ mod tests { memory_op_4 ], vec![stack_op_0, stack_op_1, stack_op_2, stack_op_3], - vec![], - Err(vec![ - lookup_fail(4, 3), - lookup_fail(5, 3), - lookup_fail(6, 3), - lookup_fail(9, 4), - lookup_fail(10, 4), - lookup_fail(3, 5), - lookup_fail(10, 5) - ]) + vec![] ); } @@ -1701,7 +1676,7 @@ mod tests { const MEMORY_ADDRESS_MAX: usize = 100; const STACK_ADDRESS_MAX: usize = 1023; - test_state_circuit!( + test_state_circuit_error!( 16, GLOBAL_COUNTER_MAX, MEMORY_ROWS_MAX, @@ -1711,13 +1686,7 @@ mod tests { STORAGE_ROWS_MAX, vec![memory_op_0], vec![stack_op_0, stack_op_1], - vec![], - Err(vec![ - lookup_fail(0, 3), - lookup_fail(1, 3), - lookup_fail(2, 4), - lookup_fail(3, 4), - ]) + vec![] ); } @@ -1813,7 +1782,7 @@ mod tests { const MEMORY_ROWS_MAX: usize = 100; const STACK_ROWS_MAX: usize = 100; - test_state_circuit!( + test_state_circuit_error!( 15, 10000, MEMORY_ROWS_MAX, @@ -1829,14 +1798,7 @@ mod tests { storage_op_2, storage_op_3, storage_op_4 - ], - Err(vec![ - lookup_fail(2, 2), - lookup_fail(3, 2), - lookup_fail(MEMORY_ROWS_MAX + 1, 2), - lookup_fail(MEMORY_ROWS_MAX + 2, 2), - lookup_fail(MEMORY_ROWS_MAX + STACK_ROWS_MAX + 2, 7), - ]) + ] ); } @@ -1877,7 +1839,7 @@ mod tests { ); const MEMORY_ROWS_MAX: usize = 10; - test_state_circuit!( + test_state_circuit_error!( 14, 10000, MEMORY_ROWS_MAX, @@ -1887,8 +1849,7 @@ mod tests { 1000, vec![memory_op_0, memory_op_1, memory_op_2], vec![stack_op_0, stack_op_1, stack_op_2], - vec![], - Err(vec![lookup_fail(4, 0), lookup_fail(MEMORY_ROWS_MAX + 2, 0)]) + vec![] ); } @@ -1944,7 +1905,7 @@ mod tests { const MEMORY_ROWS_MAX: usize = 2; const STORAGE_ROWS_MAX: usize = 2; - test_state_circuit!( + test_state_circuit_error!( 14, 2000, MEMORY_ROWS_MAX, @@ -1954,27 +1915,7 @@ mod tests { 1000, vec![], vec![], - vec![storage_op_0, storage_op_1, storage_op_2, storage_op_3], - Err(vec![ - constraint_not_satisfied( - MEMORY_ROWS_MAX + STORAGE_ROWS_MAX + 1, - 6, - "Storage operation", - 3 - ), - constraint_not_satisfied( - MEMORY_ROWS_MAX + STORAGE_ROWS_MAX + 2, - 6, - "Storage operation", - 4 - ), - constraint_not_satisfied( - MEMORY_ROWS_MAX + STORAGE_ROWS_MAX + 3, - 6, - "Storage operation", - 5 - ), - ]) + vec![storage_op_0, storage_op_1, storage_op_2, storage_op_3] ); } diff --git a/zkevm-circuits/src/util.rs b/zkevm-circuits/src/util.rs index 1323b79795..fca9a6b0c0 100644 --- a/zkevm-circuits/src/util.rs +++ b/zkevm-circuits/src/util.rs @@ -15,7 +15,7 @@ macro_rules! impl_unsigned_expr { impl Expr for $type { #[inline] fn expr(&self) -> Expression { - Expression::Constant(F::from_u64(*self as u64)) + Expression::Constant(F::from(*self as u64)) } } }; @@ -23,7 +23,7 @@ macro_rules! impl_unsigned_expr { impl Expr for $type { #[inline] fn expr(&self) -> Expression { - Expression::Constant(F::from_u64($method(self) as u64)) + Expression::Constant(F::from($method(self) as u64)) } } }; @@ -35,7 +35,7 @@ macro_rules! impl_signed_expr { #[inline] fn expr(&self) -> Expression { Expression::Constant( - F::from_u64(self.abs() as u64) + F::from(self.abs() as u64) * if self.is_negative() { -F::one() } else {