@@ -30,19 +30,21 @@ use super::num_consts::NumConsts;
30
30
///
31
31
/// # Examples
32
32
///
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:
35
35
///
36
36
/// ```
37
37
/// # 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([
40
41
/// 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
41
42
/// 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
42
43
/// 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
43
44
/// 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 1u8, 2u8,
44
45
/// ]);
45
46
/// assert_eq!(a, b);
47
+ /// assert_eq!(a, c);
46
48
/// ```
47
49
#[ derive( Copy , Clone , Default , Debug , PartialEq , Eq , PartialOrd , Ord , schemars:: JsonSchema ) ]
48
50
pub struct Uint256 ( #[ schemars( with = "String" ) ] pub ( crate ) U256 ) ;
@@ -54,12 +56,20 @@ impl Uint256 {
54
56
pub const MAX : Uint256 = Uint256 ( U256 :: MAX ) ;
55
57
pub const MIN : Uint256 = Uint256 ( U256 :: ZERO ) ;
56
58
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).
59
60
///
60
61
/// 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
+ ] )
63
73
}
64
74
65
75
/// Creates a Uint256(0)
@@ -649,22 +659,40 @@ mod tests {
649
659
650
660
#[ test]
651
661
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 ] ) ;
653
681
let a: [ u8 ; 32 ] = num. to_be_bytes ( ) ;
654
682
assert_eq ! ( a, [ 1 ; 32 ] ) ;
655
683
656
684
let be_bytes = [
657
685
0u8 , 222u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 ,
658
686
0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 1u8 , 2u8 , 3u8 ,
659
687
] ;
660
- let num = Uint256 :: new ( be_bytes) ;
688
+ let num = Uint256 :: from_be_bytes ( be_bytes) ;
661
689
let resulting_bytes: [ u8 ; 32 ] = num. to_be_bytes ( ) ;
662
690
assert_eq ! ( be_bytes, resulting_bytes) ;
663
691
}
664
692
665
693
#[ test]
666
694
fn uint256_not_works ( ) {
667
- let num = Uint256 :: new ( [ 1 ; 32 ] ) ;
695
+ let num = Uint256 :: from_be_bytes ( [ 1 ; 32 ] ) ;
668
696
let a = ( !num) . to_be_bytes ( ) ;
669
697
assert_eq ! ( a, [ 254 ; 32 ] ) ;
670
698
@@ -1000,12 +1028,10 @@ mod tests {
1000
1028
] ;
1001
1029
1002
1030
// 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) ;
1009
1035
}
1010
1036
1011
1037
#[ test]
@@ -1335,7 +1361,7 @@ mod tests {
1335
1361
#[ test]
1336
1362
#[ should_panic( expected = "attempt to add with overflow" ) ]
1337
1363
fn uint256_add_overflow_panics ( ) {
1338
- let max = Uint256 :: new ( [ 255u8 ; 32 ] ) ;
1364
+ let max = Uint256 :: from_be_bytes ( [ 255u8 ; 32 ] ) ;
1339
1365
let _ = max + Uint256 :: from ( 12u32 ) ;
1340
1366
}
1341
1367
@@ -1495,12 +1521,12 @@ mod tests {
1495
1521
1496
1522
#[ test]
1497
1523
fn uint256_shr_works ( ) {
1498
- let original = Uint256 :: new ( [
1524
+ let original = Uint256 :: from_be_bytes ( [
1499
1525
0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 ,
1500
1526
0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 2u8 , 0u8 , 4u8 , 2u8 ,
1501
1527
] ) ;
1502
1528
1503
- let shifted = Uint256 :: new ( [
1529
+ let shifted = Uint256 :: from_be_bytes ( [
1504
1530
0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 ,
1505
1531
0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 128u8 , 1u8 , 0u8 ,
1506
1532
] ) ;
@@ -1516,12 +1542,12 @@ mod tests {
1516
1542
1517
1543
#[ test]
1518
1544
fn uint256_shl_works ( ) {
1519
- let original = Uint256 :: new ( [
1545
+ let original = Uint256 :: from_be_bytes ( [
1520
1546
64u8 , 128u8 , 1u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 ,
1521
1547
0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 ,
1522
1548
] ) ;
1523
1549
1524
- let shifted = Uint256 :: new ( [
1550
+ let shifted = Uint256 :: from_be_bytes ( [
1525
1551
2u8 , 0u8 , 4u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 ,
1526
1552
0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 , 0u8 ,
1527
1553
] ) ;
0 commit comments