revm_precompile/
id.rs

1use std::borrow::Cow;
2
3use primitives::{address, Address};
4
5use crate::{Precompile, PrecompileSpecId};
6
7/// Precompile with address and function.
8/// Unique precompile identifier.
9#[derive(Clone, Debug, PartialEq, Eq, Hash)]
10pub enum PrecompileId {
11    /// Elliptic curve digital signature algorithm (ECDSA) public key recovery function.
12    EcRec,
13    /// SHA2-256 hash function.
14    Sha256,
15    /// RIPEMD-160 hash function.
16    Ripemd160,
17    /// Identity precompile.
18    Identity,
19    /// Arbitrary-precision exponentiation under modulo.
20    ModExp,
21    /// Point addition (ADD) on the elliptic curve 'alt_bn128'.
22    Bn254Add,
23    /// Scalar multiplication (MUL) on the elliptic curve 'alt_bn128'.
24    Bn254Mul,
25    /// Bilinear function on groups on the elliptic curve 'alt_bn128'.
26    Bn254Pairing,
27    /// Compression function F used in the BLAKE2 cryptographic hashing algorithm.
28    Blake2F,
29    /// Verify p(z) = y given commitment that corresponds to the polynomial p(x) and a KZG proof. Also verify that the provided commitment matches the provided versioned_hash.
30    KzgPointEvaluation,
31    /// Point addition in G1 (curve over base prime field).
32    Bls12G1Add,
33    /// Multi-scalar-multiplication (MSM) in G1 (curve over base prime field).
34    Bls12G1Msm,
35    /// Point addition in G2 (curve over quadratic extension of the base prime field).
36    Bls12G2Add,
37    /// Multi-scalar-multiplication (MSM) in G2 (curve over quadratic extension of the base prime field).
38    Bls12G2Msm,
39    /// Pairing operations between a set of pairs of (G1, G2) points.
40    Bls12Pairing,
41    /// Base field element mapping into the G1 point.
42    Bls12MapFpToGp1,
43    /// Extension field element mapping into the G2 point.
44    Bls12MapFp2ToGp2,
45    /// ECDSA signature verification over the secp256r1 elliptic curve (also known as P-256 or prime256v1).
46    P256Verify,
47    /// Custom precompile identifier.
48    Custom(Cow<'static, str>),
49}
50
51impl PrecompileId {
52    /// Create new custom precompile ID.
53    pub fn custom<I>(id: I) -> Self
54    where
55        I: Into<Cow<'static, str>>,
56    {
57        Self::Custom(id.into())
58    }
59
60    /// Returns the mainnet address for the precompile.
61    pub fn mainnet_address(&self) -> Option<Address> {
62        let address = match self {
63            Self::EcRec => address!("0x0000000000000000000000000000000000000001"),
64            Self::Sha256 => address!("0x0000000000000000000000000000000000000002"),
65            Self::Ripemd160 => address!("0x0000000000000000000000000000000000000003"),
66            Self::Identity => address!("0x0000000000000000000000000000000000000004"),
67            Self::ModExp => address!("0x0000000000000000000000000000000000000005"),
68            Self::Bn254Add => address!("0x0000000000000000000000000000000000000006"),
69            Self::Bn254Mul => address!("0x0000000000000000000000000000000000000007"),
70            Self::Bn254Pairing => address!("0x0000000000000000000000000000000000000008"),
71            Self::Blake2F => address!("0x0000000000000000000000000000000000000009"),
72            Self::KzgPointEvaluation => address!("0x000000000000000000000000000000000000000A"),
73            Self::Bls12G1Add => address!("0x000000000000000000000000000000000000000B"),
74            Self::Bls12G1Msm => address!("0x000000000000000000000000000000000000000C"),
75            Self::Bls12G2Add => address!("0x000000000000000000000000000000000000000D"),
76            Self::Bls12G2Msm => address!("0x000000000000000000000000000000000000000E"),
77            Self::Bls12Pairing => address!("0x000000000000000000000000000000000000000F"),
78            Self::Bls12MapFpToGp1 => address!("0x0000000000000000000000000000000000000010"),
79            Self::Bls12MapFp2ToGp2 => address!("0x0000000000000000000000000000000000000011"),
80            Self::P256Verify => address!("0x0000000000000000000000000000000000000012"),
81            Self::Custom(_) => return None,
82        };
83        Some(address)
84    }
85
86    /// Returns the name of the precompile as defined in EIP-7910.
87    pub fn name(&self) -> &str {
88        match self {
89            Self::EcRec => "ECREC",
90            Self::Sha256 => "SHA256",
91            Self::Ripemd160 => "RIPEMD160",
92            Self::Identity => "ID",
93            Self::ModExp => "MODEXP",
94            Self::Bn254Add => "BN254_ADD",
95            Self::Bn254Mul => "BN254_MUL",
96            Self::Bn254Pairing => "BN254_PAIRING",
97            Self::Blake2F => "BLAKE2F",
98            Self::KzgPointEvaluation => "KZG_POINT_EVALUATION",
99            Self::Bls12G1Add => "BLS12_G1ADD",
100            Self::Bls12G1Msm => "BLS12_G1MSM",
101            Self::Bls12G2Add => "BLS12_G2ADD",
102            Self::Bls12G2Msm => "BLS12_G2MSM",
103            Self::Bls12Pairing => "BLS12_PAIRING_CHECK",
104            Self::Bls12MapFpToGp1 => "BLS12_MAP_FP_TO_G1",
105            Self::Bls12MapFp2ToGp2 => "BLS12_MAP_FP2_TO_G2",
106            Self::P256Verify => "P256VERIFY",
107            Self::Custom(a) => a.as_ref(),
108        }
109    }
110
111    /// Returns the precompile function for the given spec.
112    ///
113    /// If case of [`PrecompileId::Custom`] it will return [`None`].
114    ///
115    /// For case where precompile was still not introduced in the spec,
116    /// it will return [`Some`] with fork closest to activation.
117    pub fn precompile(&self, spec: PrecompileSpecId) -> Option<Precompile> {
118        use PrecompileSpecId::*;
119
120        let precompile = match self {
121            Self::EcRec => crate::secp256k1::ECRECOVER,
122            Self::Sha256 => crate::hash::SHA256,
123            Self::Ripemd160 => crate::hash::RIPEMD160,
124            Self::Identity => crate::identity::FUN,
125            Self::ModExp => {
126                // ModExp changes gas calculation based on spec
127                if spec < BERLIN {
128                    crate::modexp::BYZANTIUM
129                } else if spec < OSAKA {
130                    crate::modexp::BERLIN
131                } else {
132                    crate::modexp::OSAKA
133                }
134            }
135            Self::Bn254Add => {
136                // BN254 add - gas cost changes in Istanbul
137                if spec < ISTANBUL {
138                    crate::bn254::add::BYZANTIUM
139                } else {
140                    crate::bn254::add::ISTANBUL
141                }
142            }
143            Self::Bn254Mul => {
144                // BN254 mul - gas cost changes in Istanbul
145                if spec < ISTANBUL {
146                    crate::bn254::mul::BYZANTIUM
147                } else {
148                    crate::bn254::mul::ISTANBUL
149                }
150            }
151            Self::Bn254Pairing => {
152                // BN254 pairing - gas cost changes in Istanbul
153                if spec < ISTANBUL {
154                    crate::bn254::pair::BYZANTIUM
155                } else {
156                    crate::bn254::pair::ISTANBUL
157                }
158            }
159            Self::Blake2F => crate::blake2::FUN,
160            Self::KzgPointEvaluation => crate::kzg_point_evaluation::POINT_EVALUATION,
161            Self::Bls12G1Add => crate::bls12_381::g1_add::PRECOMPILE,
162            Self::Bls12G1Msm => crate::bls12_381::g1_msm::PRECOMPILE,
163            Self::Bls12G2Add => crate::bls12_381::g2_add::PRECOMPILE,
164            Self::Bls12G2Msm => crate::bls12_381::g2_msm::PRECOMPILE,
165            Self::Bls12Pairing => crate::bls12_381::pairing::PRECOMPILE,
166            Self::Bls12MapFpToGp1 => crate::bls12_381::map_fp_to_g1::PRECOMPILE,
167            Self::Bls12MapFp2ToGp2 => crate::bls12_381::map_fp2_to_g2::PRECOMPILE,
168            Self::P256Verify => {
169                // P256 verify - gas cost changes in Osaka
170                if spec < OSAKA {
171                    crate::secp256r1::P256VERIFY
172                } else {
173                    crate::secp256r1::P256VERIFY_OSAKA
174                }
175            }
176            Self::Custom(_) => return None,
177        };
178
179        Some(precompile)
180    }
181}