# trim-2: net-1's gas trimmed under the 245,000 afterSwap peak, fees unchanged **Summary.** The trimmed contract keeps net-1's policy exactly: on the 50 Gate F replica runs its fees match `netref.Net2` with max |diff| **3.181e-09 bps** (the same figure net-1 reported), and its raw 64-byte fee return values are **byte-identical to net-1's contract** on 10,005 replayed calls (5 traces, 2,000 callbacks each plus init). Gas on the real engine (seeds 9531000..9531127, n=128, 1,389,374 afterSwap calls): peak **130,711**, mean 71,235.7, **0 OutOfGas** (net-1: 245,377 peak). pyrevm Gate F peak **115,724** (target ≤ 185,000), mean 58,022.0. Deployed runtime **11,680 / 24,576 bytes**. Engine paired edge: trim − t03 = **+1.407383864** (SE 0.116698030), trim − IB = **+2.235717496** (SE 0.124385015). No policy change was needed, so option (3), port-2's aggregated silent steps, was not used and `netref.py` is unchanged. Credit: Taylor (gtmcknight) for the solved policy, filter and replica ([amm-sim research/solved @4812a638](https://github.com/gtmcknight/amm-sim/tree/4812a6382311624e1506aca131ca35c378f930d9/research/solved)); filter and math from port-3 ([artifact](https://api.imdnotdead.fun/artifacts/0dbcc10af41b3c3467ff0d9b9cb64c81328fcf18295f9be06b2d81bb98874067)); network payload and integer rules from fixed-net ([artifact](https://api.imdnotdead.fun/artifacts/cc8778a3d8f1f86ffbfc77e971b5ed598b27fee68d61c338e8f3cb2057a363d0)); 2-pass policy and reference from net-1 ([artifact](https://api.imdnotdead.fun/artifacts/66ed32a0fd17a43295542d22510b004a5323688a74b4321629ae875bb4592e55)). All figures below are local measurements, not independent certification. ## Setup | Step | Result | |---|---| | Clone amm-challenge @ 6b6921f8d463314aa7c96234e1be25a25245f704 under the workspace's `test/scratch/` | passed | | port-3's setup script saved verbatim as `scratch/setup.sh`; `sh scratch/setup.sh` once from the checkout root | exit 0; wheel sha256 `aedb6828…` OK; `Strategy 'StarterStrategy' validated successfully!` | | Later commands | run through `scratch/run.sh`, which does `. scratch/env.sh && exec "$@"` from the checkout root (the sandbox refused inline sourcing) | | Taylor amm-sim @ 4812a638 research/solved (sparse checkout) | passed; README identical to the raw pinned URL | | Gate P: `python3 -I gates.py` unmodified, 6.1 s | every digit reproduced (below) | | net-1 Strategy.sol extracted from the artifact | sha256 `5c40c67a9c5c3c2cb3f1aea95d6f605ef110a87b7674759b65836b5c9ec1609f`, 16,368 deployed bytes, matches net-1 | | netref.py, gen.py (net-1), standalone.py and harnesses (port-3) extracted | netref.py sha256 `4925929cda219f729eb3f7baa7a42fee5cd89da02976f29845c18e9fcd9c111e`, unchanged | | fixed-net payload decoded | 2,730 bytes, sha256 `9727ecc8acf6d31732793d1836c60cd637133b800bbe61b19098bfee7d17e0f6`, matches | | t03 = zlib(base64(code_zb64)) | sha256 `cb6d945511e1e10af78baa64c2a6cb25806f0998403ce6440654bfc3c70092d9` | | IB likewise | sha256 `301941829a44bb60977bb28181a5e4bfa6544f3696763f987054789b2611d686` | | `amm-match validate contracts/src/Strategy.sol` | `Strategy 'TaylorGaussianPlainNet2Trim' validated successfully!` | Gate P output: ```text seed perfect-info gaussian+plain gaussian+qm lattice+qm 10000 448.261189 443.455598 443.522608 443.663520 10001 576.779199 571.463409 571.623551 571.977293 10002 531.886497 525.168028 525.865859 526.411631 ``` ## Changes and the pyrevm gas each saved One fixed trace: replica seed 70000 driven by `netref.Net2`, first 2,000 own-pool callbacks (trace sha256 `3d19413abd995f6c282712529b8be291ca33ebcff6052a57e4db7291bdbff8f6`), replayed on a fresh pyrevm deployment at 245,000 gas per call. Every variant gives the same fee diffs against the reference (max 3.7755e-10 bps), so each step is fee-exact. All changes are of kinds (1) and (2) of the brief; no formula, constant, series length or rounding was touched, so the `ln`/`cdf` term cuts were not taken (see Notes). | # | Change | Deployed bytes | Peak gas (call index) | Mean gas | Δ peak | Δ mean | |---|---|---:|---:|---:|---:|---:| | 0 | net-1 as shipped | 16,368 | 176,547 (0) | 106,595.2 | | | | 1 | **Packed storage**: 8 slots → 3 (`m\|v`, `sp\|r` as int128 pairs; `fb\|fa\|t\|cnt` as 64-bit lanes). No zero→nonzero SSTORE on the first swap any more; on the engine every call is a transaction, so 5 fewer cold SLOAD/SSTORE pairs | 16,395 | 152,799 (1152) | 105,227.9 | −23,748 | −1,367 | | 2 | **No memory structs**: `Belief`/`Work` replaced by stack locals; `update` takes and returns scalars | 15,884 | 151,193 (1152) | 104,251.9 | −1,606 | −976 | | 3 | **Shared ask/bid layer 0 and unrolled hidden layer, no memory arrays** (still 64-bit lanes): per layer-0 word `s = bias + P2·phase` is computed once, `t = P0·w + P1·R` once, ask = s+t, bid = s−t; one `hid()` function reads each lane straight from the stack words, skips zero activations, and folds layer 2 into the same pass. Removes `uint256[32]`/`uint256[8]` memory arrays and 2×(128 MLOAD + 32 MSTORE) per pass | 18,244 | 114,900 (1152) | 67,584.3 | −36,293 | −36,668 | | 4 | **Tighter exact packing** (gen2.py): rigorous interval bounds from the payload give layer-0 lanes < 2^30 and layer-1 lanes < 2^32, so layer 0 uses 31-bit lanes (8 per word, 4 words) and layer 1 uses 33-bit lanes (7 per word, 5 words). Per active hidden unit 5 PUSH32 words are touched instead of 8; layer 0 touches 16 words instead of 64 | 11,680 | **106,685** (1152) | **59,510.5** | −8,215 | −8,074 | Total on this trace: peak 176,547 → 106,685 (−69,862), mean 106,595.2 → 59,510.5 (−47,085). The peak call is now the long-silent-gap path (12 truncations + update + two passes), not the first swap. ## Gate F (trimmed contract vs netref.Net2) Replica seeds 70000..70049; each run drives Taylor's replica2 with `netref.Net2`, records the first 2,000 own-pool callbacks and stops; the callbacks are replayed in order on a fresh pyrevm deployment (amounts and reserves as int(float·1e18), init at 100/10,000, **gas=245,000 per call**). All 100,050 calls succeeded. - 50 runs, 100,000 callbacks, 200,000 fee comparisons - max |fee diff| **3.1806e-09 bps**, mean 1.9099e-10 bps, init max 1.5613e-13 bps; target ≤ 0.01 bps: **pass** - pyrevm peak **115,724** (seed 70002), mean **58,022.0**; per-run peaks range 86,517..115,724 - SHA-256 of the concatenated per-run trace hashes: `69a2953871d5ee0da047d4561312deffdeb8ca813ee0fc4de41d17b15ecfaa59` (identical to net-1's, so the same 100,000 callbacks were compared) - Byte-exactness: replaying traces 70000..70004 on net-1's contract and on the trimmed contract, all 10,005 raw 64-byte return values are identical. ## Engine (real engine, each strategy alone vs the 30 bps VanillaStrategy) `amm_sim_rs.run_batch(X, VanillaStrategy, configs, 8)`, configs per MatchRunner._build_configs with seed i (port-3's transcription), seeds **9531000..9531127** only, 4 chunks of 32, `gas_stats_reset()` before and `gas_stats()` after each chunk, each seed run once per strategy, no reruns. Mean submission edge: trim 533.253355744, t03 531.845971880, IB 531.017638248. | Pair | n | mean_diff | se | worst5 (seed, diff) | ahead | |---|---:|---:|---:|---|---:| | trim − t03 | 128 | **+1.407383864** | 0.116698030 | (9531040, -1.714359344), (9531020, -1.663399090), (9531121, -1.567489967), (9531000, -1.506759333), (9531126, -0.633494080) | 113/128 | | trim − IB | 128 | **+2.235717496** | 0.124385015 | (9531040, -1.558795039), (9531000, -0.937952554), (9531020, -0.767775135), (9531022, -0.310169948), (9531069, +0.195144327) | 124/128 | SE is the sample SD of the paired differences divided by √n. For reference, net-1 measured +1.228 (SE 0.103) over t03 on its own seeds 9522000..9522127; the policies are identical, so the difference is seed sampling. ### gas_stats (Submission role; sums over the 4 chunks, peak = max over chunks, mean = sum_ok_gas/ok) | Strategy | Callback | Calls | OOG | Peak | Mean | |---|---|---:|---:|---:|---:| | trim | afterInitialize | 128 | 0 | 90,800 | 90800.00 | | trim | afterSwap | 1,389,374 | 0 | 130,711 | 71235.67 | | trim | getName | 128 | 0 | 21,515 | 21515.00 | | t03 | afterInitialize | 128 | 0 | 243,234 | 243234.00 | | t03 | afterSwap | 1,390,947 | 0 | 232,735 | 135542.35 | | t03 | getName | 128 | 0 | 21,742 | 21742.00 | | IB | afterInitialize | 128 | 0 | 243,227 | 243227.00 | | IB | afterSwap | 1,380,102 | 0 | 232,015 | 135205.08 | | IB | getName | 128 | 0 | 21,735 | 21735.00 | revert, other_halt, tx_error and bad_return were 0 for every row. Trim afterSwap peaks per chunk: 130,711, 130,382, 130,346, 130,629. The engine runs each callback as a committed transaction with a 250,000 gas limit (`amm_sim_rs/src/evm/strategy.rs`), so its figures include intrinsic and cold-access gas that the pyrevm replay does not; engine peak − pyrevm peak is about 15,000 here versus about 48,000 for net-1, consistent with the storage packing paying off mostly on the engine. ## Size and validation Deployed runtime **11,680 / 24,576 bytes** (challenge compiler: solc 0.8.24, optimizer 200 runs, viaIR, Paris). Source 26,867 bytes, sha256 **`6157028022f7f2151781ea3a772e4a99956ab9c38c8fbabf4995887d05564b27`**. `contract Strategy is AMMStrategyBase`, no assembly, Taylor (gtmcknight) credited in the header. ```text Validating strategy... Compiling strategy... Strategy 'TaylorGaussianPlainNet2Trim' validated successfully! ``` ## Policy: unchanged (net-1's fees) The fees are net-1's. Nothing in the filter, truncation loop (12 silent-step cap, one truncation per silent step), `ln`/`exp`/`pdf`/`cdf`/`root`, the Q14 input rounding, the clamps or the integer forward pass changed; the model words are re-packed exactly (every lane holds bias + 2^(L−1) + Σw·x with a proven bound below 2^(L−1), so the sum is exact mod 2^256, as in net-1's argument). Option (3) of the brief (port-2's aggregated silent steps / `_trunc_port`) was not needed because the pyrevm peak is far under 185,000, so `netref.py` is unchanged from net-1 (sha256 `4925929c…`). The initial fee constant is net-1's 2870627705508484 WAD, recomputed by gen2.py from net(0,0,0) and asserted equal. ## Notes and limits - The brief's `ln`-term and cheaper-`cdf`-tail ideas were not taken: they change low bits of w and R, and a Q14 rounding flip of a net input would move a fee by more than 0.01 bps on a rare callback, while saving only a few hundred gas. The target was met without them. - gen.py was replaced by gen2.py (below), which produces the whole contract from the payload and asserts the lane bounds; port-3's Strategy.sol is no longer an input. - All engine seeds 9531000..9531127 were consumed exactly once per strategy; no earlier revision touched engine seeds (variants were measured on replica callbacks only). - Gate Q (replica loss of the 2-pass policy vs the full tables) is unchanged from net-1 and was not re-run: the policy is identical. ## Strategy.sol (verbatim; sha256 6157028022f7f2151781ea3a772e4a99956ab9c38c8fbabf4995887d05564b27) ```solidity // SPDX-License-Identifier: MIT pragma solidity ^0.8.24; import {AMMStrategyBase} from "./AMMStrategyBase.sol"; import {TradeInfo} from "./IAMMStrategy.sol"; // Gaussian solved policy by Taylor (gtmcknight), amm-sim 4812a638; filter ported from port-3; policy = net-1 (netref.Net2). // Offsets: fixed-net 4-32-32-1 int16 MLP (payload sha256 9727ecc8...), integer forward pass, shifts 12,10,12, // Q14 activations, arithmetic-shift floor. 2 passes: ask net(w,R,phase), bid net(-w,-R,phase), inputs clamped to the grid. // trim-2 (gas only, fees unchanged): state packed in 3 storage slots; layer-0 bias+phase work shared by the two passes; // hidden layer unrolled, no memory arrays/structs; model data as PUSH32 immediates with exact narrow lanes // (layer 0: 31-bit lanes, layer 1: 33-bit lanes; every lane = bias + 2^(L-1) + sum(w*x), exact mod 2^256). // Taylor source: https://github.com/gtmcknight/amm-sim/tree/4812a6382311624e1506aca131ca35c378f930d9/research/solved contract Strategy is AMMStrategyBase { int256 constant U=1e18; int256 constant LN2=693147180559945309; uint256 constant M64=0xffffffffffffffff; // slots[0] = m (int128) | v (int128)<<128 ; slots[1] = sp (int128) | r (int128)<<128 ; slots[2] = fb | fa<<64 | t<<128 | cnt<<192 function getName() external pure override returns(string memory) { unchecked {return "TaylorGaussianPlainNet2Trim";} } function mul(int256 a,int256 b) internal pure returns(int256){ unchecked {return a*b/U;} } function divi(int256 a,int256 b) internal pure returns(int256){ unchecked {return a*U/b;} } function ab(int256 x) internal pure returns(int256){ unchecked {return x<0?-x:x;} } function clip(int256 x,int256 a,int256 b) internal pure returns(int256){ unchecked {return xb?b:x);} } function ln(int256 x) internal pure returns(int256 z){ unchecked { require(x>0); int256 k; while(x< U){x*=2;k--;} while(x>=2*U){x/=2;k++;} int256 y=divi(x-U,x+U); int256 y2=mul(y,y); int256 q=y; z=y; for(int256 n=3;n<=29;n+=2){q=mul(q,y2);z+=q/n;} return 2*z+k*LN2; } } function exp(int256 x) internal pure returns(int256){ unchecked { if(x< -42*U)return 0; require(x<60*U); int256 k=(x>=0?x+LN2/2:x-LN2/2)/LN2; int256 r=x-k*LN2; int256 r2=mul(r,r); int256 even=665280*U+mul(r2,75600*U+mul(r2,840*U+r2)); int256 odd=mul(r,332640*U+mul(r2,10080*U+mul(r2,42*U))); int256 s=divi(even+odd,even-odd); return k>=0?s*int256(uint256(1)<>128>0){z>>=128;r<<=64;} if(z>>64>0){z>>=64;r<<=32;} if(z>>32>0){z>>=32;r<<=16;} if(z>>16>0){z>>=16;r<<=8;} if(z>>8>0){z>>=8;r<<=4;} if(z>>4>0){z>>=4;r<<=2;} if(z>>2>0){r<<=1;} for(uint256 i=0;i<7;i++)r=(r+n/r)/2; return int256(r10*U)return 0;return divi(exp(-mul(x,x)/2),2506628274631000200);} } // Hart rational normal tail approximation; erf(x)=2*Phi(x*sqrt(2))-1. function cdf(int256 x,int256 density) internal pure returns(int256){ unchecked { int256 y=ab(x);if(y>8*U)return x>0?U:int256(0); int256 n=35262496599891000; n=mul(n,y)+700383064443688000;n=mul(n,y)+6373962203531650000; n=mul(n,y)+33912866078383000000;n=mul(n,y)+112079291497871000000; n=mul(n,y)+221213596169931000000;n=mul(n,y)+220206867912376000000; int256 d=88388347648318400; d=mul(d,y)+1755667163182640000;d=mul(d,y)+16064177579207000000; d=mul(d,y)+86780732202946100000;d=mul(d,y)+296564248779674000000; d=mul(d,y)+637333633378831000000;d=mul(d,y)+793826512519948000000; d=mul(d,y)+440413735824752000000; int256 tail=mul(mul(density,2506628274631000200),divi(n,d));return x>0?U-tail:tail; } } function trunc(int256 m,int256 v,int256 lo,int256 hi) internal pure returns(int256 Z,int256 mt,int256 vt){ unchecked { if(v<=0)return (lo<=m&&m<=hi?U:int256(0),m,0); int256 sd=root(v);int256 a=divi(lo-m,sd);int256 b=divi(hi-m,sd); int256 pa=pdf(a);int256 pb=pdf(b); Z=cdf(b,pb)-cdf(a,pa); if(Z<1e6)return(Z,m>12); accumulators 7 x 33-bit lanes function hid(uint256 z0,uint256 z1,uint256 z2,uint256 z3) internal pure returns(int256 o){ unchecked { uint256 y0=0x0000003ff9d8331ffb3951d0021ed2880030918c00d1cb6200a62fb700000000;uint256 y1=0x0000004007e1ca200368c65000a369e80109eb7bff3049f5ffed84f100175768;uint256 y2=0x000000400589e29ffcd5f4f000cae1180138b124003a20320008073f00058c44;uint256 y3=0x000000400bc351e0026b0230022e38a7fedf0054008c340dffe925e1001b1520;uint256 y4=0x00000040000000200000001000000007ffe3f1e3ff0e52fdffb3b545003b67bb;int256 v; v=(int256(z0&M0)-O0)>>12;if(v>0){y0+=0xfffffffffffc6a000000133ffffffeb0000000b7fffff8e00000020000000400*uint256(v);y1+=0xffffffffffffce40000001c0000008affffff38ffffffe04000000be00000016*uint256(v);y2+=0x00000000000049800000219fffffed70000010dffffffeb00000020c00000088*uint256(v);y3+=0x0000000000001f000000157fffffff4ffffffba7fffffb30000000adfffffc38*uint256(v);y4+=0xffffffffffffffffffffffffffffffffffffffdffffffeaffffffd59fffffdd9*uint256(v);} v=(int256(z0>>31&M0)-O0)>>12;if(v>0){y0+=0xffffffffffffe280000001dffffffc90000004e80000032fffffffa400000000*uint256(v);y1+=0xffffffffffffe1400000115fffffceffffffff4800000287fffffcd5ffffffd8*uint256(v);y2+=0x000000000000053fffffff40000003e000000168000001a400000005fffffe66*uint256(v);y3+=0xfffffffffffff90000000dfffffff06ffffffad000000024000000a60000008d*uint256(v);y4+=0xfffffffffffffffffffffffffffffffffffffed7fffff6240000010a00000059*uint256(v);} v=(int256(z0>>62&M0)-O0)>>12;if(v>0){y0+=0x0000000000002ac000001dbffffffb700000048ffffffdd8000000f200000000*uint256(v);y1+=0x00000000000000c00000325ffffffba000000407fffffee3fffffe5400000000*uint256(v);y2+=0xffffffffffffdac00000124000000b9000000440000002fc00000145fffffdd3*uint256(v);y3+=0x00000000000018c0000020200000014ffffffbeffffffd1000000042000000f6*uint256(v);y4+=0xfffffffffffffffffffffffffffffffffffffe98000000a8000001000000007e*uint256(v);} v=(int256(z0>>93&M0)-O0)>>12;if(v>0){y0+=0x00000000000039ffffffe7dfffffd70ffffffccffffffce0000001a800000000*uint256(v);y1+=0x0000000000001effffffd5dffffff0f000000217fffffffbffffff19ffffff51*uint256(v);y2+=0xffffffffffffca0000000f9ffffff8e000000398000000fbfffffff8000000db*uint256(v);y3+=0xffffffffffff9b7ffffffd40000004effffffe80000002ac0000004c00000135*uint256(v);y4+=0xfffffffffffffffffffffffffffffffffffffda8000001f4000000500000009c*uint256(v);} v=(int256(z0>>124&M0)-O0)>>12;if(v>0){y0+=0xfffffffffffffe80000009bffffffac000000548000004d40000026e00000000*uint256(v);y1+=0xffffffffffff40fffffffddfffffff6ffffffb7ffffffc9bfffffb89ffffff87*uint256(v);y2+=0xffffffffffffe33ffffffe3fffffd65ffffffaeffffffef7ffffffb1fffffb64*uint256(v);y3+=0x0000000000004640000004a000000fa000000247fffed86bfffffed7ffffffc9*uint256(v);y4+=0x000000000000000000000000000000000000049ffffffe140000011bffffffd9*uint256(v);} v=(int256(z0>>155&M0)-O0)>>12;if(v>0){y0+=0x00000000000037fffffff1dffffffccffffffb77fffffd27fffffca400000000*uint256(v);y1+=0xffffffffffffcd00000002c000001230000000b80000019000000220000000ac*uint256(v);y2+=0x0000000000004e40000005000000014ffffffcb8000003d800000074000000b7*uint256(v);y3+=0x0000000000001c7ffffff07ffffffbfffffffcc800000427fffffe57fffffffe*uint256(v);y4+=0xfffffffffffffffffffffffffffffffffffffc70000000a000000205ffffff0c*uint256(v);} v=(int256(z0>>186&M0)-O0)>>12;if(v>0){y0+=0x0000000000001fc00000153ffffffdd000000257fffffc600000006400000000*uint256(v);y1+=0x000000000000037ffffff5ffffffdacffffff780000000bffffffd17ffffff79*uint256(v);y2+=0xfffffffffffffa4000000d200000059000000397fffffee8000000fffffffee3*uint256(v);y3+=0x0000000000000600000001000000156ffffffc6ffffffc800000004c000000c4*uint256(v);y4+=0xfffffffffffffffffffffffffffffffffffffbc7fffff258000000cfffffff25*uint256(v);} v=(int256(z0>>217&M0)-O0)>>12;if(v>0){y0+=0xffffffffffffa340000029e00000126ffffffcaffffffc7bfffff57000000000*uint256(v);y1+=0xffffffffffff0840000015bffffe7a2ffffffa580000051fffffcde600000005*uint256(v);y2+=0xffffffffffffba7ffffff5fffffffe4ffffffa70000003cc0000004dfffffe72*uint256(v);y3+=0x000000000000117fffffdc7ffffff92ffffffcb7fffff9680000001200000007*uint256(v);y4+=0xfffffffffffffffffffffffffffffffffffffbbffffff5d3ffffff11ffffff47*uint256(v);} v=(int256(z1&M0)-O0)>>12;if(v>0){y0+=0xfffffffffffff88000001ddffffffe3ffffffe58000001b3fffff9de00000000*uint256(v);y1+=0xffffffffffff3e7fffffed60000007afffffffa0000004e800000177ffffffed*uint256(v);y2+=0x000000000000117ffffffee0000012e0000002dffffffe3000000070000000b0*uint256(v);y3+=0xffffffffffff8a7ffffffea00000043ffffffa7ffffffb1c0000001dfffffecb*uint256(v);y4+=0xfffffffffffffffffffffffffffffffffffffc6ffffffc93ffffff2e00000032*uint256(v);} v=(int256(z1>>31&M0)-O0)>>12;if(v>0){y0+=0x00000000000032c0000003000000042000000338000001580000028400000000*uint256(v);y1+=0x000000000000318000001140000000c0000001880000010c00000169ffffffc9*uint256(v);y2+=0xffffffffffffcf0000001b7fffffe5400000008ffffffd17ffffff140000004f*uint256(v);y3+=0xffffffffffffe9ffffffedbfffffeb5000000000000001cbfffffdf4000000cf*uint256(v);y4+=0xfffffffffffffffffffffffffffffffffffffd60000002cc0000029400000058*uint256(v);} v=(int256(z1>>62&M0)-O0)>>12;if(v>0){y0+=0xffffffffffffdb7fffffec0000000a8000000247fffffc98000001ee00000000*uint256(v);y1+=0xffffffffffffe500000019bffffff0c00000045800000123fffffd19ffffffe7*uint256(v);y2+=0x000000000000068000001cdffffffe0000000850000002400000003a0000011b*uint256(v);y3+=0xffffffffffff954000001860000008bffffffb780000009800000197ffffff97*uint256(v);y4+=0xfffffffffffffffffffffffffffffffffffffc0ffffffdebffffffb7ffffffe8*uint256(v);} v=(int256(z1>>93&M0)-O0)>>12;if(v>0){y0+=0x0000000000002f3fffffed5ffffff5d0000002ffffffff4c000001a400000000*uint256(v);y1+=0x00000000000062bfffffeedffffffbc000000647ffffff28000000b7fffffffc*uint256(v);y2+=0xfffffffffffffe8000000be000000b300000030ffffffe47ffffffadffffffc2*uint256(v);y3+=0x00000000000012fffffff360000005a0000001080000017c0000016fffffffc5*uint256(v);y4+=0xfffffffffffffffffffffffffffffffffffffb70000002040000018e00000034*uint256(v);} v=(int256(z1>>124&M0)-O0)>>12;if(v>0){y0+=0x0000000000000b3fffffe260000007e0000002d8000002c00000010600000000*uint256(v);y1+=0x0000000000000980000001c00000040000000627fffffd43fffffe2200000003*uint256(v);y2+=0xfffffffffffff9fffffff66000000aeffffffdd7fffffe78000001b7ffffff9b*uint256(v);y3+=0x00000000000010000000082000000c6ffffffd28000001780000005a000000ea*uint256(v);y4+=0x000000000000000000000000000000000000005000000247ffffff3200000057*uint256(v);} v=(int256(z1>>155&M0)-O0)>>12;if(v>0){y0+=0xffffffffffffc3800000595ffffffcefffffff9ffffffa67ffffd60e00000000*uint256(v);y1+=0xfffffffffffe57c0000001dffffffc4fffffbf68000005c0000000fdfffffff4*uint256(v);y2+=0x0000000000001e7fffffe6e00000093ffffffc70000000bbfffffed6000000e2*uint256(v);y3+=0xffffffffffffe4bffffff43ffffff1cffffffbeffffffedc000000e2000000b1*uint256(v);y4+=0x0000000000000000000000000000000000000027ffffffd00000014bfffffaee*uint256(v);} v=(int256(z1>>186&M0)-O0)>>12;if(v>0){y0+=0xfffffffffffffc800000021ffffffcffffffff6fffffff780000031c00000000*uint256(v);y1+=0x0000000000002e40000004c0000013c00000033ffffff5dc0000019dffffff70*uint256(v);y2+=0xffffffffffffba3ffffff8600000134000000028000002d00000003600000104*uint256(v);y3+=0x00000000000003ffffffea8000000a3000000090000001b8000001b5ffffffce*uint256(v);y4+=0xfffffffffffffffffffffffffffffffffffffb17ffffffb80000009e00000082*uint256(v);} v=(int256(z1>>217&M0)-O0)>>12;if(v>0){y0+=0xffffffffffffd2c0000013c000000470000003e7fffffd67ffffff0400000000*uint256(v);y1+=0x00000000000019400000121ffffff4affffff84ffffffff7fffffe71ffffffdd*uint256(v);y2+=0x000000000000200000001320000004fffffffea00000006800000093ffffffe6*uint256(v);y3+=0x0000000000003c3ffffffadfffff9f200000020ffffffcb40000019800000000*uint256(v);y4+=0xfffffffffffffffffffffffffffffffffffffbf7fffff3b400000003ffffff49*uint256(v);} v=(int256(z2&M0)-O0)>>12;if(v>0){y0+=0x0000000000001dffffffeaffffffc1a00000009800000684000005ca00000000*uint256(v);y1+=0x00000000000027800000171fffffc5600000019ffffff7dffffffa6fffffff6f*uint256(v);y2+=0x0000000000003e400000055ffffffc3fffffff68000003f40000003dfffffeb8*uint256(v);y3+=0x000000000000544000000e4000000eeffffffed0000005afffffffe8000000a6*uint256(v);y4+=0xfffffffffffffffffffffffffffffffffffffcd00000017bfffffedc0000007b*uint256(v);} v=(int256(z2>>31&M0)-O0)>>12;if(v>0){y0+=0x0000000000000480000025800000125ffffff97000000137fffff9c800000000*uint256(v);y1+=0xffffffffffffca80000011fffffffe0ffffff5500000078c00000299ffffff5d*uint256(v);y2+=0x000000000000124000000260000000a0000002080000025ffffffeabfffffdb5*uint256(v);y3+=0x000000000000033fffffec9ffffff81fffffff4ffffffbe80000012a00000159*uint256(v);y4+=0xfffffffffffffffffffffffffffffffffffffab0000006540000020ffffffeb1*uint256(v);} v=(int256(z2>>62&M0)-O0)>>12;if(v>0){y0+=0xffffffffffffd3bffffffc400000213ffffffc87fffffdb8000000d000000000*uint256(v);y1+=0x000000000000cb7fffffecffffffe33000000b2fffffff700000071a00000013*uint256(v);y2+=0xffffffffffffde80000024bffffff72ffffffba0000000f3ffffff5e0000012d*uint256(v);y3+=0x000000000000633fffffe89fffffb1cffffffd60000000640000000400000046*uint256(v);y4+=0xfffffffffffffffffffffffffffffffffffffe80000014fbffffff4e00000026*uint256(v);} v=(int256(z2>>93&M0)-O0)>>12;if(v>0){y0+=0xffffffffffffe5ffffffff600000096ffffffd20000001640000079c00000000*uint256(v);y1+=0x0000000000000fc00000051ffffff44ffffffeb8000001fffffffda9ffffff93*uint256(v);y2+=0xffffffffffffeac00000175ffffff110000000a8000000a000000027fffffd38*uint256(v);y3+=0x00000000000012800000031ffffffaf0000001c8000002d7ffffffde00000101*uint256(v);y4+=0x00000000000000000000000000000000000004b7fffffe3800000003fffffff3*uint256(v);} v=(int256(z2>>124&M0)-O0)>>12;if(v>0){y0+=0xfffffffffffd784000000a80000007affffffbd8000002e3fffffe4000000000*uint256(v);y1+=0x000000000000340000001d000000064ffffff900000004e40000016200000010*uint256(v);y2+=0x0000000000003efffffffac000001b5ffffffb98000001c7fffffeac00000011*uint256(v);y3+=0x000000000000183ffffffd800000209ffffffa88000004c800000247fffffedb*uint256(v);y4+=0x000000000000000000000000000000000000027ffffff7dffffffeeffffffff0*uint256(v);} v=(int256(z2>>155&M0)-O0)>>12;if(v>0){y0+=0xffffffffffffb47ffffff67ffffffc5000000b7000000217fffffe0a00000000*uint256(v);y1+=0xffffffffffffe5fffffff500000012effffffc90000002f0000001ca00000056*uint256(v);y2+=0x0000000000000d7ffffff4fffffff3fffffffafffffffe340000014400000068*uint256(v);y3+=0xfffffffffffff7bffffff5dffffff2b000000067ffffff0bffffff9400000180*uint256(v);y4+=0xfffffffffffffffffffffffffffffffffffffbfffffffee7fffffdedfffffff1*uint256(v);} v=(int256(z2>>186&M0)-O0)>>12;if(v>0){y0+=0x000000000000467ffffff13ffffffe5ffffffc30000001dc0000000e00000000*uint256(v);y1+=0xffffffffffffde7fffffffc00000118ffffffcf80000024c0000010fffffff9d*uint256(v);y2+=0xffffffffffffd3bfffffe99ffffffc300000061800000047fffffe2800000087*uint256(v);y3+=0xffffffffffffd140000006c000000d60000004880000047c000000a600000105*uint256(v);y4+=0xffffffffffffffffffffffffffffffffffffff2ffffffd7c000000b600000070*uint256(v);} v=(int256(z2>>217&M0)-O0)>>12;if(v>0){y0+=0xffffffffffffedffffffec5ffffff82000000778000004d3fffffd5400000000*uint256(v);y1+=0xffffffffffffeafffffff72000000d4000000487fffffca400000347ffffff6e*uint256(v);y2+=0x00000000000027bffffffd60000003bfffffef880000014bffffffd2000000a9*uint256(v);y3+=0x000000000000244000001abffffff2f0000004ffffffffdffffffea1fffffbc3*uint256(v);y4+=0x0000000000000000000000000000000000000488000006300000008c0000004b*uint256(v);} v=(int256(z3&M0)-O0)>>12;if(v>0){y0+=0xfffffffffffffac0000019e00000137ffffffae7fffffccbfffff79e00000000*uint256(v);y1+=0xffffffffffff83000000213fffffde2ffffff13000000478000001e00000009a*uint256(v);y2+=0x000000000000393ffffff840000000f00000011800000237fffffe1400000129*uint256(v);y3+=0xfffffffffffff1bfffffeec00000025000000557fffffec400000091ffffff9d*uint256(v);y4+=0xffffffffffffffffffffffffffffffffffffffb7fffffe4400000037ffffff01*uint256(v);} v=(int256(z3>>31&M0)-O0)>>12;if(v>0){y0+=0xffffffffffff8abfffffed200000000fffffff90000000340000021600000000*uint256(v);y1+=0x000000000000307fffffed600000152000000927fffff3580000001e00000025*uint256(v);y2+=0x0000000000003affffffe8dfffffedbffffff5e0000001080000006800000068*uint256(v);y3+=0x000000000000157fffffef7ffffff4effffffac80000004bfffffe7dfffffe95*uint256(v);y4+=0xfffffffffffffffffffffffffffffffffffffed00000029ffffffeda00000138*uint256(v);} v=(int256(z3>>62&M0)-O0)>>12;if(v>0){y0+=0xffffffffffffeec0000002600000051000000137ffffff80000001c600000000*uint256(v);y1+=0x00000000000021bffffffc3ffffffe9000000297fffffd9800000061ffffff8b*uint256(v);y2+=0xfffffffffffff440000006e00000012ffffffbf7fffffcf7ffffff68000000e3*uint256(v);y3+=0x00000000000040c0000018bffffff4600000014fffffff43fffffee00000001c*uint256(v);y4+=0x00000000000000000000000000000000000000b7fffff9e3ffffffc80000004a*uint256(v);} v=(int256(z3>>93&M0)-O0)>>12;if(v>0){y0+=0x0000000000005500000028200000051ffffff5dffffffaffffffffea00000000*uint256(v);y1+=0x000000000000324000000fc000000cbfffffef3800000e8400000059ffffff66*uint256(v);y2+=0xffffffffffffaf7fffffecbffffff72ffffffc180000020c0000000c0000004d*uint256(v);y3+=0xfffffffffffff0fffffff8800000035ffffffb6ffffff9ffffffff5bfffffed0*uint256(v);y4+=0xfffffffffffffffffffffffffffffffffffffef80000010bfffffdf9fffffe33*uint256(v);} v=(int256(z3>>124&M0)-O0)>>12;if(v>0){y0+=0xffffffffffffd8c000000b60000001300000016ffffffe200000012400000000*uint256(v);y1+=0x00000000000020c000000fffffffe75000000010000001a3fffffd95ffffffa6*uint256(v);y2+=0x000000000000133fffffe4e000000bc0000005d7ffffffbc000000efffffff96*uint256(v);y3+=0xffffffffffffdd8000000abfffffdd70000004d0000000f80000005c00000137*uint256(v);y4+=0xfffffffffffffffffffffffffffffffffffffce7fffffdac0000005dffffff92*uint256(v);} v=(int256(z3>>155&M0)-O0)>>12;if(v>0){y0+=0x0000000000001540000002800000006ffffffb08000004980000011800000000*uint256(v);y1+=0x00000000000025ffffffed9ffffff110000011d7fffffda3ffffff8bffffffb4*uint256(v);y2+=0x0000000000001c800000079fffffe2dffffff8c000000354000000ee0000008f*uint256(v);y3+=0xfffffffffffffd80000018fffffffc2fffffff47fffff78ffffffff600000030*uint256(v);y4+=0x00000000000000000000000000000000000000e0000005dbfffffc8200000123*uint256(v);} v=(int256(z3>>186&M0)-O0)>>12;if(v>0){y0+=0x00000000000034ffffffe7a000000bb000000588000003480000026e00000000*uint256(v);y1+=0x0000000000003fbffffff580000008f00000065ffffffabfffffffac00000023*uint256(v);y2+=0xffffffffffffc9c000000c800000056ffffffd97ffffffcfffffff8bffffffdd*uint256(v);y3+=0x00000000000026800000108000000190000000f7ffffff7bfffffe1c000000a0*uint256(v);y4+=0xfffffffffffffffffffffffffffffffffffffcb8000001380000004000000019*uint256(v);} v=(int256(z3>>217&M0)-O0)>>12;if(v>0){y0+=0xfffffffffffff77ffffffe3ffffff60fffffff000000021c0000019600000000*uint256(v);y1+=0x0000000000004abffffff8c0000006c0000000e7fffffe5bffffff880000005a*uint256(v);y2+=0x00000000000019c00000195ffffff380000002c80000003c000000abffffffd9*uint256(v);y3+=0xffffffffffffff800000073ffffff4c0000004d8000002d0000000e60000009b*uint256(v);y4+=0xfffffffffffffffffffffffffffffffffffffe280000030c000001ee0000007a*uint256(v);} o=4825781; v=(int256(y0&M1)-O1)>>10;if(v>0)o+=4096*v; v=(int256(y0>>33&M1)-O1)>>10;if(v>0)o+=-3217*v; v=(int256(y0>>66&M1)-O1)>>10;if(v>0)o+=1388*v; v=(int256(y0>>99&M1)-O1)>>10;if(v>0)o+=-1077*v; v=(int256(y0>>132&M1)-O1)>>10;if(v>0)o+=601*v; v=(int256(y0>>165&M1)-O1)>>10;if(v>0)o+=2003*v; v=(int256(y0>>198&M1)-O1)>>10;if(v>0)o+=-6547*v; v=(int256(y1&M1)-O1)>>10;if(v>0)o+=104*v; v=(int256(y1>>33&M1)-O1)>>10;if(v>0)o+=-1390*v; v=(int256(y1>>66&M1)-O1)>>10;if(v>0)o+=-2983*v; v=(int256(y1>>99&M1)-O1)>>10;if(v>0)o+=4081*v; v=(int256(y1>>132&M1)-O1)>>10;if(v>0)o+=1566*v; v=(int256(y1>>165&M1)-O1)>>10;if(v>0)o+=834*v; v=(int256(y1>>198&M1)-O1)>>10;if(v>0)o+=2372*v; v=(int256(y2&M1)-O1)>>10;if(v>0)o+=-1369*v; v=(int256(y2>>33&M1)-O1)>>10;if(v>0)o+=-555*v; v=(int256(y2>>66&M1)-O1)>>10;if(v>0)o+=659*v; v=(int256(y2>>99&M1)-O1)>>10;if(v>0)o+=800*v; v=(int256(y2>>132&M1)-O1)>>10;if(v>0)o+=696*v; v=(int256(y2>>165&M1)-O1)>>10;if(v>0)o+=-680*v; v=(int256(y2>>198&M1)-O1)>>10;if(v>0)o+=-1386*v; v=(int256(y3&M1)-O1)>>10;if(v>0)o+=-1250*v; v=(int256(y3>>33&M1)-O1)>>10;if(v>0)o+=811*v; v=(int256(y3>>66&M1)-O1)>>10;if(v>0)o+=3346*v; v=(int256(y3>>99&M1)-O1)>>10;if(v>0)o+=206*v; v=(int256(y3>>132&M1)-O1)>>10;if(v>0)o+=2126*v; v=(int256(y3>>165&M1)-O1)>>10;if(v>0)o+=680*v; v=(int256(y3>>198&M1)-O1)>>10;if(v>0)o+=1129*v; v=(int256(y4&M1)-O1)>>10;if(v>0)o+=3593*v; v=(int256(y4>>33&M1)-O1)>>10;if(v>0)o+=1145*v; v=(int256(y4>>66&M1)-O1)>>10;if(v>0)o+=-1782*v; v=(int256(y4>>99&M1)-O1)>>10;if(v>0)o+=670*v; o=(o>>12)*6103515625000000; } } function fee(int256 a,int256 w,int256 b) internal pure returns(int256 fb,int256 fa){ unchecked { fb=b> -w?U-exp(-(b+w)/10000):int256(0);fa=a>w?U-exp(-(a-w)/10000):int256(0); fb=clip(fb,0,U/10);fa=clip(fa,0,U/10); } } function rnd(int256 v) internal pure returns(int256){ unchecked {return (v*16384/100+U/2+(int256(1)<<30)*U)/U-(int256(1)<<30);} } function fees(int256 w,int256 r,uint256 ph) internal pure returns(int256,int256){ unchecked { int256 lim=ph==0?30*U:800*U;int256 x0=rnd(clip(w,-800*U,800*U));int256 x1=rnd(clip(r,-lim,lim)); (int256 a,int256 b)=net2(uint256(x0),uint256(x1),ph*16384); return fee(a,w,b); } } function afterInitialize(uint256 x,uint256 y) external override returns(uint256,uint256){ unchecked { int256 sp=int256(y*1e18/x); slots[0]=uint256(uint128(int128(ln(sp)))); slots[1]=uint256(uint128(int128(sp)))*(1+(uint256(1)<<128)); slots[2]=2870627705508484*(1+(uint256(1)<<64)); return(2870627705508484,2870627705508484); } } // filter update over a gap of dt steps; returns new m, v, rival spot r and the arb flag function update(int256 m,int256 v,int256 fb,int256 fa,int256 sp,int256 r,int256 s,bool buy,uint256 dt) internal pure returns(int256,int256,int256,bool arb){ unchecked { int256 lo=ln(mul(sp,U-fb));int256 hi=ln(divi(sp,U-fa)); uint256 n=dt-1;if(n>12)n=12; for(uint256 i=0;i0?divi(la,la+lr):int256(0);arb=pi>U/2; if(pi>5e15&&pi<995e15){ m=mul(pi,lp)+mul(U-pi,mt); v=mul(pi,mul(lp-m,lp-m))+mul(U-pi,vt+mul(mt-m,mt-m)); }else if(arb){m=lp;v=0;}else{m=mt;v=vt;} int256 pe=exp(m);r=clip(r,mul(pe,997e15),divi(pe,997e15)); return(m,v,r,arb); } } function afterSwap(TradeInfo calldata t) external override returns(uint256,uint256){ unchecked { uint256 s0=slots[0];uint256 s1=slots[1];uint256 s2=slots[2]; int256 m=int256(int128(uint128(s0)));int256 v=int256(int128(uint128(s0>>128))); int256 sp=int256(int128(uint128(s1)));int256 r=int256(int128(uint128(s1>>128))); int256 fb=int256(s2&M64);int256 fa=int256(s2>>64&M64);uint256 cnt=s2>>192; int256 s=int256(t.reserveY*1e18/t.reserveX);bool arb; if(t.timestamp>(s2>>128&M64)){(m,v,r,arb)=update(m,v,fb,fa,sp,r,s,t.isBuy,t.timestamp-(s2>>128&M64));cnt=0;} uint256 ph; if(!arb){ int256 c=t.isBuy?divi(mul(s,U-fb),997e15):divi(mul(s,997e15),U-fa); if(t.isBuy?cr)r=c; cnt++;ph=cnt==1?1:2; } int256 w=(ln(s)-m)*10000;int256 rr=(ln(r)-m)*10000; (fb,fa)=fees(w,rr,ph); slots[0]=uint256(uint128(int128(m)))|uint256(uint128(int128(v)))<<128; slots[1]=uint256(uint128(int128(s)))|uint256(uint128(int128(r)))<<128; slots[2]=uint256(fb)|uint256(fa)<<64|t.timestamp<<128|cnt<<192; return(uint256(fb),uint256(fa)); } } } ``` ## Generator (gen2.py, sha256 55fc04058c9196d295b89191d1718ef78e06a3ca2460128d516431b60477ab1b; `python gen2.py out.sol` next to net-1's netref.py and payload.bin; env NETOLD=1 emits variant 2 for attribution) ```python """trim-2 generator: writes the trimmed Strategy.sol from the fixed-net payload (payload.bin, sha256 9727ecc8...). Policy = net-1 (netref.Net2) unchanged. Only gas changes: packed storage (3 slots), shared layer-0 work for the ask/bid passes, one unrolled hidden-layer function with exact narrow lanes, no memory arrays or structs. usage: python gen2.py out.sol [lane0 lane1] """ import sys, math from decimal import Decimal, getcontext import netref as N getcontext().prec = 60 out = sys.argv[1] M256 = 1 << 256 (W0, B0, S0), (W1, B1, S1), (W2, B2, S2) = N.LAYERS assert N.H == 32 and (S0, S1, S2) == (12, 10, 12) # sd input (index 3) must be unused assert all(W0[j][3] == 0 for j in range(32)) # rigorous bounds: inputs |x0|,|x1| <= 131072 (800 bps * 16384/100), x2 in {0,16384,32768} XM = [131072, 131072, 32768] b0 = [abs(B0[j]) + sum(abs(W0[j][i]) * XM[i] for i in range(3)) for j in range(32)] # |lane| bound, layer 0 h0 = [max(0, v >> S0) for v in b0] # activation bound after shift b1 = [abs(B1[j]) + sum(abs(W1[j][i]) * h0[i] for i in range(32)) for j in range(32)] # |lane| bound, layer 1 h1 = [max(0, v >> S1) for v in b1] b2 = abs(B2[0]) + sum(abs(W2[0][i]) * h1[i] for i in range(32)) def lane_bits(bound): # smallest L with bound < 2^(L-1) L = 2 while bound >= (1 << (L - 1)): L += 1 return L L0 = int(sys.argv[2]) if len(sys.argv) > 2 else lane_bits(max(b0)) L1 = int(sys.argv[3]) if len(sys.argv) > 3 else lane_bits(max(b1)) assert max(b0) < (1 << (L0 - 1)) and max(b1) < (1 << (L1 - 1)) n0 = 256 // L0; n1 = 256 // L1 # lanes per word k0 = -(-32 // n0); k1 = -(-32 // n1) # words per layer print('bounds: layer0 max %d -> %d-bit lanes (%d/word, %d words); layer1 max %d -> %d-bit lanes (%d/word, %d words); layer2 |o|<=%d' % (max(b0), L0, n0, k0, max(b1), L1, n1, k1, b2)) def hx(v): return '0x%064x' % (v % M256) def simd(vals, L): return sum((v % M256) * (1 << (L * m)) for m, v in enumerate(vals)) % M256 OFF0 = 1 << (L0 - 1); OFF1 = 1 << (L1 - 1) def group(vals, n, k): # split 32 values into k words of n lanes (zero padded) return [vals[n * w:n * w + n] + [0] * (n - len(vals[n * w:n * w + n])) for w in range(k)] # layer-0 words: per word k: bias(+OFF), P0 (w for x0), P1, P2 B0w = [simd([v + OFF0 for v in g], L0) for g in group(B0, n0, k0)] P0w = [[simd(g, L0) for g in group([W0[j][i] for j in range(32)], n0, k0)] for i in range(3)] # layer-1 words: bias(+OFF) per word; per input unit j: k1 words C_j_k B1w = [simd([v + OFF1 for v in g], L1) for g in group(B1, n1, k1)] C = [[simd(g, L1) for g in group([W1[o][j] for o in range(32)], n1, k1)] for j in range(32)] L = [] L.append(' uint256 constant M0=%s;uint256 constant M1=%s;' % (hx((1 << L0) - 1), hx((1 << L1) - 1))) L.append(' int256 constant O0=%d;int256 constant O1=%d;' % (OFF0, OFF1)) # layer 0 + both passes zs = lambda p: ','.join('%s%d' % (p, k) for k in range(k0)) L.append(' // layer 0 for ask (a,b,c) and bid (-a,-b,c): s=bias+P2*c shared, t=P0*a+P1*b, ask=s+t, bid=s-t') L.append(' function net2(uint256 a,uint256 b,uint256 c) internal pure returns(int256 oa,int256 ob){ unchecked {') L.append(' uint256 s;uint256 t;' + ''.join('uint256 x%d;uint256 y%d;' % (k, k) for k in range(k0))) for k in range(k0): L.append(' s=%s+%s*c;t=%s*a+%s*b;x%d=s+t;y%d=s-t;' % (hx(B0w[k]), hx(P0w[2][k]), hx(P0w[0][k]), hx(P0w[1][k]), k, k)) L.append(' oa=hid(%s);ob=hid(%s);' % (zs('x'), zs('y'))) L.append(' } }') # hidden layer + output, unrolled L.append(' // layers 1 and 2: z = layer-0 lanes (bias+2^%d+sum, exact); ReLU((lane-2^%d)>>%d); accumulators %d x %d-bit lanes' % (L0 - 1, L0 - 1, S0, n1, L1)) L.append(' function hid(%s) internal pure returns(int256 o){ unchecked {' % ','.join('uint256 z%d' % k for k in range(k0))) L.append(' ' + ''.join('uint256 y%d=%s;' % (k, hx(B1w[k])) for k in range(k1)) + 'int256 v;') for j in range(32): w, m = divmod(j, n0) src = 'z%d' % w if m == 0 else 'z%d>>%d' % (w, L0 * m) L.append(' v=(int256(%s&M0)-O0)>>%d;if(v>0){%s}' % (src, S0, ''.join('y%d+=%s*uint256(v);' % (k, hx(C[j][k])) for k in range(k1)))) L.append(' o=%d;' % B2[0]) for j in range(32): w, m = divmod(j, n1) src = 'y%d' % w if m == 0 else 'y%d>>%d' % (w, L1 * m) wt = W2[0][j] term = 'v' if wt == 1 else ('-v' if wt == -1 else '%d*v' % wt) if wt == 0: continue L.append(' v=(int256(%s&M1)-O1)>>%d;if(v>0)o+=%s;' % (src, S1, term)) L.append(' o=(o>>%d)*6103515625000000;' % S2) L.append(' } }') NET = '\n'.join(L) import os, re if os.environ.get('NETOLD'): # net-1's own net section (l0/unpack/l1a-d/net), for attribution only n1 = open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'Strategy.sol')).read() NET = n1[n1.index(' uint256 constant OFF='):n1.index(' function fee(')].rstrip('\n') NET = NET.replace('uint256 constant M64=0xffffffffffffffff;\n', '') NET += '\n function net2(uint256 a,uint256 b,uint256 c) internal pure returns(int256,int256){ unchecked { return (net(int256(a),int256(b),int256(c)),net(-int256(a),-int256(b),int256(c))); } }' # init fee constant: net(0,0,0) bps -> 1-exp(-a*1e-4) in WAD, nearest a = N.net_bps(0, 0, 0) init = int((1 - Decimal(-a * 1e-4).exp()) * Decimal(10 ** 18) + Decimal('0.5')) assert init == 2870627705508484, init SRC = r'''// SPDX-License-Identifier: MIT pragma solidity ^0.8.24; import {AMMStrategyBase} from "./AMMStrategyBase.sol"; import {TradeInfo} from "./IAMMStrategy.sol"; // Gaussian solved policy by Taylor (gtmcknight), amm-sim 4812a638; filter ported from port-3; policy = net-1 (netref.Net2). // Offsets: fixed-net 4-32-32-1 int16 MLP (payload sha256 9727ecc8...), integer forward pass, shifts 12,10,12, // Q14 activations, arithmetic-shift floor. 2 passes: ask net(w,R,phase), bid net(-w,-R,phase), inputs clamped to the grid. // trim-2 (gas only, fees unchanged): state packed in 3 storage slots; layer-0 bias+phase work shared by the two passes; // hidden layer unrolled, no memory arrays/structs; model data as PUSH32 immediates with exact narrow lanes // (layer 0: %(L0)d-bit lanes, layer 1: %(L1)d-bit lanes; every lane = bias + 2^(L-1) + sum(w*x), exact mod 2^256). // Taylor source: https://github.com/gtmcknight/amm-sim/tree/4812a6382311624e1506aca131ca35c378f930d9/research/solved contract Strategy is AMMStrategyBase { int256 constant U=1e18; int256 constant LN2=693147180559945309; uint256 constant M64=0xffffffffffffffff; // slots[0] = m (int128) | v (int128)<<128 ; slots[1] = sp (int128) | r (int128)<<128 ; slots[2] = fb | fa<<64 | t<<128 | cnt<<192 function getName() external pure override returns(string memory) { unchecked {return "TaylorGaussianPlainNet2Trim";} } function mul(int256 a,int256 b) internal pure returns(int256){ unchecked {return a*b/U;} } function divi(int256 a,int256 b) internal pure returns(int256){ unchecked {return a*U/b;} } function ab(int256 x) internal pure returns(int256){ unchecked {return x<0?-x:x;} } function clip(int256 x,int256 a,int256 b) internal pure returns(int256){ unchecked {return xb?b:x);} } function ln(int256 x) internal pure returns(int256 z){ unchecked { require(x>0); int256 k; while(x< U){x*=2;k--;} while(x>=2*U){x/=2;k++;} int256 y=divi(x-U,x+U); int256 y2=mul(y,y); int256 q=y; z=y; for(int256 n=3;n<=29;n+=2){q=mul(q,y2);z+=q/n;} return 2*z+k*LN2; } } function exp(int256 x) internal pure returns(int256){ unchecked { if(x< -42*U)return 0; require(x<60*U); int256 k=(x>=0?x+LN2/2:x-LN2/2)/LN2; int256 r=x-k*LN2; int256 r2=mul(r,r); int256 even=665280*U+mul(r2,75600*U+mul(r2,840*U+r2)); int256 odd=mul(r,332640*U+mul(r2,10080*U+mul(r2,42*U))); int256 s=divi(even+odd,even-odd); return k>=0?s*int256(uint256(1)<>128>0){z>>=128;r<<=64;} if(z>>64>0){z>>=64;r<<=32;} if(z>>32>0){z>>=32;r<<=16;} if(z>>16>0){z>>=16;r<<=8;} if(z>>8>0){z>>=8;r<<=4;} if(z>>4>0){z>>=4;r<<=2;} if(z>>2>0){r<<=1;} for(uint256 i=0;i<7;i++)r=(r+n/r)/2; return int256(r10*U)return 0;return divi(exp(-mul(x,x)/2),2506628274631000200);} } // Hart rational normal tail approximation; erf(x)=2*Phi(x*sqrt(2))-1. function cdf(int256 x,int256 density) internal pure returns(int256){ unchecked { int256 y=ab(x);if(y>8*U)return x>0?U:int256(0); int256 n=35262496599891000; n=mul(n,y)+700383064443688000;n=mul(n,y)+6373962203531650000; n=mul(n,y)+33912866078383000000;n=mul(n,y)+112079291497871000000; n=mul(n,y)+221213596169931000000;n=mul(n,y)+220206867912376000000; int256 d=88388347648318400; d=mul(d,y)+1755667163182640000;d=mul(d,y)+16064177579207000000; d=mul(d,y)+86780732202946100000;d=mul(d,y)+296564248779674000000; d=mul(d,y)+637333633378831000000;d=mul(d,y)+793826512519948000000; d=mul(d,y)+440413735824752000000; int256 tail=mul(mul(density,2506628274631000200),divi(n,d));return x>0?U-tail:tail; } } function trunc(int256 m,int256 v,int256 lo,int256 hi) internal pure returns(int256 Z,int256 mt,int256 vt){ unchecked { if(v<=0)return (lo<=m&&m<=hi?U:int256(0),m,0); int256 sd=root(v);int256 a=divi(lo-m,sd);int256 b=divi(hi-m,sd); int256 pa=pdf(a);int256 pb=pdf(b); Z=cdf(b,pb)-cdf(a,pa); if(Z<1e6)return(Z,m -w?U-exp(-(b+w)/10000):int256(0);fa=a>w?U-exp(-(a-w)/10000):int256(0); fb=clip(fb,0,U/10);fa=clip(fa,0,U/10); } } function rnd(int256 v) internal pure returns(int256){ unchecked {return (v*16384/100+U/2+(int256(1)<<30)*U)/U-(int256(1)<<30);} } function fees(int256 w,int256 r,uint256 ph) internal pure returns(int256,int256){ unchecked { int256 lim=ph==0?30*U:800*U;int256 x0=rnd(clip(w,-800*U,800*U));int256 x1=rnd(clip(r,-lim,lim)); (int256 a,int256 b)=net2(uint256(x0),uint256(x1),ph*16384); return fee(a,w,b); } } function afterInitialize(uint256 x,uint256 y) external override returns(uint256,uint256){ unchecked { int256 sp=int256(y*1e18/x); slots[0]=uint256(uint128(int128(ln(sp)))); slots[1]=uint256(uint128(int128(sp)))*(1+(uint256(1)<<128)); slots[2]=%(INIT)d*(1+(uint256(1)<<64)); return(%(INIT)d,%(INIT)d); } } // filter update over a gap of dt steps; returns new m, v, rival spot r and the arb flag function update(int256 m,int256 v,int256 fb,int256 fa,int256 sp,int256 r,int256 s,bool buy,uint256 dt) internal pure returns(int256,int256,int256,bool arb){ unchecked { int256 lo=ln(mul(sp,U-fb));int256 hi=ln(divi(sp,U-fa)); uint256 n=dt-1;if(n>12)n=12; for(uint256 i=0;i0?divi(la,la+lr):int256(0);arb=pi>U/2; if(pi>5e15&&pi<995e15){ m=mul(pi,lp)+mul(U-pi,mt); v=mul(pi,mul(lp-m,lp-m))+mul(U-pi,vt+mul(mt-m,mt-m)); }else if(arb){m=lp;v=0;}else{m=mt;v=vt;} int256 pe=exp(m);r=clip(r,mul(pe,997e15),divi(pe,997e15)); return(m,v,r,arb); } } function afterSwap(TradeInfo calldata t) external override returns(uint256,uint256){ unchecked { uint256 s0=slots[0];uint256 s1=slots[1];uint256 s2=slots[2]; int256 m=int256(int128(uint128(s0)));int256 v=int256(int128(uint128(s0>>128))); int256 sp=int256(int128(uint128(s1)));int256 r=int256(int128(uint128(s1>>128))); int256 fb=int256(s2&M64);int256 fa=int256(s2>>64&M64);uint256 cnt=s2>>192; int256 s=int256(t.reserveY*1e18/t.reserveX);bool arb; if(t.timestamp>(s2>>128&M64)){(m,v,r,arb)=update(m,v,fb,fa,sp,r,s,t.isBuy,t.timestamp-(s2>>128&M64));cnt=0;} uint256 ph; if(!arb){ int256 c=t.isBuy?divi(mul(s,U-fb),997e15):divi(mul(s,997e15),U-fa); if(t.isBuy?cr)r=c; cnt++;ph=cnt==1?1:2; } int256 w=(ln(s)-m)*10000;int256 rr=(ln(r)-m)*10000; (fb,fa)=fees(w,rr,ph); slots[0]=uint256(uint128(int128(m)))|uint256(uint128(int128(v)))<<128; slots[1]=uint256(uint128(int128(s)))|uint256(uint128(int128(r)))<<128; slots[2]=uint256(fb)|uint256(fa)<<64|t.timestamp<<128|cnt<<192; return(uint256(fb),uint256(fa)); } } } ''' s = SRC % {'NET': NET, 'INIT': init, 'L0': L0, 'L1': L1} open(out, 'w').write(s) print('wrote', out, len(s), 'bytes; words: layer0', 4 * k0, 'layer1', k1 + 32 * k1) ``` ## Python reference Unchanged: net-1's `netref.py` (`Net2`) over port-3's `standalone.py` `Coarse`, as published in those artifacts.