From 3c9d8e6b6d281ce09a1eac7d137b8036814f8355 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Fri, 9 Oct 2026 09:37:41 -0700 Subject: [PATCH] Reduce module size by sharing temp buffers among all three crypto_ functions. --- src/assembly/crypto_derive.ts | 7 ++----- src/assembly/crypto_sign.ts | 6 +++--- src/assembly/crypto_verify.ts | 29 ++++++++++++++--------------- src/assembly/ed25519/ge.ts | 22 ++++++++++++---------- src/assembly/ed25519/t.ts | 18 ++++++++++++++++++ src/assembly/env.ts | 6 ++---- 6 files changed, 51 insertions(+), 37 deletions(-) create mode 100644 src/assembly/ed25519/t.ts diff --git a/src/assembly/crypto_derive.ts b/src/assembly/crypto_derive.ts index 539d069..d38e072 100644 --- a/src/assembly/crypto_derive.ts +++ b/src/assembly/crypto_derive.ts @@ -2,8 +2,8 @@ //! 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 { t_fe_0, t_fe_1, t_fe_2, t_fe_3, t_fe_4, t_fe_5, t_fe_6 } from './ed25519/t' import { clamp } from './ed25519/utils' const PRIVATEKEY_BYTES: i32 = 32 @@ -17,9 +17,6 @@ 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 @@ -43,6 +40,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, t[0], t[1], t[2], t[3], t[4], t[5], t[6]) + ge_scalarmult_base_tobytes(A, s, t_fe_0, t_fe_1, t_fe_2, t_fe_3, t_fe_4, t_fe_5, t_fe_6) s.fill(0) } diff --git a/src/assembly/crypto_sign.ts b/src/assembly/crypto_sign.ts index 2e0aee7..10c459b 100644 --- a/src/assembly/crypto_sign.ts +++ b/src/assembly/crypto_sign.ts @@ -4,9 +4,9 @@ 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 { t_fe_0, t_fe_1, t_fe_2, t_fe_3, t_fe_4, t_fe_5, t_fe_6 } from './ed25519/t' import { clamp } from './ed25519/utils' // crypto_hash function @@ -21,7 +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. @@ -69,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, t[0], t[1], t[2], t[3], t[4], t[5], t[6]) + ge_scalarmult_base_tobytes(R, r, t_fe_0, t_fe_1, t_fe_2, t_fe_3, t_fe_4, t_fe_5, t_fe_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/crypto_verify.ts b/src/assembly/crypto_verify.ts index 2977e74..506a8ec 100644 --- a/src/assembly/crypto_verify.ts +++ b/src/assembly/crypto_verify.ts @@ -3,10 +3,10 @@ import { BLOCKHASH_BYTELENGTH, KEY_BYTELENGTH } from './constants' import { Blake2b } from './ed25519/blake2b' -import { FieldElement, fe } from './ed25519/fe' import { ge_double_scalarmult_vartime_to_p3, ge_frombytes, ge_frombytes_negate_vartime, ge_has_small_order, ge_is_canonical } from './ed25519/ge' import { ge_cached, ge_p1p1, ge_p3, ge_p3_tobytes, ge_sub_p3 } from './ed25519/p' import { sc_is_canonical, sc_reduce } from './ed25519/sc' +import { t_fe_0, t_fe_1, t_fe_2, t_fe_3, t_fe_4, t_fe_5, t_fe_6 } from './ed25519/t' import { equalbytes } from './ed25519/utils' // crypto_hash function @@ -23,7 +23,6 @@ const check_r = new StaticArray(32) const t_ge_cached = new ge_cached() const t_ge_p1p1 = new ge_p1p1() const t_u8x32 = new StaticArray(32) -const t_fe = StaticArray.fromArray([fe(), fe(), fe(), fe(), fe(), fe(), fe()]) /** * Verify public key `pub` can be decoded. @@ -31,7 +30,7 @@ const t_fe = StaticArray.fromArray([fe(), fe(), fe(), fe(), fe(), */ export function crypto_verify_decodepubkey (pub: StaticArray): ge_p3 | null { // fail if public key cannot be decoded - return (ge_frombytes_negate_vartime(A, pub, t_u8x32, t_fe[0], t_fe[1], t_fe[2]) != 0) ? null : A + return (ge_frombytes_negate_vartime(A, pub, t_u8x32, t_fe_0, t_fe_1, t_fe_2) != 0) ? null : A } /** @@ -61,8 +60,8 @@ export function crypto_verify_donna (sig: StaticArray, M: StaticArray, 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_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]) + ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S, t_fe_0) + 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) return equalbytes(sig, check_r, KEY_BYTELENGTH) } @@ -88,10 +87,10 @@ export function crypto_verify_sodium (sig: StaticArray, M: StaticArray, 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_sub_p3(check, expected_r, sb_ah, t_fe[0], t_ge_cached, t_ge_p1p1) + ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S, t_fe_0) + ge_sub_p3(check, expected_r, sb_ah, t_fe_0, t_ge_cached, t_ge_p1p1) - return ge_has_small_order(check, t_fe[0], t_u8x32) + return ge_has_small_order(check, t_fe_0, t_u8x32) } /** @@ -116,10 +115,10 @@ export function crypto_verify_strict (sig: StaticArray, M: StaticArray, 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_sub_p3(check, expected_r, sb_ah, t_fe[0], t_ge_cached, t_ge_p1p1) + ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S, t_fe_0) + ge_sub_p3(check, expected_r, sb_ah, t_fe_0, t_ge_cached, t_ge_p1p1) - 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]) + 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) return equalbytes(sig, check_r, KEY_BYTELENGTH) } @@ -134,10 +133,10 @@ function verify_pubkey (pub: StaticArray): boolean { if (ge_is_canonical(pub) != 0) return false // fail if public key cannot be decoded - if (ge_frombytes_negate_vartime(A, pub, t_u8x32, t_fe[0], t_fe[1], t_fe[2]) != 0) return false + if (ge_frombytes_negate_vartime(A, pub, t_u8x32, t_fe_0, t_fe_1, t_fe_2) != 0) return false // fail if public key `k` is small order - if (ge_has_small_order(A, t_fe[0], t_u8x32) != 0) return false + if (ge_has_small_order(A, t_fe_0, t_u8x32) != 0) return false return true } @@ -156,10 +155,10 @@ function verify_signature (sig: StaticArray): boolean { if (ge_is_canonical(sig) != 0) return false // fail if nonce `R` does not decode to a valid curve point - if (ge_frombytes(expected_r, sig, t_fe[0], t_fe[1], t_fe[2], t_u8x32) != 0) return false + if (ge_frombytes(expected_r, sig, t_fe_0, t_fe_1, t_fe_2, t_u8x32) != 0) return false // fail if nonce `R` is small order - if (ge_has_small_order(expected_r, t_fe[0], t_u8x32) != 0) return false + if (ge_has_small_order(expected_r, t_fe_0, t_u8x32) != 0) return false return true } diff --git a/src/assembly/ed25519/ge.ts b/src/assembly/ed25519/ge.ts index 0d43594..a0bbfe6 100644 --- a/src/assembly/ed25519/ge.ts +++ b/src/assembly/ed25519/ge.ts @@ -315,7 +315,7 @@ export function ge_scalarmult_base_tobytes (S: StaticArray, a: StaticArray(changetype(e) + i)) ge_add_precomp(r, h, t) ge_p1p1_to_p3(h, r) } @@ -331,7 +331,7 @@ export function ge_scalarmult_base_tobytes (S: StaticArray, a: StaticArray(changetype(e) + i)) ge_add_precomp(r, h, t) ge_p1p1_to_p3(h, r) } @@ -440,20 +440,22 @@ export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray for (; i >= 0; --i) { ge_p2_dbl(t, p, t_fe) - if (aslide[i] > 0) { + const aslidei = aslide[i] + if (aslidei > 0) { ge_p1p1_to_p3(u, t) - ge_add_cached(t, u, Ai[aslide[i] / 2], t_fe) - } else if (aslide[i] < 0) { + ge_add_cached(t, u, Ai[aslidei >> 1], t_fe) + } else if (aslidei < 0) { ge_p1p1_to_p3(u, t) - ge_sub_cached(t, u, Ai[(-aslide[i]) / 2]) + ge_sub_cached(t, u, Ai[(-aslidei) >> 1]) } - if (bslide[i] > 0) { + const bslidei = bslide[i] + if (bslidei > 0) { ge_p1p1_to_p3(u, t) - ge_add_precomp(t, u, Bi[bslide[i] / 2]) - } else if (bslide[i] < 0) { + ge_add_precomp(t, u, Bi[bslidei >> 1]) + } else if (bslidei < 0) { ge_p1p1_to_p3(u, t) - ge_sub_precomp(t, u, Bi[(-bslide[i]) / 2]) + ge_sub_precomp(t, u, Bi[(-bslidei) >> 1]) } ge_p1p1_to_p2(p, t) diff --git a/src/assembly/ed25519/t.ts b/src/assembly/ed25519/t.ts new file mode 100644 index 0000000..4e4afcd --- /dev/null +++ b/src/assembly/ed25519/t.ts @@ -0,0 +1,18 @@ +//! SPDX-FileCopyrightText: 2026 Chris Duncan +//! SPDX-License-Identifier: GPL-3.0-or-later + +/** + * Shared buffers for holding temporary values. To ensure buffers are not + * overwritten between dependent calls, these should only be imported from the + * highest call site (i.e. crypto_derive/crypto_sign/crypto_verify) and passed + * down to lower functions as parameter arguments. + */ +import { fe } from './fe' + +export const t_fe_0 = fe() +export const t_fe_1 = fe() +export const t_fe_2 = fe() +export const t_fe_3 = fe() +export const t_fe_4 = fe() +export const t_fe_5 = fe() +export const t_fe_6 = fe() diff --git a/src/assembly/env.ts b/src/assembly/env.ts index cbb09dc..72366e0 100644 --- a/src/assembly/env.ts +++ b/src/assembly/env.ts @@ -1,8 +1,6 @@ //! SPDX-FileCopyrightText: 2026 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -import { ERROR_UNKNOWN, } from './errors' - /** * The module's only import, installed as AssemblyScript's `abort` by * `use: ["abort=src/assembly/env/abort"]` in `asconfig.json`. @@ -15,7 +13,7 @@ import { ERROR_UNKNOWN, } from './errors' @external('env', 'abort') declare function env_abort (code: i32, line: i32, detail: i32, context: i32): void -let errorCode: i32 = ERROR_UNKNOWN +let errorCode: i32 = 0 let errorDetail: i32 = 0 let errorContext: i32 = 0 @@ -28,7 +26,7 @@ export function abort (message: usize, fileName: usize, row: u32, col: u32): voi const code = errorCode const detail = errorDetail const context = errorContext - errorCode = ERROR_UNKNOWN + errorCode = 0 errorDetail = 0 errorContext = 0 env_abort(code, i32(row), detail, context) -- 2.52.0