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
16 changes: 4 additions & 12 deletions sway-lib-std/src/array_conversions/b256.sw
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ impl b256 {
/// }
/// ```
pub fn to_le_bytes(self) -> [u8; 32] {
let (a, b, c, d): (u64, u64, u64, u64) = asm(r1: self) {
r1: (u64, u64, u64, u64)
};
let (a, b, c, d): (u64, u64, u64, u64) = __transmute::<b256, (u64, u64, u64, u64)>(self);
let a = a.to_le_bytes();
let b = b.to_le_bytes();
let c = c.to_le_bytes();
Expand Down Expand Up @@ -85,9 +83,7 @@ impl b256 {

let result = (d, c, b, a);

asm(r1: result) {
r1: b256
}
__transmute::<(u64, u64, u64, u64), b256>(result)
}

/// Converts the `b256` to a sequence of big-endian bytes.
Expand All @@ -111,9 +107,7 @@ impl b256 {
/// }
/// ```
pub fn to_be_bytes(self) -> [u8; 32] {
let (a, b, c, d): (u64, u64, u64, u64) = asm(r1: self) {
r1: (u64, u64, u64, u64)
};
let (a, b, c, d): (u64, u64, u64, u64) = __transmute::<b256, (u64, u64, u64, u64)>(self);
let a = a.to_be_bytes();
let b = b.to_be_bytes();
let c = c.to_be_bytes();
Expand Down Expand Up @@ -168,8 +162,6 @@ impl b256 {

let result = (a, b, c, d);

asm(r1: result) {
r1: b256
}
__transmute::<(u64, u64, u64, u64), b256>(result)
}
}
16 changes: 4 additions & 12 deletions sway-lib-std/src/array_conversions/u256.sw
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ impl u256 {
/// }
/// ```
pub fn to_le_bytes(self) -> [u8; 32] {
let (a, b, c, d): (u64, u64, u64, u64) = asm(r1: self) {
r1: (u64, u64, u64, u64)
};
let (a, b, c, d): (u64, u64, u64, u64) = __transmute::<u256, (u64, u64, u64, u64)>(self);
Comment thread
ironcev marked this conversation as resolved.
let a = a.to_le_bytes();
let b = b.to_le_bytes();
let c = c.to_le_bytes();
Expand Down Expand Up @@ -84,9 +82,7 @@ impl u256 {

let result = (d, c, b, a);

asm(r1: result) {
r1: u256
}
__transmute::<(u64, u64, u64, u64), u256>(result)
}

/// Converts the `u256` to a sequence of big-endian bytes.
Expand All @@ -110,9 +106,7 @@ impl u256 {
/// }
/// ```
pub fn to_be_bytes(self) -> [u8; 32] {
let (a, b, c, d): (u64, u64, u64, u64) = asm(r1: self) {
r1: (u64, u64, u64, u64)
};
let (a, b, c, d): (u64, u64, u64, u64) = __transmute::<u256, (u64, u64, u64, u64)>(self);
let a = a.to_be_bytes();
let b = b.to_be_bytes();
let c = c.to_be_bytes();
Expand Down Expand Up @@ -167,8 +161,6 @@ impl u256 {

let result = (a, b, c, d);

asm(r1: result) {
r1: u256
}
__transmute::<(u64, u64, u64, u64), u256>(result)
}
}
4 changes: 1 addition & 3 deletions sway-lib-std/src/bytes.sw
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ pub struct Bytes {
impl AsRawSlice for Bytes {
/// Returns a raw slice of all of the elements in the `Bytes`.
fn as_raw_slice(self) -> raw_slice {
asm(ptr: (self.buf.ptr, self.len)) {
ptr: raw_slice
}
__transmute::<(raw_ptr, u64), raw_slice>((self.buf.ptr, self.len))
}
}

Expand Down
8 changes: 2 additions & 6 deletions sway-lib-std/src/bytes_conversions/b256.sw
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ impl b256 {
/// }
/// ```
pub fn to_le_bytes(self) -> Bytes {
let (a, b, c, d): (u64, u64, u64, u64) = asm(r1: self) {
r1: (u64, u64, u64, u64)
};
let (a, b, c, d): (u64, u64, u64, u64) = __transmute::<b256, (u64, u64, u64, u64)>(self);
let a = a.to_le_bytes();
let b = b.to_le_bytes();
let c = c.to_le_bytes();
Expand Down Expand Up @@ -86,9 +84,7 @@ impl b256 {

let result = (d, c, b, a);

asm(r1: result) {
r1: b256
}
__transmute::<(u64, u64, u64, u64), b256>(result)
}

/// Converts the `b256` to a sequence of big-endian bytes.
Expand Down
8 changes: 2 additions & 6 deletions sway-lib-std/src/bytes_conversions/u16.sw
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ impl u16 {
ptr: raw_ptr
};

let rs = asm(parts: (ptr, 2)) {
parts: raw_slice
};
let rs = __transmute::<(raw_ptr, u64), raw_slice>((ptr, 2));

Bytes::from(rs)
}
Expand Down Expand Up @@ -114,9 +112,7 @@ impl u16 {
ptr: raw_ptr
};

let rs = asm(parts: (ptr, 2)) {
parts: raw_slice
};
let rs = __transmute::<(raw_ptr, u64), raw_slice>((ptr, 2));

Bytes::from(rs)
}
Expand Down
16 changes: 4 additions & 12 deletions sway-lib-std/src/bytes_conversions/u256.sw
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ impl u256 {
/// }
/// ```
pub fn to_le_bytes(self) -> Bytes {
let (a, b, c, d): (u64, u64, u64, u64) = asm(r1: self) {
r1: (u64, u64, u64, u64)
};
let (a, b, c, d): (u64, u64, u64, u64) = __transmute::<u256, (u64, u64, u64, u64)>(self);
let a = a.to_le_bytes();
let b = b.to_le_bytes();
let c = c.to_le_bytes();
Expand Down Expand Up @@ -86,9 +84,7 @@ impl u256 {

let result = (d, c, b, a);

asm(r1: result) {
r1: u256
}
__transmute::<(u64, u64, u64, u64), u256>(result)
}

/// Converts the `u256` to a sequence of big-endian bytes.
Expand All @@ -112,9 +108,7 @@ impl u256 {
/// }
/// ```
pub fn to_be_bytes(self) -> Bytes {
let b: b256 = asm(r1: self) {
r1: b256
};
let b: b256 = __transmute::<u256, b256>(self);
Bytes::from(b)
}

Expand Down Expand Up @@ -147,8 +141,6 @@ impl u256 {
pub fn from_be_bytes(bytes: Bytes) -> Self {
assert(bytes.len() == 32);
let b: b256 = bytes.try_into().unwrap();
asm(r1: b) {
r1: u256
}
__transmute::<b256, u256>(b)
}
}
8 changes: 2 additions & 6 deletions sway-lib-std/src/bytes_conversions/u32.sw
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ impl u32 {
ptr: raw_ptr
};

let rs = asm(parts: (ptr, 4)) {
parts: raw_slice
};
let rs = __transmute::<(raw_ptr, u64), raw_slice>((ptr, 4));

Bytes::from(rs)
}
Expand Down Expand Up @@ -164,9 +162,7 @@ impl u32 {
ptr: raw_ptr
};

let rs = asm(parts: (ptr, 4)) {
parts: raw_slice
};
let rs = __transmute::<(raw_ptr, u64), raw_slice>((ptr, 4));

Bytes::from(rs)
}
Expand Down
8 changes: 2 additions & 6 deletions sway-lib-std/src/bytes_conversions/u64.sw
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ impl u64 {
ptr: raw_ptr
};

let rs = asm(parts: (ptr, 8)) {
parts: raw_slice
};
let rs = __transmute::<(raw_ptr, u64), raw_slice>((ptr, 8));

Bytes::from(rs)
}
Expand Down Expand Up @@ -232,9 +230,7 @@ impl u64 {
ptr: raw_ptr
};

let rs = asm(parts: (ptr, 8)) {
parts: raw_slice
};
let rs = __transmute::<(raw_ptr, u64), raw_slice>((ptr, 8));

Bytes::from(rs)
}
Expand Down
12 changes: 3 additions & 9 deletions sway-lib-std/src/codec.sw
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,7 @@ impl BufferReader {
}

pub fn read_bytes(ref mut self, count: u64) -> raw_slice {
let slice = asm(ptr: (self.ptr, count)) {
ptr: raw_slice
};
let slice = __transmute::<(raw_ptr, u64), raw_slice>((self.ptr, count));
self.ptr = __ptr_add::<u8>(self.ptr, count);
slice
}
Expand Down Expand Up @@ -1715,9 +1713,7 @@ where
mcp hp src size;
hp: raw_ptr
};
asm(s: (ptr, size)) {
s: raw_slice
}
__transmute::<(raw_ptr, u64), raw_slice>((ptr, size))
} else {
let buffer = item.abi_encode(Buffer::new());
buffer.as_raw_slice()
Expand Down Expand Up @@ -1908,9 +1904,7 @@ impl AbiDecode for str {
fn abi_decode(ref mut buffer: BufferReader) -> str {
let len = buffer.read_8_bytes::<u64>();
let data = buffer.read_bytes(len);
asm(s: (data.ptr(), len)) {
s: str
}
__transmute::<(raw_ptr, u64), str>((data.ptr(), len))
}
}

Expand Down
8 changes: 2 additions & 6 deletions sway-lib-std/src/crypto/ed25519.sw
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,14 @@ impl Ed25519 {
impl From<B512> for Ed25519 {
fn from(bits: B512) -> Self {
Self {
bits: asm(bits: bits.bits()) {
bits: [u8; 64]
},
bits: __transmute::<[b256; 2], [u8; 64]>(bits.bits()),
}
}
}
impl From<(b256, b256)> for Ed25519 {
fn from(components: (b256, b256)) -> Self {
Self {
bits: asm(components: components) {
components: [u8; 64]
},
bits: __transmute::<(b256, b256), [u8; 64]>(components),
}
}
}
Expand Down
8 changes: 2 additions & 6 deletions sway-lib-std/src/crypto/secp256k1.sw
Original file line number Diff line number Diff line change
Expand Up @@ -340,19 +340,15 @@ impl Secp256k1 {
impl From<B512> for Secp256k1 {
fn from(bits: B512) -> Self {
Self {
bits: asm(bits: bits.bits()) {
bits: [u8; 64]
},
bits: __transmute::<[b256; 2], [u8; 64]>(bits.bits()),
}
}
}

impl From<(b256, b256)> for Secp256k1 {
fn from(components: (b256, b256)) -> Self {
Self {
bits: asm(components: components) {
components: [u8; 64]
},
bits: __transmute::<(b256, b256), [u8; 64]>(components),
}
}
}
Expand Down
8 changes: 2 additions & 6 deletions sway-lib-std/src/crypto/secp256r1.sw
Original file line number Diff line number Diff line change
Expand Up @@ -341,19 +341,15 @@ impl Secp256r1 {
impl From<B512> for Secp256r1 {
fn from(bits: B512) -> Self {
Self {
bits: asm(bits: bits.bits()) {
bits: [u8; 64]
},
bits: __transmute::<[b256; 2], [u8; 64]>(bits.bits()),
}
}
}

impl From<(b256, b256)> for Secp256r1 {
fn from(components: (b256, b256)) -> Self {
Self {
bits: asm(components: components) {
components: [u8; 64]
},
bits: __transmute::<(b256, b256), [u8; 64]>(components),
}
}
}
Expand Down
12 changes: 3 additions & 9 deletions sway-lib-std/src/debug.sw
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,7 @@ impl Formatter {
let mut i = 79;
while true {
let rem = value % 10;
let (_, _, _, digit) = asm(rem: rem) {
rem: (u64, u64, u64, u64)
};
let (_, _, _, digit) = __transmute::<u256, (u64, u64, u64, u64)>(rem);
let digit = asm(v: digit % 10) {
v: u8
};
Expand Down Expand Up @@ -172,9 +170,7 @@ impl Formatter {
let mut i = 65;
while true {
let rem = value % 16;
let (_, _, _, digit) = asm(rem: rem) {
rem: (u64, u64, u64, u64)
};
let (_, _, _, digit) = __transmute::<u256, (u64, u64, u64, u64)>(rem);
let digit = asm(v: digit % 16) {
v: u8
};
Expand Down Expand Up @@ -349,9 +345,7 @@ impl Debug for u256 {

impl Debug for b256 {
fn fmt(self, ref mut f: Formatter) {
f.print_u256_as_hex(asm(s: self) {
s: u256
}, true);
f.print_u256_as_hex(__transmute::<b256, u256>(self), true);
}
}

Expand Down
8 changes: 2 additions & 6 deletions sway-lib-std/src/hash.sw
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,7 @@ where
// `__elem_at` accepts only a reference to a slice or an array.
// To satisfy this requirement, we cast the pointer to the underlying
// vector data to an array reference.
let ptr = asm(ptr: self.ptr()) {
ptr: &[T; 0]
};
let ptr = __transmute::<raw_ptr, &[T; 0]>(self.ptr());

let mut i = 0;
while __lt(i, len) {
Expand All @@ -404,9 +402,7 @@ where
// `__elem_at` accepts only a reference to a slice or an array.
// To satisfy this requirement, we cast the pointer to the underlying
// vector data to an array reference.
let ptr = asm(ptr: self.ptr()) {
ptr: &[T; 0]
};
let ptr = __transmute::<raw_ptr, &[T; 0]>(self.ptr());

let mut i = 0;
while __lt(i, len) {
Expand Down
Loading
Loading