5/5
--- ### Modular Verification of the Square Root Function #### Introduction to Modular Verification To tackle the complexity of verifying a square root function, we utilize a strategy known as modular verification. This method involves dividing a complex problem into simpler, manageable subproblems—a process called modularization. #### Analyzing the Function Our focus is on two implementations of the square root function: 1. **Uniswap Square Root** 2. **Soulmate Square Root** The soulmate square root function, which we haven't focused on extensively, reveals an interesting aspect when compared side-by-side with the Uniswap version. Upon cleaning up the code for better readability, it's evident that while the first halves of these functions differ, their second halves are identical—both perform similar operations like right shifts, additions, and divisions. #### Hypothesis for Simplification The identical lower halves of these functions suggest that if the soulmate square root is accurate, then the top halves should, theoretically, produce the same results. This assumption allows us to hypothesize that difficulties in full verification might stem from the complex operations in the lower half, potentially leading to path explosion issues. Simplifying the verification to just the upper halves might offer a more manageable solution. #### Implementation of Modular Verification We split the MathMaster's square root function into two halves: - **Top half** - **Bottom half** (shared with the soulmate square root) Next, we implement these halves in a compact code base: - **`function soulmateTopHalf(uint256 x) external pure returns (uint256)`** - **`function mathMastersTopHalf(uint256 x) external pure returns (uint256)`** #### Transition to Fuzz Testing Considering the modular approach, an idea arises to utilize fuzz testing for these functions instead of continuing with formal verification. We set up a fuzz test to compare the outputs of the top halves from both implementations. Despite initial tests passing, the consideration of more extensive fuzzing remains to potentially uncover discrepancies. #### Final Verification Steps Incorporating the functions into our square root specification, we attempt another round of verification: - **`function soulmateTopHalf(uint256 x) external returns (uint256)`** - **`function mathMastersTopHalf(uint256 x) external returns (uint256)`** We assert that the outputs of both functions should match, focusing solely on the top halves to avoid the complexity of the bottom halves. #### Conclusion of the Modular Verification Our efforts reveal that the top halves of the functions do not consistently yield the same results, indicating potential issues within the Mathmaster's implementation. Although this modular verification does not fully confirm the correctness of the entire square root function, it effectively identifies a significant discrepancy that warrants further investigation and correction. This modular approach not only simplifies the verification process but also highlights the importance of dissecting complex problems to focus on critical components, enhancing the overall efficiency of the verification efforts.
A practical guide to modular verification for Solidity. The lesson discusses how to break a complex problem into simpler subproblems and how to verify each part separately. The video then walks through a real-world example of finding a bug in a square root function by utilizing modular verification and a fuzz test.
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 May 28, 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 May 28, 2025