Skip to content

Commit f2b634f

Browse files
clabbyrefcell
andauthored
feat(client): Re-accelerate precompiles (#866)
* feat(client): Re-accelerate precompiles * fix: lint --------- Co-authored-by: refcell <[email protected]>
1 parent cb39d1b commit f2b634f

File tree

8 files changed

+134
-189
lines changed

8 files changed

+134
-189
lines changed

Cargo.lock

+23-22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/client/src/kona.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use kona_preimage::{HintWriter, OracleReader};
1212
use kona_std_fpvm::{FileChannel, FileDescriptor};
1313
use kona_std_fpvm_proc::client_entry;
1414

15+
mod precompiles;
16+
1517
/// The global preimage oracle reader pipe.
1618
static ORACLE_READER_PIPE: FileChannel =
1719
FileChannel::new(FileDescriptor::PreimageRead, FileDescriptor::PreimageWrite);
@@ -37,5 +39,9 @@ fn main() -> Result<(), String> {
3739
.expect("Failed to set tracing subscriber");
3840
}
3941

40-
kona_proof::block_on(kona_client::run(ORACLE_READER, HINT_WRITER))
42+
kona_proof::block_on(kona_client::run(
43+
ORACLE_READER,
44+
HINT_WRITER,
45+
Some(precompiles::fpvm_handle_register),
46+
))
4147
}

bin/client/src/lib.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![warn(missing_debug_implementations, missing_docs, unreachable_pub, rustdoc::all)]
33
#![deny(unused_must_use, rust_2018_idioms)]
44
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
5+
#![allow(clippy::type_complexity)]
56
#![no_std]
67

78
extern crate alloc;
@@ -11,7 +12,7 @@ use alloy_consensus::{Header, Sealed};
1112
use alloy_primitives::B256;
1213
use core::fmt::Debug;
1314
use kona_driver::{Driver, DriverError};
14-
use kona_executor::{ExecutorError, TrieDBProvider};
15+
use kona_executor::{ExecutorError, KonaHandleRegister, TrieDBProvider};
1516
use kona_preimage::{
1617
CommsClient, HintWriterClient, PreimageKey, PreimageKeyType, PreimageOracleClient,
1718
};
@@ -26,12 +27,6 @@ use kona_proof::{
2627
use thiserror::Error;
2728
use tracing::{error, info, warn};
2829

29-
mod precompiles;
30-
pub use precompiles::{
31-
EcPairingAccelerated, EcPairingAcceleratedGranite, EcRecoverAccelerated,
32-
KZGPointEvalAccelerated, ECPAIRING_ADDRESS, ECRECOVER_ADDRESS, POINT_EVAL_ADDRESS,
33-
};
34-
3530
/// An error that can occur when running the fault proof program.
3631
#[derive(Error, Debug)]
3732
pub enum FaultProofProgramError {
@@ -48,7 +43,16 @@ pub enum FaultProofProgramError {
4843

4944
/// Executes the fault proof program with the given [PreimageOracleClient] and [HintWriterClient].
5045
#[inline]
51-
pub async fn run<P, H>(oracle_client: P, hint_client: H) -> Result<(), FaultProofProgramError>
46+
pub async fn run<P, H>(
47+
oracle_client: P,
48+
hint_client: H,
49+
handle_register: Option<
50+
KonaHandleRegister<
51+
OracleL2ChainProvider<CachingOracle<P, H>>,
52+
OracleL2ChainProvider<CachingOracle<P, H>>,
53+
>,
54+
>,
55+
) -> Result<(), FaultProofProgramError>
5256
where
5357
P: PreimageOracleClient + Send + Sync + Debug + Clone,
5458
H: HintWriterClient + Send + Sync + Debug + Clone,
@@ -112,7 +116,7 @@ where
112116
l1_provider.clone(),
113117
l2_provider.clone(),
114118
);
115-
let executor = KonaExecutor::new(&cfg, l2_provider.clone(), l2_provider, None, None);
119+
let executor = KonaExecutor::new(&cfg, l2_provider.clone(), l2_provider, handle_register, None);
116120
let mut driver = Driver::new(cursor, executor, pipeline);
117121

118122
// Run the derivation pipeline until we are able to produce the output root of the claimed
+18-71
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,32 @@
11
//! Contains the accelerated version of the `ecPairing` precompile.
22
3-
use alloc::{string::ToString, sync::Arc, vec::Vec};
3+
use crate::{HINT_WRITER, ORACLE_READER};
4+
use alloc::{string::ToString, vec::Vec};
45
use alloy_primitives::{keccak256, Address, Bytes};
5-
use kona_preimage::{errors::PreimageOracleError, CommsClient, PreimageKey, PreimageKeyType};
6+
use kona_preimage::{
7+
errors::PreimageOracleError, HintWriterClient, PreimageKey, PreimageKeyType,
8+
PreimageOracleClient,
9+
};
610
use kona_proof::{errors::OracleProviderError, HintType};
711
use revm::{
812
precompile::{
913
bn128::pair::{ISTANBUL_PAIR_BASE, ISTANBUL_PAIR_PER_POINT},
10-
u64_to_address, Error as PrecompileError,
14+
u64_to_address, Error as PrecompileError, PrecompileWithAddress,
1115
},
12-
primitives::{PrecompileOutput, PrecompileResult, StatefulPrecompile},
16+
primitives::{Precompile, PrecompileOutput, PrecompileResult},
1317
};
1418

15-
/// The address of the `ecPairing` precompile.
16-
pub const ECPAIRING_ADDRESS: Address = u64_to_address(8);
17-
18-
/// The length of a single pair element.
19+
const ECPAIRING_ADDRESS: Address = u64_to_address(8);
1920
const PAIR_ELEMENT_LEN: usize = 64 + 128;
2021

21-
/// An accelerated version of the `ecPairing` precompile that calls out to the host for the
22-
/// result of the precompile execution.
23-
#[derive(Debug)]
24-
pub struct EcPairingAccelerated<C>
25-
where
26-
C: CommsClient,
27-
{
28-
/// The comms client.
29-
comms_client: Arc<C>,
30-
}
31-
32-
impl<C> EcPairingAccelerated<C>
33-
where
34-
C: CommsClient,
35-
{
36-
/// Creates a new [EcPairingAccelerated] instance.
37-
pub fn new(comms_client: Arc<C>) -> Self {
38-
Self { comms_client }
39-
}
40-
}
41-
42-
impl<C> StatefulPrecompile for EcPairingAccelerated<C>
43-
where
44-
C: CommsClient + Send + Sync,
45-
{
46-
fn call(&self, input: &Bytes, gas_limit: u64, _: &revm::primitives::Env) -> PrecompileResult {
47-
fpvm_ecpairing(self.comms_client.as_ref(), input, gas_limit)
48-
}
49-
}
50-
51-
/// An accelerated version of the `ecPairing` precompile that calls out to the host for the
52-
/// result of the precompile execution after the Granite hardfork.
53-
#[derive(Debug)]
54-
pub struct EcPairingAcceleratedGranite<C> {
55-
/// The comms client.
56-
comms_client: Arc<C>,
57-
}
58-
59-
impl<C> EcPairingAcceleratedGranite<C> {
60-
/// Creates a new [EcPairingAcceleratedGranite] instance.
61-
pub fn new(comms_client: Arc<C>) -> Self {
62-
Self { comms_client }
63-
}
64-
}
22+
pub(crate) const FPVM_ECPAIRING: PrecompileWithAddress =
23+
PrecompileWithAddress(ECPAIRING_ADDRESS, Precompile::Standard(fpvm_ecpairing));
6524

66-
impl<C> StatefulPrecompile for EcPairingAcceleratedGranite<C>
67-
where
68-
C: CommsClient + Send + Sync,
69-
{
70-
fn call(&self, input: &Bytes, gas_limit: u64, _: &revm::primitives::Env) -> PrecompileResult {
71-
fpvm_ecpairing_granite(self.comms_client.as_ref(), input, gas_limit)
72-
}
73-
}
25+
pub(crate) const FPVM_ECPAIRING_GRANITE: PrecompileWithAddress =
26+
PrecompileWithAddress(ECPAIRING_ADDRESS, Precompile::Standard(fpvm_ecpairing_granite));
7427

7528
/// Performs an FPVM-accelerated `ecpairing` precompile call.
76-
fn fpvm_ecpairing<C>(comms_client: &C, input: &Bytes, gas_limit: u64) -> PrecompileResult
77-
where
78-
C: CommsClient,
79-
{
29+
fn fpvm_ecpairing(input: &Bytes, gas_limit: u64) -> PrecompileResult {
8030
let gas_used =
8131
(input.len() / PAIR_ELEMENT_LEN) as u64 * ISTANBUL_PAIR_PER_POINT + ISTANBUL_PAIR_BASE;
8232

@@ -91,7 +41,7 @@ where
9141
let result_data = kona_proof::block_on(async move {
9242
// Write the hint for the ecrecover precompile run.
9343
let hint_data = &[ECPAIRING_ADDRESS.as_ref(), input.as_ref()];
94-
comms_client
44+
HINT_WRITER
9545
.write(&HintType::L1Precompile.encode_with(hint_data))
9646
.await
9747
.map_err(OracleProviderError::Preimage)?;
@@ -101,7 +51,7 @@ where
10151
let key_hash = keccak256(&raw_key_data);
10252

10353
// Fetch the result of the ecrecover precompile run from the host.
104-
let result_data = comms_client
54+
let result_data = ORACLE_READER
10555
.get(PreimageKey::new(*key_hash, PreimageKeyType::Precompile))
10656
.await
10757
.map_err(OracleProviderError::Preimage)?;
@@ -129,14 +79,11 @@ where
12979
}
13080

13181
/// Performs an FPVM-accelerated `ecpairing` precompile call after the Granite hardfork.
132-
fn fpvm_ecpairing_granite<C>(comms_client: &C, input: &Bytes, gas_limit: u64) -> PrecompileResult
133-
where
134-
C: CommsClient,
135-
{
82+
fn fpvm_ecpairing_granite(input: &Bytes, gas_limit: u64) -> PrecompileResult {
13683
const BN256_MAX_PAIRING_SIZE_GRANITE: usize = 112_687;
13784
if input.len() > BN256_MAX_PAIRING_SIZE_GRANITE {
13885
return Err(PrecompileError::Bn128PairLength.into());
13986
}
14087

141-
fpvm_ecpairing(comms_client, input, gas_limit)
88+
fpvm_ecpairing(input, gas_limit)
14289
}

0 commit comments

Comments
 (0)