]> git.codecow.com Git - nano25519.git/commitdiff
Implement paired addition of cached point.
authorChris Duncan <chris@codecow.com>
Sun, 25 Oct 2026 09:38:56 +0000 (02:38 -0700)
committerChris Duncan <chris@codecow.com>
Sun, 25 Oct 2026 09:38:56 +0000 (02:38 -0700)
src/assembly/ed25519/fep.ts
src/assembly/ed25519/ge.ts
src/assembly/ed25519/p2.ts

index a2ad4b14d8250b42b94863a4f7d1697243de66fb..8173541da2726513008c36a246b7cf95b53cab7d 100644 (file)
@@ -130,6 +130,40 @@ export function fep_add (h: FieldElementPair, f: FieldElementPair, g: FieldEleme
        v128.store(changetype<usize>(h), h4, 4 << 4)
 }
 
+/**
+ * Add and subtract two FieldElement pairs and store the results.
+ *
+ * `f = f + g` and `g = f - g`
+ *
+ * @param {FieldElementPair} f FieldElement sum destination, summand source, and minuend source
+ * @param {FieldElementPair} g FieldElement difference destination, summand source, and subtrahend source
+ */
+export function fep_addsub (f: FieldElementPair, g: FieldElementPair): void {
+       const f0 = v128.load(changetype<usize>(f), 0 << 4)
+       const f1 = v128.load(changetype<usize>(f), 1 << 4)
+       const f2 = v128.load(changetype<usize>(f), 2 << 4)
+       const f3 = v128.load(changetype<usize>(f), 3 << 4)
+       const f4 = v128.load(changetype<usize>(f), 4 << 4)
+
+       const g0 = v128.load(changetype<usize>(g), 0 << 4)
+       const g1 = v128.load(changetype<usize>(g), 1 << 4)
+       const g2 = v128.load(changetype<usize>(g), 2 << 4)
+       const g3 = v128.load(changetype<usize>(g), 3 << 4)
+       const g4 = v128.load(changetype<usize>(g), 4 << 4)
+
+       v128.store(changetype<usize>(f), v128.add<i32>(f0, g0), 0 << 4)
+       v128.store(changetype<usize>(f), v128.add<i32>(f1, g1), 1 << 4)
+       v128.store(changetype<usize>(f), v128.add<i32>(f2, g2), 2 << 4)
+       v128.store(changetype<usize>(f), v128.add<i32>(f3, g3), 3 << 4)
+       v128.store(changetype<usize>(f), v128.add<i32>(f4, g4), 4 << 4)
+
+       v128.store(changetype<usize>(g), v128.sub<i32>(f0, g0), 0 << 4)
+       v128.store(changetype<usize>(g), v128.sub<i32>(f1, g1), 1 << 4)
+       v128.store(changetype<usize>(g), v128.sub<i32>(f2, g2), 2 << 4)
+       v128.store(changetype<usize>(g), v128.sub<i32>(f3, g3), 3 << 4)
+       v128.store(changetype<usize>(g), v128.sub<i32>(f4, g4), 4 << 4)
+}
+
 /**
  * Alternates between each `i32` from two FieldElementPairs.
  * @param {FieldElementPair} h Blended values i.e. [f0, g1, ..., f8, g9]
@@ -642,6 +676,256 @@ export function fep_mul (h: FieldElementPair, f: FieldElementPair, g: FieldEleme
        v128.store(changetype<usize>(h), v128.shuffle<i32>(h8, h9, 0, 2, 4, 6), 4 << 4) // h8a, h8b, h9a, h9b
 }
 
+/**
+ * Multiply two FieldElementPair values in parallel, double the second one, and
+ * store the products.
+ *
+ * @param {FieldElementPair} h product destination
+ * @param {FieldElementPair} f multiplicand source
+ * @param {FieldElementPair} g multiplicand source
+ */
+export function fep_mul2 (h: FieldElementPair, f: FieldElementPair, g: FieldElementPair): void {
+       const f01: v128 = v128.load(changetype<usize>(f), 0 << 4)
+       const f23: v128 = v128.load(changetype<usize>(f), 1 << 4)
+       const f45: v128 = v128.load(changetype<usize>(f), 2 << 4)
+       const f67: v128 = v128.load(changetype<usize>(f), 3 << 4)
+       const f89: v128 = v128.load(changetype<usize>(f), 4 << 4)
+
+       const f0: v128 = v128.shuffle<i32>(f01, f01, 0, 1, 0, 1)
+       const f1: v128 = v128.shuffle<i32>(f01, v128.add<i32>(f01, f01), 2, 3, 6, 7)
+       const f2: v128 = v128.shuffle<i32>(f23, f23, 0, 1, 0, 1)
+       const f3: v128 = v128.shuffle<i32>(f23, v128.add<i32>(f23, f23), 2, 3, 6, 7)
+       const f4: v128 = v128.shuffle<i32>(f45, f45, 0, 1, 0, 1)
+       const f5: v128 = v128.shuffle<i32>(f45, v128.add<i32>(f45, f45), 2, 3, 6, 7)
+       const f6: v128 = v128.shuffle<i32>(f67, f67, 0, 1, 0, 1)
+       const f7: v128 = v128.shuffle<i32>(f67, v128.add<i32>(f67, f67), 2, 3, 6, 7)
+       const f8: v128 = v128.shuffle<i32>(f89, f89, 0, 1, 0, 1)
+       const f9: v128 = v128.shuffle<i32>(f89, v128.add<i32>(f89, f89), 2, 3, 6, 7)
+
+       const g01: v128 = v128.load(changetype<usize>(g), 0 << 4)
+       const g23: v128 = v128.load(changetype<usize>(g), 1 << 4)
+       const g45: v128 = v128.load(changetype<usize>(g), 2 << 4)
+       const g67: v128 = v128.load(changetype<usize>(g), 3 << 4)
+       const g89: v128 = v128.load(changetype<usize>(g), 4 << 4)
+
+       // factor of 19 for wrap from h9 to h0
+       const g01_19: v128 = v128.add<i32>(g01, v128.add<i32>(v128.add<i32>(g01, g01), v128.shl<i32>(g01, 4)))
+       const g23_19: v128 = v128.add<i32>(g23, v128.add<i32>(v128.add<i32>(g23, g23), v128.shl<i32>(g23, 4)))
+       const g45_19: v128 = v128.add<i32>(g45, v128.add<i32>(v128.add<i32>(g45, g45), v128.shl<i32>(g45, 4)))
+       const g67_19: v128 = v128.add<i32>(g67, v128.add<i32>(v128.add<i32>(g67, g67), v128.shl<i32>(g67, 4)))
+       const g89_19: v128 = v128.add<i32>(g89, v128.add<i32>(v128.add<i32>(g89, g89), v128.shl<i32>(g89, 4)))
+
+       // f[0]
+       let h0: v128 = v128.extmul_low<i32>(f0, g01)
+       let h1: v128 = v128.extmul_high<i32>(f0, g01)
+       let h2: v128 = v128.extmul_low<i32>(f0, g23)
+       let h3: v128 = v128.extmul_high<i32>(f0, g23)
+       let h4: v128 = v128.extmul_low<i32>(f0, g45)
+       let h5: v128 = v128.extmul_high<i32>(f0, g45)
+       let h6: v128 = v128.extmul_low<i32>(f0, g67)
+       let h7: v128 = v128.extmul_high<i32>(f0, g67)
+       let h8: v128 = v128.extmul_low<i32>(f0, g89)
+       let h9: v128 = v128.extmul_high<i32>(f0, g89)
+
+       // f[1]
+       h1 = v128.add<i64>(h1, v128.extmul_low<i32>(f1, g01))
+       h2 = v128.add<i64>(h2, v128.extmul_high<i32>(f1, g01))
+       h3 = v128.add<i64>(h3, v128.extmul_low<i32>(f1, g23))
+       h4 = v128.add<i64>(h4, v128.extmul_high<i32>(f1, g23))
+       h5 = v128.add<i64>(h5, v128.extmul_low<i32>(f1, g45))
+       h6 = v128.add<i64>(h6, v128.extmul_high<i32>(f1, g45))
+       h7 = v128.add<i64>(h7, v128.extmul_low<i32>(f1, g67))
+       h8 = v128.add<i64>(h8, v128.extmul_high<i32>(f1, g67))
+       h9 = v128.add<i64>(h9, v128.extmul_low<i32>(f1, g89))
+       h0 = v128.add<i64>(h0, v128.extmul_high<i32>(f1, g89_19))
+
+       // f[2]
+       h2 = v128.add<i64>(h2, v128.extmul_low<i32>(f2, g01))
+       h3 = v128.add<i64>(h3, v128.extmul_high<i32>(f2, g01))
+       h4 = v128.add<i64>(h4, v128.extmul_low<i32>(f2, g23))
+       h5 = v128.add<i64>(h5, v128.extmul_high<i32>(f2, g23))
+       h6 = v128.add<i64>(h6, v128.extmul_low<i32>(f2, g45))
+       h7 = v128.add<i64>(h7, v128.extmul_high<i32>(f2, g45))
+       h8 = v128.add<i64>(h8, v128.extmul_low<i32>(f2, g67))
+       h9 = v128.add<i64>(h9, v128.extmul_high<i32>(f2, g67))
+       h0 = v128.add<i64>(h0, v128.extmul_low<i32>(f2, g89_19))
+       h1 = v128.add<i64>(h1, v128.extmul_high<i32>(f2, g89_19))
+
+       // f[3]
+       h3 = v128.add<i64>(h3, v128.extmul_low<i32>(f3, g01))
+       h4 = v128.add<i64>(h4, v128.extmul_high<i32>(f3, g01))
+       h5 = v128.add<i64>(h5, v128.extmul_low<i32>(f3, g23))
+       h6 = v128.add<i64>(h6, v128.extmul_high<i32>(f3, g23))
+       h7 = v128.add<i64>(h7, v128.extmul_low<i32>(f3, g45))
+       h8 = v128.add<i64>(h8, v128.extmul_high<i32>(f3, g45))
+       h9 = v128.add<i64>(h9, v128.extmul_low<i32>(f3, g67))
+       h0 = v128.add<i64>(h0, v128.extmul_high<i32>(f3, g67_19))
+       h1 = v128.add<i64>(h1, v128.extmul_low<i32>(f3, g89_19))
+       h2 = v128.add<i64>(h2, v128.extmul_high<i32>(f3, g89_19))
+
+       // f[4]
+       h4 = v128.add<i64>(h4, v128.extmul_low<i32>(f4, g01))
+       h5 = v128.add<i64>(h5, v128.extmul_high<i32>(f4, g01))
+       h6 = v128.add<i64>(h6, v128.extmul_low<i32>(f4, g23))
+       h7 = v128.add<i64>(h7, v128.extmul_high<i32>(f4, g23))
+       h8 = v128.add<i64>(h8, v128.extmul_low<i32>(f4, g45))
+       h9 = v128.add<i64>(h9, v128.extmul_high<i32>(f4, g45))
+       h0 = v128.add<i64>(h0, v128.extmul_low<i32>(f4, g67_19))
+       h1 = v128.add<i64>(h1, v128.extmul_high<i32>(f4, g67_19))
+       h2 = v128.add<i64>(h2, v128.extmul_low<i32>(f4, g89_19))
+       h3 = v128.add<i64>(h3, v128.extmul_high<i32>(f4, g89_19))
+
+       // f[5]
+       h5 = v128.add<i64>(h5, v128.extmul_low<i32>(f5, g01))
+       h6 = v128.add<i64>(h6, v128.extmul_high<i32>(f5, g01))
+       h7 = v128.add<i64>(h7, v128.extmul_low<i32>(f5, g23))
+       h8 = v128.add<i64>(h8, v128.extmul_high<i32>(f5, g23))
+       h9 = v128.add<i64>(h9, v128.extmul_low<i32>(f5, g45))
+       h0 = v128.add<i64>(h0, v128.extmul_high<i32>(f5, g45_19))
+       h1 = v128.add<i64>(h1, v128.extmul_low<i32>(f5, g67_19))
+       h2 = v128.add<i64>(h2, v128.extmul_high<i32>(f5, g67_19))
+       h3 = v128.add<i64>(h3, v128.extmul_low<i32>(f5, g89_19))
+       h4 = v128.add<i64>(h4, v128.extmul_high<i32>(f5, g89_19))
+
+       // f[6]
+       h6 = v128.add<i64>(h6, v128.extmul_low<i32>(f6, g01))
+       h7 = v128.add<i64>(h7, v128.extmul_high<i32>(f6, g01))
+       h8 = v128.add<i64>(h8, v128.extmul_low<i32>(f6, g23))
+       h9 = v128.add<i64>(h9, v128.extmul_high<i32>(f6, g23))
+       h0 = v128.add<i64>(h0, v128.extmul_low<i32>(f6, g45_19))
+       h1 = v128.add<i64>(h1, v128.extmul_high<i32>(f6, g45_19))
+       h2 = v128.add<i64>(h2, v128.extmul_low<i32>(f6, g67_19))
+       h3 = v128.add<i64>(h3, v128.extmul_high<i32>(f6, g67_19))
+       h4 = v128.add<i64>(h4, v128.extmul_low<i32>(f6, g89_19))
+       h5 = v128.add<i64>(h5, v128.extmul_high<i32>(f6, g89_19))
+
+       // f[7]
+       h7 = v128.add<i64>(h7, v128.extmul_low<i32>(f7, g01))
+       h8 = v128.add<i64>(h8, v128.extmul_high<i32>(f7, g01))
+       h9 = v128.add<i64>(h9, v128.extmul_low<i32>(f7, g23))
+       h0 = v128.add<i64>(h0, v128.extmul_high<i32>(f7, g23_19))
+       h1 = v128.add<i64>(h1, v128.extmul_low<i32>(f7, g45_19))
+       h2 = v128.add<i64>(h2, v128.extmul_high<i32>(f7, g45_19))
+       h3 = v128.add<i64>(h3, v128.extmul_low<i32>(f7, g67_19))
+       h4 = v128.add<i64>(h4, v128.extmul_high<i32>(f7, g67_19))
+       h5 = v128.add<i64>(h5, v128.extmul_low<i32>(f7, g89_19))
+       h6 = v128.add<i64>(h6, v128.extmul_high<i32>(f7, g89_19))
+
+       // f[8]
+       h8 = v128.add<i64>(h8, v128.extmul_low<i32>(f8, g01))
+       h9 = v128.add<i64>(h9, v128.extmul_high<i32>(f8, g01))
+       h0 = v128.add<i64>(h0, v128.extmul_low<i32>(f8, g23_19))
+       h1 = v128.add<i64>(h1, v128.extmul_high<i32>(f8, g23_19))
+       h2 = v128.add<i64>(h2, v128.extmul_low<i32>(f8, g45_19))
+       h3 = v128.add<i64>(h3, v128.extmul_high<i32>(f8, g45_19))
+       h4 = v128.add<i64>(h4, v128.extmul_low<i32>(f8, g67_19))
+       h5 = v128.add<i64>(h5, v128.extmul_high<i32>(f8, g67_19))
+       h6 = v128.add<i64>(h6, v128.extmul_low<i32>(f8, g89_19))
+       h7 = v128.add<i64>(h7, v128.extmul_high<i32>(f8, g89_19))
+
+       // f[9]
+       h9 = v128.add<i64>(h9, v128.extmul_low<i32>(f9, g01))
+       h0 = v128.add<i64>(h0, v128.extmul_high<i32>(f9, g01_19))
+       h1 = v128.add<i64>(h1, v128.extmul_low<i32>(f9, g23_19))
+       h2 = v128.add<i64>(h2, v128.extmul_high<i32>(f9, g23_19))
+       h3 = v128.add<i64>(h3, v128.extmul_low<i32>(f9, g45_19))
+       h4 = v128.add<i64>(h4, v128.extmul_high<i32>(f9, g45_19))
+       h5 = v128.add<i64>(h5, v128.extmul_low<i32>(f9, g67_19))
+       h6 = v128.add<i64>(h6, v128.extmul_high<i32>(f9, g67_19))
+       h7 = v128.add<i64>(h7, v128.extmul_low<i32>(f9, g89_19))
+       h8 = v128.add<i64>(h8, v128.extmul_high<i32>(f9, g89_19))
+
+       // double second field element only
+       const m = i64x2(0, -1)
+       h0 = v128.add<i64>(h0, v128.and(h0, m))
+       h1 = v128.add<i64>(h1, v128.and(h1, m))
+       h2 = v128.add<i64>(h2, v128.and(h2, m))
+       h3 = v128.add<i64>(h3, v128.and(h3, m))
+       h4 = v128.add<i64>(h4, v128.and(h4, m))
+       h5 = v128.add<i64>(h5, v128.and(h5, m))
+       h6 = v128.add<i64>(h6, v128.and(h6, m))
+       h7 = v128.add<i64>(h7, v128.and(h7, m))
+       h8 = v128.add<i64>(h8, v128.and(h8, m))
+       h9 = v128.add<i64>(h9, v128.and(h9, m))
+
+       // reduce scalars with carry
+       let carry0: v128
+       let carry1: v128
+       let carry2: v128
+       let carry3: v128
+       let carry4: v128
+       let carry5: v128
+       let carry6: v128
+       let carry7: v128
+       let carry8: v128
+       let carry9: v128
+
+       // 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<i64>((<i64>1 << 24) + (<i64>1 << 62))
+       const bias25 = v128.splat<i64>((<i64>1 << 25) + (<i64>1 << 62))
+       const unbias25 = v128.splat<i64>(<i64>1 << 37)
+       const unbias26 = v128.splat<i64>(<i64>1 << 36)
+
+       carry0 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h0, bias25), 26), unbias26)
+       h0 = v128.sub<i64>(h0, v128.shl<i64>(carry0, 26))
+       h1 = v128.add<i64>(h1, carry0)
+
+       carry4 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h4, bias25), 26), unbias26)
+       h4 = v128.sub<i64>(h4, v128.shl<i64>(carry4, 26))
+       h5 = v128.add<i64>(h5, carry4)
+
+       carry1 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h1, bias24), 25), unbias25)
+       h1 = v128.sub<i64>(h1, v128.shl<i64>(carry1, 25))
+       h2 = v128.add<i64>(h2, carry1)
+
+       carry5 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h5, bias24), 25), unbias25)
+       h5 = v128.sub<i64>(h5, v128.shl<i64>(carry5, 25))
+       h6 = v128.add<i64>(h6, carry5)
+
+       carry2 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h2, bias25), 26), unbias26)
+       h2 = v128.sub<i64>(h2, v128.shl<i64>(carry2, 26))
+       h3 = v128.add<i64>(h3, carry2)
+
+       carry6 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h6, bias25), 26), unbias26)
+       h6 = v128.sub<i64>(h6, v128.shl<i64>(carry6, 26))
+       h7 = v128.add<i64>(h7, carry6)
+
+       carry3 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h3, bias24), 25), unbias25)
+       h3 = v128.sub<i64>(h3, v128.shl<i64>(carry3, 25))
+       h4 = v128.add<i64>(h4, carry3)
+
+       carry7 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h7, bias24), 25), unbias25)
+       h7 = v128.sub<i64>(h7, v128.shl<i64>(carry7, 25))
+       h8 = v128.add<i64>(h8, carry7)
+
+       carry4 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h4, bias25), 26), unbias26)
+       h4 = v128.sub<i64>(h4, v128.shl<i64>(carry4, 26))
+       h5 = v128.add<i64>(h5, carry4)
+
+       carry8 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h8, bias25), 26), unbias26)
+       h8 = v128.sub<i64>(h8, v128.shl<i64>(carry8, 26))
+       h9 = v128.add<i64>(h9, carry8)
+
+       carry9 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h9, bias24), 25), unbias25)
+       h9 = v128.sub<i64>(h9, v128.shl<i64>(carry9, 25))
+       h0 = v128.add<i64>(h0, v128.add<i64>(carry9, v128.add<i64>(v128.add<i64>(carry9, carry9), v128.shl<i64>(carry9, 4))))
+
+       carry0 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h0, bias25), 26), unbias26)
+       h0 = v128.sub<i64>(h0, v128.shl<i64>(carry0, 26))
+       h1 = v128.add<i64>(h1, carry0)
+
+       // reduce i64x2 to i32x4 now that high bits are zero and low bits fit into i32
+       v128.store(changetype<usize>(h), v128.shuffle<i32>(h0, h1, 0, 2, 4, 6), 0 << 4) // h0a, h0b, h1a, h1b
+       v128.store(changetype<usize>(h), v128.shuffle<i32>(h2, h3, 0, 2, 4, 6), 1 << 4) // h2a, h2b ,h3a, h3b
+       v128.store(changetype<usize>(h), v128.shuffle<i32>(h4, h5, 0, 2, 4, 6), 2 << 4) // h4a, h4b, h5a, h5b
+       v128.store(changetype<usize>(h), v128.shuffle<i32>(h6, h7, 0, 2, 4, 6), 3 << 4) // h6a, h6b, h7a, h7b
+       v128.store(changetype<usize>(h), v128.shuffle<i32>(h8, h9, 0, 2, 4, 6), 4 << 4) // h8a, h8b, h9a, h9b
+}
+
 /**
  * Multiply the first interleaved lane of two FieldElementPairs and store the
  * product.
@@ -1310,6 +1594,40 @@ export function fep_sub (h: FieldElementPair, f: FieldElementPair, g: FieldEleme
        v128.store(changetype<usize>(h), h4, 4 << 4)
 }
 
+/**
+ * Subtract and add two FieldElement pairs and store the results.
+ *
+ * `f = f - g` and `g = f + g`
+ *
+ * @param {FieldElementPair} f FieldElement difference destination, summand source, and subtrahend source
+ * @param {FieldElementPair} g FieldElement sum destination, summand source, and minuend source
+ */
+export function fep_subadd (f: FieldElementPair, g: FieldElementPair): void {
+       const f0 = v128.load(changetype<usize>(f), 0 << 4)
+       const f1 = v128.load(changetype<usize>(f), 1 << 4)
+       const f2 = v128.load(changetype<usize>(f), 2 << 4)
+       const f3 = v128.load(changetype<usize>(f), 3 << 4)
+       const f4 = v128.load(changetype<usize>(f), 4 << 4)
+
+       const g0 = v128.load(changetype<usize>(g), 0 << 4)
+       const g1 = v128.load(changetype<usize>(g), 1 << 4)
+       const g2 = v128.load(changetype<usize>(g), 2 << 4)
+       const g3 = v128.load(changetype<usize>(g), 3 << 4)
+       const g4 = v128.load(changetype<usize>(g), 4 << 4)
+
+       v128.store(changetype<usize>(f), v128.sub<i32>(f0, g0), 0 << 4)
+       v128.store(changetype<usize>(f), v128.sub<i32>(f1, g1), 1 << 4)
+       v128.store(changetype<usize>(f), v128.sub<i32>(f2, g2), 2 << 4)
+       v128.store(changetype<usize>(f), v128.sub<i32>(f3, g3), 3 << 4)
+       v128.store(changetype<usize>(f), v128.sub<i32>(f4, g4), 4 << 4)
+
+       v128.store(changetype<usize>(g), v128.add<i32>(f0, g0), 0 << 4)
+       v128.store(changetype<usize>(g), v128.add<i32>(f1, g1), 1 << 4)
+       v128.store(changetype<usize>(g), v128.add<i32>(f2, g2), 2 << 4)
+       v128.store(changetype<usize>(g), v128.add<i32>(f3, g3), 3 << 4)
+       v128.store(changetype<usize>(g), v128.add<i32>(f4, g4), 4 << 4)
+}
+
 /**
  * Swaps each consecutive pair of `i32` eleements.
  * @param {FieldElementPair} h Swapped values i.e. [1, 0, 3, 2, ..., 19, 18]
index d12f39c5b670e5f26952100128dd185a2990d557..a088cf131f5f7aff19abcddf22bac60a196b0ee6 100644 (file)
@@ -469,7 +469,7 @@ export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray<u8>
                const aslidei = aslide[i]
                if (aslidei > 0) {
                        gepair_p1p1_to_p3(u, t)
-                       gepair_add_cached(t, u, Ai[aslidei >> 1], t_fe_0)
+                       gepair_add_cached(t, u, Ai[aslidei >> 1])
                } else if (aslidei < 0) {
                        gepair_p1p1_to_p3(u, t)
                        gepair_sub_cached(t, u, Ai[(-aslidei) >> 1])
index e1e009685f9e1723febd0f5e273458a565f8c249..ffb46623050c6339d6d75eec59f3bc5fcb1aa8a8 100644 (file)
@@ -3,7 +3,7 @@
 //! SPDX-License-Identifier: ISC
 
 import { FieldElement, fe_add, fe_dbl, fe_invert, fe_isnegative, fe_mul, fe_mul_dual, fe_sub, fe_tobytes } from './fe'
-import { FieldElementPair, fep, fep_add, fep_blend, fep_mul, fep_mul_lane, fep_pack, fep_sq, fep_sq2, fep_sub, fep_swap, fep_unpack, fep_unpack_a } from './fep'
+import { FieldElementPair, fep, fep_add, fep_blend, fep_mul, fep_mul2, fep_mul_lane, fep_pack, fep_sq, fep_sq2, fep_sub, fep_subadd, fep_swap, fep_unpack, fep_unpack_a } from './fep'
 import { ge_cached, ge_p1p1, ge_p2, ge_p3, ge_precomp } from './p'
 
 /**
@@ -110,15 +110,6 @@ function gepair_p2_to_fep (a: FieldElementPair, b: FieldElementPair, p: ge_p2, t
        fep_pack(b, t, p.Z)
 }
 
-/**
- * Interleaves (X:Y:Z:T) projective coordinates as X|Y and X+Y|Z, ignoring T.
- */
-function gepair_p3_to_fep (a: FieldElementPair, b: FieldElementPair, p: ge_p3, t: FieldElement): void {
-       fe_add(t, p.X, p.Y)
-       fep_pack(a, p.X, p.Y)
-       fep_pack(b, t, p.Z)
-}
-
 /**
  * `r = 2p`
  *
@@ -165,7 +156,9 @@ export function gepair_p2_dbl (r: ge_p1p1, p: ge_p2, t: FieldElement): void {
  * ```
  */
 export function gepair_p3_dbl (r: ge_p1p1, p: ge_p3, t: FieldElement): void {
-       gepair_p3_to_fep(ge_p2_dbl_pA, ge_p2_dbl_pB, p, t)
+       fe_add(t, p.X, p.Y)
+       fep_pack(ge_p2_dbl_pA, p.X, p.Y)
+       fep_pack(ge_p2_dbl_pB, t, p.Z)
 
        fep_sq(ge_p2_dbl_tA, ge_p2_dbl_pA) // (X², Y²)
        fep_sq2(ge_p2_dbl_tB, ge_p2_dbl_pB) // ((X+Y)², 2Z²)
@@ -180,6 +173,14 @@ export function gepair_p3_dbl (r: ge_p1p1, p: ge_p3, t: FieldElement): void {
        fep_unpack(r.X, r.T, ge_p2_dbl_rB) // X' = (X+Y)²-(Y²+X²), T' = 2Z²-(Y²-X²)
 }
 
+const gepair_add_cached_pA = fep()
+const gepair_add_cached_pB = fep()
+const gepair_add_cached_pC = fep()
+const gepair_add_cached_pD = fep()
+const gepair_add_cached_tA = fep()
+const gepair_add_cached_tB = fep()
+const gepair_add_cached_rA = fep()
+const gepair_add_cached_rB = fep()
 /**
  * `r = p + q`
  *
@@ -190,16 +191,27 @@ export function gepair_p3_dbl (r: ge_p1p1, p: ge_p3, t: FieldElement): void {
  * T = 2Z₁Z₂ - 2dT₁T₂
  * ```
  */
-export function gepair_add_cached (r: ge_p1p1, p: ge_p3, q: ge_cached, t: FieldElement): void {
+export function gepair_add_cached (r: ge_p1p1, p: ge_p3, q: ge_cached): void {
        fe_add(r.X, p.Y, p.X)
        fe_sub(r.Y, p.Y, p.X)
-       fe_mul_dual(r.Z, r.X, q.YplusX, r.Y, r.Y, q.YminusX)
-       fe_mul_dual(r.T, q.T2d, p.T, r.X, p.Z, q.Z)
-       fe_dbl(t, r.X)
-       fe_sub(r.X, r.Z, r.Y)
-       fe_add(r.Y, r.Z, r.Y)
-       fe_add(r.Z, t, r.T)
-       fe_sub(r.T, t, r.T)
+
+       fep_pack(gepair_add_cached_pA, r.X, r.Y)
+       fep_pack(gepair_add_cached_pB, q.YplusX, q.YminusX)
+       fep_pack(gepair_add_cached_pC, p.T, p.Z)
+       fep_pack(gepair_add_cached_pD, q.T2d, q.Z)
+
+       fep_mul(gepair_add_cached_pA, gepair_add_cached_pA, gepair_add_cached_pB) // ((Y₁+X₁)(Y₂+X₂), (Y₁-X₁)(Y₂-X₂))
+       fep_swap(gepair_add_cached_pB, gepair_add_cached_pA) // ((Y₁-X₁)(Y₂-X₂), (Y₁+X₁)(Y₂+X₂))
+       fep_subadd(gepair_add_cached_pA, gepair_add_cached_pB) // ((Y₁+X₁)(Y₂+X₂) - (Y₁-X₁)(Y₂-X₂), (Y₁-X₁)(Y₂-X₂) - (Y₁+X₁)(Y₂+X₂))
+       fep_blend(gepair_add_cached_rA, gepair_add_cached_pA, gepair_add_cached_pB) // ((Y₁+X₁)(Y₂+X₂) - (Y₁-X₁)(Y₂-X₂), (Y₁+X₁)(Y₂+X₂) + (Y₁-X₁)(Y₂-X₂)) i.e. (X', Y')
+
+       fep_mul2(gepair_add_cached_pC, gepair_add_cached_pC, gepair_add_cached_pD) // (2T₁T₂d, 2Z₁Z₂)
+       fep_swap(gepair_add_cached_pD, gepair_add_cached_pC) // (2Z₁Z₂, 2T₁T₂d)
+       fep_subadd(gepair_add_cached_pC, gepair_add_cached_pD) // (2Z₁Z₂ + 2T₁T₂d, 2T₁T₂d + 2Z₁Z₂)
+       fep_blend(gepair_add_cached_rB, gepair_add_cached_pD, gepair_add_cached_pC) // (2Z₁Z₂ + 2T₁T₂d, 2Z₁Z₂ - 2T₁T₂d) i.e. (Z', T')
+
+       fep_unpack(r.X, r.Y, gepair_add_cached_rA) // X' = (Y₁+X₁)(Y₂+X₂) - (Y₁-X₁)(Y₂-X₂), Y' = (Y₁+X₁)(Y₂+X₂) + (Y₁-X₁)(Y₂-X₂)
+       fep_unpack(r.Z, r.T, gepair_add_cached_rB) // Z' = 2Z₁Z₂ + 2T₁T₂d, T' = 2Z₁Z₂ - 2T₁T₂d
 }
 
 /**