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

Added generic sha256 #1544

Merged
merged 31 commits into from May 18, 2022
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
9ada878
Added generic sha256
May 13, 2022
fecf8a1
Update sway-lib-std/src/hash.sw
Braqzen May 13, 2022
ea4a8c0
Inlined comments and updated formatting
May 13, 2022
0b93a03
Cleaned up asm
May 14, 2022
1f0a3b6
Fixing import
May 14, 2022
cdb5214
Updating tests, WIP
May 14, 2022
cbad5ea
Pushing up tests that fail to compile
May 15, 2022
85b521d
Updating docs
May 15, 2022
37d9731
Formatting
May 15, 2022
348f856
Updating compiler intrinsics
May 15, 2022
116eba2
Saving before pulling master in
May 16, 2022
cc0443c
Merge branch 'master' into braqzen-1511
Braqzen May 16, 2022
8928646
Updated tests
May 16, 2022
c7ab048
Fixed formatting
May 16, 2022
9ae4572
Starting to add hash validation for testing
May 17, 2022
9b4519d
Updated integers
May 17, 2022
2225304
Added str hashing
May 17, 2022
a83ba57
Added concrete tuple validation
May 17, 2022
4adfcb1
Added concrete array validation
May 17, 2022
da0d75a
Added concrete enum validation
May 17, 2022
ffbe0fd
Added b256 validation
May 18, 2022
eef3c7c
Formatting & core::num update
May 18, 2022
6cd60a7
Testing struct, WIP
May 18, 2022
00122b8
Merge branch 'master' into braqzen-1511
Braqzen May 18, 2022
9ce7fcc
Merge branch 'braqzen-1511' of https://github.com/FuelLabs/sway into …
May 18, 2022
53cbc67
Hashing struct works
May 18, 2022
29355bb
Formatting
May 18, 2022
b9ebdb2
Updating examples for mdbook
May 18, 2022
d13e155
Updating to buggy formatting and removed comment
May 18, 2022
dfa306d
Updating import formatting
May 18, 2022
c2a9c1b
Update sway-lib-std/src/hash.sw
Braqzen May 18, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
74 changes: 72 additions & 2 deletions examples/hashing/src/main.sw
@@ -1,11 +1,81 @@
script;

use std::chain::log_b256;
use std::hash::{HashMethod, hash_pair, hash_u64, hash_value};
use std::{
chain::log_b256,
constants::ZERO,
core::num::*,
hash::{HashMethod, hash_pair, hash_u64, hash_value, sha256}
};

const VALUE_A = 0x9280359a3b96819889d30614068715d634ad0cf9bba70c0f430a8c201138f79f;

enum Location {
Earth: (),
Mars: (),
}

struct Person {
name: str[4],
age: u64,
alive: bool,
location: Location,
stats: Stats,
some_tuple: (bool,
u64), some_array: [u64;
adlerjohn marked this conversation as resolved.
Show resolved Hide resolved
2],
some_b256: b256,
}

struct Stats {
strength: u64,
agility: u64,
}

fn main() {
// Use the generic sha256 to hash some integers
let sha_hashed_u8 = sha256(~u8::max());
let sha_hashed_u16 = sha256(~u16::max());
let sha_hashed_u32 = sha256(~u32::max());
let sha_hashed_u64 = sha256(~u64::max());

// Or hash a b256
let sha_hashed_b256 = sha256(VALUE_A);

// You can hash booleans too
let sha_hashed_bool = sha256(true);

// Strings are not a problem either
let sha_hashed_str = sha256( "Fastest Modular Execution Layer!");

// Tuples of any size work too
let sha_hashed_tuple = sha256((true, 7));

// As do arrays
let sha_hashed_array = sha256([4, 5, 6]);

// Enums work too
let sha_hashed_enum = sha256(Location::Earth);

// Complex structs are not a problem
let sha_hashed_struct = sha256(Person {
name: "John", age: 9000, alive: true, location: Location::Mars, stats: Stats {
strength: 10, agility: 9
},
some_tuple: (true, 8), some_array: [17, 76], some_b256: ZERO
});

log_b256(sha_hashed_u8);
log_b256(sha_hashed_u16);
log_b256(sha_hashed_u32);
log_b256(sha_hashed_u64);
log_b256(sha_hashed_b256);
log_b256(sha_hashed_bool);
log_b256(sha_hashed_str);
log_b256(sha_hashed_tuple);
log_b256(sha_hashed_array);
log_b256(sha_hashed_enum);
log_b256(sha_hashed_struct);

// Hash a single u64 value.
let hashed_u64 = hash_u64(100, HashMethod::Sha256);

Expand Down
23 changes: 23 additions & 0 deletions sway-lib-std/src/hash.sw
@@ -1,5 +1,7 @@
library hash;

use ::constants::ZERO;

// Should this be a trait eventually? Do we want to allow people to customize what `!` does?
// Scala says yes, Rust says perhaps...
pub fn not(a: bool) -> bool {
Braqzen marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -124,3 +126,24 @@ pub fn hash_pair(value_a: b256, value_b: b256, method: HashMethod) -> b256 {
}
}
}

/// Returns the SHA-2-256 hash of `param`.
pub fn sha256<T>(param: T) -> b256 {
let mut result_buffer: b256 = ZERO;
if !__is_reference_type::<T>() {
asm(buffer, ptr: param, eight_bytes: 8, hash: result_buffer) {
move buffer sp; // Copy stack pointer to memory at "buffer"
Braqzen marked this conversation as resolved.
Show resolved Hide resolved
cfei i8; // Grow stack by 1 word
sw buffer ptr i0; // Save value in register at "ptr" to memory at "buffer"
s256 hash buffer eight_bytes; // Hash the next eight bytes starting from "buffer" into "hash"
cfsi i8; // Shrink stack by 1 word
hash: b256 // Return
}
} else {
let size = __size_of::<T>();
asm(hash: result_buffer, ptr: param, bytes: size) {
s256 hash ptr bytes; // Hash the next "size" number of bytes starting from "ptr" into "hash"
hash: b256 // Return
}
}
}
1 change: 1 addition & 0 deletions test/src/sdk-harness/Cargo.toml
Expand Up @@ -15,6 +15,7 @@ fuels = "0.12"
fuels-abigen-macro = "0.12"
hex = "0.4.3"
rand = "0.8"
sha2 = "0.10"
tokio = { version = "1.12", features = ["rt", "macros"] }

[dev-dependencies]
Expand Down