Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

Commit d46f7d7

Browse files
authored
move Field and ToScalar to gadgets (#1337)
1 parent 671ef55 commit d46f7d7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+223
-181
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

aggregator/src/aggregation/decoder/tables/fixed.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use eth_types::Field;
21
use gadgets::impl_expr;
2+
use gadgets::Field;
33
use halo2_proofs::{
44
circuit::{Layouter, Value},
55
halo2curves::bn256::Fr,

aggregator/src/aggregation/decoder/tables/literals_header.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use eth_types::Field;
21
use gadgets::util::{and, not, select, Expr};
2+
use gadgets::Field;
33
use halo2_proofs::{
44
circuit::{Layouter, Value},
55
halo2curves::bn256::Fr,

aggregator/src/aggregation/decoder/witgen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use eth_types::Field;
1+
use gadgets::Field;
22
use halo2_proofs::circuit::Value;
33
use revm_precompile::HashMap;
44

aggregator/src/aggregation/decoder/witgen/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::{collections::BTreeMap, io::Cursor};
22

33
use bitstream_io::{BitRead, BitReader, LittleEndian};
4-
use eth_types::Field;
54
use gadgets::impl_expr;
5+
use gadgets::Field;
66
use halo2_proofs::{circuit::Value, plonk::Expression};
77
use itertools::Itertools;
88
use std::collections::HashMap;

aggregator/src/batch.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
//! This module implements related functions that aggregates public inputs of many chunks into a
22
//! single one.
33
4-
use eth_types::{Field, ToBigEndian, H256};
4+
use eth_types::{ToBigEndian, H256};
55
use ethers_core::utils::keccak256;
6+
use gadgets::Field;
67

78
use crate::{
89
blob::{BatchData, PointEvaluationAssignments},

aggregator/src/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use eth_types::Field;
1+
use gadgets::Field;
22
use halo2_proofs::{circuit::AssignedCell, halo2curves::bn256::Fr, plonk::Error};
33

44
#[cfg(test)]

bus-mapping/src/circuit_input_builder/execution.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{
1515
use eth_types::{
1616
evm_types::{memory::MemoryWordRange, Gas, GasCost, MemoryAddress, OpcodeId, ProgramCounter},
1717
sign_types::SignData,
18-
Address, Field, GethExecStep, ToLittleEndian, Word, H256, U256,
18+
Address, GethExecStep, ToLittleEndian, Word, H256, U256,
1919
};
2020
use ethers_core::k256::elliptic_curve::subtle::CtOption;
2121
use gadgets::impl_expr;

eth-types/src/lib.rs

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ pub use ethers_core::{
4949
Address, Block, Bytes, Signature, H160, H256, H64, U256, U64,
5050
},
5151
};
52-
use halo2curves::{bn256::Fr, group::ff::PrimeField};
5352
use serde::{de, Deserialize, Deserializer, Serialize};
5453
use std::{
5554
collections::{HashMap, HashSet},
@@ -92,41 +91,6 @@ pub mod base64 {
9291
}
9392
}
9493

95-
/// Trait used to reduce verbosity with the declaration of the [`Field`]
96-
/// trait and its repr.
97-
pub trait Field:
98-
PrimeField<Repr = [u8; 32]> + poseidon_base::hash::Hashable + std::convert::From<Fr>
99-
{
100-
/// Re-expose zero element as a function
101-
fn zero() -> Self {
102-
Self::ZERO
103-
}
104-
105-
/// Re-expose one element as a function
106-
fn one() -> Self {
107-
Self::ONE
108-
}
109-
110-
/// Expose the lower 128 bits
111-
fn get_lower_128(&self) -> u128 {
112-
u128::from_le_bytes(self.to_repr().as_ref()[..16].try_into().unwrap())
113-
}
114-
}
115-
116-
// Impl custom `Field` trait for BN256 Fr to be used and consistent with the
117-
// rest of the workspace.
118-
impl Field for Fr {}
119-
120-
// Impl custom `Field` trait for BN256 Fq to be used and consistent with the
121-
// rest of the workspace.
122-
// impl Field for Fq {}
123-
124-
/// Trait used to define types that can be converted to a 256 bit scalar value.
125-
pub trait ToScalar<F> {
126-
/// Convert the type to a scalar value.
127-
fn to_scalar(&self) -> Option<F>;
128-
}
129-
13094
/// Trait used to convert a type to a [`Word`].
13195
pub trait ToWord {
13296
/// Convert the type to a [`Word`].
@@ -179,14 +143,6 @@ impl<'de> Deserialize<'de> for DebugU256 {
179143
}
180144
}
181145

182-
impl<F: Field> ToScalar<F> for DebugU256 {
183-
fn to_scalar(&self) -> Option<F> {
184-
let mut bytes = [0u8; 32];
185-
self.to_little_endian(&mut bytes);
186-
F::from_repr(bytes).into()
187-
}
188-
}
189-
190146
impl ToBigEndian for DebugU256 {
191147
/// Encode the value as byte array in big endian.
192148
fn to_be_bytes(&self) -> [u8; 32] {
@@ -243,14 +199,6 @@ impl ToU16LittleEndian for U256 {
243199
}
244200
}
245201

246-
impl<F: Field> ToScalar<F> for U256 {
247-
fn to_scalar(&self) -> Option<F> {
248-
let mut bytes = [0u8; 32];
249-
self.to_little_endian(&mut bytes);
250-
F::from_repr(bytes).into()
251-
}
252-
}
253-
254202
impl ToAddress for U256 {
255203
fn to_address(&self) -> Address {
256204
Address::from_slice(&self.to_be_bytes()[12..])
@@ -321,33 +269,6 @@ impl ToWord for Word {
321269
}
322270
}
323271

324-
impl<F: Field> ToScalar<F> for Address {
325-
fn to_scalar(&self) -> Option<F> {
326-
let mut bytes = [0u8; 32];
327-
bytes[32 - Self::len_bytes()..].copy_from_slice(self.as_bytes());
328-
bytes.reverse();
329-
F::from_repr(bytes).into()
330-
}
331-
}
332-
333-
impl<F: Field> ToScalar<F> for bool {
334-
fn to_scalar(&self) -> Option<F> {
335-
self.to_word().to_scalar()
336-
}
337-
}
338-
339-
impl<F: Field> ToScalar<F> for u64 {
340-
fn to_scalar(&self) -> Option<F> {
341-
Some(F::from(*self))
342-
}
343-
}
344-
345-
impl<F: Field> ToScalar<F> for usize {
346-
fn to_scalar(&self) -> Option<F> {
347-
u64::try_from(*self).ok().map(F::from)
348-
}
349-
}
350-
351272
/// Code hash related
352273
/// the empty keccak code hash
353274
pub static KECCAK_CODE_HASH_EMPTY: LazyLock<Hash> = LazyLock::new(|| {

gadgets/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ halo2_proofs.workspace = true
99
sha3.workspace = true
1010
eth-types = { path = "../eth-types" }
1111
strum.workspace = true
12+
poseidon-base.workspace = true
1213

1314
[dev-dependencies]
1415
rand_xorshift.workspace = true

0 commit comments

Comments
 (0)