Skip to content

Commit

Permalink
Make from_le_bytes const
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed Sep 2, 2021
1 parent ff232e0 commit f32b576
Showing 1 changed file with 72 additions and 2 deletions.
74 changes: 72 additions & 2 deletions packages/std/src/math/uint256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,22 @@ impl Uint256 {
Uint256(U256::from_big_endian(&value))
}

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

/// Returns a copy of the number as big endian bytes.
Expand Down Expand Up @@ -483,6 +497,62 @@ mod tests {
assert_eq!(be_bytes, resulting_bytes);
}

#[test]
fn uint256_from_le_bytes() {
let a = Uint256::from_le_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_le_bytes([
42, 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(42u128));
let a = Uint256::from_le_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));
let a = Uint256::from_le_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(256u128));
let a = Uint256::from_le_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(65536u128));
let a = Uint256::from_le_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(16777216u128));
let a = Uint256::from_le_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(4294967296u128));
let a = Uint256::from_le_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(72057594037927936u128));
let a = Uint256::from_le_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(18446744073709551616u128));
let a = Uint256::from_le_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(1329227995784915872903807060280344576u128));

// TODO: test values that exceed the u128 range
}

#[test]
fn uint256_endianness() {
let be_bytes = [
Expand Down

0 comments on commit f32b576

Please sign in to comment.