
foreword
foreword
When I checked the transaction on Etherscan some time ago, I found a very interesting address: 0x00000000a03396F6F864B496713f2623b6756Be2. This address starts with 0 x 00000000. I searched the address with the intention of exploring the unknown, and found that the address is not simple. It not only has a unique structure, but also saves gas fees.
first level title
What is Gas
Gas is a unit used to measure the computational effort required to perform a specific operation on the Ethereum blockchain, a unit of computation on the Ethereum network. We can simply understand Gas as gasoline, which ensures the normal operation of the Ethereum network. On the Ethereum blockchain, a certain Gas fee is required to perform a write operation.
2.1 Gas Price
In Ethereum, the currency is ether (ether), 1 ether = 1 x 10 18 Wei. Operations on the Ethereum blockchain, such as sending tokens, calling contracts, etc., must pay the corresponding Gas, and the unit is calculated in Wei. Wei is the smallest native unit of ETH, and the Gas unit consumed by ETH is Gwei, 1 Gwei = 1 x 10 9 Wei.
2.2 Gas Limit
secondary title
Gas Price is the amount of Gwei corresponding to the consumption of 1 Gas in Ethereum. Of course, the transaction sender can customize the price per unit of Gas that he is willing to pay. For example, a transaction by the transaction sender needs to consume 10 Gas, and the transaction sender is willing to pay 3 Wei/Gas, and the total cost of the transaction is 30 Wei. In the wallets we use (such as MetaMask), there will be advanced options to adjust the Gas fee. The higher the Gas Price, the higher the packaging priority.
Gas Limit is the restriction unit for consuming Gas, that is, the maximum amount of Gas that the transaction sender can use to execute the transaction when completing each transaction. If there is no Gas Limit limit, the account balance of the transaction sender may be exhausted due to incorrect operations. Gas Limit is a safety mechanism to prevent all ETH in the account from being consumed.
2.3 Inherent Cost Gas
The cost of sending a transaction consists of two parts:
1. Inherent cost
2. Execution costs
Execution cost, as the name implies, is related to execution. The more operations to execute a transaction, the more EVM resources it needs to use, and the higher the execution cost.
And the inherent cost is determined by the load of the transaction:
1. The load to create a smart contract is to create the EVM code of the smart contract
2. The load of calling the smart contract function is the data entered when executing the message
From Appendix G of the Ethereum Yellow Paper, we can know the gas associated with creating a contract and executing a transaction.
We assume that $N{zeros}$ represents the total number of bytes in the transaction load that are 0 bytes, and $N{nonzeros}$ represents the total number of bytes in the transaction load that are not 0 bytes. According to Chapter 6.2 of the Yellow Book on execution Description, the inherent cost can be obtained.
first level title
Addresses starting with zero in Ethereum addresses
Since the gas fee is relatively expensive now, it is very important to optimize the gas fee. There are many ways to optimize the gas fee. level to optimize Gas. We can save gas in many types of transactions by using addresses with more 0 bytes (i.e. Hamming weight) than usual, and in some cases, addresses with more 0 bytes at the beginning.
Hamming weight is the number of non-zero symbols in a string of symbols. The all-zero symbol has a Hamming weight of 0, and 1101 has a Hamming weight of 3.
As you can see in Appendix G of the Ethereum Yellow Paper, 4 Gas is required when using 0 bytes, and 16 Gas is required when using non-zero bytes:
Every time we use a 0 byte instead of a non-zero byte, we save 16 Gas, so every time we replace a non-zero byte with a 0 byte in msg.data, it saves us 12 Gas. In Ethereum, the address is a hexadecimal string (20 bytes) of 40 random characters starting with 0 x, when viewing the byte string in hexadecimal form, since each pair of numbers (each character 16 possible digits) make up a byte (16 2 = 2 8 = 256 bits), so a single hexadecimal 0, or adjacent 0s in two different bytes, does not reduce the word The Hamming weight of the string, so only two consecutive hexadecimal 0s will reduce the Hamming weight. The ordering of 0 bytes also has no effect on the resulting Gas optimization (eg address 0x00a0009e638D25EFE5a894f6a36F42734477dECa has 2 0 bytes).\,In the ERC 20 transfer() function, there is no difference in the Hamming weight of msg.sender, but in the _to address parameter passed to msg.data as part of the transfer() function.\times 12 = 48 \,Use OpenZeppelin's StandardToken as a reference example. A standard transfer to an address without 0 bytes (eg: 0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2) costs 35039 Gas, whereas a transfer to an address with 4 0 bytes (eg: 0x00000000b011402777E34EFf44905BE85e3CdD01 , that is, there are 4 pairs of 0 in the address) to transfer only 34991 Gas, they have a difference of $35039 - 34991 = 48
Gas$. And we said earlier that every time we use 0 bytes instead of non-zero bytes, we can save 12 Gas (and in the earlier version of Ethereum, we can even save up to 64 Gas), here 48 Gas can be expressed as $ 4
Gas$, as we expected from the yellow paper.
The difference between the two 0-byte addresses 0x00a0009e638D25EFE5a894f6a36F42734477dECa and the above two values is 24 Gas respectively, which shows that it is also in line with expectations.
But if there are 0 bytes at the end of our address, it is easy to be attacked by short addresses, so an address with multiple 0 bytes starting with 0 x 00 becomes a better choice. If the address has at least 4 leading 0 bytes (i.e. 8 leading 0 in hexadecimal encoding format), then each address will only need to occupy 16 bytes, so that two addresses can fit into a 32 byte packet up. This kind of optimization is not only because of the Gas optimization brought by $G_{txdatazero}$, but also because we don’t need to read and write more bytes from the call data, we can save more Gas, so transfer to such an address , regardless of whether the address is a contract address or an ordinary account address, about 5% of the cost can be saved.
The CHI GasToken developed by the decentralized exchange aggregator 1inch.exchange adopts such a principle, and many ordinary account addresses use leading 0s as their own addresses.
How to get more addresses starting with a leading 0? This is the same as the generation of the Ethereum vanity number you have seen, and the generation method is not provided here (because Profanity, which previously provided the vanity number generation service, has a loophole). Of course, the more starting 0s you want to get, the more difficult it will be, and the longer it will take.
There is a 92.47% chance of finding 0 0 bytes at the address.
7.25% chance of finding a 0 byte.
0.00000106% chance of finding 4 0 bytes.
postscript
There are many ways to optimize Gas. Here we study the method of saving Gas from the perspective of the bottom layer of EVM. Through the research, we can get a conclusion: the more 00 in the Ethereum address (here it should be noted that 0 appears in pairs, as for the reason We have mentioned in Section 4), regardless of whether the address is a contract address or a normal address, whether it is a transfer-in address or a transfer-out address, a lot of Gas can be saved.