Skip to content

Commit

Permalink
Added generic keccak256 function (FuelLabs#1606)
Browse files Browse the repository at this point in the history
* Added generic keccak256 function

* Updated formatting
  • Loading branch information
Braqzen authored May 19, 2022
1 parent 9cb14b5 commit 9e23221
Show file tree
Hide file tree
Showing 5 changed files with 690 additions and 213 deletions.
46 changes: 45 additions & 1 deletion examples/hashing/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
script;

use core::num::*;
use std::{hash::{HashMethod, hash_pair, hash_u64, hash_value, sha256}, logging::log};
use std::{hash::{HashMethod, hash_pair, hash_u64, hash_value, keccak256, sha256}, logging::log};

const VALUE_A = 0x9280359a3b96819889d30614068715d634ad0cf9bba70c0f430a8c201138f79f;

Expand Down Expand Up @@ -73,6 +73,50 @@ fn main() {
log(sha_hashed_enum);
log(sha_hashed_struct);

// Use the generic keccak256 to hash some integers
let keccak_hashed_u8 = keccak256(~u8::max());
let keccak_hashed_u16 = keccak256(~u16::max());
let keccak_hashed_u32 = keccak256(~u32::max());
let keccak_hashed_u64 = keccak256(~u64::max());

// Or hash a b256
let keccak_hashed_b256 = keccak256(VALUE_A);

// You can hash booleans too
let keccak_hashed_bool = keccak256(true);

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

// Tuples of any size work too
let keccak_hashed_tuple = keccak256((true, 7));

// As do arrays
let keccak_hashed_array = keccak256([4, 5, 6]);

// Enums work too
let keccak_hashed_enum = keccak256(Location::Earth);

// Complex structs are not a problem
let keccak_hashed_struct = keccak256(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(keccak_hashed_u8);
log(keccak_hashed_u16);
log(keccak_hashed_u32);
log(keccak_hashed_u64);
log(keccak_hashed_b256);
log(keccak_hashed_bool);
log(keccak_hashed_str);
log(keccak_hashed_tuple);
log(keccak_hashed_array);
log(keccak_hashed_enum);
log(keccak_hashed_struct);

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

Expand Down
21 changes: 21 additions & 0 deletions sway-lib-std/src/hash.sw
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,24 @@ pub fn sha256<T>(param: T) -> b256 {
}
}
}

/// Returns the KECCAK-256 hash of `param`.
pub fn keccak256<T>(param: T) -> b256 {
let mut result_buffer: b256 = ~b256::min();
if !__is_reference_type::<T>() {
asm(buffer, ptr: param, eight_bytes: 8, hash: result_buffer) {
move buffer sp; // Make `buffer` point to the current top of the stack
cfei i8; // Grow stack by 1 word
sw buffer ptr i0; // Save value in register at "ptr" to memory at "buffer"
k256 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) {
k256 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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ fuels-abigen-macro = "0.12"
hex = "0.4.3"
rand = "0.8"
sha2 = "0.10"
sha3 = "0.10.1"
tokio = { version = "1.12", features = ["rt", "macros"] }

[dev-dependencies]
Expand Down
Loading

0 comments on commit 9e23221

Please sign in to comment.