Learn EVM Opcodes V
SHA3, CALLDATA, CALLDATALOAD, CALLVALUE, CODESIZE and GASLIMIT new opcodes. Today, in the fifth lesson of our series, as you can guess, we will dive into more boring and difficult opcode topics.
SHA3 Opcode
Keccak256 encrypts the desired(we want it) part of the data in memory. For this reason, we must first save the data to the memory. We do this with the MSTORE opcode. I will explain it in detail in the future.
What are offset and size in MSTORE opcode?
offset: byte offset in the memory.(The point on the memory where the data will start to be received)
size: byte size to read in the memory.
We saved 32 bytes of data to the memory.
0xFFFFFFFF00000000000000000000000000000000000000000000000000000000
PUSH1 4 (This is size)
PUSH1 0 (This is offset)
SHA3
Then, starting from the 0th byte, we encrypted 4 bytes of data (FFFFFFFF) with Keccak256.
The result will be seen in the Stack screen.
29045a592007d0c246ef02c2223570da9522d0cf0f73282c79a1bc8f0bb2c238
I also confirmed the result with the keccak256 tool on the internet. Note that “hex” must be selected as input type when using this tool.
CALLDATA opcode
Calldata is actually a field/database like memory. See the following source to read the differences between memory and calldata. Now let’s see the 3 opcodes we will use in the calldata.
CALLDATALOAD opcode
It is determined which byte of the data in the calldata will be transferred to the stack.
Our data is as follows.
0xCCFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFAABBCCDDEE
By writing PUSH1 30, we requested the last two bytes to be pulled.
Result is DDEE
CALLDATASIZE Opcode
Returns the size of the calldata in bytes. It will return 0xFFEECC as 3.
CALLDATACOPY Opcode
destOffset: The data to be retrieved from calldata will be saved in memory starting from which byte
offset: Starting from which byte in calldata data will be received
size: Byte length to copy
0xCCFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFAABBCCDDEE
PUSH1 2 (Byte length to be copied)
PUSH1 30 (Starting from which byte in Calldata data will be received)
PUSH1 0 (The data to be retrieved from Calldata will be saved in memory starting from which byte)
CALLDATACOPY
The result is DDEE .
CALLVALUE Opcode
It is exactly equal to the value of msg.value when we use the call function in solidity. The importance of the opcode for us is that we can send the value we want to the stack with this opcode.
CODESIZE Opcode
This opcode measures the size of our code in bytes.
Why the result is 5?
PUSH2 (1 byte) 2227(2 byte)
POP (1 byte)
CODESIZE (1 byte)
Could you answer this question ?
PUSH3 22111
POP
CODESIZE
The answer is 6.
GASLIMIT Opcode
This opcodes gives block gas limit number. 0xffffffffffff