Skip to content

Commit

Permalink
feat: add todo
Browse files Browse the repository at this point in the history
  • Loading branch information
Th0rgal committed Oct 16, 2024
1 parent e9a91e5 commit 8f65ead
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/bitcoin/block.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct BlockHeader {
/// In Cairo, the default encoding is big-endian, so this struct allows
/// creation of a BlockHeader from human-readable values.
/// Note: The Digest fields are already in little-endian format.

// todo: BlockHeader with HumanReadable constructor and default one?
#[derive(Drop, Copy, Debug, PartialEq, Default, Serde)]
pub struct HumanReadableBlockHeader {
pub version: u32,
Expand All @@ -42,6 +42,7 @@ pub struct HumanReadableBlockHeader {
pub nonce: u32,
}

// todo: Precompiled Block Header?
impl IntoBlockHeader of Into<HumanReadableBlockHeader, BlockHeader> {
fn into(self: HumanReadableBlockHeader) -> BlockHeader {
BlockHeader {
Expand All @@ -50,6 +51,7 @@ impl IntoBlockHeader of Into<HumanReadableBlockHeader, BlockHeader> {
merkle_root_hash: self.merkle_root_hash,
time: u32_byte_reverse(self.time),
// we want to keep bits in little endian to allow for pow computation
// todo: split mantissa and precompute reversed?
bits: self.bits,
nonce: u32_byte_reverse(self.nonce),
}
Expand Down Expand Up @@ -90,6 +92,7 @@ pub impl PowVerificationImpl of PowVerificationTrait {
}

/// Computes the target threshold for the block.
/// todo: optimize for errors and maths
fn compute_target_threshold(self: @BlockHeader) -> u256 {
let (exponent, mantissa) = DivRem::div_rem(*self.bits, 0x1000000);

Expand Down
1 change: 1 addition & 0 deletions src/utils/numeric.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
///
/// This function takes a 32-bit unsigned integer and reverses the order of its bytes.
/// It is useful for converting between big-endian and little-endian formats.
/// todo: optimize with split_bytes?
pub fn u32_byte_reverse(word: u32) -> u32 {
let byte0 = (word & 0x000000FF) * 0x1000000_u32;
let byte1 = (word & 0x0000FF00) * 0x00000100_u32;
Expand Down

0 comments on commit 8f65ead

Please sign in to comment.