From 0c4c89f881adf47514868397131352858ce15414 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Fri, 19 Aug 2022 16:23:16 +0000 Subject: [PATCH 01/16] Minor optimizations --- contracts/proxy/Clones.sol | 41 +++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/contracts/proxy/Clones.sol b/contracts/proxy/Clones.sol index 8d8dc13173e..935d6ad4469 100644 --- a/contracts/proxy/Clones.sol +++ b/contracts/proxy/Clones.sol @@ -25,11 +25,9 @@ library Clones { function clone(address implementation) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { - let ptr := mload(0x40) - mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000) - mstore(add(ptr, 0x14), shl(0x60, implementation)) - mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000) - instance := create(0, ptr, 0x37) + mstore(0x00, or(0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000, shr(232, shl(96, implementation)))) + mstore(0x20, or(0x5af43d82803e903d91602b57fd5bf3, shl(120, implementation))) + instance := create(0, 0x09, 0x37) } require(instance != address(0), "ERC1167: create failed"); } @@ -44,11 +42,9 @@ library Clones { function cloneDeterministic(address implementation, bytes32 salt) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { - let ptr := mload(0x40) - mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000) - mstore(add(ptr, 0x14), shl(0x60, implementation)) - mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000) - instance := create2(0, ptr, 0x37, salt) + mstore(0x00, or(0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000, shr(232, shl(96, implementation)))) + mstore(0x20, or(0x5af43d82803e903d91602b57fd5bf3, shl(120, implementation))) + instance := create2(0, 0x09, 0x37, salt) } require(instance != address(0), "ERC1167: create2 failed"); } @@ -63,14 +59,23 @@ library Clones { ) internal pure returns (address predicted) { /// @solidity memory-safe-assembly assembly { - let ptr := mload(0x40) - mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000) - mstore(add(ptr, 0x14), shl(0x60, implementation)) - mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf3ff00000000000000000000000000000000) - mstore(add(ptr, 0x38), shl(0x60, deployer)) - mstore(add(ptr, 0x4c), salt) - mstore(add(ptr, 0x6c), keccak256(ptr, 0x37)) - predicted := keccak256(add(ptr, 0x37), 0x55) + // Cache the free memory pointer for restoring later. + let freeMemoryPointer := mload(0x40) + + mstore(0x00, or(0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000, shr(232, shl(96, implementation)))) + mstore(0x20, or(0x5af43d82803e903d91602b57fd5bf3, shl(120, implementation))) + + // Compute and Store the bytecode hash. + mstore(0x40, keccak256(0x09, 0x37)) + mstore(0x00, deployer) + // Store the prefix. + mstore8(0x0b, 0xff) + mstore(0x20, salt) + + predicted := keccak256(0x0b, 0x55) + + // Restore the free memory pointer. + mstore(0x40, freeMemoryPointer) } } From b6792abdf010fc65a188672944d38c9afc32a7ee Mon Sep 17 00:00:00 2001 From: Vectorized Date: Fri, 19 Aug 2022 16:27:05 +0000 Subject: [PATCH 02/16] Lint --- contracts/proxy/Clones.sol | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/proxy/Clones.sol b/contracts/proxy/Clones.sol index 935d6ad4469..ef1b4680cc5 100644 --- a/contracts/proxy/Clones.sol +++ b/contracts/proxy/Clones.sol @@ -64,14 +64,14 @@ library Clones { mstore(0x00, or(0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000, shr(232, shl(96, implementation)))) mstore(0x20, or(0x5af43d82803e903d91602b57fd5bf3, shl(120, implementation))) - + // Compute and Store the bytecode hash. mstore(0x40, keccak256(0x09, 0x37)) mstore(0x00, deployer) // Store the prefix. mstore8(0x0b, 0xff) mstore(0x20, salt) - + predicted := keccak256(0x0b, 0x55) // Restore the free memory pointer. From a63e18e59bd99f3057264106c6106d465d428a86 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Fri, 19 Aug 2022 16:30:52 +0000 Subject: [PATCH 03/16] Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b526dade7b0..836db502934 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ * `VestingWallet`: remove unused library `Math.sol`. ([#3605](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3605)) * `ECDSA`: Remove redundant check on the `v` value. ([#3591](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3591)) * `VestingWallet`: add `releasable` getters. ([#3580](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3580)) + * `Clones`: optimize assembly to use the scratch space instead of the free memory. ([#3640](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3640)) ### Deprecations From ca9e51d075e4022ebc07987e2226337fc64a5ff0 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Fri, 19 Aug 2022 16:35:01 +0000 Subject: [PATCH 04/16] Edit Changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 836db502934..d70ca17011e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,7 @@ * `VestingWallet`: remove unused library `Math.sol`. ([#3605](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3605)) * `ECDSA`: Remove redundant check on the `v` value. ([#3591](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3591)) * `VestingWallet`: add `releasable` getters. ([#3580](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3580)) - * `Clones`: optimize assembly to use the scratch space instead of the free memory. ([#3640](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3640)) + * `Clones`: optimize the assembly to use the scratch space instead of the free memory. ([#3640](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3640)) ### Deprecations From 068176ff80518a7cb3004fe567d401e5187c7205 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Fri, 19 Aug 2022 17:04:40 +0000 Subject: [PATCH 05/16] Add comments --- contracts/proxy/Clones.sol | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/contracts/proxy/Clones.sol b/contracts/proxy/Clones.sol index ef1b4680cc5..0420c8192de 100644 --- a/contracts/proxy/Clones.sol +++ b/contracts/proxy/Clones.sol @@ -25,8 +25,11 @@ library Clones { function clone(address implementation) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { + // Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes + // of the `implementation` address with the bytecode before the address. mstore(0x00, or(0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000, shr(232, shl(96, implementation)))) - mstore(0x20, or(0x5af43d82803e903d91602b57fd5bf3, shl(120, implementation))) + // Packs the remaining 17 bytes of `implementation` with the bytecode after the address. + mstore(0x20, or(shl(120, implementation), 0x5af43d82803e903d91602b57fd5bf3)) instance := create(0, 0x09, 0x37) } require(instance != address(0), "ERC1167: create failed"); @@ -42,8 +45,11 @@ library Clones { function cloneDeterministic(address implementation, bytes32 salt) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { + // Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes + // of the `implementation` address with the bytecode before the address. mstore(0x00, or(0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000, shr(232, shl(96, implementation)))) - mstore(0x20, or(0x5af43d82803e903d91602b57fd5bf3, shl(120, implementation))) + // Packs the remaining 17 bytes of `implementation` with the bytecode after the address. + mstore(0x20, or(shl(120, implementation), 0x5af43d82803e903d91602b57fd5bf3)) instance := create2(0, 0x09, 0x37, salt) } require(instance != address(0), "ERC1167: create2 failed"); @@ -62,14 +68,16 @@ library Clones { // Cache the free memory pointer for restoring later. let freeMemoryPointer := mload(0x40) + // Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes + // of the `implementation` address with the bytecode before the address. mstore(0x00, or(0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000, shr(232, shl(96, implementation)))) - mstore(0x20, or(0x5af43d82803e903d91602b57fd5bf3, shl(120, implementation))) + // Packs the remaining 17 bytes of `implementation` with the bytecode after the address. + mstore(0x20, or(shl(120, implementation), 0x5af43d82803e903d91602b57fd5bf3)) - // Compute and Store the bytecode hash. + // Compute and store the bytecode hash. mstore(0x40, keccak256(0x09, 0x37)) mstore(0x00, deployer) - // Store the prefix. - mstore8(0x0b, 0xff) + mstore8(0x0b, 0xff) // Store the prefix at the byte before `deployer`. mstore(0x20, salt) predicted := keccak256(0x0b, 0x55) From 8152832b7fb38293fdaf2fa7def9ba491482ff1c Mon Sep 17 00:00:00 2001 From: Vectorized Date: Fri, 19 Aug 2022 17:13:31 +0000 Subject: [PATCH 06/16] Use free memory for predict address --- contracts/proxy/Clones.sol | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/contracts/proxy/Clones.sol b/contracts/proxy/Clones.sol index 0420c8192de..5928299364f 100644 --- a/contracts/proxy/Clones.sol +++ b/contracts/proxy/Clones.sol @@ -65,25 +65,21 @@ library Clones { ) internal pure returns (address predicted) { /// @solidity memory-safe-assembly assembly { - // Cache the free memory pointer for restoring later. - let freeMemoryPointer := mload(0x40) + let ptr := mload(0x40) // Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes // of the `implementation` address with the bytecode before the address. - mstore(0x00, or(0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000, shr(232, shl(96, implementation)))) + mstore(ptr, or(0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000, shr(232, shl(96, implementation)))) // Packs the remaining 17 bytes of `implementation` with the bytecode after the address. - mstore(0x20, or(shl(120, implementation), 0x5af43d82803e903d91602b57fd5bf3)) + mstore(add(ptr, 0x20), or(shl(120, implementation), 0x5af43d82803e903d91602b57fd5bf3)) // Compute and store the bytecode hash. - mstore(0x40, keccak256(0x09, 0x37)) - mstore(0x00, deployer) - mstore8(0x0b, 0xff) // Store the prefix at the byte before `deployer`. - mstore(0x20, salt) - - predicted := keccak256(0x0b, 0x55) + mstore(add(ptr, 0x40), keccak256(add(ptr, 0x09), 0x37)) + mstore(ptr, deployer) + mstore8(add(ptr, 0x0b), 0xff) // Store the prefix at the byte before `deployer`. + mstore(add(ptr, 0x20), salt) - // Restore the free memory pointer. - mstore(0x40, freeMemoryPointer) + predicted := keccak256(add(ptr, 0x0b), 0x55) } } From ebe7262cc5c1d202ca65083471d5a3cb0f515bed Mon Sep 17 00:00:00 2001 From: Vectorized Date: Fri, 19 Aug 2022 17:27:38 +0000 Subject: [PATCH 07/16] Revert to original cloneDeterministic --- contracts/proxy/Clones.sol | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/contracts/proxy/Clones.sol b/contracts/proxy/Clones.sol index 5928299364f..26357ac56a0 100644 --- a/contracts/proxy/Clones.sol +++ b/contracts/proxy/Clones.sol @@ -66,20 +66,13 @@ library Clones { /// @solidity memory-safe-assembly assembly { let ptr := mload(0x40) - - // Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes - // of the `implementation` address with the bytecode before the address. - mstore(ptr, or(0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000, shr(232, shl(96, implementation)))) - // Packs the remaining 17 bytes of `implementation` with the bytecode after the address. - mstore(add(ptr, 0x20), or(shl(120, implementation), 0x5af43d82803e903d91602b57fd5bf3)) - - // Compute and store the bytecode hash. - mstore(add(ptr, 0x40), keccak256(add(ptr, 0x09), 0x37)) - mstore(ptr, deployer) - mstore8(add(ptr, 0x0b), 0xff) // Store the prefix at the byte before `deployer`. - mstore(add(ptr, 0x20), salt) - - predicted := keccak256(add(ptr, 0x0b), 0x55) + mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000) + mstore(add(ptr, 0x14), shl(0x60, implementation)) + mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf3ff00000000000000000000000000000000) + mstore(add(ptr, 0x38), shl(0x60, deployer)) + mstore(add(ptr, 0x4c), salt) + mstore(add(ptr, 0x6c), keccak256(ptr, 0x37)) + predicted := keccak256(add(ptr, 0x37), 0x55) } } From 3f461a9bcb69116be137a14117648d7840c79d19 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Fri, 19 Aug 2022 17:30:37 +0000 Subject: [PATCH 08/16] Edit changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d70ca17011e..50ed7c7b35d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,7 @@ * `VestingWallet`: remove unused library `Math.sol`. ([#3605](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3605)) * `ECDSA`: Remove redundant check on the `v` value. ([#3591](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3591)) * `VestingWallet`: add `releasable` getters. ([#3580](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3580)) - * `Clones`: optimize the assembly to use the scratch space instead of the free memory. ([#3640](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3640)) + * `Clones`: optimize the assembly to use the scratch space during deployments. ([#3640](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3640)) ### Deprecations From c8482fb93d3883345a7ff964e7604353315db042 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Fri, 19 Aug 2022 17:43:49 +0000 Subject: [PATCH 09/16] Change numbers to hex --- contracts/proxy/Clones.sol | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/contracts/proxy/Clones.sol b/contracts/proxy/Clones.sol index 26357ac56a0..4a360f6283b 100644 --- a/contracts/proxy/Clones.sol +++ b/contracts/proxy/Clones.sol @@ -27,9 +27,9 @@ library Clones { assembly { // Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes // of the `implementation` address with the bytecode before the address. - mstore(0x00, or(0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000, shr(232, shl(96, implementation)))) + mstore(0x00, or(0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000, shr(0xe8, shl(0x60, implementation)))) // Packs the remaining 17 bytes of `implementation` with the bytecode after the address. - mstore(0x20, or(shl(120, implementation), 0x5af43d82803e903d91602b57fd5bf3)) + mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3)) instance := create(0, 0x09, 0x37) } require(instance != address(0), "ERC1167: create failed"); @@ -47,9 +47,9 @@ library Clones { assembly { // Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes // of the `implementation` address with the bytecode before the address. - mstore(0x00, or(0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000, shr(232, shl(96, implementation)))) + mstore(0x00, or(0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000, shr(0xe8, shl(0x60, implementation)))) // Packs the remaining 17 bytes of `implementation` with the bytecode after the address. - mstore(0x20, or(shl(120, implementation), 0x5af43d82803e903d91602b57fd5bf3)) + mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3)) instance := create2(0, 0x09, 0x37, salt) } require(instance != address(0), "ERC1167: create2 failed"); From dd5a6454ff90b4a0d56a1c4389183deaa5a04f82 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Fri, 19 Aug 2022 18:03:35 +0000 Subject: [PATCH 10/16] Optimize predictDeterministicAddress --- contracts/proxy/Clones.sol | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/contracts/proxy/Clones.sol b/contracts/proxy/Clones.sol index 4a360f6283b..5b0bfb7bc75 100644 --- a/contracts/proxy/Clones.sol +++ b/contracts/proxy/Clones.sol @@ -66,13 +66,13 @@ library Clones { /// @solidity memory-safe-assembly assembly { let ptr := mload(0x40) - mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000) - mstore(add(ptr, 0x14), shl(0x60, implementation)) - mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf3ff00000000000000000000000000000000) - mstore(add(ptr, 0x38), shl(0x60, deployer)) - mstore(add(ptr, 0x4c), salt) - mstore(add(ptr, 0x6c), keccak256(ptr, 0x37)) - predicted := keccak256(add(ptr, 0x37), 0x55) + mstore(add(ptr, 0x38), deployer) + mstore(add(ptr, 0x24), 0x5af43d82803e903d91602b57fd5bf3ff) + mstore(add(ptr, 0x14), implementation) + mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73) + mstore(add(ptr, 0x58), salt) + mstore(add(ptr, 0x78), keccak256(add(ptr, 0x0c), 0x37)) + predicted := keccak256(add(ptr, 0x43), 0x55) } } From ae25cd4cf055345809046d59570fa7d9ddd9268c Mon Sep 17 00:00:00 2001 From: Vectorized Date: Fri, 19 Aug 2022 18:10:49 +0000 Subject: [PATCH 11/16] Edit changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50ed7c7b35d..f1cb9c2f06e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,7 @@ * `VestingWallet`: remove unused library `Math.sol`. ([#3605](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3605)) * `ECDSA`: Remove redundant check on the `v` value. ([#3591](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3591)) * `VestingWallet`: add `releasable` getters. ([#3580](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3580)) - * `Clones`: optimize the assembly to use the scratch space during deployments. ([#3640](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3640)) + * `Clones`: optimized the assembly to use only the scratch space during deployments, and optimized `predictDeterministicAddress` to use lesser operations. ([#3640](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3640)) ### Deprecations From 75e577044c5926f73162c60e647d392011578796 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Wed, 24 Aug 2022 16:56:41 +0800 Subject: [PATCH 12/16] Update contracts/proxy/Clones.sol Co-authored-by: Hadrien Croubois --- contracts/proxy/Clones.sol | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/contracts/proxy/Clones.sol b/contracts/proxy/Clones.sol index 5b0bfb7bc75..972c7107920 100644 --- a/contracts/proxy/Clones.sol +++ b/contracts/proxy/Clones.sol @@ -25,12 +25,11 @@ library Clones { function clone(address implementation) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { - // Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes - // of the `implementation` address with the bytecode before the address. - mstore(0x00, or(0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000, shr(0xe8, shl(0x60, implementation)))) - // Packs the remaining 17 bytes of `implementation` with the bytecode after the address. - mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3)) - instance := create(0, 0x09, 0x37) + // Packs the size 3 bytes of `implementation` with the bytecode before the address. + mstore(0x00, or(shr(0x88, implementation), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000)) + // Packs the remaining 17 bytes of `implementation` with the bytecode after the address. + mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3)) + instance := create(0, 0x00, 0x37) } require(instance != address(0), "ERC1167: create failed"); } From 6729eaee28e376304326f369068240e3823ace15 Mon Sep 17 00:00:00 2001 From: Hadrien Croubois Date: Wed, 24 Aug 2022 10:58:16 +0200 Subject: [PATCH 13/16] Update Clones.sol --- contracts/proxy/Clones.sol | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/contracts/proxy/Clones.sol b/contracts/proxy/Clones.sol index 972c7107920..f6ec0f6f977 100644 --- a/contracts/proxy/Clones.sol +++ b/contracts/proxy/Clones.sol @@ -25,10 +25,10 @@ library Clones { function clone(address implementation) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { - // Packs the size 3 bytes of `implementation` with the bytecode before the address. + // Packs the size 3 bytes of `implementation` with the bytecode before the address. mstore(0x00, or(shr(0x88, implementation), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000)) - // Packs the remaining 17 bytes of `implementation` with the bytecode after the address. - mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3)) + // Packs the remaining 17 bytes of `implementation` with the bytecode after the address. + mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3)) instance := create(0, 0x00, 0x37) } require(instance != address(0), "ERC1167: create failed"); @@ -44,9 +44,8 @@ library Clones { function cloneDeterministic(address implementation, bytes32 salt) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { - // Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes - // of the `implementation` address with the bytecode before the address. - mstore(0x00, or(0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000, shr(0xe8, shl(0x60, implementation)))) + // Packs the size 3 bytes of `implementation` with the bytecode before the address. + mstore(0x00, or(shr(0x88, implementation), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000)) // Packs the remaining 17 bytes of `implementation` with the bytecode after the address. mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3)) instance := create2(0, 0x09, 0x37, salt) From b6362c1222d43d159b8a772ec0c80d7d361327eb Mon Sep 17 00:00:00 2001 From: Hadrien Croubois Date: Wed, 24 Aug 2022 10:58:16 +0200 Subject: [PATCH 14/16] Revert "Update Clones.sol" This reverts commit 6729eaee28e376304326f369068240e3823ace15. --- contracts/proxy/Clones.sol | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/contracts/proxy/Clones.sol b/contracts/proxy/Clones.sol index f6ec0f6f977..972c7107920 100644 --- a/contracts/proxy/Clones.sol +++ b/contracts/proxy/Clones.sol @@ -25,10 +25,10 @@ library Clones { function clone(address implementation) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { - // Packs the size 3 bytes of `implementation` with the bytecode before the address. + // Packs the size 3 bytes of `implementation` with the bytecode before the address. mstore(0x00, or(shr(0x88, implementation), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000)) - // Packs the remaining 17 bytes of `implementation` with the bytecode after the address. - mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3)) + // Packs the remaining 17 bytes of `implementation` with the bytecode after the address. + mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3)) instance := create(0, 0x00, 0x37) } require(instance != address(0), "ERC1167: create failed"); @@ -44,8 +44,9 @@ library Clones { function cloneDeterministic(address implementation, bytes32 salt) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { - // Packs the size 3 bytes of `implementation` with the bytecode before the address. - mstore(0x00, or(shr(0x88, implementation), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000)) + // Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes + // of the `implementation` address with the bytecode before the address. + mstore(0x00, or(0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000, shr(0xe8, shl(0x60, implementation)))) // Packs the remaining 17 bytes of `implementation` with the bytecode after the address. mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3)) instance := create2(0, 0x09, 0x37, salt) From f09b4d00a430fc780e9dae453db9d5d8ad74fa85 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Wed, 24 Aug 2022 16:56:41 +0800 Subject: [PATCH 15/16] Revert "Update contracts/proxy/Clones.sol" This reverts commit 75e577044c5926f73162c60e647d392011578796. --- contracts/proxy/Clones.sol | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/contracts/proxy/Clones.sol b/contracts/proxy/Clones.sol index 972c7107920..5b0bfb7bc75 100644 --- a/contracts/proxy/Clones.sol +++ b/contracts/proxy/Clones.sol @@ -25,11 +25,12 @@ library Clones { function clone(address implementation) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { - // Packs the size 3 bytes of `implementation` with the bytecode before the address. - mstore(0x00, or(shr(0x88, implementation), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000)) - // Packs the remaining 17 bytes of `implementation` with the bytecode after the address. - mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3)) - instance := create(0, 0x00, 0x37) + // Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes + // of the `implementation` address with the bytecode before the address. + mstore(0x00, or(0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000, shr(0xe8, shl(0x60, implementation)))) + // Packs the remaining 17 bytes of `implementation` with the bytecode after the address. + mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3)) + instance := create(0, 0x09, 0x37) } require(instance != address(0), "ERC1167: create failed"); } From 83d95e6c23071f88b3506b7bd73adbabfbc0f304 Mon Sep 17 00:00:00 2001 From: Hadrien Croubois Date: Wed, 24 Aug 2022 11:12:58 +0200 Subject: [PATCH 16/16] minor reorder --- contracts/proxy/Clones.sol | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/proxy/Clones.sol b/contracts/proxy/Clones.sol index 5b0bfb7bc75..505fd613ad3 100644 --- a/contracts/proxy/Clones.sol +++ b/contracts/proxy/Clones.sol @@ -27,7 +27,7 @@ library Clones { assembly { // Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes // of the `implementation` address with the bytecode before the address. - mstore(0x00, or(0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000, shr(0xe8, shl(0x60, implementation)))) + mstore(0x00, or(shr(0xe8, shl(0x60, implementation)), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000)) // Packs the remaining 17 bytes of `implementation` with the bytecode after the address. mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3)) instance := create(0, 0x09, 0x37) @@ -47,7 +47,7 @@ library Clones { assembly { // Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes // of the `implementation` address with the bytecode before the address. - mstore(0x00, or(0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000, shr(0xe8, shl(0x60, implementation)))) + mstore(0x00, or(shr(0xe8, shl(0x60, implementation)), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000)) // Packs the remaining 17 bytes of `implementation` with the bytecode after the address. mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3)) instance := create2(0, 0x09, 0x37, salt)