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

/// @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"0848087a089e0a2a0af30b050b170b300b540b880b990baa0c4c0c6b0d290dd90e5d0f9f10d20d2911cb127d131f139713a813b913b913ca1435154015bf15d815ec164b1664167d16b616e116fa1713175c1783179617f81846189418e21930193e198c19da1a0b1a191a271a351a831ab41ae51b331b641b951be31c101c331c811d77";
hex"0851088308a70a330afc0b0e0b200b390b5d0b910ba20bb30c550c740d320de20e660fa810db0d3211d41286132813a013b113c213c213d3143e154915c815e115f51654166d168616bf16ea1703171c1765178c179f1801184f189d18eb19391947199519b81a061a371a451a531a611aaf1ae01b111b5f1b901bc11c0f1c3c1c5f1cad1da3";
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(0x326b2bf8fcbff49ad49d8a6bb7053696c7126ce0a9172a7a9f0d9f4488a021ce);
bytes32 constant BYTECODE_HASH = bytes32(0x3a3a3679560a95636acc278e1222b00d4f9f6d8f5f9198f2be9d8d49597cb14d);

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

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

/// @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(0x3b473d265224e8c08cb9cd39dd7d546113620
/// 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"02192ac782060c081806c6c1900011012220200283045069400114680901208401260000200000000000000000000000000000000000001000000000000000000000202d49b09d200441910d1e98a7339c08461fd6ea18118eba9713065d302aab10ec04ac72c00e7ff5a241ae0c8706fb9c2540c7bc2334acb2941b0b5fc712fec72837e7c86c18d295ed36d9a6c50063d58d35dd44c9261df07b3bcb6609012ba09832d9776c0c6cbb42306d6b2403ae6396082c4e96277f03402941d78a16172ab51a5b8f1d3e1120541c2d57c43dca85c615f871180f48502f25b9d38d0be7f2e71ef1ff4d19d140c23f1a1e3510abcc6405b702d7020f038b2b93067d220df2ef07ee624a214143471482e75b1d60aa350a3d21ff174c80cb31cc4a6a3c023c042ca9578e380cdb6909a046dc3ad89c342fd4e3cf233bc325399b388724b25e682e7b09ff28dc84e5";
hex"027360018812c0058102300804a1a02000120192e89864d1586010788a0800310122000002000000000000000000000000000000000005000000000000000000000000253bfdf302d3ac012ac8268505b44edf34182f2a14690c630d83a7eb32c5853a08ccb48c3358350a1c21b6883f98a6e5262057e32f7e09af17b43369161b9cec2b5ab5e8243c910831738e091e8a3fa7150113a32e22720213c659b9225ac7c1285da1bd41cc18770aa63c19048eb6fa018179d300dfb6a91fde8a882cb772ea03596d3111fd7ab9427b42922d4396833e58726b1d2330703694005b21f438852081752c3cff19581ac1a49e30ffeff037e1eec73824bdfe2760978f232d6a9a09d520ad061480a0294c65fb0ecb8d4d0b8b587210e7c1b518e4b09907152b041261f729198ab90539f8942b1b45c1270f22a49a3ba6838c4060cc7c0c668e383a10da5035d6d3e33de7d2ff";

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

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

import {LibOpAbs} from "./math/LibOpAbs.sol";
import {LibOpAdd} from "./math/LibOpAdd.sol";
// import {LibOpAvg} from "./math/LibOpAvg.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";
Expand Down Expand Up @@ -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 = 66;
uint256 constant ALL_STANDARD_OPS_LENGTH = 67;

/// @title LibAllStandardOps
/// @notice Every opcode available from the core repository laid out as a single
Expand Down Expand Up @@ -269,7 +269,7 @@ library LibAllStandardOps {
),
AuthoringMetaV2("abs", "The absolute value of a number."),
AuthoringMetaV2("add", "Adds all numbers together."),
// AuthoringMetaV2("avg", "Arithmetic average (mean) of two numbers."),
AuthoringMetaV2("avg", "Arithmetic average (mean) of two numbers."),
// AuthoringMetaV2("ceil", "Ceiling of a number."),
AuthoringMetaV2("div", "Divides the first number by all other numbers. Errors if any divisor is zero."),
AuthoringMetaV2("e", "The mathematical constant e."),
Expand Down Expand Up @@ -464,8 +464,8 @@ library LibAllStandardOps {
LibParseOperand.handleOperandDisallowed,
// add
LibParseOperand.handleOperandDisallowed,
// // avg
// LibParseOperand.handleOperandDisallowed,
// avg
LibParseOperand.handleOperandDisallowed,
// // ceil
// LibParseOperand.handleOperandDisallowed,
// div
Expand Down Expand Up @@ -600,7 +600,7 @@ library LibAllStandardOps {
LibOpUint256Sub.integrity,
LibOpAbs.integrity,
LibOpAdd.integrity,
// LibOpAvg.integrity,
LibOpAvg.integrity,
// LibOpCeil.integrity,
LibOpDiv.integrity,
LibOpE.integrity,
Expand Down Expand Up @@ -711,7 +711,7 @@ library LibAllStandardOps {
LibOpUint256Sub.run,
LibOpAbs.run,
LibOpAdd.run,
// LibOpAvg.run,
LibOpAvg.run,
// LibOpCeil.run,
LibOpDiv.run,
LibOpE.run,
Expand Down
94 changes: 51 additions & 43 deletions src/lib/op/math/LibOpAvg.sol
Original file line number Diff line number Diff line change
@@ -1,46 +1,54 @@
// SPDX-License-Identifier: CAL
pragma solidity ^0.8.18;

// import {UD60x18, avg} 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";

// /// @title LibOpAvg
// /// @notice Opcode for the average of two decimal 18 fixed point numbers.
// library LibOpAvg {
// function integrity(IntegrityCheckState 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(InterpreterState 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;
// }

// /// Gas intensive reference implementation of avg 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(avg(UD60x18.wrap(inputs[0]), UD60x18.wrap(inputs[1])));
// return outputs;
// }
// }
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 LibOpAvg
/// @notice Opcode for the average of two decimal floating point numbers.
library LibOpAvg {
using LibDecimalFloat for Float;

function integrity(IntegrityCheckState memory, OperandV2) internal pure returns (uint256, uint256) {
// There must be two inputs and one output.
return (2, 1);
}

/// avg
/// decimal floating point average of two numbers.
function run(InterpreterState memory, OperandV2, Pointer stackTop) internal pure returns (Pointer) {
Float a;
Float b;
assembly ("memory-safe") {
a := mload(stackTop)
stackTop := add(stackTop, 0x20)
b := mload(stackTop)
}
a = a.add(b).div(LibDecimalFloat.FLOAT_TWO);

assembly ("memory-safe") {
mstore(stackTop, a)
}
return stackTop;
}

/// Gas intensive reference implementation of avg for testing.
function referenceFn(InterpreterState memory, OperandV2, StackItem[] memory inputs)
internal
pure
returns (StackItem[] memory)
{
Float a = Float.wrap(StackItem.unwrap(inputs[0]));
Float b = Float.wrap(StackItem.unwrap(inputs[1]));

a = a.add(b).div(LibDecimalFloat.FLOAT_TWO);

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

// import {OpTest, IntegrityCheckState, Operand, InterpreterState, UnexpectedOperand} from "test/abstract/OpTest.sol";
// import {LibOpAvg} from "src/lib/op/math/LibOpAvg.sol";
// import {LibOperand} from "test/lib/operand/LibOperand.sol";
import {OpTest, IntegrityCheckState, OperandV2, InterpreterState, UnexpectedOperand} from "test/abstract/OpTest.sol";
import {LibOpAvg} from "src/lib/op/math/LibOpAvg.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 LibOpAvgTest is OpTest {
// /// Directly test the integrity logic of LibOpAvg.
// /// Inputs are always 2, outputs are always 1.
// function testOpAvgIntegrity(IntegrityCheckState memory state, Operand operand) external pure {
// (uint256 calcInputs, uint256 calcOutputs) = LibOpAvg.integrity(state, operand);
// assertEq(calcInputs, 2);
// assertEq(calcOutputs, 1);
// }
contract LibOpAvgTest is OpTest {
/// Directly test the integrity logic of LibOpAvg.
/// Inputs are always 2, outputs are always 1.
function testOpAvgIntegrity(IntegrityCheckState memory state, OperandV2 operand) external pure {
(uint256 calcInputs, uint256 calcOutputs) = LibOpAvg.integrity(state, operand);
assertEq(calcInputs, 2);
assertEq(calcOutputs, 1);
}

// /// Directly test the runtime logic of LibOpAvg.
// function testOpAvgRun(uint256 a, uint256 b, uint16 operandData) public view {
// // @TODO This is a hack to get around the fact that we are very likely
// // to overflow uint256 if we just fuzz it, and that it's clunky to
// // determine whether it will overflow or not. Basically the overflow
// // check is exactly the same as the implementation, including all the
// // intermediate squaring, so it seems like a bit of circular logic to
// // do things that way.
// a = bound(a, 0, type(uint64).max);
// b = bound(b, 0, 10);
// InterpreterState memory state = opTestDefaultInterpreterState();
/// Directly test the runtime logic of LibOpAvg.
function testOpAvgRun(
int256 signedCoefficientA,
int256 exponentA,
int256 signedCoefficientB,
int256 exponentB,
uint16 operandData
) public view {
signedCoefficientA = bound(signedCoefficientA, type(int224).min, type(int224).max);
signedCoefficientB = bound(signedCoefficientB, type(int224).min, type(int224).max);
exponentA = bound(exponentA, type(int24).min, type(int24).max);
exponentB = bound(exponentB, type(int24).min, type(int24).max);

// Operand operand = LibOperand.build(2, 1, operandData);
// uint256[] memory inputs = new uint256[](2);
// inputs[0] = a;
// inputs[1] = b;
Float a = LibDecimalFloat.packLossless(signedCoefficientA, exponentA);
Float b = LibDecimalFloat.packLossless(signedCoefficientB, exponentB);
InterpreterState memory state = opTestDefaultInterpreterState();

// opReferenceCheck(state, operand, LibOpAvg.referenceFn, LibOpAvg.integrity, LibOpAvg.run, inputs);
// }
OperandV2 operand = LibOperand.build(2, 1, operandData);
StackItem[] memory inputs = new StackItem[](2);
inputs[0] = StackItem.wrap(Float.unwrap(a));
inputs[1] = StackItem.wrap(Float.unwrap(b));

// /// Test the eval of `avg`.
// function testOpAvgEval() external view {
// checkHappy("_: avg(0 0);", 0, "0 0");
// checkHappy("_: avg(0 1);", 5e17, "0 1");
// checkHappy("_: avg(1 0);", 5e17, "1 0");
// checkHappy("_: avg(1 1);", 1e18, "1 1");
// checkHappy("_: avg(1 2);", 1.5e18, "1 2");
// checkHappy("_: avg(2 2);", 2e18, "2 2");
// checkHappy("_: avg(2 3);", 2.5e18, "2 3");
// checkHappy("_: avg(2 4);", 3e18, "2 4");
// checkHappy("_: avg(4 0.5);", 2.25e18, "4 5");
// }
opReferenceCheck(state, operand, LibOpAvg.referenceFn, LibOpAvg.integrity, LibOpAvg.run, inputs);
}

// /// Test the eval of `avg` for bad inputs.
// function testOpAvgEvalOneInput() external {
// checkBadInputs("_: avg(1);", 1, 2, 1);
// }
/// Test the eval of `avg`.
function testOpAvgEval() external view {
checkHappy("_: avg(0 0);", Float.unwrap(LibDecimalFloat.packLossless(0, -1)), "0 0");
checkHappy("_: avg(0 1);", Float.unwrap(LibDecimalFloat.packLossless(5e37, -38)), "0 1");
checkHappy("_: avg(1 0);", Float.unwrap(LibDecimalFloat.packLossless(5e37, -38)), "1 0");
checkHappy("_: avg(1 1);", Float.unwrap(LibDecimalFloat.packLossless(1e38, -38)), "1 1");
checkHappy("_: avg(1 2);", Float.unwrap(LibDecimalFloat.packLossless(15e37, -38)), "1 2");
checkHappy("_: avg(2 2);", Float.unwrap(LibDecimalFloat.packLossless(2e38, -38)), "2 2");
checkHappy("_: avg(2 3);", Float.unwrap(LibDecimalFloat.packLossless(25e37, -38)), "2 3");
checkHappy("_: avg(2 4);", Float.unwrap(LibDecimalFloat.packLossless(3e38, -38)), "2 4");
checkHappy("_: avg(4 0.5);", Float.unwrap(LibDecimalFloat.packLossless(225e36, -38)), "4 5");
}

// function testOpAvgEvalThreeInputs() external {
// checkBadInputs("_: avg(1 1 1);", 3, 2, 3);
// }
/// Test the eval of `avg` for bad inputs.
function testOpAvgEvalOneInput() external {
checkBadInputs("_: avg(1);", 1, 2, 1);
}

// function testOpAvgEvalZeroOutputs() external {
// checkBadOutputs(": avg(0 0);", 2, 1, 0);
// }
function testOpAvgEvalThreeInputs() external {
checkBadInputs("_: avg(1 1 1);", 3, 2, 3);
}

// function testOpAvgEvalTwoOutputs() external {
// checkBadOutputs("_ _: avg(0 0);", 2, 1, 2);
// }
function testOpAvgEvalZeroOutputs() external {
checkBadOutputs(": avg(0 0);", 2, 1, 0);
}

// /// Test that operand is disallowed.
// function testOpAvgEvalOperandDisallowed() external {
// checkUnhappyParse("_: avg<0>(1 1);", abi.encodeWithSelector(UnexpectedOperand.selector));
// }
// }
function testOpAvgEvalTwoOutputs() external {
checkBadOutputs("_ _: avg(0 0);", 2, 1, 2);
}

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