Skip to content

Commit

Permalink
feat: concat_counts into a single felt (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
yoavGrs authored May 9, 2024
1 parent 4182c83 commit 77ff5a3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/block_hash.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod block_hash_calculator;
pub mod event_commitment;
pub mod receipt_commitment;
pub mod state_diff_hash;
Expand Down
37 changes: 37 additions & 0 deletions src/block_hash/block_hash_calculator.rs
Original file line number Diff line number Diff line change
@@ -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()
}
12 changes: 12 additions & 0 deletions src/block_hash/block_hash_calculator_test.rs
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 77ff5a3

Please sign in to comment.