revm_precompile/
interface.rs

1//! Interface for the precompiles. It contains the precompile result type,
2//! the precompile output type, and the precompile error type.
3use core::fmt;
4use primitives::Bytes;
5use std::string::String;
6
7/// A precompile operation result type
8///
9/// Returns either `Ok((gas_used, return_bytes))` or `Err(error)`.
10pub type PrecompileResult = Result<PrecompileOutput, PrecompileError>;
11
12/// Precompile execution output
13#[derive(Clone, Debug, PartialEq, Eq, Hash)]
14pub struct PrecompileOutput {
15    /// Gas used by the precompile
16    pub gas_used: u64,
17    /// Output bytes
18    pub bytes: Bytes,
19}
20
21impl PrecompileOutput {
22    /// Returns new precompile output with the given gas used and output bytes.
23    pub fn new(gas_used: u64, bytes: Bytes) -> Self {
24        Self { gas_used, bytes }
25    }
26}
27
28/// Precompile function type. Takes input and gas limit and returns precompile result.
29pub type PrecompileFn = fn(&[u8], u64) -> PrecompileResult;
30
31/// Precompile error type.
32#[derive(Clone, Debug, PartialEq, Eq, Hash)]
33pub enum PrecompileError {
34    /// out of gas is the main error. Others are here just for completeness
35    OutOfGas,
36    /// Blake2 errors
37    Blake2WrongLength,
38    /// Blake2 wrong final indicator flag
39    Blake2WrongFinalIndicatorFlag,
40    /// Modexp errors
41    ModexpExpOverflow,
42    /// Modexp base overflow
43    ModexpBaseOverflow,
44    /// Modexp mod overflow
45    ModexpModOverflow,
46    /// Bn128 errors
47    Bn128FieldPointNotAMember,
48    /// Bn128 affine g failed to create
49    Bn128AffineGFailedToCreate,
50    /// Bn128 pair length
51    Bn128PairLength,
52    // Blob errors
53    /// The input length is not exactly 192 bytes
54    BlobInvalidInputLength,
55    /// The commitment does not match the versioned hash
56    BlobMismatchedVersion,
57    /// The proof verification failed
58    BlobVerifyKzgProofFailed,
59    /// Fatal error with a custom error message
60    Fatal(String),
61    /// Catch-all variant for other errors
62    Other(String),
63}
64
65impl PrecompileError {
66    /// Returns another error with the given message.
67    pub fn other(err: impl Into<String>) -> Self {
68        Self::Other(err.into())
69    }
70
71    /// Returns `true` if the error is out of gas.
72    pub fn is_oog(&self) -> bool {
73        matches!(self, Self::OutOfGas)
74    }
75}
76
77impl core::error::Error for PrecompileError {}
78
79impl fmt::Display for PrecompileError {
80    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81        let s = match self {
82            Self::OutOfGas => "out of gas",
83            Self::Blake2WrongLength => "wrong input length for blake2",
84            Self::Blake2WrongFinalIndicatorFlag => "wrong final indicator flag for blake2",
85            Self::ModexpExpOverflow => "modexp exp overflow",
86            Self::ModexpBaseOverflow => "modexp base overflow",
87            Self::ModexpModOverflow => "modexp mod overflow",
88            Self::Bn128FieldPointNotAMember => "field point not a member of bn128 curve",
89            Self::Bn128AffineGFailedToCreate => "failed to create affine g point for bn128 curve",
90            Self::Bn128PairLength => "bn128 invalid pair length",
91            Self::BlobInvalidInputLength => "invalid blob input length",
92            Self::BlobMismatchedVersion => "mismatched blob version",
93            Self::BlobVerifyKzgProofFailed => "verifying blob kzg proof failed",
94            Self::Fatal(s) => s,
95            Self::Other(s) => s,
96        };
97        f.write_str(s)
98    }
99}