-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/compute_block_reward
- Loading branch information
Showing
7 changed files
with
266 additions
and
37 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
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
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 @@ | ||
mod utils; | ||
pub mod validation; | ||
mod state; | ||
mod validation; | ||
mod main; |
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
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,36 @@ | ||
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: u32) -> u256 { | ||
if exp == 0 { | ||
return 1_u256; | ||
} | ||
|
||
let mut res: u256 = 1_u256; | ||
let mut base: u256 = base; | ||
let mut exp: u32 = exp; | ||
|
||
loop { | ||
if exp % 2 == 1 { | ||
res = res * base; | ||
} | ||
exp = exp / 2; | ||
if exp == 0 { | ||
break res; | ||
} | ||
base = base * base; | ||
} | ||
} |
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
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,63 @@ | ||
use raito::validation::target_to_bits; | ||
|
||
#[test] | ||
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_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 target: u256 = 0x12345600; | ||
let result = target_to_bits(target).unwrap(); | ||
assert!(result == 0x04123456, "Incorrect bits for medium target"); | ||
} | ||
|
||
#[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"); | ||
} | ||
|
||
#[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"); | ||
} |