@noble/curves 1.9.5 → 2.0.0-beta.1
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/README.md +267 -421
- package/abstract/bls.d.ts +49 -111
- package/abstract/bls.d.ts.map +1 -1
- package/abstract/bls.js +108 -152
- package/abstract/bls.js.map +1 -1
- package/abstract/curve.d.ts +7 -48
- package/abstract/curve.d.ts.map +1 -1
- package/abstract/curve.js +22 -47
- package/abstract/curve.js.map +1 -1
- package/abstract/edwards.d.ts +17 -68
- package/abstract/edwards.d.ts.map +1 -1
- package/abstract/edwards.js +98 -175
- package/abstract/edwards.js.map +1 -1
- package/abstract/fft.js +14 -27
- package/abstract/fft.js.map +1 -1
- package/abstract/hash-to-curve.d.ts +11 -24
- package/abstract/hash-to-curve.d.ts.map +1 -1
- package/abstract/hash-to-curve.js +30 -35
- package/abstract/hash-to-curve.js.map +1 -1
- package/abstract/modular.d.ts +5 -17
- package/abstract/modular.d.ts.map +1 -1
- package/abstract/modular.js +166 -167
- package/abstract/modular.js.map +1 -1
- package/abstract/montgomery.d.ts +4 -9
- package/abstract/montgomery.d.ts.map +1 -1
- package/abstract/montgomery.js +17 -20
- package/abstract/montgomery.js.map +1 -1
- package/abstract/oprf.d.ts +282 -0
- package/abstract/oprf.d.ts.map +1 -0
- package/abstract/oprf.js +297 -0
- package/abstract/oprf.js.map +1 -0
- package/abstract/poseidon.js +20 -24
- package/abstract/poseidon.js.map +1 -1
- package/abstract/tower.d.ts +9 -7
- package/abstract/tower.d.ts.map +1 -1
- package/abstract/tower.js +600 -364
- package/abstract/tower.js.map +1 -1
- package/abstract/weierstrass.d.ts +12 -145
- package/abstract/weierstrass.d.ts.map +1 -1
- package/abstract/weierstrass.js +153 -377
- package/abstract/weierstrass.js.map +1 -1
- package/bls12-381.d.ts +2 -2
- package/bls12-381.d.ts.map +1 -1
- package/bls12-381.js +174 -216
- package/bls12-381.js.map +1 -1
- package/bn254.d.ts +58 -10
- package/bn254.d.ts.map +1 -1
- package/bn254.js +70 -130
- package/bn254.js.map +1 -1
- package/ed25519.d.ts +12 -31
- package/ed25519.d.ts.map +1 -1
- package/ed25519.js +104 -146
- package/ed25519.js.map +1 -1
- package/ed448.d.ts +14 -33
- package/ed448.d.ts.map +1 -1
- package/ed448.js +105 -132
- package/ed448.js.map +1 -1
- package/index.js +1 -1
- package/misc.d.ts +10 -14
- package/misc.d.ts.map +1 -1
- package/misc.js +51 -60
- package/misc.js.map +1 -1
- package/nist.d.ts +11 -14
- package/nist.d.ts.map +1 -1
- package/nist.js +46 -55
- package/nist.js.map +1 -1
- package/package.json +9 -224
- package/secp256k1.d.ts +7 -23
- package/secp256k1.d.ts.map +1 -1
- package/secp256k1.js +72 -83
- package/secp256k1.js.map +1 -1
- package/src/abstract/bls.ts +197 -344
- package/src/abstract/curve.ts +10 -83
- package/src/abstract/edwards.ts +96 -223
- package/src/abstract/hash-to-curve.ts +32 -45
- package/src/abstract/modular.ts +144 -130
- package/src/abstract/montgomery.ts +21 -22
- package/src/abstract/oprf.ts +600 -0
- package/src/abstract/tower.ts +627 -382
- package/src/abstract/weierstrass.ts +101 -482
- package/src/bls12-381.ts +148 -176
- package/src/bn254.ts +67 -122
- package/src/ed25519.ts +65 -118
- package/src/ed448.ts +63 -113
- package/src/index.ts +1 -1
- package/src/misc.ts +66 -49
- package/src/nist.ts +48 -57
- package/src/secp256k1.ts +56 -88
- package/src/utils.ts +41 -61
- package/src/webcrypto.ts +362 -0
- package/utils.d.ts +28 -19
- package/utils.d.ts.map +1 -1
- package/utils.js +45 -121
- package/utils.js.map +1 -1
- package/webcrypto.d.ts +47 -0
- package/webcrypto.d.ts.map +1 -0
- package/webcrypto.js +231 -0
- package/webcrypto.js.map +1 -0
- package/esm/_shortw_utils.d.ts +0 -19
- package/esm/_shortw_utils.d.ts.map +0 -1
- package/esm/_shortw_utils.js +0 -16
- package/esm/_shortw_utils.js.map +0 -1
- package/esm/abstract/bls.d.ts +0 -190
- package/esm/abstract/bls.d.ts.map +0 -1
- package/esm/abstract/bls.js +0 -408
- package/esm/abstract/bls.js.map +0 -1
- package/esm/abstract/curve.d.ts +0 -231
- package/esm/abstract/curve.d.ts.map +0 -1
- package/esm/abstract/curve.js +0 -465
- package/esm/abstract/curve.js.map +0 -1
- package/esm/abstract/edwards.d.ts +0 -237
- package/esm/abstract/edwards.d.ts.map +0 -1
- package/esm/abstract/edwards.js +0 -632
- package/esm/abstract/edwards.js.map +0 -1
- package/esm/abstract/fft.d.ts +0 -122
- package/esm/abstract/fft.d.ts.map +0 -1
- package/esm/abstract/fft.js +0 -425
- package/esm/abstract/fft.js.map +0 -1
- package/esm/abstract/hash-to-curve.d.ts +0 -102
- package/esm/abstract/hash-to-curve.d.ts.map +0 -1
- package/esm/abstract/hash-to-curve.js +0 -203
- package/esm/abstract/hash-to-curve.js.map +0 -1
- package/esm/abstract/modular.d.ts +0 -171
- package/esm/abstract/modular.d.ts.map +0 -1
- package/esm/abstract/modular.js +0 -530
- package/esm/abstract/modular.js.map +0 -1
- package/esm/abstract/montgomery.d.ts +0 -30
- package/esm/abstract/montgomery.d.ts.map +0 -1
- package/esm/abstract/montgomery.js +0 -157
- package/esm/abstract/montgomery.js.map +0 -1
- package/esm/abstract/poseidon.d.ts +0 -68
- package/esm/abstract/poseidon.d.ts.map +0 -1
- package/esm/abstract/poseidon.js +0 -296
- package/esm/abstract/poseidon.js.map +0 -1
- package/esm/abstract/tower.d.ts +0 -93
- package/esm/abstract/tower.d.ts.map +0 -1
- package/esm/abstract/tower.js +0 -502
- package/esm/abstract/tower.js.map +0 -1
- package/esm/abstract/utils.d.ts +0 -5
- package/esm/abstract/utils.d.ts.map +0 -1
- package/esm/abstract/utils.js +0 -7
- package/esm/abstract/utils.js.map +0 -1
- package/esm/abstract/weierstrass.d.ts +0 -412
- package/esm/abstract/weierstrass.d.ts.map +0 -1
- package/esm/abstract/weierstrass.js +0 -1428
- package/esm/abstract/weierstrass.js.map +0 -1
- package/esm/bls12-381.d.ts +0 -16
- package/esm/bls12-381.d.ts.map +0 -1
- package/esm/bls12-381.js +0 -738
- package/esm/bls12-381.js.map +0 -1
- package/esm/bn254.d.ts +0 -18
- package/esm/bn254.d.ts.map +0 -1
- package/esm/bn254.js +0 -246
- package/esm/bn254.js.map +0 -1
- package/esm/ed25519.d.ts +0 -106
- package/esm/ed25519.d.ts.map +0 -1
- package/esm/ed25519.js +0 -467
- package/esm/ed25519.js.map +0 -1
- package/esm/ed448.d.ts +0 -101
- package/esm/ed448.d.ts.map +0 -1
- package/esm/ed448.js +0 -448
- package/esm/ed448.js.map +0 -1
- package/esm/index.d.ts +0 -2
- package/esm/index.d.ts.map +0 -1
- package/esm/index.js +0 -17
- package/esm/index.js.map +0 -1
- package/esm/jubjub.d.ts +0 -12
- package/esm/jubjub.d.ts.map +0 -1
- package/esm/jubjub.js +0 -12
- package/esm/jubjub.js.map +0 -1
- package/esm/misc.d.ts +0 -19
- package/esm/misc.d.ts.map +0 -1
- package/esm/misc.js +0 -109
- package/esm/misc.js.map +0 -1
- package/esm/nist.d.ts +0 -21
- package/esm/nist.d.ts.map +0 -1
- package/esm/nist.js +0 -132
- package/esm/nist.js.map +0 -1
- package/esm/p256.d.ts +0 -16
- package/esm/p256.d.ts.map +0 -1
- package/esm/p256.js +0 -16
- package/esm/p256.js.map +0 -1
- package/esm/p384.d.ts +0 -16
- package/esm/p384.d.ts.map +0 -1
- package/esm/p384.js +0 -16
- package/esm/p384.js.map +0 -1
- package/esm/p521.d.ts +0 -16
- package/esm/p521.d.ts.map +0 -1
- package/esm/p521.js +0 -16
- package/esm/p521.js.map +0 -1
- package/esm/package.json +0 -4
- package/esm/pasta.d.ts +0 -10
- package/esm/pasta.d.ts.map +0 -1
- package/esm/pasta.js +0 -10
- package/esm/pasta.js.map +0 -1
- package/esm/secp256k1.d.ts +0 -89
- package/esm/secp256k1.d.ts.map +0 -1
- package/esm/secp256k1.js +0 -292
- package/esm/secp256k1.js.map +0 -1
- package/esm/utils.d.ts +0 -110
- package/esm/utils.d.ts.map +0 -1
- package/esm/utils.js +0 -322
- package/esm/utils.js.map +0 -1
- package/src/_shortw_utils.ts +0 -21
- package/src/abstract/utils.ts +0 -7
- package/src/jubjub.ts +0 -12
- package/src/p256.ts +0 -15
- package/src/p384.ts +0 -15
- package/src/p521.ts +0 -15
- package/src/package.json +0 -3
- package/src/pasta.ts +0 -9
package/src/ed448.ts
CHANGED
|
@@ -8,19 +8,13 @@
|
|
|
8
8
|
*/
|
|
9
9
|
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
10
10
|
import { shake256 } from '@noble/hashes/sha3.js';
|
|
11
|
-
import {
|
|
12
|
-
abytes,
|
|
13
|
-
concatBytes,
|
|
14
|
-
utf8ToBytes,
|
|
15
|
-
createHasher as wrapConstructor,
|
|
16
|
-
} from '@noble/hashes/utils.js';
|
|
11
|
+
import { concatBytes, hexToBytes, createHasher as wrapConstructor } from '@noble/hashes/utils.js';
|
|
17
12
|
import type { AffinePoint } from './abstract/curve.ts';
|
|
18
|
-
import { pippenger } from './abstract/curve.ts';
|
|
19
13
|
import {
|
|
14
|
+
eddsa,
|
|
20
15
|
edwards,
|
|
21
16
|
PrimeEdwardsPoint,
|
|
22
|
-
|
|
23
|
-
type CurveFn,
|
|
17
|
+
type EdDSA,
|
|
24
18
|
type EdwardsOpts,
|
|
25
19
|
type EdwardsPoint,
|
|
26
20
|
type EdwardsPointCons,
|
|
@@ -31,12 +25,12 @@ import {
|
|
|
31
25
|
expand_message_xof,
|
|
32
26
|
type H2CHasher,
|
|
33
27
|
type H2CHasherBase,
|
|
34
|
-
type H2CMethod,
|
|
35
28
|
type htfBasicOpts,
|
|
36
29
|
} from './abstract/hash-to-curve.ts';
|
|
37
30
|
import { Field, FpInvertBatch, isNegativeLE, mod, pow2, type IField } from './abstract/modular.ts';
|
|
38
|
-
import { montgomery, type MontgomeryECDH
|
|
39
|
-
import {
|
|
31
|
+
import { montgomery, type MontgomeryECDH } from './abstract/montgomery.ts';
|
|
32
|
+
import { createORPF, type OPRF } from './abstract/oprf.ts';
|
|
33
|
+
import { abytes, asciiToBytes, bytesToNumberLE, equalBytes } from './utils.ts';
|
|
40
34
|
|
|
41
35
|
// edwards448 curve
|
|
42
36
|
// a = 1n
|
|
@@ -140,33 +134,27 @@ function uvRatio(u: bigint, v: bigint): { isValid: boolean; value: bigint } {
|
|
|
140
134
|
}
|
|
141
135
|
|
|
142
136
|
// Finite field 2n**448n - 2n**224n - 1n
|
|
137
|
+
// The value fits in 448 bits, but we use 456-bit (57-byte) elements because of bitflags.
|
|
138
|
+
// - ed25519 fits in 255 bits, allowing using last 1 byte for specifying bit flag of point negation.
|
|
139
|
+
// - ed448 fits in 448 bits. We can't use last 1 byte: we can only use a bit 224 in the middle.
|
|
143
140
|
const Fp = /* @__PURE__ */ (() => Field(ed448_CURVE.p, { BITS: 456, isLE: true }))();
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
141
|
+
const Fn = /* @__PURE__ */ (() => Field(ed448_CURVE.n, { BITS: 456, isLE: true }))();
|
|
142
|
+
// decaf448 uses 448-bit (56-byte) keys
|
|
143
|
+
const Fp448 = /* @__PURE__ */ (() => Field(ed448_CURVE.p, { BITS: 448, isLE: true }))();
|
|
144
|
+
const Fn448 = /* @__PURE__ */ (() => Field(ed448_CURVE.n, { BITS: 448, isLE: true }))();
|
|
147
145
|
|
|
148
146
|
// SHAKE256(dom4(phflag,context)||x, 114)
|
|
149
147
|
function dom4(data: Uint8Array, ctx: Uint8Array, phflag: boolean) {
|
|
150
148
|
if (ctx.length > 255) throw new Error('context must be smaller than 255, got: ' + ctx.length);
|
|
151
149
|
return concatBytes(
|
|
152
|
-
|
|
150
|
+
asciiToBytes('SigEd448'),
|
|
153
151
|
new Uint8Array([phflag ? 1 : 0, ctx.length]),
|
|
154
152
|
ctx,
|
|
155
153
|
data
|
|
156
154
|
);
|
|
157
155
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
const ED448_DEF = /* @__PURE__ */ (() => ({
|
|
162
|
-
...ed448_CURVE,
|
|
163
|
-
Fp,
|
|
164
|
-
Fn,
|
|
165
|
-
hash: shake256_114,
|
|
166
|
-
adjustScalarBytes,
|
|
167
|
-
domain: dom4,
|
|
168
|
-
uvRatio,
|
|
169
|
-
}))();
|
|
156
|
+
const ed448_eddsa_opts = { adjustScalarBytes, domain: dom4 };
|
|
157
|
+
const ed448_Point = edwards(ed448_CURVE, { Fp, Fn, uvRatio });
|
|
170
158
|
|
|
171
159
|
/**
|
|
172
160
|
* ed448 EdDSA curve and methods.
|
|
@@ -177,15 +165,14 @@ const ED448_DEF = /* @__PURE__ */ (() => ({
|
|
|
177
165
|
* const sig = ed448.sign(msg, secretKey);
|
|
178
166
|
* const isValid = ed448.verify(sig, msg, publicKey);
|
|
179
167
|
*/
|
|
180
|
-
export const ed448:
|
|
168
|
+
export const ed448: EdDSA = eddsa(ed448_Point, shake256_114, ed448_eddsa_opts);
|
|
181
169
|
|
|
182
170
|
// There is no ed448ctx, since ed448 supports ctx by default
|
|
183
171
|
/** Prehashed version of ed448. Accepts already-hashed messages in sign() and verify(). */
|
|
184
|
-
export const ed448ph:
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}))();
|
|
172
|
+
export const ed448ph: EdDSA = /* @__PURE__ */ eddsa(ed448_Point, shake256_114, {
|
|
173
|
+
...ed448_eddsa_opts,
|
|
174
|
+
prehash: shake256_64,
|
|
175
|
+
});
|
|
189
176
|
|
|
190
177
|
/**
|
|
191
178
|
* E448 curve, defined by NIST.
|
|
@@ -199,7 +186,7 @@ export const E448: EdwardsPointCons = edwards(E448_CURVE);
|
|
|
199
186
|
* x448 has 56-byte keys as per RFC 7748, while
|
|
200
187
|
* ed448 has 57-byte keys as per RFC 8032.
|
|
201
188
|
*/
|
|
202
|
-
export const x448:
|
|
189
|
+
export const x448: MontgomeryECDH = /* @__PURE__ */ (() => {
|
|
203
190
|
const P = ed448_CURVE.p;
|
|
204
191
|
return montgomery({
|
|
205
192
|
P,
|
|
@@ -291,8 +278,8 @@ function map_to_curve_elligator2_edwards448(u: bigint) {
|
|
|
291
278
|
}
|
|
292
279
|
|
|
293
280
|
/** Hashing / encoding to ed448 points / field. RFC 9380 methods. */
|
|
294
|
-
export const ed448_hasher: H2CHasher<
|
|
295
|
-
createHasher(
|
|
281
|
+
export const ed448_hasher: H2CHasher<EdwardsPointCons> = /* @__PURE__ */ (() =>
|
|
282
|
+
createHasher(ed448_Point, (scalars: bigint[]) => map_to_curve_elligator2_edwards448(scalars[0]), {
|
|
296
283
|
DST: 'edwards448_XOF:SHAKE256_ELL2_RO_',
|
|
297
284
|
encodeDST: 'edwards448_XOF:SHAKE256_ELL2_NU_',
|
|
298
285
|
p: Fp.ORDER,
|
|
@@ -317,19 +304,12 @@ const INVSQRT_MINUS_D = /* @__PURE__ */ BigInt(
|
|
|
317
304
|
// Calculates 1/√(number)
|
|
318
305
|
const invertSqrt = (number: bigint) => uvRatio(_1n, number);
|
|
319
306
|
|
|
320
|
-
const MAX_448B = /* @__PURE__ */ BigInt(
|
|
321
|
-
'0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
|
|
322
|
-
);
|
|
323
|
-
const bytes448ToNumberLE = (bytes: Uint8Array) => Fp.create(bytesToNumberLE(bytes) & MAX_448B);
|
|
324
|
-
|
|
325
|
-
type ExtendedPoint = EdwardsPoint;
|
|
326
|
-
|
|
327
307
|
/**
|
|
328
308
|
* Elligator map for hash-to-curve of decaf448.
|
|
329
309
|
* Described in [RFC9380](https://www.rfc-editor.org/rfc/rfc9380#appendix-C)
|
|
330
310
|
* and [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-element-derivation-2).
|
|
331
311
|
*/
|
|
332
|
-
function calcElligatorDecafMap(r0: bigint):
|
|
312
|
+
function calcElligatorDecafMap(r0: bigint): EdwardsPoint {
|
|
333
313
|
const { d } = ed448_CURVE;
|
|
334
314
|
const P = Fp.ORDER;
|
|
335
315
|
const mod = (n: bigint) => Fp.create(n);
|
|
@@ -355,22 +335,26 @@ function calcElligatorDecafMap(r0: bigint): ExtendedPoint {
|
|
|
355
335
|
const W1 = mod(s2 + _1n); // 9
|
|
356
336
|
const W2 = mod(s2 - _1n); // 10
|
|
357
337
|
const W3 = mod(v_prime * s * (r - _1n) * ONE_MINUS_TWO_D + sgn); // 11
|
|
358
|
-
return new
|
|
338
|
+
return new ed448_Point(mod(W0 * W3), mod(W2 * W1), mod(W1 * W3), mod(W0 * W2));
|
|
359
339
|
}
|
|
360
340
|
|
|
361
341
|
function decaf448_map(bytes: Uint8Array): _DecafPoint {
|
|
362
342
|
abytes(bytes, 112);
|
|
363
|
-
const
|
|
343
|
+
const skipValidation = true;
|
|
344
|
+
// Note: Similar to the field element decoding described in
|
|
345
|
+
// [RFC7748], and unlike the field element decoding described in
|
|
346
|
+
// Section 5.3.1, non-canonical values are accepted.
|
|
347
|
+
const r1 = Fp448.create(Fp448.fromBytes(bytes.subarray(0, 56), skipValidation));
|
|
364
348
|
const R1 = calcElligatorDecafMap(r1);
|
|
365
|
-
const r2 =
|
|
349
|
+
const r2 = Fp448.create(Fp448.fromBytes(bytes.subarray(56, 112), skipValidation));
|
|
366
350
|
const R2 = calcElligatorDecafMap(r2);
|
|
367
351
|
return new _DecafPoint(R1.add(R2));
|
|
368
352
|
}
|
|
369
353
|
|
|
370
354
|
/**
|
|
371
|
-
* Each ed448/
|
|
355
|
+
* Each ed448/EdwardsPoint has 4 different equivalent points. This can be
|
|
372
356
|
* a source of bugs for protocols like ring signatures. Decaf was created to solve this.
|
|
373
|
-
* Decaf point operates in X:Y:Z:T extended coordinates like
|
|
357
|
+
* Decaf point operates in X:Y:Z:T extended coordinates like EdwardsPoint,
|
|
374
358
|
* but it should work in its own namespace: do not combine those two.
|
|
375
359
|
* See [RFC9496](https://www.rfc-editor.org/rfc/rfc9496).
|
|
376
360
|
*/
|
|
@@ -378,23 +362,23 @@ class _DecafPoint extends PrimeEdwardsPoint<_DecafPoint> {
|
|
|
378
362
|
// The following gymnastics is done because typescript strips comments otherwise
|
|
379
363
|
// prettier-ignore
|
|
380
364
|
static BASE: _DecafPoint =
|
|
381
|
-
/* @__PURE__ */ (() => new _DecafPoint(
|
|
365
|
+
/* @__PURE__ */ (() => new _DecafPoint(ed448_Point.BASE).multiplyUnsafe(_2n))();
|
|
382
366
|
// prettier-ignore
|
|
383
367
|
static ZERO: _DecafPoint =
|
|
384
|
-
/* @__PURE__ */ (() => new _DecafPoint(
|
|
368
|
+
/* @__PURE__ */ (() => new _DecafPoint(ed448_Point.ZERO))();
|
|
385
369
|
// prettier-ignore
|
|
386
370
|
static Fp: IField<bigint> =
|
|
387
|
-
/* @__PURE__ */ (() =>
|
|
371
|
+
/* @__PURE__ */ (() => Fp448)();
|
|
388
372
|
// prettier-ignore
|
|
389
373
|
static Fn: IField<bigint> =
|
|
390
|
-
/* @__PURE__ */ (() =>
|
|
374
|
+
/* @__PURE__ */ (() => Fn448)();
|
|
391
375
|
|
|
392
|
-
constructor(ep:
|
|
376
|
+
constructor(ep: EdwardsPoint) {
|
|
393
377
|
super(ep);
|
|
394
378
|
}
|
|
395
379
|
|
|
396
380
|
static fromAffine(ap: AffinePoint<bigint>): _DecafPoint {
|
|
397
|
-
return new _DecafPoint(
|
|
381
|
+
return new _DecafPoint(ed448_Point.fromAffine(ap));
|
|
398
382
|
}
|
|
399
383
|
|
|
400
384
|
protected assertSame(other: _DecafPoint): void {
|
|
@@ -405,21 +389,16 @@ class _DecafPoint extends PrimeEdwardsPoint<_DecafPoint> {
|
|
|
405
389
|
return new _DecafPoint(ep);
|
|
406
390
|
}
|
|
407
391
|
|
|
408
|
-
/** @deprecated use `import { decaf448_hasher } from '@noble/curves/ed448.js';` */
|
|
409
|
-
static hashToCurve(hex: Hex): _DecafPoint {
|
|
410
|
-
return decaf448_map(ensureBytes('decafHash', hex, 112));
|
|
411
|
-
}
|
|
412
|
-
|
|
413
392
|
static fromBytes(bytes: Uint8Array): _DecafPoint {
|
|
414
393
|
abytes(bytes, 56);
|
|
415
394
|
const { d } = ed448_CURVE;
|
|
416
395
|
const P = Fp.ORDER;
|
|
417
|
-
const mod = (n: bigint) =>
|
|
418
|
-
const s =
|
|
396
|
+
const mod = (n: bigint) => Fp448.create(n);
|
|
397
|
+
const s = Fp448.fromBytes(bytes);
|
|
419
398
|
|
|
420
399
|
// 1. Check that s_bytes is the canonical encoding of a field element, or else abort.
|
|
421
400
|
// 2. Check that s is non-negative, or else abort
|
|
422
|
-
if (!equalBytes(
|
|
401
|
+
if (!equalBytes(Fn448.toBytes(s), bytes) || isNegativeLE(s, P))
|
|
423
402
|
throw new Error('invalid decaf448 encoding 1');
|
|
424
403
|
|
|
425
404
|
const s2 = mod(s * s); // 1
|
|
@@ -437,7 +416,7 @@ class _DecafPoint extends PrimeEdwardsPoint<_DecafPoint> {
|
|
|
437
416
|
const t = mod(x * y); // 8
|
|
438
417
|
|
|
439
418
|
if (!isValid) throw new Error('invalid decaf448 encoding 2');
|
|
440
|
-
return new _DecafPoint(new
|
|
419
|
+
return new _DecafPoint(new ed448_Point(x, y, _1n, t));
|
|
441
420
|
}
|
|
442
421
|
|
|
443
422
|
/**
|
|
@@ -445,13 +424,8 @@ class _DecafPoint extends PrimeEdwardsPoint<_DecafPoint> {
|
|
|
445
424
|
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-decode-2).
|
|
446
425
|
* @param hex Decaf-encoded 56 bytes. Not every 56-byte string is valid decaf encoding
|
|
447
426
|
*/
|
|
448
|
-
static fromHex(hex:
|
|
449
|
-
return _DecafPoint.fromBytes(
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
/** @deprecated use `import { pippenger } from '@noble/curves/abstract/curve.js';` */
|
|
453
|
-
static msm(points: _DecafPoint[], scalars: bigint[]): _DecafPoint {
|
|
454
|
-
return pippenger(_DecafPoint, Fn, points, scalars);
|
|
427
|
+
static fromHex(hex: string): _DecafPoint {
|
|
428
|
+
return _DecafPoint.fromBytes(hexToBytes(hex));
|
|
455
429
|
}
|
|
456
430
|
|
|
457
431
|
/**
|
|
@@ -462,20 +436,15 @@ class _DecafPoint extends PrimeEdwardsPoint<_DecafPoint> {
|
|
|
462
436
|
const { X, Z, T } = this.ep;
|
|
463
437
|
const P = Fp.ORDER;
|
|
464
438
|
const mod = (n: bigint) => Fp.create(n);
|
|
465
|
-
|
|
466
439
|
const u1 = mod(mod(X + T) * mod(X - T)); // 1
|
|
467
440
|
const x2 = mod(X * X);
|
|
468
441
|
const { value: invsqrt } = invertSqrt(mod(u1 * ONE_MINUS_D * x2)); // 2
|
|
469
|
-
|
|
470
442
|
let ratio = mod(invsqrt * u1 * SQRT_MINUS_D); // 3
|
|
471
443
|
if (isNegativeLE(ratio, P)) ratio = mod(-ratio);
|
|
472
|
-
|
|
473
444
|
const u2 = mod(INVSQRT_MINUS_D * ratio * Z - T); // 4
|
|
474
|
-
|
|
475
445
|
let s = mod(ONE_MINUS_D * invsqrt * X * u2); // 5
|
|
476
446
|
if (isNegativeLE(s, P)) s = mod(-s);
|
|
477
|
-
|
|
478
|
-
return numberToBytesLE(s, 56);
|
|
447
|
+
return Fn448.toBytes(s);
|
|
479
448
|
}
|
|
480
449
|
|
|
481
450
|
/**
|
|
@@ -486,9 +455,8 @@ class _DecafPoint extends PrimeEdwardsPoint<_DecafPoint> {
|
|
|
486
455
|
this.assertSame(other);
|
|
487
456
|
const { X: X1, Y: Y1 } = this.ep;
|
|
488
457
|
const { X: X2, Y: Y2 } = other.ep;
|
|
489
|
-
const mod = (n: bigint) => Fp.create(n);
|
|
490
458
|
// (x1 * y2 == y1 * x2)
|
|
491
|
-
return
|
|
459
|
+
return Fp.create(X1 * Y2) === Fp.create(Y1 * X2);
|
|
492
460
|
}
|
|
493
461
|
|
|
494
462
|
is0(): boolean {
|
|
@@ -501,23 +469,29 @@ export const decaf448: {
|
|
|
501
469
|
} = { Point: _DecafPoint };
|
|
502
470
|
|
|
503
471
|
/** Hashing to decaf448 points / field. RFC 9380 methods. */
|
|
504
|
-
export const decaf448_hasher: H2CHasherBase<
|
|
472
|
+
export const decaf448_hasher: H2CHasherBase<_DecafPoint> = {
|
|
505
473
|
hashToCurve(msg: Uint8Array, options?: htfBasicOpts): _DecafPoint {
|
|
506
474
|
const DST = options?.DST || 'decaf448_XOF:SHAKE256_D448MAP_RO_';
|
|
507
475
|
return decaf448_map(expand_message_xof(msg, DST, 112, 224, shake256));
|
|
508
476
|
},
|
|
477
|
+
// Warning: has big modulo bias of 2^-64.
|
|
478
|
+
// RFC is invalid. RFC says "use 64-byte xof", while for 2^-112 bias
|
|
479
|
+
// it must use 84-byte xof (56+56/2), not 64.
|
|
509
480
|
hashToScalar(msg: Uint8Array, options: htfBasicOpts = { DST: _DST_scalar }) {
|
|
510
|
-
|
|
481
|
+
// Can't use `Fn448.fromBytes()`. 64-byte input => 56-byte field element
|
|
482
|
+
const xof = expand_message_xof(msg, options.DST, 64, 256, shake256);
|
|
483
|
+
return Fn448.create(bytesToNumberLE(xof));
|
|
511
484
|
},
|
|
512
485
|
};
|
|
513
486
|
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
487
|
+
export const decaf448_oprf: OPRF = /* @__PURE__ */ (() =>
|
|
488
|
+
createORPF({
|
|
489
|
+
name: 'decaf448-SHAKE256',
|
|
490
|
+
Point: _DecafPoint,
|
|
491
|
+
hash: (msg: Uint8Array) => shake256(msg, { dkLen: 64 }),
|
|
492
|
+
hashToGroup: decaf448_hasher.hashToCurve,
|
|
493
|
+
hashToScalar: decaf448_hasher.hashToScalar,
|
|
494
|
+
}))();
|
|
521
495
|
|
|
522
496
|
/**
|
|
523
497
|
* Weird / bogus points, useful for debugging.
|
|
@@ -531,27 +505,3 @@ export const ED448_TORSION_SUBGROUP: string[] = [
|
|
|
531
505
|
'000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
|
|
532
506
|
'000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080',
|
|
533
507
|
];
|
|
534
|
-
|
|
535
|
-
type DcfHasher = (msg: Uint8Array, options: htfBasicOpts) => _DecafPoint;
|
|
536
|
-
|
|
537
|
-
/** @deprecated use `decaf448.Point` */
|
|
538
|
-
export const DecafPoint: typeof _DecafPoint = _DecafPoint;
|
|
539
|
-
/** @deprecated use `import { ed448_hasher } from '@noble/curves/ed448.js';` */
|
|
540
|
-
export const hashToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => ed448_hasher.hashToCurve)();
|
|
541
|
-
/** @deprecated use `import { ed448_hasher } from '@noble/curves/ed448.js';` */
|
|
542
|
-
export const encodeToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() =>
|
|
543
|
-
ed448_hasher.encodeToCurve)();
|
|
544
|
-
/** @deprecated use `import { decaf448_hasher } from '@noble/curves/ed448.js';` */
|
|
545
|
-
export const hashToDecaf448: DcfHasher = /* @__PURE__ */ (() =>
|
|
546
|
-
decaf448_hasher.hashToCurve as DcfHasher)();
|
|
547
|
-
/** @deprecated use `import { decaf448_hasher } from '@noble/curves/ed448.js';` */
|
|
548
|
-
export const hash_to_decaf448: DcfHasher = /* @__PURE__ */ (() =>
|
|
549
|
-
decaf448_hasher.hashToCurve as DcfHasher)();
|
|
550
|
-
|
|
551
|
-
/** @deprecated use `ed448.utils.toMontgomery` */
|
|
552
|
-
export function edwardsToMontgomeryPub(edwardsPub: string | Uint8Array): Uint8Array {
|
|
553
|
-
return ed448.utils.toMontgomery(ensureBytes('pub', edwardsPub));
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
/** @deprecated use `ed448.utils.toMontgomery` */
|
|
557
|
-
export const edwardsToMontgomery: typeof edwardsToMontgomeryPub = edwardsToMontgomeryPub;
|
package/src/index.ts
CHANGED
|
@@ -9,7 +9,7 @@ import { ed448, ed448ph, ed448ctx, x448 } from '@noble/curves/ed448.js';
|
|
|
9
9
|
import { p256, p384, p521 } from '@noble/curves/nist.js';
|
|
10
10
|
import { bls12_381 } from '@noble/curves/bls12-381.js';
|
|
11
11
|
import { bn254 } from '@noble/curves/bn254.js';
|
|
12
|
-
import { bytesToHex, hexToBytes, concatBytes, utf8ToBytes } from '@noble/curves/
|
|
12
|
+
import { bytesToHex, hexToBytes, concatBytes, utf8ToBytes } from '@noble/curves/utils.js';
|
|
13
13
|
```
|
|
14
14
|
*/
|
|
15
15
|
throw new Error('root module cannot be imported: import submodules instead. Check out README');
|
package/src/misc.ts
CHANGED
|
@@ -6,18 +6,19 @@
|
|
|
6
6
|
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
7
7
|
import { blake256 } from '@noble/hashes/blake1.js';
|
|
8
8
|
import { blake2s } from '@noble/hashes/blake2.js';
|
|
9
|
-
import { sha256, sha512 } from '@noble/hashes/sha2.js';
|
|
10
|
-
import { concatBytes
|
|
9
|
+
import { sha256, sha384, sha512 } from '@noble/hashes/sha2.js';
|
|
10
|
+
import { concatBytes } from '@noble/hashes/utils.js';
|
|
11
11
|
import {
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
eddsa,
|
|
13
|
+
edwards,
|
|
14
|
+
type EdDSA,
|
|
14
15
|
type EdwardsOpts,
|
|
15
16
|
type EdwardsPoint,
|
|
16
17
|
} from './abstract/edwards.ts';
|
|
17
|
-
import {
|
|
18
|
-
import { weierstrass, type CurveFn as WCurveFn } from './abstract/weierstrass.ts';
|
|
18
|
+
import { ecdsa, weierstrass, type ECDSA, type WeierstrassOpts } from './abstract/weierstrass.ts';
|
|
19
19
|
import { bls12_381_Fr } from './bls12-381.ts';
|
|
20
20
|
import { bn254_Fr } from './bn254.ts';
|
|
21
|
+
import { asciiToBytes } from './utils.ts';
|
|
21
22
|
|
|
22
23
|
// Jubjub curves have 𝔽p over scalar fields of other curves. They are friendly to ZK proofs.
|
|
23
24
|
// jubjub Fp = bls n. babyjubjub Fp = bn254 n.
|
|
@@ -32,11 +33,7 @@ const jubjub_CURVE: EdwardsOpts = {
|
|
|
32
33
|
Gy: BigInt('0x1d523cf1ddab1a1793132e78c866c0c33e26ba5cc220fed7cc3f870e59d292aa'),
|
|
33
34
|
};
|
|
34
35
|
/** Curve over scalar field of bls12-381. jubjub Fp = bls n */
|
|
35
|
-
export const jubjub:
|
|
36
|
-
...jubjub_CURVE,
|
|
37
|
-
Fp: bls12_381_Fr,
|
|
38
|
-
hash: sha512,
|
|
39
|
-
});
|
|
36
|
+
export const jubjub: EdDSA = /* @__PURE__ */ eddsa(edwards(jubjub_CURVE), sha512);
|
|
40
37
|
|
|
41
38
|
const babyjubjub_CURVE: EdwardsOpts = {
|
|
42
39
|
p: bn254_Fr.ORDER,
|
|
@@ -48,13 +45,9 @@ const babyjubjub_CURVE: EdwardsOpts = {
|
|
|
48
45
|
Gy: BigInt('0xc19139cb84c680a6e14116da06056174a0cfa121e6e5c2450f87d64fc000001'),
|
|
49
46
|
};
|
|
50
47
|
/** Curve over scalar field of bn254. babyjubjub Fp = bn254 n */
|
|
51
|
-
export const babyjubjub:
|
|
52
|
-
...babyjubjub_CURVE,
|
|
53
|
-
Fp: bn254_Fr,
|
|
54
|
-
hash: blake256,
|
|
55
|
-
});
|
|
48
|
+
export const babyjubjub: EdDSA = /* @__PURE__ */ eddsa(edwards(babyjubjub_CURVE), blake256);
|
|
56
49
|
|
|
57
|
-
const jubjub_gh_first_block =
|
|
50
|
+
const jubjub_gh_first_block = asciiToBytes(
|
|
58
51
|
'096b36a5804bfacef1691e173c366a47ff5ba84a44f26ddd7e8d9f79d5b42df0'
|
|
59
52
|
);
|
|
60
53
|
|
|
@@ -87,38 +80,62 @@ export function jubjub_findGroupHash(m: Uint8Array, personalization: Uint8Array)
|
|
|
87
80
|
return hashes[0];
|
|
88
81
|
}
|
|
89
82
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
'
|
|
94
|
-
)
|
|
95
|
-
|
|
96
|
-
'
|
|
97
|
-
)
|
|
83
|
+
const brainpoolP256r1_CURVE: WeierstrassOpts<bigint> = {
|
|
84
|
+
p: BigInt('0xa9fb57dba1eea9bc3e660a909d838d726e3bf623d52620282013481d1f6e5377'),
|
|
85
|
+
a: BigInt('0x7d5a0975fc2c3057eef67530417affe7fb8055c126dc5c6ce94a4b44f330b5d9'),
|
|
86
|
+
b: BigInt('0x26dc5c6ce94a4b44f330b5d9bbd77cbf958416295cf7e1ce6bccdc18ff8c07b6'),
|
|
87
|
+
n: BigInt('0xa9fb57dba1eea9bc3e660a909d838d718c397aa3b561a6f7901e0e82974856a7'),
|
|
88
|
+
Gx: BigInt('0x8bd2aeb9cb7e57cb2c4b482ffc81b7afb9de27e1e3bd23c23a4453bd9ace3262'),
|
|
89
|
+
Gy: BigInt('0x547ef835c3dac4fd97f8461a14611dc9c27745132ded8e545c1d54c72f046997'),
|
|
90
|
+
h: BigInt(1),
|
|
91
|
+
};
|
|
92
|
+
/** Brainpool P256r1 with sha256, from RFC 5639. */
|
|
93
|
+
export const brainpoolP256r1: ECDSA = ecdsa(weierstrass(brainpoolP256r1_CURVE), sha256);
|
|
98
94
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
a: BigInt(
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
95
|
+
const brainpoolP384r1_CURVE: WeierstrassOpts<bigint> = {
|
|
96
|
+
p: BigInt(
|
|
97
|
+
'0x8cb91e82a3386d280f5d6f7e50e641df152f7109ed5456b412b1da197fb71123acd3a729901d1a71874700133107ec53'
|
|
98
|
+
),
|
|
99
|
+
a: BigInt(
|
|
100
|
+
'0x7bc382c63d8c150c3c72080ace05afa0c2bea28e4fb22787139165efba91f90f8aa5814a503ad4eb04a8c7dd22ce2826'
|
|
101
|
+
),
|
|
102
|
+
b: BigInt(
|
|
103
|
+
'0x04a8c7dd22ce28268b39b55416f0447c2fb77de107dcd2a62e880ea53eeb62d57cb4390295dbc9943ab78696fa504c11'
|
|
104
|
+
),
|
|
105
|
+
n: BigInt(
|
|
106
|
+
'0x8cb91e82a3386d280f5d6f7e50e641df152f7109ed5456b31f166e6cac0425a7cf3ab6af6b7fc3103b883202e9046565'
|
|
107
|
+
),
|
|
108
|
+
Gx: BigInt(
|
|
109
|
+
'0x1d1c64f068cf45ffa2a63a81b7c13f6b8847a3e77ef14fe3db7fcafe0cbd10e8e826e03436d646aaef87b2e247d4af1e'
|
|
110
|
+
),
|
|
111
|
+
Gy: BigInt(
|
|
112
|
+
'0x8abe1d7520f9c2a45cb1eb8e95cfd55262b70b29feec5864e19c054ff99129280e4646217791811142820341263c5315'
|
|
113
|
+
),
|
|
109
114
|
h: BigInt(1),
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
115
|
+
};
|
|
116
|
+
/** Brainpool P384r1 with sha384, from RFC 5639. */
|
|
117
|
+
export const brainpoolP384r1: ECDSA = ecdsa(weierstrass(brainpoolP384r1_CURVE), sha384);
|
|
118
|
+
|
|
119
|
+
const brainpoolP512r1_CURVE: WeierstrassOpts<bigint> = {
|
|
120
|
+
p: BigInt(
|
|
121
|
+
'0xaadd9db8dbe9c48b3fd4e6ae33c9fc07cb308db3b3c9d20ed6639cca703308717d4d9b009bc66842aecda12ae6a380e62881ff2f2d82c68528aa6056583a48f3'
|
|
122
|
+
),
|
|
123
|
+
a: BigInt(
|
|
124
|
+
'0x7830a3318b603b89e2327145ac234cc594cbdd8d3df91610a83441caea9863bc2ded5d5aa8253aa10a2ef1c98b9ac8b57f1117a72bf2c7b9e7c1ac4d77fc94ca'
|
|
125
|
+
),
|
|
126
|
+
b: BigInt(
|
|
127
|
+
'0x3df91610a83441caea9863bc2ded5d5aa8253aa10a2ef1c98b9ac8b57f1117a72bf2c7b9e7c1ac4d77fc94cadc083e67984050b75ebae5dd2809bd638016f723'
|
|
128
|
+
),
|
|
129
|
+
n: BigInt(
|
|
130
|
+
'0xaadd9db8dbe9c48b3fd4e6ae33c9fc07cb308db3b3c9d20ed6639cca70330870553e5c414ca92619418661197fac10471db1d381085ddaddb58796829ca90069'
|
|
131
|
+
),
|
|
132
|
+
Gx: BigInt(
|
|
133
|
+
'0x81aee4bdd82ed9645a21322e9c4c6a9385ed9f70b5d916c1b43b62eef4d0098eff3b1f78e2d0d48d50d1687b93b97d5f7c6d5047406a5e688b352209bcb9f822'
|
|
134
|
+
),
|
|
135
|
+
Gy: BigInt(
|
|
136
|
+
'0x7dde385d566332ecc0eabfa9cf7822fdf209f70024a57b1aa000c55b881f8111b2dcde494a5f485e5bca4bd88a2763aed1ca2b2fa8f0540678cd1e0f3ad80892'
|
|
137
|
+
),
|
|
122
138
|
h: BigInt(1),
|
|
123
|
-
|
|
124
|
-
|
|
139
|
+
};
|
|
140
|
+
/** Brainpool P521r1 with sha512, from RFC 5639. */
|
|
141
|
+
export const brainpoolP512r1: ECDSA = ecdsa(weierstrass(brainpoolP512r1_CURVE), sha512);
|