This page contains the mathematical derivations for the Newton's method implementations used in Curve's AMM contracts.
StableSwap Derivations
The StableSwap invariant is defined as:
Ann∑xi+D=ADnn+nn∏xiDn+1
Where:
- A is the amplification coefficient
- n is the number of coins
- xi is the amount of the i-th coin
- D is the invariant (i.e. the total virtual balance when all coins are equal)
Newton’s Method for Solving D
We derive Newton's iteration for solving D given {xi}i=1n and A.
Start with:
f(D)=Ann∑xi+D(1−Ann)−nn∏xiDn+1=0
Or equivalently:
f(D)=nn∏xiDn+1−D(1−Ann)−Ann∑xi
Taking the derivative:
f′(D)=nn∏xi(n+1)Dn+Ann−1
Newton Iteration
Using Newton’s formula:
Dnew=D−f′(D)f(D)=f′(D)Df′(D)−f(D)
Substituting f(D) and f′(D):
Dnew=nn∏xi(n+1)Dn+Ann−1D(nn∏xi(n+1)Dn+Ann−1)−(nn∏xiDn+1−D(1−Ann)−Ann∑xi)=nn∏xi(n+1)Dn+Ann−1nn∏xi(n+1)Dn+1+AnnD−D−nn∏xiDn+1+D−AnnD+Ann∑xi=nn∏xi(n+1)Dn+Ann−1nn∏xinDn+1+Ann∑xi
This corresponds to the newton_D function in math.vy.
Newton’s Method for Solving xj
Now derive Newton’s iteration for solving xj, given {xi}i=j, A, and D.
Start with the invariant:
Ann∑xi+D=ADnn+nn∏xiDn+1
Isolate xj:
Annxj+i=j∑xi+D=ADnn+nn(xj∏i=jxi)Dn+1
Multiply both sides by xj:
Annxj2+xji=j∑xi+Dxj=ADnnxj+nn∏i=jxiDn+1
Divide by Ann:
xj2+xji=j∑xi+AnnDxj=Dxj+An2n∏i=jxiDn+1
Bring terms to one side:
xj2+xji=j∑xi+AnnD−D−An2n∏i=jxiDn+1=0
Define:
f(xj)=xj2+xji=j∑xi+AnnD−D−An2n∏i=jxiDn+1
Taking the derivative:
f′(xj)=2xj+i=j∑xi+AnnD−D
Newton Iteration
xjnew=xj−f′(xj)f(xj)=f′(xj)xjf′(xj)−f(xj)
Substitute in the expressions:
xjnew=2xj+∑i=jxi+AnnD−Dxj(2xj+∑i=jxi+AnnD−D)−(xj2+xj(∑i=jxi+AnnD−D)−An2n∏i=jxiDn+1)=2xj+∑i=jxi+AnnD−Dxj2+An2n∏i=jxiDn+1
This corresponds to the newton_x function in math.vy.
Cryptoswap Derivations
Newton Step for newton_D() in Tricrypto and Twocrypto
This derivation explains the mathematical logic behind the newton_D() function used in Curve’s tricrypto and twocrypto pools.
Invariant Function Definitions
We start with the core function:
F=KDn−1S+P−KDn−(nD)n
Where:
- D: the invariant
- S=∑xi
- P=∏xi
- n: number of tokens (typically 2 or 3)
- γ: price scale parameter
- A: amplification coefficient
K=(γ+1−K0)2AK0γ2,K0=DnPnn
g=γ+1−K0,A^=nnA
m1=A^γ2Dg2,m2=g2nK0
neg_fprime=S+Sm2+K0m1n−m2D
Derivative of K
K′=((γ+1−K0)2AK0γ2)′
=((γ+1−K0)2Aγ2+(γ+1−K0)32AK0γ2)⋅(−nDn+1Pnn)
=−n(g2Aγ2+g32AK0γ2)⋅DK0
Derivative of F
F=KDn−1S+P−KDn−(nD)n
Taking the derivative:
F′=(n−1)KDn−2S−nKDn−1−nn−1Dn−1+K′Dn−1(S−D)
Substitute K and K′:
F′=−g2AK0γ2Dn−2S−g32nAK02γ2Dn−2(S−D)−nn−1Dn−1
Derivation of F′F
F′F=−g2AK0γ2Dn−2S+g32nAK02γ2Dn−2(D−S)−(nD)n−1g2AK0γ2Dn−1S+P−g2AK0γ2Dn−(nD)n
Divide numerator and denominator by Dn/nn:
=−g2A^K0γ2D−2S+g32nA^K02γ2D−2(D−S)−Dng2A^K0γ2D−1S+K0−g2A^K0γ2−1
Divide numerator and denominator by g2DA^γ2:
=−DK0S+Dm2K0(D−S)−Dnm1K0S+m1(K0−1)−K0D
Multiply numerator and denominator by D:
=−K0S+m2K0(D−S)−nm1K0SD+m1(K0−1)D−K0D2
Divide numerator and denominator by K0:
=−S+m2(D−S)−K0nm1SD+m1(1−K01)D−D2
Distribute:
=−S−m2S+m2D−K0nm1SD+m1(1−K01)D−D2
Substitute the denominator with −neg_fprime:
=−neg_fprimeSD+m1(1−K01)D−D2
Newton Iteration Step
Dk+1=Dk−F′F=Dk+neg_fprimeSDk+m1(1−K01)Dk−Dk2
=neg_fprimeneg_fprimeDk+SDk+m1(1−K01)Dk−Dk2
Separate into two parts:
Positive Term D+:
D+=neg_fprime(neg_fprime+S)Dk
Negative Term D−:
D−=neg_fprimeDk2−m1(K0K0−1)Dk
Final Newton Step:
Dk+1=D+−D−