What is @noble/ed25519?
@noble/ed25519 is a JavaScript library for the Ed25519 public-key signature system. It provides functionalities for key generation, signing, and verification using the Ed25519 algorithm, which is known for its high performance and security.
What are @noble/ed25519's main functionalities?
Key Generation
This feature allows you to generate a new pair of public and private keys using the Ed25519 algorithm.
const { generateKeyPair } = require('@noble/ed25519');
(async () => {
const { publicKey, privateKey } = await generateKeyPair();
console.log('Public Key:', publicKey);
console.log('Private Key:', privateKey);
})();
Signing
This feature allows you to sign a message using a private key. The resulting signature can be used to verify the authenticity of the message.
const { sign, generateKeyPair } = require('@noble/ed25519');
(async () => {
const { privateKey } = await generateKeyPair();
const message = new TextEncoder().encode('Hello, world!');
const signature = await sign(message, privateKey);
console.log('Signature:', signature);
})();
Verification
This feature allows you to verify a signature using the corresponding public key. It ensures that the message was signed by the holder of the private key.
const { verify, generateKeyPair, sign } = require('@noble/ed25519');
(async () => {
const { publicKey, privateKey } = await generateKeyPair();
const message = new TextEncoder().encode('Hello, world!');
const signature = await sign(message, privateKey);
const isValid = await verify(signature, message, publicKey);
console.log('Is the signature valid?', isValid);
})();
Other packages similar to @noble/ed25519
tweetnacl
TweetNaCl is a cryptographic library that provides similar functionalities for the Ed25519 algorithm, including key generation, signing, and verification. It is known for its simplicity and small size, making it suitable for environments with limited resources.
libsodium
Libsodium is a widely-used cryptographic library that offers a comprehensive set of cryptographic primitives, including support for the Ed25519 algorithm. It is known for its high performance and security, and it provides additional features such as encryption and key exchange.
elliptic
Elliptic is a JavaScript library for elliptic curve cryptography, including support for various algorithms such as Ed25519. It provides a flexible and modular approach to cryptographic operations, making it suitable for a wide range of applications.
noble-ed25519
Fastest 4KB JS implementation of ed25519
elliptic curve. Auditable, high-security, 0-dependency EdDSA signatures compliant with
RFC8032 and ZIP215.
The library is a tiny single-feature version of
noble-curves, with some features
removed. Check out curves as a drop-in replacement with
ristretto255,
X25519 / curve25519, ed25519ph and ed25519ctx.
Take a look at: Upgrading section for v1 to v2 transition instructions,
the online demo and
ed25519-keygen if you need
SSH/PGP/HDKey implementation using the library.
This library belongs to noble crypto
noble-crypto — high-security, easily auditable set of contained cryptographic libraries and tools.
- No dependencies, protection against supply chain attacks
- Auditable TypeScript / JS code
- Supported in all major browsers and stable node.js versions
- All releases are signed with PGP keys
- Check out homepage & all libraries:
curves
(4kb versions secp256k1,
ed25519),
hashes
Usage
Browser, deno, node.js and unpkg are supported:
npm install @noble/ed25519
import * as ed from '@noble/ed25519';
(async () => {
const privKey = ed.utils.randomPrivateKey();
const message = Uint8Array.from([0xab, 0xbc, 0xcd, 0xde]);
const pubKey = await ed.getPublicKeyAsync(privKey);
const signature = await ed.signAsync(message, privKey);
const isValid = await ed.verifyAsync(signature, message, pubKey);
})();
Advanced examples:
import { sha512 } from '@noble/hashes/sha512';
ed.etc.sha512Sync = (...m) => sha512(ed.etc.concatBytes(...m));
ed.getPublicKey(privateKey);
ed.sign(message, privateKey);
ed.verify(signature, message, publicKey);
import { webcrypto } from 'node:crypto';
if (!globalThis.crypto) globalThis.crypto = webcrypto;
API
There are 3 main methods: getPublicKey(privateKey)
, sign(message, privateKey)
and verify(signature, message, publicKey)
.
type Hex = Uint8Array | string;
function getPublicKey(privateKey: Hex): Uint8Array;
function getPublicKeyAsync(privateKey: Hex): Promise<Uint8Array>;
function sign(
message: Hex,
privateKey: Hex
): Uint8Array;
function signAsync(message: Hex, privateKey: Hex): Promise<Uint8Array>;
function verify(
signature: Hex,
message: Hex,
publicKey: Hex
): boolean;
function verifyAsync(signature: Hex, message: Hex, publicKey: Hex): Promise<boolean>;
A bunch of useful utilities are also exposed:
export const etc: {
bytesToHex: (b: Bytes) => string;
hexToBytes: (hex: string) => Bytes;
concatBytes: (...arrs: Bytes[]) => Uint8Array;
mod: (a: bigint, b?: bigint) => bigint;
invert: (num: bigint, md?: bigint) => bigint;
randomBytes: (len: number) => Bytes;
sha512Async: (...messages: Bytes[]) => Promise<Bytes>;
sha512Sync: Sha512FnSync;
};
export const utils: {
getExtendedPublicKeyAsync: (priv: Hex) => Promise<ExtK>;
getExtendedPublicKey: (priv: Hex) => ExtK;
precompute(p: Point, w?: number): Point;
randomPrivateKey: () => Bytes;
};
export class ExtendedPoint {
constructor(x: bigint, y: bigint, z: bigint, t: bigint);
static fromAffine(point: AffinePoint): ExtendedPoint;
static fromHex(hash: string);
toRawBytes(): Uint8Array;
toHex(): string;
isTorsionFree(): boolean;
toAffine(): Point;
equals(other: ExtendedPoint): boolean;
add(other: ExtendedPoint): ExtendedPoint;
subtract(other: ExtendedPoint): ExtendedPoint;
multiply(scalar: bigint): ExtendedPoint;
}
ed25519.CURVE.p
ed25519.CURVE.n
ed25519.ExtendedPoint.BASE
Security
The module is production-ready.
It is cross-tested against noble-curves,
and has similar security.
- The current version is rewrite of v1, which has been audited by cure53:
PDF.
- It's being fuzzed by Guido Vranken's cryptofuzz:
run the fuzzer by yourself to check.
Our EC multiplication is hardened to be algorithmically constant time.
We're using built-in JS BigInt
, which is potentially vulnerable to
timing attacks as
per MDN.
But, JIT-compiler and Garbage Collector make "constant time" extremely hard
to achieve in a scripting language. Which means any other JS library doesn't
use constant-time bigints. Including bn.js or anything else.
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.
We consider infrastructure attacks like rogue NPM modules very important;
that's why it's crucial to minimize the amount of 3rd-party dependencies & native
bindings. If your app uses 500 dependencies, any dep could get hacked and you'll
be downloading malware with every npm install
. Our goal is to minimize this attack vector.
Speed
Benchmarks done with Apple M2 on macOS 13 with Node.js 19.
getPublicKey 1 bit x 8,260 ops/sec @ 121μs/op
getPublicKey(utils.randomPrivateKey()) x 8,096 ops/sec @ 123μs/op
sign x 4,084 ops/sec @ 244μs/op
verify x 872 ops/sec @ 1ms/op
Point.fromHex decompression x 14,523 ops/sec @ 68μs/op
Compare to alternative implementations:
tweetnacl@1.0.3 getPublicKey x 1,808 ops/sec @ 552μs/op ± 1.64%
tweetnacl@1.0.3 sign x 651 ops/sec @ 1ms/op
ristretto255@0.1.2 getPublicKey x 640 ops/sec @ 1ms/op ± 1.59%
sodium-native#sign x 83,654 ops/sec @ 11μs/op
Contributing
- Clone the repository
npm install
to install build dependencies like TypeScriptnpm run build
to compile TypeScript codenpm run test
to run jest on test/index.ts
Upgrading
noble-ed25519 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
- Common.js support
- Support for node.js 18 and older without shim
Other changes for upgrading from @noble/ed25519 1.7 to 2.0:
- Methods are now sync by default; use
getPublicKeyAsync
, signAsync
, verifyAsync
for async versions bigint
is no longer allowed in getPublicKey
, sign
, verify
. Reason: ed25519 is LE, can lead to bugsPoint
(2d xy) has been changed to ExtendedPoint
(xyzt)Signature
was removed: just use raw bytes or hex nowutils
were split into utils
(same api as in noble-curves) and
etc
(sha512Sync
and others)
License
MIT (c) 2019 Paul Miller (https://paulmillr.com), see LICENSE file.