From d46f7d72fe5ba1cf65e019b626e42893a067b18c Mon Sep 17 00:00:00 2001 From: Zhang Zhuo Date: Wed, 12 Jun 2024 12:00:21 +0800 Subject: [PATCH] move Field and ToScalar to gadgets (#1337) --- Cargo.lock | 1 + .../src/aggregation/decoder/tables/fixed.rs | 2 +- .../decoder/tables/literals_header.rs | 2 +- aggregator/src/aggregation/decoder/witgen.rs | 2 +- .../src/aggregation/decoder/witgen/types.rs | 2 +- aggregator/src/batch.rs | 3 +- aggregator/src/util.rs | 2 +- .../src/circuit_input_builder/execution.rs | 2 +- eth-types/src/lib.rs | 79 ------------------ gadgets/Cargo.toml | 1 + gadgets/src/batched_is_zero.rs | 2 +- gadgets/src/binary_number.rs | 2 +- gadgets/src/comparator.rs | 2 +- gadgets/src/evm_word.rs | 2 +- gadgets/src/is_equal.rs | 4 +- gadgets/src/is_zero.rs | 4 +- gadgets/src/less_than.rs | 4 +- gadgets/src/lib.rs | 80 ++++++++++++++++++- gadgets/src/monotone.rs | 4 +- gadgets/src/mul_add.rs | 9 ++- gadgets/src/range.rs | 2 +- gadgets/src/util.rs | 17 ++-- .../src/bytecode_circuit/circuit.rs | 5 +- .../circuit/to_poseidon_hash.rs | 3 +- zkevm-circuits/src/ecc_circuit.rs | 3 +- .../src/evm_circuit/execution/begin_tx.rs | 3 +- .../src/evm_circuit/execution/blockhash.rs | 6 +- .../src/evm_circuit/execution/calldatacopy.rs | 3 +- .../src/evm_circuit/execution/callop.rs | 3 +- .../src/evm_circuit/execution/codecopy.rs | 3 +- .../src/evm_circuit/execution/create.rs | 3 +- .../src/evm_circuit/execution/end_block.rs | 5 +- .../src/evm_circuit/execution/end_tx.rs | 5 +- .../execution/error_oog_precompile.rs | 3 +- .../execution/error_oog_sload_sstore.rs | 3 +- .../execution/error_return_data_oo_bound.rs | 3 +- .../src/evm_circuit/execution/exp.rs | 3 +- .../src/evm_circuit/execution/extcodecopy.rs | 3 +- .../src/evm_circuit/execution/logs.rs | 3 +- .../src/evm_circuit/execution/mcopy.rs | 3 +- .../execution/precompiles/ec_add.rs | 3 +- .../execution/precompiles/ec_mul.rs | 3 +- .../execution/precompiles/ec_pairing.rs | 3 +- .../execution/precompiles/ecrecover.rs | 3 +- .../execution/precompiles/identity.rs | 3 +- .../evm_circuit/execution/precompiles/mod.rs | 2 +- .../execution/precompiles/modexp.rs | 3 +- .../execution/precompiles/sha256.rs | 3 +- .../evm_circuit/execution/return_revert.rs | 3 +- .../evm_circuit/execution/returndatacopy.rs | 3 +- .../src/evm_circuit/execution/selfbalance.rs | 2 +- .../src/evm_circuit/execution/sha3.rs | 3 +- .../src/evm_circuit/execution/sload.rs | 2 +- .../src/evm_circuit/execution/sstore.rs | 3 +- .../src/evm_circuit/execution/tload.rs | 3 +- .../src/evm_circuit/execution/tstore.rs | 3 +- .../src/evm_circuit/util/common_gadget.rs | 3 +- .../evm_circuit/util/common_gadget/curie.rs | 1 - .../util/common_gadget/tx_l1_fee.rs | 5 +- .../evm_circuit/util/constraint_builder.rs | 3 +- .../evm_circuit/util/math_gadget/add_words.rs | 3 +- .../util/math_gadget/batched_is_zero.rs | 1 + .../util/math_gadget/comparison.rs | 1 + .../evm_circuit/util/math_gadget/is_equal.rs | 1 + .../evm_circuit/util/math_gadget/is_zero.rs | 7 +- .../evm_circuit/util/math_gadget/min_max.rs | 3 +- .../util/math_gadget/mul_add_words.rs | 3 +- .../util/math_gadget/pair_select.rs | 1 + .../util/math_gadget/range_check.rs | 1 + .../src/evm_circuit/util/math_gadget/rlp.rs | 3 +- zkevm-circuits/src/exp_circuit.rs | 3 +- zkevm-circuits/src/keccak_circuit/util.rs | 3 +- zkevm-circuits/src/table.rs | 3 +- zkevm-circuits/src/tx_circuit.rs | 3 +- zkevm-circuits/src/util.rs | 2 +- zkevm-circuits/src/witness/block.rs | 5 +- zkevm-circuits/src/witness/mpt.rs | 3 +- zkevm-circuits/src/witness/rw.rs | 3 +- zkevm-circuits/src/witness/tx.rs | 7 +- 79 files changed, 223 insertions(+), 181 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1b049f1007..d7aebad556 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1952,6 +1952,7 @@ version = "0.11.0" dependencies = [ "eth-types", "halo2_proofs", + "poseidon-base", "rand", "rand_xorshift", "sha3 0.10.8", diff --git a/aggregator/src/aggregation/decoder/tables/fixed.rs b/aggregator/src/aggregation/decoder/tables/fixed.rs index c07750aee1..0c38785097 100644 --- a/aggregator/src/aggregation/decoder/tables/fixed.rs +++ b/aggregator/src/aggregation/decoder/tables/fixed.rs @@ -1,5 +1,5 @@ -use eth_types::Field; use gadgets::impl_expr; +use gadgets::Field; use halo2_proofs::{ circuit::{Layouter, Value}, halo2curves::bn256::Fr, diff --git a/aggregator/src/aggregation/decoder/tables/literals_header.rs b/aggregator/src/aggregation/decoder/tables/literals_header.rs index 142d89c2e2..ace547eee2 100644 --- a/aggregator/src/aggregation/decoder/tables/literals_header.rs +++ b/aggregator/src/aggregation/decoder/tables/literals_header.rs @@ -1,5 +1,5 @@ -use eth_types::Field; use gadgets::util::{and, not, select, Expr}; +use gadgets::Field; use halo2_proofs::{ circuit::{Layouter, Value}, halo2curves::bn256::Fr, diff --git a/aggregator/src/aggregation/decoder/witgen.rs b/aggregator/src/aggregation/decoder/witgen.rs index bda688e465..b0dde28d46 100644 --- a/aggregator/src/aggregation/decoder/witgen.rs +++ b/aggregator/src/aggregation/decoder/witgen.rs @@ -1,4 +1,4 @@ -use eth_types::Field; +use gadgets::Field; use halo2_proofs::circuit::Value; use revm_precompile::HashMap; diff --git a/aggregator/src/aggregation/decoder/witgen/types.rs b/aggregator/src/aggregation/decoder/witgen/types.rs index 0bd3af5535..9a2acfc0c0 100644 --- a/aggregator/src/aggregation/decoder/witgen/types.rs +++ b/aggregator/src/aggregation/decoder/witgen/types.rs @@ -1,8 +1,8 @@ use std::{collections::BTreeMap, io::Cursor}; use bitstream_io::{BitRead, BitReader, LittleEndian}; -use eth_types::Field; use gadgets::impl_expr; +use gadgets::Field; use halo2_proofs::{circuit::Value, plonk::Expression}; use itertools::Itertools; use std::collections::HashMap; diff --git a/aggregator/src/batch.rs b/aggregator/src/batch.rs index 5c7ffc49da..683439f3cb 100644 --- a/aggregator/src/batch.rs +++ b/aggregator/src/batch.rs @@ -1,8 +1,9 @@ //! This module implements related functions that aggregates public inputs of many chunks into a //! single one. -use eth_types::{Field, ToBigEndian, H256}; +use eth_types::{ToBigEndian, H256}; use ethers_core::utils::keccak256; +use gadgets::Field; use crate::{ blob::{BatchData, PointEvaluationAssignments}, diff --git a/aggregator/src/util.rs b/aggregator/src/util.rs index ff6cdc2403..dff5c854e0 100644 --- a/aggregator/src/util.rs +++ b/aggregator/src/util.rs @@ -1,4 +1,4 @@ -use eth_types::Field; +use gadgets::Field; use halo2_proofs::{circuit::AssignedCell, halo2curves::bn256::Fr, plonk::Error}; #[cfg(test)] diff --git a/bus-mapping/src/circuit_input_builder/execution.rs b/bus-mapping/src/circuit_input_builder/execution.rs index b70a22865f..7291070ddd 100644 --- a/bus-mapping/src/circuit_input_builder/execution.rs +++ b/bus-mapping/src/circuit_input_builder/execution.rs @@ -15,7 +15,7 @@ use crate::{ use eth_types::{ evm_types::{memory::MemoryWordRange, Gas, GasCost, MemoryAddress, OpcodeId, ProgramCounter}, sign_types::SignData, - Address, Field, GethExecStep, ToLittleEndian, Word, H256, U256, + Address, GethExecStep, ToLittleEndian, Word, H256, U256, }; use ethers_core::k256::elliptic_curve::subtle::CtOption; use gadgets::impl_expr; diff --git a/eth-types/src/lib.rs b/eth-types/src/lib.rs index 4990cc7547..806692eab7 100644 --- a/eth-types/src/lib.rs +++ b/eth-types/src/lib.rs @@ -49,7 +49,6 @@ pub use ethers_core::{ Address, Block, Bytes, Signature, H160, H256, H64, U256, U64, }, }; -use halo2curves::{bn256::Fr, group::ff::PrimeField}; use serde::{de, Deserialize, Deserializer, Serialize}; use std::{ collections::{HashMap, HashSet}, @@ -92,41 +91,6 @@ pub mod base64 { } } -/// Trait used to reduce verbosity with the declaration of the [`Field`] -/// trait and its repr. -pub trait Field: - PrimeField + poseidon_base::hash::Hashable + std::convert::From -{ - /// Re-expose zero element as a function - fn zero() -> Self { - Self::ZERO - } - - /// Re-expose one element as a function - fn one() -> Self { - Self::ONE - } - - /// Expose the lower 128 bits - fn get_lower_128(&self) -> u128 { - u128::from_le_bytes(self.to_repr().as_ref()[..16].try_into().unwrap()) - } -} - -// Impl custom `Field` trait for BN256 Fr to be used and consistent with the -// rest of the workspace. -impl Field for Fr {} - -// Impl custom `Field` trait for BN256 Fq to be used and consistent with the -// rest of the workspace. -// impl Field for Fq {} - -/// Trait used to define types that can be converted to a 256 bit scalar value. -pub trait ToScalar { - /// Convert the type to a scalar value. - fn to_scalar(&self) -> Option; -} - /// Trait used to convert a type to a [`Word`]. pub trait ToWord { /// Convert the type to a [`Word`]. @@ -179,14 +143,6 @@ impl<'de> Deserialize<'de> for DebugU256 { } } -impl ToScalar for DebugU256 { - fn to_scalar(&self) -> Option { - let mut bytes = [0u8; 32]; - self.to_little_endian(&mut bytes); - F::from_repr(bytes).into() - } -} - impl ToBigEndian for DebugU256 { /// Encode the value as byte array in big endian. fn to_be_bytes(&self) -> [u8; 32] { @@ -243,14 +199,6 @@ impl ToU16LittleEndian for U256 { } } -impl ToScalar for U256 { - fn to_scalar(&self) -> Option { - let mut bytes = [0u8; 32]; - self.to_little_endian(&mut bytes); - F::from_repr(bytes).into() - } -} - impl ToAddress for U256 { fn to_address(&self) -> Address { Address::from_slice(&self.to_be_bytes()[12..]) @@ -321,33 +269,6 @@ impl ToWord for Word { } } -impl ToScalar for Address { - fn to_scalar(&self) -> Option { - let mut bytes = [0u8; 32]; - bytes[32 - Self::len_bytes()..].copy_from_slice(self.as_bytes()); - bytes.reverse(); - F::from_repr(bytes).into() - } -} - -impl ToScalar for bool { - fn to_scalar(&self) -> Option { - self.to_word().to_scalar() - } -} - -impl ToScalar for u64 { - fn to_scalar(&self) -> Option { - Some(F::from(*self)) - } -} - -impl ToScalar for usize { - fn to_scalar(&self) -> Option { - u64::try_from(*self).ok().map(F::from) - } -} - /// Code hash related /// the empty keccak code hash pub static KECCAK_CODE_HASH_EMPTY: LazyLock = LazyLock::new(|| { diff --git a/gadgets/Cargo.toml b/gadgets/Cargo.toml index f773d511f2..4a0f98fede 100644 --- a/gadgets/Cargo.toml +++ b/gadgets/Cargo.toml @@ -9,6 +9,7 @@ halo2_proofs.workspace = true sha3.workspace = true eth-types = { path = "../eth-types" } strum.workspace = true +poseidon-base.workspace = true [dev-dependencies] rand_xorshift.workspace = true diff --git a/gadgets/src/batched_is_zero.rs b/gadgets/src/batched_is_zero.rs index f3a0dd58c7..933cf30adb 100644 --- a/gadgets/src/batched_is_zero.rs +++ b/gadgets/src/batched_is_zero.rs @@ -5,7 +5,7 @@ //! otherwise //! - is_zero: 1 if all `values` are `0`, `0` otherwise -use eth_types::Field; +use crate::Field; use halo2_proofs::{ circuit::{Region, Value}, plonk::{Advice, Column, ConstraintSystem, Error, Expression, Phase, VirtualCells}, diff --git a/gadgets/src/binary_number.rs b/gadgets/src/binary_number.rs index 62fbec2043..959797c1c7 100644 --- a/gadgets/src/binary_number.rs +++ b/gadgets/src/binary_number.rs @@ -3,7 +3,7 @@ //! equality. use crate::util::{and, not, Expr}; -use eth_types::Field; +use crate::Field; use halo2_proofs::{ circuit::{Region, Value}, plonk::{Advice, Any, Column, ConstraintSystem, Error, Expression, Fixed, VirtualCells}, diff --git a/gadgets/src/comparator.rs b/gadgets/src/comparator.rs index a9af6ebe44..9d9322727d 100644 --- a/gadgets/src/comparator.rs +++ b/gadgets/src/comparator.rs @@ -1,7 +1,7 @@ //! Comparator can be used to compare LT, EQ (and indirectly GT) for two //! expressions LHS and RHS. -use eth_types::Field; +use crate::Field; use halo2_proofs::{ circuit::{Chip, Region, Value}, plonk::{ConstraintSystem, Error, Expression, TableColumn, VirtualCells}, diff --git a/gadgets/src/evm_word.rs b/gadgets/src/evm_word.rs index 1019304f1e..b1ab183e66 100644 --- a/gadgets/src/evm_word.rs +++ b/gadgets/src/evm_word.rs @@ -6,8 +6,8 @@ //! In the zkevm circuit, this `encode(word)` expression will not be directly //! looked up. Instead, it will be folded into the bus mapping lookup. +use crate::Field; use crate::Variable; -use eth_types::Field; use halo2_proofs::{ circuit::{Region, Value}, plonk::{Advice, Column, ConstraintSystem, Error, Expression, Fixed, Selector}, diff --git a/gadgets/src/is_equal.rs b/gadgets/src/is_equal.rs index bd066d101e..9fab9707c9 100644 --- a/gadgets/src/is_equal.rs +++ b/gadgets/src/is_equal.rs @@ -1,6 +1,6 @@ //! IsEqual chip can be used to check equality of two expressions. -use eth_types::Field; +use crate::Field; use halo2_proofs::{ circuit::{Chip, Region, Value}, plonk::{Advice, Column, ConstraintSystem, Error, Expression, VirtualCells}, @@ -127,7 +127,7 @@ impl Chip for IsEqualChip { mod tests { use std::marker::PhantomData; - use eth_types::Field; + use crate::Field; use halo2_proofs::{ circuit::{Layouter, SimpleFloorPlanner, Value}, dev::MockProver, diff --git a/gadgets/src/is_zero.rs b/gadgets/src/is_zero.rs index b69bb23f13..10f8928a39 100644 --- a/gadgets/src/is_zero.rs +++ b/gadgets/src/is_zero.rs @@ -4,7 +4,7 @@ //! - witnesses `inv0(value)`, where `inv0(x)` is 0 when `x` = 0, and //! `1/x` otherwise -use eth_types::Field; +use crate::Field; use halo2_proofs::{ circuit::{Chip, Region, Value}, plonk::{Advice, Column, ConstraintSystem, Error, Expression, VirtualCells}, @@ -157,7 +157,7 @@ impl Chip for IsZeroChip { mod test { use super::{IsZeroChip, IsZeroConfig, IsZeroInstruction}; - use eth_types::Field; + use crate::Field; use halo2_proofs::{ circuit::{Layouter, SimpleFloorPlanner, Value}, dev::MockProver, diff --git a/gadgets/src/less_than.rs b/gadgets/src/less_than.rs index 171cd61e64..573f4df22f 100644 --- a/gadgets/src/less_than.rs +++ b/gadgets/src/less_than.rs @@ -1,6 +1,6 @@ //! Lt chip can be used to compare LT for two expressions LHS and RHS. -use eth_types::Field; +use crate::Field; use halo2_proofs::{ circuit::{Chip, Region, Value}, plonk::{Advice, Column, ConstraintSystem, Error, Expression, TableColumn, VirtualCells}, @@ -188,7 +188,7 @@ impl Chip for LtChip { #[cfg(test)] mod test { use super::{LtChip, LtConfig, LtInstruction}; - use eth_types::Field; + use crate::Field; use halo2_proofs::{ circuit::{Layouter, SimpleFloorPlanner, Value}, dev::MockProver, diff --git a/gadgets/src/lib.rs b/gadgets/src/lib.rs index c997babb15..18d9a78f39 100644 --- a/gadgets/src/lib.rs +++ b/gadgets/src/lib.rs @@ -23,12 +23,90 @@ pub mod mul_add; pub mod range; pub mod util; -use eth_types::Field; +use eth_types::Address; +use eth_types::DebugU256; +use eth_types::ToWord; +use eth_types::U256; use halo2_proofs::{ circuit::{AssignedCell, Value}, + halo2curves::{bn256::Fr, ff::PrimeField}, plonk::Expression, }; +/// Trait used to reduce verbosity with the declaration of the [`Field`] +/// trait and its repr. +pub trait Field: + PrimeField + poseidon_base::hash::Hashable + std::convert::From +{ + /// Re-expose zero element as a function + fn zero() -> Self { + Self::ZERO + } + + /// Re-expose one element as a function + fn one() -> Self { + Self::ONE + } + + /// Expose the lower 128 bits + fn get_lower_128(&self) -> u128 { + u128::from_le_bytes(self.to_repr().as_ref()[..16].try_into().unwrap()) + } +} + +// Impl custom `Field` trait for BN256 Fr to be used and consistent with the +// rest of the workspace. +impl Field for Fr {} + +/// Trait used to define types that can be converted to a 256 bit scalar value. +pub trait ToScalar { + /// Convert the type to a scalar value. + fn to_scalar(&self) -> Option; +} + +impl ToScalar for DebugU256 { + fn to_scalar(&self) -> Option { + let mut bytes = [0u8; 32]; + self.to_little_endian(&mut bytes); + F::from_repr(bytes).into() + } +} + +impl ToScalar for U256 { + fn to_scalar(&self) -> Option { + let mut bytes = [0u8; 32]; + self.to_little_endian(&mut bytes); + F::from_repr(bytes).into() + } +} + +impl ToScalar for Address { + fn to_scalar(&self) -> Option { + let mut bytes = [0u8; 32]; + bytes[32 - Self::len_bytes()..].copy_from_slice(self.as_bytes()); + bytes.reverse(); + F::from_repr(bytes).into() + } +} + +impl ToScalar for bool { + fn to_scalar(&self) -> Option { + self.to_word().to_scalar() + } +} + +impl ToScalar for u64 { + fn to_scalar(&self) -> Option { + Some(F::from(*self)) + } +} + +impl ToScalar for usize { + fn to_scalar(&self) -> Option { + u64::try_from(*self).ok().map(F::from) + } +} + #[allow(dead_code)] /// An assigned cell in the circuit. #[derive(Clone, Debug)] diff --git a/gadgets/src/monotone.rs b/gadgets/src/monotone.rs index 072e8ba90d..8c433dad2e 100644 --- a/gadgets/src/monotone.rs +++ b/gadgets/src/monotone.rs @@ -2,7 +2,7 @@ //! Monotone gadget helps to check if an advice column is monotonically //! increasing within a range. With strict enabled, it disallows equality of two //! cell. -use eth_types::Field; +use crate::Field; use halo2_proofs::{ circuit::{Chip, Layouter, Value}, plonk::{Advice, Column, ConstraintSystem, Error, Expression, Fixed, VirtualCells}, @@ -108,7 +108,7 @@ impl Chip #[cfg(test)] mod test { use super::{MonotoneChip, MonotoneConfig, Value}; - use eth_types::Field; + use crate::Field; use halo2_proofs::{ circuit::{Layouter, SimpleFloorPlanner}, dev::{ diff --git a/gadgets/src/mul_add.rs b/gadgets/src/mul_add.rs index e10fb58183..0f2bc52142 100644 --- a/gadgets/src/mul_add.rs +++ b/gadgets/src/mul_add.rs @@ -16,7 +16,7 @@ // |--------|-----------|-----------|-----------|-----------| // last row is padding to fit in 8 rows range_check_64 chip -use eth_types::{Field, Word, ToU16LittleEndian}; +use eth_types::{Word, ToU16LittleEndian}; use halo2_proofs::{ circuit::{Region, Value}, plonk::{Advice, Column, ConstraintSystem, Error, Expression, TableColumn, VirtualCells}, @@ -26,6 +26,7 @@ use halo2_proofs::{ use crate::{ range::{UIntRangeCheckChip, UIntRangeCheckInstruction}, util::{expr_from_u16, pow_of_two, split_u256, split_u256_limb64, Expr}, + Field, }; /// Config for the MulAddChip. @@ -438,9 +439,8 @@ impl MulAddChip { #[cfg(test)] mod test { - use std::marker::PhantomData; - - use eth_types::{Field, Word}; + use crate::Field; + use eth_types::Word; use halo2_proofs::{ circuit::{SimpleFloorPlanner, Value}, dev::MockProver, @@ -448,6 +448,7 @@ mod test { plonk::{Circuit, Selector}, }; use rand::Rng; + use std::marker::PhantomData; use crate::mul_add::{MulAddChip, MulAddConfig}; diff --git a/gadgets/src/range.rs b/gadgets/src/range.rs index 81cdebaba4..0b71fdfb45 100644 --- a/gadgets/src/range.rs +++ b/gadgets/src/range.rs @@ -7,7 +7,7 @@ //! u16 check can use U16Table directly use crate::util::Expr; -use eth_types::Field; +use crate::Field; use halo2_proofs::{ circuit::{Chip, Region, Value}, plonk::{Advice, Column, ConstraintSystem, Error, Expression, TableColumn, VirtualCells}, diff --git a/gadgets/src/util.rs b/gadgets/src/util.rs index 9799928c39..50726faa9f 100644 --- a/gadgets/src/util.rs +++ b/gadgets/src/util.rs @@ -1,14 +1,15 @@ //! Utility traits, functions used in the crate. +use crate::Field; use eth_types::{ evm_types::{GasCost, OpcodeId}, - Field, U256, + U256, }; use halo2_proofs::plonk::Expression; /// Returns the sum of the passed in cells pub mod sum { use crate::util::Expr; - use eth_types::Field; + use crate::Field; use halo2_proofs::plonk::Expression; /// Returns an expression for the sum of the list of expressions. @@ -30,7 +31,7 @@ pub mod sum { /// otherwise. Inputs need to be boolean pub mod and { use crate::util::Expr; - use eth_types::Field; + use crate::Field; use halo2_proofs::plonk::Expression; /// Returns an expression that evaluates to 1 only if all the expressions in @@ -52,7 +53,7 @@ pub mod and { pub mod or { use super::{and, not}; use crate::util::Expr; - use eth_types::Field; + use crate::Field; use halo2_proofs::plonk::Expression; /// Returns an expression that evaluates to 1 if any expression in the given @@ -71,7 +72,7 @@ pub mod or { /// `b` needs to be boolean pub mod not { use crate::util::Expr; - use eth_types::Field; + use crate::Field; use halo2_proofs::plonk::Expression; /// Returns an expression that represents the NOT of the given expression. @@ -89,7 +90,7 @@ pub mod not { /// `a` and `b` needs to be boolean pub mod xor { use crate::util::Expr; - use eth_types::Field; + use crate::Field; use halo2_proofs::plonk::Expression; /// Returns an expression that represents the XOR of the given expression. @@ -107,7 +108,7 @@ pub mod xor { /// `selector == 0`. `selector` needs to be boolean. pub mod select { use crate::util::Expr; - use eth_types::Field; + use crate::Field; use halo2_proofs::plonk::Expression; /// Returns the `when_true` expression when the selector is true, else @@ -160,7 +161,7 @@ macro_rules! impl_expr { } }; ($type:ty, $method:path) => { - impl $crate::util::Expr for $type { + impl $crate::util::Expr for $type { #[inline] fn expr(&self) -> Expression { Expression::Constant(F::from($method(self) as u64)) diff --git a/zkevm-circuits/src/bytecode_circuit/circuit.rs b/zkevm-circuits/src/bytecode_circuit/circuit.rs index 5dc3c255d4..5af5e91dbc 100644 --- a/zkevm-circuits/src/bytecode_circuit/circuit.rs +++ b/zkevm-circuits/src/bytecode_circuit/circuit.rs @@ -8,10 +8,9 @@ use crate::{ util::{get_push_size, Challenges, Expr, Field, SubCircuit, SubCircuitConfig}, witness, }; -use eth_types::{ - state_db::EMPTY_CODE_HASH_LE, ToLittleEndian, ToScalar, ToWord, POSEIDON_CODE_HASH_EMPTY, -}; +use eth_types::{state_db::EMPTY_CODE_HASH_LE, ToLittleEndian, ToWord, POSEIDON_CODE_HASH_EMPTY}; use gadgets::is_zero::{IsZeroChip, IsZeroConfig, IsZeroInstruction}; +use gadgets::ToScalar; use halo2_proofs::{ circuit::{Layouter, Region, Value}, plonk::{Advice, Column, ConstraintSystem, Error, Expression, Fixed, VirtualCells}, diff --git a/zkevm-circuits/src/bytecode_circuit/circuit/to_poseidon_hash.rs b/zkevm-circuits/src/bytecode_circuit/circuit/to_poseidon_hash.rs index f614afe5e3..f5fcdb967c 100644 --- a/zkevm-circuits/src/bytecode_circuit/circuit/to_poseidon_hash.rs +++ b/zkevm-circuits/src/bytecode_circuit/circuit/to_poseidon_hash.rs @@ -8,8 +8,9 @@ use crate::{ util::{Challenges, Expr, Field, SubCircuitConfig}, }; use bus_mapping::util::POSEIDON_CODE_HASH_EMPTY; -use eth_types::{ToScalar, ToWord}; +use eth_types::ToWord; use gadgets::is_zero::IsZeroChip; +use gadgets::ToScalar; use halo2_proofs::{ circuit::{Layouter, Region, Value}, plonk::{Advice, Column, ConstraintSystem, Error, Expression, VirtualCells}, diff --git a/zkevm-circuits/src/ecc_circuit.rs b/zkevm-circuits/src/ecc_circuit.rs index 376dd7e6e5..20331c8210 100644 --- a/zkevm-circuits/src/ecc_circuit.rs +++ b/zkevm-circuits/src/ecc_circuit.rs @@ -8,7 +8,8 @@ use bus_mapping::{ circuit_input_builder::{EcAddOp, EcMulOp, EcPairingOp, N_BYTES_PER_PAIR, N_PAIRING_PER_OP}, precompile::PrecompileCalls, }; -use eth_types::{ToLittleEndian, ToScalar, U256}; +use eth_types::{ToLittleEndian, U256}; +use gadgets::ToScalar; use halo2_base::{ gates::{GateInstructions, RangeInstructions}, utils::{decompose_bigint_option, fe_to_biguint, modulus}, diff --git a/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs b/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs index e7846f5ae2..efb92489ab 100644 --- a/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs +++ b/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs @@ -31,9 +31,10 @@ use crate::{ }; use array_init::array_init; use bus_mapping::{circuit_input_builder::CopyDataType, precompile::PrecompileCalls}; -use eth_types::{utils::is_precompiled, Address, ToLittleEndian, ToScalar, U256}; +use eth_types::{utils::is_precompiled, Address, ToLittleEndian, U256}; use ethers_core::utils::{get_contract_address, keccak256, rlp::RlpStream}; use gadgets::util::{expr_from_bytes, not, select, Expr}; +use gadgets::ToScalar; use halo2_proofs::{circuit::Value, plonk::Error}; const PRECOMPILE_COUNT: usize = 9; diff --git a/zkevm-circuits/src/evm_circuit/execution/blockhash.rs b/zkevm-circuits/src/evm_circuit/execution/blockhash.rs index b78acb4838..64ecc4cfce 100644 --- a/zkevm-circuits/src/evm_circuit/execution/blockhash.rs +++ b/zkevm-circuits/src/evm_circuit/execution/blockhash.rs @@ -20,11 +20,9 @@ use crate::{ util::{Expr, Field}, }; use bus_mapping::evm::OpcodeId; -use eth_types::{ - evm_types::block_utils::{is_valid_block_number, NUM_PREV_BLOCK_ALLOWED}, - ToScalar, -}; +use eth_types::evm_types::block_utils::{is_valid_block_number, NUM_PREV_BLOCK_ALLOWED}; use gadgets::util::not; +use gadgets::ToScalar; use halo2_proofs::{circuit::Value, plonk::Error}; #[derive(Clone, Debug)] diff --git a/zkevm-circuits/src/evm_circuit/execution/calldatacopy.rs b/zkevm-circuits/src/evm_circuit/execution/calldatacopy.rs index 7512e12da8..acc2aaf8fa 100644 --- a/zkevm-circuits/src/evm_circuit/execution/calldatacopy.rs +++ b/zkevm-circuits/src/evm_circuit/execution/calldatacopy.rs @@ -21,7 +21,8 @@ use crate::{ util::{Expr, Field}, }; use bus_mapping::{circuit_input_builder::CopyDataType, evm::OpcodeId}; -use eth_types::{evm_types::GasCost, ToScalar}; +use eth_types::evm_types::GasCost; +use gadgets::ToScalar; use halo2_proofs::{circuit::Value, plonk::Error}; #[derive(Clone, Debug)] diff --git a/zkevm-circuits/src/evm_circuit/execution/callop.rs b/zkevm-circuits/src/evm_circuit/execution/callop.rs index cab1e13f66..4e37388fae 100644 --- a/zkevm-circuits/src/evm_circuit/execution/callop.rs +++ b/zkevm-circuits/src/evm_circuit/execution/callop.rs @@ -29,8 +29,9 @@ use bus_mapping::{ use eth_types::{ evm_types::{memory::MemoryWordRange, GAS_STIPEND_CALL_WITH_VALUE}, utils::is_precompiled, - ToAddress, ToBigEndian, ToLittleEndian, ToScalar, U256, + ToAddress, ToBigEndian, ToLittleEndian, U256, }; +use gadgets::ToScalar; use halo2_proofs::{circuit::Value, plonk::Error}; use log::trace; use std::cmp::min; diff --git a/zkevm-circuits/src/evm_circuit/execution/codecopy.rs b/zkevm-circuits/src/evm_circuit/execution/codecopy.rs index cf7727f980..78c27230f9 100644 --- a/zkevm-circuits/src/evm_circuit/execution/codecopy.rs +++ b/zkevm-circuits/src/evm_circuit/execution/codecopy.rs @@ -1,6 +1,7 @@ use crate::util::Field; use bus_mapping::{circuit_input_builder::CopyDataType, evm::OpcodeId}; -use eth_types::{evm_types::GasCost, ToScalar}; +use eth_types::evm_types::GasCost; +use gadgets::ToScalar; use halo2_proofs::{circuit::Value, plonk::Error}; use crate::{ diff --git a/zkevm-circuits/src/evm_circuit/execution/create.rs b/zkevm-circuits/src/evm_circuit/execution/create.rs index ffa9afc516..c7877b6e9c 100644 --- a/zkevm-circuits/src/evm_circuit/execution/create.rs +++ b/zkevm-circuits/src/evm_circuit/execution/create.rs @@ -29,10 +29,11 @@ use bus_mapping::{circuit_input_builder::CopyDataType, evm::OpcodeId}; use eth_types::{ evm_types::{GasCost, CREATE2_GAS_PER_CODE_WORD, CREATE_GAS_PER_CODE_WORD, MAX_INIT_CODE_SIZE}, state_db::CodeDB, - ToBigEndian, ToLittleEndian, ToScalar, ToWord, H256, KECCAK_CODE_HASH_EMPTY, U256, + ToBigEndian, ToLittleEndian, ToWord, H256, KECCAK_CODE_HASH_EMPTY, U256, }; use ethers_core::utils::keccak256; use gadgets::util::{and, expr_from_bytes}; +use gadgets::ToScalar; use halo2_proofs::{circuit::Value, plonk::Error}; use log::trace; use std::iter::once; diff --git a/zkevm-circuits/src/evm_circuit/execution/end_block.rs b/zkevm-circuits/src/evm_circuit/execution/end_block.rs index 55bb141207..358e2aa132 100644 --- a/zkevm-circuits/src/evm_circuit/execution/end_block.rs +++ b/zkevm-circuits/src/evm_circuit/execution/end_block.rs @@ -23,8 +23,8 @@ use bus_mapping::l2_predeployed::message_queue::{ use eth_types::{ forks::HardforkId, utils::{hash_code, hash_code_keccak}, - ToScalar, }; +use gadgets::ToScalar; use halo2_proofs::{ circuit::{Cell as AssignedCell, Value}, plonk::{Error, Expression}, @@ -108,8 +108,7 @@ impl ExecutionGadget for EndBlockGadget { (HardforkId::Curie as u64).expr(), chain_id.expr(), cb.curr.state.block_number.expr(), - ], - }, + ]}, ); // Ref: bus-mapping/src/circuit_input_builder/curie.rs // Bytecode changes diff --git a/zkevm-circuits/src/evm_circuit/execution/end_tx.rs b/zkevm-circuits/src/evm_circuit/execution/end_tx.rs index b1c491a2ec..860005b9bf 100644 --- a/zkevm-circuits/src/evm_circuit/execution/end_tx.rs +++ b/zkevm-circuits/src/evm_circuit/execution/end_tx.rs @@ -24,10 +24,9 @@ use crate::{ }, util::{Expr, Field}, }; -use eth_types::{ - evm_types::MAX_REFUND_QUOTIENT_OF_GAS_USED, geth_types::TxType, ToLittleEndian, ToScalar, -}; +use eth_types::{evm_types::MAX_REFUND_QUOTIENT_OF_GAS_USED, geth_types::TxType, ToLittleEndian}; use gadgets::util::{not, select}; +use gadgets::ToScalar; use halo2_proofs::{circuit::Value, plonk::Error}; use strum::EnumCount; diff --git a/zkevm-circuits/src/evm_circuit/execution/error_oog_precompile.rs b/zkevm-circuits/src/evm_circuit/execution/error_oog_precompile.rs index 5a1761a271..2d006b44cf 100644 --- a/zkevm-circuits/src/evm_circuit/execution/error_oog_precompile.rs +++ b/zkevm-circuits/src/evm_circuit/execution/error_oog_precompile.rs @@ -15,8 +15,9 @@ use crate::{ witness::{Block, Call, ExecStep, Transaction}, }; use bus_mapping::precompile::PrecompileCalls; -use eth_types::{evm_types::GasCost, ToScalar}; +use eth_types::evm_types::GasCost; use gadgets::util::{sum, Expr}; +use gadgets::ToScalar; use halo2_proofs::{circuit::Value, plonk::Error}; #[derive(Clone, Debug)] diff --git a/zkevm-circuits/src/evm_circuit/execution/error_oog_sload_sstore.rs b/zkevm-circuits/src/evm_circuit/execution/error_oog_sload_sstore.rs index fce1822e8e..5e47c9cb0d 100644 --- a/zkevm-circuits/src/evm_circuit/execution/error_oog_sload_sstore.rs +++ b/zkevm-circuits/src/evm_circuit/execution/error_oog_sload_sstore.rs @@ -20,8 +20,9 @@ use crate::{ }; use eth_types::{ evm_types::{GasCost, OpcodeId}, - ToScalar, U256, + U256, }; +use gadgets::ToScalar; use halo2_proofs::{circuit::Value, plonk::Error}; /// Gadget to implement the corresponding out of gas errors for diff --git a/zkevm-circuits/src/evm_circuit/execution/error_return_data_oo_bound.rs b/zkevm-circuits/src/evm_circuit/execution/error_return_data_oo_bound.rs index 06ecb86ba4..6199015de4 100644 --- a/zkevm-circuits/src/evm_circuit/execution/error_return_data_oo_bound.rs +++ b/zkevm-circuits/src/evm_circuit/execution/error_return_data_oo_bound.rs @@ -12,7 +12,8 @@ use crate::{ table::CallContextFieldTag, util::{Expr, Field}, }; -use eth_types::{evm_types::OpcodeId, ToLittleEndian, ToScalar}; +use eth_types::{evm_types::OpcodeId, ToLittleEndian}; +use gadgets::ToScalar; use halo2_proofs::{circuit::Value, plonk::Error}; #[derive(Clone, Debug)] diff --git a/zkevm-circuits/src/evm_circuit/execution/exp.rs b/zkevm-circuits/src/evm_circuit/execution/exp.rs index d5872e1fa6..6ae45e906a 100644 --- a/zkevm-circuits/src/evm_circuit/execution/exp.rs +++ b/zkevm-circuits/src/evm_circuit/execution/exp.rs @@ -1,7 +1,8 @@ use crate::util::Field; use bus_mapping::evm::OpcodeId; -use eth_types::{evm_types::GasCost, ToLittleEndian, ToScalar}; +use eth_types::{evm_types::GasCost, ToLittleEndian}; use gadgets::util::{and, not, split_u256, Expr}; +use gadgets::ToScalar; use halo2_proofs::plonk::Error; use crate::evm_circuit::{ diff --git a/zkevm-circuits/src/evm_circuit/execution/extcodecopy.rs b/zkevm-circuits/src/evm_circuit/execution/extcodecopy.rs index 4bf266a6b4..a7370e8e1f 100644 --- a/zkevm-circuits/src/evm_circuit/execution/extcodecopy.rs +++ b/zkevm-circuits/src/evm_circuit/execution/extcodecopy.rs @@ -22,8 +22,9 @@ use crate::{ util::Field, }; use bus_mapping::circuit_input_builder::CopyDataType; -use eth_types::{evm_types::GasCost, ToLittleEndian, ToScalar}; +use eth_types::{evm_types::GasCost, ToLittleEndian}; use gadgets::util::Expr; +use gadgets::ToScalar; use halo2_proofs::{circuit::Value, plonk::Error}; use super::ExecutionGadget; diff --git a/zkevm-circuits/src/evm_circuit/execution/logs.rs b/zkevm-circuits/src/evm_circuit/execution/logs.rs index e850ca0abf..a8716c22db 100644 --- a/zkevm-circuits/src/evm_circuit/execution/logs.rs +++ b/zkevm-circuits/src/evm_circuit/execution/logs.rs @@ -23,8 +23,9 @@ use array_init::array_init; use bus_mapping::circuit_input_builder::CopyDataType; use eth_types::{ evm_types::{GasCost, OpcodeId}, - ToScalar, U256, + U256, }; +use gadgets::ToScalar; use halo2_proofs::{circuit::Value, plonk::Error}; #[derive(Clone, Debug)] diff --git a/zkevm-circuits/src/evm_circuit/execution/mcopy.rs b/zkevm-circuits/src/evm_circuit/execution/mcopy.rs index a9ae0f8ed1..e6fa7d7228 100644 --- a/zkevm-circuits/src/evm_circuit/execution/mcopy.rs +++ b/zkevm-circuits/src/evm_circuit/execution/mcopy.rs @@ -18,7 +18,8 @@ use crate::{ util::{Expr, Field}, }; use bus_mapping::{circuit_input_builder::CopyDataType, evm::OpcodeId}; -use eth_types::{evm_types::GasCost, ToScalar}; +use eth_types::evm_types::GasCost; +use gadgets::ToScalar; // use gadgets::util::Expr; use halo2_proofs::{circuit::Value, plonk::Error}; diff --git a/zkevm-circuits/src/evm_circuit/execution/precompiles/ec_add.rs b/zkevm-circuits/src/evm_circuit/execution/precompiles/ec_add.rs index fc7bf4661d..7e6aa4c4ac 100644 --- a/zkevm-circuits/src/evm_circuit/execution/precompiles/ec_add.rs +++ b/zkevm-circuits/src/evm_circuit/execution/precompiles/ec_add.rs @@ -1,7 +1,8 @@ use crate::util::Field; use bus_mapping::precompile::{PrecompileAuxData, PrecompileCalls}; -use eth_types::{evm_types::GasCost, ToLittleEndian, ToScalar}; +use eth_types::{evm_types::GasCost, ToLittleEndian}; use gadgets::util::{not, select, Expr}; +use gadgets::ToScalar; use halo2_proofs::{circuit::Value, plonk::Error}; use crate::{ diff --git a/zkevm-circuits/src/evm_circuit/execution/precompiles/ec_mul.rs b/zkevm-circuits/src/evm_circuit/execution/precompiles/ec_mul.rs index f589782ed1..fcf5ce3840 100644 --- a/zkevm-circuits/src/evm_circuit/execution/precompiles/ec_mul.rs +++ b/zkevm-circuits/src/evm_circuit/execution/precompiles/ec_mul.rs @@ -16,8 +16,9 @@ use crate::{ witness::{Block, Call, ExecStep, Transaction}, }; use bus_mapping::precompile::{PrecompileAuxData, PrecompileCalls}; -use eth_types::{evm_types::GasCost, ToLittleEndian, ToScalar, U256}; +use eth_types::{evm_types::GasCost, ToLittleEndian, U256}; use gadgets::util::{and, not, or, select, split_u256, sum, Expr}; +use gadgets::ToScalar; use halo2_proofs::{ circuit::Value, plonk::{Error, Expression}, diff --git a/zkevm-circuits/src/evm_circuit/execution/precompiles/ec_pairing.rs b/zkevm-circuits/src/evm_circuit/execution/precompiles/ec_pairing.rs index 08d14167f4..05aa25777c 100644 --- a/zkevm-circuits/src/evm_circuit/execution/precompiles/ec_pairing.rs +++ b/zkevm-circuits/src/evm_circuit/execution/precompiles/ec_pairing.rs @@ -3,8 +3,9 @@ use bus_mapping::{ circuit_input_builder::{N_BYTES_PER_PAIR, N_PAIRING_PER_OP}, precompile::{EcPairingError, PrecompileAuxData, PrecompileCalls}, }; -use eth_types::{evm_types::GasCost, ToScalar}; +use eth_types::evm_types::GasCost; use gadgets::util::{and, not, or, select, Expr}; +use gadgets::ToScalar; use halo2_proofs::{circuit::Value, plonk::Error}; use crate::{ diff --git a/zkevm-circuits/src/evm_circuit/execution/precompiles/ecrecover.rs b/zkevm-circuits/src/evm_circuit/execution/precompiles/ecrecover.rs index b9b3175fcc..8d2687846c 100644 --- a/zkevm-circuits/src/evm_circuit/execution/precompiles/ecrecover.rs +++ b/zkevm-circuits/src/evm_circuit/execution/precompiles/ecrecover.rs @@ -1,7 +1,8 @@ use crate::util::Field; use bus_mapping::precompile::{PrecompileAuxData, PrecompileCalls}; -use eth_types::{evm_types::GasCost, word, ToLittleEndian, ToScalar, U256}; +use eth_types::{evm_types::GasCost, word, ToLittleEndian, U256}; use gadgets::util::{and, not, or, select, sum, Expr}; +use gadgets::ToScalar; use halo2_proofs::{ circuit::Value, plonk::{Error, Expression}, diff --git a/zkevm-circuits/src/evm_circuit/execution/precompiles/identity.rs b/zkevm-circuits/src/evm_circuit/execution/precompiles/identity.rs index 9044f53e01..16a2252b63 100644 --- a/zkevm-circuits/src/evm_circuit/execution/precompiles/identity.rs +++ b/zkevm-circuits/src/evm_circuit/execution/precompiles/identity.rs @@ -1,7 +1,8 @@ use crate::util::Field; use bus_mapping::precompile::PrecompileAuxData; -use eth_types::{evm_types::GasCost, ToScalar}; +use eth_types::evm_types::GasCost; use gadgets::util::{select, Expr}; +use gadgets::ToScalar; use halo2_proofs::{circuit::Value, plonk::Error}; use crate::{ diff --git a/zkevm-circuits/src/evm_circuit/execution/precompiles/mod.rs b/zkevm-circuits/src/evm_circuit/execution/precompiles/mod.rs index e4c576c710..b6cf969c1e 100644 --- a/zkevm-circuits/src/evm_circuit/execution/precompiles/mod.rs +++ b/zkevm-circuits/src/evm_circuit/execution/precompiles/mod.rs @@ -13,8 +13,8 @@ use crate::{ witness::{Block, Call, ExecStep, Transaction}, }; use bus_mapping::precompile::PrecompileAuxData; -use eth_types::ToScalar; use gadgets::util::{select, Expr}; +use gadgets::ToScalar; use halo2_proofs::{ circuit::Value, plonk::{Error, Expression}, diff --git a/zkevm-circuits/src/evm_circuit/execution/precompiles/modexp.rs b/zkevm-circuits/src/evm_circuit/execution/precompiles/modexp.rs index 713592a479..85e6b81092 100755 --- a/zkevm-circuits/src/evm_circuit/execution/precompiles/modexp.rs +++ b/zkevm-circuits/src/evm_circuit/execution/precompiles/modexp.rs @@ -2,8 +2,9 @@ use crate::util::Field; use bus_mapping::precompile::{ PrecompileAuxData, PrecompileCalls, MODEXP_INPUT_LIMIT, MODEXP_SIZE_LIMIT, }; -use eth_types::{evm_types::GasCost, ToBigEndian, ToScalar, U256}; +use eth_types::{evm_types::GasCost, ToBigEndian, U256}; use gadgets::util::{self, not, select, Expr}; +use gadgets::ToScalar; use halo2_proofs::{ circuit::Value, plonk::{Error, Expression}, diff --git a/zkevm-circuits/src/evm_circuit/execution/precompiles/sha256.rs b/zkevm-circuits/src/evm_circuit/execution/precompiles/sha256.rs index 54bb5bd119..0ce2cc0659 100644 --- a/zkevm-circuits/src/evm_circuit/execution/precompiles/sha256.rs +++ b/zkevm-circuits/src/evm_circuit/execution/precompiles/sha256.rs @@ -1,7 +1,8 @@ use crate::util::Field; use bus_mapping::precompile::PrecompileAuxData; -use eth_types::{evm_types::GasCost, ToScalar}; +use eth_types::evm_types::GasCost; use gadgets::util::{select, Expr}; +use gadgets::ToScalar; use halo2_proofs::{circuit::Value, plonk::Error}; use crate::{ diff --git a/zkevm-circuits/src/evm_circuit/execution/return_revert.rs b/zkevm-circuits/src/evm_circuit/execution/return_revert.rs index fa49615397..3fe6f3782d 100644 --- a/zkevm-circuits/src/evm_circuit/execution/return_revert.rs +++ b/zkevm-circuits/src/evm_circuit/execution/return_revert.rs @@ -24,8 +24,9 @@ use bus_mapping::circuit_input_builder::CopyDataType; use eth_types::{ evm_types::{GasCost, OpcodeId}, utils::{hash_code, hash_code_keccak}, - ToScalar, ToWord, + ToWord, }; +use gadgets::ToScalar; use halo2_proofs::{circuit::Value, plonk::Error}; #[derive(Clone, Debug)] diff --git a/zkevm-circuits/src/evm_circuit/execution/returndatacopy.rs b/zkevm-circuits/src/evm_circuit/execution/returndatacopy.rs index d515ae736f..35f0c522a9 100644 --- a/zkevm-circuits/src/evm_circuit/execution/returndatacopy.rs +++ b/zkevm-circuits/src/evm_circuit/execution/returndatacopy.rs @@ -22,8 +22,9 @@ use crate::{ util::{Expr, Field}, }; use bus_mapping::{circuit_input_builder::CopyDataType, evm::OpcodeId}; -use eth_types::{evm_types::GasCost, ToScalar}; +use eth_types::evm_types::GasCost; use gadgets::util::not; +use gadgets::ToScalar; use halo2_proofs::{circuit::Value, plonk::Error}; #[derive(Clone, Debug)] diff --git a/zkevm-circuits/src/evm_circuit/execution/selfbalance.rs b/zkevm-circuits/src/evm_circuit/execution/selfbalance.rs index b4cab6fea0..796adc26dc 100644 --- a/zkevm-circuits/src/evm_circuit/execution/selfbalance.rs +++ b/zkevm-circuits/src/evm_circuit/execution/selfbalance.rs @@ -13,7 +13,7 @@ use crate::{ util::{Expr, Field}, }; use bus_mapping::evm::OpcodeId; -use eth_types::ToScalar; +use gadgets::ToScalar; use halo2_proofs::{circuit::Value, plonk::Error}; #[derive(Clone, Debug)] diff --git a/zkevm-circuits/src/evm_circuit/execution/sha3.rs b/zkevm-circuits/src/evm_circuit/execution/sha3.rs index 7d2b3f87a9..f0c7af5a5f 100644 --- a/zkevm-circuits/src/evm_circuit/execution/sha3.rs +++ b/zkevm-circuits/src/evm_circuit/execution/sha3.rs @@ -1,7 +1,8 @@ use crate::util::Field; use bus_mapping::{circuit_input_builder::CopyDataType, evm::OpcodeId}; -use eth_types::{evm_types::GasCost, ToLittleEndian, ToScalar}; +use eth_types::{evm_types::GasCost, ToLittleEndian}; use gadgets::util::{not, Expr}; +use gadgets::ToScalar; use halo2_proofs::{circuit::Value, plonk::Error}; use crate::evm_circuit::{ diff --git a/zkevm-circuits/src/evm_circuit/execution/sload.rs b/zkevm-circuits/src/evm_circuit/execution/sload.rs index f49d9ab484..fb7adbebe1 100644 --- a/zkevm-circuits/src/evm_circuit/execution/sload.rs +++ b/zkevm-circuits/src/evm_circuit/execution/sload.rs @@ -14,7 +14,7 @@ use crate::{ table::CallContextFieldTag, util::{Expr, Field}, }; -use eth_types::ToScalar; +use gadgets::ToScalar; use halo2_proofs::{circuit::Value, plonk::Error}; #[derive(Clone, Debug)] diff --git a/zkevm-circuits/src/evm_circuit/execution/sstore.rs b/zkevm-circuits/src/evm_circuit/execution/sstore.rs index a08feeeedc..ab9ca8cf94 100644 --- a/zkevm-circuits/src/evm_circuit/execution/sstore.rs +++ b/zkevm-circuits/src/evm_circuit/execution/sstore.rs @@ -19,9 +19,10 @@ use crate::{ table::CallContextFieldTag, util::Expr, }; +use gadgets::ToScalar; use crate::util::Field; -use eth_types::{evm_types::GasCost, ToScalar}; +use eth_types::evm_types::GasCost; use halo2_proofs::{ circuit::Value, plonk::{Error, Expression}, diff --git a/zkevm-circuits/src/evm_circuit/execution/tload.rs b/zkevm-circuits/src/evm_circuit/execution/tload.rs index 14d19035bb..6081e39fec 100644 --- a/zkevm-circuits/src/evm_circuit/execution/tload.rs +++ b/zkevm-circuits/src/evm_circuit/execution/tload.rs @@ -13,7 +13,8 @@ use crate::{ util::{Expr, Field}, }; use bus_mapping::evm::OpcodeId; -use eth_types::{ToLittleEndian, ToScalar}; +use eth_types::ToLittleEndian; +use gadgets::ToScalar; use halo2_proofs::{circuit::Value, plonk::Error}; #[derive(Clone, Debug)] diff --git a/zkevm-circuits/src/evm_circuit/execution/tstore.rs b/zkevm-circuits/src/evm_circuit/execution/tstore.rs index 7313c4fefe..eb469fcc61 100644 --- a/zkevm-circuits/src/evm_circuit/execution/tstore.rs +++ b/zkevm-circuits/src/evm_circuit/execution/tstore.rs @@ -16,7 +16,8 @@ use crate::{ util::{Expr, Field}, }; use bus_mapping::evm::OpcodeId; -use eth_types::{ToLittleEndian, ToScalar}; +use eth_types::ToLittleEndian; +use gadgets::ToScalar; use halo2_proofs::{circuit::Value, plonk::Error}; #[derive(Clone, Debug)] diff --git a/zkevm-circuits/src/evm_circuit/util/common_gadget.rs b/zkevm-circuits/src/evm_circuit/util/common_gadget.rs index 0716cd2b89..266ba2d1f0 100644 --- a/zkevm-circuits/src/evm_circuit/util/common_gadget.rs +++ b/zkevm-circuits/src/evm_circuit/util/common_gadget.rs @@ -24,8 +24,9 @@ use crate::{ witness::{Block, Call, ExecStep}, }; use either::Either; -use eth_types::{evm_types::GasCost, ToLittleEndian, ToScalar, U256}; +use eth_types::{evm_types::GasCost, ToLittleEndian, U256}; use gadgets::util::{select, sum}; +use gadgets::ToScalar; use halo2_proofs::{ circuit::Value, plonk::{Error, Expression}, diff --git a/zkevm-circuits/src/evm_circuit/util/common_gadget/curie.rs b/zkevm-circuits/src/evm_circuit/util/common_gadget/curie.rs index 7a3f086698..96172ebe1e 100644 --- a/zkevm-circuits/src/evm_circuit/util/common_gadget/curie.rs +++ b/zkevm-circuits/src/evm_circuit/util/common_gadget/curie.rs @@ -12,7 +12,6 @@ use crate::{ }; use eth_types::forks::{HardforkId, SCROLL_DEVNET_CHAIN_ID, SCROLL_MAINNET_CHAIN_ID}; -//use eth_types::{ToLittleEndian, ToScalar, U256}; use gadgets::util::not; use halo2_proofs::{ circuit::Value, diff --git a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs index fb63a29ffe..041e597db9 100644 --- a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs +++ b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs @@ -15,8 +15,9 @@ use bus_mapping::{ circuit_input_builder::{TxL1Fee, TX_L1_COMMIT_EXTRA_COST, TX_L1_FEE_PRECISION}, l2_predeployed::l1_gas_price_oracle, }; -use eth_types::{ToLittleEndian, ToScalar, U256}; +use eth_types::{ToLittleEndian, U256}; use gadgets::util::not; +use gadgets::ToScalar; use halo2_proofs::plonk::{Error, Expression}; /// Transaction L1 fee gadget for L1GasPriceOracle contract @@ -337,7 +338,7 @@ mod tests { constraint_builder::ConstrainBuilderCommon, math_gadget::test_util::{test_math_gadget_container, try_test, MathGadgetContainer}, }; - use eth_types::{ToScalar, U256}; + use eth_types::U256; use halo2_proofs::{circuit::Value, halo2curves::bn256::Fr}; //refer to test in diff --git a/zkevm-circuits/src/evm_circuit/util/constraint_builder.rs b/zkevm-circuits/src/evm_circuit/util/constraint_builder.rs index 37a2708263..a009687417 100644 --- a/zkevm-circuits/src/evm_circuit/util/constraint_builder.rs +++ b/zkevm-circuits/src/evm_circuit/util/constraint_builder.rs @@ -12,8 +12,9 @@ use crate::{ util::{build_tx_log_expression, Challenges, Expr, Field}, }; use bus_mapping::util::{KECCAK_CODE_HASH_EMPTY, POSEIDON_CODE_HASH_EMPTY}; -use eth_types::{state_db::EMPTY_CODE_HASH_LE, ToLittleEndian, ToScalar, ToWord, H256}; +use eth_types::{state_db::EMPTY_CODE_HASH_LE, ToLittleEndian, ToWord, H256}; use gadgets::util::{and, not}; +use gadgets::ToScalar; use halo2_proofs::{ circuit::Value, plonk::{ diff --git a/zkevm-circuits/src/evm_circuit/util/math_gadget/add_words.rs b/zkevm-circuits/src/evm_circuit/util/math_gadget/add_words.rs index 1f954f748c..6387c5f939 100644 --- a/zkevm-circuits/src/evm_circuit/util/math_gadget/add_words.rs +++ b/zkevm-circuits/src/evm_circuit/util/math_gadget/add_words.rs @@ -6,7 +6,8 @@ use crate::{ }, util::{Expr, Field}, }; -use eth_types::{ToLittleEndian, ToScalar, Word}; +use eth_types::{ToLittleEndian, Word}; +use gadgets::ToScalar; use halo2_proofs::{circuit::Value, plonk::Error}; /// Construction of 2 256-bit words addition and result, which is useful for diff --git a/zkevm-circuits/src/evm_circuit/util/math_gadget/batched_is_zero.rs b/zkevm-circuits/src/evm_circuit/util/math_gadget/batched_is_zero.rs index 8af72c4129..f7c8d925d8 100644 --- a/zkevm-circuits/src/evm_circuit/util/math_gadget/batched_is_zero.rs +++ b/zkevm-circuits/src/evm_circuit/util/math_gadget/batched_is_zero.rs @@ -88,6 +88,7 @@ mod tests { use super::{super::test_util::*, *}; use crate::util::Field; use eth_types::*; + use gadgets::ToScalar; use halo2_proofs::{halo2curves::bn256::Fr, plonk::Error}; #[derive(Clone)] diff --git a/zkevm-circuits/src/evm_circuit/util/math_gadget/comparison.rs b/zkevm-circuits/src/evm_circuit/util/math_gadget/comparison.rs index 0c9c7d6a13..dbcc06223d 100644 --- a/zkevm-circuits/src/evm_circuit/util/math_gadget/comparison.rs +++ b/zkevm-circuits/src/evm_circuit/util/math_gadget/comparison.rs @@ -58,6 +58,7 @@ mod tests { util::Field, }; use eth_types::*; + use gadgets::ToScalar; use halo2_proofs::{halo2curves::bn256::Fr, plonk::Error}; #[derive(Clone)] diff --git a/zkevm-circuits/src/evm_circuit/util/math_gadget/is_equal.rs b/zkevm-circuits/src/evm_circuit/util/math_gadget/is_equal.rs index 374530fb12..c8b0bcc8b3 100644 --- a/zkevm-circuits/src/evm_circuit/util/math_gadget/is_equal.rs +++ b/zkevm-circuits/src/evm_circuit/util/math_gadget/is_equal.rs @@ -62,6 +62,7 @@ mod tests { util::Field, }; use eth_types::*; + use gadgets::ToScalar; use halo2_proofs::{halo2curves::bn256::Fr, plonk::Error}; #[derive(Clone)] diff --git a/zkevm-circuits/src/evm_circuit/util/math_gadget/is_zero.rs b/zkevm-circuits/src/evm_circuit/util/math_gadget/is_zero.rs index 4c48678397..20a27d1733 100644 --- a/zkevm-circuits/src/evm_circuit/util/math_gadget/is_zero.rs +++ b/zkevm-circuits/src/evm_circuit/util/math_gadget/is_zero.rs @@ -70,14 +70,13 @@ impl IsZeroGadget { #[cfg(test)] mod tests { + use super::{super::test_util::*, *}; use crate::{ evm_circuit::util::{constraint_builder::ConstrainBuilderCommon, Cell}, util::Expr, }; - - use super::{super::test_util::*, *}; - - use eth_types::{ToScalar, Word}; + use eth_types::Word; + use gadgets::ToScalar; use halo2_proofs::{halo2curves::bn256::Fr, plonk::Error}; #[derive(Clone)] diff --git a/zkevm-circuits/src/evm_circuit/util/math_gadget/min_max.rs b/zkevm-circuits/src/evm_circuit/util/math_gadget/min_max.rs index e2e9a7eda6..e8a8882b30 100644 --- a/zkevm-circuits/src/evm_circuit/util/math_gadget/min_max.rs +++ b/zkevm-circuits/src/evm_circuit/util/math_gadget/min_max.rs @@ -73,7 +73,8 @@ impl MinMaxGadget { mod tests { use super::{test_util::*, *}; use crate::evm_circuit::util::{constraint_builder::ConstrainBuilderCommon, Cell}; - use eth_types::{ToScalar, Word}; + use eth_types::Word; + use gadgets::ToScalar; use halo2_proofs::{halo2curves::bn256::Fr, plonk::Error}; #[derive(Clone)] diff --git a/zkevm-circuits/src/evm_circuit/util/math_gadget/mul_add_words.rs b/zkevm-circuits/src/evm_circuit/util/math_gadget/mul_add_words.rs index a94642871b..907455ab1f 100644 --- a/zkevm-circuits/src/evm_circuit/util/math_gadget/mul_add_words.rs +++ b/zkevm-circuits/src/evm_circuit/util/math_gadget/mul_add_words.rs @@ -151,7 +151,8 @@ impl MulAddWordsGadget { #[cfg(test)] mod tests { use super::{super::test_util::*, *}; - use eth_types::{ToScalar, Word}; + use eth_types::Word; + use gadgets::ToScalar; use halo2_proofs::{halo2curves::bn256::Fr, plonk::Error}; #[derive(Clone)] diff --git a/zkevm-circuits/src/evm_circuit/util/math_gadget/pair_select.rs b/zkevm-circuits/src/evm_circuit/util/math_gadget/pair_select.rs index 0afcb66776..1b38d1cf2d 100644 --- a/zkevm-circuits/src/evm_circuit/util/math_gadget/pair_select.rs +++ b/zkevm-circuits/src/evm_circuit/util/math_gadget/pair_select.rs @@ -66,6 +66,7 @@ mod tests { use super::{super::test_util::*, *}; use crate::util::Field; use eth_types::*; + use gadgets::ToScalar; use halo2_proofs::{halo2curves::bn256::Fr, plonk::Error}; #[derive(Clone)] diff --git a/zkevm-circuits/src/evm_circuit/util/math_gadget/range_check.rs b/zkevm-circuits/src/evm_circuit/util/math_gadget/range_check.rs index 7352a72cae..566f6b0f4f 100644 --- a/zkevm-circuits/src/evm_circuit/util/math_gadget/range_check.rs +++ b/zkevm-circuits/src/evm_circuit/util/math_gadget/range_check.rs @@ -56,6 +56,7 @@ mod tests { use crate::util::Field; use eth_types::*; use gadgets::util::Expr; + use gadgets::ToScalar; use halo2_proofs::{circuit::Value, halo2curves::bn256::Fr, plonk::Error}; #[derive(Clone)] diff --git a/zkevm-circuits/src/evm_circuit/util/math_gadget/rlp.rs b/zkevm-circuits/src/evm_circuit/util/math_gadget/rlp.rs index d6f1f65377..74f485d591 100644 --- a/zkevm-circuits/src/evm_circuit/util/math_gadget/rlp.rs +++ b/zkevm-circuits/src/evm_circuit/util/math_gadget/rlp.rs @@ -1,6 +1,7 @@ use crate::util::Field; -use eth_types::{Address, ToLittleEndian, ToScalar, Word}; +use eth_types::{Address, ToLittleEndian, Word}; use gadgets::util::{and, expr_from_bytes, not, select, sum, Expr}; +use gadgets::ToScalar; use halo2_proofs::{ circuit::Value, plonk::{Error, Expression}, diff --git a/zkevm-circuits/src/exp_circuit.rs b/zkevm-circuits/src/exp_circuit.rs index 938a3e6311..33a285e840 100644 --- a/zkevm-circuits/src/exp_circuit.rs +++ b/zkevm-circuits/src/exp_circuit.rs @@ -15,7 +15,8 @@ use crate::{ witness, }; use bus_mapping::circuit_input_builder::{ExpEvent, ExpStep}; -use eth_types::{ToScalar, U256}; +use eth_types::U256; +use gadgets::ToScalar; use gadgets::{ mul_add::{MulAddChip, MulAddConfig}, util::{and, not, Expr}, diff --git a/zkevm-circuits/src/keccak_circuit/util.rs b/zkevm-circuits/src/keccak_circuit/util.rs index 3180e4bb9f..57ac7a64b3 100644 --- a/zkevm-circuits/src/keccak_circuit/util.rs +++ b/zkevm-circuits/src/keccak_circuit/util.rs @@ -2,7 +2,8 @@ use super::{keccak_packed_multi::keccak_unusable_rows, param::*}; use crate::util::Field; -use eth_types::{ToScalar, Word}; +use eth_types::Word; +use gadgets::ToScalar; use halo2_proofs::circuit::Value; use std::env::var; diff --git a/zkevm-circuits/src/table.rs b/zkevm-circuits/src/table.rs index 4aeec25c2e..637eb50d11 100644 --- a/zkevm-circuits/src/table.rs +++ b/zkevm-circuits/src/table.rs @@ -22,8 +22,9 @@ use bus_mapping::{ precompile::PrecompileCalls, }; use core::iter::once; -use eth_types::{sign_types::SignData, ToLittleEndian, ToScalar, ToWord, Word, H256, U256}; +use eth_types::{sign_types::SignData, ToLittleEndian, ToWord, Word, H256, U256}; use ethers_core::utils::keccak256; +use gadgets::ToScalar; use gadgets::{ binary_number::{BinaryNumberChip, BinaryNumberConfig}, util::{and, not, pow_of_two, split_u256, split_u256_limb64, Expr}, diff --git a/zkevm-circuits/src/tx_circuit.rs b/zkevm-circuits/src/tx_circuit.rs index 613bf7ae08..7b40d9c3e6 100644 --- a/zkevm-circuits/src/tx_circuit.rs +++ b/zkevm-circuits/src/tx_circuit.rs @@ -51,9 +51,10 @@ use eth_types::{ TxType::{Eip155, Eip1559, Eip2930, L1Msg, PreEip155}, }, sign_types::SignData, - AccessList, Address, ToAddress, ToBigEndian, ToScalar, + AccessList, Address, ToAddress, ToBigEndian, }; use ethers_core::utils::keccak256; +use gadgets::ToScalar; use gadgets::{ binary_number::{BinaryNumberChip, BinaryNumberConfig}, comparator::{ComparatorChip, ComparatorConfig, ComparatorInstruction}, diff --git a/zkevm-circuits/src/util.rs b/zkevm-circuits/src/util.rs index 627c93dbce..29c4e6e317 100644 --- a/zkevm-circuits/src/util.rs +++ b/zkevm-circuits/src/util.rs @@ -22,7 +22,7 @@ pub use gadgets::util::Expr; pub mod is_zero; /// The field used in circuits. We only support bn254fr now. -pub trait Field = eth_types::Field + halo2_base::utils::ScalarField; +pub trait Field = gadgets::Field + halo2_base::utils::ScalarField; pub(crate) fn query_expression( meta: &mut ConstraintSystem, diff --git a/zkevm-circuits/src/witness/block.rs b/zkevm-circuits/src/witness/block.rs index 11a8e53f42..b48b51be15 100644 --- a/zkevm-circuits/src/witness/block.rs +++ b/zkevm-circuits/src/witness/block.rs @@ -1,4 +1,5 @@ use ethers_core::types::Signature; +use gadgets::ToScalar; use std::collections::{BTreeMap, HashMap}; #[cfg(any(feature = "test", test))] @@ -17,9 +18,7 @@ use bus_mapping::{ }, Error, }; -use eth_types::{ - sign_types::SignData, Address, ToBigEndian, ToLittleEndian, ToScalar, Word, H256, U256, -}; +use eth_types::{sign_types::SignData, Address, ToBigEndian, ToLittleEndian, Word, H256, U256}; use halo2_proofs::{circuit::Value, halo2curves::bn256::Fr}; use itertools::Itertools; diff --git a/zkevm-circuits/src/witness/mpt.rs b/zkevm-circuits/src/witness/mpt.rs index 8c6a3340dc..ef81b61c81 100644 --- a/zkevm-circuits/src/witness/mpt.rs +++ b/zkevm-circuits/src/witness/mpt.rs @@ -3,7 +3,8 @@ use crate::{ table::AccountFieldTag, util::Field, }; -use eth_types::{Address, ToLittleEndian, ToScalar, Word, U256}; +use eth_types::{Address, ToLittleEndian, Word, U256}; +use gadgets::ToScalar; use halo2_proofs::circuit::Value; use itertools::Itertools; use mpt_circuits::{serde::SMTTrace, MPTProofType}; diff --git a/zkevm-circuits/src/witness/rw.rs b/zkevm-circuits/src/witness/rw.rs index 48a44c6b68..dd510de77b 100644 --- a/zkevm-circuits/src/witness/rw.rs +++ b/zkevm-circuits/src/witness/rw.rs @@ -6,7 +6,8 @@ use bus_mapping::{ operation::{self, AccountField, CallContextField, TxLogField, TxReceiptField}, Error, }; -use eth_types::{Address, ToLittleEndian, ToScalar, Word, U256}; +use eth_types::{Address, ToLittleEndian, Word, U256}; +use gadgets::ToScalar; use halo2_proofs::{circuit::Value, halo2curves::bn256::Fr}; use itertools::Itertools; diff --git a/zkevm-circuits/src/witness/tx.rs b/zkevm-circuits/src/witness/tx.rs index 1d8dd1fd71..c544c325a4 100644 --- a/zkevm-circuits/src/witness/tx.rs +++ b/zkevm-circuits/src/witness/tx.rs @@ -22,13 +22,13 @@ use eth_types::{ sign_types::{ biguint_to_32bytes_le, ct_option_ok_or, get_dummy_tx, recover_pk2, SignData, SECP256K1_Q, }, - AccessList, Address, Error, Signature, ToBigEndian, ToLittleEndian, ToScalar, ToWord, Word, - H256, + AccessList, Address, Error, Signature, ToBigEndian, ToLittleEndian, ToWord, Word, H256, }; use ethers_core::{ types::TransactionRequest, utils::{keccak256, rlp::Encodable}, }; +use gadgets::ToScalar; use halo2_proofs::{ circuit::Value, halo2curves::{group::ff::PrimeField, secp256k1}, @@ -1319,12 +1319,13 @@ pub(super) fn tx_convert( mod tests { use crate::witness::{tx::Challenges, RlpTag, Tag, Transaction}; use eth_types::{ - evm_types::gas_utils::tx_data_gas_cost, geth_types::TxType, Address, ToBigEndian, ToScalar, + evm_types::gas_utils::tx_data_gas_cost, geth_types::TxType, Address, ToBigEndian, }; use ethers_core::{ types::{Transaction as EthTransaction, TransactionRequest}, utils::rlp::{Decodable, Rlp}, }; + use gadgets::ToScalar; use halo2_proofs::{circuit::Value, dev::unwrap_value, halo2curves::bn256::Fr}; fn rlc(be_bytes: &[u8], rand: Fr) -> Fr {