Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/rain.interpreter.interface
4 changes: 2 additions & 2 deletions src/generated/Rainterpreter.pointers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
pragma solidity =0.8.25;

/// @dev Hash of the known bytecode.
bytes32 constant BYTECODE_HASH = bytes32(0x1a3fbd5bbbf67323604d9d6e171b5922ce1c661c9f890518b6d8c7c948349acb);
bytes32 constant BYTECODE_HASH = bytes32(0xef5d5c21b4a25cc7b3dfdd4402f2d8a35099c9d415c4b6a4f0f76ebfed67e0b0);

/// @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"07ab07dd0801098d0a560a680a7a0a930ab70aeb0afc0b0d0baf0bce0c8c0d3c0dc00f0210350c8c112e11e0128212fa130b131c131c132d137413f6140f1423143c14671480149314f51543159115df162d167b16c917171748179617e418321919";
hex"07bd07ef0813099f0a680a7a0a8c0aa50ac90afd0b0e0b1f0bc10be00c9e0d4e0dd20f1410470c9e114011f21294130c131d132e132e133f1386140814211435144e1467149214ab14c414d71539158715d51623167116bf170d175b178c17da18281876195d";
6 changes: 3 additions & 3 deletions src/generated/RainterpreterExpressionDeployer.pointers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
pragma solidity =0.8.25;

/// @dev Hash of the known bytecode.
bytes32 constant BYTECODE_HASH = bytes32(0xd09b4991a12ba8c2a22269ff879e110f22450dba8c3850ebdfd90d37a76936a9);
bytes32 constant BYTECODE_HASH = bytes32(0xce3a44f2ed702547f2da7cc4e7adfc9d9dd5a2bfbfd806b228db3cfcdd1da125);

/// @dev The hash of the meta that describes the contract.
bytes32 constant DESCRIBED_BY_META_HASH = bytes32(0x7145a744b38f6a8a05a5a427fd2200e8c6e21c50eb3fd5642d251ecac809f67e);
bytes32 constant DESCRIBED_BY_META_HASH = bytes32(0xf8bdd3a04e26f925f45376a4f3cfe9455904c1abbbfbf60f84d982803beea817);

/// @dev The function pointers for the integrity check fns.
bytes constant INTEGRITY_FUNCTION_POINTERS =
hex"0e400ebe0f22109c10a610a610b010b910d4117a117a11d6124e125b10a610b0125b10a610b010a610a610a610b0109c109c109c109c1265128a10a610a610a610b010a6109c12941294129412941294129412ae1294109c129412ae12ae10b0128a";
hex"0e500ece0f3210ac10b610b610c010c910e4118a118a11e6125e126b10b610c0126b10b610c010b610b610b610c010ac10ac10ac10ac1275129a10b610b610b610b610c010b610b610ac12a412a412a412a412a412a412be12a410ac12a412be12be10c0129a";
8 changes: 4 additions & 4 deletions src/generated/RainterpreterParser.pointers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
pragma solidity =0.8.25;

/// @dev Hash of the known bytecode.
bytes32 constant BYTECODE_HASH = bytes32(0x2e38cc130243a36a0482c2e7e70b5928cedb02e1ad0972b7bd7b232b936b7eae);
bytes32 constant BYTECODE_HASH = bytes32(0x802ffd0c76897b508453402052a3a1840b73d060ed67e227d1c874d3cff34ec4);

/// @dev The parse meta that is used to lookup word definitions.
/// The structure of the parse meta is:
Expand All @@ -29,7 +29,7 @@ bytes32 constant BYTECODE_HASH = bytes32(0x2e38cc130243a36a0482c2e7e70b5928cedb0
/// bit count of the previous bloom filter. If we reach the end of the bloom
/// filters then we have a miss.
bytes constant PARSE_META =
hex"0178ac806000299010810908a00a34484001280030060100180000200cc2200408a00c95d8382f48145c3034b3f32bd587e204afc8f01e95a26007b9a51f0d5e6f2803ce312215fae0a1251233de118cfd53051c784d23a55ade1681ac9621ac6dfc2ca4e5e91f5fc32a0e92b2401dbbd837100ef76b00cd8814289714ec13313592270d473b20f880501427634f0f23b4970ac0e53d0b895f991ac908be0983724f12c080bc2d9d7b7d26757aea246f62e0295c0ab5086d2a7b19bcb24402af7d4506414b8117b04fb31cb2182f18ef532922c301e31b9314ad2efa5d8001336a592aab2461";
hex"0178ac80600029b010810908a00a3448c001280030060100180000200cc2200408a00c95d8383148145c3234b3f32dd587e204afc8f01e95a26007b9a51f0d5e6f2803ce312215fae0a1271233de118cfd53051c784d25a55ade1681ac9622ac6dfc2ea4e5e91f5fc32a0e92b2401dbbd837100ef76b00cd8814205902e62a9714ec13313592290d473b21f880501427634f0f23b4970ac0e53d0b895f991ac908be0983724f12c080bc2f9d7b7d28757aea266f62e02b5c0ab5086d2a7b2390a6b319bcb24402af7d4506414b8117b04fb31cb2182f18ef532924c301e31b9314ad30fa5d8001336a592cab2461";

/// @dev The build depth of the parser meta.

Expand All @@ -39,11 +39,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"1a231a231a231af81c0f1c0f1c0f1af81af81a231a231a231c0f1c0f1c0f1c0f1c0f1c0f1c0f1c0f1c0f1c0f1c0f1c0f1c0f1c0f1c0f1c0f1c0f1c0f1c0f1c0f1c0f1c0f1c0f1c0f1c0f1c0f1c0f1c0f1c0f1c0f1c0f1c0f1c0f1c0f1a231c0f1c0f";
hex"1a331a331a331b081c1f1c1f1c1f1b081b081a331a331a331c1f1c1f1c1f1c1f1c1f1c1f1c1f1c1f1c1f1c1f1c1f1c1f1c1f1c1f1c1f1c1f1c1f1c1f1c1f1c1f1c1f1c1f1c1f1c1f1c1f1c1f1c1f1c1f1c1f1c1f1c1f1c1f1c1f1c1f1c1f1c1f1a331c1f1c1f";

/// @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"156b179d17e0187e";
bytes constant LITERAL_PARSER_FUNCTION_POINTERS = hex"157b17ad17f0188e";
36 changes: 18 additions & 18 deletions src/lib/op/LibAllStandardOps.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ import {LibOpEqualTo} from "./logic/LibOpEqualTo.sol";
import {LibOpBinaryEqualTo} from "./logic/LibOpBinaryEqualTo.sol";
import {LibOpEveryNP} from "./logic/LibOpEveryNP.sol";
import {LibOpGreaterThan} from "./logic/LibOpGreaterThan.sol";
import {LibOpGreaterThanOrEqualToNP} from "./logic/LibOpGreaterThanOrEqualToNP.sol";
import {LibOpGreaterThanOrEqualTo} from "./logic/LibOpGreaterThanOrEqualTo.sol";
import {LibOpIfNP} from "./logic/LibOpIfNP.sol";
import {LibOpIsZero} from "./logic/LibOpIsZero.sol";
import {LibOpLessThan} from "./logic/LibOpLessThan.sol";
import {LibOpLessThanOrEqualToNP} from "./logic/LibOpLessThanOrEqualToNP.sol";
import {LibOpLessThanOrEqualTo} from "./logic/LibOpLessThanOrEqualTo.sol";

// import {LibOpExponentialGrowth} from "./math/growth/LibOpExponentialGrowth.sol";
// import {LibOpLinearGrowth} from "./math/growth/LibOpLinearGrowth.sol";
Expand Down Expand Up @@ -108,7 +108,7 @@ import {LibParseLiteralHex} from "../parse/literal/LibParseLiteralHex.sol";
import {LibParseLiteralSubParseable} from "../parse/literal/LibParseLiteralSubParseable.sol";

/// @dev Number of ops currently provided by `AllStandardOps`.
uint256 constant ALL_STANDARD_OPS_LENGTH = 49;
uint256 constant ALL_STANDARD_OPS_LENGTH = 51;

/// @title LibAllStandardOps
/// @notice Every opcode available from the core repository laid out as a single
Expand Down Expand Up @@ -219,10 +219,10 @@ library LibAllStandardOps {
AuthoringMetaV2("binary-equal-to", "1 if all inputs are equal, 0 otherwise. Equality is binary."),
// AuthoringMetaV2("every", "The last nonzero value out of all inputs, or 0 if any input is 0."),
AuthoringMetaV2("greater-than", "true if the first input is greater than the second input, false otherwise."),
// AuthoringMetaV2(
// "greater-than-or-equal-to",
// "1 if the first input is greater than or equal to the second input, 0 otherwise."
// ),
AuthoringMetaV2(
"greater-than-or-equal-to",
"1 if the first input is greater than or equal to the second input, 0 otherwise."
),
// AuthoringMetaV2(
// "if",
// "If the first input is nonzero, the second input is used. Otherwise, the third input is used. If is eagerly evaluated."
Expand All @@ -232,9 +232,9 @@ library LibAllStandardOps {
"1 if the input is 0, 0 otherwise. The input is any numerical 0 value, not just binary 0 e.g. 0e20 is considered 0."
),
AuthoringMetaV2("less-than", "true if the first input is less than the second input, false otherwise."),
// AuthoringMetaV2(
// "less-than-or-equal-to", "1 if the first input is less than or equal to the second input, 0 otherwise."
// ),
AuthoringMetaV2(
"less-than-or-equal-to", "1 if the first input is less than or equal to the second input, 0 otherwise."
),
// AuthoringMetaV2(
// "exponential-growth",
// "Calculates an exponential growth curve as `base(1 + rate)^t` where `base` is the initial value, `rate` is the rate of growth and `t` is units of time. Inputs in order are `base`, `rate`, and `t` respectively."
Expand Down Expand Up @@ -444,16 +444,16 @@ library LibAllStandardOps {
// LibParseOperand.handleOperandDisallowed,
// greater-than
LibParseOperand.handleOperandDisallowed,
// // greater-than-or-equal-to
// LibParseOperand.handleOperandDisallowed,
// greater-than-or-equal-to
LibParseOperand.handleOperandDisallowed,
// // if
// LibParseOperand.handleOperandDisallowed,
// is-zero
LibParseOperand.handleOperandDisallowed,
// less-than
LibParseOperand.handleOperandDisallowed,
// // less-than-or-equal-to
// LibParseOperand.handleOperandDisallowed,
// less-than-or-equal-to
LibParseOperand.handleOperandDisallowed,
// // exponential-growth
// LibParseOperand.handleOperandDisallowed,
// // linear-growth
Expand Down Expand Up @@ -599,11 +599,11 @@ library LibAllStandardOps {
LibOpBinaryEqualTo.integrity,
// LibOpEveryNP.integrity,
LibOpGreaterThan.integrity,
// LibOpGreaterThanOrEqualToNP.integrity,
LibOpGreaterThanOrEqualTo.integrity,
// LibOpIfNP.integrity,
LibOpIsZero.integrity,
LibOpLessThan.integrity,
// LibOpLessThanOrEqualToNP.integrity,
LibOpLessThanOrEqualTo.integrity,
// LibOpExponentialGrowth.integrity,
// LibOpLinearGrowth.integrity,
LibOpMaxUint256.integrity,
Expand Down Expand Up @@ -714,11 +714,11 @@ library LibAllStandardOps {
LibOpBinaryEqualTo.run,
// LibOpEveryNP.run,
LibOpGreaterThan.run,
// LibOpGreaterThanOrEqualToNP.run,
LibOpGreaterThanOrEqualTo.run,
// LibOpIfNP.run,
LibOpIsZero.run,
LibOpLessThan.run,
// LibOpLessThanOrEqualToNP.run,
LibOpLessThanOrEqualTo.run,
// LibOpExponentialGrowth.run,
// LibOpLinearGrowth.run,
LibOpMaxUint256.run,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import {OperandV2} from "rain.interpreter.interface/interface/unstable/IInterpre
import {Pointer} from "rain.solmem/lib/LibPointer.sol";
import {InterpreterState} from "../../state/LibInterpreterState.sol";
import {IntegrityCheckState} from "../../integrity/LibIntegrityCheck.sol";
import {StackItem} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol";
import {Float, LibDecimalFloat} from "rain.math.float/lib/LibDecimalFloat.sol";

/// @title LibOpGreaterThanOrEqualToNP
/// @title LibOpGreaterThanOrEqualTo
/// @notice Opcode to return 1 if the first item on the stack is greater than or
/// equal to the second item on the stack, else 0.
library LibOpGreaterThanOrEqualToNP {
library LibOpGreaterThanOrEqualTo {
function integrity(IntegrityCheckState memory, OperandV2) internal pure returns (uint256, uint256) {
return (2, 1);
}
Expand All @@ -18,21 +20,30 @@ library LibOpGreaterThanOrEqualToNP {
/// GTE is 1 if the first item is greater than or equal to the second item,
/// else 0.
function run(InterpreterState memory, OperandV2, Pointer stackTop) internal pure returns (Pointer) {
Float a;
Float b;
assembly ("memory-safe") {
let a := mload(stackTop)
a := mload(stackTop)
stackTop := add(stackTop, 0x20)
mstore(stackTop, iszero(lt(a, mload(stackTop))))
b := mload(stackTop)
}
bool greaterThanOrEqual = LibDecimalFloat.gte(a, b);
assembly ("memory-safe") {
mstore(stackTop, greaterThanOrEqual)
}
return stackTop;
}

/// Gas intensive reference implementation of GTE for testing.
function referenceFn(InterpreterState memory, OperandV2, uint256[] memory inputs)
function referenceFn(InterpreterState memory, OperandV2, StackItem[] memory inputs)
internal
pure
returns (uint256[] memory outputs)
returns (StackItem[] memory outputs)
{
outputs = new uint256[](1);
outputs[0] = inputs[0] >= inputs[1] ? 1 : 0;
Float a = Float.wrap(StackItem.unwrap(inputs[0]));
Float b = Float.wrap(StackItem.unwrap(inputs[1]));
bool greaterThanOrEqual = LibDecimalFloat.gte(a, b);
outputs = new StackItem[](1);
outputs[0] = StackItem.wrap(bytes32(uint256(greaterThanOrEqual ? 1 : 0)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import {OperandV2} from "rain.interpreter.interface/interface/unstable/IInterpre
import {Pointer} from "rain.solmem/lib/LibPointer.sol";
import {IntegrityCheckState} from "../../integrity/LibIntegrityCheck.sol";
import {InterpreterState} from "../../state/LibInterpreterState.sol";
import {StackItem} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol";
import {Float, LibDecimalFloat} from "rain.math.float/lib/LibDecimalFloat.sol";

/// @title LibOpLessThanOrEqualToNP
/// @title LibOpLessThanOrEqualTo
/// @notice Opcode to return 1 if the first item on the stack is less than or
/// equal to the second item on the stack, else 0.
library LibOpLessThanOrEqualToNP {
library LibOpLessThanOrEqualTo {
function integrity(IntegrityCheckState memory, OperandV2) internal pure returns (uint256, uint256) {
return (2, 1);
}
Expand All @@ -18,21 +20,30 @@ library LibOpLessThanOrEqualToNP {
/// LTE is 1 if the first item is less than or equal to the second item,
/// else 0.
function run(InterpreterState memory, OperandV2, Pointer stackTop) internal pure returns (Pointer) {
Float a;
Float b;
assembly ("memory-safe") {
let a := mload(stackTop)
a := mload(stackTop)
stackTop := add(stackTop, 0x20)
mstore(stackTop, iszero(gt(a, mload(stackTop))))
b := mload(stackTop)
}
bool lessThanOrEqual = LibDecimalFloat.lte(a, b);
assembly ("memory-safe") {
mstore(stackTop, lessThanOrEqual)
}
return stackTop;
}

/// Gas intensive reference implementation of LTE for testing.
function referenceFn(InterpreterState memory, OperandV2, uint256[] memory inputs)
function referenceFn(InterpreterState memory, OperandV2, StackItem[] memory inputs)
internal
pure
returns (uint256[] memory outputs)
returns (StackItem[] memory outputs)
{
outputs = new uint256[](1);
outputs[0] = inputs[0] <= inputs[1] ? 1 : 0;
Float a = Float.wrap(StackItem.unwrap(inputs[0]));
Float b = Float.wrap(StackItem.unwrap(inputs[1]));
bool lessThanOrEqual = LibDecimalFloat.lte(a, b);
outputs = new StackItem[](1);
outputs[0] = StackItem.wrap(bytes32(uint256(lessThanOrEqual ? 1 : 0)));
}
}
99 changes: 99 additions & 0 deletions test/src/lib/op/logic/LibOpGreaterThanOrEqualTo.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// SPDX-License-Identifier: CAL
pragma solidity =0.8.25;

import {OpTest} from "test/abstract/OpTest.sol";
import {LibOpGreaterThanOrEqualTo} from "src/lib/op/logic/LibOpGreaterThanOrEqualTo.sol";
import {IntegrityCheckState, BadOpInputsLength} from "src/lib/integrity/LibIntegrityCheck.sol";
import {
IInterpreterV4,
OperandV2,
SourceIndexV2,
FullyQualifiedNamespace
} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol";
import {InterpreterState} from "src/lib/state/LibInterpreterState.sol";
import {LibOperand} from "test/lib/operand/LibOperand.sol";
import {StackItem} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol";

contract LibOpGreaterThanOrEqualToTest is OpTest {
/// Directly test the integrity logic of LibOpGreaterThanOrEqualTo. No matter the
/// operand inputs, the calc inputs must be 2, and the calc outputs must be
/// 1.
function testOpGreaterThanOrEqualToIntegrityHappy(IntegrityCheckState memory state, uint8 inputs) external pure {
(uint256 calcInputs, uint256 calcOutputs) =
LibOpGreaterThanOrEqualTo.integrity(state, OperandV2.wrap(bytes32(uint256(inputs) << 0x10)));

// The inputs from the operand are ignored. The op is always 2 inputs.
assertEq(calcInputs, 2);
assertEq(calcOutputs, 1);
}

/// Directly test the runtime logic of LibOpGreaterThanOrEqualTo.
function testOpGreaterThanOrEqualToRun(StackItem input1, StackItem input2) external view {
InterpreterState memory state = opTestDefaultInterpreterState();
StackItem[] memory inputs = new StackItem[](2);
inputs[0] = input1;
inputs[1] = input2;
OperandV2 operand = LibOperand.build(uint8(inputs.length), 1, 0);
opReferenceCheck(
state,
operand,
LibOpGreaterThanOrEqualTo.referenceFn,
LibOpGreaterThanOrEqualTo.integrity,
LibOpGreaterThanOrEqualTo.run,
inputs
);
}

/// Test the eval of greater than or equal to opcode parsed from a string.
/// Tests 2 inputs. Both inputs are 0.
function testOpGreaterThanOrEqualToEval2ZeroInputs() external view {
checkHappy("_: greater-than-or-equal-to(0 0);", bytes32(uint256(1)), "");
}

/// Test the eval of greater than or equal to opcode parsed from a string.
/// Tests 2 inputs. The first input is 0, the second input is 1.
function testOpGreaterThanOrEqualToEval2InputsFirstZeroSecondOne() external view {
checkHappy("_: greater-than-or-equal-to(0 1);", 0, "");
}

/// Test the eval of greater than or equal to opcode parsed from a string.
/// Tests 2 inputs. The first input is 1, the second input is 0.
function testOpGreaterThanOrEqualToEval2InputsFirstOneSecondZero() external view {
checkHappy("_: greater-than-or-equal-to(1 0);", bytes32(uint256(1)), "");
}

/// Test the eval of greater than or equal to opcode parsed from a string.
/// Tests 2 inputs. Both inputs are 1.
function testOpGreaterThanOrEqualToEval2InputsBothOne() external view {
checkHappy("_: greater-than-or-equal-to(1 1);", bytes32(uint256(1)), "");
}

/// Test that a greater than or equal to without inputs fails integrity check.
function testOpGreaterThanOrEqualToEvalFail0Inputs() public {
vm.expectRevert(abi.encodeWithSelector(BadOpInputsLength.selector, 0, 2, 0));
bytes memory bytecode = iDeployer.parse2("_: greater-than-or-equal-to();");
(bytecode);
}

/// Test that a greater than or equal to with 1 input fails integrity check.
function testOpGreaterThanOrEqualToEvalFail1Input() public {
vm.expectRevert(abi.encodeWithSelector(BadOpInputsLength.selector, 1, 2, 1));
bytes memory bytecode = iDeployer.parse2("_: greater-than-or-equal-to(0x00);");
(bytecode);
}

/// Test that a greater than or equal to with 3 inputs fails integrity check.
function testOpGreaterThanOrEqualToEvalFail3Inputs() public {
vm.expectRevert(abi.encodeWithSelector(BadOpInputsLength.selector, 3, 2, 3));
bytes memory bytecode = iDeployer.parse2("_: greater-than-or-equal-to(0x00 0x00 0x00);");
(bytecode);
}

function testOpGreaterThanOrEqualToZeroOutputs() external {
checkBadOutputs(": greater-than-or-equal-to(1 2);", 2, 1, 0);
}

function testOpGreaterThanOrEqualToTwoOutputs() external {
checkBadOutputs("_ _: greater-than-or-equal-to(1 2);", 2, 1, 2);
}
}
Loading
Loading