]> git.codecow.com Git - nano25519.git/commitdiff
Update tests and agent file.
authorChris Duncan <chris@codecow.com>
Tue, 3 Nov 2026 22:03:57 +0000 (14:03 -0800)
committerChris Duncan <chris@codecow.com>
Tue, 3 Nov 2026 22:03:57 +0000 (14:03 -0800)
AGENTS.md
test/arithmetic.mjs
test/module.mjs

index 299c365afff315b7ba650f6ceb8d4cc0078b92c2..ec28a3ab7ef2eaaeef1a3d8283d2c098d63041aa 100644 (file)
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -117,7 +117,7 @@ every compile. Edit the generator, not its output.
 | `src/assembly/ed25519/{base,base2}.ts` | Precomputed base point tables as static data segments |
 | `src/assembly/ed25519/blake2b.ts` | Generated by `scripts/blake2b-gen.mjs`; do not edit |
 | `src/assembly/ed25519/utils.ts` | `equalbytes` and the other constant-time helpers |
-| `src/assembly/ed25519/t.ts` | Shared temporaries `t_fe_0`–`t_fe_6`, imported only by the `crypto_` functions and passed down — see **Gotchas** |
+| `src/assembly/ed25519/z.ts` | `fe_invert` and `fe_pow22523`, in a module of their own so their four temporaries cannot collide with a caller's — see **Gotchas** |
 | `src/assembly/constants.ts` | Byte lengths and caps; imported by everything, imports nothing |
 | `src/assembly/errors.ts` | Error codes and buffer ids, exported as wasm globals |
 | `src/assembly/env.ts` | The `abort` replacement and `raise()`: errors cross as four integers |
@@ -462,8 +462,9 @@ length too; a scoped fill or a dropped throw-path scrub fails there.
 **Internal scratch holds key material too, and in shapes a scan will miss.**
 The gotcha above is about the *input* buffers of exported functions. The harder
 case is module-level scratch on a secret path: `ge_scalarmult_base` expands its
-scalar into 64 signed radix-16 digits in `ge_scalarmult_base_e`, and
-`ge_cmov8_base` copies the selected base-table entry into `minust`. Neither was
+scalar into 64 signed radix-16 digits in `ge_scalarmult_base_tobytes_e`, and
+`ge_cmov8_base` copies the selected base-table entry into
+`ge_scalarmult_base_tobytes_t` (then called `minust`). Neither was
 cleared, so `derive` left the clamped secret scalar behind and `sign` left the
 nonce `r` — enough, with the signature, to recover the key and forge. Both are
 scrubbed now; the rule is that **any** static a secret passes through gets
@@ -481,29 +482,37 @@ Better still, **diff rather than search**. Build a probe with
 instances, call `clearMemory()`, and compare all of memory: every byte that
 differs is input-dependent, whatever its representation. That is how the last
 leftovers were found — the result point's projective coordinates in
-`ge_scalarmult_base_tobytes` and the inversion chain in `ge_p3_tobytes`, twelve
+`ge_scalarmult_base_tobytes` and the inversion chain in `p3_tobytes` (then `ge_p3_tobytes`), twelve
 field elements a byte search could never have named — and, after 53a72ed,
 verified gone: memory after `derive` and after `sign` was byte-identical across
 different keys on 2026-10-06, and again on 2026-10-09 after 3c9d8e6 moved the
 point temporaries into shared buffers. **`npm run test:module` now runs this diff**
 after `derive`, `sign` and a failed `sign`, and proves it can fire by building a
-second probe with `clear_t()` removed from `crypto_derive`: that leaves 248
+second probe with `z_0()` removed from `fe_invert`: that leaves 144
 input-dependent bytes, which the diff sees and the representation search does
 not. The diff runs after `clearMemory()`, so it cannot see an export that leaves
 its own input buffers to the host; the input check above covers that.
 
-**Shared temporaries are safe only while nothing holds a value across a call
-that uses them.** `t_fe_0` to `t_fe_6` in `src/assembly/ed25519/t.ts` are one
-set of field elements shared by `crypto_derive`, `crypto_sign` and the
-`crypto_verify` functions, and passed down as `t_` parameters. Calls already
-nest: `crypto_sign` runs `crypto_derive`, which overwrites all seven, before it
-uses them for `[r]B`. That is correct today only because `crypto_sign` keeps
-nothing in them across that call. A value parked in a `t_` buffer across any
-call that also takes `t_` buffers is silently clobbered, so pass them down
-explicitly and keep each one's use between calls. Each `crypto_` function
-clears all seven with `clear_t()` when it is done with them, including on its
-early-return paths; the lower functions that use them, such as
-`ge_scalarmult_base_tobytes`, do not.
+**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
+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
+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.
+`z.ts` imports only `fe.ts`; keep it that way, so it never forms a cycle
+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
+`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;
+carry both in the same commit.
 
 **Establish fail-closed defaults before the first early exit.** `verify_blocks`
 runs `OUTPUT_VERIFY.fill(0)` *before* validating the public key, so a batch
@@ -909,7 +918,7 @@ skipped byte 31, or bytes 1 to 31, passed every suite.
 public API cannot show: no input-dependent bytes after `clearMemory()` (see
 **Gotchas**), every export leaving its own input buffers zeroed on all eight
 success and throw paths, and `OUTPUT_VERIFY` holding only `0` and `1`. Each has
-a seeded control: `clear_t()` dropped from `crypto_derive`, and the `INPUT_SIG`
+a seeded control: `z_0()` dropped from `fe_invert`, 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
@@ -970,11 +979,13 @@ 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 parameters are named `t_` plus their type, numbered when there are
-  several (`t_fe_0`, `t_u8x32`, `t_ge_p1p1`), and come last in the parameter
-  list. A static 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.
+- 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()`.
 
 ## Git
 
index 8d24008c848139924b87941b16121f7d6e9e98f1..3cbd33670811dc8484f5730f346614735d0e57d4 100644 (file)
@@ -6,18 +6,17 @@
  * independent BigInt reference in `test/reference.mjs`, below the public API.
  *
  * A probe is compiled from a copy of the sources with memory exported and one
- * extra entry file that wraps internal functions over buffers of its own, never
- * the shared `t_fe_*`. That reaches inputs no signature can: edge values, loose
- * limbs at the bounds ref10 documents, aliased operands, small-order and
- * non-canonical points, and zero scalars.
+ * extra entry file that wraps internal functions over buffers of its own. That
+ * reaches inputs no signature can: edge values, loose limbs at the bounds ref10
+ * documents, aliased operands, small-order and non-canonical points, and zero
+ * scalars.
  *
  * **The checks `verify()` makes before its equation are tested at their call
  * sites.** `verify_pubkey` and `verify_signature` reject non-canonical encodings
  * of the key and of `R`, and no valid signature can carry one, so no public-API
  * test can tell a build without those checks apart. Called directly, they must
  * reject every non-canonical encoding, including the twenty that decode to
- * points of large order and so pass every other check. These two use the shared
- * `t_fe_*` themselves, as they do in `verify()`.
+ * points of large order and so pass every other check.
  *
  * **A degenerate point fails closed at both kinds of call site.** `verify()`
  * accepts when `R - [S]B + [k]A` is small order, through `ge_accept_small_order`,
@@ -65,19 +64,19 @@ 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 { 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 { 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 { p3, p3_sub_p3, p3_tobytes, p_cached } from './ed25519/p'
+import { p3, p3_dbl_to_p3, p3_sub_p3, p3_tobytes, p_cached } from './ed25519/p'
+import { fe_invert, fe_pow22523 } from './ed25519/z'
 import { sc_is_canonical, sc_muladd, sc_reduce } from './ed25519/sc'
 import { verify_pubkey, verify_signature } from './crypto_verify'
 
 // 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 p3(), PB = new p3(), PC = new p3(), TP3 = new p3()
+const U8W = new StaticArray<u8>(64)
+const PA = new p3(), PB = new p3(), PC = new p3(), A2 = 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 {
@@ -116,10 +115,10 @@ export function ptr (id: i32): usize {
        return 0
 }
 export function fe_frombytes_ (): void { fe_frombytes(FH, U8A) }
-export function fe_tobytes_ (): void { fe_tobytes(U8A, FA, T0) }
+export function fe_tobytes_ (): void { fe_tobytes(U8A, FA) }
 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_iszero_ (): i32 { return fe_iszero(FA) }
+export function fe_isnegative_ (): i32 { return fe_isnegative(FA) }
 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]) }
@@ -142,24 +141,25 @@ 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 fe_invert_ (out: i32, z: i32): void { fe_invert(F[out], F[z]) }
+export function fe_pow22523_ (z: i32): void { fe_pow22523(F[z]) }
 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_ (): 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 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_frombytes_ (): i32 { return ge_frombytes(PA, U8A) }
+export function ge_frombytes_negate_ (): i32 { return ge_frombytes_negate(PA, U8A) }
+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) }
+export function ge_scalarmult_base_tobytes_ (): void { ge_scalarmult_base_tobytes(U8B, U8A) }
 // 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)
+       p3_dbl_to_p3(A2, PA)
+       ge_cache_odd_multiples(AI, A2, PA)
        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 p3_sub_p3_ (): void { p3_sub_p3(PC, PA, PB) }
 export function verify_pubkey_ (): i32 { return verify_pubkey(U8A) ? 1 : 0 }
 export function verify_signature_ (): i32 { return verify_signature(U8W) ? 1 : 0 }
 `
@@ -536,7 +536,7 @@ try {
                { 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)] },
+               { name: 'fe_pow22523', slots: 1, 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)
index 8fa7a2c736cca7d00fb294687f529741894b83db..1f2161eb78c323a5bb67bd333d878dee84a9a179 100644 (file)
@@ -26,7 +26,7 @@
  *
  * Positive controls prove each check can fire: the search finds the public key
  * before `clearMemory()`, the diff sees the public key output, a second probe
- * with `clear_t()` removed from `crypto_derive` must show residue, a third with
+ * with `z_0()` removed from `fe_invert` must show residue, a third with
  * the `INPUT_SIG` scrub removed from the length check in `verify` must fail the
  * input check there and nowhere else, and both result values must actually
  * occur.
@@ -47,7 +47,7 @@ const work = new URL('build/module/', root)
 const asc = new URL('node_modules/assemblyscript/bin/asc.js', root).pathname
 
 /** The line a dropped scrub removes; `from` must occur once inside `fn`. */
-const DROPPED_SCRUB = { file: 'src/assembly/crypto_derive.ts', fn: 'crypto_derive', from: '\n\tclear_t()' }
+const DROPPED_SCRUB = { file: 'src/assembly/ed25519/z.ts', fn: 'fe_invert', from: '\n\tz_0()' }
 
 /** A throw-path scrub that only the input-buffer check can see. */
 const DROPPED_INPUT_SCRUB = { file: 'src/assembly/index.ts', fn: 'verify', from: '\n\t\tINPUT_SIG.fill(0)' }
@@ -255,7 +255,7 @@ try {
        check('diff sees the public key output before clearMemory()',
                differing(run(clean, 'derive', keyOf(1)).before, run(clean, 'derive', keyOf(2)).before) > 0)
        const seeded = dropped instanceof WebAssembly.Module ? residue(dropped, 'derive') : 0
-       check('diff sees the residue left by a crypto_derive without clear_t()', seeded > 0,
+       check('diff sees the residue left by an fe_invert without z_0()', seeded > 0,
                dropped instanceof WebAssembly.Module ? `${seeded} bytes` : String(dropped?.message ?? dropped))
 
        const sec = secrets(keyOf(1))