1use crate::OpSpecId;
3use revm::{
4 context::Cfg,
5 context_interface::ContextTr,
6 handler::{EthPrecompiles, PrecompileProvider},
7 interpreter::{CallInputs, InterpreterResult},
8 precompile::{
9 self, bn254, secp256r1, Precompile, PrecompileError, PrecompileId, PrecompileResult,
10 Precompiles,
11 },
12 primitives::{hardfork::SpecId, Address, OnceLock},
13};
14use std::boxed::Box;
15use std::string::String;
16
17#[derive(Debug, Clone)]
19pub struct OpPrecompiles {
20 inner: EthPrecompiles,
22 spec: OpSpecId,
24}
25
26impl OpPrecompiles {
27 #[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 #[inline]
51 pub fn precompiles(&self) -> &'static Precompiles {
52 self.inner.precompiles
53 }
54}
55
56pub fn fjord() -> &'static Precompiles {
58 static INSTANCE: OnceLock<Precompiles> = OnceLock::new();
59 INSTANCE.get_or_init(|| {
60 let mut precompiles = Precompiles::cancun().clone();
61 precompiles.extend([secp256r1::P256VERIFY]);
63 precompiles
64 })
65}
66
67pub fn granite() -> &'static Precompiles {
69 static INSTANCE: OnceLock<Precompiles> = OnceLock::new();
70 INSTANCE.get_or_init(|| {
71 let mut precompiles = fjord().clone();
72 precompiles.extend([bn254_pair::GRANITE]);
74 precompiles
75 })
76}
77
78pub fn isthmus() -> &'static Precompiles {
80 static INSTANCE: OnceLock<Precompiles> = OnceLock::new();
81 INSTANCE.get_or_init(|| {
82 let mut precompiles = granite().clone();
83 precompiles.extend(precompile::bls12_381::precompiles());
85 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 inputs: &CallInputs,
115 ) -> Result<Option<Self::Output>, String> {
116 self.inner.run(context, inputs)
117 }
118
119 #[inline]
120 fn warm_addresses(&self) -> Box<impl Iterator<Item = Address>> {
121 self.inner.warm_addresses()
122 }
123
124 #[inline]
125 fn contains(&self, address: &Address) -> bool {
126 self.inner.contains(address)
127 }
128}
129
130impl Default for OpPrecompiles {
131 fn default() -> Self {
132 Self::new_with_spec(OpSpecId::ISTHMUS)
133 }
134}
135
136pub mod bn254_pair {
138 use super::*;
139
140 pub const GRANITE_MAX_INPUT_SIZE: usize = 112687;
142 pub const GRANITE: Precompile =
144 Precompile::new(PrecompileId::Bn254Pairing, bn254::pair::ADDRESS, run_pair);
145
146 pub fn run_pair(input: &[u8], gas_limit: u64) -> PrecompileResult {
148 if input.len() > GRANITE_MAX_INPUT_SIZE {
149 return Err(PrecompileError::Bn254PairLength);
150 }
151 bn254::run_pair(
152 input,
153 bn254::pair::ISTANBUL_PAIR_PER_POINT,
154 bn254::pair::ISTANBUL_PAIR_BASE,
155 gas_limit,
156 )
157 }
158}
159
160pub mod bls12_381 {
162 use super::*;
163 use revm::precompile::bls12_381_const::{G1_MSM_ADDRESS, G2_MSM_ADDRESS, PAIRING_ADDRESS};
164
165 #[cfg(not(feature = "std"))]
166 use crate::std::string::ToString;
167
168 pub const ISTHMUS_G1_MSM_MAX_INPUT_SIZE: usize = 513760;
170 pub const ISTHMUS_G2_MSM_MAX_INPUT_SIZE: usize = 488448;
172 pub const ISTHMUS_PAIRING_MAX_INPUT_SIZE: usize = 235008;
174
175 pub const ISTHMUS_G1_MSM: Precompile =
177 Precompile::new(PrecompileId::Bls12G1Msm, G1_MSM_ADDRESS, run_g1_msm);
178 pub const ISTHMUS_G2_MSM: Precompile =
180 Precompile::new(PrecompileId::Bls12G2Msm, G2_MSM_ADDRESS, run_g2_msm);
181 pub const ISTHMUS_PAIRING: Precompile =
183 Precompile::new(PrecompileId::Bls12Pairing, PAIRING_ADDRESS, run_pair);
184
185 pub fn run_g1_msm(input: &[u8], gas_limit: u64) -> PrecompileResult {
187 if input.len() > ISTHMUS_G1_MSM_MAX_INPUT_SIZE {
188 return Err(PrecompileError::Other(
189 "G1MSM input length too long for OP Stack input size limitation".to_string(),
190 ));
191 }
192 precompile::bls12_381::g1_msm::g1_msm(input, gas_limit)
193 }
194
195 pub fn run_g2_msm(input: &[u8], gas_limit: u64) -> PrecompileResult {
197 if input.len() > ISTHMUS_G2_MSM_MAX_INPUT_SIZE {
198 return Err(PrecompileError::Other(
199 "G2MSM input length too long for OP Stack input size limitation".to_string(),
200 ));
201 }
202 precompile::bls12_381::g2_msm::g2_msm(input, gas_limit)
203 }
204
205 pub fn run_pair(input: &[u8], gas_limit: u64) -> PrecompileResult {
207 if input.len() > ISTHMUS_PAIRING_MAX_INPUT_SIZE {
208 return Err(PrecompileError::Other(
209 "Pairing input length too long for OP Stack input size limitation".to_string(),
210 ));
211 }
212 precompile::bls12_381::pairing::pairing(input, gas_limit)
213 }
214}
215
216#[cfg(test)]
217mod tests {
218 use crate::precompiles::bls12_381::{
219 run_g1_msm, run_g2_msm, ISTHMUS_G1_MSM_MAX_INPUT_SIZE, ISTHMUS_G2_MSM_MAX_INPUT_SIZE,
220 ISTHMUS_PAIRING_MAX_INPUT_SIZE,
221 };
222
223 use super::*;
224 use revm::{
225 precompile::PrecompileError,
226 primitives::{hex, Bytes},
227 };
228 use std::vec;
229
230 #[test]
231 fn test_bn254_pair() {
232 let input = hex::decode(
233 "\
234 1c76476f4def4bb94541d57ebba1193381ffa7aa76ada664dd31c16024c43f59\
235 3034dd2920f673e204fee2811c678745fc819b55d3e9d294e45c9b03a76aef41\
236 209dd15ebff5d46c4bd888e51a93cf99a7329636c63514396b4a452003a35bf7\
237 04bf11ca01483bfa8b34b43561848d28905960114c8ac04049af4b6315a41678\
238 2bb8324af6cfc93537a2ad1a445cfd0ca2a71acd7ac41fadbf933c2a51be344d\
239 120a2a4cf30c1bf9845f20c6fe39e07ea2cce61f0c9bb048165fe5e4de877550\
240 111e129f1cf1097710d41c4ac70fcdfa5ba2023c6ff1cbeac322de49d1b6df7c\
241 2032c61a830e3c17286de9462bf242fca2883585b93870a73853face6a6bf411\
242 198e9393920d483a7260bfb731fb5d25f1aa493335a9e71297e485b7aef312c2\
243 1800deef121f1e76426a00665e5c4479674322d4f75edadd46debd5cd992f6ed\
244 090689d0585ff075ec9e99ad690c3395bc4b313370b38ef355acdadcd122975b\
245 12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa",
246 )
247 .unwrap();
248 let expected =
249 hex::decode("0000000000000000000000000000000000000000000000000000000000000001")
250 .unwrap();
251 let outcome = bn254_pair::run_pair(&input, 260_000).unwrap();
252 assert_eq!(outcome.bytes, expected);
253
254 let input = hex::decode(
256 "\
257 1111111111111111111111111111111111111111111111111111111111111111\
258 1111111111111111111111111111111111111111111111111111111111111111\
259 111111111111111111111111111111\
260 ",
261 )
262 .unwrap();
263
264 let res = bn254_pair::run_pair(&input, 260_000);
265 assert!(matches!(res, Err(PrecompileError::Bn254PairLength)));
266
267 let input = vec![1u8; 586 * bn254::PAIR_ELEMENT_LEN];
269 let res = bn254_pair::run_pair(&input, 260_000);
270 assert!(matches!(res, Err(PrecompileError::OutOfGas)));
271
272 let input = vec![1u8; 587 * bn254::PAIR_ELEMENT_LEN];
274 let res = bn254_pair::run_pair(&input, 260_000);
275 assert!(matches!(res, Err(PrecompileError::Bn254PairLength)));
276 }
277
278 #[test]
279 fn test_cancun_precompiles_in_fjord() {
280 assert_eq!(fjord().difference(Precompiles::cancun()).len(), 1)
282 }
283
284 #[test]
285 fn test_cancun_precompiles_in_granite() {
286 assert_eq!(granite().difference(Precompiles::cancun()).len(), 1)
289 }
290
291 #[test]
292 fn test_prague_precompiles_in_isthmus() {
293 let new_prague_precompiles = Precompiles::prague().difference(Precompiles::cancun());
294
295 assert!(new_prague_precompiles.difference(isthmus()).is_empty())
297 }
298
299 #[test]
300 fn test_default_precompiles_is_latest() {
301 let latest = OpPrecompiles::new_with_spec(OpSpecId::default())
302 .inner
303 .precompiles;
304 let default = OpPrecompiles::default().inner.precompiles;
305 assert_eq!(latest.len(), default.len());
306
307 let intersection = default.intersection(latest);
308 assert_eq!(intersection.len(), latest.len())
309 }
310
311 #[test]
312 fn test_g1_isthmus_max_size() {
313 let oversized_input = vec![0u8; ISTHMUS_G1_MSM_MAX_INPUT_SIZE + 1];
314 let input = Bytes::from(oversized_input);
315
316 let res = run_g1_msm(&input, 260_000);
317
318 assert!(
319 matches!(res, Err(PrecompileError::Other(msg)) if msg.contains("input length too long"))
320 );
321 }
322 #[test]
323 fn test_g2_isthmus_max_size() {
324 let oversized_input = vec![0u8; ISTHMUS_G2_MSM_MAX_INPUT_SIZE + 1];
325 let input = Bytes::from(oversized_input);
326
327 let res = run_g2_msm(&input, 260_000);
328
329 assert!(
330 matches!(res, Err(PrecompileError::Other(msg)) if msg.contains("input length too long"))
331 );
332 }
333 #[test]
334 fn test_pair_isthmus_max_size() {
335 let oversized_input = vec![0u8; ISTHMUS_PAIRING_MAX_INPUT_SIZE + 1];
336 let input = Bytes::from(oversized_input);
337
338 let res = bls12_381::run_pair(&input, 260_000);
339
340 assert!(
341 matches!(res, Err(PrecompileError::Other(msg)) if msg.contains("input length too long"))
342 );
343 }
344}