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

Add implementation of hash160 #124

Merged
merged 9 commits into from
Oct 18, 2023
Merged

Add implementation of hash160 #124

merged 9 commits into from
Oct 18, 2023

Conversation

Acaccia
Copy link
Collaborator

@Acaccia Acaccia commented Oct 17, 2023

This PR adds the function hash160.

The algorithm for ripemd160 (which is part of hash160 with sha256) is described in this paper.

I had a few options for the implementation, depending on the factors I wanted to prioritize: code size, speed, readability, or maintainability.
It is possible to write this algorithm with a single loop that dispatches the functions used depending on the iteration, or to unroll it completely.

At first, I did the intermediate implementation: the compression consists of 5 loops with 16 iterations, which all use the same function inside. It was a nightmare: the code was several hundredth lines long, and any bug in a loop had to be fixed in all loops.

I decided then to go to the single loop with 80 iterations. The function dispatch is done by looking in a table for the function to pick. This implementation is less fast (because of the call_indirect), but is 5 times smaller than the previous one.

Still, if we find later that this implementation might be too slow, it is possible to rewrite it and unroll it by hand.

It was also possible to compute during runtime some of the values that are used in this algorithm. I decided to precompute them all and to store them in memory (360 bytes total). Another possibility here would have been to compute them with v128.
I unilaterally decided that we could use 360 bytes and avoid some calculations at runtime.

As for #117 , there are no property tests. This algorithm has massive changes in the results for any single mistake, so the fact that the current tests work can give us confidence that this implementation is correct.
Still, we should add some in the future.

Closes #69

@Acaccia Acaccia self-assigned this Oct 17, 2023
@Acaccia Acaccia requested a review from obycode October 17, 2023 17:07
Copy link
Collaborator

@obycode obycode left a comment

Choose a reason for hiding this comment

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

Looks great!

@Acaccia Acaccia added this pull request to the merge queue Oct 18, 2023
Merged via the queue into main with commit 4fb7fc3 Oct 18, 2023
4 checks passed
@Acaccia Acaccia deleted the feat/hash160 branch October 18, 2023 13:16
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.

Computation functions
2 participants