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

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

Closed
xrvdg opened this issue Jul 3, 2024 · 0 comments · Fixed by #720
Closed

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

xrvdg opened this issue Jul 3, 2024 · 0 comments · Fixed by #720
Assignees

Comments

@xrvdg
Copy link

xrvdg commented Jul 3, 2024

The hi and lo field of U256 within cairo native doesn't correspond to high and low fields of u256 in cairo; they are swapped.

Reproducer

In the starknet example keccak is implemented as:

    fn keccak(
        &mut self,
        input: &[u64],
        _gas: &mut u128,
    ) -> SyscallResult<cairo_native::starknet::U256> {
        println!("Called `keccak({input:?})` from MLIR.");
        Ok(U256 {
            hi: 0,
            lo: 1234567890,
        })
    }

Invoking this from cairo as follows:

    #[external(v0)]
    fn echo(ref self: ContractState, value: felt252) -> felt252 {
        let input : Array::<u64> = Default::default();
        let output = starknet::syscalls::keccak_syscall(input.span()).unwrap();
        if output.low == 0 && output.high == 1234567890 {
            panic_with_felt252('arguments swapped');
        }
        output.low.into()
    }

leads to the panic 'arguments swapped'.

The reproducer can be cloned from here and executed with cargo r --example starknet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants