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: Solidity-compatible hashes #123

Open
enitrat opened this issue Nov 22, 2023 · 8 comments
Open

bug: Solidity-compatible hashes #123

enitrat opened this issue Nov 22, 2023 · 8 comments
Assignees

Comments

@enitrat
Copy link
Collaborator

enitrat commented Nov 22, 2023

There is a problem with the example of solidity-compatible hashes. The way it's currently done is that it calls keccak_u256s_be_inputs to hash a span composed of u256 words - however this function expects only full u256 words!

Consider the following:

use debug::PrintTrait;
use keccak::keccak_u256s_be_inputs;

#[test]
fn test_full_world() {
    let input_data: Span<u256> = array![
        0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
    ]
        .span();
    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 };

    assert(
        compatible_hash == 0xa9c584056064687e149968cbab758a3376d22aedc6a55823d1b3ecbee81b8fb9,
        'wrong hash'
    )
}

#[test]
fn test_partial_word() {
    let input_data: Span<u256> = array![0xAA].span();
    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 };

    assert(
        compatible_hash == 0xdb81b4d58595fbbbb592d3661a34cdca14d7ab379441400cbfa1b78bc447c365,
        'wrong hash'
    )
}

If you run the tests, you'll notice that it works for the full u256 word (with all bits set) but not for the word that is not full.

This is a bit tricky, but basically what you should do instead is:

  • Split the u256 words into u64 chunks
  • If you're hashing multiple words, you need to pack the partially-filled last u64 word of the input n with the first u64 word of the input n+1
  • This requires counting how many bytes each word actually uses.

See how it's implemented in Alexandria for bytes inputs, or Herodotus for u64-words inputs.

@julio4
Copy link
Contributor

julio4 commented Nov 23, 2023

@julienbrs will work on it

@fontanellag
Copy link

Start working on it too.

@julio4 julio4 added the ODHack label May 24, 2024
@IjayAbby
Copy link

@julio4 kindly assign me this issue

@IjayAbby
Copy link

@julio4 where is the best place for me to test the bug?
Can't seem to find it

@julio4
Copy link
Contributor

julio4 commented May 31, 2024

@julio4 where is the best place for me to test the bug? Can't seem to find it

You need to add a new test that specifically verify this case

@IjayAbby
Copy link

IjayAbby commented Jun 1, 2024

Should I add it under listings/testing_how_to directory?

@julio4 julio4 removed the ODHack label Jun 3, 2024
@julio4
Copy link
Contributor

julio4 commented Jun 3, 2024

Should I add it under listings/testing_how_to directory?

This is a bug with the solidity-compatible hashes example, so you should fix the bug and add tests directly in this listing directory.

@IjayAbby
Copy link

Hello @julio4 I need little clarity
Am I supposed to replace the existing code on tests.cairo with the code snippet provided above?

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

No branches or pull requests

4 participants