|
| 1 | +# noble-ed25519 |
| 2 | +> **noble-crypto** — high-security, easily auditable set of contained cryptographic libraries and tools. |
| 3 | +
|
| 4 | +Noble [ed25519](https://en.wikipedia.org/wiki/EdDSA), an elliptic curve that could be used for assymetric encryption and EDDSA signature scheme. |
| 5 | + |
| 6 | +- No dependencies, one small file |
| 7 | +- Easily auditable TypeScript/JS code |
| 8 | +- Uses es2019 bigint. Supported in Chrome, Firefox, node 10+ |
| 9 | + |
| 10 | +## Usage |
| 11 | + |
| 12 | +```js |
| 13 | +import * as ed25519 from "noble-ed25519"; |
| 14 | + |
| 15 | +const PRIVATE_KEY = 0xa665a45920422f9d417e4867ef; |
| 16 | +const HASH_MESSSAGE = new Uint8Array([99, 100, 101, 102, 103]); |
| 17 | + |
| 18 | +(async () => { |
| 19 | + const publicKey = await ed25519.getPublicKey(PRIVATE_KEY); |
| 20 | + const signature = await ed25519.sign(HASH_MESSAGE, PRIVATE_KEY, publicKey); |
| 21 | + const isMessageSigned = await ed25519.verify(signature, HASH_MESSAGE, publicKey); |
| 22 | +})(); |
| 23 | +``` |
| 24 | + |
| 25 | +## API |
| 26 | + |
| 27 | +```typescript |
| 28 | +function getPublicKey(privateKey: Uint8Array): Promise<Uint8Array>; |
| 29 | +function getPublicKey(privateKey: string): Promise<string>; |
| 30 | +function getPublicKey(privateKey: bigint): Promise<Point>; |
| 31 | +``` |
| 32 | +- `privateKey: Uint8Array | string | bigint` will be used to generate public key. |
| 33 | + Public key is generated by executing scalar multiplication of a base Point(x, y) by a fixed |
| 34 | + integer. The result is another `Point(x, y)` which we will by default encode to hex Uint8Array. |
| 35 | +- Returns: |
| 36 | + * `Promise<Uint8Array>` if `Uint8Array` was passed |
| 37 | + * `Promise<string>` if hex `string` was passed |
| 38 | + * `Promise<Point(x, y)>` instance if `bigint` was passed |
| 39 | + * Uses **promises**, because ed25519 uses sha internally; and we're using built-in browser `window.crypto`, which returns `Promise`. |
| 40 | + |
| 41 | +```typescript |
| 42 | +function scalarmultBase(privateKey: Uint8Array): Uint8Array; |
| 43 | +function scalarmultBase(privateKey: string): string; |
| 44 | +function scalarmultBase(privateKey: bigint): Point; |
| 45 | +``` |
| 46 | +- `privateKey: Uint8Array | string | bigint` basically a number on which the lib will execute base point scalar multiplication. |
| 47 | +- Returns: |
| 48 | + * `Uint8Array` if `Uint8Array` was passed |
| 49 | + * `string` if hex `string` was passed |
| 50 | + * `Point(x, y)` instance if `bigint` was passed |
| 51 | + |
| 52 | +```typescript |
| 53 | +function sign(hash: Uint8Array, privateKey: Uint8Array | bigint, k?: bigint): Promise<Uint8Array>; |
| 54 | +function sign(hash: string, privateKey: string | bigint, k?: bigint): Promise<string>; |
| 55 | +``` |
| 56 | +- `hash: Uint8Array` - message hash which would be signed |
| 57 | +- `privateKey: Uint8Array | bigint` - private key which will sign the hash |
| 58 | +- `publicKey: Uint8Array | Point` - e.g. that was generated from `privateKey` by `getPublicKey` |
| 59 | +- Returns DER encoded EdDSA signature. You can consume it with `SignResult.fromHex()` method: |
| 60 | + - `SignResult.fromHex(ed25519.sign(hash, privateKey, publicKey))` |
| 61 | + |
| 62 | +```typescript |
| 63 | +function verify( |
| 64 | + signature: Uint8Array | string | SignResult, |
| 65 | + hash: Uint8Array | string, |
| 66 | + publicKey: string | Point | Uint8Array |
| 67 | +): Promise<boolean> |
| 68 | +``` |
| 69 | +- `signature: Uint8Array` - object returned by the `sign` function |
| 70 | +- `hash: string | Uint8Array` - message hash that needs to be verified |
| 71 | +- `publicKey: string | Uint8Array | Point` - e.g. that was generated from `privateKey` by `getPublicKey` |
| 72 | +- Returns `Promise<boolean>`: `Promise<true>` if `signature == hash`; otherwise `Promise<false>` |
| 73 | + |
| 74 | +The library also exports helpers: |
| 75 | + |
| 76 | +```typescript |
| 77 | +// 𝔽p |
| 78 | +ed25519.P // 2 ^ 255 - 19 |
| 79 | +
|
| 80 | +// Prime order |
| 81 | +ed25519.PRIME_ORDER // 2 ^ 252 - 27742317777372353535851937790883648493 |
| 82 | +
|
| 83 | +// Base point |
| 84 | +ed25519.BASE_POINT // new ed25519.Point(x, y) where |
| 85 | +// x = 15112221349535400772501151409588531511454012693041857206046113283949847762202n; |
| 86 | +// y = 46316835694926478169428394003475163141307993866256225615783033603165251855960n; |
| 87 | +
|
| 88 | +// Elliptic curve point |
| 89 | +ed25519.Point { |
| 90 | + constructor(x: bigint, y: bigint); |
| 91 | + toHex(): string; |
| 92 | +} |
| 93 | +secp256k1.SignResult { |
| 94 | + constructor(r: bigint, s: bigint); |
| 95 | + toHex(): string; |
| 96 | +} |
| 97 | +``` |
0 commit comments