diff --git a/bin/client/src/precompiles/bls12_g1_add.rs b/bin/client/src/precompiles/bls12_g1_add.rs index 295371d333..17ee6d3f13 100644 --- a/bin/client/src/precompiles/bls12_g1_add.rs +++ b/bin/client/src/precompiles/bls12_g1_add.rs @@ -6,13 +6,9 @@ //! //! [revm implementation]: https://github.com/bluealloy/revm/blob/main/crates/precompile/src/bls12_381/g1_add.rs -use crate::{HINT_WRITER, ORACLE_READER}; +use crate::precompiles::utils::precompile_run; use alloc::{string::ToString, vec::Vec}; use alloy_primitives::{address, keccak256, Address, Bytes}; -use kona_preimage::{ - errors::PreimageOracleError, PreimageKey, PreimageKeyType, PreimageOracleClient, -}; -use kona_proof::{errors::OracleProviderError, HintType}; use revm::{ precompile::{Error as PrecompileError, Precompile, PrecompileResult, PrecompileWithAddress}, primitives::PrecompileOutput, @@ -50,37 +46,8 @@ fn fpvm_bls12_g1_add(input: &Bytes, gas_limit: u64) -> PrecompileResult { .into()); } - let result_data = kona_proof::block_on(async move { - // Write the hint for the ecrecover precompile run. - let hint_data = &[BLS12_G1_ADD_CHECK.as_ref(), input.as_ref()]; - HintType::L1Precompile.with_data(hint_data).send(&HINT_WRITER).await?; - - // Construct the key hash for the ecrecover precompile run. - let raw_key_data = hint_data.iter().copied().flatten().copied().collect::>(); - let key_hash = keccak256(&raw_key_data); - - // Fetch the result of the ecrecover precompile run from the host. - let result_data = ORACLE_READER - .get(PreimageKey::new(*key_hash, PreimageKeyType::Precompile)) - .await - .map_err(OracleProviderError::Preimage)?; - - // Ensure we've received valid result data. - if result_data.is_empty() { - return Err(OracleProviderError::Preimage(PreimageOracleError::Other( - "Invalid result data".to_string(), - ))); - } - - // Ensure we've not received an error from the host. - if result_data[0] == 0 { - return Err(OracleProviderError::Preimage(PreimageOracleError::Other( - "Error executing ecrecover precompile in host".to_string(), - ))); - } - - // Return the result data. - Ok(result_data[1..].to_vec()) + let result_data = kona_proof::block_on(precompile_run! { + &[BLS12_G1_ADD_CHECK.as_ref(), input.as_ref()] }) .map_err(|e| PrecompileError::Other(e.to_string()))?; diff --git a/bin/client/src/precompiles/bls12_g1_msm.rs b/bin/client/src/precompiles/bls12_g1_msm.rs index 7e661f3d39..db8106a387 100644 --- a/bin/client/src/precompiles/bls12_g1_msm.rs +++ b/bin/client/src/precompiles/bls12_g1_msm.rs @@ -6,13 +6,9 @@ //! //! [revm implementation]: https://github.com/bluealloy/revm/blob/main/crates/precompile/src/bls12_381/g1_msm.rs -use crate::{precompiles::utils::msm_required_gas, HINT_WRITER, ORACLE_READER}; +use crate::precompiles::utils::{msm_required_gas, precompile_run}; use alloc::{string::ToString, vec::Vec}; use alloy_primitives::{address, keccak256, Address, Bytes}; -use kona_preimage::{ - errors::PreimageOracleError, PreimageKey, PreimageKeyType, PreimageOracleClient, -}; -use kona_proof::{errors::OracleProviderError, HintType}; use revm::{ precompile::{Error as PrecompileError, Precompile, PrecompileResult, PrecompileWithAddress}, primitives::PrecompileOutput, @@ -67,37 +63,8 @@ fn fpvm_bls12_g1_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult { return Err(PrecompileError::OutOfGas.into()); } - let result_data = kona_proof::block_on(async move { - // Write the hint for the ecrecover precompile run. - let hint_data = &[BLS12_G1_MSM_CHECK.as_ref(), input.as_ref()]; - HintType::L1Precompile.with_data(hint_data).send(&HINT_WRITER).await?; - - // Construct the key hash for the ecrecover precompile run. - let raw_key_data = hint_data.iter().copied().flatten().copied().collect::>(); - let key_hash = keccak256(&raw_key_data); - - // Fetch the result of the ecrecover precompile run from the host. - let result_data = ORACLE_READER - .get(PreimageKey::new(*key_hash, PreimageKeyType::Precompile)) - .await - .map_err(OracleProviderError::Preimage)?; - - // Ensure we've received valid result data. - if result_data.is_empty() { - return Err(OracleProviderError::Preimage(PreimageOracleError::Other( - "Invalid result data".to_string(), - ))); - } - - // Ensure we've not received an error from the host. - if result_data[0] == 0 { - return Err(OracleProviderError::Preimage(PreimageOracleError::Other( - "Error executing ecrecover precompile in host".to_string(), - ))); - } - - // Return the result data. - Ok(result_data[1..].to_vec()) + let result_data = kona_proof::block_on(precompile_run! { + &[BLS12_G1_MSM_CHECK.as_ref(), input.as_ref()] }) .map_err(|e| PrecompileError::Other(e.to_string()))?; diff --git a/bin/client/src/precompiles/bls12_g2_add.rs b/bin/client/src/precompiles/bls12_g2_add.rs index a5063a0ac0..b6dfdc5a83 100644 --- a/bin/client/src/precompiles/bls12_g2_add.rs +++ b/bin/client/src/precompiles/bls12_g2_add.rs @@ -6,13 +6,9 @@ //! //! [revm implementation]: https://github.com/bluealloy/revm/blob/main/crates/precompile/src/bls12_381/g2_add.rs -use crate::{HINT_WRITER, ORACLE_READER}; +use crate::precompiles::utils::precompile_run; use alloc::{string::ToString, vec::Vec}; use alloy_primitives::{address, keccak256, Address, Bytes}; -use kona_preimage::{ - errors::PreimageOracleError, PreimageKey, PreimageKeyType, PreimageOracleClient, -}; -use kona_proof::{errors::OracleProviderError, HintType}; use revm::{ precompile::{Error as PrecompileError, Precompile, PrecompileResult, PrecompileWithAddress}, primitives::PrecompileOutput, @@ -50,37 +46,8 @@ fn fpvm_bls12_g2_add(input: &Bytes, gas_limit: u64) -> PrecompileResult { .into()); } - let result_data = kona_proof::block_on(async move { - // Write the hint for the ecrecover precompile run. - let hint_data = &[BLS12_G2_ADD_CHECK.as_ref(), input.as_ref()]; - HintType::L1Precompile.with_data(hint_data).send(&HINT_WRITER).await?; - - // Construct the key hash for the ecrecover precompile run. - let raw_key_data = hint_data.iter().copied().flatten().copied().collect::>(); - let key_hash = keccak256(&raw_key_data); - - // Fetch the result of the ecrecover precompile run from the host. - let result_data = ORACLE_READER - .get(PreimageKey::new(*key_hash, PreimageKeyType::Precompile)) - .await - .map_err(OracleProviderError::Preimage)?; - - // Ensure we've received valid result data. - if result_data.is_empty() { - return Err(OracleProviderError::Preimage(PreimageOracleError::Other( - "Invalid result data".to_string(), - ))); - } - - // Ensure we've not received an error from the host. - if result_data[0] == 0 { - return Err(OracleProviderError::Preimage(PreimageOracleError::Other( - "Error executing ecrecover precompile in host".to_string(), - ))); - } - - // Return the result data. - Ok(result_data[1..].to_vec()) + let result_data = kona_proof::block_on(precompile_run! { + &[BLS12_G2_ADD_CHECK.as_ref(), input.as_ref()] }) .map_err(|e| PrecompileError::Other(e.to_string()))?; diff --git a/bin/client/src/precompiles/bls12_g2_msm.rs b/bin/client/src/precompiles/bls12_g2_msm.rs index 1677dfbb77..059838adee 100644 --- a/bin/client/src/precompiles/bls12_g2_msm.rs +++ b/bin/client/src/precompiles/bls12_g2_msm.rs @@ -6,13 +6,9 @@ //! //! [revm implementation]: https://github.com/bluealloy/revm/blob/main/crates/precompile/src/bls12_381/g2_msm.rs -use crate::{precompiles::utils::msm_required_gas, HINT_WRITER, ORACLE_READER}; +use crate::precompiles::utils::{msm_required_gas, precompile_run}; use alloc::{string::ToString, vec::Vec}; use alloy_primitives::{address, keccak256, Address, Bytes}; -use kona_preimage::{ - errors::PreimageOracleError, PreimageKey, PreimageKeyType, PreimageOracleClient, -}; -use kona_proof::{errors::OracleProviderError, HintType}; use revm::{ precompile::{Error as PrecompileError, Precompile, PrecompileResult, PrecompileWithAddress}, primitives::PrecompileOutput, @@ -67,37 +63,8 @@ fn fpvm_bls12_g2_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult { return Err(PrecompileError::OutOfGas.into()); } - let result_data = kona_proof::block_on(async move { - // Write the hint for the ecrecover precompile run. - let hint_data = &[BLS12_G2_MSM_CHECK.as_ref(), input.as_ref()]; - HintType::L1Precompile.with_data(hint_data).send(&HINT_WRITER).await?; - - // Construct the key hash for the ecrecover precompile run. - let raw_key_data = hint_data.iter().copied().flatten().copied().collect::>(); - let key_hash = keccak256(&raw_key_data); - - // Fetch the result of the ecrecover precompile run from the host. - let result_data = ORACLE_READER - .get(PreimageKey::new(*key_hash, PreimageKeyType::Precompile)) - .await - .map_err(OracleProviderError::Preimage)?; - - // Ensure we've received valid result data. - if result_data.is_empty() { - return Err(OracleProviderError::Preimage(PreimageOracleError::Other( - "Invalid result data".to_string(), - ))); - } - - // Ensure we've not received an error from the host. - if result_data[0] == 0 { - return Err(OracleProviderError::Preimage(PreimageOracleError::Other( - "Error executing ecrecover precompile in host".to_string(), - ))); - } - - // Return the result data. - Ok(result_data[1..].to_vec()) + let result_data = kona_proof::block_on(precompile_run! { + &[BLS12_G2_MSM_CHECK.as_ref(), input.as_ref()] }) .map_err(|e| PrecompileError::Other(e.to_string()))?; diff --git a/bin/client/src/precompiles/bls12_map_fp.rs b/bin/client/src/precompiles/bls12_map_fp.rs index a3251db7b0..9a6d3ea9bf 100644 --- a/bin/client/src/precompiles/bls12_map_fp.rs +++ b/bin/client/src/precompiles/bls12_map_fp.rs @@ -6,13 +6,9 @@ //! //! [revm implementation]: https://github.com/bluealloy/revm/blob/main/crates/precompile/src/bls12_381/map_fp_to_g1.rs -use crate::{HINT_WRITER, ORACLE_READER}; +use crate::precompiles::utils::precompile_run; use alloc::{string::ToString, vec::Vec}; use alloy_primitives::{address, keccak256, Address, Bytes}; -use kona_preimage::{ - errors::PreimageOracleError, PreimageKey, PreimageKeyType, PreimageOracleClient, -}; -use kona_proof::{errors::OracleProviderError, HintType}; use revm::{ precompile::{Error as PrecompileError, Precompile, PrecompileResult, PrecompileWithAddress}, primitives::PrecompileOutput, @@ -50,37 +46,8 @@ fn fpvm_bls12_map_fp(input: &Bytes, gas_limit: u64) -> PrecompileResult { .into()); } - let result_data = kona_proof::block_on(async move { - // Write the hint for the ecrecover precompile run. - let hint_data = &[BLS12_MAP_FP_CHECK.as_ref(), input.as_ref()]; - HintType::L1Precompile.with_data(hint_data).send(&HINT_WRITER).await?; - - // Construct the key hash for the ecrecover precompile run. - let raw_key_data = hint_data.iter().copied().flatten().copied().collect::>(); - let key_hash = keccak256(&raw_key_data); - - // Fetch the result of the ecrecover precompile run from the host. - let result_data = ORACLE_READER - .get(PreimageKey::new(*key_hash, PreimageKeyType::Precompile)) - .await - .map_err(OracleProviderError::Preimage)?; - - // Ensure we've received valid result data. - if result_data.is_empty() { - return Err(OracleProviderError::Preimage(PreimageOracleError::Other( - "Invalid result data".to_string(), - ))); - } - - // Ensure we've not received an error from the host. - if result_data[0] == 0 { - return Err(OracleProviderError::Preimage(PreimageOracleError::Other( - "Error executing ecrecover precompile in host".to_string(), - ))); - } - - // Return the result data. - Ok(result_data[1..].to_vec()) + let result_data = kona_proof::block_on(precompile_run! { + &[BLS12_MAP_FP_CHECK.as_ref(), input.as_ref()] }) .map_err(|e| PrecompileError::Other(e.to_string()))?; diff --git a/bin/client/src/precompiles/bls12_map_fp2.rs b/bin/client/src/precompiles/bls12_map_fp2.rs index 325ef94ca5..edc3df7305 100644 --- a/bin/client/src/precompiles/bls12_map_fp2.rs +++ b/bin/client/src/precompiles/bls12_map_fp2.rs @@ -6,13 +6,9 @@ //! //! [revm implementation]: https://github.com/bluealloy/revm/blob/main/crates/precompile/src/bls12_381/map_fp_to_g1.rs -use crate::{HINT_WRITER, ORACLE_READER}; +use crate::precompiles::utils::precompile_run; use alloc::{string::ToString, vec::Vec}; use alloy_primitives::{address, keccak256, Address, Bytes}; -use kona_preimage::{ - errors::PreimageOracleError, PreimageKey, PreimageKeyType, PreimageOracleClient, -}; -use kona_proof::{errors::OracleProviderError, HintType}; use revm::{ precompile::{Error as PrecompileError, Precompile, PrecompileResult, PrecompileWithAddress}, primitives::PrecompileOutput, @@ -50,37 +46,8 @@ fn fpvm_bls12_map_fp2(input: &Bytes, gas_limit: u64) -> PrecompileResult { .into()); } - let result_data = kona_proof::block_on(async move { - // Write the hint for the ecrecover precompile run. - let hint_data = &[BLS12_MAP_FP2_CHECK.as_ref(), input.as_ref()]; - HintType::L1Precompile.with_data(hint_data).send(&HINT_WRITER).await?; - - // Construct the key hash for the ecrecover precompile run. - let raw_key_data = hint_data.iter().copied().flatten().copied().collect::>(); - let key_hash = keccak256(&raw_key_data); - - // Fetch the result of the ecrecover precompile run from the host. - let result_data = ORACLE_READER - .get(PreimageKey::new(*key_hash, PreimageKeyType::Precompile)) - .await - .map_err(OracleProviderError::Preimage)?; - - // Ensure we've received valid result data. - if result_data.is_empty() { - return Err(OracleProviderError::Preimage(PreimageOracleError::Other( - "Invalid result data".to_string(), - ))); - } - - // Ensure we've not received an error from the host. - if result_data[0] == 0 { - return Err(OracleProviderError::Preimage(PreimageOracleError::Other( - "Error executing ecrecover precompile in host".to_string(), - ))); - } - - // Return the result data. - Ok(result_data[1..].to_vec()) + let result_data = kona_proof::block_on(precompile_run! { + &[BLS12_MAP_FP2_CHECK.as_ref(), input.as_ref()] }) .map_err(|e| PrecompileError::Other(e.to_string()))?; diff --git a/bin/client/src/precompiles/bls12_pairing.rs b/bin/client/src/precompiles/bls12_pairing.rs index 22aaf12cba..0015fcb3b0 100644 --- a/bin/client/src/precompiles/bls12_pairing.rs +++ b/bin/client/src/precompiles/bls12_pairing.rs @@ -6,13 +6,9 @@ //! //! [revm implementation]: https://github.com/bluealloy/revm/blob/main/crates/precompile/src/bls12_381/pairing.rs -use crate::{HINT_WRITER, ORACLE_READER}; +use crate::precompiles::utils::precompile_run; use alloc::{string::ToString, vec::Vec}; use alloy_primitives::{address, keccak256, Address, Bytes}; -use kona_preimage::{ - errors::PreimageOracleError, PreimageKey, PreimageKeyType, PreimageOracleClient, -}; -use kona_proof::{errors::OracleProviderError, HintType}; use revm::{ precompile::{Error as PrecompileError, Precompile, PrecompileResult, PrecompileWithAddress}, primitives::PrecompileOutput, @@ -53,37 +49,8 @@ fn fpvm_bls12_pairing(input: &Bytes, gas_limit: u64) -> PrecompileResult { return Err(PrecompileError::OutOfGas.into()); } - let result_data = kona_proof::block_on(async move { - // Write the hint for the ecrecover precompile run. - let hint_data = &[BLS12_PAIRING_CHECK.as_ref(), input.as_ref()]; - HintType::L1Precompile.with_data(hint_data).send(&HINT_WRITER).await?; - - // Construct the key hash for the ecrecover precompile run. - let raw_key_data = hint_data.iter().copied().flatten().copied().collect::>(); - let key_hash = keccak256(&raw_key_data); - - // Fetch the result of the ecrecover precompile run from the host. - let result_data = ORACLE_READER - .get(PreimageKey::new(*key_hash, PreimageKeyType::Precompile)) - .await - .map_err(OracleProviderError::Preimage)?; - - // Ensure we've received valid result data. - if result_data.is_empty() { - return Err(OracleProviderError::Preimage(PreimageOracleError::Other( - "Invalid result data".to_string(), - ))); - } - - // Ensure we've not received an error from the host. - if result_data[0] == 0 { - return Err(OracleProviderError::Preimage(PreimageOracleError::Other( - "Error executing ecrecover precompile in host".to_string(), - ))); - } - - // Return the result data. - Ok(result_data[1..].to_vec()) + let result_data = kona_proof::block_on(precompile_run! { + &[BLS12_PAIRING_CHECK.as_ref(), input.as_ref()] }) .map_err(|e| PrecompileError::Other(e.to_string()))?; diff --git a/bin/client/src/precompiles/bn128_pair.rs b/bin/client/src/precompiles/bn128_pair.rs index 5501d0c888..fce1b9719c 100644 --- a/bin/client/src/precompiles/bn128_pair.rs +++ b/bin/client/src/precompiles/bn128_pair.rs @@ -1,12 +1,8 @@ //! Contains the accelerated version of the `ecPairing` precompile. -use crate::{HINT_WRITER, ORACLE_READER}; +use crate::precompiles::utils::precompile_run; use alloc::{string::ToString, vec::Vec}; use alloy_primitives::{keccak256, Address, Bytes}; -use kona_preimage::{ - errors::PreimageOracleError, PreimageKey, PreimageKeyType, PreimageOracleClient, -}; -use kona_proof::{errors::OracleProviderError, HintType}; use revm::{ precompile::{ bn128::pair::{ISTANBUL_PAIR_BASE, ISTANBUL_PAIR_PER_POINT}, @@ -37,37 +33,8 @@ fn fpvm_ecpairing(input: &Bytes, gas_limit: u64) -> PrecompileResult { return Err(PrecompileError::Bn128PairLength.into()); } - let result_data = kona_proof::block_on(async move { - // Write the hint for the ecrecover precompile run. - let hint_data = &[ECPAIRING_ADDRESS.as_ref(), input.as_ref()]; - HintType::L1Precompile.with_data(hint_data).send(&HINT_WRITER).await?; - - // Construct the key hash for the ecrecover precompile run. - let raw_key_data = hint_data.iter().copied().flatten().copied().collect::>(); - let key_hash = keccak256(&raw_key_data); - - // Fetch the result of the ecrecover precompile run from the host. - let result_data = ORACLE_READER - .get(PreimageKey::new(*key_hash, PreimageKeyType::Precompile)) - .await - .map_err(OracleProviderError::Preimage)?; - - // Ensure we've received valid result data. - if result_data.is_empty() { - return Err(OracleProviderError::Preimage(PreimageOracleError::Other( - "Invalid result data".to_string(), - ))); - } - - // Ensure we've not received an error from the host. - if result_data[0] == 0 { - return Err(OracleProviderError::Preimage(PreimageOracleError::Other( - "Error executing ecrecover precompile in host".to_string(), - ))); - } - - // Return the result data. - Ok(result_data[1..].to_vec()) + let result_data = kona_proof::block_on(precompile_run! { + &[ECPAIRING_ADDRESS.as_ref(), input.as_ref()] }) .map_err(|e| PrecompileError::Other(e.to_string()))?; diff --git a/bin/client/src/precompiles/ecrecover.rs b/bin/client/src/precompiles/ecrecover.rs index 3c09df2743..da988f86ae 100644 --- a/bin/client/src/precompiles/ecrecover.rs +++ b/bin/client/src/precompiles/ecrecover.rs @@ -1,12 +1,8 @@ //! Contains the accelerated version of the `ecrecover` precompile. -use crate::{HINT_WRITER, ORACLE_READER}; +use crate::precompiles::utils::precompile_run; use alloc::{string::ToString, vec::Vec}; use alloy_primitives::{keccak256, Address, Bytes}; -use kona_preimage::{ - errors::PreimageOracleError, PreimageKey, PreimageKeyType, PreimageOracleClient, -}; -use kona_proof::{errors::OracleProviderError, HintType}; use revm::{ precompile::{u64_to_address, Error as PrecompileError, PrecompileWithAddress}, primitives::{Precompile, PrecompileOutput, PrecompileResult}, @@ -25,37 +21,8 @@ fn fpvm_ecrecover(input: &Bytes, gas_limit: u64) -> PrecompileResult { return Err(PrecompileError::OutOfGas.into()); } - let result_data = kona_proof::block_on(async move { - // Write the hint for the ecrecover precompile run. - let hint_data = &[ECRECOVER_ADDRESS.as_ref(), input.as_ref()]; - HintType::L1Precompile.with_data(hint_data).send(&HINT_WRITER).await?; - - // Construct the key hash for the ecrecover precompile run. - let raw_key_data = hint_data.iter().copied().flatten().copied().collect::>(); - let key_hash = keccak256(&raw_key_data); - - // Fetch the result of the ecrecover precompile run from the host. - let result_data = ORACLE_READER - .get(PreimageKey::new(*key_hash, PreimageKeyType::Precompile)) - .await - .map_err(OracleProviderError::Preimage)?; - - // Ensure we've received valid result data. - if result_data.is_empty() { - return Err(OracleProviderError::Preimage(PreimageOracleError::Other( - "Invalid result data".to_string(), - ))); - } - - // Ensure we've not received an error from the host. - if result_data[0] == 0 { - return Err(OracleProviderError::Preimage(PreimageOracleError::Other( - "Error executing ecrecover precompile in host".to_string(), - ))); - } - - // Return the result data. - Ok(result_data[1..].to_vec()) + let result_data = kona_proof::block_on(precompile_run! { + &[ECRECOVER_ADDRESS.as_ref(), input.as_ref()] }) .map_err(|e| PrecompileError::Other(e.to_string()))?; diff --git a/bin/client/src/precompiles/kzg_point_eval.rs b/bin/client/src/precompiles/kzg_point_eval.rs index 986db290b5..ddbc6834f4 100644 --- a/bin/client/src/precompiles/kzg_point_eval.rs +++ b/bin/client/src/precompiles/kzg_point_eval.rs @@ -1,12 +1,8 @@ //! Contains the accelerated version of the KZG point evaluation precompile. -use crate::{HINT_WRITER, ORACLE_READER}; +use crate::precompiles::utils::precompile_run; use alloc::{string::ToString, vec::Vec}; use alloy_primitives::{keccak256, Address, Bytes}; -use kona_preimage::{ - errors::PreimageOracleError, PreimageKey, PreimageKeyType, PreimageOracleClient, -}; -use kona_proof::{errors::OracleProviderError, HintType}; use revm::{ precompile::{u64_to_address, Error as PrecompileError, PrecompileWithAddress}, primitives::{Precompile, PrecompileOutput, PrecompileResult}, @@ -29,37 +25,8 @@ fn fpvm_kzg_point_eval(input: &Bytes, gas_limit: u64) -> PrecompileResult { return Err(PrecompileError::BlobInvalidInputLength.into()); } - let result_data = kona_proof::block_on(async move { - // Write the hint for the ecrecover precompile run. - let hint_data = &[POINT_EVAL_ADDRESS.as_ref(), input.as_ref()]; - HintType::L1Precompile.with_data(hint_data).send(&HINT_WRITER).await?; - - // Construct the key hash for the ecrecover precompile run. - let raw_key_data = hint_data.iter().copied().flatten().copied().collect::>(); - let key_hash = keccak256(&raw_key_data); - - // Fetch the result of the ecrecover precompile run from the host. - let result_data = ORACLE_READER - .get(PreimageKey::new(*key_hash, PreimageKeyType::Precompile)) - .await - .map_err(OracleProviderError::Preimage)?; - - // Ensure we've received valid result data. - if result_data.is_empty() { - return Err(OracleProviderError::Preimage(PreimageOracleError::Other( - "Invalid result data".to_string(), - ))); - } - - // Ensure we've not received an error from the host. - if result_data[0] == 0 { - return Err(OracleProviderError::Preimage(PreimageOracleError::Other( - "Error executing ecrecover precompile in host".to_string(), - ))); - } - - // Return the result data. - Ok(result_data[1..].to_vec()) + let result_data = kona_proof::block_on(precompile_run! { + &[POINT_EVAL_ADDRESS.as_ref(), input.as_ref()] }) .map_err(|e| PrecompileError::Other(e.to_string()))?; diff --git a/bin/client/src/precompiles/utils.rs b/bin/client/src/precompiles/utils.rs index 8cfc90c196..0f2b6c58f0 100644 --- a/bin/client/src/precompiles/utils.rs +++ b/bin/client/src/precompiles/utils.rs @@ -18,3 +18,55 @@ pub(crate) fn msm_required_gas(k: usize, discount_table: &[u16], multiplication_ (k as u64 * discount * multiplication_cost) / MSM_MULTIPLIER } + +/// A macro that generates an async block that sends a hint to the host, constructs a key hash +/// from the hint data, fetches the result of the precompile run from the host, and returns the +/// result data. +/// +/// The macro takes the following arguments: +/// - `hint_data`: The hint data to send to the host. +#[macro_export] +macro_rules! precompile_run { + ($hint_data:expr) => { + async move { + use kona_preimage::{ + errors::PreimageOracleError, PreimageKey, PreimageKeyType, PreimageOracleClient, + }; + use kona_proof::{errors::OracleProviderError, HintType}; + use $crate::{HINT_WRITER, ORACLE_READER}; + + // Write the hint for the precompile run. + let hint_data = $hint_data; + HintType::L1Precompile.with_data(hint_data).send(&HINT_WRITER).await?; + + // Construct the key hash for the precompile run. + let raw_key_data = hint_data.iter().copied().flatten().copied().collect::>(); + let key_hash = keccak256(&raw_key_data); + + // Fetch the result of the precompile run from the host. + let result_data = ORACLE_READER + .get(PreimageKey::new(*key_hash, PreimageKeyType::Precompile)) + .await + .map_err(OracleProviderError::Preimage)?; + + // Ensure we've received valid result data. + if result_data.is_empty() { + return Err(OracleProviderError::Preimage(PreimageOracleError::Other( + "Invalid result data".to_string(), + ))); + } + + // Ensure we've not received an error from the host. + if result_data[0] == 0 { + return Err(OracleProviderError::Preimage(PreimageOracleError::Other( + "Error executing precompile in host".to_string(), + ))); + } + + // Return the result data. + Ok(result_data[1..].to_vec()) + } + }; +} + +pub(crate) use precompile_run;