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

fixed a bug where strings with will get the wrong sha256 value #6030

Merged
merged 1 commit into from
Jul 17, 2024

Conversation

TomerStarkware
Copy link
Collaborator

@TomerStarkware TomerStarkware commented Jul 16, 2024

This change is Reviewable

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 2 files reviewed, 2 unresolved discussions (waiting on @TomerStarkware)


corelib/src/test/sha256_test.cairo line 15 at r1 (raw file):

    assert_eq!(a, 0x9834876d);
    let [a, _, _, _, _, _, _, _,] = compute_sha256_byte_array(@"aaaa");
    assert_eq!(a, 0x61be55a8);

extend these tests as well.

Code quote:

    let [a, _, _, _, _, _, _, _,] = compute_sha256_byte_array(@"a");
    assert_eq!(a, 0xca978112);
    let [a, _, _, _, _, _, _, _,] = compute_sha256_byte_array(@"aa");
    assert_eq!(a, 0x961b6dd3);
    let [a, _, _, _, _, _, _, _,] = compute_sha256_byte_array(@"aaa");
    assert_eq!(a, 0x9834876d);
    let [a, _, _, _, _, _, _, _,] = compute_sha256_byte_array(@"aaaa");
    assert_eq!(a, 0x61be55a8);

corelib/src/test/sha256_test.cairo line 408 at r1 (raw file):

            3889463563,
            3109547134,
            3732685805

make all tests with hex numeric values.

Code quote:

            3545823548,
            3587795446,
            676063049,
            3347034665,
            1546494401,
            3889463563,
            3109547134,
            3732685805

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 2 files reviewed, 3 unresolved discussions (waiting on @TomerStarkware)


corelib/src/test/sha256_test.cairo line 410 at r1 (raw file):

            3732685805
        ]
    );

also - consider adding a test helper function converting a u32 array to u256 to vastly shorten the tests.

fn to_u256(arr: [u32; 8]) -> u256 {
     let mut value: u256 = 0;
     for word in arr.span() {
          value *= 0x100000000;
          value += word.into();
     }
     value
}

@TomerStarkware TomerStarkware force-pushed the tomer/sha256_bug branch 2 times, most recently from 69b7ac7 to a549f01 Compare July 17, 2024 07:06
Copy link
Collaborator Author

@TomerStarkware TomerStarkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 2 files reviewed, 2 unresolved discussions (waiting on @orizi)


corelib/src/test/sha256_test.cairo line 15 at r1 (raw file):

Previously, orizi wrote…

extend these tests as well.

Done.


corelib/src/test/sha256_test.cairo line 408 at r1 (raw file):

Previously, orizi wrote…

make all tests with hex numeric values.

Done.


corelib/src/test/sha256_test.cairo line 410 at r1 (raw file):

Previously, orizi wrote…

also - consider adding a test helper function converting a u32 array to u256 to vastly shorten the tests.

fn to_u256(arr: [u32; 8]) -> u256 {
     let mut value: u256 = 0;
     for word in arr.span() {
          value *= 0x100000000;
          value += word.into();
     }
     value
}

Done.

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 2 files at r1, all commit messages.
Reviewable status: 1 of 2 files reviewed, 2 unresolved discussions (waiting on @TomerStarkware)


corelib/src/test/sha256_test.cairo line 12 at r3 (raw file):

    value
}
#[test]

put after the test, have emptyline between the functions and doc.

also - consider just making it also call the sha itself:

Suggestion:

use core::starknet::SyscallResultTrait;
use core::sha256::compute_sha256_byte_array;

fn sha256_as_u256(input: ByteArray) -> u256 {
    let hash_result = compute_sha256_byte_array(@input);
    let mut value: u256 = 0;
    for word in hash_result.span() {
        value *= 0x100000000;
        value = value + (*word).into();
    };
    value
}

#[test]

Copy link
Collaborator Author

@TomerStarkware TomerStarkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 1 of 2 files reviewed, 1 unresolved discussion (waiting on @orizi)


corelib/src/test/sha256_test.cairo line 12 at r3 (raw file):

Previously, orizi wrote…

put after the test, have emptyline between the functions and doc.

also - consider just making it also call the sha itself:

Done.

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewed 1 of 1 files at r4, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @TomerStarkware)

@TomerStarkware TomerStarkware added this pull request to the merge queue Jul 17, 2024
Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @TomerStarkware)

Merged via the queue into main with commit 3cb7291 Jul 17, 2024
44 checks passed
@orizi orizi deleted the tomer/sha256_bug branch July 31, 2024 21:18
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

Successfully merging this pull request may close these issues.

None yet

2 participants