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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions packages/std/src/math/uint128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
35 changes: 34 additions & 1 deletion packages/std/src/math/uint256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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]);
Expand All @@ -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([
Expand Down
38 changes: 37 additions & 1 deletion packages/std/src/math/uint512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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]);
Expand All @@ -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 = [
Expand Down
18 changes: 18 additions & 0 deletions packages/std/src/math/uint64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down