From 40d9eb73ead952c4642f011db84c75b46919d24c Mon Sep 17 00:00:00 2001 From: Harsh Pratap Singh Date: Thu, 1 Aug 2024 06:45:28 +0530 Subject: [PATCH 01/10] add target_to_bits --- src/engine.cairo | 64 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/src/engine.cairo b/src/engine.cairo index 556fc702..c71ada32 100644 --- a/src/engine.cairo +++ b/src/engine.cairo @@ -168,8 +168,47 @@ fn bits_to_target(bits: u32) -> u256 { 0 } -fn target_to_bits(target: u256) -> u32 { - 0 +fn target_to_bits(target: u256) -> Result { + if target == 0 { + return Result::Err('Target is zero'); + } + + // Find the most significant byte + let mut size: u32 = 32; + let mut compact = target; + + while size > 3 { + compact = compact / 256; + if compact != 0 { + size -= 1; + } else { + break; + } + }; + + // The mantissa is the most significant 3 bytes + let mut mantissa: u32 = (target / u256_pow(256, size - 3)).try_into().unwrap(); + + // Normalize: ensure the most significant byte is non-zero + if mantissa < 0x800000 { + mantissa = mantissa * 256; + size -= 1; + } + + // Ensure the result is valid and normalized + if mantissa > 0x7fffff { + return Result::Err('Mantissa too large'); + } + + // Combine exponent and mantissa into the compact format + let result: u32 = (size * 0x1000000) + mantissa; + + // Check if the result is within the valid range + if result > MAX_BITS { + return Result::Err('Exceeds max value'); + } + + Result::Ok(result) } fn compute_work_from_target(target: u256) -> u256 { @@ -179,3 +218,24 @@ fn compute_work_from_target(target: u256) -> u256 { fn compute_timestamps_median(timestamps: Span) -> u32 { 0 } + +// Custom power function for u256 +fn u256_pow(base: u256, exp: u32) -> u256 { + if exp == 0 { + return 1.into(); + } + let mut result: u256 = 1.into(); + let mut base = base; + let mut exp = exp; + loop { + if exp & 1 != 0 { + result = result * base; + } + exp = exp / 2; + if exp == 0 { + break; + } + base = base * base; + }; + result +} \ No newline at end of file From 38bb86d421602693a5fb8d4e7329ddd8667142c4 Mon Sep 17 00:00:00 2001 From: Harsh Pratap Singh Date: Sun, 4 Aug 2024 11:09:35 +0530 Subject: [PATCH 02/10] add some tests --- src/engine.cairo | 81 ++++++++++++++++++++++++++--------------------- tests/tests.cairo | 24 ++++++++++++++ 2 files changed, 69 insertions(+), 36 deletions(-) create mode 100644 tests/tests.cairo diff --git a/src/engine.cairo b/src/engine.cairo index c71ada32..8a1fc602 100644 --- a/src/engine.cairo +++ b/src/engine.cairo @@ -168,45 +168,43 @@ fn bits_to_target(bits: u32) -> u256 { 0 } -fn target_to_bits(target: u256) -> Result { +pub fn target_to_bits(target: u256) -> Result { if target == 0 { return Result::Err('Target is zero'); } + if target > MAX_TARGET { + return Result::Err('Exceeds max value'); + } + // Find the most significant byte let mut size: u32 = 32; let mut compact = target; - while size > 3 { - compact = compact / 256; - if compact != 0 { - size -= 1; - } else { - break; - } + // Count leading zero bytes by finding the first non-zero byte + while size > 1 && (compact / u256_pow(256, size - 1)) == 0 { + size -= 1; }; - // The mantissa is the most significant 3 bytes - let mut mantissa: u32 = (target / u256_pow(256, size - 3)).try_into().unwrap(); + size += 1; - // Normalize: ensure the most significant byte is non-zero - if mantissa < 0x800000 { - mantissa = mantissa * 256; - size -= 1; - } + // Extract mantissa (most significant 3 bytes) + let mut mantissa: u32 = ((compact / u256_pow(256, size - 3))).try_into().unwrap(); - // Ensure the result is valid and normalized + // Normalize if mantissa > 0x7fffff { - return Result::Err('Mantissa too large'); + mantissa = (mantissa + 0x80) / 0x100; + size += 1; } - // Combine exponent and mantissa into the compact format - let result: u32 = (size * 0x1000000) + mantissa; + // Ensure the mantissa is only 3 bytes + mantissa = mantissa & 0xffffff; - // Check if the result is within the valid range - if result > MAX_BITS { - return Result::Err('Exceeds max value'); - } + // Simulate (size << 24) by multiplying size with 0x1000000 (256^3) + let size_component: u32 = (u256_pow(256.into(), 3) * size.into()).try_into().unwrap(); + + // Combine size and mantissa + let result: u32 = size_component + mantissa; Result::Ok(result) } @@ -219,23 +217,34 @@ fn compute_timestamps_median(timestamps: Span) -> u32 { 0 } -// Custom power function for u256 +// Helper function to calculate power of 256 fn u256_pow(base: u256, exp: u32) -> u256 { if exp == 0 { return 1.into(); } let mut result: u256 = 1.into(); - let mut base = base; - let mut exp = exp; - loop { - if exp & 1 != 0 { - result = result * base; - } - exp = exp / 2; - if exp == 0 { - break; - } - base = base * base; + let mut i = 0; + while i < exp { + result = checked_mul(result, base).expect('u256_mul Overflow'); + i += 1; }; result -} \ No newline at end of file +} + +fn checked_mul(a: u256, b: u256) -> Option { + // If either number is zero, return zero immediately + if a == 0.into() || b == 0.into() { + return Option::Some(0.into()); + } + + // Perform multiplication and check for overflow + let product = a * b; + + // Check if the product divided by either of the operands does not yield the other operand, + // which would indicate an overflow. + if product / a != b || product / b != a { + return Option::None; + } + + Option::Some(product) +} diff --git a/tests/tests.cairo b/tests/tests.cairo new file mode 100644 index 00000000..bb3e3250 --- /dev/null +++ b/tests/tests.cairo @@ -0,0 +1,24 @@ +use core::result::ResultTrait; +use core::option::OptionTrait; +use core::traits::Into; +use raito::engine::target_to_bits; + +#[test] +fn test_target_to_bits_zero() { + let result = target_to_bits(0.into()); + assert!(result.is_err(), "Should error on zero input"); +} + +#[test] +fn test_target_to_bits_overflow() { + let target: u256 = 0x01000000000000000000000000000000000000000000000000000000000000000; + let result = target_to_bits(target); + assert!(result.is_err(), "Should error on overflow"); +} + +#[test] +fn test_target_to_bits_medium_target() { + let medium_target: u256 = 0x00000000000FFFFF000000000000000000000000000000000000000000000000; + let result = target_to_bits(medium_target).unwrap(); + assert!(result == 0x1c000fff, "Incorrect bits for medium target"); +} \ No newline at end of file From 8572c057f96b2b2f29893c3ccbfd09e274418202 Mon Sep 17 00:00:00 2001 From: Harsh Pratap Singh Date: Mon, 5 Aug 2024 09:14:57 +0530 Subject: [PATCH 03/10] nit --- src/engine.cairo | 4 +--- tests/tests.cairo | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/engine.cairo b/src/engine.cairo index 8a1fc602..21cf340c 100644 --- a/src/engine.cairo +++ b/src/engine.cairo @@ -186,8 +186,6 @@ pub fn target_to_bits(target: u256) -> Result { size -= 1; }; - size += 1; - // Extract mantissa (most significant 3 bytes) let mut mantissa: u32 = ((compact / u256_pow(256, size - 3))).try_into().unwrap(); @@ -202,7 +200,7 @@ pub fn target_to_bits(target: u256) -> Result { // Simulate (size << 24) by multiplying size with 0x1000000 (256^3) let size_component: u32 = (u256_pow(256.into(), 3) * size.into()).try_into().unwrap(); - + // Combine size and mantissa let result: u32 = size_component + mantissa; diff --git a/tests/tests.cairo b/tests/tests.cairo index bb3e3250..a6b01546 100644 --- a/tests/tests.cairo +++ b/tests/tests.cairo @@ -20,5 +20,5 @@ fn test_target_to_bits_overflow() { fn test_target_to_bits_medium_target() { let medium_target: u256 = 0x00000000000FFFFF000000000000000000000000000000000000000000000000; let result = target_to_bits(medium_target).unwrap(); - assert!(result == 0x1c000fff, "Incorrect bits for medium target"); + assert!(result == 454033407, "Incorrect bits for medium target"); } \ No newline at end of file From 331ba78e9e7cfeac5b1f1295162ff1513d375f7d Mon Sep 17 00:00:00 2001 From: Harsh Pratap Singh Date: Mon, 5 Aug 2024 09:54:03 +0530 Subject: [PATCH 04/10] normalization test --- src/engine.cairo | 5 +++++ tests/tests.cairo | 9 ++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/engine.cairo b/src/engine.cairo index 21cf340c..c9b7ea26 100644 --- a/src/engine.cairo +++ b/src/engine.cairo @@ -198,6 +198,11 @@ pub fn target_to_bits(target: u256) -> Result { // Ensure the mantissa is only 3 bytes mantissa = mantissa & 0xffffff; + // Check size doesn't exceed maximum + if size > 34 { + return Result::Err('Overflow'); + } + // Simulate (size << 24) by multiplying size with 0x1000000 (256^3) let size_component: u32 = (u256_pow(256.into(), 3) * size.into()).try_into().unwrap(); diff --git a/tests/tests.cairo b/tests/tests.cairo index a6b01546..cf30dc30 100644 --- a/tests/tests.cairo +++ b/tests/tests.cairo @@ -21,4 +21,11 @@ fn test_target_to_bits_medium_target() { let medium_target: u256 = 0x00000000000FFFFF000000000000000000000000000000000000000000000000; let result = target_to_bits(medium_target).unwrap(); assert!(result == 454033407, "Incorrect bits for medium target"); -} \ No newline at end of file +} + +#[test] +fn test_target_to_bits_normalization() { + let target: u256 = 0x00000000007FFFFF800000000000000000000000000000000000000000000000; + let result = target_to_bits(target).unwrap(); + assert!(result == 0x1b7fffff, "Incorrect bits after normalization"); +} From 2d9bbd397eea946ad6cbcf60268f9a3531afeecb Mon Sep 17 00:00:00 2001 From: Harsh Pratap Singh Date: Mon, 5 Aug 2024 09:55:03 +0530 Subject: [PATCH 05/10] max target test --- tests/tests.cairo | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/tests.cairo b/tests/tests.cairo index cf30dc30..3b0ad9f0 100644 --- a/tests/tests.cairo +++ b/tests/tests.cairo @@ -29,3 +29,10 @@ fn test_target_to_bits_normalization() { let result = target_to_bits(target).unwrap(); assert!(result == 0x1b7fffff, "Incorrect bits after normalization"); } + +#[test] +fn test_target_to_bits_max_target() { + let max_target: u256 = 0x00000000FFFF0000000000000000000000000000000000000000000000000000; + let result = target_to_bits(max_target).unwrap(); + assert!(result == 0x1d00ffff, "Incorrect bits for max target"); +} \ No newline at end of file From 1961cd3323d4f5c6ff53484ce103dc5cf59261a6 Mon Sep 17 00:00:00 2001 From: Harsh Pratap Singh Date: Wed, 7 Aug 2024 06:53:08 +0530 Subject: [PATCH 06/10] major refactor --- src/engine.cairo | 51 +++++++++----------------------------- src/lib.cairo | 1 + src/utils.cairo | 35 ++++++++++++++++++++++++++ tests/tests.cairo | 62 ++++++++++++++++++++++++++++++++++------------- 4 files changed, 92 insertions(+), 57 deletions(-) create mode 100644 src/utils.cairo diff --git a/src/engine.cairo b/src/engine.cairo index c9b7ea26..bd95cbef 100644 --- a/src/engine.cairo +++ b/src/engine.cairo @@ -2,6 +2,9 @@ use core::result::Result; use core::option::OptionTrait; use core::traits::Into; use core::byte_array::ByteArray; +use raito::utils::shl; +use raito::utils::shr; +use raito::utils::fast_pow; // Constants const BLOCK_HEADER_SIZE: u32 = 80; @@ -182,14 +185,14 @@ pub fn target_to_bits(target: u256) -> Result { let mut compact = target; // Count leading zero bytes by finding the first non-zero byte - while size > 1 && (compact / u256_pow(256, size - 1)) == 0 { + while size > 1 && shr(compact, (size - 1) * 8) == 0 { size -= 1; }; // Extract mantissa (most significant 3 bytes) - let mut mantissa: u32 = ((compact / u256_pow(256, size - 3))).try_into().unwrap(); + let mut mantissa: u32 = shr(compact, (size - 3) * 8).try_into().unwrap(); - // Normalize + // Normalize if mantissa > 0x7fffff { mantissa = (mantissa + 0x80) / 0x100; size += 1; @@ -203,11 +206,11 @@ pub fn target_to_bits(target: u256) -> Result { return Result::Err('Overflow'); } - // Simulate (size << 24) by multiplying size with 0x1000000 (256^3) - let size_component: u32 = (u256_pow(256.into(), 3) * size.into()).try_into().unwrap(); + // Convert size to u256 + let size_u256: u256 = size.into(); - // Combine size and mantissa - let result: u32 = size_component + mantissa; + // Combine size and mantissa + let result: u32 = (shl(size_u256, 24) + mantissa.into()).try_into().unwrap(); Result::Ok(result) } @@ -218,36 +221,4 @@ fn compute_work_from_target(target: u256) -> u256 { fn compute_timestamps_median(timestamps: Span) -> u32 { 0 -} - -// Helper function to calculate power of 256 -fn u256_pow(base: u256, exp: u32) -> u256 { - if exp == 0 { - return 1.into(); - } - let mut result: u256 = 1.into(); - let mut i = 0; - while i < exp { - result = checked_mul(result, base).expect('u256_mul Overflow'); - i += 1; - }; - result -} - -fn checked_mul(a: u256, b: u256) -> Option { - // If either number is zero, return zero immediately - if a == 0.into() || b == 0.into() { - return Option::Some(0.into()); - } - - // Perform multiplication and check for overflow - let product = a * b; - - // Check if the product divided by either of the operands does not yield the other operand, - // which would indicate an overflow. - if product / a != b || product / b != a { - return Option::None; - } - - Option::Some(product) -} +} \ No newline at end of file diff --git a/src/lib.cairo b/src/lib.cairo index 829eb99c..985a50dd 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -1,3 +1,4 @@ pub mod engine; +pub mod utils; mod main; diff --git a/src/utils.cairo b/src/utils.cairo new file mode 100644 index 00000000..c525c415 --- /dev/null +++ b/src/utils.cairo @@ -0,0 +1,35 @@ +use core::traits::Into; +use core::traits::TryInto; + +// Bitwise shift left for u256 +pub fn shl(value: u256, shift: u32) -> u256 { + value * fast_pow(2.into(), shift.into()) +} + +// Bitwise shift right for u256 +pub fn shr(value: u256, shift: u32) -> u256 { + value / fast_pow(2.into(), shift.into()) +} + +// Fast exponentiation using the square-and-multiply algorithm +// Reference: https://github.com/keep-starknet-strange/alexandria/blob/bcdca70afdf59c9976148e95cebad5cf63d75a7f/packages/math/src/fast_power.cairo#L12 +pub fn fast_pow(base: u256, exp: u256) -> u256 { + if exp == 0.into() { + return 1.into(); + } + + let mut res: u256 = 1.into(); + let mut base: u256 = base; + let mut exp: u256 = exp; + + loop { + if exp % 2.into() == 1.into() { + res = res * base; + } + exp = exp / 2.into(); + if exp == 0.into() { + break res; + } + base = base * base; + } +} \ No newline at end of file diff --git a/tests/tests.cairo b/tests/tests.cairo index 3b0ad9f0..2ee9eb93 100644 --- a/tests/tests.cairo +++ b/tests/tests.cairo @@ -4,35 +4,63 @@ use core::traits::Into; use raito::engine::target_to_bits; #[test] -fn test_target_to_bits_zero() { - let result = target_to_bits(0.into()); - assert!(result.is_err(), "Should error on zero input"); +fn test_target_to_bits_large_target() { + let target: u256 = 0x1bc330000000000000000000000000000000000000000000; + let result = target_to_bits(target).unwrap(); + assert!(result == 0x181bc330, "Incorrect bits for large target"); } #[test] -fn test_target_to_bits_overflow() { - let target: u256 = 0x01000000000000000000000000000000000000000000000000000000000000000; - let result = target_to_bits(target); - assert!(result.is_err(), "Should error on overflow"); +fn test_target_to_bits_small_target() { + let target: u256 = 0x92340000; + let result = target_to_bits(target).unwrap(); + assert!(result == 0x05009234, "Incorrect bits for small target"); } #[test] fn test_target_to_bits_medium_target() { - let medium_target: u256 = 0x00000000000FFFFF000000000000000000000000000000000000000000000000; - let result = target_to_bits(medium_target).unwrap(); - assert!(result == 454033407, "Incorrect bits for medium target"); -} - -#[test] -fn test_target_to_bits_normalization() { - let target: u256 = 0x00000000007FFFFF800000000000000000000000000000000000000000000000; + let target: u256 = 0x12345600; let result = target_to_bits(target).unwrap(); - assert!(result == 0x1b7fffff, "Incorrect bits after normalization"); + assert!(result == 0x04123456, "Incorrect bits for medium target"); } #[test] fn test_target_to_bits_max_target() { - let max_target: u256 = 0x00000000FFFF0000000000000000000000000000000000000000000000000000; + let max_target: u256 = 0x00000000ffff0000000000000000000000000000000000000000000000000000; let result = target_to_bits(max_target).unwrap(); assert!(result == 0x1d00ffff, "Incorrect bits for max target"); +} + +#[test] +fn test_target_to_bits_high_precision_target() { + let target: u256 = 0x000000000d314200000000000000000000000000000000000000000000000000; + let result = target_to_bits(target).unwrap(); + assert!(result == 0x1c0d3142, "Incorrect bits for high precision target"); +} + +#[test] +fn test_target_to_bits_low_precision_target() { + let target: u256 = 0x00000000000000000007a4290000000000000000000000000000000000000000; + let result = target_to_bits(target).unwrap(); + assert!(result == 0x1707a429, "Incorrect bits for low precision target"); +} + +#[test] +fn test_target_to_bits_full_mantissa() { + let target: u256 = 0xd86a528bc8bc8bc8bc8bc8bc8bc8bc8bc8bc8bc8bc8bc8bc8bc8bc8b; + let result = target_to_bits(target).unwrap(); + assert!(result == 0x1d00d86a, "Incorrect bits for full mantissa target"); +} + +#[test] +fn test_target_to_bits_zero_target() { + let result = target_to_bits(0.into()); + assert!(result.is_err(), "Should error on zero target"); +} + +#[test] +fn test_target_to_bits_overflow_target() { + let target: u256 = 0x01000000000000000000000000000000000000000000000000000000000000000; + let result = target_to_bits(target); + assert!(result.is_err(), "Should error on overflow target"); } \ No newline at end of file From e0bd6e2d5d8c49ee4122aba083cd3a38ba88af0a Mon Sep 17 00:00:00 2001 From: Harsh Pratap Singh Date: Wed, 7 Aug 2024 16:25:46 +0530 Subject: [PATCH 07/10] nits --- src/engine.cairo | 1 - src/utils.cairo | 12 ++++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/engine.cairo b/src/engine.cairo index bd95cbef..f0ef83f3 100644 --- a/src/engine.cairo +++ b/src/engine.cairo @@ -4,7 +4,6 @@ use core::traits::Into; use core::byte_array::ByteArray; use raito::utils::shl; use raito::utils::shr; -use raito::utils::fast_pow; // Constants const BLOCK_HEADER_SIZE: u32 = 80; diff --git a/src/utils.cairo b/src/utils.cairo index c525c415..e188b13f 100644 --- a/src/utils.cairo +++ b/src/utils.cairo @@ -13,21 +13,21 @@ pub fn shr(value: u256, shift: u32) -> u256 { // Fast exponentiation using the square-and-multiply algorithm // Reference: https://github.com/keep-starknet-strange/alexandria/blob/bcdca70afdf59c9976148e95cebad5cf63d75a7f/packages/math/src/fast_power.cairo#L12 -pub fn fast_pow(base: u256, exp: u256) -> u256 { - if exp == 0.into() { +pub fn fast_pow(base: u256, exp: u32) -> u256 { + if exp == 0 { return 1.into(); } let mut res: u256 = 1.into(); let mut base: u256 = base; - let mut exp: u256 = exp; + let mut exp: u32 = exp; loop { - if exp % 2.into() == 1.into() { + if exp % 2 == 1 { res = res * base; } - exp = exp / 2.into(); - if exp == 0.into() { + exp = exp / 2; + if exp == 0 { break res; } base = base * base; From 55cdd3bb900ba219d53e99ee9d38181937f48a63 Mon Sep 17 00:00:00 2001 From: Harsh Pratap Singh Date: Wed, 7 Aug 2024 18:51:59 +0530 Subject: [PATCH 08/10] fmt nit --- src/utils.cairo | 2 +- tests/tests.cairo | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils.cairo b/src/utils.cairo index e188b13f..51655339 100644 --- a/src/utils.cairo +++ b/src/utils.cairo @@ -32,4 +32,4 @@ pub fn fast_pow(base: u256, exp: u32) -> u256 { } base = base * base; } -} \ No newline at end of file +} diff --git a/tests/tests.cairo b/tests/tests.cairo index 2ee9eb93..76c15f51 100644 --- a/tests/tests.cairo +++ b/tests/tests.cairo @@ -1,7 +1,7 @@ use core::result::ResultTrait; use core::option::OptionTrait; use core::traits::Into; -use raito::engine::target_to_bits; +use raito::validation::target_to_bits; #[test] fn test_target_to_bits_large_target() { @@ -63,4 +63,4 @@ fn test_target_to_bits_overflow_target() { let target: u256 = 0x01000000000000000000000000000000000000000000000000000000000000000; let result = target_to_bits(target); assert!(result.is_err(), "Should error on overflow target"); -} \ No newline at end of file +} From 26472d7642e3509094715da5a517951b5b4d29d5 Mon Sep 17 00:00:00 2001 From: Harsh Pratap Singh Date: Wed, 7 Aug 2024 20:45:31 +0530 Subject: [PATCH 09/10] nits --- src/lib.cairo | 2 +- src/utils.cairo | 4 ++-- src/validation.cairo | 2 ++ 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/lib.cairo b/src/lib.cairo index 6653860b..ce41db81 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -1,5 +1,5 @@ pub mod utils; +pub mod validation; mod state; -mod validation; mod main; diff --git a/src/utils.cairo b/src/utils.cairo index 51655339..fdf589fe 100644 --- a/src/utils.cairo +++ b/src/utils.cairo @@ -15,10 +15,10 @@ pub fn shr(value: u256, shift: u32) -> u256 { // Reference: https://github.com/keep-starknet-strange/alexandria/blob/bcdca70afdf59c9976148e95cebad5cf63d75a7f/packages/math/src/fast_power.cairo#L12 pub fn fast_pow(base: u256, exp: u32) -> u256 { if exp == 0 { - return 1.into(); + return 1_u256; } - let mut res: u256 = 1.into(); + let mut res: u256 = 1_u256; let mut base: u256 = base; let mut exp: u32 = exp; diff --git a/src/validation.cairo b/src/validation.cairo index ccd56c82..37b26906 100644 --- a/src/validation.cairo +++ b/src/validation.cairo @@ -2,6 +2,8 @@ use super::state::{Block, ChainState, UtreexoState}; use raito::utils::shl; use raito::utils::shr; +const MAX_TARGET: u256 = 0x00000000FFFF0000000000000000000000000000000000000000000000000000; + #[generate_trait] impl BlockValidatorImpl of BlockValidator { fn validate_and_apply(self: ChainState, block: Block) -> Result { From 7981cd14e1c6c2cc8baa336fe5d0de9783fc4345 Mon Sep 17 00:00:00 2001 From: Harsh Pratap Singh Date: Wed, 7 Aug 2024 20:55:28 +0530 Subject: [PATCH 10/10] fmt nit --- src/utils.cairo | 3 ++- src/validation.cairo | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/utils.cairo b/src/utils.cairo index fdf589fe..0e542db3 100644 --- a/src/utils.cairo +++ b/src/utils.cairo @@ -12,7 +12,8 @@ pub fn shr(value: u256, shift: u32) -> u256 { } // Fast exponentiation using the square-and-multiply algorithm -// Reference: https://github.com/keep-starknet-strange/alexandria/blob/bcdca70afdf59c9976148e95cebad5cf63d75a7f/packages/math/src/fast_power.cairo#L12 +// Reference: +// https://github.com/keep-starknet-strange/alexandria/blob/bcdca70afdf59c9976148e95cebad5cf63d75a7f/packages/math/src/fast_power.cairo#L12 pub fn fast_pow(base: u256, exp: u32) -> u256 { if exp == 0 { return 1_u256; diff --git a/src/validation.cairo b/src/validation.cairo index 37b26906..02501b68 100644 --- a/src/validation.cairo +++ b/src/validation.cairo @@ -96,7 +96,7 @@ pub fn target_to_bits(target: u256) -> Result { // Extract mantissa (most significant 3 bytes) let mut mantissa: u32 = shr(compact, (size - 3) * 8).try_into().unwrap(); - // Normalize + // Normalize if mantissa > 0x7fffff { mantissa = (mantissa + 0x80) / 0x100; size += 1; @@ -113,7 +113,7 @@ pub fn target_to_bits(target: u256) -> Result { // Convert size to u256 let size_u256: u256 = size.into(); - // Combine size and mantissa + // Combine size and mantissa let result: u32 = (shl(size_u256, 24) + mantissa.into()).try_into().unwrap(); Result::Ok(result)