Skip to content
This repository was archived by the owner on Jul 5, 2024. It is now read-only.

Commit 5683ec9

Browse files
add OpsIdentity trait
1 parent 81e715a commit 5683ec9

File tree

2 files changed

+68
-24
lines changed

2 files changed

+68
-24
lines changed

eth-types/src/lib.rs

+56-4
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ pub use keccak::{keccak256, Keccak};
2424

2525
pub use bytecode::Bytecode;
2626
pub use error::Error;
27-
use halo2_proofs::halo2curves::{
28-
bn256::{Fq, Fr},
29-
ff::{Field as Halo2Field, FromUniformBytes, PrimeField},
27+
use halo2_proofs::{
28+
halo2curves::{
29+
bn256::{Fq, Fr},
30+
ff::{Field as Halo2Field, FromUniformBytes, PrimeField},
31+
},
32+
plonk::Expression,
3033
};
3134

3235
use crate::evm_types::{memory::Memory, stack::Stack, storage::Storage, OpcodeId};
@@ -42,9 +45,58 @@ pub use ethers_core::{
4245
use serde::{de, Deserialize, Serialize};
4346
use std::{collections::HashMap, fmt, str::FromStr};
4447

48+
/// trait to retrieve general operation itentity element
49+
pub trait OpsIdentity {
50+
/// output type
51+
type Output;
52+
/// additive identity
53+
fn zero() -> Self::Output;
54+
/// multiplicative identity
55+
fn one() -> Self::Output;
56+
}
57+
58+
impl<F: Field> OpsIdentity for Expression<F> {
59+
type Output = Expression<F>;
60+
fn zero() -> Self::Output {
61+
Expression::Constant(F::ZERO)
62+
}
63+
64+
fn one() -> Self::Output {
65+
Expression::Constant(F::ONE)
66+
}
67+
}
68+
69+
// Impl OpsIdentity for Fr
70+
impl OpsIdentity for Fr {
71+
type Output = Fr;
72+
73+
fn zero() -> Self::Output {
74+
Fr::zero()
75+
}
76+
77+
fn one() -> Self::Output {
78+
Fr::one()
79+
}
80+
}
81+
82+
// Impl OpsIdentity for Fq
83+
impl OpsIdentity for Fq {
84+
type Output = Fq;
85+
86+
fn zero() -> Self::Output {
87+
Fq::zero()
88+
}
89+
90+
fn one() -> Self::Output {
91+
Fq::one()
92+
}
93+
}
94+
4595
/// Trait used to reduce verbosity with the declaration of the [`PrimeField`]
4696
/// trait and its repr.
47-
pub trait Field: Halo2Field + PrimeField<Repr = [u8; 32]> + FromUniformBytes<64> + Ord {
97+
pub trait Field:
98+
Halo2Field + PrimeField<Repr = [u8; 32]> + FromUniformBytes<64> + Ord + OpsIdentity<Output = Self>
99+
{
48100
/// Gets the lower 128 bits of this field element when expressed
49101
/// canonically.
50102
fn get_lower_128(&self) -> u128 {

zkevm-circuits/src/util/word.rs

+12-20
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// limb is 256/4 = 64 bits
55

66
use bus_mapping::state_db::CodeDB;
7-
use eth_types::{Field, ToLittleEndian, H160, H256};
7+
use eth_types::{Field, OpsIdentity, ToLittleEndian, H160, H256};
88
use gadgets::util::{not, or, Expr};
99
use halo2_proofs::{
1010
circuit::{AssignedCell, Region, Value},
@@ -267,6 +267,17 @@ impl<F: Field> From<eth_types::Word> for Word<F> {
267267
}
268268
}
269269

270+
impl<T: Clone + OpsIdentity<Output = T>> OpsIdentity for Word<T> {
271+
/// output type
272+
type Output = Word<T>;
273+
fn zero() -> Self::Output {
274+
Word::new([T::zero(), T::zero()])
275+
}
276+
fn one() -> Self::Output {
277+
Word::new([T::one(), T::zero()])
278+
}
279+
}
280+
270281
impl<F: Field> From<H256> for Word<F> {
271282
/// Construct the word from H256
272283
fn from(h: H256) -> Self {
@@ -354,16 +365,6 @@ impl<F: Field, T: Expr<F> + Clone> WordExpr<F> for Word<T> {
354365
}
355366

356367
impl<F: Field> Word<F> {
357-
/// zero word
358-
pub fn zero_f() -> Self {
359-
Self::new([F::ZERO, F::ZERO])
360-
}
361-
362-
/// one word
363-
pub fn one_f() -> Self {
364-
Self::new([F::ONE, F::ZERO])
365-
}
366-
367368
/// Convert address (h160) to single field element.
368369
/// This method is Address specific
369370
pub fn compress_f(&self) -> F {
@@ -376,15 +377,6 @@ impl<F: Field> Word<Expression<F>> {
376377
pub fn from_lo_unchecked(lo: Expression<F>) -> Self {
377378
Self::new([lo, 0.expr()])
378379
}
379-
/// zero word
380-
pub fn zero() -> Self {
381-
Self::new([0.expr(), 0.expr()])
382-
}
383-
384-
/// one word
385-
pub fn one() -> Self {
386-
Self::new([1.expr(), 0.expr()])
387-
}
388380

389381
/// select based on selector. Here assume selector is 1/0 therefore no overflow check
390382
pub fn select<T: Expr<F> + Clone>(

0 commit comments

Comments
 (0)