diff --git a/src/block_hash.rs b/src/block_hash.rs index d30979e..879a196 100644 --- a/src/block_hash.rs +++ b/src/block_hash.rs @@ -1,3 +1,4 @@ +pub mod block_hash_calculator; pub mod event_commitment; pub mod receipt_commitment; pub mod state_diff_hash; diff --git a/src/block_hash/block_hash_calculator.rs b/src/block_hash/block_hash_calculator.rs new file mode 100644 index 0000000..1b5f544 --- /dev/null +++ b/src/block_hash/block_hash_calculator.rs @@ -0,0 +1,37 @@ +use crate::data_availability::L1DataAvailabilityMode; +use crate::hash::StarkFelt; + +#[cfg(test)] +#[path = "block_hash_calculator_test.rs"] +mod block_hash_calculator_test; + +// A single felt: [ +// transaction_count (64 bits) | event_count (64 bits) | state_diff_length (64 bits) +// | L1 data availability mode: 0 for calldata, 1 for blob (1 bit) | 0 ... +// ]. +#[allow(dead_code)] +fn concat_counts( + transaction_count: usize, + event_count: usize, + state_diff_length: usize, + l1_data_availability_mode: L1DataAvailabilityMode, +) -> StarkFelt { + let l1_data_availability_byte: u8 = match l1_data_availability_mode { + L1DataAvailabilityMode::Calldata => 0, + L1DataAvailabilityMode::Blob => 0b10000000, + }; + let concat_bytes = [ + to_64_bits(transaction_count).as_slice(), + to_64_bits(event_count).as_slice(), + to_64_bits(state_diff_length).as_slice(), + &[l1_data_availability_byte], + &[0_u8; 7], // zero padding + ] + .concat(); + StarkFelt::new_unchecked(concat_bytes.try_into().expect("Expect 32 bytes")) +} + +fn to_64_bits(num: usize) -> [u8; 8] { + let sized_transaction_count: u64 = num.try_into().expect("Expect usize is at most 8 bytes"); + sized_transaction_count.to_be_bytes() +} diff --git a/src/block_hash/block_hash_calculator_test.rs b/src/block_hash/block_hash_calculator_test.rs new file mode 100644 index 0000000..b015041 --- /dev/null +++ b/src/block_hash/block_hash_calculator_test.rs @@ -0,0 +1,12 @@ +use super::concat_counts; +use crate::data_availability::L1DataAvailabilityMode; +use crate::hash::StarkFelt; + +#[test] +fn concat_counts_test() { + let concated = concat_counts(4, 3, 2, L1DataAvailabilityMode::Blob); + let expected_felt = + StarkFelt::try_from("0x0000000000000004000000000000000300000000000000028000000000000000") + .unwrap(); + assert_eq!(concated, expected_felt) +}