]> git.codecow.com Git - nano25519.git/commitdiff
Reduce boilerplate code by building and calling point arithmetic functions internally.
authorChris Duncan <chris@codecow.com>
Thu, 5 Nov 2026 08:08:21 +0000 (00:08 -0800)
committerChris Duncan <chris@codecow.com>
Thu, 5 Nov 2026 08:08:21 +0000 (00:08 -0800)
src/assembly/crypto_verify.ts
src/assembly/ed25519/ge/double_scalarmult.ts
src/assembly/ed25519/ge/ge.ts
src/assembly/ed25519/p.ts
test/arithmetic.mjs

index 74276876e8a94bcdbc1d8ec48b231648f78b23d2..3bdbb302b0ae123affa92360e3ae4b57964c02f1 100644 (file)
@@ -4,7 +4,8 @@
 import { BLOCKHASH_BYTELENGTH, KEY_BYTELENGTH } from './constants'
 import { Blake2b } from './ed25519/blake2b'
 import { ge_accept_small_order, ge_cache_odd_multiples, ge_double_scalarmult_vartime_to_p3, ge_frombytes, ge_frombytes_negate, ge_is_canonical, ge_reject_small_order } from './ed25519/ge'
-import { p3, p3_0, p3_dbl_to_p3, p3_sub_p3, p3_tobytes, p_cached } from './ed25519/p'
+import { ge_double_scalarmult_vartime_to_cached } from './ed25519/ge/double_scalarmult'
+import { p3, p3_0, p3_dbl_to_p3, p3_sub_cached_to_p3, p3_tobytes, p_cached } from './ed25519/p'
 import { sc_is_canonical, sc_reduce } from './ed25519/sc'
 import { equalbytes } from './ed25519/utils'
 
@@ -29,6 +30,7 @@ const S = new StaticArray<u8>(32)
 const expected_r = new p3()
 const h = new StaticArray<u8>(64)
 const sb_ah = new p3()
+const sb_ah_cached = new p_cached()
 const check = new p3()
 const check_r = new StaticArray<u8>(32)
 
@@ -104,8 +106,8 @@ export function crypto_verify_sodium (sig: StaticArray<u8>, M: StaticArray<u8>,
 
        p3_dbl_to_p3(A2, A)
        ge_cache_odd_multiples(Ai, A2, A)
-       ge_double_scalarmult_vartime_to_p3(sb_ah, h, Ai, S)
-       p3_sub_p3(check, expected_r, sb_ah)
+       ge_double_scalarmult_vartime_to_cached(sb_ah_cached, h, Ai, S)
+       p3_sub_cached_to_p3(check, expected_r, sb_ah_cached)
 
        // after cofactoring, `check` should be small order and not malformed in order
        // to pass, unlike `verify_pubkey()` and `verify_signature()` below
index fdedb01729c99882d9e500c670f327d08c73c18c..8030f9436aa0c853b7093792991d5198dab20dbf 100644 (file)
@@ -18,7 +18,7 @@
  * - precomp (Duif): (y+x,y-x,2dxy)
 */
 import { base2 as Bi } from '../constants/base2'
-import { p1p1, p1p1_to_p2, p1p1_to_p3, p2, p2_0, p2_dbl, p3, p3_0, p3_add_cached, p3_add_precomp, p3_sub_cached, p3_sub_precomp, p_cached } from '../p'
+import { p1p1, p1p1_0, p1p1_to_cached, p1p1_to_p2, p1p1_to_p3, p2, p2_0, p2_dbl, p3, p3_add_cached, p3_add_precomp, p3_sub_cached, p3_sub_precomp, p_cached } from '../p'
 
 const aslide: StaticArray<i8> = new StaticArray<i8>(256)
 const bslide: StaticArray<i8> = new StaticArray<i8>(256)
@@ -41,8 +41,18 @@ const u: p3 = new p3()
  * Variable-time; only used for signature verification.
  */
 export function ge_double_scalarmult_vartime_to_p3 (r: p3, a: StaticArray<u8>, Ai: StaticArray<p_cached>, b: StaticArray<u8>): void {
-       p3_0(r)
+       ge_double_scalarmult_vartime_to_p1p1(t, a, Ai, b)
+       p1p1_to_p3(r, t)
+       p1p1_0(t)
+}
+
+export function ge_double_scalarmult_vartime_to_cached (r: p_cached, a: StaticArray<u8>, Ai: StaticArray<p_cached>, b: StaticArray<u8>): void {
+       ge_double_scalarmult_vartime_to_p1p1(t, a, Ai, b)
+       p1p1_to_cached(r, t)
+       p1p1_0(t)
+}
 
+function ge_double_scalarmult_vartime_to_p1p1 (r: p1p1, a: StaticArray<u8>, Ai: StaticArray<p_cached>, b: StaticArray<u8>): void {
        slide_vartime(aslide, a)
        slide_vartime(bslide, b)
 
@@ -54,30 +64,28 @@ export function ge_double_scalarmult_vartime_to_p3 (r: p3, a: StaticArray<u8>, A
        }
 
        while (i >= 0) {
-               p2_dbl(t, p)
+               p2_dbl(r, p)
 
                const aslidei = aslide[i]
                if (aslidei > 0) {
-                       p1p1_to_p3(u, t)
+                       p1p1_to_p3(u, r)
                        p3_add_cached(t, u, Ai[aslidei >> 1])
                } else if (aslidei < 0) {
-                       p1p1_to_p3(u, t)
-                       p3_sub_cached(t, u, Ai[(-aslidei) >> 1])
+                       p1p1_to_p3(u, r)
+                       p3_sub_cached(r, u, Ai[(-aslidei) >> 1])
                }
 
                const bslidei = bslide[i]
                if (bslidei > 0) {
-                       p1p1_to_p3(u, t)
-                       p3_add_precomp(t, u, Bi[bslidei >> 1])
+                       p1p1_to_p3(u, r)
+                       p3_add_precomp(r, u, Bi[bslidei >> 1])
                } else if (bslidei < 0) {
-                       p1p1_to_p3(u, t)
-                       p3_sub_precomp(t, u, Bi[(-bslidei) >> 1])
+                       p1p1_to_p3(u, r)
+                       p3_sub_precomp(r, u, Bi[(-bslidei) >> 1])
                }
 
                if (i > 0) {
-                       p1p1_to_p2(p, t)
-               } else {
-                       p1p1_to_p3(r, t)
+                       p1p1_to_p2(p, r)
                }
                i--
        }
index 8cc31ee48fde04a11b9d2571f03872b89c6ba43a..ec846feff3fc2e32c020b0e41010200b56d5f3db 100644 (file)
@@ -18,7 +18,7 @@
  * - precomp (Duif): (y+x,y-x,2dxy)
 */
 import { FieldElement, fe, fe_1, fe_add, fe_addsub, fe_cmov, fe_frombytes, fe_isnegative, fe_iszero, fe_mul, fe_mul_d, fe_mul_pair_sqrtm1, fe_mul_sqrtm1, fe_neg, fe_sq, fe_sub } from '../fe'
-import { p3, p3_add_cached_output_cached, p3_to_cached, p_cached } from '../p'
+import { p3, p3_add_cached_to_cached, p3_to_cached, p_cached } from '../p'
 import { fe_pow22523 } from '../z'
 
 const ge_frombytes_u: FieldElement = fe()
@@ -183,6 +183,6 @@ export function ge_is_canonical (s: StaticArray<u8>): i32 {
 export function ge_cache_odd_multiples (Ai: StaticArray<p_cached>, A2: p3, A: p3): void {
        p3_to_cached(Ai[0], A)
        for (let i = 0; i < 7; i++) {
-               p3_add_cached_output_cached(Ai[i + 1], A2, Ai[i])
+               p3_add_cached_to_cached(Ai[i + 1], A2, Ai[i])
        }
 }
index d5b3795291a476868802ce9f5d0a641c04b0c3b6..caba4739bafb740c5a2c191bdba45d970d32d38a 100644 (file)
@@ -2,7 +2,7 @@
 //! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
 //! SPDX-License-Identifier: ISC
 
-import { FieldElement, fe, fe_0, fe_1, fe_add, fe_addsub, fe_copy, fe_dbl, fe_isnegative, fe_mul, fe_mul_2d, fe_mul_pair, fe_mul_pair_2d, fe_mul_pair_dbl, fe_mul_pair_dbl_2d, fe_sq_pair, fe_sq_pair_dbl, fe_sub, fe_tobytes } from './fe'
+import { FieldElement, fe, fe_0, fe_1, fe_add, fe_addsub, fe_copy, fe_dbl, fe_isnegative, fe_mul, fe_mul_2d, fe_mul_pair, fe_mul_pair_2d, fe_mul_pair_dbl, fe_sq_pair, fe_sq_pair_dbl, fe_sub, fe_tobytes } from './fe'
 import { fe_invert } from './z'
 
 /**
@@ -57,7 +57,7 @@ export class p_precomp {
        xy2d: FieldElement = fe()
 }
 
-const t_p3 = new p3()
+const t = new p1p1()
 
 export function p2_0 (h: p2): void {
        fe_0(h.X)
@@ -165,6 +165,25 @@ export function p2_dbl (r: p1p1, p: p2): void {
        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₁²)
+ * ```
+ */
+export function p3_dbl (r: p1p1, p: p3): void {
+       fe_add(r.X, p.X, p.Y)
+       fe_sq_pair_dbl(r.X, r.X, r.T, p.Z)
+       fe_sq_pair(r.Z, p.X, r.Y, p.Y)
+       fe_addsub(r.Y, r.Z, r.Y, r.Z)
+       fe_sub(r.X, r.X, r.Y)
+       fe_sub(r.T, r.T, r.Z)
+}
+
 /**
  * `r = 2p`
  *
@@ -185,17 +204,9 @@ export function p2_dbl (r: p1p1, p: p2): void {
  * ```
  */
 export function p3_dbl_to_p3 (r: p3, p: p3): void {
-       fe_add(t_p3.X, p.X, p.Y)
-       fe_sq_pair_dbl(t_p3.X, t_p3.X, t_p3.T, p.Z)
-       fe_sq_pair(t_p3.Z, p.X, t_p3.Y, p.Y)
-       fe_addsub(t_p3.Y, t_p3.Z, t_p3.Y, t_p3.Z)
-       fe_sub(t_p3.X, t_p3.X, t_p3.Y)
-       fe_sub(t_p3.T, t_p3.T, t_p3.Z)
-
-       fe_mul_pair(r.X, t_p3.X, t_p3.T, r.T, t_p3.X, t_p3.Y)
-       fe_mul_pair(r.Y, t_p3.Y, t_p3.Z, r.Z, t_p3.Z, t_p3.T)
-
-       p3_0(t_p3)
+       p3_dbl(t, p)
+       p1p1_to_p3(r, t)
+       p1p1_0(t)
 }
 
 /**
@@ -217,17 +228,9 @@ export function p3_dbl_to_p3 (r: p3, p: p3): void {
  * ```
  */
 export function p3_dbl_to_p2 (r: p2, p: p3): void {
-       fe_add(t_p3.T, p.X, p.Y)
-       fe_sq_pair_dbl(t_p3.X, t_p3.T, t_p3.T, p.Z)
-       fe_sq_pair(t_p3.Z, p.X, t_p3.Y, p.Y)
-       fe_addsub(t_p3.Y, t_p3.Z, t_p3.Y, t_p3.Z)
-       fe_sub(t_p3.X, t_p3.X, t_p3.Y)
-       fe_sub(t_p3.T, t_p3.T, t_p3.Z)
-
-       fe_mul_pair(r.X, t_p3.X, t_p3.T, r.Y, t_p3.Y, t_p3.Z)
-       fe_mul(r.Z, t_p3.Z, t_p3.T)
-
-       p3_0(t_p3)
+       p3_dbl(t, p)
+       p1p1_to_p2(r, t)
+       p1p1_0(t)
 }
 
 /**
@@ -267,20 +270,10 @@ export function p3_add_cached (r: p1p1, p: p3, q: p_cached): void {
  * T2d = 2X₁Y₁d
  * ```
  */
-export function p3_add_cached_output_cached (r: p_cached, p: p3, q: p_cached): void {
-       // p3 + cached
-       fe_addsub(t_p3.X, t_p3.Y, p.Y, p.X)
-       fe_mul_pair(t_p3.X, t_p3.X, q.YplusX, t_p3.Y, t_p3.Y, q.YminusX)
-       fe_mul_pair_dbl(t_p3.Z, p.Z, q.Z, t_p3.T, q.T2d, p.T)
-       fe_addsub(t_p3.X, t_p3.Y, t_p3.X, t_p3.Y)
-       fe_addsub(t_p3.Z, t_p3.T, t_p3.Z, t_p3.T)
-
-       // p1p1 -> cached
-       fe_mul_pair_2d(t_p3.Y, t_p3.Y, t_p3.T, r.T2d, t_p3.Y, t_p3.X)
-       fe_mul_pair(t_p3.X, t_p3.X, t_p3.Z, r.Z, t_p3.Z, t_p3.T)
-       fe_addsub(r.YplusX, r.YminusX, t_p3.X, t_p3.Y)
-
-       p3_0(t_p3)
+export function p3_add_cached_to_cached (r: p_cached, p: p3, q: p_cached): void {
+       p3_add_cached(t, p, q)
+       p1p1_to_cached(r, t)
+       p1p1_0(t)
 }
 
 /**
@@ -312,18 +305,10 @@ export function p3_add_precomp (r: p1p1, p: p3, q: p_precomp): void {
  * T = X₃Y₃ = ((Y₁+X₁)(Y₂-X₂) - (Y₁-X₁)(Y₂+X₂))((Y₁+X₁)(Y₂-X₂) + (Y₁-X₁)(Y₂+X₂))
  * ```
  */
-export function p3_sub_p3 (r: p3, p: p3, q: p3): void {
-       fe_addsub(t_p3.X, t_p3.Y, p.Y, p.X) // X = Y₁+X₁, Y = Y₁-X₁
-       fe_addsub(t_p3.Z, t_p3.T, q.Y, q.X) // Z = Y₂+X₂, T = Y₂-X₂
-       fe_mul_pair(t_p3.X, t_p3.X, t_p3.T, t_p3.Y, t_p3.Y, t_p3.Z) // X = (Y₁+X₁)(Y₂-X₂), Y = (Y₁-X₁)(Y₂+X₂)
-       fe_mul_pair_dbl_2d(t_p3.Z, p.Z, q.Z, t_p3.T, p.T, q.T) // Z = 2Z₁Z₂, T = 2T₁T₂d
-       fe_addsub(t_p3.Y, t_p3.X, t_p3.X, t_p3.Y) // Y = (Y₁+X₁)(Y₂-X₂) + (Y₁-X₁)(Y₂+X₂), X = (Y₁+X₁)(Y₂-X₂) - (Y₁-X₁)(Y₂+X₂)
-       fe_addsub(t_p3.T, t_p3.Z, t_p3.Z, t_p3.T) // t = 2Z₁Z₂ + 2T₁T₂d, Z = 2Z₁Z₂ - 2T₁T₂d
-
-       fe_mul_pair(r.Y, t_p3.Y, t_p3.Z, r.T, t_p3.X, t_p3.Y)
-       fe_mul_pair(r.X, t_p3.X, t_p3.T, r.Z, t_p3.Z, t_p3.T)
-
-       p3_0(t_p3)
+export function p3_sub_cached_to_p3 (r: p3, p: p3, q: p_cached): void {
+       p3_sub_cached(t, p, q)
+       p1p1_to_p3(r, t)
+       p1p1_0(t)
 }
 
 /**
@@ -337,9 +322,9 @@ export function p3_sub_p3 (r: p3, p: p3, q: p3): void {
  * ```
  */
 export function p3_sub_cached (r: p1p1, p: p3, q: p_cached): void {
-       fe_addsub(r.X, r.Y, p.Y, p.X)
-       fe_mul_pair(r.X, r.X, q.YminusX, r.Y, r.Y, q.YplusX)
-       fe_mul_pair_dbl(r.Z, p.Z, q.Z, r.T, q.T2d, p.T)
+       fe_addsub(r.X, r.Y, p.Y, p.X) // X = Y₁+X₁, Y = Y₁-X₁
+       fe_mul_pair(r.X, r.X, q.YminusX, r.Y, r.Y, q.YplusX) // X = (Y₁+X₁)(Y₂-X₂), Y = (Y₁-X₁)(Y₂+X₂)
+       fe_mul_pair_dbl(r.Z, p.Z, q.Z, r.T, q.T2d, p.T) // Z = 2Z₁Z₂, T = 2T₁T₂d
        fe_addsub(r.Y, r.X, r.X, r.Y)
        fe_addsub(r.T, r.Z, r.Z, r.T)
 }
@@ -364,9 +349,9 @@ export function p3_sub_precomp (r: p1p1, p: p3, q: p_precomp): void {
 }
 
 export function p3_tobytes (s: StaticArray<u8>, h: p3): void {
-       fe_invert(t_p3.T, h.Z)
-       fe_mul_pair(t_p3.X, h.X, t_p3.T, t_p3.Y, h.Y, t_p3.T)
-       fe_tobytes(s, t_p3.Y)
-       s[31] ^= fe_isnegative(t_p3.X) << 7
-       p3_0(t_p3)
+       fe_invert(t.T, h.Z)
+       fe_mul_pair(t.X, h.X, t.T, t.Y, h.Y, t.T)
+       fe_tobytes(s, t.Y)
+       s[31] ^= fe_isnegative(t.X) << 7
+       p1p1_0(t)
 }
index 2a695ff4bd8b4587d64c8071ffa76c3d073ce513..647ea2c8694982a4bcea5692ce3d5bde720c835e 100644 (file)
@@ -66,7 +66,7 @@ const HARNESS = `
 import './env'
 import { FieldElement, fe, fe_0, fe_1, fe_add, fe_addsub, fe_cmov, fe_copy, fe_dbl, fe_frombytes, fe_isnegative, fe_iszero, fe_mul, fe_mul_2d, fe_mul_d, fe_mul_pair, fe_mul_pair_2d, fe_mul_pair_dbl, fe_mul_pair_dbl_2d, fe_mul_pair_sqrtm1, fe_mul_sqrtm1, fe_neg, fe_reduce, fe_sq, fe_sq_n, fe_sq_pair, fe_sq_pair_dbl, fe_sub, fe_tobytes } from './ed25519/fe'
 import { ge_cache_odd_multiples, ge_double_scalarmult_vartime_to_p3, ge_frombytes, ge_frombytes_negate, ge_accept_small_order, ge_reject_small_order, ge_is_canonical, ge_scalarmult_base_tobytes } from './ed25519/ge'
-import { p3, p3_dbl_to_p3, p3_sub_p3, p3_tobytes, p_cached } from './ed25519/p'
+import { p3, p3_dbl_to_p3, p3_sub_cached_to_p3, p3_tobytes, p_cached } from './ed25519/p'
 import { fe_invert, fe_pow22523 } from './ed25519/z'
 import { sc_is_canonical, sc_muladd, sc_reduce } from './ed25519/sc'
 import { verify_pubkey, verify_signature } from './crypto_verify'
@@ -159,7 +159,7 @@ export function ge_double_scalarmult_ (): void {
        ge_cache_odd_multiples(AI, A2, PA)
        ge_double_scalarmult_vartime_to_p3(PB, U8A, AI, U8B)
 }
-export function p3_sub_p3_ (): void { p3_sub_p3(PC, PA, PB) }
+export function p3_sub_cached_to_p3_ (): void { p3_sub_cached_to_p3(PC, PA, PB) }
 export function verify_pubkey_ (): i32 { return verify_pubkey(U8A) ? 1 : 0 }
 export function verify_signature_ (): i32 { return verify_signature(U8W) ? 1 : 0 }
 `
@@ -289,9 +289,9 @@ function doubleScalarmult (h, note, count) {
                // R - ([a]A + [b]B), as verify() computes it, with R decoded so Z = 1
                const R = mul(B, randomInt(L))
                setPoint(PA, R)
-               x.p3_sub_p3_()
+               x.p3_sub_cached_to_p3()
                const diff = getPoint(PC)
-               note('p3_sub_p3 of the double scalar multiplication', equal(diff, add(R, negate(want))) && isExtended(diff), k)
+               note('p3_sub_cached_to_p3 of the double scalar multiplication', equal(diff, add(R, negate(want))) && isExtended(diff), k)
        }
        // both scalars zero right after a call that left a point in r
        setPoint(PA, mul(B, 12345n))