Skip to content

Commit

Permalink
Fix BigInt => u128 conversion. (#352)
Browse files Browse the repository at this point in the history
Fix BigInt => u128 conversion.
  • Loading branch information
oskin1 authored Aug 31, 2024
1 parent 25ff92b commit 1dc9b61
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 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,36 @@ mod tests {
assert_eq!(x.to_string(), "18446744073709551615");
}

#[test]
fn bigint_uint_u128_roundtrip() {
let int = 462_164_030_739_157_517;
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_roundtrip_max() {
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_min() {
let bytes = [0x00];
Expand Down

0 comments on commit 1dc9b61

Please sign in to comment.