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

Adding Hash Solidity compatible example #116

Merged
merged 5 commits into from
Nov 21, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "hash_solidity_compatible"
version = "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "hash_solidity_compatible"
version = "0.1.0"

[dependencies]
starknet = ">=2.3.0"

[[target.starknet-contract]]
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#[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
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod contract;

#[cfg(test)]
mod tests;
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
mod tests {
use hash_solidity_compatible::{contract::{SolidityHashExample, ISolidityHashExample}};
use debug::PrintTrait;

use starknet::{
ContractAddress, get_contract_address, contract_address_const, call_contract_syscall,
testing::{set_contract_address}
};

fn setup() -> SolidityHashExample::ContractState {
let mut state = SolidityHashExample::contract_state_for_testing();
let contract_address = contract_address_const::<0x1>();
set_contract_address(contract_address);
state
}

#[test]
#[available_gas(2000000000)]
fn get_same_hash_solidity() {
let mut state = setup();
let mut array: Array<u256> = ArrayTrait::new();
array.append(1);

let hash_expected: u256 =
0xb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6;
let hash_received: u256 = state.hash_data(array.span());

assert(hash_received == hash_expected, 'hash_received != hash_expected');
}
}
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ Summary
- [Writing to any storage slot](./ch02/write_to_any_slot.md)
- [Storing Arrays](./ch02/storing_arrays.md)
- [Struct as mapping key](./ch02/struct-mapping-key.md)
- [Hash Solidity Compatible](./ch02/hash-solidity-compatible.md)
- [Optimisations](./ch02/optimisations/optimisations.md)
- [Storage Optimisations](./ch02/optimisations/store_using_packing.md)
11 changes: 11 additions & 0 deletions src/ch02/hash-solidity-compatible.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Hash Solidity Compatible

This contract demonstrates Keccak hashing in Cairo to match Solidity's keccak256. While both use Keccak, their endianness differs: Cairo is little-endian, Solidity big-endian. The contract achieves compatibility by hashing in big-endian using `keccak_u256s_be_inputs`, and reversing the bytes of the result with `u128_byte_reverse`.

For example:

```rust
{{#include ../../listings/ch02-advanced-concepts/hash_solidity_compatible/src/contract.cairo}}
```

Play with the contract in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch02-advanced-concepts/hash_solidity_compatible/src/contract.cairo).