Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Uint256::from_{le,be}_bytes and ::new const #1071

Merged
merged 5 commits into from
Sep 6, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make Uint256::from_be_bytes const
  • Loading branch information
webmaster128 committed Sep 6, 2021
commit 9d754e08f3647a35820b847e9789c24d87b398e5
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ and this project adheres to

- cosmwasm-std: Implement `Mul` and `MulAssign` for `Uint128`.
- cosmwasm-std: Implement `FromStr` for `Uint128`, `Uint256`, and `Uint512`.
- cosmwasm-std: Make `Uint256::from_le_bytes` const.
- cosmwasm-std: Make `Uint256::from_le_bytes` and `::from_be_bytes` const.

### Changed

174 changes: 172 additions & 2 deletions packages/std/src/math/uint256.rs
Original file line number Diff line number Diff line change
@@ -61,8 +61,22 @@ impl Uint256 {
Uint256(U256::zero())
}

pub fn from_be_bytes(value: [u8; 32]) -> Self {
Uint256(U256::from_big_endian(&value))
pub const fn from_be_bytes(data: [u8; 32]) -> Self {
let words: [u64; 4] = [
u64::from_le_bytes([
data[31], data[30], data[29], data[28], data[27], data[26], data[25], data[24],
]),
u64::from_le_bytes([
data[23], data[22], data[21], data[20], data[19], data[18], data[17], data[16],
]),
u64::from_le_bytes([
data[15], data[14], data[13], data[12], data[11], data[10], data[9], data[8],
]),
u64::from_le_bytes([
data[7], data[6], data[5], data[4], data[3], data[2], data[1], data[0],
]),
];
Uint256(U256(words))
}

pub const fn from_le_bytes(data: [u8; 32]) -> Self {
@@ -526,6 +540,162 @@ mod tests {
assert_eq!(be_bytes, resulting_bytes);
}

#[test]
fn uint256_from_be_bytes() {
let a = Uint256::from_be_bytes([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,
]);
assert_eq!(a, Uint256::from(0u128));
let a = Uint256::from_be_bytes([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 42,
]);
assert_eq!(a, Uint256::from(42u128));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Douglas Adams is the best.

let a = Uint256::from_be_bytes([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1,
]);
assert_eq!(a, Uint256::from(1u128));
let a = Uint256::from_be_bytes([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0,
]);
assert_eq!(a, Uint256::from(256u128));
let a = Uint256::from_be_bytes([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0,
]);
assert_eq!(a, Uint256::from(65536u128));
let a = Uint256::from_be_bytes([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0,
]);
assert_eq!(a, Uint256::from(16777216u128));
let a = Uint256::from_be_bytes([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
0, 0, 0,
]);
assert_eq!(a, Uint256::from(4294967296u128));
let a = Uint256::from_be_bytes([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0,
]);
assert_eq!(a, Uint256::from(1099511627776u128));
let a = Uint256::from_be_bytes([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0,
]);
assert_eq!(a, Uint256::from(281474976710656u128));
let a = Uint256::from_be_bytes([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0,
]);
assert_eq!(a, Uint256::from(72057594037927936u128));
let a = Uint256::from_be_bytes([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0,
]);
assert_eq!(a, Uint256::from(18446744073709551616u128));
let a = Uint256::from_be_bytes([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0,
]);
assert_eq!(a, Uint256::from(4722366482869645213696u128));
let a = Uint256::from_be_bytes([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,
]);
assert_eq!(a, Uint256::from(1208925819614629174706176u128));
let a = Uint256::from_be_bytes([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,
]);
assert_eq!(a, Uint256::from(1329227995784915872903807060280344576u128));

// Values > u128::MAX
let a = Uint256::from_be_bytes([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,
]);
assert_eq!(a, Uint256::from(1u128) << (8 * 16));
let a = Uint256::from_be_bytes([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,
]);
assert_eq!(a, Uint256::from(1u128) << (8 * 17));
let a = Uint256::from_be_bytes([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,
]);
assert_eq!(a, Uint256::from(1u128) << (8 * 18));
let a = Uint256::from_be_bytes([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,
]);
assert_eq!(a, Uint256::from(1u128) << (8 * 19));
let a = Uint256::from_be_bytes([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,
]);
assert_eq!(a, Uint256::from(1u128) << (8 * 20));
let a = Uint256::from_be_bytes([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,
]);
assert_eq!(a, Uint256::from(1u128) << (8 * 21));
let a = Uint256::from_be_bytes([
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,
]);
assert_eq!(a, Uint256::from(1u128) << (8 * 22));
let a = Uint256::from_be_bytes([
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,
]);
assert_eq!(a, Uint256::from(1u128) << (8 * 23));
let a = Uint256::from_be_bytes([
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,
]);
assert_eq!(a, Uint256::from(1u128) << (8 * 24));
let a = Uint256::from_be_bytes([
0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,
]);
assert_eq!(a, Uint256::from(1u128) << (8 * 25));
let a = Uint256::from_be_bytes([
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,
]);
assert_eq!(a, Uint256::from(1u128) << (8 * 26));
let a = Uint256::from_be_bytes([
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,
]);
assert_eq!(a, Uint256::from(1u128) << (8 * 27));
let a = Uint256::from_be_bytes([
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,
]);
assert_eq!(a, Uint256::from(1u128) << (8 * 28));
let a = Uint256::from_be_bytes([
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,
]);
assert_eq!(a, Uint256::from(1u128) << (8 * 29));
let a = Uint256::from_be_bytes([
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,
]);
assert_eq!(a, Uint256::from(1u128) << (8 * 30));
let a = Uint256::from_be_bytes([
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,
]);
assert_eq!(a, Uint256::from(1u128) << (8 * 31));
}

#[test]
fn uint256_from_le_bytes() {
let a = Uint256::from_le_bytes([