From 77bca80b7c5f76fcb910c868eed5ec8b2cbbfb5e Mon Sep 17 00:00:00 2001 From: Jakub Bogucki Date: Tue, 29 Mar 2022 12:40:56 +0200 Subject: [PATCH 1/4] Add const implementations for is_zero/atomics/decimal_places methods --- packages/std/src/math/decimal.rs | 6 +-- packages/std/src/math/decimal256.rs | 6 +-- packages/std/src/math/uint128.rs | 2 +- packages/std/src/math/uint256.rs | 36 ++++++++++++++- packages/std/src/math/uint512.rs | 68 ++++++++++++++++++++++++++++- packages/std/src/math/uint64.rs | 2 +- 6 files changed, 108 insertions(+), 12 deletions(-) diff --git a/packages/std/src/math/decimal.rs b/packages/std/src/math/decimal.rs index c7d78c97eb..d5522db999 100644 --- a/packages/std/src/math/decimal.rs +++ b/packages/std/src/math/decimal.rs @@ -119,7 +119,7 @@ impl Decimal { ) } - pub fn is_zero(&self) -> bool { + pub const fn is_zero(&self) -> bool { self.0.is_zero() } @@ -141,7 +141,7 @@ impl Decimal { /// assert_eq!(b.decimal_places(), 18); /// assert_eq!(b.atomics(), Uint128::new(1)); /// ``` - pub fn atomics(&self) -> Uint128 { + pub const fn atomics(&self) -> Uint128 { self.0 } @@ -149,7 +149,7 @@ impl Decimal { /// but this could potentially change as the type evolves. /// /// See also [`Decimal::atomics()`]. - pub fn decimal_places(&self) -> u32 { + pub const fn decimal_places(&self) -> u32 { Self::DECIMAL_PLACES as u32 } diff --git a/packages/std/src/math/decimal256.rs b/packages/std/src/math/decimal256.rs index 98c5059214..a71d29fdd8 100644 --- a/packages/std/src/math/decimal256.rs +++ b/packages/std/src/math/decimal256.rs @@ -131,7 +131,7 @@ impl Decimal256 { ) } - pub fn is_zero(&self) -> bool { + pub const fn is_zero(&self) -> bool { self.0.is_zero() } @@ -153,7 +153,7 @@ impl Decimal256 { /// assert_eq!(b.decimal_places(), 18); /// assert_eq!(b.atomics(), Uint256::from(1u128)); /// ``` - pub fn atomics(&self) -> Uint256 { + pub const fn atomics(&self) -> Uint256 { self.0 } @@ -161,7 +161,7 @@ impl Decimal256 { /// but this could potentially change as the type evolves. /// /// See also [`Decimal256::atomics()`]. - pub fn decimal_places(&self) -> u32 { + pub const fn decimal_places(&self) -> u32 { Self::DECIMAL_PLACES as u32 } diff --git a/packages/std/src/math/uint128.rs b/packages/std/src/math/uint128.rs index 2cde35fdd2..1cf031e52a 100644 --- a/packages/std/src/math/uint128.rs +++ b/packages/std/src/math/uint128.rs @@ -63,7 +63,7 @@ impl Uint128 { self.0.to_le_bytes() } - pub fn is_zero(&self) -> bool { + pub const fn is_zero(&self) -> bool { self.0 == 0 } diff --git a/packages/std/src/math/uint256.rs b/packages/std/src/math/uint256.rs index 7b506b1f23..6d6f614117 100644 --- a/packages/std/src/math/uint256.rs +++ b/packages/std/src/math/uint256.rs @@ -206,8 +206,40 @@ impl Uint256 { ] } - pub fn is_zero(&self) -> bool { - self.0.is_zero() + pub const fn is_zero(&self) -> bool { + let bytes = self.to_be_bytes(); + bytes[0] == 0 + && bytes[1] == 0 + && bytes[2] == 0 + && bytes[3] == 0 + && bytes[4] == 0 + && bytes[5] == 0 + && bytes[6] == 0 + && bytes[7] == 0 + && bytes[8] == 0 + && bytes[9] == 0 + && bytes[10] == 0 + && bytes[11] == 0 + && bytes[12] == 0 + && bytes[13] == 0 + && bytes[14] == 0 + && bytes[15] == 0 + && bytes[16] == 0 + && bytes[17] == 0 + && bytes[18] == 0 + && bytes[19] == 0 + && bytes[20] == 0 + && bytes[21] == 0 + && bytes[22] == 0 + && bytes[23] == 0 + && bytes[24] == 0 + && bytes[25] == 0 + && bytes[26] == 0 + && bytes[27] == 0 + && bytes[28] == 0 + && bytes[29] == 0 + && bytes[30] == 0 + && bytes[31] == 0 } pub fn pow(self, exp: u32) -> Self { diff --git a/packages/std/src/math/uint512.rs b/packages/std/src/math/uint512.rs index 7c7aa0fb52..91e4ec87d6 100644 --- a/packages/std/src/math/uint512.rs +++ b/packages/std/src/math/uint512.rs @@ -305,8 +305,72 @@ impl Uint512 { ] } - pub fn is_zero(&self) -> bool { - self.0.is_zero() + pub const fn is_zero(&self) -> bool { + let bytes = self.to_be_bytes(); + bytes[0] == 0 + && bytes[1] == 0 + && bytes[2] == 0 + && bytes[3] == 0 + && bytes[4] == 0 + && bytes[5] == 0 + && bytes[6] == 0 + && bytes[7] == 0 + && bytes[8] == 0 + && bytes[9] == 0 + && bytes[10] == 0 + && bytes[11] == 0 + && bytes[12] == 0 + && bytes[13] == 0 + && bytes[14] == 0 + && bytes[15] == 0 + && bytes[16] == 0 + && bytes[17] == 0 + && bytes[18] == 0 + && bytes[19] == 0 + && bytes[20] == 0 + && bytes[21] == 0 + && bytes[22] == 0 + && bytes[23] == 0 + && bytes[24] == 0 + && bytes[25] == 0 + && bytes[26] == 0 + && bytes[27] == 0 + && bytes[28] == 0 + && bytes[29] == 0 + && bytes[30] == 0 + && bytes[31] == 0 + && bytes[32] == 0 + && bytes[33] == 0 + && bytes[34] == 0 + && bytes[35] == 0 + && bytes[36] == 0 + && bytes[37] == 0 + && bytes[38] == 0 + && bytes[39] == 0 + && bytes[40] == 0 + && bytes[41] == 0 + && bytes[42] == 0 + && bytes[43] == 0 + && bytes[44] == 0 + && bytes[45] == 0 + && bytes[46] == 0 + && bytes[47] == 0 + && bytes[48] == 0 + && bytes[49] == 0 + && bytes[50] == 0 + && bytes[51] == 0 + && bytes[52] == 0 + && bytes[53] == 0 + && bytes[54] == 0 + && bytes[55] == 0 + && bytes[56] == 0 + && bytes[57] == 0 + && bytes[58] == 0 + && bytes[59] == 0 + && bytes[60] == 0 + && bytes[61] == 0 + && bytes[62] == 0 + && bytes[63] == 0 } pub fn pow(self, exp: u32) -> Self { diff --git a/packages/std/src/math/uint64.rs b/packages/std/src/math/uint64.rs index a719d90613..8ed7759b19 100644 --- a/packages/std/src/math/uint64.rs +++ b/packages/std/src/math/uint64.rs @@ -59,7 +59,7 @@ impl Uint64 { self.0.to_le_bytes() } - pub fn is_zero(&self) -> bool { + pub const fn is_zero(&self) -> bool { self.0 == 0 } From f16d4fe1dbd33b5392fbc75cdf5b29127723688e Mon Sep 17 00:00:00 2001 From: Jakub Bogucki Date: Tue, 29 Mar 2022 13:34:01 +0200 Subject: [PATCH 2/4] Uint512 makes contructor const --- packages/std/src/math/uint512.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/std/src/math/uint512.rs b/packages/std/src/math/uint512.rs index 91e4ec87d6..7480533315 100644 --- a/packages/std/src/math/uint512.rs +++ b/packages/std/src/math/uint512.rs @@ -58,7 +58,7 @@ impl Uint512 { /// Creates a Uint512(value) from a big endian representation. It's just an alias for /// `from_big_endian`. - pub fn new(value: [u8; 64]) -> Self { + pub const fn new(value: [u8; 64]) -> Self { Self::from_be_bytes(value) } From 47c3227cb0deffd7bc924d157b9c292f4e6a10bc Mon Sep 17 00:00:00 2001 From: Jakub Bogucki Date: Tue, 29 Mar 2022 13:35:00 +0200 Subject: [PATCH 3/4] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c8e4be7f25..03d3e50af2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,8 @@ and this project adheres to - cosmwasm-std: Implement `pow`/`checked_pow` for `Uint64`/`Uint128`/`Uint512`. - cosmwasm-std: Implement `SubAssign`/`AddAssign` for `Decimal`/`Decimal256`. - cosmwasm-std: Implement `MulAssign` for `Decimal`/`Decimal256`. +- cosmwasm-std: Implement `is_zero`/`atomics`/`decimal_places` as const for Uint + and Decimal types. - cosmwasm-crypto: Upgrade ed25519-zebra to version 3. ### Changed From fafb30abffcf282c24021108ad818ce8f128ef16 Mon Sep 17 00:00:00 2001 From: Jakub Bogucki Date: Wed, 30 Mar 2022 10:04:02 +0200 Subject: [PATCH 4/4] Use words instead of bytes in is_zero implementation inside Uint256/Uint512 --- packages/std/src/math/uint256.rs | 35 +-------------- packages/std/src/math/uint512.rs | 74 ++++---------------------------- 2 files changed, 11 insertions(+), 98 deletions(-) diff --git a/packages/std/src/math/uint256.rs b/packages/std/src/math/uint256.rs index 6d6f614117..ced2407874 100644 --- a/packages/std/src/math/uint256.rs +++ b/packages/std/src/math/uint256.rs @@ -207,39 +207,8 @@ impl Uint256 { } pub const fn is_zero(&self) -> bool { - let bytes = self.to_be_bytes(); - bytes[0] == 0 - && bytes[1] == 0 - && bytes[2] == 0 - && bytes[3] == 0 - && bytes[4] == 0 - && bytes[5] == 0 - && bytes[6] == 0 - && bytes[7] == 0 - && bytes[8] == 0 - && bytes[9] == 0 - && bytes[10] == 0 - && bytes[11] == 0 - && bytes[12] == 0 - && bytes[13] == 0 - && bytes[14] == 0 - && bytes[15] == 0 - && bytes[16] == 0 - && bytes[17] == 0 - && bytes[18] == 0 - && bytes[19] == 0 - && bytes[20] == 0 - && bytes[21] == 0 - && bytes[22] == 0 - && bytes[23] == 0 - && bytes[24] == 0 - && bytes[25] == 0 - && bytes[26] == 0 - && bytes[27] == 0 - && bytes[28] == 0 - && bytes[29] == 0 - && bytes[30] == 0 - && bytes[31] == 0 + let words = (self.0).0; + words[0] == 0 && words[1] == 0 && words[2] == 0 && words[3] == 0 } pub fn pow(self, exp: u32) -> Self { diff --git a/packages/std/src/math/uint512.rs b/packages/std/src/math/uint512.rs index 7480533315..42aba05b62 100644 --- a/packages/std/src/math/uint512.rs +++ b/packages/std/src/math/uint512.rs @@ -306,71 +306,15 @@ impl Uint512 { } pub const fn is_zero(&self) -> bool { - let bytes = self.to_be_bytes(); - bytes[0] == 0 - && bytes[1] == 0 - && bytes[2] == 0 - && bytes[3] == 0 - && bytes[4] == 0 - && bytes[5] == 0 - && bytes[6] == 0 - && bytes[7] == 0 - && bytes[8] == 0 - && bytes[9] == 0 - && bytes[10] == 0 - && bytes[11] == 0 - && bytes[12] == 0 - && bytes[13] == 0 - && bytes[14] == 0 - && bytes[15] == 0 - && bytes[16] == 0 - && bytes[17] == 0 - && bytes[18] == 0 - && bytes[19] == 0 - && bytes[20] == 0 - && bytes[21] == 0 - && bytes[22] == 0 - && bytes[23] == 0 - && bytes[24] == 0 - && bytes[25] == 0 - && bytes[26] == 0 - && bytes[27] == 0 - && bytes[28] == 0 - && bytes[29] == 0 - && bytes[30] == 0 - && bytes[31] == 0 - && bytes[32] == 0 - && bytes[33] == 0 - && bytes[34] == 0 - && bytes[35] == 0 - && bytes[36] == 0 - && bytes[37] == 0 - && bytes[38] == 0 - && bytes[39] == 0 - && bytes[40] == 0 - && bytes[41] == 0 - && bytes[42] == 0 - && bytes[43] == 0 - && bytes[44] == 0 - && bytes[45] == 0 - && bytes[46] == 0 - && bytes[47] == 0 - && bytes[48] == 0 - && bytes[49] == 0 - && bytes[50] == 0 - && bytes[51] == 0 - && bytes[52] == 0 - && bytes[53] == 0 - && bytes[54] == 0 - && bytes[55] == 0 - && bytes[56] == 0 - && bytes[57] == 0 - && bytes[58] == 0 - && bytes[59] == 0 - && bytes[60] == 0 - && bytes[61] == 0 - && bytes[62] == 0 - && bytes[63] == 0 + let words = (self.0).0; + words[0] == 0 + && words[1] == 0 + && words[2] == 0 + && words[3] == 0 + && words[4] == 0 + && words[5] == 0 + && words[6] == 0 + && words[7] == 0 } pub fn pow(self, exp: u32) -> Self {