op_revm/
precompiles.rs

1//! Contains Optimism specific precompiles.
2use crate::OpSpecId;
3use revm::{
4    context::Cfg,
5    context_interface::ContextTr,
6    handler::{EthPrecompiles, PrecompileProvider},
7    interpreter::{InputsImpl, InterpreterResult},
8    precompile::{
9        self, bn254, secp256r1, PrecompileError, PrecompileResult, PrecompileWithAddress,
10        Precompiles,
11    },
12    primitives::{hardfork::SpecId, Address, OnceLock},
13};
14use std::boxed::Box;
15use std::string::String;
16
17/// Optimism precompile provider
18#[derive(Debug, Clone)]
19pub struct OpPrecompiles {
20    /// Inner precompile provider is same as Ethereums.
21    inner: EthPrecompiles,
22    /// Spec id of the precompile provider.
23    spec: OpSpecId,
24}
25
26impl OpPrecompiles {
27    /// Create a new precompile provider with the given OpSpec.
28    #[inline]
29    pub fn new_with_spec(spec: OpSpecId) -> Self {
30        let precompiles = match spec {
31            spec @ (OpSpecId::BEDROCK
32            | OpSpecId::REGOLITH
33            | OpSpecId::CANYON
34            | OpSpecId::ECOTONE) => Precompiles::new(spec.into_eth_spec().into()),
35            OpSpecId::FJORD => fjord(),
36            OpSpecId::GRANITE | OpSpecId::HOLOCENE => granite(),
37            OpSpecId::ISTHMUS | OpSpecId::INTEROP | OpSpecId::OSAKA => isthmus(),
38        };
39
40        Self {
41            inner: EthPrecompiles {
42                precompiles,
43                spec: SpecId::default(),
44            },
45            spec,
46        }
47    }
48
49    /// Precompiles getter.
50    #[inline]
51    pub fn precompiles(&self) -> &'static Precompiles {
52        self.inner.precompiles
53    }
54}
55
56/// Returns precompiles for Fjord spec.
57pub fn fjord() -> &'static Precompiles {
58    static INSTANCE: OnceLock<Precompiles> = OnceLock::new();
59    INSTANCE.get_or_init(|| {
60        let mut precompiles = Precompiles::cancun().clone();
61        // RIP-7212: secp256r1 P256verify
62        precompiles.extend([secp256r1::P256VERIFY]);
63        precompiles
64    })
65}
66
67/// Returns precompiles for Granite spec.
68pub fn granite() -> &'static Precompiles {
69    static INSTANCE: OnceLock<Precompiles> = OnceLock::new();
70    INSTANCE.get_or_init(|| {
71        let mut precompiles = fjord().clone();
72        // Restrict bn254Pairing input size
73        precompiles.extend([bn254_pair::GRANITE]);
74        precompiles
75    })
76}
77
78/// Returns precompiles for isthumus spec.
79pub fn isthmus() -> &'static Precompiles {
80    static INSTANCE: OnceLock<Precompiles> = OnceLock::new();
81    INSTANCE.get_or_init(|| {
82        let mut precompiles = granite().clone();
83        // Prague bls12 precompiles
84        precompiles.extend(precompile::bls12_381::precompiles());
85        // Isthmus bls12 precompile modifications
86        precompiles.extend([
87            bls12_381::ISTHMUS_G1_MSM,
88            bls12_381::ISTHMUS_G2_MSM,
89            bls12_381::ISTHMUS_PAIRING,
90        ]);
91        precompiles
92    })
93}
94
95impl<CTX> PrecompileProvider<CTX> for OpPrecompiles
96where
97    CTX: ContextTr<Cfg: Cfg<Spec = OpSpecId>>,
98{
99    type Output = InterpreterResult;
100
101    #[inline]
102    fn set_spec(&mut self, spec: <CTX::Cfg as Cfg>::Spec) -> bool {
103        if spec == self.spec {
104            return false;
105        }
106        *self = Self::new_with_spec(spec);
107        true
108    }
109
110    #[inline]
111    fn run(
112        &mut self,
113        context: &mut CTX,
114        address: &Address,
115        inputs: &InputsImpl,
116        is_static: bool,
117        gas_limit: u64,
118    ) -> Result<Option<Self::Output>, String> {
119        self.inner
120            .run(context, address, inputs, is_static, gas_limit)
121    }
122
123    #[inline]
124    fn warm_addresses(&self) -> Box<impl Iterator<Item = Address>> {
125        self.inner.warm_addresses()
126    }
127
128    #[inline]
129    fn contains(&self, address: &Address) -> bool {
130        self.inner.contains(address)
131    }
132}
133
134impl Default for OpPrecompiles {
135    fn default() -> Self {
136        Self::new_with_spec(OpSpecId::ISTHMUS)
137    }
138}
139
140/// Bn254 pair precompile.
141pub mod bn254_pair {
142    use super::*;
143
144    /// Max input size for the bn254 pair precompile.
145    pub const GRANITE_MAX_INPUT_SIZE: usize = 112687;
146    /// Bn254 pair precompile.
147    pub const GRANITE: PrecompileWithAddress =
148        PrecompileWithAddress(bn254::pair::ADDRESS, |input, gas_limit| {
149            run_pair(input, gas_limit)
150        });
151
152    /// Run the bn254 pair precompile with Optimism input limit.
153    pub fn run_pair(input: &[u8], gas_limit: u64) -> PrecompileResult {
154        if input.len() > GRANITE_MAX_INPUT_SIZE {
155            return Err(PrecompileError::Bn254PairLength);
156        }
157        bn254::run_pair(
158            input,
159            bn254::pair::ISTANBUL_PAIR_PER_POINT,
160            bn254::pair::ISTANBUL_PAIR_BASE,
161            gas_limit,
162        )
163    }
164}
165
166/// Bls12_381 precompile.
167pub mod bls12_381 {
168    use super::*;
169    use revm::precompile::bls12_381_const::{G1_MSM_ADDRESS, G2_MSM_ADDRESS, PAIRING_ADDRESS};
170
171    #[cfg(not(feature = "std"))]
172    use crate::std::string::ToString;
173
174    /// Max input size for the g1 msm precompile.
175    pub const ISTHMUS_G1_MSM_MAX_INPUT_SIZE: usize = 513760;
176    /// Max input size for the g2 msm precompile.
177    pub const ISTHMUS_G2_MSM_MAX_INPUT_SIZE: usize = 488448;
178    /// Max input size for the pairing precompile.
179    pub const ISTHMUS_PAIRING_MAX_INPUT_SIZE: usize = 235008;
180
181    /// G1 msm precompile.
182    pub const ISTHMUS_G1_MSM: PrecompileWithAddress =
183        PrecompileWithAddress(G1_MSM_ADDRESS, run_g1_msm);
184    /// G2 msm precompile.
185    pub const ISTHMUS_G2_MSM: PrecompileWithAddress =
186        PrecompileWithAddress(G2_MSM_ADDRESS, run_g2_msm);
187    /// Pairing precompile.
188    pub const ISTHMUS_PAIRING: PrecompileWithAddress =
189        PrecompileWithAddress(PAIRING_ADDRESS, run_pair);
190
191    /// Run the g1 msm precompile with Optimism input limit.
192    pub fn run_g1_msm(input: &[u8], gas_limit: u64) -> PrecompileResult {
193        if input.len() > ISTHMUS_G1_MSM_MAX_INPUT_SIZE {
194            return Err(PrecompileError::Other(
195                "G1MSM input length too long for OP Stack input size limitation".to_string(),
196            ));
197        }
198        precompile::bls12_381::g1_msm::g1_msm(input, gas_limit)
199    }
200
201    /// Run the g2 msm precompile with Optimism input limit.
202    pub fn run_g2_msm(input: &[u8], gas_limit: u64) -> PrecompileResult {
203        if input.len() > ISTHMUS_G2_MSM_MAX_INPUT_SIZE {
204            return Err(PrecompileError::Other(
205                "G2MSM input length too long for OP Stack input size limitation".to_string(),
206            ));
207        }
208        precompile::bls12_381::g2_msm::g2_msm(input, gas_limit)
209    }
210
211    /// Run the pairing precompile with Optimism input limit.
212    pub fn run_pair(input: &[u8], gas_limit: u64) -> PrecompileResult {
213        if input.len() > ISTHMUS_PAIRING_MAX_INPUT_SIZE {
214            return Err(PrecompileError::Other(
215                "Pairing input length too long for OP Stack input size limitation".to_string(),
216            ));
217        }
218        precompile::bls12_381::pairing::pairing(input, gas_limit)
219    }
220}
221
222#[cfg(test)]
223mod tests {
224    use crate::precompiles::bls12_381::{
225        run_g1_msm, run_g2_msm, ISTHMUS_G1_MSM_MAX_INPUT_SIZE, ISTHMUS_G2_MSM_MAX_INPUT_SIZE,
226        ISTHMUS_PAIRING_MAX_INPUT_SIZE,
227    };
228
229    use super::*;
230    use revm::{
231        precompile::PrecompileError,
232        primitives::{hex, Bytes},
233    };
234    use std::vec;
235
236    #[test]
237    fn test_bn254_pair() {
238        let input = hex::decode(
239            "\
240      1c76476f4def4bb94541d57ebba1193381ffa7aa76ada664dd31c16024c43f59\
241      3034dd2920f673e204fee2811c678745fc819b55d3e9d294e45c9b03a76aef41\
242      209dd15ebff5d46c4bd888e51a93cf99a7329636c63514396b4a452003a35bf7\
243      04bf11ca01483bfa8b34b43561848d28905960114c8ac04049af4b6315a41678\
244      2bb8324af6cfc93537a2ad1a445cfd0ca2a71acd7ac41fadbf933c2a51be344d\
245      120a2a4cf30c1bf9845f20c6fe39e07ea2cce61f0c9bb048165fe5e4de877550\
246      111e129f1cf1097710d41c4ac70fcdfa5ba2023c6ff1cbeac322de49d1b6df7c\
247      2032c61a830e3c17286de9462bf242fca2883585b93870a73853face6a6bf411\
248      198e9393920d483a7260bfb731fb5d25f1aa493335a9e71297e485b7aef312c2\
249      1800deef121f1e76426a00665e5c4479674322d4f75edadd46debd5cd992f6ed\
250      090689d0585ff075ec9e99ad690c3395bc4b313370b38ef355acdadcd122975b\
251      12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa",
252        )
253        .unwrap();
254        let expected =
255            hex::decode("0000000000000000000000000000000000000000000000000000000000000001")
256                .unwrap();
257        let outcome = bn254_pair::run_pair(&input, 260_000).unwrap();
258        assert_eq!(outcome.bytes, expected);
259
260        // Invalid input length
261        let input = hex::decode(
262            "\
263          1111111111111111111111111111111111111111111111111111111111111111\
264          1111111111111111111111111111111111111111111111111111111111111111\
265          111111111111111111111111111111\
266      ",
267        )
268        .unwrap();
269
270        let res = bn254_pair::run_pair(&input, 260_000);
271        assert!(matches!(res, Err(PrecompileError::Bn254PairLength)));
272
273        // Valid input length shorter than 112687
274        let input = vec![1u8; 586 * bn254::PAIR_ELEMENT_LEN];
275        let res = bn254_pair::run_pair(&input, 260_000);
276        assert!(matches!(res, Err(PrecompileError::OutOfGas)));
277
278        // Input length longer than 112687
279        let input = vec![1u8; 587 * bn254::PAIR_ELEMENT_LEN];
280        let res = bn254_pair::run_pair(&input, 260_000);
281        assert!(matches!(res, Err(PrecompileError::Bn254PairLength)));
282    }
283
284    #[test]
285    fn test_cancun_precompiles_in_fjord() {
286        // additional to cancun, fjord has p256verify
287        assert_eq!(fjord().difference(Precompiles::cancun()).len(), 1)
288    }
289
290    #[test]
291    fn test_cancun_precompiles_in_granite() {
292        // granite has p256verify (fjord)
293        // granite has modification of cancun's bn254 pair (doesn't count as new precompile)
294        assert_eq!(granite().difference(Precompiles::cancun()).len(), 1)
295    }
296
297    #[test]
298    fn test_prague_precompiles_in_isthmus() {
299        let new_prague_precompiles = Precompiles::prague().difference(Precompiles::cancun());
300
301        // isthmus contains all precompiles that were new in prague, without modifications
302        assert!(new_prague_precompiles.difference(isthmus()).is_empty())
303    }
304
305    #[test]
306    fn test_default_precompiles_is_latest() {
307        let latest = OpPrecompiles::new_with_spec(OpSpecId::default())
308            .inner
309            .precompiles;
310        let default = OpPrecompiles::default().inner.precompiles;
311        assert_eq!(latest.len(), default.len());
312
313        let intersection = default.intersection(latest);
314        assert_eq!(intersection.len(), latest.len())
315    }
316
317    #[test]
318    fn test_g1_isthmus_max_size() {
319        let oversized_input = vec![0u8; ISTHMUS_G1_MSM_MAX_INPUT_SIZE + 1];
320        let input = Bytes::from(oversized_input);
321
322        let res = run_g1_msm(&input, 260_000);
323
324        assert!(
325            matches!(res, Err(PrecompileError::Other(msg)) if msg.contains("input length too long"))
326        );
327    }
328    #[test]
329    fn test_g2_isthmus_max_size() {
330        let oversized_input = vec![0u8; ISTHMUS_G2_MSM_MAX_INPUT_SIZE + 1];
331        let input = Bytes::from(oversized_input);
332
333        let res = run_g2_msm(&input, 260_000);
334
335        assert!(
336            matches!(res, Err(PrecompileError::Other(msg)) if msg.contains("input length too long"))
337        );
338    }
339    #[test]
340    fn test_pair_isthmus_max_size() {
341        let oversized_input = vec![0u8; ISTHMUS_PAIRING_MAX_INPUT_SIZE + 1];
342        let input = Bytes::from(oversized_input);
343
344        let res = bls12_381::run_pair(&input, 260_000);
345
346        assert!(
347            matches!(res, Err(PrecompileError::Other(msg)) if msg.contains("input length too long"))
348        );
349    }
350}