]> git.codecow.com Git - nano25519.git/commitdiff
Move up scratch buffers.
authorChris Duncan <chris@codecow.com>
Fri, 9 Oct 2026 06:07:40 +0000 (23:07 -0700)
committerChris Duncan <chris@codecow.com>
Fri, 9 Oct 2026 06:07:40 +0000 (23:07 -0700)
src/assembly/crypto_derive.ts
src/assembly/crypto_sign.ts
src/assembly/ed25519/fe.ts
src/assembly/ed25519/ge.ts
src/assembly/ed25519/p.ts

index 2fa58aa586e0636b9ed081c9a41b171c76eb011b..539d069f8b3492ea08b86d459b78cf381d16418e 100644 (file)
@@ -2,6 +2,7 @@
 //! SPDX-License-Identifier: GPL-3.0-or-later
 
 import { Blake2b } from './ed25519/blake2b'
+import { FieldElement, fe } from './ed25519/fe'
 import { ge_scalarmult_base_tobytes } from './ed25519/ge'
 import { clamp } from './ed25519/utils'
 
@@ -16,6 +17,9 @@ const blake2b = new Blake2b()
 const h: StaticArray<u8> = new StaticArray<u8>(SECRETKEY_BYTES)
 const s: StaticArray<u8> = new StaticArray<u8>(PRIVATEKEY_BYTES)
 
+// temporary buffers
+const t = StaticArray.fromArray<FieldElement>([fe(), fe(), fe(), fe(), fe(), fe(), fe()])
+
 /**
  * Hash seed to 32-byte scalar `a`, clamp it, then point-multiply it by the
  * Ed25519 base point. The Nano specification uses BLAKE2b as the hash function
@@ -39,6 +43,6 @@ export function crypto_derive (A: StaticArray<u8>, key: StaticArray<u8>): void {
        clamp(s)
 
        // Perform fixed-base scalar multiplication `[s]B`, output to public key `A`
-       ge_scalarmult_base_tobytes(A, s)
+       ge_scalarmult_base_tobytes(A, s, t[0], t[1], t[2], t[3], t[4], t[5], t[6])
        s.fill(0)
 }
index 5b2ace8a3bc36d61deb0c740a42ff88b44e1607f..2e0aee75eabdccda6c35ceee6875a048895a3d53 100644 (file)
@@ -4,6 +4,7 @@
 import { KEY_BYTELENGTH } from './constants'
 import { crypto_derive } from './crypto_derive'
 import { Blake2b } from './ed25519/blake2b'
+import { FieldElement, fe } from './ed25519/fe'
 import { ge_scalarmult_base_tobytes } from './ed25519/ge'
 import { sc_muladd, sc_reduce } from './ed25519/sc'
 import { clamp } from './ed25519/utils'
@@ -20,6 +21,7 @@ const r = new StaticArray<u8>(64)
 const R = new StaticArray<u8>(64)
 const k = new StaticArray<u8>(64)
 const S = new StaticArray<u8>(64)
+const t = StaticArray.fromArray<FieldElement>([fe(), fe(), fe(), fe(), fe(), fe(), fe()])
 /**
  * Sign a message with a private key. The Nano specification uses BLAKE2b as the
  * hash function instead of SHA-512 specified by RFC 8032.
@@ -67,7 +69,7 @@ export function crypto_sign (RS: StaticArray<u8>, M: StaticArray<u8>, mlen: i32,
        sc_reduce(r)
 
        // Perform fixed-base scalar multiplication `[r]B`, output to point `R`
-       ge_scalarmult_base_tobytes(R, r)
+       ge_scalarmult_base_tobytes(R, r, t[0], t[1], t[2], t[3], t[4], t[5], t[6])
 
        // Compute challenge hash `blake2b(R || A || M)`, output to `k`
        blake2b.init().update(R, 32).update(A, 32).update(M, mlen).digest(k)
index 393f3d2197cde47234870ea186bd0291560a3e9b..0c00f9c6ae4382ad5ecb3c2b63c93d605dfd7e3b 100644 (file)
@@ -181,11 +181,7 @@ export function fe_frombytes (h: FieldElement, s: StaticArray<u8>): void {
 /**
  * Inversion - sets `out` = 0 if `z` == 0
  */
-export function fe_invert (out: FieldElement, z: FieldElement, fe_scratch_0: FieldElement, fe_scratch_1: FieldElement, fe_scratch_2: FieldElement, fe_scratch_3: FieldElement): void {
-       const t0: FieldElement = fe_scratch_0
-       const t1: FieldElement = fe_scratch_1
-       const t2: FieldElement = fe_scratch_2
-       const t3: FieldElement = fe_scratch_3
+export function fe_invert (out: FieldElement, z: FieldElement, t0: FieldElement, t1: FieldElement, t2: FieldElement, t3: FieldElement): void {
        fe_sq(t0, z)
        fe_sq(t1, t0)
        fe_sq(t1, t1)
@@ -239,9 +235,9 @@ export function fe_invert (out: FieldElement, z: FieldElement, fe_scratch_0: Fie
  * Preconditions:
  * |f| bounded by 1.1x2²⁶,1.1x2²⁵,1.1x2²⁶,1.1x2²⁵,etc.
  */
-export function fe_isnegative (f: FieldElement, fe_scratch: FieldElement): u8 {
-       fe_reduce(fe_scratch, f)
-       return u8(fe_scratch[0] & 1)
+export function fe_isnegative (f: FieldElement, t: FieldElement): u8 {
+       fe_reduce(t, f)
+       return u8(t[0] & 1)
 }
 
 /**
@@ -251,8 +247,8 @@ export function fe_isnegative (f: FieldElement, fe_scratch: FieldElement): u8 {
  * Preconditions:
  * |f| bounded by 1.1x2²⁶,1.1x2²⁵,1.1x2²⁶,1.1x2²⁵,etc.
  */
-export function fe_iszero (f: FieldElement, u8x32_scratch: StaticArray<u8>, fe_scratch: FieldElement): u8 {
-       fe_tobytes(u8x32_scratch, f, fe_scratch)
+export function fe_iszero (f: FieldElement, u8x32_scratch: StaticArray<u8>, t: FieldElement): u8 {
+       fe_tobytes(u8x32_scratch, f, t)
        let d: i32 = 0
        for (let i: i32 = 0; i < 32; i++) {
                d |= u8x32_scratch[i]
@@ -533,11 +529,7 @@ export function fe_neg (h: FieldElement, f: FieldElement): void {
  * returns z^((p-5)/8) = z^(2²⁵²-3)
  * used to compute square roots since we have p=5 (mod 8); see Cohen and Frey.
  */
-export function fe_pow22523 (out: FieldElement, z: FieldElement, fe_scratch_0: FieldElement, fe_scratch_1: FieldElement, fe_scratch_2: FieldElement): void {
-       const t0 = fe_scratch_0
-       const t1 = fe_scratch_1
-       const t2 = fe_scratch_2
-
+export function fe_pow22523 (out: FieldElement, z: FieldElement, t0: FieldElement, t1: FieldElement, t2: FieldElement): void {
        fe_sq(t0, z)
        fe_sq(t1, t0)
        fe_sq(t1, t1)
@@ -1094,9 +1086,9 @@ export function fe_sub (h: FieldElement, f: FieldElement, g: FieldElement): void
  *
  * Goal: Output h0+...+2²³⁰ h9.
  */
-export function fe_tobytes (s: StaticArray<u8>, h: FieldElement, fe_scratch: FieldElement): void {
-       fe_reduce(fe_scratch, h)
-       const t_ptr: usize = changetype<usize>(fe_scratch)
+export function fe_tobytes (s: StaticArray<u8>, h: FieldElement, t: FieldElement): void {
+       fe_reduce(t, h)
+       const t_ptr: usize = changetype<usize>(t)
        const t0 = load<i32>(t_ptr, 0)
        const t1 = load<i32>(t_ptr, 4)
        const t2 = load<i32>(t_ptr, 8)
index 16161b6c3a907505ffe702ffe5e29d8e1f0eedee..d2b418e79c7a86645c8997f0e4dd1267676cd284 100644 (file)
@@ -237,8 +237,8 @@ export function ge_frombytes (h: ge_p3, s: StaticArray<u8>): i32 {
 
 const ge_has_small_order_y_sqrtm1: FieldElement = fe()
 const ge_has_small_order_c: FieldElement = fe()
-const ge_has_small_order_u8x32_scratch = new StaticArray<u8>(32)
-const ge_has_small_order_fe_scratch: FieldElement = fe()
+const ge_has_small_order_t_u8x32 = new StaticArray<u8>(32)
+const ge_has_small_order_t_fe: FieldElement = fe()
 /**
  * @param {ge_p3} p
  * @returns 1 if `p` has small order, else 0
@@ -247,19 +247,19 @@ export function ge_has_small_order (p: ge_p3): i32 {
        const y_sqrtm1 = ge_has_small_order_y_sqrtm1
        const c = ge_has_small_order_c
 
-       const u8x32_scratch = ge_has_small_order_u8x32_scratch
-       const fe_scratch = ge_has_small_order_fe_scratch
+       const t_u8x32 = ge_has_small_order_t_u8x32
+       const t_fe = ge_has_small_order_t_fe
 
        let ret: i32 = 0
 
-       ret |= fe_iszero(p.X, u8x32_scratch, fe_scratch)
-       ret |= fe_iszero(p.Y, u8x32_scratch, fe_scratch)
-       ret |= fe_iszero(p.Z, u8x32_scratch, fe_scratch)
+       ret |= fe_iszero(p.X, t_u8x32, t_fe)
+       ret |= fe_iszero(p.Y, t_u8x32, t_fe)
+       ret |= fe_iszero(p.Z, t_u8x32, t_fe)
        fe_mul(y_sqrtm1, p.Y, fe_sqrtm1)
        fe_sub(c, y_sqrtm1, p.X)
-       ret |= fe_iszero(c, u8x32_scratch, fe_scratch)
+       ret |= fe_iszero(c, t_u8x32, t_fe)
        fe_add(c, y_sqrtm1, p.X)
-       ret |= fe_iszero(c, u8x32_scratch, fe_scratch)
+       ret |= fe_iszero(c, t_u8x32, t_fe)
 
        return ret
 }
@@ -311,19 +311,14 @@ const ge_scalarmult_base_tobytes_fe_scratch_6: FieldElement = fe()
  * @param S 32-byte output buffer for scalar * base point
  * @param a 32-byte input buffer for scalar
  */
-export function ge_scalarmult_base_tobytes (S: StaticArray<u8>, a: StaticArray<u8>): void {
+export function ge_scalarmult_base_tobytes (S: StaticArray<u8>, a: StaticArray<u8>,
+       t0: FieldElement, t1: FieldElement, t2: FieldElement, t3: FieldElement, t4: FieldElement, t5: FieldElement, t6: FieldElement
+): void {
        const h = ge_scalarmult_base_tobytes_h
        const e = ge_scalarmult_base_tobytes_e
        const r = ge_scalarmult_base_tobytes_r
        const s = ge_scalarmult_base_tobytes_s
        const t = ge_scalarmult_base_tobytes_t
-       const fe_scratch_0 = ge_scalarmult_base_tobytes_fe_scratch_0
-       const fe_scratch_1 = ge_scalarmult_base_tobytes_fe_scratch_1
-       const fe_scratch_2 = ge_scalarmult_base_tobytes_fe_scratch_2
-       const fe_scratch_3 = ge_scalarmult_base_tobytes_fe_scratch_3
-       const fe_scratch_4 = ge_scalarmult_base_tobytes_fe_scratch_4
-       const fe_scratch_5 = ge_scalarmult_base_tobytes_fe_scratch_5
-       const fe_scratch_6 = ge_scalarmult_base_tobytes_fe_scratch_6
 
        for (let i = 0; i < 32; ++i) {
                e[(i << 1) + 0] = (a[i] >> 0) & 15
@@ -351,13 +346,13 @@ export function ge_scalarmult_base_tobytes (S: StaticArray<u8>, a: StaticArray<u
                ge_p1p1_to_p3(h, r)
        }
 
-       ge_p3_dbl(r, h, fe_scratch_0)
+       ge_p3_dbl(r, h, t0)
        ge_p1p1_to_p2(s, r)
-       ge_p2_dbl(r, s, fe_scratch_0)
+       ge_p2_dbl(r, s, t0)
        ge_p1p1_to_p2(s, r)
-       ge_p2_dbl(r, s, fe_scratch_0)
+       ge_p2_dbl(r, s, t0)
        ge_p1p1_to_p2(s, r)
-       ge_p2_dbl(r, s, fe_scratch_0)
+       ge_p2_dbl(r, s, t0)
        ge_p1p1_to_p3(h, r)
 
        // base pointer offset = 4 bytes * 12 values * 3 fe * 8 ge = 1152 bytes
@@ -368,7 +363,7 @@ export function ge_scalarmult_base_tobytes (S: StaticArray<u8>, a: StaticArray<u
        }
 
        ge_p3_tobytes(S, h,
-               fe_scratch_0, fe_scratch_1, fe_scratch_2, fe_scratch_3, fe_scratch_4, fe_scratch_5, fe_scratch_6
+               t0, t1, t2, t3, t4, t5, t6
        )
 
        // clear sensitive data
@@ -377,13 +372,13 @@ export function ge_scalarmult_base_tobytes (S: StaticArray<u8>, a: StaticArray<u
        ge_precomp_0(t)
        ge_p3_0(h)
        e.fill(0)
-       fe_scratch_0.fill(0)
-       fe_scratch_1.fill(0)
-       fe_scratch_2.fill(0)
-       fe_scratch_3.fill(0)
-       fe_scratch_4.fill(0)
-       fe_scratch_5.fill(0)
-       fe_scratch_6.fill(0)
+       t0.fill(0)
+       t1.fill(0)
+       t2.fill(0)
+       t3.fill(0)
+       t4.fill(0)
+       t5.fill(0)
+       t6.fill(0)
 }
 
 const ge_double_scalarmult_vartime_aslide: StaticArray<i8> = new StaticArray<i8>(256)
index 6e39622b6c6a301ddccc64f60541fc176aecb217..655509ff1899ec2c80ec1ee92edc31d58ed49ccc 100644 (file)
@@ -108,49 +108,48 @@ export function ge_p3_to_cached (r: ge_cached, p: ge_p3): void {
 /**
  * r = 2 * p
  */
-export function ge_p2_dbl (r: ge_p1p1, p: ge_p2, fe_scratch: FieldElement): void {
+export function ge_p2_dbl (r: ge_p1p1, p: ge_p2, t: FieldElement): void {
        fe_sq(r.X, p.X)
        fe_sq(r.Z, p.Y)
        fe_sq2(r.T, p.Z)
        fe_add(r.Y, p.X, p.Y)
-       fe_sq(fe_scratch, r.Y)
+       fe_sq(t, r.Y)
        fe_add(r.Y, r.Z, r.X)
        fe_sub(r.Z, r.Z, r.X)
-       fe_sub(r.X, fe_scratch, r.Y)
+       fe_sub(r.X, t, r.Y)
        fe_sub(r.T, r.T, r.Z)
 }
 
 /**
  * r = 2 * p
  */
-export function ge_p3_dbl (r: ge_p1p1, p: ge_p3, fe_scratch: FieldElement): void {
+export function ge_p3_dbl (r: ge_p1p1, p: ge_p3, t: FieldElement): void {
        fe_sq(r.X, p.X)
        fe_sq(r.Z, p.Y)
        fe_sq2(r.T, p.Z)
        fe_add(r.Y, p.X, p.Y)
-       fe_sq(fe_scratch, r.Y)
+       fe_sq(t, r.Y)
        fe_add(r.Y, r.Z, r.X)
        fe_sub(r.Z, r.Z, r.X)
-       fe_sub(r.X, fe_scratch, r.Y)
+       fe_sub(r.X, t, r.Y)
        fe_sub(r.T, r.T, r.Z)
 }
 
 /**
  * r = p + q
  */
-export function ge_add_cached (r: ge_p1p1, p: ge_p3, q: ge_cached, fe_scratch: FieldElement): void {
-       const t0 = fe_scratch
+export function ge_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(r.Z, r.X, q.YplusX)
        fe_mul(r.Y, r.Y, q.YminusX)
        fe_mul(r.T, q.T2d, p.T)
        fe_mul(r.X, p.Z, q.Z)
-       fe_dbl(t0, r.X)
+       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, t0, r.T)
-       fe_sub(r.T, t0, r.T)
+       fe_add(r.Z, t, r.T)
+       fe_sub(r.T, t, r.T)
 }
 
 /**
@@ -173,15 +172,15 @@ export function ge_add_precomp (r: ge_p1p1, p: ge_p3, q: ge_precomp): void {
 }
 
 /* r = p-q */
-export function ge_sub_p3 (r: ge_p3, p: ge_p3, q: ge_p3, fe_scratch: FieldElement, ge_cached_scratch: ge_cached, ge_p1p1_scratch: ge_p1p1): void {
+export function ge_sub_p3 (r: ge_p3, p: ge_p3, q: ge_p3, t_fe: FieldElement, t_ge_cached: ge_cached, t_ge_p1p1: ge_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(ge_cached_scratch, r)
-       ge_add_cached(ge_p1p1_scratch, p, ge_cached_scratch, fe_scratch)
-       ge_p1p1_to_p3(r, ge_p1p1_scratch)
+       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)
 }
 
 /**
@@ -224,17 +223,12 @@ export function ge_sub_precomp (r: ge_p1p1, p: ge_p3, q: ge_precomp): void {
 }
 
 export function ge_p3_tobytes (s: StaticArray<u8>, h: ge_p3,
-       reciprocal_scratch: FieldElement,
-       x_scratch: FieldElement,
-       y_scratch: FieldElement,
-       fe_scratch_0: FieldElement,
-       fe_scratch_1: FieldElement,
-       fe_scratch_2: FieldElement,
-       fe_scratch_3: FieldElement
+       reciprocal: FieldElement, x: FieldElement, y: FieldElement,
+       t0: FieldElement, t1: FieldElement, t2: FieldElement, t3: FieldElement
 ): void {
-       fe_invert(reciprocal_scratch, h.Z, fe_scratch_0, fe_scratch_1, fe_scratch_2, fe_scratch_3)
-       fe_mul(x_scratch, h.X, reciprocal_scratch)
-       fe_mul(y_scratch, h.Y, reciprocal_scratch)
-       fe_tobytes(s, y_scratch, fe_scratch_0)
-       s[31] ^= fe_isnegative(x_scratch, fe_scratch_0) << 7
+       fe_invert(reciprocal, h.Z, t0, t1, t2, t3)
+       fe_mul(x, h.X, reciprocal)
+       fe_mul(y, h.Y, reciprocal)
+       fe_tobytes(s, y, t0)
+       s[31] ^= fe_isnegative(x, t0) << 7
 }