Research · 2 of 3

The math

From the paper's real geometry to integers that never revert for a numerical reason: one correction, a new search, and what we proved about it.

Contents

1Notation and state

In shortWe follow the paper's notation. Every later formula lives in one plane, (α,∥w∥)(\alpha, \|w\|), and the whole state fits in four storage words. In the text, a symbol shows its definition when you hover or tap it.

SymbolMeaning
xx, nnVirtual reserves, one per asset; nn assets. The contract fixes n=3n = 3.
vv(1,…,1)/n(1, \dots, 1)/\sqrt{n}, the direction of equal reserves.
α\alpha, wwα=x⋅v\alpha = x \cdot v and w=x−αvw = x - \alpha v. With n=3n = 3, α=S/3\alpha = S/\sqrt{3} and ∥w∥2=Q−S2/3\|w\|^2 = Q - S^2/3.
SS, QQ∑xi\sum x_i and ∑xi2\sum x_i^2, exact integers recomputed from the stored reserves.
(r,k)(r, k), ssA tick's radius and plane constant, and its boundary circle radius s=r2−(k−rn)2s = \sqrt{r^2 - (k - r\sqrt{n})^2}.
κ\kappak/rk/r, a tick's normalized plane.
rintr_{int}∑ri\sum r_i over interior ticks: the radius of the consolidated sphere.
kboundk_{bound}, sbounds_{bound}∑ki\sum k_i and ∑si\sum s_i over boundary ticks.
RR∑ri\sum r_i over all ticks, the total radius.
xminx_{min}A tick's lowest reserve on its cap. Their sum ∑xmin\sum x_{min} is the pool's floor.
partitionThe set of boundary ticks: always a prefix of the initialized ticks, selected by SS alone (section 4).

Split the reserves into their component along vv, α=x⋅v\alpha = x \cdot v, and the rest, w=x−αvw = x - \alpha v. The consolidated invariant only sees α\alpha and ∥w∥\|w\|:

(α−kbound−rintn)2+(∥w∥−sbound)2=rint2(\alpha - \htmlClass{tb}{k_{bound}} - \htmlClass{ti}{r_{int}}\sqrt{n})^2 + (\|w\| - \htmlClass{tb}{s_{bound}})^2 = \htmlClass{ti}{r_{int}}^2

highlighted boundary sums · plain interior radius

In the (α,∥w∥)(\alpha, \|w\|) plane, this is a circle of radius rintr_{int} centred at (kbound+rintn, sbound)(k_{bound} + r_{int}\sqrt{n},\ s_{bound}). Turned around the line of vv, it sweeps a torus. A tick's plane becomes a vertical line, at the α\alpha where the interior point reaches it; when the reserves cross it, the tick changes class and the circle changes with it.

Figure 1. Left: the torus is one circle turned around the line of vv, at height sbounds_{bound}. Right: the same circle in the (α,∥w∥)(\alpha, \|w\|) plane, where the surface is its arc nearest the peg (solid). Every later figure is drawn in this plane.

The state the invariant needs fits in four storage words: the three real reserves xi−∑xminx_i - \sum x_{min}, from which the pool recomputes SS and QQ exactly; the interior radius rintr_{int}, the boundary sums kboundk_{bound} and sbounds_{bound}, the total radius and the floor ∑xmin\sum x_{min}; and one integer for the current partition. A one-word bitmap marks initialized ticks (bits 40 to 238). A swap touches two reserves and reads one tick record per partition it walks: the paper's constant cost in nn holds in practice.

Figure 2. The hot state, least significant bit first, to scale. The narrow black fields locate the current partition and cache the next tick up; the last one is the shutdown flag. The bitmap has a slot of its own.

2The boundary radius is a sum, not a root

In shortThe paper consolidates boundary ticks in two places, and the two forms disagree. The sum is the right one: with it, the surface stays smooth and the marginal price continuous when a tick crosses.

Paper · Tick Consolidation, case 2

Two boundary ticks combine into one sphere in the subspace orthogonal to vv, with radius sc=sa+sbs_c = s_a + s_b.

Paper · Global Trade Invariant

All boundary ticks are treated as a single tick (rbound,kbound)(r_{bound}, k_{bound}), with ∥wbound∥2=sbound2=rbound2−(kbound−rboundn)2\|w_{bound}\|^2 = s_{bound}^2 = r_{bound}^2 - (k_{bound} - r_{bound}\sqrt{n})^2.

Where they differ

The two forms agree for a single boundary tick, or for several on the same normalized plane. Otherwise they differ, and always in the same direction: the root form is larger.

What we use

The case-2 argument is the right one: boundary reserves add along a common direction orthogonal to vv. So sbound=∑sis_{bound} = \sum s_i, with rint=∑rir_{int} = \sum r_i over interior ticks and kboundk_{bound} summed over boundary ticks.

Why the root form overstates

As a function of (r,k)(r, k), the boundary radius ss is concave and homogeneous of degree one, hence superadditive. The root of the sums is therefore at least the sum of the roots, with equality only when all boundary ticks share the same k/rk/r.

Proof
  1. r2−(k−rn)2=(r−k+rn)(r+k−rn)r^2 - (k - r\sqrt{n})^2 = (r - k + r\sqrt{n})(r + k - r\sqrt{n}), a product of two linear forms in (r,k)(r, k) that are both positive on every valid tick.
  2. So s(r,k)s(r, k) is their geometric mean: concave and homogeneous of degree one, hence superadditive.
  3. The root of the sums is therefore at least the sum of the roots. Equality holds only when all boundary ticks share the same k/rk/r.
Figure 3. Take n=3n = 3 with boundary ticks at 0.99 and 0.90, computed from the catalog's plane formula. With equal radii, the root form overstates sbounds_{bound} by 30%. With the 0.99 tick ten times larger, it overstates it by 76%.

What the sum buys

With the sum, the invariant is a circle in the (α,∥w∥)(\alpha, \|w\|) plane. When tick ii crosses, rir_i moves between rintr_{int} and the boundary sums, and its sis_i moves with it. The circles of the two partitions are then internally tangent at the crossing point: their radii are RR and R−riR - r_i, and their normals there are parallel. With the root form, the new partition's circle generally misses the crossing point, and the invariant jumps.

Two partition circles tangent at a tick crossing; the root-form circle misses the crossing point. α ‖w‖ plane of tick i root form jump C C′ tangent R R − rᵢ
Figure 4. The surface (solid) follows the circle of radius RR around CC up to the plane of tick ii, then the circle of radius R−riR - r_i around C′C'. With sbound=∑sis_{bound} = \sum s_i, CC, C′C' and the crossing lie on one line (dotted): the circles are tangent there. Switch to the root form: it moves the second center, and the surface jumps at the plane. Schematic, not to scale.

Result 1provedtested

With sbound=∑sis_{bound} = \sum s_i, the surface is not only continuous across a crossing, it is smooth. The marginal price does not jump when a tick flips. In exact arithmetic, a fee-free round trip through a crossing returns exactly to its start.

We checked the consolidated model against an independent convex program, which maximizes output under per-tick sphere and plane constraints. The two agreed to about 10−1310^{-13} on swaps that cross ticks, including swaps that pass the point where the two traded reserves are equal.

3Why the path breaks in integers

In shortThe paper's trade segmentation does not survive integer rounding: near a crossing, several independently rounded conditions can leave no valid integer at all.

The paper computes the trade in the current torus. If the common interior point has crossed the nearest plane, it solves a quadratic for the exact crossover, flips that tick, and repeats with the remaining input. Inside a segment, it solves the quartic by Newton's method.

Figure 5. The paper's procedure on a surface of three tangent circles: Newton on the quartic inside each segment, a quadratic for the exact crossover at each plane, then a flip. Schematic.

Our first engine followed this procedure. In integers, every event must satisfy several independently rounded computations at once: the classification of the reserve sum against the plane, the closed-form crossover, the segment solver's root, and a tolerance on both sides of the flip. Near an event, sometimes no integer satisfies all of them, and a fail-closed engine reverts a swap that should succeed.

Figure 6. Four rounded conditions near one crossing. Each holds on its own interval of inputs; their real intersection exists but holds no integer, so the engine reverts. Schematic.

Two rounds of fixes closed specific configurations, and others stayed open. Bands of input 607 wei wide reverted, and isolated inputs failed while their neighbours passed. The case space (event type × direction × regime × rounding side) is combinatorial. Separately, an unbounded Newton on the quartic could converge to the mirror root on the other side of the pair diagonal.

4The endpoint search

In shortAdd the whole input, cut the line of outcomes at integer thresholds, and solve once in the piece that holds the endpoint. The geometry is the paper's; the algorithm is not.

Add the whole net input first. The input reserve aa and the untouched reserve zz are then fixed, and only the output reserve uu is free. Along that line, S=a+z+uS = a + z + u grows with uu, and every tick plane becomes an integer threshold on SS. In partition mm, the next tick TmT_m is a boundary tick exactly when

S≥σm=⌈3 (kbound,m+rint,m κTm)⌉S \geq \sigma_m = \left\lceil \sqrt{3}\,\big(\htmlClass{tb}{k_{bound,m}} + \htmlClass{ti}{r_{int,m}}\,\kappa_{T_m}\big) \right\rceil

where κ=k/r\kappa = k/r is the tick's normalized plane. These thresholds cut the line into pieces, each with one fixed torus. Neighbouring partitions compute the same integer, so pieces share their seams exactly.

The engine finds the piece that holds the endpoint and solves once there. On a sphere, when every tick is interior, it uses a closed form. On a torus, it runs Newton, then bisection, inside a certified bracket. A state's partition is a function of its reserve sum alone: no event history is needed. Inside a piece, the offset to the surface falls at a guaranteed rate, and at a seam it can rise by at most half a unit; the endpoint is the first point certified on the pool side. How it works draws this line.

  1. Take the feeon the gross input
  2. Locate the piecethresholds on SS
  3. Solve onceclosed form or Newton
  4. Certifyon or inside the surface
  5. Commitreserves, partition, flips
  6. Credit feesfrom the two ends

Exact output

An exact-output swap runs the same search on the inverse line. The output reserve falls by exactly the requested amount and the untouched reserve stays fixed; only the input reserve is free. One solve gives the smallest certified input reserve, within the same bound of its integer optimum, and the pool commits that endpoint. It charges the smallest gross input whose net input reaches that endpoint, rounded up to raw units.

Figure 7. The same search on two lines. Exact input fixes aa and zz and finds the largest certified output; exact output fixes uu and zz and finds the smallest certified input reserve. Schematic.

The state follows from the output, not from the input, so the swap emits its own event. The exact input of the amount charged certifies a different point and, near a seam, can pay a few wei less than the requested output.

5What we proved

For the stored integers and the pinned constant 3\sqrt{3}:

Theorem 1 · Up-setproved

Every piece lies on the near side of its circle's center, where the arc is steeper than any output line can climb. Inside a piece, the pool side of the line is therefore an up-set of integers. Root selection disappears, and the mirror root with it.

Theorem 2 · Seamsproved

Consecutive stored circles are internally tangent to within half a unit.

Theorem 3 · Boundproved

The search terminates and reverts only for capacity. It returns an output at most 50 wei below the exact integer optimum, or 77 wei at the largest supported radius.

0numerical reverts across about 780 swaps on six pool states; every refusal was a capacity limittested
0–3 weifrom the integer optimum, on 864 swaps checked against an independent oracletested
151,117gas for a public swap on our reference fixture, down from 280,760; the contract got smallermeasured

6Rounding, everywhere

In shortTwo rules replace every tolerance of the first engine, and they set the rounding of every operation in the pool.

Rule 1

Certified inside

Every stored state is proven on or inside the surface of the partition its sum selects. The proof uses the exact integers SS and QQ, one square root and outward rounding.

Rule 2

Doubt favours the pool

When a sign is uncertain, the engine takes the pool-favourable integer: a few wei less output. It reverts only for capacity, slippage or zero output.

OperationRoundedToward the pool§
Exact-input swapoutputdown: at most 50 wei below the integer optimum, 77 at the largest radius5
Exact-output swapgross inputup, to raw units4
Depositdepositup: a few wei more when a repair is needed7
Withdrawalwithdrawaldown: a few wei less when a repair is needed7
Fee spliteach sharedown; the interior class takes the remainder8

7Deposits keep the depth

In shortThe paper assumes “each tick has only a single LP” and does not describe deposits or withdrawals once some ticks sit on their boundary. Homogeneity gives the rules; integer states need one more step, or the next trader collects the difference.

The invariant is homogeneous of degree 2 in (x,r)(x, r), which gives two rules:

Interior tick

Adding drdr scales the consolidated interior circle by (rint+dr)/rint(r_{int} + dr)/r_{int} about its offset (kbound,sbound)(k_{bound}, s_{bound}).

Boundary tick

Adding liquidity translates the point and the circle together, by the tick's own dkdk along the axis and dsds orthogonally.

In both cases, no tick changes class. The virtual reserves ∑xmin\sum x_{min} become dynamic and move with every deposit and withdrawal.

The trap

An integer state sits slightly inside the surface, and a plain homothety of the reserve point scales that depth too. After a large mint, the point sits far inside every output line, and the next swap captures the surplus. Our 1,000-run fuzz campaign found exactly this: a minimal round trip returned 390 wei more than its input.

Figure 8. 1: a stored state sits inside the surface by a few units, δ. 2: a plain homothety multiplies that depth with the pool, and the next swap takes the surplus. 3: the fix scales the point's radial projection on the circle, then restores the same δ. Schematic.

The fix applies the homothety to the point's radial projection on the circle and keeps the depth unchanged.

Rounding

Each new state goes through the same certification as a swap endpoint. If rounding leaves it outside, the scarcest reserve is raised along its own line, with the same search a swap uses. The LP pays the difference: the minter deposits a few wei more, the burner receives a few wei less.

97.6%of 1,076 certifications in our test suite needed no repairtested
1–3 weifor the rest, paid by the LPtested

8Fees from two states

In shortThe paper does not discuss fees. Ours are charged on the gross input. The net input enters the reserves, and the fee stays outside the invariant, owed to LPs. There is no protocol fee. Each tick's share is read from the swap's start and end states.

A tick's holding is a function of the state

The paper shows that wintw_{int} is parallel to wboundw_{bound}. So a boundary tick holds k/n+s w^ik/\sqrt{n} + s\,\hat{w}_i of asset ii, where w^=w/∥w∥\hat{w} = w/\|w\|. The interior ticks share the rest,

xi−kbound/n−sbound w^ix_i - \htmlClass{tb}{k_{bound}}/\sqrt{n} - \htmlClass{tb}{s_{bound}}\,\hat{w}_i

in proportion to their radii. The holdings of all ticks sum to xix_i at every state. What each tick absorbs of the input is therefore read from the swap's start and endpoint alone, with no event path and no crossing point.

Figure 9. The input asset's reserve xix_i, split by tick at the start and at the end of a swap. Each holding is a function of the state, so the change in each segment is what that tick absorbed. Schematic.

A holding is continuous where its tick flips: on the tick's plane, the interior ticks' common point is the tick's own point, so its interior and boundary holdings coincide.

Three classes of ticks

The boundary ticks are a prefix of the initialized ticks, so a swap flips one run of consecutive ticks and keeps the class of the others. Each class is paid in proportion to what it absorbs.

ClassWhat each tick absorbsHow it is paid
Boundary at both endsIts own ss times Δw^in\Delta\hat{w}_{in}One boundary stream, per unit of ss, pays them all.
FlippedIts own gainCredited per unit of its own rr, and nothing from either stream for that swap.
Interior at both endsIn proportion to its radiusOne interior stream, per unit of rr, pays them the rest of the fee.

Each tick keeps a fee history that is updated at every flip. Fees survive flips, full burns and the re-creation of a tick, without granting old growth to a new position.

Result 2provedtested

The total fee is exact, and the split is exact up to a few wei: every gain is a lower bound, every share rounds down, and the interior class takes the remainder. LPs are never paid more than the fee taken.

A separate oracle, which evaluates every tick's holding at both ends, gives the same class fees on 13 swaps, 7 of them with flips. Vectors computed to 90 digits are met to the wei.

Attribution pays net absorption. Input that a tick absorbs and gives back within one swap, as on a path that turns at pair parity, earns it nothing, and a class whose holding falls is paid nothing for that swap. Question 1 asks whether that is the right choice.

ABeyond three assets

In shortThe contract fixes n=3n = 3. The proof's numeric premises, however, can be recomputed for any nn with the same 199-price catalog. The endpoint bound grows slowly with nn, and gas limits nn before the proof does.

The up-set property holds for every nn. For a tick at depeg price pp, the premise that the arc is steeper than any output line reduces to p>0p > 0.

The endpoint bound has a closed form. Along an output line, the offset to the surface falls at a rate of at least

μ=pminpmin2+n−1\mu = \frac{p_{min}}{\sqrt{p_{min}^2 + n - 1}}

where pmin=0.10p_{min} = 0.10 is the widest tick's price. The endpoint bound is about 3.5/μ3.5/\mu wei.

nnμ\muBound, R≤1030R \le 10^{30}Bound, R=1036R = 10^{36}
3 · deployed0.070550 wei77 wei
40.057661 wei94 wei
50.049971 wei109 wei
100.0333107 wei163 wei
200.0229157 wei239 wei
Figure 10. The table's bounds against nn (dots: R≤1030R \le 10^{30}; rings: R=1036R = 10^{36}) and the closed form 3.5/μ3.5/\mu (dashed). Highlighted: the deployed pool, n=3n = 3.

What tightens is precision at the tightest tick. There, (s/r)2=(n−1)(1−p)2/(n(p2+n−1))(s/r)^2 = (n-1)(1-p)^2 / \big(n(p^2 + n - 1)\big), which shrinks like 1/n1/n. From n=8n = 8, one constant in the liquidity-repair bound has to be re-derived.

These results cover the premises only. Other steps of the proof use identities specific to n=3n = 3 and would have to be redone. In practice, gas limits nn before the proof does: each tick a swap flips updates two fee words per asset.