-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minor improvements to Utils package (#1206)
* change to for loop * minor doc fix * add further in-code docs for snip12 * remove mut from compute_hash param * fix comment * add changelog entry * fix comment * add domain separator comment * add tests for average * add u256 average test * tidy up test * Apply suggestions from code review Co-authored-by: Eric Nordelo <[email protected]> --------- Co-authored-by: Eric Nordelo <[email protected]>
- Loading branch information
1 parent
2d5b458
commit c77b4b1
Showing
7 changed files
with
100 additions
and
15 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
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 test_checkpoint; | ||
mod test_math; | ||
mod test_nonces; | ||
mod test_snip12; |
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,75 @@ | ||
use core::integer::{u512, u512_safe_div_rem_by_u256}; | ||
use core::num::traits::OverflowingAdd; | ||
use crate::math::average; | ||
|
||
#[test] | ||
fn test_average_u8(a: u8, b: u8) { | ||
let actual = average(a, b); | ||
|
||
let a: u256 = a.into(); | ||
let b: u256 = b.into(); | ||
let expected = (a + b) / 2; | ||
|
||
assert_eq!(actual, expected.try_into().unwrap()); | ||
} | ||
|
||
#[test] | ||
fn test_average_u16(a: u16, b: u16) { | ||
let actual = average(a, b); | ||
|
||
let a: u256 = a.into(); | ||
let b: u256 = b.into(); | ||
let expected = (a + b) / 2; | ||
|
||
assert_eq!(actual, expected.try_into().unwrap()); | ||
} | ||
|
||
#[test] | ||
fn test_average_u32(a: u32, b: u32) { | ||
let actual = average(a, b); | ||
|
||
let a: u256 = a.into(); | ||
let b: u256 = b.into(); | ||
let expected = (a + b) / 2; | ||
|
||
assert_eq!(actual, expected.try_into().unwrap()); | ||
} | ||
|
||
#[test] | ||
fn test_average_u64(a: u64, b: u64) { | ||
let actual = average(a, b); | ||
|
||
let a: u256 = a.into(); | ||
let b: u256 = b.into(); | ||
let expected = (a + b) / 2; | ||
|
||
assert_eq!(actual, expected.try_into().unwrap()); | ||
} | ||
|
||
#[test] | ||
fn test_average_u128(a: u128, b: u128) { | ||
let actual = average(a, b); | ||
|
||
let a: u256 = a.into(); | ||
let b: u256 = b.into(); | ||
let expected = (a + b) / 2; | ||
|
||
assert_eq!(actual, expected.try_into().unwrap()); | ||
} | ||
|
||
#[test] | ||
fn test_average_u256(a: u256, b: u256) { | ||
let actual = average(a, b); | ||
let mut expected = 0; | ||
|
||
let (sum, overflow) = a.overflowing_add(b); | ||
if !overflow { | ||
expected = sum / 2; | ||
} else { | ||
let u512_sum = u512 { limb0: sum.low, limb1: sum.high, limb2: 1, limb3: 0 }; | ||
let (res, _) = u512_safe_div_rem_by_u256(u512_sum, 2); | ||
expected = res.try_into().unwrap(); | ||
}; | ||
|
||
assert_eq!(actual, expected); | ||
} |