Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Hash trait for Bytes and String types #5089

Merged
merged 4 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
54 changes: 0 additions & 54 deletions sway-lib-std/src/bytes.sw
Original file line number Diff line number Diff line change
Expand Up @@ -537,60 +537,6 @@ impl Bytes {
pub fn is_empty(self) -> bool {
self.len == 0
}

/// Returns the `SHA-2-256` hash of the elements.
///
/// # Returns
///
/// * [b256] - The `SHA-2-256` hash of the elements.
///
/// # Examples
///
/// ```sway
/// use std:bytes::Bytes;
///
/// fn foo() {
/// let bytes = Bytes::new();
/// bytes.push(1);
/// bytes.push(2);
/// bytes.push(3);
/// let sha256_hash = bytes.sha256();
/// }
/// ```
pub fn sha256(self) -> b256 {
let mut result_buffer = b256::min();
asm(hash: result_buffer, ptr: self.buf.ptr, bytes: self.len) {
s256 hash ptr bytes;
hash: b256
}
}

/// Returns the `KECCAK-256` hash of the elements.
///
/// # Returns
///
/// * [b256] - The `KECCAK-256` hash of the elements.
///
/// # Examples
///
/// ```sway
/// use std:bytes::Bytes;
///
/// fn foo() {
/// let bytes = Bytes::new();
/// bytes.push(1);
/// bytes.push(2);
/// bytes.push(3);
/// let keccak256_hash = bytes.keccak256();
/// }
/// ```
pub fn keccak256(self) -> b256 {
let mut result_buffer = b256::min();
asm(hash: result_buffer, ptr: self.buf.ptr, bytes: self.len) {
k256 hash ptr bytes;
hash: b256
}
}
}

// Need to use seperate impl blocks for now: https://github.com/FuelLabs/sway/issues/1548
Expand Down
38 changes: 35 additions & 3 deletions sway-lib-std/src/hash.sw
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,19 @@ impl Hasher {
}

pub fn sha256(self) -> b256 {
self.bytes.sha256()
let mut result_buffer = b256::min();
bitzoic marked this conversation as resolved.
Show resolved Hide resolved
asm(hash: result_buffer, ptr: self.bytes.buf.ptr, bytes: self.bytes.len) {
s256 hash ptr bytes;
hash: b256
}
}

pub fn keccak256(self) -> b256 {
self.bytes.keccak256()
let mut result_buffer = b256::min();
asm(hash: result_buffer, ptr: self.bytes.buf.ptr, bytes: self.bytes.len) {
k256 hash ptr bytes;
hash: b256
}
}
}

Expand Down Expand Up @@ -123,6 +131,12 @@ impl Hash for bool {
}
}

impl Hash for Bytes {
fn hash(self, ref mut state: Hasher) {
bitzoic marked this conversation as resolved.
Show resolved Hide resolved
state.write(self);
}
}

impl<A, B> Hash for (A, B) where A: Hash, B: Hash {
#![inline(never)]
fn hash(self, ref mut state: Hasher) {
Expand Down Expand Up @@ -418,4 +432,22 @@ fn test_hasher_sha256_bool() {
true.hash(hasher);
let sha256 = hasher.sha256();
assert(sha256 == 0x4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a);
}
}

#[test]
bitzoic marked this conversation as resolved.
Show resolved Hide resolved
fn test_hasher_sha256_bytes() {
use ::assert::assert;
let mut hasher = Hasher::new();
let mut bytes = Bytes::new();
bytes.push(0u8);
bytes.hash(hasher);
let sha256 = hasher.sha256();
assert(sha256 == 0x6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d);

let mut hasher = Hasher::new();
let mut bytes = Bytes::new();
bytes.push(1u8);
bytes.hash(hasher);
let sha256 = hasher.sha256();
assert(sha256 == 0x4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a);
}
21 changes: 20 additions & 1 deletion sway-lib-std/src/string.sw
bitzoic marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
library;

use ::assert::assert;
use ::bytes::Bytes;
use ::convert::From;
use ::hash::{Hash, Hasher};
use ::option::Option;
use ::assert::assert;


/// A UTF-8 encoded growable string.
Expand Down Expand Up @@ -265,6 +266,12 @@ impl Eq for String {
}
}

impl Hash for String {
fn hash(self, ref mut state: Hasher) {
state.write(self.bytes);
}
}

// Tests

#[test]
Expand Down Expand Up @@ -495,3 +502,15 @@ fn string_test_equal() {
assert(string1 == string2);
assert(string1 != string3);
}

#[test]
fn string_test_hash() {
use ::hash::sha256;

let mut bytes = Bytes::new();
bytes.push(0u8);

let string = String::from(bytes);

assert(sha256(string) == sha256(bytes));
}
Loading