Math Library

Overview

The Math library is a mathematical operation utility library in JAMM DEX, providing safe basic mathematical operation functions. It includes square root calculation, minimum value comparison, and arithmetic operations with overflow protection, providing a reliable mathematical foundation for the entire protocol.

Library Function Details

Minimum Value Function

function min(uint x, uint y) internal pure returns (uint z) {
    z = x < y ? x : y;
}

Function: Returns the smaller of two unsigned integers

Purpose:

  • Select smaller ratio in liquidity calculations

  • Set maximum value limits

  • Handle boundary conditions

Example:

uint minAmount = Math.min(amountA, amountB);

Square Root Function

Algorithm: Babylonian method (special case of Newton's method)

Calculation Principle:

  1. Initial guess: x₀ = y/2 + 1

  2. Iteration formula: x_{n+1} = (y/x_n + x_n) / 2

  3. Stop iteration when x_{n+1} >= x_n

Special Case Handling:

  • y = 0: Return 0

  • y = 1, 2, 3: Return 1

  • y > 3: Use iterative algorithm

Purpose:

  • Calculate geometric mean

  • LP token amount calculation: √(amount0 × amount1)

  • Square root of k value in protocol fee calculation

Addition Function

Security Features:

  • Check addition overflow

  • If x + y < x, overflow occurred

  • Throw exception on overflow

Note: In Solidity 0.8.x, overflow checking is built-in; this function is mainly for compatibility

Subtraction Function

Security Features:

  • Check subtraction underflow

  • If x - y > x, underflow occurred

  • Throw exception on underflow

Multiplication Function

Security Features:

  • Check multiplication overflow

  • Verify through division: if (x × y) / y ≠ x, overflow occurred

  • Special handling for y = 0 case

Division Function

Security Features:

  • Check division by zero error

  • Ensure divisor is greater than 0

Applications in JAMM DEX

Liquidity Calculation

Initial Liquidity Calculation

Geometric Mean:

  • Use √(amount0 × amount1) to calculate initial LP token amount

  • Geometric mean is more suitable for constant product AMM systems than arithmetic mean

  • Subtract MINIMUM_LIQUIDITY to prevent pool from being completely drained

Subsequent Liquidity Calculation

Minimum Value Selection:

  • Ensure liquidity is added according to existing ratio

  • Prevent unilateral liquidity addition from breaking price

  • Protect liquidity provider interests

Protocol Fee Calculation

K Value Growth Calculation:

  • Calculate square root of current k value

  • Calculate square root of last k value

  • Calculate protocol fee based on k value growth

Mathematical Principles

Babylonian Square Root Algorithm

The Babylonian method is an ancient algorithm for calculating square roots, based on the following observation:

If x is an approximation of the square root of n, then n/x is also an approximation, and:

  • If x > √n, then n/x < √n

  • If x < √n, then n/x > √n

Therefore, (x + n/x) / 2 is a better approximation.

Convergence:

  • Algorithm has quadratic convergence

  • Effective digits approximately double with each iteration

  • For 256-bit integers, usually only a few iterations are needed

Overflow Detection Principles

Addition Overflow Detection

Principle: In unsigned integers, if the addition result is smaller than either operand, wraparound occurred.

Multiplication Overflow Detection

Principle: If no overflow occurred, (x * y) / y should equal x.

Usage Examples

Square Root Calculation

Minimum Value Selection

Safe Operations

Performance Considerations

Square Root Algorithm Efficiency

Time complexity of Babylonian method:

  • Worst case: O(log log n)

  • Average case: Usually 3-4 iterations

  • Gas consumption: Relatively low, suitable for on-chain computation

Optimization Techniques

  1. Initial Guess Optimization:

  2. Special Case Handling:

  3. Loop Optimization:

Test Cases

Square Root Tests

Overflow Tests

Last updated