1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Copyright 2025 RISC Zero, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![cfg_attr(not(doctest), doc = include_str!("../README.md"))]
#![deny(rustdoc::broken_intra_doc_links)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

/// Re-export of [alloy], provided to ensure that the correct version of the types used in the
/// public API are available in case multiple versions of [alloy] are in use.
///
/// Because [alloy] is a v0.x crate, it is not covered under the semver policy of this crate.
#[cfg(feature = "host")]
pub use alloy;

use ::serde::{Deserialize, Serialize};
use alloy_primitives::{uint, BlockNumber, Bloom, Log, Sealable, Sealed, B256, U256};
use alloy_rpc_types::{Filter, FilteredParams};
use alloy_sol_types::SolValue;
use config::ChainSpec;
use revm::{
    primitives::{BlockEnv, CfgEnvWithHandlerCfg, SpecId},
    Database as RevmDatabase,
};

pub mod account;
pub mod beacon;
mod block;
pub mod config;
mod contract;
pub mod ethereum;
#[cfg(feature = "unstable-event")]
pub mod event;
#[cfg(feature = "unstable-history")]
pub mod history;
#[cfg(not(feature = "unstable-history"))]
mod history;
#[cfg(feature = "host")]
pub mod host;
mod merkle;
mod mpt;
pub mod serde;
mod state;
#[cfg(feature = "unstable-verifier")]
mod verifier;

pub use account::Account;
pub use beacon::BeaconInput;
pub use block::BlockInput;
pub use contract::{CallBuilder, Contract};
pub use mpt::MerkleTrie;
pub use state::{StateAccount, StateDb};

#[cfg(feature = "unstable-event")]
pub use event::Event;
#[cfg(feature = "unstable-history")]
pub use history::HistoryInput;
#[cfg(not(feature = "unstable-history"))]
pub(crate) use history::HistoryInput;
#[cfg(feature = "unstable-verifier")]
pub use verifier::SteelVerifier;

/// The serializable input to derive and validate an [EvmEnv] from.
#[non_exhaustive]
#[derive(Clone, Serialize, Deserialize)]
pub enum EvmInput<H> {
    /// Input committing to the corresponding execution block hash.
    Block(BlockInput<H>),
    /// Input committing to the corresponding Beacon Chain block root.
    Beacon(BeaconInput<H>),
    /// Input recursively committing to multiple Beacon Chain block root.
    History(HistoryInput<H>),
}

impl<H: EvmBlockHeader> EvmInput<H> {
    /// Converts the input into a [EvmEnv] for execution.
    ///
    /// This method verifies that the state matches the state root in the header and panics if not.
    #[inline]
    pub fn into_env(self) -> GuestEvmEnv<H> {
        match self {
            EvmInput::Block(input) => input.into_env(),
            EvmInput::Beacon(input) => input.into_env(),
            EvmInput::History(input) => input.into_env(),
        }
    }
}

/// A trait linking the block header to a commitment.
pub trait BlockHeaderCommit<H: EvmBlockHeader> {
    /// Creates a verifiable [Commitment] of the `header`.
    fn commit(self, header: &Sealed<H>, config_id: B256) -> Commitment;
}

/// A generalized input type consisting of a block-based input and a commitment wrapper.
///
/// The `commit` field provides a mechanism to generate a commitment to the block header
/// contained within the `input` field.
#[derive(Clone, Serialize, Deserialize)]
pub struct ComposeInput<H, C> {
    input: BlockInput<H>,
    commit: C,
}

impl<H: EvmBlockHeader, C: BlockHeaderCommit<H>> ComposeInput<H, C> {
    /// Creates a new composed input from a [BlockInput] and a [BlockHeaderCommit].
    pub const fn new(input: BlockInput<H>, commit: C) -> Self {
        Self { input, commit }
    }

    /// Disassembles this `ComposeInput`, returning the underlying input and commitment creator.
    pub fn into_parts(self) -> (BlockInput<H>, C) {
        (self.input, self.commit)
    }

    /// Converts the input into a [EvmEnv] for verifiable state access in the guest.
    pub fn into_env(self) -> GuestEvmEnv<H> {
        let mut env = self.input.into_env();
        env.commit = self.commit.commit(&env.header, env.commit.configID);

        env
    }
}

/// A database abstraction for the Steel EVM.
pub trait EvmDatabase: RevmDatabase {
    /// Retrieves all the logs matching the given [Filter].
    ///
    /// It returns an error, if the corresponding logs cannot be retrieved from DB.
    /// The filter must match the block hash corresponding to the DB, it will panic otherwise.
    fn logs(&mut self, filter: Filter) -> Result<Vec<Log>, <Self as RevmDatabase>::Error>;
}

/// Checks if a bloom filter matches the given filter parameters.
// TODO: Move to `event` once no longer unstable
#[allow(dead_code)]
#[inline]
pub(crate) fn matches_filter(bloom: Bloom, filter: &Filter) -> bool {
    FilteredParams::matches_address(bloom, &FilteredParams::address_filter(&filter.address))
        && FilteredParams::matches_topics(bloom, &FilteredParams::topics_filter(&filter.topics))
}

/// Alias for readability, do not make public.
pub(crate) type GuestEvmEnv<H> = EvmEnv<StateDb, H, Commitment>;

/// The environment to execute the contract calls in.
pub struct EvmEnv<D, H, C> {
    db: Option<D>,
    cfg_env: CfgEnvWithHandlerCfg,
    header: Sealed<H>,
    commit: C,
}

impl<D, H: EvmBlockHeader, C> EvmEnv<D, H, C> {
    /// Creates a new environment.
    ///
    /// It uses the default configuration for the latest specification.
    pub(crate) fn new(db: D, header: Sealed<H>, commit: C) -> Self {
        let cfg_env = CfgEnvWithHandlerCfg::new_with_spec_id(Default::default(), SpecId::LATEST);

        Self {
            db: Some(db),
            cfg_env,
            header,
            commit,
        }
    }

    /// Returns the sealed header of the environment.
    #[inline]
    pub fn header(&self) -> &Sealed<H> {
        &self.header
    }

    pub(crate) fn db(&self) -> &D {
        // safe unwrap: self cannot be borrowed without a DB
        self.db.as_ref().unwrap()
    }

    #[cfg(feature = "host")]
    pub(crate) fn db_mut(&mut self) -> &mut D {
        // safe unwrap: self cannot be borrowed without a DB
        self.db.as_mut().unwrap()
    }
}

impl<D, H: EvmBlockHeader> EvmEnv<D, H, Commitment> {
    /// Sets the chain ID and specification ID from the given chain spec.
    ///
    /// This will panic when there is no valid specification ID for the current block.
    pub fn with_chain_spec(mut self, chain_spec: &ChainSpec) -> Self {
        self.cfg_env.chain_id = chain_spec.chain_id();
        self.cfg_env.handler_cfg.spec_id = chain_spec
            .active_fork(self.header.number(), self.header.timestamp())
            .unwrap();
        self.commit.configID = chain_spec.digest();

        self
    }

    /// Returns the [Commitment] used to validate the environment.
    #[inline]
    pub fn commitment(&self) -> &Commitment {
        &self.commit
    }

    /// Consumes and returns the [Commitment] used to validate the environment.
    #[inline]
    pub fn into_commitment(self) -> Commitment {
        self.commit
    }
}

/// An EVM abstraction of a block header.
pub trait EvmBlockHeader: Sealable {
    /// Returns the hash of the parent block's header.
    fn parent_hash(&self) -> &B256;
    /// Returns the block number.
    fn number(&self) -> BlockNumber;
    /// Returns the block timestamp.
    fn timestamp(&self) -> u64;
    /// Returns the state root hash.
    fn state_root(&self) -> &B256;
    #[cfg(feature = "unstable-event")]
    /// Returns the receipts root hash of the block.
    fn receipts_root(&self) -> &B256;
    #[cfg(feature = "unstable-event")]
    /// Returns the logs bloom filter of the block
    fn logs_bloom(&self) -> &Bloom;

    /// Fills the EVM block environment with the header's data.
    fn fill_block_env(&self, blk_env: &mut BlockEnv);
}

// Keep everything in the Steel library private except the commitment.
mod private {
    alloy_sol_types::sol! {
        /// A Solidity struct representing a commitment used for validation.
        ///
        /// This struct is used to commit to a specific claim, such as the hash of an execution block
        /// or a beacon chain state. It includes a version, an identifier, the claim itself, and a
        /// configuration ID to ensure the commitment is valid for the intended network.
        #[derive(Default, PartialEq, Eq, Hash)]
        struct Commitment {
            /// Commitment ID.
            ///
            /// This ID combines the version and the actual identifier of the claim, such as the block number.
            uint256 id;
            /// The cryptographic digest of the commitment.
            ///
            /// This is the core of the commitment, representing the data being committed to,
            /// e.g., the hash of the execution block.
            bytes32 digest;
            /// The cryptographic digest of the network configuration.
            ///
            /// This ID ensures that the commitment is valid only for the specific network configuration
            /// it was created for.
            bytes32 configID;
        }
    }
}

pub use private::Commitment;

/// Version of a [`Commitment`].
#[repr(u16)]
#[derive(Debug, PartialEq, Eq)]
pub enum CommitmentVersion {
    /// Commitment to an execution block.
    Block,
    /// Commitment to a beacon chain state.
    Beacon,
}

impl Commitment {
    /// The size in bytes of the ABI-encoded commitment.
    pub const ABI_ENCODED_SIZE: usize = 3 * 32;

    /// Creates a new commitment.
    #[inline]
    pub const fn new(version: u16, id: u64, digest: B256, config_id: B256) -> Commitment {
        Self {
            id: Commitment::encode_id(id, version),
            digest,
            configID: config_id,
        }
    }

    /// Decodes the `id` field into the claim ID and the commitment version.
    #[inline]
    pub fn decode_id(&self) -> (U256, u16) {
        let decoded = self.id
            & uint!(0x0000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256);
        let version = (self.id.as_limbs()[3] >> 48) as u16;
        (decoded, version)
    }

    /// ABI-encodes the commitment.
    #[inline]
    pub fn abi_encode(&self) -> Vec<u8> {
        SolValue::abi_encode(self)
    }

    /// Encodes an ID and version into a single [U256] value.
    const fn encode_id(id: u64, version: u16) -> U256 {
        U256::from_limbs([id, 0, 0, (version as u64) << 48])
    }
}

impl std::fmt::Debug for Commitment {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let (id, version) = self.decode_id();
        f.debug_struct("Commitment")
            .field("version", &version)
            .field("id", &id)
            .field("digest", &self.digest)
            .field("configID", &self.configID)
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloy_primitives::B256;

    #[test]
    fn size() {
        let tests = vec![
            Commitment::default(),
            Commitment::new(
                u16::MAX,
                u64::MAX,
                B256::repeat_byte(0xFF),
                B256::repeat_byte(0xFF),
            ),
        ];
        for test in tests {
            assert_eq!(test.abi_encode().len(), Commitment::ABI_ENCODED_SIZE);
        }
    }

    #[test]
    fn versioned_id() {
        let tests = vec![(u64::MAX, u16::MAX), (u64::MAX, 0), (0, u16::MAX), (0, 0)];
        for test in tests {
            let commit = Commitment::new(test.1, test.0, B256::default(), B256::default());
            let (id, version) = commit.decode_id();
            assert_eq!((id.to(), version), test);
        }
    }
}