Skip to content

Commit

Permalink
rename sha256 for str arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
xunilrj committed Sep 7, 2023
1 parent 3d0942a commit 0f0d5f9
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 32 deletions.
8 changes: 5 additions & 3 deletions sway-core/src/ir_generation/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1792,12 +1792,14 @@ impl<'eng> FnCompiler<'eng> {
)
.or(self.compile_const_decl(context, md_mgr, const_decl, span_md_idx, true))?;

// parse constant string data into a string slice
// String slices are not allowed in constants
if let Some(TypeContent::StringSlice) =
result.get_type(context).map(|t| t.get_content(context))
{
todo!();
// return self.compile_string_slice(context, span_md_idx, result, *len);
return Err(CompileError::TypeNotAllowed {
reason: sway_error::error::TypeNotAllowedReason::StringSliceInConst,
span: const_decl.span.clone(),
});
}

Ok(result)
Expand Down
20 changes: 16 additions & 4 deletions sway-lib-std/src/hash.sw
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,24 @@ pub fn sha256<T>(s: T) -> b256 where T: Hash {
hasher.sha256()
}

/// Returns the `SHA-2-256` hash of `param`.
/// This function is specific for string arrays
///
/// # Examples
///
/// ```sway
/// use std::hash::*;
///
/// fn foo() {
/// let result = sha256_str_array(__to_str_array("Fuel"));
/// assert(result = 0xa80f942f4112036dfc2da86daf6d2ef6ede3164dd56d1000eb82fa87c992450f);
/// }
/// ```
#![inline(never)]
pub fn sha256_str<S>(s: S) -> b256 {
pub fn sha256_str_array<S>(param: S) -> b256 {
__check_str_type::<S>();
let str_size = __size_of_str::<S>();
let str_ptr = __addr_of(s);
let str_ptr = __addr_of(param);

let mut bytes = Bytes::with_capacity(str_size);
bytes.len = str_size;
Expand Down Expand Up @@ -369,10 +382,9 @@ pub fn keccak256<T>(s: T) -> b256 where T: Hash {
}

// Tests
//

#[test()]
fn test_hasher_sha256_str() {
fn test_hasher_sha256_str_array() {
use ::assert::assert;
let mut hasher = Hasher::new();
hasher.write_str("test");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ script;
use std::hash::*;

fn main() -> u64 {
let _hash = sha256_str(0_u8);
let _hash = sha256_str_array(0_u8);
5
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn test_first_use() {
assert(C3.y == true);
assert(C4 == MyEnum::A(42));
assert(C5 == MyEnum::B(true));
assert(sha256_str(C6) == sha256("fuel"));
assert(sha256_str_array(C6) == sha256("fuel"));
assert(C7[0] == 1);
assert(C7[1] == 2);
assert(C7[2] == 3);
Expand All @@ -61,7 +61,7 @@ fn test_second_use() {
assert(C3.y == true);
assert(C4 == MyEnum::A(42));
assert(C5 == MyEnum::B(true));
assert(sha256_str(C6) == sha256("fuel"));
assert(sha256_str_array(C6) == sha256("fuel"));
assert(C7[0] == 1);
assert(C7[1] == 2);
assert(C7[2] == 3);
Expand All @@ -78,7 +78,7 @@ fn test_inline_use() {
assert(C3.y == true);
assert(C4 == MyEnum::A(42));
assert(C5 == MyEnum::B(true));
assert(sha256_str(C6) == sha256("fuel"));
assert(sha256_str_array(C6) == sha256("fuel"));
assert(C7[0] == 1);
assert(C7[1] == 2);
assert(C7[2] == 3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn main() -> u64 {
__to_str_array("222"),
__to_str_array("333")
]);
assert(sha256("111") == sha256_str(result));
assert(sha256("111") == sha256_str_array(result));

1
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn main() -> bool {
revert(0)
}
}
assert(sha256_str(caller.get_string()) == sha256("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
assert(sha256_str_array(caller.get_string()) == sha256("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));

// Test 1
caller.set_x(1);
Expand Down Expand Up @@ -192,7 +192,7 @@ fn main() -> bool {
caller.set_string(__to_str_array("fuelfuelfuelfuelfuelfuelfuelfuelfuelfuel"));

// Can't compare strings right now so compare hashes instead
assert(sha256_str(caller.get_string()) == sha256("fuelfuelfuelfuelfuelfuelfuelfuelfuelfuel"));
assert(sha256_str_array(caller.get_string()) == sha256("fuelfuelfuelfuelfuelfuelfuelfuelfuelfuel"));

true
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ fn main() -> bool {
let read_small_string_1 = buf_ptr.read::<str[4]>();
buf_ptr.write(small_string_2);
let read_small_string_2 = buf_ptr.read::<str[4]>();
assert(sha256_str(small_string_1) == sha256_str(read_small_string_1));
assert(sha256_str(small_string_2) == sha256_str(read_small_string_2));
assert(sha256_str_array(small_string_1) == sha256_str_array(read_small_string_1));
assert(sha256_str_array(small_string_2) == sha256_str_array(read_small_string_2));

let buf_ptr = alloc::<u64>(2);
let large_string_1 = __to_str_array("fuelfuelfuel");
Expand All @@ -89,8 +89,8 @@ fn main() -> bool {
let read_large_string_1 = buf_ptr.read::<str[12]>();
buf_ptr.write(large_string_2);
let read_large_string_2 = buf_ptr.read::<str[12]>();
assert(sha256_str(large_string_1) == sha256_str(read_large_string_1));
assert(sha256_str(large_string_2) == sha256_str(read_large_string_2));
assert(sha256_str_array(large_string_1) == sha256_str_array(read_large_string_1));
assert(sha256_str_array(large_string_2) == sha256_str_array(read_large_string_2));

true
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ impl Eq for [u64; 3] {

impl Eq for str[4] {
fn eq(self, other: Self) -> bool {
sha256_str(self) == sha256_str(other)
sha256_str_array(self) == sha256_str_array(other)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl core::ops::Eq for Tuple {
pub type StringTy = str[9];
impl core::ops::Eq for StringTy {
fn eq(self, other: Self) -> bool {
sha256_str(self) == sha256_str(other)
sha256_str_array(self) == sha256_str_array(other)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,5 +209,5 @@ fn test_storage() {
// all the ASM blocks.
#[inline(never)]
fn assert_streq<S1, S2>(lhs: S1, rhs: str) {
assert(sha256_str(lhs) == sha256(rhs));
assert(sha256_str_array(lhs) == sha256(rhs));
}
22 changes: 16 additions & 6 deletions test/src/sdk-harness/test_projects/hashing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,19 +306,19 @@ mod sha256 {

let call_1 = instance
.methods()
.sha256_str(SizedAsciiString::try_from("John").unwrap())
.sha256_str_array(SizedAsciiString::try_from("John").unwrap())
.call()
.await
.unwrap();
let call_2 = instance
.methods()
.sha256_str(SizedAsciiString::try_from("John").unwrap())
.sha256_str_array(SizedAsciiString::try_from("John").unwrap())
.call()
.await
.unwrap();
let call_3 = instance
.methods()
.sha256_str(SizedAsciiString::try_from("Nick").unwrap())
.sha256_str_array(SizedAsciiString::try_from("Nick").unwrap())
.call()
.await
.unwrap();
Expand Down Expand Up @@ -468,11 +468,21 @@ mod sha256 {
Hash::Sha256,
));

let call_1 = instance.methods().sha256_struct(true).call().await.unwrap();
let call_2 = instance.methods().sha256_struct(true).call().await.unwrap();
let call_1 = instance
.methods()
.sha256_str_arrayuct(true)
.call()
.await
.unwrap();
let call_2 = instance
.methods()
.sha256_str_arrayuct(true)
.call()
.await
.unwrap();
let call_3 = instance
.methods()
.sha256_struct(false)
.sha256_str_arrayuct(false)
.call()
.await
.unwrap();
Expand Down
8 changes: 4 additions & 4 deletions test/src/sdk-harness/test_projects/hashing/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ abi MyContract {
fn sha256_u32(value: u32) -> b256;
fn sha256_u64(value: u64) -> b256;
fn sha256_bool(value: bool) -> b256;
fn sha256_str(value: str[4]) -> b256;
fn sha256_str_array(value: str[4]) -> b256;
fn sha256_b256(value: b256) -> b256;
fn sha256_tuple(value: (bool, u64)) -> b256;
fn sha256_array(value1: u64, value2: u64) -> b256;
fn sha256_enum(location: bool) -> b256;
fn sha256_struct(location: bool) -> b256;
fn sha256_str_arrayuct(location: bool) -> b256;

fn keccak256_u8(value: u8) -> b256;
fn keccak256_u16(value: u16) -> b256;
Expand Down Expand Up @@ -106,7 +106,7 @@ impl MyContract for Contract {
sha256(value)
}

fn sha256_str(value: str[4]) -> b256 {
fn sha256_str_array(value: str[4]) -> b256 {
sha256(value)
}

Expand All @@ -130,7 +130,7 @@ impl MyContract for Contract {
})
}

fn sha256_struct(location: bool) -> b256 {
fn sha256_str_arrayuct(location: bool) -> b256 {
sha256(Person {
name: "John",
age: 18,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl ExperimentalStorageInitTest for Contract {
assert(storage.s.t.int32.read() == s.t.int32);
assert(storage.e.read() == e);
assert(storage.e2.read() == e2);
assert(sha256_str(storage.string.read()) == sha256_str(string));
assert(sha256_str_array(storage.string.read()) == sha256_str_array(string));
true
}
}

0 comments on commit 0f0d5f9

Please sign in to comment.