From 1380127b329ece4e5e5b31d77b2f1e5a0384d3e3 Mon Sep 17 00:00:00 2001 From: Hugo Demeyere Date: Thu, 9 Jan 2025 23:01:51 +0100 Subject: [PATCH 1/2] feat(corelib): ByteArray impl Hash trait --- corelib/src/hash.cairo | 13 +++++++++++++ corelib/src/test/hash_test.cairo | 21 +++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/corelib/src/hash.cairo b/corelib/src/hash.cairo index 0eb164dc231..ae2d0fe54dc 100644 --- a/corelib/src/hash.cairo +++ b/corelib/src/hash.cairo @@ -229,3 +229,16 @@ impl TupleNextHash< state.update_with(head).update_with(rest) } } + +impl ByteArrayHash, +Drop> of Hash { + fn update_state(mut state: S, value: ByteArray) -> S { + state = state.update(value.data.len().into()); + + for word_index in 0..value.data.len() { + let word_in_data = (*value.data.at(word_index)).into(); + state = state.update(word_in_data); + }; + + state.update(value.pending_word_len.into()).update(value.pending_word) + } +} diff --git a/corelib/src/test/hash_test.cairo b/corelib/src/test/hash_test.cairo index 17bf43e13b3..c3046c414d6 100644 --- a/corelib/src/test/hash_test.cairo +++ b/corelib/src/test/hash_test.cairo @@ -85,3 +85,24 @@ fn test_user_defined_hash() { 'Bad hash of StructForHash', ); } + +#[test] +fn test_byte_array_hash() { + let byte_array: ByteArray = "This is a sentence that is longer than 31 characters."; + + let word_1 = 'This is a sentence that is long'; + let data_length = 1; + let pending_word = 'er than 31 characters.'; + let pending_word_len = 22; + + assert_eq( + @PoseidonTrait::new().update_with(byte_array).finalize(), + @PoseidonTrait::new() + .update(data_length) + .update(word_1) + .update(pending_word_len) + .update(pending_word) + .finalize(), + 'Bad hash for ByteArray', + ); +} From a3172b42fc173390e900d460142447d6ad7b3d08 Mon Sep 17 00:00:00 2001 From: Hugo Demeyere Date: Fri, 10 Jan 2025 16:02:39 +0100 Subject: [PATCH 2/2] feat(corelib): Added test for ByteArray hash using Pedersen --- corelib/src/test/hash_test.cairo | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/corelib/src/test/hash_test.cairo b/corelib/src/test/hash_test.cairo index c3046c414d6..c909a3bcc96 100644 --- a/corelib/src/test/hash_test.cairo +++ b/corelib/src/test/hash_test.cairo @@ -1,5 +1,6 @@ use crate::hash::{HashStateExTrait, HashStateTrait}; use crate::poseidon::PoseidonTrait; +use crate::pedersen::PedersenTrait; use crate::test::test_utils::assert_eq; #[test] @@ -103,6 +104,26 @@ fn test_byte_array_hash() { .update(pending_word_len) .update(pending_word) .finalize(), - 'Bad hash for ByteArray', + 'Bad ByteArray hash w/ Poseidon', + ); + + // Perform same test using Pedersen hashing function + + let byte_array: ByteArray = "This is a sentence that is longer than 31 characters."; + + let word_1 = 'This is a sentence that is long'; + let data_length = 1; + let pending_word = 'er than 31 characters.'; + let pending_word_len = 22; + + assert_eq( + @PedersenTrait::new(0).update_with(byte_array).finalize(), + @PedersenTrait::new(0) + .update(data_length) + .update(word_1) + .update(pending_word_len) + .update(pending_word) + .finalize(), + 'Bad ByteArray hash w/ Pedersen', ); }