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

VM: Fix Blake2B with messages with a length >= 5 #1486

Merged
merged 5 commits into from
Sep 23, 2021
Merged

Conversation

jochem-brouwer
Copy link
Member

@jochem-brouwer jochem-brouwer commented Sep 21, 2021

Closes #1482

I need to verify that these changes are correct. I noticed that in the original spec (https://github.com/dcposch/blakejs/blob/master/blake2b.js) the B2B_GET32 receives an Uint8Array, where we pass an Uint32Array. The Uint32Array we are using is already little endian.

I have locally fuzzed 1000 random messages (rounds <= 2^16-1) against Infura and verified these were the same as which the VM reports after these changes.

@codecov
Copy link

codecov bot commented Sep 21, 2021

Codecov Report

Merging #1486 (4458bcd) into master (d361b55) will decrease coverage by 0.14%.
The diff coverage is 100.00%.

Impacted file tree graph

Flag Coverage Δ
block 86.60% <ø> (ø)
blockchain 82.78% <ø> (-0.07%) ⬇️
client 83.67% <ø> (-0.48%) ⬇️
common 94.08% <ø> (-0.19%) ⬇️
devp2p 82.79% <ø> (+0.13%) ⬆️
ethash 86.56% <ø> (ø)
tx 87.34% <ø> (-0.13%) ⬇️
vm 79.38% <100.00%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

@jochem-brouwer
Copy link
Member Author

I noticed that Uint32 arrays endianness is based upon the OS endianness. So we might want to test this also on big endian OS - then this might fail.

@holgerd77
Copy link
Member

@jochem-brouwer pretty cool that you got something on this that quickly! 😄

This is what I got from Sina, asked him per DM if he has got some insight:

oof that's a tricky one 🙂 I copied the blake2 implementation from a library and added the wrapping which makes it a precompile. From the issue description I gather the problem should be somehow with the m variable (message being hashed). But having looked at it for some time nothing jumps out in the precompile-part of the code and I don't really understand the blake2 part enough...

@jochem-brouwer
Copy link
Member Author

jochem-brouwer commented Sep 22, 2021

I have created a dirty fuzzer and ran 1000 random inputs against Infura and checked if the VM reported the same outcome.

I have tested rounds up to 2^16-1, if I use more rounds (one could theoretically do 2^32-1 rounds) I run against out of gas at Infura.

(If necessary I can dump the fuzzer in a comment here)

@jochem-brouwer
Copy link
Member Author

To clear up any confusion about endianness; we actually read the message in little endian as can be seen here.

@jochem-brouwer jochem-brouwer marked this pull request as ready for review September 22, 2021 16:19
@jochem-brouwer
Copy link
Member Author

I am pretty convinced this is now fixed.

@axic
Copy link
Member

axic commented Sep 22, 2021

Is it worth adding an upstream test case for this in https://github.com/ethereum/tests?

@jochem-brouwer
Copy link
Member Author

Hi @axic, I have contacted @winsvega from ethereum/tests, they will add this case to ethereum/tests 😄

Copy link
Contributor

@s1na s1na left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@holgerd77 holgerd77 left a comment

Choose a reason for hiding this comment

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

Very cool, Ori has even already added some new test cases for this (see ethereum/tests#948).

Will merge, planning monorepo releases for Monday or Tuesday anyhow, so this will get into a release soon.

@holgerd77 holgerd77 merged commit c8fecf7 into master Sep 23, 2021
@holgerd77 holgerd77 deleted the fix-blake2b branch September 23, 2021 09:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

VM: Blake2 precompiled contract gives incorrect result on some inputs
4 participants