Skip to content

Commit 36ded8a

Browse files
committed
Create noble-ed25519.
0 parents  commit 36ded8a

File tree

8 files changed

+920
-0
lines changed

8 files changed

+920
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2019 Paul Miller (https://paulmillr.com)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the “Software”), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
```

index.d.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*! noble-secp256k1 - MIT License (c) Paul Miller (paulmillr.com) */
2+
export declare const P: bigint;
3+
export declare const PRIME_ORDER: bigint;
4+
declare type PrivKey = Uint8Array | string | bigint | number;
5+
declare type PubKey = Uint8Array | string | Point;
6+
declare type Hex = Uint8Array | string;
7+
declare type Signature = Uint8Array | string | SignResult;
8+
export declare class Point {
9+
x: bigint;
10+
y: bigint;
11+
constructor(x: bigint, y: bigint);
12+
static fromHex(hash: Hex): Point;
13+
encode(): Uint8Array;
14+
toHex(): string;
15+
}
16+
export declare const BASE_POINT: Point;
17+
export declare class SignResult {
18+
r: Point;
19+
s: bigint;
20+
constructor(r: Point, s: bigint);
21+
static fromHex(hex: Hex): SignResult;
22+
toHex(): string;
23+
}
24+
export declare function scalarmultBase(privateKey: Uint8Array): Uint8Array;
25+
export declare function scalarmultBase(privateKey: string): string;
26+
export declare function scalarmultBase(privateKey: bigint | number): Point;
27+
export declare function getPublicKey(privateKey: Uint8Array): Promise<Uint8Array>;
28+
export declare function getPublicKey(privateKey: string): Promise<string>;
29+
export declare function getPublicKey(privateKey: bigint | number): Promise<Point>;
30+
export declare function sign(hash: Uint8Array, privateKey: PrivKey, publicKey: PubKey): Promise<Uint8Array>;
31+
export declare function sign(hash: string, privateKey: PrivKey, publicKey: PubKey): Promise<string>;
32+
export declare function verify(signature: Signature, hash: Hex, publicKey: PubKey): Promise<boolean>;
33+
export {};

0 commit comments

Comments
 (0)