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

/// @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"083f087108950a210aea0afc0b0e0b270b4b0b7f0b900ba10c430c620d200dd00e540f9610c90d2011c212741316138e139f13b013b013c1142c153715b615cf15e31642165b167416ad16d816f1170a1753177a178d17ef183d188b18d919271935198319d11a021a101a1e1a2c1a7a1aab1adc1b2a1b5b1b8c1bda1c071c551d4b";
hex"0848087a089e0a2a0af30b050b170b300b540b880b990baa0c4c0c6b0d290dd90e5d0f9f10d20d2911cb127d131f139713a813b913b913ca1435154015bf15d815ec164b1664167d16b616e116fa1713175c1783179617f81846189418e21930193e198c19da1a0b1a191a271a351a831ab41ae51b331b641b951be31c101c331c811d77";
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(0x332f44ea870a144ee68d08d1ee0959b99372cc76824247c8f256299ddce31111);
bytes32 constant BYTECODE_HASH = bytes32(0x326b2bf8fcbff49ad49d8a6bb7053696c7126ce0a9172a7a9f0d9f4488a021ce);

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

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

/// @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(0xbb928fe4d73b4e338f52b27b686becb028a6b
/// 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"02192ac782060c081806c6c1900010012220200283045069400114680901208401260000200000000000000000000000000000000000001000000000000000000000202d49b09d200441910d1e98a7339c08461fd6ea18118eba9713065d302aab10ec04ac72c00e7ff5a240ae0c8706fb9c253fc7bc2334acb2941b0b5fc712fec72837e7c86c18d295ed36d9a6c50063d58d35dd44c9261df07b3bcb6609012ba09832d9776c0c6cbb42306d6b2403ae6396082c4e96277f03402941d78a16172ab51a5b8f1d1c2d57c43dca85c615f871180f48502f25b9d38d0be7f2e71ef1ff4d19d140c23e1a1e3510abcc6405b702d7020f038b2b93067d220df2ef07ee624a214143471482e75b1d60aa350a3d21ff174c80cb31cc4a6a3c023c042ca9578e380cdb6909a046dc3ad89c342fd4e3cf233bc325399b388724b25e682e7b09ff28dc84e5";
hex"02192ac782060c081806c6c1900011012220200283045069400114680901208401260000200000000000000000000000000000000000001000000000000000000000202d49b09d200441910d1e98a7339c08461fd6ea18118eba9713065d302aab10ec04ac72c00e7ff5a241ae0c8706fb9c2540c7bc2334acb2941b0b5fc712fec72837e7c86c18d295ed36d9a6c50063d58d35dd44c9261df07b3bcb6609012ba09832d9776c0c6cbb42306d6b2403ae6396082c4e96277f03402941d78a16172ab51a5b8f1d3e1120541c2d57c43dca85c615f871180f48502f25b9d38d0be7f2e71ef1ff4d19d140c23f1a1e3510abcc6405b702d7020f038b2b93067d220df2ef07ee624a214143471482e75b1d60aa350a3d21ff174c80cb31cc4a6a3c023c042ca9578e380cdb6909a046dc3ad89c342fd4e3cf233bc325399b388724b25e682e7b09ff28dc84e5";

/// @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"1aa51aa51aa51b7a1c911c911c911b7a1b7a1aa51aa51aa51c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911c911aa51c911c91";
hex"1aad1aad1aad1b821c991c991c991b821b821aad1aad1aad1c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991c991aad1c991c99";

/// @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"15ed181f18621900";
bytes constant LITERAL_PARSER_FUNCTION_POINTERS = hex"15f51827186a1908";
14 changes: 7 additions & 7 deletions src/lib/op/LibAllStandardOps.sol
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ import {LibOpPow} from "./math/LibOpPow.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 {LibOpSqrt} from "./math/LibOpSqrt.sol";
import {LibOpSub} from "./math/LibOpSub.sol";

import {LibOpGet} from "./store/LibOpGet.sol";
Expand All @@ -112,7 +112,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 = 65;
uint256 constant ALL_STANDARD_OPS_LENGTH = 66;

/// @title LibAllStandardOps
/// @notice Every opcode available from the core repository laid out as a single
Expand Down Expand Up @@ -311,7 +311,7 @@ library LibAllStandardOps {
// "snap-to-unit",
// "Rounds a number to the nearest whole number if it is within the threshold distance from that whole number. The first input is the threshold and the second is the value to snap to the nearest unit."
// ),
// AuthoringMetaV2("sqrt", "Calculates the square root of the input. Errors if the input is negative."),
AuthoringMetaV2("sqrt", "Calculates the square root of the input. Errors if the input is negative."),
AuthoringMetaV2("sub", "Subtracts all numbers from the first number."),
AuthoringMetaV2("get", "Gets a value from storage. The first operand is the key to lookup."),
AuthoringMetaV2(
Expand Down Expand Up @@ -512,8 +512,8 @@ library LibAllStandardOps {
LibParseOperand.handleOperandDisallowed,
// // snap-to-unit
// LibParseOperand.handleOperandDisallowed,
// // sqrt
// LibParseOperand.handleOperandDisallowed,
// sqrt
LibParseOperand.handleOperandDisallowed,
// sub
LibParseOperand.handleOperandSingleFull,
// get
Expand Down Expand Up @@ -624,7 +624,7 @@ library LibAllStandardOps {
LibOpMul.integrity,
LibOpPow.integrity,
// LibOpSnapToUnit.integrity,
// LibOpSqrt.integrity,
LibOpSqrt.integrity,
LibOpSub.integrity,
LibOpGet.integrity,
LibOpSet.integrity
Expand Down Expand Up @@ -735,7 +735,7 @@ library LibAllStandardOps {
LibOpMul.run,
LibOpPow.run,
// LibOpSnapToUnit.run,
// LibOpSqrt.run,
LibOpSqrt.run,
LibOpSub.run,
LibOpGet.run,
LibOpSet.run
Expand Down
78 changes: 42 additions & 36 deletions src/lib/op/math/LibOpSqrt.sol
Original file line number Diff line number Diff line change
@@ -1,43 +1,49 @@
// SPDX-License-Identifier: CAL
pragma solidity ^0.8.18;

// import {UD60x18, sqrt} from "prb-math/UD60x18.sol";
// import {OperandV2} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol";
// import {Pointer} from "rain.solmem/lib/LibPointer.sol";
// import {InterpreterState} from "../../state/LibInterpreterState.sol";
// import {IntegrityCheckState} from "../../integrity/LibIntegrityCheck.sol";
import {OperandV2} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol";
import {Pointer} from "rain.solmem/lib/LibPointer.sol";
import {InterpreterState} from "../../state/LibInterpreterState.sol";
import {IntegrityCheckState} from "../../integrity/LibIntegrityCheck.sol";
import {Float, LibDecimalFloat} from "rain.math.float/lib/LibDecimalFloat.sol";
import {StackItem} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol";

// /// @title LibOpSqrt
// /// @notice Opcode for the square root of an decimal 18 fixed point number.
// library LibOpSqrt {
// function integrity(IntegrityCheckState 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 a decimal floating point number.
library LibOpSqrt {
using LibDecimalFloat for Float;

// /// sqrt
// /// 18 decimal fixed point square root of a number.
// function run(InterpreterState memory, Operand, Pointer stackTop) internal pure returns (Pointer) {
// uint256 a;
// assembly ("memory-safe") {
// a := mload(stackTop)
// }
// a = UD60x18.unwrap(sqrt(UD60x18.wrap(a)));
function integrity(IntegrityCheckState memory, OperandV2) internal pure returns (uint256, uint256) {
// There must be one inputs and one output.
return (1, 1);
}

// assembly ("memory-safe") {
// mstore(stackTop, a)
// }
// return stackTop;
// }
/// sqrt
/// decimal floating point square root of a number.
function run(InterpreterState memory, OperandV2, Pointer stackTop) internal view returns (Pointer) {
Float a;
assembly ("memory-safe") {
a := mload(stackTop)
}
a = a.sqrt(LibDecimalFloat.LOG_TABLES_ADDRESS);

// /// Gas intensive reference implementation of sqrt for testing.
// function referenceFn(InterpreterState 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;
// }
// }
assembly ("memory-safe") {
mstore(stackTop, a)
}
return stackTop;
}

/// Gas intensive reference implementation of sqrt for testing.
function referenceFn(InterpreterState memory, OperandV2, StackItem[] memory inputs)
internal
view
returns (StackItem[] memory)
{
Float a = Float.wrap(StackItem.unwrap(inputs[0]));
a = a.sqrt(LibDecimalFloat.LOG_TABLES_ADDRESS);

StackItem[] memory outputs = new StackItem[](1);
outputs[0] = StackItem.wrap(Float.unwrap(a));
return outputs;
}
}
118 changes: 65 additions & 53 deletions test/src/lib/op/math/LibOpSqrt.t.sol
Original file line number Diff line number Diff line change
@@ -1,56 +1,68 @@
// SPDX-License-Identifier: CAL
pragma solidity =0.8.25;

// import {OpTest, IntegrityCheckState, Operand, InterpreterState, UnexpectedOperand} from "test/abstract/OpTest.sol";
// import {LibOpSqrt} from "src/lib/op/math/LibOpSqrt.sol";
// import {LibOperand} from "test/lib/operand/LibOperand.sol";

// contract LibOpSqrtTest is OpTest {
// /// Directly test the integrity logic of LibOpSqrt.
// /// Inputs are always 1, outputs are always 1.
// function testOpSqrtIntegrity(IntegrityCheckState memory state, Operand operand) external pure {
// (uint256 calcInputs, uint256 calcOutputs) = LibOpSqrt.integrity(state, operand);
// assertEq(calcInputs, 1);
// assertEq(calcOutputs, 1);
// }

// /// Directly test the runtime logic of LibOpSqrt.
// function testOpSqrtRun(uint256 a) public view {
// a = bound(a, 0, type(uint64).max - 1e18);
// InterpreterState memory state = opTestDefaultInterpreterState();

// Operand operand = LibOperand.build(1, 1, 0);
// uint256[] memory inputs = new uint256[](1);
// inputs[0] = a;

// opReferenceCheck(state, operand, LibOpSqrt.referenceFn, LibOpSqrt.integrity, LibOpSqrt.run, inputs);
// }

// /// Test the eval of `sqrt`.
// function testOpSqrtEval() external view {
// checkHappy("_: sqrt(0);", 0, "0");
// checkHappy("_: sqrt(1);", 1e18, "1");
// checkHappy("_: sqrt(0.5);", 707106781186547524, "0.5");
// checkHappy("_: sqrt(2);", 1414213562373095048, "2");
// checkHappy("_: sqrt(2.5);", 1581138830084189665, "2.5");
// }

// /// Test the eval of `sqrt` for bad inputs.
// function testOpSqrtEvalBad() external {
// checkBadInputs("_: sqrt();", 0, 1, 0);
// checkBadInputs("_: sqrt(1 1);", 2, 1, 2);
// }

// function testOpSqrtEvalZeroOutputs() external {
// checkBadOutputs(": sqrt(1);", 1, 1, 0);
// }

// function testOpSqrtEvalTwoOutputs() external {
// checkBadOutputs("_ _: sqrt(1);", 1, 1, 2);
// }

// /// Test that operand is disallowed.
// function testOpSqrtEvalOperandDisallowed() external {
// checkUnhappyParse("_: sqrt<0>(1);", abi.encodeWithSelector(UnexpectedOperand.selector));
// }
// }
import {OpTest, IntegrityCheckState, OperandV2, InterpreterState, UnexpectedOperand} from "test/abstract/OpTest.sol";
import {LibOpSqrt} from "src/lib/op/math/LibOpSqrt.sol";
import {LibOperand} from "test/lib/operand/LibOperand.sol";
import {LibDecimalFloat, Float} from "rain.math.float/lib/LibDecimalFloat.sol";
import {StackItem} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol";

contract LibOpSqrtTest is OpTest {
using LibDecimalFloat for Float;

function beforeOpTestConstructor() internal virtual override {
vm.createSelectFork(vm.envString("ETH_RPC_URL"));
}

/// Directly test the integrity logic of LibOpSqrt.
/// Inputs are always 1, outputs are always 1.
function testOpSqrtIntegrity(IntegrityCheckState memory state, OperandV2 operand) external pure {
(uint256 calcInputs, uint256 calcOutputs) = LibOpSqrt.integrity(state, operand);
assertEq(calcInputs, 1);
assertEq(calcOutputs, 1);
}

/// Directly test the runtime logic of LibOpSqrt.
function testOpSqrtRun(Float a) public view {
a = a.abs();
InterpreterState memory state = opTestDefaultInterpreterState();

OperandV2 operand = LibOperand.build(1, 1, 0);
StackItem[] memory inputs = new StackItem[](1);
inputs[0] = StackItem.wrap(Float.unwrap(a));

opReferenceCheck(state, operand, LibOpSqrt.referenceFn, LibOpSqrt.integrity, LibOpSqrt.run, inputs);
}

/// Test the eval of `sqrt`.
function testOpSqrtEval() external view {
checkHappy("_: sqrt(0);", 0, "0");
checkHappy("_: sqrt(1);", Float.unwrap(LibDecimalFloat.packLossless(1e3, -3)), "1");
checkHappy(
"_: sqrt(0.5);",
Float.unwrap(LibDecimalFloat.packLossless(70671378091872791519434628975265017667, -38)),
"0.5"
);
checkHappy("_: sqrt(2);", Float.unwrap(LibDecimalFloat.packLossless(1415, -3)), "2");
checkHappy("_: sqrt(2.5);", Float.unwrap(LibDecimalFloat.packLossless(1581, -3)), "2.5");
}

/// Test the eval of `sqrt` for bad inputs.
function testOpSqrtEvalBad() external {
checkBadInputs("_: sqrt();", 0, 1, 0);
checkBadInputs("_: sqrt(1 1);", 2, 1, 2);
}

function testOpSqrtEvalZeroOutputs() external {
checkBadOutputs(": sqrt(1);", 1, 1, 0);
}

function testOpSqrtEvalTwoOutputs() external {
checkBadOutputs("_ _: sqrt(1);", 1, 1, 2);
}

/// Test that operand is disallowed.
function testOpSqrtEvalOperandDisallowed() external {
checkUnhappyParse("_: sqrt<0>(1);", abi.encodeWithSelector(UnexpectedOperand.selector));
}
}
Loading