cairo_lang_starknet_classes/
keccak.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use num_bigint::BigUint;
use sha3::{Digest, Keccak256};

#[cfg(test)]
#[path = "keccak_test.rs"]
mod test;

/// A variant of eth-keccak that computes a value that fits in a Starknet field element.
pub fn starknet_keccak(data: &[u8]) -> BigUint {
    let mut hasher = Keccak256::new();
    hasher.update(data);
    let mut result = hasher.finalize();

    // Truncate result to 250 bits.
    *result.first_mut().unwrap() &= 3;
    BigUint::from_bytes_be(&result)
}