]> git.codecow.com Git - nano25519.git/commitdiff
Improve doc comments, fix test, and update agent file.
authorChris Duncan <chris@codecow.com>
Mon, 9 Nov 2026 14:31:14 +0000 (06:31 -0800)
committerChris Duncan <chris@codecow.com>
Mon, 9 Nov 2026 14:31:14 +0000 (06:31 -0800)
AGENTS.md
src/assembly/crypto_sign.ts
src/assembly/ed25519/p.ts
test/arithmetic.mjs

index 64398b70dfc46bc5665962231ed5f5d3aceffc84..c895f058a2fa5b34828ba19bb7ded8b8249ebad9 100644 (file)
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -68,11 +68,20 @@ multiplication, which is a different algorithm entirely.
 **`sign` costs two fixed-base scalar multiplications, deliberately.** It derives
 the public key from the private key and throws `Invalid public key` if the one
 passed in disagrees. That is RFC 8032 §5.1.6's definition of signing, and the
-comparison buys keypair integrity. It roughly halves signing throughput: one
-derivation is ~19 µs of a ~39 µs signature. **This is the committed shape — do
-not "optimise" it away.**
-Removing the derivation would break RFC conformance; removing the comparison
-would silently accept a mismatched keypair.
+comparison buys keypair integrity. **This is the committed shape — do not
+"optimise" it away.** Removing the derivation would break RFC conformance;
+removing the comparison would silently accept a mismatched keypair.
+
+What it costs is now the multiplication alone. Each fixed-base multiplication
+ends in an inversion to encode its point, ~5.1 µs of its ~16.5 µs, so
+`crypto_sign` calls `ge_scalarmult_base_tobytes_pair`, which computes `[s]B` and
+`[r]B` and encodes both with `p3_tobytes_pair`, sharing one inversion between
+them (Montgomery's trick). The private key is hashed once for both. Measured
+2026-11-08 at module level, best of 9, interleaved: `sign` went from 34.4 to
+29.3 µs, and the derivation's share is ~11 µs of it. The price is order: the
+keypair is checked after the nonce work rather than before, so a mismatched key
+costs a whole signature to refuse. Both paths leave through one scrub of `A`,
+`R`, `S`, `k`, `s` and `r`, and only a match writes `RS`.
 
 ## Commands
 
@@ -496,11 +505,13 @@ its own input buffers to the host; the input check above covers that.
 
 **Each module owns its scratch, and each function clears what it used.**
 There are no shared temporaries passed down as parameters any more. `fe.ts`
-has `t_fe` and `t_u8x32`, `p.ts` has `t_p3`, `z.ts` has `a`, `b`, `c` and
-`t`, and `ge.ts` gives each function statics named after it. A value held
+has `t_fe` and `t_u8x32`, `p.ts` has the `p1p1` `t`, `z.ts` has `a`, `b`, `c`
+and `t`, `scalarmult_base.ts` has `e`, `s`, `t`, `u` and `v`,
+`double_scalarmult.ts` has `t` and `u`, and `ge.ts` gives each function statics
+named after it. A value held
 in a module's scratch is clobbered by any call back into that module, so a
 function may call *down* while holding its scratch but never into a function
-that uses the same buffers. `p3_tobytes` hands `t_p3.T` to `fe_invert` as its
+that uses the same buffers. `p3_tobytes` hands `t.Z` to `fe_invert` as its
 output; that works only because the inversion runs on `z.ts`'s buffers, which
 is why it lives in a module of its own. Inside that module, `fe_invert`
 relies on `fe_pow22501` leaving `z¹¹` in `t`, while `fe_pow22523` overwrites it.
@@ -509,7 +520,12 @@ with `p.ts`, which imports it.
 
 A function on a secret path clears its scratch before it returns, since
 anything left there is residue: `fe_0(t_fe)`, `p3_0(t_p3)`, `z_0()`, and the
-`_0` calls at the end of `ge_scalarmult_base_tobytes`. When `t.ts` went, its
+`_0` calls at the end of `ge_scalarmult_base`. That function clears its own
+scratch but leaves its result point to the caller, which must clear it once
+encoded, so it is private to its module, and its two callers,
+`ge_scalarmult_base_tobytes` and `ge_scalarmult_base_tobytes_pair`, clear `u`
+and `v` after encoding; `test:module` reports the nonce point's 145 bytes if the
+pair's clear of `v` is dropped. When `t.ts` went, its
 `clear_t()` calls went with it, and nothing replaced them in what is now
 `z.ts`: `derive` and `sign` left 144 bytes of the inversion chain
 behind until `test:module` failed on it. Moving a buffer moves its scrub;
@@ -555,8 +571,12 @@ libsodium master, which still has it; releases are cofactorless and unaffected.
 The fix converts the final `p1p1` straight to `p3`. Whenever a point leaves a
 function in one representation and enters another, check the representation's
 own invariant, not just the point: `test:arithmetic` asserts `XY = ZT` on every
-`p3` it reads back, and its control recomputes `T` as `X·Y` to prove a point
-comparison alone misses it.
+`p3` and cached point it reads back, and its control rebuilds the cached result
+of the double scalar multiplication through a `p3` whose `T` is recomputed as
+`X·Y`, to prove a point comparison alone misses it. That control lives on the
+cached path because that is the one carrying `T` out of the module: the encoded
+result, `ge_double_scalarmult_vartime_to_bytes`, never reads `T`, so a wrong one
+there changes nothing a caller can see.
 
 **Small order means *valid* at one call site and *reject* at the others, so
 each needs its own predicate.** On a well-formed point, `X = 0`, `Y = 0` or
@@ -634,7 +654,11 @@ Useful scale anchors, measured 2026-10-06 in Node 24 on a Ryzen 5800X: a wasm
 call boundary is ~2 ns, a 32 KiB `memory.fill` is ~0.1 µs, one `derive` is
 ~19 µs, one `sign` is ~39 µs, one `verify` is ~71 µs with `Uint8Array` inputs
 and ~74 µs with hex strings, and the hoisted per-key work is ~6.2 µs (measured
-earlier). Buffer scrubbing and boundary crossing never
+earlier). Those are through the public API. Timed at module level on
+2026-11-08, `derive` is 16.6 µs and `sign` 29.3 µs, the latter after its two
+encodings came to share an inversion. Inside one fixed-base multiplication, 64 additions of a table entry
+are ~56%, the inversion ~31%, the 64 constant-time table scans ~8% and the four
+doublings ~3%. Buffer scrubbing and boundary crossing never
 show up on the clock; only the point arithmetic does.
 
 ## Testing
@@ -642,7 +666,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 **6403, 6403, 6263, 17, 16,
-109 and 10 passing, 0 failing**; the frozen run skips the 140 checks under
+115 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`
@@ -956,7 +980,7 @@ its own code and details, and `setInputPrv` must ignore everything above each
 the `setInputMsg` guard moved 32 bytes late and the key halves widened to `u32`.
 
 `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 103,500
+test, that wraps internals over buffers of its own, and compares about 108,000
 results with `test/reference.mjs`. It covers:
 
 - **every `fe_*` operation**, the paired SIMD ones included, with its operands
@@ -970,8 +994,10 @@ results with `test/reference.mjs`. It covers:
   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,
+- **decoding, small order and encoding**, including two points encoded with
+  one shared inversion, each in its own scaling; the table of odd multiples;
+  and both scalar multiplications, the fixed-base one singly and paired, and
+  the double one both encoded and as a cached point, 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.
@@ -985,7 +1011,8 @@ results with `test/reference.mjs`. It covers:
 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 make `p1p1_0` leave `T = 0`, recompute `T` as `X·Y`, and, in one
+reproduces. Its controls make `p1p1_0` leave `T = 0`, recompute `T` as `X·Y` in
+the cached result of the double scalar multiplication, 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. A seventh
 reorders `dbl_to_p1p1` to square its inputs before summing them, which is
@@ -1023,13 +1050,15 @@ gone by the time the scan reads it, so a clean result there means nothing.
   header and its original copyright line.
 - Use real mathematical symbols in comments (`≤`, `²⁵⁵`) — the codebase already
   does, and it reads against the RFC.
-- Scratch is module-level, never a parameter. A static shared within its
-  module is named `t_` plus its type (`t_fe`, `t_u8x32`, `t_p3`); one that only
-  one function uses is named after it (`ge_frombytes_u`,
-  `ge_scalarmult_base_tobytes_h`), so a shared buffer is never mistaken for a
-  private one. `z.ts` is the exception: the whole module is one computation,
-  so its four buffers are plain `a`, `b`, `c` and `t`, cleared together by
-  `z_0()`.
+- Scratch is module-level, never a parameter. Statics take short names:
+  in the `crypto_*` files, the symbol the value has in RFC 8032 (`A`, `R`, `k`,
+  `S`, `r`, `h`, `prefix`); in the group and field modules, plain temporaries
+  (`t`, `u`, `v`, and `a`, `b`, `c` in `z.ts`). Two older conventions survive,
+  statics named after the one function that uses them in `ge.ts`
+  (`ge_frombytes_u`) and a `t_` type prefix in `fe.ts` (`t_fe`, `t_u8x32`), and
+  are not the pattern to follow. A short name says nothing about which
+  functions share a buffer, so check the rule under **Gotchas** (*Each module
+  owns its scratch*) before calling across one.
 
 ## Git
 
index 4f0570b99313f5a9c3ba93e1e6dbcd3bca8deabf..42c4c245d392a912aff781188369f7ab893af679 100644 (file)
@@ -31,8 +31,8 @@ const S = new StaticArray<u8>(64)
  *
  * The derivation and the nonce point `R` are both fixed-base multiplications,
  * and each ends in an expensive inversion to encode its point. Both points are
- * computed first and encoded together so that they share one inversion and save
- * about a third of the cost of encoding. The public key check runs after the
+ * computed first and encoded together so that they share one inversion, which
+ * saves nearly all of one encoding. The public key check runs after the
  * nonce work (instead of before) because the costly computation is already
  * done, and the failure path clears everything.
  *
index 6485362e5691bb5493bd6c005735a06b34ecf068..86faca0f43129b5f1db1df051f35806ba5b45ce0 100644 (file)
@@ -394,9 +394,15 @@ export function p3_sub_precomp (r: p1p1, p: p3, q: p_precomp): void {
  * Encodes `p` as 32 bytes where bits 0 to 254 are `y` little-endian and bit 255
  * is the sign of `x` which is its least significant bit.
  *
+ * The `(X:Y:Z:T)` components from the `p3` input can be anything; in this
+ * implementation, every call site first calls `p1p1_to_p3` to create the `p3`
+ * representation, so the `Z` component passed here function always represents
+ * the product of the completed point's two denominators (`Z₁T₁`). Thus,
+ * dividing by `Z` recovers `x = Xₚ₁ₚ₁/Zₚ₁ₚ₁` and `y = Yₚ₁ₚ₁/Tₚ₁ₚ₁`.
+ *
  * ```
- * x = X/Z = X₁T₁ / Z₁T₁ = X₁/Z₁
- * y = Y/Z = Y₁Z₁ / Z₁T₁ = Y₁/T₁
+ * x = Xₚ₃/Zₚ₃ = (Xₚ₁ₚ₁Tₚ₁ₚ₁)/(Zₚ₁ₚ₁Tₚ₁ₚ₁) = Xₚ₁ₚ₁/Zₚ₁ₚ₁
+ * y = Yₚ₃/Zₚ₃ = (Yₚ₁ₚ₁Zₚ₁ₚ₁)/(Zₚ₁ₚ₁Tₚ₁ₚ₁) = Yₚ₁ₚ₁/Tₚ₁ₚ₁
  * s = y + 2²⁵⁵(x mod 2)
  * ```
  */
index 0a698d8a0d6368ca0215d8a256fb0f2f60bb0dfb..b8e3596e800c8b569e7b94a39a1988ca157765bc 100644 (file)
  * of `z`, and no caller passes the same buffer for both.
  *
  * Positive controls: one probe whose `p1p1_0` leaves `T = 0`, so `[0]A + [0]B`
- * is not 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
+ * is not the identity, and one that builds the cached result of the double
+ * scalar multiplication through a `p3` whose `T` is recomputed as `X·Y`, must
+ * each be seen, and the second must pass the point comparison while failing the
+ * representation check. The encoded result cannot show that one: encoding never
+ * reads `T`. A third
  * probe drops both canonicity checks and the malformed-point term from each of
  * the two small-order predicates, and each of the four must be seen.
  *
@@ -65,11 +67,10 @@ const asc = new URL('node_modules/assemblyscript/bin/asc.js', root).pathname
 const HARNESS = `
 import './env'
 import { FieldElement, fe, fe_0, fe_1, fe_add, fe_addsub, fe_cmov, fe_copy, fe_dbl, fe_frombytes, 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_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 { p1p1, p1p1_0, p1p1_dbl_to_p1p1, p1p1_to_cached, p1p1_to_p2, p1p1_to_p3, p2, p2_0, p2_dbl_to_p2, p2_dbl_to_p3, p3, p3_0, p3_add_cached, p3_add_cached_to_cached, p3_add_precomp, p3_dbl_to_p2, p3_dbl_to_p3, p3_sub_cached, p3_sub_cached_to_p3, p3_sub_precomp, p3_to_cached, p3_tobytes, p_cached, p_precomp, precomp_0 } from './ed25519/p'
+import { ge_cache_odd_multiples, ge_double_scalarmult_vartime_to_bytes, ge_double_scalarmult_vartime_to_cached, ge_frombytes, ge_frombytes_negate, ge_accept_small_order, ge_reject_small_order, ge_is_canonical, ge_scalarmult_base_tobytes, ge_scalarmult_base_tobytes_pair } from './ed25519/ge'
+import { p1p1, p1p1_0, p1p1_dbl_to_p1p1, p1p1_to_cached, p1p1_to_p2, p1p1_to_p3, p2, p2_0, p2_dbl_to_p2, p2_dbl_to_p3, p3, p3_0, p3_add_cached, p3_add_cached_to_cached, p3_add_precomp, p3_dbl_to_p2, p3_dbl_to_p3, p3_sub_cached, p3_sub_cached_to_p3, p3_sub_precomp, p3_to_cached, p3_tobytes, p3_tobytes_pair, p_cached, p_precomp, precomp_0 } from './ed25519/p'
 import { fe_invert, fe_pow22523 } from './ed25519/z'
 import { sc_is_canonical, sc_muladd, sc_reduce } from './ed25519/sc'
-import { ge_double_scalarmult_vartime_to_cached } from './ed25519/ge/double_scalarmult'
 import { verify_pubkey, verify_signature } from './crypto_verify'
 
 // operand slots for the field operations, addressed by index so any of them can alias
@@ -114,6 +115,10 @@ export function ptr (id: i32): usize {
                case 31: return changetype<usize>(PC.Y)
                case 32: return changetype<usize>(PC.Z)
                case 33: return changetype<usize>(PC.T)
+               case 150: return changetype<usize>(CB.YplusX)
+               case 151: return changetype<usize>(CB.YminusX)
+               case 152: return changetype<usize>(CB.Z)
+               case 153: return changetype<usize>(CB.T2d)
                case 50: return changetype<usize>(R1.X)
                case 51: return changetype<usize>(R1.Y)
                case 52: return changetype<usize>(R1.Z)
@@ -180,12 +185,20 @@ export function ge_frombytes_negate_ (): i32 { return ge_frombytes_negate(PA, U8
 export function ge_accept_small_order_ (): i32 { return ge_accept_small_order(PA) }
 export function ge_reject_small_order_ (): i32 { return ge_reject_small_order(PA) }
 export function p3_tobytes_ (): void { p3_tobytes(U8B, PA) }
+// with alias set, both points are the same buffer
+export function p3_tobytes_pair_ (alias: i32): void { p3_tobytes_pair(U8B, PA, U8C, alias ? PA : PB) }
 export function ge_scalarmult_base_tobytes_ (): void { ge_scalarmult_base_tobytes(U8B, U8A) }
+// with alias set, both scalars are the same buffer
+export function ge_scalarmult_base_tobytes_pair_ (alias: i32): void { ge_scalarmult_base_tobytes_pair(U8B, U8A, U8C, alias ? U8A : U8D) }
 // 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 {
        p3_dbl_to_p3(A2, PA)
        ge_cache_odd_multiples(AI, A2, PA)
-       ge_double_scalarmult_vartime_to_p3(PB, U8A, AI, U8B)
+       ge_double_scalarmult_vartime_to_bytes(U8C, U8A, AI, U8B)
+}
+// the same scalars and table into a cached point, as crypto_verify_sodium takes it
+export function ge_double_scalarmult_to_cached_ (): void {
+       ge_double_scalarmult_vartime_to_cached(CB, U8A, AI, U8B)
 }
 // as crypto_verify_sodium does it: the same scalars and table into a cached point, subtracted from R in PA
 export function p3_sub_cached_to_p3_ (): void {
@@ -222,7 +235,7 @@ export function verify_signature_ (): i32 { return verify_signature(U8W) ? 1 : 0
 
 /** Seeded controls: each `from` must occur once inside its `fn`; an optional `import` line is added to the file. */
 const NO_IDENTITY = [{ file: 'src/assembly/ed25519/p.ts', fn: 'p1p1_0', from: 'fe_1(h.T)', to: 'fe_0(h.T)' }]
-const STALE_T = [{ file: 'src/assembly/ed25519/ge/double_scalarmult.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)', import: "import { fe_mul } from '../fe'" }]
+const STALE_T = [{ file: 'src/assembly/ed25519/ge/double_scalarmult.ts', fn: 'ge_double_scalarmult_vartime_to_cached', from: 'p1p1_to_cached(r, t)', to: 'p1p1_to_p3(u, t)\n\tfe_mul(u.T, u.X, u.Y)\n\tp3_to_cached(r, u)', import: "import { fe_mul } from '../fe'\nimport { p3_to_cached } from '../p'" }]
 const DBL_ORDER = [{ file: 'src/assembly/ed25519/p.ts', fn: 'dbl_to_p1p1', from: 'fe_add(r.X, X, Y) // X = X₁+Y₁\n\tfe_sq_pair_dbl(r.X, r.X, r.T, Z) // X = (X₁+Y₁)², T = 2Z₁²\n\tfe_sq_pair(r.Z, X, r.Y, Y) // Z = X₁², Y = Y₁²', to: 'fe_sq_pair(r.Z, X, r.Y, Y)\n\tfe_add(r.X, X, Y)\n\tfe_sq_pair_dbl(r.X, r.X, r.T, Z)' }]
 const NO_PRECONDITIONS = [
        { file: 'src/assembly/crypto_verify.ts', fn: 'verify_pubkey', from: 'if (ge_is_canonical(pub) != 0) {', to: 'if (false) {' },
@@ -317,20 +330,27 @@ function harness (compiled) {
                getPoint: (base) => [0, 1, 2, 3].map(k => mod(value(Array.from(limbs(base + k))))),
                /** `n` coordinates from `base` as values, or `null` if any has dirty padding limbs. */
                getCoords: (base, n) => Array.from({ length: n }, (_, k) => limbs(base + k)).every(a => a[10] === 0 && a[11] === 0) ? Array.from({ length: n }, (_, k) => mod(value(Array.from(limbs(base + k))))) : null,
-               /** 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)))))
+               /** A cached point, `(Y + X, Y - X, Z, 2dT)`, as extended `(X, Y, Z, T)`. */
+               getCachedAt: (base) => {
+                       const [plus, minus, Z, T2d] = [0, 1, 2, 3].map(f => mod(value(Array.from(limbs(base + 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, FS, AI] = [0, 1, 2, 3, 4, 5, 6, 7, 10, 20, 30, 40, 100]
-const [R1, R1B, Q2, Q2B, CA, CC, PP] = [50, 60, 70, 80, 90, 94, 140]
+const [R1, R1B, Q2, Q2B, CA, CC, PP, CB] = [50, 60, 70, 80, 90, 94, 140, 150]
 const HALF = modinv(2n)
 
-/** Runs `[a]A + [b]B` and the subtraction `verify()` makes of it, collecting results under `note`. */
+/**
+ * Runs `[a]A + [b]B` both ways it leaves the module: encoded, as
+ * `verify_blocks` compares it, and as a cached point, which is checked as a
+ * representation too, since only `T` can be wrong in one that compares equal.
+ * Then the subtraction `verify()` makes of the cached point. Collects results
+ * under `note`.
+ */
 function doubleScalarmult (h, note, count) {
-       const { x, bytes, setPoint, getPoint, getCached } = h
+       const { x, bytes, setPoint, getPoint, getCachedAt } = h
+       const getCached = (k) => getCachedAt(AI + 4 * k)
        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)
@@ -343,10 +363,12 @@ function doubleScalarmult (h, note, count) {
                        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)
-               note('ge_double_scalarmult_vartime_to_p3: XY = ZT', isExtended(got), k)
+               note('ge_double_scalarmult_vartime_to_bytes', toLE(bytes(U8C)) === toLE(encode(want)), k)
+               x.ge_double_scalarmult_to_cached_()
+               const got = getCachedAt(CB)
+               note('ge_double_scalarmult_vartime_to_cached: point', equal(got, want), k)
+               note('ge_double_scalarmult_vartime_to_cached: XY = ZT', isExtended(got), k)
                // R - ([a]A + [b]B), as verify() computes it, with R decoded so Z = 1
                const R = mul(B, randomInt(L))
                setPoint(PA, R)
@@ -362,8 +384,10 @@ function doubleScalarmult (h, note, count) {
        bytes(U8A).set(fromLE(0n))
        bytes(U8B).set(fromLE(0n))
        x.ge_double_scalarmult_()
-       const zero = getPoint(PB)
-       note('ge_double_scalarmult_vartime_to_p3: [0]A + [0]B is the identity', equal(zero, IDENTITY) && isExtended(zero), 0)
+       note('ge_double_scalarmult_vartime_to_bytes: [0]A + [0]B is the identity', toLE(bytes(U8C)) === toLE(encode(IDENTITY)), 0)
+       x.ge_double_scalarmult_to_cached_()
+       const zero = getCachedAt(CB)
+       note('ge_double_scalarmult_vartime_to_cached: [0]A + [0]B is the identity', equal(zero, IDENTITY) && isExtended(zero), 0)
 }
 
 /**
@@ -589,8 +613,8 @@ try {
 
        // positive controls
        for (const [name, compiled, expect] of [
-               ['sees [0]A + [0]B as a malformed point when p1p1_0 is not the identity', 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 }],
+               ['sees [0]A + [0]B as a malformed point when p1p1_0 is not the identity', noIdentity, { 'ge_double_scalarmult_vartime_to_bytes: [0]A + [0]B is the identity': false, 'ge_double_scalarmult_vartime_to_cached: [0]A + [0]B is the identity': false }],
+               ['sees T recomputed as X·Y in the cached point, which the point comparison alone does not', staleT, { 'ge_double_scalarmult_vartime_to_cached: point': true, 'ge_double_scalarmult_vartime_to_cached: XY = ZT': false }],
        ]) {
                if (!(compiled instanceof WebAssembly.Module)) {
                        check(`control ${name}`, false, String(compiled?.message ?? compiled))
@@ -830,6 +854,25 @@ try {
                x.p3_tobytes_()
                note('p3_tobytes', toLE(bytes(U8B)) === toLE(encode(p)), k)
        }
+       // two points sharing one inversion, each in its own scaling, so a swapped
+       // Z₀⁻¹ and Z₁⁻¹ cannot cancel, and once the same point in the same buffer.
+       // Draws from its own generator, so the inputs below do not move.
+       {
+               const draw = generator('nano25519 pair encoding')
+               const scale = () => toLE(draw(32)) % (P - 1n) + 1n
+               const point = (k) => k < 16 ? TORSION[k % 8] : k < 32 ? add(mul(B, toLE(draw(32)) % L), TORSION[k % 8]) : mul(B, toLE(draw(32)) % L)
+               for (let k = 0; k < 300; k++) {
+                       const p0 = point(k), p1 = point((k * 7 + 3) % 300)
+                       const [l0, l1] = [scale(), scale()]
+                       setProjective(PA, p0.map(c => mod(c * l0)))
+                       setProjective(PB, p1.map(c => mod(c * l1)))
+                       x.p3_tobytes_pair_(0)
+                       note('p3_tobytes_pair', toLE(bytes(U8B)) === toLE(encode(p0)) && toLE(bytes(U8C)) === toLE(encode(p1)), k)
+                       setProjective(PA, p0.map(c => mod(c * l0)))
+                       x.p3_tobytes_pair_(1)
+                       note('p3_tobytes_pair of one point twice', toLE(bytes(U8B)) === toLE(encode(p0)) && toLE(bytes(U8C)) === toLE(encode(p0)), 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]
        for (const v of [0x88, 0x77, 0x08, 0x80, 0x7f, 0x11]) scalars.push(toLE(new Uint8Array(32).fill(v)) & ((1n << 255n) - 1n))
@@ -840,6 +883,18 @@ try {
                x.ge_scalarmult_base_tobytes_()
                note('ge_scalarmult_base_tobytes', toLE(bytes(U8B)) === toLE(encode(mul(B, a))), a)
        }
+       // the pair, each scalar beside another from the same list, and once with
+       // both read from one buffer, since the two multiplications share the digit
+       // buffer and the first clears it before the second expands its own scalar
+       for (const [i, a0] of scalars.entries()) {
+               const a1 = scalars[(i * 7 + 3) % scalars.length]
+               bytes(U8A).set(fromLE(a0))
+               bytes(U8D).set(fromLE(a1))
+               x.ge_scalarmult_base_tobytes_pair_(0)
+               note('ge_scalarmult_base_tobytes_pair', toLE(bytes(U8B)) === toLE(encode(mul(B, a0))) && toLE(bytes(U8C)) === toLE(encode(mul(B, a1))), [a0, a1])
+               x.ge_scalarmult_base_tobytes_pair_(1)
+               note('ge_scalarmult_base_tobytes_pair of one scalar twice', toLE(bytes(U8B)) === toLE(encode(mul(B, a0))) && toLE(bytes(U8C)) === toLE(encode(mul(B, a0))), a0)
+       }
        doubleScalarmult(h, note, 400)
        preconditions(h, note)
        degenerate(h, note)