From: Chris Duncan Date: Fri, 9 Oct 2026 06:07:40 +0000 (-0700) Subject: Move up scratch buffers. X-Git-Tag: v2.0.0~2^2~7 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=4495985181a80bbfcba34bfcda913a3b963da46c;p=nano25519.git Move up scratch buffers. --- diff --git a/src/assembly/crypto_derive.ts b/src/assembly/crypto_derive.ts index 2fa58aa..539d069 100644 --- a/src/assembly/crypto_derive.ts +++ b/src/assembly/crypto_derive.ts @@ -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 = new StaticArray(SECRETKEY_BYTES) const s: StaticArray = new StaticArray(PRIVATEKEY_BYTES) +// temporary buffers +const t = StaticArray.fromArray([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, key: StaticArray): 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) } diff --git a/src/assembly/crypto_sign.ts b/src/assembly/crypto_sign.ts index 5b2ace8..2e0aee7 100644 --- a/src/assembly/crypto_sign.ts +++ b/src/assembly/crypto_sign.ts @@ -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(64) const R = new StaticArray(64) const k = new StaticArray(64) const S = new StaticArray(64) +const t = StaticArray.fromArray([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, M: StaticArray, 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) diff --git a/src/assembly/ed25519/fe.ts b/src/assembly/ed25519/fe.ts index 393f3d2..0c00f9c 100644 --- a/src/assembly/ed25519/fe.ts +++ b/src/assembly/ed25519/fe.ts @@ -181,11 +181,7 @@ export function fe_frombytes (h: FieldElement, s: StaticArray): 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, fe_scratch: FieldElement): u8 { - fe_tobytes(u8x32_scratch, f, fe_scratch) +export function fe_iszero (f: FieldElement, u8x32_scratch: StaticArray, 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, h: FieldElement, fe_scratch: FieldElement): void { - fe_reduce(fe_scratch, h) - const t_ptr: usize = changetype(fe_scratch) +export function fe_tobytes (s: StaticArray, h: FieldElement, t: FieldElement): void { + fe_reduce(t, h) + const t_ptr: usize = changetype(t) const t0 = load(t_ptr, 0) const t1 = load(t_ptr, 4) const t2 = load(t_ptr, 8) diff --git a/src/assembly/ed25519/ge.ts b/src/assembly/ed25519/ge.ts index 16161b6..d2b418e 100644 --- a/src/assembly/ed25519/ge.ts +++ b/src/assembly/ed25519/ge.ts @@ -237,8 +237,8 @@ export function ge_frombytes (h: ge_p3, s: StaticArray): 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(32) -const ge_has_small_order_fe_scratch: FieldElement = fe() +const ge_has_small_order_t_u8x32 = new StaticArray(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, a: StaticArray): void { +export function ge_scalarmult_base_tobytes (S: StaticArray, a: StaticArray, + 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, a: StaticArray, a: StaticArray, a: StaticArray = new StaticArray(256) diff --git a/src/assembly/ed25519/p.ts b/src/assembly/ed25519/p.ts index 6e39622..655509f 100644 --- a/src/assembly/ed25519/p.ts +++ b/src/assembly/ed25519/p.ts @@ -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, 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 }