Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

Bug fix and additional tests for Gimli hash function #110

Merged
merged 8 commits into from
May 18, 2021

Conversation

tanmay2004
Copy link
Contributor

@tanmay2004 tanmay2004 commented May 16, 2021

Recently, I discovered that the gimli hash function implemented here in Rust was giving different outputs than the expected outputs of the original python implementation for some inputs.

So, I randomly generated many test cases of variable input length. With some help from @franziskuskiefer, I discovered that the output was wrong only when the length of the input was a multiple of 16, as is evident from the test cases in gimli_hash.rs! We can use these tests to verify the accuracy of future attempts to fix the bug.

Due to the large size of the new gimli hash tests, I have split the gimli tests into 2 files: one for testing the gimli cipher and the other for the gimli hash exclusively. I have shifted the hash KATs to gimli_hash.rs.

Added three random generated test cases in tg_gimli_hash function
Divide gimli testing into separate files for the cipher and the hash function
@franziskuskiefer franziskuskiefer self-requested a review May 16, 2021 18:36
@karthikbhargavan
Copy link
Contributor

karthikbhargavan commented May 17, 2021 via email

@tanmay2004
Copy link
Contributor Author

I have found and fixed the bug! It was in the gimli_hash_state function.

for i in 0..input.num_chunks(rate) {

The issue was that when the input length was a multiple of rate, 16 in this case, input.num_chunks(rate) returned length / rate when it should instead have been 1 more than that. As the picture below (taken from gimli's official documentation) states, the input ends with one non-full (empty or partial) block.

image

Thus, even a 0 block of input left must be processed as mentioned in the documentation. However, the current gimli hash function currently did not consider that 0 block leftover and didn't process it, which led to errors in the final hash output when the input length was a multiple of 16.

I have made the fix in gimli.rs and it should mean that all tests added will pass!

@tanmay2004 tanmay2004 changed the title Additional tests for Gimli hash function and a bug find Bug fix and additional tests for Gimli hash function May 17, 2021
Copy link
Member

@franziskuskiefer franziskuskiefer left a comment

Choose a reason for hiding this comment

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

Thanks for finding and fixing this!
See comments inline.

Could you run rustfmt on the test files to make them look a little nicer?

examples/gimli/src/gimli.rs Outdated Show resolved Hide resolved
examples/gimli/src/gimli.rs Show resolved Hide resolved
Resolves changes suggested on PR hacspec#110
Formatted test cases by running rustfmt
Copy link
Member

@franziskuskiefer franziskuskiefer left a comment

Choose a reason for hiding this comment

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

Thanks!

@franziskuskiefer franziskuskiefer merged commit 1080392 into hacspec:master May 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants