]> git.codecow.com Git - nano25519.git/commitdiff
Start migrating point arithmetic in paired form.
authorChris Duncan <chris@codecow.com>
Fri, 23 Oct 2026 20:51:12 +0000 (13:51 -0700)
committerChris Duncan <chris@codecow.com>
Fri, 23 Oct 2026 20:51:12 +0000 (13:51 -0700)
src/assembly/ed25519/p2.ts [new file with mode: 0644]

diff --git a/src/assembly/ed25519/p2.ts b/src/assembly/ed25519/p2.ts
new file mode 100644 (file)
index 0000000..61c09ad
--- /dev/null
@@ -0,0 +1,333 @@
+//! SPDX-FileCopyrightText: 2013-2026 Frank Denis <j@pureftpd.org>
+//! 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
+])
+
+/**
+ * Base class for building branded subclasses of coordinate systems.
+ */
+class gepair_coord {
+       X: FieldElementPair = fep()
+       Y: FieldElementPair = fep()
+       Z: FieldElementPair = fep()
+}
+
+/**
+ * Completed `(x, y)` where`x = X/Z`, `y = Y/T`
+ */
+export class gepair_p1p1 extends gepair_coord {
+       T: FieldElement = fe()
+       __brand: 'ge_p1p1' = 'ge_p1p1'
+}
+
+/**
+ * Projective coordinates.
+ */
+export class gepair_p2 extends gepair_coord {
+       __brand: 'gepair_p2' = 'gepair_p2'
+}
+
+/**
+ * Extended projective coordinates.
+ */
+export class gepair_p3 extends gepair_coord {
+       T: FieldElement = fe()
+       __brand: 'gepair_p3' = 'gepair_p3'
+}
+
+/**
+ * Computed at runtime.
+ */
+export class gepair_cached {
+       YplusX: FieldElement = fep()
+       YminusX: FieldElement = fep()
+       Z: FieldElement = fep()
+       T2d: FieldElement = 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)
+}
+
+/**
+ * r = p
+ *
+ * ```
+ * 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)
+}
+
+/**
+ * r = p
+ *
+ * ```
+ * X = X₁ * T₁
+ * Y = Y₁ * Z₁
+ * Z = Z₁ * T₁
+ * T = X₁ * Y₁
+ * ```
+ */
+const pA = fep()
+const pB = fep()
+const rA = fep()
+const rB = fep()
+export function gepair_p1p1_to_p3 (r: ge_p3, p: ge_p1p1): 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(rB, pA, pB)
+       fep_unpack(r.X, r.Y, rA)
+       fep_unpack(r.Z, r.T, rB)
+}
+
+/**
+ * r = p
+ */
+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)
+}
+
+/**
+ * `r = 2p`
+ *
+ * ```
+ * X = (X₁ + Y₁)² - (Y₁² + X₁²)
+ * Y = Y₁² + X₁²
+ * Z = Y₁² - X₁²
+ * T = 2Z₁² - (Y₁² - X₁²)
+ * ```
+ */
+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)
+}
+
+/**
+ * `r = 2p`
+ *
+ * ```
+ * X = (X₁ + Y₁)² - (Y₁² + X₁²)
+ * Y = Y₁² + X₁²
+ * Z = Y₁² - X₁²
+ * T = 2Z₁² - (Y₁² - X₁²)
+ * ```
+ */
+const ge_p2_dbl_pA = fep()
+const ge_p2_dbl_pB = fep()
+const ge_p2_dbl_tA = fep()
+const ge_p2_dbl_tB = fep()
+const ge_p2_dbl_tC = fep()
+const ge_p2_dbl_tD = fep()
+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.Z)
+       fep_pack(ge_p2_dbl_pB, t, p.Z)
+
+       fep_sq(ge_p2_dbl_tA, pA)
+       fep_sq2(ge_p2_dbl_tB, pB)
+
+       fep_swap(ge_p2_dbl_tC, ge_p2_dbl_tA)
+       fep_add(ge_p2_dbl_tD, ge_p2_dbl_tA, ge_p2_dbl_tC)
+       fep_sub(ge_p2_dbl_tE, ge_p2_dbl_tA, ge_p2_dbl_tC)
+       fep_blend(ge_p2_dbl_rA, ge_p2_dbl_tD, ge_p2_dbl_tE)
+       fep_sub(ge_p2_dbl_rB, ge_p2_dbl_tB, ge_p2_dbl_rA)
+
+       fep_unpack(r.X, r.Y, ge_p2_dbl_rA)
+       fep_unpack(r.Z, r.T, ge_p2_dbl_rB)
+}
+
+/**
+ * `r = 2p`
+ *
+ * ```
+ * X = (X₁ + Y₁)² - (Y₁² + X₁²)
+ * Y = Y₁² + X₁²
+ * Z = Y₁² - X₁²
+ * 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)
+}
+
+/**
+ * `r = p + q`
+ *
+ * ```
+ * X = (Y₁ + X₁)(Y₂ + X₂) - (Y₁ - X₁)(Y₂ - X₂)
+ * Y = (Y₁ + X₁)(Y₂ + X₂) + (Y₁ - X₁)(Y₂ - X₂)
+ * Z = 2Z₁Z₂ + 2dT₁T₂
+ * T = 2Z₁Z₂ - 2dT₁T₂
+ * ```
+ */
+export function ge_add_cached (r: gepair_p1p1, p: gepair_p3, q: gepair_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)
+       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)
+}
+
+/**
+ * `r = p + q`
+ *
+ * `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 {
+       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(r.T, q.xy2d, p.T)
+       fe_dbl(p.Z, p.Z)
+       fe_sub(r.X, r.Z, r.Y)
+       fe_add(r.Y, r.Z, r.Y)
+       fe_add(r.Z, p.Z, r.T)
+       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 {
+       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)
+       fe_mul_dual(r.T, q.T2d, p.T, r.X, p.Z, q.Z)
+       fe_dbl(p.X, r.X)
+       fe_sub(r.X, r.Z, r.Y)
+       fe_add(r.Y, r.Z, r.Y)
+       fe_sub(r.Z, p.X, r.T)
+       fe_add(r.T, p.X, r.T)
+}
+
+/**
+ * `r = p - q`
+ *
+ * `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 {
+       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)
+       fe_mul(r.T, q.xy2d, p.T)
+       fe_dbl(p.Z, p.Z)
+       fe_sub(r.X, r.Z, r.Y)
+       fe_add(r.Y, r.Z, r.Y)
+       fe_sub(r.Z, p.Z, r.T)
+       fe_add(r.T, p.Z, r.T)
+}
+
+export 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 {
+       fe_invert(reciprocal, h.Z, t_fe_0, t_fe_1, t_fe_2, t_fe_3)
+       fe_mul_dual(x, h.X, reciprocal, y, h.Y, reciprocal)
+       fe_tobytes(s, y, t_fe_0)
+       s[31] ^= fe_isnegative(x, t_fe_0) << 7
+}