From: Chris Duncan Date: Mon, 9 Nov 2026 07:39:15 +0000 (-0800) Subject: Implement shared encoding for both base point scalar multiplications in signing API. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=05922b111acfdf73246e352287ea8283bf8b67b2;p=nano25519.git Implement shared encoding for both base point scalar multiplications in signing API. --- diff --git a/src/assembly/crypto_sign.ts b/src/assembly/crypto_sign.ts index 3f8b711..c8db162 100644 --- a/src/assembly/crypto_sign.ts +++ b/src/assembly/crypto_sign.ts @@ -2,9 +2,8 @@ //! SPDX-License-Identifier: GPL-3.0-or-later import { KEY_BYTELENGTH } from './constants' -import { crypto_derive } from './crypto_derive' import { Blake2b } from './ed25519/blake2b' -import { ge_scalarmult_base_tobytes } from './ed25519/ge' +import { ge_scalarmult_base_tobytes_pair } from './ed25519/ge' import { sc_muladd, sc_reduce } from './ed25519/sc' import { clamp } from './ed25519/utils' @@ -26,11 +25,16 @@ const S = new StaticArray(64) * hash function instead of SHA-512 specified by RFC 8032. * * In this implementation, both the private key and the public key are required. - * This ensures the user has a full correct keypair for data integrity. It also - * offers a small performance improvement in the case of an invalid public key - * by returning early and avoiding expensive point multiplication; RFC 8032 - * indicates the public key should be recomputed from the private key when - * signing anyway, so there is no penalty for checking key validity first. + * The public key is derived from the private key, as RFC 8032 defines signing, + * and the signature is refused if the two differ, which ensures the user has a + * full correct keypair for data integrity. + * + * The derivation and the nonce point `R` are both fixed-base multiplications, + * and each ends in an expenstive inversion to encode its point. Both points are + * computed first and encoded together so that they share one inversion and save + * about a third of the cost of encoding. The public key check runs after the + * nonce work (instead of before) because the costly computation is already + * done, and the failure path clears everything. * * https://www.rfc-editor.org/info/rfc8032/#section-5.1.6 * @@ -41,18 +45,6 @@ const S = new StaticArray(64) * @param {StaticArray} pub 32-byte public key from input buffer */ export function crypto_sign (RS: StaticArray, M: StaticArray, mlen: i32, prv: StaticArray, pub: StaticArray): i32 { - // Derive `A` from private key - crypto_derive(A, prv) - let c = 0 - for (let i = 0; i < KEY_BYTELENGTH; i++) { - c |= A[i] ^ pub[i] - } - // Return early if `A` does not match public key - if (c != 0) { - A.fill(0) - return c - } - // Hash private key to `h` blake2b.init().update(prv, KEY_BYTELENGTH).digest(h) prv.fill(0) @@ -70,27 +62,38 @@ export function crypto_sign (RS: StaticArray, M: StaticArray, mlen: i32, // Reduce `r` modulo `L`, the group order of the base point `B` sc_reduce(r) - // Perform fixed-base scalar multiplication `[r]B`, output to point `R` - ge_scalarmult_base_tobytes(R, r) + // Perform fixed-base scalar multiplications `[s]B` and `[r]B`, output to + // public key `A` and nonce point `R`, sharing one inversion to encode them + ge_scalarmult_base_tobytes_pair(A, s, R, r) - // Compute challenge hash `blake2b(R || A || M)`, output to `k` - blake2b.init().update(R, 32).update(A, 32).update(M, mlen).digest(k) - A.fill(0) + // Compare `A` to public key + let c = 0 + for (let i = 0; i < KEY_BYTELENGTH; i++) { + c |= A[i] ^ pub[i] + } - // Reduce `k` modulo `L` for efficiency - sc_reduce(k) + // If `A` matches public key, proceed to signing, else refuse and return early + if (c == 0) { - // Compute `S = (k * s + r) mod L` - sc_muladd(S, k, s, r) - k.fill(0) - s.fill(0) - r.fill(0) + // Compute challenge hash `blake2b(R || A || M)`, output to `k` + blake2b.init().update(R, 32).update(A, 32).update(M, mlen).digest(k) + + // Reduce `k` modulo `L` for efficiency + sc_reduce(k) + + // Compute `S = (k * s + r) mod L` + sc_muladd(S, k, s, r) + + // Construct final signature `(R || S)` + memory.copy(changetype(RS), changetype(R), 32) + memory.copy(changetype(RS) + 32, changetype(S), 32) + } - // Construct final signature `(R || S)` - memory.copy(changetype(RS), changetype(R), 32) + A.fill(0) R.fill(0) - memory.copy(changetype(RS) + 32, changetype(S), 32) S.fill(0) - - return 0 + k.fill(0) + s.fill(0) + r.fill(0) + return c }