Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TryFrom< B512 > for b256, u256 #6010

Merged
merged 23 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
9fbb73c
init
SwayStar123 May 14, 2024
2c459ee
add imports
SwayStar123 May 14, 2024
5146d65
remove _b256
SwayStar123 May 14, 2024
cd683e4
move constants import to top lib.sw
SwayStar123 May 14, 2024
2fb0c24
move up b512 lib.sw
SwayStar123 May 14, 2024
a370d25
remove use of b512 in test
SwayStar123 May 14, 2024
04dcedd
add tryfrom import in u256
SwayStar123 May 14, 2024
509d573
import option
SwayStar123 May 14, 2024
af2edea
impl for u256 change b to u
SwayStar123 May 14, 2024
cc353a0
remove _u256 postfix
SwayStar123 May 15, 2024
8da38b9
reverse bits zero check
SwayStar123 May 15, 2024
07b80e7
add libs to reduced std lib
SwayStar123 May 15, 2024
817144d
Merge branch 'master' into tryfromb512foru256b256
SwayStar123 May 15, 2024
16df2b3
Merge branch 'master' into tryfromb512foru256b256
SwayStar123 May 15, 2024
50f9bfa
Apply suggestions from code review
SwayStar123 May 16, 2024
6afb906
Merge branch 'master' into tryfromb512foru256b256
SwayStar123 May 16, 2024
185f2f4
Update sway-lib-std/src/primitive_conversions/u256.sw
SwayStar123 May 17, 2024
fb7089a
Update sway-lib-std/src/primitive_conversions/b256.sw
SwayStar123 May 17, 2024
9c2d427
Merge branch 'master' into tryfromb512foru256b256
SwayStar123 May 17, 2024
092c31c
fix fmting
SwayStar123 May 17, 2024
a58d012
Merge branch 'master' into tryfromb512foru256b256
K1-R1 May 17, 2024
08f92c2
Merge branch 'master' into tryfromb512foru256b256
SwayStar123 May 20, 2024
d76ef36
Merge branch 'master' into tryfromb512foru256b256
SwayStar123 May 21, 2024
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: 2 additions & 2 deletions sway-lib-std/src/lib.sw
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
library;

pub mod constants;
pub mod error_signals;
pub mod logging;
pub mod revert;
Expand All @@ -16,17 +17,16 @@ pub mod bytes;
pub mod math;
pub mod flags;
pub mod u128;
pub mod b512;
pub mod primitive_conversions;
pub mod alias;
pub mod hash;
pub mod asset_id;
pub mod contract_id;
pub mod execution;
pub mod constants;
pub mod call_frames;
pub mod context;
pub mod external;
pub mod b512;
pub mod tx;
pub mod outputs;
pub mod address;
Expand Down
50 changes: 50 additions & 0 deletions sway-lib-std/src/primitive_conversions/b256.sw
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
library;

use ::bytes::Bytes;
use ::constants::ZERO_B256;
use ::convert::{From, TryFrom};
use ::option::Option::{self, *};
use ::u128::U128;
use ::b512::B512;

impl TryFrom<Bytes> for b256 {
fn try_from(b: Bytes) -> Option<Self> {
Expand All @@ -18,6 +20,41 @@ impl TryFrom<Bytes> for b256 {
}
}

impl TryFrom<B512> for b256 {
/// Attempts conversion from a `B512` to a `b256`.
///
/// # Additional Information
///
/// If the high bits of the `B512` are not zero, the conversion will fail.
SwayStar123 marked this conversation as resolved.
Show resolved Hide resolved
///
/// # Arguments
///
/// * `val`: [B512] - The `B512` to be converted.
///
/// # Returns
///
/// * [Option<b256>] - The `b256` representation of the `B512` value.
///
/// # Examples
///
/// ```sway
/// use std::b512::B512;
///
/// fn foo() {
/// let b512_value = B512::new();
/// let b256_value = b256::try_from(b512_value).unwrap();
/// }
/// ```
fn try_from(val: B512) -> Option<Self> {
let bits = val.bits();
if bits[0] == ZERO_B256 {
Some(bits[1])
} else {
None
}
}
}

impl From<u256> for b256 {
/// Casts a `u256` to raw `b256` data.
///
Expand Down Expand Up @@ -152,3 +189,16 @@ fn test_b256_from_tuple() {
b256_value == 0x0000000000000001000000000000000200000000000000030000000000000004,
);
}

#[test]
fn test_b256_try_from_b512() {
use ::assert::assert;

let b512_value = B512::new();
let b256_value = b256::try_from(b512_value);
assert(b256_value.is_some());

let b512_value = B512::from((0x0000000000000000000000000000000000000000000000000000000000000001, ZERO_B256));
let b256_value = b256::try_from(b512_value);
assert(b256_value.is_none());
}
53 changes: 52 additions & 1 deletion sway-lib-std/src/primitive_conversions/u256.sw
Original file line number Diff line number Diff line change
@@ -1,7 +1,45 @@
library;

use ::convert::From;
use ::constants::ZERO_B256;
use ::convert::{From, TryFrom};
use ::option::Option::{self, *};
use ::u128::U128;
use ::b512::B512;

impl TryFrom<B512> for u256 {
/// Attempts conversion from a `B512` to a `u256`.
///
/// # Additional Information
///
/// If the high bits of the `B512` are not zero, the conversion will fail.
SwayStar123 marked this conversation as resolved.
Show resolved Hide resolved
///
/// # Arguments
///
/// * `val`: [B512] - The `B512` to be converted.
///
/// # Returns
///
/// * [Option<u256>] - The `u256` representation of the `B512` value.
///
/// # Examples
///
/// ```sway
/// use std::b512::B512;
///
/// fn foo() {
/// let b512_value = B512::new();
/// let u256_value = u256::try_from(b512_value).unwrap();
/// }
/// ```
fn try_from(val: B512) -> Option<Self> {
let bits = val.bits();
if bits[0] == ZERO_B256 {
Some(bits[1].as_u256())
} else {
None
}
}
}

/// Functions for casting between `u256` and other types.
impl From<u8> for u256 {
Expand Down Expand Up @@ -255,3 +293,16 @@ fn test_u256_from_tuple() {
u256_value == 0x0000000000000001000000000000000200000000000000030000000000000004_u256,
);
}

#[test]
fn test_u256_try_from_b512() {
use ::assert::assert;

let b512_value = B512::new();
let u256_value = u256::try_from(b512_value);
assert(u256_value.is_some());

let b512_value = B512::from((0x0000000000000000000000000000000000000000000000000000000000000001, ZERO_B256));
let u256_value = u256::try_from(b512_value);
assert(u256_value.is_none());
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ registers.sw
flags.sw
math.sw
u128.sw
b512.sw
bytes_conversions.sw
bytes_conversions/b256.sw
bytes_conversions/u16.sw
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
library;

pub mod constants;
pub mod error_signals;
pub mod logging;
pub mod revert;
Expand All @@ -16,6 +17,7 @@ pub mod registers;
pub mod flags;
pub mod math;
pub mod u128;
pub mod b512;
pub mod primitive_conversions;
pub mod array_conversions;
pub mod bytes_conversions;
Expand Down
Loading