-
Notifications
You must be signed in to change notification settings - Fork 2
2025 07 12 mul #355
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
Merged
Merged
2025 07 12 mul #355
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ac15b4a
pointers
thedavidmeister f002ef0
wip on mul tests
thedavidmeister 87fd638
mul tests
thedavidmeister 2cd35c6
Merge branch 'main' into 2025-07-12-mul
thedavidmeister 452612a
pointers
thedavidmeister 071c326
Merge branch '2025-07-12-mul' of github.com:rainlanguage/rain.interpr…
thedavidmeister ba130d7
pointers
thedavidmeister File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,80 +1,99 @@ | ||
| // SPDX-License-Identifier: CAL | ||
| pragma solidity ^0.8.18; | ||
|
|
||
| // /// Used for reference implementation so that we have two independent | ||
| // /// upstreams to compare against. | ||
| // import {Math as OZMath} from "openzeppelin-contracts/contracts/utils/math/Math.sol"; | ||
| // import {UD60x18, mul} from "prb-math/UD60x18.sol"; | ||
| // import {OperandV2} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; | ||
| // import {Pointer} from "rain.solmem/lib/LibPointer.sol"; | ||
| // import {InterpreterState} from "../../state/LibInterpreterState.sol"; | ||
| // import {IntegrityCheckState} from "../../integrity/LibIntegrityCheck.sol"; | ||
| // import {LibWillOverflow} from "rain.math.fixedpoint/lib/LibWillOverflow.sol"; | ||
| import {OperandV2} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; | ||
| import {Pointer} from "rain.solmem/lib/LibPointer.sol"; | ||
| import {InterpreterState} from "../../state/LibInterpreterState.sol"; | ||
| import {IntegrityCheckState} from "../../integrity/LibIntegrityCheck.sol"; | ||
| import {LibDecimalFloat, Float} from "rain.math.float/lib/LibDecimalFloat.sol"; | ||
| import {StackItem} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; | ||
| import {LibDecimalFloatImplementation} from "rain.math.float/lib/implementation/LibDecimalFloatImplementation.sol"; | ||
|
|
||
| // /// @title LibOpMul | ||
| // /// @notice Opcode to mul N 18 decimal fixed point values. Errors on overflow. | ||
| // library LibOpMul { | ||
| // function integrity(IntegrityCheckState memory, Operand operand) internal pure returns (uint256, uint256) { | ||
| // // There must be at least two inputs. | ||
| // uint256 inputs = (Operand.unwrap(operand) >> 0x10) & 0x0F; | ||
| // inputs = inputs > 1 ? inputs : 2; | ||
| // return (inputs, 1); | ||
| // } | ||
| /// @title LibOpMul | ||
| /// @notice Opcode to mul N 18 floating point values. | ||
| library LibOpMul { | ||
| function integrity(IntegrityCheckState memory, OperandV2 operand) internal pure returns (uint256, uint256) { | ||
| // There must be at least two inputs. | ||
| uint256 inputs = uint256(OperandV2.unwrap(operand) >> 0x10) & 0x0F; | ||
| inputs = inputs > 1 ? inputs : 2; | ||
| return (inputs, 1); | ||
| } | ||
|
|
||
| // /// mul | ||
| // /// 18 decimal fixed point multiplication with implied overflow checks from | ||
| // /// PRB Math. | ||
| // function run(InterpreterState memory, Operand operand, Pointer stackTop) internal pure returns (Pointer) { | ||
| // uint256 a; | ||
| // uint256 b; | ||
| // assembly ("memory-safe") { | ||
| // a := mload(stackTop) | ||
| // b := mload(add(stackTop, 0x20)) | ||
| // stackTop := add(stackTop, 0x40) | ||
| // } | ||
| // a = UD60x18.unwrap(mul(UD60x18.wrap(a), UD60x18.wrap(b))); | ||
| /// mul | ||
| function run(InterpreterState memory, OperandV2 operand, Pointer stackTop) internal pure returns (Pointer) { | ||
| Float a; | ||
| Float b; | ||
| assembly ("memory-safe") { | ||
| a := mload(stackTop) | ||
| b := mload(add(stackTop, 0x20)) | ||
| stackTop := add(stackTop, 0x40) | ||
| } | ||
| a = LibDecimalFloat.mul(a, b); | ||
|
|
||
| // { | ||
| // uint256 inputs = (Operand.unwrap(operand) >> 0x10) & 0x0F; | ||
| // uint256 i = 2; | ||
| // while (i < inputs) { | ||
| // assembly ("memory-safe") { | ||
| // b := mload(stackTop) | ||
| // stackTop := add(stackTop, 0x20) | ||
| // } | ||
| // a = UD60x18.unwrap(mul(UD60x18.wrap(a), UD60x18.wrap(b))); | ||
| // unchecked { | ||
| // i++; | ||
| // } | ||
| // } | ||
| // } | ||
| // assembly ("memory-safe") { | ||
| // stackTop := sub(stackTop, 0x20) | ||
| // mstore(stackTop, a) | ||
| // } | ||
| // return stackTop; | ||
| // } | ||
| { | ||
| uint256 inputs = uint256(OperandV2.unwrap(operand) >> 0x10) & 0x0F; | ||
| uint256 i = 2; | ||
| while (i < inputs) { | ||
| assembly ("memory-safe") { | ||
| b := mload(stackTop) | ||
| stackTop := add(stackTop, 0x20) | ||
| } | ||
| a = LibDecimalFloat.mul(a, b); | ||
| unchecked { | ||
| i++; | ||
| } | ||
| } | ||
| } | ||
| assembly ("memory-safe") { | ||
| stackTop := sub(stackTop, 0x20) | ||
| mstore(stackTop, a) | ||
| } | ||
| return stackTop; | ||
| } | ||
|
|
||
| // /// Gas intensive reference implementation of multiplication for testing. | ||
| // function referenceFn(InterpreterState memory, Operand, uint256[] memory inputs) | ||
| // internal | ||
| // pure | ||
| // returns (uint256[] memory outputs) | ||
| // { | ||
| // // Unchecked so that when we assert that an overflow error is thrown, we | ||
| // // see the revert from the real function and not the reference function. | ||
| // unchecked { | ||
| // uint256 a = inputs[0]; | ||
| // for (uint256 i = 1; i < inputs.length; i++) { | ||
| // uint256 b = inputs[i]; | ||
| // if (LibWillOverflow.mulDivWillOverflow(a, b, 1e18)) { | ||
| // a = uint256(keccak256(abi.encodePacked("overflow sentinel"))); | ||
| // break; | ||
| // } | ||
| // a = OZMath.mulDiv(a, b, 1e18); | ||
| // } | ||
| // outputs = new uint256[](1); | ||
| // outputs[0] = a; | ||
| // } | ||
| // } | ||
| // } | ||
| /// Gas intensive reference implementation of multiplication for testing. | ||
| function referenceFn(InterpreterState memory, OperandV2, StackItem[] memory inputs) | ||
| internal | ||
| pure | ||
| returns (StackItem[] memory outputs) | ||
| { | ||
| // Unchecked so that when we assert that an overflow error is thrown, we | ||
| // see the revert from the real function and not the reference function. | ||
| unchecked { | ||
| Float a; | ||
| uint256 overflows = 0; | ||
| (int256 signedCoefficientA, int256 exponentA) = | ||
| LibDecimalFloat.unpack(Float.wrap(StackItem.unwrap(inputs[0]))); | ||
| if (int32(exponentA) != exponentA) { | ||
| overflows++; | ||
| } | ||
| for (uint256 i = 1; i < inputs.length; i++) { | ||
| (int256 signedCoefficientB, int256 exponentB) = | ||
| LibDecimalFloat.unpack(Float.wrap(StackItem.unwrap(inputs[i]))); | ||
| if (int32(exponentB) != exponentB) { | ||
| overflows++; | ||
| break; | ||
| } | ||
|
|
||
| (signedCoefficientA, exponentA) = | ||
| LibDecimalFloatImplementation.mul(signedCoefficientA, exponentA, signedCoefficientB, exponentB); | ||
|
|
||
| if (int32(exponentA) != exponentA) { | ||
| overflows++; | ||
| break; | ||
| } | ||
| } | ||
| outputs = new StackItem[](1); | ||
|
|
||
| if (overflows > 0) { | ||
| a = Float.wrap(keccak256(abi.encodePacked("overflow sentinel"))); | ||
| } else { | ||
| a = LibDecimalFloat.packLossless(signedCoefficientA, exponentA); | ||
| } | ||
|
|
||
| outputs[0] = StackItem.wrap(Float.unwrap(a)); | ||
|
|
||
| return outputs; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
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.
🧹 Nitpick (assertive)
Well-structured reference implementation with explicit overflow detection.
The reference function provides a comprehensive test implementation with explicit overflow checking for exponents that exceed int32 bounds. The unchecked block is appropriate for testing scenarios.
Consider using a constant for the overflow sentinel instead of the magic string:
📝 Committable suggestion
🤖 Prompt for AI Agents