Skip to content

Commit

Permalink
feat(block): block signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
yair-starkware committed Dec 25, 2023
1 parent 849af44 commit 7d2dbbc
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
29 changes: 27 additions & 2 deletions src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ mod block_test;
use derive_more::Display;
use serde::{Deserialize, Serialize};

use crate::core::{ContractAddress, GlobalRoot};
use crate::hash::StarkHash;
use crate::core::{ContractAddress, GlobalRoot, SequencerPublicKey};
use crate::crypto::{verify_message_hash_signature, Signature};
use crate::hash::{poseidon_hash_array, StarkHash};
use crate::serde_utils::{BytesAsHex, PrefixedBytesAsHex};
use crate::transaction::{Transaction, TransactionHash, TransactionOutput};
use crate::StarknetApiError;

/// A block.
#[derive(Debug, Default, Clone, Eq, PartialEq, Deserialize, Serialize)]
Expand Down Expand Up @@ -135,3 +137,26 @@ impl From<GasPrice> for PrefixedBytesAsHex<16_usize> {
Debug, Default, Copy, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord,
)]
pub struct BlockTimestamp(pub u64);

/// The signature of a [Block](`crate::block::Block`), signed by the sequencer. The signed message
/// is defined as poseidon_hash(block_hash, state_diff_commitment).
#[derive(
Debug, Default, Copy, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord,
)]
pub struct BlockSignature(pub Signature);

/// Verifies that the the block header was signed by the expected sequencer.
pub fn verify_block_signature(
sequencer_pub_key: &SequencerPublicKey,
signature: &BlockSignature,
state_diff_commitment: &GlobalRoot,
block_hash: &BlockHash,
) -> Result<bool, StarknetApiError> {
let message_hash = poseidon_hash_array(&[block_hash.0, state_diff_commitment.0]);
verify_message_hash_signature(&message_hash.0, &signature.0, &sequencer_pub_key.0).map_err(
|err| StarknetApiError::BlockSignatureVerificationFailed {
block_hash: *block_hash,
error: err,
},
)
}
28 changes: 27 additions & 1 deletion src/block_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use crate::block::BlockNumber;
use super::verify_block_signature;
use crate::block::{BlockNumber, BlockSignature};
use crate::core::{GlobalRoot, SequencerPublicKey};
use crate::crypto::{Signature, PublicKey};
use crate::hash::StarkFelt;
use crate::{stark_felt, BlockHash};

#[test]
fn test_block_number_iteration() {
Expand All @@ -20,3 +25,24 @@ fn test_block_number_iteration() {

assert_eq!(expected, from_iter);
}

#[test]
fn block_signature_verification() {
let block_hash =
BlockHash(stark_felt!("0x7d5db04c5ca2aea828180dc441afb1580e3cee7547a3567ced3aa5bb8b273c0"));
let state_commitment = GlobalRoot(stark_felt!(
"0x64689c12248e1110af4b3af0e2b43cd51ad13e8855f10e37669e2a4baf919c6"
));
let signature = BlockSignature(Signature {
r: stark_felt!("0x1b382bbfd693011c9b7692bc932b23ed9c288deb27c8e75772e172abbe5950c"),
s: stark_felt!("0xbe4438085057e1a7c704a0da3b30f7b8340fe3d24c86772abfd24aa597e42"),
});
let sequencer_pub_key = SequencerPublicKey(PublicKey(stark_felt!(
"0x48253ff2c3bed7af18bde0b611b083b39445959102d4947c51c4db6aa4f4e58"
)));

assert!(
verify_block_signature(&sequencer_pub_key, &signature, &state_commitment, &block_hash)
.unwrap()
);
}
7 changes: 7 additions & 0 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use primitive_types::H160;
use serde::{Deserialize, Serialize};
use starknet_crypto::FieldElement;

use crate::crypto::PublicKey;
use crate::hash::{pedersen_hash_array, StarkFelt, StarkHash};
use crate::serde_utils::{BytesAsHex, PrefixedBytesAsHex};
use crate::transaction::{Calldata, ContractAddressSalt};
Expand Down Expand Up @@ -275,3 +276,9 @@ impl From<EthAddress> for PrefixedBytesAsHex<20_usize> {
BytesAsHex(felt.0.to_fixed_bytes())
}
}

/// A public key of a sequencer.
#[derive(
Debug, Copy, Clone, Default, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord,
)]
pub struct SequencerPublicKey(pub PublicKey);
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub mod type_utils;

use std::num::ParseIntError;

use block::BlockHash;
use crypto::CryptoError;
use serde_utils::InnerDeserializationError;

/// The error type returned by StarknetApi.
Expand All @@ -32,4 +34,6 @@ pub enum StarknetApiError {
/// Missing resource type / duplicated resource type.
#[error("Missing resource type / duplicated resource type; got {0}.")]
InvalidResourceMappingInitializer(String),
#[error("Failed to verify the signature of block {block_hash}. Error: {error}")]
BlockSignatureVerificationFailed { block_hash: BlockHash, error: CryptoError },
}

0 comments on commit 7d2dbbc

Please sign in to comment.