From 1eba240e7fbe5b01e7b82399939eec34684a884a Mon Sep 17 00:00:00 2001 From: Rappie Date: Fri, 1 Mar 2024 20:39:42 +0100 Subject: [PATCH 1/3] Add more types to assertion wrapper functions --- src/AssertWrapper.sol | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/AssertWrapper.sol b/src/AssertWrapper.sol index ed247bd..45f00a2 100644 --- a/src/AssertWrapper.sol +++ b/src/AssertWrapper.sol @@ -8,26 +8,54 @@ abstract contract AssertWrapper is AssertHelper { assertGt(a, b, message); } + function gt(int256 a, int256 b, string memory message) internal { + assertGt(a, b, message); + } + function lt(uint256 a, uint256 b, string memory message) internal { assertLt(a, b, message); } + function lt(int256 a, int256 b, string memory message) internal { + assertLt(a, b, message); + } + function gte(uint256 a, uint256 b, string memory message) internal { assertGte(a, b, message); } + function gte(int256 a, int256 b, string memory message) internal { + assertGte(a, b, message); + } + function lte(uint256 a, uint256 b, string memory message) internal { assertLte(a, b, message); } + function lte(int256 a, int256 b, string memory message) internal { + assertLte(a, b, message); + } + function eq(uint256 a, uint256 b, string memory message) internal { assertEq(a, b, message); } + function eq(int256 a, int256 b, string memory message) internal { + assertEq(a, b, message); + } + + function eq(bytes4 a, bytes4 b, string memory message) internal { + assertEq(a, b, message); + } + function neq(uint256 a, uint256 b, string memory message) internal { assertNeq(a, b, message); } + function neq(int256 a, int256 b, string memory message) internal { + assertNeq(a, b, message); + } + function t(bool a, string memory message) internal { assertWithMsg(a, message); } From 3f37e931c715128b90263b3ece50e8703397022d Mon Sep 17 00:00:00 2001 From: Rappie Date: Wed, 6 Mar 2024 20:59:35 +0100 Subject: [PATCH 2/3] Add Math and Random helpers --- src/FuzzBase.sol | 15 ++++++++++++--- src/MathHelper.sol | 8 ++++++++ src/RandomHelper.sol | 18 ++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 src/MathHelper.sol create mode 100644 src/RandomHelper.sol diff --git a/src/FuzzBase.sol b/src/FuzzBase.sol index 5ccb6dd..c05df26 100644 --- a/src/FuzzBase.sol +++ b/src/FuzzBase.sol @@ -1,12 +1,21 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.0; -import "./Logging.sol"; -import "./Constants.sol"; import "./AssertWrapper.sol"; import "./ClampWrapper.sol"; +import "./MathHelper.sol"; +import "./RandomHelper.sol"; +import "./Logging.sol"; +import "./Constants.sol"; import "./IHevm.sol"; -abstract contract FuzzBase is AssertWrapper, ClampWrapper, Logging, Constants { +abstract contract FuzzBase is + AssertWrapper, + ClampWrapper, + MathHelper, + RandomHelper, + Logging, + Constants +{ IHevm internal vm = IHevm(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D); } diff --git a/src/MathHelper.sol b/src/MathHelper.sol new file mode 100644 index 0000000..1e17229 --- /dev/null +++ b/src/MathHelper.sol @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +abstract contract MathHelper { + function min(uint256 a, uint256 b) internal pure returns (uint256) { + return a < b ? a : b; + } +} diff --git a/src/RandomHelper.sol b/src/RandomHelper.sol new file mode 100644 index 0000000..fed6854 --- /dev/null +++ b/src/RandomHelper.sol @@ -0,0 +1,18 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +abstract contract RandomHelper { + /// @notice Shuffle an array using Fisher-Yates algorithm + /// @dev Based on https://gist.github.com/scammi/602387a22e04c77beb73c0ebc0f0bc18 + function shuffleArray(uint256[] memory shuffle, uint256 entropy) internal { + for (uint256 i = shuffle.length - 1; i > 0; i--) { + uint256 swapIndex = entropy % (shuffle.length - i); + + uint256 currentIndex = shuffle[i]; + uint256 indexToSwap = shuffle[swapIndex]; + + shuffle[i] = indexToSwap; + shuffle[swapIndex] = currentIndex; + } + } +} From e632f36866b1fc6547885c0e607ecfe99971f8ed Mon Sep 17 00:00:00 2001 From: Rappie Date: Fri, 8 Mar 2024 12:40:34 +0100 Subject: [PATCH 3/3] Update src/MathHelper.sol Co-authored-by: 0xScourgedev --- src/MathHelper.sol | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/MathHelper.sol b/src/MathHelper.sol index 1e17229..f8d3ec2 100644 --- a/src/MathHelper.sol +++ b/src/MathHelper.sol @@ -5,4 +5,14 @@ abstract contract MathHelper { function min(uint256 a, uint256 b) internal pure returns (uint256) { return a < b ? a : b; } + + // Forked from https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v5.0.0/contracts/utils/math/SignedMath.sol + function max(int256 a, int256 b) internal pure returns (int256) { + return a > b ? a : b; + } + + // Forked with modifications from https://ethereum.stackexchange.com/a/84391 + function abs(int128 n) internal pure returns (int128) { + return n >= 0 ? n : -n; + } }