From: Chris Duncan Date: Tue, 20 Oct 2026 19:58:16 +0000 (-0700) Subject: Apply unsigned biasing to dual multiplication. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=c53ca3e9605b650677cdb6e5a2b2ac25f4777ccd;p=nano25519.git Apply unsigned biasing to dual multiplication. --- diff --git a/src/assembly/ed25519/fe.ts b/src/assembly/ed25519/fe.ts index b745cbc..d5b95b6 100644 --- a/src/assembly/ed25519/fe.ts +++ b/src/assembly/ed25519/fe.ts @@ -527,20 +527,25 @@ export function fe_mul_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldEle const f_b_0123: v128 = v128.load(changetype(f_b), 0) const f0: v128 = v128.shuffle(f_a_0123, f_b_0123, 0, 4, 0, 4) let f1: v128 = v128.shuffle(f_a_0123, f_b_0123, 1, 5, 1, 5) + f1 = v128.add(f1, v128.and(f1, odds)) const f2: v128 = v128.shuffle(f_a_0123, f_b_0123, 2, 6, 2, 6) let f3: v128 = v128.shuffle(f_a_0123, f_b_0123, 3, 7, 3, 7) + f3 = v128.add(f3, v128.and(f3, odds)) const f_a_4567: v128 = v128.load(changetype(f_a), 16) const f_b_4567: v128 = v128.load(changetype(f_b), 16) const f4: v128 = v128.shuffle(f_a_4567, f_b_4567, 0, 4, 0, 4) let f5: v128 = v128.shuffle(f_a_4567, f_b_4567, 1, 5, 1, 5) + f5 = v128.add(f5, v128.and(f5, odds)) const f6: v128 = v128.shuffle(f_a_4567, f_b_4567, 2, 6, 2, 6) let f7: v128 = v128.shuffle(f_a_4567, f_b_4567, 3, 7, 3, 7) + f7 = v128.add(f7, v128.and(f7, odds)) const f_a_89xx: v128 = v128.load64_zero(changetype(f_a), 32) const f_b_89xx: v128 = v128.load64_zero(changetype(f_b), 32) const f8: v128 = v128.shuffle(f_a_89xx, f_b_89xx, 0, 4, 0, 4) let f9: v128 = v128.shuffle(f_a_89xx, f_b_89xx, 1, 5, 1, 5) + f9 = v128.add(f9, v128.and(f9, odds)) // pack each g index once const g_a_0123: v128 = v128.load(changetype(g_a), 0) @@ -577,8 +582,6 @@ export function fe_mul_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldEle let h9: v128 = v128.extmul_high(f0, g89) // f[1] - f1 = v128.add(f1, v128.and(f1, odds)) - h1 = v128.add(h1, v128.extmul_low(f1, g01)) h2 = v128.add(h2, v128.extmul_high(f1, g01)) h3 = v128.add(h3, v128.extmul_low(f1, g23)) @@ -603,8 +606,6 @@ export function fe_mul_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldEle h1 = v128.add(h1, v128.extmul_high(f2, g89_19)) // f[3] - f3 = v128.add(f3, v128.and(f3, odds)) - h3 = v128.add(h3, v128.extmul_low(f3, g01)) h4 = v128.add(h4, v128.extmul_high(f3, g01)) h5 = v128.add(h5, v128.extmul_low(f3, g23)) @@ -629,8 +630,6 @@ export function fe_mul_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldEle h3 = v128.add(h3, v128.extmul_high(f4, g89_19)) // f[5] - f5 = v128.add(f5, v128.and(f5, odds)) - h5 = v128.add(h5, v128.extmul_low(f5, g01)) h6 = v128.add(h6, v128.extmul_high(f5, g01)) h7 = v128.add(h7, v128.extmul_low(f5, g23)) @@ -655,8 +654,6 @@ export function fe_mul_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldEle h5 = v128.add(h5, v128.extmul_high(f6, g89_19)) // f[7] - f7 = v128.add(f7, v128.and(f7, odds)) - h7 = v128.add(h7, v128.extmul_low(f7, g01)) h8 = v128.add(h8, v128.extmul_high(f7, g01)) h9 = v128.add(h9, v128.extmul_low(f7, g23)) @@ -681,8 +678,6 @@ export function fe_mul_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldEle h7 = v128.add(h7, v128.extmul_high(f8, g89_19)) // f[9] - f9 = v128.add(f9, v128.and(f9, odds)) - h9 = v128.add(h9, v128.extmul_low(f9, g01)) h0 = v128.add(h0, v128.extmul_high(f9, g01_19)) h1 = v128.add(h1, v128.extmul_low(f9, g23_19)) @@ -695,8 +690,6 @@ export function fe_mul_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldEle h8 = v128.add(h8, v128.extmul_high(f9, g89_19)) // reduce scalars with carry - const bias25 = i64x2.splat(1 << 25) - const bias24 = i64x2.splat(1 << 24) let carry0: v128 let carry1: v128 let carry2: v128 @@ -708,69 +701,79 @@ export function fe_mul_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldEle let carry8: v128 let carry9: v128 - carry0 = v128.shr(v128.add(h0, bias25), 26) + // Signed i64x2 shifts are emulated on x64, unsigned ones are native. Adding + // 2⁶² keeps every sum non-negative (|h| ≤ 2⁶⁰·⁴), and the unsigned shift then + // floors in fewer instructions. The bias is removed by subtraction, and the + // value to remove is the bias minus the original size: 2⁶²⁻²⁶ = 2³⁶ (or + // 2⁶²⁻²⁵ = 2³⁷). After unbiasing, the result is the signed carry. + const bias24 = v128.splat((1 << 24) + (1 << 62)) + const bias25 = v128.splat((1 << 25) + (1 << 62)) + const unbias26 = v128.splat(1 << 37) + const unbias25 = v128.splat(1 << 36) + + carry0 = v128.sub(v128.shr(v128.add(h0, bias25), 26), unbias25) h0 = v128.sub(h0, v128.shl(carry0, 26)) h1 = v128.add(h1, carry0) - carry4 = v128.shr(v128.add(h4, bias25), 26) + carry4 = v128.sub(v128.shr(v128.add(h4, bias25), 26), unbias25) h4 = v128.sub(h4, v128.shl(carry4, 26)) h5 = v128.add(h5, carry4) - carry1 = v128.shr(v128.add(h1, bias24), 25) + carry1 = v128.sub(v128.shr(v128.add(h1, bias24), 25), unbias26) h1 = v128.sub(h1, v128.shl(carry1, 25)) h2 = v128.add(h2, carry1) - carry5 = v128.shr(v128.add(h5, bias24), 25) + carry5 = v128.sub(v128.shr(v128.add(h5, bias24), 25), unbias26) h5 = v128.sub(h5, v128.shl(carry5, 25)) h6 = v128.add(h6, carry5) - carry2 = v128.shr(v128.add(h2, bias25), 26) + carry2 = v128.sub(v128.shr(v128.add(h2, bias25), 26), unbias25) h2 = v128.sub(h2, v128.shl(carry2, 26)) h3 = v128.add(h3, carry2) - carry6 = v128.shr(v128.add(h6, bias25), 26) + carry6 = v128.sub(v128.shr(v128.add(h6, bias25), 26), unbias25) h6 = v128.sub(h6, v128.shl(carry6, 26)) h7 = v128.add(h7, carry6) - carry3 = v128.shr(v128.add(h3, bias24), 25) + carry3 = v128.sub(v128.shr(v128.add(h3, bias24), 25), unbias26) h3 = v128.sub(h3, v128.shl(carry3, 25)) h4 = v128.add(h4, carry3) - carry7 = v128.shr(v128.add(h7, bias24), 25) + carry7 = v128.sub(v128.shr(v128.add(h7, bias24), 25), unbias26) h7 = v128.sub(h7, v128.shl(carry7, 25)) h8 = v128.add(h8, carry7) - carry4 = v128.shr(v128.add(h4, bias25), 26) + carry4 = v128.sub(v128.shr(v128.add(h4, bias25), 26), unbias25) h4 = v128.sub(h4, v128.shl(carry4, 26)) h5 = v128.add(h5, carry4) - carry8 = v128.shr(v128.add(h8, bias25), 26) + carry8 = v128.sub(v128.shr(v128.add(h8, bias25), 26), unbias25) h8 = v128.sub(h8, v128.shl(carry8, 26)) h9 = v128.add(h9, carry8) - carry9 = v128.shr(v128.add(h9, bias24), 25) + carry9 = v128.sub(v128.shr(v128.add(h9, bias24), 25), unbias26) h9 = v128.sub(h9, v128.shl(carry9, 25)) h0 = v128.add(h0, v128.add(carry9, v128.add(v128.shl(carry9, 1), v128.shl(carry9, 4)))) - carry0 = v128.shr(v128.add(h0, bias25), 26) + carry0 = v128.sub(v128.shr(v128.add(h0, bias25), 26), unbias25) h0 = v128.sub(h0, v128.shl(carry0, 26)) h1 = v128.add(h1, carry0) // reduce i64x2 to i32x4 now that only low half has a value and fits into i32 - const h01: v128 = v128.shuffle(h0, h1, 0, 4, 2, 6) // h0a, h1a, h0b, h1b - const h23: v128 = v128.shuffle(h2, h3, 0, 4, 2, 6) // h2a, h3a, h2b, h3b - const h45: v128 = v128.shuffle(h4, h5, 0, 4, 2, 6) // h4a, h5a, h4b, h5b - const h67: v128 = v128.shuffle(h6, h7, 0, 4, 2, 6) // h6a, h7a, h6b, h7b - const h89: v128 = v128.shuffle(h8, h9, 0, 4, 2, 6) // h8a, h9a, h8b, h9b + const h01: v128 = v128.shuffle(h0, h1, 0, 2, 4, 6) // h0a, h0b, h1a, h1b + const h23: v128 = v128.shuffle(h2, h3, 0, 2, 4, 6) // h2a, h2b ,h3a, h3b + const h45: v128 = v128.shuffle(h4, h5, 0, 2, 4, 6) // h4a, h4b, h5a, h5b + const h67: v128 = v128.shuffle(h6, h7, 0, 2, 4, 6) // h6a, h6b, h7a, h7b + const h89: v128 = v128.shuffle(h8, h9, 0, 2, 4, 6) // h8a, h8b, h9a, h9b // assign results to parallel outputs - v128.store(changetype(h_a), v128.shuffle(h01, h23, 0, 1, 4, 5), 0) - v128.store(changetype(h_a), v128.shuffle(h45, h67, 0, 1, 4, 5), 16) - v128.store_lane(changetype(h_a), h89, 0, 32) + v128.store(changetype(h_a), v128.shuffle(h01, h23, 0, 2, 4, 6), 0) + v128.store(changetype(h_a), v128.shuffle(h45, h67, 0, 2, 4, 6), 16) + v128.store_lane(changetype(h_a), v128.shuffle(h89, h89, 0, 2, 4, 6), 0, 32) - v128.store(changetype(h_b), v128.shuffle(h01, h23, 2, 3, 6, 7), 0) - v128.store(changetype(h_b), v128.shuffle(h45, h67, 2, 3, 6, 7), 16) - v128.store_lane(changetype(h_b), h89, 1, 32) + v128.store(changetype(h_b), v128.shuffle(h01, h23, 1, 3, 5, 7), 0) + v128.store(changetype(h_b), v128.shuffle(h45, h67, 1, 3, 5, 7), 16) + v128.store_lane(changetype(h_b), v128.shuffle(h89, h89, 1, 3, 5, 7), 0, 32) } /**