From: Chris Duncan Date: Fri, 6 Nov 2026 17:32:08 +0000 (-0800) Subject: Add p1p1 doubling helper to deprecate another temporary in double scalarmult and... X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=25673bf08f1ac7eff1139af51a46b2a4cc7224ac;p=nano25519.git Add p1p1 doubling helper to deprecate another temporary in double scalarmult and simply its loop. --- diff --git a/src/assembly/ed25519/ge/double_scalarmult.ts b/src/assembly/ed25519/ge/double_scalarmult.ts index 7f4ab5f..09053b6 100644 --- a/src/assembly/ed25519/ge/double_scalarmult.ts +++ b/src/assembly/ed25519/ge/double_scalarmult.ts @@ -18,11 +18,10 @@ * - precomp (Duif): (y+x,y-x,2dxy) */ import { base2 as Bi } from '../constants/base2' -import { p1p1, p1p1_0, p1p1_to_cached, p1p1_to_p2, p1p1_to_p3, p2, p2_0, p2_dbl, p3, p3_add_cached, p3_add_precomp, p3_sub_cached, p3_sub_precomp, p_cached } from '../p' +import { p1p1, p1p1_0, p1p1_dbl, p1p1_to_cached, p1p1_to_p3, p3, p3_add_cached, p3_add_precomp, p3_sub_cached, p3_sub_precomp, p_cached } from '../p' const aslide: StaticArray = new StaticArray(256) const bslide: StaticArray = new StaticArray(256) -const p: p2 = new p2() const t: p1p1 = new p1p1() const u: p3 = new p3() @@ -53,7 +52,6 @@ export function ge_double_scalarmult_vartime_to_cached (r: p_cached, a: StaticAr } function ge_double_scalarmult_vartime_to_p1p1 (r: p1p1, a: StaticArray, Ai: StaticArray, b: StaticArray): void { - p2_0(p) p1p1_0(r) slide_vartime(aslide, a) @@ -65,7 +63,7 @@ function ge_double_scalarmult_vartime_to_p1p1 (r: p1p1, a: StaticArray, Ai: } while (i >= 0) { - p2_dbl(r, p) + p1p1_dbl(r, r) const aslidei = aslide[i] if (aslidei > 0) { @@ -85,9 +83,6 @@ function ge_double_scalarmult_vartime_to_p1p1 (r: p1p1, a: StaticArray, Ai: p3_sub_precomp(r, u, Bi[(-bslidei) >> 1]) } - if (i > 0) { - p1p1_to_p2(p, r) - } i-- } } diff --git a/src/assembly/ed25519/p.ts b/src/assembly/ed25519/p.ts index 0b70e83..9a1667d 100644 --- a/src/assembly/ed25519/p.ts +++ b/src/assembly/ed25519/p.ts @@ -146,6 +146,38 @@ export function p1p1_to_cached (r: p_cached, p: p1p1): void { fe_addsub(r.YplusX, r.YminusX, r.YplusX, r.YminusX) } +/** + * `r = 2p` + * + * ``` + * X = X₁T₁ + * Y = Y₁Z₁ + * Z = Z₁T₁ + * ``` + * + * then + * + * ``` + * X = (X₁+Y₁)² - (Y₁²+X₁²) + * Y = Y₁² + X₁² + * Z = Y₁² - X₁² + * T = 2Z₁² - (Y₁²-X₁²) + * ``` + */ +export function p1p1_dbl (r: p1p1, p: p1p1): void { + fe_mul_pair(t.X, p.X, p.T, t.Y, p.Y, p.Z) + fe_mul(t.Z, p.Z, p.T) + + fe_add(r.X, t.X, t.Y) + fe_sq_pair_dbl(r.X, r.X, r.T, t.Z) + fe_sq_pair(r.Z, t.X, r.Y, t.Y) + fe_addsub(r.Y, r.Z, r.Y, r.Z) + fe_sub(r.X, r.X, r.Y) + fe_sub(r.T, r.T, r.Z) + + p1p1_0(t) +} + /** * `r = 2p` * @@ -156,7 +188,7 @@ export function p1p1_to_cached (r: p_cached, p: p1p1): void { * T = 2Z₁² - (Y₁²-X₁²) * ``` */ -export function p2_dbl (r: p1p1, p: p2): void { +function p2_dbl (r: p1p1, p: p2): void { fe_add(r.X, p.X, p.Y) fe_sq_pair_dbl(r.X, r.X, r.T, p.Z) fe_sq_pair(r.Z, p.X, r.Y, p.Y)