Hash Solidity Compatible

Este contrato demuestra el hashing Keccak en Cairo para que coincida con el keccak256 de Solidity. Mientras que ambos usan Keccak, su endianness difiere: Cairo es little-endian, Solidity big-endian. El contrato consigue la compatibilidad haciendo hashing en big-endian usando keccak_u256s_be_inputs, e invirtiendo los bytes del resultado con u128_byte_reverse.

Por ejemplo:

#[starknet::interface]
trait ISolidityHashExample<TContractState> {
    fn hash_data(ref self: TContractState, input_data: Span<u256>) -> u256;
}


#[starknet::contract]
mod SolidityHashExample {
    use keccak::{keccak_u256s_be_inputs};
    use array::Span;

    #[storage]
    struct Storage {}

    #[abi(embed_v0)]
    impl SolidityHashExample of super::ISolidityHashExample<ContractState> {
        fn hash_data(ref self: ContractState, input_data: Span<u256>) -> u256 {
            let hashed = keccak_u256s_be_inputs(input_data);

            // Split the hashed value into two 128-bit segments
            let low: u128 = hashed.low;
            let high: u128 = hashed.high;

            // Reverse each 128-bit segment
            let reversed_low = integer::u128_byte_reverse(low);
            let reversed_high = integer::u128_byte_reverse(high);

            // Reverse merge the reversed segments back into a u256 value
            let compatible_hash = u256 { low: reversed_high, high: reversed_low };

            compatible_hash
        }
    }
}

Juega con el contrato en Remix.

Last change: 2023-11-21, commit: 58cde22