diff --git a/solidity/contracts/lib/common.sol b/solidity/contracts/lib/common.sol index f99c4ce..7b3d712 100644 --- a/solidity/contracts/lib/common.sol +++ b/solidity/contracts/lib/common.sol @@ -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 - } - } } diff --git a/solidity/contracts/lib/zeto_common.sol b/solidity/contracts/lib/zeto_common.sol index 03d607e..79013a1 100644 --- a/solidity/contracts/lib/zeto_common.sol +++ b/solidity/contracts/lib/zeto_common.sol @@ -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 @@ -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); } }