Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to
- cosmwasm-std: Implement `Mul`/`MulAssign` for `Uint64`.
- cosmwasm-std: Implement `RemAssign` for
`Uint64`/`Uint128`/`Uint256`/`Uint512`.
- cosmwasm-std: Implement `pow`/`checked_pow` for `Uint64`/`Uint128`/`Uint512`.
- cosmwasm-crypto: Upgrade ed25519-zebra to version 3.

### Changed
Expand Down
50 changes: 32 additions & 18 deletions packages/std/src/math/uint128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ impl Uint128 {
self.0 == 0
}

pub fn pow(self, exp: u32) -> Self {
self.0.pow(exp).into()
}

pub fn checked_add(self, other: Self) -> Result<Self, OverflowError> {
self.0
.checked_add(other.0)
Expand Down Expand Up @@ -726,6 +730,18 @@ mod tests {
assert_eq!(a, Uint128::from(30u32));
}

#[test]
fn uint128_pow_works() {
assert_eq!(Uint128::from(2u32).pow(2), Uint128::from(4u32));
assert_eq!(Uint128::from(2u32).pow(10), Uint128::from(1024u32));
}

#[test]
#[should_panic]
fn uint128_pow_overflow_panics() {
Uint128::MAX.pow(2u32);
}

#[test]
fn uint128_multiply_ratio_works() {
let base = Uint128(500);
Expand Down Expand Up @@ -788,50 +804,48 @@ mod tests {
fn uint128_methods() {
// checked_*
assert!(matches!(
Uint128(u128::MAX).checked_add(Uint128(1)),
Uint128::MAX.checked_add(Uint128(1)),
Err(OverflowError { .. })
));
assert!(matches!(
Uint128(0).checked_sub(Uint128(1)),
Err(OverflowError { .. })
));
assert!(matches!(
Uint128(u128::MAX).checked_mul(Uint128(2)),
Uint128::MAX.checked_mul(Uint128(2)),
Err(OverflowError { .. })
));
assert!(matches!(
Uint128::MAX.checked_pow(2u32),
Err(OverflowError { .. })
));
assert!(matches!(
Uint128(u128::MAX).checked_div(Uint128(0)),
Uint128::MAX.checked_div(Uint128(0)),
Err(DivideByZeroError { .. })
));
assert!(matches!(
Uint128(u128::MAX).checked_div_euclid(Uint128(0)),
Uint128::MAX.checked_div_euclid(Uint128(0)),
Err(DivideByZeroError { .. })
));
assert!(matches!(
Uint128(u128::MAX).checked_rem(Uint128(0)),
Uint128::MAX.checked_rem(Uint128(0)),
Err(DivideByZeroError { .. })
));

// saturating_*
assert_eq!(
Uint128(u128::MAX).saturating_add(Uint128(1)),
Uint128(u128::MAX)
);
assert_eq!(Uint128::MAX.saturating_add(Uint128(1)), Uint128::MAX);
assert_eq!(Uint128(0).saturating_sub(Uint128(1)), Uint128(0));
assert_eq!(
Uint128(u128::MAX).saturating_mul(Uint128(2)),
Uint128(u128::MAX)
);
assert_eq!(Uint128(u128::MAX).saturating_pow(2), Uint128(u128::MAX));
assert_eq!(Uint128::MAX.saturating_mul(Uint128(2)), Uint128::MAX);
assert_eq!(Uint128::MAX.saturating_pow(2), Uint128::MAX);

// wrapping_*
assert_eq!(Uint128(u128::MAX).wrapping_add(Uint128(1)), Uint128(0));
assert_eq!(Uint128(0).wrapping_sub(Uint128(1)), Uint128(u128::MAX));
assert_eq!(Uint128::MAX.wrapping_add(Uint128(1)), Uint128(0));
assert_eq!(Uint128(0).wrapping_sub(Uint128(1)), Uint128::MAX);
assert_eq!(
Uint128(u128::MAX).wrapping_mul(Uint128(2)),
Uint128::MAX.wrapping_mul(Uint128(2)),
Uint128(u128::MAX - 1)
);
assert_eq!(Uint128(u128::MAX).wrapping_pow(2), Uint128(1));
assert_eq!(Uint128::MAX.wrapping_pow(2), Uint128(1));
}

#[test]
Expand Down
40 changes: 28 additions & 12 deletions packages/std/src/math/uint256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ impl Uint256 {
self.0.is_zero()
}

pub fn pow(self, exp: u32) -> Self {
let res = self.0.pow(exp.into());
Self(res)
}

pub fn checked_add(self, other: Self) -> Result<Self, OverflowError> {
self.0
.checked_add(other.0)
Expand All @@ -231,6 +236,13 @@ impl Uint256 {
.ok_or_else(|| OverflowError::new(OverflowOperation::Mul, self, other))
}

pub fn checked_pow(self, exp: u32) -> Result<Self, OverflowError> {
self.0
.checked_pow(exp.into())
.map(Self)
.ok_or_else(|| OverflowError::new(OverflowOperation::Pow, self, exp))
}

pub fn checked_div(self, other: Self) -> Result<Self, DivideByZeroError> {
self.0
.checked_div(other.0)
Expand All @@ -245,18 +257,6 @@ impl Uint256 {
.ok_or_else(|| DivideByZeroError::new(self))
}

pub fn checked_pow(self, exp: u32) -> Result<Self, OverflowError> {
self.0
.checked_pow(exp.into())
.map(Self)
.ok_or_else(|| OverflowError::new(OverflowOperation::Pow, self, exp))
}

pub fn pow(self, exp: u32) -> Self {
self.checked_pow(exp)
.expect("attempt to raise to a power with overflow")
}

pub fn checked_shr(self, other: u32) -> Result<Self, OverflowError> {
if other >= 256 {
return Err(OverflowError::new(OverflowOperation::Shr, self, other));
Expand Down Expand Up @@ -1285,6 +1285,18 @@ mod tests {
assert_eq!(a, Uint256::from(30u32));
}

#[test]
fn uint256_pow_works() {
assert_eq!(Uint256::from(2u32).pow(2), Uint256::from(4u32));
assert_eq!(Uint256::from(2u32).pow(10), Uint256::from(1024u32));
}

#[test]
#[should_panic]
fn uint256_pow_overflow_panics() {
Uint256::MAX.pow(2u32);
}

#[test]
fn uint256_multiply_ratio_works() {
let base = Uint256::from(500u32);
Expand Down Expand Up @@ -1390,6 +1402,10 @@ mod tests {
Uint256::MAX.checked_mul(Uint256::from(2u32)),
Err(OverflowError { .. })
));
assert!(matches!(
Uint256::MAX.checked_pow(2u32),
Err(OverflowError { .. })
));
assert!(matches!(
Uint256::MAX.checked_div(Uint256::from(0u32)),
Err(DivideByZeroError { .. })
Expand Down
28 changes: 28 additions & 0 deletions packages/std/src/math/uint512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,11 @@ impl Uint512 {
self.0.is_zero()
}

pub fn pow(self, exp: u32) -> Self {
let res = self.0.pow(exp.into());
Self(res)
}

pub fn checked_add(self, other: Self) -> Result<Self, OverflowError> {
self.0
.checked_add(other.0)
Expand All @@ -330,6 +335,13 @@ impl Uint512 {
.ok_or_else(|| OverflowError::new(OverflowOperation::Mul, self, other))
}

pub fn checked_pow(self, exp: u32) -> Result<Self, OverflowError> {
self.0
.checked_pow(exp.into())
.map(Self)
.ok_or_else(|| OverflowError::new(OverflowOperation::Pow, self, exp))
}

pub fn checked_div(self, other: Self) -> Result<Self, DivideByZeroError> {
self.0
.checked_div(other.0)
Expand Down Expand Up @@ -1056,6 +1068,18 @@ mod tests {
assert_eq!(a, Uint512::from(30u32));
}

#[test]
fn uint512_pow_works() {
assert_eq!(Uint512::from(2u32).pow(2), Uint512::from(4u32));
assert_eq!(Uint512::from(2u32).pow(10), Uint512::from(1024u32));
}

#[test]
#[should_panic]
fn uint512_pow_overflow_panics() {
Uint512::MAX.pow(2u32);
}

#[test]
fn uint512_shr_works() {
let original = Uint512::new([
Expand Down Expand Up @@ -1113,6 +1137,10 @@ mod tests {
Uint512::MAX.checked_mul(Uint512::from(2u32)),
Err(OverflowError { .. })
));
assert!(matches!(
Uint512::MAX.checked_pow(2u32),
Err(OverflowError { .. })
));
assert!(matches!(
Uint512::MAX.checked_div(Uint512::from(0u32)),
Err(DivideByZeroError { .. })
Expand Down
54 changes: 39 additions & 15 deletions packages/std/src/math/uint64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ impl Uint64 {
self.0 == 0
}

pub fn pow(self, exp: u32) -> Self {
self.0.pow(exp).into()
}

pub fn checked_add(self, other: Self) -> Result<Self, OverflowError> {
self.0
.checked_add(other.0)
Expand All @@ -84,6 +88,13 @@ impl Uint64 {
.ok_or_else(|| OverflowError::new(OverflowOperation::Mul, self, other))
}

pub fn checked_pow(self, exp: u32) -> Result<Self, OverflowError> {
self.0
.checked_pow(exp)
.map(Self)
.ok_or_else(|| OverflowError::new(OverflowOperation::Pow, self, exp))
}

pub fn checked_div(self, other: Self) -> Result<Self, DivideByZeroError> {
self.0
.checked_div(other.0)
Expand Down Expand Up @@ -633,6 +644,18 @@ mod tests {
assert_eq!(a, Uint64::from(30u32));
}

#[test]
fn uint64_pow_works() {
assert_eq!(Uint64::from(2u32).pow(2), Uint64::from(4u32));
assert_eq!(Uint64::from(2u32).pow(10), Uint64::from(1024u32));
}

#[test]
#[should_panic]
fn uint64_pow_overflow_panics() {
Uint64::MAX.pow(2u32);
}

#[test]
#[should_panic]
fn uint64_math_overflow_panics() {
Expand Down Expand Up @@ -703,44 +726,45 @@ mod tests {
fn uint64_methods() {
// checked_*
assert!(matches!(
Uint64(u64::MAX).checked_add(Uint64(1)),
Uint64::MAX.checked_add(Uint64(1)),
Err(OverflowError { .. })
));
assert!(matches!(
Uint64(0).checked_sub(Uint64(1)),
Err(OverflowError { .. })
));
assert!(matches!(
Uint64(u64::MAX).checked_mul(Uint64(2)),
Uint64::MAX.checked_mul(Uint64(2)),
Err(OverflowError { .. })
));
assert!(matches!(
Uint64(u64::MAX).checked_div(Uint64(0)),
Uint64::MAX.checked_pow(2u32),
Err(OverflowError { .. })
));
assert!(matches!(
Uint64::MAX.checked_div(Uint64(0)),
Err(DivideByZeroError { .. })
));
assert!(matches!(
Uint64(u64::MAX).checked_div_euclid(Uint64(0)),
Uint64::MAX.checked_div_euclid(Uint64(0)),
Err(DivideByZeroError { .. })
));
assert!(matches!(
Uint64(u64::MAX).checked_rem(Uint64(0)),
Uint64::MAX.checked_rem(Uint64(0)),
Err(DivideByZeroError { .. })
));

// saturating_*
assert_eq!(Uint64(u64::MAX).saturating_add(Uint64(1)), Uint64(u64::MAX));
assert_eq!(Uint64::MAX.saturating_add(Uint64(1)), Uint64::MAX);
assert_eq!(Uint64(0).saturating_sub(Uint64(1)), Uint64(0));
assert_eq!(Uint64(u64::MAX).saturating_mul(Uint64(2)), Uint64(u64::MAX));
assert_eq!(Uint64(u64::MAX).saturating_pow(2), Uint64(u64::MAX));
assert_eq!(Uint64::MAX.saturating_mul(Uint64(2)), Uint64::MAX);
assert_eq!(Uint64::MAX.saturating_pow(2), Uint64::MAX);

// wrapping_*
assert_eq!(Uint64(u64::MAX).wrapping_add(Uint64(1)), Uint64(0));
assert_eq!(Uint64(0).wrapping_sub(Uint64(1)), Uint64(u64::MAX));
assert_eq!(
Uint64(u64::MAX).wrapping_mul(Uint64(2)),
Uint64(u64::MAX - 1)
);
assert_eq!(Uint64(u64::MAX).wrapping_pow(2), Uint64(1));
assert_eq!(Uint64::MAX.wrapping_add(Uint64(1)), Uint64(0));
assert_eq!(Uint64(0).wrapping_sub(Uint64(1)), Uint64::MAX);
assert_eq!(Uint64::MAX.wrapping_mul(Uint64(2)), Uint64(u64::MAX - 1));
assert_eq!(Uint64::MAX.wrapping_pow(2), Uint64(1));
}

#[test]
Expand Down