Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion aggregator/src/aggregation/decoder/tables/fixed.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use eth_types::Field;
use gadgets::impl_expr;
use gadgets::Field;
use halo2_proofs::{
circuit::{Layouter, Value},
halo2curves::bn256::Fr,
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion aggregator/src/aggregation/decoder/witgen.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use eth_types::Field;
use gadgets::Field;
use halo2_proofs::circuit::Value;
use revm_precompile::HashMap;

Expand Down
2 changes: 1 addition & 1 deletion aggregator/src/aggregation/decoder/witgen/types.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
3 changes: 2 additions & 1 deletion aggregator/src/batch.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down
2 changes: 1 addition & 1 deletion aggregator/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use eth_types::Field;
use gadgets::Field;
use halo2_proofs::{circuit::AssignedCell, halo2curves::bn256::Fr, plonk::Error};

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion bus-mapping/src/circuit_input_builder/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
79 changes: 0 additions & 79 deletions eth-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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<Repr = [u8; 32]> + poseidon_base::hash::Hashable + std::convert::From<Fr>
{
/// 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<F> {
/// Convert the type to a scalar value.
fn to_scalar(&self) -> Option<F>;
}

/// Trait used to convert a type to a [`Word`].
pub trait ToWord {
/// Convert the type to a [`Word`].
Expand Down Expand Up @@ -179,14 +143,6 @@ impl<'de> Deserialize<'de> for DebugU256 {
}
}

impl<F: Field> ToScalar<F> for DebugU256 {
fn to_scalar(&self) -> Option<F> {
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] {
Expand Down Expand Up @@ -243,14 +199,6 @@ impl ToU16LittleEndian for U256 {
}
}

impl<F: Field> ToScalar<F> for U256 {
fn to_scalar(&self) -> Option<F> {
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..])
Expand Down Expand Up @@ -321,33 +269,6 @@ impl ToWord for Word {
}
}

impl<F: Field> ToScalar<F> for Address {
fn to_scalar(&self) -> Option<F> {
let mut bytes = [0u8; 32];
bytes[32 - Self::len_bytes()..].copy_from_slice(self.as_bytes());
bytes.reverse();
F::from_repr(bytes).into()
}
}

impl<F: Field> ToScalar<F> for bool {
fn to_scalar(&self) -> Option<F> {
self.to_word().to_scalar()
}
}

impl<F: Field> ToScalar<F> for u64 {
fn to_scalar(&self) -> Option<F> {
Some(F::from(*self))
}
}

impl<F: Field> ToScalar<F> for usize {
fn to_scalar(&self) -> Option<F> {
u64::try_from(*self).ok().map(F::from)
}
}

/// Code hash related
/// the empty keccak code hash
pub static KECCAK_CODE_HASH_EMPTY: LazyLock<Hash> = LazyLock::new(|| {
Expand Down
1 change: 1 addition & 0 deletions gadgets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion gadgets/src/batched_is_zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
2 changes: 1 addition & 1 deletion gadgets/src/binary_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
2 changes: 1 addition & 1 deletion gadgets/src/comparator.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down
2 changes: 1 addition & 1 deletion gadgets/src/evm_word.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
4 changes: 2 additions & 2 deletions gadgets/src/is_equal.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -127,7 +127,7 @@ impl<F: Field> Chip<F> for IsEqualChip<F> {
mod tests {
use std::marker::PhantomData;

use eth_types::Field;
use crate::Field;
use halo2_proofs::{
circuit::{Layouter, SimpleFloorPlanner, Value},
dev::MockProver,
Expand Down
4 changes: 2 additions & 2 deletions gadgets/src/is_zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -157,7 +157,7 @@ impl<F: Field> Chip<F> for IsZeroChip<F> {
mod test {
use super::{IsZeroChip, IsZeroConfig, IsZeroInstruction};

use eth_types::Field;
use crate::Field;
use halo2_proofs::{
circuit::{Layouter, SimpleFloorPlanner, Value},
dev::MockProver,
Expand Down
4 changes: 2 additions & 2 deletions gadgets/src/less_than.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -188,7 +188,7 @@ impl<F: Field, const N_BYTES: usize> Chip<F> for LtChip<F, N_BYTES> {
#[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,
Expand Down
80 changes: 79 additions & 1 deletion gadgets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Repr = [u8; 32]> + poseidon_base::hash::Hashable + std::convert::From<Fr>
{
/// 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<F> {
/// Convert the type to a scalar value.
fn to_scalar(&self) -> Option<F>;
}

impl<F: Field> ToScalar<F> for DebugU256 {
fn to_scalar(&self) -> Option<F> {
let mut bytes = [0u8; 32];
self.to_little_endian(&mut bytes);
F::from_repr(bytes).into()
}
}

impl<F: Field> ToScalar<F> for U256 {
fn to_scalar(&self) -> Option<F> {
let mut bytes = [0u8; 32];
self.to_little_endian(&mut bytes);
F::from_repr(bytes).into()
}
}

impl<F: Field> ToScalar<F> for Address {
fn to_scalar(&self) -> Option<F> {
let mut bytes = [0u8; 32];
bytes[32 - Self::len_bytes()..].copy_from_slice(self.as_bytes());
bytes.reverse();
F::from_repr(bytes).into()
}
}

impl<F: Field> ToScalar<F> for bool {
fn to_scalar(&self) -> Option<F> {
self.to_word().to_scalar()
}
}

impl<F: Field> ToScalar<F> for u64 {
fn to_scalar(&self) -> Option<F> {
Some(F::from(*self))
}
}

impl<F: Field> ToScalar<F> for usize {
fn to_scalar(&self) -> Option<F> {
u64::try_from(*self).ok().map(F::from)
}
}

#[allow(dead_code)]
/// An assigned cell in the circuit.
#[derive(Clone, Debug)]
Expand Down
4 changes: 2 additions & 2 deletions gadgets/src/monotone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -108,7 +108,7 @@ impl<F: Field, const RANGE: usize, const INCR: bool, const STRICT: bool> Chip<F>
#[cfg(test)]
mod test {
use super::{MonotoneChip, MonotoneConfig, Value};
use eth_types::Field;
use crate::Field;
use halo2_proofs::{
circuit::{Layouter, SimpleFloorPlanner},
dev::{
Expand Down
Loading