diff --git a/.gitmodules b/.gitmodules index 009d1205..1a199148 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,18 +4,9 @@ [submodule "lib/rain.lib.memkv"] path = lib/rain.lib.memkv url = https://github.com/rainprotocol/rain.lib.memkv -[submodule "lib/prb-math"] - path = lib/prb-math - url = https://github.com/PaulRBerg/prb-math [submodule "lib/sol.lib.binmaskflag"] path = lib/sol.lib.binmaskflag url = https://github.com/rainprotocol/sol.lib.binmaskflag -[submodule "lib/rain.math.fixedpoint"] - path = lib/rain.math.fixedpoint - url = https://github.com/rainprotocol/rain.math.fixedpoint -[submodule "lib/rain.chainlink"] - path = lib/rain.chainlink - url = https://github.com/rainprotocol/rain.chainlink [submodule "lib/rain.intorastring"] path = lib/rain.intorastring url = https://github.com/rainlanguage/rain.intorastring diff --git a/foundry.toml b/foundry.toml index bc40f6ec..a3eae5b8 100644 --- a/foundry.toml +++ b/foundry.toml @@ -34,7 +34,6 @@ remappings = [ # The auto remapping wasn't appending /src/ for some reason. "forge-std/=lib/rain.interpreter.interface/lib/forge-std/src/", "rain.metadata/=lib/rain.metadata/src/", - "rain.math.saturating/=lib/rain.math.fixedpoint/lib/rain.math.saturating/src/", "rain.sol.codegen/=lib/rain.sol.codegen/src/", "rain.solmem/=lib/rain.interpreter.interface/lib/rain.solmem/src/", "openzeppelin-contracts/=lib/rain.interpreter.interface/lib/openzeppelin-contracts/", diff --git a/lib/prb-math b/lib/prb-math deleted file mode 160000 index 77fa88ed..00000000 --- a/lib/prb-math +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 77fa88eda4a4a91b3f3e9431df291292c26b6c71 diff --git a/lib/rain.chainlink b/lib/rain.chainlink deleted file mode 160000 index c9844b3f..00000000 --- a/lib/rain.chainlink +++ /dev/null @@ -1 +0,0 @@ -Subproject commit c9844b3f49432d809f506fb104fe9ea6c7be7031 diff --git a/lib/rain.interpreter.interface b/lib/rain.interpreter.interface index 20da616b..1b713f5d 160000 --- a/lib/rain.interpreter.interface +++ b/lib/rain.interpreter.interface @@ -1 +1 @@ -Subproject commit 20da616b981d45578cd1c56fca234f19b3db9f5b +Subproject commit 1b713f5d00b8b17762ecc8a57438b50fbb8bf702 diff --git a/lib/rain.math.fixedpoint b/lib/rain.math.fixedpoint deleted file mode 160000 index f0d74ed3..00000000 --- a/lib/rain.math.fixedpoint +++ /dev/null @@ -1 +0,0 @@ -Subproject commit f0d74ed3ef41f2a71fc43c99e81c3918b64e874b diff --git a/lib/rain.math.float b/lib/rain.math.float index 455a0f44..9b63eb73 160000 --- a/lib/rain.math.float +++ b/lib/rain.math.float @@ -1 +1 @@ -Subproject commit 455a0f4472c7084b751ae062f2a243e6e18cb958 +Subproject commit 9b63eb73e147ddee00465e1147c8d6fbaa2d2f6c diff --git a/slither.config.json b/slither.config.json index 2e39af90..c062e765 100644 --- a/slither.config.json +++ b/slither.config.json @@ -1,4 +1,4 @@ { "detectors_to_exclude": "assembly-usage,solc-version,different-pragma-directives-are-used,unused-imports", - "filter_paths": "lib/openzeppelin-contracts,lib/prb-math,test/" + "filter_paths": "lib/rain.math.float,lib/openzeppelin-contracts,lib/prb-math,lib/rain.interpreter.interface,test/" } diff --git a/src/abstract/BaseRainterpreterSubParserNPE2.sol b/src/abstract/BaseRainterpreterSubParserNPE2.sol index b00d171f..f8a4c2b3 100644 --- a/src/abstract/BaseRainterpreterSubParserNPE2.sol +++ b/src/abstract/BaseRainterpreterSubParserNPE2.sol @@ -4,9 +4,9 @@ pragma solidity =0.8.25; import {ERC165} from "openzeppelin-contracts/contracts/utils/introspection/ERC165.sol"; import {LibBytes, Pointer} from "rain.solmem/lib/LibBytes.sol"; -import {ISubParserV3, COMPATIBILITY_V4, AuthoringMetaV2} from "rain.interpreter.interface/interface/ISubParserV3.sol"; +import {ISubParserV3, AuthoringMetaV2} from "rain.interpreter.interface/interface/ISubParserV3.sol"; import {IncompatibleSubParser} from "../error/ErrSubParse.sol"; -import {LibSubParse, ParseState} from "../lib/parse/LibSubParse.sol"; +import {LibSubParse, ParseState, CURRENT_COMPATIBILITY} from "../lib/parse/LibSubParse.sol"; import {CMASK_RHS_WORD_TAIL} from "../lib/parse/LibParseCMask.sol"; import {LibParse, Operand} from "../lib/parse/LibParse.sol"; import {LibParseMeta} from "rain.interpreter.interface/lib/parse/LibParseMeta.sol"; @@ -118,7 +118,7 @@ abstract contract BaseRainterpreterSubParserNPE2 is /// same commit the abstract sub parser is pulled from. //slither-disable-next-line dead-code function subParserCompatibility() internal pure virtual returns (bytes32) { - return COMPATIBILITY_V4; + return CURRENT_COMPATIBILITY; } /// Overrideable function to allow implementations to define their diff --git a/src/concrete/extern/RainterpreterReferenceExternNPE2.sol b/src/concrete/extern/RainterpreterReferenceExternNPE2.sol index 097bc5fa..b617fd80 100644 --- a/src/concrete/extern/RainterpreterReferenceExternNPE2.sol +++ b/src/concrete/extern/RainterpreterReferenceExternNPE2.sol @@ -12,11 +12,10 @@ import { } from "../../abstract/BaseRainterpreterSubParserNPE2.sol"; import {LibExtern, EncodedExternDispatch} from "../../lib/extern/LibExtern.sol"; import {IInterpreterExternV3} from "rain.interpreter.interface/interface/IInterpreterExternV3.sol"; -import {LibSubParse} from "../../lib/parse/LibSubParse.sol"; +import {LibSubParse, CURRENT_COMPATIBILITY} from "../../lib/parse/LibSubParse.sol"; import {LibParseState, ParseState} from "../../lib/parse/LibParseState.sol"; import {LibParseOperand} from "../../lib/parse/LibParseOperand.sol"; import {LibParseLiteral} from "../../lib/parse/literal/LibParseLiteral.sol"; -import {COMPATIBILITY_V4} from "rain.interpreter.interface/interface/ISubParserV3.sol"; import {LibExternOpIntIncNPE2, OP_INDEX_INCREMENT} from "../../lib/extern/reference/op/LibExternOpIntIncNPE2.sol"; import {LibExternOpStackOperandNPE2} from "../../lib/extern/reference/op/LibExternOpStackOperandNPE2.sol"; import {LibExternOpContextSenderNPE2} from "../../lib/extern/reference/op/LibExternOpContextSenderNPE2.sol"; @@ -25,7 +24,6 @@ import {LibExternOpContextCallingContractNPE2} from import {LibExternOpContextRainlenNPE2} from "../../lib/extern/reference/op/LibExternOpContextRainlenNPE2.sol"; import {LibParseLiteralRepeat} from "../../lib/extern/reference/literal/LibParseLiteralRepeat.sol"; import {LibParseLiteralDecimal} from "../../lib/parse/literal/LibParseLiteralDecimal.sol"; -import {LibFixedPointDecimalScale} from "rain.math.fixedpoint/lib/LibFixedPointDecimalScale.sol"; import { DESCRIBED_BY_META_HASH, PARSE_META as SUB_PARSER_PARSE_META, @@ -36,6 +34,7 @@ import { INTEGRITY_FUNCTION_POINTERS, OPCODE_FUNCTION_POINTERS } from "../../generated/RainterpreterReferenceExternNPE2.pointers.sol"; +import {LibDecimalFloat} from "rain.math.float/src/lib/LibDecimalFloat.sol"; /// @dev The number of subparser functions available to the parser. This is NOT /// 1:1 with the number of opcodes provided by the extern component of this @@ -66,7 +65,7 @@ bytes32 constant SUB_PARSER_LITERAL_REPEAT_KEYWORD_MASK = uint256 constant SUB_PARSER_LITERAL_REPEAT_INDEX = 0; /// @dev Thrown when the repeat literal parser is not a single digit. -error InvalidRepeatCount(uint256 value); +error InvalidRepeatCount(); /// @dev Number of opcode function pointers available to run at eval time. uint256 constant OPCODE_FUNCTION_POINTERS_LENGTH = 1; @@ -186,7 +185,7 @@ contract RainterpreterReferenceExternNPE2 is BaseRainterpreterSubParserNPE2, Bas /// known constant value, which should allow the compiler to optimise the /// entire function call away. function subParserCompatibility() internal pure override returns (bytes32) { - return COMPATIBILITY_V4; + return CURRENT_COMPATIBILITY; } /// Overrides the base function pointers for opcodes. Simply returns the @@ -247,17 +246,17 @@ contract RainterpreterReferenceExternNPE2 is BaseRainterpreterSubParserNPE2, Bas ParseState memory state = LibParseState.newState("", "", "", ""); // If we have a match on the keyword then the next chars MUST // be a decimal, otherwise it's an error. - uint256 value; - (cursor, value) = LibParseLiteralDecimal.parseDecimal( + int256 signedCoefficient; + int256 exponent; + (cursor, signedCoefficient, exponent) = LibParseLiteralDecimal.parseDecimalFloat( state, cursor + SUB_PARSER_LITERAL_REPEAT_KEYWORD_BYTES_LENGTH, end ); - value = LibFixedPointDecimalScale.scaleToIntegerLossless(value); // We can only repeat a single digit. - if (value > 9) { - revert InvalidRepeatCount(value); + if (LibDecimalFloat.gt(signedCoefficient, exponent, 9, 0)) { + revert InvalidRepeatCount(); } - return (true, SUB_PARSER_LITERAL_REPEAT_INDEX, value); + return (true, SUB_PARSER_LITERAL_REPEAT_INDEX, LibDecimalFloat.pack(signedCoefficient, exponent)); } else { return (false, 0, 0); } diff --git a/src/error/ErrParse.sol b/src/error/ErrParse.sol index 76b7e525..d6724c63 100644 --- a/src/error/ErrParse.sol +++ b/src/error/ErrParse.sol @@ -148,3 +148,6 @@ error BadSubParserResult(bytes bytecode); /// Thrown when there are more than 16 inputs or outputs for a given opcode. error OpcodeIOOverflow(uint256 offset); + +/// Thrown when an operand value is larger than the maximum allowed. +error OperandOverflow(); diff --git a/src/generated/RainterpreterNPE2.pointers.sol b/src/generated/RainterpreterNPE2.pointers.sol index a370d024..6621bead 100644 --- a/src/generated/RainterpreterNPE2.pointers.sol +++ b/src/generated/RainterpreterNPE2.pointers.sol @@ -9,10 +9,10 @@ pragma solidity =0.8.25; /// @dev Hash of the known bytecode. -bytes32 constant BYTECODE_HASH = bytes32(0xf8382556fc29d39e6e42d3c8a93f6004c731efce7e61a4bbb7eac4a772f05151); +bytes32 constant BYTECODE_HASH = bytes32(0xb2bb360cac3a5504625d94621550913d5024da27fc6dbb11140efcc425d9cf7b); /// @dev The function pointers known to the interpreter for dynamic dispatch. /// By setting these as a constant they can be inlined into the interpreter /// and loaded at eval time for very low gas (~100) due to the compiler /// optimising it to a single `codecopy` to build the in memory bytes array. -bytes constant OPCODE_FUNCTION_POINTERS = hex"06b407040746091209f90a0b0a1d0a400a820ad40ae50af60b980bd50c840d080d570e4d"; +bytes constant OPCODE_FUNCTION_POINTERS = hex"06b407040746091209f90a0b0a1d0a360a780aca0adb0aec0b8e0bcb0c7a0cfe0d4d0e43"; diff --git a/src/generated/RainterpreterParserNPE2.pointers.sol b/src/generated/RainterpreterParserNPE2.pointers.sol index 55155c0b..4ca46971 100644 --- a/src/generated/RainterpreterParserNPE2.pointers.sol +++ b/src/generated/RainterpreterParserNPE2.pointers.sol @@ -9,7 +9,7 @@ pragma solidity =0.8.25; /// @dev Hash of the known bytecode. -bytes32 constant BYTECODE_HASH = bytes32(0x2b76af53c41e816a07cc5750e27098896d9d7ed695b884f2ff0b39621dfdb8c3); +bytes32 constant BYTECODE_HASH = bytes32(0x6ceb7c73c69eaf63e3a95805892de0f17ff921c8cc8ae7694745fd7f832ecad5); /// @dev The parse meta that is used to lookup word definitions. /// The structure of the parse meta is: @@ -38,11 +38,11 @@ uint8 constant PARSE_META_BUILD_DEPTH = 2; /// These positional indexes all map to the same indexes looked up in the parse /// meta. bytes constant OPERAND_HANDLER_FUNCTION_POINTERS = - hex"173517351735179a181318131813179a179a173517351735181318131813181318131813"; + hex"1450145014501515160c160c160c15151515145014501450160c160c160c160c160c160c"; /// @dev Every two bytes is a function pointer for a literal parser. /// Literal dispatches are determined by the first byte(s) of the literal /// rather than a full word lookup, and are done with simple conditional /// jumps as the possibilities are limited compared to the number of words we /// have. -bytes constant LITERAL_PARSER_FUNCTION_POINTERS = hex"0dab1073147a1554"; +bytes constant LITERAL_PARSER_FUNCTION_POINTERS = hex"0dab10731185125f"; diff --git a/src/generated/RainterpreterReferenceExternNPE2.pointers.sol b/src/generated/RainterpreterReferenceExternNPE2.pointers.sol index 99fd0d65..fa0d9c0d 100644 --- a/src/generated/RainterpreterReferenceExternNPE2.pointers.sol +++ b/src/generated/RainterpreterReferenceExternNPE2.pointers.sol @@ -9,7 +9,7 @@ pragma solidity =0.8.25; /// @dev Hash of the known bytecode. -bytes32 constant BYTECODE_HASH = bytes32(0x4247a5ea9bd8abcbe989b8ae09f2b9b29cafa521add389fc7b6f2b1ca3c49ecc); +bytes32 constant BYTECODE_HASH = bytes32(0xf8597dadf69ee1df0975d01a6e9e43563ff5966c950ed23c981086f46b4d7a8c); /// @dev The hash of the meta that describes the contract. bytes32 constant DESCRIBED_BY_META_HASH = bytes32(0xadf71693c6ecf3fd560904bc46973d1b6e651440d15366673f9b3984749e7c16); @@ -47,17 +47,17 @@ bytes constant SUB_PARSER_WORD_PARSERS = hex"0775079707a607b607c7"; /// @dev Every two bytes is a function pointer for an operand handler. /// These positional indexes all map to the same indexes looked up in the parse /// meta. -bytes constant OPERAND_HANDLER_FUNCTION_POINTERS = hex"0a270a6c0a270a270a27"; +bytes constant OPERAND_HANDLER_FUNCTION_POINTERS = hex"0abf0b040abf0abf0abf"; /// @dev Every two bytes is a function pointer for a literal parser. /// Literal dispatches are determined by the first byte(s) of the literal /// rather than a full word lookup, and are done with simple conditional /// jumps as the possibilities are limited compared to the number of words we /// have. -bytes constant LITERAL_PARSER_FUNCTION_POINTERS = hex"09f8"; +bytes constant LITERAL_PARSER_FUNCTION_POINTERS = hex"0a8e"; /// @dev The function pointers for the integrity check fns. -bytes constant INTEGRITY_FUNCTION_POINTERS = hex"08ad"; +bytes constant INTEGRITY_FUNCTION_POINTERS = hex"0935"; /// @dev The function pointers known to the interpreter for dynamic dispatch. /// By setting these as a constant they can be inlined into the interpreter diff --git a/src/lib/extern/reference/op/LibExternOpIntIncNPE2.sol b/src/lib/extern/reference/op/LibExternOpIntIncNPE2.sol index 2cf856c6..043455fc 100644 --- a/src/lib/extern/reference/op/LibExternOpIntIncNPE2.sol +++ b/src/lib/extern/reference/op/LibExternOpIntIncNPE2.sol @@ -4,6 +4,7 @@ pragma solidity ^0.8.25; import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; import {LibSubParse} from "../../../parse/LibSubParse.sol"; import {IInterpreterExternV3} from "rain.interpreter.interface/interface/IInterpreterExternV3.sol"; +import {LibDecimalFloat} from "rain.math.float/src/lib/LibDecimalFloat.sol"; /// @dev Opcode index of the extern increment opcode. Needs to be manually kept /// in sync with the extern opcode function pointers. Definitely write tests for @@ -20,7 +21,9 @@ library LibExternOpIntIncNPE2 { //slither-disable-next-line dead-code function run(Operand, uint256[] memory inputs) internal pure returns (uint256[] memory) { for (uint256 i = 0; i < inputs.length; i++) { - ++inputs[i]; + (int256 signedCoefficient, int256 exponent) = LibDecimalFloat.unpack(inputs[i]); + (signedCoefficient, exponent) = LibDecimalFloat.add(signedCoefficient, exponent, 1e37, -37); + inputs[i] = LibDecimalFloat.pack(signedCoefficient, exponent); } return inputs; } diff --git a/src/lib/op/LibAllStandardOpsNP.sol b/src/lib/op/LibAllStandardOpsNP.sol index b35775d6..725d6cd3 100644 --- a/src/lib/op/LibAllStandardOpsNP.sol +++ b/src/lib/op/LibAllStandardOpsNP.sol @@ -32,9 +32,9 @@ import {LibOpUint256ERC20Allowance} from "./erc20/uint256/LibOpUint256ERC20Allow import {LibOpUint256ERC20BalanceOf} from "./erc20/uint256/LibOpUint256ERC20BalanceOf.sol"; import {LibOpUint256ERC20TotalSupply} from "./erc20/uint256/LibOpUint256ERC20TotalSupply.sol"; -import {LibOpERC20Allowance} from "./erc20/LibOpERC20Allowance.sol"; -import {LibOpERC20BalanceOf} from "./erc20/LibOpERC20BalanceOf.sol"; -import {LibOpERC20TotalSupply} from "./erc20/LibOpERC20TotalSupply.sol"; +// import {LibOpERC20Allowance} from "./erc20/LibOpERC20Allowance.sol"; +// import {LibOpERC20BalanceOf} from "./erc20/LibOpERC20BalanceOf.sol"; +// import {LibOpERC20TotalSupply} from "./erc20/LibOpERC20TotalSupply.sol"; import {LibOpUint256ERC721BalanceOf} from "./erc721/uint256/LibOpUint256ERC721BalanceOf.sol"; import {LibOpERC721OwnerOf} from "./erc721/LibOpERC721OwnerOf.sol"; @@ -58,40 +58,40 @@ import {LibOpIsZeroNP} from "./logic/LibOpIsZeroNP.sol"; import {LibOpLessThanNP} from "./logic/LibOpLessThanNP.sol"; import {LibOpLessThanOrEqualToNP} from "./logic/LibOpLessThanOrEqualToNP.sol"; -import {LibOpExponentialGrowth} from "./math/growth/LibOpExponentialGrowth.sol"; -import {LibOpLinearGrowth} from "./math/growth/LibOpLinearGrowth.sol"; +// import {LibOpExponentialGrowth} from "./math/growth/LibOpExponentialGrowth.sol"; +// import {LibOpLinearGrowth} from "./math/growth/LibOpLinearGrowth.sol"; import {LibOpUint256Div} from "./math/uint256/LibOpUint256Div.sol"; import {LibOpUint256Mul} from "./math/uint256/LibOpUint256Mul.sol"; import {LibOpUint256Pow} from "./math/uint256/LibOpUint256Pow.sol"; import {LibOpAdd} from "./math/LibOpAdd.sol"; -import {LibOpAvg} from "./math/LibOpAvg.sol"; -import {LibOpCeil} from "./math/LibOpCeil.sol"; -import {LibOpMul} from "./math/LibOpMul.sol"; -import {LibOpDiv} from "./math/LibOpDiv.sol"; -import {LibOpE} from "./math/LibOpE.sol"; -import {LibOpExp} from "./math/LibOpExp.sol"; -import {LibOpExp2} from "./math/LibOpExp2.sol"; -import {LibOpFloor} from "./math/LibOpFloor.sol"; -import {LibOpFrac} from "./math/LibOpFrac.sol"; -import {LibOpGm} from "./math/LibOpGm.sol"; -import {LibOpHeadroom} from "./math/LibOpHeadroom.sol"; -import {LibOpInv} from "./math/LibOpInv.sol"; -import {LibOpLn} from "./math/LibOpLn.sol"; -import {LibOpLog10} from "./math/LibOpLog10.sol"; +// import {LibOpAvg} from "./math/LibOpAvg.sol"; +// import {LibOpCeil} from "./math/LibOpCeil.sol"; +// import {LibOpMul} from "./math/LibOpMul.sol"; +// import {LibOpDiv} from "./math/LibOpDiv.sol"; +// import {LibOpE} from "./math/LibOpE.sol"; +// import {LibOpExp} from "./math/LibOpExp.sol"; +// import {LibOpExp2} from "./math/LibOpExp2.sol"; +// import {LibOpFloor} from "./math/LibOpFloor.sol"; +// import {LibOpFrac} from "./math/LibOpFrac.sol"; +// import {LibOpGm} from "./math/LibOpGm.sol"; +// import {LibOpHeadroom} from "./math/LibOpHeadroom.sol"; +// import {LibOpInv} from "./math/LibOpInv.sol"; +// import {LibOpLn} from "./math/LibOpLn.sol"; +// import {LibOpLog10} from "./math/LibOpLog10.sol"; import {LibOpMax} from "./math/LibOpMax.sol"; import {LibOpMin} from "./math/LibOpMin.sol"; import {LibOpMod} from "./math/LibOpMod.sol"; -import {LibOpLog2} from "./math/LibOpLog2.sol"; -import {LibOpPow} from "./math/LibOpPow.sol"; -import {LibOpScale18Dynamic} from "./math/LibOpScale18Dynamic.sol"; -import {LibOpScale18} from "./math/LibOpScale18.sol"; -import {LibOpScaleNDynamic} from "./math/LibOpScaleNDynamic.sol"; -import {LibOpScaleN} from "./math/LibOpScaleN.sol"; -import {LibOpSnapToUnit} from "./math/LibOpSnapToUnit.sol"; -import {LibOpSqrt} from "./math/LibOpSqrt.sol"; -import {LibOpSub} from "./math/LibOpSub.sol"; +// import {LibOpLog2} from "./math/LibOpLog2.sol"; +// import {LibOpPow} from "./math/LibOpPow.sol"; +// import {LibOpScale18Dynamic} from "./math/LibOpScale18Dynamic.sol"; +// import {LibOpScale18} from "./math/LibOpScale18.sol"; +// import {LibOpScaleNDynamic} from "./math/LibOpScaleNDynamic.sol"; +// import {LibOpScaleN} from "./math/LibOpScaleN.sol"; +// import {LibOpSnapToUnit} from "./math/LibOpSnapToUnit.sol"; +// import {LibOpSqrt} from "./math/LibOpSqrt.sol"; +// import {LibOpSub} from "./math/LibOpSub.sol"; import {LibOpGetNP} from "./store/LibOpGetNP.sol"; import {LibOpSetNP} from "./store/LibOpSetNP.sol"; @@ -329,7 +329,7 @@ library LibAllStandardOpsNP { memory pointersFixed = [ lengthPointer, LibParseLiteralHex.parseHex, - LibParseLiteralDecimal.parseDecimal, + LibParseLiteralDecimal.parseDecimalFloatPacked, LibParseLiteralString.parseString, LibParseLiteralSubParseable.parseSubParseable ]; diff --git a/src/lib/op/bitwise/LibOpCtPopNP.sol b/src/lib/op/bitwise/LibOpCtPopNP.sol index 9797d360..8261423d 100644 --- a/src/lib/op/bitwise/LibOpCtPopNP.sol +++ b/src/lib/op/bitwise/LibOpCtPopNP.sol @@ -6,7 +6,6 @@ import {Operand} from "rain.interpreter.interface/interface/unstable/IInterprete import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; import {LibCtPop} from "rain.math.binary/lib/LibCtPop.sol"; -import {FIXED_POINT_ONE} from "rain.math.fixedpoint/lib/FixedPointDecimalConstants.sol"; /// @title LibOpCtPopNP /// @notice An opcode that counts the number of bits set in a word. This is @@ -29,7 +28,7 @@ library LibOpCtPopNP { value := mload(stackTop) } unchecked { - value = LibCtPop.ctpop(value) * FIXED_POINT_ONE; + value = LibCtPop.ctpop(value); } assembly ("memory-safe") { mstore(stackTop, value) @@ -43,7 +42,7 @@ library LibOpCtPopNP { pure returns (uint256[] memory) { - inputs[0] = LibCtPop.ctpopSlow(inputs[0]) * FIXED_POINT_ONE; + inputs[0] = LibCtPop.ctpopSlow(inputs[0]); return inputs; } } diff --git a/src/lib/op/erc20/LibOpERC20Allowance.sol b/src/lib/op/erc20/LibOpERC20Allowance.sol index 5400af1c..4917e06f 100644 --- a/src/lib/op/erc20/LibOpERC20Allowance.sol +++ b/src/lib/op/erc20/LibOpERC20Allowance.sol @@ -1,69 +1,68 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {IERC20} from "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {FLAG_SATURATE, LibFixedPointDecimalScale} from "rain.math.fixedpoint/lib/LibFixedPointDecimalScale.sol"; -import {IERC20Metadata} from "openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol"; +// import {IERC20} from "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {IERC20Metadata} from "openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol"; -/// @title LibOpERC20Allowance -/// @notice Opcode for getting the current erc20 allowance of an account. -library LibOpERC20Allowance { - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - // Always 3 inputs, the token, the owner and the spender. - // Always 1 output, the allowance. - return (3, 1); - } +// /// @title LibOpERC20Allowance +// /// @notice Opcode for getting the current erc20 allowance of an account. +// library LibOpERC20Allowance { +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// // Always 3 inputs, the token, the owner and the spender. +// // Always 1 output, the allowance. +// return (3, 1); +// } - function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal view returns (Pointer) { - uint256 token; - uint256 owner; - uint256 spender; - assembly ("memory-safe") { - token := mload(stackTop) - owner := mload(add(stackTop, 0x20)) - stackTop := add(stackTop, 0x40) - spender := mload(stackTop) - } - uint256 tokenAllowance = - IERC20(address(uint160(token))).allowance(address(uint160(owner)), address(uint160(spender))); +// function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal view returns (Pointer) { +// uint256 token; +// uint256 owner; +// uint256 spender; +// assembly ("memory-safe") { +// token := mload(stackTop) +// owner := mload(add(stackTop, 0x20)) +// stackTop := add(stackTop, 0x40) +// spender := mload(stackTop) +// } +// uint256 tokenAllowance = +// IERC20(address(uint160(token))).allowance(address(uint160(owner)), address(uint160(spender))); - // This can fail as `decimals` is an OPTIONAL part of the ERC20 standard. - uint256 tokenDecimals = IERC20Metadata(address(uint160(token))).decimals(); - tokenAllowance = LibFixedPointDecimalScale.scale18( - tokenAllowance, - tokenDecimals, - // Saturate scaling as "infinite approve" is a fairly common pattern - // so erroring would make a lot of contracts unusable in practise. - // Rounding down is the default. - FLAG_SATURATE - ); +// // This can fail as `decimals` is an OPTIONAL part of the ERC20 standard. +// uint256 tokenDecimals = IERC20Metadata(address(uint160(token))).decimals(); +// tokenAllowance = LibFixedPointDecimalScale.scale18( +// tokenAllowance, +// tokenDecimals, +// // Saturate scaling as "infinite approve" is a fairly common pattern +// // so erroring would make a lot of contracts unusable in practise. +// // Rounding down is the default. +// FLAG_SATURATE +// ); - assembly ("memory-safe") { - mstore(stackTop, tokenAllowance) - } - return stackTop; - } +// assembly ("memory-safe") { +// mstore(stackTop, tokenAllowance) +// } +// return stackTop; +// } - function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) - internal - view - returns (uint256[] memory) - { - uint256 token = inputs[0]; - uint256 owner = inputs[1]; - uint256 spender = inputs[2]; +// function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) +// internal +// view +// returns (uint256[] memory) +// { +// uint256 token = inputs[0]; +// uint256 owner = inputs[1]; +// uint256 spender = inputs[2]; - uint256 tokenDecimals = IERC20Metadata(address(uint160(token))).decimals(); - uint256 tokenAllowance = - IERC20(address(uint160(token))).allowance(address(uint160(owner)), address(uint160(spender))); - tokenAllowance = LibFixedPointDecimalScale.scale18(tokenAllowance, tokenDecimals, FLAG_SATURATE); +// uint256 tokenDecimals = IERC20Metadata(address(uint160(token))).decimals(); +// uint256 tokenAllowance = +// IERC20(address(uint160(token))).allowance(address(uint160(owner)), address(uint160(spender))); +// tokenAllowance = LibFixedPointDecimalScale.scale18(tokenAllowance, tokenDecimals, FLAG_SATURATE); - uint256[] memory outputs = new uint256[](1); - outputs[0] = tokenAllowance; - return outputs; - } -} +// uint256[] memory outputs = new uint256[](1); +// outputs[0] = tokenAllowance; +// return outputs; +// } +// } diff --git a/src/lib/op/erc20/LibOpERC20BalanceOf.sol b/src/lib/op/erc20/LibOpERC20BalanceOf.sol index be1ab49c..6fa02dfb 100644 --- a/src/lib/op/erc20/LibOpERC20BalanceOf.sol +++ b/src/lib/op/erc20/LibOpERC20BalanceOf.sol @@ -1,70 +1,69 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {IERC20} from "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {LibFixedPointDecimalScale} from "rain.math.fixedpoint/lib/LibFixedPointDecimalScale.sol"; -import {IERC20Metadata} from "openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol"; +// import {IERC20} from "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {IERC20Metadata} from "openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol"; -/// @title LibOpERC20BalanceOf -/// @notice Opcode for getting the current erc20 balance of an account. -library LibOpERC20BalanceOf { - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - // Always 2 inputs, the token and the account. - // Always 1 output, the balance. - return (2, 1); - } +// /// @title LibOpERC20BalanceOf +// /// @notice Opcode for getting the current erc20 balance of an account. +// library LibOpERC20BalanceOf { +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// // Always 2 inputs, the token and the account. +// // Always 1 output, the balance. +// return (2, 1); +// } - function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal view returns (Pointer) { - uint256 token; - uint256 account; - assembly ("memory-safe") { - token := mload(stackTop) - stackTop := add(stackTop, 0x20) - account := mload(stackTop) - } - uint256 tokenBalance = IERC20(address(uint160(token))).balanceOf(address(uint160(account))); +// function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal view returns (Pointer) { +// uint256 token; +// uint256 account; +// assembly ("memory-safe") { +// token := mload(stackTop) +// stackTop := add(stackTop, 0x20) +// account := mload(stackTop) +// } +// uint256 tokenBalance = IERC20(address(uint160(token))).balanceOf(address(uint160(account))); - // This can fail as `decimals` is an OPTIONAL part of the ERC20 standard. - uint256 tokenDecimals = IERC20Metadata(address(uint160(token))).decimals(); - tokenBalance = LibFixedPointDecimalScale.scale18( - tokenBalance, - tokenDecimals, - // Error on overflow as balance is a critical value. - // Rounding down is the default. - 0 - ); +// // This can fail as `decimals` is an OPTIONAL part of the ERC20 standard. +// uint256 tokenDecimals = IERC20Metadata(address(uint160(token))).decimals(); +// tokenBalance = LibFixedPointDecimalScale.scale18( +// tokenBalance, +// tokenDecimals, +// // Error on overflow as balance is a critical value. +// // Rounding down is the default. +// 0 +// ); - assembly ("memory-safe") { - mstore(stackTop, tokenBalance) - } - return stackTop; - } +// assembly ("memory-safe") { +// mstore(stackTop, tokenBalance) +// } +// return stackTop; +// } - function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) - internal - view - returns (uint256[] memory) - { - uint256 token = inputs[0]; - uint256 account = inputs[1]; +// function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) +// internal +// view +// returns (uint256[] memory) +// { +// uint256 token = inputs[0]; +// uint256 account = inputs[1]; - uint256 tokenBalance = IERC20(address(uint160(token))).balanceOf(address(uint160(account))); +// uint256 tokenBalance = IERC20(address(uint160(token))).balanceOf(address(uint160(account))); - uint256 tokenDecimals = IERC20Metadata(address(uint160(token))).decimals(); - tokenBalance = LibFixedPointDecimalScale.scale18( - tokenBalance, - tokenDecimals, - // Error on overflow as balance is a critical value. - // Rounding down is the default. - 0 - ); +// uint256 tokenDecimals = IERC20Metadata(address(uint160(token))).decimals(); +// tokenBalance = LibFixedPointDecimalScale.scale18( +// tokenBalance, +// tokenDecimals, +// // Error on overflow as balance is a critical value. +// // Rounding down is the default. +// 0 +// ); - uint256[] memory outputs = new uint256[](1); - outputs[0] = tokenBalance; - return outputs; - } -} +// uint256[] memory outputs = new uint256[](1); +// outputs[0] = tokenBalance; +// return outputs; +// } +// } diff --git a/src/lib/op/erc20/LibOpERC20TotalSupply.sol b/src/lib/op/erc20/LibOpERC20TotalSupply.sol index 957e630e..5a0cd4c4 100644 --- a/src/lib/op/erc20/LibOpERC20TotalSupply.sol +++ b/src/lib/op/erc20/LibOpERC20TotalSupply.sol @@ -1,64 +1,63 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.25; - -import {IERC20} from "openzeppelin-contracts/contracts/interfaces/IERC20.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {LibFixedPointDecimalScale} from "rain.math.fixedpoint/lib/LibFixedPointDecimalScale.sol"; -import {IERC20Metadata} from "openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol"; - -/// @title LibOpERC20TotalSupply -/// @notice Opcode for ERC20 `totalSupply`. -library LibOpERC20TotalSupply { - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - // Always 1 input, the contract. - // Always 1 output, the total supply. - return (1, 1); - } - - function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal view returns (Pointer) { - uint256 token; - assembly ("memory-safe") { - token := mload(stackTop) - } - uint256 totalSupply = IERC20(address(uint160(token))).totalSupply(); - - uint256 tokenDecimals = IERC20Metadata(address(uint160(token))).decimals(); - totalSupply = LibFixedPointDecimalScale.scale18( - totalSupply, - tokenDecimals, - // Error on overflow as total supply is a critical value. - // Rounding down is the default. - 0 - ); - - assembly ("memory-safe") { - mstore(stackTop, totalSupply) - } - return stackTop; - } - - function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) - internal - view - returns (uint256[] memory) - { - uint256 account = inputs[0]; - uint256 totalSupply = IERC20(address(uint160(account))).totalSupply(); - - uint256 tokenDecimals = IERC20Metadata(address(uint160(account))).decimals(); - totalSupply = LibFixedPointDecimalScale.scale18( - totalSupply, - tokenDecimals, - // Error on overflow as total supply is a critical value. - // Rounding down is the default. - 0 - ); - - uint256[] memory outputs = new uint256[](1); - outputs[0] = totalSupply; - return outputs; - } -} +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.25; + +// import {IERC20} from "openzeppelin-contracts/contracts/interfaces/IERC20.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {IERC20Metadata} from "openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol"; + +// /// @title LibOpERC20TotalSupply +// /// @notice Opcode for ERC20 `totalSupply`. +// library LibOpERC20TotalSupply { +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// // Always 1 input, the contract. +// // Always 1 output, the total supply. +// return (1, 1); +// } + +// function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal view returns (Pointer) { +// uint256 token; +// assembly ("memory-safe") { +// token := mload(stackTop) +// } +// uint256 totalSupply = IERC20(address(uint160(token))).totalSupply(); + +// uint256 tokenDecimals = IERC20Metadata(address(uint160(token))).decimals(); +// totalSupply = LibFixedPointDecimalScale.scale18( +// totalSupply, +// tokenDecimals, +// // Error on overflow as total supply is a critical value. +// // Rounding down is the default. +// 0 +// ); + +// assembly ("memory-safe") { +// mstore(stackTop, totalSupply) +// } +// return stackTop; +// } + +// function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) +// internal +// view +// returns (uint256[] memory) +// { +// uint256 account = inputs[0]; +// uint256 totalSupply = IERC20(address(uint160(account))).totalSupply(); + +// uint256 tokenDecimals = IERC20Metadata(address(uint160(account))).decimals(); +// totalSupply = LibFixedPointDecimalScale.scale18( +// totalSupply, +// tokenDecimals, +// // Error on overflow as total supply is a critical value. +// // Rounding down is the default. +// 0 +// ); + +// uint256[] memory outputs = new uint256[](1); +// outputs[0] = totalSupply; +// return outputs; +// } +// } diff --git a/src/lib/op/evm/LibOpBlockNumberNP.sol b/src/lib/op/evm/LibOpBlockNumberNP.sol index a6b02616..4503f22f 100644 --- a/src/lib/op/evm/LibOpBlockNumberNP.sol +++ b/src/lib/op/evm/LibOpBlockNumberNP.sol @@ -5,7 +5,6 @@ import {Pointer} from "rain.solmem/lib/LibPointer.sol"; import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -import {FIXED_POINT_ONE} from "rain.math.fixedpoint/lib/FixedPointDecimalConstants.sol"; /// @title LibOpBlockNumberNP /// Implementation of the EVM `BLOCKNUMBER` opcode as a standard Rainlang opcode. @@ -15,10 +14,9 @@ library LibOpBlockNumberNP { } function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal view returns (Pointer) { - uint256 decimalOne = FIXED_POINT_ONE; assembly ("memory-safe") { stackTop := sub(stackTop, 0x20) - mstore(stackTop, mul(number(), decimalOne)) + mstore(stackTop, number()) } return stackTop; } @@ -29,7 +27,7 @@ library LibOpBlockNumberNP { returns (uint256[] memory) { uint256[] memory outputs = new uint256[](1); - outputs[0] = block.number * FIXED_POINT_ONE; + outputs[0] = block.number; return outputs; } } diff --git a/src/lib/op/evm/LibOpChainIdNP.sol b/src/lib/op/evm/LibOpChainIdNP.sol index cb97e5bf..28e7c0ce 100644 --- a/src/lib/op/evm/LibOpChainIdNP.sol +++ b/src/lib/op/evm/LibOpChainIdNP.sol @@ -5,7 +5,6 @@ import {Pointer} from "rain.solmem/lib/LibPointer.sol"; import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -import {FIXED_POINT_ONE} from "rain.math.fixedpoint/lib/FixedPointDecimalConstants.sol"; /// @title LibOpChainIdNP /// Implementation of the EVM `CHAINID` opcode as a standard Rainlang opcode. @@ -15,10 +14,9 @@ library LibOpChainIdNP { } function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal view returns (Pointer) { - uint256 decimalOne = FIXED_POINT_ONE; assembly ("memory-safe") { stackTop := sub(stackTop, 0x20) - mstore(stackTop, mul(chainid(), decimalOne)) + mstore(stackTop, chainid()) } return stackTop; } @@ -29,7 +27,7 @@ library LibOpChainIdNP { returns (uint256[] memory) { uint256[] memory outputs = new uint256[](1); - outputs[0] = block.chainid * FIXED_POINT_ONE; + outputs[0] = block.chainid; return outputs; } } diff --git a/src/lib/op/evm/LibOpTimestampNP.sol b/src/lib/op/evm/LibOpTimestampNP.sol index f7d08feb..65c5b201 100644 --- a/src/lib/op/evm/LibOpTimestampNP.sol +++ b/src/lib/op/evm/LibOpTimestampNP.sol @@ -5,7 +5,6 @@ import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; import {InterpreterStateNP, LibInterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {FIXED_POINT_ONE} from "rain.math.fixedpoint/lib/FixedPointDecimalConstants.sol"; /// @title LibOpTimestampNP /// Implementation of the EVM `TIMESTAMP` opcode as a standard Rainlang opcode. @@ -15,10 +14,9 @@ library LibOpTimestampNP { } function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal view returns (Pointer) { - uint256 decimalOne = FIXED_POINT_ONE; assembly ("memory-safe") { stackTop := sub(stackTop, 0x20) - mstore(stackTop, mul(timestamp(), decimalOne)) + mstore(stackTop, timestamp()) } return stackTop; } @@ -29,7 +27,7 @@ library LibOpTimestampNP { returns (uint256[] memory) { uint256[] memory outputs = new uint256[](1); - outputs[0] = block.timestamp * FIXED_POINT_ONE; + outputs[0] = block.timestamp; return outputs; } } diff --git a/src/lib/op/math/LibOpAvg.sol b/src/lib/op/math/LibOpAvg.sol index 63f1006e..d2e92aef 100644 --- a/src/lib/op/math/LibOpAvg.sol +++ b/src/lib/op/math/LibOpAvg.sol @@ -1,46 +1,46 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {UD60x18, avg} from "prb-math/UD60x18.sol"; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {UD60x18, avg} from "prb-math/UD60x18.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -/// @title LibOpAvg -/// @notice Opcode for the average of two decimal 18 fixed point numbers. -library LibOpAvg { - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - // There must be two inputs and one output. - return (2, 1); - } +// /// @title LibOpAvg +// /// @notice Opcode for the average of two decimal 18 fixed point numbers. +// library LibOpAvg { +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// // There must be two inputs and one output. +// return (2, 1); +// } - /// avg - /// 18 decimal fixed point average of two numbers. - function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { - uint256 a; - uint256 b; - assembly ("memory-safe") { - a := mload(stackTop) - stackTop := add(stackTop, 0x20) - b := mload(stackTop) - } - a = UD60x18.unwrap(avg(UD60x18.wrap(a), UD60x18.wrap(b))); +// /// avg +// /// 18 decimal fixed point average of two numbers. +// function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { +// uint256 a; +// uint256 b; +// assembly ("memory-safe") { +// a := mload(stackTop) +// stackTop := add(stackTop, 0x20) +// b := mload(stackTop) +// } +// a = UD60x18.unwrap(avg(UD60x18.wrap(a), UD60x18.wrap(b))); - assembly ("memory-safe") { - mstore(stackTop, a) - } - return stackTop; - } +// assembly ("memory-safe") { +// mstore(stackTop, a) +// } +// return stackTop; +// } - /// Gas intensive reference implementation of avg for testing. - function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) - internal - pure - returns (uint256[] memory) - { - uint256[] memory outputs = new uint256[](1); - outputs[0] = UD60x18.unwrap(avg(UD60x18.wrap(inputs[0]), UD60x18.wrap(inputs[1]))); - return outputs; - } -} +// /// Gas intensive reference implementation of avg for testing. +// function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) +// internal +// pure +// returns (uint256[] memory) +// { +// uint256[] memory outputs = new uint256[](1); +// outputs[0] = UD60x18.unwrap(avg(UD60x18.wrap(inputs[0]), UD60x18.wrap(inputs[1]))); +// return outputs; +// } +// } diff --git a/src/lib/op/math/LibOpCeil.sol b/src/lib/op/math/LibOpCeil.sol index edc40e22..ecfdea9e 100644 --- a/src/lib/op/math/LibOpCeil.sol +++ b/src/lib/op/math/LibOpCeil.sol @@ -1,43 +1,43 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {UD60x18, ceil} from "prb-math/UD60x18.sol"; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {UD60x18, ceil} from "prb-math/UD60x18.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -/// @title LibOpCeil -/// @notice Opcode for the ceiling of an decimal 18 fixed point number. -library LibOpCeil { - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - // There must be one inputs and one output. - return (1, 1); - } +// /// @title LibOpCeil +// /// @notice Opcode for the ceiling of an decimal 18 fixed point number. +// library LibOpCeil { +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// // There must be one inputs and one output. +// return (1, 1); +// } - /// ceil - /// 18 decimal fixed point ceiling of a number. - function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { - uint256 a; - assembly ("memory-safe") { - a := mload(stackTop) - } - a = UD60x18.unwrap(ceil(UD60x18.wrap(a))); +// /// ceil +// /// 18 decimal fixed point ceiling of a number. +// function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { +// uint256 a; +// assembly ("memory-safe") { +// a := mload(stackTop) +// } +// a = UD60x18.unwrap(ceil(UD60x18.wrap(a))); - assembly ("memory-safe") { - mstore(stackTop, a) - } - return stackTop; - } +// assembly ("memory-safe") { +// mstore(stackTop, a) +// } +// return stackTop; +// } - /// Gas intensive reference implementation of ceil for testing. - function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) - internal - pure - returns (uint256[] memory) - { - uint256[] memory outputs = new uint256[](1); - outputs[0] = UD60x18.unwrap(ceil(UD60x18.wrap(inputs[0]))); - return outputs; - } -} +// /// Gas intensive reference implementation of ceil for testing. +// function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) +// internal +// pure +// returns (uint256[] memory) +// { +// uint256[] memory outputs = new uint256[](1); +// outputs[0] = UD60x18.unwrap(ceil(UD60x18.wrap(inputs[0]))); +// return outputs; +// } +// } diff --git a/src/lib/op/math/LibOpDiv.sol b/src/lib/op/math/LibOpDiv.sol index 783cdb33..c620d3ad 100644 --- a/src/lib/op/math/LibOpDiv.sol +++ b/src/lib/op/math/LibOpDiv.sol @@ -1,86 +1,86 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // 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 {LibWillOverflow} from "rain.math.fixedpoint/lib/LibWillOverflow.sol"; -import {UD60x18, div} from "prb-math/UD60x18.sol"; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// /// 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 {LibWillOverflow} from "rain.math.fixedpoint/lib/LibWillOverflow.sol"; +// import {UD60x18, div} from "prb-math/UD60x18.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -/// @title LibOpDiv -/// @notice Opcode to div N 18 decimal fixed point values. Errors on overflow. -library LibOpDiv { - function integrity(IntegrityCheckStateNP 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 LibOpDiv +// /// @notice Opcode to div N 18 decimal fixed point values. Errors on overflow. +// library LibOpDiv { +// function integrity(IntegrityCheckStateNP 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); +// } - /// div - /// 18 decimal fixed point division with implied overflow checks from PRB - /// Math. - function run(InterpreterStateNP 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(div(UD60x18.wrap(a), UD60x18.wrap(b))); +// /// div +// /// 18 decimal fixed point division with implied overflow checks from PRB +// /// Math. +// function run(InterpreterStateNP 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(div(UD60x18.wrap(a), UD60x18.wrap(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(div(UD60x18.wrap(a), UD60x18.wrap(b))); - unchecked { - i++; - } - } - } - assembly ("memory-safe") { - stackTop := sub(stackTop, 0x20) - mstore(stackTop, a) - } - return stackTop; - } +// { +// 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(div(UD60x18.wrap(a), UD60x18.wrap(b))); +// unchecked { +// i++; +// } +// } +// } +// assembly ("memory-safe") { +// stackTop := sub(stackTop, 0x20) +// mstore(stackTop, a) +// } +// return stackTop; +// } - /// Gas intensive reference implementation of division for testing. - function referenceFn(InterpreterStateNP 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]; - // Just bail out with a = some sentinel value if we're going to - // overflow or divide by zero. This gives the real implementation - // space to throw its own error that the test harness is expecting. - // We don't want the real implementation to fail to throw the - // error and also produce the same result, so a needs to have - // some collision resistant value. - if (b == 0 || LibWillOverflow.mulDivWillOverflow(a, 1e18, b)) { - a = uint256(keccak256(abi.encodePacked("overflow sentinel"))); - break; - } - a = OZMath.mulDiv(a, 1e18, b); - } - outputs = new uint256[](1); - outputs[0] = a; - } - } -} +// /// Gas intensive reference implementation of division for testing. +// function referenceFn(InterpreterStateNP 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]; +// // Just bail out with a = some sentinel value if we're going to +// // overflow or divide by zero. This gives the real implementation +// // space to throw its own error that the test harness is expecting. +// // We don't want the real implementation to fail to throw the +// // error and also produce the same result, so a needs to have +// // some collision resistant value. +// if (b == 0 || LibWillOverflow.mulDivWillOverflow(a, 1e18, b)) { +// a = uint256(keccak256(abi.encodePacked("overflow sentinel"))); +// break; +// } +// a = OZMath.mulDiv(a, 1e18, b); +// } +// outputs = new uint256[](1); +// outputs[0] = a; +// } +// } +// } diff --git a/src/lib/op/math/LibOpE.sol b/src/lib/op/math/LibOpE.sol index 5a24912d..55626a9d 100644 --- a/src/lib/op/math/LibOpE.sol +++ b/src/lib/op/math/LibOpE.sol @@ -1,35 +1,34 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -import {FIXED_POINT_ONE, FIXED_POINT_E} from "rain.math.fixedpoint/lib/FixedPointDecimalConstants.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -/// @title LibOpE -/// Stacks the mathematical constant e. -library LibOpE { - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - return (0, 1); - } +// /// @title LibOpE +// /// Stacks the mathematical constant e. +// library LibOpE { +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// return (0, 1); +// } - function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { - uint256 e = FIXED_POINT_E; - assembly ("memory-safe") { - stackTop := sub(stackTop, 0x20) - mstore(stackTop, e) - } - return stackTop; - } +// function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { +// uint256 e = FIXED_POINT_E; +// assembly ("memory-safe") { +// stackTop := sub(stackTop, 0x20) +// mstore(stackTop, e) +// } +// return stackTop; +// } - function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory) - internal - pure - returns (uint256[] memory) - { - uint256[] memory outputs = new uint256[](1); - outputs[0] = FIXED_POINT_E; - return outputs; - } -} +// function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory) +// internal +// pure +// returns (uint256[] memory) +// { +// uint256[] memory outputs = new uint256[](1); +// outputs[0] = FIXED_POINT_E; +// return outputs; +// } +// } diff --git a/src/lib/op/math/LibOpExp.sol b/src/lib/op/math/LibOpExp.sol index c5a2c68f..5ed4bef1 100644 --- a/src/lib/op/math/LibOpExp.sol +++ b/src/lib/op/math/LibOpExp.sol @@ -1,43 +1,43 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {UD60x18, exp} from "prb-math/UD60x18.sol"; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {UD60x18, exp} from "prb-math/UD60x18.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -/// @title LibOpExp -/// @notice Opcode for the natural exponential e^x as decimal 18 fixed point. -library LibOpExp { - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - // There must be one inputs and one output. - return (1, 1); - } +// /// @title LibOpExp +// /// @notice Opcode for the natural exponential e^x as decimal 18 fixed point. +// library LibOpExp { +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// // There must be one inputs and one output. +// return (1, 1); +// } - /// exp - /// 18 decimal fixed point natural exponent of a number. - function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { - uint256 a; - assembly ("memory-safe") { - a := mload(stackTop) - } - a = UD60x18.unwrap(exp(UD60x18.wrap(a))); +// /// exp +// /// 18 decimal fixed point natural exponent of a number. +// function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { +// uint256 a; +// assembly ("memory-safe") { +// a := mload(stackTop) +// } +// a = UD60x18.unwrap(exp(UD60x18.wrap(a))); - assembly ("memory-safe") { - mstore(stackTop, a) - } - return stackTop; - } +// assembly ("memory-safe") { +// mstore(stackTop, a) +// } +// return stackTop; +// } - /// Gas intensive reference implementation of exp for testing. - function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) - internal - pure - returns (uint256[] memory) - { - uint256[] memory outputs = new uint256[](1); - outputs[0] = UD60x18.unwrap(exp(UD60x18.wrap(inputs[0]))); - return outputs; - } -} +// /// Gas intensive reference implementation of exp for testing. +// function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) +// internal +// pure +// returns (uint256[] memory) +// { +// uint256[] memory outputs = new uint256[](1); +// outputs[0] = UD60x18.unwrap(exp(UD60x18.wrap(inputs[0]))); +// return outputs; +// } +// } diff --git a/src/lib/op/math/LibOpExp2.sol b/src/lib/op/math/LibOpExp2.sol index 691223a6..95c696f2 100644 --- a/src/lib/op/math/LibOpExp2.sol +++ b/src/lib/op/math/LibOpExp2.sol @@ -1,43 +1,43 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {UD60x18, exp2} from "prb-math/UD60x18.sol"; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {UD60x18, exp2} from "prb-math/UD60x18.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -/// @title LibOpExp2 -/// @notice Opcode for the binary exponential 2^x as decimal 18 fixed point. -library LibOpExp2 { - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - // There must be one inputs and one output. - return (1, 1); - } +// /// @title LibOpExp2 +// /// @notice Opcode for the binary exponential 2^x as decimal 18 fixed point. +// library LibOpExp2 { +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// // There must be one inputs and one output. +// return (1, 1); +// } - /// exp2 - /// 18 decimal fixed point binary exponent of a number. - function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { - uint256 a; - assembly ("memory-safe") { - a := mload(stackTop) - } - a = UD60x18.unwrap(exp2(UD60x18.wrap(a))); +// /// exp2 +// /// 18 decimal fixed point binary exponent of a number. +// function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { +// uint256 a; +// assembly ("memory-safe") { +// a := mload(stackTop) +// } +// a = UD60x18.unwrap(exp2(UD60x18.wrap(a))); - assembly ("memory-safe") { - mstore(stackTop, a) - } - return stackTop; - } +// assembly ("memory-safe") { +// mstore(stackTop, a) +// } +// return stackTop; +// } - /// Gas intensive reference implementation of exp for testing. - function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) - internal - pure - returns (uint256[] memory) - { - uint256[] memory outputs = new uint256[](1); - outputs[0] = UD60x18.unwrap(exp2(UD60x18.wrap(inputs[0]))); - return outputs; - } -} +// /// Gas intensive reference implementation of exp for testing. +// function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) +// internal +// pure +// returns (uint256[] memory) +// { +// uint256[] memory outputs = new uint256[](1); +// outputs[0] = UD60x18.unwrap(exp2(UD60x18.wrap(inputs[0]))); +// return outputs; +// } +// } diff --git a/src/lib/op/math/LibOpFloor.sol b/src/lib/op/math/LibOpFloor.sol index e3d10597..2c4fd1db 100644 --- a/src/lib/op/math/LibOpFloor.sol +++ b/src/lib/op/math/LibOpFloor.sol @@ -1,43 +1,43 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {UD60x18, floor} from "prb-math/UD60x18.sol"; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {UD60x18, floor} from "prb-math/UD60x18.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -/// @title LibOpFloor -/// @notice Opcode for the floor of an decimal 18 fixed point number. -library LibOpFloor { - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - // There must be one input and one output. - return (1, 1); - } +// /// @title LibOpFloor +// /// @notice Opcode for the floor of an decimal 18 fixed point number. +// library LibOpFloor { +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// // There must be one input and one output. +// return (1, 1); +// } - /// floor - /// 18 decimal fixed point floor of a number. - function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { - uint256 a; - assembly ("memory-safe") { - a := mload(stackTop) - } - a = UD60x18.unwrap(floor(UD60x18.wrap(a))); +// /// floor +// /// 18 decimal fixed point floor of a number. +// function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { +// uint256 a; +// assembly ("memory-safe") { +// a := mload(stackTop) +// } +// a = UD60x18.unwrap(floor(UD60x18.wrap(a))); - assembly ("memory-safe") { - mstore(stackTop, a) - } - return stackTop; - } +// assembly ("memory-safe") { +// mstore(stackTop, a) +// } +// return stackTop; +// } - /// Gas intensive reference implementation of floor for testing. - function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) - internal - pure - returns (uint256[] memory) - { - uint256[] memory outputs = new uint256[](1); - outputs[0] = UD60x18.unwrap(floor(UD60x18.wrap(inputs[0]))); - return outputs; - } -} +// /// Gas intensive reference implementation of floor for testing. +// function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) +// internal +// pure +// returns (uint256[] memory) +// { +// uint256[] memory outputs = new uint256[](1); +// outputs[0] = UD60x18.unwrap(floor(UD60x18.wrap(inputs[0]))); +// return outputs; +// } +// } diff --git a/src/lib/op/math/LibOpFrac.sol b/src/lib/op/math/LibOpFrac.sol index 734317f2..e1f580f5 100644 --- a/src/lib/op/math/LibOpFrac.sol +++ b/src/lib/op/math/LibOpFrac.sol @@ -1,43 +1,43 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {UD60x18, frac} from "prb-math/UD60x18.sol"; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {UD60x18, frac} from "prb-math/UD60x18.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -/// @title LibOpFrac -/// @notice Opcode for the frac of an decimal 18 fixed point number. -library LibOpFrac { - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - // There must be one input and one output. - return (1, 1); - } +// /// @title LibOpFrac +// /// @notice Opcode for the frac of an decimal 18 fixed point number. +// library LibOpFrac { +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// // There must be one input and one output. +// return (1, 1); +// } - /// frac - /// 18 decimal fixed point frac of a number. - function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { - uint256 a; - assembly ("memory-safe") { - a := mload(stackTop) - } - a = UD60x18.unwrap(frac(UD60x18.wrap(a))); +// /// frac +// /// 18 decimal fixed point frac of a number. +// function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { +// uint256 a; +// assembly ("memory-safe") { +// a := mload(stackTop) +// } +// a = UD60x18.unwrap(frac(UD60x18.wrap(a))); - assembly ("memory-safe") { - mstore(stackTop, a) - } - return stackTop; - } +// assembly ("memory-safe") { +// mstore(stackTop, a) +// } +// return stackTop; +// } - /// Gas intensive reference implementation of frac for testing. - function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) - internal - pure - returns (uint256[] memory) - { - uint256[] memory outputs = new uint256[](1); - outputs[0] = UD60x18.unwrap(frac(UD60x18.wrap(inputs[0]))); - return outputs; - } -} +// /// Gas intensive reference implementation of frac for testing. +// function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) +// internal +// pure +// returns (uint256[] memory) +// { +// uint256[] memory outputs = new uint256[](1); +// outputs[0] = UD60x18.unwrap(frac(UD60x18.wrap(inputs[0]))); +// return outputs; +// } +// } diff --git a/src/lib/op/math/LibOpGm.sol b/src/lib/op/math/LibOpGm.sol index cece5216..f856adfa 100644 --- a/src/lib/op/math/LibOpGm.sol +++ b/src/lib/op/math/LibOpGm.sol @@ -1,47 +1,47 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {UD60x18, gm} from "prb-math/UD60x18.sol"; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {UD60x18, gm} from "prb-math/UD60x18.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -/// @title LibOpGm -/// @notice Opcode for the geometric average of two decimal 18 fixed point -/// numbers. -library LibOpGm { - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - // There must be two inputs and one output. - return (2, 1); - } +// /// @title LibOpGm +// /// @notice Opcode for the geometric average of two decimal 18 fixed point +// /// numbers. +// library LibOpGm { +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// // There must be two inputs and one output. +// return (2, 1); +// } - /// gm - /// 18 decimal fixed point geometric average of two numbers. - function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { - uint256 a; - uint256 b; - assembly ("memory-safe") { - a := mload(stackTop) - stackTop := add(stackTop, 0x20) - b := mload(stackTop) - } - a = UD60x18.unwrap(gm(UD60x18.wrap(a), UD60x18.wrap(b))); +// /// gm +// /// 18 decimal fixed point geometric average of two numbers. +// function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { +// uint256 a; +// uint256 b; +// assembly ("memory-safe") { +// a := mload(stackTop) +// stackTop := add(stackTop, 0x20) +// b := mload(stackTop) +// } +// a = UD60x18.unwrap(gm(UD60x18.wrap(a), UD60x18.wrap(b))); - assembly ("memory-safe") { - mstore(stackTop, a) - } - return stackTop; - } +// assembly ("memory-safe") { +// mstore(stackTop, a) +// } +// return stackTop; +// } - /// Gas intensive reference implementation of gm for testing. - function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) - internal - pure - returns (uint256[] memory) - { - uint256[] memory outputs = new uint256[](1); - outputs[0] = UD60x18.unwrap(gm(UD60x18.wrap(inputs[0]), UD60x18.wrap(inputs[1]))); - return outputs; - } -} +// /// Gas intensive reference implementation of gm for testing. +// function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) +// internal +// pure +// returns (uint256[] memory) +// { +// uint256[] memory outputs = new uint256[](1); +// outputs[0] = UD60x18.unwrap(gm(UD60x18.wrap(inputs[0]), UD60x18.wrap(inputs[1]))); +// return outputs; +// } +// } diff --git a/src/lib/op/math/LibOpHeadroom.sol b/src/lib/op/math/LibOpHeadroom.sol index 8ecffc40..eea84e8c 100644 --- a/src/lib/op/math/LibOpHeadroom.sol +++ b/src/lib/op/math/LibOpHeadroom.sol @@ -1,47 +1,47 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {UD60x18, frac} from "prb-math/UD60x18.sol"; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {UD60x18, frac} from "prb-math/UD60x18.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -/// @title LibOpHeadroom -/// @notice Opcode for the headroom (distance to ceil) of an decimal 18 fixed -/// point number. -library LibOpHeadroom { - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - // There must be one input and one output. - return (1, 1); - } +// /// @title LibOpHeadroom +// /// @notice Opcode for the headroom (distance to ceil) of an decimal 18 fixed +// /// point number. +// library LibOpHeadroom { +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// // There must be one input and one output. +// return (1, 1); +// } - /// headroom - /// 18 decimal fixed point headroom of a number. - function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { - uint256 a; - assembly ("memory-safe") { - a := mload(stackTop) - } - // Can't underflow as frac is always less than 1e18. - unchecked { - a = 1e18 - UD60x18.unwrap(frac(UD60x18.wrap(a))); - } +// /// headroom +// /// 18 decimal fixed point headroom of a number. +// function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { +// uint256 a; +// assembly ("memory-safe") { +// a := mload(stackTop) +// } +// // Can't underflow as frac is always less than 1e18. +// unchecked { +// a = 1e18 - UD60x18.unwrap(frac(UD60x18.wrap(a))); +// } - assembly ("memory-safe") { - mstore(stackTop, a) - } - return stackTop; - } +// assembly ("memory-safe") { +// mstore(stackTop, a) +// } +// return stackTop; +// } - /// Gas intensive reference implementation of headroom for testing. - function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) - internal - pure - returns (uint256[] memory) - { - uint256[] memory outputs = new uint256[](1); - outputs[0] = 1e18 - UD60x18.unwrap(frac(UD60x18.wrap(inputs[0]))); - return outputs; - } -} +// /// Gas intensive reference implementation of headroom for testing. +// function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) +// internal +// pure +// returns (uint256[] memory) +// { +// uint256[] memory outputs = new uint256[](1); +// outputs[0] = 1e18 - UD60x18.unwrap(frac(UD60x18.wrap(inputs[0]))); +// return outputs; +// } +// } diff --git a/src/lib/op/math/LibOpInv.sol b/src/lib/op/math/LibOpInv.sol index 4cf9fdff..593ae1ba 100644 --- a/src/lib/op/math/LibOpInv.sol +++ b/src/lib/op/math/LibOpInv.sol @@ -1,43 +1,43 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {UD60x18, inv} from "prb-math/UD60x18.sol"; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {UD60x18, inv} from "prb-math/UD60x18.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -/// @title LibOpInv -/// @notice Opcode for the inverse 1 / x of an decimal 18 fixed point number. -library LibOpInv { - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - // There must be one inputs and one output. - return (1, 1); - } +// /// @title LibOpInv +// /// @notice Opcode for the inverse 1 / x of an decimal 18 fixed point number. +// library LibOpInv { +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// // There must be one inputs and one output. +// return (1, 1); +// } - /// inv - /// 18 decimal fixed point inverse of a number. - function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { - uint256 a; - assembly ("memory-safe") { - a := mload(stackTop) - } - a = UD60x18.unwrap(inv(UD60x18.wrap(a))); +// /// inv +// /// 18 decimal fixed point inverse of a number. +// function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { +// uint256 a; +// assembly ("memory-safe") { +// a := mload(stackTop) +// } +// a = UD60x18.unwrap(inv(UD60x18.wrap(a))); - assembly ("memory-safe") { - mstore(stackTop, a) - } - return stackTop; - } +// assembly ("memory-safe") { +// mstore(stackTop, a) +// } +// return stackTop; +// } - /// Gas intensive reference implementation of inv for testing. - function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) - internal - pure - returns (uint256[] memory) - { - uint256[] memory outputs = new uint256[](1); - outputs[0] = UD60x18.unwrap(inv(UD60x18.wrap(inputs[0]))); - return outputs; - } -} +// /// Gas intensive reference implementation of inv for testing. +// function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) +// internal +// pure +// returns (uint256[] memory) +// { +// uint256[] memory outputs = new uint256[](1); +// outputs[0] = UD60x18.unwrap(inv(UD60x18.wrap(inputs[0]))); +// return outputs; +// } +// } diff --git a/src/lib/op/math/LibOpLn.sol b/src/lib/op/math/LibOpLn.sol index 5d4da12c..3ba6d8b1 100644 --- a/src/lib/op/math/LibOpLn.sol +++ b/src/lib/op/math/LibOpLn.sol @@ -1,43 +1,43 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {UD60x18, ln} from "prb-math/UD60x18.sol"; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {UD60x18, ln} from "prb-math/UD60x18.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -/// @title LibOpLn -/// @notice Opcode for the natural logarithm of an decimal 18 fixed point number. -library LibOpLn { - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - // There must be one inputs and one output. - return (1, 1); - } +// /// @title LibOpLn +// /// @notice Opcode for the natural logarithm of an decimal 18 fixed point number. +// library LibOpLn { +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// // There must be one inputs and one output. +// return (1, 1); +// } - /// ln - /// 18 decimal fixed point natural logarithm of a number. - function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { - uint256 a; - assembly ("memory-safe") { - a := mload(stackTop) - } - a = UD60x18.unwrap(ln(UD60x18.wrap(a))); +// /// ln +// /// 18 decimal fixed point natural logarithm of a number. +// function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { +// uint256 a; +// assembly ("memory-safe") { +// a := mload(stackTop) +// } +// a = UD60x18.unwrap(ln(UD60x18.wrap(a))); - assembly ("memory-safe") { - mstore(stackTop, a) - } - return stackTop; - } +// assembly ("memory-safe") { +// mstore(stackTop, a) +// } +// return stackTop; +// } - /// Gas intensive reference implementation of ln for testing. - function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) - internal - pure - returns (uint256[] memory) - { - uint256[] memory outputs = new uint256[](1); - outputs[0] = UD60x18.unwrap(ln(UD60x18.wrap(inputs[0]))); - return outputs; - } -} +// /// Gas intensive reference implementation of ln for testing. +// function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) +// internal +// pure +// returns (uint256[] memory) +// { +// uint256[] memory outputs = new uint256[](1); +// outputs[0] = UD60x18.unwrap(ln(UD60x18.wrap(inputs[0]))); +// return outputs; +// } +// } diff --git a/src/lib/op/math/LibOpLog10.sol b/src/lib/op/math/LibOpLog10.sol index ba61a3a1..7022a55c 100644 --- a/src/lib/op/math/LibOpLog10.sol +++ b/src/lib/op/math/LibOpLog10.sol @@ -1,43 +1,43 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {UD60x18, log10} from "prb-math/UD60x18.sol"; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {UD60x18, log10} from "prb-math/UD60x18.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -/// @title LibOpLog10 -/// @notice Opcode for the common logarithm of an decimal 18 fixed point number. -library LibOpLog10 { - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - // There must be one inputs and one output. - return (1, 1); - } +// /// @title LibOpLog10 +// /// @notice Opcode for the common logarithm of an decimal 18 fixed point number. +// library LibOpLog10 { +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// // There must be one inputs and one output. +// return (1, 1); +// } - /// log10 - /// 18 decimal fixed point common logarithm of a number. - function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { - uint256 a; - assembly ("memory-safe") { - a := mload(stackTop) - } - a = UD60x18.unwrap(log10(UD60x18.wrap(a))); +// /// log10 +// /// 18 decimal fixed point common logarithm of a number. +// function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { +// uint256 a; +// assembly ("memory-safe") { +// a := mload(stackTop) +// } +// a = UD60x18.unwrap(log10(UD60x18.wrap(a))); - assembly ("memory-safe") { - mstore(stackTop, a) - } - return stackTop; - } +// assembly ("memory-safe") { +// mstore(stackTop, a) +// } +// return stackTop; +// } - /// Gas intensive reference implementation of log10 for testing. - function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) - internal - pure - returns (uint256[] memory) - { - uint256[] memory outputs = new uint256[](1); - outputs[0] = UD60x18.unwrap(log10(UD60x18.wrap(inputs[0]))); - return outputs; - } -} +// /// Gas intensive reference implementation of log10 for testing. +// function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) +// internal +// pure +// returns (uint256[] memory) +// { +// uint256[] memory outputs = new uint256[](1); +// outputs[0] = UD60x18.unwrap(log10(UD60x18.wrap(inputs[0]))); +// return outputs; +// } +// } diff --git a/src/lib/op/math/LibOpLog2.sol b/src/lib/op/math/LibOpLog2.sol index 7b461cc3..815bc94a 100644 --- a/src/lib/op/math/LibOpLog2.sol +++ b/src/lib/op/math/LibOpLog2.sol @@ -1,43 +1,43 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {UD60x18, log2} from "prb-math/UD60x18.sol"; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {UD60x18, log2} from "prb-math/UD60x18.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -/// @title LibOpLog2 -/// @notice Opcode for the binary logarithm of an decimal 18 fixed point number. -library LibOpLog2 { - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - // There must be one inputs and one output. - return (1, 1); - } +// /// @title LibOpLog2 +// /// @notice Opcode for the binary logarithm of an decimal 18 fixed point number. +// library LibOpLog2 { +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// // There must be one inputs and one output. +// return (1, 1); +// } - /// log2 - /// 18 decimal fixed point binary logarithm of a number. - function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { - uint256 a; - assembly ("memory-safe") { - a := mload(stackTop) - } - a = UD60x18.unwrap(log2(UD60x18.wrap(a))); +// /// log2 +// /// 18 decimal fixed point binary logarithm of a number. +// function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { +// uint256 a; +// assembly ("memory-safe") { +// a := mload(stackTop) +// } +// a = UD60x18.unwrap(log2(UD60x18.wrap(a))); - assembly ("memory-safe") { - mstore(stackTop, a) - } - return stackTop; - } +// assembly ("memory-safe") { +// mstore(stackTop, a) +// } +// return stackTop; +// } - /// Gas intensive reference implementation of log2 for testing. - function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) - internal - pure - returns (uint256[] memory) - { - uint256[] memory outputs = new uint256[](1); - outputs[0] = UD60x18.unwrap(log2(UD60x18.wrap(inputs[0]))); - return outputs; - } -} +// /// Gas intensive reference implementation of log2 for testing. +// function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) +// internal +// pure +// returns (uint256[] memory) +// { +// uint256[] memory outputs = new uint256[](1); +// outputs[0] = UD60x18.unwrap(log2(UD60x18.wrap(inputs[0]))); +// return outputs; +// } +// } diff --git a/src/lib/op/math/LibOpMul.sol b/src/lib/op/math/LibOpMul.sol index f364a622..625373ee 100644 --- a/src/lib/op/math/LibOpMul.sol +++ b/src/lib/op/math/LibOpMul.sol @@ -1,80 +1,80 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // 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 {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -import {LibWillOverflow} from "rain.math.fixedpoint/lib/LibWillOverflow.sol"; +// /// 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 {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {LibWillOverflow} from "rain.math.fixedpoint/lib/LibWillOverflow.sol"; -/// @title LibOpMul -/// @notice Opcode to mul N 18 decimal fixed point values. Errors on overflow. -library LibOpMul { - function integrity(IntegrityCheckStateNP 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 decimal fixed point values. Errors on overflow. +// library LibOpMul { +// function integrity(IntegrityCheckStateNP 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); +// } - /// mul - /// 18 decimal fixed point multiplication with implied overflow checks from - /// PRB Math. - function run(InterpreterStateNP 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 +// /// 18 decimal fixed point multiplication with implied overflow checks from +// /// PRB Math. +// function run(InterpreterStateNP 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))); - { - 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 = (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; +// } - /// Gas intensive reference implementation of multiplication for testing. - function referenceFn(InterpreterStateNP 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(InterpreterStateNP 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; +// } +// } +// } diff --git a/src/lib/op/math/LibOpPow.sol b/src/lib/op/math/LibOpPow.sol index 75d453f8..33173d44 100644 --- a/src/lib/op/math/LibOpPow.sol +++ b/src/lib/op/math/LibOpPow.sol @@ -1,47 +1,47 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {UD60x18, pow} from "prb-math/UD60x18.sol"; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {UD60x18, pow} from "prb-math/UD60x18.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -/// @title LibOpPow -/// @notice Opcode to pow N 18 decimal fixed point values to an 18 decimal power. -library LibOpPow { - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - // There must be two inputs and one output. - return (2, 1); - } +// /// @title LibOpPow +// /// @notice Opcode to pow N 18 decimal fixed point values to an 18 decimal power. +// library LibOpPow { +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// // There must be two inputs and one output. +// return (2, 1); +// } - /// pow - /// 18 decimal fixed point exponentiation with implied overflow checks from - /// PRB Math. - function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { - uint256 a; - uint256 b; - assembly ("memory-safe") { - a := mload(stackTop) - stackTop := add(stackTop, 0x20) - b := mload(stackTop) - } - a = UD60x18.unwrap(pow(UD60x18.wrap(a), UD60x18.wrap(b))); +// /// pow +// /// 18 decimal fixed point exponentiation with implied overflow checks from +// /// PRB Math. +// function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { +// uint256 a; +// uint256 b; +// assembly ("memory-safe") { +// a := mload(stackTop) +// stackTop := add(stackTop, 0x20) +// b := mload(stackTop) +// } +// a = UD60x18.unwrap(pow(UD60x18.wrap(a), UD60x18.wrap(b))); - assembly ("memory-safe") { - mstore(stackTop, a) - } - return stackTop; - } +// assembly ("memory-safe") { +// mstore(stackTop, a) +// } +// return stackTop; +// } - /// Gas intensive reference implementation of pow for testing. - function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) - internal - pure - returns (uint256[] memory) - { - uint256[] memory outputs = new uint256[](1); - outputs[0] = UD60x18.unwrap(pow(UD60x18.wrap(inputs[0]), UD60x18.wrap(inputs[1]))); - return outputs; - } -} +// /// Gas intensive reference implementation of pow for testing. +// function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) +// internal +// pure +// returns (uint256[] memory) +// { +// uint256[] memory outputs = new uint256[](1); +// outputs[0] = UD60x18.unwrap(pow(UD60x18.wrap(inputs[0]), UD60x18.wrap(inputs[1]))); +// return outputs; +// } +// } diff --git a/src/lib/op/math/LibOpScale18.sol b/src/lib/op/math/LibOpScale18.sol index 2904537b..473b346e 100644 --- a/src/lib/op/math/LibOpScale18.sol +++ b/src/lib/op/math/LibOpScale18.sol @@ -1,41 +1,40 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {LibFixedPointDecimalScale} from "rain.math.fixedpoint/lib/LibFixedPointDecimalScale.sol"; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -/// @title LibOpScale18 -/// @notice Opcode for scaling a number to 18 decimal fixed point. -library LibOpScale18 { - using LibFixedPointDecimalScale for uint256; +// /// @title LibOpScale18 +// /// @notice Opcode for scaling a number to 18 decimal fixed point. +// library LibOpScale18 { +// using LibFixedPointDecimalScale for uint256; - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - return (1, 1); - } +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// return (1, 1); +// } - /// scale-18 - /// 18 decimal fixed point scaling. - function run(InterpreterStateNP memory, Operand operand, Pointer stackTop) internal pure returns (Pointer) { - uint256 a; - assembly ("memory-safe") { - a := mload(stackTop) - } - a = a.scale18(Operand.unwrap(operand) & 0xFF, Operand.unwrap(operand) >> 8); - assembly ("memory-safe") { - mstore(stackTop, a) - } - return stackTop; - } +// /// scale-18 +// /// 18 decimal fixed point scaling. +// function run(InterpreterStateNP memory, Operand operand, Pointer stackTop) internal pure returns (Pointer) { +// uint256 a; +// assembly ("memory-safe") { +// a := mload(stackTop) +// } +// a = a.scale18(Operand.unwrap(operand) & 0xFF, Operand.unwrap(operand) >> 8); +// assembly ("memory-safe") { +// mstore(stackTop, a) +// } +// return stackTop; +// } - function referenceFn(InterpreterStateNP memory, Operand operand, uint256[] memory inputs) - internal - pure - returns (uint256[] memory outputs) - { - outputs = new uint256[](1); - outputs[0] = inputs[0].scale18(Operand.unwrap(operand) & 0xFF, Operand.unwrap(operand) >> 8); - } -} +// function referenceFn(InterpreterStateNP memory, Operand operand, uint256[] memory inputs) +// internal +// pure +// returns (uint256[] memory outputs) +// { +// outputs = new uint256[](1); +// outputs[0] = inputs[0].scale18(Operand.unwrap(operand) & 0xFF, Operand.unwrap(operand) >> 8); +// } +// } diff --git a/src/lib/op/math/LibOpScale18Dynamic.sol b/src/lib/op/math/LibOpScale18Dynamic.sol index 071d6d24..d64f3dc4 100644 --- a/src/lib/op/math/LibOpScale18Dynamic.sol +++ b/src/lib/op/math/LibOpScale18Dynamic.sol @@ -1,53 +1,52 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {LibFixedPointDecimalScale, DECIMAL_MAX_SAFE_INT} from "rain.math.fixedpoint/lib/LibFixedPointDecimalScale.sol"; -import {MASK_2BIT} from "sol.lib.binmaskflag/Binary.sol"; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {LibParseLiteral} from "../../parse/literal/LibParseLiteral.sol"; +// import {MASK_2BIT} from "sol.lib.binmaskflag/Binary.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {LibParseLiteral} from "../../parse/literal/LibParseLiteral.sol"; -/// @title LibOpScale18Dynamic -/// @notice Opcode for scaling a number to 18 decimal fixed point based on -/// runtime scale input. -library LibOpScale18Dynamic { - using LibFixedPointDecimalScale for uint256; +// /// @title LibOpScale18Dynamic +// /// @notice Opcode for scaling a number to 18 decimal fixed point based on +// /// runtime scale input. +// library LibOpScale18Dynamic { +// using LibFixedPointDecimalScale for uint256; - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - return (2, 1); - } +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// return (2, 1); +// } - /// scale-18-dynamic - /// 18 decimal fixed point scaling from runtime value. - function run(InterpreterStateNP memory, Operand operand, Pointer stackTop) internal pure returns (Pointer) { - uint256 a; - uint256 scale; - assembly ("memory-safe") { - scale := mload(stackTop) - stackTop := add(stackTop, 0x20) - a := mload(stackTop) - } - // There's no upper bound because we might be saturating all the way to - // infinity. `scale18` will handle catching such things. - scale = LibFixedPointDecimalScale.decimalOrIntToInt(scale, DECIMAL_MAX_SAFE_INT); - a = a.scale18(scale, Operand.unwrap(operand)); - assembly ("memory-safe") { - mstore(stackTop, a) - } - return stackTop; - } +// /// scale-18-dynamic +// /// 18 decimal fixed point scaling from runtime value. +// function run(InterpreterStateNP memory, Operand operand, Pointer stackTop) internal pure returns (Pointer) { +// uint256 a; +// uint256 scale; +// assembly ("memory-safe") { +// scale := mload(stackTop) +// stackTop := add(stackTop, 0x20) +// a := mload(stackTop) +// } +// // There's no upper bound because we might be saturating all the way to +// // infinity. `scale18` will handle catching such things. +// scale = LibFixedPointDecimalScale.decimalOrIntToInt(scale, DECIMAL_MAX_SAFE_INT); +// a = a.scale18(scale, Operand.unwrap(operand)); +// assembly ("memory-safe") { +// mstore(stackTop, a) +// } +// return stackTop; +// } - function referenceFn(InterpreterStateNP memory, Operand operand, uint256[] memory inputs) - internal - pure - returns (uint256[] memory outputs) - { - outputs = new uint256[](1); - outputs[0] = inputs[1].scale18( - LibFixedPointDecimalScale.decimalOrIntToInt(inputs[0], DECIMAL_MAX_SAFE_INT), - Operand.unwrap(operand) & MASK_2BIT - ); - } -} +// function referenceFn(InterpreterStateNP memory, Operand operand, uint256[] memory inputs) +// internal +// pure +// returns (uint256[] memory outputs) +// { +// outputs = new uint256[](1); +// outputs[0] = inputs[1].scale18( +// LibFixedPointDecimalScale.decimalOrIntToInt(inputs[0], DECIMAL_MAX_SAFE_INT), +// Operand.unwrap(operand) & MASK_2BIT +// ); +// } +// } diff --git a/src/lib/op/math/LibOpScaleN.sol b/src/lib/op/math/LibOpScaleN.sol index b6149139..905c7384 100644 --- a/src/lib/op/math/LibOpScaleN.sol +++ b/src/lib/op/math/LibOpScaleN.sol @@ -1,41 +1,40 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {LibFixedPointDecimalScale} from "rain.math.fixedpoint/lib/LibFixedPointDecimalScale.sol"; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -/// @title LibOpScaleN -/// @notice Opcode for scaling a decimal18 number to some other scale N. -library LibOpScaleN { - using LibFixedPointDecimalScale for uint256; +// /// @title LibOpScaleN +// /// @notice Opcode for scaling a decimal18 number to some other scale N. +// library LibOpScaleN { +// using LibFixedPointDecimalScale for uint256; - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - return (1, 1); - } +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// return (1, 1); +// } - /// scale-n - /// Scale from 18 decimal to n decimal. - function run(InterpreterStateNP memory, Operand operand, Pointer stackTop) internal pure returns (Pointer) { - uint256 a; - assembly ("memory-safe") { - a := mload(stackTop) - } - a = a.scaleN(Operand.unwrap(operand) & 0xFF, Operand.unwrap(operand) >> 8); - assembly ("memory-safe") { - mstore(stackTop, a) - } - return stackTop; - } +// /// scale-n +// /// Scale from 18 decimal to n decimal. +// function run(InterpreterStateNP memory, Operand operand, Pointer stackTop) internal pure returns (Pointer) { +// uint256 a; +// assembly ("memory-safe") { +// a := mload(stackTop) +// } +// a = a.scaleN(Operand.unwrap(operand) & 0xFF, Operand.unwrap(operand) >> 8); +// assembly ("memory-safe") { +// mstore(stackTop, a) +// } +// return stackTop; +// } - function referenceFn(InterpreterStateNP memory, Operand operand, uint256[] memory inputs) - internal - pure - returns (uint256[] memory outputs) - { - outputs = new uint256[](1); - outputs[0] = inputs[0].scaleN(Operand.unwrap(operand) & 0xFF, Operand.unwrap(operand) >> 8); - } -} +// function referenceFn(InterpreterStateNP memory, Operand operand, uint256[] memory inputs) +// internal +// pure +// returns (uint256[] memory outputs) +// { +// outputs = new uint256[](1); +// outputs[0] = inputs[0].scaleN(Operand.unwrap(operand) & 0xFF, Operand.unwrap(operand) >> 8); +// } +// } diff --git a/src/lib/op/math/LibOpScaleNDynamic.sol b/src/lib/op/math/LibOpScaleNDynamic.sol index b19c6789..56a2aeeb 100644 --- a/src/lib/op/math/LibOpScaleNDynamic.sol +++ b/src/lib/op/math/LibOpScaleNDynamic.sol @@ -1,53 +1,52 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {LibFixedPointDecimalScale, DECIMAL_MAX_SAFE_INT} from "rain.math.fixedpoint/lib/LibFixedPointDecimalScale.sol"; -import {MASK_2BIT} from "sol.lib.binmaskflag/Binary.sol"; -import {LibParseLiteral} from "../../parse/literal/LibParseLiteral.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {MASK_2BIT} from "sol.lib.binmaskflag/Binary.sol"; +// import {LibParseLiteral} from "../../parse/literal/LibParseLiteral.sol"; -/// @title LibOpScaleNDynamic -/// @notice Opcode for scaling a number from 18 decimal fixed point based on -/// runtime scale input. -library LibOpScaleNDynamic { - using LibFixedPointDecimalScale for uint256; +// /// @title LibOpScaleNDynamic +// /// @notice Opcode for scaling a number from 18 decimal fixed point based on +// /// runtime scale input. +// library LibOpScaleNDynamic { +// using LibFixedPointDecimalScale for uint256; - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - return (2, 1); - } +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// return (2, 1); +// } - /// scaleN-dynamic - /// 18 decimal fixed point scaling from runtime value. - function run(InterpreterStateNP memory, Operand operand, Pointer stackTop) internal pure returns (Pointer) { - uint256 a; - uint256 scale; - assembly ("memory-safe") { - scale := mload(stackTop) - stackTop := add(stackTop, 0x20) - a := mload(stackTop) - } - a = a.scaleN( - LibFixedPointDecimalScale.decimalOrIntToInt(scale, DECIMAL_MAX_SAFE_INT), - Operand.unwrap(operand) & MASK_2BIT - ); - assembly ("memory-safe") { - mstore(stackTop, a) - } - return stackTop; - } +// /// scaleN-dynamic +// /// 18 decimal fixed point scaling from runtime value. +// function run(InterpreterStateNP memory, Operand operand, Pointer stackTop) internal pure returns (Pointer) { +// uint256 a; +// uint256 scale; +// assembly ("memory-safe") { +// scale := mload(stackTop) +// stackTop := add(stackTop, 0x20) +// a := mload(stackTop) +// } +// a = a.scaleN( +// LibFixedPointDecimalScale.decimalOrIntToInt(scale, DECIMAL_MAX_SAFE_INT), +// Operand.unwrap(operand) & MASK_2BIT +// ); +// assembly ("memory-safe") { +// mstore(stackTop, a) +// } +// return stackTop; +// } - function referenceFn(InterpreterStateNP memory, Operand operand, uint256[] memory inputs) - internal - pure - returns (uint256[] memory outputs) - { - outputs = new uint256[](1); - outputs[0] = inputs[1].scaleN( - LibFixedPointDecimalScale.decimalOrIntToInt(inputs[0], DECIMAL_MAX_SAFE_INT), - Operand.unwrap(operand) & MASK_2BIT - ); - } -} +// function referenceFn(InterpreterStateNP memory, Operand operand, uint256[] memory inputs) +// internal +// pure +// returns (uint256[] memory outputs) +// { +// outputs = new uint256[](1); +// outputs[0] = inputs[1].scaleN( +// LibFixedPointDecimalScale.decimalOrIntToInt(inputs[0], DECIMAL_MAX_SAFE_INT), +// Operand.unwrap(operand) & MASK_2BIT +// ); +// } +// } diff --git a/src/lib/op/math/LibOpSnapToUnit.sol b/src/lib/op/math/LibOpSnapToUnit.sol index 77c576e1..d0ef709a 100644 --- a/src/lib/op/math/LibOpSnapToUnit.sol +++ b/src/lib/op/math/LibOpSnapToUnit.sol @@ -1,68 +1,68 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {UD60x18, frac, ceil, floor} from "prb-math/UD60x18.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {UD60x18, frac, ceil, floor} from "prb-math/UD60x18.sol"; -/// @title LibOpSnapToUnit -/// @notice Opcode for the snap to unit of an decimal 18 fixed point number. -library LibOpSnapToUnit { - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - // There must be two inputs and one output. - return (2, 1); - } +// /// @title LibOpSnapToUnit +// /// @notice Opcode for the snap to unit of an decimal 18 fixed point number. +// library LibOpSnapToUnit { +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// // There must be two inputs and one output. +// return (2, 1); +// } - /// snap-to-unit - function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { - unchecked { - uint256 threshold; - uint256 value; - assembly ("memory-safe") { - threshold := mload(stackTop) - stackTop := add(stackTop, 0x20) - value := mload(stackTop) - } - uint256 valueFrac = UD60x18.unwrap(frac(UD60x18.wrap(value))); - if (valueFrac <= threshold) { - value = UD60x18.unwrap(floor(UD60x18.wrap(value))); - assembly ("memory-safe") { - mstore(stackTop, value) - } - } - // Frac cannot be more than 1e18, so we can safely subtract it from 1e18 - // as unchecked. - else if ((1e18 - valueFrac) <= threshold) { - value = UD60x18.unwrap(ceil(UD60x18.wrap(value))); - assembly ("memory-safe") { - mstore(stackTop, value) - } - } - return stackTop; - } - } +// /// snap-to-unit +// function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { +// unchecked { +// uint256 threshold; +// uint256 value; +// assembly ("memory-safe") { +// threshold := mload(stackTop) +// stackTop := add(stackTop, 0x20) +// value := mload(stackTop) +// } +// uint256 valueFrac = UD60x18.unwrap(frac(UD60x18.wrap(value))); +// if (valueFrac <= threshold) { +// value = UD60x18.unwrap(floor(UD60x18.wrap(value))); +// assembly ("memory-safe") { +// mstore(stackTop, value) +// } +// } +// // Frac cannot be more than 1e18, so we can safely subtract it from 1e18 +// // as unchecked. +// else if ((1e18 - valueFrac) <= threshold) { +// value = UD60x18.unwrap(ceil(UD60x18.wrap(value))); +// assembly ("memory-safe") { +// mstore(stackTop, value) +// } +// } +// return stackTop; +// } +// } - /// Gas intensive reference implementation of snap-to-unit for testing. - function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) - internal - pure - returns (uint256[] memory) - { - uint256[] memory outputs = new uint256[](1); - uint256 threshold = inputs[0]; - uint256 value = inputs[1]; - uint256 valueFrac = UD60x18.unwrap(frac(UD60x18.wrap(value))); - if (valueFrac <= threshold) { - value = UD60x18.unwrap(floor(UD60x18.wrap(value))); - outputs[0] = value; - } else if ((1e18 - valueFrac) <= threshold) { - value = UD60x18.unwrap(ceil(UD60x18.wrap(value))); - outputs[0] = value; - } else { - outputs[0] = value; - } - return outputs; - } -} +// /// Gas intensive reference implementation of snap-to-unit for testing. +// function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) +// internal +// pure +// returns (uint256[] memory) +// { +// uint256[] memory outputs = new uint256[](1); +// uint256 threshold = inputs[0]; +// uint256 value = inputs[1]; +// uint256 valueFrac = UD60x18.unwrap(frac(UD60x18.wrap(value))); +// if (valueFrac <= threshold) { +// value = UD60x18.unwrap(floor(UD60x18.wrap(value))); +// outputs[0] = value; +// } else if ((1e18 - valueFrac) <= threshold) { +// value = UD60x18.unwrap(ceil(UD60x18.wrap(value))); +// outputs[0] = value; +// } else { +// outputs[0] = value; +// } +// return outputs; +// } +// } diff --git a/src/lib/op/math/LibOpSqrt.sol b/src/lib/op/math/LibOpSqrt.sol index c58f9b72..51a6064c 100644 --- a/src/lib/op/math/LibOpSqrt.sol +++ b/src/lib/op/math/LibOpSqrt.sol @@ -1,43 +1,43 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {UD60x18, sqrt} from "prb-math/UD60x18.sol"; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {UD60x18, sqrt} from "prb-math/UD60x18.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -/// @title LibOpSqrt -/// @notice Opcode for the square root of an decimal 18 fixed point number. -library LibOpSqrt { - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - // There must be one inputs and one output. - return (1, 1); - } +// /// @title LibOpSqrt +// /// @notice Opcode for the square root of an decimal 18 fixed point number. +// library LibOpSqrt { +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// // There must be one inputs and one output. +// return (1, 1); +// } - /// sqrt - /// 18 decimal fixed point square root of a number. - function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { - uint256 a; - assembly ("memory-safe") { - a := mload(stackTop) - } - a = UD60x18.unwrap(sqrt(UD60x18.wrap(a))); +// /// sqrt +// /// 18 decimal fixed point square root of a number. +// function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { +// uint256 a; +// assembly ("memory-safe") { +// a := mload(stackTop) +// } +// a = UD60x18.unwrap(sqrt(UD60x18.wrap(a))); - assembly ("memory-safe") { - mstore(stackTop, a) - } - return stackTop; - } +// assembly ("memory-safe") { +// mstore(stackTop, a) +// } +// return stackTop; +// } - /// Gas intensive reference implementation of sqrt for testing. - function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) - internal - pure - returns (uint256[] memory) - { - uint256[] memory outputs = new uint256[](1); - outputs[0] = UD60x18.unwrap(sqrt(UD60x18.wrap(inputs[0]))); - return outputs; - } -} +// /// Gas intensive reference implementation of sqrt for testing. +// function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) +// internal +// pure +// returns (uint256[] memory) +// { +// uint256[] memory outputs = new uint256[](1); +// outputs[0] = UD60x18.unwrap(sqrt(UD60x18.wrap(inputs[0]))); +// return outputs; +// } +// } diff --git a/src/lib/op/math/LibOpSub.sol b/src/lib/op/math/LibOpSub.sol index 79e44671..229c7da5 100644 --- a/src/lib/op/math/LibOpSub.sol +++ b/src/lib/op/math/LibOpSub.sol @@ -1,79 +1,79 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {SaturatingMath} from "rain.math.saturating/SaturatingMath.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {SaturatingMath} from "rain.math.saturating/SaturatingMath.sol"; -/// @title LibOpSub -/// @notice Opcode to subtract N integers. -library LibOpSub { - function integrity(IntegrityCheckStateNP 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 LibOpSub +// /// @notice Opcode to subtract N integers. +// library LibOpSub { +// function integrity(IntegrityCheckStateNP 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); +// } - function sub(uint256 a, uint256 b) internal pure returns (uint256) { - return a - b; - } +// function sub(uint256 a, uint256 b) internal pure returns (uint256) { +// return a - b; +// } - /// sub - /// Subtraction with implied overflow checks from the Solidity 0.8.x compiler. - function run(InterpreterStateNP memory, Operand operand, Pointer stackTop) internal pure returns (Pointer) { - uint256 a; - uint256 b; - uint256 saturate; - assembly ("memory-safe") { - a := mload(stackTop) - b := mload(add(stackTop, 0x20)) - stackTop := add(stackTop, 0x40) - saturate := and(operand, 1) - } - function (uint256, uint256) internal pure returns (uint256) f = - saturate > 0 ? SaturatingMath.saturatingSub : sub; - a = f(a, b); +// /// sub +// /// Subtraction with implied overflow checks from the Solidity 0.8.x compiler. +// function run(InterpreterStateNP memory, Operand operand, Pointer stackTop) internal pure returns (Pointer) { +// uint256 a; +// uint256 b; +// uint256 saturate; +// assembly ("memory-safe") { +// a := mload(stackTop) +// b := mload(add(stackTop, 0x20)) +// stackTop := add(stackTop, 0x40) +// saturate := and(operand, 1) +// } +// function (uint256, uint256) internal pure returns (uint256) f = +// saturate > 0 ? SaturatingMath.saturatingSub : sub; +// a = f(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 = f(a, b); - unchecked { - i++; - } - } - } +// { +// uint256 inputs = (Operand.unwrap(operand) >> 0x10) & 0x0F; +// uint256 i = 2; +// while (i < inputs) { +// assembly ("memory-safe") { +// b := mload(stackTop) +// stackTop := add(stackTop, 0x20) +// } +// a = f(a, b); +// unchecked { +// i++; +// } +// } +// } - assembly ("memory-safe") { - stackTop := sub(stackTop, 0x20) - mstore(stackTop, a) - } - return stackTop; - } +// assembly ("memory-safe") { +// stackTop := sub(stackTop, 0x20) +// mstore(stackTop, a) +// } +// return stackTop; +// } - /// Gas intensive reference implementation of subtraction for testing. - function referenceFn(InterpreterStateNP 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 acc = inputs[0]; - for (uint256 i = 1; i < inputs.length; i++) { - acc -= inputs[i]; - } - outputs = new uint256[](1); - outputs[0] = acc; - } - } -} +// /// Gas intensive reference implementation of subtraction for testing. +// function referenceFn(InterpreterStateNP 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 acc = inputs[0]; +// for (uint256 i = 1; i < inputs.length; i++) { +// acc -= inputs[i]; +// } +// outputs = new uint256[](1); +// outputs[0] = acc; +// } +// } +// } diff --git a/src/lib/op/math/growth/LibOpExponentialGrowth.sol b/src/lib/op/math/growth/LibOpExponentialGrowth.sol index f8f6c2cd..e107332e 100644 --- a/src/lib/op/math/growth/LibOpExponentialGrowth.sol +++ b/src/lib/op/math/growth/LibOpExponentialGrowth.sol @@ -1,49 +1,49 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {UD60x18, mul, pow} from "prb-math/UD60x18.sol"; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {InterpreterStateNP} from "../../../state/LibInterpreterStateNP.sol"; -import {IntegrityCheckStateNP} from "../../../integrity/LibIntegrityCheckNP.sol"; +// import {UD60x18, mul, pow} from "prb-math/UD60x18.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {InterpreterStateNP} from "../../../state/LibInterpreterStateNP.sol"; +// import {IntegrityCheckStateNP} from "../../../integrity/LibIntegrityCheckNP.sol"; -/// @title LibOpExponentialGrowth -/// @notice Exponential growth is base(1 + rate)^t where base is the initial -/// value, rate is the growth rate, and t is time. -library LibOpExponentialGrowth { - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - // There must be three inputs and one output. - return (3, 1); - } +// /// @title LibOpExponentialGrowth +// /// @notice Exponential growth is base(1 + rate)^t where base is the initial +// /// value, rate is the growth rate, and t is time. +// library LibOpExponentialGrowth { +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// // There must be three inputs and one output. +// return (3, 1); +// } - /// exponential-growth - function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { - uint256 base; - uint256 rate; - uint256 t; - assembly ("memory-safe") { - base := mload(stackTop) - rate := mload(add(stackTop, 0x20)) - stackTop := add(stackTop, 0x40) - t := mload(stackTop) - } - base = UD60x18.unwrap(mul(UD60x18.wrap(base), pow(UD60x18.wrap(1e18 + rate), UD60x18.wrap(t)))); +// /// exponential-growth +// function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { +// uint256 base; +// uint256 rate; +// uint256 t; +// assembly ("memory-safe") { +// base := mload(stackTop) +// rate := mload(add(stackTop, 0x20)) +// stackTop := add(stackTop, 0x40) +// t := mload(stackTop) +// } +// base = UD60x18.unwrap(mul(UD60x18.wrap(base), pow(UD60x18.wrap(1e18 + rate), UD60x18.wrap(t)))); - assembly ("memory-safe") { - mstore(stackTop, base) - } - return stackTop; - } +// assembly ("memory-safe") { +// mstore(stackTop, base) +// } +// return stackTop; +// } - /// Gas intensive reference implementation for testing. - function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) - internal - pure - returns (uint256[] memory) - { - uint256[] memory outputs = new uint256[](1); - outputs[0] = - UD60x18.unwrap(mul(UD60x18.wrap(inputs[0]), pow(UD60x18.wrap(1e18 + inputs[1]), UD60x18.wrap(inputs[2])))); - return outputs; - } -} +// /// Gas intensive reference implementation for testing. +// function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) +// internal +// pure +// returns (uint256[] memory) +// { +// uint256[] memory outputs = new uint256[](1); +// outputs[0] = +// UD60x18.unwrap(mul(UD60x18.wrap(inputs[0]), pow(UD60x18.wrap(1e18 + inputs[1]), UD60x18.wrap(inputs[2])))); +// return outputs; +// } +// } diff --git a/src/lib/op/math/growth/LibOpLinearGrowth.sol b/src/lib/op/math/growth/LibOpLinearGrowth.sol index b9cbcdba..cb29fc67 100644 --- a/src/lib/op/math/growth/LibOpLinearGrowth.sol +++ b/src/lib/op/math/growth/LibOpLinearGrowth.sol @@ -1,48 +1,48 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {UD60x18, mul, add} from "prb-math/UD60x18.sol"; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {InterpreterStateNP} from "../../../state/LibInterpreterStateNP.sol"; -import {IntegrityCheckStateNP} from "../../../integrity/LibIntegrityCheckNP.sol"; +// import {UD60x18, mul, add} from "prb-math/UD60x18.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {InterpreterStateNP} from "../../../state/LibInterpreterStateNP.sol"; +// import {IntegrityCheckStateNP} from "../../../integrity/LibIntegrityCheckNP.sol"; -/// @title LibOpLinearGrowth -/// @notice Linear growth is base + rate * t where a is the initial value, r is -/// the growth rate, and t is time. -library LibOpLinearGrowth { - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - // There must be three inputs and one output. - return (3, 1); - } +// /// @title LibOpLinearGrowth +// /// @notice Linear growth is base + rate * t where a is the initial value, r is +// /// the growth rate, and t is time. +// library LibOpLinearGrowth { +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// // There must be three inputs and one output. +// return (3, 1); +// } - /// linear-growth - function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { - uint256 base; - uint256 rate; - uint256 t; - assembly ("memory-safe") { - base := mload(stackTop) - rate := mload(add(stackTop, 0x20)) - stackTop := add(stackTop, 0x40) - t := mload(stackTop) - } - base = UD60x18.unwrap(add(UD60x18.wrap(base), mul(UD60x18.wrap(rate), UD60x18.wrap(t)))); +// /// linear-growth +// function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { +// uint256 base; +// uint256 rate; +// uint256 t; +// assembly ("memory-safe") { +// base := mload(stackTop) +// rate := mload(add(stackTop, 0x20)) +// stackTop := add(stackTop, 0x40) +// t := mload(stackTop) +// } +// base = UD60x18.unwrap(add(UD60x18.wrap(base), mul(UD60x18.wrap(rate), UD60x18.wrap(t)))); - assembly ("memory-safe") { - mstore(stackTop, base) - } - return stackTop; - } +// assembly ("memory-safe") { +// mstore(stackTop, base) +// } +// return stackTop; +// } - /// Gas intensive reference implementation for testing. - function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) - internal - pure - returns (uint256[] memory) - { - uint256[] memory outputs = new uint256[](1); - outputs[0] = UD60x18.unwrap(add(UD60x18.wrap(inputs[0]), mul(UD60x18.wrap(inputs[1]), UD60x18.wrap(inputs[2])))); - return outputs; - } -} +// /// Gas intensive reference implementation for testing. +// function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) +// internal +// pure +// returns (uint256[] memory) +// { +// uint256[] memory outputs = new uint256[](1); +// outputs[0] = UD60x18.unwrap(add(UD60x18.wrap(inputs[0]), mul(UD60x18.wrap(inputs[1]), UD60x18.wrap(inputs[2])))); +// return outputs; +// } +// } diff --git a/src/lib/parse/LibParseCMask.sol b/src/lib/parse/LibParseCMask.sol index ee30d438..d5ce4f05 100644 --- a/src/lib/parse/LibParseCMask.sol +++ b/src/lib/parse/LibParseCMask.sol @@ -471,8 +471,8 @@ uint128 constant CMASK_WHITESPACE = CMASK_LINE_FEED | CMASK_CARRIAGE_RETURN | CM /// @dev Rainlang stack item delimiter is whitespace uint128 constant CMASK_LHS_STACK_DELIMITER = CMASK_WHITESPACE; -/// @dev Rainlang supports numeric literals as anything starting with 0-9 -uint128 constant CMASK_NUMERIC_LITERAL_HEAD = CMASK_NUMERIC_0_9; +/// @dev Rainlang supports numeric literals as anything starting with 0-9 or - +uint128 constant CMASK_NUMERIC_LITERAL_HEAD = CMASK_NUMERIC_0_9 | CMASK_NEGATIVE_SIGN; /// @dev Rainlang supports string literals as anything starting with " uint128 constant CMASK_STRING_LITERAL_HEAD = CMASK_QUOTATION_MARK; diff --git a/src/lib/parse/LibParseOperand.sol b/src/lib/parse/LibParseOperand.sol index e5fc6723..161aa304 100644 --- a/src/lib/parse/LibParseOperand.sol +++ b/src/lib/parse/LibParseOperand.sol @@ -6,7 +6,8 @@ import { UnclosedOperand, OperandValuesOverflow, UnexpectedOperand, - UnexpectedOperandValue + UnexpectedOperandValue, + OperandOverflow } from "../../error/ErrParse.sol"; import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; import {LibParse} from "./LibParse.sol"; @@ -15,7 +16,7 @@ import {CMASK_OPERAND_END, CMASK_WHITESPACE, CMASK_OPERAND_START} from "./LibPar import {ParseState, OPERAND_VALUES_LENGTH, FSM_YANG_MASK} from "./LibParseState.sol"; import {LibParseError} from "./LibParseError.sol"; import {LibParseInterstitial} from "./LibParseInterstitial.sol"; -import {LibFixedPointDecimalScale, DECIMAL_MAX_SAFE_INT} from "rain.math.fixedpoint/lib/LibFixedPointDecimalScale.sol"; +import {LibDecimalFloat} from "rain.math.float/src/lib/LibDecimalFloat.sol"; library LibParseOperand { using LibParseError for ParseState; @@ -157,9 +158,11 @@ library LibParseOperand { assembly ("memory-safe") { operand := mload(add(values, 0x20)) } - operand = Operand.wrap( - LibFixedPointDecimalScale.decimalOrIntToInt(Operand.unwrap(operand), uint256(type(uint16).max)) - ); + (int256 signedCoefficient, int256 exponent) = LibDecimalFloat.unpack(Operand.unwrap(operand)); + operand = Operand.wrap(LibDecimalFloat.toFixedDecimalLossless(signedCoefficient, exponent, 0)); + if (Operand.unwrap(operand) > type(uint16).max) { + revert OperandOverflow(); + } } else if (values.length == 0) { operand = Operand.wrap(0); } else { @@ -174,9 +177,11 @@ library LibParseOperand { assembly ("memory-safe") { operand := mload(add(values, 0x20)) } - operand = Operand.wrap( - LibFixedPointDecimalScale.decimalOrIntToInt(Operand.unwrap(operand), uint256(type(uint16).max)) - ); + (int256 signedCoefficient, int256 exponent) = LibDecimalFloat.unpack(Operand.unwrap(operand)); + operand = Operand.wrap(LibDecimalFloat.toFixedDecimalLossless(signedCoefficient, exponent, 0)); + if (Operand.unwrap(operand) > type(uint16).max) { + revert OperandOverflow(); + } } else if (values.length == 0) { revert ExpectedOperand(); } else { @@ -195,8 +200,14 @@ library LibParseOperand { a := mload(add(values, 0x20)) b := mload(add(values, 0x40)) } - a = LibFixedPointDecimalScale.decimalOrIntToInt(a, type(uint8).max); - b = LibFixedPointDecimalScale.decimalOrIntToInt(b, type(uint8).max); + (int256 signedCoefficient, int256 exponent) = LibDecimalFloat.unpack(a); + a = LibDecimalFloat.toFixedDecimalLossless(signedCoefficient, exponent, 0); + (signedCoefficient, exponent) = LibDecimalFloat.unpack(b); + b = LibDecimalFloat.toFixedDecimalLossless(signedCoefficient, exponent, 0); + + if (a > type(uint8).max || b > type(uint8).max) { + revert OperandOverflow(); + } operand = Operand.wrap(a | (b << 8)); } else if (values.length < 2) { @@ -235,9 +246,16 @@ library LibParseOperand { c = 0; } - a = LibFixedPointDecimalScale.decimalOrIntToInt(a, type(uint8).max); - b = LibFixedPointDecimalScale.decimalOrIntToInt(b, 1); - c = LibFixedPointDecimalScale.decimalOrIntToInt(c, 1); + (int256 signedCoefficient, int256 exponent) = LibDecimalFloat.unpack(a); + a = LibDecimalFloat.toFixedDecimalLossless(signedCoefficient, exponent, 0); + (signedCoefficient, exponent) = LibDecimalFloat.unpack(b); + b = LibDecimalFloat.toFixedDecimalLossless(signedCoefficient, exponent, 0); + (signedCoefficient, exponent) = LibDecimalFloat.unpack(c); + c = LibDecimalFloat.toFixedDecimalLossless(signedCoefficient, exponent, 0); + + if (a > type(uint8).max || b > 1 || c > 1) { + revert OperandOverflow(); + } operand = Operand.wrap(a | (b << 8) | (c << 9)); } else if (length == 0) { @@ -271,8 +289,14 @@ library LibParseOperand { b = 0; } - a = LibFixedPointDecimalScale.decimalOrIntToInt(a, 1); - b = LibFixedPointDecimalScale.decimalOrIntToInt(b, 1); + (int256 signedCoefficient, int256 exponent) = LibDecimalFloat.unpack(a); + a = LibDecimalFloat.toFixedDecimalLossless(signedCoefficient, exponent, 0); + (signedCoefficient, exponent) = LibDecimalFloat.unpack(b); + b = LibDecimalFloat.toFixedDecimalLossless(signedCoefficient, exponent, 0); + + if (a > 1 || b > 1) { + revert OperandOverflow(); + } operand = Operand.wrap(a | (b << 1)); } else { diff --git a/src/lib/parse/LibSubParse.sol b/src/lib/parse/LibSubParse.sol index 0b439d80..6bee0a88 100644 --- a/src/lib/parse/LibSubParse.sol +++ b/src/lib/parse/LibSubParse.sol @@ -10,7 +10,7 @@ import { Operand } from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; import {LibBytecode, Pointer} from "rain.interpreter.interface/lib/bytecode/LibBytecode.sol"; -import {ISubParserV3, COMPATIBILITY_V4} from "rain.interpreter.interface/interface/ISubParserV3.sol"; +import {ISubParserV3, COMPATIBILITY_V5} from "rain.interpreter.interface/interface/ISubParserV3.sol"; import {BadSubParserResult, UnknownWord, UnsupportedLiteralType} from "../../error/ErrParse.sol"; import {LibExtern, EncodedExternDispatch} from "../extern/LibExtern.sol"; import {IInterpreterExternV3} from "rain.interpreter.interface/interface/IInterpreterExternV3.sol"; @@ -18,6 +18,8 @@ import {ExternDispatchConstantsHeightOverflow} from "../../error/ErrSubParse.sol import {LibMemCpy} from "rain.solmem/lib/LibMemCpy.sol"; import {LibParseError} from "./LibParseError.sol"; +bytes32 constant CURRENT_COMPATIBILITY = COMPATIBILITY_V5; + library LibSubParse { using LibParseState for ParseState; using LibParseError for ParseState; @@ -203,7 +205,7 @@ library LibSubParse { } (bool success, bytes memory subBytecode, uint256[] memory subConstants) = - subParser.subParseWord(COMPATIBILITY_V4, data); + subParser.subParseWord(CURRENT_COMPATIBILITY, data); if (success) { // The sub bytecode must be exactly 4 bytes to // represent an op. @@ -310,7 +312,7 @@ library LibSubParse { deref := mload(shr(0xf0, deref)) } - (bool success, uint256 value) = subParser.subParseLiteral(COMPATIBILITY_V4, data); + (bool success, uint256 value) = subParser.subParseLiteral(CURRENT_COMPATIBILITY, data); if (success) { return value; } diff --git a/src/lib/parse/literal/LibParseLiteral.sol b/src/lib/parse/literal/LibParseLiteral.sol index bc96453d..a470d30a 100644 --- a/src/lib/parse/literal/LibParseLiteral.sol +++ b/src/lib/parse/literal/LibParseLiteral.sol @@ -31,7 +31,6 @@ import {ParseState} from "../LibParseState.sol"; import {LibParseError} from "../LibParseError.sol"; import {LibParseInterstitial} from "../LibParseInterstitial.sol"; import {LibSubParse} from "../LibSubParse.sol"; -import {LibFixedPointDecimalScale} from "rain.math.fixedpoint/lib/LibFixedPointDecimalScale.sol"; uint256 constant LITERAL_PARSERS_LENGTH = 4; diff --git a/src/lib/parse/literal/LibParseLiteralDecimal.sol b/src/lib/parse/literal/LibParseLiteralDecimal.sol index 6825ae69..21baed77 100644 --- a/src/lib/parse/literal/LibParseLiteralDecimal.sol +++ b/src/lib/parse/literal/LibParseLiteralDecimal.sol @@ -18,6 +18,7 @@ import { } from "../LibParseCMask.sol"; import {LibParseError} from "../LibParseError.sol"; import {LibParse} from "../LibParse.sol"; +import {LibDecimalFloatImplementation, LibDecimalFloat} from "rain.math.float/src/lib/LibDecimalFloat.sol"; /// @dev The default is 18 decimal places for a fractional number. uint256 constant DECIMAL_SCALE = 18; @@ -115,6 +116,32 @@ library LibParseLiteralDecimal { } } + function parseDecimalFloatPacked(ParseState memory state, uint256 start, uint256 end) + internal + pure + returns (uint256 cursor, uint256 packedFloat) + { + int256 signedCoefficient; + int256 exponent; + (cursor, signedCoefficient, exponent) = parseDecimalFloat(state, start, end); + + // Prenormalize signed coefficients that are smaller than their + // normalized form at parse time, as this can save runtime gas that would + // be needed to normalize the value at runtime. + // We only do normalization that will scale up, to avoid causing + // unneccessary precision loss. + if (-1e37 < signedCoefficient && signedCoefficient < 1e37) { + (signedCoefficient, exponent) = LibDecimalFloatImplementation.normalize(signedCoefficient, exponent); + } + + packedFloat = LibDecimalFloat.pack(signedCoefficient, exponent); + + (int256 unpackedSignedCoefficient, int256 unpackedExponent) = LibDecimalFloat.unpack(packedFloat); + if (unpackedSignedCoefficient != signedCoefficient || unpackedExponent != exponent) { + revert DecimalLiteralPrecisionLoss(state.parseErrorOffset(start)); + } + } + function parseDecimalFloat(ParseState memory state, uint256 start, uint256 end) internal pure @@ -189,87 +216,4 @@ library LibParseLiteralDecimal { } } } - - /// Returns cursor after, value - function parseDecimal(ParseState memory state, uint256 cursor, uint256 end) - internal - pure - returns (uint256, uint256) - { - uint256 fracOffset = 0; - uint256 fracValue = 0; - uint256 scale = 0; - uint256 intValue = 0; - - unchecked { - { - uint256 start = cursor; - cursor = LibParse.skipMask(cursor, end, CMASK_NUMERIC_0_9); - if (cursor == start) { - revert ZeroLengthDecimal(state.parseErrorOffset(cursor)); - } - intValue = state.unsafeStrToInt(start, cursor); - } - - uint256 isFrac = LibParse.isMask(cursor, end, CMASK_DECIMAL_POINT); - if (isFrac == 1) { - cursor++; - uint256 fracStart = cursor; - cursor = LibParse.skipMask(cursor, end, CMASK_NUMERIC_0_9); - if (cursor == fracStart) { - revert MalformedDecimalPoint(state.parseErrorOffset(cursor)); - } - - // Trailing zeros are allowed in fractional literals but should - // not be counted in the precision. - uint256 nonZeroCursor = cursor; - while (LibParse.isMask(nonZeroCursor - 1, end, CMASK_ZERO) == 1) { - nonZeroCursor--; - } - - fracValue = state.unsafeStrToInt(fracStart, nonZeroCursor); - fracOffset = nonZeroCursor - fracStart; - } - - uint256 eValue = 0; - uint256 eNeg = 0; - if (LibParse.isMask(cursor, end, CMASK_E_NOTATION) > 0) { - cursor++; - eNeg = LibParse.isMask(cursor, end, CMASK_NEGATIVE_SIGN); - cursor += eNeg; - - uint256 eStart = cursor; - cursor = LibParse.skipMask(cursor, end, CMASK_NUMERIC_0_9); - if (cursor == eStart) { - revert MalformedExponentDigits(state.parseErrorOffset(cursor)); - } - eValue = state.unsafeStrToInt(eStart, cursor); - } - - { - // If this is a fractional number, then we need to scale it up to - // 1e18 being "one". Otherwise we treat as an integer. - if (eNeg > 0) { - if (DECIMAL_SCALE < eValue) { - revert DecimalLiteralPrecisionLoss(state.parseErrorOffset(cursor)); - } - scale = DECIMAL_SCALE - eValue; - } else { - scale = DECIMAL_SCALE + eValue; - } - } - - if (scale >= 77) { - revert DecimalLiteralOverflow(state.parseErrorOffset(cursor)); - } - - if (scale < fracOffset) { - revert DecimalLiteralPrecisionLoss(state.parseErrorOffset(cursor)); - } - } - - // Do this bit with checked math in case we missed an edge case above - // that causes overflow. - return (cursor, intValue * (10 ** scale) + fracValue * (10 ** (scale - fracOffset))); - } } diff --git a/test/src/abstract/BaseRainterpreterSubParserNPE2.compatibility.t.sol b/test/src/abstract/BaseRainterpreterSubParserNPE2.compatibility.t.sol index 71fac9d0..c608ad83 100644 --- a/test/src/abstract/BaseRainterpreterSubParserNPE2.compatibility.t.sol +++ b/test/src/abstract/BaseRainterpreterSubParserNPE2.compatibility.t.sol @@ -3,7 +3,7 @@ pragma solidity =0.8.25; import {Test} from "forge-std/Test.sol"; import {IERC165} from "openzeppelin-contracts/contracts/utils/introspection/IERC165.sol"; -import {BaseRainterpreterSubParserNPE2, COMPATIBILITY_V4} from "src/abstract/BaseRainterpreterSubParserNPE2.sol"; +import {BaseRainterpreterSubParserNPE2, CURRENT_COMPATIBILITY} from "src/abstract/BaseRainterpreterSubParserNPE2.sol"; import {ISubParserV3} from "rain.interpreter.interface/interface/ISubParserV3.sol"; import {IncompatibleSubParser} from "src/error/ErrSubParse.sol"; @@ -31,7 +31,7 @@ contract ChildRainterpreterSubParserNPE2 is BaseRainterpreterSubParserNPE2 { contract BaseRainterpreterSubParserNPE2CompatibilityTest is Test { /// Test that any compatibility ID other than the correct one will revert. function testRainterpreterSubParserNPE2Compatibility(bytes32 badCompatibility, bytes memory data) external { - vm.assume(badCompatibility != COMPATIBILITY_V4); + vm.assume(badCompatibility != CURRENT_COMPATIBILITY); ChildRainterpreterSubParserNPE2 subParser = new ChildRainterpreterSubParserNPE2(); vm.expectRevert(abi.encodeWithSelector(IncompatibleSubParser.selector)); diff --git a/test/src/concrete/RainterpreterNPE2.stateOverlay.t.sol b/test/src/concrete/RainterpreterNPE2.stateOverlay.t.sol index abad6499..348f55cd 100644 --- a/test/src/concrete/RainterpreterNPE2.stateOverlay.t.sol +++ b/test/src/concrete/RainterpreterNPE2.stateOverlay.t.sol @@ -6,13 +6,14 @@ import {RainterpreterExpressionDeployerNPE2DeploymentTest} from import {FullyQualifiedNamespace, StateNamespace} from "rain.interpreter.interface/interface/IInterpreterStoreV2.sol"; import {EvalV4, SourceIndexV2} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; import {LibNamespace} from "rain.interpreter.interface/lib/ns/LibNamespace.sol"; +import {LibDecimalFloat} from "rain.math.float/src/lib/LibDecimalFloat.sol"; contract RainterpreterNPE2StateOverlayTest is RainterpreterExpressionDeployerNPE2DeploymentTest { /// Show that state overlay can prewarm a get. - function testStateOverlayGet() external { + function testStateOverlayGet() external view { bytes memory bytecode = iDeployer.parse2("_: get(9);"); - uint256 k = 9e18; + uint256 k = LibDecimalFloat.pack(9e37, -37); uint256 v = 42; uint256[] memory stateOverlay = new uint256[](2); stateOverlay[0] = k; @@ -38,11 +39,11 @@ contract RainterpreterNPE2StateOverlayTest is RainterpreterExpressionDeployerNPE } /// Show that state overlay can be overridden by a set in the bytecode. - function testStateOverlaySet() external { + function testStateOverlaySet() external view { bytes memory bytecode = iDeployer.parse2("_:get(9),:set(9 42),_:get(9);"); - uint256 k = 9e18; - uint256 v = 43e18; + uint256 k = LibDecimalFloat.pack(9e37, -37); + uint256 v = LibDecimalFloat.pack(43e37, -37); uint256[] memory stateOverlay = new uint256[](2); stateOverlay[0] = k; stateOverlay[1] = v; @@ -60,10 +61,10 @@ contract RainterpreterNPE2StateOverlayTest is RainterpreterExpressionDeployerNPE ); assertEq(stack.length, 2); - assertEq(stack[0], 42e18); + assertEq(stack[0], LibDecimalFloat.pack(42e37, -37)); assertEq(stack[1], v); assertEq(kvs.length, 2); assertEq(kvs[0], k); - assertEq(kvs[1], 42e18); + assertEq(kvs[1], LibDecimalFloat.pack(42e37, -37)); } } diff --git a/test/src/concrete/RainterpreterReferenceExternNPE2.intInc.t.sol b/test/src/concrete/RainterpreterReferenceExternNPE2.intInc.t.sol index 960e3ee9..88fd7ebf 100644 --- a/test/src/concrete/RainterpreterReferenceExternNPE2.intInc.t.sol +++ b/test/src/concrete/RainterpreterReferenceExternNPE2.intInc.t.sol @@ -17,9 +17,10 @@ import { import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; import {LibExtern} from "src/lib/extern/LibExtern.sol"; import {Strings} from "openzeppelin-contracts/contracts/utils/Strings.sol"; -import {COMPATIBILITY_V4} from "rain.interpreter.interface/interface/ISubParserV3.sol"; import {OPCODE_EXTERN} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; import {ExternDispatchConstantsHeightOverflow} from "src/error/ErrSubParse.sol"; +import {LibDecimalFloat} from "rain.math.float/src/lib/LibDecimalFloat.sol"; +import {CURRENT_COMPATIBILITY} from "src/lib/parse/LibSubParse.sol"; contract RainterpreterReferenceExternNPE2IntIncTest is OpTest { using Strings for address; @@ -38,15 +39,15 @@ contract RainterpreterReferenceExternNPE2IntIncTest is OpTest { ); uint256[] memory expectedStack = new uint256[](3); - expectedStack[0] = 4; - expectedStack[1] = 3; + expectedStack[0] = LibDecimalFloat.pack(4e37, -37); + expectedStack[1] = LibDecimalFloat.pack(3e37, -37); expectedStack[2] = EncodedExternDispatch.unwrap(encodedExternDispatch); checkHappy( // Need the constant in the constant array to be indexable in the operand. "_: 0x000000000000000000000000c7183455a4c133ae270771860664b6b7ec320bb1," // Operand is the constant index of the dispatch. - "three four: extern<0>(2e-18 3e-18);", + "three four: extern<0>(2 3);", expectedStack, "inc 2 3 = 3 4" ); @@ -56,14 +57,12 @@ contract RainterpreterReferenceExternNPE2IntIncTest is OpTest { RainterpreterReferenceExternNPE2 extern = new RainterpreterReferenceExternNPE2(); uint256[] memory expectedStack = new uint256[](2); - expectedStack[0] = 4; - expectedStack[1] = 3; + expectedStack[0] = LibDecimalFloat.pack(4e37, -37); + expectedStack[1] = LibDecimalFloat.pack(3e37, -37); checkHappy( bytes( - string.concat( - "using-words-from ", address(extern).toHexString(), " three four: ref-extern-inc(2e-18 3e-18);" - ) + string.concat("using-words-from ", address(extern).toHexString(), " three four: ref-extern-inc(2 3);") ), expectedStack, "sugared inc 2 3 = 3 4" @@ -80,7 +79,7 @@ contract RainterpreterReferenceExternNPE2IntIncTest is OpTest { bytes memory wordToParse = bytes("ref-extern-inc"); (bool success, bytes memory bytecode, uint256[] memory constants) = subParser.subParseWord( - COMPATIBILITY_V4, + CURRENT_COMPATIBILITY, bytes.concat(bytes2(constantsHeight), ioByte, bytes2(uint16(wordToParse.length)), wordToParse, bytes32(0)) ); assertTrue(success); @@ -120,7 +119,7 @@ contract RainterpreterReferenceExternNPE2IntIncTest is OpTest { RainterpreterReferenceExternNPE2 subParser = new RainterpreterReferenceExternNPE2(); (bool success, bytes memory bytecode, uint256[] memory constants) = subParser.subParseWord( - COMPATIBILITY_V4, + CURRENT_COMPATIBILITY, bytes.concat(bytes2(constantsHeight), ioByte, bytes2(uint16(unknownWord.length)), unknownWord, bytes32(0)) ); assertFalse(success); @@ -133,8 +132,10 @@ contract RainterpreterReferenceExternNPE2IntIncTest is OpTest { function testRainterpreterReferenceExternNPE2IntIncRun(Operand operand, uint256[] memory inputs) external pure { uint256[] memory expectedOutputs = new uint256[](inputs.length); for (uint256 i = 0; i < inputs.length; i++) { - vm.assume(inputs[i] < type(uint256).max); - expectedOutputs[i] = inputs[i] + 1; + inputs[i] = bound(inputs[i], 0, uint256(int256(type(int128).max))); + (int256 signedCoefficient, int256 exponent) = LibDecimalFloat.unpack(inputs[i]); + (signedCoefficient, exponent) = LibDecimalFloat.add(signedCoefficient, exponent, 1e37, -37); + expectedOutputs[i] = LibDecimalFloat.pack(signedCoefficient, exponent); } uint256[] memory actualOutputs = LibExternOpIntIncNPE2.run(operand, inputs); diff --git a/test/src/lib/op/00/LibOpConstantNP.t.sol b/test/src/lib/op/00/LibOpConstantNP.t.sol index b6da9ca4..9ca735cf 100644 --- a/test/src/lib/op/00/LibOpConstantNP.t.sol +++ b/test/src/lib/op/00/LibOpConstantNP.t.sol @@ -18,6 +18,7 @@ import {IInterpreterStoreV2} from "rain.interpreter.interface/interface/IInterpr import {SignedContextV1} from "rain.interpreter.interface/interface/IInterpreterCallerV3.sol"; import {LibOperand} from "test/lib/operand/LibOperand.sol"; import {BadOpOutputsLength} from "src/error/ErrIntegrity.sol"; +import {LibDecimalFloat} from "rain.math.float/src/lib/LibDecimalFloat.sol"; /// @title LibOpConstantNPTest /// @notice Test the runtime and integrity time logic of LibOpConstantNP. @@ -95,8 +96,8 @@ contract LibOpConstantNPTest is OpTest { }) ); assertEq(stack.length, 2); - assertEq(stack[0], 1001e15); - assertEq(stack[1], 2e18); + assertEq(stack[0], LibDecimalFloat.pack(1.001e37, -37)); + assertEq(stack[1], LibDecimalFloat.pack(2e37, -37)); assertEq(kvs.length, 0); } diff --git a/test/src/lib/op/00/LibOpExternNP.t.sol b/test/src/lib/op/00/LibOpExternNP.t.sol index 71bfa68a..6bbe475b 100644 --- a/test/src/lib/op/00/LibOpExternNP.t.sol +++ b/test/src/lib/op/00/LibOpExternNP.t.sol @@ -17,6 +17,7 @@ import { import {IERC165} from "openzeppelin-contracts/contracts/utils/introspection/IERC165.sol"; import {LibOperand} from "test/lib/operand/LibOperand.sol"; import {LibUint256Array} from "rain.solmem/lib/LibUint256Array.sol"; +import {LibDecimalFloat} from "rain.math.float/src/lib/LibDecimalFloat.sol"; /// @title LibOpExternNPTest /// @notice Test the runtime and integrity time logic of LibOpExternNP. @@ -211,10 +212,10 @@ contract LibOpExternNPTest is OpTest { ); uint256[] memory externInputs = new uint256[](2); - externInputs[0] = 20; - externInputs[1] = 83; + externInputs[0] = LibDecimalFloat.pack(20e36, -36); + externInputs[1] = LibDecimalFloat.pack(83e36, -36); uint256[] memory externOutputs = new uint256[](1); - externOutputs[0] = 99; + externOutputs[0] = LibDecimalFloat.pack(99e36, -36); vm.mockCall( address(extern), abi.encodeWithSelector(IInterpreterExternV3.extern.selector, externDispatch, externInputs), @@ -222,7 +223,7 @@ contract LibOpExternNPTest is OpTest { ); uint256[] memory expectedStack = new uint256[](2); - expectedStack[0] = 99; + expectedStack[0] = LibDecimalFloat.pack(99e36, -36); expectedStack[1] = 0x00000000000000000005001000000000000000000000000000000000deadbeef; checkHappy( @@ -230,7 +231,7 @@ contract LibOpExternNPTest is OpTest { "_: 0x00000000000000000005001000000000000000000000000000000000deadbeef," // Operand is the constant index of the dispatch then the number of outputs. // 2 inputs and 1 output matches the mocked integrity check. - "_: extern<0>(20e-18 83e-18);", + "_: extern<0>(20 83);", expectedStack, "0xdeadbeef 20 83 99" ); @@ -264,19 +265,19 @@ contract LibOpExternNPTest is OpTest { ); uint256[] memory externInputs = new uint256[](3); - externInputs[0] = 1; - externInputs[1] = 2; - externInputs[2] = 3; + externInputs[0] = LibDecimalFloat.pack(1e37, -37); + externInputs[1] = LibDecimalFloat.pack(2e37, -37); + externInputs[2] = LibDecimalFloat.pack(3e37, -37); uint256[] memory externOutputs = new uint256[](3); - externOutputs[0] = 4; - externOutputs[1] = 5; - externOutputs[2] = 6; + externOutputs[0] = LibDecimalFloat.pack(4e37, -37); + externOutputs[1] = LibDecimalFloat.pack(5e37, -37); + externOutputs[2] = LibDecimalFloat.pack(6e37, -37); uint256[] memory expectedStack = new uint256[](4); - expectedStack[0] = 6; - expectedStack[1] = 5; - expectedStack[2] = 4; + expectedStack[0] = LibDecimalFloat.pack(6e37, -37); + expectedStack[1] = LibDecimalFloat.pack(5e37, -37); + expectedStack[2] = LibDecimalFloat.pack(4e37, -37); expectedStack[3] = 0x00000000000000000005001000000000000000000000000000000000deadbeef; vm.mockCall( @@ -295,7 +296,7 @@ contract LibOpExternNPTest is OpTest { "_: 0x00000000000000000005001000000000000000000000000000000000deadbeef," // Operand is the constant index of the dispatch then the number of outputs. // 3 inputs and 3 outputs matches the mocked integrity check. - "four five six: extern<0>(1e-18 2e-18 3e-18);", + "four five six: extern<0>(1 2 3);", expectedStack, "0xdeadbeef 1 2 3 4 5 6" ); diff --git a/test/src/lib/op/00/LibOpStackNP.t.sol b/test/src/lib/op/00/LibOpStackNP.t.sol index 738af12c..077cf7d1 100644 --- a/test/src/lib/op/00/LibOpStackNP.t.sol +++ b/test/src/lib/op/00/LibOpStackNP.t.sol @@ -21,6 +21,7 @@ import {OpTest, PRE, POST} from "test/abstract/OpTest.sol"; import {SignedContextV1} from "rain.interpreter.interface/interface/IInterpreterCallerV3.sol"; import {LibOperand} from "test/lib/operand/LibOperand.sol"; import {BadOpOutputsLength} from "src/error/ErrIntegrity.sol"; +import {LibDecimalFloat} from "rain.math.float/src/lib/LibDecimalFloat.sol"; /// @title LibOpStackNPTest /// @notice Test the runtime and integrity time logic of LibOpStackNP. @@ -128,7 +129,7 @@ contract LibOpStackNPTest is OpTest { /// Test the eval of a stack opcode parsed from a string. function testOpStackEval() external view { - bytes memory bytecode = iDeployer.parse2("foo: 1, bar: foo;"); + bytes memory bytecode = iDeployer.parse2("foo: 1, bar: foo, _: -1;"); (uint256[] memory stack, uint256[] memory kvs) = iInterpreter.eval4( EvalV4({ store: iStore, @@ -140,9 +141,10 @@ contract LibOpStackNPTest is OpTest { stateOverlay: new uint256[](0) }) ); - assertEq(stack.length, 2); - assertEq(stack[0], 1e18); - assertEq(stack[1], 1e18); + assertEq(stack.length, 3); + assertEq(stack[0], LibDecimalFloat.pack(-1e37, -37)); + assertEq(stack[1], stack[2]); + assertEq(stack[2], LibDecimalFloat.pack(1e37, -37)); assertEq(kvs.length, 0); } @@ -162,12 +164,12 @@ contract LibOpStackNPTest is OpTest { }) ); assertEq(stack.length, 6); - assertEq(stack[0], 1e18); - assertEq(stack[1], 1e18); - assertEq(stack[2], 1e18); - assertEq(stack[3], 1e18); - assertEq(stack[4], 1e18); - assertEq(stack[5], 1e18); + assertEq(stack[0], LibDecimalFloat.pack(1e37, -37)); + assertEq(stack[1], LibDecimalFloat.pack(1e37, -37)); + assertEq(stack[2], LibDecimalFloat.pack(1e37, -37)); + assertEq(stack[3], LibDecimalFloat.pack(1e37, -37)); + assertEq(stack[4], LibDecimalFloat.pack(1e37, -37)); + assertEq(stack[5], LibDecimalFloat.pack(1e37, -37)); assertEq(kvs.length, 0); } diff --git a/test/src/lib/op/bitwise/LibOpBitwiseAndNP.t.sol b/test/src/lib/op/bitwise/LibOpBitwiseAndNP.t.sol index 7be891cd..9c2f7ea8 100644 --- a/test/src/lib/op/bitwise/LibOpBitwiseAndNP.t.sol +++ b/test/src/lib/op/bitwise/LibOpBitwiseAndNP.t.sol @@ -33,22 +33,22 @@ contract LibOpBitwiseAndNPTest is OpTest { /// Test the eval of bitwise AND parsed from a string. function testOpBitwiseAndNPEvalHappy() external view { - checkHappy("_: bitwise-and(0 0);", 0, "0 0"); - checkHappy("_: bitwise-and(0 1e-18);", 0, "0 1"); - checkHappy("_: bitwise-and(1e-18 0);", 0, "1 0"); - checkHappy("_: bitwise-and(1e-18 1e-18);", 1, "1 1"); - checkHappy("_: bitwise-and(0 2e-18);", 0, "0 2"); - checkHappy("_: bitwise-and(2e-18 0);", 0, "2 0"); - checkHappy("_: bitwise-and(1e-18 2e-18);", 0, "1 2"); - checkHappy("_: bitwise-and(2e-18 1e-18);", 0, "2 1"); - checkHappy("_: bitwise-and(2e-18 2e-18);", 2, "2 2"); - checkHappy("_: bitwise-and(0 3e-18);", 0, "0 3"); - checkHappy("_: bitwise-and(3e-18 0);", 0, "3 0"); - checkHappy("_: bitwise-and(1e-18 3e-18);", 1, "1 3"); - checkHappy("_: bitwise-and(3e-18 1e-18);", 1, "3 1"); - checkHappy("_: bitwise-and(2e-18 3e-18);", 2, "2 3"); - checkHappy("_: bitwise-and(3e-18 2e-18);", 2, "3 2"); - checkHappy("_: bitwise-and(3e-18 3e-18);", 3, "3 3"); + checkHappy("_: bitwise-and(0x00 0x00);", 0, "0 0"); + checkHappy("_: bitwise-and(0x00 0x01);", 0, "0 1"); + checkHappy("_: bitwise-and(0x01 0x00);", 0, "1 0"); + checkHappy("_: bitwise-and(0x01 0x01);", 1, "1 1"); + checkHappy("_: bitwise-and(0x00 0x02);", 0, "0 2"); + checkHappy("_: bitwise-and(0x02 0x00);", 0, "2 0"); + checkHappy("_: bitwise-and(0x01 0x02);", 0, "1 2"); + checkHappy("_: bitwise-and(0x02 0x01);", 0, "2 1"); + checkHappy("_: bitwise-and(0x02 0x02);", 2, "2 2"); + checkHappy("_: bitwise-and(0x00 0x03);", 0, "0 3"); + checkHappy("_: bitwise-and(0x03 0x00);", 0, "3 0"); + checkHappy("_: bitwise-and(0x01 0x03);", 1, "1 3"); + checkHappy("_: bitwise-and(0x03 0x01);", 1, "3 1"); + checkHappy("_: bitwise-and(0x02 0x03);", 2, "2 3"); + checkHappy("_: bitwise-and(0x03 0x02);", 2, "3 2"); + checkHappy("_: bitwise-and(0x03 0x03);", 3, "3 3"); } /// Test that a bitwise OR with bad inputs fails integrity. diff --git a/test/src/lib/op/bitwise/LibOpBitwiseOrNP.t.sol b/test/src/lib/op/bitwise/LibOpBitwiseOrNP.t.sol index 787acbb4..194dd634 100644 --- a/test/src/lib/op/bitwise/LibOpBitwiseOrNP.t.sol +++ b/test/src/lib/op/bitwise/LibOpBitwiseOrNP.t.sol @@ -33,22 +33,22 @@ contract LibOpBitwiseOrNPTest is OpTest { /// Test the eval of bitwise OR parsed from a string. function testOpBitwiseORNPEval() external view { - checkHappy("_: bitwise-or(0 0);", 0, "0 0"); - checkHappy("_: bitwise-or(0 1e-18);", 1, "0 1"); - checkHappy("_: bitwise-or(1e-18 0);", 1, "1 0"); - checkHappy("_: bitwise-or(1e-18 1e-18);", 1, "1 1"); - checkHappy("_: bitwise-or(0 2e-18);", 2, "0 2"); - checkHappy("_: bitwise-or(2e-18 0);", 2, "2 0"); - checkHappy("_: bitwise-or(1e-18 2e-18);", 3, "1 2"); - checkHappy("_: bitwise-or(2e-18 1e-18);", 3, "2 1"); - checkHappy("_: bitwise-or(2e-18 2e-18);", 2, "2 2"); - checkHappy("_: bitwise-or(0 3e-18);", 3, "0 3"); - checkHappy("_: bitwise-or(3e-18 0);", 3, "3 0"); - checkHappy("_: bitwise-or(1e-18 3e-18);", 3, "1 3"); - checkHappy("_: bitwise-or(3e-18 1e-18);", 3, "3 1"); - checkHappy("_: bitwise-or(2e-18 3e-18);", 3, "2 3"); - checkHappy("_: bitwise-or(3e-18 2e-18);", 3, "3 2"); - checkHappy("_: bitwise-or(3e-18 3e-18);", 3, "3 3"); + checkHappy("_: bitwise-or(0x00 0x00);", 0, "0 0"); + checkHappy("_: bitwise-or(0x00 0x01);", 1, "0 1"); + checkHappy("_: bitwise-or(0x01 0x00);", 1, "1 0"); + checkHappy("_: bitwise-or(0x01 0x01);", 1, "1 1"); + checkHappy("_: bitwise-or(0x00 0x02);", 2, "0 2"); + checkHappy("_: bitwise-or(0x02 0x00);", 2, "2 0"); + checkHappy("_: bitwise-or(0x01 0x02);", 3, "1 2"); + checkHappy("_: bitwise-or(0x02 0x01);", 3, "2 1"); + checkHappy("_: bitwise-or(0x02 0x02);", 2, "2 2"); + checkHappy("_: bitwise-or(0x00 0x03);", 3, "0 3"); + checkHappy("_: bitwise-or(0x03 0x00);", 3, "3 0"); + checkHappy("_: bitwise-or(0x01 0x03);", 3, "1 3"); + checkHappy("_: bitwise-or(0x03 0x01);", 3, "3 1"); + checkHappy("_: bitwise-or(0x02 0x03);", 3, "2 3"); + checkHappy("_: bitwise-or(0x03 0x02);", 3, "3 2"); + checkHappy("_: bitwise-or(0x03 0x03);", 3, "3 3"); } /// Test that a bitwise OR with bad inputs fails integrity. diff --git a/test/src/lib/op/bitwise/LibOpCtPopNP.t.sol b/test/src/lib/op/bitwise/LibOpCtPopNP.t.sol index e7093089..d546ea60 100644 --- a/test/src/lib/op/bitwise/LibOpCtPopNP.t.sol +++ b/test/src/lib/op/bitwise/LibOpCtPopNP.t.sol @@ -41,8 +41,8 @@ contract LibOpCtPopNPTest is OpTest { /// Test the eval of a ct pop opcode parsed from a string. function testOpCtPopNPEval(uint256 x) external view { uint256[] memory stack = new uint256[](1); - stack[0] = LibCtPop.ctpop(x) * 1e18; - checkHappy(bytes(string.concat("_: bitwise-count-ones(", Strings.toString(x), "e-18);")), stack, ""); + stack[0] = LibCtPop.ctpop(x); + checkHappy(bytes(string.concat("_: bitwise-count-ones(", Strings.toHexString(x), ");")), stack, ""); } /// Test that a bitwise count with bad inputs fails integrity. diff --git a/test/src/lib/op/bitwise/LibOpDecodeBitsNP.t.sol b/test/src/lib/op/bitwise/LibOpDecodeBitsNP.t.sol index 44b62c48..2cec9cf3 100644 --- a/test/src/lib/op/bitwise/LibOpDecodeBitsNP.t.sol +++ b/test/src/lib/op/bitwise/LibOpDecodeBitsNP.t.sol @@ -70,19 +70,19 @@ contract LibOpDecodeBitsNPTest is OpTest { /// Test the eval of decoding bits parsed from a string. function testOpDecodeBitsNPEvalHappy() external view { - checkHappy("_:bitwise-decode<0 1>(0);", 0, "0 1 0"); - checkHappy("_:bitwise-decode<0 1>(1e-18);", 1, "0 1 1"); - checkHappy("_:bitwise-decode<0 1>(2e-18);", 0, "0 1 2"); - checkHappy("_:bitwise-decode<0 1>(3e-18);", 1, "0 1 3"); - checkHappy("_:bitwise-decode<0 1>(4e-18);", 0, "0 1 4"); - checkHappy("_:bitwise-decode<0 1>(5e-18);", 1, "0 1 5"); - checkHappy("_:bitwise-decode<0 1>(6e-18);", 0, "0 1 6"); - checkHappy("_:bitwise-decode<0 1>(7e-18);", 1, "0 1 7"); - checkHappy("_:bitwise-decode<0 2>(0);", 0, "0 2 0"); - checkHappy("_:bitwise-decode<0 2>(1e-18);", 1, "0 2 1"); - checkHappy("_:bitwise-decode<0 2>(2e-18);", 2, "0 2 2"); - checkHappy("_:bitwise-decode<0 2>(3e-18);", 3, "0 2 3"); - checkHappy("_:bitwise-decode<0 2>(4e-18);", 0, "0 2 4"); + checkHappy("_:bitwise-decode<0 1>(0x00);", 0, "0 1 0"); + checkHappy("_:bitwise-decode<0 1>(0x01);", 1, "0 1 1"); + checkHappy("_:bitwise-decode<0 1>(0x02);", 0, "0 1 2"); + checkHappy("_:bitwise-decode<0 1>(0x03);", 1, "0 1 3"); + checkHappy("_:bitwise-decode<0 1>(0x04);", 0, "0 1 4"); + checkHappy("_:bitwise-decode<0 1>(0x05);", 1, "0 1 5"); + checkHappy("_:bitwise-decode<0 1>(0x06);", 0, "0 1 6"); + checkHappy("_:bitwise-decode<0 1>(0x07);", 1, "0 1 7"); + checkHappy("_:bitwise-decode<0 2>(0x00);", 0, "0 2 0"); + checkHappy("_:bitwise-decode<0 2>(0x01);", 1, "0 2 1"); + checkHappy("_:bitwise-decode<0 2>(0x02);", 2, "0 2 2"); + checkHappy("_:bitwise-decode<0 2>(0x03);", 3, "0 2 3"); + checkHappy("_:bitwise-decode<0 2>(0x04);", 0, "0 2 4"); checkHappy("_:bitwise-decode<0 2>(uint256-max-value());", 3, "0 2 uint256-max-value"); checkHappy("_:bitwise-decode<0 0xFF>(uint256-max-value());", type(uint256).max >> 1, "0 0xFF uint256-max-value"); checkHappy("_:bitwise-decode<0xFF 1>(uint256-max-value());", 1, "0xFF 1 uint256-max-value"); diff --git a/test/src/lib/op/bitwise/LibOpEncodeBitsNP.t.sol b/test/src/lib/op/bitwise/LibOpEncodeBitsNP.t.sol index 6fb3850c..41ce550e 100644 --- a/test/src/lib/op/bitwise/LibOpEncodeBitsNP.t.sol +++ b/test/src/lib/op/bitwise/LibOpEncodeBitsNP.t.sol @@ -66,21 +66,21 @@ contract LibOpEncodeBitsNPTest is OpTest { /// Test the eval of encoding bits parsed from a string. function testOpEncodeBitsNPEvalHappy() external view { - checkHappy("_:bitwise-encode<0 1>(0 0);", 0, "0 0"); - checkHappy("_:bitwise-encode<0 1>(0 1e-18);", 0, "0 1"); - checkHappy("_:bitwise-encode<0 1>(1e-18 0);", 1, "1 0"); - checkHappy("_:bitwise-encode<0 1>(1e-18 1e-18);", 1, "1 1"); - checkHappy("_:bitwise-encode<0 1>(0 2e-18);", 2, "0 2"); - checkHappy("_:bitwise-encode<0 1>(1e-18 2e-18);", 3, "1 2"); - checkHappy("_:bitwise-encode<0 1>(uint256-max-value() 0);", 1, "uint256-max-value 0"); - checkHappy("_:bitwise-encode<0 1>(uint256-max-value() 1e-18);", 1, "uint256-max-value 1"); - checkHappy("_:bitwise-encode<0 1>(uint256-max-value() 2e-18);", 3, "uint256-max-value 2"); - checkHappy("_:bitwise-encode<0 1>(uint256-max-value() 3e-18);", 3, "uint256-max-value 3"); - checkHappy("_:bitwise-encode<0 2>(uint256-max-value() 0);", 3, "uint256-max-value 0 0 2"); - checkHappy("_:bitwise-encode<1 1>(uint256-max-value() 0);", 2, "uint256-max-value 1 1 1"); - checkHappy("_:bitwise-encode<1 1>(uint256-max-value() 1e-18);", 3, "uint256-max-value 1 1 1"); - checkHappy("_:bitwise-encode<1 1>(uint256-max-value() 2e-18);", 2, "uint256-max-value 2 1 1"); - checkHappy("_:bitwise-encode<0xFF 1>(uint256-max-value() 0);", 1 << 255, "uint256-max-value 2 0xFF 1"); + checkHappy("_:bitwise-encode<0 1>(0x00 0x00);", 0, "0 0"); + checkHappy("_:bitwise-encode<0 1>(0x00 0x01);", 0, "0 1"); + checkHappy("_:bitwise-encode<0 1>(0x01 0x00);", 1, "1 0"); + checkHappy("_:bitwise-encode<0 1>(0x01 0x01);", 1, "1 1"); + checkHappy("_:bitwise-encode<0 1>(0x00 0x02);", 2, "0 2"); + checkHappy("_:bitwise-encode<0 1>(0x01 0x02);", 3, "1 2"); + checkHappy("_:bitwise-encode<0 1>(uint256-max-value() 0x00);", 1, "uint256-max-value 0"); + checkHappy("_:bitwise-encode<0 1>(uint256-max-value() 0x01);", 1, "uint256-max-value 1"); + checkHappy("_:bitwise-encode<0 1>(uint256-max-value() 0x02);", 3, "uint256-max-value 2"); + checkHappy("_:bitwise-encode<0 1>(uint256-max-value() 0x03);", 3, "uint256-max-value 3"); + checkHappy("_:bitwise-encode<0 2>(uint256-max-value() 0x00);", 3, "uint256-max-value 0 0 2"); + checkHappy("_:bitwise-encode<1 1>(uint256-max-value() 0x00);", 2, "uint256-max-value 1 1 1"); + checkHappy("_:bitwise-encode<1 1>(uint256-max-value() 0x01);", 3, "uint256-max-value 1 1 1"); + checkHappy("_:bitwise-encode<1 1>(uint256-max-value() 0x02);", 2, "uint256-max-value 2 1 1"); + checkHappy("_:bitwise-encode<0xFF 1>(uint256-max-value() 0x00);", 1 << 255, "uint256-max-value 2 0xFF 1"); checkHappy( "_:bitwise-encode<0 0xFF>(uint256-max-value() 0);", type(uint256).max >> 1, "uint256-max-value 2 0xFF 1" ); diff --git a/test/src/lib/op/bitwise/LibOpShiftBitsLeftNP.t.sol b/test/src/lib/op/bitwise/LibOpShiftBitsLeftNP.t.sol index 74d5d74d..36fc17df 100644 --- a/test/src/lib/op/bitwise/LibOpShiftBitsLeftNP.t.sol +++ b/test/src/lib/op/bitwise/LibOpShiftBitsLeftNP.t.sol @@ -15,8 +15,8 @@ import {IInterpreterStoreV2} from "rain.interpreter.interface/interface/IInterpr import {SignedContextV1} from "rain.interpreter.interface/interface/IInterpreterCallerV3.sol"; import {LibContext} from "rain.interpreter.interface/lib/caller/LibContext.sol"; import {UnsupportedBitwiseShiftAmount} from "src/error/ErrBitwise.sol"; -import {IntegerOverflow} from "rain.math.fixedpoint/error/ErrScale.sol"; import {LibOperand} from "test/lib/operand/LibOperand.sol"; +import {OperandOverflow} from "src/error/ErrParse.sol"; contract LibOpShiftBitsLeftNPTest is OpTest { /// Directly test the integrity logic of LibOpShiftBitsLeftNP. Tests the @@ -81,28 +81,28 @@ contract LibOpShiftBitsLeftNPTest is OpTest { /// Test the eval of a shift bits left opcode parsed from a string. function testOpShiftBitsLeftNPEval() external view { - checkHappy("_: bitwise-shift-left<1>(0);", 0, "1, 0"); - checkHappy("_: bitwise-shift-left<2>(0);", 0, "2, 0"); - checkHappy("_: bitwise-shift-left<3>(0);", 0, "3, 0"); - checkHappy("_: bitwise-shift-left<255>(0);", 0, "255, 0"); + checkHappy("_: bitwise-shift-left<1>(0x00);", 0, "1, 0"); + checkHappy("_: bitwise-shift-left<2>(0x00);", 0, "2, 0"); + checkHappy("_: bitwise-shift-left<3>(0x00);", 0, "3, 0"); + checkHappy("_: bitwise-shift-left<255>(0x00);", 0, "255, 0"); - checkHappy("_: bitwise-shift-left<1>(1e-18);", 1 << 1, "1, 1"); - checkHappy("_: bitwise-shift-left<2>(1e-18);", 1 << 2, "2, 1"); - checkHappy("_: bitwise-shift-left<3>(1e-18);", 1 << 3, "3, 1"); - checkHappy("_: bitwise-shift-left<255>(1e-18);", 1 << 255, "255, 1"); + checkHappy("_: bitwise-shift-left<1>(0x01);", 1 << 1, "1, 1"); + checkHappy("_: bitwise-shift-left<2>(0x01);", 1 << 2, "2, 1"); + checkHappy("_: bitwise-shift-left<3>(0x01);", 1 << 3, "3, 1"); + checkHappy("_: bitwise-shift-left<255>(0x01);", 1 << 255, "255, 1"); - checkHappy("_: bitwise-shift-left<1>(2e-18);", 2 << 1, "1, 2"); - checkHappy("_: bitwise-shift-left<2>(2e-18);", 2 << 2, "2, 2"); - checkHappy("_: bitwise-shift-left<3>(2e-18);", 2 << 3, "3, 2"); + checkHappy("_: bitwise-shift-left<1>(0x02);", 2 << 1, "1, 2"); + checkHappy("_: bitwise-shift-left<2>(0x02);", 2 << 2, "2, 2"); + checkHappy("_: bitwise-shift-left<3>(0x02);", 2 << 3, "3, 2"); // 2 gets shifted out of the 256 bit word, so this is 0. - checkHappy("_: bitwise-shift-left<255>(2e-18);", 0, "255, 2"); + checkHappy("_: bitwise-shift-left<255>(0x02);", 0, "255, 2"); - checkHappy("_: bitwise-shift-left<1>(3e-18);", 3 << 1, "1, 3"); - checkHappy("_: bitwise-shift-left<2>(3e-18);", 3 << 2, "2, 3"); - checkHappy("_: bitwise-shift-left<3>(3e-18);", 3 << 3, "3, 3"); + checkHappy("_: bitwise-shift-left<1>(0x03);", 3 << 1, "1, 3"); + checkHappy("_: bitwise-shift-left<2>(0x03);", 3 << 2, "2, 3"); + checkHappy("_: bitwise-shift-left<3>(0x03);", 3 << 3, "3, 3"); // The high bit of 3 gets shifted out of the 256 bit word, so this is the // same as shifting 1. - checkHappy("_: bitwise-shift-left<255>(3e-18);", 1 << 255, "255, 3"); + checkHappy("_: bitwise-shift-left<255>(0x03);", 1 << 255, "255, 3"); checkHappy("_: bitwise-shift-left<1>(uint256-max-value());", type(uint256).max << 1, "1, max"); checkHappy("_: bitwise-shift-left<2>(uint256-max-value());", type(uint256).max << 2, "2, max"); @@ -143,8 +143,6 @@ contract LibOpShiftBitsLeftNPTest is OpTest { "_: bitwise-shift-left<65535>(0);", abi.encodeWithSelector(UnsupportedBitwiseShiftAmount.selector, 65535) ); // Lets go ahead and overflow the operand. - checkUnhappyParse( - "_: bitwise-shift-left<65536>(0);", abi.encodeWithSelector(IntegerOverflow.selector, 65536, 65535) - ); + checkUnhappyParse("_: bitwise-shift-left<65536>(0);", abi.encodeWithSelector(OperandOverflow.selector)); } } diff --git a/test/src/lib/op/bitwise/LibOpShiftBitsRightNP.t.sol b/test/src/lib/op/bitwise/LibOpShiftBitsRightNP.t.sol index ba5e7dbb..d4d9353d 100644 --- a/test/src/lib/op/bitwise/LibOpShiftBitsRightNP.t.sol +++ b/test/src/lib/op/bitwise/LibOpShiftBitsRightNP.t.sol @@ -15,8 +15,8 @@ import {IInterpreterStoreV2} from "rain.interpreter.interface/interface/IInterpr import {SignedContextV1} from "rain.interpreter.interface/interface/IInterpreterCallerV3.sol"; import {LibContext} from "rain.interpreter.interface/lib/caller/LibContext.sol"; import {UnsupportedBitwiseShiftAmount} from "src/error/ErrBitwise.sol"; -import {IntegerOverflow} from "rain.math.fixedpoint/error/ErrScale.sol"; import {LibOperand} from "test/lib/operand/LibOperand.sol"; +import {OperandOverflow} from "src/error/ErrParse.sol"; contract LibOpShiftBitsRightNPTest is OpTest { /// Directly test the integrity logic of LibOpShiftBitsRightNP. Tests the @@ -80,30 +80,30 @@ contract LibOpShiftBitsRightNPTest is OpTest { /// Test the eval of a shift bits right opcode parsed from a string. function testOpShiftBitsRightNPEval() external view { - checkHappy("_: bitwise-shift-right<1>(0);", 0, "1, 0"); - checkHappy("_: bitwise-shift-right<2>(0);", 0, "2, 0"); - checkHappy("_: bitwise-shift-right<3>(0);", 0, "3, 0"); - checkHappy("_: bitwise-shift-right<255>(0);", 0, "255, 0"); - - checkHappy("_: bitwise-shift-right<1>(1e-18);", 0, "1, 1"); - checkHappy("_: bitwise-shift-right<2>(1e-18);", 0, "2, 1"); - checkHappy("_: bitwise-shift-right<3>(1e-18);", 0, "3, 1"); - checkHappy("_: bitwise-shift-right<255>(1e-18);", 0, "255, 1"); - - checkHappy("_: bitwise-shift-right<1>(2e-18);", 1, "1, 2"); - checkHappy("_: bitwise-shift-right<2>(2e-18);", 0, "2, 2"); - checkHappy("_: bitwise-shift-right<3>(2e-18);", 0, "3, 2"); - checkHappy("_: bitwise-shift-right<255>(2e-18);", 0, "255, 2"); - - checkHappy("_: bitwise-shift-right<1>(3e-18);", 1, "1, 3"); - checkHappy("_: bitwise-shift-right<2>(3e-18);", 0, "2, 3"); - checkHappy("_: bitwise-shift-right<3>(3e-18);", 0, "3, 3"); - checkHappy("_: bitwise-shift-right<255>(3e-18);", 0, "255, 3"); - - checkHappy("_: bitwise-shift-right<1>(4e-18);", 2, "1, 4"); - checkHappy("_: bitwise-shift-right<2>(4e-18);", 1, "2, 4"); - checkHappy("_: bitwise-shift-right<3>(4e-18);", 0, "3, 4"); - checkHappy("_: bitwise-shift-right<255>(4e-18);", 0, "255, 4"); + checkHappy("_: bitwise-shift-right<1>(0x00);", 0, "1, 0"); + checkHappy("_: bitwise-shift-right<2>(0x00);", 0, "2, 0"); + checkHappy("_: bitwise-shift-right<3>(0x00);", 0, "3, 0"); + checkHappy("_: bitwise-shift-right<255>(0x00);", 0, "255, 0"); + + checkHappy("_: bitwise-shift-right<1>(0x01);", 0, "1, 1"); + checkHappy("_: bitwise-shift-right<2>(0x01);", 0, "2, 1"); + checkHappy("_: bitwise-shift-right<3>(0x01);", 0, "3, 1"); + checkHappy("_: bitwise-shift-right<255>(0x01);", 0, "255, 1"); + + checkHappy("_: bitwise-shift-right<1>(0x02);", 1, "1, 2"); + checkHappy("_: bitwise-shift-right<2>(0x02);", 0, "2, 2"); + checkHappy("_: bitwise-shift-right<3>(0x02);", 0, "3, 2"); + checkHappy("_: bitwise-shift-right<255>(0x02);", 0, "255, 2"); + + checkHappy("_: bitwise-shift-right<1>(0x03);", 1, "1, 3"); + checkHappy("_: bitwise-shift-right<2>(0x03);", 0, "2, 3"); + checkHappy("_: bitwise-shift-right<3>(0x03);", 0, "3, 3"); + checkHappy("_: bitwise-shift-right<255>(0x03);", 0, "255, 3"); + + checkHappy("_: bitwise-shift-right<1>(0x04);", 2, "1, 4"); + checkHappy("_: bitwise-shift-right<2>(0x04);", 1, "2, 4"); + checkHappy("_: bitwise-shift-right<3>(0x04);", 0, "3, 4"); + checkHappy("_: bitwise-shift-right<255>(0x04);", 0, "255, 4"); checkHappy("_: bitwise-shift-right<1>(uint256-max-value());", type(uint256).max >> 1, "1, max"); checkHappy("_: bitwise-shift-right<2>(uint256-max-value());", type(uint256).max >> 2, "2, max"); @@ -142,8 +142,6 @@ contract LibOpShiftBitsRightNPTest is OpTest { "_: bitwise-shift-right<65535>(0);", abi.encodeWithSelector(UnsupportedBitwiseShiftAmount.selector, 65535) ); // Lets go ahead and overflow the operand. - checkUnhappyParse( - "_: bitwise-shift-right<65536>(0);", abi.encodeWithSelector(IntegerOverflow.selector, 65536, 65535) - ); + checkUnhappyParse("_: bitwise-shift-right<65536>(0);", abi.encodeWithSelector(OperandOverflow.selector)); } } diff --git a/test/src/lib/op/call/LibOpCallNP.t.sol b/test/src/lib/op/call/LibOpCallNP.t.sol index 0c2d5c6d..144936ac 100644 --- a/test/src/lib/op/call/LibOpCallNP.t.sol +++ b/test/src/lib/op/call/LibOpCallNP.t.sol @@ -17,6 +17,7 @@ import {LibBytecode, SourceIndexOutOfBounds} from "rain.interpreter.interface/li import {BadOpInputsLength} from "src/lib/integrity/LibIntegrityCheckNP.sol"; import {STACK_TRACER} from "src/lib/state/LibInterpreterStateNP.sol"; import {LibOperand} from "test/lib/operand/LibOperand.sol"; +import {LibDecimalFloat} from "rain.math.float/src/lib/LibDecimalFloat.sol"; /// @title LibOpCallNPTest /// @notice Test the LibOpCallNP library that includes the "call" word. @@ -161,7 +162,7 @@ contract LibOpCallNPTest is OpTest, BytecodeTest { ExpectedTrace[] memory traces = new ExpectedTrace[](1); traces[0].sourceIndex = 0; traces[0].stack = new uint256[](1); - traces[0].stack[0] = 1e18; + traces[0].stack[0] = LibDecimalFloat.pack(1e37, -37); checkCallNPTraces("_: 1;", traces); } @@ -171,7 +172,7 @@ contract LibOpCallNPTest is OpTest, BytecodeTest { traces[0].stack = new uint256[](0); traces[1].sourceIndex = 1; traces[1].stack = new uint256[](1); - traces[1].stack[0] = 1e18; + traces[1].stack[0] = LibDecimalFloat.pack(1e37, -37); checkCallNPTraces(":call<1>();_:1;", traces); } diff --git a/test/src/lib/op/crypto/LibOpHashNP.t.sol b/test/src/lib/op/crypto/LibOpHashNP.t.sol index 2c160bb2..304eb0c2 100644 --- a/test/src/lib/op/crypto/LibOpHashNP.t.sol +++ b/test/src/lib/op/crypto/LibOpHashNP.t.sol @@ -23,6 +23,7 @@ import {SignedContextV1} from "rain.interpreter.interface/interface/IInterpreter import {LibIntegrityCheckNP, IntegrityCheckStateNP} from "src/lib/integrity/LibIntegrityCheckNP.sol"; import {InterpreterStateNP, LibInterpreterStateNP} from "src/lib/state/LibInterpreterStateNP.sol"; import {LibOperand} from "test/lib/operand/LibOperand.sol"; +import {LibDecimalFloat} from "rain.math.float/src/lib/LibDecimalFloat.sol"; /// @title LibOpHashNPTest /// @notice Test the runtime and integrity time logic of LibOpHashNP. @@ -57,12 +58,12 @@ contract LibOpHashNPTest is OpTest { } /// Test the eval of a hash opcode parsed from a string. Tests 0 inputs. - function testOpHashNPEval0Inputs() external { + function testOpHashNPEval0Inputs() external view { checkHappy("_: hash();", uint256(keccak256("")), ""); } /// Test the eval of a hash opcode parsed from a string. Tests 1 input. - function testOpHashNPEval1Input() external { + function testOpHashNPEval1Input() external view { checkHappy( "_: hash(0x1234567890abcdef);", uint256(keccak256(abi.encodePacked(uint256(0x1234567890abcdef)))), "" ); @@ -70,7 +71,7 @@ contract LibOpHashNPTest is OpTest { /// Test the eval of a hash opcode parsed from a string. Tests 2 inputs that /// are identical to each other. - function testOpHashNPEval2Inputs() external { + function testOpHashNPEval2Inputs() external view { checkHappy( "_: hash(0x1234567890abcdef 0x1234567890abcdef);", uint256(keccak256(abi.encodePacked(uint256(0x1234567890abcdef), uint256(0x1234567890abcdef)))), @@ -80,7 +81,7 @@ contract LibOpHashNPTest is OpTest { /// Test the eval of a hash opcode parsed from a string. Tests 2 inputs that /// are different from each other. - function testOpHashNPEval2InputsDifferent() external { + function testOpHashNPEval2InputsDifferent() external view { checkHappy( "_: hash(0x1234567890abcdef 0xfedcba0987654321);", uint256(keccak256(abi.encodePacked(uint256(0x1234567890abcdef), uint256(0xfedcba0987654321)))), @@ -104,11 +105,11 @@ contract LibOpHashNPTest is OpTest { }) ); assertEq(stack.length, 3); - assertEq(stack[0], uint256(9e18)); + assertEq(stack[0], LibDecimalFloat.pack(9e37, -37)); assertEq( stack[1], uint256(keccak256(abi.encodePacked(uint256(0x1234567890abcdef), uint256(0xfedcba0987654321)))) ); - assertEq(stack[2], uint256(5e18)); + assertEq(stack[2], LibDecimalFloat.pack(5e37, -37)); assertEq(kvs.length, 0); } diff --git a/test/src/lib/op/evm/LibOpMaxUint256NP.t.sol b/test/src/lib/op/evm/LibOpMaxUint256NP.t.sol index c9a09dc9..4491e445 100644 --- a/test/src/lib/op/evm/LibOpMaxUint256NP.t.sol +++ b/test/src/lib/op/evm/LibOpMaxUint256NP.t.sol @@ -49,7 +49,7 @@ contract LibOpMaxUint256NPTest is OpTest { } /// Test the eval of LibOpMaxUint256NP parsed from a string. - function testOpMaxUint256NPEval() external { + function testOpMaxUint256NPEval() external view { checkHappy("_: uint256-max-value();", type(uint256).max, ""); } diff --git a/test/src/lib/parse/LibParse.literalIntegerDecimal.t.sol b/test/src/lib/parse/LibParse.literalIntegerDecimal.t.sol index 2e5b80e4..23ca2183 100644 --- a/test/src/lib/parse/LibParse.literalIntegerDecimal.t.sol +++ b/test/src/lib/parse/LibParse.literalIntegerDecimal.t.sol @@ -8,6 +8,7 @@ import {DecimalLiteralOverflow} from "src/lib/parse/literal/LibParseLiteral.sol" import {LibParse, UnexpectedRHSChar, UnexpectedRightParen} from "src/lib/parse/LibParse.sol"; import {LibBytecode} from "rain.interpreter.interface/lib/bytecode/LibBytecode.sol"; import {ParseState} from "src/lib/parse/LibParseState.sol"; +import {LibDecimalFloat} from "rain.math.float/src/lib/LibDecimalFloat.sol"; /// @title LibParseLiteralIntegerDecimalTest /// Tests parsing integer literal decimal values. @@ -46,7 +47,7 @@ contract LibParseLiteralIntegerDecimalTest is Test { ); assertEq(constants.length, 1); - assertEq(constants[0], 1e18); + assertEq(constants[0], LibDecimalFloat.pack(1e37, -37)); } /// Check 2 decimal literals. Should not revert and return one source and @@ -83,8 +84,8 @@ contract LibParseLiteralIntegerDecimalTest is Test { ); assertEq(constants.length, 2); - assertEq(constants[0], 10e18); - assertEq(constants[1], 25e18); + assertEq(constants[0], LibDecimalFloat.pack(10e36, -36)); + assertEq(constants[1], LibDecimalFloat.pack(25e36, -36)); } /// Check 3 decimal literals with 2 dupes. Should dedupe and respect ordering. @@ -123,15 +124,14 @@ contract LibParseLiteralIntegerDecimalTest is Test { hex"01100000" ); assertEq(constants.length, 2); - assertEq(constants[0], 11e18); - assertEq(constants[1], 233e18); + assertEq(constants[0], LibDecimalFloat.pack(11e36, -36)); + assertEq(constants[1], LibDecimalFloat.pack(233e35, -35)); } - /// Check that we can parse uint256 max int in decimal form. - function testParseIntegerLiteralDecimalUint256Max() external view { - (bytes memory bytecode, uint256[] memory constants) = LibMetaFixture.newState( - "_: 115792089237316195423570985008687907853269984665640564039457584007913129639935e-18;" - ).parse(); + /// Check that we can parse the max int128 value in decimal form. + function testParseIntegerLiteralDecimalInt128Max() external view { + (bytes memory bytecode, uint256[] memory constants) = + LibMetaFixture.newState("_: 170141183460469231731687303715884105727;").parse(); uint256 sourceIndex = 0; assertEq(LibBytecode.sourceCount(bytecode), 1); assertEq(LibBytecode.sourceRelativeOffset(bytecode, sourceIndex), 0); @@ -160,15 +160,14 @@ contract LibParseLiteralIntegerDecimalTest is Test { ); assertEq(constants.length, 1); - assertEq(constants[0], type(uint256).max); + assertEq(constants[0], uint256(int256(type(int128).max))); } /// Check that we can parse uint256 max int in decimal form with leading /// zeros. - function testParseIntegerLiteralDecimalUint256MaxLeadingZeros() external view { - (bytes memory bytecode, uint256[] memory constants) = LibMetaFixture.newState( - "_: 000115792089237316195423570985008687907853269984665640564039457584007913129639935e-18;" - ).parse(); + function testParseIntegerLiteralDecimalInt128MaxLeadingZeros() external view { + (bytes memory bytecode, uint256[] memory constants) = + LibMetaFixture.newState("_: 000170141183460469231731687303715884105727;").parse(); uint256 sourceIndex = 0; assertEq(LibBytecode.sourceCount(bytecode), 1); assertEq(LibBytecode.sourceRelativeOffset(bytecode, sourceIndex), 0); @@ -196,7 +195,7 @@ contract LibParseLiteralIntegerDecimalTest is Test { hex"01100000" ); assertEq(constants.length, 1); - assertEq(constants[0], type(uint256).max); + assertEq(constants[0], uint256(int256(type(int128).max))); } /// Check that decimal literals will revert if they overflow uint256. @@ -282,11 +281,11 @@ contract LibParseLiteralIntegerDecimalTest is Test { ); assertEq(constants.length, 5); - assertEq(constants[0], 1e20); - assertEq(constants[1], 10e20); - assertEq(constants[2], 1e48); - assertEq(constants[3], 1e36); - assertEq(constants[4], 1001e33); + assertEq(constants[0], LibDecimalFloat.pack(1e37, -35)); + assertEq(constants[1], LibDecimalFloat.pack(1e37, -34)); + assertEq(constants[2], LibDecimalFloat.pack(1e37, -7)); + assertEq(constants[3], LibDecimalFloat.pack(1e37, -19)); + assertEq(constants[4], LibDecimalFloat.pack(1001e34, -19)); } /// Check that decimals cause yang. diff --git a/test/src/lib/parse/LibParse.namedLHS.t.sol b/test/src/lib/parse/LibParse.namedLHS.t.sol index 39ae8e3c..315c8007 100644 --- a/test/src/lib/parse/LibParse.namedLHS.t.sol +++ b/test/src/lib/parse/LibParse.namedLHS.t.sol @@ -15,6 +15,7 @@ import {Operand, LibParseOperand} from "src/lib/parse/LibParseOperand.sol"; import {LibConvert} from "rain.lib.typecast/LibConvert.sol"; import {LibAllStandardOpsNP} from "src/lib/op/LibAllStandardOpsNP.sol"; import {LibGenParseMeta} from "rain.sol.codegen/lib/LibGenParseMeta.sol"; +import {LibDecimalFloat} from "rain.math.float/src/lib/LibDecimalFloat.sol"; /// @title LibParseNamedLHSTest contract LibParseNamedLHSTest is Test { @@ -159,9 +160,9 @@ contract LibParseNamedLHSTest is Test { hex"00100003" ); assertEq(constants.length, 3); - assertEq(constants[0], 1e18); - assertEq(constants[1], 2e18); - assertEq(constants[2], 3e18); + assertEq(constants[0], LibDecimalFloat.pack(1e37, -37)); + assertEq(constants[1], LibDecimalFloat.pack(2e37, -37)); + assertEq(constants[2], LibDecimalFloat.pack(3e37, -37)); } /// Duplicate names are disallowed in the same source. @@ -213,9 +214,9 @@ contract LibParseNamedLHSTest is Test { hex"00100001" ); assertEq(constants.length, 4); - assertEq(constants[0], 1e18); - assertEq(constants[1], 2e18); - assertEq(constants[2], 3e18); - assertEq(constants[3], 4e18); + assertEq(constants[0], LibDecimalFloat.pack(1e37, -37)); + assertEq(constants[1], LibDecimalFloat.pack(2e37, -37)); + assertEq(constants[2], LibDecimalFloat.pack(3e37, -37)); + assertEq(constants[3], LibDecimalFloat.pack(4e37, -37)); } } diff --git a/test/src/lib/parse/LibParse.operand8M1M1.t.sol b/test/src/lib/parse/LibParse.operand8M1M1.t.sol index 3d0a0865..ab2ea2ea 100644 --- a/test/src/lib/parse/LibParse.operand8M1M1.t.sol +++ b/test/src/lib/parse/LibParse.operand8M1M1.t.sol @@ -4,8 +4,8 @@ pragma solidity =0.8.25; import {UnsupportedLiteralType} from "src/lib/parse/literal/LibParseLiteral.sol"; import {ParserOutOfBounds} from "src/lib/parse/LibParse.sol"; import {ExpectedOperand, UnclosedOperand} from "src/error/ErrParse.sol"; -import {IntegerOverflow} from "rain.math.fixedpoint/error/ErrScale.sol"; import {OperandTest} from "test/abstract/OperandTest.sol"; +import {OperandOverflow} from "src/error/ErrParse.sol"; import {LibMetaFixture} from "test/lib/parse/LibMetaFixture.sol"; @@ -25,7 +25,7 @@ contract LibParseOperand8M1M1Test is OperandTest { checkOperandParse("_:e<255>();", hex"061000ff"); // Above uint8 max will overflow. - checkParseError("_:e<256>();", abi.encodeWithSelector(IntegerOverflow.selector, 256, 255)); + checkParseError("_:e<256>();", abi.encodeWithSelector(OperandOverflow.selector)); } /// Single value and one bit can be provided, other bit will default to zero. @@ -38,10 +38,10 @@ contract LibParseOperand8M1M1Test is OperandTest { checkOperandParse("_:e<255 1>();", hex"061001ff"); // Non binary bit will overflow. - checkParseError("_:e<1 2>();", abi.encodeWithSelector(IntegerOverflow.selector, 2, 1)); - checkParseError("_:e<1 3>();", abi.encodeWithSelector(IntegerOverflow.selector, 3, 1)); - checkParseError("_:e<2 2>();", abi.encodeWithSelector(IntegerOverflow.selector, 2, 1)); - checkParseError("_:e<255 2>();", abi.encodeWithSelector(IntegerOverflow.selector, 2, 1)); + checkParseError("_:e<1 2>();", abi.encodeWithSelector(OperandOverflow.selector)); + checkParseError("_:e<1 3>();", abi.encodeWithSelector(OperandOverflow.selector)); + checkParseError("_:e<2 2>();", abi.encodeWithSelector(OperandOverflow.selector)); + checkParseError("_:e<255 2>();", abi.encodeWithSelector(OperandOverflow.selector)); } /// Single value and two bits can be provided. @@ -60,21 +60,21 @@ contract LibParseOperand8M1M1Test is OperandTest { checkOperandParse("_:e<255 1 1>();", hex"061003ff"); // Non binary bit will overflow. - checkParseError("_:e<1 0 2>();", abi.encodeWithSelector(IntegerOverflow.selector, 2, 1)); - checkParseError("_:e<1 1 2>();", abi.encodeWithSelector(IntegerOverflow.selector, 2, 1)); - checkParseError("_:e<1 2 0>();", abi.encodeWithSelector(IntegerOverflow.selector, 2, 1)); - checkParseError("_:e<1 2 1>();", abi.encodeWithSelector(IntegerOverflow.selector, 2, 1)); - checkParseError("_:e<1 2 2>();", abi.encodeWithSelector(IntegerOverflow.selector, 2, 1)); - checkParseError("_:e<2 0 2>();", abi.encodeWithSelector(IntegerOverflow.selector, 2, 1)); - checkParseError("_:e<2 1 2>();", abi.encodeWithSelector(IntegerOverflow.selector, 2, 1)); - checkParseError("_:e<2 2 0>();", abi.encodeWithSelector(IntegerOverflow.selector, 2, 1)); - checkParseError("_:e<2 2 1>();", abi.encodeWithSelector(IntegerOverflow.selector, 2, 1)); - checkParseError("_:e<2 2 2>();", abi.encodeWithSelector(IntegerOverflow.selector, 2, 1)); - checkParseError("_:e<255 0 2>();", abi.encodeWithSelector(IntegerOverflow.selector, 2, 1)); - checkParseError("_:e<255 1 2>();", abi.encodeWithSelector(IntegerOverflow.selector, 2, 1)); - checkParseError("_:e<255 2 0>();", abi.encodeWithSelector(IntegerOverflow.selector, 2, 1)); - checkParseError("_:e<255 2 1>();", abi.encodeWithSelector(IntegerOverflow.selector, 2, 1)); - checkParseError("_:e<255 2 2>();", abi.encodeWithSelector(IntegerOverflow.selector, 2, 1)); + checkParseError("_:e<1 0 2>();", abi.encodeWithSelector(OperandOverflow.selector)); + checkParseError("_:e<1 1 2>();", abi.encodeWithSelector(OperandOverflow.selector)); + checkParseError("_:e<1 2 0>();", abi.encodeWithSelector(OperandOverflow.selector)); + checkParseError("_:e<1 2 1>();", abi.encodeWithSelector(OperandOverflow.selector)); + checkParseError("_:e<1 2 2>();", abi.encodeWithSelector(OperandOverflow.selector)); + checkParseError("_:e<2 0 2>();", abi.encodeWithSelector(OperandOverflow.selector)); + checkParseError("_:e<2 1 2>();", abi.encodeWithSelector(OperandOverflow.selector)); + checkParseError("_:e<2 2 0>();", abi.encodeWithSelector(OperandOverflow.selector)); + checkParseError("_:e<2 2 1>();", abi.encodeWithSelector(OperandOverflow.selector)); + checkParseError("_:e<2 2 2>();", abi.encodeWithSelector(OperandOverflow.selector)); + checkParseError("_:e<255 0 2>();", abi.encodeWithSelector(OperandOverflow.selector)); + checkParseError("_:e<255 1 2>();", abi.encodeWithSelector(OperandOverflow.selector)); + checkParseError("_:e<255 2 0>();", abi.encodeWithSelector(OperandOverflow.selector)); + checkParseError("_:e<255 2 1>();", abi.encodeWithSelector(OperandOverflow.selector)); + checkParseError("_:e<255 2 2>();", abi.encodeWithSelector(OperandOverflow.selector)); } /// Unclosed operand is disallowed. diff --git a/test/src/lib/parse/LibParse.operandDoublePerByteNoDefault.t.sol b/test/src/lib/parse/LibParse.operandDoublePerByteNoDefault.t.sol index 49b3dc2d..40cd291f 100644 --- a/test/src/lib/parse/LibParse.operandDoublePerByteNoDefault.t.sol +++ b/test/src/lib/parse/LibParse.operandDoublePerByteNoDefault.t.sol @@ -6,7 +6,7 @@ import {ExpectedOperand, UnclosedOperand, UnexpectedOperandValue} from "src/erro import {LibParse} from "src/lib/parse/LibParse.sol"; import {LibMetaFixture} from "test/lib/parse/LibMetaFixture.sol"; import {ParseState} from "src/lib/parse/LibParseState.sol"; -import {IntegerOverflow} from "rain.math.fixedpoint/error/ErrScale.sol"; +import {OperandOverflow} from "src/error/ErrParse.sol"; contract LibParseOperandDoublePerByteNoDefaultTest is Test { using LibParse for ParseState; @@ -153,7 +153,7 @@ contract LibParseOperandDoublePerByteNoDefaultTest is Test { /// 2 literals are expected for this operand parser. Tests 256 256. function testOperandDoublePerByteNoDefaultSecondOverflow() external { - vm.expectRevert(abi.encodeWithSelector(IntegerOverflow.selector, 256, 255)); + vm.expectRevert(abi.encodeWithSelector(OperandOverflow.selector)); (bytes memory bytecode, uint256[] memory constants) = LibMetaFixture.newState("_:c<256 256>();").parse(); (bytecode); (constants); @@ -161,7 +161,7 @@ contract LibParseOperandDoublePerByteNoDefaultTest is Test { /// 2 literals are expected for this operand parser. Tests 256 255. function testOperandDoublePerByteNoDefaultSecondOverflowFirst() external { - vm.expectRevert(abi.encodeWithSelector(IntegerOverflow.selector, 256, 255)); + vm.expectRevert(abi.encodeWithSelector(OperandOverflow.selector)); (bytes memory bytecode, uint256[] memory constants) = LibMetaFixture.newState("_:c<256 255>();").parse(); (bytecode); (constants); diff --git a/test/src/lib/parse/LibParse.operandM1M1.t.sol b/test/src/lib/parse/LibParse.operandM1M1.t.sol index ad315020..fc878ad2 100644 --- a/test/src/lib/parse/LibParse.operandM1M1.t.sol +++ b/test/src/lib/parse/LibParse.operandM1M1.t.sol @@ -6,7 +6,7 @@ import {ParserOutOfBounds, LibParse, ExpectedLeftParen} from "src/lib/parse/LibP import {OperandTest} from "test/abstract/OperandTest.sol"; import {LibMetaFixture} from "test/lib/parse/LibMetaFixture.sol"; import {ParseState} from "src/lib/parse/LibParseState.sol"; -import {IntegerOverflow} from "rain.math.fixedpoint/error/ErrScale.sol"; +import {OperandOverflow} from "src/error/ErrParse.sol"; contract LibParseOperandM1M1Test is OperandTest { using LibParse for ParseState; @@ -82,7 +82,7 @@ contract LibParseOperandM1M1Test is OperandTest { /// Default is zero for this operand parser. Tests first overflow. function testOperandM1M1FirstOverflow() external { - vm.expectRevert(abi.encodeWithSelector(IntegerOverflow.selector, 2, 1)); + vm.expectRevert(abi.encodeWithSelector(OperandOverflow.selector)); (bytes memory bytecode, uint256[] memory constants) = LibMetaFixture.newState("_:d<2>();").parse(); (bytecode); (constants); @@ -136,7 +136,7 @@ contract LibParseOperandM1M1Test is OperandTest { /// Default is zero for this operand parser. Tests 0 2. function testOperandM1M1SecondOverflow() external { - vm.expectRevert(abi.encodeWithSelector(IntegerOverflow.selector, 2, 1)); + vm.expectRevert(abi.encodeWithSelector(OperandOverflow.selector)); (bytes memory bytecode, uint256[] memory constants) = LibMetaFixture.newState("_:d<0 2>();").parse(); (bytecode); (constants); diff --git a/test/src/lib/parse/LibParse.operandSingleFull.t.sol b/test/src/lib/parse/LibParse.operandSingleFull.t.sol index 03a193d3..60b45e0c 100644 --- a/test/src/lib/parse/LibParse.operandSingleFull.t.sol +++ b/test/src/lib/parse/LibParse.operandSingleFull.t.sol @@ -6,7 +6,7 @@ import {ExpectedOperand, UnclosedOperand, UnexpectedOperandValue} from "src/erro import {LibParse, ExpectedLeftParen} from "src/lib/parse/LibParse.sol"; import {LibMetaFixture} from "test/lib/parse/LibMetaFixture.sol"; import {ParseState} from "src/lib/parse/LibParseState.sol"; -import {IntegerOverflow} from "rain.math.fixedpoint/error/ErrScale.sol"; +import {OperandOverflow} from "src/error/ErrParse.sol"; contract LibParseOperandSingleFullTest is Test { using LibParse for ParseState; @@ -205,7 +205,7 @@ contract LibParseOperandSingleFullTest is Test { /// Overflowing decimal uint16 max as single full operand reverts. function testOperandSingleFullUint16MaxOverflow() external { - vm.expectRevert(abi.encodeWithSelector(IntegerOverflow.selector, 65536, 65535)); + vm.expectRevert(abi.encodeWithSelector(OperandOverflow.selector)); (bytes memory bytecode, uint256[] memory constants) = LibMetaFixture.newState("_:b<65536>();").parse(); (bytecode); (constants); @@ -213,7 +213,7 @@ contract LibParseOperandSingleFullTest is Test { /// Overflowing hexadecimal uint16 max as a single full operand reverts. function testOperandSingleFullHexUint16MaxOverflow() external { - vm.expectRevert(abi.encodeWithSelector(IntegerOverflow.selector, 65536, 65535)); + vm.expectRevert(abi.encodeWithSelector(OperandOverflow.selector)); (bytes memory bytecode, uint256[] memory constants) = LibMetaFixture.newState("_:b<0x010000>();").parse(); (bytecode); (constants); diff --git a/test/src/lib/parse/LibParseOperand.handleOperand8M1M1.t.sol b/test/src/lib/parse/LibParseOperand.handleOperand8M1M1.t.sol index 42b22062..cc269aa7 100644 --- a/test/src/lib/parse/LibParseOperand.handleOperand8M1M1.t.sol +++ b/test/src/lib/parse/LibParseOperand.handleOperand8M1M1.t.sol @@ -5,8 +5,7 @@ import {Test} from "forge-std/Test.sol"; import {LibParseOperand, Operand} from "src/lib/parse/LibParseOperand.sol"; import {ExpectedOperand, UnexpectedOperandValue} from "src/error/ErrParse.sol"; import {LibParseLiteral} from "src/lib/parse/literal/LibParseLiteral.sol"; -import {IntegerOverflow} from "rain.math.fixedpoint/error/ErrScale.sol"; -import {LibFixedPointDecimalScale, DECIMAL_MAX_SAFE_INT} from "rain.math.fixedpoint/lib/LibFixedPointDecimalScale.sol"; +import {OperandOverflow} from "src/error/ErrParse.sol"; contract LibParseOperandHandleOperand8M1M1Test is Test { // The first value must be 1 byte and is mandatory. Zero values is an error. @@ -24,22 +23,12 @@ contract LibParseOperandHandleOperand8M1M1Test is Test { } // If the first value is greater than 1 byte, it is an error. - function testHandleOperand8M1M1FirstValueTooLarge(uint256 value) external { - value = bound(value, uint256(type(uint8).max) + 1, DECIMAL_MAX_SAFE_INT); - - // If value is a decimal, scale it above 256 as a decimal. - if (value >= 1e18) { - value = bound(value, 256e18, type(uint256).max); - value = value - (value % 1e18); - } + function testHandleOperand8M1M1FirstValueTooLarge(int256 value) external { + value = bound(value, int256(uint256(type(uint8).max)) + 1, type(int128).max); uint256[] memory values = new uint256[](1); - values[0] = value; - vm.expectRevert( - abi.encodeWithSelector( - IntegerOverflow.selector, LibFixedPointDecimalScale.decimalOrIntToInt(value, DECIMAL_MAX_SAFE_INT), 0xFF - ) - ); + values[0] = uint256(value); + vm.expectRevert(abi.encodeWithSelector(OperandOverflow.selector)); LibParseOperand.handleOperand8M1M1(values); } @@ -58,22 +47,12 @@ contract LibParseOperandHandleOperand8M1M1Test is Test { // but the second is greater than 1 bit, it is an error. function testHandleOperand8M1M1FirstAndSecondValueSecondValueTooLarge(uint256 a, uint256 b) external { a = bound(a, 0, type(uint8).max); - b = bound(b, 2, DECIMAL_MAX_SAFE_INT); - - // If b is a decimal, scale it above 256 as a decimal. - if (b >= 1e18) { - b = bound(b, 256e18, type(uint256).max); - b = b - (b % 1e18); - } + b = bound(b, 2, uint256(int256(type(int128).max))); uint256[] memory values = new uint256[](2); values[0] = a; values[1] = b; - vm.expectRevert( - abi.encodeWithSelector( - IntegerOverflow.selector, LibFixedPointDecimalScale.decimalOrIntToInt(b, DECIMAL_MAX_SAFE_INT), 1 - ) - ); + vm.expectRevert(abi.encodeWithSelector(OperandOverflow.selector)); LibParseOperand.handleOperand8M1M1(values); } @@ -95,23 +74,13 @@ contract LibParseOperandHandleOperand8M1M1Test is Test { function testHandleOperand8M1M1AllValuesThirdValueTooLarge(uint256 a, uint256 b, uint256 c) external { a = bound(a, 0, type(uint8).max); b = bound(b, 0, 1); - c = bound(c, 2, DECIMAL_MAX_SAFE_INT); - - // If c is a decimal, scale it above 256 as a decimal. - if (c >= 1e18) { - c = bound(c, 256e18, type(uint256).max); - c = c - (c % 1e18); - } + c = bound(c, 2, uint256(int256(type(int128).max))); uint256[] memory values = new uint256[](3); values[0] = a; values[1] = b; values[2] = c; - vm.expectRevert( - abi.encodeWithSelector( - IntegerOverflow.selector, LibFixedPointDecimalScale.decimalOrIntToInt(c, DECIMAL_MAX_SAFE_INT), 1 - ) - ); + vm.expectRevert(abi.encodeWithSelector(OperandOverflow.selector)); LibParseOperand.handleOperand8M1M1(values); } diff --git a/test/src/lib/parse/LibParseOperand.handleOperandDoublePerByteNoDefault.t.sol b/test/src/lib/parse/LibParseOperand.handleOperandDoublePerByteNoDefault.t.sol index 8c493f46..56f0a1ac 100644 --- a/test/src/lib/parse/LibParseOperand.handleOperandDoublePerByteNoDefault.t.sol +++ b/test/src/lib/parse/LibParseOperand.handleOperandDoublePerByteNoDefault.t.sol @@ -5,8 +5,7 @@ import {Test} from "forge-std/Test.sol"; import {LibParseOperand, Operand} from "src/lib/parse/LibParseOperand.sol"; import {ExpectedOperand, UnexpectedOperandValue} from "src/error/ErrParse.sol"; import {LibParseLiteral} from "src/lib/parse/literal/LibParseLiteral.sol"; -import {IntegerOverflow} from "rain.math.fixedpoint/error/ErrScale.sol"; -import {LibFixedPointDecimalScale, DECIMAL_MAX_SAFE_INT} from "rain.math.fixedpoint/lib/LibFixedPointDecimalScale.sol"; +import {OperandOverflow} from "src/error/ErrParse.sol"; contract LibParseOperandHandleOperandDoublePerByteNoDefaultTest is Test { // There must be exactly two values so zero values is an error. @@ -33,46 +32,26 @@ contract LibParseOperandHandleOperandDoublePerByteNoDefaultTest is Test { // If the first value is greater than 1 byte, it is an error. function testHandleOperandDoublePerByteNoDefaultFirstValueTooLarge(uint256 a, uint256 b) external { - a = bound(a, uint256(type(uint8).max) + 1, DECIMAL_MAX_SAFE_INT); + a = bound(a, uint256(type(uint8).max) + 1, uint256(int256(type(int128).max))); b = bound(b, 0, type(uint8).max); - // If a is a decimal, scale it above 256 as a decimal. - if (a >= 1e18) { - a = bound(a, 256e18, type(uint256).max); - a = a - (a % 1e18); - } - uint256[] memory values = new uint256[](2); values[0] = a; values[1] = b; - vm.expectRevert( - abi.encodeWithSelector( - IntegerOverflow.selector, LibFixedPointDecimalScale.decimalOrIntToInt(a, DECIMAL_MAX_SAFE_INT), 255 - ) - ); + vm.expectRevert(abi.encodeWithSelector(OperandOverflow.selector)); LibParseOperand.handleOperandDoublePerByteNoDefault(values); } // If the second value is greater than 1 byte, it is an error. function testHandleOperandDoublePerByteNoDefaultSecondValueTooLarge(uint256 a, uint256 b) external { a = bound(a, 0, type(uint8).max); - b = bound(b, uint256(type(uint8).max) + 1, DECIMAL_MAX_SAFE_INT); - - // If b is a decimal, scale it above 256 as a decimal. - if (b >= 1e18) { - b = bound(b, 256e18, type(uint256).max); - b = b - (b % 1e18); - } + b = bound(b, uint256(type(uint8).max) + 1, uint256(int256(type(int128).max))); uint256[] memory values = new uint256[](2); values[0] = a; values[1] = b; - vm.expectRevert( - abi.encodeWithSelector( - IntegerOverflow.selector, LibFixedPointDecimalScale.decimalOrIntToInt(b, DECIMAL_MAX_SAFE_INT), 255 - ) - ); + vm.expectRevert(abi.encodeWithSelector(OperandOverflow.selector)); LibParseOperand.handleOperandDoublePerByteNoDefault(values); } diff --git a/test/src/lib/parse/LibParseOperand.handleOperandM1M1.t.sol b/test/src/lib/parse/LibParseOperand.handleOperandM1M1.t.sol index d8643df2..672a5a50 100644 --- a/test/src/lib/parse/LibParseOperand.handleOperandM1M1.t.sol +++ b/test/src/lib/parse/LibParseOperand.handleOperandM1M1.t.sol @@ -5,8 +5,7 @@ import {Test} from "forge-std/Test.sol"; import {LibParseOperand, Operand} from "src/lib/parse/LibParseOperand.sol"; import {ExpectedOperand, UnexpectedOperandValue} from "src/error/ErrParse.sol"; import {LibParseLiteral} from "src/lib/parse/literal/LibParseLiteral.sol"; -import {IntegerOverflow} from "rain.math.fixedpoint/error/ErrScale.sol"; -import {LibFixedPointDecimalScale, DECIMAL_MAX_SAFE_INT} from "rain.math.fixedpoint/lib/LibFixedPointDecimalScale.sol"; +import {OperandOverflow} from "src/error/ErrParse.sol"; contract LibParseOperandHandleOperandM1M1Test is Test { // Both values are optional so if nothing is provided everything falls back @@ -25,21 +24,11 @@ contract LibParseOperandHandleOperandM1M1Test is Test { // If one value is provided and it is greater than 1 bit, it is an error. function testHandleOperandM1M1OneValueTooLarge(uint256 value) external { - value = bound(value, 2, DECIMAL_MAX_SAFE_INT); - - // If value is a decimal, scale it above 256 as a decimal. - if (value >= 1e18) { - value = bound(value, 256e18, type(uint256).max); - value = value - (value % 1e18); - } + value = bound(value, 2, uint256(int256(type(int128).max))); uint256[] memory values = new uint256[](1); values[0] = value; - vm.expectRevert( - abi.encodeWithSelector( - IntegerOverflow.selector, LibFixedPointDecimalScale.decimalOrIntToInt(value, DECIMAL_MAX_SAFE_INT), 1 - ) - ); + vm.expectRevert(abi.encodeWithSelector(OperandOverflow.selector)); LibParseOperand.handleOperandM1M1(values); } @@ -57,22 +46,12 @@ contract LibParseOperandHandleOperandM1M1Test is Test { // an error. function testHandleOperandM1M1TwoValuesSecondValueTooLarge(uint256 a, uint256 b) external { a = bound(a, 0, 1); - b = bound(b, 2, DECIMAL_MAX_SAFE_INT); - - // If b is a decimal, scale it above 256 as a decimal. - if (b >= 1e18) { - b = bound(b, 256e18, type(uint256).max); - b = b - (b % 1e18); - } + b = bound(b, 2, uint256(int256(type(int128).max))); uint256[] memory values = new uint256[](2); values[0] = a; values[1] = b; - vm.expectRevert( - abi.encodeWithSelector( - IntegerOverflow.selector, LibFixedPointDecimalScale.decimalOrIntToInt(b, DECIMAL_MAX_SAFE_INT), 1 - ) - ); + vm.expectRevert(abi.encodeWithSelector(OperandOverflow.selector)); LibParseOperand.handleOperandM1M1(values); } diff --git a/test/src/lib/parse/LibParseOperand.handleOperandSingleFull.t.sol b/test/src/lib/parse/LibParseOperand.handleOperandSingleFull.t.sol index 135d4f00..406b4ef3 100644 --- a/test/src/lib/parse/LibParseOperand.handleOperandSingleFull.t.sol +++ b/test/src/lib/parse/LibParseOperand.handleOperandSingleFull.t.sol @@ -5,8 +5,7 @@ import {Test} from "forge-std/Test.sol"; import {LibParseOperand, Operand} from "src/lib/parse/LibParseOperand.sol"; import {UnexpectedOperandValue} from "src/error/ErrParse.sol"; import {LibParseLiteral} from "src/lib/parse/literal/LibParseLiteral.sol"; -import {IntegerOverflow} from "rain.math.fixedpoint/error/ErrScale.sol"; -import {LibFixedPointDecimalScale, DECIMAL_MAX_SAFE_INT} from "rain.math.fixedpoint/lib/LibFixedPointDecimalScale.sol"; +import {OperandOverflow} from "src/error/ErrParse.sol"; contract LibParseOperandHandleOperandSingleFullTest is Test { // No values falls back to zero. @@ -17,25 +16,17 @@ contract LibParseOperandHandleOperandSingleFullTest is Test { // A single value of up to 2 bytes is allowed. function testHandleOperandSingleFullSingleValue(uint256 value) external pure { value = bound(value, 0, type(uint16).max); - uint256 valueScaled = value * 1e18; uint256[] memory values = new uint256[](1); - values[0] = valueScaled; + values[0] = value; assertEq(Operand.unwrap(LibParseOperand.handleOperandSingleFull(values)), value); } // Single values outside 2 bytes are disallowed. function testHandleOperandSingleFullSingleValueDisallowed(uint256 value) external { - value = bound(value, uint256(type(uint16).max) + 1, DECIMAL_MAX_SAFE_INT); - value *= 1e18; + value = bound(value, uint256(type(uint16).max) + 1, uint256(int256(type(int128).max))); uint256[] memory values = new uint256[](1); values[0] = value; - vm.expectRevert( - abi.encodeWithSelector( - IntegerOverflow.selector, - LibFixedPointDecimalScale.decimalOrIntToInt(value, DECIMAL_MAX_SAFE_INT), - 0xFFFF - ) - ); + vm.expectRevert(abi.encodeWithSelector(OperandOverflow.selector)); LibParseOperand.handleOperandSingleFull(values); } diff --git a/test/src/lib/parse/LibParseOperand.handleOperandSingleFullNoDefault.t.sol b/test/src/lib/parse/LibParseOperand.handleOperandSingleFullNoDefault.t.sol index 4749b50f..24e9c4e5 100644 --- a/test/src/lib/parse/LibParseOperand.handleOperandSingleFullNoDefault.t.sol +++ b/test/src/lib/parse/LibParseOperand.handleOperandSingleFullNoDefault.t.sol @@ -5,8 +5,7 @@ import {Test} from "forge-std/Test.sol"; import {LibParseOperand, Operand} from "src/lib/parse/LibParseOperand.sol"; import {UnexpectedOperandValue, ExpectedOperand} from "src/error/ErrParse.sol"; import {LibParseLiteral} from "src/lib/parse/literal/LibParseLiteral.sol"; -import {IntegerOverflow} from "rain.math.fixedpoint/error/ErrScale.sol"; -import {LibFixedPointDecimalScale, DECIMAL_MAX_SAFE_INT} from "rain.math.fixedpoint/lib/LibFixedPointDecimalScale.sol"; +import {OperandOverflow} from "src/error/ErrParse.sol"; contract LibParseOperandHandleOperandSingleFullTest is Test { // No values errors. @@ -25,24 +24,11 @@ contract LibParseOperandHandleOperandSingleFullTest is Test { // Single values outside 2 bytes are disallowed. function testHandleOperandSingleFullSingleValueNoDefaultDisallowed(uint256 value) external { - value = bound(value, uint256(type(uint16).max) + 1, DECIMAL_MAX_SAFE_INT); - value = value * 1e18; - - // If value is a decimal, scale it above 256 as a decimal. - if (value >= 1e18) { - value = bound(value, 256e18, type(uint256).max); - value = value - (value % 1e18); - } + value = bound(value, uint256(type(uint16).max) + 1, uint256(int256(type(int128).max))); uint256[] memory values = new uint256[](1); values[0] = value; - vm.expectRevert( - abi.encodeWithSelector( - IntegerOverflow.selector, - LibFixedPointDecimalScale.decimalOrIntToInt(value, DECIMAL_MAX_SAFE_INT), - 0xFFFF - ) - ); + vm.expectRevert(abi.encodeWithSelector(OperandOverflow.selector)); LibParseOperand.handleOperandSingleFullNoDefault(values); } diff --git a/test/src/lib/parse/LibParseOperand.parseOperand.t.sol b/test/src/lib/parse/LibParseOperand.parseOperand.t.sol index 12ca97ef..3d832975 100644 --- a/test/src/lib/parse/LibParseOperand.parseOperand.t.sol +++ b/test/src/lib/parse/LibParseOperand.parseOperand.t.sol @@ -10,11 +10,14 @@ import {LibMetaFixture} from "test/lib/parse/LibMetaFixture.sol"; import {LibLiteralString} from "test/lib/literal/LibLiteralString.sol"; import {OperandValuesOverflow, UnclosedOperand} from "src/error/ErrParse.sol"; import {LibParseLiteral} from "src/lib/parse/literal/LibParseLiteral.sol"; +import {LibDecimalFloat, LibDecimalFloatImplementation} from "rain.math.float/src/lib/LibDecimalFloat.sol"; +import {SIGNED_NORMALIZED_MAX} from "rain.math.float/src/lib/implementation/LibDecimalFloatImplementation.sol"; contract LibParseOperandParseOperandTest is Test { using LibBytes for bytes; using LibParseOperand for ParseState; using Strings for uint256; + using Strings for int256; function checkParsingOperandFromData(string memory s, uint256[] memory expectedValues, uint256 expectedEnd) internal @@ -61,7 +64,7 @@ contract LibParseOperandParseOperandTest is Test { // Test that we can parse a single literal. function testParseOperandSingleDecimalLiteral( bool asHex, - uint256 value, + int256 value, string memory maybeWhitespaceA, string memory maybeWhitespaceB, string memory suffix @@ -69,13 +72,14 @@ contract LibParseOperandParseOperandTest is Test { LibLiteralString.conformStringToWhitespace(maybeWhitespaceA); LibLiteralString.conformStringToWhitespace(maybeWhitespaceB); - value = bound(value, 0, type(uint256).max / 1e18); + value = bound(value, 0, SIGNED_NORMALIZED_MAX); - string memory valueString = asHex ? value.toHexString() : value.toString(); + string memory valueString = asHex ? uint256(value).toHexString() : value.toString(); string memory s = string.concat("<", maybeWhitespaceA, valueString, maybeWhitespaceB, ">", suffix); uint256[] memory expectedValues = new uint256[](1); - expectedValues[0] = value * (asHex ? 1 : 1e18); + (int256 signedCoefficient, int256 exponent) = LibDecimalFloatImplementation.normalize(value, 0); + expectedValues[0] = asHex ? uint256(value) : LibDecimalFloat.pack(signedCoefficient, exponent); checkParsingOperandFromData( s, @@ -88,8 +92,8 @@ contract LibParseOperandParseOperandTest is Test { function testParseOperandTwoDecimalLiterals( bool asHexA, bool asHexB, - uint256 valueA, - uint256 valueB, + int256 valueA, + int256 valueB, string memory maybeWhitespaceA, string memory maybeWhitespaceB, string memory maybeWhitespaceC, @@ -97,23 +101,26 @@ contract LibParseOperandParseOperandTest is Test { ) external pure { vm.assume(bytes(maybeWhitespaceB).length > 0); - valueA = bound(valueA, 0, type(uint256).max / 1e18); - valueB = bound(valueB, 0, type(uint256).max / 1e18); + valueA = bound(valueA, 0, SIGNED_NORMALIZED_MAX); + valueB = bound(valueB, 0, SIGNED_NORMALIZED_MAX); LibLiteralString.conformStringToWhitespace(maybeWhitespaceA); LibLiteralString.conformStringToWhitespace(maybeWhitespaceB); LibLiteralString.conformStringToWhitespace(maybeWhitespaceC); - string memory valueAString = asHexA ? valueA.toHexString() : valueA.toString(); - string memory valueBString = asHexB ? valueB.toHexString() : valueB.toString(); + string memory valueAString = asHexA ? uint256(valueA).toHexString() : valueA.toString(); + string memory valueBString = asHexB ? uint256(valueB).toHexString() : valueB.toString(); string memory s = string.concat( "<", maybeWhitespaceA, valueAString, maybeWhitespaceB, valueBString, maybeWhitespaceC, ">", suffix ); uint256[] memory expectedValues = new uint256[](2); - expectedValues[0] = valueA * (asHexA ? 1 : 1e18); - expectedValues[1] = valueB * (asHexB ? 1 : 1e18); + (int256 signedCoefficient, int256 exponent) = LibDecimalFloatImplementation.normalize(valueA, 0); + expectedValues[0] = (asHexA ? uint256(valueA) : LibDecimalFloat.pack(signedCoefficient, exponent)); + + (signedCoefficient, exponent) = LibDecimalFloatImplementation.normalize(valueB, 0); + expectedValues[1] = (asHexB ? uint256(valueB) : LibDecimalFloat.pack(signedCoefficient, exponent)); checkParsingOperandFromData( s, @@ -128,9 +135,9 @@ contract LibParseOperandParseOperandTest is Test { bool asHexA, bool asHexB, bool asHexC, - uint256 valueA, - uint256 valueB, - uint256 valueC, + int256 valueA, + int256 valueB, + int256 valueC, string memory maybeWhitespaceA, string memory maybeWhitespaceB, string memory maybeWhitespaceC, @@ -140,46 +147,53 @@ contract LibParseOperandParseOperandTest is Test { vm.assume(bytes(maybeWhitespaceB).length > 0); vm.assume(bytes(maybeWhitespaceC).length > 0); - valueA = bound(valueA, 0, type(uint256).max / 1e18); - valueB = bound(valueB, 0, type(uint256).max / 1e18); - valueC = bound(valueC, 0, type(uint256).max / 1e18); + valueA = bound(valueA, 0, SIGNED_NORMALIZED_MAX); + valueB = bound(valueB, 0, SIGNED_NORMALIZED_MAX); + valueC = bound(valueC, 0, SIGNED_NORMALIZED_MAX); LibLiteralString.conformStringToWhitespace(maybeWhitespaceA); LibLiteralString.conformStringToWhitespace(maybeWhitespaceB); LibLiteralString.conformStringToWhitespace(maybeWhitespaceC); LibLiteralString.conformStringToWhitespace(maybeWhitespaceD); - string memory valueAString = asHexA ? valueA.toHexString() : valueA.toString(); - string memory valueBString = asHexB ? valueB.toHexString() : valueB.toString(); - string memory valueCString = asHexC ? valueC.toHexString() : valueC.toString(); - - string memory s = string.concat( - string.concat( - "<", maybeWhitespaceA, valueAString, maybeWhitespaceB, valueBString, maybeWhitespaceC, valueCString - ), - maybeWhitespaceD, - ">", - suffix - ); + string memory s; + uint256 expectedLength; + { + string memory valueAString = asHexA ? uint256(valueA).toHexString() : valueA.toString(); + string memory valueBString = asHexB ? uint256(valueB).toHexString() : valueB.toString(); + string memory valueCString = asHexC ? uint256(valueC).toHexString() : valueC.toString(); + + s = string.concat( + string.concat( + "<", maybeWhitespaceA, valueAString, maybeWhitespaceB, valueBString, maybeWhitespaceC, valueCString + ), + maybeWhitespaceD, + ">", + suffix + ); + + expectedLength = bytes(valueAString).length + bytes(valueBString).length + bytes(valueCString).length + 2 + + bytes(maybeWhitespaceA).length + bytes(maybeWhitespaceB).length + bytes(maybeWhitespaceC).length + + bytes(maybeWhitespaceD).length; + } uint256[] memory expectedValues = new uint256[](3); - expectedValues[0] = valueA * (asHexA ? 1 : 1e18); - expectedValues[1] = valueB * (asHexB ? 1 : 1e18); - expectedValues[2] = valueC * (asHexC ? 1 : 1e18); + (int256 signedCoefficient, int256 exponent) = LibDecimalFloatImplementation.normalize(valueA, 0); + expectedValues[0] = (asHexA ? uint256(valueA) : LibDecimalFloat.pack(signedCoefficient, exponent)); - checkParsingOperandFromData( - s, - expectedValues, - bytes(valueAString).length + bytes(valueBString).length + bytes(valueCString).length + 2 - + bytes(maybeWhitespaceA).length + bytes(maybeWhitespaceB).length + bytes(maybeWhitespaceC).length - + bytes(maybeWhitespaceD).length - ); + (signedCoefficient, exponent) = LibDecimalFloatImplementation.normalize(valueB, 0); + expectedValues[1] = (asHexB ? uint256(valueB) : LibDecimalFloat.pack(signedCoefficient, exponent)); + + (signedCoefficient, exponent) = LibDecimalFloatImplementation.normalize(valueC, 0); + expectedValues[2] = (asHexC ? uint256(valueC) : LibDecimalFloat.pack(signedCoefficient, exponent)); + + checkParsingOperandFromData(s, expectedValues, expectedLength); } // Test that we can parse four literals. function testParseOperandFourDecimalLiterals( bool[4] memory asHex, - uint256[4] memory values, + int256[4] memory values, string[5] memory maybeWhitespace, string memory suffix ) external pure { @@ -195,7 +209,7 @@ contract LibParseOperandParseOperandTest is Test { } for (uint256 i = 0; i < 4; i++) { - values[i] = bound(values[i], 0, type(uint256).max / 1e18); + values[i] = bound(values[i], 0, SIGNED_NORMALIZED_MAX); } uint256 expectedLength; @@ -204,10 +218,10 @@ contract LibParseOperandParseOperandTest is Test { + bytes(maybeWhitespace[2]).length + bytes(maybeWhitespace[3]).length + bytes(maybeWhitespace[4]).length; } - string memory valueAString = asHex[0] ? values[0].toHexString() : values[0].toString(); - string memory valueBString = asHex[1] ? values[1].toHexString() : values[1].toString(); - string memory valueCString = asHex[2] ? values[2].toHexString() : values[2].toString(); - string memory valueDString = asHex[3] ? values[3].toHexString() : values[3].toString(); + string memory valueAString = asHex[0] ? uint256(values[0]).toHexString() : values[0].toString(); + string memory valueBString = asHex[1] ? uint256(values[1]).toHexString() : values[1].toString(); + string memory valueCString = asHex[2] ? uint256(values[2]).toHexString() : values[2].toString(); + string memory valueDString = asHex[3] ? uint256(values[3]).toHexString() : values[3].toString(); { expectedLength += bytes(valueAString).length + bytes(valueBString).length + bytes(valueCString).length @@ -231,7 +245,8 @@ contract LibParseOperandParseOperandTest is Test { uint256[] memory expectedValues = new uint256[](4); for (uint256 i = 0; i < 4; i++) { - expectedValues[i] = values[i] * (asHex[i] ? 1 : 1e18); + (int256 signedCoefficient, int256 exponent) = LibDecimalFloatImplementation.normalize(values[i], 0); + expectedValues[i] = asHex[i] ? uint256(values[i]) : LibDecimalFloat.pack(signedCoefficient, exponent); } checkParsingOperandFromData(s, expectedValues, expectedLength); } diff --git a/test/src/lib/parse/literal/LibParseLiteralDecimal.parseDecimal.t.sol b/test/src/lib/parse/literal/LibParseLiteralDecimal.parseDecimal.t.sol deleted file mode 100644 index 1c510c1b..00000000 --- a/test/src/lib/parse/literal/LibParseLiteralDecimal.parseDecimal.t.sol +++ /dev/null @@ -1,524 +0,0 @@ -// SPDX-License-Identifier: CAL -pragma solidity =0.8.25; - -import {Test} from "forge-std/Test.sol"; - -import {Strings} from "openzeppelin-contracts/contracts/utils/Strings.sol"; -import {LibBytes, Pointer} from "rain.solmem/lib/LibBytes.sol"; -import {LibParseLiteral, ZeroLengthDecimal} from "src/lib/parse/literal/LibParseLiteral.sol"; -import {LibParseState, ParseState} from "src/lib/parse/LibParseState.sol"; -import {LibAllStandardOpsNP} from "src/lib/op/LibAllStandardOpsNP.sol"; -import {LibParseLiteralDecimal} from "src/lib/parse/literal/LibParseLiteralDecimal.sol"; -import { - MalformedExponentDigits, - MalformedDecimalPoint, - DecimalLiteralOverflow, - DecimalLiteralPrecisionLoss -} from "src/error/ErrParse.sol"; - -/// @title LibParseLiteralDecimalTest -/// Tests parsing decimal literal values with the LibParseLiteral library. -contract LibParseLiteralDecimalTest is Test { - using LibBytes for bytes; - using LibParseLiteral for ParseState; - using LibParseLiteralDecimal for ParseState; - - function checkParseDecimal(string memory data, uint256 expectedValue, uint256 expectedCursorAfter) internal pure { - ParseState memory state = LibParseState.newState(bytes(data), "", "", ""); - uint256 cursor = Pointer.unwrap(state.data.dataPointer()); - (uint256 cursorAfter, uint256 value) = state.parseDecimal(cursor, Pointer.unwrap(state.data.endDataPointer())); - assertEq(cursorAfter - cursor, expectedCursorAfter); - assertEq(value, expectedValue); - } - - function checkParseDecimalRevert(string memory data, bytes memory err) internal { - ParseState memory state = LibParseState.newState(bytes(data), "", "", ""); - vm.expectRevert(err); - (uint256 cursorAfter, uint256 value) = - state.parseDecimal(Pointer.unwrap(state.data.dataPointer()), Pointer.unwrap(state.data.endDataPointer())); - (cursorAfter, value); - } - - /// Check that an empty string literal is an error. - function testParseLiteralDecimalEmpty() external { - ParseState memory state = LibParseState.newState("", "", "", ""); - vm.expectRevert(abi.encodeWithSelector(ZeroLengthDecimal.selector, 0)); - (uint256 cursorAfter, uint256 value) = - state.parseDecimal(Pointer.unwrap(state.data.dataPointer()), Pointer.unwrap(state.data.endDataPointer())); - (cursorAfter, value); - } - - /// A non decimal literal is an error. - function testParseLiteralDecimalNonDecimal() external { - ParseState memory state = LibParseState.newState("hello", "", "", ""); - vm.expectRevert(abi.encodeWithSelector(ZeroLengthDecimal.selector, 0)); - (uint256 cursorAfter, uint256 value) = - state.parseDecimal(Pointer.unwrap(state.data.dataPointer()), Pointer.unwrap(state.data.endDataPointer())); - (cursorAfter, value); - } - - /// Fuzz and round trip. - function testParseLiteralDecimalRoundTrip(uint256 value) external pure { - value = bound(value, 0, type(uint256).max / 1e18); - string memory valueStr = Strings.toString(value); - checkParseDecimal(valueStr, value * 1e18, bytes(valueStr).length); - } - - /// Check some specific examples. - function testParseLiteralDecimalSpecific() external pure { - checkParseDecimal("0", 0e18, 1); - checkParseDecimal("1", 1e18, 1); - checkParseDecimal("2", 2e18, 1); - checkParseDecimal("3", 3e18, 1); - checkParseDecimal("4", 4e18, 1); - checkParseDecimal("5", 5e18, 1); - checkParseDecimal("6", 6e18, 1); - checkParseDecimal("7", 7e18, 1); - checkParseDecimal("8", 8e18, 1); - checkParseDecimal("9", 9e18, 1); - checkParseDecimal("10", 10e18, 2); - } - - /// Check some examples of decimals. - function testParseLiteralDecimalDecimals() external pure { - checkParseDecimal("0.0", 0e18, 3); - checkParseDecimal("1.0", 1e18, 3); - checkParseDecimal("2.0", 2e18, 3); - checkParseDecimal("3.0", 3e18, 3); - checkParseDecimal("4.0", 4e18, 3); - checkParseDecimal("5.0", 5e18, 3); - checkParseDecimal("6.0", 6e18, 3); - checkParseDecimal("7.0", 7e18, 3); - checkParseDecimal("8.0", 8e18, 3); - checkParseDecimal("9.0", 9e18, 3); - checkParseDecimal("10.0", 10e18, 4); - - checkParseDecimal("0.1", 0.1e18, 3); - checkParseDecimal("1.1", 1.1e18, 3); - checkParseDecimal("2.1", 2.1e18, 3); - checkParseDecimal("3.1", 3.1e18, 3); - checkParseDecimal("4.1", 4.1e18, 3); - checkParseDecimal("5.1", 5.1e18, 3); - checkParseDecimal("6.1", 6.1e18, 3); - checkParseDecimal("7.1", 7.1e18, 3); - checkParseDecimal("8.1", 8.1e18, 3); - checkParseDecimal("9.1", 9.1e18, 3); - checkParseDecimal("10.1", 10.1e18, 4); - - checkParseDecimal("0.01", 0.01e18, 4); - checkParseDecimal("1.01", 1.01e18, 4); - checkParseDecimal("2.01", 2.01e18, 4); - checkParseDecimal("3.01", 3.01e18, 4); - checkParseDecimal("4.01", 4.01e18, 4); - checkParseDecimal("5.01", 5.01e18, 4); - checkParseDecimal("6.01", 6.01e18, 4); - checkParseDecimal("7.01", 7.01e18, 4); - checkParseDecimal("8.01", 8.01e18, 4); - checkParseDecimal("9.01", 9.01e18, 4); - checkParseDecimal("10.01", 10.01e18, 5); - } - - /// Check some examples of exponents. - /// Checks e in the 2nd position. - function testParseLiteralDecimalExponents() external pure { - checkParseDecimal("0e0", 0e18, 3); - checkParseDecimal("1e0", 1e18, 3); - checkParseDecimal("2e0", 2e18, 3); - checkParseDecimal("3e0", 3e18, 3); - checkParseDecimal("4e0", 4e18, 3); - checkParseDecimal("5e0", 5e18, 3); - checkParseDecimal("6e0", 6e18, 3); - checkParseDecimal("7e0", 7e18, 3); - checkParseDecimal("8e0", 8e18, 3); - checkParseDecimal("9e0", 9e18, 3); - checkParseDecimal("10e0", 10e18, 4); - - checkParseDecimal("0e1", 0e18, 3); - checkParseDecimal("1e1", 10e18, 3); - checkParseDecimal("2e1", 20e18, 3); - checkParseDecimal("3e1", 30e18, 3); - checkParseDecimal("4e1", 40e18, 3); - checkParseDecimal("5e1", 50e18, 3); - checkParseDecimal("6e1", 60e18, 3); - checkParseDecimal("7e1", 70e18, 3); - checkParseDecimal("8e1", 80e18, 3); - checkParseDecimal("9e1", 90e18, 3); - checkParseDecimal("10e1", 100e18, 4); - - checkParseDecimal("0e2", 0e18, 3); - checkParseDecimal("1e2", 100e18, 3); - checkParseDecimal("2e2", 200e18, 3); - checkParseDecimal("3e2", 300e18, 3); - checkParseDecimal("4e2", 400e18, 3); - checkParseDecimal("5e2", 500e18, 3); - checkParseDecimal("6e2", 600e18, 3); - checkParseDecimal("7e2", 700e18, 3); - checkParseDecimal("8e2", 800e18, 3); - checkParseDecimal("9e2", 900e18, 3); - checkParseDecimal("10e2", 1000e18, 4); - } - - /// Check some examples of exponents. - /// Checks e in the 3rd position. - function testParseLiteralDecimalExponents2() external pure { - checkParseDecimal("0e00", 0e18, 4); - checkParseDecimal("1e00", 1e18, 4); - checkParseDecimal("2e00", 2e18, 4); - checkParseDecimal("3e00", 3e18, 4); - checkParseDecimal("4e00", 4e18, 4); - checkParseDecimal("5e00", 5e18, 4); - checkParseDecimal("6e00", 6e18, 4); - checkParseDecimal("7e00", 7e18, 4); - checkParseDecimal("8e00", 8e18, 4); - checkParseDecimal("9e00", 9e18, 4); - checkParseDecimal("10e00", 10e18, 5); - - checkParseDecimal("0e01", 0e18, 4); - checkParseDecimal("1e01", 10e18, 4); - checkParseDecimal("2e01", 20e18, 4); - checkParseDecimal("3e01", 30e18, 4); - checkParseDecimal("4e01", 40e18, 4); - checkParseDecimal("5e01", 50e18, 4); - checkParseDecimal("6e01", 60e18, 4); - checkParseDecimal("7e01", 70e18, 4); - checkParseDecimal("8e01", 80e18, 4); - checkParseDecimal("9e01", 90e18, 4); - checkParseDecimal("10e01", 100e18, 5); - - checkParseDecimal("0e02", 0e18, 4); - checkParseDecimal("1e02", 100e18, 4); - checkParseDecimal("2e02", 200e18, 4); - checkParseDecimal("3e02", 300e18, 4); - checkParseDecimal("4e02", 400e18, 4); - checkParseDecimal("5e02", 500e18, 4); - checkParseDecimal("6e02", 600e18, 4); - checkParseDecimal("7e02", 700e18, 4); - checkParseDecimal("8e02", 800e18, 4); - checkParseDecimal("9e02", 900e18, 4); - checkParseDecimal("10e02", 1000e18, 5); - - checkParseDecimal("0e10", 0e18, 4); - checkParseDecimal("1e10", 1e28, 4); - checkParseDecimal("2e10", 2e28, 4); - checkParseDecimal("3e10", 3e28, 4); - checkParseDecimal("4e10", 4e28, 4); - checkParseDecimal("5e10", 5e28, 4); - checkParseDecimal("6e10", 6e28, 4); - checkParseDecimal("7e10", 7e28, 4); - checkParseDecimal("8e10", 8e28, 4); - checkParseDecimal("9e10", 9e28, 4); - checkParseDecimal("10e10", 10e28, 5); - } - - // Test integer with capital E - function testParseLiteralDecimalExponents2Capital() external pure { - checkParseDecimal("0E00", 0e18, 4); - checkParseDecimal("1E00", 1e18, 4); - checkParseDecimal("2E00", 2e18, 4); - checkParseDecimal("3E00", 3e18, 4); - checkParseDecimal("4E00", 4e18, 4); - checkParseDecimal("5E00", 5e18, 4); - checkParseDecimal("6E00", 6e18, 4); - checkParseDecimal("7E00", 7e18, 4); - checkParseDecimal("8E00", 8e18, 4); - checkParseDecimal("9E00", 9e18, 4); - checkParseDecimal("10E00", 10e18, 5); - - checkParseDecimal("0E01", 0e18, 4); - checkParseDecimal("1E01", 10e18, 4); - checkParseDecimal("2E01", 20e18, 4); - checkParseDecimal("3E01", 30e18, 4); - checkParseDecimal("4E01", 40e18, 4); - checkParseDecimal("5E01", 50e18, 4); - checkParseDecimal("6E01", 60e18, 4); - checkParseDecimal("7E01", 70e18, 4); - checkParseDecimal("8E01", 80e18, 4); - checkParseDecimal("9E01", 90e18, 4); - checkParseDecimal("10E01", 100e18, 5); - - checkParseDecimal("0E02", 0e18, 4); - checkParseDecimal("1E02", 100e18, 4); - checkParseDecimal("2E02", 200e18, 4); - checkParseDecimal("3E02", 300e18, 4); - checkParseDecimal("4E02", 400e18, 4); - checkParseDecimal("5E02", 500e18, 4); - checkParseDecimal("6E02", 600e18, 4); - checkParseDecimal("7E02", 700e18, 4); - checkParseDecimal("8E02", 800e18, 4); - checkParseDecimal("9E02", 900e18, 4); - } - - // Test decimals with exponents. - function testParseLiteralDecimalExponents3() external pure { - checkParseDecimal("0.0e0", 0, 5); - checkParseDecimal("1.0e0", 1e18, 5); - checkParseDecimal("2.0e0", 2e18, 5); - checkParseDecimal("3.0e0", 3e18, 5); - checkParseDecimal("4.0e0", 4e18, 5); - checkParseDecimal("5.0e0", 5e18, 5); - checkParseDecimal("6.0e0", 6e18, 5); - checkParseDecimal("7.0e0", 7e18, 5); - checkParseDecimal("8.0e0", 8e18, 5); - checkParseDecimal("9.0e0", 9e18, 5); - checkParseDecimal("10.0e0", 10e18, 6); - - checkParseDecimal("0.1e0", 0.1e18, 5); - checkParseDecimal("1.1e0", 1.1e18, 5); - checkParseDecimal("2.1e0", 2.1e18, 5); - checkParseDecimal("3.1e0", 3.1e18, 5); - checkParseDecimal("4.1e0", 4.1e18, 5); - checkParseDecimal("5.1e0", 5.1e18, 5); - checkParseDecimal("6.1e0", 6.1e18, 5); - checkParseDecimal("7.1e0", 7.1e18, 5); - checkParseDecimal("8.1e0", 8.1e18, 5); - checkParseDecimal("9.1e0", 9.1e18, 5); - checkParseDecimal("10.1e0", 10.1e18, 6); - - checkParseDecimal("0.01e000", 0.01e18, 8); - - checkParseDecimal("0.01e0", 0.01e18, 6); - checkParseDecimal("1.01e0", 1.01e18, 6); - checkParseDecimal("2.01e0", 2.01e18, 6); - checkParseDecimal("3.01e0", 3.01e18, 6); - - checkParseDecimal("0.0e1", 0.0e19, 5); - checkParseDecimal("1.0e1", 1.0e19, 5); - checkParseDecimal("2.0e1", 2.0e19, 5); - checkParseDecimal("3.0e1", 3.0e19, 5); - checkParseDecimal("4.0e1", 4.0e19, 5); - checkParseDecimal("5.0e1", 5.0e19, 5); - - checkParseDecimal("0.0e001", 0.0e20, 7); - - checkParseDecimal("0.0e2", 0.0e20, 5); - checkParseDecimal("1.0e2", 1.0e20, 5); - checkParseDecimal("2.0e2", 2.0e20, 5); - checkParseDecimal("3.0e2", 3.0e20, 5); - checkParseDecimal("4.0e2", 4.0e20, 5); - checkParseDecimal("5.0e2", 5.0e20, 5); - - checkParseDecimal("0.0101e10", 0.0101e28, 9); - checkParseDecimal("1.0101e10", 1.0101e28, 9); - checkParseDecimal("2.0101e10", 2.0101e28, 9); - checkParseDecimal("3.0101e10", 3.0101e28, 9); - } - - /// Test capital E - function testParseLiteralDecimalExponents4() external pure { - checkParseDecimal("0.0E0", 0, 5); - checkParseDecimal("1.0E0", 1e18, 5); - checkParseDecimal("2.0E0", 2e18, 5); - checkParseDecimal("3.0E0", 3e18, 5); - checkParseDecimal("4.0E0", 4e18, 5); - checkParseDecimal("5.0E0", 5e18, 5); - checkParseDecimal("6.0E0", 6e18, 5); - checkParseDecimal("7.0E0", 7e18, 5); - checkParseDecimal("8.0E0", 8e18, 5); - checkParseDecimal("9.0E0", 9e18, 5); - checkParseDecimal("10.0E0", 10e18, 6); - - checkParseDecimal("0.1E0", 0.1e18, 5); - checkParseDecimal("1.1E0", 1.1e18, 5); - checkParseDecimal("2.1E0", 2.1e18, 5); - checkParseDecimal("3.1E0", 3.1e18, 5); - checkParseDecimal("4.1E0", 4.1e18, 5); - checkParseDecimal("5.1E0", 5.1e18, 5); - checkParseDecimal("6.1E0", 6.1e18, 5); - checkParseDecimal("7.1E0", 7.1e18, 5); - checkParseDecimal("8.1E0", 8.1e18, 5); - checkParseDecimal("9.1E0", 9.1e18, 5); - checkParseDecimal("10.1E0", 10.1e18, 6); - - checkParseDecimal("0.01E000", 0.01e18, 8); - - checkParseDecimal("0.01E0", 0.01e18, 6); - } - - /// Test some negative exponents. - function testParseLiteralDecimalNegativeExponents() external pure { - checkParseDecimal("0.0e-0", 0, 6); - checkParseDecimal("1.0e-0", 1e18, 6); - checkParseDecimal("2.0e-0", 2e18, 6); - checkParseDecimal("3.0e-0", 3e18, 6); - checkParseDecimal("4.0e-0", 4e18, 6); - checkParseDecimal("5.0e-0", 5e18, 6); - checkParseDecimal("6.0e-0", 6e18, 6); - checkParseDecimal("7.0e-0", 7e18, 6); - checkParseDecimal("8.0e-0", 8e18, 6); - checkParseDecimal("9.0e-0", 9e18, 6); - checkParseDecimal("10.0e-0", 10e18, 7); - - checkParseDecimal("0.1e-0", 0.1e18, 6); - checkParseDecimal("1.1e-0", 1.1e18, 6); - checkParseDecimal("2.1e-0", 2.1e18, 6); - checkParseDecimal("3.1e-0", 3.1e18, 6); - checkParseDecimal("4.1e-0", 4.1e18, 6); - checkParseDecimal("5.1e-0", 5.1e18, 6); - checkParseDecimal("6.1e-0", 6.1e18, 6); - checkParseDecimal("7.1e-0", 7.1e18, 6); - checkParseDecimal("8.1e-0", 8.1e18, 6); - checkParseDecimal("9.1e-0", 9.1e18, 6); - checkParseDecimal("10.1e-0", 10.1e18, 7); - - checkParseDecimal("0.01e-0", 0.01e18, 7); - - checkParseDecimal("0.0e-1", 0.0e17, 6); - checkParseDecimal("1.0e-1", 1.0e17, 6); - checkParseDecimal("2.0e-1", 2.0e17, 6); - checkParseDecimal("3.0e-1", 3.0e17, 6); - checkParseDecimal("4.0e-1", 4.0e17, 6); - - checkParseDecimal("0.0e-2", 0.0e16, 6); - checkParseDecimal("1.0e-2", 1.0e16, 6); - checkParseDecimal("2.0e-2", 2.0e16, 6); - checkParseDecimal("3.0e-2", 3.0e16, 6); - - checkParseDecimal("0.0101e-10", 0.0101e8, 10); - checkParseDecimal("1.0101e-10", 1.0101e8, 10); - checkParseDecimal("2.0101e-10", 2.0101e8, 10); - - checkParseDecimal("0.0e-18", 0, 7); - checkParseDecimal("1.0e-18", 1, 7); - checkParseDecimal("2.0e-18", 2, 7); - } - - /// Test trailing zeros. - function testParseLiteralDecimalTrailingZeros() external pure { - checkParseDecimal("0.000000000000000000", 0, 20); - checkParseDecimal("1.000000000000000000", 1e18, 20); - checkParseDecimal("2.000000000000000000", 2e18, 20); - checkParseDecimal("3.000000000000000000", 3e18, 20); - checkParseDecimal("4.000000000000000000", 4e18, 20); - checkParseDecimal("5.000000000000000000", 5e18, 20); - checkParseDecimal("6.000000000000000000", 6e18, 20); - checkParseDecimal("7.000000000000000000", 7e18, 20); - checkParseDecimal("8.000000000000000000", 8e18, 20); - checkParseDecimal("9.000000000000000000", 9e18, 20); - checkParseDecimal("10.000000000000000000", 10e18, 21); - - checkParseDecimal("0.100000000000000000", 0.1e18, 20); - checkParseDecimal("1.100000000000000000", 1.1e18, 20); - checkParseDecimal("2.100000000000000000", 2.1e18, 20); - checkParseDecimal("3.100000000000000000", 3.1e18, 20); - checkParseDecimal("4.100000000000000000", 4.1e18, 20); - checkParseDecimal("5.100000000000000000", 5.1e18, 20); - checkParseDecimal("6.100000000000000000", 6.1e18, 20); - checkParseDecimal("7.100000000000000000", 7.1e18, 20); - checkParseDecimal("8.100000000000000000", 8.1e18, 20); - checkParseDecimal("9.100000000000000000", 9.1e18, 20); - - checkParseDecimal("0.010000000000000000e5", 0.01e23, 22); - checkParseDecimal("1.010000000000000000e-5", 1.01e13, 23); - checkParseDecimal("2.000000000000000000e-18", 2, 24); - } - - // Test some unrelated data after the decimal. - function testParseLiteralDecimalUnrelated() external pure { - checkParseDecimal("0.0hello", 0, 3); - checkParseDecimal("1.0hello", 1e18, 3); - checkParseDecimal("2.0hello", 2e18, 3); - checkParseDecimal("3.0hello", 3e18, 3); - - checkParseDecimal("0.0e0e10", 0, 5); - checkParseDecimal("1.0e0e10", 1e18, 5); - checkParseDecimal("2.0e0e10", 2e18, 5); - - checkParseDecimal("0.0e0.5", 0, 5); - checkParseDecimal("1.0e0.5", 1e18, 5); - checkParseDecimal("2.0e0.5", 2e18, 5); - - checkParseDecimal("0.0e1.5", 0, 5); - checkParseDecimal("1.0e1.5", 1e19, 5); - checkParseDecimal("2.0e1.5", 2e19, 5); - } - - // e without a digit is an error. - function testParseLiteralDecimalExponentsError() external { - checkParseDecimalRevert("e", abi.encodeWithSelector(ZeroLengthDecimal.selector, 0)); - } - - // e with a left digit but not a right digit is an error. - function testParseLiteralDecimalExponentsError3() external { - checkParseDecimalRevert("1e", abi.encodeWithSelector(MalformedExponentDigits.selector, 2)); - } - - // e with a right digit but not a left digit is an error. - // This should never happen in practise as it would be parsed as a word not - // a literal. - // Tests e in the 2nd place. - function testParseLiteralDecimalExponentsError4() external { - checkParseDecimalRevert("e0", abi.encodeWithSelector(ZeroLengthDecimal.selector, 0)); - } - - // e with a right digit but not a left digit is an error. - // This should never happen in practise as it would be parsed as a word not - // a literal. - // Tests e in the 3rd place. - function testParseLiteralDecimalExponentsError5() external { - checkParseDecimalRevert("e00", abi.encodeWithSelector(ZeroLengthDecimal.selector, 0)); - } - - /// Dot without digits is an error. - function testParseLiteralDecimalDotError() external { - checkParseDecimalRevert(".", abi.encodeWithSelector(ZeroLengthDecimal.selector, 0)); - } - - /// Dot without leading digits is an error. - function testParseLiteralDecimalDotError2() external { - checkParseDecimalRevert(".0", abi.encodeWithSelector(ZeroLengthDecimal.selector, 0)); - } - - /// Dot without trailing digits is an error. - function testParseLiteralDecimalDotError3() external { - checkParseDecimalRevert("0.", abi.encodeWithSelector(MalformedDecimalPoint.selector, 2)); - } - - /// Dot e is an error. - function testParseLiteralDecimalDotError4() external { - checkParseDecimalRevert(".e", abi.encodeWithSelector(ZeroLengthDecimal.selector, 0)); - } - - /// Dot e0 is an error. - function testParseLiteralDecimalDotError5() external { - checkParseDecimalRevert(".e0", abi.encodeWithSelector(ZeroLengthDecimal.selector, 0)); - } - - /// e Dot is an error. - function testParseLiteralDecimalDotError6() external { - checkParseDecimalRevert("e.", abi.encodeWithSelector(ZeroLengthDecimal.selector, 0)); - } - - /// Negative e with no digits is an error. - function testParseLiteralDecimalNegativeExponentsError() external { - checkParseDecimalRevert("0.0e-", abi.encodeWithSelector(MalformedExponentDigits.selector, 5)); - } - - /// Large e will cause overflow. - function testParseLiteralDecimalOverflow() external { - checkParseDecimalRevert("1.0e100", abi.encodeWithSelector(DecimalLiteralOverflow.selector, 7)); - } - - /// Integer precision loss will revert. - function testParseLiteralDecimalPrecisionLossInteger() external { - checkParseDecimalRevert("1.0e-19", abi.encodeWithSelector(DecimalLiteralPrecisionLoss.selector, 7)); - } - - /// Decimal precision loss will revert. - function testParseLiteralDecimalPrecisionLossDecimal() external { - checkParseDecimalRevert("1.5e-18", abi.encodeWithSelector(DecimalLiteralPrecisionLoss.selector, 7)); - } - - /// Decimal precision loss will revert. Max zeros. - function testParseLiteralDecimalPrecisionLossDecimalMax() external { - checkParseDecimalRevert( - "1.000000000000000001e-1", abi.encodeWithSelector(DecimalLiteralPrecisionLoss.selector, 23) - ); - } - - /// Decimal precision loss will revert. No e, just too small. - function testParseLiteralDecimalPrecisionLossDecimalSmall() external { - checkParseDecimalRevert( - "0.0000000000000000001", abi.encodeWithSelector(DecimalLiteralPrecisionLoss.selector, 21) - ); - } -} diff --git a/test/src/lib/parse/literal/LibParseLiteralSubParseable.parseSubParseable.t.sol b/test/src/lib/parse/literal/LibParseLiteralSubParseable.parseSubParseable.t.sol index 2acfaadc..fbc6d508 100644 --- a/test/src/lib/parse/literal/LibParseLiteralSubParseable.parseSubParseable.t.sol +++ b/test/src/lib/parse/literal/LibParseLiteralSubParseable.parseSubParseable.t.sol @@ -6,9 +6,10 @@ import {ParseState, Pointer, LibParseState} from "src/lib/parse/LibParseState.so import {LibBytes} from "rain.solmem/lib/LibBytes.sol"; import {LibParseLiteralSubParseable} from "src/lib/parse/literal/LibParseLiteralSubParseable.sol"; import {UnclosedSubParseableLiteral, SubParseableMissingDispatch} from "src/error/ErrParse.sol"; -import {ISubParserV3, COMPATIBILITY_V4} from "rain.interpreter.interface/interface/ISubParserV3.sol"; +import {ISubParserV3} from "rain.interpreter.interface/interface/ISubParserV3.sol"; import {LibLiteralString} from "test/lib/literal/LibLiteralString.sol"; import {CMASK_WHITESPACE, CMASK_SUB_PARSEABLE_LITERAL_END} from "src/lib/parse/LibParseCMask.sol"; +import {CURRENT_COMPATIBILITY} from "src/lib/parse/LibSubParse.sol"; contract LibParseLiteralSubParseableTest is Test { using LibBytes for bytes; @@ -30,11 +31,12 @@ contract LibParseLiteralSubParseableTest is Test { uint256 returnValue = 99; vm.mockCall( subParser, - abi.encodeWithSelector(ISubParserV3.subParseLiteral.selector, COMPATIBILITY_V4, subParseData), + abi.encodeWithSelector(ISubParserV3.subParseLiteral.selector, CURRENT_COMPATIBILITY, subParseData), abi.encode(true, returnValue) ); vm.expectCall( - subParser, abi.encodeWithSelector(ISubParserV3.subParseLiteral.selector, COMPATIBILITY_V4, subParseData) + subParser, + abi.encodeWithSelector(ISubParserV3.subParseLiteral.selector, CURRENT_COMPATIBILITY, subParseData) ); (uint256 cursorAfter, uint256 value) = state.parseSubParseable(cursor, Pointer.unwrap(state.data.endDataPointer()));