diff --git a/sway-lib-std/src/primitive_conversions/b256.sw b/sway-lib-std/src/primitive_conversions/b256.sw index b530259dcdd..52d3157c500 100644 --- a/sway-lib-std/src/primitive_conversions/b256.sw +++ b/sway-lib-std/src/primitive_conversions/b256.sw @@ -37,6 +37,32 @@ impl From for b256 { } } +impl From<(u64, u64, u64, u64)> for b256 { + /// Casts a tuple of 4 `u64` values to a `b256`. + /// + /// # Arguments + /// + /// * `nums`: (u64, u64, u64, u64) - The tuple of `u64` values to be casted. + /// + /// # Returns + /// + /// * [b256] - The `b256` representation of the tuple of `u64` values. + /// + /// # Examples + /// + /// ```sway + /// + /// fn foo() { + /// let b256_value = b256::from((1, 2, 3, 4)); + /// } + /// ``` + fn from(nums: (u64, u64, u64, u64)) -> Self { + asm(nums: nums) { + nums: b256 + } + } +} + #[test] fn test_b256_try_from_bytes() { use ::assert::assert; @@ -76,3 +102,13 @@ fn test_b256_from_u256() { let res = b256::from(val); assert(res == 0x0000000000000000000000000000000000000000000000000000000000000000); } + +#[test] +fn test_b256_from_tuple() { + use ::assert::assert; + + let b256_value = >::from((1, 2, 3, 4)); + assert( + b256_value == 0x0000000000000001000000000000000200000000000000030000000000000004, + ); +} diff --git a/sway-lib-std/src/primitive_conversions/u256.sw b/sway-lib-std/src/primitive_conversions/u256.sw index 4c32f18a772..8e840404cd5 100644 --- a/sway-lib-std/src/primitive_conversions/u256.sw +++ b/sway-lib-std/src/primitive_conversions/u256.sw @@ -124,6 +124,32 @@ impl From for u256 { } } +impl From<(u64, u64, u64, u64)> for u256 { + /// Casts a tuple of 4 `u64` values to a `u256`. + /// + /// # Arguments + /// + /// * `nums`: (u64, u64, u64, u64) - The tuple of `u64` values to be casted. + /// + /// # Returns + /// + /// * [u256] - The `u256` representation of the tuple of `u64` values. + /// + /// # Examples + /// + /// ```sway + /// + /// fn foo() { + /// let u256_value = u256::from((1, 2, 3, 4)); + /// } + /// ``` + fn from(nums: (u64, u64, u64, u64)) -> Self { + asm(nums: nums) { + nums: u256 + } + } +} + // TODO: Replace > with u256::from when https://github.com/FuelLabs/sway/issues/5798 is resolved. #[test] fn test_u256_from_u8() { @@ -179,3 +205,13 @@ fn test_u256_from_b256() { u256_value == 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_u256, ); } + +#[test] +fn test_u256_from_tuple() { + use ::assert::assert; + + let u256_value = >::from((1, 2, 3, 4)); + assert( + u256_value == 0x0000000000000001000000000000000200000000000000030000000000000004_u256, + ); +}