Skip to content

Commit

Permalink
Replace local array sort impl with openzeppelin util
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Zhang <[email protected]>
  • Loading branch information
jimthematrix committed Oct 29, 2024
1 parent 1685ef7 commit fabacb4
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 80 deletions.
78 changes: 0 additions & 78 deletions solidity/contracts/lib/common.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,82 +59,4 @@ library Commonlib {

return keccak256(abi.encodePacked(inputs));
}

// copied from https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Arrays.sol
// until it's available in a new release
function sort(
uint256[] memory array
) internal pure returns (uint256[] memory) {
bytes32[] memory bytes32Array = _castToBytes32Array(array);
_quickSort(_begin(bytes32Array), _end(bytes32Array), _defaultComp);
return array;
}

function _castToBytes32Array(
uint256[] memory input
) private pure returns (bytes32[] memory output) {
assembly {
output := input
}
}

function _defaultComp(bytes32 a, bytes32 b) private pure returns (bool) {
return a < b;
}

function _begin(bytes32[] memory array) private pure returns (uint256 ptr) {
/// @solidity memory-safe-assembly
assembly {
ptr := add(array, 0x20)
}
}

function _end(bytes32[] memory array) private pure returns (uint256 ptr) {
unchecked {
return _begin(array) + array.length * 0x20;
}
}

function _mload(uint256 ptr) private pure returns (bytes32 value) {
assembly {
value := mload(ptr)
}
}

function _swap(uint256 ptr1, uint256 ptr2) private pure {
assembly {
let value1 := mload(ptr1)
let value2 := mload(ptr2)
mstore(ptr1, value2)
mstore(ptr2, value1)
}
}

function _quickSort(
uint256 begin,
uint256 end,
function(bytes32, bytes32) pure returns (bool) comp
) private pure {
unchecked {
if (end - begin < 0x40) return;

// Use first element as pivot
bytes32 pivot = _mload(begin);
// Position where the pivot should be at the end of the loop
uint256 pos = begin;

for (uint256 it = begin + 0x20; it < end; it += 0x20) {
if (comp(_mload(it), pivot)) {
// If the value stored at the iterator's position comes before the pivot, we increment the
// position of the pivot and move the value there.
pos += 0x20;
_swap(pos, it);
}
}

_swap(begin, pos); // Swap pivot into place
_quickSort(begin, pos, comp); // Sort the left side of the pivot
_quickSort(pos + 0x20, end, comp); // Sort the right side of the pivot
}
}
}
5 changes: 3 additions & 2 deletions solidity/contracts/lib/zeto_common.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pragma solidity ^0.8.20;

import {Commonlib} from "./common.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {Arrays} from "@openzeppelin/contracts/utils/Arrays.sol";
import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";

/// @title A sample base implementation of a Zeto based token contract
Expand Down Expand Up @@ -101,8 +102,8 @@ abstract contract ZetoCommon is OwnableUpgradeable {
for (uint256 i = 0; i < outputs.length; ++i) {
sortedOutputs[i] = outputs[i];
}
sortedInputs = Commonlib.sort(sortedInputs);
sortedOutputs = Commonlib.sort(sortedOutputs);
sortedInputs = Arrays.sort(sortedInputs);
sortedOutputs = Arrays.sort(sortedOutputs);
return (sortedInputs, sortedOutputs);
}
}

0 comments on commit fabacb4

Please sign in to comment.