From 74a91e384e2b474f35202306072021df8b6fc8bf Mon Sep 17 00:00:00 2001 From: brandon Date: Sun, 24 Sep 2023 19:51:42 -0400 Subject: [PATCH 1/5] Added initial keccak test --- sway-lib-std/src/hash.sw | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/sway-lib-std/src/hash.sw b/sway-lib-std/src/hash.sw index 339cc050ff3..e542b84b4a3 100644 --- a/sway-lib-std/src/hash.sw +++ b/sway-lib-std/src/hash.sw @@ -394,7 +394,7 @@ fn test_hasher_sha256_str_array() { let mut hasher = Hasher::new(); hasher.write_str("Fastest Modular Execution Layer!"); let sha256 = hasher.sha256(); - assert(sha256 == 0x4a3cd7c8b44dbf7941e55179425f746adeaa97fe2d99b571fffee78e9b41743c); + assert(sha256 != 0x4a3cd7c8b44dbf7941e55179425f746adeaa97fe2d99b571fffee78e9b41743c); } // The hashes for the following test can be obtained in Rust by running the following script: @@ -500,3 +500,20 @@ fn test_hasher_sha256_bytes() { let sha256 = hasher.sha256(); assert(sha256 == 0x4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a); } + +// The hashes for the following test can be obtained in Rust by running the following script: +// https://www.rustexplorer.com/b/kbg0oc +#[test()] +fn test_hasher_keccak256_u8() { + use ::assert::assert; + let mut hasher = Hasher::new(); + 0_u8.hash(hasher); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0xbc36789e7a1e281436464229828f817d6612f7b477d66591ff96a9e064bcc98a); + + let mut hasher = Hasher::new(); + 1_u8.hash(hasher); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0x5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2); + +} \ No newline at end of file From 72b9b72aff4d9078dc210047b246ae9e89a66198 Mon Sep 17 00:00:00 2001 From: Brandon Date: Tue, 3 Oct 2023 15:02:11 -0400 Subject: [PATCH 2/5] Reverted assert test --- sway-lib-std/src/hash.sw | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sway-lib-std/src/hash.sw b/sway-lib-std/src/hash.sw index e542b84b4a3..906c750abe6 100644 --- a/sway-lib-std/src/hash.sw +++ b/sway-lib-std/src/hash.sw @@ -394,7 +394,7 @@ fn test_hasher_sha256_str_array() { let mut hasher = Hasher::new(); hasher.write_str("Fastest Modular Execution Layer!"); let sha256 = hasher.sha256(); - assert(sha256 != 0x4a3cd7c8b44dbf7941e55179425f746adeaa97fe2d99b571fffee78e9b41743c); + assert(sha256 == 0x4a3cd7c8b44dbf7941e55179425f746adeaa97fe2d99b571fffee78e9b41743c); } // The hashes for the following test can be obtained in Rust by running the following script: From 1786ae25f93e87cbc191c823daecca710935667d Mon Sep 17 00:00:00 2001 From: Brandon Date: Wed, 4 Oct 2023 23:46:54 -0400 Subject: [PATCH 3/5] Added keccak256 tests --- sway-lib-std/src/hash.sw | 102 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/sway-lib-std/src/hash.sw b/sway-lib-std/src/hash.sw index 906c750abe6..8d9d4b2f01d 100644 --- a/sway-lib-std/src/hash.sw +++ b/sway-lib-std/src/hash.sw @@ -503,6 +503,21 @@ fn test_hasher_sha256_bytes() { // The hashes for the following test can be obtained in Rust by running the following script: // https://www.rustexplorer.com/b/kbg0oc + +#[test()] +fn test_hasher_keccak256_str_array() { + use ::assert::assert; + let mut hasher = Hasher::new(); + hasher.write_str("test"); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0x9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658); + + let mut hasher = Hasher::new(); + hasher.write_str("Fastest Modular Execution Layer!"); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0xab8e83e041e001bcf797c9cc7d6bc472bfdb8c736bab7999f13b7c26f48c354f); +} + #[test()] fn test_hasher_keccak256_u8() { use ::assert::assert; @@ -515,5 +530,92 @@ fn test_hasher_keccak256_u8() { 1_u8.hash(hasher); let keccak256 = hasher.keccak256(); assert(keccak256 == 0x5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2); +} +#[test()] +fn test_hasher_keccak256_u16() { + use ::assert::assert; + let mut hasher = Hasher::new(); + 0_u16.hash(hasher); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0x54a8c0ab653c15bfb48b47fd011ba2b9617af01cb45cab344acd57c924d56798); + + let mut hasher = Hasher::new(); + 1_u16.hash(hasher); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0x49d03a195e239b52779866b33024210fc7dc66e9c2998975c0aa45c1702549d5); +} + +#[test()] +fn test_hasher_keccak256_u32() { + use ::assert::assert; + let mut hasher = Hasher::new(); + 0_u32.hash(hasher); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0xe8e77626586f73b955364c7b4bbf0bb7f7685ebd40e852b164633a4acbd3244c); + + let mut hasher = Hasher::new(); + 1_u32.hash(hasher); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0x51f81bcdfc324a0dff2b5bec9d92e21cbebc4d5e29d3a3d30de3e03fbeab8d7f); +} + +#[test()] +fn test_hasher_keccak256_u64() { + use ::assert::assert; + let mut hasher = Hasher::new(); + 0_u64.hash(hasher); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0x011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce); + + let mut hasher = Hasher::new(); + 1_u64.hash(hasher); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0x6c31fc15422ebad28aaf9089c306702f67540b53c7eea8b7d2941044b027100f); +} + +#[test()] +fn test_hasher_keccak256_b256() { + use ::assert::assert; + let mut hasher = Hasher::new(); + 0x0000000000000000000000000000000000000000000000000000000000000000.hash(hasher); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563); + + let mut hasher = Hasher::new(); + 0x0000000000000000000000000000000000000000000000000000000000000001.hash(hasher); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0xb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6); +} + +#[test()] +fn test_hasher_keccak256_bool() { + use ::assert::assert; + let mut hasher = Hasher::new(); + false.hash(hasher); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0xbc36789e7a1e281436464229828f817d6612f7b477d66591ff96a9e064bcc98a); + + let mut hasher = Hasher::new(); + true.hash(hasher); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0x5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2); +} + +#[test] +fn test_hasher_keccak256_bytes() { + use ::assert::assert; + let mut hasher = Hasher::new(); + let mut bytes = Bytes::new(); + bytes.push(0u8); + bytes.hash(hasher); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0xbc36789e7a1e281436464229828f817d6612f7b477d66591ff96a9e064bcc98a); + + let mut hasher = Hasher::new(); + let mut bytes = Bytes::new(); + bytes.push(1u8); + bytes.hash(hasher); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0x5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2); } \ No newline at end of file From c445308e1c30279711e09fa46aa96b7d7ef50b4a Mon Sep 17 00:00:00 2001 From: Brandon <89608157+brandonsurh@users.noreply.github.com> Date: Thu, 5 Oct 2023 09:37:38 -0400 Subject: [PATCH 4/5] Update sway-lib-std/src/hash.sw Co-authored-by: Braqzen <103777923+Braqzen@users.noreply.github.com> --- sway-lib-std/src/hash.sw | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sway-lib-std/src/hash.sw b/sway-lib-std/src/hash.sw index 8d9d4b2f01d..ecc9a387a98 100644 --- a/sway-lib-std/src/hash.sw +++ b/sway-lib-std/src/hash.sw @@ -606,16 +606,16 @@ fn test_hasher_keccak256_bool() { fn test_hasher_keccak256_bytes() { use ::assert::assert; let mut hasher = Hasher::new(); - let mut bytes = Bytes::new(); + let mut bytes = Bytes::with_capacity(1); bytes.push(0u8); bytes.hash(hasher); let keccak256 = hasher.keccak256(); assert(keccak256 == 0xbc36789e7a1e281436464229828f817d6612f7b477d66591ff96a9e064bcc98a); let mut hasher = Hasher::new(); - let mut bytes = Bytes::new(); + let mut bytes = Bytes::with_capacity(1); bytes.push(1u8); bytes.hash(hasher); let keccak256 = hasher.keccak256(); assert(keccak256 == 0x5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2); -} \ No newline at end of file +} From e1f543a54e1b9bb816ee1de219c09e2f263b972b Mon Sep 17 00:00:00 2001 From: Brandon <89608157+brandonsurh@users.noreply.github.com> Date: Thu, 5 Oct 2023 09:37:52 -0400 Subject: [PATCH 5/5] Update sway-lib-std/src/hash.sw Co-authored-by: Braqzen <103777923+Braqzen@users.noreply.github.com> --- sway-lib-std/src/hash.sw | 3 --- 1 file changed, 3 deletions(-) diff --git a/sway-lib-std/src/hash.sw b/sway-lib-std/src/hash.sw index ecc9a387a98..2e09a41187c 100644 --- a/sway-lib-std/src/hash.sw +++ b/sway-lib-std/src/hash.sw @@ -501,9 +501,6 @@ fn test_hasher_sha256_bytes() { assert(sha256 == 0x4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a); } -// The hashes for the following test can be obtained in Rust by running the following script: -// https://www.rustexplorer.com/b/kbg0oc - #[test()] fn test_hasher_keccak256_str_array() { use ::assert::assert;