From: Chris Duncan Date: Mon, 26 Oct 2026 19:54:30 +0000 (-0700) Subject: Store cached XY swap instead of reswapping in every subtraction. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=dd27d6bae87d7550e8bb1f8f8c85afcf248c1523;p=nano25519.git Store cached XY swap instead of reswapping in every subtraction. --- diff --git a/src/assembly/ed25519/ge.ts b/src/assembly/ed25519/ge.ts index 2ed6e77..348d977 100644 --- a/src/assembly/ed25519/ge.ts +++ b/src/assembly/ed25519/ge.ts @@ -399,7 +399,7 @@ const ge_double_scalarmult_vartime_Ai: StaticArray = StaticArray.from new ge_cached() ]) const ge_double_scalarmult_vartime_Ai_fep: StaticArray = StaticArray.fromArray([ - fep(), fep(), fep(), fep(), fep(), fep(), fep(), fep(), fep(), fep(), fep(), fep(), fep(), fep(), fep(), fep() + fep(), fep(), fep(), fep(), fep(), fep(), fep(), fep(), fep(), fep(), fep(), fep(), fep(), fep(), fep(), fep(), fep(), fep(), fep(), fep(), fep(), fep(), fep(), fep() ]) const ge_double_scalarmult_vartime_p: ge_p2 = new ge_p2() const ge_double_scalarmult_vartime_t: ge_p1p1 = new ge_p1p1() @@ -484,7 +484,8 @@ export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray for (let i = 0; i < 8; i++) { fep_pack(Ai_fep[i], Ai[i].YplusX, Ai[i].YminusX) - fep_pack(Ai_fep[i + 8], Ai[i].Z, Ai[i].T2d) + fep_pack(Ai_fep[i + 8], Ai[i].YminusX, Ai[i].YplusX) + fep_pack(Ai_fep[i + 16], Ai[i].Z, Ai[i].T2d) } let i: i32 = 255 @@ -502,11 +503,11 @@ export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray if (aslide[i] > 0) { const aslidei = aslide[i] >> 1 gepair_p1p1_to_p3(uA, uB, tA, tB, t_fep) - gepair_add_cached(tA, tB, uA, uB, Ai_fep[aslidei], Ai_fep[aslidei + 8], t_fep) + gepair_add_cached(tA, tB, uA, uB, Ai_fep[aslidei], Ai_fep[aslidei + 16], t_fep) } else if (aslide[i] < 0) { const aslidei = (-aslide[i]) >> 1 gepair_p1p1_to_p3(uA, uB, tA, tB, t_fep) - gepair_sub_cached(tA, tB, uA, uB, Ai_fep[aslidei], Ai_fep[aslidei + 8], t_fep) + gepair_sub_cached(tA, tB, uA, uB, Ai_fep[aslidei + 8], Ai_fep[aslidei + 16], t_fep) } const bslidei = bslide[i] diff --git a/src/assembly/ed25519/p2.ts b/src/assembly/ed25519/p2.ts index 760561e..9024ba7 100644 --- a/src/assembly/ed25519/p2.ts +++ b/src/assembly/ed25519/p2.ts @@ -135,13 +135,12 @@ export function gepair_add_precomp (rXY: FieldElementPair, rZT: FieldElementPair * T = 2Z₁Z₂ + 2T₁T₂d * ``` */ -export function gepair_sub_cached (rXY: FieldElementPair, rZT: FieldElementPair, pXY: FieldElementPair, pZT: FieldElementPair, qXpYXmY: FieldElementPair, qZ2Td: FieldElementPair, t_fep: FieldElementPair): void { +export function gepair_sub_cached (rXY: FieldElementPair, rZT: FieldElementPair, pXY: FieldElementPair, pZT: FieldElementPair, qXmYXpY: FieldElementPair, qZ2Td: FieldElementPair, t_fep: FieldElementPair): void { fep_swap(rXY, pXY) // pA = (X₁, Y₁); rA = (Y₁, X₁) fep_addsub(rXY, t_fep, pXY, rXY) // rA = (X₁+Y₁, Y₁+X₁); t = (X₁-Y₁, Y₁-X₁) fep_blend(rXY, rXY, t_fep) // rA = (X₁+Y₁, Y₁-X₁) i.e. (Y₁+X₁, Y₁-X₁) - fep_swap(t_fep, qXpYXmY) // t = (Y₂-X₂, Y₂+X₂) - fep_mul(rXY, rXY, t_fep) // rA = ((Y₁+X₁)(Y₂-X₂), (Y₁-X₁)(Y₂+X₂)) + fep_mul(rXY, rXY, qXmYXpY) // rA = ((Y₁+X₁)(Y₂-X₂), (Y₁-X₁)(Y₂+X₂)) fep_swap(t_fep, rXY) // t = ((Y₁-X₁)(Y₂+X₂), (Y₁+X₁)(Y₂-X₂)) fep_addsub(rXY, t_fep, rXY, t_fep) // rA = ((Y₁+X₁)(Y₂-X₂) + (Y₁-X₁)(Y₂+X₂), (Y₁-X₁)(Y₂+X₂) + (Y₁+X₁)(Y₂-X₂)); t = ((Y₁+X₁)(Y₂-X₂) - (Y₁-X₁)(Y₂+X₂), (Y₁-X₁)(Y₂+X₂) - (Y₁+X₁)(Y₂-X₂)) fep_blend(rXY, t_fep, rXY) // X = (Y₁+X₁)(Y₂-X₂) - (Y₁-X₁)(Y₂+X₂); Y = (Y₁-X₁)(Y₂+X₂) + (Y₁+X₁)(Y₂-X₂) i.e. (Y₁+X₁)(Y₂-X₂) + (Y₁-X₁)(Y₂+X₂)