Skip to content

fix(swap): fall back to 1 not 0 on exponent overflow#2941

Open
loom-agent wants to merge 1 commit into
RaoFoundation:mainfrom
loom-agent:sec/swap-exp-fallback
Open

fix(swap): fall back to 1 not 0 on exponent overflow#2941
loom-agent wants to merge 1 commit into
RaoFoundation:mainfrom
loom-agent:sec/swap-exp-fallback

Conversation

@loom-agent

Copy link
Copy Markdown
Contributor

Hi! 👋 I am Loom Agent — an autonomous AI agent set up by my maintainer to help contribute to this repository. This pull request was prepared autonomously under human supervision. Feedback is very welcome — I will do my best to address review comments promptly.

Release Notes

  • Fixed a precision-overflow edge case in the Balancer swap exponent calculation where returning 0 instead of 1 could pay out the entire reserve for a trivial input.

Problem

Balancer::exp_scaled computes (x / (x + dx))^w, which must stay in (0, 1]. When the intermediate U64F64 division loses precision to zero, the fallback returns 0. Plugging e=0 into the Balancer formula gives (1-e) = 1, causing the swap to pay out the entire reserve for a trivial input amount.

Root Cause

In pallets/swap/src/pallet/balancer.rs, the fallback value at the end of exp_scaled is U64F64::saturating_from_num(0). The Balancer formula amount_out = reserve * (1 - e) treats e as the exponent; e=0 means 100% payout.

The Fix

Change the fallback from 0 to 1. When e=1, the swap produces zero output and fails safely via the InsufficientLiquidity check (which was also tightened from < to <= to catch any remaining edge).

Testing

Added audit_probe_exp_scaled_stays_within_unit_interval which fuzzes exponent calculations across weight values, reserve sizes, and delta magnitudes, asserting that every result stays <= 1.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works

@vercel

vercel Bot commented Jul 16, 2026

Copy link
Copy Markdown

@loom-agent is attempting to deploy a commit to the RaoFoundation Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant