diff --git a/crates/common/consensus/Cargo.toml b/crates/common/consensus/Cargo.toml index 7814548ec..a372c642e 100644 --- a/crates/common/consensus/Cargo.toml +++ b/crates/common/consensus/Cargo.toml @@ -9,6 +9,9 @@ repository.workspace = true rust-version.workspace = true version.workspace = true +[features] +zkvm = [] + [dependencies] alloy-consensus.workspace = true alloy-primitives.workspace = true diff --git a/crates/common/consensus/src/electra/beacon_state.rs b/crates/common/consensus/src/electra/beacon_state.rs index 3d0d44312..6926cbc0d 100644 --- a/crates/common/consensus/src/electra/beacon_state.rs +++ b/crates/common/consensus/src/electra/beacon_state.rs @@ -20,7 +20,7 @@ use ssz_derive::{Decode, Encode}; use ssz_types::{ BitVector, FixedVector, VariableList, serde_utils::{quoted_u64_fixed_vec, quoted_u64_var_list}, - typenum::{U4, U2048, U8192, U65536, U262144, U16777216, U134217728, U1099511627776}, + typenum::{U4, U2048, U8192, U65536, U262144, U16777216, U134217728}, }; use tree_hash::TreeHash; use tree_hash_derive::TreeHash; @@ -30,6 +30,7 @@ use super::{ beacon_block_body::BeaconBlockBody, execution_payload::ExecutionPayload, execution_payload_header::ExecutionPayloadHeader, + zkvm_types::ValidatorRegistryLimit, }; use crate::{ attestation::Attestation, @@ -100,7 +101,7 @@ pub mod quoted_u8_var_list { use super::*; pub fn serialize( - value: &VariableList, + value: &VariableList, serializer: S, ) -> Result where @@ -112,7 +113,7 @@ pub mod quoted_u8_var_list { pub fn deserialize<'de, D>( deserializer: D, - ) -> Result, D::Error> + ) -> Result, D::Error> where D: Deserializer<'de>, { @@ -127,6 +128,14 @@ pub mod quoted_u8_var_list { } } +/// The BeaconState contains some "zkvm" features that addresses where 32-bit zkVMs would fail +/// on constructing a VariableList larger than 2^32 size (i.e. 2^40). When "zkvm" feature +/// is enabled, it would construct the BeaconState with 2^29 list instead. +/// +/// This "zkvm" feature needs to be used with the modified ssz_types crate at +/// https://github.com/ReamLabs/ssz_types/tree/magic-extended-list +/// where the crate would detect 2^29 as a magic number when computing the root hash, +/// and it will compute as a 2^40 list root instead. #[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Encode, Decode, TreeHash)] pub struct BeaconState { // Versioning @@ -151,9 +160,9 @@ pub struct BeaconState { pub eth1_deposit_index: u64, // Registry - pub validators: VariableList, + pub validators: VariableList, #[serde(with = "quoted_u64_var_list")] - pub balances: VariableList, + pub balances: VariableList, // Randomness pub randao_mixes: FixedVector, @@ -164,9 +173,9 @@ pub struct BeaconState { // Participation #[serde(with = "quoted_u8_var_list")] - pub previous_epoch_participation: VariableList, + pub previous_epoch_participation: VariableList, #[serde(with = "quoted_u8_var_list")] - pub current_epoch_participation: VariableList, + pub current_epoch_participation: VariableList, // Finality pub justification_bits: BitVector, @@ -176,7 +185,7 @@ pub struct BeaconState { // Inactivity #[serde(with = "quoted_u64_var_list")] - pub inactivity_scores: VariableList, + pub inactivity_scores: VariableList, // Sync pub current_sync_committee: Arc, diff --git a/crates/common/consensus/src/electra/mod.rs b/crates/common/consensus/src/electra/mod.rs index 9ef497d76..8bc5c908a 100644 --- a/crates/common/consensus/src/electra/mod.rs +++ b/crates/common/consensus/src/electra/mod.rs @@ -5,3 +5,4 @@ pub mod blinded_beacon_block; pub mod blinded_beacon_block_body; pub mod execution_payload; pub mod execution_payload_header; +pub mod zkvm_types; diff --git a/crates/common/consensus/src/electra/zkvm_types.rs b/crates/common/consensus/src/electra/zkvm_types.rs new file mode 100644 index 000000000..e673b6b01 --- /dev/null +++ b/crates/common/consensus/src/electra/zkvm_types.rs @@ -0,0 +1,10 @@ +#[cfg(feature = "zkvm")] +use ssz_types::typenum::U536870912; +#[cfg(not(feature = "zkvm"))] +use ssz_types::typenum::U1099511627776; + +// VALIDATOR_REGISTRY_LIMIT +#[cfg(not(feature = "zkvm"))] +pub type ValidatorRegistryLimit = U1099511627776; +#[cfg(feature = "zkvm")] +pub type ValidatorRegistryLimit = U536870912;