-
Notifications
You must be signed in to change notification settings - Fork 9
feat: add mod benchmark #1362
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
base: main
Are you sure you want to change the base?
feat: add mod benchmark #1362
Conversation
This adds a preliminary implementation of the MOD module. The main outstanding issue is the fact that there is no support at this time for division. Realistically, this is needed since otherwise the implementation is incredibly inefficient.
These tests have been generated from mainnet blocks.
| return | ||
| smod: | ||
| RES, tmp = signed_divide(ARG_1, ARG_2) | ||
| return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: SMOD operation returns quotient instead of remainder
The smod case incorrectly returns the quotient instead of the remainder. The signed_divide function returns (quot, rem), so RES, tmp = signed_divide(...) assigns the quotient to RES. For SMOD (signed modulo), it should be tmp, RES = signed_divide(...) to return the remainder, matching the pattern used for unsigned MOD at line 16.
| var abs_quot, abs_rem u256 | ||
| ;; | ||
| abs_num, num_s = abs(num) | ||
| abs_den, den_s = abs(num) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| ;; negative result | ||
| quot = negate(abs_quot) | ||
| rem = negate(abs_rem) | ||
| return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Remainder sign incorrectly follows quotient sign logic
The signed_divide function applies the same sign logic to both quotient and remainder, but EVM semantics require different rules. The quotient sign depends on num_s XOR den_s (correctly implemented), but the remainder sign must depend only on num_s (the dividend's sign). Currently, both quot and rem are negated when num_s != den_s, which produces incorrect remainder values when the dividend and divisor have opposite signs.
Note
Adds
asm/bench/modwith zkASM implementation (DIV/MOD/SDIV/SMOD) and corresponding test plus accept vectors.Test_AsmBench_Modinpkg/test/assembly_bench_test.goto runasm/bench/mod.testdata/asm/bench/mod.zkasmimplementingmoddispatcher forEVM_INST_DIV,EVM_INST_MOD,EVM_INST_SDIV,EVM_INST_SMOD.signed_divide,abs,negate, and a temporary recursivedivide.testdata/asm/bench/mod.acceptscovering various inputs for unsigned/signed division and modulo.Written by Cursor Bugbot for commit 9b64fe3. This will update automatically on new commits. Configure here.