Skip to content
Merged

gt op #356

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
414 changes: 225 additions & 189 deletions .gas-snapshot

Large diffs are not rendered by default.

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(0x6ba365de507490ff7cb262df51a7bb9b131cf720d1268f29e533709a7cd70c61);
bytes32 constant BYTECODE_HASH = bytes32(0xddf6e7bcbe3d3394d100881801381404f5f91c19b9d21d348482ff09fbabd3f4);

/// @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"07010733075708e309ac09be09d009e90a0d0a410a520a630b050b240bd30c570c680c790c790c8a0d170d300d440d570db90e070e380e860ed40fbb";
hex"070a073c076008ec09b509c709d909f20a160a4a0a5b0a6c0b0e0b2d0bdc0c600c710c820c820c930d200d390d4d0d660d790ddb0e290e5a0ea80ef60fdd";
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(0x010a570b11206177d53f0f66cd89962bbfca589385bcb0ec0ab4bc6c1dd3b35c);
bytes32 constant BYTECODE_HASH = bytes32(0x92914df17907d045c2e205ffc7629e9362435836efff6ff7bf1d5cb6ead71c74);

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

/// @dev The function pointers for the integrity check fns.
bytes constant INTEGRITY_FUNCTION_POINTERS =
hex"0da80e260e8a1004100e100e10181021103c10e210e2113e11b6100e1018100410041004100411c3100e100e100411cd11f5100411f511f5101811c3";
hex"0db00e2e0e92100c1016101610201029104410ea10ea114611be10161020100c100c100c100c11cb101610161016100c11d511fd100c11fd11fd102011cb";
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(0xb416945b30bdad60766ef143f600b093a45edb74bf6b644c2d72a654960b6a85);
bytes32 constant BYTECODE_HASH = bytes32(0x58f6018d11af765b99cb6f438c3cfcbb09b18a8406b4fafa5cb1645b3ac674af);

/// @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(0xb416945b30bdad60766ef143f600b093a45ed
/// 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"01092404900810009008201088000400e0010080000800001028010100080800090002cd70471c14fbe600f861d118d0e20c0e8be82d1d7c7f8004e5ab37174d06d00d67df8415fedd510602c61f08bcae760bcd088610f1c17805ac4b9f0a09bc09160ce432091ef7b011dfb72601a91367121faf890c2b9f481ae804c119bd68ce14811fdb034a067c0fffb07907743c4a1b15024513edf10f";
hex"01092404900810009008201088000400e0010080000800001028010100080900090002cd70471d14fbe61685cf8700f861d119d0e20c0e8be82d1e7c7f8004e5ab37184d06d00d67df8415fedd510602c61f08bcae760bcd088610f1c17805ac4b9f0a09bc09170ce432091ef7b011dfb72601a91367121faf890c2b9f481be804c11abd68ce14811fdb034a067c0fffb07907743c4a1c15024513edf10f";

/// @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"198a198a198a1a5f1b761b761b761a5f1a5f198a198a198a1b761b761b761b761b761b761b761b761b761b761b761b761b761b761b76198a1b761b76";
hex"1992199219921a671b7e1b7e1b7e1a671a671992199219921b7e1b7e1b7e1b7e1b7e1b7e1b7e1b7e1b7e1b7e1b7e1b7e1b7e1b7e1b7e1b7e19921b7e1b7e";

/// @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"14d21704174717e5";
bytes constant LITERAL_PARSER_FUNCTION_POINTERS = hex"14da170c174f17ed";
14 changes: 7 additions & 7 deletions src/lib/op/LibAllStandardOps.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import {LibOpEnsure} from "./logic/LibOpEnsure.sol";
import {LibOpEqualTo} from "./logic/LibOpEqualTo.sol";
import {LibOpBinaryEqualTo} from "./logic/LibOpBinaryEqualTo.sol";
import {LibOpEveryNP} from "./logic/LibOpEveryNP.sol";
import {LibOpGreaterThanNP} from "./logic/LibOpGreaterThanNP.sol";
import {LibOpGreaterThan} from "./logic/LibOpGreaterThan.sol";
import {LibOpGreaterThanOrEqualToNP} from "./logic/LibOpGreaterThanOrEqualToNP.sol";
import {LibOpIfNP} from "./logic/LibOpIfNP.sol";
import {LibOpIsZeroNP} from "./logic/LibOpIsZeroNP.sol";
Expand Down Expand Up @@ -105,7 +105,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 = 30;
uint256 constant ALL_STANDARD_OPS_LENGTH = 31;

/// @title LibAllStandardOps
/// @notice Every opcode available from the core repository laid out as a single
Expand Down Expand Up @@ -211,7 +211,7 @@ library LibAllStandardOps {
AuthoringMetaV2("equal-to", "1 if all inputs are equal, 0 otherwise. Equality is numerical."),
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", "1 if the first input is greater than the second input, 0 otherwise."),
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."
Expand Down Expand Up @@ -423,8 +423,8 @@ library LibAllStandardOps {
LibParseOperand.handleOperandDisallowed,
// // every
// LibParseOperand.handleOperandDisallowed,
// // greater-than
// LibParseOperand.handleOperandDisallowed,
// greater-than
LibParseOperand.handleOperandDisallowed,
// // greater-than-or-equal-to
// LibParseOperand.handleOperandDisallowed,
// // if
Expand Down Expand Up @@ -574,7 +574,7 @@ library LibAllStandardOps {
LibOpEqualTo.integrity,
LibOpBinaryEqualTo.integrity,
// LibOpEveryNP.integrity,
// LibOpGreaterThanNP.integrity,
LibOpGreaterThan.integrity,
// LibOpGreaterThanOrEqualToNP.integrity,
// LibOpIfNP.integrity,
// LibOpIsZeroNP.integrity,
Expand Down Expand Up @@ -686,7 +686,7 @@ library LibAllStandardOps {
LibOpEqualTo.run,
LibOpBinaryEqualTo.run,
// LibOpEveryNP.run,
// LibOpGreaterThanNP.run,
LibOpGreaterThan.run,
// LibOpGreaterThanOrEqualToNP.run,
// LibOpIfNP.run,
// LibOpIsZeroNP.run,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,44 @@ 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 LibOpGreaterThanNP
/// @title LibOpGreaterThan
/// @notice Opcode to return 1 if the first item on the stack is greater than
/// the second item on the stack, else 0.
library LibOpGreaterThanNP {
library LibOpGreaterThan {
function integrity(IntegrityCheckState memory, OperandV2) internal pure returns (uint256, uint256) {
return (2, 1);
}

/// GT
/// GT is 1 if the first item is greater than 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, gt(a, mload(stackTop)))
b := mload(stackTop)
}
bool greaterThan = LibDecimalFloat.gt(a, b);
assembly ("memory-safe") {
mstore(stackTop, greaterThan)
}
return stackTop;
}

/// Gas intensive reference implementation of GT 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 greaterThan = LibDecimalFloat.gt(a, b);
outputs = new StackItem[](1);
outputs[0] = StackItem.wrap(bytes32(uint256(greaterThan ? 1 : 0)));
}
}
3 changes: 1 addition & 2 deletions src/lib/op/math/LibOpDiv.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ library LibOpDiv {
}

/// div
/// 18 decimal fixed point division with implied overflow checks from PRB
/// Math.
/// 18 decimal floating point division.
function run(InterpreterState memory, OperandV2 operand, Pointer stackTop) internal pure returns (Pointer) {
Float a;
Float b;
Expand Down
121 changes: 121 additions & 0 deletions test/src/lib/op/logic/LibOpGreaterThan.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// SPDX-License-Identifier: CAL
pragma solidity =0.8.25;

import {OpTest} from "test/abstract/OpTest.sol";
import {LibOpGreaterThan} from "src/lib/op/logic/LibOpGreaterThan.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 LibOpGreaterThanTest is OpTest {
/// Directly test the integrity logic of LibOpGreaterThan. No matter the
/// operand inputs, the calc inputs must be 2, and the calc outputs must be
/// 1.
function testOpGreaterThanIntegrityHappy(
IntegrityCheckState memory state,
uint8 inputs,
uint8 outputs,
uint16 operandData
) external pure {
inputs = uint8(bound(inputs, 0, 0x0F));
outputs = uint8(bound(outputs, 0, 0x0F));
(uint256 calcInputs, uint256 calcOutputs) =
LibOpGreaterThan.integrity(state, LibOperand.build(inputs, outputs, operandData));

// 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 LibOpGreaterThan.
function testOpGreaterThanRun(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, LibOpGreaterThan.referenceFn, LibOpGreaterThan.integrity, LibOpGreaterThan.run, inputs
);
}

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

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

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

/// Test the eval of greater than opcode parsed from a string. Tests 2
/// inputs. Both inputs are 1.
function testOpGreaterThanEval2InputsBothOne() external view {
checkHappy("_: greater-than(1 1);", 0, "");
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

/// Test 1.1 gt 1.2, which should return 0.
function testOpGreaterThanEval1_1Gt1_2() external view {
checkHappy("_: greater-than(1.1 1.2);", 0, "");
}

/// Test 1.0 gt 1 which should return 0.
function testOpGreaterThanEval1_0Gt1() external view {
checkHappy("_: greater-than(1.0 1);", 0, "");
}

/// Test -1.1 gt -1.2, which should return 1.
function testOpGreaterThanEvalNeg1_1GtNeg1_2() external view {
checkHappy("_: greater-than(-1.1 -1.2);", bytes32(uint256(1)), "");
}

/// Test -1 gt 0, which should return 0.
function testOpGreaterThanEvalNeg1Gt0() external view {
checkHappy("_: greater-than(-1 0);", 0, "");
}

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

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

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

function testOpGreaterThanZeroOutputs() external {
checkBadOutputs(": greater-than(1 2);", 2, 1, 0);
}

function testOpGreaterThanTwoOutputs() external {
checkBadOutputs("_ _: greater-than(1 2);", 2, 1, 2);
}
}
Loading
Loading