foundry_evm_traces/decoder/
precompiles.rs1use crate::{CallTrace, DecodedCallData};
2use alloy_primitives::{hex, B256, U256};
3use alloy_sol_types::{abi, sol, SolCall};
4use foundry_evm_core::precompiles::{
5 BLAKE_2F, EC_ADD, EC_MUL, EC_PAIRING, EC_RECOVER, IDENTITY, MOD_EXP, POINT_EVALUATION,
6 RIPEMD_160, SHA_256,
7};
8use itertools::Itertools;
9use revm_inspectors::tracing::types::DecodedCallTrace;
10
11sol! {
12interface Precompiles {
17 struct EcPairingInput {
18 uint256 x1;
19 uint256 y1;
20 uint256 x2;
21 uint256 y2;
22 uint256 x3;
23 uint256 y3;
24 }
25
26 function ecrecover(bytes32 hash, uint8 v, uint256 r, uint256 s) returns (address publicAddress);
27 function sha256(bytes data) returns (bytes32 hash);
28 function ripemd(bytes data) returns (bytes20 hash);
29 function identity(bytes data) returns (bytes data);
30 function modexp(uint256 Bsize, uint256 Esize, uint256 Msize, bytes B, bytes E, bytes M) returns (bytes value);
31 function ecadd(uint256 x1, uint256 y1, uint256 x2, uint256 y2) returns (uint256 x, uint256 y);
32 function ecmul(uint256 x1, uint256 y1, uint256 s) returns (uint256 x, uint256 y);
33 function ecpairing(EcPairingInput[] input) returns (bool success);
34 function blake2f(uint32 rounds, uint64[8] h, uint64[16] m, uint64[2] t, bool f) returns (uint64[8] h);
35 function pointEvaluation(bytes32 versionedHash, bytes32 z, bytes32 y, bytes1[48] commitment, bytes1[48] proof) returns (bytes value);
36}
37}
38use Precompiles::*;
39
40macro_rules! tri {
41 ($e:expr) => {
42 match $e {
43 Ok(x) => x,
44 Err(_) => return None,
45 }
46 };
47}
48
49pub(super) fn decode(trace: &CallTrace, _chain_id: u64) -> Option<DecodedCallTrace> {
51 if !trace.address[..19].iter().all(|&x| x == 0) {
52 return None;
53 }
54
55 let data = &trace.data;
56
57 let (signature, args) = match trace.address {
58 EC_RECOVER => {
59 let (sig, ecrecoverCall { hash, v, r, s }) = tri!(abi_decode_call(data));
60 (sig, vec![hash.to_string(), v.to_string(), r.to_string(), s.to_string()])
61 }
62 SHA_256 => (sha256Call::SIGNATURE, vec![data.to_string()]),
63 RIPEMD_160 => (ripemdCall::SIGNATURE, vec![data.to_string()]),
64 IDENTITY => (identityCall::SIGNATURE, vec![data.to_string()]),
65 MOD_EXP => (modexpCall::SIGNATURE, tri!(decode_modexp(data))),
66 EC_ADD => {
67 let (sig, ecaddCall { x1, y1, x2, y2 }) = tri!(abi_decode_call(data));
68 (sig, vec![x1.to_string(), y1.to_string(), x2.to_string(), y2.to_string()])
69 }
70 EC_MUL => {
71 let (sig, ecmulCall { x1, y1, s }) = tri!(abi_decode_call(data));
72 (sig, vec![x1.to_string(), y1.to_string(), s.to_string()])
73 }
74 EC_PAIRING => (ecpairingCall::SIGNATURE, tri!(decode_ecpairing(data))),
75 BLAKE_2F => (blake2fCall::SIGNATURE, tri!(decode_blake2f(data))),
76 POINT_EVALUATION => (pointEvaluationCall::SIGNATURE, tri!(decode_kzg(data))),
77 _ => return None,
78 };
79
80 Some(DecodedCallTrace {
81 label: Some("PRECOMPILES".to_string()),
82 call_data: Some(DecodedCallData { signature: signature.to_string(), args }),
83 return_data: None,
85 })
86}
87
88fn decode_modexp(data: &[u8]) -> alloy_sol_types::Result<Vec<String>> {
92 let mut decoder = abi::Decoder::new(data, false);
93 let b_size = decoder.take_offset()?;
94 let e_size = decoder.take_offset()?;
95 let m_size = decoder.take_offset()?;
96 let b = decoder.take_slice_unchecked(b_size)?;
97 let e = decoder.take_slice_unchecked(e_size)?;
98 let m = decoder.take_slice_unchecked(m_size)?;
99 Ok(vec![
100 b_size.to_string(),
101 e_size.to_string(),
102 m_size.to_string(),
103 hex::encode_prefixed(b),
104 hex::encode_prefixed(e),
105 hex::encode_prefixed(m),
106 ])
107}
108
109fn decode_ecpairing(data: &[u8]) -> alloy_sol_types::Result<Vec<String>> {
110 let mut decoder = abi::Decoder::new(data, false);
111 let mut values = Vec::new();
112 let mut tmp = <[&B256; 6]>::default();
114 while !decoder.is_empty() {
115 for tmp in &mut tmp {
116 *tmp = decoder.take_word()?;
117 }
118 values.push(iter_to_string(tmp.iter().map(|x| U256::from_be_bytes(x.0))));
119 }
120 Ok(values)
121}
122
123fn decode_blake2f<'a>(data: &'a [u8]) -> alloy_sol_types::Result<Vec<String>> {
124 let mut decoder = abi::Decoder::new(data, false);
125 let rounds = u32::from_be_bytes(decoder.take_slice_unchecked(4)?.try_into().unwrap());
126 let u64_le_list =
127 |x: &'a [u8]| x.chunks_exact(8).map(|x| u64::from_le_bytes(x.try_into().unwrap()));
128 let h = u64_le_list(decoder.take_slice_unchecked(64)?);
129 let m = u64_le_list(decoder.take_slice_unchecked(128)?);
130 let t = u64_le_list(decoder.take_slice_unchecked(16)?);
131 let f = decoder.take_slice_unchecked(1)?[0];
132 Ok(vec![
133 rounds.to_string(),
134 iter_to_string(h),
135 iter_to_string(m),
136 iter_to_string(t),
137 f.to_string(),
138 ])
139}
140
141fn decode_kzg(data: &[u8]) -> alloy_sol_types::Result<Vec<String>> {
142 let mut decoder = abi::Decoder::new(data, false);
143 let versioned_hash = decoder.take_word()?;
144 let z = decoder.take_word()?;
145 let y = decoder.take_word()?;
146 let commitment = decoder.take_slice_unchecked(48)?;
147 let proof = decoder.take_slice_unchecked(48)?;
148 Ok(vec![
149 versioned_hash.to_string(),
150 z.to_string(),
151 y.to_string(),
152 hex::encode_prefixed(commitment),
153 hex::encode_prefixed(proof),
154 ])
155}
156
157fn abi_decode_call<T: SolCall>(data: &[u8]) -> alloy_sol_types::Result<(&'static str, T)> {
158 Ok((T::SIGNATURE, T::abi_decode_raw(data, false)?))
160}
161
162fn iter_to_string<I: Iterator<Item = T>, T: std::fmt::Display>(iter: I) -> String {
163 format!("[{}]", iter.format(", "))
164}
165
166#[cfg(test)]
167mod tests {
168 use super::*;
169 use alloy_primitives::hex;
170
171 #[test]
172 fn ecpairing() {
173 let data = hex!(
175 "
176 26bbb723f965460ca7282cd75f0e3e7c67b15817f7cee60856b394936ed02917
177 0fbe873ac672168143a91535450bab6c412dce8dc8b66a88f2da6e245f9282df
178 13cd4f0451538ece5014fe6688b197aefcc611a5c6a7c319f834f2188ba04b08
179 126ff07e81490a1b6ae92b2d9e700c8e23e9d5c7f6ab857027213819a6c9ae7d
180 04183624c9858a56c54deb237c26cb4355bc2551312004e65fc5b299440b15a3
181 2e4b11aa549ad6c667057b18be4f4437fda92f018a59430ebb992fa3462c9ca1
182 2d4d9aa7e302d9df41749d5507949d05dbea33fbb16c643b22f599a2be6df2e2
183 14bedd503c37ceb061d8ec60209fe345ce89830a19230301f076caff004d1926
184 0967032fcbf776d1afc985f88877f182d38480a653f2decaa9794cbc3bf3060c
185 0e187847ad4c798374d0d6732bf501847dd68bc0e071241e0213bc7fc13db7ab
186 304cfbd1e08a704a99f5e847d93f8c3caafddec46b7a0d379da69a4d112346a7
187 1739c1b1a457a8c7313123d24d2f9192f896b7c63eea05a9d57f06547ad0cec8
188 001d6fedb032f70e377635238e0563f131670001f6abf439adb3a9d5d52073c6
189 1889afe91e4e367f898a7fcd6464e5ca4e822fe169bccb624f6aeb87e4d060bc
190 198e9393920d483a7260bfb731fb5d25f1aa493335a9e71297e485b7aef312c2
191 1800deef121f1e76426a00665e5c4479674322d4f75edadd46debd5cd992f6ed
192 090689d0585ff075ec9e99ad690c3395bc4b313370b38ef355acdadcd122975b
193 12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa
194 2dde6d7baf0bfa09329ec8d44c38282f5bf7f9ead1914edd7dcaebb498c84519
195 0c359f868a85c6e6c1ea819cfab4a867501a3688324d74df1fe76556558b1937
196 29f41c6e0e30802e2749bfb0729810876f3423e6f24829ad3e30adb1934f1c8a
197 030e7a5f70bb5daa6e18d80d6d447e772efb0bb7fb9d0ffcd54fc5a48af1286d
198 0ea726b117e48cda8bce2349405f006a84cdd3dcfba12efc990df25970a27b6d
199 30364cd4f8a293b1a04f0153548d3e01baad091c69097ca4e9f26be63e4095b5
200 "
201 );
202 let decoded = decode_ecpairing(&data).unwrap();
203 assert_eq!(decoded.len(), 4);
205 }
206}