-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Minor Mulmod v2 refactor #10253
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
Minor Mulmod v2 refactor #10253
Changes from 2 commits
66e15f2
e2cab1d
f935549
cef643e
eec0607
a6c0500
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -50,33 +50,39 @@ public Operation.OperationResult executeFixedCostOperation( | |||||||||||||||||||
| public static OperationResult staticOperation(final MessageFrame frame, final long[] stack) { | ||||||||||||||||||||
| if (!frame.stackHasItems(3)) return UNDERFLOW_RESPONSE; | ||||||||||||||||||||
| int top = frame.stackTopV2(); | ||||||||||||||||||||
| mulMod(stack, top); | ||||||||||||||||||||
| // consumed three items and produced one item | ||||||||||||||||||||
| frame.setTopV2(top - 2); | ||||||||||||||||||||
| final int aOffset = (--top) << 2; | ||||||||||||||||||||
| final int bOffset = (--top) << 2; | ||||||||||||||||||||
| final int mOffset = (--top) << 2; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| mulMod(stack, aOffset, bOffset, mOffset); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| frame.setTopV2(++top); | ||||||||||||||||||||
| return mulModSuccess; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /** | ||||||||||||||||||||
| * Performs EVM MULMOD (modular multiplication) on the three top stack items. | ||||||||||||||||||||
| * Performs EVM MULMOD (modular multiplication) | ||||||||||||||||||||
| * | ||||||||||||||||||||
| * <p>MULMOD: mulmod(a,b,m) = (a * b) mod m | ||||||||||||||||||||
| * <p>MULMOD: mulmod(a, b, m) = (a * b) mod m | ||||||||||||||||||||
| * | ||||||||||||||||||||
| * <p>MULMOD: stack[top-3] = (stack[top-1] * stack[top-2]) mod stack[top-3]. | ||||||||||||||||||||
| * | ||||||||||||||||||||
| * @param stack the flat limb array | ||||||||||||||||||||
| * @param top current stack-top (item count) | ||||||||||||||||||||
| * @param aOffset the stack offset of the first multiplicand | ||||||||||||||||||||
| * @param bOffset the stack offset of the second multiplicand | ||||||||||||||||||||
| * @param mOffset the stack offset of the modulus | ||||||||||||||||||||
|
||||||||||||||||||||
| * @param aOffset the stack offset of the first multiplicand | |
| * @param bOffset the stack offset of the second multiplicand | |
| * @param mOffset the stack offset of the modulus | |
| * @param aOffset the limb-array index of the first multiplicand in {@code stack}; this is an | |
| * offset into the flat 4-limb backing array, not a stack item index | |
| * @param bOffset the limb-array index of the second multiplicand in {@code stack}; this is an | |
| * offset into the flat 4-limb backing array, not a stack item index | |
| * @param mOffset the limb-array index of the modulus in {@code stack}; this is an offset into | |
| * the flat 4-limb backing array, not a stack item index |
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.
nit: I don't get what's the benefit of this method, it's just creating another layer of indirection TBH. Wouldn't it be best to inline all of this logic in the operation directly? it doesn't feel like it increases complexity that much.
Anyway that being said it's more of a coding taste thing so won't block it.
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.
Agree, inline is better cef643e
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -184,6 +184,19 @@ public MessageFrame build() { | |||||||||||||||||||||||||
| return frame; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||
| * Reads a 256-bit word from the V2 stack at the given depth below the current top. | ||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||
| * @param frame the message frame with a V2 stack | ||||||||||||||||||||||||||
| * @param offset 0 for the topmost item, 1 for the item below, etc. | ||||||||||||||||||||||||||
| * @return the value as a {@link UInt256} | ||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| public static UInt256 getV2StackItem(final MessageFrame frame, final int offset) { | ||||||||||||||||||||||||||
| final long[] s = frame.stackDataV2(); | ||||||||||||||||||||||||||
| final int idx = (frame.stackTopV2() - 1 - offset) << 2; | ||||||||||||||||||||||||||
|
Comment on lines
+195
to
+196
|
||||||||||||||||||||||||||
| final long[] s = frame.stackDataV2(); | |
| final int idx = (frame.stackTopV2() - 1 - offset) << 2; | |
| final int stackTop = frame.stackTopV2(); | |
| if (offset < 0 || offset >= stackTop) { | |
| throw new IllegalArgumentException( | |
| "Offset must be between 0 (inclusive) and stack size (exclusive): offset=" | |
| + offset | |
| + ", stackTop=" | |
| + stackTop); | |
| } | |
| final long[] s = frame.stackDataV2(); | |
| final int idx = (stackTop - 1 - offset) << 2; |
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.
Using side-effecting
--top/++topon the same variable makes the stack-height update fragile and harder to verify during future edits (e.g., inserting another read would silently change the resultingtop). Consider using a separate stack pointer variable (e.g.,int sp = frame.stackTopV2();then decrementspfor offsets) and compute the final height explicitly (e.g.,frame.setTopV2(sp + 1)after the three pops), avoiding pre-increment inside the setter.