Skip to content

Commit 9029248

Browse files
committed
Let Uint256::new take an u128 argument
1 parent bb326f8 commit 9029248

File tree

2 files changed

+50
-22
lines changed

2 files changed

+50
-22
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ and this project adheres to
4343
([#2201])
4444
- cosmwasm-std: `Int256::new`/`Int512::new` now take an `i128` argument instead
4545
of bytes. Use `::from_be_bytes` if you need the old behaviour.
46+
- cosmwasm-std: `Uint256::new` now takes an `u128` argument instead of bytes.
47+
Use `::from_be_bytes` if you need the old behaviour.
4648

4749
## Fixed
4850

packages/std/src/math/uint256.rs

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,21 @@ use super::num_consts::NumConsts;
3030
///
3131
/// # Examples
3232
///
33-
/// Use `from` to create instances out of primitive uint types or `new` to provide big
34-
/// endian bytes:
33+
/// Use `new` to create instances out of u128, `from` for other primitive uint types
34+
/// or `from_be_bytes` to provide big endian bytes:
3535
///
3636
/// ```
3737
/// # use cosmwasm_std::Uint256;
38-
/// let a = Uint256::from(258u128);
39-
/// let b = Uint256::new([
38+
/// let a = Uint256::new(258u128);
39+
/// let b = Uint256::from(258u16);
40+
/// let c = Uint256::from_be_bytes([
4041
/// 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
4142
/// 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
4243
/// 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
4344
/// 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 1u8, 2u8,
4445
/// ]);
4546
/// assert_eq!(a, b);
47+
/// assert_eq!(a, c);
4648
/// ```
4749
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, schemars::JsonSchema)]
4850
pub struct Uint256(#[schemars(with = "String")] pub(crate) U256);
@@ -54,12 +56,20 @@ impl Uint256 {
5456
pub const MAX: Uint256 = Uint256(U256::MAX);
5557
pub const MIN: Uint256 = Uint256(U256::ZERO);
5658

57-
/// Creates a Uint256(value) from a big endian representation. It's just an alias for
58-
/// [`Uint256::from_be_bytes`].
59+
/// Creates a Uint256(value).
5960
///
6061
/// This method is less flexible than `from` but can be called in a const context.
61-
pub const fn new(value: [u8; 32]) -> Self {
62-
Self::from_be_bytes(value)
62+
///
63+
/// Before CosmWasm 3 this took a byte array as an argument. You can get this behaviour
64+
/// with [`from_be_bytes`].
65+
///
66+
/// [`from_be_bytes`]: Self::from_be_bytes
67+
pub const fn new(value: u128) -> Self {
68+
let b = value.to_be_bytes();
69+
Self::from_be_bytes([
70+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, b[0], b[1], b[2], b[3], b[4], b[5],
71+
b[6], b[7], b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15],
72+
])
6373
}
6474

6575
/// Creates a Uint256(0)
@@ -649,22 +659,40 @@ mod tests {
649659

650660
#[test]
651661
fn uint256_new_works() {
652-
let num = Uint256::new([1; 32]);
662+
let num = Uint256::new(1);
663+
assert_eq!(
664+
num.to_be_bytes(),
665+
[
666+
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,
667+
0, 0, 0, 1
668+
]
669+
);
670+
671+
for v in [0, 1, 18, 875786576, u128::MAX] {
672+
// From is implemented by bnum, so we test two independent implementations against each other
673+
let uut = Uint256::new(v);
674+
assert_eq!(uut, Uint256::from(v));
675+
}
676+
}
677+
678+
#[test]
679+
fn uint256_from_be_bytes_works() {
680+
let num = Uint256::from_be_bytes([1; 32]);
653681
let a: [u8; 32] = num.to_be_bytes();
654682
assert_eq!(a, [1; 32]);
655683

656684
let be_bytes = [
657685
0u8, 222u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
658686
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 1u8, 2u8, 3u8,
659687
];
660-
let num = Uint256::new(be_bytes);
688+
let num = Uint256::from_be_bytes(be_bytes);
661689
let resulting_bytes: [u8; 32] = num.to_be_bytes();
662690
assert_eq!(be_bytes, resulting_bytes);
663691
}
664692

665693
#[test]
666694
fn uint256_not_works() {
667-
let num = Uint256::new([1; 32]);
695+
let num = Uint256::from_be_bytes([1; 32]);
668696
let a = (!num).to_be_bytes();
669697
assert_eq!(a, [254; 32]);
670698

@@ -1000,12 +1028,10 @@ mod tests {
10001028
];
10011029

10021030
// These should all be the same.
1003-
let num1 = Uint256::new(be_bytes);
1004-
let num2 = Uint256::from_be_bytes(be_bytes);
1005-
let num3 = Uint256::from_le_bytes(le_bytes);
1006-
assert_eq!(num1, Uint256::from(65536u32 + 512 + 3));
1007-
assert_eq!(num1, num2);
1008-
assert_eq!(num1, num3);
1031+
let a = Uint256::from_be_bytes(be_bytes);
1032+
let b = Uint256::from_le_bytes(le_bytes);
1033+
assert_eq!(a, Uint256::from(65536u32 + 512 + 3));
1034+
assert_eq!(a, b);
10091035
}
10101036

10111037
#[test]
@@ -1335,7 +1361,7 @@ mod tests {
13351361
#[test]
13361362
#[should_panic(expected = "attempt to add with overflow")]
13371363
fn uint256_add_overflow_panics() {
1338-
let max = Uint256::new([255u8; 32]);
1364+
let max = Uint256::from_be_bytes([255u8; 32]);
13391365
let _ = max + Uint256::from(12u32);
13401366
}
13411367

@@ -1495,12 +1521,12 @@ mod tests {
14951521

14961522
#[test]
14971523
fn uint256_shr_works() {
1498-
let original = Uint256::new([
1524+
let original = Uint256::from_be_bytes([
14991525
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
15001526
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 2u8, 0u8, 4u8, 2u8,
15011527
]);
15021528

1503-
let shifted = Uint256::new([
1529+
let shifted = Uint256::from_be_bytes([
15041530
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
15051531
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 128u8, 1u8, 0u8,
15061532
]);
@@ -1516,12 +1542,12 @@ mod tests {
15161542

15171543
#[test]
15181544
fn uint256_shl_works() {
1519-
let original = Uint256::new([
1545+
let original = Uint256::from_be_bytes([
15201546
64u8, 128u8, 1u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
15211547
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
15221548
]);
15231549

1524-
let shifted = Uint256::new([
1550+
let shifted = Uint256::from_be_bytes([
15251551
2u8, 0u8, 4u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
15261552
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
15271553
]);

0 commit comments

Comments
 (0)