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

Starknet Keccak #57

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ indexmap = { version = "1.9.2", features = ["serde"] }
once_cell = { version = "1.16.0" }
serde = { version = "1.0.130", features = ["derive", "rc"] }
serde_json = { version = "1.0.81" }
sha3 = { version = "0.10.6"}
starknet-crypto = { version = "0.2.0" }
thiserror = { version = "1.0.31" }
web3 = { version = "0.18.0" }
Expand Down
16 changes: 16 additions & 0 deletions src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use std::fmt::{Debug, Display};
use std::io::Error;

use serde::{Deserialize, Serialize};
use sha3::Digest;
use starknet_crypto::{pedersen_hash as starknet_crypto_pedersen_hash, FieldElement};
use web3::types::U256;

use crate::serde_utils::{
bytes_from_hex_str, hex_str_from_bytes, BytesAsHex, NonPrefixedBytesAsHex, PrefixedBytesAsHex,
Expand All @@ -19,6 +21,9 @@ pub const GENESIS_HASH: &str = "0x0";
// Felt encoding constants.
const CHOOSER_FULL: u8 = 15;
const CHOOSER_HALF: u8 = 14;
/// The MASK equals to U256::pow(U256::from(2), U256::from(250)) - U256::from(1)
const MASK: U256 =
U256([18446744073709551615, 18446744073709551615, 18446744073709551615, 288230376151711743]);

/// An alias for [`StarkFelt`].
/// The output of the [Pedersen hash](https://docs.starknet.io/documentation/architecture_and_concepts/Hashing/hash-functions/#pedersen_hash).
Expand All @@ -43,6 +48,17 @@ pub fn pedersen_hash_array(felts: &[StarkFelt]) -> StarkHash {
pedersen_hash(&current_hash, &data_len)
}

/// Computes Starknet Keccak Hash, as defined
/// in <https://docs.starknet.io/documentation/architecture_and_concepts/Hashing/hash-functions/#starknet-keccak.>
pub fn starknet_keccak(data: &[u8]) -> Result<StarkFelt, StarknetApiError> {
let keccak256 = sha3::Keccak256::digest(data);
let number = U256::from_big_endian(keccak256.as_slice());
let masked_number = number & MASK;
let mut res_bytes: [u8; 32] = [0; 32];
masked_number.to_big_endian(&mut res_bytes);
StarkFelt::new(res_bytes)
}

// TODO: Move to a different crate.
/// The StarkNet [field element](https://docs.starknet.io/documentation/architecture_and_concepts/Hashing/hash-functions/#domain_and_range).
#[derive(Copy, Clone, Eq, PartialEq, Default, Hash, Deserialize, Serialize, PartialOrd, Ord)]
Expand Down
21 changes: 20 additions & 1 deletion src/hash_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::hash::{pedersen_hash, pedersen_hash_array, StarkFelt};
use web3::types::U256;

use crate::hash::{pedersen_hash, pedersen_hash_array, starknet_keccak, StarkFelt, MASK};
use crate::stark_felt;

#[test]
Expand Down Expand Up @@ -67,3 +69,20 @@ fn hash_serde() {
assert_eq!(bytes, d.0);
}
}

#[test]
fn starknet_keccak_mask() {
let mask: U256 = U256::pow(U256::from(2), U256::from(250)) - U256::from(1);
assert_eq!(mask, MASK);
}

#[test]
fn starknet_keccak_calculation() {
// Test result is taken from <https://www.cairo-lang.org/docs/hello_starknet/intro.html> tutorial.
// increase_balance function selector can be found in contract_compiled.json ->
// entry_points_by_type object.
let expected_keccak_felt =
stark_felt!("0x362398bec32bc0ebb411203221a35a0301193a96f317ebe5e40be9f60d15320");
let increase_balance_keccak_felt = starknet_keccak("increase_balance".as_bytes()).unwrap();
assert_eq!(increase_balance_keccak_felt, expected_keccak_felt);
}