Repository navigation
Fix byte order in MODEXP output and BYTE - #31
Merged
Merged
Conversation
`encoded` stored each whole limb as `swap_bytes(value)` through a `*u64`, which writes the limb's bytes big-endian only where memory is little-endian. Compiled for the EVM, whose memory is big-endian, each whole eight-byte limb of the output came out reversed: a zero exponent with a 17-byte modulus returned 0000000000000000000100000000000000 instead of 0000000000000000000000000000000001. The store now writes `to_be(value)`, which is the same byte swap on native targets: the O2 native IR of `modexp::run` is unchanged. fevm's tests run natively, on a little-endian host, where the old code is right, and the precompiles ingot does not compile for the EVM (`sha256` uses `std::native::sha256_compress`), so no fevm test can show the bug. The regression tests are fe-equivm-proofs' `scripts/ModexpVectors.lean` and `scripts/ModexpDiff.lean`, which run the EVM-target export of this code on go-ethereum's MODEXP vectors and against EVMLean: before the fix 43 of 47 vectors and 81 of 200 generated inputs disagree, after it all 47 vectors agree.
`op_byte` read byte `31 - i` of the stack word's representation, which is the byte `i` places from the word's most significant end only where memory is little-endian. It now reads byte `i` where memory is big-endian, by a test of `to_le` that is a constant: on native targets the O2 IR of `op_byte` is unchanged. Native hosts are little-endian, so no behaviour changes there; the Osaka frame differential passes at O0, O1 and O2.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two places assumed little-endian memory.
encodedstored each whole output limb asswap_bytes(value)through a*u64, which is big-endian only where memory is little-endian. Compiled for the EVM, each whole eight-byte limb came out reversed: a zero exponent with a 17-byte modulus returned0000000000000000000100000000000000instead of0000000000000000000000000000000001. It now storesto_be(value).op_byteread byte31 - iof the stack word's representation. It now picks the index by the target's byte order.Native code is unchanged: the O2 IR of
modexp::runandop_byteis the same before and after. The tests run natively on little-endian hosts and the precompiles ingot does not build for the EVM, so no test here can fail before the fix; it was found by running the EVM-target export ofmodexp.feagainst go-ethereum's MODEXP vectors (43 of 47 disagreed, all agree now).