Unlocking the Secrets of Ethereum’s Uniswap v3-core: Understanding TickMath
As one of the most popular decentralized exchanges (DEX) on the Ethereum blockchain, Uniswap has become a platform of choice for cryptocurrency traders and investors. The uniswap-v3 core codebase is a crucial component of the Uniswap protocol, enabling seamless interactions between users and facilitating the exchange of cryptocurrencies. One of the key features in the TickMath.sol v3-core codebase is the use of a lookup table to derive the square root of 1.0001 using fixed-point binary arithmetic in Q128 and downcasting to Q96.
The Lookup Table: A Critical Component
In this article, we will delve into the details of how Uniswap v3-core derives the lookup table used in TickMath. We will explore the mathematics behind this process, providing insight into the binary fixed-point arithmetic and Q128 downcasting used by the codebase.
Background: The sqrt Function
The “sqrt” function is a fundamental element of mathematical operations that require precise control over precision levels. In the context of TickMath, this function calculates the square root of 1.0001 using binary fixed-point arithmetic in Q128 and downcasting to Q96.
To understand how Uniswap v3-core accomplishes this feat, let’s break down the process:
Calculating sqrt(1.0001) using binary fixed-point arithmetic
Binary fixed-point arithmetic is a mathematical technique used to represent decimal numbers with a fixed number of bits, allowing for precise calculations and efficient storage of values. In TickMath, the sqrt function uses this approach to calculate the square root of 1.0001.
The process involves dividing 1.0001 by 2^n (where n is an integer), where n represents the level of precision. This division is performed using a loop that iterates through the bits of 1.0001, updating the quotient and remainder at each step. The final result is then reduced to Q96 using bit-shifting and masking operations.
Lookup Table Derivation
The derived lookup table is crucial to enabling efficient lookups for various mathematical functions, including square root. Uniswap v3-core uses a technique called “lookup tables” or “indexed matrices” to store precomputed values of these functions. In this case, the lookup table contains precomputed values for the sqrt function, allowing for fast and accurate calculations.
Implementation Details
The TickMath implementation in the v3-core codebase is a bit dense, but it is essential to understand how the lookup table is used:
pragma solidity ^0.8.0;
contract TickMath {
// Lookup table storage
public map(address => uint64[256]) lookuptables;
// Function to get the square root of 1.0001 at a given tick value
public function getSqrtRatioAtTick(uint24 tick) return view(uint64 memory) {
// Check if the lookup table already contains the result for this tick value
require(lookupTables[msg.sender][tick] != 0);
// If not, calculate and store the result in the lookup table
uint64[] memory lookupTable = lookupTables[msg.sender].get(tick, 256);
lookupTables[msg.sender][tick] = lookupTable[0];
return lookuptable[1]; // Return the second value (index 1) for demonstration purposes
}
}
Conclusion
Uniswap v3-core’s TickMath.sol demonstrates a robust implementation of binary fixed-point arithmetic and Q128 reduction, using a precomputed lookup table to achieve efficient results. By understanding how this process works, developers can build on the same principles to create their own optimized mathematical functions for various applications on the Ethereum blockchain.

