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

Commit be9fed5

Browse files
### Description to address discussion in PR privacy-scaling-explorations#1748 ### Rationale implement OpsIdentity for word<T> so that we no longer need to type hint `::<Expression<F>>` or `::<F>` every time.
1 parent 81e715a commit be9fed5

File tree

18 files changed

+107
-59
lines changed

18 files changed

+107
-59
lines changed

eth-types/src/lib.rs

Lines changed: 56 additions & 4 deletions
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/evm_circuit/execution/begin_tx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::{
2727
},
2828
};
2929
use bus_mapping::state_db::CodeDB;
30-
use eth_types::{evm_types::PRECOMPILE_COUNT, keccak256, Field, ToWord, U256};
30+
use eth_types::{evm_types::PRECOMPILE_COUNT, keccak256, Field, OpsIdentity, ToWord, U256};
3131
use halo2_proofs::{
3232
circuit::Value,
3333
plonk::{Error, Expression},

zkevm-circuits/src/evm_circuit/execution/callop.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ use bus_mapping::{
3131
evm::OpcodeId,
3232
precompile::{is_precompiled, PrecompileCalls},
3333
};
34-
use eth_types::{evm_types::GAS_STIPEND_CALL_WITH_VALUE, Field, ToAddress, ToScalar, U256};
34+
use eth_types::{
35+
evm_types::GAS_STIPEND_CALL_WITH_VALUE, Field, OpsIdentity, ToAddress, ToScalar, U256,
36+
};
3537
use halo2_proofs::{circuit::Value, plonk::Error};
3638
use std::cmp::min;
3739

zkevm-circuits/src/evm_circuit/execution/create.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use bus_mapping::{
3434
};
3535
use eth_types::{
3636
evm_types::{GasCost, INIT_CODE_WORD_GAS},
37-
Field, ToBigEndian, ToScalar, ToWord, U256,
37+
Field, OpsIdentity, ToBigEndian, ToScalar, ToWord, U256,
3838
};
3939
use ethers_core::utils::keccak256;
4040
use gadgets::util::{and, select};

zkevm-circuits/src/evm_circuit/execution/end_block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414
table::{CallContextFieldTag, TxContextFieldTag},
1515
util::{word::Word, Expr},
1616
};
17-
use eth_types::Field;
17+
use eth_types::{Field, OpsIdentity};
1818
use gadgets::util::select;
1919
use halo2_proofs::{circuit::Value, plonk::Error};
2020

zkevm-circuits/src/evm_circuit/execution/error_precompile_failed.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::{
1717
witness::{Block, Call, ExecStep, Transaction},
1818
};
1919
use bus_mapping::evm::OpcodeId;
20-
use eth_types::{Field, U256};
20+
use eth_types::{Field, OpsIdentity, U256};
2121
use halo2_proofs::{circuit::Value, plonk::Error};
2222

2323
#[derive(Clone, Debug)]

zkevm-circuits/src/evm_circuit/execution/error_write_protection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{
1616
Expr,
1717
},
1818
};
19-
use eth_types::{evm_types::OpcodeId, Field, ToAddress, U256};
19+
use eth_types::{evm_types::OpcodeId, Field, OpsIdentity, ToAddress, U256};
2020
use halo2_proofs::{circuit::Value, plonk::Error};
2121

2222
#[derive(Clone, Debug)]

zkevm-circuits/src/evm_circuit/execution/stop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::{
2121
},
2222
};
2323
use bus_mapping::evm::OpcodeId;
24-
use eth_types::Field;
24+
use eth_types::{Field, OpsIdentity};
2525
use halo2_proofs::{circuit::Value, plonk::Error};
2626

2727
#[derive(Clone, Debug)]

zkevm-circuits/src/evm_circuit/util/common_gadget.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ use crate::{
2828
witness::{Block, Call, ExecStep},
2929
};
3030
use bus_mapping::state_db::CodeDB;
31-
use eth_types::{evm_types::GasCost, Field, ToAddress, ToLittleEndian, ToScalar, ToWord, U256};
31+
use eth_types::{
32+
evm_types::GasCost, Field, OpsIdentity, ToAddress, ToLittleEndian, ToScalar, ToWord, U256,
33+
};
3234
use gadgets::util::{select, sum};
3335
use halo2_proofs::{
3436
circuit::Value,

zkevm-circuits/src/evm_circuit/util/constraint_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::{
1818
use bus_mapping::{
1919
circuit_input_builder::FeatureConfig, operation::Target, state_db::EMPTY_CODE_HASH_LE,
2020
};
21-
use eth_types::Field;
21+
use eth_types::{Field, OpsIdentity};
2222
use gadgets::util::{not, sum};
2323
use halo2_proofs::{
2424
circuit::Value,

0 commit comments

Comments
 (0)