Skip to content
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

feat: concat_counts into a single felt #247

Merged
merged 1 commit into from
May 9, 2024
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
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)
}
Loading