]> git.codecow.com Git - nano25519.git/commitdiff
Transition more fe pair functions.
authorChris Duncan <chris@codecow.com>
Sat, 24 Oct 2026 08:01:13 +0000 (01:01 -0700)
committerChris Duncan <chris@codecow.com>
Sat, 24 Oct 2026 08:01:13 +0000 (01:01 -0700)
src/assembly/crypto_verify.ts
src/assembly/ed25519/fep.ts
src/assembly/ed25519/ge.ts
src/assembly/ed25519/p2.ts

index 58264bc8d14ec572c9acc2ac430fd846fcf78cfe..a9a9fa52e51abd2c78e4356d40fba50b55c51d91 100644 (file)
@@ -61,7 +61,7 @@ export function crypto_verify_donna (sig: StaticArray<u8>, M: StaticArray<u8>, A
        blake2b.init().update(sig, 32).update(pub, KEY_BYTELENGTH).update(M, BLOCKHASH_BYTELENGTH).digest(h)
        sc_reduce(h)
 
-       ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S, t_fe_0)
+       ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S, t_fe_0, t_fe_1, t_fe_2)
        ge_p3_tobytes(check_r, sb_ah, t_fe_0, t_fe_1, t_fe_2, t_fe_3, t_fe_4, t_fe_5, t_fe_6)
        clear_t()
 
@@ -89,7 +89,7 @@ export function crypto_verify_sodium (sig: StaticArray<u8>, M: StaticArray<u8>,
        blake2b.init().update(sig, 32).update(pub, 32).update(M, mlen).digest(h)
        sc_reduce(h)
 
-       ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S, t_fe_0)
+       ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S, t_fe_0, t_fe_1, t_fe_2)
        ge_sub_p3(check, expected_r, sb_ah, t_fe_0, t_ge_cached, t_ge_p1p1)
 
        // after cofactoring, `check` should be small order and not malformed in order
@@ -123,7 +123,7 @@ export function crypto_verify_strict (sig: StaticArray<u8>, M: StaticArray<u8>,
        blake2b.init().update(sig, 32).update(pub, 32).update(M, mlen).digest(h)
        sc_reduce(h)
 
-       ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S, t_fe_0)
+       ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S, t_fe_0, t_fe_1, t_fe_2)
        ge_p3_tobytes(check_r, sb_ah, t_fe_0, t_fe_1, t_fe_2, t_fe_3, t_fe_4, t_fe_5, t_fe_6)
        clear_t()
        return equalbytes(sig, check_r, KEY_BYTELENGTH)
index de7091ec34ad6caf1a9bd09d490bd9093a081e18..a2ad4b14d8250b42b94863a4f7d1697243de66fb 100644 (file)
@@ -15,7 +15,7 @@
  *
  * `n = 2⁰a[0] + 2²⁶a[1] + 2⁷⁷a[2] ... + 2²³⁰a[9]`
  */
-import { FieldElement } from './fe'
+import { FieldElement, fe_mul } from './fe'
 // import { load_3, load_4 } from './utils'
 
 /**
@@ -31,6 +31,26 @@ export function fep (): FieldElementPair {
        return new StaticArray<i32>(20)
 }
 
+/**
+ * Set all values of a FieldElementPair to zero.
+ *
+ * @param {FieldElementPair} h destination
+ */
+export function fep_0 (h: FieldElementPair): void {
+       memory.fill(changetype<usize>(h), 0, 80)
+}
+
+/**
+ * Set the first limb of each FieldElement in the pair to 1 and all subsequent
+ * limbs to zero.
+ *
+ * @param {FieldElementPair} h destination
+ */
+export function fep_1 (h: FieldElementPair): void {
+       fep_0(h)
+       store<i64>(changetype<usize>(h), (<i64>1 << 32) | 1, 0)
+}
+
 export function fep_pack (f: FieldElementPair, f_a: FieldElement, f_b: FieldElement): void {
        for (let i = 0, j = 0; i < 10; i++, j += 2) {
                f[j] = f_a[i]
@@ -38,6 +58,18 @@ export function fep_pack (f: FieldElementPair, f_a: FieldElement, f_b: FieldElem
        }
 }
 
+export function fep_unpack_a (f_a: FieldElement, f: FieldElementPair): void {
+       for (let i = 0, j = 0; i < 10; i++, j += 2) {
+               f_a[i] = f[j]
+       }
+}
+
+function fep_unpack_b (f_b: FieldElement, f: FieldElementPair): void {
+       for (let i = 0, j = 1; i < 10; i++, j += 2) {
+               f_b[i] = f[j]
+       }
+}
+
 export function fep_unpack (f_a: FieldElement, f_b: FieldElement, f: FieldElementPair): void {
        for (let i = 0, j = 0; i < 10; i++, j += 2) {
                f_a[i] = f[j]
@@ -46,23 +78,23 @@ export function fep_unpack (f_a: FieldElement, f_b: FieldElement, f: FieldElemen
 }
 
 /**
- * Set all values of a FieldElementPair to zero.
- *
- * @param {FieldElementPair} h destination
+ * Turns `[a0, b0, a1, b1, ...]` into `[a0, a1, ..., b0, b1, ...]`
  */
-export function fep_0 (h: FieldElementPair): void {
-       memory.fill(changetype<usize>(h), 0, 80)
+export function fep_unweave (h: FieldElementPair, f: FieldElementPair): void {
+       for (let i = 0, j = 0; i < 10; i++, j += 2) {
+               h[i] = f[j]
+               h[i + 10] = f[j + 1]
+       }
 }
 
 /**
- * Set the first limb of each FieldElement in the pair to 1 and all subsequent
- * limbs to zero.
- *
- * @param {FieldElementPair} h destination
+ * Turns `[a0, a1, ..., b0, b1, ...]` into `[a0, b0, a1, b1, ...]`
  */
-export function fep_1 (h: FieldElementPair): void {
-       fep_0(h)
-       store<i64>(changetype<usize>(h), (<i64>1 << 32) | 1, 0)
+export function fep_weave (h: FieldElementPair, f: FieldElementPair): void {
+       for (let i = 0, j = 0; i < 10; i++, j += 2) {
+               h[j] = f[i]
+               h[j + 1] = f[i + 10]
+       }
 }
 
 /**
@@ -610,6 +642,21 @@ 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 the first interleaved lane of two FieldElementPairs and store the
+ * product.
+ *
+ * @param {FieldElementPair} h product destination
+ * @param {FieldElementPair} f multiplicand source
+ * @param {FieldElementPair} g multiplicand source
+ */
+export function fep_mul_lane (h: FieldElementPair, f: FieldElementPair, g: FieldElementPair, t_fe_0: FieldElement, t_fe_1: FieldElement, t_fe_2: FieldElement): void {
+       fep_unpack(t_fe_0, t_fe_2, f)
+       fep_unpack(t_fe_1, t_fe_2, g)
+       fe_mul(t_fe_2, t_fe_0, t_fe_1)
+       fep_pack(h, t_fe_2, t_fe_0)
+}
+
 /**
  * Negate the values of a FieldElementPair and store the result.
  *
index b5e5f58e9212596fbc7eebd3fbacb18574d34bed..103f68a1b7136b34bc9fa7148961b751a3a0e500 100644 (file)
@@ -20,8 +20,8 @@
 import { base } from './base'
 import { base2 } from './base2'
 import { FieldElement, fe, fe_0, fe_1, fe_add, fe_cmov, fe_frombytes, fe_isnegative, fe_iszero, fe_mul, fe_neg, fe_pow22523, fe_sq, fe_sub } from './fe'
-import { ge_add_cached, ge_add_precomp, ge_cached, ge_p1p1, ge_p1p1_0, ge_p1p1_to_p2, ge_p1p1_to_p3, ge_p2, ge_p2_0, ge_p2_dbl, ge_p3, ge_p3_0, ge_p3_dbl, ge_p3_to_cached, ge_p3_tobytes, ge_precomp, ge_precomp_0, ge_sub_cached, ge_sub_precomp } from './p'
-import { gepair_p1p1_to_p3, gepair_p2_dbl } from './p2'
+import { ge_add_cached, ge_add_precomp, ge_cached, ge_p1p1, ge_p1p1_0, ge_p1p1_to_p2, ge_p1p1_to_p3, ge_p2, ge_p2_0, ge_p2_dbl, ge_p3, ge_p3_0, ge_p3_to_cached, ge_p3_tobytes, ge_precomp, ge_precomp_0 } from './p'
+import { gepair_add_cached, gepair_add_precomp, gepair_p1p1_to_p2, gepair_p1p1_to_p3, gepair_p2_dbl, gepair_p3_dbl, gepair_sub_cached, gepair_sub_precomp } from './p2'
 
 /**
  * √-1
@@ -355,7 +355,7 @@ export function ge_scalarmult_base_tobytes (S: StaticArray<u8>, a: StaticArray<u
                ge_p1p1_to_p3(h, r)
        }
 
-       ge_p3_dbl(r, h, t_fe_0)
+       gepair_p3_dbl(r, h, t_fe_0)
        ge_p1p1_to_p2(s, r)
        gepair_p2_dbl(r, s, t_fe_0)
        ge_p1p1_to_p2(s, r)
@@ -408,7 +408,7 @@ const ge_double_scalarmult_vartime_A2: ge_p3 = new ge_p3()
  *
  * Variable-time; only used for signature verification.
  */
-export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray<u8>, A: ge_p3, b: StaticArray<u8>, t_fe: FieldElement): void {
+export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray<u8>, A: ge_p3, b: StaticArray<u8>, t_fe_0: FieldElement, t_fe_1: FieldElement, t_fe_2: FieldElement): void {
        const Bi = base2
        const aslide = ge_double_scalarmult_vartime_aslide
        const bslide = ge_double_scalarmult_vartime_bslide
@@ -425,34 +425,34 @@ export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray<u8>
 
        ge_p3_to_cached(Ai[0], A)
 
-       ge_p3_dbl(t, A, t_fe)
+       gepair_p3_dbl(t, A, t_fe_0)
        ge_p1p1_to_p3(A2, t)
 
-       ge_add_cached(t, A2, Ai[0], t_fe)
+       ge_add_cached(t, A2, Ai[0], t_fe_0)
        ge_p1p1_to_p3(u, t)
        ge_p3_to_cached(Ai[1], u)
 
-       ge_add_cached(t, A2, Ai[1], t_fe)
+       ge_add_cached(t, A2, Ai[1], t_fe_0)
        ge_p1p1_to_p3(u, t)
        ge_p3_to_cached(Ai[2], u)
 
-       ge_add_cached(t, A2, Ai[2], t_fe)
+       ge_add_cached(t, A2, Ai[2], t_fe_0)
        ge_p1p1_to_p3(u, t)
        ge_p3_to_cached(Ai[3], u)
 
-       ge_add_cached(t, A2, Ai[3], t_fe)
+       ge_add_cached(t, A2, Ai[3], t_fe_0)
        ge_p1p1_to_p3(u, t)
        ge_p3_to_cached(Ai[4], u)
 
-       ge_add_cached(t, A2, Ai[4], t_fe)
+       ge_add_cached(t, A2, Ai[4], t_fe_0)
        ge_p1p1_to_p3(u, t)
        ge_p3_to_cached(Ai[5], u)
 
-       ge_add_cached(t, A2, Ai[5], t_fe)
+       ge_add_cached(t, A2, Ai[5], t_fe_0)
        ge_p1p1_to_p3(u, t)
        ge_p3_to_cached(Ai[6], u)
 
-       ge_add_cached(t, A2, Ai[6], t_fe)
+       ge_add_cached(t, A2, Ai[6], t_fe_0)
        ge_p1p1_to_p3(u, t)
        ge_p3_to_cached(Ai[7], u)
 
@@ -464,34 +464,36 @@ export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray<u8>
        }
 
        while (i >= 0) {
-               ge_p2_dbl(t, p, t_fe)
+               gepair_p2_dbl(t, p, t_fe_0)
 
                const aslidei = aslide[i]
                if (aslidei > 0) {
                        gepair_p1p1_to_p3(u, t)
-                       ge_add_cached(t, u, Ai[aslidei >> 1], t_fe)
+                       gepair_add_cached(t, u, Ai[aslidei >> 1], t_fe_0)
                } else if (aslidei < 0) {
                        gepair_p1p1_to_p3(u, t)
-                       ge_sub_cached(t, u, Ai[(-aslidei) >> 1])
+                       gepair_sub_cached(t, u, Ai[(-aslidei) >> 1])
                }
 
                const bslidei = bslide[i]
                if (bslidei > 0) {
                        gepair_p1p1_to_p3(u, t)
-                       ge_add_precomp(t, u, Bi[bslidei >> 1])
+                       gepair_add_precomp(t, u, Bi[bslidei >> 1])
                } else if (bslidei < 0) {
                        gepair_p1p1_to_p3(u, t)
-                       ge_sub_precomp(t, u, Bi[(-bslidei) >> 1])
+                       gepair_sub_precomp(t, u, Bi[(-bslidei) >> 1])
                }
 
                if (i > 0) {
-                       ge_p1p1_to_p2(p, t)
+                       gepair_p1p1_to_p2(p, t, t_fe_0, t_fe_1, t_fe_2)
                } else {
                        gepair_p1p1_to_p3(r, t)
                }
                i--
        }
-       fe_0(t_fe)
+       fe_0(t_fe_0)
+       fe_0(t_fe_1)
+       fe_0(t_fe_2)
 }
 
 function slide_vartime (r: StaticArray<i8>, a: StaticArray<u8>): void {
index a81bdad35ca98a5b205779f78101f46a872d7c44..e1e009685f9e1723febd0f5e273458a565f8c249 100644 (file)
@@ -2,17 +2,9 @@
 //! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
 //! SPDX-License-Identifier: ISC
 
-import { FieldElement, fe, fe_0, fe_1, fe_add, fe_copy, fe_dbl, fe_invert, fe_isnegative, fe_mul, fe_mul_dual, fe_neg, fe_sq2_dual, fe_sq_dual, fe_sub, fe_tobytes } from './fe'
-import { ge_p1p1, ge_p2, ge_p3 } from './p'
-import { FieldElementPair, fep, fep_0, fep_1, fep_add, fep_blend, fep_copy, fep_dbl, fep_mul, fep_neg, fep_pack, fep_unpack, fep_sq, fep_sq2, fep_sub, fep_swap } from './fep'
-
-/**
- * 2d = −121665 / 60833
- *    = 16295367250680780974490674513165176452449235426866156013048779062215315747161
- */
-const ed25519_d2: FieldElement = StaticArray.fromArray<i32>([
-       -21827239, -5839606, -30745221, 13898782, 229458, 15978800, -12551817, -6495438, 29715968, 9444199, 0, 0
-])
+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 { ge_cached, ge_p1p1, ge_p2, ge_p3, ge_precomp } from './p'
 
 /**
  * Base class for building branded subclasses of coordinate systems.
@@ -26,84 +18,44 @@ class gepair_coord {
 /**
  * Completed `(x, y)` where`x = X/Z`, `y = Y/T`
  */
-export class gepair_p1p1 extends gepair_coord {
-       T: FieldElement = fe()
+class gepair_p1p1 extends gepair_coord {
+       T: FieldElementPair = fep()
        __brand: 'ge_p1p1' = 'ge_p1p1'
 }
 
 /**
  * Projective coordinates.
  */
-export class gepair_p2 extends gepair_coord {
+class gepair_p2 extends gepair_coord {
        __brand: 'gepair_p2' = 'gepair_p2'
 }
 
 /**
  * Extended projective coordinates.
  */
-export class gepair_p3 extends gepair_coord {
-       T: FieldElement = fe()
+class gepair_p3 extends gepair_coord {
+       T: FieldElementPair = fep()
        __brand: 'gepair_p3' = 'gepair_p3'
 }
 
 /**
  * Computed at runtime.
  */
-export class gepair_cached {
-       YplusX: FieldElement = fep()
-       YminusX: FieldElement = fep()
-       Z: FieldElement = fep()
-       T2d: FieldElement = fep()
+class gepair_cached {
+       YplusX: FieldElementPair = fep()
+       YminusX: FieldElementPair = fep()
+       Z: FieldElementPair = fep()
+       T2d: FieldElementPair = fep()
 }
 
 /**
  * Precomputed from base point and compiled into the module.
  * Requires an expensive `fe_invert` up front but saves one `fe_mul` at runtime.
  */
-export class gepair_precomp {
-       yplusx: FieldElement = fep()
-       yminusx: FieldElement = fep()
-       xy2d: FieldElement = fep()
-}
-
-export function ge_p2_0 (h: gepair_p2): void {
-       fe_0(h.X)
-       fe_1(h.Y)
-       fe_1(h.Z)
-}
-
-export function ge_p1p1_0 (h: gepair_p1p1): void {
-       fe_0(h.X)
-       fe_1(h.Y)
-       fe_1(h.Z)
-       fe_0(h.T)
-}
-
-export function ge_p3_0 (h: gepair_p3): void {
-       fe_0(h.X)
-       fe_1(h.Y)
-       fe_1(h.Z)
-       fe_0(h.T)
-}
-
-export function ge_precomp_0 (h: gepair_precomp): void {
-       fe_1(h.yplusx)
-       fe_1(h.yminusx)
-       fe_0(h.xy2d)
-}
-
-/**
- * r = p
- *
- * ```
- * X = X₁ * T₁
- * Y = Y₁ * Z₁
- * Z = Z₁ * T₁
- * ```
- */
-export function ge_p1p1_to_p2 (r: gepair_p2, p: gepair_p1p1): void {
-       fe_mul_dual(r.X, p.X, p.T, r.Y, p.Y, p.Z)
-       fe_mul(r.Z, p.Z, p.T)
+class gepair_precomp {
+       yplusx: FieldElementPair = fep()
+       yminusx: FieldElementPair = fep()
+       xy2d: FieldElementPair = fep()
 }
 
 /**
@@ -113,14 +65,16 @@ export function ge_p1p1_to_p2 (r: gepair_p2, p: gepair_p1p1): void {
  * X = X₁ * T₁
  * Y = Y₁ * Z₁
  * Z = Z₁ * T₁
- * T = X₁ * Y₁
  * ```
  */
-export function ge_p1p1_to_p3 (r: gepair_p3, p: gepair_p1p1): void {
-       fe_mul(r.X, p.X, p.T)
-       fe_mul(r.Y, p.Y, p.Z)
-       fe_mul(r.Z, p.Z, p.T)
-       fe_mul(r.T, p.X, p.Y)
+export function gepair_p1p1_to_p2 (r: ge_p2, p: ge_p1p1, t_fe_0: FieldElement, t_fe_1: FieldElement, t_fe_2: FieldElement): void {
+       fep_pack(pA, p.X, p.Z)
+       fep_pack(pB, p.T, p.Y)
+       fep_mul(rA, pA, pB)
+       fep_swap(pA, pA)
+       fep_mul_lane(rB, pA, pB, t_fe_0, t_fe_1, t_fe_2)
+       fep_unpack(r.X, r.Y, rA)
+       fep_unpack_a(r.Z, rB)
 }
 
 /**
@@ -148,33 +102,21 @@ export function gepair_p1p1_to_p3 (r: ge_p3, p: ge_p1p1): void {
 }
 
 /**
- * r = p
+ * Interleaves (X:Y:Z) projective coordinates as X|Y and X+Y|Z.
  */
-export function ge_p3_to_cached (r: gepair_cached, p: gepair_p3): void {
-       fe_add(r.YplusX, p.Y, p.X)
-       fe_sub(r.YminusX, p.Y, p.X)
-       fe_copy(r.Z, p.Z)
-       fe_mul(r.T2d, p.T, ed25519_d2)
+function gepair_p2_to_fep (a: FieldElementPair, b: FieldElementPair, p: ge_p2, 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`
- *
- * ```
- * X = (X₁ + Y₁)² - (Y₁² + X₁²)
- * Y = Y₁² + X₁²
- * Z = Y₁² - X₁²
- * T = 2Z₁² - (Y₁² - X₁²)
- * ```
+ * Interleaves (X:Y:Z:T) projective coordinates as X|Y and X+Y|Z, ignoring T.
  */
-export function ge_p2_dbl (r: gepair_p1p1, p: gepair_p2, t: FieldElement): void {
-       fe_add(r.Y, p.X, p.Y)
-       fe_sq_dual(r.X, p.X, r.Z, p.Y)
-       fe_sq2_dual(t, r.Y, r.T, p.Z)
-       fe_add(r.Y, r.Z, r.X)
-       fe_sub(r.Z, r.Z, r.X)
-       fe_sub(r.X, t, r.Y)
-       fe_sub(r.T, r.T, r.Z)
+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)
 }
 
 /**
@@ -197,9 +139,7 @@ const ge_p2_dbl_tE = fep()
 const ge_p2_dbl_rA = fep()
 const ge_p2_dbl_rB = fep()
 export function gepair_p2_dbl (r: ge_p1p1, p: ge_p2, t: FieldElement): void {
-       fe_add(t, p.X, p.Y)
-       fep_pack(ge_p2_dbl_pA, p.X, p.Y) // (X, Y)
-       fep_pack(ge_p2_dbl_pB, t, p.Z) // (X+Y, Z)
+       gepair_p2_to_fep(ge_p2_dbl_pA, ge_p2_dbl_pB, p, t)
 
        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²)
@@ -224,14 +164,20 @@ export function gepair_p2_dbl (r: ge_p1p1, p: ge_p2, t: FieldElement): void {
  * T = 2Z₁² - (Y₁² - X₁²)
  * ```
  */
-export function ge_p3_dbl (r: gepair_p1p1, p: gepair_p3, t: FieldElement): void {
-       fe_add(r.Y, p.X, p.Y)
-       fe_sq_dual(r.X, p.X, r.Z, p.Y)
-       fe_sq2_dual(t, r.Y, r.T, p.Z)
-       fe_add(r.Y, r.Z, r.X)
-       fe_sub(r.Z, r.Z, r.X)
-       fe_sub(r.X, t, r.Y)
-       fe_sub(r.T, r.T, r.Z)
+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)
+
+       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²)
+
+       fep_swap(ge_p2_dbl_tC, ge_p2_dbl_tA) // (Y², X²)
+       fep_add(ge_p2_dbl_tD, ge_p2_dbl_tC, ge_p2_dbl_tA) // (Y²+X², X²+Y²)
+       fep_sub(ge_p2_dbl_tE, ge_p2_dbl_tA, ge_p2_dbl_tC) // (X²-Y², Y²-X²)
+       fep_blend(ge_p2_dbl_rA, ge_p2_dbl_tD, ge_p2_dbl_tE) // (Y²+X², Y²-X²) i.e. (Y', Z')
+       fep_sub(ge_p2_dbl_rB, ge_p2_dbl_tB, ge_p2_dbl_rA) // ((X+Y)²-Y', 2Z²-Z') i.e. (X', T')
+
+       fep_unpack(r.Y, r.Z, ge_p2_dbl_rA) // Y' = Y²+X², Z' = Y²-X²
+       fep_unpack(r.X, r.T, ge_p2_dbl_rB) // X' = (X+Y)²-(Y²+X²), T' = 2Z²-(Y²-X²)
 }
 
 /**
@@ -244,7 +190,7 @@ export function ge_p3_dbl (r: gepair_p1p1, p: gepair_p3, t: FieldElement): void
  * T = 2Z₁Z₂ - 2dT₁T₂
  * ```
  */
-export function ge_add_cached (r: gepair_p1p1, p: gepair_p3, q: gepair_cached, t: FieldElement): void {
+export function gepair_add_cached (r: ge_p1p1, p: ge_p3, q: ge_cached, t: FieldElement): 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)
@@ -262,7 +208,7 @@ export function ge_add_cached (r: gepair_p1p1, p: gepair_p3, q: gepair_cached, t
  * `p.Z` is doubled and stored to itself because its value is not referenced
  * again before being reassigned at all three call sites of this function.
  */
-export function ge_add_precomp (r: gepair_p1p1, p: gepair_p3, q: gepair_precomp): void {
+export function gepair_add_precomp (r: ge_p1p1, p: ge_p3, q: ge_precomp): 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)
@@ -274,25 +220,13 @@ export function ge_add_precomp (r: gepair_p1p1, p: gepair_p3, q: gepair_precomp)
        fe_sub(r.T, p.Z, r.T)
 }
 
-/* r = p-q */
-export function ge_sub_p3 (r: gepair_p3, p: gepair_p3, q: gepair_p3, t_fe: FieldElement, t_ge_cached: gepair_cached, t_ge_p1p1: gepair_p1p1): void {
-       fe_neg(r.X, q.X)
-       fe_copy(r.Y, q.Y)
-       fe_copy(r.Z, q.Z)
-       fe_neg(r.T, q.T)
-
-       ge_p3_to_cached(t_ge_cached, r)
-       ge_add_cached(t_ge_p1p1, p, t_ge_cached, t_fe)
-       ge_p1p1_to_p3(r, t_ge_p1p1)
-}
-
 /**
  * `r = p - q`
  *
  * `r.X` is doubled and stored to `p.X` because `p` is not referenced again
  * before being reassigned at the only call site of this function.
  */
-export function ge_sub_cached (r: gepair_p1p1, p: gepair_p3, q: gepair_cached): void {
+export function gepair_sub_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.YminusX, r.Y, r.Y, q.YplusX)
@@ -310,7 +244,7 @@ export function ge_sub_cached (r: gepair_p1p1, p: gepair_p3, q: gepair_cached):
  * `p.Z` is doubled and stored to itself because its value is not referenced
  * again before being reassigned at the only call site of this function.
  */
-export function ge_sub_precomp (r: gepair_p1p1, p: gepair_p3, q: gepair_precomp): void {
+export function gepair_sub_precomp (r: ge_p1p1, p: ge_p3, q: ge_precomp): 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.yminusx, r.Y, r.Y, q.yplusx)
@@ -322,7 +256,7 @@ export function ge_sub_precomp (r: gepair_p1p1, p: gepair_p3, q: gepair_precomp)
        fe_add(r.T, p.Z, r.T)
 }
 
-export function ge_p3_tobytes (s: StaticArray<u8>, h: gepair_p3,
+function ge_p3_tobytes (s: StaticArray<u8>, h: gepair_p3,
        reciprocal: FieldElement, x: FieldElement, y: FieldElement,
        t_fe_0: FieldElement, t_fe_1: FieldElement, t_fe_2: FieldElement, t_fe_3: FieldElement
 ): void {