]> git.codecow.com Git - nano25519.git/commitdiff
Fix test. next/verify-blocks-invert-pair
authorChris Duncan <chris@codecow.com>
Mon, 9 Nov 2026 18:40:27 +0000 (10:40 -0800)
committerChris Duncan <chris@codecow.com>
Mon, 9 Nov 2026 18:40:27 +0000 (10:40 -0800)
test/arithmetic.mjs

index 993e41cb27936a36ece4276e05abc182cf3c7b54..6a6565884fb40f7b691c7f3c1d9d5593fd2999ad 100644 (file)
@@ -67,7 +67,7 @@ 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_to_bytes } from './ed25519/fe'
-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_to_bytes, ge_scalarmult_base_to_bytes_pair } from './ed25519/ge'
+import { ge_cache_odd_multiples, ge_double_scalarmult_vartime_to_bytes, ge_double_scalarmult_vartime_to_bytes_pair, 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_to_bytes, ge_scalarmult_base_to_bytes_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_to_bytes, p3_to_bytes_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'
@@ -78,6 +78,7 @@ const F: StaticArray<FieldElement> = StaticArray.fromArray<FieldElement>([fe(),
 const FA = F[0], FB = F[1], FH = F[2]
 const U8A = new StaticArray<u8>(32), U8B = new StaticArray<u8>(32), U8C = new StaticArray<u8>(32), U8D = new StaticArray<u8>(32)
 const U8W = new StaticArray<u8>(64)
+const U8E = new StaticArray<u8>(32), U8F = new StaticArray<u8>(32), U8G = new StaticArray<u8>(32)
 const PA = new p3(), PB = new p3(), PC = new p3(), A2 = new p3(), CB = new p_cached()
 // one of each representation for the point operations, two where an operation can write over its input
 const R1 = new p1p1(), R1B = new p1p1(), Q2 = new p2(), Q2B = new p2(), CA = new p_cached(), CC = new p_cached(), PP = new p_precomp()
@@ -103,6 +104,9 @@ export function ptr (id: i32): usize {
                case 5: return changetype<usize>(U8C)
                case 6: return changetype<usize>(U8D)
                case 7: return changetype<usize>(U8W)
+               case 8: return changetype<usize>(U8E)
+               case 9: return changetype<usize>(U8F)
+               case 14: return changetype<usize>(U8G)
                case 10: return changetype<usize>(PA.X)
                case 11: return changetype<usize>(PA.Y)
                case 12: return changetype<usize>(PA.Z)
@@ -194,15 +198,21 @@ export function ge_scalarmult_base_to_bytes_pair_ (alias: i32): void { ge_scalar
 export function ge_double_scalarmult_ (): void {
        p3_dbl_to_p3(A2, PA)
        ge_cache_odd_multiples(AI, A2, PA)
-       ge_double_scalarmult_vartime_to_bytes(U8C, U8A, AI, U8B)
+       ge_double_scalarmult_vartime_to_bytes(U8C, U8A, U8B, AI)
+}
+// two double scalar multiplications under the table left by the last call, as
+// crypto_verify_donna_pair runs them: the first pair of scalars in U8A and U8B,
+// encoded to U8C, and the second in U8E and U8F, encoded to U8G
+export function ge_double_scalarmult_pair_ (): void {
+       ge_double_scalarmult_vartime_to_bytes_pair(U8C, U8A, U8B, U8G, U8E, U8F, AI)
 }
 // 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)
+       ge_double_scalarmult_vartime_to_cached(CB, U8A, U8B, AI)
 }
 // 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 {
-       ge_double_scalarmult_vartime_to_cached(CB, U8A, AI, U8B)
+       ge_double_scalarmult_vartime_to_cached(CB, U8A, U8B, AI)
        p3_sub_cached_to_p3(PC, PA, CB)
 }
 // every operation in p.ts, by number; with alias set, the output is also the input it shares a representation with
@@ -237,6 +247,10 @@ export function verify_signature_ (): i32 { return verify_signature(U8W) ? 1 : 0
 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_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)' }]
+// the pair losing its first result while the second is computed, as it did when
+// it parked that result in the scratch the second multiplication's loop writes:
+// both parked points are cleared between the two, whichever holds the first
+const PAIR_SCRATCH = [{ file: 'src/assembly/ed25519/ge/double_scalarmult.ts', fn: 'ge_double_scalarmult_vartime_to_bytes_pair', from: 'ge_double_scalarmult_vartime_to_p1p1(t, a_1, b_1, Ai)', to: 'p3_0(u)\n\tp3_0(v)\n\tge_double_scalarmult_vartime_to_p1p1(t, a_1, b_1, Ai)' }]
 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) {' },
@@ -338,6 +352,7 @@ function harness (compiled) {
        }
 }
 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 [U8E, U8F, U8G] = [8, 9, 14]
 const [R1, R1B, Q2, Q2B, CA, CC, PP, CB] = [50, 60, 70, 80, 90, 94, 140, 150]
 const HALF = modinv(2n)
 
@@ -369,6 +384,15 @@ function doubleScalarmult (h, note, count) {
                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)
+               // the pair, its second scalars derived from the first so that no draw
+               // moves, and both encodings checked: the first result has to survive the
+               // second multiplication, which runs on the same scratch
+               const [a1, b1] = [(a * 3n + 7n) % L, (b * 5n + 11n) % (1n << 253n)]
+               bytes(U8E).set(fromLE(a1))
+               bytes(U8F).set(fromLE(b1))
+               x.ge_double_scalarmult_pair_()
+               const want1 = add(mul(A, a1), mul(B, b1))
+               note('ge_double_scalarmult_vartime_to_bytes_pair', toLE(bytes(U8C)) === toLE(encode(want)) && toLE(bytes(U8G)) === toLE(encode(want1)), 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)
@@ -603,17 +627,19 @@ function check (name, test, detail = '') {
 const started = performance.now()
 await rm(work, { recursive: true, force: true })
 try {
-       const [clean, noIdentity, staleT, noPreconditions, dblOrder] = await Promise.all([
+       const [clean, noIdentity, staleT, noPreconditions, dblOrder, pairScratch] = await Promise.all([
                probe('clean'),
                probe('no-identity', NO_IDENTITY).catch((err) => err),
                probe('stale-t', STALE_T).catch((err) => err),
                probe('no-preconditions', NO_PRECONDITIONS).catch((err) => err),
                probe('dbl-order', DBL_ORDER).catch((err) => err),
+               probe('pair-scratch', PAIR_SCRATCH).catch((err) => err),
        ])
 
        // 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_bytes: [0]A + [0]B is the identity': false, 'ge_double_scalarmult_vartime_to_cached: [0]A + [0]B is the identity': false }],
+               ['sees the pair encode its first result from scratch the second multiplication overwrote', pairScratch, { 'ge_double_scalarmult_vartime_to_bytes': true, 'ge_double_scalarmult_vartime_to_bytes_pair': 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)) {
@@ -912,5 +938,5 @@ try {
 
 console.log(`${passes} \x1b[32mPASSED\x1b[0m`)
 console.log(`${failures} \x1b[31mFAILED\x1b[0m`)
-console.log(`5 builds in ${((performance.now() - started) / 1000).toFixed(1)} s`)
+console.log(`6 builds in ${((performance.now() - started) / 1000).toFixed(1)} s`)
 process.exitCode = failures > 0 ? 1 : 0