Skip to content

Commit 905bb49

Browse files
authored
Add u256 conversions (#5767)
## Description Adds conversion methods from u8, u16, u32, u64, b256 to u256, and u256 to b256 Closes #4800 ## Checklist - [ ] I have linked to any relevant issues. - [ ] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [ ] I have requested a review from the relevant team or maintainers.
1 parent 41f31d3 commit 905bb49

File tree

4 files changed

+214
-2
lines changed

4 files changed

+214
-2
lines changed

sway-lib-std/src/primitive_conversions.sw

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ pub mod u8;
66
pub mod u16;
77
pub mod u32;
88
pub mod u64;
9+
pub mod u256;

sway-lib-std/src/primitive_conversions/b256.sw

+31-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
library;
22

33
use ::bytes::Bytes;
4-
use ::convert::TryFrom;
4+
use ::convert::{From, TryFrom};
55
use ::option::Option::{self, *};
66

77
impl TryFrom<Bytes> for b256 {
@@ -17,8 +17,28 @@ impl TryFrom<Bytes> for b256 {
1717
}
1818
}
1919

20+
impl From<u256> for b256 {
21+
/// Casts a `u256` to raw `b256` data.
22+
///
23+
/// # Returns
24+
///
25+
/// * [b256] - The underlying raw `b256` data of the `u256`.
26+
///
27+
/// # Examples
28+
///
29+
/// ```sway
30+
///
31+
/// fn foo() {
32+
/// let b256_value = b256::from(0x0000000000000000000000000000000000000000000000000000000000000000_u256);
33+
/// }
34+
/// ```
35+
fn from(num: u256) -> Self {
36+
num.as_b256()
37+
}
38+
}
39+
2040
#[test]
21-
fn test_b256_try_from() {
41+
fn test_b256_try_from_bytes() {
2242
use ::assert::assert;
2343

2444
let mut initial_bytes = Bytes::with_capacity(32);
@@ -47,3 +67,12 @@ fn test_b256_try_from() {
4767
assert(second_bytes.len() == 33);
4868
assert(second_bytes.capacity() == 33);
4969
}
70+
71+
#[test]
72+
fn test_b256_from_u256() {
73+
use ::assert::assert;
74+
75+
let val = 0x0000000000000000000000000000000000000000000000000000000000000000_u256;
76+
let res = b256::from(val);
77+
assert(res == 0x0000000000000000000000000000000000000000000000000000000000000000);
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
library;
2+
3+
use ::convert::From;
4+
5+
/// Functions for casting between `u256` and other types.
6+
impl From<u8> for u256 {
7+
/// Casts a `u8` to a `u256`.
8+
///
9+
/// # Arguments
10+
///
11+
/// * `num`: [u8] - The `u8` to be casted.
12+
///
13+
/// # Returns
14+
///
15+
/// * [u256] - The `u256` representation of the `u8` value.
16+
///
17+
/// # Examples
18+
///
19+
/// ```sway
20+
///
21+
/// fn foo() {
22+
/// let u256_value = u256::from(255_u8);
23+
/// }
24+
/// ```
25+
fn from(num: u8) -> Self {
26+
num.as_u256()
27+
}
28+
}
29+
30+
impl From<u16> for u256 {
31+
/// Casts a `u16` to a `u256`.
32+
///
33+
/// # Arguments
34+
///
35+
/// * `num`: [u16] - The `u16` to be casted.
36+
///
37+
/// # Returns
38+
///
39+
/// * [u256] - The `u256` representation of the `u16` value.
40+
///
41+
/// # Examples
42+
///
43+
/// ```sway
44+
///
45+
/// fn foo() {
46+
/// let u256_value = u256::from(65535_u16);
47+
/// }
48+
/// ```
49+
fn from(num: u16) -> Self {
50+
num.as_u256()
51+
}
52+
}
53+
54+
impl From<u32> for u256 {
55+
/// Casts a `u32` to a `u256`.
56+
///
57+
/// # Arguments
58+
///
59+
/// * `num`: [u32] - The `u32` to be casted.
60+
///
61+
/// # Returns
62+
///
63+
/// * [u256] - The `u256` representation of the `u32` value.
64+
///
65+
/// # Examples
66+
///
67+
/// ```sway
68+
///
69+
/// fn foo() {
70+
/// let u256_value = u256::from(4294967295_u32);
71+
/// }
72+
/// ```
73+
fn from(num: u32) -> Self {
74+
num.as_u256()
75+
}
76+
}
77+
78+
impl From<u64> for u256 {
79+
/// Casts a `u64` to a `u256`.
80+
///
81+
/// # Arguments
82+
///
83+
/// * `num`: [u64] - The `u64` to be casted.
84+
///
85+
/// # Returns
86+
///
87+
/// * [u256] - The `u256` representation of the `u64` value.
88+
///
89+
/// # Examples
90+
///
91+
/// ```sway
92+
///
93+
/// fn foo() {
94+
/// let u256_value = u256::from(18446744073709551615_u64);
95+
/// }
96+
/// ```
97+
fn from(num: u64) -> Self {
98+
num.as_u256()
99+
}
100+
}
101+
102+
impl From<b256> for u256 {
103+
/// Casts raw `b256` data to a `u256`.
104+
///
105+
/// # Arguments
106+
///
107+
/// * `bits`: [b256] - The raw `b256` data to be casted.
108+
///
109+
/// # Returns
110+
///
111+
/// * [u256] - The `u256` representation of the raw `b256`.
112+
///
113+
/// # Examples
114+
///
115+
/// ```sway
116+
/// use std::constants::ZERO_B256;
117+
///
118+
/// fn foo() {
119+
/// let u256_value = u256::from(ZERO_B256);
120+
/// }
121+
/// ```
122+
fn from(bits: b256) -> Self {
123+
bits.as_u256()
124+
}
125+
}
126+
127+
// TODO: Replace <u256 as From<T>> with u256::from when https://github.com/FuelLabs/sway/issues/5798 is resolved.
128+
#[test]
129+
fn test_u256_from_u8() {
130+
use ::assert::assert;
131+
132+
let u256_value = <u256 as From<u8>>::from(255_u8);
133+
assert(
134+
u256_value == 0x00000000000000000000000000000000000000000000000000000000000000ff_u256,
135+
);
136+
}
137+
138+
#[test]
139+
fn test_u256_from_u16() {
140+
use ::assert::assert;
141+
142+
let u256_value = <u256 as From<u16>>::from(65535_u16);
143+
assert(
144+
u256_value == 0x000000000000000000000000000000000000000000000000000000000000ffff_u256,
145+
);
146+
}
147+
148+
#[test]
149+
fn test_u256_from_u32() {
150+
use ::assert::assert;
151+
152+
let u256_value = <u256 as From<u32>>::from(4294967295_u32);
153+
assert(
154+
u256_value == 0x00000000000000000000000000000000000000000000000000000000ffffffff_u256,
155+
);
156+
}
157+
158+
#[test]
159+
fn test_u256_from_u64() {
160+
use ::assert::assert;
161+
162+
let u256_value = <u256 as From<u64>>::from(18446744073709551615_u64);
163+
assert(
164+
u256_value == 0x000000000000000000000000000000000000000000000000ffffffffffffffff_u256,
165+
);
166+
}
167+
168+
#[test]
169+
fn test_u256_from_b256() {
170+
use ::assert::assert;
171+
172+
let u256_value = <u256 as From<b256>>::from(0x0000000000000000000000000000000000000000000000000000000000000000);
173+
assert(
174+
u256_value == 0x0000000000000000000000000000000000000000000000000000000000000000_u256,
175+
);
176+
177+
let u256_value = <u256 as From<b256>>::from(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff);
178+
assert(
179+
u256_value == 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_u256,
180+
);
181+
}

test/src/e2e_vm_tests/reduced_std_libs/sway-lib-std-conversions/reduced_lib.config

+1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ primitive_conversions/str.sw
2929
primitive_conversions/u16.sw
3030
primitive_conversions/u32.sw
3131
primitive_conversions/u64.sw
32+
primitive_conversions/u256.sw
3233
primitive_conversions/u8.sw

0 commit comments

Comments
 (0)