-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
index.js
274 lines (274 loc) · 8.95 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
"use strict";
/*! noble-ed25519 - MIT License (c) Paul Miller (paulmillr.com) */
Object.defineProperty(exports, "__esModule", { value: true });
const ENCODING_LENGTH = 32;
const A = -1n;
const C = 1n;
exports.P = 2n ** 255n - 19n;
exports.PRIME_ORDER = 2n ** 252n + 27742317777372353535851937790883648493n;
const d = -121665n * inversion(121666n);
const I = powMod(2n, (exports.P - 1n) / 4n, exports.P);
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
static fromHex(hash) {
const bytes = hash instanceof Uint8Array ? hash : hexToArray(hash);
const len = bytes.length - 1;
const normedLast = bytes[len] & ~0x80;
const normed = new Uint8Array([...bytes.slice(0, -1), normedLast]);
const y = arrayToNumberLE(normed);
const sqrY = y * y;
const sqrX = mod((sqrY - C) * inversion(C * d * sqrY - A), exports.P);
let x = powMod(sqrX, (exports.P + 3n) / 8n, exports.P);
const isLastByteOdd = (bytes[len] & 0x80) !== 0;
if (mod(x * x - sqrX, exports.P) !== 0n) {
x = mod(x * I, exports.P);
}
const isXOdd = (x & 1n) === 1n;
if (isLastByteOdd !== isXOdd) {
x = mod(-x, exports.P);
}
return new Point(x, y);
}
encode() {
let hex = this.y.toString(16);
hex = hex.length & 1 ? `0${hex}` : hex;
const u8 = new Uint8Array(ENCODING_LENGTH);
for (let i = hex.length - 2, j = 0; j < ENCODING_LENGTH && i >= 0; i -= 2, j++) {
u8[j] = parseInt(hex[i] + hex[i + 1], 16);
}
const mask = this.x & 1n ? 0x80 : 0;
u8[ENCODING_LENGTH - 1] |= mask;
return u8;
}
toHex() {
const bytes = this.encode();
let hex = "";
for (let i = 0; i < bytes.length; i++) {
const value = bytes[i].toString(16);
hex = `${hex}${value.length > 1 ? value : `0${value}`}`;
}
return hex;
}
reverseY() {
return new Point(this.x, -this.y);
}
add(p2) {
const p1 = this;
const x = (p1.x * p2.y + p2.x * p1.y) *
inversion(1n + d * p1.x * p2.x * p1.y * p2.y);
const y = (p1.y * p2.y + p1.x * p2.x) *
inversion(1n - d * p1.x * p2.x * p1.y * p2.y);
return new Point(mod(x, exports.P), mod(y, exports.P));
}
sub(p2) {
return this.add(p2.reverseY());
}
multiple(n) {
let q = new Point(0n, 1n);
for (let db = this; n > 0n; n >>= 1n, db = db.add(db)) {
if ((n & 1n) === 1n) {
q = q.add(db);
}
}
return q;
}
}
exports.Point = Point;
exports.BASE_POINT = new Point(15112221349535400772501151409588531511454012693041857206046113283949847762202n, 46316835694926478169428394003475163141307993866256225615783033603165251855960n);
class SignResult {
constructor(r, s) {
this.r = r;
this.s = s;
}
static fromHex(hex) {
hex = normalizeHash(hex);
const r = Point.fromHex(hex.slice(0, 32));
const s = arrayToNumberLE(hex.slice(32));
return new SignResult(r, s);
}
toHex() {
const numberBytes = numberToUint8Array(this.s).reverse();
const sBytes = new Uint8Array(ENCODING_LENGTH);
sBytes.set(numberBytes);
const bytes = concatTypedArrays(this.r.encode(), sBytes);
let hex = "";
for (let i = 0; i < bytes.length; i++) {
const value = bytes[i].toString(16);
hex = `${hex}${value.length > 1 ? value : `0${value}`}`;
}
return hex;
}
}
exports.SignResult = SignResult;
let sha512;
if (typeof window == "object" && "crypto" in window) {
sha512 = async (message) => {
const buffer = await window.crypto.subtle.digest("SHA-512", message.buffer);
return new Uint8Array(buffer);
};
}
else if (typeof process === "object" && "node" in process.versions) {
const { createHash } = require("crypto");
sha512 = async (message) => {
const hash = createHash("sha512");
hash.update(message);
return Uint8Array.from(hash.digest());
};
}
else {
throw new Error("The environment doesn't have sha512 function");
}
function concatTypedArrays(...args) {
const result = new Uint8Array(args.reduce((a, arr) => a + arr.length, 0));
for (let i = 0, pad = 0; i < args.length; i++) {
const arr = args[i];
result.set(arr, pad);
pad += arr.length;
}
return result;
}
function numberToUint8Array(num) {
let hex = num.toString(16);
hex = hex.length & 1 ? `0${hex}` : hex;
const len = hex.length / 2;
const u8 = new Uint8Array(len);
for (let j = 0, i = 0; i < hex.length; i += 2, j++) {
u8[j] = parseInt(hex[i] + hex[i + 1], 16);
}
return u8;
}
function arrayToNumberLE(bytes) {
let value = 0n;
for (let i = 0; i < bytes.length; i++) {
value += (BigInt(bytes[i]) & 255n) << (8n * BigInt(i));
}
return value;
}
function powMod(x, power, order) {
let res = 1n;
while (power > 0) {
if (power & 1n) {
res = mod(res * x, order);
}
power >>= 1n;
x = mod(x * x, order);
}
return res;
}
function arrayToHex(uint8a) {
return Array.from(uint8a)
.map(c => c.toString(16).padStart(2, "0"))
.join("");
}
function hexToArray(hash) {
hash = hash.length & 1 ? `0${hash}` : hash;
const len = hash.length;
const result = new Uint8Array(len / 2);
for (let i = 0, j = 0; i < len - 1; i += 2, j++) {
result[j] = parseInt(hash[i] + hash[i + 1], 16);
}
return result;
}
function hexToNumber(hex) {
return BigInt(`0x${hex}`);
}
function arrayToNumberBE(bytes) {
let value = 0n;
for (let i = bytes.length - 1, j = 0; i >= 0; i--, j++) {
value += (BigInt(bytes[i]) & 255n) << (8n * BigInt(j));
}
return value;
}
async function hashNumber(...args) {
const messageArray = concatTypedArrays(...args);
const hash = await sha512(messageArray);
const value = arrayToNumberLE(hash);
return mod(value, exports.PRIME_ORDER);
}
function getPrivateBytes(privateKey) {
return sha512(privateKey instanceof Uint8Array
? privateKey
: numberToUint8Array(privateKey));
}
function keyPrefix(privateBytes) {
return privateBytes.slice(ENCODING_LENGTH);
}
function mod(a, b) {
const res = a % b;
return res >= 0 ? res : b + res;
}
function inversion(num) {
return powMod(num, exports.P - 2n, exports.P);
}
function encodePrivate(privateBytes) {
const last = ENCODING_LENGTH - 1;
const head = privateBytes.slice(0, ENCODING_LENGTH);
head[0] &= 248;
head[last] &= 127;
head[last] |= 64;
return arrayToNumberLE(head);
}
function normalizePrivateKey(privateKey) {
if (privateKey instanceof Uint8Array) {
return arrayToNumberBE(privateKey);
}
if (typeof privateKey === "string") {
return hexToNumber(privateKey);
}
return BigInt(privateKey);
}
function normalizePublicKey(publicKey) {
return publicKey instanceof Point ? publicKey : Point.fromHex(publicKey);
}
function normalizePoint(point, privateKey) {
if (privateKey instanceof Uint8Array) {
return point.encode();
}
if (typeof privateKey === "string") {
return point.toHex();
}
return point;
}
function normalizeSignature(signature) {
return signature instanceof SignResult
? signature
: SignResult.fromHex(signature);
}
function normalizeHash(hash) {
return hash instanceof Uint8Array ? hash : hexToArray(hash);
}
async function getPublicKey(privateKey) {
const multiplier = normalizePrivateKey(privateKey);
const privateBytes = await getPrivateBytes(multiplier);
const privateInt = encodePrivate(privateBytes);
const publicKey = exports.BASE_POINT.multiple(privateInt);
return normalizePoint(publicKey, privateKey);
}
exports.getPublicKey = getPublicKey;
async function sign(hash, privateKey, publicKey) {
const message = normalizeHash(hash);
publicKey = normalizePublicKey(publicKey);
privateKey = normalizePrivateKey(privateKey);
const privateBytes = await getPrivateBytes(privateKey);
const privatePrefix = keyPrefix(privateBytes);
const r = await hashNumber(privatePrefix, message);
const R = exports.BASE_POINT.multiple(r);
const h = await hashNumber(R.encode(), publicKey.encode(), message);
const S = mod(r + h * encodePrivate(privateBytes), exports.PRIME_ORDER);
const signature = new SignResult(R, S).toHex();
return hash instanceof Uint8Array ? hexToArray(signature) : signature;
}
exports.sign = sign;
async function verify(signature, hash, publicKey) {
hash = normalizeHash(hash);
publicKey = normalizePublicKey(publicKey);
signature = normalizeSignature(signature);
const h = await hashNumber(signature.r.encode(), publicKey.encode(), hash);
const S = exports.BASE_POINT.multiple(signature.s);
const R = signature.r.add(publicKey.multiple(h));
return S.x === R.x && S.y === R.y;
}
exports.verify = verify;