]> git.codecow.com Git - nano25519.git/commitdiff
Fix pairwise packing and unpacking.
authorChris Duncan <chris@codecow.com>
Mon, 26 Oct 2026 16:32:45 +0000 (09:32 -0700)
committerChris Duncan <chris@codecow.com>
Mon, 26 Oct 2026 16:32:45 +0000 (09:32 -0700)
src/assembly/crypto_verify.ts
src/assembly/ed25519/ge.ts
src/assembly/ed25519/p2.ts

index a9a9fa52e51abd2c78e4356d40fba50b55c51d91..f21a152580215a09382f011350fe57579a1a2d34 100644 (file)
@@ -61,7 +61,7 @@ export function crypto_verify_donna (sig: StaticArray<u8>, M: StaticArray<u8>, A
        blake2b.init().update(sig, 32).update(pub, KEY_BYTELENGTH).update(M, BLOCKHASH_BYTELENGTH).digest(h)
        sc_reduce(h)
 
-       ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S, t_fe_0, t_fe_1, t_fe_2)
+       ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S)
        ge_p3_tobytes(check_r, sb_ah, t_fe_0, t_fe_1, t_fe_2, t_fe_3, t_fe_4, t_fe_5, t_fe_6)
        clear_t()
 
@@ -89,7 +89,7 @@ export function crypto_verify_sodium (sig: StaticArray<u8>, M: StaticArray<u8>,
        blake2b.init().update(sig, 32).update(pub, 32).update(M, mlen).digest(h)
        sc_reduce(h)
 
-       ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S, t_fe_0, t_fe_1, t_fe_2)
+       ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S)
        ge_sub_p3(check, expected_r, sb_ah, t_fe_0, t_ge_cached, t_ge_p1p1)
 
        // after cofactoring, `check` should be small order and not malformed in order
@@ -123,7 +123,7 @@ export function crypto_verify_strict (sig: StaticArray<u8>, M: StaticArray<u8>,
        blake2b.init().update(sig, 32).update(pub, 32).update(M, mlen).digest(h)
        sc_reduce(h)
 
-       ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S, t_fe_0, t_fe_1, t_fe_2)
+       ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S)
        ge_p3_tobytes(check_r, sb_ah, t_fe_0, t_fe_1, t_fe_2, t_fe_3, t_fe_4, t_fe_5, t_fe_6)
        clear_t()
        return equalbytes(sig, check_r, KEY_BYTELENGTH)
index a89d5226dbb7d205af71b7ee91fa83df6df24796..bdf08273351e87256cb80239684f833ef6c97a07 100644 (file)
@@ -20,9 +20,9 @@
 import { base } from './base'
 import { base2 } from './base2'
 import { FieldElement, fe, fe_0, fe_1, fe_add, fe_cmov, fe_frombytes, fe_isnegative, fe_iszero, fe_mul, fe_neg, fe_pow22523, fe_sq, fe_sub } from './fe'
-import { FieldElementPair, fep, fep_unpack } from './fep'
+import { FieldElementPair, fep, fep_pack, fep_unpack } from './fep'
 import { ge_add_cached, ge_add_precomp, ge_p3_dbl, ge_cached, ge_p1p1, ge_p1p1_0, ge_p1p1_to_p2, ge_p1p1_to_p3, ge_p2, ge_p2_0, ge_p2_dbl, ge_p3, ge_p3_0, ge_p3_to_cached, ge_p3_tobytes, ge_precomp, ge_precomp_0 } from './p'
-import { gepair_add_cached, gepair_add_precomp, gepair_p1p1_to_p2, gepair_p1p1_to_p3, gepair_p2_dbl, gepair_p2_to_fep, gepair_sub_cached, gepair_sub_precomp } from './p2'
+import { gepair_add_cached, gepair_add_precomp, gepair_p1p1_to_p2, gepair_p1p1_to_p3, gepair_p2_dbl, gepair_sub_cached, gepair_sub_precomp } from './p2'
 
 /**
  * √-1
@@ -421,7 +421,7 @@ const ge_double_scalarmult_vartime_uB: FieldElementPair = fep()
  *
  * Variable-time; only used for signature verification.
  */
-export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray<u8>, A: ge_p3, b: StaticArray<u8>, t_fe_0: FieldElement, t_fe_1: FieldElement, t_fe_2: FieldElement): void {
+export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray<u8>, A: ge_p3, b: StaticArray<u8>): void {
        const Bi = base2
        const aslide = ge_double_scalarmult_vartime_aslide
        const bslide = ge_double_scalarmult_vartime_bslide
@@ -477,14 +477,14 @@ export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray<u8>
        ge_p1p1_to_p3(u, t)
        ge_p3_to_cached(Ai[7], u)
 
-       ge_p2_0(p)
-
        let i: i32 = 255
        while (i >= 0 && !aslide[i] && !bslide[i]) {
                i--
        }
 
-       gepair_p2_to_fep(pA, pB, p, t_fe_0)
+       ge_p2_0(p)
+       fep_pack(pA, p.X, p.Y)
+       fep_pack(pB, p.Z, p.X) // T = p.X = 0
 
        while (i >= 0) {
                gepair_p2_dbl(tA, tB, pA, pB)
@@ -510,15 +510,12 @@ export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray<u8>
                if (i > 0) {
                        gepair_p1p1_to_p2(pA, pB, tA, tB)
                } else {
-                       gepair_p1p1_to_p3(rA, rB, tA, tB)
-                       fep_unpack(r.X, r.Y, rA)
-                       fep_unpack(r.Z, r.T, rB)
+                       gepair_p1p1_to_p3(pA, pB, tA, tB)
                }
                i--
        }
-       fe_0(t_fe_0)
-       fe_0(t_fe_1)
-       fe_0(t_fe_2)
+       fep_unpack(r.X, r.Y, pA)
+       fep_unpack(r.Z, r.T, pB)
 }
 
 function slide_vartime (r: StaticArray<i8>, a: StaticArray<u8>): void {
index d54dfe7b7b8e0091c2cf250f0fe6fdc98354eafb..e3e4c79147257afbc6e6aba36603d4acb79ba109 100644 (file)
@@ -42,15 +42,6 @@ export function gepair_p1p1_to_p3 (rA: FieldElementPair, rB: FieldElementPair, p
        fep_mul(rB, rB, gepair_p1p1_to_p3_t) // rB = (Z₁T₁, X₁Y₁)
 }
 
-/**
- * Interleaves (X:Y:Z) projective coordinates as X|Y and X+Y|Z.
- */
-export function gepair_p2_to_fep (a: FieldElementPair, b: FieldElementPair, p: ge_p2, t: FieldElement): void {
-       fe_add(t, p.X, p.Y)
-       fep_pack(a, p.X, p.Y)
-       fep_pack(b, t, p.Z)
-}
-
 const gepair_p2_dbl_t = fep()
 /**
  * `r = 2p`