Skip to content

Commit

Permalink
Fix BigInt => u128 conversion.
Browse files Browse the repository at this point in the history
  • Loading branch information
oskin1 committed Aug 29, 2024
1 parent 25ff92b commit 78ad394
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions chain/rust/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,13 +383,13 @@ impl BigInteger {
match *u32_digits {
[] => Some(0),
[a] => Some(u128::from(a)),
[a, b] => Some(u128::from(b) | (u128::from(a) << 32)),
[a, b, c] => Some(u128::from(c) | (u128::from(b) << 32) | (u128::from(a) << 64)),
[a, b] => Some(u128::from(a) | (u128::from(b) << 32)),
[a, b, c] => Some(u128::from(a) | (u128::from(b) << 32) | (u128::from(c) << 64)),
[a, b, c, d] => Some(
u128::from(d)
| (u128::from(c) << 32)
| (u128::from(b) << 64)
| (u128::from(a) << 96),
u128::from(a)
| (u128::from(b) << 32)
| (u128::from(c) << 64)
| (u128::from(d) << 96),
),
_ => None,
}
Expand Down Expand Up @@ -1047,6 +1047,26 @@ mod tests {
assert_eq!(x.to_string(), "18446744073709551615");
}

#[test]
fn bigint_uint_u128_roundtrip() {
let int = u64::MAX;
let x = BigInteger::from_int(&Int::Uint {
value: int,
encoding: None,
});
assert_eq!(x.as_u128(), Some(int as u128))
}

#[test]
fn bigint_uint_u128_roundtrip_min() {
let int = u64::MIN;
let x = BigInteger::from_int(&Int::Uint {
value: int,
encoding: None,
});
assert_eq!(x.as_u128(), Some(int as u128))
}

#[test]
fn bigint_uint_u128_min() {
let bytes = [0x00];
Expand Down

0 comments on commit 78ad394

Please sign in to comment.