Skip to content

Commit 1dc9b61

Browse files
authored
Fix BigInt => u128 conversion. (#352)
Fix BigInt => u128 conversion.
1 parent 25ff92b commit 1dc9b61

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

chain/rust/src/utils.rs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -383,13 +383,13 @@ impl BigInteger {
383383
match *u32_digits {
384384
[] => Some(0),
385385
[a] => Some(u128::from(a)),
386-
[a, b] => Some(u128::from(b) | (u128::from(a) << 32)),
387-
[a, b, c] => Some(u128::from(c) | (u128::from(b) << 32) | (u128::from(a) << 64)),
386+
[a, b] => Some(u128::from(a) | (u128::from(b) << 32)),
387+
[a, b, c] => Some(u128::from(a) | (u128::from(b) << 32) | (u128::from(c) << 64)),
388388
[a, b, c, d] => Some(
389-
u128::from(d)
390-
| (u128::from(c) << 32)
391-
| (u128::from(b) << 64)
392-
| (u128::from(a) << 96),
389+
u128::from(a)
390+
| (u128::from(b) << 32)
391+
| (u128::from(c) << 64)
392+
| (u128::from(d) << 96),
393393
),
394394
_ => None,
395395
}
@@ -1047,6 +1047,36 @@ mod tests {
10471047
assert_eq!(x.to_string(), "18446744073709551615");
10481048
}
10491049

1050+
#[test]
1051+
fn bigint_uint_u128_roundtrip() {
1052+
let int = 462_164_030_739_157_517;
1053+
let x = BigInteger::from_int(&Int::Uint {
1054+
value: int,
1055+
encoding: None,
1056+
});
1057+
assert_eq!(x.as_u128(), Some(int as u128))
1058+
}
1059+
1060+
#[test]
1061+
fn bigint_uint_u128_roundtrip_min() {
1062+
let int = u64::MIN;
1063+
let x = BigInteger::from_int(&Int::Uint {
1064+
value: int,
1065+
encoding: None,
1066+
});
1067+
assert_eq!(x.as_u128(), Some(int as u128))
1068+
}
1069+
1070+
#[test]
1071+
fn bigint_uint_u128_roundtrip_max() {
1072+
let int = u64::MAX;
1073+
let x = BigInteger::from_int(&Int::Uint {
1074+
value: int,
1075+
encoding: None,
1076+
});
1077+
assert_eq!(x.as_u128(), Some(int as u128))
1078+
}
1079+
10501080
#[test]
10511081
fn bigint_uint_u128_min() {
10521082
let bytes = [0x00];

0 commit comments

Comments
 (0)