@decryption/curves 0.2.0
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/LICENSE +21 -0
- package/README.md +75 -0
- package/abstract/bls.d.ts +410 -0
- package/abstract/bls.js +433 -0
- package/abstract/curve.d.ts +441 -0
- package/abstract/curve.js +622 -0
- package/abstract/edwards.d.ts +319 -0
- package/abstract/edwards.js +697 -0
- package/abstract/fft.d.ts +438 -0
- package/abstract/fft.js +580 -0
- package/abstract/frost.d.ts +292 -0
- package/abstract/frost.js +705 -0
- package/abstract/hash-to-curve.d.ts +238 -0
- package/abstract/hash-to-curve.js +353 -0
- package/abstract/modular.d.ts +550 -0
- package/abstract/modular.js +872 -0
- package/abstract/montgomery.d.ts +95 -0
- package/abstract/montgomery.js +180 -0
- package/abstract/oprf.d.ts +354 -0
- package/abstract/oprf.js +358 -0
- package/abstract/poseidon.d.ts +198 -0
- package/abstract/poseidon.js +471 -0
- package/abstract/tower.d.ts +213 -0
- package/abstract/tower.js +963 -0
- package/abstract/weierstrass.d.ts +587 -0
- package/abstract/weierstrass.js +1421 -0
- package/bls12-381.d.ts +23 -0
- package/bls12-381.js +680 -0
- package/bn254.d.ts +74 -0
- package/bn254.js +248 -0
- package/ed25519.d.ts +211 -0
- package/ed25519.js +626 -0
- package/ed448.d.ts +184 -0
- package/ed448.js +596 -0
- package/esm/abstract/bls.js +429 -0
- package/esm/abstract/curve.js +610 -0
- package/esm/abstract/edwards.js +691 -0
- package/esm/abstract/fft.js +567 -0
- package/esm/abstract/frost.js +702 -0
- package/esm/abstract/hash-to-curve.js +345 -0
- package/esm/abstract/modular.js +849 -0
- package/esm/abstract/montgomery.js +177 -0
- package/esm/abstract/oprf.js +355 -0
- package/esm/abstract/poseidon.js +462 -0
- package/esm/abstract/tower.js +925 -0
- package/esm/abstract/weierstrass.js +1411 -0
- package/esm/bls12-381.js +677 -0
- package/esm/bn254.js +244 -0
- package/esm/ed25519.js +622 -0
- package/esm/ed448.js +593 -0
- package/esm/index.js +38 -0
- package/esm/misc.js +211 -0
- package/esm/nist.js +266 -0
- package/esm/secp256k1.js +465 -0
- package/esm/utils.js +597 -0
- package/esm/webcrypto.js +362 -0
- package/index.d.ts +0 -0
- package/index.js +38 -0
- package/misc.d.ts +113 -0
- package/misc.js +216 -0
- package/nist.d.ts +136 -0
- package/nist.js +269 -0
- package/package.json +46 -0
- package/secp256k1.d.ts +152 -0
- package/secp256k1.js +468 -0
- package/utils.d.ts +535 -0
- package/utils.js +626 -0
- package/webcrypto.d.ts +240 -0
- package/webcrypto.js +365 -0
package/esm/webcrypto.js
ADDED
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
/** Raw type */
|
|
2
|
+
const TYPE_RAW = 'raw';
|
|
3
|
+
const TYPE_JWK = 'jwk';
|
|
4
|
+
const TYPE_SPKI = 'spki';
|
|
5
|
+
const TYPE_PKCS = 'pkcs8';
|
|
6
|
+
// default formats
|
|
7
|
+
const dfsec = TYPE_PKCS;
|
|
8
|
+
const dfpub = TYPE_SPKI;
|
|
9
|
+
function getSubtle() {
|
|
10
|
+
const s = globalThis?.crypto?.subtle;
|
|
11
|
+
if (typeof s === 'object' && s != null)
|
|
12
|
+
return s;
|
|
13
|
+
throw new Error('crypto.subtle must be defined');
|
|
14
|
+
}
|
|
15
|
+
function createKeygenA(randomSecretKey, getPublicKey) {
|
|
16
|
+
// Runtime accepts an accidental `keygen(seed)` argument for parity with other wrappers, but the
|
|
17
|
+
// seed is intentionally ignored because WebCrypto keygen here always goes through fresh keygen.
|
|
18
|
+
return async function keygenA(_seed) {
|
|
19
|
+
const secretKey = (await randomSecretKey());
|
|
20
|
+
return { secretKey, publicKey: (await getPublicKey(secretKey)) };
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
// Internal helper only: strict hex parser for the local hardcoded PKCS8 header constants.
|
|
24
|
+
function hexToBytesLocal(hex) {
|
|
25
|
+
const pairs = hex.match(/[0-9a-f]{2}/gi);
|
|
26
|
+
if (!pairs || pairs.length * 2 !== hex.length)
|
|
27
|
+
throw new Error('invalid hex');
|
|
28
|
+
return Uint8Array.from(pairs, (b) => Number.parseInt(b, 16));
|
|
29
|
+
}
|
|
30
|
+
export const __TEST = /* @__PURE__ */ Object.freeze({
|
|
31
|
+
hexToBytesLocal,
|
|
32
|
+
});
|
|
33
|
+
function assertType(type, key) {
|
|
34
|
+
// Callers are expected to pass a non-null key-like object; `null` / `undefined` still fail first
|
|
35
|
+
// via property access before reaching the explicit wrapper error.
|
|
36
|
+
if (key.type !== type)
|
|
37
|
+
throw new Error(`invalid key type, expected ${type}`);
|
|
38
|
+
}
|
|
39
|
+
function createKeyUtils(algo, derive, keyLen, pkcs8header) {
|
|
40
|
+
const secUsage = derive ? ['deriveBits'] : ['sign'];
|
|
41
|
+
const pubUsage = derive ? [] : ['verify'];
|
|
42
|
+
// Return Uint8Array instead of ArrayBuffer
|
|
43
|
+
const arrBufToU8 = (res, format) => (format === TYPE_JWK
|
|
44
|
+
? res
|
|
45
|
+
: new Uint8Array(res));
|
|
46
|
+
const pub = {
|
|
47
|
+
async import(key, format) {
|
|
48
|
+
// For sign/verify wrappers we pass caller-provided JWK metadata through unchanged and let
|
|
49
|
+
// WebCrypto enforce mismatched `key_ops` / extractability instead of normalizing it here.
|
|
50
|
+
const keyi = await getSubtle().importKey(format, key, algo, true, pubUsage);
|
|
51
|
+
assertType('public', keyi);
|
|
52
|
+
return keyi;
|
|
53
|
+
},
|
|
54
|
+
async export(key, format) {
|
|
55
|
+
assertType('public', key);
|
|
56
|
+
const keyi = await getSubtle().exportKey(format, key);
|
|
57
|
+
return arrBufToU8(keyi, format);
|
|
58
|
+
},
|
|
59
|
+
async convert(key, inFormat, outFormat) {
|
|
60
|
+
return pub.export(await pub.import(key, inFormat), outFormat);
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
const priv = {
|
|
64
|
+
async import(key, format) {
|
|
65
|
+
const crypto = getSubtle();
|
|
66
|
+
let keyi;
|
|
67
|
+
if (format === TYPE_RAW) {
|
|
68
|
+
// Chrome, node, bun, deno: works
|
|
69
|
+
// Safari, Firefox: Data provided to an operation does not meet requirements
|
|
70
|
+
// This is the best one can do. JWK can't be used: it contains public key component inside.
|
|
71
|
+
const k = key;
|
|
72
|
+
const head = hexToBytesLocal(pkcs8header);
|
|
73
|
+
const all = new Uint8Array(head.length + k.length);
|
|
74
|
+
all.set(head, 0);
|
|
75
|
+
all.set(k, head.length);
|
|
76
|
+
keyi = await crypto.importKey(TYPE_PKCS, all, algo, true, secUsage);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
// Sign/verify wrappers keep caller JWK metadata as-is and assume the supplied `key_ops`
|
|
80
|
+
// already match the requested operation. ECDH is different: noble treats the same key
|
|
81
|
+
// material as usable for both sign and derive, so JWK imported through the derive path
|
|
82
|
+
// must rewrite `key_ops` or WebCrypto refuses otherwise-correct keys exported by keygen.
|
|
83
|
+
if (derive && format === TYPE_JWK)
|
|
84
|
+
key = { ...key, key_ops: secUsage };
|
|
85
|
+
keyi = await crypto.importKey(format, key, algo, true, secUsage);
|
|
86
|
+
}
|
|
87
|
+
assertType('private', keyi);
|
|
88
|
+
return keyi;
|
|
89
|
+
},
|
|
90
|
+
async export(key, format) {
|
|
91
|
+
const crypto = getSubtle();
|
|
92
|
+
assertType('private', key);
|
|
93
|
+
if (format === TYPE_RAW) {
|
|
94
|
+
// scure-base base64urlnopad could have been used, but we can't add more deps.
|
|
95
|
+
// pkcs8 would be even more fragile
|
|
96
|
+
const jwk = await crypto.exportKey(TYPE_JWK, key);
|
|
97
|
+
const base64 = jwk.d.replace(/-/g, '+').replace(/_/g, '/'); // base64url
|
|
98
|
+
const pad = base64.length % 4 ? '='.repeat(4 - (base64.length % 4)) : ''; // add padding
|
|
99
|
+
const binary = atob(base64 + pad);
|
|
100
|
+
// This is not ASCII, and not text: this is only semi-safe with atob output
|
|
101
|
+
const raw = Uint8Array.from(binary, (c) => c.charCodeAt(0));
|
|
102
|
+
// Pad key to key len because Bun strips leading zero for P-521 only
|
|
103
|
+
const res = new Uint8Array(keyLen);
|
|
104
|
+
res.set(raw, keyLen - raw.length);
|
|
105
|
+
return res;
|
|
106
|
+
}
|
|
107
|
+
const keyi = await crypto.exportKey(format, key);
|
|
108
|
+
return arrBufToU8(keyi, format);
|
|
109
|
+
},
|
|
110
|
+
async convert(key, inFormat, outFormat) {
|
|
111
|
+
return priv.export(await priv.import(key, inFormat), outFormat);
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
async function getPublicKey(secretKey, opts = {}) {
|
|
115
|
+
const fsec = opts.formatSec ?? dfsec;
|
|
116
|
+
const fpub = opts.formatPub ?? dfpub;
|
|
117
|
+
// Export to jwk, remove private scalar and then convert to format
|
|
118
|
+
const jwk = (fsec === TYPE_JWK ? { ...secretKey } : await priv.convert(secretKey, fsec, TYPE_JWK));
|
|
119
|
+
delete jwk.d;
|
|
120
|
+
jwk.key_ops = pubUsage;
|
|
121
|
+
if (fpub === TYPE_JWK)
|
|
122
|
+
return jwk;
|
|
123
|
+
return pub.convert(jwk, TYPE_JWK, fpub);
|
|
124
|
+
}
|
|
125
|
+
async function randomSecretKey(format = dfsec) {
|
|
126
|
+
const keyPair = await getSubtle().generateKey(algo, true, secUsage);
|
|
127
|
+
return priv.export(keyPair.privateKey, format);
|
|
128
|
+
}
|
|
129
|
+
// Key generation could be slow, so we cache result once.
|
|
130
|
+
let supported;
|
|
131
|
+
return {
|
|
132
|
+
pub: pub,
|
|
133
|
+
priv: priv,
|
|
134
|
+
async isSupported() {
|
|
135
|
+
if (supported !== undefined)
|
|
136
|
+
return supported;
|
|
137
|
+
try {
|
|
138
|
+
const crypto = getSubtle();
|
|
139
|
+
const key = await crypto.generateKey(algo, true, secUsage);
|
|
140
|
+
// Deno is broken and generates key for unsupported curves, but then fails on export
|
|
141
|
+
await priv.export(key.privateKey, TYPE_JWK);
|
|
142
|
+
// Bun fails on derive for x25519, but not x448
|
|
143
|
+
if (derive) {
|
|
144
|
+
await crypto.deriveBits({ name: typeof algo === 'string' ? algo : algo.name, public: key.publicKey }, key.privateKey, 8);
|
|
145
|
+
}
|
|
146
|
+
return (supported = true);
|
|
147
|
+
}
|
|
148
|
+
catch (e) {
|
|
149
|
+
return (supported = false);
|
|
150
|
+
}
|
|
151
|
+
},
|
|
152
|
+
getPublicKey,
|
|
153
|
+
keygen: createKeygenA(randomSecretKey, getPublicKey),
|
|
154
|
+
utils: Object.freeze({
|
|
155
|
+
randomSecretKey,
|
|
156
|
+
// Runtime expects both formats explicitly here; omitted formats just flow into
|
|
157
|
+
// `subtle.importKey(...)`, and JWK conversion also assumes extractable keys (`ext !== false`).
|
|
158
|
+
convertPublicKey: pub.convert,
|
|
159
|
+
// Runtime expects both formats explicitly here; omitted formats just flow into
|
|
160
|
+
// `subtle.importKey(...)`, and JWK conversion also assumes extractable keys (`ext !== false`).
|
|
161
|
+
convertSecretKey: priv.convert,
|
|
162
|
+
}),
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
function createSigner(keys, algo) {
|
|
166
|
+
return {
|
|
167
|
+
// Historical param name: wrappers pass message bytes here, while WebCrypto performs the
|
|
168
|
+
// algorithm-specific hashing itself for ECDSA. We also return provider signatures verbatim:
|
|
169
|
+
// this wrapper is intentionally "raw WebCrypto", so it does not parse scalars or normalize
|
|
170
|
+
// high-S ECDSA outputs into software noble's low-S convention.
|
|
171
|
+
async sign(msgHash, secretKey, opts = {}) {
|
|
172
|
+
const key = await keys.priv.import(secretKey, opts.formatSec ?? dfsec);
|
|
173
|
+
const sig = await getSubtle().sign(algo, key, msgHash);
|
|
174
|
+
return new Uint8Array(sig);
|
|
175
|
+
},
|
|
176
|
+
async verify(signature, msgHash, publicKey, opts = {}) {
|
|
177
|
+
const key = await keys.pub.import(publicKey, opts.formatPub ?? dfpub);
|
|
178
|
+
return await getSubtle().verify(algo, key, signature, msgHash);
|
|
179
|
+
},
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
function createECDH(keys, algo, keyLen) {
|
|
183
|
+
return {
|
|
184
|
+
// Runtime accepts the alternate key formats supported by `keys.import(...)`; the public type is
|
|
185
|
+
// still narrower than that accepted surface.
|
|
186
|
+
async getSharedSecret(secretKeyA, publicKeyB, opts = {}) {
|
|
187
|
+
// if (_isCompressed !== true) throw new Error('WebCrypto only supports compressed keys');
|
|
188
|
+
const secKey = await keys.priv.import(secretKeyA, opts.formatSec === undefined ? dfsec : opts.formatSec);
|
|
189
|
+
const pubKey = await keys.pub.import(publicKeyB, opts.formatPub === undefined ? dfpub : opts.formatPub);
|
|
190
|
+
const shared = await getSubtle().deriveBits({ name: typeof algo === 'string' ? algo : algo.name, public: pubKey }, secKey, 8 * keyLen);
|
|
191
|
+
return new Uint8Array(shared);
|
|
192
|
+
},
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
function wrapECDSA(curve, hash, keyLen, pkcs8header) {
|
|
196
|
+
const ECDH_ALGO = { name: 'ECDH', namedCurve: curve };
|
|
197
|
+
const keys = createKeyUtils({ name: 'ECDSA', namedCurve: curve }, false, keyLen, pkcs8header);
|
|
198
|
+
const keysEcdh = createKeyUtils(ECDH_ALGO, true, keyLen, pkcs8header);
|
|
199
|
+
return Object.freeze({
|
|
200
|
+
name: curve,
|
|
201
|
+
// Support probing comes from the sign-side wrapper only; ECDH availability is not checked
|
|
202
|
+
// independently here even though the public wrapper also exposes `getSharedSecret(...)`.
|
|
203
|
+
isSupported: keys.isSupported,
|
|
204
|
+
getPublicKey: keys.getPublicKey,
|
|
205
|
+
keygen: createKeygenA(keys.utils.randomSecretKey, keys.getPublicKey),
|
|
206
|
+
...createSigner(keys, { name: 'ECDSA', hash: { name: hash } }),
|
|
207
|
+
...createECDH(keysEcdh, ECDH_ALGO, keyLen),
|
|
208
|
+
utils: Object.freeze({
|
|
209
|
+
...keys.utils,
|
|
210
|
+
async convertSecretKey(key, inFormat, outFormat) {
|
|
211
|
+
const jwk = inFormat === TYPE_JWK ? key : undefined;
|
|
212
|
+
// `wrapECDSA(...)` exposes the same key material for both sign and derive, so an ECDH-flavored
|
|
213
|
+
// JWK secret key from `getSharedSecret(...)` should still round-trip through `utils`.
|
|
214
|
+
if (Array.isArray(jwk?.key_ops) &&
|
|
215
|
+
jwk.key_ops.length === 1 &&
|
|
216
|
+
jwk.key_ops[0] === 'deriveBits')
|
|
217
|
+
return keysEcdh.utils.convertSecretKey(key, inFormat, outFormat);
|
|
218
|
+
return keys.utils.convertSecretKey(key, inFormat, outFormat);
|
|
219
|
+
},
|
|
220
|
+
}),
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
function wrapEdDSA(curve, keyLen, pkcs8header) {
|
|
224
|
+
const keys = createKeyUtils(curve, false, keyLen, pkcs8header);
|
|
225
|
+
return Object.freeze({
|
|
226
|
+
name: curve,
|
|
227
|
+
isSupported: keys.isSupported,
|
|
228
|
+
// This wrapper intentionally re-exports the generic WebCrypto key-conversion/signing behavior
|
|
229
|
+
// without adding extra JWK-metadata or extractability guardrails of its own.
|
|
230
|
+
getPublicKey: keys.getPublicKey,
|
|
231
|
+
keygen: createKeygenA(keys.utils.randomSecretKey, keys.getPublicKey),
|
|
232
|
+
...createSigner(keys, { name: curve }),
|
|
233
|
+
utils: keys.utils,
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
function wrapMontgomery(curve, keyLen, pkcs8header) {
|
|
237
|
+
const keys = createKeyUtils(curve, true, keyLen, pkcs8header);
|
|
238
|
+
return Object.freeze({
|
|
239
|
+
name: curve,
|
|
240
|
+
isSupported: keys.isSupported,
|
|
241
|
+
// This wrapper intentionally re-exports the generic ECDH key-format behavior without widening
|
|
242
|
+
// the narrow public `Uint8Array` key types.
|
|
243
|
+
getPublicKey: keys.getPublicKey,
|
|
244
|
+
keygen: createKeygenA(keys.utils.randomSecretKey, keys.getPublicKey),
|
|
245
|
+
...createECDH(keys, curve, keyLen),
|
|
246
|
+
utils: keys.utils,
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Friendly wrapper over built-in WebCrypto NIST P-256 (secp256r1).
|
|
251
|
+
* Inherits the generic WebCrypto ECDSA caveats: `isSupported()` only probes the sign-side API, and
|
|
252
|
+
* the conversion/signing helpers keep the shared `createKeyUtils(...)` / `createSigner(...)` quirks,
|
|
253
|
+
* including raw WebCrypto ECDSA signatures without low-S normalization.
|
|
254
|
+
* @example
|
|
255
|
+
* Check support, then sign and verify once with WebCrypto P-256.
|
|
256
|
+
*
|
|
257
|
+
* ```ts
|
|
258
|
+
* if (await p256.isSupported()) {
|
|
259
|
+
* const { secretKey, publicKey } = await p256.keygen();
|
|
260
|
+
* const msg = new TextEncoder().encode('hello noble');
|
|
261
|
+
* const sig = await p256.sign(msg, secretKey);
|
|
262
|
+
* const isValid = await p256.verify(sig, msg, publicKey);
|
|
263
|
+
* }
|
|
264
|
+
* ```
|
|
265
|
+
*/
|
|
266
|
+
export const p256 = /* @__PURE__ */ wrapECDSA('P-256', 'SHA-256', 32, '3041020100301306072a8648ce3d020106082a8648ce3d030107042730250201010420');
|
|
267
|
+
/**
|
|
268
|
+
* Friendly wrapper over built-in WebCrypto NIST P-384 (secp384r1).
|
|
269
|
+
* Inherits the generic WebCrypto ECDSA caveats around support probing and key/signing conversion.
|
|
270
|
+
* @example
|
|
271
|
+
* Check support, then sign and verify once with WebCrypto P-384.
|
|
272
|
+
*
|
|
273
|
+
* ```ts
|
|
274
|
+
* if (await p384.isSupported()) {
|
|
275
|
+
* const { secretKey, publicKey } = await p384.keygen();
|
|
276
|
+
* const msg = new TextEncoder().encode('hello noble');
|
|
277
|
+
* const sig = await p384.sign(msg, secretKey);
|
|
278
|
+
* const isValid = await p384.verify(sig, msg, publicKey);
|
|
279
|
+
* }
|
|
280
|
+
* ```
|
|
281
|
+
*/
|
|
282
|
+
export const p384 = /* @__PURE__ */ wrapECDSA('P-384', 'SHA-384', 48, '304e020100301006072a8648ce3d020106052b81040022043730350201010430');
|
|
283
|
+
/**
|
|
284
|
+
* Friendly wrapper over built-in WebCrypto NIST P-521 (secp521r1).
|
|
285
|
+
* Inherits the generic WebCrypto ECDSA caveats around support probing and key/signing conversion.
|
|
286
|
+
* @example
|
|
287
|
+
* Check support, then sign and verify once with WebCrypto P-521.
|
|
288
|
+
*
|
|
289
|
+
* ```ts
|
|
290
|
+
* if (await p521.isSupported()) {
|
|
291
|
+
* const { secretKey, publicKey } = await p521.keygen();
|
|
292
|
+
* const msg = new TextEncoder().encode('hello noble');
|
|
293
|
+
* const sig = await p521.sign(msg, secretKey);
|
|
294
|
+
* const isValid = await p521.verify(sig, msg, publicKey);
|
|
295
|
+
* }
|
|
296
|
+
* ```
|
|
297
|
+
*/
|
|
298
|
+
export const p521 = /* @__PURE__ */ wrapECDSA('P-521', 'SHA-512', 66, '3060020100301006072a8648ce3d020106052b81040023044930470201010442');
|
|
299
|
+
/**
|
|
300
|
+
* Friendly wrapper over built-in WebCrypto ed25519.
|
|
301
|
+
* Inherits the generic WebCrypto EdDSA caveats around JWK conversion metadata and extractability.
|
|
302
|
+
* @example
|
|
303
|
+
* Check support, then sign and verify once with WebCrypto Ed25519.
|
|
304
|
+
*
|
|
305
|
+
* ```ts
|
|
306
|
+
* if (await ed25519.isSupported()) {
|
|
307
|
+
* const { secretKey, publicKey } = await ed25519.keygen();
|
|
308
|
+
* const msg = new TextEncoder().encode('hello noble');
|
|
309
|
+
* const sig = await ed25519.sign(msg, secretKey);
|
|
310
|
+
* const isValid = await ed25519.verify(sig, msg, publicKey);
|
|
311
|
+
* }
|
|
312
|
+
* ```
|
|
313
|
+
*/
|
|
314
|
+
export const ed25519 = /* @__PURE__ */ wrapEdDSA('Ed25519', 32, '302e020100300506032b657004220420');
|
|
315
|
+
/**
|
|
316
|
+
* Friendly wrapper over built-in WebCrypto ed448.
|
|
317
|
+
* Inherits the generic WebCrypto EdDSA caveats around JWK conversion metadata and extractability.
|
|
318
|
+
* @example
|
|
319
|
+
* Check support, then sign and verify once with WebCrypto Ed448.
|
|
320
|
+
*
|
|
321
|
+
* ```ts
|
|
322
|
+
* if (await ed448.isSupported()) {
|
|
323
|
+
* const { secretKey, publicKey } = await ed448.keygen();
|
|
324
|
+
* const msg = new TextEncoder().encode('hello noble');
|
|
325
|
+
* const sig = await ed448.sign(msg, secretKey);
|
|
326
|
+
* const isValid = await ed448.verify(sig, msg, publicKey);
|
|
327
|
+
* }
|
|
328
|
+
* ```
|
|
329
|
+
*/
|
|
330
|
+
export const ed448 = /* @__PURE__ */ wrapEdDSA('Ed448', 57, '3047020100300506032b6571043b0439');
|
|
331
|
+
/**
|
|
332
|
+
* Friendly wrapper over built-in WebCrypto x25519 (ECDH over Curve25519).
|
|
333
|
+
* Inherits the generic WebCrypto Montgomery caveat that runtime accepts more key formats than the
|
|
334
|
+
* narrow public `Uint8Array` argument types suggest.
|
|
335
|
+
* @example
|
|
336
|
+
* Check support, then derive one shared secret with WebCrypto X25519.
|
|
337
|
+
*
|
|
338
|
+
* ```ts
|
|
339
|
+
* if (await x25519.isSupported()) {
|
|
340
|
+
* const alice = await x25519.keygen();
|
|
341
|
+
* const bob = await x25519.keygen();
|
|
342
|
+
* const shared = await x25519.getSharedSecret(alice.secretKey, bob.publicKey);
|
|
343
|
+
* }
|
|
344
|
+
* ```
|
|
345
|
+
*/
|
|
346
|
+
export const x25519 = /* @__PURE__ */ wrapMontgomery('X25519', 32, '302e020100300506032b656e04220420');
|
|
347
|
+
/**
|
|
348
|
+
* Friendly wrapper over built-in WebCrypto x448 (ECDH over Curve448).
|
|
349
|
+
* Inherits the generic WebCrypto Montgomery caveat that runtime accepts more key formats than the
|
|
350
|
+
* narrow public `Uint8Array` argument types suggest.
|
|
351
|
+
* @example
|
|
352
|
+
* Check support, then derive one shared secret with WebCrypto X448.
|
|
353
|
+
*
|
|
354
|
+
* ```ts
|
|
355
|
+
* if (await x448.isSupported()) {
|
|
356
|
+
* const alice = await x448.keygen();
|
|
357
|
+
* const bob = await x448.keygen();
|
|
358
|
+
* const shared = await x448.getSharedSecret(alice.secretKey, bob.publicKey);
|
|
359
|
+
* }
|
|
360
|
+
* ```
|
|
361
|
+
*/
|
|
362
|
+
export const x448 = /* @__PURE__ */ wrapMontgomery('X448', 56, '3046020100300506032b656f043a0438');
|
package/index.d.ts
ADDED
|
File without changes
|
package/index.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Audited & minimal JS implementation of elliptic curve cryptography.
|
|
4
|
+
* @module
|
|
5
|
+
* @example
|
|
6
|
+
```js
|
|
7
|
+
import { secp256k1, schnorr } from './secp256k1';
|
|
8
|
+
import { ed25519, ed25519ph, ed25519ctx, x25519, ristretto255 } from './ed25519';
|
|
9
|
+
import { ed448, ed448ph, x448, decaf448 } from './ed448';
|
|
10
|
+
import { p256, p384, p521 } from './nist';
|
|
11
|
+
import { bls12_381 } from './bls12-381';
|
|
12
|
+
import { bn254 } from './bn254';
|
|
13
|
+
import {
|
|
14
|
+
jubjub,
|
|
15
|
+
babyjubjub,
|
|
16
|
+
brainpoolP256r1,
|
|
17
|
+
brainpoolP384r1,
|
|
18
|
+
brainpoolP512r1,
|
|
19
|
+
} from './misc';
|
|
20
|
+
import * as webcrypto from './webcrypto';
|
|
21
|
+
|
|
22
|
+
// hash-to-curve
|
|
23
|
+
import { secp256k1_hasher } from './secp256k1';
|
|
24
|
+
import { p256_hasher, p384_hasher, p521_hasher } from './nist';
|
|
25
|
+
import { ristretto255_hasher } from './ed25519';
|
|
26
|
+
import { decaf448_hasher } from './ed448';
|
|
27
|
+
|
|
28
|
+
// OPRFs
|
|
29
|
+
import { p256_oprf, p384_oprf, p521_oprf } from './nist';
|
|
30
|
+
import { ristretto255_oprf } from './ed25519';
|
|
31
|
+
import { decaf448_oprf } from './ed448';
|
|
32
|
+
|
|
33
|
+
// utils
|
|
34
|
+
import { bytesToHex, hexToBytes, concatBytes } from './abstract/utils';
|
|
35
|
+
import { Field } from './abstract/modular';
|
|
36
|
+
```
|
|
37
|
+
*/
|
|
38
|
+
throw new Error('root module cannot be imported: import submodules instead. Check out README');
|
package/misc.d.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { type EdDSA, type EdwardsPoint } from './abstract/edwards';
|
|
2
|
+
import { type ECDSA } from './abstract/weierstrass';
|
|
3
|
+
import { type TArg } from './utils';
|
|
4
|
+
/**
|
|
5
|
+
* Generic EdDSA-over-Jubjub convenience wrapper with `sha512`.
|
|
6
|
+
* This is not the Zcash RedJubjub / Sapling signature scheme.
|
|
7
|
+
* @example
|
|
8
|
+
* Generate one Jubjub keypair, sign a message, and verify it.
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* const { secretKey, publicKey } = jubjub.keygen();
|
|
12
|
+
* const msg = new TextEncoder().encode('hello noble');
|
|
13
|
+
* const sig = jubjub.sign(msg, secretKey);
|
|
14
|
+
* const isValid = jubjub.verify(sig, msg, publicKey);
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export declare const jubjub: EdDSA;
|
|
18
|
+
/**
|
|
19
|
+
* Curve over scalar field of bn254. babyjubjub Fp = bn254 n
|
|
20
|
+
* This is a working generic EdDSA-over-BabyJubJub wrapper that uses `blake512` for the 64-byte
|
|
21
|
+
* secret expansion required by the shared EdDSA helper.
|
|
22
|
+
* It is not the BabyJubJub stack used by iden3/circomlib, `babyjubjub-rs`, or
|
|
23
|
+
* `@zk-kit/eddsa-poseidon`: those pair the subgroup base B/B8 with Blake-style secret expansion
|
|
24
|
+
* plus dedicated Poseidon / MiMC / Pedersen transcript hashing. This wrapper stays generic and is
|
|
25
|
+
* not meant as an interoperability target for those BabyJubJub signing stacks.
|
|
26
|
+
* @example
|
|
27
|
+
* Access the BabyJubJub base point and round-trip it through the point codec.
|
|
28
|
+
*
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { babyjubjub } from './misc';
|
|
31
|
+
* const base = babyjubjub.Point.BASE;
|
|
32
|
+
* const encoded = base.toBytes();
|
|
33
|
+
* const decoded = babyjubjub.Point.fromBytes(encoded);
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare const babyjubjub: EdDSA;
|
|
37
|
+
/**
|
|
38
|
+
* @param tag - Hash input.
|
|
39
|
+
* @param personalization - BLAKE2 personalization bytes.
|
|
40
|
+
* @returns Prime-order Jubjub point.
|
|
41
|
+
* @throws If the digest does not decode to a Jubjub point, or if the
|
|
42
|
+
* cofactor-cleared point has small order. {@link Error}
|
|
43
|
+
* @example
|
|
44
|
+
* Hash a tag into a prime-order Jubjub point.
|
|
45
|
+
*
|
|
46
|
+
* ```ts
|
|
47
|
+
* import { jubjub_groupHash } from './misc';
|
|
48
|
+
* import { asciiToBytes } from './utils';
|
|
49
|
+
* const tag = Uint8Array.of(2);
|
|
50
|
+
* const personalization = asciiToBytes('Zcash_G_');
|
|
51
|
+
* const point = jubjub_groupHash(tag, personalization);
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export declare function jubjub_groupHash(tag: TArg<Uint8Array>, personalization: TArg<Uint8Array>): EdwardsPoint;
|
|
55
|
+
/**
|
|
56
|
+
* No secret data is leaked here at all.
|
|
57
|
+
* It operates over public data.
|
|
58
|
+
* @param m - Message prefix.
|
|
59
|
+
* @param personalization - 8-byte BLAKE2 personalization bytes.
|
|
60
|
+
* @returns First non-zero group hash.
|
|
61
|
+
* @throws If the personalization is invalid, or if no non-zero Jubjub group
|
|
62
|
+
* hash can be found. {@link Error}
|
|
63
|
+
* @example
|
|
64
|
+
* Derive the first non-zero Jubjub group hash for one personalization tag.
|
|
65
|
+
*
|
|
66
|
+
* ```ts
|
|
67
|
+
* import { jubjub_findGroupHash } from './misc';
|
|
68
|
+
* import { asciiToBytes } from './utils';
|
|
69
|
+
* const msg = Uint8Array.of();
|
|
70
|
+
* const personalization = asciiToBytes('Zcash_G_');
|
|
71
|
+
* const point = jubjub_findGroupHash(msg, personalization);
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export declare function jubjub_findGroupHash(m: TArg<Uint8Array>, personalization: TArg<Uint8Array>): EdwardsPoint;
|
|
75
|
+
/**
|
|
76
|
+
* Brainpool P256r1 with sha256, from RFC 5639.
|
|
77
|
+
* @example
|
|
78
|
+
* Generate one Brainpool P256r1 keypair, sign a message, and verify it.
|
|
79
|
+
*
|
|
80
|
+
* ```ts
|
|
81
|
+
* const { secretKey, publicKey } = brainpoolP256r1.keygen();
|
|
82
|
+
* const msg = new TextEncoder().encode('hello noble');
|
|
83
|
+
* const sig = brainpoolP256r1.sign(msg, secretKey);
|
|
84
|
+
* const isValid = brainpoolP256r1.verify(sig, msg, publicKey);
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export declare const brainpoolP256r1: ECDSA;
|
|
88
|
+
/**
|
|
89
|
+
* Brainpool P384r1 with sha384, from RFC 5639.
|
|
90
|
+
* @example
|
|
91
|
+
* Generate one Brainpool P384r1 keypair, sign a message, and verify it.
|
|
92
|
+
*
|
|
93
|
+
* ```ts
|
|
94
|
+
* const { secretKey, publicKey } = brainpoolP384r1.keygen();
|
|
95
|
+
* const msg = new TextEncoder().encode('hello noble');
|
|
96
|
+
* const sig = brainpoolP384r1.sign(msg, secretKey);
|
|
97
|
+
* const isValid = brainpoolP384r1.verify(sig, msg, publicKey);
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
export declare const brainpoolP384r1: ECDSA;
|
|
101
|
+
/**
|
|
102
|
+
* Brainpool P512r1 with sha512, from RFC 5639.
|
|
103
|
+
* @example
|
|
104
|
+
* Generate one Brainpool P512r1 keypair, sign a message, and verify it.
|
|
105
|
+
*
|
|
106
|
+
* ```ts
|
|
107
|
+
* const { secretKey, publicKey } = brainpoolP512r1.keygen();
|
|
108
|
+
* const msg = new TextEncoder().encode('hello noble');
|
|
109
|
+
* const sig = brainpoolP512r1.sign(msg, secretKey);
|
|
110
|
+
* const isValid = brainpoolP512r1.verify(sig, msg, publicKey);
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
export declare const brainpoolP512r1: ECDSA;
|