Skip to content

Instantly share code, notes, and snippets.

@IllIllI000
Last active June 24, 2023 13:48
Show Gist options
  • Save IllIllI000/2ee970e4f05af4d2a3d89a56b5cc93a5 to your computer and use it in GitHub Desktop.
Save IllIllI000/2ee970e4f05af4d2a3d89a56b5cc93a5 to your computer and use it in GitHub Desktop.

AbiEncodeVsEPacked_e.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.13; // optimize 200

/**
 * @title AbiEncodeVsEPacked
 * @author IllIllI
 */
contract AbiEncodeVsEPacked {
  function test(uint256 x) external view returns (bytes32) {
    return keccak256(
      abi.encode(msg.sender, x, block.timestamp));
  }
}

AbiEncodeVsEPacked_p.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.13; // optimize 200

/**
 * @title AbiEncodeVsEPacked
 * @author IllIllI
 */
contract AbiEncodeVsEPacked {
  function test(uint256 x) external view returns (bytes32) {
    return keccak256(
      abi.encodePacked(msg.sender, x, block.timestamp));
  }
}

Diff

diff --git a/AbiEncodeVsEPacked_e.sol b/AbiEncodeVsEPacked_p.sol
index 74cf9a7..4784efc 100644
--- a/AbiEncodeVsEPacked_e.sol
+++ b/AbiEncodeVsEPacked_p.sol
@@ -8,7 +8,7 @@ pragma solidity 0.8.13; // optimize 200
 contract AbiEncodeVsEPacked {
   function test(uint256 x) external view returns (bytes32) {
     return keccak256(
-      abi.encode(msg.sender, x, block.timestamp));
+      abi.encodePacked(msg.sender, x, block.timestamp));
   }
 }
 

Compiled binary

$ solc AbiEncodeVsEPacked_e.sol --bin --optimize --optimize-runs 200 | egrep "^60[0-9]+"
608060405234801561001057600080fd5b5060d18061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c806329e99f0714602d575b600080fd5b607160383660046083565b60408051336020820152908101829052426060820152600090608001604051602081830303815290604052805190602001209050919050565b60405190815260200160405180910390f35b600060208284031215609457600080fd5b503591905056fea2646970667358221220d58dc3743d9dd1d55aebe82d595ae50ff14388a83e158a2f3946abf9c9fec00664736f6c634300080d0033

$ solc AbiEncodeVsEPacked_p.sol --bin --optimize --optimize-runs 200 | egrep "^60[0-9]+"
608060405234801561001057600080fd5b5060e38061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c806329e99f0714602d575b600080fd5b608360383660046095565b6040516bffffffffffffffffffffffff193360601b16602082015260348101829052426054820152600090607401604051602081830303815290604052805190602001209050919050565b60405190815260200160405180910390f35b60006020828403121560a657600080fd5b503591905056fea2646970667358221220567d221768b9532e70210e1c699400fc2386b188b303ef3e50b55899d292ff3f64736f6c634300080d0033

Opcodes

PC.Enc Operation.Enc Gas.Enc PC.Pkd Operation.Pkd Gas.Pkd
87(0x57) JUMPDEST(0x5b)@0x57 1 87(0x57) JUMPDEST(0x5b)@0x57 1
88(0x58) PUSH1(0x60) ["0x40"] 3 88(0x58) PUSH1(0x60) ["0x40"] 3
90(0x5a) DUP1(0x80) 3
91(0x5b) MLOAD(0x51) 3 90(0x5a) MLOAD(0x51) 3
91(0x5b) PUSH12(0x6b) ["0xff","0xff",... 3
104(0x68) NOT(0x19) 3
92(0x5c) CALLER(0x33) 2 105(0x69) CALLER(0x33) 2
93(0x5d) PUSH1(0x60) ["0x20"] 3 106(0x6a) PUSH1(0x60) ["0x60"] 3
108(0x6c) SHL(0x1b) 3
109(0x6d) AND(0x16) 3
110(0x6e) PUSH1(0x60) ["0x20"] 3
95(0x5f) DUP3(0x82) 3 112(0x70) DUP3(0x82) 3
96(0x60) ADD(0x1) 3 113(0x71) ADD(0x1) 3
97(0x61) MSTORE(0x52) 3 114(0x72) MSTORE(0x52) 3
115(0x73) PUSH1(0x60) ["0x34"] 3
98(0x62) SWAP1(0x90) 3
99(0x63) DUP2(0x81) 3 117(0x75) DUP2(0x81) 3
100(0x64) ADD(0x1) 3 118(0x76) ADD(0x1) 3
101(0x65) DUP3(0x82) 3 119(0x77) DUP3(0x82) 3
102(0x66) SWAP1(0x90) 3 120(0x78) SWAP1(0x90) 3
103(0x67) MSTORE(0x52) 3 121(0x79) MSTORE(0x52) 3
104(0x68) TIMESTAMP(0x42) 2 122(0x7a) TIMESTAMP(0x42) 2
105(0x69) PUSH1(0x60) ["0x60"] 3 123(0x7b) PUSH1(0x60) ["0x54"] 3
107(0x6b) DUP3(0x82) 3 125(0x7d) DUP3(0x82) 3
108(0x6c) ADD(0x1) 3 126(0x7e) ADD(0x1) 3
109(0x6d) MSTORE(0x52) 3 127(0x7f) MSTORE(0x52) 3
110(0x6e) PUSH1(0x60) ["0x00"] 3 128(0x80) PUSH1(0x60) ["0x00"] 3
112(0x70) SWAP1(0x90) 3 130(0x82) SWAP1(0x90) 3
113(0x71) PUSH1(0x60) ["0x80"] 3 131(0x83) PUSH1(0x60) ["0x74"] 3
115(0x73) ADD(0x1) 3 133(0x85) ADD(0x1) 3
116(0x74) PUSH1(0x60) ["0x40"] 3 134(0x86) PUSH1(0x60) ["0x40"] 3
118(0x76) MLOAD(0x51) 3 136(0x88) MLOAD(0x51) 3
119(0x77) PUSH1(0x60) ["0x20"] 3 137(0x89) PUSH1(0x60) ["0x20"] 3
121(0x79) DUP2(0x81) 3 139(0x8b) DUP2(0x81) 3
122(0x7a) DUP4(0x83) 3 140(0x8c) DUP4(0x83) 3
123(0x7b) SUB(0x3) 3 141(0x8d) SUB(0x3) 3
124(0x7c) SUB(0x3) 3 142(0x8e) SUB(0x3) 3
125(0x7d) DUP2(0x81) 3 143(0x8f) DUP2(0x81) 3
126(0x7e) MSTORE(0x52) 3 144(0x90) MSTORE(0x52) 3
127(0x7f) SWAP1(0x90) 3 145(0x91) SWAP1(0x90) 3
128(0x80) PUSH1(0x60) ["0x40"] 3 146(0x92) PUSH1(0x60) ["0x40"] 3
130(0x82) MSTORE(0x52) 3 148(0x94) MSTORE(0x52) 3
131(0x83) DUP1(0x80) 3 149(0x95) DUP1(0x80) 3
132(0x84) MLOAD(0x51) 3 150(0x96) MLOAD(0x51) 3
133(0x85) SWAP1(0x90) 3 151(0x97) SWAP1(0x90) 3
134(0x86) PUSH1(0x60) ["0x20"] 3 152(0x98) PUSH1(0x60) ["0x20"] 3
136(0x88) ADD(0x1) 3 154(0x9a) ADD(0x1) 3
137(0x89) SHA3(0x20) 999 155(0x9b) SHA3(0x20) 999
138(0x8a) SWAP1(0x90) 3 156(0x9c) SWAP1(0x90) 3
139(0x8b) POP(0x50) 2 157(0x9d) POP(0x50) 2
140(0x8c) SWAP2(0x91) 3 158(0x9e) SWAP2(0x91) 3
141(0x8d) SWAP1(0x90) 3 159(0x9f) SWAP1(0x90) 3
142(0x8e) POP(0x50) 2 160(0xa0) POP(0x50) 2
143(0x8f) JUMP(0x56) 8 161(0xa1) JUMP(0x56) 8
Total.Enc: 1139 Total.Pkd: 1151

The change costs 12 more gas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment