]> git.codecow.com Git - nano25519.git/commitdiff
Improve small-order check semantics.
authorChris Duncan <chris@codecow.com>
Fri, 16 Oct 2026 21:00:29 +0000 (14:00 -0700)
committerChris Duncan <chris@codecow.com>
Fri, 16 Oct 2026 21:00:29 +0000 (14:00 -0700)
src/assembly/crypto_verify.ts
src/assembly/ed25519/ge.ts
test/arithmetic.mjs

index 817940fb73112dd30f1151da3aae98be7d823fb2..5bd47d332e443e2173f21b607bbd64e3a8850b50 100644 (file)
@@ -3,7 +3,7 @@
 
 import { BLOCKHASH_BYTELENGTH, KEY_BYTELENGTH } from './constants'
 import { Blake2b } from './ed25519/blake2b'
-import { ge_double_scalarmult_vartime_to_p3, ge_frombytes, ge_frombytes_negate_vartime, ge_has_small_order, ge_is_canonical } from './ed25519/ge'
+import { ge_double_scalarmult_vartime_to_p3, ge_frombytes, ge_frombytes_negate_vartime, ge_is_small_order_point, ge_is_canonical } from './ed25519/ge'
 import { ge_cached, ge_p1p1, ge_p3, ge_p3_tobytes, ge_sub_p3 } from './ed25519/p'
 import { sc_is_canonical, sc_reduce } from './ed25519/sc'
 import { clear_t, t_fe_0, t_fe_1, t_fe_2, t_fe_3, t_fe_4, t_fe_5, t_fe_6 } from './ed25519/t'
@@ -92,7 +92,9 @@ export function crypto_verify_sodium (sig: StaticArray<u8>, M: StaticArray<u8>,
        ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S, t_fe_0)
        ge_sub_p3(check, expected_r, sb_ah, t_fe_0, t_ge_cached, t_ge_p1p1)
 
-       const b = ge_has_small_order(check, t_fe_0, t_u8x32)
+       // after cofactoring, `check` should be small order to pass, unlike
+       // `verify_pubkey()` and `verify_signature()` below
+       const b = ge_is_small_order_point(check, t_fe_0, t_u8x32)
        clear_t()
        return b
 }
@@ -144,7 +146,7 @@ function verify_pubkey (pub: StaticArray<u8>): boolean {
        }
 
        // fail if public key `k` is small order
-       if (ge_has_small_order(A, t_fe_0, t_u8x32) != 0) {
+       if (ge_is_small_order_point(A, t_fe_0, t_u8x32) != 0) {
                clear_t()
                return false
        }
@@ -159,6 +161,7 @@ function verify_pubkey (pub: StaticArray<u8>): boolean {
  * @returns 0 if signature fails checks, else return 1 if signature is good
  */
 function verify_signature (sig: StaticArray<u8>): boolean {
+
        // fail if scalar `S` is non-canonical (`L ≤ S`)
        memory.copy(changetype<usize>(S), changetype<usize>(sig) + 32, 32)
        if (!sc_is_canonical(S)) {
@@ -177,7 +180,7 @@ function verify_signature (sig: StaticArray<u8>): boolean {
        }
 
        // fail if nonce `R` is small order
-       if (ge_has_small_order(expected_r, t_fe_0, t_u8x32) != 0) {
+       if (ge_is_small_order_point(expected_r, t_fe_0, t_u8x32) != 0) {
                clear_t()
                return false
        }
index e692e2472cda83ae38e89afce9cf3ebb6a3a36ed..94d53f6a37e225f656ad357f7f628caf1874b6de 100644 (file)
@@ -222,28 +222,30 @@ export function ge_frombytes (h: ge_p3, s: StaticArray<u8>,
        return i32(has_m_root | has_p_root) - 1
 }
 
-const ge_has_small_order_y_sqrtm1: FieldElement = fe()
-const ge_has_small_order_c: FieldElement = fe()
+const ge_is_small_order_point_y_sqrtm1: FieldElement = fe()
+const ge_is_small_order_point_c: FieldElement = fe()
 /**
  * @param {ge_p3} p
- * @returns 1 if `p` has small order, else 0
+ * @returns 1 if `p` is a valid projective point with small order, else 0
  */
-export function ge_has_small_order (p: ge_p3, t_fe: FieldElement, t_u8x32: StaticArray<u8>): i32 {
-       const y_sqrtm1 = ge_has_small_order_y_sqrtm1
-       const c = ge_has_small_order_c
+export function ge_is_small_order_point (p: ge_p3, t_fe: FieldElement, t_u8x32: StaticArray<u8>): i32 {
+       const y_sqrtm1 = ge_is_small_order_point_y_sqrtm1
+       const c = ge_is_small_order_point_c
 
-       let ret: i32 = 0
+       const x_iszero = fe_iszero(p.X, t_fe, t_u8x32)
+       const y_iszero = fe_iszero(p.Y, t_fe, t_u8x32)
+       const z_iszero = fe_iszero(p.Z, t_fe, t_u8x32)
 
-       ret |= fe_iszero(p.X, t_fe, t_u8x32)
-       ret |= fe_iszero(p.Y, t_fe, t_u8x32)
-       ret |= fe_iszero(p.Z, t_fe, t_u8x32)
        fe_mul(y_sqrtm1, p.Y, fe_sqrtm1)
        fe_sub(c, y_sqrtm1, p.X)
-       ret |= fe_iszero(c, t_fe, t_u8x32)
+       let c_iszero = fe_iszero(c, t_fe, t_u8x32)
        fe_add(c, y_sqrtm1, p.X)
-       ret |= fe_iszero(c, t_fe, t_u8x32)
+       c_iszero |= fe_iszero(c, t_fe, t_u8x32)
 
-       return ret
+       const torsion = x_iszero | y_iszero | c_iszero
+       const valid = ((x_iszero & y_iszero) | z_iszero) ^ 1
+
+       return torsion & valid
 }
 
 /**
index 91ccc65882c1b6faa827ed9938b436f47b21ba47..0b4439fa5c3d73e06331989ef80a0b4a3f4e6a52 100644 (file)
@@ -44,7 +44,7 @@ const asc = new URL('node_modules/assemblyscript/bin/asc.js', root).pathname
 const HARNESS = `
 import './env'
 import { fe, fe_add, fe_cmov, fe_dbl, fe_frombytes, fe_invert, fe_isnegative, fe_iszero, fe_mul, fe_neg, fe_pow22523, fe_reduce, fe_sq, fe_sq2, fe_sub, fe_tobytes } from './ed25519/fe'
-import { ge_double_scalarmult_vartime_to_p3, ge_frombytes, ge_frombytes_negate_vartime, ge_has_small_order, ge_is_canonical, ge_scalarmult_base_tobytes } from './ed25519/ge'
+import { ge_double_scalarmult_vartime_to_p3, ge_frombytes, ge_frombytes_negate_vartime, ge_is_small_order_point, ge_is_canonical, ge_scalarmult_base_tobytes } from './ed25519/ge'
 import { ge_cached, ge_p1p1, ge_p3, ge_p3_tobytes, ge_sub_p3 } from './ed25519/p'
 import { sc_is_canonical, sc_muladd, sc_reduce } from './ed25519/sc'
 
@@ -106,7 +106,7 @@ 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_has_small_order_ (): i32 { return ge_has_small_order(PA, T0, U8T) }
+export function ge_is_small_order_point_ (): i32 { return ge_is_small_order_point(PA, T0, U8T) }
 export function ge_p3_tobytes_ (): void { ge_p3_tobytes(U8B, PA, T0, T1, T2, T3, T4, T5, T6) }
 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) }
@@ -187,7 +187,7 @@ function harness (compiled) {
                set: (id, l) => { const a = limbs(id); a.fill(0); a.set(l.slice(0, 10)) },
                get: (id) => Array.from(limbs(id)),
                padded: (id) => { const a = limbs(id); return a[10] === 0 && a[11] === 0 },
-               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)) }) },
+               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))))),
        }
@@ -411,7 +411,7 @@ try {
                const rc = x.ge_frombytes_()
                const got = getPoint(PA)
                note('ge_frombytes', want == null ? rc === -1 : rc === 0 && equal(got, want) && isExtended(got), e)
-               if (rc === 0) note('ge_has_small_order on decoded points', x.ge_has_small_order_() === (isSmall(want) ? 1 : 0), e)
+               if (rc === 0) note('ge_is_small_order_point on decoded points', x.ge_is_small_order_point_() === (isSmall(want) ? 1 : 0), e)
                bytes(U8A).set(e)
                const rn = x.ge_frombytes_negate_vartime_()
                const gn = getPoint(PA)
@@ -422,7 +422,7 @@ try {
                const p = k < 64 ? add(TORSION[k % 8], k % 2 ? TORSION[(k >> 1) % 8] : IDENTITY) : k < 128 ? add(mul(B, randomInt(L)), TORSION[k % 8]) : mul(B, randomInt(L))
                const lambda = randomInt(P - 1n) + 1n
                setProjective(PA, p.map(c => mod(c * lambda)))
-               note('ge_has_small_order on projective points', x.ge_has_small_order_() === (isSmall(p) ? 1 : 0), k)
+               note('ge_is_small_order_point on projective points', x.ge_is_small_order_point_() === (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)