Skip to content

Commit

Permalink
Fix: cairo_native swaps the high and low fields of U256 compared to C…
Browse files Browse the repository at this point in the history
…airo's u256 (#720)

* fix u256 fields swapped

* fix tests

* apply suggestion

* add test

* remove dbg

* fix gas
  • Loading branch information
edg-l authored Jul 8, 2024
1 parent ccbd2da commit 9527cc9
Show file tree
Hide file tree
Showing 7 changed files with 300 additions and 227 deletions.
2 changes: 1 addition & 1 deletion src/starknet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ pub struct Felt252Abi(pub [u8; 32]);
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(C, align(16))]
pub struct U256 {
pub hi: u128,
pub lo: u128,
pub hi: u128,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down
241 changes: 119 additions & 122 deletions src/starknet_stub.rs

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions tests/tests/starknet/contracts/test_u256_order.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#[starknet::interface]
trait IKeccak<TContractState> {
fn cairo_keccak_test(self: @TContractState) -> felt252;
}

#[starknet::contract]
mod Keccak {
use core::clone::Clone;
use array::{Array, ArrayTrait};
use core::traits::Into;

#[storage]
struct Storage {}

#[abi(embed_v0)]
impl Keccak of super::IKeccak<ContractState> {
fn cairo_keccak_test(self: @ContractState) -> felt252 {
let input : Array::<u64> = array![1,2,4,5,6,6,7,2,3,4,4,5,5,6,7,7,2];
let output = starknet::syscalls::keccak_syscall(input.span()).unwrap();

if output.low == 0x9293867273ef341e81577655f28aeade && output.high == 0xf70cba9bb86caa97b086fdfa3df602ed {
panic_with_felt252('arguments swapped');
}
output.low.into()
}
}
}
1 change: 1 addition & 0 deletions tests/tests/starknet/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod keccak;
mod secp256;
mod u256;

#[cfg(feature = "with-cheatcode")]
mod syscalls;
Loading

0 comments on commit 9527cc9

Please sign in to comment.