From 69e936264d1f8bbd32c7884ddbf5925dfe2c8aaf Mon Sep 17 00:00:00 2001 From: d1onys1us Date: Tue, 29 Nov 2022 23:43:36 -0500 Subject: [PATCH] chore(all): update to MIT license (#341) --- LICENSE.md | 9 + .../contracts/libs/LibAnchorSignature.sol | 10 +- .../contracts/libs/LibUint512Math.sol | 58 +++ .../contracts/thirdparty/AddressManager.sol | 22 + .../contracts/thirdparty/ERC20Upgradeable.sol | 34 +- .../thirdparty/LibBlockHeaderDecoder.sol | 2 +- .../contracts/thirdparty/LibBytesUtils.sol | 24 +- .../contracts/thirdparty/LibMerkleTrie.sol | 24 +- .../contracts/thirdparty/LibRLPReader.sol | 24 +- .../contracts/thirdparty/LibRLPWriter.sol | 24 +- .../thirdparty/LibSecureMerkleTrie.sol | 23 ++ .../contracts/thirdparty/LibUint512.sol | 390 ------------------ packages/protocol/tasks/deploy_L1.ts | 3 - packages/protocol/test/L1/TaikoL1.test.ts | 6 - .../test/libs/LibAnchorSignature.test.ts | 18 +- 15 files changed, 240 insertions(+), 431 deletions(-) create mode 100644 LICENSE.md create mode 100644 packages/protocol/contracts/libs/LibUint512Math.sol delete mode 100644 packages/protocol/contracts/thirdparty/LibUint512.sol diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 00000000000..04f9610f137 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,9 @@ +MIT License + +Copyright 2022 Taiko Labs + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/packages/protocol/contracts/libs/LibAnchorSignature.sol b/packages/protocol/contracts/libs/LibAnchorSignature.sol index 7256fd32410..7de1e9b93b1 100644 --- a/packages/protocol/contracts/libs/LibAnchorSignature.sol +++ b/packages/protocol/contracts/libs/LibAnchorSignature.sol @@ -7,7 +7,7 @@ // ╱╱┃┃┃╭╮┃┃╭╮┫╰╯┃┃╰━╯┃╭╮┃╰╯┣━━┃ // ╱╱╰╯╰╯╰┻┻╯╰┻━━╯╰━━━┻╯╰┻━━┻━━╯ pragma solidity ^0.8.9; -import "../thirdparty/LibUint512.sol"; +import "./LibUint512Math.sol"; /// @author david library LibAnchorSignature { @@ -32,7 +32,7 @@ library LibAnchorSignature { // ( // uint256 GX_MUL_GOLDEN_TOUCH_PRIVATEKEY_LOW, // uint256 GX_MUL_GOLDEN_TOUCH_PRIVATEKEY_HIGH - // ) = Uint512.mul256x256(GX, TAIKO_GOLDEN_TOUCH_PRIVATEKEY); + // ) = LibUint512Math.mul(GX, TAIKO_GOLDEN_TOUCH_PRIVATEKEY); uint256 public constant GX_MUL_GOLDEN_TOUCH_PRIVATEKEY_LOW = 0xb4a95509ce05fe8d45987859a067780d16a367c0e2cacf79cd301b93fb717940; uint256 public constant GX_MUL_GOLDEN_TOUCH_PRIVATEKEY_HIGH = @@ -41,7 +41,7 @@ library LibAnchorSignature { // ( // uint256 GX2_MUL_GOLDEN_TOUCH_PRIVATEKEY_LOW, // uint256 GX2_MUL_GOLDEN_TOUCH_PRIVATEKEY_HIGH - // ) = Uint512.mul256x256(GX2, TAIKO_GOLDEN_TOUCH_PRIVATEKEY); + // ) = LibUint512Math.mul(GX2, TAIKO_GOLDEN_TOUCH_PRIVATEKEY); uint256 public constant GX2_MUL_GOLDEN_TOUCH_PRIVATEKEY_LOW = 0xad77eceea844778cb4376153fc8f06f12f1695df4585bf75bfb17ec19ce90818; uint256 public constant GX2_MUL_GOLDEN_TOUCH_PRIVATEKEY_HIGH = @@ -67,7 +67,7 @@ library LibAnchorSignature { ? GX_MUL_GOLDEN_TOUCH_PRIVATEKEY_HIGH : GX2_MUL_GOLDEN_TOUCH_PRIVATEKEY_HIGH; - (low256, high256) = Uint512.add512x512( + (low256, high256) = LibUint512Math.add( low256, high256, uint256(digest), @@ -77,7 +77,7 @@ library LibAnchorSignature { if (k == 1) { s = expmod(low256, high256, 1, N); } else { - (low256, high256) = Uint512.mul256x256( + (low256, high256) = LibUint512Math.mul( K_2_INVM_N, expmod(low256, high256, 1, N) ); diff --git a/packages/protocol/contracts/libs/LibUint512Math.sol b/packages/protocol/contracts/libs/LibUint512Math.sol new file mode 100644 index 00000000000..5c29772cd9a --- /dev/null +++ b/packages/protocol/contracts/libs/LibUint512Math.sol @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: MIT +// The MIT License (MIT) + +// Copyright (c) 2021 Remco Bloemen +// Copyright (c) 2022 Taiko Labs + +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: + +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +pragma solidity ^0.8.9; + +library LibUint512Math { + /** + * Optimized full 512 bit multiplication in Solidity. + * Taken from: https://xn--2-umb.com/17/full-mul/index.html + */ + function mul( + uint256 a, + uint256 b + ) internal pure returns (uint256 r0, uint256 r1) { + assembly { + let mm := mulmod(a, b, not(0)) + r0 := mul(a, b) + r1 := sub(sub(mm, r0), lt(mm, r0)) + } + } + + /** + * Simple 512-bit addition. + * Taken from: https://xn--2-umb.com/17/512-bit-division/#add-subtract-two-512-bit-numbers + */ + function add( + uint256 a0, + uint256 a1, + uint256 b0, + uint256 b1 + ) internal pure returns (uint256 r0, uint256 r1) { + assembly { + r0 := add(a0, b0) + r1 := add(add(a1, b1), lt(r0, a0)) + } + } +} diff --git a/packages/protocol/contracts/thirdparty/AddressManager.sol b/packages/protocol/contracts/thirdparty/AddressManager.sol index 57ecfa6cb33..5b7c129e4c0 100644 --- a/packages/protocol/contracts/thirdparty/AddressManager.sol +++ b/packages/protocol/contracts/thirdparty/AddressManager.sol @@ -5,7 +5,29 @@ // - `Ownable.sol` modified to `OwnableUpgradeable.sol` // - `init()` added to initialize // - `setAddress` modified to `addAddress` to conform to ABI of `IAddressManager.sol` +// (The MIT License) +// Copyright 2020-2021 Optimism +// Copyright 2022 Taiko Labs + +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: + +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. pragma solidity ^0.8.9; /* External Imports */ diff --git a/packages/protocol/contracts/thirdparty/ERC20Upgradeable.sol b/packages/protocol/contracts/thirdparty/ERC20Upgradeable.sol index ed90869ac86..ddf9ff3b57d 100644 --- a/packages/protocol/contracts/thirdparty/ERC20Upgradeable.sol +++ b/packages/protocol/contracts/thirdparty/ERC20Upgradeable.sol @@ -1,12 +1,32 @@ // SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v4.5.0) (token/ERC20/ERC20.sol) - -/* -This file is copied from "@openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol" with the following modifications: - -1. decimals can be customized -2. `Transfer` events are not emitted from _mint and _burn. -*/ +// This file is copied from "@openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol" +// with the following modifications: +// - decimals can be customized +// - `Transfer` events are not emitted from _mint and _burn. +// The MIT License (MIT) + +// Copyright (c) 2016-2022 zOS Global Limited and contributors +// Copyright (c) 2022 Taiko Labs + +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: + +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. pragma solidity ^0.8.0; import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol"; diff --git a/packages/protocol/contracts/thirdparty/LibBlockHeaderDecoder.sol b/packages/protocol/contracts/thirdparty/LibBlockHeaderDecoder.sol index ed4e728b996..e66ff7e2c33 100644 --- a/packages/protocol/contracts/thirdparty/LibBlockHeaderDecoder.sol +++ b/packages/protocol/contracts/thirdparty/LibBlockHeaderDecoder.sol @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // Taken from https://github.com/privacy-scaling-explorations/zkevm-chain/blob/master/contracts/ZkEvmL2MessageDeliverer.sol#L23 -/* solhint-disable */ +// NOTE: No MIT license provided at the time, only SPDX-License-Identifier pragma solidity ^0.8.9; /** diff --git a/packages/protocol/contracts/thirdparty/LibBytesUtils.sol b/packages/protocol/contracts/thirdparty/LibBytesUtils.sol index 5363f867b95..fe11d3cca98 100644 --- a/packages/protocol/contracts/thirdparty/LibBytesUtils.sol +++ b/packages/protocol/contracts/thirdparty/LibBytesUtils.sol @@ -1,6 +1,28 @@ // SPDX-License-Identifier: MIT // Taken from https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts/contracts/libraries/utils/LibBytesUtils.sol - +// (The MIT License) + +// Copyright 2020-2021 Optimism +// Copyright 2022 Taiko Labs + +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: + +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. pragma solidity ^0.8.9; /** diff --git a/packages/protocol/contracts/thirdparty/LibMerkleTrie.sol b/packages/protocol/contracts/thirdparty/LibMerkleTrie.sol index 7a59bd6abe6..cd667a2466c 100644 --- a/packages/protocol/contracts/thirdparty/LibMerkleTrie.sol +++ b/packages/protocol/contracts/thirdparty/LibMerkleTrie.sol @@ -1,6 +1,28 @@ // SPDX-License-Identifier: MIT // Taken from https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts/contracts/libraries/trie/LibMerkleTrie.sol - +// (The MIT License) + +// Copyright 2020-2021 Optimism +// Copyright 2022 Taiko Labs + +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: + +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. pragma solidity ^0.8.9; /* Library Imports */ diff --git a/packages/protocol/contracts/thirdparty/LibRLPReader.sol b/packages/protocol/contracts/thirdparty/LibRLPReader.sol index 180221d8967..3309b893e1a 100644 --- a/packages/protocol/contracts/thirdparty/LibRLPReader.sol +++ b/packages/protocol/contracts/thirdparty/LibRLPReader.sol @@ -1,6 +1,28 @@ // SPDX-License-Identifier: MIT // Taken from https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts/contracts/libraries/rlp/LibRLPReader.sol - +// (The MIT License) + +// Copyright 2020-2021 Optimism +// Copyright 2022 Taiko Labs + +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: + +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. pragma solidity ^0.8.9; /** diff --git a/packages/protocol/contracts/thirdparty/LibRLPWriter.sol b/packages/protocol/contracts/thirdparty/LibRLPWriter.sol index 12ad25a63c9..f1622b38d17 100644 --- a/packages/protocol/contracts/thirdparty/LibRLPWriter.sol +++ b/packages/protocol/contracts/thirdparty/LibRLPWriter.sol @@ -1,7 +1,29 @@ // SPDX-License-Identifier: MIT // Taken from https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts/contracts/libraries/rlp/LibRLPWriter.sol // Modified to support writeBytes32/writeUint64 - +// (The MIT License) + +// Copyright 2020-2021 Optimism +// Copyright 2022 Taiko Labs + +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: + +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. pragma solidity ^0.8.9; /** diff --git a/packages/protocol/contracts/thirdparty/LibSecureMerkleTrie.sol b/packages/protocol/contracts/thirdparty/LibSecureMerkleTrie.sol index 207ea9cc171..6607c64d3de 100644 --- a/packages/protocol/contracts/thirdparty/LibSecureMerkleTrie.sol +++ b/packages/protocol/contracts/thirdparty/LibSecureMerkleTrie.sol @@ -1,5 +1,28 @@ // SPDX-License-Identifier: MIT // Taken from https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts/contracts/libraries/trie/LibSecureMerkleTrie.sol +// (The MIT License) + +// Copyright 2020-2021 Optimism +// Copyright 2022 Taiko Labs + +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: + +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. pragma solidity ^0.8.9; /* Library Imports */ diff --git a/packages/protocol/contracts/thirdparty/LibUint512.sol b/packages/protocol/contracts/thirdparty/LibUint512.sol deleted file mode 100644 index d63eaef3e09..00000000000 --- a/packages/protocol/contracts/thirdparty/LibUint512.sol +++ /dev/null @@ -1,390 +0,0 @@ -// SPDX-License-Identifier: GPLv3 -// Taken from https://github.com/SimonSuckut/Solidity_Uint512/blob/main/contracts/Uint512.sol - -pragma solidity ^0.8.9; - -library Uint512 { - /// @notice Calculates the product of two uint256 - /// @dev Used the chinese remainder theoreme - /// @param a A uint256 representing the first factor. - /// @param b A uint256 representing the second factor. - /// @return r0 The result as an uint512. r0 contains the lower bits. - /// @return r1 The higher bits of the result. - function mul256x256( - uint256 a, - uint256 b - ) public pure returns (uint256 r0, uint256 r1) { - assembly { - let mm := mulmod(a, b, not(0)) - r0 := mul(a, b) - r1 := sub(sub(mm, r0), lt(mm, r0)) - } - } - - /// @notice Calculates the product of two uint512 and uint256 - /// @dev Used the chinese remainder theoreme - /// @param a0 A uint256 representing lower bits of the first factor. - /// @param a1 A uint256 representing higher bits of the first factor. - /// @param b A uint256 representing the second factor. - /// @return r0 The result as an uint512. r0 contains the lower bits. - /// @return r1 The higher bits of the result. - function mul512x256( - uint256 a0, - uint256 a1, - uint256 b - ) public pure returns (uint256 r0, uint256 r1) { - assembly { - let mm := mulmod(a0, b, not(0)) - r0 := mul(a0, b) - r1 := sub(sub(mm, r0), lt(mm, r0)) - r1 := add(r1, mul(a1, b)) - } - } - - /// @notice Calculates the product and remainder of two uint256 - /// @dev Used the chinese remainder theoreme - /// @param a A uint256 representing the first factor. - /// @param b A uint256 representing the second factor. - /// @return r0 The result as an uint512. r0 contains the lower bits. - /// @return r1 The higher bits of the result. - /// @return r2 The remainder. - function mulMod256x256( - uint256 a, - uint256 b, - uint256 c - ) public pure returns (uint256 r0, uint256 r1, uint256 r2) { - assembly { - let mm := mulmod(a, b, not(0)) - r0 := mul(a, b) - r1 := sub(sub(mm, r0), lt(mm, r0)) - r2 := mulmod(a, b, c) - } - } - - /// @notice Calculates the difference of two uint512 - /// @param a0 A uint256 representing the lower bits of the first addend. - /// @param a1 A uint256 representing the higher bits of the first addend. - /// @param b0 A uint256 representing the lower bits of the seccond addend. - /// @param b1 A uint256 representing the higher bits of the seccond addend. - /// @return r0 The result as an uint512. r0 contains the lower bits. - /// @return r1 The higher bits of the result. - function add512x512( - uint256 a0, - uint256 a1, - uint256 b0, - uint256 b1 - ) public pure returns (uint256 r0, uint256 r1) { - assembly { - r0 := add(a0, b0) - r1 := add(add(a1, b1), lt(r0, a0)) - } - } - - /// @notice Calculates the difference of two uint512 - /// @param a0 A uint256 representing the lower bits of the minuend. - /// @param a1 A uint256 representing the higher bits of the minuend. - /// @param b0 A uint256 representing the lower bits of the subtrahend. - /// @param b1 A uint256 representing the higher bits of the subtrahend. - /// @return r0 The result as an uint512. r0 contains the lower bits. - /// @return r1 The higher bits of the result. - function sub512x512( - uint256 a0, - uint256 a1, - uint256 b0, - uint256 b1 - ) public pure returns (uint256 r0, uint256 r1) { - assembly { - r0 := sub(a0, b0) - r1 := sub(sub(a1, b1), lt(a0, b0)) - } - } - - /// @notice Calculates the division of a 512 bit unsigned integer by a 256 bit integer. It - /// requires the remainder to be known and the result must fit in a 256 bit integer. - /// @dev For a detailed explaination see: - /// https://www.researchgate.net/publication/235765881_Efficient_long_division_via_Montgomery_multiply. - /// @param a0 A uint256 representing the low bits of the nominator. - /// @param a1 A uint256 representing the high bits of the nominator. - /// @param b A uint256 representing the denominator. - /// @param rem A uint256 representing the remainder of the devision. The algorithm is cheaper to compute if the remainder is known. The remainder often be retreived cheaply using the mulmod and addmod operations. - /// @return r The result as an uint256. Result must have at most 256 bit. - function divRem512x256( - uint256 a0, - uint256 a1, - uint256 b, - uint256 rem - ) public pure returns (uint256 r) { - assembly { - // subtract the remainder - a1 := sub(a1, lt(a0, rem)) - a0 := sub(a0, rem) - - // The integer space mod 2**256 is not an abilian group on the multiplication operation. In fact the - // multiplicative inserve only exists for odd numbers. The denominator gets shifted right until the - // least significant bit is 1. To do this we find the biggest power of 2 that devides the denominator. - let pow := and(sub(0, b), b) - b := div(b, pow) - - // Also shift the nominator. We only shift a0 and the lower bits of a1 which are transfered into a0 - // by the shift operation. a1 no longer required for the calculation. This might sound odd, but in - // fact under the conditions that r < 2**255 and a / b = (r * a) + rem with rem = 0 the value of a1 - // is uniquely identified. Thus the value is not needed for the calculation. - a0 := div(a0, pow) - pow := add(div(sub(0, pow), pow), 1) - a0 := or(a0, mul(a1, pow)) - } - - // if a1 is zero after the shifting, a single word division is sufficient - if (a1 == 0) return a0 / b; - - assembly { - // Calculate the multiplicative inverse mod 2**256 of b. See the paper for details. - let inv := xor(mul(3, b), 2) - inv := mul(inv, sub(2, mul(b, inv))) - inv := mul(inv, sub(2, mul(b, inv))) - inv := mul(inv, sub(2, mul(b, inv))) - inv := mul(inv, sub(2, mul(b, inv))) - inv := mul(inv, sub(2, mul(b, inv))) - inv := mul(inv, sub(2, mul(b, inv))) - - r := mul(a0, inv) - } - } - - /// @notice Calculates the division of a 512 bit unsigned integer by a 256 bit integer. It - /// requires the result to fit in a 256 bit integer. - /// @dev For a detailed explaination see: - /// https://www.researchgate.net/publication/235765881_Efficient_long_division_via_Montgomery_multiply. - /// @param a0 A uint256 representing the low bits of the nominator. - /// @param a1 A uint256 representing the high bits of the nominator. - /// @param b A uint256 representing the denominator. - /// @return r The result as an uint256. Result must have at most 256 bit. - function div512x256( - uint256 a0, - uint256 a1, - uint256 b - ) public pure returns (uint256 r) { - assembly { - // calculate the remainder - let rem := mulmod(a1, not(0), b) - rem := addmod(rem, a1, b) - rem := addmod(rem, a0, b) - - // subtract the remainder - a1 := sub(a1, lt(a0, rem)) - a0 := sub(a0, rem) - - // The integer space mod 2**256 is not an abilian group on the multiplication operation. In fact the - // multiplicative inserve only exists for odd numbers. The denominator gets shifted right until the - // least significant bit is 1. To do this we find the biggest power of 2 that devides the denominator. - let pow := and(sub(0, b), b) - b := div(b, pow) - - // Also shift the nominator. We only shift a0 and the lower bits of a1 which are transfered into a0 - // by the shift operation. a1 no longer required for the calculation. This might sound odd, but in - // fact under the conditions that r < 2**255 and a / b = (r * a) + rem with rem = 0 the value of a1 - // is uniquely identified. Thus the value is not needed for the calculation. - a0 := div(a0, pow) - pow := add(div(sub(0, pow), pow), 1) - a0 := or(a0, mul(a1, pow)) - } - - // if a1 is zero after the shifting, a single word division is sufficient - if (a1 == 0) return a0 / b; - - assembly { - // Calculate the multiplicative inverse mod 2**256 of b. See the paper for details. - let inv := xor(mul(3, b), 2) - inv := mul(inv, sub(2, mul(b, inv))) - inv := mul(inv, sub(2, mul(b, inv))) - inv := mul(inv, sub(2, mul(b, inv))) - inv := mul(inv, sub(2, mul(b, inv))) - inv := mul(inv, sub(2, mul(b, inv))) - inv := mul(inv, sub(2, mul(b, inv))) - - r := mul(a0, inv) - } - } - - /// @notice Calculates the square root of x, rounding down. - /// @dev Uses the Babylonian method https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method. - /// @param x The uint256 number for which to calculate the square root. - /// @return s The square root as an uint256. - function sqrt256(uint256 x) public pure returns (uint256 s) { - if (x == 0) return 0; - - assembly { - s := 1 - - let xAux := x - - let cmp := or( - gt(xAux, 0x100000000000000000000000000000000), - eq(xAux, 0x100000000000000000000000000000000) - ) - xAux := sar(mul(cmp, 128), xAux) - s := shl(mul(cmp, 64), s) - - cmp := or( - gt(xAux, 0x10000000000000000), - eq(xAux, 0x10000000000000000) - ) - xAux := sar(mul(cmp, 64), xAux) - s := shl(mul(cmp, 32), s) - - cmp := or(gt(xAux, 0x100000000), eq(xAux, 0x100000000)) - xAux := sar(mul(cmp, 32), xAux) - s := shl(mul(cmp, 16), s) - - cmp := or(gt(xAux, 0x10000), eq(xAux, 0x10000)) - xAux := sar(mul(cmp, 16), xAux) - s := shl(mul(cmp, 8), s) - - cmp := or(gt(xAux, 0x100), eq(xAux, 0x100)) - xAux := sar(mul(cmp, 8), xAux) - s := shl(mul(cmp, 4), s) - - cmp := or(gt(xAux, 0x10), eq(xAux, 0x10)) - xAux := sar(mul(cmp, 4), xAux) - s := shl(mul(cmp, 2), s) - - s := shl(mul(or(gt(xAux, 0x8), eq(xAux, 0x8)), 2), s) - } - - unchecked { - s = (s + x / s) >> 1; - s = (s + x / s) >> 1; - s = (s + x / s) >> 1; - s = (s + x / s) >> 1; - s = (s + x / s) >> 1; - s = (s + x / s) >> 1; - s = (s + x / s) >> 1; - uint256 roundedDownResult = x / s; - return s >= roundedDownResult ? roundedDownResult : s; - } - } - - /// @notice Calculates the square root of a 512 bit unsigned integer, rounding down. - /// @dev Uses the Karatsuba Square Root method. See https://hal.inria.fr/inria-00072854/document for details. - /// @param a0 A uint256 representing the low bits of the input. - /// @param a1 A uint256 representing the high bits of the input. - /// @return s The square root as an uint256. Result has at most 256 bit. - function sqrt512(uint256 a0, uint256 a1) public pure returns (uint256 s) { - // A simple 256 bit square root is sufficient - if (a1 == 0) return sqrt256(a0); - - // The used algorithm has the pre-condition a1 >= 2**254 - uint256 shift; - - assembly { - let digits := mul(lt(a1, 0x100000000000000000000000000000000), 128) - a1 := shl(digits, a1) - shift := add(shift, digits) - - digits := mul( - lt(a1, 0x1000000000000000000000000000000000000000000000000), - 64 - ) - a1 := shl(digits, a1) - shift := add(shift, digits) - - digits := mul( - lt( - a1, - 0x100000000000000000000000000000000000000000000000000000000 - ), - 32 - ) - a1 := shl(digits, a1) - shift := add(shift, digits) - - digits := mul( - lt( - a1, - 0x1000000000000000000000000000000000000000000000000000000000000 - ), - 16 - ) - a1 := shl(digits, a1) - shift := add(shift, digits) - - digits := mul( - lt( - a1, - 0x100000000000000000000000000000000000000000000000000000000000000 - ), - 8 - ) - a1 := shl(digits, a1) - shift := add(shift, digits) - - digits := mul( - lt( - a1, - 0x1000000000000000000000000000000000000000000000000000000000000000 - ), - 4 - ) - a1 := shl(digits, a1) - shift := add(shift, digits) - - digits := mul( - lt( - a1, - 0x4000000000000000000000000000000000000000000000000000000000000000 - ), - 2 - ) - a1 := shl(digits, a1) - shift := add(shift, digits) - - a1 := or(a1, shr(sub(256, shift), a0)) - a0 := shl(shift, a0) - } - - uint256 sp = sqrt256(a1); - uint256 rp = a1 - (sp * sp); - - uint256 nom; - uint256 denom; - uint256 u; - uint256 q; - - assembly { - nom := or(shl(128, rp), shr(128, a0)) - denom := shl(1, sp) - q := div(nom, denom) - u := mod(nom, denom) - - // The nominator can be bigger than 2**256. We know that rp < (sp+1) * (sp+1). As sp can be - // at most floor(sqrt(2**256 - 1)) we can conclude that the nominator has at most 513 bits - // set. An expensive 512x256 bit division can be avoided by treating the bit at position 513 manually - let carry := shr(128, rp) - let x := mul( - carry, - 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - ) - q := add(q, div(x, denom)) - u := add(u, add(carry, mod(x, denom))) - q := add(q, div(u, denom)) - u := mod(u, denom) - } - - unchecked { - s = (sp << 128) + q; - - uint256 rl = ((u << 128) | - (a0 & 0xffffffffffffffffffffffffffffffff)); - uint256 rr = q * q; - - if ( - (q >> 128) > (u >> 128) || - (((q >> 128) == (u >> 128)) && rl < rr) - ) { - s = s - 1; - } - - return s >> (shift / 2); - } - } -} diff --git a/packages/protocol/tasks/deploy_L1.ts b/packages/protocol/tasks/deploy_L1.ts index da0787961c4..07fcd7a7f42 100644 --- a/packages/protocol/tasks/deploy_L1.ts +++ b/packages/protocol/tasks/deploy_L1.ts @@ -162,7 +162,6 @@ async function deployBaseLibs(hre: any) { "LibReceiptDecoder" ) const libTxDecoder = await utils.deployContract(hre, "LibTxDecoder") - const libUint512 = await utils.deployContract(hre, "Uint512") const v1Utils = await utils.deployContract(hre, "V1Utils") const v1Finalizing = await utils.deployContract(hre, "V1Finalizing", { @@ -176,7 +175,6 @@ async function deployBaseLibs(hre: any) { LibZKP: libZKP.address, LibReceiptDecoder: libReceiptDecoder.address, LibTxDecoder: libTxDecoder.address, - Uint512: libUint512.address, V1Utils: v1Utils.address, }) @@ -185,7 +183,6 @@ async function deployBaseLibs(hre: any) { V1Proposing: v1Proposing.address, V1Proving: v1Proving.address, V1Utils: v1Utils.address, - Uint512: libUint512.address, } } diff --git a/packages/protocol/test/L1/TaikoL1.test.ts b/packages/protocol/test/L1/TaikoL1.test.ts index 2a08f5e833d..03ee88752c3 100644 --- a/packages/protocol/test/L1/TaikoL1.test.ts +++ b/packages/protocol/test/L1/TaikoL1.test.ts @@ -9,10 +9,6 @@ describe("TaikoL1", function () { ).deploy() await addressManager.init() - const uint512 = await ( - await ethers.getContractFactory("Uint512") - ).deploy() - const libReceiptDecoder = await ( await ethers.getContractFactory("LibReceiptDecoder") ).deploy() @@ -43,7 +39,6 @@ describe("TaikoL1", function () { LibReceiptDecoder: libReceiptDecoder.address, LibTxDecoder: libTxDecoder.address, LibZKP: libZKP.address, - Uint512: uint512.address, V1Utils: v1Utils.address, }, }) @@ -62,7 +57,6 @@ describe("TaikoL1", function () { V1Finalizing: v1Finalizing.address, V1Proposing: v1Proposing.address, V1Proving: v1Proving.address, - Uint512: uint512.address, V1Utils: v1Utils.address, }, }) diff --git a/packages/protocol/test/libs/LibAnchorSignature.test.ts b/packages/protocol/test/libs/LibAnchorSignature.test.ts index 0bafd46cda7..ddb083210de 100644 --- a/packages/protocol/test/libs/LibAnchorSignature.test.ts +++ b/packages/protocol/test/libs/LibAnchorSignature.test.ts @@ -3,8 +3,6 @@ import { UnsignedTransaction } from "ethers" import { ethers } from "hardhat" describe("LibAnchorSignature", function () { - let libAnchorSignature: any - const unsignedLegacyTx: UnsignedTransaction = { type: 0, chainId: Math.floor(Math.random() * 1024), @@ -16,21 +14,11 @@ describe("LibAnchorSignature", function () { data: ethers.utils.randomBytes(32), } - before(async function () { - const libUint512 = await ( - await ethers.getContractFactory("Uint512") - ).deploy() - - libAnchorSignature = await ( - await ethers.getContractFactory("TestLibAnchorSignature", { - libraries: { - Uint512: libUint512.address, - }, - }) + it("should calculate correct signature values", async function () { + const libAnchorSignature: any = await ( + await ethers.getContractFactory("TestLibAnchorSignature") ).deploy() - }) - it("should calculate correct signature values", async function () { const validKs = [1, 2] for (const k of validKs) {