Skip to content

Commit 5558178

Browse files
committed
Let Uint512::new take an u128 argument
1 parent 9029248 commit 5558178

File tree

2 files changed

+53
-23
lines changed

2 files changed

+53
-23
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +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.
46+
- cosmwasm-std: `Uint256::new`/`Uint512::new` now take an `u128` argument
47+
instead of bytes. Use `::from_be_bytes` if you need the old behaviour.
4848

4949
## Fixed
5050

packages/std/src/math/uint512.rs

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ use super::num_consts::NumConsts;
2828
///
2929
/// # Examples
3030
///
31-
/// Use `from` to create instances out of primitive uint types or `new` to provide big
32-
/// endian bytes:
31+
/// Use `new` to create instances out of u128, `from` for other primitive uint types
32+
/// or `from_be_bytes` to provide big endian bytes:
3333
///
3434
/// ```
3535
/// # use cosmwasm_std::Uint512;
36-
/// let a = Uint512::from(258u128);
37-
/// let b = Uint512::new([
36+
/// let a = Uint512::new(258u128);
37+
/// let b = Uint512::from(258u16);
38+
/// let c = Uint512::from_be_bytes([
3839
/// 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
3940
/// 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
4041
/// 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
@@ -45,6 +46,7 @@ use super::num_consts::NumConsts;
4546
/// 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 1u8, 2u8,
4647
/// ]);
4748
/// assert_eq!(a, b);
49+
/// assert_eq!(a, c);
4850
/// ```
4951
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, schemars::JsonSchema)]
5052
pub struct Uint512(#[schemars(with = "String")] pub(crate) U512);
@@ -56,10 +58,21 @@ impl Uint512 {
5658
pub const MAX: Uint512 = Uint512(U512::MAX);
5759
pub const MIN: Uint512 = Uint512(U512::ZERO);
5860

59-
/// Creates a Uint512(value) from a big endian representation. It's just an alias for
60-
/// `from_be_bytes`.
61-
pub const fn new(value: [u8; 64]) -> Self {
62-
Self::from_be_bytes(value)
61+
/// Creates a Uint512(value).
62+
///
63+
/// This method is less flexible than `from` but can be called in a const context.
64+
///
65+
/// Before CosmWasm 3 this took a byte array as an argument. You can get this behaviour
66+
/// with [`from_be_bytes`].
67+
///
68+
/// [`from_be_bytes`]: Self::from_be_bytes
69+
pub const fn new(value: u128) -> Self {
70+
let b = value.to_be_bytes();
71+
Self::from_be_bytes([
72+
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,
73+
0, 0, 0, 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],
74+
b[5], b[6], b[7], b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15],
75+
])
6376
}
6477

6578
/// Creates a Uint512(0)
@@ -622,7 +635,26 @@ mod tests {
622635

623636
#[test]
624637
fn uint512_new_works() {
625-
let num = Uint512::new([1; 64]);
638+
let num = Uint512::new(1);
639+
assert_eq!(
640+
num.to_be_bytes(),
641+
[
642+
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,
643+
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,
644+
0, 0, 0, 0, 0, 0, 0, 1
645+
]
646+
);
647+
648+
for v in [0, 1, 18, 875786576, u128::MAX] {
649+
// From is implemented by bnum, so we test two independent implementations against each other
650+
let uut = Uint512::new(v);
651+
assert_eq!(uut, Uint512::from(v));
652+
}
653+
}
654+
655+
#[test]
656+
fn uint512_from_be_bytes_works() {
657+
let num = Uint512::from_be_bytes([1; 64]);
626658
let a: [u8; 64] = num.to_be_bytes();
627659
assert_eq!(a, [1; 64]);
628660

@@ -632,14 +664,14 @@ mod tests {
632664
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
633665
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 1u8, 2u8, 3u8,
634666
];
635-
let num = Uint512::new(be_bytes);
667+
let num = Uint512::from_be_bytes(be_bytes);
636668
let resulting_bytes: [u8; 64] = num.to_be_bytes();
637669
assert_eq!(be_bytes, resulting_bytes);
638670
}
639671

640672
#[test]
641673
fn uint512_not_works() {
642-
let num = Uint512::new([1; 64]);
674+
let num = Uint512::from_be_bytes([1; 64]);
643675
let a = (!num).to_be_bytes();
644676
assert_eq!(a, [254; 64]);
645677

@@ -689,12 +721,10 @@ mod tests {
689721
];
690722

691723
// These should all be the same.
692-
let num1 = Uint512::new(be_bytes);
693-
let num2 = Uint512::from_be_bytes(be_bytes);
694-
let num3 = Uint512::from_le_bytes(le_bytes);
695-
assert_eq!(num1, Uint512::from(65536u32 + 512 + 3));
696-
assert_eq!(num1, num2);
697-
assert_eq!(num1, num3);
724+
let a = Uint512::from_be_bytes(be_bytes);
725+
let b = Uint512::from_le_bytes(le_bytes);
726+
assert_eq!(a, Uint512::from(65536u32 + 512 + 3));
727+
assert_eq!(a, b);
698728
}
699729

700730
#[test]
@@ -1140,14 +1170,14 @@ mod tests {
11401170

11411171
#[test]
11421172
fn uint512_shr_works() {
1143-
let original = Uint512::new([
1173+
let original = Uint512::from_be_bytes([
11441174
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
11451175
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
11461176
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
11471177
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 2u8, 0u8, 4u8, 2u8,
11481178
]);
11491179

1150-
let shifted = Uint512::new([
1180+
let shifted = Uint512::from_be_bytes([
11511181
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
11521182
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
11531183
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
@@ -1165,14 +1195,14 @@ mod tests {
11651195

11661196
#[test]
11671197
fn uint512_shl_works() {
1168-
let original = Uint512::new([
1198+
let original = Uint512::from_be_bytes([
11691199
64u8, 128u8, 1u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
11701200
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
11711201
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
11721202
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
11731203
]);
11741204

1175-
let shifted = Uint512::new([
1205+
let shifted = Uint512::from_be_bytes([
11761206
2u8, 0u8, 4u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
11771207
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
11781208
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,

0 commit comments

Comments
 (0)