Repository navigation
Compute concrete ADDMOD and MULMOD with Integer - #1251
Open
DicksonWu654 wants to merge 1 commit into
Open
DicksonWu654 wants to merge 1 commit into
DicksonWu654 wants to merge 1 commit into
Conversation
Concrete ADDMOD and MULMOD widened their operands to Word512 and did the addition or multiplication and the remainder in software 512-bit arithmetic. Use Integer instead. For 256-bit operands the sum is below 2^257 and the product below 2^512, so the result is the same, and the remainder is below the modulus, so it fits in a W256. A zero modulus still gives zero, and symbolic operands are handled as before. Add tests for full-width operands and a property comparing both operations with the Word512 computation. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
Description
Concrete
ADDMODandMULMOD(Expr.addmod/Expr.mulmodon literals) widen their operands toWord512and do the addition or multiplication and themodin software 512-bit arithmetic. This PR usesIntegerinstead; the change isinto @Word512→into @Integeron two lines. For 256-bit operands the sum is below 2^257 and the product below 2^512, so the exact value is the same as before, and the remainder is below the modulus, so it still fits in aW256. A zero modulus still gives 0, and symbolic operands go throughop3as before.It matters for code that calls
mulmodin a loop, such as Uniswap'sFullMath.mulDiv(twomulmods per call).New tests:
conc-modular-arithmetic-full-width(all-ones operands with moduli2^256-1and2^256-2, and a zero modulus), and a QuickCheck property comparing both operations with the previousWord512computation.Numbers
Linux x86-64, GHC 9.8.4,
-O2, both builds made the same way from this branch and from its base (8007137), runs pinned to two cores on a shared machine. Ratios are base/PR, so above 1 is faster.hevm execof a contract that calls the unmodifiedFullMath.mulDiv(from Echidna'stests/solidity/assert/fullmath.sol) 100,000 times with large operands: median 16.64 s → 3.16 s, median paired ratio 5.09x (3 inputs, AB and BA order, 6 pairs between 4.86x and 5.83x). The return value and both output streams are identical, and for 3-call runs the--json-tracefiles are byte-identical.Echidna master built against this branch and against 8007137, six campaigns from Echidna's
tests/solidity(50,000 calls, 1 worker, 5 seeds × ABBA, 10 pairs per campaign; wall time includes compilation and shrinking):assert/fullmath.solbasic/flags.solbasic/assert.soloptimize/linear.solresearch/vera_dschief.solresearch/ilf_crowdsale.solThe five campaigns that do not use
mulmodallocate the same to within 0.01%, and in other blocks of the same benchmark they moved by a few percent in either direction, so I read them as noise. With a fixed seed, all four runs of every campaign/seed produced byte-identical corpora, reports and coverage on both builds.Echidna pins hevm 8007137, so it would get this with its next hevm bump.
Checklist
test1100/1100 (1098 existing + 2 new,-j1 --quickcheck-replay 43),ethereum-tests14881 passed / 2108 ignored (execution-spec-tests v5.4.0 as pinned inflake.nix),cli-test30/30, built with-f ci(-Werror). Echidna's test suite against this branch passes with solc 0.8.10 and 0.8.25. Not run locally:rpc-tests(needs an RPC key) andforge-symbolic-tests.🤖 Generated with Claude Code