Skip to content

Commit 90ccb52

Browse files
committed
fix precompile
1 parent e61c7d6 commit 90ccb52

File tree

18 files changed

+174
-678
lines changed

18 files changed

+174
-678
lines changed

Cargo.lock

Lines changed: 0 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,19 @@
11
use crate::{EvmDatabase, VerificationError};
22
use sbv_kv::KeyValueStoreGet;
3-
use sbv_precompile::PrecompileProvider;
43
use sbv_primitives::{
54
B256, Bytes,
65
chainspec::ChainSpec,
76
types::{
8-
evm::precompiles::PrecompilesMap,
97
reth::{
10-
evm::{
11-
ConfigureEvm, Database, EthEvm, EthEvmConfig, EvmEnv, EvmFactory,
12-
eth::EthEvmContext,
13-
execute::Executor,
14-
revm::{
15-
Context, Inspector, MainBuilder, MainContext,
16-
context::{
17-
BlockEnv, CfgEnv, TxEnv,
18-
result::{EVMError, HaltReason},
19-
},
20-
inspector::NoOpInspector,
21-
},
22-
},
8+
evm::{ConfigureEvm, EthEvmConfig, execute::Executor},
239
execution_types::BlockExecutionOutput,
2410
primitives::{Block, Receipt, RecoveredBlock},
2511
},
26-
revm::{SpecId, database::CacheDB, precompile::PrecompileSpecId},
12+
revm::database::CacheDB,
2713
},
2814
};
2915
use std::sync::Arc;
3016

31-
/// Ethereum-related EVM configuration with [`SbvEthEvmFactory`] as the factory.
32-
pub type EvmConfig = EthEvmConfig<ChainSpec, SbvEthEvmFactory>;
33-
3417
/// EVM executor that handles the block.
3518
#[derive(Debug)]
3619
pub struct EvmExecutor<'a, CodeDb, BlockHashProvider> {
@@ -59,7 +42,7 @@ impl<CodeDb: KeyValueStoreGet<B256, Bytes>, BlockHashProvider: KeyValueStoreGet<
5942
{
6043
/// Handle the block with the given witness
6144
pub fn execute(self) -> Result<BlockExecutionOutput<Receipt>, VerificationError> {
62-
let provider = EvmConfig::new_with_evm_factory(self.chain_spec.clone(), SbvEthEvmFactory);
45+
let provider = EthEvmConfig::new(self.chain_spec.clone());
6346

6447
let output = cycle_track!(
6548
provider.executor(CacheDB::new(self.db)).execute(self.block),
@@ -69,53 +52,3 @@ impl<CodeDb: KeyValueStoreGet<B256, Bytes>, BlockHashProvider: KeyValueStoreGet<
6952
Ok(output)
7053
}
7154
}
72-
73-
/// Factory producing [`EthEvm`].
74-
#[derive(Debug, Default, Clone, Copy)]
75-
#[non_exhaustive]
76-
pub struct SbvEthEvmFactory;
77-
78-
impl EvmFactory for SbvEthEvmFactory {
79-
type Evm<DB: Database, I: Inspector<EthEvmContext<DB>>> = EthEvm<DB, I, Self::Precompiles>;
80-
type Context<DB: Database> = Context<BlockEnv, TxEnv, CfgEnv, DB>;
81-
type Tx = TxEnv;
82-
type Error<DBError: core::error::Error + Send + Sync + 'static> = EVMError<DBError>;
83-
type HaltReason = HaltReason;
84-
type Spec = SpecId;
85-
type Precompiles = PrecompilesMap;
86-
87-
fn create_evm<DB: Database>(&self, db: DB, input: EvmEnv) -> Self::Evm<DB, NoOpInspector> {
88-
let spec_id = input.cfg_env.spec;
89-
EthEvm::new(
90-
Context::mainnet()
91-
.with_block(input.block_env)
92-
.with_cfg(input.cfg_env)
93-
.with_db(db)
94-
.build_mainnet_with_inspector(NoOpInspector {})
95-
.with_precompiles(PrecompileProvider::with_spec(
96-
PrecompileSpecId::from_spec_id(spec_id),
97-
)),
98-
false,
99-
)
100-
}
101-
102-
fn create_evm_with_inspector<DB: Database, I: Inspector<Self::Context<DB>>>(
103-
&self,
104-
db: DB,
105-
input: EvmEnv,
106-
inspector: I,
107-
) -> Self::Evm<DB, I> {
108-
let spec_id = input.cfg_env.spec;
109-
EthEvm::new(
110-
Context::mainnet()
111-
.with_block(input.block_env)
112-
.with_cfg(input.cfg_env)
113-
.with_db(db)
114-
.build_mainnet_with_inspector(inspector)
115-
.with_precompiles(PrecompileProvider::with_spec(
116-
PrecompileSpecId::from_spec_id(spec_id),
117-
)),
118-
true,
119-
)
120-
}
121-
}

crates/core/src/executor/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#[cfg(not(feature = "scroll"))]
22
mod ethereum;
33
#[cfg(not(feature = "scroll"))]
4-
pub use ethereum::{EvmConfig, EvmExecutor, SbvEthEvmFactory};
4+
pub use ethereum::EvmExecutor;
55

66
#[cfg(feature = "scroll")]
77
mod scroll;
88
#[cfg(feature = "scroll")]
9-
pub use scroll::{EvmConfig, EvmExecutor};
9+
pub use scroll::EvmExecutor;

crates/core/src/executor/scroll.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,15 @@ use sbv_primitives::{
55
chainspec::ChainSpec,
66
types::{
77
reth::{
8-
evm::{ConfigureEvm, EthEvmConfig, RethReceiptBuilder},
8+
evm::{ConfigureEvm, EthEvmConfig},
99
execution_types::BlockExecutionOutput,
10-
primitives::{Block, EthPrimitives, Receipt, RecoveredBlock},
10+
primitives::{Block, Receipt, RecoveredBlock},
1111
},
1212
revm::database::CacheDB,
1313
},
1414
};
1515
use std::sync::Arc;
1616

17-
/// Ethereum-related EVM configuration.
18-
pub type EvmConfig =
19-
EthEvmConfig<ChainSpec, EthPrimitives, RethReceiptBuilder, sbv_precompile::PrecompileProvider>;
20-
2117
/// EVM executor that handles the block.
2218
#[derive(Debug)]
2319
pub struct EvmExecutor<'a, CodeDb, BlockHashProvider, CompressionRatios> {
@@ -60,7 +56,7 @@ impl<
6056
revm::database::{State, states::bundle_state::BundleRetention},
6157
};
6258

63-
let provider = EvmConfig::new(self.chain_spec.clone(), Default::default());
59+
let provider = EthEvmConfig::scroll(self.chain_spec.clone());
6460
let factory = provider.block_executor_factory();
6561

6662
let mut db = State::builder()

crates/core/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ mod error;
1414
pub use error::VerificationError;
1515

1616
mod executor;
17-
#[cfg(not(feature = "scroll"))]
18-
pub use executor::SbvEthEvmFactory;
19-
pub use executor::{EvmConfig, EvmExecutor};
17+
pub use executor::EvmExecutor;
2018

2119
pub mod verifier;
2220

crates/precompile/Cargo.toml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ repository.workspace = true
1212
sbv-primitives = { workspace = true, features = ["evm-types", "revm-types"] }
1313

1414
openvm-ecc-guest = { workspace = true, optional = true }
15-
openvm-keccak256 = { workspace = true, optional = true }
1615
openvm-k256 = { workspace = true, optional = true }
1716
openvm-sha2 = { workspace = true, optional = true }
1817
openvm-pairing = { workspace = true, optional = true }
@@ -41,31 +40,22 @@ ethereum-openvm = [
4140
"openvm-sha256",
4241
]
4342

44-
bn128 = [] # marker, won't be used in dependencies
4543
openvm-bn128 = [
46-
"bn128",
4744
"dep:openvm-ecc-guest",
4845
"dep:openvm-pairing",
4946
"openvm-pairing/bn254",
5047
]
5148

52-
kzg = [] # marker, won't be used in dependencies
5349
openvm-kzg = [
54-
"kzg",
5550
"dep:openvm-kzg",
5651
]
5752

58-
secp256k1 = [] # marker, won't be used in dependencies
5953
openvm-secp256k1 = [
60-
"secp256k1",
6154
"dep:openvm-ecc-guest",
62-
"dep:openvm-keccak256",
6355
"dep:openvm-k256",
6456
]
6557

66-
sha256 = []
6758
openvm-sha256 = [
68-
"sha256",
6959
"dep:openvm-sha2"
7060
]
7161

crates/precompile/src/imps/bn128/openvm.rs renamed to crates/precompile/src/bn128.rs

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ const G1_LEN: usize = 2 * FQ_LEN;
3737
#[inline]
3838
fn read_fq(input: &[u8]) -> Result<Fp, PrecompileError> {
3939
if input.len() < FQ_LEN {
40-
Err(PrecompileError::Bn128FieldPointNotAMember)
40+
Err(PrecompileError::Bn254FieldPointNotAMember)
4141
} else {
42-
Fp::from_be_bytes(&input[..32]).ok_or(PrecompileError::Bn128FieldPointNotAMember)
42+
Fp::from_be_bytes(&input[..32]).ok_or(PrecompileError::Bn254FieldPointNotAMember)
4343
}
4444
}
4545

@@ -62,7 +62,7 @@ fn read_fq2(input: &[u8]) -> Result<Fp2, PrecompileError> {
6262

6363
#[inline]
6464
fn new_g1_affine_point(px: Fp, py: Fp) -> Result<G1Affine, PrecompileError> {
65-
G1Affine::from_xy(px, py).ok_or(PrecompileError::Bn128AffineGFailedToCreate)
65+
G1Affine::from_xy(px, py).ok_or(PrecompileError::Bn254AffineGFailedToCreate)
6666
}
6767

6868
/// Reads a G1 point from the input slice.
@@ -114,7 +114,7 @@ pub(super) fn read_g2_point(input: &[u8]) -> Result<G2Affine, PrecompileError> {
114114
let ba = read_fq2(&input[0..FQ2_LEN])?;
115115
let bb = read_fq2(&input[FQ2_LEN..2 * FQ2_LEN])?;
116116

117-
G2Affine::from_xy(ba, bb).ok_or(PrecompileError::Bn128AffineGFailedToCreate)
117+
G2Affine::from_xy(ba, bb).ok_or(PrecompileError::Bn254AffineGFailedToCreate)
118118
}
119119

120120
/// Reads a scalar from the input slice
@@ -153,22 +153,31 @@ pub(super) fn g1_point_mul(p: G1Affine, fr: Scalar) -> G1Affine {
153153
/// Note: If the input is empty, this function returns true.
154154
/// This is different to EIP2537 which disallows the empty input.
155155
#[inline]
156-
pub(super) fn pairing_check(pairs: &[(G1Affine, G2Affine)]) -> bool {
157-
if pairs.is_empty() {
158-
return true;
156+
pub(super) fn pairing_check(pairs: &[(&[u8], &[u8])]) -> Result<bool, PrecompileError> {
157+
let mut g1_points = Vec::with_capacity(pairs.len());
158+
let mut g2_points = Vec::with_capacity(pairs.len());
159+
160+
for (g1_bytes, g2_bytes) in pairs {
161+
let g1_is_zero = g1_bytes.iter().all(|i| *i == 0);
162+
let g2_is_zero = g2_bytes.iter().all(|i| *i == 0);
163+
164+
let g1 = read_g1_point(g1_bytes)?;
165+
let g2 = read_g2_point(g2_bytes)?;
166+
167+
let (g1x, g1y) = g1.into_coords();
168+
let (g2x, g2y) = g2.into_coords();
169+
170+
// Skip pairs where either point is at infinity
171+
if !g1_is_zero && !g2_is_zero {
172+
let g1 = AffinePoint::new(g1x, g1y);
173+
let g2 = AffinePoint::new(g2x, g2y);
174+
g1_points.push(g1);
175+
g2_points.push(g2);
176+
}
159177
}
160-
let (g1_points, g2_points): (Vec<_>, Vec<_>) = pairs
161-
.iter()
162-
.cloned()
163-
.map(|(g1, g2)| {
164-
let (g1_x, g1_y) = g1.into_coords();
165-
let g1 = AffinePoint::new(g1_x, g1_y);
166-
167-
let (g2_x, g2_y) = g2.into_coords();
168-
let g2 = AffinePoint::new(g2_x, g2_y);
169-
(g1, g2)
170-
})
171-
.unzip();
172-
173-
Bn254::pairing_check(&g1_points, &g2_points).is_ok()
178+
if g1_points.is_empty() {
179+
return Ok(true);
180+
}
181+
182+
Ok(Bn254::pairing_check(&g1_points, &g2_points).is_ok())
174183
}

crates/precompile/src/ethereum.rs

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)