Skip to content

Commit

Permalink
refactor: remove unsafe in starknet_keccak
Browse files Browse the repository at this point in the history
  • Loading branch information
xJonathanLEI authored and incrypto32 committed Jan 22, 2024
1 parent 3a9c18d commit 4b412f7
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions chain/starknet/src/data_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,21 @@ fn starknet_keccak(data: &[u8]) -> [u8; 32] {
// Remove the first 6 bits
hash[0] &= 0b00000011;

// Because we know hash is always 32 bytes
*unsafe { &*(hash[..].as_ptr() as *const [u8; 32]) }
hash.into()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_starknet_keccak() {
let expected_hash: [u8; 32] =
hex::decode("016c11b0b5b808960df26f5bfc471d04c1995b0ffd2055925ad1be28d6baadfd")
.unwrap()
.try_into()
.unwrap();

assert_eq!(starknet_keccak("Hello world".as_bytes()), expected_hash);
}
}

0 comments on commit 4b412f7

Please sign in to comment.