|
1 | 1 | //! Contains the accelerated version of the `ecPairing` precompile.
|
2 | 2 |
|
3 |
| -use alloc::{string::ToString, sync::Arc, vec::Vec}; |
| 3 | +use crate::{HINT_WRITER, ORACLE_READER}; |
| 4 | +use alloc::{string::ToString, vec::Vec}; |
4 | 5 | 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 | +}; |
6 | 10 | use kona_proof::{errors::OracleProviderError, HintType};
|
7 | 11 | use revm::{
|
8 | 12 | precompile::{
|
9 | 13 | bn128::pair::{ISTANBUL_PAIR_BASE, ISTANBUL_PAIR_PER_POINT},
|
10 |
| - u64_to_address, Error as PrecompileError, |
| 14 | + u64_to_address, Error as PrecompileError, PrecompileWithAddress, |
11 | 15 | },
|
12 |
| - primitives::{PrecompileOutput, PrecompileResult, StatefulPrecompile}, |
| 16 | + primitives::{Precompile, PrecompileOutput, PrecompileResult}, |
13 | 17 | };
|
14 | 18 |
|
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); |
19 | 20 | const PAIR_ELEMENT_LEN: usize = 64 + 128;
|
20 | 21 |
|
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)); |
65 | 24 |
|
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)); |
74 | 27 |
|
75 | 28 | /// 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 { |
80 | 30 | let gas_used =
|
81 | 31 | (input.len() / PAIR_ELEMENT_LEN) as u64 * ISTANBUL_PAIR_PER_POINT + ISTANBUL_PAIR_BASE;
|
82 | 32 |
|
|
91 | 41 | let result_data = kona_proof::block_on(async move {
|
92 | 42 | // Write the hint for the ecrecover precompile run.
|
93 | 43 | let hint_data = &[ECPAIRING_ADDRESS.as_ref(), input.as_ref()];
|
94 |
| - comms_client |
| 44 | + HINT_WRITER |
95 | 45 | .write(&HintType::L1Precompile.encode_with(hint_data))
|
96 | 46 | .await
|
97 | 47 | .map_err(OracleProviderError::Preimage)?;
|
|
101 | 51 | let key_hash = keccak256(&raw_key_data);
|
102 | 52 |
|
103 | 53 | // Fetch the result of the ecrecover precompile run from the host.
|
104 |
| - let result_data = comms_client |
| 54 | + let result_data = ORACLE_READER |
105 | 55 | .get(PreimageKey::new(*key_hash, PreimageKeyType::Precompile))
|
106 | 56 | .await
|
107 | 57 | .map_err(OracleProviderError::Preimage)?;
|
@@ -129,14 +79,11 @@ where
|
129 | 79 | }
|
130 | 80 |
|
131 | 81 | /// 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 { |
136 | 83 | const BN256_MAX_PAIRING_SIZE_GRANITE: usize = 112_687;
|
137 | 84 | if input.len() > BN256_MAX_PAIRING_SIZE_GRANITE {
|
138 | 85 | return Err(PrecompileError::Bn128PairLength.into());
|
139 | 86 | }
|
140 | 87 |
|
141 |
| - fpvm_ecpairing(comms_client, input, gas_limit) |
| 88 | + fpvm_ecpairing(input, gas_limit) |
142 | 89 | }
|
0 commit comments