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: BytesTrait keccak computation mismatch #317

Closed
JordyRo1 opened this issue Jun 20, 2024 · 14 comments
Closed

bug: BytesTrait keccak computation mismatch #317

JordyRo1 opened this issue Jun 20, 2024 · 14 comments
Labels
stale No activity for quite some time.

Comments

@JordyRo1
Copy link
Contributor

JordyRo1 commented Jun 20, 2024

Bug Report

Alexandria version:

This bug appears for the most recent Alexandria version cairo-v2.6.0

Current behavior:
The BytesTrait keccak function sometimes output incorrect hash, notably when 0-padding is involved. Let me explain with an example.
The objective was to compute the keccak hash of the felt252 element: 0x48595045524c414e455f414e4e4f554e43454d454e54. The python implementation provided on tests
was used to check if the result matches the expectation. The expected result from the python script was 0xda15cbb01a970c130a1bf6ba8b35fcff0fabd4695bfb03f4454482023f2ae84c. The function keccak from BytesTrait and from the python script have inverse endianness, so the expected result should be 0x4CE82A3F02824445F403FB5B69D4AB0FFFFC358BBAF61B0A130C971AB0CB15DA.
However, the output result from the bytes.keccak() is 0x93F59F2604EE54DE51A2DFCFB3E0A6D74EDE53826CF2D1CBA9422A06AF66BC65. After further investigation, the input argument that could possibly lead to this hash is 0000000000000000000048595045524c414e455f414e4e4f554e43454d454e54.
This is why I assume the error might be linked to the padding , unless I am missing something.

Expected behavior:

The output hash of the keccak function should match the one computed using the python script.

Steps to reproduce:

You can just append the following lines to the test_bytes_keccak function for example to test.

Related code:

let mut bytes : Bytes = BytesTrait::new_empty();
bytes.append_felt252(0x48595045524c414e455f414e4e4f554e43454d454e54);
let res = bytes.keccak();
assert_eq!(res, 0x4CE82A3F02824445F403FB5B69D4AB0FFFFC358BBAF61B0A130C971AB0CB15DA);
from Crypto.Hash import keccak
k = keccak.new(digest_bits=256)
k.update(bytes.fromhex('48595045524c414e455f414e4e4f554e43454d454e54'))
print(k.hexdigest()) 
# output: da15cbb01a970c130a1bf6ba8b35fcff0fabd4695bfb03f4454482023f2ae84c
@JordyRo1 JordyRo1 changed the title bug: BytesTrait computation mismatch bug: BytesTrait keccak computation mismatch Jun 20, 2024
@gaetbout
Copy link
Collaborator

Gm gm,
Could only have a very quick look so far so I'll take a look more in depth later when I can.
Are you sure it didn't happen before v2.6.0?
Because the changes done in that diff are only syntactic changes.

@JordyRo1
Copy link
Contributor Author

Hey, no sorry I meant that the version that I used for the test was 2.6.0. But it occurs with previous versions too. Thank you for your time.

@gaetbout
Copy link
Collaborator

GM @zkcarter
You are the one that did this file, would you have any idea :) ?

@gaetbout
Copy link
Collaborator

This code should be removed thanks to:
starkware-libs/cairo#5877
When they release it

@gaetbout
Copy link
Collaborator

@JordyRo1 do you still think it needs to be addressed given that it'll be part of the standard library?
Don't close this issue, we'll keep it as a reminder to remove that piece of code :)

@JordyRo1
Copy link
Contributor Author

@gaetbout No it's not necessary anymore, no problem I will not delete it !

@zkcarter
Copy link
Contributor

Hi @gaetbout BytesTrait is a supplement to the standard library that has been developed for some time. I believe that once these functions are implemented in the standard library, this code should be removed, and developers should be encouraged to use the standard library.

@gaetbout
Copy link
Collaborator

We'll do it, thanks @zkcarter !

Copy link

There hasn't been any activity on this issue recently, and in order to prioritize active issues, it will be marked as stale.
Please make sure to update to the latest version and check if that solves the issue. Let us know if that works for you by leaving a 👍
Because this issue is marked as stale, it will be closed and locked in 7 days if no further activity occurs.
Thank you for your contributions!

@github-actions github-actions bot added the stale No activity for quite some time. label Jul 27, 2024
@gaetbout
Copy link
Collaborator

Stay alive

@github-actions github-actions bot removed the stale No activity for quite some time. label Jul 28, 2024
@gaetbout
Copy link
Collaborator

gaetbout commented Aug 5, 2024

Should be solved with #321

Copy link

github-actions bot commented Sep 5, 2024

There hasn't been any activity on this issue recently, and in order to prioritize active issues, it will be marked as stale.
Please make sure to update to the latest version and check if that solves the issue. Let us know if that works for you by leaving a 👍
Because this issue is marked as stale, it will be closed and locked in 7 days if no further activity occurs.
Thank you for your contributions!

@github-actions github-actions bot added the stale No activity for quite some time. label Sep 5, 2024
@gaetbout
Copy link
Collaborator

Solved with #321

@gaetbout
Copy link
Collaborator

@JordyRo1 Can you close this :) ?

@github-actions github-actions bot locked and limited conversation to collaborators Oct 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
stale No activity for quite some time.
Projects
None yet
Development

No branches or pull requests

3 participants