//! 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'
* 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
*
* @param {StaticArray<u8>} pub 32-byte public key from input buffer
*/
export function crypto_sign (RS: StaticArray<u8>, M: StaticArray<u8>, mlen: i32, prv: StaticArray<u8>, pub: StaticArray<u8>): 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)
// 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<usize>(RS), changetype<usize>(R), 32)
+ memory.copy(changetype<usize>(RS) + 32, changetype<usize>(S), 32)
+ }
- // Construct final signature `(R || S)`
- memory.copy(changetype<usize>(RS), changetype<usize>(R), 32)
+ A.fill(0)
R.fill(0)
- memory.copy(changetype<usize>(RS) + 32, changetype<usize>(S), 32)
S.fill(0)
-
- return 0
+ k.fill(0)
+ s.fill(0)
+ r.fill(0)
+ return c
}