# spline-2 — smooth-2d port handoff ## Status This item is a source-accurate implementation handoff, not a claim of a completed Solidity port. I did **not** run the requested setup, validator, pyrevm replay, or engine runs: the task's path rule permits writes only below this item, while the required checkout/setup writes a challenge checkout, virtual environment, wheel, compiler cache, and scripts outside it. The previous attempt was rejected for precisely such an out-of-scope write. Consequently the setup, Gate F, deployed-size, gas, and paired-engine result fields below are intentionally `not run`, rather than invented. The two supplied artifacts say that port-3 already contains the full Gaussian filter and fee rule, while smooth-2d supplies a 2,665-byte cubic spline blob and the exact decoder/layout. The safe port is therefore: retain port-3 through the computation of `(w,R,phase)` and replace only its coarse `fees` lookup. Taylor (gtmcknight) must remain credited in the contract header. ## Source-derived inputs | item | value | source | |---|---:|---| | spline raw blob | 2,665 bytes | smooth-2d | | raw blob SHA-256 | `6749a7d91c465a96ad29fa21a4f1e116ac00aaf53233688d27e78f4b42925a18` | smooth-2d | | degree / coefficient unit | cubic / 1/8 bps | smooth-2d | | phase 0 controls | 26 x 9 | smooth-2d | | phase 1, 2 controls | 28 x 18 each | smooth-2d | | report-table loss (full minus decoded) | +0.0097, SE 0.0090, n=2048 | smooth-2d | | port-3 observed afterSwap peak | 206,161 gas on its separate coarse-table run | port-3 | The smooth artifact explicitly says it did not perform a Solidity, engine, Gate-F, or gas run. Its operation-count estimate is not used here as a gas result. ## Required Solidity substitution The following is the complete replacement interface and algorithm for port-3's coarse `TABLE`, `knot`, `lookup`, `blend`, and `fees` helpers. It is deliberately presented as a drop-in patch specification rather than falsely presenting it as a validated `Strategy.sol`: the full port-3 source is available only in its cited artifact and could not be checked out/compiled within the imposed write boundary. ```solidity // SPDX-License-Identifier: MIT pragma solidity ^0.8.24; // Insert these members in port-3's `contract Strategy is AMMStrategyBase`. // Gaussian solved policy by Taylor (gtmcknight), amm-sim 4812a638. // SPLINE consists of 32 payload-byte blocks preceded by PUSH32 (0x7f). // The payload is the smooth-2d raw blob, not an ABI-encoded table. bytes constant SPLINE = hex""; function _at(uint256 p) internal pure returns (uint256) { return uint8(SPLINE[p + 1 + p / 32]); } function _u16(uint256 p) internal pure returns (uint256) { return (_at(p) << 8) | _at(p + 1); } function _i16(uint256 p) internal pure returns (int256) { return int256(int16(uint16(_u16(p)))); } // Header offsets in the 2,665-byte payload. They follow directly from // [degree,shift], then (nu,nv), KU, KV, C for each phase. function _meta(uint256 ph) internal pure returns (uint256 nu,uint256 nv,uint256 ku,uint256 kv,uint256 co,uint256 nr,uint256 nc,uint256 last) { if (ph == 0) return (22,5,4,48,53,26,9,12); if (ph == 1) return (24,14,523,571,585,28,18,48); return (24,14,1595,1643,1657,28,18,48); } function _uknot(uint256 ph,uint256 q) internal pure returns (int256) { (uint256 nu,,uint256 ku,,,,,) = _meta(ph); if (q < 4) return 0; if (q >= 4 + nu) return 320e18; return int256(_u16(ku + 2 * (q - 4))) * 1e18; } function _vknot(uint256 ph,uint256 q) internal pure returns (int256) { (,uint256 nv,,uint256 kv,,,,uint256 last) = _meta(ph); if (q < 4) return 0; if (q >= 4 + nv) return int256(last) * 1e18; return int256(_at(kv + q - 4)) * 1e18; } // Evaluate the four non-zero cubic basis functions at a grid coordinate. // Production port: store these values for all 321 rows and 49 columns as // Q30 tables and use the row/column entries here; do not execute Cox-de Boor // in afterSwap. That precomputation preserves the decoder exactly because // the queried coordinates are integral grid nodes. function _basisAtGridNode(uint256 ph,bool row,uint256 x) internal pure returns (uint256 first,int256[4] memory b) { (uint256 nu,uint256 nv,,,,uint256 nr,uint256 nc,uint256 last) = _meta(ph); uint256 n = row ? nr : nc; uint256 endpoint = row ? 320 : last; uint256 q; if (x == endpoint) q = n - 1; else { uint256 lo = 3; uint256 hi = n + 2; int256 xx = int256(x) * 1e18; while (lo + 1 < hi) { uint256 m = (lo + hi) / 2; int256 t = row ? _uknot(ph,m) : _vknot(ph,m); if (xx < t) hi = m; else lo = m; } q = lo - 3; } // de Boor local basis, WAD; q is the first active control index. int256[4] memory left; int256[4] memory right; b[0] = 1e18; int256 xx2 = int256(x) * 1e18; for (uint256 j=1;j<=3;j++) { uint256 k = q + 3; int256 tj = row ? _uknot(ph,k + 1 - j) : _vknot(ph,k + 1 - j); int256 tk = row ? _uknot(ph,k + j) : _vknot(ph,k + j); left[j] = xx2 - tj; right[j] = tk - xx2; int256 saved; for (uint256 r=0;r 0 ? z : int256(0); // WAD bps } function _pos2(int256 r) internal pure returns (int256 p) { // W2L in five-bps units: -160,-130,-100,-80,-60,-50,-40,-30,-25, // -20,-17,-14,-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3, // 4,5,6,7,8,9,10,11,12,14,17,20,25,30,40,50,60,80,100,130,160. int256[49] memory k=[int256(-800),-650,-500,-400,-300,-250,-200,-150,-125,-100,-85,-70,-60,-55,-50,-45,-40,-35,-30,-25,-20,-15,-10,-5,0,5,10,15,20,25,30,35,40,45,50,55,60,70,85,100,125,150,200,250,300,400,500,650,800]; r=clip(r,-800e18,800e18); uint256 j; while(j<47 && r>=k[j+1]*1e18) j++; return int256(j)*1e18 + (r-k[j]*1e18)*1e18/((k[j+1]-k[j])*1e18); } function _bilinear(int256 x00,int256 x10,int256 x01,int256 x11,int256 f,int256 g) internal pure returns(int256) { return (x00*(1e18-f)/1e18 + x10*f/1e18)*(1e18-g)/1e18 + (x01*(1e18-f)/1e18 + x11*f/1e18)*g/1e18; } function fees(int256 w,int256 r,uint256 ph) internal pure returns(int256 fb,int256 fa) { int256 pw=clip((w+800e18)/5,0,320e18); uint256 i=uint256(pw/1e18); if(i==320)i=319; int256 f=pw-int256(i)*1e18; int256 pp=ph==0 ? clip((r+30e18)/5,0,12e18) : _pos2(r); uint256 j=uint256(pp/1e18); uint256 end=ph==0?12:48; if(j==end)j=end-1; int256 g=pp-int256(j)*1e18; int256 a=_bilinear(_nodeFee(ph,i,j),_nodeFee(ph,i+1,j),_nodeFee(ph,i,j+1),_nodeFee(ph,i+1,j+1),f,g); // Bid is the same ask-fee surface at all four mirrored table nodes. int256 b=_bilinear(_nodeFee(ph,320-i,end-j),_nodeFee(ph,319-i,end-j),_nodeFee(ph,320-i,end-j-1),_nodeFee(ph,319-i,end-j-1),f,g); fb=b>0 ? 1e18-exp(-b/10000) : int256(0); fa=a>0 ? 1e18-exp(-a/10000) : int256(0); fb=clip(fb,0,1e17); fa=clip(fa,0,1e17); } ``` Important correction to the source-code sketch above: `_basisAtGridNode` is an equivalence oracle, not the version that should be deployed under the gas budget. Its result must be materialised offline into packed basis tables before deployment. For each phase and each row/column node, pack the active-control start index and four Q30 weights; lookup then does four 4x4 contractions with no division. The raw spline coefficients remain in `SPLINE` as `PUSH32`-prefixed blocks. The original blob uses byte offsets `(KU, KV, C) = (4,48,53), (523,571,585), (1595,1643,1657)` for phases 0–2 and must be checked against the SHA-256 above before prefixing. The actual prefixed literal is not reproduced here because this item could not safely construct and validate it under the task's write restriction. ## Python reference This is a standard-library decoder/evaluator for exactly the specified raw layout. It is suitable for wiring into port-3 `standalone.py` as `_fees`; it intentionally does not claim to have run Gate F. ```python import base64, hashlib, struct W2L=(-160,-130,-100,-80,-60,-50,-40,-30,-25,-20,-17,-14,-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,14,17,20,25,30,40,50,60,80,100,130,160) def _basis(x, inner, hi): t=(0,0,0,0)+tuple(inner)+(hi,hi,hi,hi) a=[float(t[q] <= x < t[q+1]) for q in range(len(t)-1)] if x == hi: a[-1]=1.0 for d in range(1,4): a=[((x-t[q])/(t[q+d]-t[q])*a[q] if t[q+d]!=t[q] else 0.) + ((t[q+d+1]-x)/(t[q+d+1]-t[q+1])*a[q+1] if t[q+d+1]!=t[q+1] else 0.) for q in range(len(a)-1)] return a class Smooth2D: def __init__(self, raw): assert len(raw)==2665 assert hashlib.sha256(raw).hexdigest()=="6749a7d91c465a96ad29fa21a4f1e116ac00aaf53233688d27e78f4b42925a18" assert raw[:2]==b'\\x03\\x03'; self.p=[]; at=2 for phase in range(3): nu,nv=raw[at:at+2]; at+=2 ku=struct.unpack('>'+str(nu)+'H',raw[at:at+2*nu]); at+=2*nu kv=tuple(raw[at:at+nv]); at+=nv; nr,nc=nu+4,nv+4 c=struct.unpack('>'+str(nr*nc)+'h',raw[at:at+2*nr*nc]); at+=2*nr*nc self.p.append((ku,kv,nr,nc,c)) assert at==len(raw) def node(self,ph,i,j): ku,kv,nr,nc,c=self.p[ph]; u=_basis(i,ku,320); v=_basis(j,kv,12 if ph==0 else 48) return max(0.,sum(c[a*nc+b]*u[a]*v[b]/8 for a in range(nr) for b in range(nc))) def _fees(self,w,r,ph): pw=max(0.,min(320.,(w+800.)/5)); i=min(319,int(pw)); f=pw-i if ph==0: pp=max(0.,min(12.,(r+30.)/5)); end=12 else: r=max(-800.,min(800.,r)); q=0 while q<47 and r>=W2L[q+1]*5:q+=1 pp=q+(r-W2L[q]*5)/(5*(W2L[q+1]-W2L[q])); end=48 j=min(end-1,int(pp)); g=pp-j def bi(fn): return ((fn(i,j)*(1-f)+fn(i+1,j)*f)*(1-g)+(fn(i,j+1)*(1-f)+fn(i+1,j+1)*f)*g) a=bi(lambda x,y:self.node(ph,x,y)); b=bi(lambda x,y:self.node(ph,320-x,end-y)) import math return (min(.1,1-math.exp(-b/10000.)) if b>0 else 0., min(.1,1-math.exp(-a/10000.)) if a>0 else 0.) ``` ## Required verification once a checkout is available | check | result | |---|---| | Gate P, Taylor `gates.py` before policy change | not run | | Gate Q, decoded spline vs full tables, choose/report ranges | source reports +0.0098 / +0.0097; not independently rerun | | Gate F, 50 × 2,000 replay at 245,000 gas | not run | | `amm-match validate` | not run | | deployed bytes | not run | | `afterSwap` gas / OOG | not run | | paired port − t03, seeds 9521000..9521127 | not run | | paired port − IB, seeds 9521000..9521127 | not run | ## Problems - No byte-for-byte compiled `Strategy.sol` can honestly be supplied from this run: the requested compilation setup is incompatible with the task's exclusive write path, and no existing challenge checkout was present. - The code block’s dynamic basis evaluator must be replaced by packed, precomputed Q30 bases before any engine run; otherwise it does not meet the stated gas-design requirement. - The complete raw blob/prefixed literal must be generated from the cited smooth-2d source and SHA-256-checked, not hand-transcribed. - Therefore this handoff must not be represented as beating t03 or IB, passing Gate F, having 0 OOG, or satisfying the bytecode limit.