Skip to content

Develop a highly-optimized integer square root algorithm for constant-function automated market makers (AMM) #475

Description

@Damola-Sodiq

Description

Implement a native, gas-efficient integer square root (isqrt) function in Rust to handle complex liquidity pool calculations for fractional token pricing.

Architecture & Context

When swapping fractional tokens natively on-chain, calculating the exact xy=k AMM curve requires square roots. Floating point math is non-deterministic and unsupported in WASM. A highly optimized, bitwise-shifting integer square root algorithm is critical for cheap execution.

Technical Requirements

  • Write an isqrt function utilizing Newton's method or fast bitwise shifts in #![no_std] Rust.
  • Eliminate any potential integer overflow panics up to u128 constraints.
  • Write extensive proptest cases validating accuracy against known mathematical bounds.

Acceptance Criteria

  • The algorithm calculates accurate roots without precision loss.
  • CPU instructions consumed are orders of magnitude lower than unoptimized iteration.
  • The execution is deterministic and safe across all architectures.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions