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 RIPEMD160 hash function and EVM precompile #505

Merged
merged 12 commits into from
Jan 13, 2025
Merged

Add RIPEMD160 hash function and EVM precompile #505

merged 12 commits into from
Jan 13, 2025

Conversation

Vindaar
Copy link
Collaborator

@Vindaar Vindaar commented Dec 31, 2024

The hash function implementation right now is a direct port of the bitcoin-core implementation here:

https://github.com/bitcoin-core/gui/blob/228aba2c4d9ac0b2ca3edd3c2cdf0a92e55f669b/src/crypto/ripemd160.cpp

  • Currently there are a few mini test cases in the ripemd160_generic.nim test file under a when isMainModule section, which need to move to a proper test case. Probably these static ones and then similar to the sha256 / keccak256 cases compared to OpenSSL for random data.

@mratsim mratsim changed the base branch from ecRecover to master January 2, 2025 10:22
@mratsim
Copy link
Owner

mratsim commented Jan 2, 2025

The base branch of this PR is ecRecover instead of master so failure is due to OpenSSL as in #504.

@mratsim mratsim changed the base branch from master to ecRecover January 2, 2025 10:23
" " & "result: " & r.toHex() & "\n" &
" " & "expected: " & expectedbytes.toHex() & '\n'

stdout.write "Success\n"
Copy link
Owner

Choose a reason for hiding this comment

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

That's the only test vector available?

I think we need to add fuzzing vs OpenSSL on Linux as well, similar to SHA256.

Copy link
Owner

Choose a reason for hiding this comment

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

There are some extra in last page of https://www.esat.kuleuven.be/cosic/publications/article-317.pdf

Hash of "" =
0x9c1185a5c5e9fc54612808977ee8f548b2258d31
Hash of "a" =
0x0bdc9d2d256b3ee9daae347be6f4dc835a467ffe
Hash of "abc" =
0x8eb208f7e05d987a9b044a8e98c6b087f15a0bfc
Hash of "message digest" =
0x5d0689ef49d2fae572b881b123a85ffa21595f36
Hash of "abcdefghijklmnopqrstuvwxyz" =
0xf71c27109c692c1b56bbdceb5b9d2865b3708dbc
Hash of "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" =
0x12a053384a9c0c88e405a06c27dcf49ada62eb2b
Hash of "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" =
0xb0e20b6e3116640286ed3a87a5713079b21f5189
Hash of 8 times "1234567890" =
0x9b752e45573d4b39f4dbd3323cab82bf63326bfb
Hash of 1 million times "a" =
0x52783243c1697bdbe16d37f97f68f08325dc1528

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, those test cases are where the ones in the file under isMainModule are from (and what the TODO in the PR message refers to).

R51(c1, d1, e1, a1, b1, w15, 5)
R52(c2, d2, e2, a2, b2, w9 , 11)
R51(b1, c1, d1, e1, a1, w13, 6)
R52(b2, c2, d2, e2, a2, w11, 11)
Copy link
Owner

Choose a reason for hiding this comment

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

That's super annoying to audit and RipeMD-160 is actually unused (https://ethereum-magicians.org/t/discussion-removal-of-ripemd-160-and-blake2f-precompiles/14857)

ideally we rewrite with this simpler pseudocode:

for(i := 0 to blocks - 1) {
    aLeft := h0
    bLeft := h1
    cLeft := h2
    dLeft := h3
    eLeft := h4

    aRight := h0
    bRight := h1
    cRight := h2
    dRight := h3
    eRight := h4

    for(int j := 0 to 79) {
        t := rotleft(s[j]) (aLeft + f(bLeft, cLeft, dLeft) + X[r[i]]) + eLeft
        aLeft := eLeft;
        eLeft := dLeft
        dLeft := rotleft(10) (c)
        cLeft := bLeft
        bLeft := t

        Do same for right
    }

    t := h1 + cLeft + dRight
    h1 := h2 + dLeft + eRight
    h2 := h3 + eLeft + aRight
    h3 := h4 + aLeft + bRight
    h4 := h0 + bLeft + cRight
    h0 := t
}

but at the same time, probably not worth the effort and Bitcoin is the biggest user of RIPEMD-160 (and with most stakes). It can be made a low-priority issue for first-time contributors.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I went with the current implementation (aside from time constraints), because it is essentially identical to the Bitcoin implementation (and I assume that one has seen plenty of eyes).

A first step towards a more custom solution could be to simply generate all those RXY calls from a macro & a constant array that contains the integers for the w_i to use.

But as you say, probably not that high on the priority list.

@mratsim mratsim changed the base branch from ecRecover to master January 7, 2025 07:52
@mratsim
Copy link
Owner

mratsim commented Jan 7, 2025

This needs a rebase so that it uses master, I don't think it depends on anything from ECDSA/ecrecover

@Vindaar
Copy link
Collaborator Author

Vindaar commented Jan 12, 2025

Rebased, force pushed and added the test vectors from https://homes.esat.kuleuven.be/~bosselae/ripemd160.html as well as fuzzing against OpenSSL following the SHA256 test case.

@mratsim mratsim merged commit ef2a91a into master Jan 13, 2025
12 checks passed
@mratsim mratsim deleted the ripemd160 branch January 13, 2025 21:10
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.

2 participants