From fddf42aca0cf27b90546c23f5c5f92b69b97a2aa Mon Sep 17 00:00:00 2001 From: SwayStar123 Date: Mon, 25 Mar 2024 13:49:58 +0530 Subject: [PATCH 01/12] add u256 conversions --- sway-lib-std/src/lib.sw | 1 + sway-lib-std/src/u256.sw | 145 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 sway-lib-std/src/u256.sw diff --git a/sway-lib-std/src/lib.sw b/sway-lib-std/src/lib.sw index 7f52116b17c..4e594feace6 100644 --- a/sway-lib-std/src/lib.sw +++ b/sway-lib-std/src/lib.sw @@ -17,6 +17,7 @@ pub mod primitive_conversions; pub mod math; pub mod flags; pub mod u128; +pub mod u256; pub mod alias; pub mod hash; pub mod asset_id; diff --git a/sway-lib-std/src/u256.sw b/sway-lib-std/src/u256.sw new file mode 100644 index 00000000000..7741c529971 --- /dev/null +++ b/sway-lib-std/src/u256.sw @@ -0,0 +1,145 @@ +library; + +use ::convert::From; + +/// Functions for casting between `u256` and other types. +impl From for u256 { + /// Casts a `u8` to a `u256`. + /// + /// # Arguments + /// + /// * `num`: [u8] - The `u8` to be casted. + /// + /// # Returns + /// + /// * [u256] - The `u256` representation of the `u8` value. + /// + /// # Examples + /// + /// ```sway + /// + /// fn foo() { + /// let u256_value = u256::from(255_u8); + /// } + /// ``` + fn from(num: u8) -> Self { + num.as_u256() + } +} + +impl From for u256 { + /// Casts a `u16` to a `u256`. + /// + /// # Arguments + /// + /// * `num`: [u16] - The `u16` to be casted. + /// + /// # Returns + /// + /// * [u256] - The `u256` representation of the `u16` value. + /// + /// # Examples + /// + /// ```sway + /// + /// fn foo() { + /// let u256_value = u256::from(65535_u16); + /// } + /// ``` + fn from(num: u16) -> Self { + num.as_u256() + } +} + +impl From for u256 { + /// Casts a `u32` to a `u256`. + /// + /// # Arguments + /// + /// * `num`: [u32] - The `u32` to be casted. + /// + /// # Returns + /// + /// * [u256] - The `u256` representation of the `u32` value. + /// + /// # Examples + /// + /// ```sway + /// + /// fn foo() { + /// let u256_value = u256::from(4294967295_u32); + /// } + /// ``` + fn from(num: u32) -> Self { + num.as_u256() + } +} + +impl From for u256 { + /// Casts a `u64` to a `u256`. + /// + /// # Arguments + /// + /// * `num`: [u64] - The `u64` to be casted. + /// + /// # Returns + /// + /// * [u256] - The `u256` representation of the `u64` value. + /// + /// # Examples + /// + /// ```sway + /// + /// fn foo() { + /// let u256_value = u256::from(18446744073709551615_u64); + /// } + /// ``` + fn from(num: u64) -> Self { + num.as_u256() + } +} + +impl From for u256 { + /// Casts raw `b256` data to a `u256`. + /// + /// # Arguments + /// + /// * `bits`: [b256] - The raw `b256` data to be casted. + /// + /// # Returns + /// + /// * [u256] - The `u256` representation of the raw `b256`. + /// + /// # Examples + /// + /// ```sway + /// use std::constants::ZERO_B256; + /// + /// fn foo() { + /// let u256_value = u256::from(ZERO_B256); + /// } + /// ``` + fn from(bits: b256) -> Self { + bits.as_u256() + } +} + +impl From for b256 { + /// Casts a `u256` to raw `b256` data. + /// + /// # Returns + /// + /// * [b256] - The underlying raw `b256` data of the `u256`. + /// + /// # Examples + /// + /// ```sway + /// + /// fn foo() { + /// let b256_value = b256::from(0x0000000000000000000000000000000000000000000000000000000000000000_u256); + /// } + /// ``` + fn from(num: u256) -> Self { + num.as_b256() + } +} \ No newline at end of file From 1b27c5d3f7183a5af1bbe82e09f925638a655f86 Mon Sep 17 00:00:00 2001 From: SwayStar123 Date: Mon, 25 Mar 2024 14:16:36 +0530 Subject: [PATCH 02/12] add line at end --- sway-lib-std/src/u256.sw | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sway-lib-std/src/u256.sw b/sway-lib-std/src/u256.sw index 7741c529971..509a969dd47 100644 --- a/sway-lib-std/src/u256.sw +++ b/sway-lib-std/src/u256.sw @@ -142,4 +142,4 @@ impl From for b256 { fn from(num: u256) -> Self { num.as_b256() } -} \ No newline at end of file +} From 2f1ebec3817c7b8e3d2ccc632b50e7d74d0de50e Mon Sep 17 00:00:00 2001 From: SwayStar123 Date: Thu, 28 Mar 2024 12:15:00 +0530 Subject: [PATCH 03/12] add tests and move files --- .../src/primitive_conversions/b256.sw | 33 +++++++++++- .../src/{ => primitive_conversions}/u256.sw | 51 ++++++++++++------- 2 files changed, 64 insertions(+), 20 deletions(-) rename sway-lib-std/src/{ => primitive_conversions}/u256.sw (68%) diff --git a/sway-lib-std/src/primitive_conversions/b256.sw b/sway-lib-std/src/primitive_conversions/b256.sw index de4625cd3e1..b530259dcdd 100644 --- a/sway-lib-std/src/primitive_conversions/b256.sw +++ b/sway-lib-std/src/primitive_conversions/b256.sw @@ -1,7 +1,7 @@ library; use ::bytes::Bytes; -use ::convert::TryFrom; +use ::convert::{From, TryFrom}; use ::option::Option::{self, *}; impl TryFrom for b256 { @@ -17,8 +17,28 @@ impl TryFrom for b256 { } } +impl From for b256 { + /// Casts a `u256` to raw `b256` data. + /// + /// # Returns + /// + /// * [b256] - The underlying raw `b256` data of the `u256`. + /// + /// # Examples + /// + /// ```sway + /// + /// fn foo() { + /// let b256_value = b256::from(0x0000000000000000000000000000000000000000000000000000000000000000_u256); + /// } + /// ``` + fn from(num: u256) -> Self { + num.as_b256() + } +} + #[test] -fn test_b256_try_from() { +fn test_b256_try_from_bytes() { use ::assert::assert; let mut initial_bytes = Bytes::with_capacity(32); @@ -47,3 +67,12 @@ fn test_b256_try_from() { assert(second_bytes.len() == 33); assert(second_bytes.capacity() == 33); } + +#[test] +fn test_b256_from_u256() { + use ::assert::assert; + + let val = 0x0000000000000000000000000000000000000000000000000000000000000000_u256; + let res = b256::from(val); + assert(res == 0x0000000000000000000000000000000000000000000000000000000000000000); +} diff --git a/sway-lib-std/src/u256.sw b/sway-lib-std/src/primitive_conversions/u256.sw similarity index 68% rename from sway-lib-std/src/u256.sw rename to sway-lib-std/src/primitive_conversions/u256.sw index 509a969dd47..76fe652865f 100644 --- a/sway-lib-std/src/u256.sw +++ b/sway-lib-std/src/primitive_conversions/u256.sw @@ -124,22 +124,37 @@ impl From for u256 { } } -impl From for b256 { - /// Casts a `u256` to raw `b256` data. - /// - /// # Returns - /// - /// * [b256] - The underlying raw `b256` data of the `u256`. - /// - /// # Examples - /// - /// ```sway - /// - /// fn foo() { - /// let b256_value = b256::from(0x0000000000000000000000000000000000000000000000000000000000000000_u256); - /// } - /// ``` - fn from(num: u256) -> Self { - num.as_b256() - } +#[test] +fn test_u256_from_u8() { + let u256_value = u256::from(255_u8); + assert(u256_value == 0x00000000000000000000000000000000000000000000000000000000000000ff_u256); +} + +#[test] +fn test_u256_from_u16() { + let u256_value = u256::from(65535_u16); + assert(u256_value == 0x000000000000000000000000000000000000000000000000000000000000ffff_u256); +} + +#[test] +fn test_u256_from_u32() { + let u256_value = u256::from(4294967295_u32); + assert(u256_value == 0x00000000000000000000000000000000000000000000000000000000ffffffff_u256); +} + +#[test] +fn test_u256_from_u64() { + let u256_value = u256::from(18446744073709551615_u64); + assert(u256_value == 0x000000000000000000000000000000000000000000000000ffffffffffffffff_u256); +} + +#[test] +fn test_u256_from_b256() { + use std::constants::ZERO_B256; + + let u256_value = u256::from(ZERO_B256); + assert(u256_value == 0x0000000000000000000000000000000000000000000000000000000000000000_u256); + + let u256_value = u256::from(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); + assert(u256_value == 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_u256); } From ccd6e3b321ba85b82ed8f8714ff7e354d95350d3 Mon Sep 17 00:00:00 2001 From: SwayStar123 Date: Thu, 28 Mar 2024 12:15:40 +0530 Subject: [PATCH 04/12] add lib to primitive_conversions.sw --- sway-lib-std/src/lib.sw | 1 - sway-lib-std/src/primitive_conversions.sw | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/sway-lib-std/src/lib.sw b/sway-lib-std/src/lib.sw index 4e594feace6..7f52116b17c 100644 --- a/sway-lib-std/src/lib.sw +++ b/sway-lib-std/src/lib.sw @@ -17,7 +17,6 @@ pub mod primitive_conversions; pub mod math; pub mod flags; pub mod u128; -pub mod u256; pub mod alias; pub mod hash; pub mod asset_id; diff --git a/sway-lib-std/src/primitive_conversions.sw b/sway-lib-std/src/primitive_conversions.sw index 99d170048fa..6d3856bfa80 100644 --- a/sway-lib-std/src/primitive_conversions.sw +++ b/sway-lib-std/src/primitive_conversions.sw @@ -6,3 +6,4 @@ pub mod u8; pub mod u16; pub mod u32; pub mod u64; +pub mod u256; \ No newline at end of file From b734126e8f0eed28406254f152db7b041bf326b4 Mon Sep 17 00:00:00 2001 From: SwayStar123 Date: Thu, 28 Mar 2024 13:06:00 +0530 Subject: [PATCH 05/12] add assert import to tests --- sway-lib-std/src/primitive_conversions/u256.sw | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/sway-lib-std/src/primitive_conversions/u256.sw b/sway-lib-std/src/primitive_conversions/u256.sw index 76fe652865f..1c01083ac64 100644 --- a/sway-lib-std/src/primitive_conversions/u256.sw +++ b/sway-lib-std/src/primitive_conversions/u256.sw @@ -126,31 +126,40 @@ impl From for u256 { #[test] fn test_u256_from_u8() { + use ::assert::assert; + let u256_value = u256::from(255_u8); assert(u256_value == 0x00000000000000000000000000000000000000000000000000000000000000ff_u256); } #[test] fn test_u256_from_u16() { + use ::assert::assert; + let u256_value = u256::from(65535_u16); assert(u256_value == 0x000000000000000000000000000000000000000000000000000000000000ffff_u256); } #[test] fn test_u256_from_u32() { + use ::assert::assert; + let u256_value = u256::from(4294967295_u32); assert(u256_value == 0x00000000000000000000000000000000000000000000000000000000ffffffff_u256); } #[test] fn test_u256_from_u64() { + use ::assert::assert; + let u256_value = u256::from(18446744073709551615_u64); assert(u256_value == 0x000000000000000000000000000000000000000000000000ffffffffffffffff_u256); } #[test] fn test_u256_from_b256() { - use std::constants::ZERO_B256; + use ::assert::assert; + use ::constants::ZERO_B256; let u256_value = u256::from(ZERO_B256); assert(u256_value == 0x0000000000000000000000000000000000000000000000000000000000000000_u256); From badef5fa6a8729c1c5c56b7e04828febb0b0de47 Mon Sep 17 00:00:00 2001 From: SwayStar123 Date: Thu, 28 Mar 2024 13:49:35 +0530 Subject: [PATCH 06/12] add type hints --- sway-lib-std/src/primitive_conversions/u256.sw | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sway-lib-std/src/primitive_conversions/u256.sw b/sway-lib-std/src/primitive_conversions/u256.sw index 1c01083ac64..e0012bb08af 100644 --- a/sway-lib-std/src/primitive_conversions/u256.sw +++ b/sway-lib-std/src/primitive_conversions/u256.sw @@ -128,7 +128,7 @@ impl From for u256 { fn test_u256_from_u8() { use ::assert::assert; - let u256_value = u256::from(255_u8); + let u256_value = u256::from::(255_u8); assert(u256_value == 0x00000000000000000000000000000000000000000000000000000000000000ff_u256); } @@ -136,7 +136,7 @@ fn test_u256_from_u8() { fn test_u256_from_u16() { use ::assert::assert; - let u256_value = u256::from(65535_u16); + let u256_value = u256::from::(65535_u16); assert(u256_value == 0x000000000000000000000000000000000000000000000000000000000000ffff_u256); } @@ -144,7 +144,7 @@ fn test_u256_from_u16() { fn test_u256_from_u32() { use ::assert::assert; - let u256_value = u256::from(4294967295_u32); + let u256_value = u256::from::(4294967295_u32); assert(u256_value == 0x00000000000000000000000000000000000000000000000000000000ffffffff_u256); } @@ -152,7 +152,7 @@ fn test_u256_from_u32() { fn test_u256_from_u64() { use ::assert::assert; - let u256_value = u256::from(18446744073709551615_u64); + let u256_value = u256::from::(18446744073709551615_u64); assert(u256_value == 0x000000000000000000000000000000000000000000000000ffffffffffffffff_u256); } @@ -161,7 +161,7 @@ fn test_u256_from_b256() { use ::assert::assert; use ::constants::ZERO_B256; - let u256_value = u256::from(ZERO_B256); + let u256_value = u256::from::(ZERO_B256); assert(u256_value == 0x0000000000000000000000000000000000000000000000000000000000000000_u256); let u256_value = u256::from(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); From aa3812b83193c025554c1b144290498cbf420a5f Mon Sep 17 00:00:00 2001 From: SwayStar123 Date: Thu, 28 Mar 2024 14:01:18 +0530 Subject: [PATCH 07/12] try rust trait disambiguation --- sway-lib-std/src/primitive_conversions/u256.sw | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sway-lib-std/src/primitive_conversions/u256.sw b/sway-lib-std/src/primitive_conversions/u256.sw index e0012bb08af..a704680e2b0 100644 --- a/sway-lib-std/src/primitive_conversions/u256.sw +++ b/sway-lib-std/src/primitive_conversions/u256.sw @@ -128,7 +128,7 @@ impl From for u256 { fn test_u256_from_u8() { use ::assert::assert; - let u256_value = u256::from::(255_u8); + let u256_value = >::from(255_u8); assert(u256_value == 0x00000000000000000000000000000000000000000000000000000000000000ff_u256); } @@ -136,7 +136,7 @@ fn test_u256_from_u8() { fn test_u256_from_u16() { use ::assert::assert; - let u256_value = u256::from::(65535_u16); + let u256_value = >::from(65535_u16); assert(u256_value == 0x000000000000000000000000000000000000000000000000000000000000ffff_u256); } @@ -144,7 +144,7 @@ fn test_u256_from_u16() { fn test_u256_from_u32() { use ::assert::assert; - let u256_value = u256::from::(4294967295_u32); + let u256_value = >::from(4294967295_u32); assert(u256_value == 0x00000000000000000000000000000000000000000000000000000000ffffffff_u256); } @@ -152,7 +152,7 @@ fn test_u256_from_u32() { fn test_u256_from_u64() { use ::assert::assert; - let u256_value = u256::from::(18446744073709551615_u64); + let u256_value = >::from(18446744073709551615_u64); assert(u256_value == 0x000000000000000000000000000000000000000000000000ffffffffffffffff_u256); } @@ -161,9 +161,9 @@ fn test_u256_from_b256() { use ::assert::assert; use ::constants::ZERO_B256; - let u256_value = u256::from::(ZERO_B256); + let u256_value = >::from(ZERO_B256); assert(u256_value == 0x0000000000000000000000000000000000000000000000000000000000000000_u256); - let u256_value = u256::from(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); + let u256_value = >::from(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); assert(u256_value == 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_u256); } From a3db69a540b3feedc25bf18a60d4fed8321b95a5 Mon Sep 17 00:00:00 2001 From: SwayStar123 Date: Thu, 28 Mar 2024 14:11:07 +0530 Subject: [PATCH 08/12] add missing fs to b256 and fmt --- .../src/primitive_conversions/u256.sw | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/sway-lib-std/src/primitive_conversions/u256.sw b/sway-lib-std/src/primitive_conversions/u256.sw index a704680e2b0..694ff541921 100644 --- a/sway-lib-std/src/primitive_conversions/u256.sw +++ b/sway-lib-std/src/primitive_conversions/u256.sw @@ -129,7 +129,9 @@ fn test_u256_from_u8() { use ::assert::assert; let u256_value = >::from(255_u8); - assert(u256_value == 0x00000000000000000000000000000000000000000000000000000000000000ff_u256); + assert( + u256_value == 0x00000000000000000000000000000000000000000000000000000000000000ff_u256, + ); } #[test] @@ -137,7 +139,9 @@ fn test_u256_from_u16() { use ::assert::assert; let u256_value = >::from(65535_u16); - assert(u256_value == 0x000000000000000000000000000000000000000000000000000000000000ffff_u256); + assert( + u256_value == 0x000000000000000000000000000000000000000000000000000000000000ffff_u256, + ); } #[test] @@ -145,7 +149,9 @@ fn test_u256_from_u32() { use ::assert::assert; let u256_value = >::from(4294967295_u32); - assert(u256_value == 0x00000000000000000000000000000000000000000000000000000000ffffffff_u256); + assert( + u256_value == 0x00000000000000000000000000000000000000000000000000000000ffffffff_u256, + ); } #[test] @@ -153,7 +159,9 @@ fn test_u256_from_u64() { use ::assert::assert; let u256_value = >::from(18446744073709551615_u64); - assert(u256_value == 0x000000000000000000000000000000000000000000000000ffffffffffffffff_u256); + assert( + u256_value == 0x000000000000000000000000000000000000000000000000ffffffffffffffff_u256, + ); } #[test] @@ -162,8 +170,12 @@ fn test_u256_from_b256() { use ::constants::ZERO_B256; let u256_value = >::from(ZERO_B256); - assert(u256_value == 0x0000000000000000000000000000000000000000000000000000000000000000_u256); - - let u256_value = >::from(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); - assert(u256_value == 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_u256); + assert( + u256_value == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, + ); + + let u256_value = >::from(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); + assert( + u256_value == 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_u256, + ); } From bfbbfb641b5f890b049b8490410e06015b2178fe Mon Sep 17 00:00:00 2001 From: SwayStar123 Date: Thu, 28 Mar 2024 14:21:37 +0530 Subject: [PATCH 09/12] add todo and remove use of ::constants::ZEROB256 --- sway-lib-std/src/primitive_conversions/u256.sw | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sway-lib-std/src/primitive_conversions/u256.sw b/sway-lib-std/src/primitive_conversions/u256.sw index 694ff541921..630470c08bf 100644 --- a/sway-lib-std/src/primitive_conversions/u256.sw +++ b/sway-lib-std/src/primitive_conversions/u256.sw @@ -124,6 +124,7 @@ impl From for u256 { } } +// TODO: Replace > with u256::from when https://github.com/FuelLabs/sway/issues/5798 is resolved. #[test] fn test_u256_from_u8() { use ::assert::assert; @@ -167,9 +168,8 @@ fn test_u256_from_u64() { #[test] fn test_u256_from_b256() { use ::assert::assert; - use ::constants::ZERO_B256; - let u256_value = >::from(ZERO_B256); + let u256_value = >::from(0x0000000000000000000000000000000000000000000000000000000000000000); assert( u256_value == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, ); From faa12340b9ac070392b5672a13b4fc87b1f03c0c Mon Sep 17 00:00:00 2001 From: SwayStar123 Date: Thu, 28 Mar 2024 14:24:17 +0530 Subject: [PATCH 10/12] remove space --- sway-lib-std/src/primitive_conversions/u256.sw | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sway-lib-std/src/primitive_conversions/u256.sw b/sway-lib-std/src/primitive_conversions/u256.sw index 630470c08bf..4c32f18a772 100644 --- a/sway-lib-std/src/primitive_conversions/u256.sw +++ b/sway-lib-std/src/primitive_conversions/u256.sw @@ -158,7 +158,7 @@ fn test_u256_from_u32() { #[test] fn test_u256_from_u64() { use ::assert::assert; - + let u256_value = >::from(18446744073709551615_u64); assert( u256_value == 0x000000000000000000000000000000000000000000000000ffffffffffffffff_u256, From 8800e9c09659b6d30849defc532dd300a70eb078 Mon Sep 17 00:00:00 2001 From: SwayStar123 Date: Thu, 28 Mar 2024 14:29:46 +0530 Subject: [PATCH 11/12] fmt --- sway-lib-std/src/primitive_conversions.sw | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sway-lib-std/src/primitive_conversions.sw b/sway-lib-std/src/primitive_conversions.sw index 6d3856bfa80..bc781f579b4 100644 --- a/sway-lib-std/src/primitive_conversions.sw +++ b/sway-lib-std/src/primitive_conversions.sw @@ -6,4 +6,4 @@ pub mod u8; pub mod u16; pub mod u32; pub mod u64; -pub mod u256; \ No newline at end of file +pub mod u256; From d0458758a64b7f35a44a979ac4b8aadbf83290b4 Mon Sep 17 00:00:00 2001 From: SwayStar123 Date: Thu, 28 Mar 2024 14:59:39 +0530 Subject: [PATCH 12/12] add u256.sw to reduced_lib.config --- .../reduced_std_libs/sway-lib-std-conversions/reduced_lib.config | 1 + 1 file changed, 1 insertion(+) diff --git a/test/src/e2e_vm_tests/reduced_std_libs/sway-lib-std-conversions/reduced_lib.config b/test/src/e2e_vm_tests/reduced_std_libs/sway-lib-std-conversions/reduced_lib.config index 84f405ef96c..624471573d5 100644 --- a/test/src/e2e_vm_tests/reduced_std_libs/sway-lib-std-conversions/reduced_lib.config +++ b/test/src/e2e_vm_tests/reduced_std_libs/sway-lib-std-conversions/reduced_lib.config @@ -29,4 +29,5 @@ primitive_conversions/str.sw primitive_conversions/u16.sw primitive_conversions/u32.sw primitive_conversions/u64.sw +primitive_conversions/u256.sw primitive_conversions/u8.sw \ No newline at end of file