Skip to content

Fix Newton-Raphson derivative sign in swap output solver#2

Open
lolieatapple wants to merge 1 commit intoApostlex0:mainfrom
lolieatapple:fix/newton-raphson-derivative-sign
Open

Fix Newton-Raphson derivative sign in swap output solver#2
lolieatapple wants to merge 1 commit intoApostlex0:mainfrom
lolieatapple:fix/newton-raphson-derivative-sign

Conversation

@lolieatapple
Copy link
Copy Markdown

@lolieatapple lolieatapple commented Mar 17, 2026

Bug: Missing chain-rule negation in newton_raphson_solve

Summary

The newton_raphson_solve function in invariant.move uses the wrong sign for the derivative when solving for swap output amounts. The function compute_invariant_derivative returns ∂I/∂y (or ∂I/∂x), but the Newton-Raphson objective function is f(d) = I(x, y − d, L), so by the chain rule the correct derivative is df/dd = −∂I/∂y. Without negating, each Newton step moves in the opposite direction from the root.

Root Cause

In newton_raphson_solve (line 252–253), the derivative is used directly:

let deriv = compute_invariant_derivative(&curr_x, &curr_y, liquidity_L, solving_for_y);
// ...
let adjust = signed_fixed_point::div(&f_val, &deriv);

compute_invariant_derivative returns:

  • For y-direction: ∂I/∂y = Φ(z) − 1 = P − 1 (always negative, since P < 1)
  • For x-direction: ∂I/∂x = −Φ(z) = −P (always negative)

But the solver is finding the output amount d, not the reserve coordinate directly. Since y_actual = y_after − d:

df/dd = ∂I/∂y · ∂(y_after − d)/∂d = ∂I/∂y · (−1) = −(P − 1) = 1 − P  (positive)

The Newton update d_new = d − f/f' requires the correct derivative sign. Using P − 1 (negative) instead of 1 − P (positive) reverses the step direction.

Numerical Example

Pool at P = 0.5, L = 100,000 → reserves ≈ (39,759, 39,759). Swap input: 397 X tokens.

Step d (output guess) f(d) = I(x+397, y−d, L) Correct step Buggy step
0 393 (initial) +131.5 d → 132 ✓ d → 654 ✗
1 132 / 654 ≈ 0 / +264 converged still diverging

With the correct derivative, Newton converges in ~2 iterations. With the wrong sign, each step moves away from the solution.

Why the code doesn't completely break

The damping logic partially masks the bug:

if (fixed_point::greater_than(&adjust_abs, &half_current)) {
    adjust_abs = fixed_point::div(&adjust_abs, &fixed_point::two());
};

This clamps the step size, preventing the solver from completely exploding. Combined with 20 max iterations, the solver sometimes stumbles close enough to the root through oscillation. However:

  1. Convergence is unreliable — depends on damping behavior rather than mathematical convergence
  2. Large swaps fail — when the swap amount is a significant fraction of reserves, the solver diverges and reverts with E_INSUFFICIENT_OUTPUT
  3. Gas waste — uses up to 20 iterations (each computing CDF + PDF) instead of the expected 2–3

Fix

One-line change: negate the derivative before using it in the Newton step.

let raw_deriv = compute_invariant_derivative(&curr_x, &curr_y, liquidity_L, solving_for_y);
let deriv = signed_fixed_point::negate(&raw_deriv);  // chain rule: df/dd = -dI/dy

The newton_raphson_solve function uses compute_invariant_derivative to
get dI/dy (or dI/dx), but the objective function f(d) = I(x, y - d, L)
requires df/dd = -dI/dy by the chain rule. Without negating, the Newton
step moves in the wrong direction, causing divergence or reliance on
damping for convergence.
@vercel
Copy link
Copy Markdown

vercel Bot commented Mar 17, 2026

@lolieatapple is attempting to deploy a commit to the sachin's projects 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