]> git.codecow.com Git - nano25519.git/commitdiff
Update tests and agent file.
authorChris Duncan <chris@codecow.com>
Tue, 3 Nov 2026 04:21:26 +0000 (20:21 -0800)
committerChris Duncan <chris@codecow.com>
Tue, 3 Nov 2026 04:24:17 +0000 (20:24 -0800)
AGENTS.md
src/assembly/tests.ts
test/arithmetic.mjs

index 2966db26f286ab5702a7035bcee40530cd479286..299c365afff315b7ba650f6ceb8d4cc0078b92c2 100644 (file)
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -632,7 +632,7 @@ show up on the clock; only the point arithmetic does.
 `npm test` builds, then runs seven suites: `test/node.mjs` little-endian,
 big-endian and under `--frozen-intrinsics`, then `test:mutants`, `test:module`,
 `test:arithmetic` and `test:heap`. Current state is **6368, 6368, 6228, 17, 12,
-47 and 10 passing, 0 failing**; the frozen run skips the 140 checks under
+80 and 10 passing, 0 failing**; the frozen run skips the 140 checks under
 prototype and static patches, since frozen intrinsics cannot be patched, but
 keeps the 15 under global replacements. `test:heap` reports SKIP and passes
 nothing off Linux. There is no expected failure — if anything fails, it is a regression. `test:all`
@@ -720,11 +720,11 @@ ge_is_canonical build error from "max": expected non-canonical [237,255,…,127]
 ### The `fe_iszero` guard
 
 `fe_iszero` decides whether a decoded point is valid, in the square-root checks
-of `ge_frombytes` and `ge_frombytes_negate_vartime`, and whether a key or `R` is
-small order. Its dangerous failure is reporting zero for a value that is not,
-which would let an invalid point decode. cc8fffd rewrote it from a byte loop
-into two 16-byte loads ORed together and tested with `v128.any_true`, so the
-guard covers every byte of both halves and the reduction in front of them:
+of `ge_frombytes`, which `ge_frombytes_negate` runs too, and whether a key or
+`R` is small order. Its dangerous failure is reporting zero for a value that
+is not, which would let an invalid point decode. cc8fffd rewrote it from a byte
+loop into two 16-byte loads ORed together and tested with `v128.any_true`, so
+the guard covers every byte of both halves and the reduction in front of them:
 
 - **zero**, which must read as zero;
 - **every single bit of every byte**, which must read as nonzero — catches a
@@ -913,17 +913,30 @@ a seeded control: `clear_t()` dropped from `crypto_derive`, and the `INPUT_SIG`
 scrub dropped from the length check in `verify`.
 
 `test:arithmetic` compiles a probe with an extra entry file, embedded in the
-test, that wraps internals over buffers of its own, and compares about 57,600
-results with `test/reference.mjs`: every `fe_*` operation including aliased
-operands, limbs at the bounds ref10 documents and `p + m` as loose limbs; the
-scalar functions over every bit position, dense `2²⁵² ≤ S < 2²⁵³` and the
-neighbourhood of `L`; and decoding, small order, encoding and both scalar
-multiplications over torsion, non-canonical and random points, checking `XY = ZT`
-and `[0]A + [0]B`; the checks `verify()` makes on the key and `R` at their call
-sites; and degenerate points, which both small-order predicates must refuse. Inputs are tight signed limbs, as real callers produce: the
+test, that wraps internals over buffers of its own, and compares about 95,800
+results with `test/reference.mjs`. It covers:
+
+- **every `fe_*` operation**, the paired SIMD ones included, with its operands
+  in every way they can share buffers. Callers alias across lanes: `p2_dbl`
+  squares `r.T` in one lane and writes the other lane's result over it. The one
+  exception is `out = z` in `fe_invert`, which it does not support and no caller
+  passes.
+- **limbs out to ref10's multiplication bound of 1.65·2²⁶**, `p + m` as loose
+  limbs, and `fe_sq_n` at every count its callers use. The harness measures
+  limbs in half-ranges, so that bound is a scale of 3.3; until 2026-11-02 its
+  loose shapes stopped at 1.65, half the bound.
+- **the scalar functions** over every bit position, dense `2²⁵² ≤ S < 2²⁵³` and
+  the neighbourhood of `L`.
+- **decoding, small order and encoding**, the table of odd multiples, and both
+  scalar multiplications, over torsion, non-canonical and random points,
+  checking `XY = ZT` and `[0]A + [0]B`.
+- **the checks `verify()` makes on the key and `R`**, at their call sites.
+- **degenerate points**, which both small-order predicates must refuse.
+
+Point inputs are tight signed limbs, as real callers produce: the
 canonical non-negative form overflows `fe_mul`'s i32 `19·g` lanes on a sum like
 `Y + X`, a false mismatch no caller can trigger. It is seeded, so a failure
-reproduces. Its controls drop `ge_p3_0(r)`, recompute `T` as `X·Y`, and, in one
+reproduces. Its controls drop `p3_0(r)`, recompute `T` as `X·Y`, and, in one
 probe, drop both canonicity checks and the malformed-point term from each of
 the two small-order predicates; each of the six must be seen.
 
index a788be363dd190049217777de4a4b0934902fbd3..e968c811b500f8679df7811531a56492454896e6 100644 (file)
@@ -173,7 +173,7 @@ for (let sign = 0; sign < 2; sign++) {
 /**
  * `fe_iszero` must report zero exactly when a field element is 0 mod `p`. It
  * decides whether a point decodes, in the square-root checks of `ge_frombytes`
- * and `ge_frombytes_negate_vartime`, and whether a key or `R` is small order,
+ * (which `ge_frombytes_negate` runs too), and whether a key or `R` is small order,
  * and its dangerous failure is reporting zero for a value that is not, which
  * would let an invalid point decode. It reduces the value to 32 canonical bytes
  * and tests them as two 16-byte halves, so the cases cover every byte of both
index a971fce84a39c4e77683db322194d136d6eea7b6..8d24008c848139924b87941b16121f7d6e9e98f1 100644 (file)
  * the i32 `19·g` lanes of `fe_mul`. No caller does that, so it would only ever
  * show up as a false mismatch.
  *
- * Positive controls: one probe without the `ge_p3_0(r)` that makes `[0]A + [0]B`
+ * **Every field operation is run with its operands in every arrangement of
+ * shared buffers**, outputs kept apart, because callers alias freely and across
+ * lanes: `p2_dbl` squares `r.T` into one lane and writes the other lane's result
+ * over it. `fe_invert` is the exception; it writes `out` before its last read
+ * of `z`, and no caller passes the same buffer for both.
+ *
+ * Positive controls: one probe without the `p3_0(r)` that makes `[0]A + [0]B`
  * the identity, and one that recomputes `T` as `X·Y` after the final conversion
  * of the double scalar multiplication, must each be seen, and the second must
  * pass the point comparison while failing the representation check. A third
@@ -59,20 +65,32 @@ const asc = new URL('node_modules/assemblyscript/bin/asc.js', root).pathname
 /** Wrappers over dedicated buffers, compiled as the probe's entry file. */
 const HARNESS = `
 import './env'
-import { fe, fe_add, fe_cmov, fe_dbl, fe_frombytes, fe_invert, fe_isnegative, fe_iszero, fe_mul_ab, fe_neg, fe_pow22523, fe_reduce, fe_sq, fe_sub, fe_tobytes } from './ed25519/fe'
-import { ge_double_scalarmult_vartime_to_p3, ge_frombytes, ge_frombytes_negate_vartime, ge_accept_small_order, ge_reject_small_order, ge_is_canonical, ge_scalarmult_base_tobytes } from './ed25519/ge'
-import { ge_cached, ge_p1p1, ge_p3, ge_p3_tobytes, ge_p3_sub_p3 } from './ed25519/p'
+import { FieldElement, fe, fe_0, fe_1, fe_add, fe_addsub, fe_cmov, fe_copy, fe_dbl, fe_frombytes, fe_invert, fe_isnegative, fe_iszero, fe_mul, fe_mul_2d, fe_mul_d, fe_mul_pair, fe_mul_pair_2d, fe_mul_pair_dbl, fe_mul_pair_dbl_2d, fe_mul_pair_sqrtm1, fe_mul_sqrtm1, fe_neg, fe_pow22523, fe_reduce, fe_sq, fe_sq_n, fe_sq_pair, fe_sq_pair_dbl, fe_sub, fe_tobytes } from './ed25519/fe'
+import { ge_cache_odd_multiples, ge_double_scalarmult_vartime_to_p3, ge_frombytes, ge_frombytes_negate, ge_accept_small_order, ge_reject_small_order, ge_is_canonical, ge_scalarmult_base_tobytes } from './ed25519/ge'
+import { p3, p3_sub_p3, p3_tobytes, p_cached } from './ed25519/p'
 import { sc_is_canonical, sc_muladd, sc_reduce } from './ed25519/sc'
 import { verify_pubkey, verify_signature } from './crypto_verify'
 
-const FA = fe(), FB = fe(), FH = fe()
+// operand slots for the field operations, addressed by index so any of them can alias
+const F: StaticArray<FieldElement> = StaticArray.fromArray<FieldElement>([fe(), fe(), fe(), fe(), fe(), fe()])
+const FA = F[0], FB = F[1], FH = F[2]
 const T0 = fe(), T1 = fe(), T2 = fe(), T3 = fe(), T4 = fe(), T5 = fe(), T6 = fe()
 const U8A = new StaticArray<u8>(32), U8B = new StaticArray<u8>(32), U8C = new StaticArray<u8>(32), U8D = new StaticArray<u8>(32)
 const U8T = new StaticArray<u8>(32), U8W = new StaticArray<u8>(64)
-const PA = new ge_p3(), PB = new ge_p3(), PC = new ge_p3()
-const CACHED = new ge_cached(), P1P1 = new ge_p1p1()
+const PA = new p3(), PB = new p3(), PC = new p3(), TP3 = new p3()
+const AI: StaticArray<p_cached> = StaticArray.fromArray<p_cached>([new p_cached(), new p_cached(), new p_cached(), new p_cached(), new p_cached(), new p_cached(), new p_cached(), new p_cached()])
 
 export function ptr (id: i32): usize {
+       if (id >= 40 && id < 46) return changetype<usize>(F[id - 40])
+       if (id >= 100 && id < 132) {
+               const c = AI[(id - 100) >> 2]
+               switch ((id - 100) & 3) {
+                       case 0: return changetype<usize>(c.YplusX)
+                       case 1: return changetype<usize>(c.YminusX)
+                       case 2: return changetype<usize>(c.Z)
+                       case 3: return changetype<usize>(c.T2d)
+               }
+       }
        switch (id) {
                case 0: return changetype<usize>(FA)
                case 1: return changetype<usize>(FB)
@@ -102,39 +120,53 @@ export function fe_tobytes_ (): void { fe_tobytes(U8A, FA, T0) }
 export function fe_reduce_ (): void { fe_reduce(FH, FA) }
 export function fe_iszero_ (): i32 { return fe_iszero(FA, T0, U8T) }
 export function fe_isnegative_ (): i32 { return fe_isnegative(FA, T0) }
-export function fe_mul_ (): void { fe_mul_ab(FH, FA, FB) }
-export function fe_mul_hf (): void { fe_mul_ab(FA, FA, FB) }
-export function fe_mul_hg (): void { fe_mul_ab(FB, FA, FB) }
-export function fe_mul_hfg (): void { fe_mul_ab(FA, FA, FA) }
-export function fe_sq_ (): void { fe_sq(FH, FA) }
-export function fe_sq_hf (): void { fe_sq(FA, FA) }
-export function fe_add_ (): void { fe_add(FH, FA, FB) }
-export function fe_sub_ (): void { fe_sub(FH, FA, FB) }
-export function fe_neg_ (): void { fe_neg(FH, FA) }
-export function fe_dbl_ (): void { fe_dbl(FH, FA) }
-export function fe_cmov_ (b: i32): void { fe_cmov(FA, FB, u64(b)) }
-export function fe_invert_ (): void { fe_invert(FH, FA, T0, T1, T2, T3) }
-export function fe_pow22523_ (): void { fe_pow22523(FH, FA, T0, T1, T2) }
-export function fe_pow22523_hz (): void { fe_pow22523(FA, FA, T0, T1, T2) }
+export function fe_0_ (h: i32): void { fe_0(F[h]) }
+export function fe_1_ (h: i32): void { fe_1(F[h]) }
+export function fe_copy_ (h: i32, f: i32): void { fe_copy(F[h], F[f]) }
+export function fe_add_ (h: i32, f: i32, g: i32): void { fe_add(F[h], F[f], F[g]) }
+export function fe_sub_ (h: i32, f: i32, g: i32): void { fe_sub(F[h], F[f], F[g]) }
+export function fe_addsub_ (a: i32, b: i32, f: i32, g: i32): void { fe_addsub(F[a], F[b], F[f], F[g]) }
+export function fe_neg_ (h: i32, f: i32): void { fe_neg(F[h], F[f]) }
+export function fe_dbl_ (h: i32, f: i32): void { fe_dbl(F[h], F[f]) }
+export function fe_cmov_ (f: i32, g: i32, b: i32): void { fe_cmov(F[f], F[g], u64(b)) }
+export function fe_mul_ (f: i32, a: i32, b: i32): void { fe_mul(F[f], F[a], F[b]) }
+export function fe_mul_d_ (f: i32, a: i32): void { fe_mul_d(F[f], F[a]) }
+export function fe_mul_2d_ (f: i32, a: i32): void { fe_mul_2d(F[f], F[a]) }
+export function fe_mul_sqrtm1_ (f: i32, a: i32): void { fe_mul_sqrtm1(F[f], F[a]) }
+export function fe_mul_pair_ (f0: i32, a0: i32, b0: i32, f1: i32, a1: i32, b1: i32): void { fe_mul_pair(F[f0], F[a0], F[b0], F[f1], F[a1], F[b1]) }
+export function fe_mul_pair_2d_ (f0: i32, a0: i32, b0: i32, f1: i32, a1: i32, b1: i32): void { fe_mul_pair_2d(F[f0], F[a0], F[b0], F[f1], F[a1], F[b1]) }
+export function fe_mul_pair_dbl_ (f0: i32, a0: i32, b0: i32, f1: i32, a1: i32, b1: i32): void { fe_mul_pair_dbl(F[f0], F[a0], F[b0], F[f1], F[a1], F[b1]) }
+export function fe_mul_pair_dbl_2d_ (f0: i32, a0: i32, b0: i32, f1: i32, a1: i32, b1: i32): void { fe_mul_pair_dbl_2d(F[f0], F[a0], F[b0], F[f1], F[a1], F[b1]) }
+export function fe_mul_pair_sqrtm1_ (f0: i32, a0: i32, b0: i32, f1: i32, a1: i32): void { fe_mul_pair_sqrtm1(F[f0], F[a0], F[b0], F[f1], F[a1]) }
+export function fe_sq_ (f: i32, a: i32): void { fe_sq(F[f], F[a]) }
+export function fe_sq_n_ (f: i32, a: i32, n: i32): void { fe_sq_n(F[f], F[a], n) }
+export function fe_sq_pair_ (f0: i32, a0: i32, f1: i32, a1: i32): void { fe_sq_pair(F[f0], F[a0], F[f1], F[a1]) }
+export function fe_sq_pair_dbl_ (f0: i32, a0: i32, f1: i32, a1: i32): void { fe_sq_pair_dbl(F[f0], F[a0], F[f1], F[a1]) }
+export function fe_invert_ (out: i32, z: i32): void { fe_invert(F[out], F[z], T0, T1, T2) }
+export function fe_pow22523_ (out: i32, z: i32): void { fe_pow22523(F[out], F[z], T0, T1, T2) }
 export function sc_reduce_ (): void { sc_reduce(U8W) }
 export function sc_muladd_ (): void { sc_muladd(U8D, U8A, U8B, U8C) }
 export function sc_is_canonical_ (): i32 { return sc_is_canonical(U8A) ? 1 : 0 }
 export function ge_is_canonical_ (): i32 { return ge_is_canonical(U8A) }
 export function ge_frombytes_ (): i32 { return ge_frombytes(PA, U8A, T0, T1, T2, U8T) }
-export function ge_frombytes_negate_vartime_ (): i32 { return ge_frombytes_negate_vartime(PA, U8A, U8T, T0, T1, T2) }
+export function ge_frombytes_negate_ (): i32 { return ge_frombytes_negate(PA, U8A, T0, T1, T2, U8T) }
 export function ge_accept_small_order_ (): i32 { return ge_accept_small_order(PA, T0, U8T) }
 export function ge_reject_small_order_ (): i32 { return ge_reject_small_order(PA, T0, U8T) }
-export function ge_p3_tobytes_ (): void { ge_p3_tobytes(U8B, PA, T0, T1, T2, T3, T4, T5, T6) }
+export function p3_tobytes_ (): void { p3_tobytes(U8B, PA, T0, T1, T2, T3, T4, T5) }
 export function ge_scalarmult_base_tobytes_ (): void { ge_scalarmult_base_tobytes(U8B, U8A, T0, T1, T2, T3, T4, T5, T6) }
-export function ge_double_scalarmult_ (): void { ge_double_scalarmult_vartime_to_p3(PB, U8A, PA, U8B, T0) }
-export function ge_p3_sub_p3_ (): void { ge_p3_sub_p3(PC, PA, PB, T0, CACHED, P1P1) }
+// the table of odd multiples is left in AI for the caller to check, as crypto_verify builds it
+export function ge_double_scalarmult_ (): void {
+       ge_cache_odd_multiples(AI, PA, TP3, T0)
+       ge_double_scalarmult_vartime_to_p3(PB, U8A, AI, U8B)
+}
+export function p3_sub_p3_ (): void { p3_sub_p3(PC, PA, PB, T0) }
 export function verify_pubkey_ (): i32 { return verify_pubkey(U8A) ? 1 : 0 }
 export function verify_signature_ (): i32 { return verify_signature(U8W) ? 1 : 0 }
 `
 
 /** Seeded controls: each `from` must occur once inside its `fn`. */
-const NO_IDENTITY = [{ file: 'src/assembly/ed25519/ge.ts', fn: 'ge_double_scalarmult_vartime_to_p3', from: '\n\tge_p3_0(r)', to: '' }]
-const STALE_T = [{ file: 'src/assembly/ed25519/ge.ts', fn: 'ge_double_scalarmult_vartime_to_p3', from: 'ge_p1p1_to_p3(r, t)', to: 'ge_p1p1_to_p3(r, t)\n\t\t\tfe_mul(r.T, r.X, r.Y)' }]
+const NO_IDENTITY = [{ file: 'src/assembly/ed25519/ge.ts', fn: 'ge_double_scalarmult_vartime_to_p3', from: '\n\tp3_0(r)', to: '' }]
+const STALE_T = [{ file: 'src/assembly/ed25519/ge.ts', fn: 'ge_double_scalarmult_vartime_to_p3', from: 'p1p1_to_p3(r, t)', to: 'p1p1_to_p3(r, t)\n\t\t\tfe_mul(r.T, r.X, r.Y)' }]
 const NO_PRECONDITIONS = [
        { file: 'src/assembly/crypto_verify.ts', fn: 'verify_pubkey', from: 'if (ge_is_canonical(pub) != 0) {', to: 'if (false) {' },
        { file: 'src/assembly/crypto_verify.ts', fn: 'verify_signature', from: 'if (ge_is_canonical(sig) != 0) {', to: 'if (false) {' },
@@ -198,7 +230,11 @@ function tight (v) {
        }
        return limbs
 }
-/** Limbs up to `scale` times their width's half-range, positive or negative. */
+/**
+ * Limbs up to `scale` times their width's half-range, positive or negative. The
+ * half-range is 2²⁵ for a 26-bit limb, so ref10's multiplication bound of
+ * 1.65·2²⁶ is a scale of 3.3, and its 1.1·2²⁵ for encoding is 1.1.
+ */
 const loose = (scale) => WIDTH.map(w => { const b = Math.floor(scale * 2 ** (w - 1)); return Number(randomInt(2 * b + 1)) - b })
 const within = (limbs, scale) => limbs.slice(0, 10).every((l, i) => Math.abs(l) <= scale * 2 ** (WIDTH[i] - 1))
 
@@ -215,13 +251,19 @@ function harness (compiled) {
                setPoint: (base, p) => { const zi = modinv(p[2]), xa = mod(p[0] * zi), ya = mod(p[1] * zi);[xa, ya, 1n, mod(xa * ya)].forEach((c, k) => { const a = limbs(base + k); a.fill(0); a.set(tight(c)) }) },
                setProjective: (base, p) => p.forEach((c, k) => { const a = limbs(base + k); a.fill(0); a.set(tight(c)) }),
                getPoint: (base) => [0, 1, 2, 3].map(k => mod(value(Array.from(limbs(base + k))))),
+               /** Entry `k` of the table of odd multiples, `(Y + X, Y - X, Z, 2dT)`, as extended `(X, Y, Z, T)`. */
+               getCached: (k) => {
+                       const [plus, minus, Z, T2d] = [0, 1, 2, 3].map(f => mod(value(Array.from(limbs(AI + 4 * k + f)))))
+                       return [mod((plus - minus) * HALF), mod((plus + minus) * HALF), Z, mod(T2d * modinv(2n * D))]
+               },
        }
 }
-const [FA, FB, FH, U8A, U8B, U8C, U8D, U8W, PA, PB, PC] = [0, 1, 2, 3, 4, 5, 6, 7, 10, 20, 30]
+const [FA, FB, FH, U8A, U8B, U8C, U8D, U8W, PA, PB, PC, FS, AI] = [0, 1, 2, 3, 4, 5, 6, 7, 10, 20, 30, 40, 100]
+const HALF = modinv(2n)
 
 /** Runs `[a]A + [b]B` and the subtraction `verify()` makes of it, collecting results under `note`. */
 function doubleScalarmult (h, note, count) {
-       const { x, bytes, setPoint, getPoint } = h
+       const { x, bytes, setPoint, getPoint, getCached } = h
        for (let k = 0; k < count; k++) {
                const A = k < 16 ? TORSION[k % 8] : mul(B, randomInt(L))
                const a = k < 48 ? [0n, 1n, L - 1n][k % 3] : randomInt(L)
@@ -230,6 +272,10 @@ function doubleScalarmult (h, note, count) {
                bytes(U8A).set(fromLE(a))
                bytes(U8B).set(fromLE(b))
                x.ge_double_scalarmult_()
+               for (let i = 0; i < 8; i++) {
+                       const entry = getCached(i)
+                       note('ge_cache_odd_multiples: odd multiples, with T consistent', equal(entry, mul(A, BigInt(2 * i + 1))) && isExtended(entry), [k, i])
+               }
                const got = getPoint(PB)
                const want = add(mul(A, a), mul(B, b))
                note('ge_double_scalarmult_vartime_to_p3: point', equal(got, want), k)
@@ -237,8 +283,9 @@ function doubleScalarmult (h, note, count) {
                // R - ([a]A + [b]B), as verify() computes it, with R decoded so Z = 1
                const R = mul(B, randomInt(L))
                setPoint(PA, R)
-               x.ge_p3_sub_p3_()
-               note('ge_p3_sub_p3 of the double scalar multiplication', equal(getPoint(PC), add(R, negate(want))), k)
+               x.p3_sub_p3_()
+               const diff = getPoint(PC)
+               note('p3_sub_p3 of the double scalar multiplication', equal(diff, add(R, negate(want))) && isExtended(diff), k)
        }
        // both scalars zero right after a call that left a point in r
        setPoint(PA, mul(B, 12345n))
@@ -355,7 +402,7 @@ try {
 
        // positive controls
        for (const [name, compiled, expect] of [
-               ['sees [0]A + [0]B left as the previous result without ge_p3_0(r)', noIdentity, { 'ge_double_scalarmult_vartime_to_p3: [0]A + [0]B is the identity': false }],
+               ['sees [0]A + [0]B left as the previous result without p3_0(r)', noIdentity, { 'ge_double_scalarmult_vartime_to_p3: [0]A + [0]B is the identity': false }],
                ['sees T recomputed as X·Y, which the point comparison alone does not', staleT, { 'ge_double_scalarmult_vartime_to_p3: point': true, 'ge_double_scalarmult_vartime_to_p3: XY = ZT': false }],
        ]) {
                if (!(compiled instanceof WebAssembly.Module)) {
@@ -409,11 +456,11 @@ try {
                tightInputs.push(l.slice(0, 10))
        }
        const shapes = []
-       for (const scale of [1.1, 1.65]) {
+       for (const scale of [1.1, 1.65, 3.3]) {
                const max = WIDTH.map(w => Math.floor(scale * 2 ** (w - 1)))
                shapes.push([scale, max], [scale, max.map(v => -v)], [scale, max.map((v, i) => i & 1 ? -v : v)], [scale, max.map((v, i) => i & 1 ? v : -v)])
        }
-       for (let k = 0; k < 400; k++) shapes.push([1.65, loose(1.65)], [1.1, loose(1.1)])
+       for (let k = 0; k < 400; k++) shapes.push([3.3, loose(3.3)], [1.65, loose(1.65)], [1.1, loose(1.1)])
        // p + m with every limb at its maximum: only the final reduction brings it below p
        for (let m = 0; m < 256; m++) shapes.push([1.1, WIDTH.map((w, i) => i === 0 ? 2 ** 26 - 19 + m : 2 ** w - 1)])
 
@@ -432,44 +479,75 @@ try {
 
        const operands = [...tightInputs, ...shapes.map(([, l]) => l)]
        const pick = () => operands[Number(randomInt(operands.length))]
-       for (let k = 0; k < 3000; k++) {
-               const f = k < operands.length ? operands[k] : pick()
-               const g = pick()
-               const vf = value(f)
-               const vg = value(g)
-               set(FA, f); set(FB, g); x.fe_mul_()
-               note('fe_mul', mod(value(get(FH))) === mod(vf * vg) && padded(FH) && within(get(FH), 1.1), [f, g])
-               set(FA, f); set(FB, g); x.fe_mul_hf()
-               note('fe_mul with h = f', mod(value(get(FA))) === mod(vf * vg) && padded(FA), [f, g])
-               set(FA, f); set(FB, g); x.fe_mul_hg()
-               note('fe_mul with h = g', mod(value(get(FB))) === mod(vf * vg) && padded(FB), [f, g])
-               set(FA, f); x.fe_mul_hfg()
-               note('fe_mul with h = f = g', mod(value(get(FA))) === mod(vf * vf) && padded(FA), f)
-               set(FA, f); x.fe_sq_()
-               note('fe_sq', mod(value(get(FH))) === mod(vf * vf) && padded(FH) && within(get(FH), 1.1), f)
-               set(FA, f); x.fe_sq_hf()
-               note('fe_sq with h = f', mod(value(get(FA))) === mod(vf * vf) && padded(FA), f)
-               set(FA, f); x.fe_sq2_()
-               note('fe_sq2', mod(value(get(FH))) === mod(2n * vf * vf) && padded(FH) && within(get(FH), 1.1), f)
-               set(FA, f); set(FB, g); x.fe_add_()
-               note('fe_add', value(get(FH)) === vf + vg && padded(FH), [f, g])
-               set(FA, f); set(FB, g); x.fe_sub_()
-               note('fe_sub', value(get(FH)) === vf - vg && padded(FH), [f, g])
-               set(FA, f); x.fe_neg_()
-               note('fe_neg', value(get(FH)) === -vf && padded(FH), f)
-               set(FA, f); x.fe_dbl_()
-               note('fe_dbl', value(get(FH)) === 2n * vf && padded(FH), f)
-               set(FA, f); set(FB, g); x.fe_cmov_(k & 1)
-               note('fe_cmov', value(get(FA)) === (k & 1 ? vg : vf) && padded(FA), [f, g])
-               if (k < 600) {
-                       set(FA, f); x.fe_invert_()
-                       const inverse = mod(value(get(FH)))
-                       note('fe_invert', mod(vf) === 0n ? inverse === 0n : mod(inverse * vf) === 1n, f)
-                       const e = (1n << 252n) - 3n
-                       set(FA, f); x.fe_pow22523_()
-                       note('fe_pow22523', mod(value(get(FH))) === powmod(vf, e), f)
-                       set(FA, f); x.fe_pow22523_hz()
-                       note('fe_pow22523 with out = z', mod(value(get(FA))) === powmod(vf, e), f)
+       /**
+        * Runs `op` with its slots on the buffers `assign` names, every buffer filled
+        * first, and checks each output against the values the slots held before the
+        * call. `lead`, if given, goes in the last slot's buffer, so the first runs
+        * walk every operand through an input.
+        */
+       const run = (op, name, assign, extra, lead) => {
+               const buffers = Math.max(...assign) + 1
+               const limbs = Array.from({ length: buffers }, (_, b) => b === assign[op.slots - 1] && lead ? lead : pick())
+               limbs.forEach((l, b) => set(FS + b, l))
+               x[`${op.name}_`](...assign, ...(extra == null ? [] : [extra]))
+               const want = op.want(assign.map(b => value(limbs[b])), extra)
+               const ok = op.out.every((s, k) => {
+                       const got = get(FS + assign[s])
+                       return padded(FS + assign[s]) && (op.exact ? value(got) === want[k] : mod(value(got)) === mod(want[k]) && within(got, 1.1))
+               })
+               note(name, ok, `${op.name}(${assign.join(', ')}${extra == null ? '' : `, ${extra}`}) ${JSON.stringify(limbs)}`)
+       }
+       /** Every way to share buffers among `slots` operands with the `out` slots apart, as restricted growth strings. */
+       const patterns = (slots, out) => {
+               const all = []
+               const walk = (p, next) => {
+                       if (p.length === slots) {
+                               if (new Set(out.map(s => p[s])).size === out.length) all.push(p)
+                               return
+                       }
+                       for (let b = 0; b <= next; b++) walk([...p, b], Math.max(next, b + 1))
+               }
+               walk([], 0)
+               return all
+       }
+       const twoD = mod(2n * D)
+       // slots are in parameter order; `want` maps the slots' values to the outputs'
+       const FIELD_OPS = [
+               { name: 'fe_0', slots: 1, out: [0], exact: true, want: () => [0n] },
+               { name: 'fe_1', slots: 1, out: [0], exact: true, want: () => [1n] },
+               { name: 'fe_copy', slots: 2, out: [0], exact: true, want: ([, f]) => [f] },
+               { name: 'fe_add', slots: 3, out: [0], exact: true, want: ([, f, g]) => [f + g] },
+               { name: 'fe_sub', slots: 3, out: [0], exact: true, want: ([, f, g]) => [f - g] },
+               { name: 'fe_addsub', slots: 4, out: [0, 1], exact: true, want: ([, , f, g]) => [f + g, f - g] },
+               { name: 'fe_neg', slots: 2, out: [0], exact: true, want: ([, f]) => [-f] },
+               { name: 'fe_dbl', slots: 2, out: [0], exact: true, want: ([, f]) => [2n * f] },
+               { name: 'fe_cmov', slots: 2, out: [0], exact: true, extra: [0, 1], want: ([f, g], b) => [b ? g : f] },
+               { name: 'fe_mul', slots: 3, out: [0], want: ([, a, b]) => [a * b] },
+               { name: 'fe_mul_d', slots: 2, out: [0], want: ([, a]) => [D * a] },
+               { name: 'fe_mul_2d', slots: 2, out: [0], want: ([, a]) => [twoD * a] },
+               { name: 'fe_mul_sqrtm1', slots: 2, out: [0], want: ([, a]) => [SQRT_M1 * a] },
+               { name: 'fe_mul_pair', slots: 6, out: [0, 3], want: ([, a0, b0, , a1, b1]) => [a0 * b0, a1 * b1] },
+               { name: 'fe_mul_pair_2d', slots: 6, out: [0, 3], want: ([, a0, b0, , a1, b1]) => [a0 * b0, twoD * a1 * b1] },
+               { name: 'fe_mul_pair_dbl', slots: 6, out: [0, 3], want: ([, a0, b0, , a1, b1]) => [2n * a0 * b0, a1 * b1] },
+               { name: 'fe_mul_pair_dbl_2d', slots: 6, out: [0, 3], want: ([, a0, b0, , a1, b1]) => [2n * a0 * b0, twoD * a1 * b1] },
+               { name: 'fe_mul_pair_sqrtm1', slots: 5, out: [0, 3], want: ([, a0, b0, , a1]) => [a0 * b0, SQRT_M1 * a1] },
+               { name: 'fe_sq', slots: 2, out: [0], want: ([, a]) => [a * a] },
+               { name: 'fe_sq_n', slots: 2, out: [0], extra: [1, 2, 3, 4, 5, 10, 20, 50, 100], want: ([, a], n) => [powmod(a, 1n << BigInt(n))] },
+               { name: 'fe_sq_pair', slots: 4, out: [0, 2], want: ([, a0, , a1]) => [a0 * a0, a1 * a1] },
+               { name: 'fe_sq_pair_dbl', slots: 4, out: [0, 2], want: ([, a0, , a1]) => [a0 * a0, 2n * a1 * a1] },
+               { name: 'fe_invert', slots: 2, out: [0], count: 600, patterns: [], want: ([, z]) => [mod(z) === 0n ? 0n : modinv(mod(z))] },
+               { name: 'fe_pow22523', slots: 2, out: [0], count: 600, want: ([, z]) => [powmod(z, (1n << 252n) - 3n)] },
+       ]
+       for (const op of FIELD_OPS) {
+               const distinct = Array.from({ length: op.slots }, (_, s) => s)
+               const count = op.count ?? 3000
+               for (let k = 0; k < count; k++) {
+                       run(op, op.name, distinct, op.extra?.[k % op.extra.length], operands[k])
+               }
+               for (const p of op.patterns ?? patterns(op.slots, op.out)) {
+                       for (let k = 0; k < 8; k++) {
+                               run(op, `${op.name}, every sharing of buffers`, p, op.extra?.[k % op.extra.length])
+                       }
                }
        }
 
@@ -533,9 +611,9 @@ try {
                        note('ge_reject_small_order on decoded points', x.ge_reject_small_order_() === (isSmall(want) ? -1 : 0), e)
                }
                bytes(U8A).set(e)
-               const rn = x.ge_frombytes_negate_vartime_()
+               const rn = x.ge_frombytes_negate_()
                const gn = getPoint(PA)
-               note('ge_frombytes_negate_vartime', want == null ? rn === -1 : rn === 0 && equal(gn, negate(want)) && isExtended(gn), e)
+               note('ge_frombytes_negate', want == null ? rn === -1 : rn === 0 && equal(gn, negate(want)) && isExtended(gn), e)
        }
        // projective representations with Z ≠ 1, including small-order and mixed points
        for (let k = 0; k < 400; k++) {
@@ -546,8 +624,8 @@ try {
                setProjective(PA, p.map(c => mod(c * lambda)))
                note('ge_reject_small_order on projective points', x.ge_reject_small_order_() === (isSmall(p) ? -1 : 0), k)
                setProjective(PA, p.map(c => mod(c * lambda)))
-               x.ge_p3_tobytes_()
-               note('ge_p3_tobytes', toLE(bytes(U8B)) === toLE(encode(p)), k)
+               x.p3_tobytes_()
+               note('p3_tobytes', toLE(bytes(U8B)) === toLE(encode(p)), k)
        }
        // fixed-base multiplication over every signed radix-16 digit at every position
        const scalars = [0n, 1n, 2n, L - 1n, L, L + 1n, (1n << 255n) - 1n, 1n << 254n, 1n << 252n]