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

Fix: cairo_native swaps the high and low fields of U256 compared to Cairo's u256 #720

Merged
merged 8 commits into from
Jul 8, 2024
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
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
Loading