Merged
Conversation
This was referenced Jan 27, 2026
Contributor
Author
|
Updated benchmarks with the new commits that support the other operations: AddMod/MulMod/SMod
|
Contributor
Author
|
Changing single digit quotient estimates using 3 digits by 2 digits (div3by2) to 2 digits by 1 digit (div2by1).
|
Contributor
Code reviewFound 1 issue:
🤖 Generated with Claude Code - If this code review was useful, please react with 👍. Otherwise, react with 👎. |
Contributor
Unit test showing bug here https://github.com/thomas-quadratic/besu/pull/5/changes |
lu-pinto
reviewed
Feb 14, 2026
| long x63 = (x + 1) >>> 1; | ||
| long v0 = LUT[x9 - 256] & 0xFFFFL; | ||
| long v1 = (v0 << 11) - ((v0 * v0 * x40) >>> 40) - 1; | ||
| long v2 = (v1 << 13) + ((v1 * ((1L << 60) - v1 * x40)) >>> 47); |
Contributor
There was a problem hiding this comment.
No need for doing computation of a constant number here:
Suggested change
| long v2 = (v1 << 13) + ((v1 * ((1L << 60) - v1 * x40)) >>> 47); | |
| long v2 = (v1 << 13) + ((v1 * (0x1000000000000000L- v1 * x40)) >>> 47); |
I you want maybe just a static constant just above reciprocal method would do as well
lu-pinto
reviewed
Feb 16, 2026
lu-pinto
reviewed
Feb 16, 2026
lu-pinto
reviewed
Feb 16, 2026
lu-pinto
reviewed
Feb 16, 2026
Before, limbs were stored in little-endian. But to use Arrays.mismatch to our advantage, it is better to have it big-endian. This commit makes UInt256.java big-endian in limbs. We still need to migrate all tests and benchmark. Signed-off-by: Thomas Zamojski <thomas.zamojski@quadratic-labs.com>
Also added tests that were failing and now pass. Signed-off-by: Thomas Zamojski <thomas.zamojski@quadratic-labs.com>
Signed-off-by: Thomas Zamojski <thomas.zamojski@quadratic-labs.com>
Signed-off-by: Thomas Zamojski <thomas.zamojski@quadratic-labs.com>
Small cleaning up of the private methods for addition and compareLimbs. Should be easier for the compiler. Signed-off-by: Thomas Zamojski <thomas.zamojski@quadratic-labs.com>
UInt256 used int[] for limbs, primarily for simplicity, e.g. having the possibility to widen to long. However, methods exists to work with long[] and no widening. This commit implements long limbs. To avoid widening, we do: 1. add: overflow check 2. mul: native multiplyHigh (compiled to assembly mulq) 3. div: more complicated, see the gnump division paper. Signed-off-by: Thomas Zamojski <thomas.zamojski@quadratic-labs.com>
Signed-off-by: Thomas Zamojski <thomas.zamojski@quadratic-labs.com>
Signed-off-by: Thomas Zamojski <thomas.zamojski@quadratic-labs.com>
Signed-off-by: Thomas Zamojski <thomas.zamojski@quadratic-labs.com>
Signed-off-by: Thomas Zamojski <thomas.zamojski@quadratic-labs.com>
Signed-off-by: Thomas Zamojski <thomas.zamojski@quadratic-labs.com>
Signed-off-by: Thomas Zamojski <thomas.zamojski@quadratic-labs.com>
Signed-off-by: Thomas Zamojski <thomas.zamojski@quadratic-labs.com>
Add refactor-focused jqwik properties for the UInt256 4xlong record
representation.
Run:
./gradlew :evm:test --tests org.hyperledger.besu.evm.UInt256RecordProp
Observed failures (at introduction time):
- property_shiftLeft_matches_big_integer_mod_2_256 (seed 1123581321)
Shrunk: a = 0x00000000000000000000000000000000000000000000000000000000000000ff
shift = -512
Expected: 0x00..00
Got: 0xff..ff
- property_shiftRight_matches_big_integer_mod_2_256 (seed 867530900)
Shrunk: a = 0x00000000000000000000000000000000000000000000000000000000000000ff
shift = -512
Expected: 0x00..00
Got: 0x00..ff
- property_mod_matches_big_integer_unsigned (seed 123456789)
Sample: arg0 = byte[] [-128, 0, 0, 0, 0, 0, 0, 0, -128]
arg1 = 64-byte MSB-heavy pattern (truncated to 32)
Expected: 0x00..00
Got: 0x00..80
- property_signedMod_matches_evm_semantics (seed 987654321)
Sample: arg0 = byte[] [-128, 0, 0, 0, 0, 0, 0, 0, -128]
arg1 = byte[] [-128]
Expected: 0x00..00
Got: 0x00..80
- property_addMod_matches_big_integer_unsigned (seed 42424242)
Sample: arg0 = byte[] []
arg1 = byte[] [-128, 0, 0, 0, 0, 0, 0, 0, -128]
arg2 = 64-byte MSB-heavy pattern (truncated to 32)
Expected: 0x00..00
Got: 0x00..80
Signed-off-by: Nikos Baxevanis <nikos.baxevanis@gmail.com>
UInt256.shiftLeft/shiftRight are only defined for 0 <= shift < 64. Gate the corresponding properties with an assumption so they don't assert EVM-wide shift semantics. This removes shift-related false positives; remaining failures are isolated to mod/signedMod/addMod. Signed-off-by: Nikos Baxevanis <nikos.baxevanis@gmail.com>
Signed-off-by: Thomas Zamojski <thomas.zamojski@quadratic-labs.com>
Signed-off-by: Thomas Zamojski <thomas.zamojski@quadratic-labs.com>
Signed-off-by: Simon Dudley <simon.dudley@consensys.net>
Signed-off-by: Luis Pinto <luis.pinto@consensys.net>
Signed-off-by: Luis Pinto <luis.pinto@consensys.net>
Signed-off-by: Luis Pinto <luis.pinto@consensys.net>
Signed-off-by: Luis Pinto <luis.pinto@consensys.net>
Signed-off-by: Luis Pinto <luis.pinto@consensys.net>
Signed-off-by: Luis Pinto <luis.pinto@consensys.net>
Signed-off-by: Luis Pinto <luis.pinto@consensys.net>
Signed-off-by: Luis Pinto <luis.pinto@consensys.net>
Signed-off-by: Luis Pinto <luis.pinto@consensys.net>
Signed-off-by: Luis Pinto <luis.pinto@consensys.net>
Signed-off-by: Luis Pinto <luis.pinto@consensys.net>
4ac107c to
c0dc03f
Compare
lu-pinto
approved these changes
Feb 24, 2026
Signed-off-by: Thomas Zamojski <thomas.zamojski@quadratic-labs.com>
Contributor
|
I'm running a hoodi sync just as a smoke test. will merge when that succeeds |
10 tasks
This was referenced Mar 19, 2026
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.
PR description
This PR cumulates a few improvements/refactoring for UInt256 modular arithmetics:
Long limbs are more efficient because they divide by 2 the number of digits, thus reducing the number of steps in arithmetics operations. However, it complexifies the implementation for division as widening is not possible anymore. Fortunately, quotient estimates algorithms that avoids widening exists, see division paper.
Records are chosen to represent fixed-width UInt256 because of potential future support for Vahalla value records, which would require almost no change to the code.
Here are the benchmarks compared to the actual implementation:
Current status
Currently working and tested ops:
Thanks for sending a pull request! Have you done the following?
doc-change-requiredlabel to this PR if updates are required.Locally, you can run these tests to catch failures early:
./gradlew spotlessApply./gradlew build./gradlew acceptanceTest./gradlew integrationTest./gradlew ethereum:referenceTests:referenceTests