lambdaclass/cairo_native

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

Closed this issue · 0 comments

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.