@flickfyi/flkr 0.0.0 → 0.0.106
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/node_modules/@flickfyi/core/dist/index.js +2 -2
- package/node_modules/@flickfyi/http-message-sig/dist/index.js +11 -11
- package/node_modules/@flickfyi/identity/dist/{base-B-urclbM.js → base-B1Vicu5t.js} +1 -1
- package/node_modules/@flickfyi/identity/dist/biscuit.js +1 -1
- package/node_modules/@flickfyi/identity/dist/{curve-CpcHwjN8.js → curve-BVH9bh0Y.js} +7 -7
- package/node_modules/@flickfyi/identity/dist/{cwt-DySP8lzM.js → cwt-CfubTfiF.js} +25 -25
- package/node_modules/@flickfyi/identity/dist/cwt.js +1 -1
- package/node_modules/@flickfyi/identity/dist/index.js +3 -3
- package/node_modules/@flickfyi/identity/dist/{nist-DAZq4YJ7.js → nist-C8Poq91M.js} +4 -4
- package/node_modules/@flickfyi/identity/dist/signing.js +2 -2
- package/node_modules/@flickfyi/lens/dist/index.js +1 -1
- package/node_modules/@msgpackr-extract/msgpackr-extract-linux-x64/README.md +1 -0
- package/node_modules/@msgpackr-extract/msgpackr-extract-linux-x64/node.abi115.glibc.node +0 -0
- package/node_modules/@msgpackr-extract/msgpackr-extract-linux-x64/node.abi115.musl.node +0 -0
- package/node_modules/@msgpackr-extract/msgpackr-extract-linux-x64/node.napi.glibc.node +0 -0
- package/node_modules/@msgpackr-extract/msgpackr-extract-linux-x64/node.napi.musl.node +0 -0
- package/node_modules/@msgpackr-extract/{msgpackr-extract-darwin-arm64 → msgpackr-extract-linux-x64}/package.json +4 -4
- package/package.json +1 -1
- package/node_modules/@flickfyi/identity/dist/base-ClGB6hXM.js +0 -354
- package/node_modules/@flickfyi/identity/dist/curve-DALK-Fi3.js +0 -2768
- package/node_modules/@flickfyi/identity/dist/cwt-tfd4xMR_.js +0 -3397
- package/node_modules/@flickfyi/identity/dist/nist-luaoKQ1s.js +0 -1194
- package/node_modules/@msgpackr-extract/msgpackr-extract-darwin-arm64/README.md +0 -1
- package/node_modules/@msgpackr-extract/msgpackr-extract-darwin-arm64/node.napi.glibc.node +0 -0
- /package/node_modules/@msgpackr-extract/{msgpackr-extract-darwin-arm64 → msgpackr-extract-linux-x64}/index.js +0 -0
|
@@ -1,1194 +0,0 @@
|
|
|
1
|
-
import { A as isBytes, B as clean, C as bytesToHex, E as concatBytes, I as abytes, L as aexists, M as randomBytes, N as validateObject, O as createHmacDrbg, P as sha256, R as ahash, S as bitMask, _ as abool, a as normalizeZ, b as astring, f as mapHashToField, g as abignumber, h as aInRange, i as mulAddUnsafe, j as numberToHexUnpadded, k as hexToBytes, l as getMinHashLength, n as createCurveFields, o as probeRandomBytes, r as createKeygen, s as validatePointCons, t as ScalarMultiplier, u as invertCt, v as abytes$1, w as bytesToNumberBE, x as bitLen, y as asafenumber, z as aoutput } from "./curve-CpcHwjN8.js";
|
|
2
|
-
//#region ../../../../../../.pnpm-store/v11/links/@noble/hashes/2.4.0/a02ec2573a7b07204dfaf0508ab31b11f6a9bd74c18f15e8ab2933e00265fdfc/node_modules/@noble/hashes/hmac.js
|
|
3
|
-
/**
|
|
4
|
-
* HMAC: RFC2104 message authentication code.
|
|
5
|
-
* @module
|
|
6
|
-
*/
|
|
7
|
-
/**
|
|
8
|
-
* Internal class for HMAC.
|
|
9
|
-
* Accepts any byte key, although RFC 2104 §3 recommends keys at least
|
|
10
|
-
* `HashLen` bytes long.
|
|
11
|
-
*/
|
|
12
|
-
var _HMAC = class {
|
|
13
|
-
oHash;
|
|
14
|
-
iHash;
|
|
15
|
-
blockLen;
|
|
16
|
-
outputLen;
|
|
17
|
-
canXOF = false;
|
|
18
|
-
finished = false;
|
|
19
|
-
destroyed = false;
|
|
20
|
-
constructor(hash, key) {
|
|
21
|
-
ahash(hash);
|
|
22
|
-
abytes(key, void 0, "key");
|
|
23
|
-
this.iHash = hash.create();
|
|
24
|
-
if (typeof this.iHash.update !== "function") throw new Error("expected Hash instance");
|
|
25
|
-
this.blockLen = this.iHash.blockLen;
|
|
26
|
-
this.outputLen = this.iHash.outputLen;
|
|
27
|
-
const blockLen = this.blockLen;
|
|
28
|
-
const pad = new Uint8Array(blockLen);
|
|
29
|
-
pad.set(key.length > blockLen ? hash.create().update(key).digest() : key);
|
|
30
|
-
for (let i = 0; i < pad.length; i++) pad[i] ^= 54;
|
|
31
|
-
this.iHash.update(pad);
|
|
32
|
-
this.oHash = hash.create();
|
|
33
|
-
for (let i = 0; i < pad.length; i++) pad[i] ^= 106;
|
|
34
|
-
this.oHash.update(pad);
|
|
35
|
-
clean(pad);
|
|
36
|
-
}
|
|
37
|
-
update(buf) {
|
|
38
|
-
aexists(this);
|
|
39
|
-
this.iHash.update(buf);
|
|
40
|
-
return this;
|
|
41
|
-
}
|
|
42
|
-
digestInto(out) {
|
|
43
|
-
aexists(this);
|
|
44
|
-
aoutput(out, this);
|
|
45
|
-
this.finished = true;
|
|
46
|
-
const buf = out.subarray(0, this.outputLen);
|
|
47
|
-
this.iHash.digestInto(buf);
|
|
48
|
-
this.oHash.update(buf);
|
|
49
|
-
this.oHash.digestInto(buf);
|
|
50
|
-
this.destroy();
|
|
51
|
-
}
|
|
52
|
-
digest() {
|
|
53
|
-
const out = new Uint8Array(this.oHash.outputLen);
|
|
54
|
-
this.digestInto(out);
|
|
55
|
-
return out;
|
|
56
|
-
}
|
|
57
|
-
_cloneInto(to) {
|
|
58
|
-
to ||= Object.create(Object.getPrototypeOf(this), {});
|
|
59
|
-
const { oHash, iHash, finished, destroyed, blockLen, outputLen, canXOF } = this;
|
|
60
|
-
to = to;
|
|
61
|
-
to.finished = finished;
|
|
62
|
-
to.destroyed = destroyed;
|
|
63
|
-
to.blockLen = blockLen;
|
|
64
|
-
to.outputLen = outputLen;
|
|
65
|
-
to.canXOF = canXOF;
|
|
66
|
-
to.oHash = oHash._cloneInto(to.oHash);
|
|
67
|
-
to.iHash = iHash._cloneInto(to.iHash);
|
|
68
|
-
return to;
|
|
69
|
-
}
|
|
70
|
-
clone() {
|
|
71
|
-
return this._cloneInto();
|
|
72
|
-
}
|
|
73
|
-
destroy() {
|
|
74
|
-
this.destroyed = true;
|
|
75
|
-
this.oHash.destroy();
|
|
76
|
-
this.iHash.destroy();
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
var hmac = /* @__PURE__ */ (() => {
|
|
80
|
-
const hmac_ = ((hash, key, message) => new _HMAC(hash, key).update(message).digest());
|
|
81
|
-
hmac_.create = (hash, key) => new _HMAC(hash, key);
|
|
82
|
-
return hmac_;
|
|
83
|
-
})();
|
|
84
|
-
//#endregion
|
|
85
|
-
//#region ../../../../../../.pnpm-store/v11/links/@noble/curves/2.4.0/766129a921e8dfd2ef1de557b88b41133e3334d958e354fac1f5d17f98661627/node_modules/@noble/curves/abstract/der.js
|
|
86
|
-
/**
|
|
87
|
-
* ASN.1 DER (Distinguished Encoding Rules) helpers for ECDSA signatures.
|
|
88
|
-
* Only implements the tiny subset needed for `SEQUENCE(INTEGER r, INTEGER s)`.
|
|
89
|
-
* @module
|
|
90
|
-
*/
|
|
91
|
-
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
92
|
-
var _0n$1 = /* @__PURE__ */ BigInt(0);
|
|
93
|
-
/**
|
|
94
|
-
* @param m - Error message.
|
|
95
|
-
* @example
|
|
96
|
-
* Throw a DER-specific error when signature parsing encounters invalid bytes.
|
|
97
|
-
*
|
|
98
|
-
* ```ts
|
|
99
|
-
* new DERErr('bad der');
|
|
100
|
-
* ```
|
|
101
|
-
*/
|
|
102
|
-
var DERErr = class extends Error {
|
|
103
|
-
constructor(m = "") {
|
|
104
|
-
super(m);
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
var _DER = {
|
|
108
|
-
Err: DERErr,
|
|
109
|
-
_tlv: {
|
|
110
|
-
encode: (tag, data) => {
|
|
111
|
-
const { Err: E } = _DER;
|
|
112
|
-
asafenumber(tag, "tag");
|
|
113
|
-
if (tag < 0 || tag > 255) throw new E("tlv.encode: wrong tag");
|
|
114
|
-
astring(data, "data");
|
|
115
|
-
if (data.length & 1) throw new E("tlv.encode: unpadded data");
|
|
116
|
-
const dataLen = data.length / 2;
|
|
117
|
-
const len = numberToHexUnpadded(dataLen);
|
|
118
|
-
if (len.length / 2 & 128) throw new E("tlv.encode: long form length too big");
|
|
119
|
-
const lenLen = dataLen > 127 ? numberToHexUnpadded(len.length / 2 | 128) : "";
|
|
120
|
-
return numberToHexUnpadded(tag) + lenLen + len + data;
|
|
121
|
-
},
|
|
122
|
-
decode(tag, data) {
|
|
123
|
-
const { Err: E } = _DER;
|
|
124
|
-
data = abytes$1(data, void 0, "DER data");
|
|
125
|
-
let pos = 0;
|
|
126
|
-
if (tag < 0 || tag > 255) throw new E("tlv.decode: wrong tag");
|
|
127
|
-
if (data.length < 2 || data[pos++] !== tag) throw new E("tlv.decode: wrong tlv");
|
|
128
|
-
const first = data[pos++];
|
|
129
|
-
const isLong = !!(first & 128);
|
|
130
|
-
let length = 0;
|
|
131
|
-
if (!isLong) length = first;
|
|
132
|
-
else {
|
|
133
|
-
const lenLen = first & 127;
|
|
134
|
-
if (!lenLen) throw new E("tlv.decode(long): indefinite length not supported");
|
|
135
|
-
if (lenLen > 4) throw new E("tlv.decode(long): byte length is too big");
|
|
136
|
-
const lengthBytes = data.subarray(pos, pos + lenLen);
|
|
137
|
-
if (lengthBytes.length !== lenLen) throw new E("tlv.decode: length bytes not complete");
|
|
138
|
-
if (lengthBytes[0] === 0) throw new E("tlv.decode(long): zero leftmost byte");
|
|
139
|
-
for (const b of lengthBytes) length = length << 8 | b;
|
|
140
|
-
pos += lenLen;
|
|
141
|
-
if (length < 128) throw new E("tlv.decode(long): not minimal encoding");
|
|
142
|
-
}
|
|
143
|
-
const v = data.subarray(pos, pos + length);
|
|
144
|
-
if (v.length !== length) throw new E("tlv.decode: wrong value length");
|
|
145
|
-
return {
|
|
146
|
-
v,
|
|
147
|
-
l: data.subarray(pos + length)
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
},
|
|
151
|
-
_int: {
|
|
152
|
-
encode(num) {
|
|
153
|
-
const { Err: E } = _DER;
|
|
154
|
-
abignumber(num);
|
|
155
|
-
if (num < _0n$1) throw new E("integer: negative integers are not allowed");
|
|
156
|
-
let hex = numberToHexUnpadded(num);
|
|
157
|
-
if (Number.parseInt(hex[0], 16) & 8) hex = "00" + hex;
|
|
158
|
-
if (hex.length & 1) throw new E("unexpected DER parsing assertion: unpadded hex");
|
|
159
|
-
return hex;
|
|
160
|
-
},
|
|
161
|
-
decode(data) {
|
|
162
|
-
const { Err: E } = _DER;
|
|
163
|
-
if (data.length < 1) throw new E("invalid signature integer: empty");
|
|
164
|
-
if (data[0] & 128) throw new E("invalid signature integer: negative");
|
|
165
|
-
if (data.length > 1 && data[0] === 0 && !(data[1] & 128)) throw new E("invalid signature integer: unnecessary leading zero");
|
|
166
|
-
return bytesToNumberBE(data);
|
|
167
|
-
}
|
|
168
|
-
},
|
|
169
|
-
toSig(bytes, maxScalarBytes) {
|
|
170
|
-
const { Err: E, _int: int, _tlv: tlv } = _DER;
|
|
171
|
-
if (maxScalarBytes !== void 0) {
|
|
172
|
-
asafenumber(maxScalarBytes, "maxScalarBytes");
|
|
173
|
-
if (maxScalarBytes < 1) throw new E("invalid signature: maxScalarBytes must be positive");
|
|
174
|
-
}
|
|
175
|
-
const data = abytes$1(bytes, void 0, "signature");
|
|
176
|
-
const { v: seqBytes, l: seqLeftBytes } = tlv.decode(48, data);
|
|
177
|
-
if (seqLeftBytes.length) throw new E("invalid signature: left bytes after parsing");
|
|
178
|
-
const { v: rBytes, l: rLeftBytes } = tlv.decode(2, seqBytes);
|
|
179
|
-
const { v: sBytes, l: sLeftBytes } = tlv.decode(2, rLeftBytes);
|
|
180
|
-
if (sLeftBytes.length) throw new E("invalid signature: left bytes after parsing");
|
|
181
|
-
if (maxScalarBytes !== void 0 && (rBytes.length > maxScalarBytes || sBytes.length > maxScalarBytes)) throw new E("invalid signature: integer too large");
|
|
182
|
-
return {
|
|
183
|
-
r: int.decode(rBytes),
|
|
184
|
-
s: int.decode(sBytes)
|
|
185
|
-
};
|
|
186
|
-
},
|
|
187
|
-
hexFromSig(sig) {
|
|
188
|
-
const { _tlv: tlv, _int: int } = _DER;
|
|
189
|
-
validateObject(sig, {
|
|
190
|
-
r: "bigint",
|
|
191
|
-
s: "bigint"
|
|
192
|
-
}, {}, "sig");
|
|
193
|
-
const seq = tlv.encode(2, int.encode(sig.r)) + tlv.encode(2, int.encode(sig.s));
|
|
194
|
-
return tlv.encode(48, seq);
|
|
195
|
-
}
|
|
196
|
-
};
|
|
197
|
-
/**
|
|
198
|
-
* ASN.1 DER encoding utilities. ASN is very complex & fragile. Format:
|
|
199
|
-
*
|
|
200
|
-
* [0x30 (SEQUENCE), bytelength, 0x02 (INTEGER), intLength, R, 0x02 (INTEGER), intLength, S]
|
|
201
|
-
*
|
|
202
|
-
* Docs: {@link https://letsencrypt.org/docs/a-warm-welcome-to-asn1-and-der/ | Let's Encrypt ASN.1 guide} and
|
|
203
|
-
* {@link https://luca.ntop.org/Teaching/Appunti/asn1.html | Luca Deri's ASN.1 notes}.
|
|
204
|
-
* @example
|
|
205
|
-
* ASN.1 DER encoding utilities.
|
|
206
|
-
*
|
|
207
|
-
* ```ts
|
|
208
|
-
* const der = DER.hexFromSig({ r: 1n, s: 2n });
|
|
209
|
-
* ```
|
|
210
|
-
*/
|
|
211
|
-
var DER = /* @__PURE__ */ (() => {
|
|
212
|
-
Object.freeze(_DER._tlv);
|
|
213
|
-
Object.freeze(_DER._int);
|
|
214
|
-
return Object.freeze(_DER);
|
|
215
|
-
})();
|
|
216
|
-
//#endregion
|
|
217
|
-
//#region ../../../../../../.pnpm-store/v11/links/@noble/curves/2.4.0/766129a921e8dfd2ef1de557b88b41133e3334d958e354fac1f5d17f98661627/node_modules/@noble/curves/abstract/weierstrass.js
|
|
218
|
-
/**
|
|
219
|
-
* Short Weierstrass curve methods. The formula is: y² = x³ + ax + b.
|
|
220
|
-
*
|
|
221
|
-
* ### Design rationale for types
|
|
222
|
-
*
|
|
223
|
-
* * Interaction between classes from different curves should fail:
|
|
224
|
-
* `k256.Point.BASE.add(p256.Point.BASE)`
|
|
225
|
-
* * For this purpose we want to use `instanceof` operator, which is fast and works during runtime
|
|
226
|
-
* * Different calls of `curve()` would return different classes -
|
|
227
|
-
* `curve(params) !== curve(params)`: if somebody decided to monkey-patch their curve,
|
|
228
|
-
* it won't affect others
|
|
229
|
-
*
|
|
230
|
-
* TypeScript can't infer types for classes created inside a function. Classes is one instance
|
|
231
|
-
* of nominative types in TypeScript and interfaces only check for shape, so it's hard to create
|
|
232
|
-
* unique type for every function call.
|
|
233
|
-
*
|
|
234
|
-
* We can use generic types via some param, like curve opts, but that would:
|
|
235
|
-
* 1. Enable interaction between `curve(params)` and `curve(params)` (curves of same params)
|
|
236
|
-
* which is hard to debug.
|
|
237
|
-
* 2. Params can be generic and we can't enforce them to be constant value:
|
|
238
|
-
* if somebody creates curve from non-constant params,
|
|
239
|
-
* it would be allowed to interact with other curves with non-constant params
|
|
240
|
-
*
|
|
241
|
-
* @todo https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html#unique-symbol
|
|
242
|
-
* @module
|
|
243
|
-
*/
|
|
244
|
-
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
245
|
-
var divNearest = (num, den) => (num + (num >= 0 ? den : -den) / _2n) / den;
|
|
246
|
-
/** Splits scalar for GLV endomorphism. */
|
|
247
|
-
function _splitEndoScalar(k, basis, n) {
|
|
248
|
-
aInRange("scalar", k, _0n, n);
|
|
249
|
-
const [[a1, b1], [a2, b2]] = basis;
|
|
250
|
-
const c1 = divNearest(b2 * k, n);
|
|
251
|
-
const c2 = divNearest(-b1 * k, n);
|
|
252
|
-
let k1 = k - c1 * a1 - c2 * a2;
|
|
253
|
-
let k2 = -c1 * b1 - c2 * b2;
|
|
254
|
-
const k1neg = k1 < _0n;
|
|
255
|
-
const k2neg = k2 < _0n;
|
|
256
|
-
if (k1neg) k1 = -k1;
|
|
257
|
-
if (k2neg) k2 = -k2;
|
|
258
|
-
const MAX_NUM = bitMask(Math.ceil(bitLen(n) / 2)) + _1n;
|
|
259
|
-
if (k1 < _0n || k1 >= MAX_NUM || k2 < _0n || k2 >= MAX_NUM) throw new Error("splitScalar (endomorphism): failed for k");
|
|
260
|
-
return {
|
|
261
|
-
k1neg,
|
|
262
|
-
k1,
|
|
263
|
-
k2neg,
|
|
264
|
-
k2
|
|
265
|
-
};
|
|
266
|
-
}
|
|
267
|
-
function validateSigFormat(format) {
|
|
268
|
-
if (![
|
|
269
|
-
"compact",
|
|
270
|
-
"recovered",
|
|
271
|
-
"der"
|
|
272
|
-
].includes(format)) throw new Error("Signature format must be \"compact\", \"recovered\", or \"der\"");
|
|
273
|
-
return format;
|
|
274
|
-
}
|
|
275
|
-
function validateSigOpts(opts, def) {
|
|
276
|
-
validateObject(opts);
|
|
277
|
-
const optsn = {};
|
|
278
|
-
for (let optName of Object.keys(def)) optsn[optName] = opts[optName] === void 0 ? def[optName] : opts[optName];
|
|
279
|
-
abool(optsn.lowS, "lowS");
|
|
280
|
-
abool(optsn.prehash, "prehash");
|
|
281
|
-
if (optsn.format !== void 0) validateSigFormat(optsn.format);
|
|
282
|
-
return optsn;
|
|
283
|
-
}
|
|
284
|
-
var _0n = /* @__PURE__ */ BigInt(0);
|
|
285
|
-
var _1n = /* @__PURE__ */ BigInt(1);
|
|
286
|
-
var _2n = /* @__PURE__ */ BigInt(2);
|
|
287
|
-
var _3n = /* @__PURE__ */ BigInt(3);
|
|
288
|
-
var _4n = /* @__PURE__ */ BigInt(4);
|
|
289
|
-
/**
|
|
290
|
-
* Creates weierstrass Point constructor, based on specified curve options.
|
|
291
|
-
*
|
|
292
|
-
* See {@link WeierstrassOpts}.
|
|
293
|
-
* @param params - Curve parameters. See {@link WeierstrassOpts}.
|
|
294
|
-
* @param extraOpts - Optional helpers and overrides. See {@link WeierstrassExtraOpts}.
|
|
295
|
-
* @returns Weierstrass point constructor.
|
|
296
|
-
* @throws If the curve parameters, overrides, or point codecs are invalid. {@link Error}
|
|
297
|
-
*
|
|
298
|
-
* @example
|
|
299
|
-
* Construct a point type from explicit Weierstrass curve parameters.
|
|
300
|
-
*
|
|
301
|
-
* ```js
|
|
302
|
-
* const opts = {
|
|
303
|
-
* p: 0xfffffffffffffffffffffffffffffffeffffac73n,
|
|
304
|
-
* n: 0x100000000000000000001b8fa16dfab9aca16b6b3n,
|
|
305
|
-
* h: 1n,
|
|
306
|
-
* a: 0n,
|
|
307
|
-
* b: 7n,
|
|
308
|
-
* Gx: 0x3b4c382ce37aa192a4019e763036f4f5dd4d7ebbn,
|
|
309
|
-
* Gy: 0x938cf935318fdced6bc28286531733c3f03c4feen,
|
|
310
|
-
* };
|
|
311
|
-
* const secp160k1_Point = weierstrass(opts);
|
|
312
|
-
* ```
|
|
313
|
-
*/
|
|
314
|
-
function weierstrass(params, extraOpts = {}) {
|
|
315
|
-
const validated = createCurveFields("weierstrass", params, extraOpts);
|
|
316
|
-
const Fp = validated.Fp;
|
|
317
|
-
const Fn = validated.Fn;
|
|
318
|
-
let CURVE = validated.CURVE;
|
|
319
|
-
const { h: cofactor, n: CURVE_ORDER } = CURVE;
|
|
320
|
-
validateObject(extraOpts, {}, {
|
|
321
|
-
allowInfinityPoint: "boolean",
|
|
322
|
-
clearCofactor: "function",
|
|
323
|
-
isTorsionFree: "function",
|
|
324
|
-
fromBytes: "function",
|
|
325
|
-
toBytes: "function",
|
|
326
|
-
endo: "object",
|
|
327
|
-
randomBytes: "function"
|
|
328
|
-
});
|
|
329
|
-
const { endo: endoOpts, allowInfinityPoint, clearCofactor, isTorsionFree, fromBytes, toBytes } = extraOpts;
|
|
330
|
-
const randomBytes$1 = extraOpts.randomBytes === void 0 ? randomBytes : extraOpts.randomBytes;
|
|
331
|
-
if (endoOpts) {
|
|
332
|
-
if (!Fp.is0(CURVE.a) || typeof endoOpts.beta !== "bigint" || !Array.isArray(endoOpts.basises)) throw new Error("invalid endo: expected \"beta\": bigint and \"basises\": array");
|
|
333
|
-
}
|
|
334
|
-
const endo = endoOpts ? {
|
|
335
|
-
beta: endoOpts.beta,
|
|
336
|
-
basises: endoOpts.basises.map((basis) => [...basis])
|
|
337
|
-
} : void 0;
|
|
338
|
-
const lengths = getWLengths(Fp, Fn);
|
|
339
|
-
function assertCompressionIsSupported() {
|
|
340
|
-
if (!Fp.isOdd) throw new Error("compression is not supported: Field does not have .isOdd()");
|
|
341
|
-
}
|
|
342
|
-
function pointToBytes(_c, point, isCompressed) {
|
|
343
|
-
if (point.is0()) {
|
|
344
|
-
if (!allowInfinityPoint) throw new Error("bad point: ZERO");
|
|
345
|
-
return Uint8Array.of(0);
|
|
346
|
-
}
|
|
347
|
-
const { x, y } = point.toAffine();
|
|
348
|
-
const bx = Fp.toBytes(x);
|
|
349
|
-
abool(isCompressed, "isCompressed");
|
|
350
|
-
if (isCompressed) {
|
|
351
|
-
assertCompressionIsSupported();
|
|
352
|
-
const hasEvenY = !Fp.isOdd(y);
|
|
353
|
-
return concatBytes(pprefix(hasEvenY), bx);
|
|
354
|
-
} else return concatBytes(Uint8Array.of(4), bx, Fp.toBytes(y));
|
|
355
|
-
}
|
|
356
|
-
function pointFromBytes(bytes) {
|
|
357
|
-
abytes$1(bytes, void 0, "Point");
|
|
358
|
-
const { publicKey: comp, publicKeyUncompressed: uncomp } = lengths;
|
|
359
|
-
const length = bytes.length;
|
|
360
|
-
const head = bytes[0];
|
|
361
|
-
const tail = bytes.subarray(1);
|
|
362
|
-
if (allowInfinityPoint && length === 1 && head === 0) return {
|
|
363
|
-
x: Fp.ZERO,
|
|
364
|
-
y: Fp.ZERO
|
|
365
|
-
};
|
|
366
|
-
if (length === comp && (head === 2 || head === 3)) {
|
|
367
|
-
const x = Fp.fromBytes(tail);
|
|
368
|
-
if (!Fp.isValid(x)) throw new Error("bad point: is not on curve, wrong x");
|
|
369
|
-
const y2 = weierstrassEquation(x);
|
|
370
|
-
let y;
|
|
371
|
-
try {
|
|
372
|
-
y = Fp.sqrt(y2);
|
|
373
|
-
} catch (sqrtError) {
|
|
374
|
-
const err = sqrtError instanceof Error ? ": " + sqrtError.message : "";
|
|
375
|
-
throw new Error("bad point: is not on curve, sqrt error" + err);
|
|
376
|
-
}
|
|
377
|
-
assertCompressionIsSupported();
|
|
378
|
-
const evenY = Fp.isOdd(y);
|
|
379
|
-
if ((head & 1) === 1 !== evenY) y = Fp.neg(y);
|
|
380
|
-
return {
|
|
381
|
-
x,
|
|
382
|
-
y
|
|
383
|
-
};
|
|
384
|
-
} else if (length === uncomp && head === 4) {
|
|
385
|
-
const L = Fp.BYTES;
|
|
386
|
-
const x = Fp.fromBytes(tail.subarray(0, L));
|
|
387
|
-
const y = Fp.fromBytes(tail.subarray(L, L * 2));
|
|
388
|
-
if (!isValidXY(x, y)) throw new Error("bad point: is not on curve");
|
|
389
|
-
return {
|
|
390
|
-
x,
|
|
391
|
-
y
|
|
392
|
-
};
|
|
393
|
-
} else throw new Error(`bad point: got length ${length}, expected compressed=${comp} or uncompressed=${uncomp}`);
|
|
394
|
-
}
|
|
395
|
-
const encodePoint = toBytes === void 0 ? pointToBytes : toBytes;
|
|
396
|
-
const decodePoint = fromBytes === void 0 ? pointFromBytes : fromBytes;
|
|
397
|
-
const b3 = Fp.mul(CURVE.b, _3n);
|
|
398
|
-
const mulA = Fp.is0(CURVE.a) ? (_) => Fp.ZERO : (x) => Fp.mul(CURVE.a, x);
|
|
399
|
-
function weierstrassEquation(x) {
|
|
400
|
-
const x2 = Fp.sqr(x);
|
|
401
|
-
const x3 = Fp.mul(x2, x);
|
|
402
|
-
return Fp.add(Fp.add(x3, Fp.mul(x, CURVE.a)), CURVE.b);
|
|
403
|
-
}
|
|
404
|
-
/** Checks whether equation holds for given x, y: y² == x³ + ax + b */
|
|
405
|
-
function isValidXY(x, y) {
|
|
406
|
-
const left = Fp.sqr(y);
|
|
407
|
-
const right = weierstrassEquation(x);
|
|
408
|
-
return Fp.eql(left, right);
|
|
409
|
-
}
|
|
410
|
-
if (!isValidXY(CURVE.Gx, CURVE.Gy)) throw new Error("bad curve params: generator point");
|
|
411
|
-
const _4a3 = Fp.mul(Fp.pow(CURVE.a, _3n), _4n);
|
|
412
|
-
const _27b2 = Fp.mul(Fp.sqr(CURVE.b), BigInt(27));
|
|
413
|
-
if (Fp.is0(Fp.add(_4a3, _27b2))) throw new Error("bad curve params: a or b");
|
|
414
|
-
/** Asserts coordinate is valid: 0 <= n < Fp.ORDER. */
|
|
415
|
-
function acoord(title, n, banZero = false) {
|
|
416
|
-
if (!Fp.isValid(n) || banZero && Fp.is0(n)) throw new Error(`bad point coordinate ${title}`);
|
|
417
|
-
return typeof n === "object" && n !== null ? Fp.create(n) : n;
|
|
418
|
-
}
|
|
419
|
-
function aprjpoint(other) {
|
|
420
|
-
if (!(other instanceof Point)) throw new Error("Weierstrass Point expected");
|
|
421
|
-
}
|
|
422
|
-
function splitEndoScalarN(k) {
|
|
423
|
-
if (!endo || !endo.basises) throw new Error("no endo");
|
|
424
|
-
return _splitEndoScalar(k, endo.basises, Fn.ORDER);
|
|
425
|
-
}
|
|
426
|
-
/**
|
|
427
|
-
* Appends a (point, scalar) pair to the inputs of a vartime wNAF walk
|
|
428
|
-
* ({@link mulAddUnsafe}). With GLV endomorphism the scalar is split into two half-width
|
|
429
|
-
* pairs against P and ψ(P) = (β⋅x, y), halving the walk's shared doubling chain;
|
|
430
|
-
* split signs fold into the points.
|
|
431
|
-
*/
|
|
432
|
-
function pushWnafPair(points, scalars, p, k) {
|
|
433
|
-
if (!Fn.isValid(k)) throw new RangeError("invalid scalar: out of range");
|
|
434
|
-
if (endo) {
|
|
435
|
-
const { k1neg, k1, k2neg, k2 } = splitEndoScalarN(k);
|
|
436
|
-
const psi = new Point(Fp.mul(p.X, endo.beta), p.Y, p.Z);
|
|
437
|
-
points.push(k1neg ? p.negate() : p, k2neg ? psi.negate() : psi);
|
|
438
|
-
scalars.push(k1, k2);
|
|
439
|
-
} else {
|
|
440
|
-
points.push(p);
|
|
441
|
-
scalars.push(k);
|
|
442
|
-
}
|
|
443
|
-
}
|
|
444
|
-
const validityCache = /* @__PURE__ */ new WeakSet();
|
|
445
|
-
/**
|
|
446
|
-
* Projective Point works in 3d / projective (homogeneous) coordinates:(X, Y, Z) ∋ (x=X/Z, y=Y/Z).
|
|
447
|
-
* Default Point works in 2d / affine coordinates: (x, y).
|
|
448
|
-
* We're doing calculations in projective, because its operations don't require costly inversion.
|
|
449
|
-
*/
|
|
450
|
-
class Point {
|
|
451
|
-
static BASE = new Point(CURVE.Gx, CURVE.Gy, Fp.ONE);
|
|
452
|
-
static ZERO = new Point(Fp.ZERO, Fp.ONE, Fp.ZERO);
|
|
453
|
-
static Fp = Fp;
|
|
454
|
-
static Fn = Fn;
|
|
455
|
-
X;
|
|
456
|
-
Y;
|
|
457
|
-
Z;
|
|
458
|
-
/** Does NOT validate if the point is valid. Use `.assertValidity()`. */
|
|
459
|
-
constructor(X, Y, Z) {
|
|
460
|
-
this.X = acoord("x", X);
|
|
461
|
-
this.Y = acoord("y", Y, true);
|
|
462
|
-
this.Z = acoord("z", Z);
|
|
463
|
-
Object.freeze(this);
|
|
464
|
-
}
|
|
465
|
-
static CURVE() {
|
|
466
|
-
return CURVE;
|
|
467
|
-
}
|
|
468
|
-
/** Does NOT validate if the point is valid. Use `.assertValidity()`. */
|
|
469
|
-
static fromAffine(p) {
|
|
470
|
-
const { x, y } = p || {};
|
|
471
|
-
if (!p || !Fp.isValid(x) || !Fp.isValid(y)) throw new Error("invalid affine point");
|
|
472
|
-
if (p instanceof Point) throw new Error("projective point not allowed");
|
|
473
|
-
if (Fp.is0(x) && Fp.is0(y)) return Point.ZERO;
|
|
474
|
-
return new Point(x, y, Fp.ONE);
|
|
475
|
-
}
|
|
476
|
-
static fromBytes(bytes) {
|
|
477
|
-
const P = Point.fromAffine(decodePoint(abytes$1(bytes, void 0, "point")));
|
|
478
|
-
P.assertValidity();
|
|
479
|
-
return P;
|
|
480
|
-
}
|
|
481
|
-
static fromHex(hex) {
|
|
482
|
-
return Point.fromBytes(hexToBytes(hex));
|
|
483
|
-
}
|
|
484
|
-
get x() {
|
|
485
|
-
return this.toAffine().x;
|
|
486
|
-
}
|
|
487
|
-
get y() {
|
|
488
|
-
return this.toAffine().y;
|
|
489
|
-
}
|
|
490
|
-
/**
|
|
491
|
-
* @param isLazy - true will defer table computation until the first multiplication
|
|
492
|
-
*/
|
|
493
|
-
precompute(windowSize = 6, isLazy = true) {
|
|
494
|
-
wnaf.setWindowSize(this, windowSize);
|
|
495
|
-
if (!isLazy) this.multiply(_3n);
|
|
496
|
-
return this;
|
|
497
|
-
}
|
|
498
|
-
/** A point on curve is valid if it conforms to equation. */
|
|
499
|
-
assertValidity() {
|
|
500
|
-
const p = this;
|
|
501
|
-
if (p.is0()) {
|
|
502
|
-
if (allowInfinityPoint && Fp.is0(p.X) && Fp.eql(p.Y, Fp.ONE) && Fp.is0(p.Z)) return;
|
|
503
|
-
throw new Error("bad point: ZERO");
|
|
504
|
-
}
|
|
505
|
-
if (validityCache.has(p)) return;
|
|
506
|
-
const { x, y } = p.toAffine();
|
|
507
|
-
if (!Fp.isValid(x) || !Fp.isValid(y)) throw new Error("bad point: x or y not field elements");
|
|
508
|
-
if (!isValidXY(x, y)) throw new Error("bad point: equation left != right");
|
|
509
|
-
if (!p.isTorsionFree()) throw new Error("bad point: not in prime-order subgroup");
|
|
510
|
-
validityCache.add(p);
|
|
511
|
-
}
|
|
512
|
-
hasEvenY() {
|
|
513
|
-
const { y } = this.toAffine();
|
|
514
|
-
if (!Fp.isOdd) throw new Error("Field doesn't support isOdd");
|
|
515
|
-
return !Fp.isOdd(y);
|
|
516
|
-
}
|
|
517
|
-
/** Compare one point to another. */
|
|
518
|
-
equals(other) {
|
|
519
|
-
aprjpoint(other);
|
|
520
|
-
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
521
|
-
const { X: X2, Y: Y2, Z: Z2 } = other;
|
|
522
|
-
const U1 = Fp.eql(Fp.mul(X1, Z2), Fp.mul(X2, Z1));
|
|
523
|
-
const U2 = Fp.eql(Fp.mul(Y1, Z2), Fp.mul(Y2, Z1));
|
|
524
|
-
return U1 && U2;
|
|
525
|
-
}
|
|
526
|
-
/** Flips point to one corresponding to (x, -y) in Affine coordinates. */
|
|
527
|
-
negate() {
|
|
528
|
-
return new Point(this.X, Fp.neg(this.Y), this.Z);
|
|
529
|
-
}
|
|
530
|
-
double() {
|
|
531
|
-
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
532
|
-
let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO;
|
|
533
|
-
let t0 = Fp.mul(X1, X1);
|
|
534
|
-
let t1 = Fp.mul(Y1, Y1);
|
|
535
|
-
let t2 = Fp.mul(Z1, Z1);
|
|
536
|
-
let t3 = Fp.mul(X1, Y1);
|
|
537
|
-
t3 = Fp.add(t3, t3);
|
|
538
|
-
Z3 = Fp.mul(X1, Z1);
|
|
539
|
-
Z3 = Fp.add(Z3, Z3);
|
|
540
|
-
X3 = mulA(Z3);
|
|
541
|
-
Y3 = Fp.mul(b3, t2);
|
|
542
|
-
Y3 = Fp.add(X3, Y3);
|
|
543
|
-
X3 = Fp.sub(t1, Y3);
|
|
544
|
-
Y3 = Fp.add(t1, Y3);
|
|
545
|
-
Y3 = Fp.mul(X3, Y3);
|
|
546
|
-
X3 = Fp.mul(t3, X3);
|
|
547
|
-
Z3 = Fp.mul(b3, Z3);
|
|
548
|
-
t2 = mulA(t2);
|
|
549
|
-
t3 = Fp.sub(t0, t2);
|
|
550
|
-
t3 = mulA(t3);
|
|
551
|
-
t3 = Fp.add(t3, Z3);
|
|
552
|
-
Z3 = Fp.add(t0, t0);
|
|
553
|
-
t0 = Fp.add(Z3, t0);
|
|
554
|
-
t0 = Fp.add(t0, t2);
|
|
555
|
-
t0 = Fp.mul(t0, t3);
|
|
556
|
-
Y3 = Fp.add(Y3, t0);
|
|
557
|
-
t2 = Fp.mul(Y1, Z1);
|
|
558
|
-
t2 = Fp.add(t2, t2);
|
|
559
|
-
t0 = Fp.mul(t2, t3);
|
|
560
|
-
X3 = Fp.sub(X3, t0);
|
|
561
|
-
Z3 = Fp.mul(t2, t1);
|
|
562
|
-
Z3 = Fp.add(Z3, Z3);
|
|
563
|
-
Z3 = Fp.add(Z3, Z3);
|
|
564
|
-
return new Point(X3, Y3, Z3);
|
|
565
|
-
}
|
|
566
|
-
add(other) {
|
|
567
|
-
aprjpoint(other);
|
|
568
|
-
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
569
|
-
const { X: X2, Y: Y2, Z: Z2 } = other;
|
|
570
|
-
let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO;
|
|
571
|
-
let t0 = Fp.mul(X1, X2);
|
|
572
|
-
let t1 = Fp.mul(Y1, Y2);
|
|
573
|
-
let t2 = Fp.mul(Z1, Z2);
|
|
574
|
-
let t3 = Fp.add(X1, Y1);
|
|
575
|
-
let t4 = Fp.add(X2, Y2);
|
|
576
|
-
t3 = Fp.mul(t3, t4);
|
|
577
|
-
t4 = Fp.add(t0, t1);
|
|
578
|
-
t3 = Fp.sub(t3, t4);
|
|
579
|
-
t4 = Fp.add(X1, Z1);
|
|
580
|
-
let t5 = Fp.add(X2, Z2);
|
|
581
|
-
t4 = Fp.mul(t4, t5);
|
|
582
|
-
t5 = Fp.add(t0, t2);
|
|
583
|
-
t4 = Fp.sub(t4, t5);
|
|
584
|
-
t5 = Fp.add(Y1, Z1);
|
|
585
|
-
X3 = Fp.add(Y2, Z2);
|
|
586
|
-
t5 = Fp.mul(t5, X3);
|
|
587
|
-
X3 = Fp.add(t1, t2);
|
|
588
|
-
t5 = Fp.sub(t5, X3);
|
|
589
|
-
Z3 = mulA(t4);
|
|
590
|
-
X3 = Fp.mul(b3, t2);
|
|
591
|
-
Z3 = Fp.add(X3, Z3);
|
|
592
|
-
X3 = Fp.sub(t1, Z3);
|
|
593
|
-
Z3 = Fp.add(t1, Z3);
|
|
594
|
-
Y3 = Fp.mul(X3, Z3);
|
|
595
|
-
t1 = Fp.add(t0, t0);
|
|
596
|
-
t1 = Fp.add(t1, t0);
|
|
597
|
-
t2 = mulA(t2);
|
|
598
|
-
t4 = Fp.mul(b3, t4);
|
|
599
|
-
t1 = Fp.add(t1, t2);
|
|
600
|
-
t2 = Fp.sub(t0, t2);
|
|
601
|
-
t2 = mulA(t2);
|
|
602
|
-
t4 = Fp.add(t4, t2);
|
|
603
|
-
t0 = Fp.mul(t1, t4);
|
|
604
|
-
Y3 = Fp.add(Y3, t0);
|
|
605
|
-
t0 = Fp.mul(t5, t4);
|
|
606
|
-
X3 = Fp.mul(t3, X3);
|
|
607
|
-
X3 = Fp.sub(X3, t0);
|
|
608
|
-
t0 = Fp.mul(t3, t1);
|
|
609
|
-
Z3 = Fp.mul(t5, Z3);
|
|
610
|
-
Z3 = Fp.add(Z3, t0);
|
|
611
|
-
return new Point(X3, Y3, Z3);
|
|
612
|
-
}
|
|
613
|
-
subtract(other) {
|
|
614
|
-
aprjpoint(other);
|
|
615
|
-
return this.add(other.negate());
|
|
616
|
-
}
|
|
617
|
-
is0() {
|
|
618
|
-
return this.equals(Point.ZERO);
|
|
619
|
-
}
|
|
620
|
-
/**
|
|
621
|
-
* Constant time multiplication.
|
|
622
|
-
* Uses precomputed tables (signed fixed-window wNAF) when available.
|
|
623
|
-
* Uses scalar blinding and avoids endomorphism splitting in the secret-scalar path.
|
|
624
|
-
* @param scalar - by which the point would be multiplied
|
|
625
|
-
* @returns New point
|
|
626
|
-
*/
|
|
627
|
-
multiply(scalar) {
|
|
628
|
-
if (!Fn.isValidNot0(scalar)) throw new RangeError("invalid scalar: out of range");
|
|
629
|
-
const { p, f } = wnaf.mulSecret(this, scalar, cofactor, normalize);
|
|
630
|
-
return normalize([p, f])[0];
|
|
631
|
-
}
|
|
632
|
-
/**
|
|
633
|
-
* Non-constant-time multiplication. Uses width-4 wNAF with GLV endomorphism splitting
|
|
634
|
-
* when available (two half-width scalars sharing one halved doubling chain).
|
|
635
|
-
* It's faster, but should only be used when you don't care about
|
|
636
|
-
* an exposed secret key e.g. sig verification, which works over *public* keys.
|
|
637
|
-
*/
|
|
638
|
-
multiplyUnsafe(scalar) {
|
|
639
|
-
const p = this;
|
|
640
|
-
const sc = scalar;
|
|
641
|
-
if (!Fn.isValid(sc)) throw new RangeError("invalid scalar: out of range");
|
|
642
|
-
if (sc === _0n || p.is0()) return Point.ZERO;
|
|
643
|
-
if (sc === _1n) return p;
|
|
644
|
-
if (wnaf.hasWindowSize(this)) return wnaf.mulUnsafe(p, sc, normalize);
|
|
645
|
-
const points = [];
|
|
646
|
-
const scalars = [];
|
|
647
|
-
pushWnafPair(points, scalars, p, sc);
|
|
648
|
-
return mulAddUnsafe(Point, points, scalars);
|
|
649
|
-
}
|
|
650
|
-
/**
|
|
651
|
-
* Non-constant-time double-scalar multiplication `a⋅this + b⋅other` (Strauss–Shamir).
|
|
652
|
-
* Both walks share one doubling chain via {@link mulAddUnsafe}, and GLV endomorphism
|
|
653
|
-
* (when available) halves the chain again by splitting each scalar into two half-width
|
|
654
|
-
* parts. Used by ECDSA verification and public-key recovery for `R = u1⋅G + u2⋅P`.
|
|
655
|
-
* Only for public scalars.
|
|
656
|
-
*/
|
|
657
|
-
mulAddUnsafe(a, other, b) {
|
|
658
|
-
aprjpoint(other);
|
|
659
|
-
const points = [];
|
|
660
|
-
const scalars = [];
|
|
661
|
-
pushWnafPair(points, scalars, this, a);
|
|
662
|
-
pushWnafPair(points, scalars, other, b);
|
|
663
|
-
return mulAddUnsafe(Point, points, scalars);
|
|
664
|
-
}
|
|
665
|
-
/**
|
|
666
|
-
* Converts Projective point to affine (x, y) coordinates.
|
|
667
|
-
* (X, Y, Z) ∋ (x=X/Z, y=Y/Z).
|
|
668
|
-
* @param invertedZ - Z^-1 (inverted zero) - optional, precomputation is useful for invertBatch
|
|
669
|
-
*/
|
|
670
|
-
toAffine(invertedZ) {
|
|
671
|
-
const p = this;
|
|
672
|
-
let iz = invertedZ;
|
|
673
|
-
if (iz != null && !Fp.isValid(iz)) throw new RangeError("\"invertedZ\" expected valid field element");
|
|
674
|
-
const { X, Y, Z } = p;
|
|
675
|
-
if (Fp.eql(Z, Fp.ONE)) return {
|
|
676
|
-
x: X,
|
|
677
|
-
y: Y
|
|
678
|
-
};
|
|
679
|
-
const is0 = p.is0();
|
|
680
|
-
if (iz == null) iz = is0 ? Fp.ONE : Fp.inv(Z);
|
|
681
|
-
const x = Fp.mul(X, iz);
|
|
682
|
-
const y = Fp.mul(Y, iz);
|
|
683
|
-
const zz = Fp.mul(Z, iz);
|
|
684
|
-
if (is0) return {
|
|
685
|
-
x: Fp.ZERO,
|
|
686
|
-
y: Fp.ZERO
|
|
687
|
-
};
|
|
688
|
-
if (!Fp.eql(zz, Fp.ONE)) throw new Error("invZ was invalid");
|
|
689
|
-
return {
|
|
690
|
-
x,
|
|
691
|
-
y
|
|
692
|
-
};
|
|
693
|
-
}
|
|
694
|
-
/**
|
|
695
|
-
* Checks whether Point is free of torsion elements (is in prime subgroup).
|
|
696
|
-
* Always torsion-free for cofactor=1 curves.
|
|
697
|
-
*/
|
|
698
|
-
isTorsionFree() {
|
|
699
|
-
if (cofactor === _1n) return true;
|
|
700
|
-
if (isTorsionFree) return isTorsionFree(Point, this);
|
|
701
|
-
return wnaf.mulUnsafe(this, CURVE_ORDER).is0();
|
|
702
|
-
}
|
|
703
|
-
clearCofactor() {
|
|
704
|
-
if (cofactor === _1n) return this;
|
|
705
|
-
if (clearCofactor) return clearCofactor(Point, this);
|
|
706
|
-
return this.multiplyUnsafe(cofactor);
|
|
707
|
-
}
|
|
708
|
-
isSmallOrder() {
|
|
709
|
-
if (cofactor === _1n) return this.is0();
|
|
710
|
-
return this.clearCofactor().is0();
|
|
711
|
-
}
|
|
712
|
-
toBytes(isCompressed = true) {
|
|
713
|
-
abool(isCompressed, "isCompressed");
|
|
714
|
-
this.assertValidity();
|
|
715
|
-
return encodePoint(Point, this, isCompressed);
|
|
716
|
-
}
|
|
717
|
-
toHex(isCompressed = true) {
|
|
718
|
-
return bytesToHex(this.toBytes(isCompressed));
|
|
719
|
-
}
|
|
720
|
-
toString() {
|
|
721
|
-
return `<Point ${this.is0() ? "ZERO" : this.toHex()}>`;
|
|
722
|
-
}
|
|
723
|
-
}
|
|
724
|
-
const normalize = (points) => normalizeZ(Point, points);
|
|
725
|
-
const wnaf = new ScalarMultiplier(Point, randomBytes$1);
|
|
726
|
-
if (wnaf.bits >= 6) Point.BASE.precompute(6);
|
|
727
|
-
Object.freeze(Point.prototype);
|
|
728
|
-
Object.freeze(Point);
|
|
729
|
-
return Point;
|
|
730
|
-
}
|
|
731
|
-
function pprefix(hasEvenY) {
|
|
732
|
-
return Uint8Array.of(hasEvenY ? 2 : 3);
|
|
733
|
-
}
|
|
734
|
-
function getWLengths(Fp, Fn) {
|
|
735
|
-
return {
|
|
736
|
-
secretKey: Fn.BYTES,
|
|
737
|
-
publicKey: 1 + Fp.BYTES,
|
|
738
|
-
publicKeyUncompressed: 1 + 2 * Fp.BYTES,
|
|
739
|
-
publicKeyHasPrefix: true,
|
|
740
|
-
signature: 2 * Fn.BYTES
|
|
741
|
-
};
|
|
742
|
-
}
|
|
743
|
-
/**
|
|
744
|
-
* Sometimes users only need getPublicKey, getSharedSecret, and secret key handling.
|
|
745
|
-
* This helper ensures no signature functionality is present. Less code, smaller bundle size.
|
|
746
|
-
* @param Point - Weierstrass point constructor.
|
|
747
|
-
* @param ecdhOpts - Optional randomness helpers:
|
|
748
|
-
* - `randomBytes` (optional): Optional RNG override.
|
|
749
|
-
* @returns ECDH helper namespace.
|
|
750
|
-
* @example
|
|
751
|
-
* Sometimes users only need getPublicKey, getSharedSecret, and secret key handling.
|
|
752
|
-
*
|
|
753
|
-
* ```ts
|
|
754
|
-
* import { ecdh } from '@noble/curves/abstract/weierstrass.js';
|
|
755
|
-
* import { p256 } from '@noble/curves/nist.js';
|
|
756
|
-
* const dh = ecdh(p256.Point);
|
|
757
|
-
* const alice = dh.keygen();
|
|
758
|
-
* const shared = dh.getSharedSecret(alice.secretKey, alice.publicKey);
|
|
759
|
-
* ```
|
|
760
|
-
*/
|
|
761
|
-
function ecdh(Point, ecdhOpts = {}) {
|
|
762
|
-
validatePointCons(Point);
|
|
763
|
-
const { Fn } = Point;
|
|
764
|
-
const randomBytes_ = ecdhOpts.randomBytes === void 0 ? randomBytes : ecdhOpts.randomBytes;
|
|
765
|
-
const lengths = Object.assign(getWLengths(Point.Fp, Fn), { seed: Math.max(getMinHashLength(Fn.ORDER), 16) });
|
|
766
|
-
function isValidSecretKey(secretKey) {
|
|
767
|
-
try {
|
|
768
|
-
const num = Fn.fromBytes(secretKey);
|
|
769
|
-
return Fn.isValidNot0(num);
|
|
770
|
-
} catch (error) {
|
|
771
|
-
return false;
|
|
772
|
-
}
|
|
773
|
-
}
|
|
774
|
-
function isValidPublicKey(publicKey, isCompressed) {
|
|
775
|
-
const { publicKey: comp, publicKeyUncompressed } = lengths;
|
|
776
|
-
try {
|
|
777
|
-
const l = publicKey.length;
|
|
778
|
-
if (isCompressed === true && l !== comp) return false;
|
|
779
|
-
if (isCompressed === false && l !== publicKeyUncompressed) return false;
|
|
780
|
-
return !Point.fromBytes(publicKey).is0();
|
|
781
|
-
} catch (error) {
|
|
782
|
-
return false;
|
|
783
|
-
}
|
|
784
|
-
}
|
|
785
|
-
/**
|
|
786
|
-
* Produces cryptographically secure secret key from random of size
|
|
787
|
-
* (groupLen + ceil(groupLen / 2)) with modulo bias being negligible.
|
|
788
|
-
*/
|
|
789
|
-
function randomSecretKey(seed) {
|
|
790
|
-
seed = seed === void 0 ? randomBytes_(lengths.seed) : seed;
|
|
791
|
-
return mapHashToField(abytes$1(seed, lengths.seed, "seed"), Fn.ORDER);
|
|
792
|
-
}
|
|
793
|
-
/**
|
|
794
|
-
* Computes public key for a secret key. Checks for validity of the secret key.
|
|
795
|
-
* @param isCompressed - whether to return compact (default), or full key
|
|
796
|
-
* @returns Public key, full when isCompressed=false; short when isCompressed=true
|
|
797
|
-
*/
|
|
798
|
-
function getPublicKey(secretKey, isCompressed = true) {
|
|
799
|
-
return Point.BASE.multiply(Fn.fromBytes(secretKey)).toBytes(isCompressed);
|
|
800
|
-
}
|
|
801
|
-
/**
|
|
802
|
-
* Quick and dirty check for item being public key. Does not validate hex, or being on-curve.
|
|
803
|
-
*/
|
|
804
|
-
function isProbPub(item) {
|
|
805
|
-
const { secretKey, publicKey, publicKeyUncompressed } = lengths;
|
|
806
|
-
const allowedLengths = Fn._lengths;
|
|
807
|
-
if (!isBytes(item)) return void 0;
|
|
808
|
-
const l = abytes$1(item, void 0, "key").length;
|
|
809
|
-
const isPub = l === publicKey || l === publicKeyUncompressed;
|
|
810
|
-
const isSec = l === secretKey || !!allowedLengths?.includes(l);
|
|
811
|
-
if (isPub && isSec) return void 0;
|
|
812
|
-
return isPub;
|
|
813
|
-
}
|
|
814
|
-
/**
|
|
815
|
-
* ECDH (Elliptic Curve Diffie Hellman).
|
|
816
|
-
* Computes encoded shared point from secret key A and public key B.
|
|
817
|
-
* Checks: 1) secret key validity 2) shared key is on-curve.
|
|
818
|
-
* Does NOT hash the result or expose the SEC 1 x-coordinate-only `z`.
|
|
819
|
-
* Returns the encoded shared point on purpose: callers that need `x_P`
|
|
820
|
-
* can derive it from the encoded point, but `x_P` alone cannot recover the
|
|
821
|
-
* point/parity back.
|
|
822
|
-
* This helper only exposes the fully validated public-key path, not cofactor DH.
|
|
823
|
-
* @param isCompressed - whether to return compact (default), or full key
|
|
824
|
-
* @returns shared point encoding
|
|
825
|
-
*/
|
|
826
|
-
function getSharedSecret(secretKeyA, publicKeyB, isCompressed = true) {
|
|
827
|
-
if (isProbPub(secretKeyA) === true) throw new Error("first arg must be private key");
|
|
828
|
-
if (isProbPub(publicKeyB) === false) throw new Error("second arg must be public key");
|
|
829
|
-
const s = Fn.fromBytes(secretKeyA);
|
|
830
|
-
const b = Point.fromBytes(publicKeyB);
|
|
831
|
-
if (b.is0()) throw new Error("invalid public key: point at infinity");
|
|
832
|
-
return b.multiply(s).toBytes(isCompressed);
|
|
833
|
-
}
|
|
834
|
-
const utils = {
|
|
835
|
-
isValidSecretKey,
|
|
836
|
-
isValidPublicKey,
|
|
837
|
-
randomSecretKey
|
|
838
|
-
};
|
|
839
|
-
const keygen = createKeygen(randomSecretKey, getPublicKey);
|
|
840
|
-
Object.freeze(utils);
|
|
841
|
-
Object.freeze(lengths);
|
|
842
|
-
return Object.freeze({
|
|
843
|
-
getPublicKey,
|
|
844
|
-
getSharedSecret,
|
|
845
|
-
keygen,
|
|
846
|
-
Point,
|
|
847
|
-
utils,
|
|
848
|
-
lengths
|
|
849
|
-
});
|
|
850
|
-
}
|
|
851
|
-
/**
|
|
852
|
-
* Creates ECDSA signing interface for given elliptic curve `Point` and `hash` function.
|
|
853
|
-
*
|
|
854
|
-
* @param Point - created using {@link weierstrass} function
|
|
855
|
-
* @param hash - used for 1) message prehash-ing 2) k generation in `sign`, using hmac_drbg(hash)
|
|
856
|
-
* @param ecdsaOpts - rarely needed, see {@link ECDSAOpts}:
|
|
857
|
-
* - `lowS`: Default low-S policy.
|
|
858
|
-
* - `hmac`: HMAC implementation used by RFC6979 DRBG.
|
|
859
|
-
* - `randomBytes`: Optional RNG override.
|
|
860
|
-
* - `bits2int`: Optional hash-to-int conversion override.
|
|
861
|
-
* - `bits2int_modN`: Optional hash-to-int-mod-n conversion override.
|
|
862
|
-
*
|
|
863
|
-
* @returns ECDSA helper namespace.
|
|
864
|
-
* @example
|
|
865
|
-
* Create an ECDSA signer/verifier bundle for one curve implementation.
|
|
866
|
-
*
|
|
867
|
-
* ```ts
|
|
868
|
-
* import { ecdsa } from '@noble/curves/abstract/weierstrass.js';
|
|
869
|
-
* import { p256 } from '@noble/curves/nist.js';
|
|
870
|
-
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
871
|
-
* const p256ecdsa = ecdsa(p256.Point, sha256);
|
|
872
|
-
* const { secretKey, publicKey } = p256ecdsa.keygen();
|
|
873
|
-
* const msg = new TextEncoder().encode('hello noble');
|
|
874
|
-
* const sig = p256ecdsa.sign(msg, secretKey);
|
|
875
|
-
* const isValid = p256ecdsa.verify(sig, msg, publicKey);
|
|
876
|
-
* ```
|
|
877
|
-
*/
|
|
878
|
-
function ecdsa(Point, hash, ecdsaOpts = {}) {
|
|
879
|
-
validatePointCons(Point);
|
|
880
|
-
const hash_ = hash;
|
|
881
|
-
ahash(hash_);
|
|
882
|
-
validateObject(ecdsaOpts, {}, {
|
|
883
|
-
hmac: "function",
|
|
884
|
-
lowS: "boolean",
|
|
885
|
-
randomBytes: "function",
|
|
886
|
-
bits2int: "function",
|
|
887
|
-
bits2int_modN: "function"
|
|
888
|
-
});
|
|
889
|
-
const opts = Object.assign({}, ecdsaOpts);
|
|
890
|
-
const randomBytes$2 = opts.randomBytes === void 0 ? randomBytes : opts.randomBytes;
|
|
891
|
-
const hmac$1 = opts.hmac === void 0 ? (key, msg) => hmac(hash_, key, msg) : opts.hmac;
|
|
892
|
-
const { Fp, Fn } = Point;
|
|
893
|
-
const { ORDER: CURVE_ORDER, BITS: fnBits } = Fn;
|
|
894
|
-
const blindLength = getMinHashLength(CURVE_ORDER);
|
|
895
|
-
const csprng = probeRandomBytes(randomBytes$2, blindLength);
|
|
896
|
-
const { keygen, getPublicKey, getSharedSecret, utils, lengths } = ecdh(Point, opts);
|
|
897
|
-
const defaultSigOpts = {
|
|
898
|
-
prehash: true,
|
|
899
|
-
lowS: typeof opts.lowS === "boolean" ? opts.lowS : true,
|
|
900
|
-
format: "compact",
|
|
901
|
-
extraEntropy: false
|
|
902
|
-
};
|
|
903
|
-
const hasLargeRecoveryLifts = CURVE_ORDER * _2n + _1n < Fp.ORDER;
|
|
904
|
-
function isBiggerThanHalfOrder(number) {
|
|
905
|
-
return number > CURVE_ORDER >> _1n;
|
|
906
|
-
}
|
|
907
|
-
function validateRS(title, num) {
|
|
908
|
-
if (!Fn.isValidNot0(num)) throw new Error(`invalid signature ${title}: out of range 1..Point.Fn.ORDER`);
|
|
909
|
-
return num;
|
|
910
|
-
}
|
|
911
|
-
function assertFieldSignIsSupported() {
|
|
912
|
-
if (!Fp.isOdd) throw new Error("Field doesn't support isOdd");
|
|
913
|
-
}
|
|
914
|
-
function getRecoveryBit(x, y, r) {
|
|
915
|
-
assertFieldSignIsSupported();
|
|
916
|
-
return (x === r ? 0 : 2) | Number(Fp.isOdd(y));
|
|
917
|
-
}
|
|
918
|
-
function assertRecoverableCurve() {
|
|
919
|
-
if (hasLargeRecoveryLifts) throw new Error("\"recovered\" sig type is not supported for cofactor >2 curves");
|
|
920
|
-
}
|
|
921
|
-
function validateSigLength(bytes, format) {
|
|
922
|
-
validateSigFormat(format);
|
|
923
|
-
const size = lengths.signature;
|
|
924
|
-
const sizer = format === "compact" ? size : format === "recovered" ? size + 1 : void 0;
|
|
925
|
-
return abytes$1(bytes, sizer);
|
|
926
|
-
}
|
|
927
|
-
/**
|
|
928
|
-
* ECDSA signature with its (r, s) properties. Supports compact, recovered & DER representations.
|
|
929
|
-
*/
|
|
930
|
-
class Signature {
|
|
931
|
-
r;
|
|
932
|
-
s;
|
|
933
|
-
recovery;
|
|
934
|
-
constructor(r, s, recovery) {
|
|
935
|
-
this.r = validateRS("r", r);
|
|
936
|
-
this.s = validateRS("s", s);
|
|
937
|
-
if (recovery != null) {
|
|
938
|
-
assertRecoverableCurve();
|
|
939
|
-
if (![
|
|
940
|
-
0,
|
|
941
|
-
1,
|
|
942
|
-
2,
|
|
943
|
-
3
|
|
944
|
-
].includes(recovery)) throw new Error("invalid recovery id");
|
|
945
|
-
this.recovery = recovery;
|
|
946
|
-
}
|
|
947
|
-
Object.freeze(this);
|
|
948
|
-
}
|
|
949
|
-
static fromBytes(bytes, format = defaultSigOpts.format) {
|
|
950
|
-
validateSigLength(bytes, format);
|
|
951
|
-
let recid;
|
|
952
|
-
if (format === "der") {
|
|
953
|
-
if (bytes.length > 2 * Fn.BYTES + 16) throw new DER.Err("invalid signature: DER signature too long");
|
|
954
|
-
const { r, s } = DER.toSig(abytes$1(bytes), Fn.BYTES + 1);
|
|
955
|
-
return new Signature(r, s);
|
|
956
|
-
}
|
|
957
|
-
if (format === "recovered") {
|
|
958
|
-
recid = bytes[0];
|
|
959
|
-
format = "compact";
|
|
960
|
-
bytes = bytes.subarray(1);
|
|
961
|
-
}
|
|
962
|
-
const L = lengths.signature / 2;
|
|
963
|
-
const r = bytes.subarray(0, L);
|
|
964
|
-
const s = bytes.subarray(L, L * 2);
|
|
965
|
-
return new Signature(Fn.fromBytes(r), Fn.fromBytes(s), recid);
|
|
966
|
-
}
|
|
967
|
-
static fromHex(hex, format) {
|
|
968
|
-
return this.fromBytes(hexToBytes(hex), format);
|
|
969
|
-
}
|
|
970
|
-
assertRecovery() {
|
|
971
|
-
const { recovery } = this;
|
|
972
|
-
if (recovery == null) throw new Error("invalid recovery id: must be present");
|
|
973
|
-
return recovery;
|
|
974
|
-
}
|
|
975
|
-
addRecoveryBit(recovery) {
|
|
976
|
-
return new Signature(this.r, this.s, recovery);
|
|
977
|
-
}
|
|
978
|
-
recoverPublicKey(messageHash) {
|
|
979
|
-
const { r, s } = this;
|
|
980
|
-
const recovery = this.assertRecovery();
|
|
981
|
-
const radj = recovery === 2 || recovery === 3 ? r + CURVE_ORDER : r;
|
|
982
|
-
if (!Fp.isValid(radj)) throw new Error("invalid recovery id: sig.r+curve.n != R.x");
|
|
983
|
-
const x = Fp.toBytes(radj);
|
|
984
|
-
const R = Point.fromBytes(concatBytes(pprefix((recovery & 1) === 0), x));
|
|
985
|
-
const ir = Fn.inv(radj);
|
|
986
|
-
const h = bits2int_modN(abytes$1(messageHash, void 0, "msgHash"));
|
|
987
|
-
const u1 = Fn.create(-h * ir);
|
|
988
|
-
const u2 = Fn.create(s * ir);
|
|
989
|
-
const Q = Point.BASE.mulAddUnsafe(u1, R, u2);
|
|
990
|
-
if (Q.is0()) throw new Error("invalid recovery: point at infinify");
|
|
991
|
-
Q.assertValidity();
|
|
992
|
-
return Q;
|
|
993
|
-
}
|
|
994
|
-
hasHighS() {
|
|
995
|
-
return isBiggerThanHalfOrder(this.s);
|
|
996
|
-
}
|
|
997
|
-
toBytes(format = defaultSigOpts.format) {
|
|
998
|
-
validateSigFormat(format);
|
|
999
|
-
if (format === "der") return hexToBytes(DER.hexFromSig(this));
|
|
1000
|
-
const { r, s } = this;
|
|
1001
|
-
const rb = Fn.toBytes(r);
|
|
1002
|
-
const sb = Fn.toBytes(s);
|
|
1003
|
-
if (format === "recovered") {
|
|
1004
|
-
assertRecoverableCurve();
|
|
1005
|
-
return concatBytes(Uint8Array.of(this.assertRecovery()), rb, sb);
|
|
1006
|
-
}
|
|
1007
|
-
return concatBytes(rb, sb);
|
|
1008
|
-
}
|
|
1009
|
-
toHex(format) {
|
|
1010
|
-
return bytesToHex(this.toBytes(format));
|
|
1011
|
-
}
|
|
1012
|
-
}
|
|
1013
|
-
Object.freeze(Signature.prototype);
|
|
1014
|
-
Object.freeze(Signature);
|
|
1015
|
-
const bits2int = opts.bits2int === void 0 ? function bits2int_def(bytes) {
|
|
1016
|
-
if (bytes.length > 8192) throw new Error("input is too large");
|
|
1017
|
-
const num = bytesToNumberBE(bytes);
|
|
1018
|
-
const delta = bytes.length * 8 - fnBits;
|
|
1019
|
-
return delta > 0 ? num >> BigInt(delta) : num;
|
|
1020
|
-
} : opts.bits2int;
|
|
1021
|
-
const bits2int_modN = opts.bits2int_modN === void 0 ? function bits2int_modN_def(bytes) {
|
|
1022
|
-
return Fn.create(bits2int(bytes));
|
|
1023
|
-
} : opts.bits2int_modN;
|
|
1024
|
-
const ORDER_MASK = bitMask(fnBits);
|
|
1025
|
-
/** Converts to bytes. Checks if num in `[0..ORDER_MASK-1]` e.g.: `[0..2^256-1]`. */
|
|
1026
|
-
function int2octets(num) {
|
|
1027
|
-
aInRange("num < 2^" + fnBits, num, _0n, ORDER_MASK);
|
|
1028
|
-
return Fn.toBytes(num);
|
|
1029
|
-
}
|
|
1030
|
-
function validateMsgAndHash(message, prehash) {
|
|
1031
|
-
abytes$1(message, void 0, "message");
|
|
1032
|
-
return prehash ? abytes$1(hash_(message), void 0, "prehashed message") : message;
|
|
1033
|
-
}
|
|
1034
|
-
/**
|
|
1035
|
-
* Steps A, D of RFC6979 3.2.
|
|
1036
|
-
* Creates RFC6979 seed; converts msg/privKey to numbers.
|
|
1037
|
-
* Used only in sign, not in verify.
|
|
1038
|
-
*
|
|
1039
|
-
* Warning: we cannot assume here that message has same amount of bytes as curve order,
|
|
1040
|
-
* this will be invalid at least for P521. Also it can be bigger for P224 + SHA256.
|
|
1041
|
-
*/
|
|
1042
|
-
function prepSig(message, secretKey, opts) {
|
|
1043
|
-
const { lowS, prehash, extraEntropy } = validateSigOpts(opts, defaultSigOpts);
|
|
1044
|
-
message = validateMsgAndHash(message, prehash);
|
|
1045
|
-
const h1int = bits2int_modN(message);
|
|
1046
|
-
const d = Fn.fromBytes(secretKey);
|
|
1047
|
-
if (!Fn.isValidNot0(d)) throw new Error("invalid private key");
|
|
1048
|
-
const seedArgs = [int2octets(d), int2octets(h1int)];
|
|
1049
|
-
if (extraEntropy != null && extraEntropy !== false) {
|
|
1050
|
-
const e = extraEntropy === true ? randomBytes$2(lengths.secretKey) : extraEntropy;
|
|
1051
|
-
seedArgs.push(abytes$1(e, void 0, "extraEntropy"));
|
|
1052
|
-
}
|
|
1053
|
-
const seed = concatBytes(...seedArgs);
|
|
1054
|
-
const m = h1int;
|
|
1055
|
-
function k2sig(kBytes) {
|
|
1056
|
-
const k = bits2int(kBytes);
|
|
1057
|
-
if (!Fn.isValidNot0(k)) return;
|
|
1058
|
-
const q = Point.BASE.multiply(k).toAffine();
|
|
1059
|
-
const r = Fn.create(q.x);
|
|
1060
|
-
if (r === _0n) return;
|
|
1061
|
-
let s;
|
|
1062
|
-
if (csprng !== void 0) {
|
|
1063
|
-
const b = bytesToNumberBE(mapHashToField(csprng(blindLength), CURVE_ORDER));
|
|
1064
|
-
const ibk = Fn.inv(Fn.mul(b, k));
|
|
1065
|
-
const bm = Fn.mul(b, m);
|
|
1066
|
-
const bd = Fn.mul(b, d);
|
|
1067
|
-
s = Fn.create(ibk * Fn.create(bm + bd * r));
|
|
1068
|
-
} else {
|
|
1069
|
-
const ik = invertCt(k, CURVE_ORDER);
|
|
1070
|
-
s = Fn.create(ik * Fn.create(m + r * d));
|
|
1071
|
-
}
|
|
1072
|
-
if (s === _0n) return;
|
|
1073
|
-
let recovery = getRecoveryBit(q.x, q.y, r);
|
|
1074
|
-
let normS = s;
|
|
1075
|
-
if (lowS && isBiggerThanHalfOrder(s)) {
|
|
1076
|
-
normS = Fn.neg(s);
|
|
1077
|
-
recovery ^= 1;
|
|
1078
|
-
}
|
|
1079
|
-
return new Signature(r, normS, hasLargeRecoveryLifts ? void 0 : recovery);
|
|
1080
|
-
}
|
|
1081
|
-
return {
|
|
1082
|
-
seed,
|
|
1083
|
-
k2sig
|
|
1084
|
-
};
|
|
1085
|
-
}
|
|
1086
|
-
/**
|
|
1087
|
-
* Signs a message or message hash with a secret key.
|
|
1088
|
-
* With the default `prehash: true`, raw message bytes are hashed internally;
|
|
1089
|
-
* only `{ prehash: false }` expects a caller-supplied digest.
|
|
1090
|
-
*
|
|
1091
|
-
* ```
|
|
1092
|
-
* sign(m, d) where
|
|
1093
|
-
* k = rfc6979_hmac_drbg(m, d)
|
|
1094
|
-
* (x, y) = G × k
|
|
1095
|
-
* r = x mod n
|
|
1096
|
-
* s = (m + dr) / k mod n
|
|
1097
|
-
* ```
|
|
1098
|
-
*/
|
|
1099
|
-
function sign(message, secretKey, opts = {}) {
|
|
1100
|
-
const { seed, k2sig } = prepSig(message, secretKey, opts);
|
|
1101
|
-
return createHmacDrbg(hash_.outputLen, Fn.BYTES, hmac$1)(seed, k2sig).toBytes(opts.format);
|
|
1102
|
-
}
|
|
1103
|
-
/**
|
|
1104
|
-
* Verifies a signature against message and public key.
|
|
1105
|
-
* Rejects lowS signatures by default: see {@link ECDSAVerifyOpts}.
|
|
1106
|
-
* Implements section 4.1.4 from https://www.secg.org/sec1-v2.pdf:
|
|
1107
|
-
*
|
|
1108
|
-
* ```
|
|
1109
|
-
* verify(r, s, h, P) where
|
|
1110
|
-
* u1 = hs^-1 mod n
|
|
1111
|
-
* u2 = rs^-1 mod n
|
|
1112
|
-
* R = u1⋅G + u2⋅P
|
|
1113
|
-
* mod(R.x, n) == r
|
|
1114
|
-
* ```
|
|
1115
|
-
*/
|
|
1116
|
-
function verify(signature, message, publicKey, opts = {}) {
|
|
1117
|
-
const { lowS, prehash, format } = validateSigOpts(opts, defaultSigOpts);
|
|
1118
|
-
publicKey = abytes$1(publicKey, void 0, "publicKey");
|
|
1119
|
-
message = validateMsgAndHash(message, prehash);
|
|
1120
|
-
if (!isBytes(signature)) {
|
|
1121
|
-
const end = signature instanceof Signature ? ", use sig.toBytes()" : "";
|
|
1122
|
-
throw new Error("verify expects Uint8Array signature" + end);
|
|
1123
|
-
}
|
|
1124
|
-
validateSigLength(signature, format);
|
|
1125
|
-
try {
|
|
1126
|
-
const sig = Signature.fromBytes(signature, format);
|
|
1127
|
-
const P = Point.fromBytes(publicKey);
|
|
1128
|
-
if (P.is0()) return false;
|
|
1129
|
-
if (lowS && sig.hasHighS()) return false;
|
|
1130
|
-
const { r, s } = sig;
|
|
1131
|
-
const h = bits2int_modN(message);
|
|
1132
|
-
const is = Fn.inv(s);
|
|
1133
|
-
const u1 = Fn.create(h * is);
|
|
1134
|
-
const u2 = Fn.create(r * is);
|
|
1135
|
-
const R = Point.BASE.mulAddUnsafe(u1, P, u2);
|
|
1136
|
-
if (R.is0()) return false;
|
|
1137
|
-
const q = R.toAffine();
|
|
1138
|
-
if (Fn.create(q.x) !== r) return false;
|
|
1139
|
-
if (format === "recovered" && sig.recovery !== getRecoveryBit(q.x, q.y, r)) return false;
|
|
1140
|
-
return true;
|
|
1141
|
-
} catch (e) {
|
|
1142
|
-
return false;
|
|
1143
|
-
}
|
|
1144
|
-
}
|
|
1145
|
-
function recoverPublicKey(signature, message, opts = {}) {
|
|
1146
|
-
const { prehash } = validateSigOpts(opts, defaultSigOpts);
|
|
1147
|
-
message = validateMsgAndHash(message, prehash);
|
|
1148
|
-
return Signature.fromBytes(signature, "recovered").recoverPublicKey(message).toBytes();
|
|
1149
|
-
}
|
|
1150
|
-
return Object.freeze({
|
|
1151
|
-
keygen,
|
|
1152
|
-
getPublicKey,
|
|
1153
|
-
getSharedSecret,
|
|
1154
|
-
utils,
|
|
1155
|
-
lengths,
|
|
1156
|
-
Point,
|
|
1157
|
-
sign,
|
|
1158
|
-
verify,
|
|
1159
|
-
recoverPublicKey,
|
|
1160
|
-
Signature,
|
|
1161
|
-
hash: hash_
|
|
1162
|
-
});
|
|
1163
|
-
}
|
|
1164
|
-
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
1165
|
-
/**
|
|
1166
|
-
* NIST P256 (aka secp256r1, prime256v1) curve, ECDSA and ECDH methods.
|
|
1167
|
-
* Hashes inputs with sha256 by default.
|
|
1168
|
-
*
|
|
1169
|
-
* @example
|
|
1170
|
-
* Generate one P-256 keypair, sign a message, and verify it.
|
|
1171
|
-
*
|
|
1172
|
-
* ```js
|
|
1173
|
-
* import { p256 } from '@noble/curves/nist.js';
|
|
1174
|
-
* const { secretKey, publicKey } = p256.keygen();
|
|
1175
|
-
* const recovered = p256.getPublicKey(secretKey);
|
|
1176
|
-
* const peer = p256.keygen();
|
|
1177
|
-
* const shared = p256.getSharedSecret(secretKey, peer.publicKey);
|
|
1178
|
-
* const msg = new TextEncoder().encode('hello noble');
|
|
1179
|
-
* const sig = p256.sign(msg, secretKey, { lowS: true, prehash: true });
|
|
1180
|
-
* const isValid = p256.verify(sig, msg, publicKey);
|
|
1181
|
-
* // const sigKeccak = p256.sign(keccak256(msg), secretKey, { prehash: false });
|
|
1182
|
-
* ```
|
|
1183
|
-
*/
|
|
1184
|
-
var p256 = /* @__PURE__ */ ecdsa(/* @__PURE__ */ weierstrass(/* @__PURE__ */ (() => ({
|
|
1185
|
-
p: BigInt("0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff"),
|
|
1186
|
-
n: BigInt("0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551"),
|
|
1187
|
-
h: BigInt(1),
|
|
1188
|
-
a: BigInt("0xffffffff00000001000000000000000000000000fffffffffffffffffffffffc"),
|
|
1189
|
-
b: BigInt("0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b"),
|
|
1190
|
-
Gx: BigInt("0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296"),
|
|
1191
|
-
Gy: BigInt("0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5")
|
|
1192
|
-
}))()), sha256);
|
|
1193
|
-
//#endregion
|
|
1194
|
-
export { p256 as t };
|