-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: concat_counts into a single felt (#247)
- Loading branch information
Showing
3 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod block_hash_calculator; | ||
pub mod event_commitment; | ||
pub mod receipt_commitment; | ||
pub mod state_diff_hash; | ||
|
This file contains 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,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() | ||
} |
This file contains 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,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) | ||
} |