🔖 SUF-CMA (strong unforgeability under chosen message attacks) and
SBS (non-repudiation / exclusive ownership)
🪶 3.9KB (gzipped)
The module is a sister project of noble-curves.
Use noble-ed25519 if you need smaller attack surface & better auditability.
Switch to noble-curves (drop-in) if you need features like ristretto255, x25519 / curve25519, ed25519ph, hash-to-curve, oprf.
This library belongs to noble cryptography
noble-cryptography — high-security, easily auditable set of contained cryptographic libraries and tools.
Only async methods are available by default, to keep the library dependency-free.
To enable sync methods:
npm install @noble/hashes
import * as ed from '@noble/ed25519';import { sha512 } from '@noble/hashes/sha2.js';ed.hashes.sha512 = sha512;// Sync methods can be used now:const { secretKey, publicKey } = ed.keygen();const msg = new TextEncoder().encode('hello noble');// const publicKey = ed.getPublicKey(secretKey);const sig = ed.sign(msg, secretKey);const isValid = ed.verify(sig, msg, publicKey);
React Native: polyfill getRandomValues and sha512
React Native does not provide secure getRandomValues by default.
This can't be securely polyfilled from our end, so one will need a RN-specific compile-time dep.
import 'react-native-get-random-values';import * as ed from '@noble/ed25519';import { sha512 } from '@noble/hashes/sha2.js';ed.hashes.sha512 = sha512;ed.hashes.sha512Async = (m: Uint8Array) => Promise.resolve(sha512(m));
API
There are 4 main methods, which accept Uint8Array-s:
keygen() and keygenAsync()
getPublicKey(secretKey) and getPublicKeyAsync(secretKey)
sign(message, secretKey) and signAsync(message, secretKey)
verify(signature, message, publicKey) and verifyAsync(signature, message, publicKey)
keygen
import * as ed from '@noble/ed25519';import { sha512 } from '@noble/hashes/sha2.js';ed.hashes.sha512 = sha512;(async () => { const keys = ed.keygen(); const { secretKey, publicKey } = keys; const keysA = await ed.keygenAsync();})();
getPublicKey
import * as ed from '@noble/ed25519';import { sha512 } from '@noble/hashes/sha2.js';ed.hashes.sha512 = sha512;(async () => { const { secretKey: secretKeyA } = ed.keygen(); const pubKey = ed.getPublicKey(secretKeyA); const pubKeyA = await ed.getPublicKeyAsync(secretKeyA); const pubKeyPoint = ed.Point.fromBytes(pubKey); const pubKeyExtended = ed.utils.getExtendedPublicKey(secretKeyA);})();
Generates 32-byte public key from 32-byte private key.
Some libraries have 64-byte private keys - those are just priv+pub concatenated
Use ExtendedPoint.fromHex(publicKey) if you want to convert hex / bytes into Point.
It will use decompression algorithm 5.1.3 of RFC 8032.
Use utils.getExtendedPublicKey if you need full SHA512 hash of seed
sign
import * as ed from '@noble/ed25519';import { sha512 } from '@noble/hashes/sha2.js';ed.hashes.sha512 = sha512;(async () => { const { secretKey, publicKey } = ed.keygen(); const message = new TextEncoder().encode('hello noble'); const signature = ed.sign(message, secretKey); const signatureA = await ed.signAsync(message, secretKey);})();
Generates deterministic EdDSA signature. message would be hashed by ed25519 internally.
For prehashed ed25519ph, switch to noble-curves.
Verifies EdDSA signature. Has SUF-CMA (strong unforgeability under chosen message attacks).
By default, follows ZIP215 1 and can be used in consensus-critical apps 2.
zip215: false option switches verification criteria to strict
RFC8032 / FIPS 186-5 and provides non-repudiation with SBS (Strongly Binding Signatures) 3.
[!NOTE]
Most other libraries don't have SUF-CMA & SBS - less optimal choice for their security.
[!NOTE]
Any message with pubkey from ED25519_TORSION_SUBGROUP would be valid in sigs under ZIP215.
If you see anything unusual: investigate and report.
Constant-timeness
We're targetting algorithmic constant time. JIT-compiler and Garbage Collector make "constant time"
extremely hard to achieve timing attack resistance
in a scripting language. Which means any other JS library can't have
constant-timeness. Even statically typed Rust, a language without GC,
makes it harder to achieve constant-time
for some cases. If your goal is absolute security, don't use any JS lib — including bindings to native ones.
Use low-level libraries & languages.
Supply chain security
Commits are signed with PGP keys to prevent forgery. Be sure to verify the commit signatures
Releases are made transparently through token-less GitHub CI and Trusted Publishing. Be sure to verify the provenance logs for authenticity.
Rare releasing is practiced to minimize the need for re-audits by end-users.
Dependencies are minimized and strictly pinned to reduce supply-chain risk.
We use as few dependencies as possible.
Version ranges are locked, and changes are checked with npm-diff.
Dev dependencies are excluded from end-user installs; they’re only used for development and build steps.
For this package, there are 0 dependencies; and a few dev dependencies:
jsbt is used for benchmarking / testing / build tooling and developed by the same author
prettier, fast-check and typescript are used for code quality / test generation / ts compilation
Randomness
We rely on the built-in
crypto.getRandomValues,
which is considered a cryptographically secure PRNG.
Browsers have had weaknesses in the past - and could again - but implementing a userspace CSPRNG is even worse, as there’s no reliable userspace source of high-quality entropy.
Quantum computers
Cryptographically relevant quantum computer, if built, will allow to
break elliptic curve cryptography (both ECDSA / EdDSA & ECDH) using Shor's algorithm.
Consider switching to newer / hybrid algorithms, such as SPHINCS+. They are available in
noble-post-quantum.
NIST prohibits classical cryptography (RSA, DSA, ECDSA, ECDH) after 2035. Australian ASD prohibits it after 2030.
Upgrading
v2 to v3
v3 brings the package closer to noble-curves v2.
Most methods now expect Uint8Array, string hex inputs are prohibited
Add keygen, keygenAsync method
Node v20.19 is now the minimum required version
Various small changes for types and Point class
etc: hashes are now set in hashes object:
import * as ed from '@noble/ed25519';import { sha512 } from '@noble/hashes/sha2.js';// beforeed.etc.sha512Sync = (...m: Uint8Array[]) => sha512(ed.etc.concatBytes(...m));ed.etc.sha512Async = (...m: Uint8Array[]) => Promise.resolve(sha512(ed.etc.concatBytes(...m)));// aftered.hashes.sha512 = sha512;ed.hashes.sha512Async = (m: Uint8Array) => Promise.resolve(sha512(m));
v1 to v2
v2 features improved security and smaller attack surface.
The goal of v2 is to provide minimum possible JS library which is safe and fast.
That means the library was reduced 4x, to just over 300 lines. In order to
achieve the goal, some features were moved to
noble-curves, which is
even safer and faster drop-in replacement library with same API.
Switch to curves if you intend to keep using these features:
x25519 / curve25519 / getSharedSecret
ristretto255 / RistrettoPoint
Using utils.precompute() for non-base point
Support for environments which don't support bigint literals