-
Notifications
You must be signed in to change notification settings - Fork 97
Beacon Kit Verifier #612
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
dharjeezy
wants to merge
17
commits into
main
Choose a base branch
from
dami/beacon-kit-verifier
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Beacon Kit Verifier #612
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
dea2d55
beacon kit verifier
dharjeezy 7217aac
all encompassing beacon kit
dharjeezy c722d7d
beacon kit prover
dharjeezy 30a098b
initial prover work
dharjeezy 9d19052
ismp beacon kit
dharjeezy 27f5d73
tesseract beaconkit
dharjeezy 70c4639
address concerns, ensure isolated passes
dharjeezy 3406eda
tesseract integration test, and resolve concerns
dharjeezy 923ad72
try running CI
dharjeezy 164ca05
Merge branch 'main' of github.com:polytope-labs/hyperbridge into dami…
dharjeezy 1157755
skip tesseract beacon kit test
dharjeezy c3793b9
use beacon api for cometbft prover
dharjeezy 0f20242
remove redundant url
dharjeezy fd07c6e
Merge branch 'main' of github.com:polytope-labs/hyperbridge into dami…
dharjeezy 643c826
cargo lock update
dharjeezy 497b047
beacon kit api client
dharjeezy ec3f121
add subtle storage pallet for beacon kit chains
dharjeezy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| [package] | ||
| name = "beacon-kit-verifier" | ||
| version = "0.1.1" | ||
| edition = "2021" | ||
| authors = ["Polytope Labs"] | ||
| publish = false | ||
|
|
||
| [dependencies] | ||
| hex-literal = { workspace = true } | ||
| sync-committee-primitives = { workspace = true, default-features = false } | ||
| bsc-verifier = { workspace = true, default-features = false } | ||
| log = { workspace = true, default-features = false } | ||
| ssz-rs = { git = "https://github.com/polytope-labs/ssz-rs", branch = "main", default-features = false } | ||
| bls = { workspace = true } | ||
| codec = { workspace = true, features = ["derive"] } | ||
| primitive-types = { workspace = true, features = [ | ||
| "serde_no_std", | ||
| "impl-codec", | ||
| ] } | ||
| thiserror = { workspace = true} | ||
|
|
||
| [features] | ||
| default = ["std"] | ||
| std = [ | ||
| "ssz-rs/std", | ||
| "ssz-rs/default", | ||
| "ssz-rs/serde", | ||
| 'codec/std', | ||
| "log/std", | ||
| "primitive-types/std", | ||
| "sync-committee-primitives/std", | ||
| "log/std", | ||
| "bls/std", | ||
| "bsc-verifier/std" | ||
| ] | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| #![cfg_attr(not(feature = "std"), no_std)] | ||
|
|
||
| use thiserror::Error; | ||
|
|
||
| #[derive(Debug, Error)] | ||
| pub enum BeaconKitError { | ||
| #[error("Not enough signers to meet consensus threshold")] | ||
| InsufficientSigners, | ||
| #[error("Update contains a signer not present in the trusted validator set")] | ||
| UnknownSigner, | ||
| #[error("Failed to compute domain")] | ||
| DomainComputationFailed, | ||
| #[error("Failed to compute signing root")] | ||
| SigningRootComputationFailed, | ||
| #[error("Failed to verify aggregate BLS signature")] | ||
| SignatureVerificationFailed, | ||
| #[error("Failed to hash execution payload")] | ||
| ExecutionPayloadHashFailed, | ||
| #[error("Invalid Execution Payload Merkle Proof")] | ||
| InvalidExecutionPayloadProof, | ||
| #[error("Failed to hash validator set")] | ||
| ValidatorSetHashFailed, | ||
| #[error("Invalid Validator Set Merkle Proof")] | ||
| InvalidValidatorSetProof, | ||
| #[error("Failed to create SSZ List from validators")] | ||
| SszListCreationFailure, | ||
| #[error("Invalid public key provided")] | ||
| InvalidPublicKey, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| // Copyright (C) 2022 Polytope Labs. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // 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(feature = "std"), no_std)] | ||
| extern crate alloc; | ||
| extern crate core; | ||
|
|
||
| mod error; | ||
| pub mod primitives; | ||
|
|
||
| use crate::error::BeaconKitError; | ||
| use alloc::vec::Vec; | ||
| use bsc_verifier::aggregate_public_keys; | ||
| use primitive_types::H256; | ||
| use primitives::{BeaconKitUpdate, Config, VerificationResult}; | ||
| use ssz_rs::{prelude::*, Merkleized}; | ||
| use sync_committee_primitives::{ | ||
| constants::{BlsPublicKey, Root, VALIDATOR_REGISTRY_LIMIT}, | ||
| domains::DomainType, | ||
| util::{compute_domain, compute_signing_root}, | ||
| }; | ||
|
|
||
| /// Verifies a Beacon Kit light client update | ||
| pub fn verify_beacon_kit_header<C: Config>( | ||
| current_validators: &Vec<BlsPublicKey>, | ||
| mut update: BeaconKitUpdate, | ||
| ) -> Result<VerificationResult, BeaconKitError> { | ||
| let signers_set_len = update.signers.len(); | ||
| let total_validators = current_validators.len(); | ||
| let threshold = (2 * total_validators / 3) + 1; | ||
|
|
||
| if signers_set_len < threshold { | ||
| return Err(BeaconKitError::InsufficientSigners); | ||
| } | ||
|
|
||
| if update.signers.iter().any(|signer| !current_validators.contains(signer)) { | ||
| return Err(BeaconKitError::UnknownSigner); | ||
| } | ||
|
|
||
| let domain = compute_domain( | ||
| DomainType::BeaconProposer, | ||
| Some(C::BEACON_KIT_FORK_VERSION), | ||
| Some(Root::from_bytes(C::GENESIS_VALIDATORS_ROOT)), | ||
| C::GENESIS_FORK_VERSION, | ||
| ) | ||
| .map_err(|_| BeaconKitError::DomainComputationFailed)?; | ||
|
|
||
| let mut header = update.beacon_header.clone(); | ||
| let signing_root = compute_signing_root(&mut header, domain) | ||
| .map_err(|_| BeaconKitError::SigningRootComputationFailed)?; | ||
|
|
||
| let aggregate_pubkey = aggregate_public_keys(&update.signers); | ||
|
|
||
| let verify_sig = bls::verify( | ||
| &aggregate_pubkey, | ||
| &signing_root.as_bytes().to_vec(), | ||
| &update.signature.to_vec(), | ||
| &bls::DST_ETHEREUM.as_bytes().to_vec(), | ||
| ); | ||
|
|
||
| if !verify_sig { | ||
| return Err(BeaconKitError::SignatureVerificationFailed)? | ||
| } | ||
|
|
||
| let execution_payload_root = update | ||
| .execution_payload | ||
| .hash_tree_root() | ||
| .map_err(|_| BeaconKitError::ExecutionPayloadHashFailed)?; | ||
| let execution_payload_proof_nodes: Vec<Node> = update | ||
| .execution_payload_proof | ||
| .iter() | ||
| .map(|byte| Node::from_bytes(byte.as_ref().try_into().expect("Infallible"))) | ||
| .collect(); | ||
|
|
||
| let is_payload_valid = is_valid_merkle_branch( | ||
| &execution_payload_root, | ||
| execution_payload_proof_nodes.iter(), | ||
| C::EXECTION_PAYLOAD_INDEX_LOG2, | ||
| C::EXECUTION_PAYLOAD_INDEX, | ||
| &update.beacon_header.state_root, | ||
| ); | ||
|
|
||
| if !is_payload_valid { | ||
| return Err(BeaconKitError::InvalidExecutionPayloadProof)? | ||
| } | ||
|
|
||
| let next_validators = if let Some(validator_proof) = update.validator_set_proof { | ||
| let mut validators_list = List::<BlsPublicKey, VALIDATOR_REGISTRY_LIMIT>::try_from( | ||
| validator_proof.validators.clone(), | ||
| ) | ||
| .map_err(|_| BeaconKitError::SszListCreationFailure)?; | ||
|
|
||
| let validators_root = validators_list | ||
| .hash_tree_root() | ||
| .map_err(|_| BeaconKitError::ValidatorSetHashFailed)?; | ||
| let validator_proof_nodes: Vec<Node> = validator_proof | ||
| .proof | ||
| .iter() | ||
| .map(|byte| Node::from_bytes(byte.as_ref().try_into().expect("Infallible"))) | ||
| .collect(); | ||
|
|
||
| let is_validator_proof_valid = is_valid_merkle_branch( | ||
| &validators_root, | ||
| validator_proof_nodes.iter(), | ||
| C::VALIDATOR_REGISTRY_INDEX_LOG2, | ||
| C::VALIDATOR_REGSITRY_INDEX, | ||
| &update.beacon_header.state_root, | ||
| ); | ||
|
|
||
| if !is_validator_proof_valid { | ||
| return Err(BeaconKitError::InvalidValidatorSetProof)?; | ||
| } | ||
|
|
||
| Some(validator_proof.validators) | ||
| } else { | ||
| None | ||
| }; | ||
|
|
||
| Ok(VerificationResult { | ||
| hash: H256::from_slice(signing_root.as_bytes()), | ||
| finalized_header: update.beacon_header, | ||
| next_validators, | ||
| }) | ||
| } |
105 changes: 105 additions & 0 deletions
105
modules/consensus/beacon-kit-verifier/src/primitives.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| // Copyright (C) 2022 Polytope Labs. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // 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(feature = "std"), no_std)] | ||
|
|
||
| use alloc::vec::Vec; | ||
| use codec::{Decode, Encode}; | ||
| use hex_literal::hex; | ||
| use primitive_types::H256; | ||
| use ssz_rs::prelude::*; | ||
| use sync_committee_primitives::{ | ||
| consensus_types::{BeaconBlockHeader, ExecutionPayloadHeader}, | ||
| constants::{BlsPublicKey, BlsSignature}, | ||
| }; | ||
|
|
||
| const BYTES_PER_LOGS_BLOOM: usize = 256; | ||
| const MAX_EXTRA_DATA_BYTES: usize = 32; | ||
|
|
||
| pub trait Config: Clone + Send + Sync { | ||
| const EXECUTION_PAYLOAD_INDEX: usize; | ||
| const EXECTION_PAYLOAD_INDEX_LOG2: usize; | ||
| const VALIDATOR_REGSITRY_INDEX: usize; | ||
| const VALIDATOR_REGISTRY_INDEX_LOG2: usize; | ||
| const GENESIS_VALIDATORS_ROOT: [u8; 32]; | ||
| const GENESIS_FORK_VERSION: [u8; 4]; | ||
| const BEACON_KIT_FORK_VERSION: [u8; 4]; | ||
| } | ||
|
|
||
| /// Config for the Berachain Bepolia Testnet | ||
| #[derive(Clone, Default)] | ||
| pub struct BepoliaConfig; | ||
|
|
||
| impl Config for BepoliaConfig { | ||
| const EXECUTION_PAYLOAD_INDEX: usize = 25; | ||
| const EXECTION_PAYLOAD_INDEX_LOG2: usize = 5; | ||
| const VALIDATOR_REGSITRY_INDEX: usize = 11; | ||
| const VALIDATOR_REGISTRY_INDEX_LOG2: usize = 5; | ||
| const GENESIS_VALIDATORS_ROOT: [u8; 32] = | ||
| hex!("3cbcf75b02fe4750c592f1c1ff8b5500a74406f80f038e9ff250e2e294c5615e"); | ||
| const GENESIS_FORK_VERSION: [u8; 4] = hex!("04000000"); | ||
| const BEACON_KIT_FORK_VERSION: [u8; 4] = hex!("05010000"); | ||
| } | ||
|
|
||
| /// Config for the Berachain mainnet | ||
| #[derive(Clone, Default)] | ||
| pub struct BerachainConfig; | ||
|
|
||
| impl Config for BerachainConfig { | ||
| const EXECUTION_PAYLOAD_INDEX: usize = 25; | ||
| const EXECTION_PAYLOAD_INDEX_LOG2: usize = 5; | ||
| const VALIDATOR_REGSITRY_INDEX: usize = 11; | ||
| const VALIDATOR_REGISTRY_INDEX_LOG2: usize = 5; | ||
| const GENESIS_VALIDATORS_ROOT: [u8; 32] = | ||
| hex!("df609e3b062842c6425ff716aec2d2092c46455d9b2e1a2c9e32c6ba63ff0bda"); | ||
| const GENESIS_FORK_VERSION: [u8; 4] = hex!("04000000"); | ||
| const BEACON_KIT_FORK_VERSION: [u8; 4] = hex!("05010000"); | ||
| } | ||
|
|
||
| /// Represents a light client update for Beacon Kit consensus | ||
| #[derive(Debug, Clone, Encode, Decode)] | ||
| pub struct BeaconKitUpdate { | ||
| /// The header of the Beacon Block being verified | ||
| pub beacon_header: BeaconBlockHeader, | ||
| /// The aggregate BLS signature covering the header | ||
| pub signature: BlsSignature, | ||
| /// The public keys of the validators that signed this update | ||
| pub signers: Vec<BlsPublicKey>, | ||
| /// The execution payload header to be verified against the Beacon state | ||
| pub execution_payload: ExecutionPayloadHeader<BYTES_PER_LOGS_BLOOM, MAX_EXTRA_DATA_BYTES>, | ||
| /// The SSZ Merkle proof of the execution payload | ||
| pub execution_payload_proof: Vec<H256>, | ||
| /// Optional proof for the validator set, for auhtority set rotation | ||
| pub validator_set_proof: Option<ValidatorSetProof>, | ||
| } | ||
|
|
||
| /// Proof data for verifying the next validator set | ||
| #[derive(Debug, Clone, Encode, Decode)] | ||
| pub struct ValidatorSetProof { | ||
| /// The list of validators in the new set | ||
| pub validators: Vec<BlsPublicKey>, | ||
| /// The SSZ Merkle proof of the validator registry | ||
| pub proof: Vec<H256>, | ||
| } | ||
|
|
||
| /// The result of a successful light client verification | ||
| #[derive(Debug, Clone, Encode, Decode)] | ||
| pub struct VerificationResult { | ||
| /// The hash of the verified block (signing root) | ||
| pub hash: H256, | ||
| /// The verified beacon block header | ||
| pub finalized_header: BeaconBlockHeader, | ||
| /// The next authority set for a verified rotation | ||
| pub next_validators: Option<Vec<BlsPublicKey>>, | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.