revm_precompile/
interface.rs1use core::fmt;
4use primitives::Bytes;
5use std::string::String;
6
7pub type PrecompileResult = Result<PrecompileOutput, PrecompileError>;
11
12#[derive(Clone, Debug, PartialEq, Eq, Hash)]
14pub struct PrecompileOutput {
15 pub gas_used: u64,
17 pub bytes: Bytes,
19}
20
21impl PrecompileOutput {
22 pub fn new(gas_used: u64, bytes: Bytes) -> Self {
24 Self { gas_used, bytes }
25 }
26}
27
28pub type PrecompileFn = fn(&[u8], u64) -> PrecompileResult;
30
31#[derive(Clone, Debug, PartialEq, Eq, Hash)]
33pub enum PrecompileError {
34 OutOfGas,
36 Blake2WrongLength,
38 Blake2WrongFinalIndicatorFlag,
40 ModexpExpOverflow,
42 ModexpBaseOverflow,
44 ModexpModOverflow,
46 ModexpEip7823LimitSize,
48 Bn128FieldPointNotAMember,
50 Bn128AffineGFailedToCreate,
52 Bn128PairLength,
54 BlobInvalidInputLength,
57 BlobMismatchedVersion,
59 BlobVerifyKzgProofFailed,
61 Fatal(String),
63 Other(String),
65}
66
67impl PrecompileError {
68 pub fn other(err: impl Into<String>) -> Self {
70 Self::Other(err.into())
71 }
72
73 pub fn is_oog(&self) -> bool {
75 matches!(self, Self::OutOfGas)
76 }
77}
78
79impl core::error::Error for PrecompileError {}
80
81impl fmt::Display for PrecompileError {
82 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83 let s = match self {
84 Self::OutOfGas => "out of gas",
85 Self::Blake2WrongLength => "wrong input length for blake2",
86 Self::Blake2WrongFinalIndicatorFlag => "wrong final indicator flag for blake2",
87 Self::ModexpExpOverflow => "modexp exp overflow",
88 Self::ModexpBaseOverflow => "modexp base overflow",
89 Self::ModexpModOverflow => "modexp mod overflow",
90 Self::ModexpEip7823LimitSize => "Modexp limit all input sizes.",
91 Self::Bn128FieldPointNotAMember => "field point not a member of bn128 curve",
92 Self::Bn128AffineGFailedToCreate => "failed to create affine g point for bn128 curve",
93 Self::Bn128PairLength => "bn128 invalid pair length",
94 Self::BlobInvalidInputLength => "invalid blob input length",
95 Self::BlobMismatchedVersion => "mismatched blob version",
96 Self::BlobVerifyKzgProofFailed => "verifying blob kzg proof failed",
97 Self::Fatal(s) => s,
98 Self::Other(s) => s,
99 };
100 f.write_str(s)
101 }
102}