]> git.codecow.com Git - nano25519.git/commitdiff
Store cached XY swap instead of reswapping in every subtraction.
authorChris Duncan <chris@codecow.com>
Mon, 26 Oct 2026 19:54:30 +0000 (12:54 -0700)
committerChris Duncan <chris@codecow.com>
Mon, 26 Oct 2026 19:54:30 +0000 (12:54 -0700)
src/assembly/ed25519/ge.ts
src/assembly/ed25519/p2.ts

index 2ed6e778a784637ba567d1d9f9bb8d716a2f7399..348d97755c0246d64a32fdd3804d21615525e55d 100644 (file)
@@ -399,7 +399,7 @@ const ge_double_scalarmult_vartime_Ai: StaticArray<ge_cached> = StaticArray.from
        new ge_cached()
 ])
 const ge_double_scalarmult_vartime_Ai_fep: StaticArray<FieldElementPair> = StaticArray.fromArray<FieldElementPair>([
-       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<u8>
 
        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<u8>
                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]
index 760561e3c030173ac2c186e1d5279c934f5cba55..9024ba738a34a072331739dc9a53ae637932a141 100644 (file)
@@ -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₂)