Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into complete-merkle-tree
Browse files Browse the repository at this point in the history
  • Loading branch information
dmfxyz committed Mar 23, 2024
2 parents 55db21b + 60c3fa6 commit 1665dd7
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 137 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ jobs:
version: nightly

- name: Run Fuzzed Unit Tests
run: forge test --no-match-path src/test/StandardInput.t.sol
run: forge test --no-match-path src/test/StandardInput.t.sol --fuzz-runs 10000

- name: Run Differential Tests
run: |
npm --prefix differential_testing/scripts/ install
npm --prefix differential_testing/scripts/ run compile
FOUNDRY_FUZZ_RUNS=512 forge test --ffi --contracts differential_testing/test/DifferentialTests.t.sol
forge test --ffi --contracts differential_testing/test/DifferentialTests.t.sol --fuzz-runs 512
- name: Run Standard Gas Snapshotting
run: forge snapshot --gas-report --ffi --match-path src/test/StandardInput.t.sol
5 changes: 2 additions & 3 deletions .github/workflows/slither.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: crytic/slither-action@v0.2.0
- uses: crytic/slither-action@v0.3.1
with:
target: 'src/'
slither-args: '--exclude-informational --checklist'
slither-args: '--exclude-informational --checklist --show-ignored-findings'
fail-on: 'low'
23 changes: 17 additions & 6 deletions script/Merkle.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ contract MerkleScript is Script, ScriptHelper {
string[] private types = elements.readStringArray(".types");
uint256 private count = elements.readUint(".count");


bytes32[] private leafs = new bytes32[](count);

string[] private inputs = new string[](count);
Expand All @@ -41,13 +40,25 @@ contract MerkleScript is Script, ScriptHelper {
}

/// @dev Generate the JSON entries for the output file
function generateJsonEntries(string memory _inputs, string memory _proof, string memory _root, string memory _leaf) internal pure returns (string memory) {
function generateJsonEntries(string memory _inputs, string memory _proof, string memory _root, string memory _leaf)
internal
pure
returns (string memory)
{
string memory result = string.concat(
"{",
"\"inputs\":", _inputs, ",",
"\"proof\":", _proof, ",",
"\"root\":\"", _root, "\",",
"\"leaf\":\"", _leaf, "\"",
"\"inputs\":",
_inputs,
",",
"\"proof\":",
_proof,
",",
"\"root\":\"",
_root,
"\",",
"\"leaf\":\"",
_leaf,
"\"",
"}"
);

Expand Down
34 changes: 16 additions & 18 deletions script/common/ScriptHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import "forge-std/Script.sol";

/// @notice Helper functions for scripts
contract ScriptHelper is Script {

/// @dev Compares two strings and returns true iff they are equal.
function compareStrings(string memory a, string memory b) internal pure returns (bool) {
return (keccak256(abi.encodePacked((a))) == keccak256(abi.encodePacked((b))));
Expand All @@ -15,7 +14,7 @@ contract ScriptHelper is Script {
function ltrim64(bytes memory _bytes) internal pure returns (bytes memory) {
return slice(_bytes, 64, _bytes.length - 64);
}

/// @dev Returns a slice of `_bytes` starting at index `_start` and of length `_length`.
/// referenece: https://github.com/GNSPS/solidity-bytes-utils/blob/6458fb2780a3092bc756e737f246be1de6d3d362/contracts/BytesLib.sol#L228
function slice(bytes memory _bytes, uint256 _start, uint256 _length) internal pure returns (bytes memory) {
Expand All @@ -33,14 +32,10 @@ contract ScriptHelper is Script {
let mc := add(add(tempBytes, lengthmod), mul(0x20, iszero(lengthmod)))
let end := add(mc, _length)

for {
let cc := add(add(add(_bytes, lengthmod), mul(0x20, iszero(lengthmod))), _start)
} lt(mc, end) {
for { let cc := add(add(add(_bytes, lengthmod), mul(0x20, iszero(lengthmod))), _start) } lt(mc, end) {
mc := add(mc, 0x20)
cc := add(cc, 0x20)
} {
mstore(mc, mload(cc))
}
} { mstore(mc, mload(cc)) }

mstore(tempBytes, _length)
mstore(0x40, and(add(mc, 31), not(31)))
Expand All @@ -59,11 +54,12 @@ contract ScriptHelper is Script {
function stringArrayToString(string[] memory array) internal pure returns (string memory) {
string memory result = "[";

for (uint i = 0; i < array.length; i++) {
if (i != array.length - 1)
for (uint256 i = 0; i < array.length; i++) {
if (i != array.length - 1) {
result = string.concat(result, "\"", array[i], "\",");
else
} else {
result = string.concat(result, "\"", array[i], "\"");
}
}

return string.concat(result, "]");
Expand All @@ -73,11 +69,12 @@ contract ScriptHelper is Script {
function stringArrayToArrayString(string[] memory array) internal pure returns (string memory) {
string memory result = "[";

for (uint i = 0; i < array.length; i++) {
if (i != array.length - 1)
for (uint256 i = 0; i < array.length; i++) {
if (i != array.length - 1) {
result = string.concat(result, array[i], ",");
else
} else {
result = string.concat(result, array[i]);
}
}

return string.concat(result, "]");
Expand All @@ -87,13 +84,14 @@ contract ScriptHelper is Script {
function bytes32ArrayToString(bytes32[] memory array) internal pure returns (string memory) {
string memory result = "[";

for (uint i = 0; i < array.length; i++) {
if (i != array.length - 1)
for (uint256 i = 0; i < array.length; i++) {
if (i != array.length - 1) {
result = string.concat(result, "\"", vm.toString(array[i]), "\",");
else
} else {
result = string.concat(result, "\"", vm.toString(array[i]), "\"");
}
}

return string.concat(result, "]");
}
}
}
12 changes: 3 additions & 9 deletions script/test/Merkle.s.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@ import "openzeppelin-contracts/contracts/utils/cryptography/MerkleProof.sol";
import "../common/ScriptHelper.sol";

contract MerkleScriptOutputTest is Test, ScriptHelper {

// TODO: hardcoding the output from /script/target/output.json for simplicity now

bytes32[] proof = [
bytes32(0x073180b765e1cf92cb05630069c8481ec19171661317b4fab88b9c3c9835fb5b),
bytes32(0x7040f273bcf51ac5aeb606be19c784802b4a81469d609428e2487c5fa23e855e),
bytes32(0x00d9335e1f2b15d085d331fc240a20196e4ae5037b7d317f05d597c855a329b4)
];
bytes32 root =
0xb61922e9343b32c54f90f4cca2561cc277e090b279ba4f9e75d1a9994b895123;
bytes32 leaf =
0x035e33df50de019c2fdafb75e088976405fe8806b0341fa28db67c78e5e7f0e7;
bytes32 root = 0xb61922e9343b32c54f90f4cca2561cc277e090b279ba4f9e75d1a9994b895123;
bytes32 leaf = 0x035e33df50de019c2fdafb75e088976405fe8806b0341fa28db67c78e5e7f0e7;

address addr = 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266;
uint256 value = 10000;
Expand All @@ -39,10 +36,7 @@ contract MerkleScriptOutputTest is Test, ScriptHelper {
_bytes[1] = b_bytes;
_bytes[2] = c_bytes;

assertEq(
abi.encode(a, b, c),
ltrim64(abi.encode(_bytes))
);
assertEq(abi.encode(a, b, c), ltrim64(abi.encode(_bytes)));
}

function testComputeLeaf() public {
Expand Down
35 changes: 18 additions & 17 deletions src/Merkle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,25 @@ import "./common/MurkyBase.sol";
/// @author dmfxyz
/// @dev Note Generic Merkle Tree
contract Merkle is MurkyBase {

/********************
* HASHING FUNCTION *
********************/
/**
*
* HASHING FUNCTION *
*
*/

/// ascending sort and concat prior to hashing
function hashLeafPairs(bytes32 left, bytes32 right) public pure override returns (bytes32 _hash) {
assembly {
switch lt(left, right)
case 0 {
mstore(0x0, right)
mstore(0x20, left)
}
default {
mstore(0x0, left)
mstore(0x20, right)
}
_hash := keccak256(0x0, 0x40)
}
assembly {
switch lt(left, right)
case 0 {
mstore(0x0, right)
mstore(0x20, left)
}
default {
mstore(0x0, left)
mstore(0x20, right)
}
_hash := keccak256(0x0, 0x40)
}
}
}
}
14 changes: 7 additions & 7 deletions src/Xorkle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import "./common/MurkyBase.sol";
/// @author dmfxyz
/// @dev Note Xor Based "Merkle" Tree
contract Xorkle is MurkyBase {

/********************
* HASHING FUNCTION *
********************/

/**
*
* HASHING FUNCTION *
*
*/
function hashLeafPairs(bytes32 left, bytes32 right) public pure override returns (bytes32 _hash) {
// saves a few gas lol
assembly {
mstore(0x0, xor(left,right))
_hash := keccak256(0x0, 0x20)
mstore(0x0, xor(left, right))
_hash := keccak256(0x0, 0x20)
}
}
}
Loading

0 comments on commit 1665dd7

Please sign in to comment.