Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions crates/common/consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 17 additions & 8 deletions crates/common/consensus/src/electra/beacon_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,6 +30,7 @@ use super::{
beacon_block_body::BeaconBlockBody,
execution_payload::ExecutionPayload,
execution_payload_header::ExecutionPayloadHeader,
zkvm_types::ValidatorRegistryLimit,
};
use crate::{
attestation::Attestation,
Expand Down Expand Up @@ -100,7 +101,7 @@ pub mod quoted_u8_var_list {
use super::*;

pub fn serialize<S>(
value: &VariableList<u8, U1099511627776>,
value: &VariableList<u8, ValidatorRegistryLimit>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
Expand All @@ -112,7 +113,7 @@ pub mod quoted_u8_var_list {

pub fn deserialize<'de, D>(
deserializer: D,
) -> Result<VariableList<u8, U1099511627776>, D::Error>
) -> Result<VariableList<u8, ValidatorRegistryLimit>, D::Error>
where
D: Deserializer<'de>,
{
Expand All @@ -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
Expand All @@ -151,9 +160,9 @@ pub struct BeaconState {
pub eth1_deposit_index: u64,

// Registry
pub validators: VariableList<Validator, U1099511627776>,
pub validators: VariableList<Validator, ValidatorRegistryLimit>,
#[serde(with = "quoted_u64_var_list")]
pub balances: VariableList<u64, U1099511627776>,
pub balances: VariableList<u64, ValidatorRegistryLimit>,

// Randomness
pub randao_mixes: FixedVector<B256, U65536>,
Expand All @@ -164,9 +173,9 @@ pub struct BeaconState {

// Participation
#[serde(with = "quoted_u8_var_list")]
pub previous_epoch_participation: VariableList<u8, U1099511627776>,
pub previous_epoch_participation: VariableList<u8, ValidatorRegistryLimit>,
#[serde(with = "quoted_u8_var_list")]
pub current_epoch_participation: VariableList<u8, U1099511627776>,
pub current_epoch_participation: VariableList<u8, ValidatorRegistryLimit>,

// Finality
pub justification_bits: BitVector<U4>,
Expand All @@ -176,7 +185,7 @@ pub struct BeaconState {

// Inactivity
#[serde(with = "quoted_u64_var_list")]
pub inactivity_scores: VariableList<u64, U1099511627776>,
pub inactivity_scores: VariableList<u64, ValidatorRegistryLimit>,

// Sync
pub current_sync_committee: Arc<SyncCommittee>,
Expand Down
1 change: 1 addition & 0 deletions crates/common/consensus/src/electra/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
10 changes: 10 additions & 0 deletions crates/common/consensus/src/electra/zkvm_types.rs
Original file line number Diff line number Diff line change
@@ -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;