diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e30584dcb..045d57213b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to ## [Unreleased] +### Added + +- cosmwasm-std: Add `Uint{64,128,256,512}::one`. + ## [1.0.0] - 2022-05-14 ### Added diff --git a/packages/std/src/math/uint128.rs b/packages/std/src/math/uint128.rs index 33723f9dbd..11d214bddd 100644 --- a/packages/std/src/math/uint128.rs +++ b/packages/std/src/math/uint128.rs @@ -49,6 +49,12 @@ impl Uint128 { Uint128(0) } + /// Creates a Uint128(1) + #[inline] + pub const fn one() -> Self { + Self(1) + } + /// Returns a copy of the internal data pub const fn u128(&self) -> u128 { self.0 @@ -502,6 +508,24 @@ mod tests { use super::*; use crate::{from_slice, to_vec}; + #[test] + fn uint128_zero_works() { + let zero = Uint128::zero(); + assert_eq!( + zero.to_be_bytes(), + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] + ); + } + + #[test] + fn uint128_one_works() { + let one = Uint128::one(); + assert_eq!( + one.to_be_bytes(), + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1] + ); + } + #[test] fn uint128_convert_into() { let original = Uint128(12345); diff --git a/packages/std/src/math/uint256.rs b/packages/std/src/math/uint256.rs index 0bb423826e..a4b7c4b002 100644 --- a/packages/std/src/math/uint256.rs +++ b/packages/std/src/math/uint256.rs @@ -66,6 +66,15 @@ impl Uint256 { Uint256(U256::zero()) } + /// Creates a Uint256(1) + #[inline] + pub const fn one() -> Self { + Self::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, + ]) + } + pub const fn from_be_bytes(data: [u8; 32]) -> Self { let words: [u64; 4] = [ u64::from_le_bytes([ @@ -597,7 +606,7 @@ mod tests { use crate::{from_slice, to_vec}; #[test] - fn uint256_construct() { + fn uint256_new_works() { let num = Uint256::new([1; 32]); let a: [u8; 32] = num.to_be_bytes(); assert_eq!(a, [1; 32]); @@ -611,6 +620,30 @@ mod tests { assert_eq!(be_bytes, resulting_bytes); } + #[test] + fn uint256_zero_works() { + let zero = Uint256::zero(); + assert_eq!( + zero.to_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 + ] + ); + } + + #[test] + fn uin256_one_works() { + let one = Uint256::one(); + assert_eq!( + one.to_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, + ] + ); + } + #[test] fn uint256_from_be_bytes() { let a = Uint256::from_be_bytes([ diff --git a/packages/std/src/math/uint512.rs b/packages/std/src/math/uint512.rs index 6a1069aa9e..a8779b7c91 100644 --- a/packages/std/src/math/uint512.rs +++ b/packages/std/src/math/uint512.rs @@ -66,6 +66,16 @@ impl Uint512 { Uint512(U512::zero()) } + /// Creates a Uint512(1) + #[inline] + pub const fn one() -> Self { + Self::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, 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, + ]) + } + pub const fn from_be_bytes(data: [u8; 64]) -> Self { let words: [u64; 8] = [ u64::from_le_bytes([ @@ -557,7 +567,7 @@ mod tests { use crate::{from_slice, to_vec}; #[test] - fn uint512_construct() { + fn uint512_new_works() { let num = Uint512::new([1; 64]); let a: [u8; 64] = num.to_be_bytes(); assert_eq!(a, [1; 64]); @@ -573,6 +583,32 @@ mod tests { assert_eq!(be_bytes, resulting_bytes); } + #[test] + fn uint512_zero_works() { + let zero = Uint512::zero(); + assert_eq!( + zero.to_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, 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 + ] + ); + } + + #[test] + fn uin512_one_works() { + let one = Uint512::one(); + assert_eq!( + one.to_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, 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 + ] + ); + } + #[test] fn uint512_endianness() { let be_bytes = [ diff --git a/packages/std/src/math/uint64.rs b/packages/std/src/math/uint64.rs index 906e58f3c1..cfe437f995 100644 --- a/packages/std/src/math/uint64.rs +++ b/packages/std/src/math/uint64.rs @@ -45,6 +45,12 @@ impl Uint64 { Uint64(0) } + /// Creates a Uint64(1) + #[inline] + pub const fn one() -> Self { + Self(1) + } + /// Returns a copy of the internal data pub const fn u64(&self) -> u64 { self.0 @@ -456,6 +462,18 @@ mod tests { use super::*; use crate::{from_slice, to_vec}; + #[test] + fn uint64_zero_works() { + let zero = Uint64::zero(); + assert_eq!(zero.to_be_bytes(), [0, 0, 0, 0, 0, 0, 0, 0]); + } + + #[test] + fn uint64_one_works() { + let one = Uint64::one(); + assert_eq!(one.to_be_bytes(), [0, 0, 0, 0, 0, 0, 0, 1]); + } + #[test] fn uint64_convert_into() { let original = Uint64(12345);