1/5
_Follow along with this video:_ --- In this lesson we're going to take the next step in routing our `call data` to the appropriate function! Previously we'd isolated our function selector and we'd said: ``` If f_select == updateHorseNumber -> jump to that data in the contract If f_select == readNumberOfHorses -> jump to that data in the contract ``` Let's look at how we perform these comparisons in Huff. Our function selectors for these functions are as follows: ``` updateHorseNumber = 0xcdfead2e readNumberOfHorses = 0xe026c017 ``` These selectors can be determines by running `cast sig "updateHorseNumber(uint256)"` and `cast sig readNumberOfHorses()` respectfully. Once we have these signatures, there's an op code we can use to check equality, the `EQ` op code! Let's review how it works in [**evm.codes**](https://www.evm.codes/?fork=shanghai) ::image{src='/formal-verification-1/19-opcode-eq/opcode-eq-1.png' style='width: 75%; height: auto;'} As we can see, the `EQ` op code takes the top item on our stack and compares it to the next item in the stack. This op code will return `1` if the values compared are equal and `0` if they are not equal. Let's remind ourselves of our current contract state: ```js #define macro MAIN() = takes(0) returns(0){ 0x00 calldataload 0xe0 shr // [function_selector] } ``` Our stack currently has the isolated `function selector` from our received `calldata` on it, so to perform our comparison we'll need to `PUSH` our function selector to the stack and then execute the `EQ` operation! Our contract (and stack) are going to look this like as a result: ```js #define macro MAIN() = takes(0) returns(0){ 0x00 calldataload 0xe0 shr // [function_selector] 0xcdfead2e // [0xcdfead2e, function_selector] eq // [true_if_func_selector_matches] } ``` Let's next take a look at how to handle when a match is found!
A comprehensive guide to function selectors in Solidity smart contracts - This lesson covers how to compute a function selector in Solidity, how to check for function selector matches in assembly, and how to use the 'EQ' opcode to check for equality between the top two items on the stack.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Assembly
Writing smart contracts using Huff and Yul
Ethereum Virtual Machine OPCodes
Formal verification testing
Smart contract invariant testing
Halmos, Certora, Kontrol
Security researcher
$49,999 - $120,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Guest lecturers:
Josselin Feist
Head of Blockchain at Trail of Bits
Last updated on January 17, 2025
Solidity Developer
Assembly and Formal VerificationDuration: 30min
Duration: 4h 38min
Duration: 3h 57min
Duration: 1h 56min
Course Overview
About the course
Assembly
Writing smart contracts using Huff and Yul
Ethereum Virtual Machine OPCodes
Formal verification testing
Smart contract invariant testing
Halmos, Certora, Kontrol
Security researcher
$49,999 - $120,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Guest lecturers:
Josselin Feist
Head of Blockchain at Trail of Bits
Last updated on January 17, 2025
Testimonials
Read what our students have to say about this course.
Chainlink
Chainlink
Gustavo Gonzalez
Solutions Engineer at OpenZeppelin
Francesco Andreoli
Lead Devrel at Metamask
Albert Hu
DeForm Founding Engineer
Radek
Senior Developer Advocate at Ceramic
Boidushya
WalletConnect
Idris
Developer Relations Engineer at Axelar