@noble/curves 1.9.7 → 2.0.0-beta.2
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 +520 -505
- package/abstract/bls.d.ts +58 -120
- 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 +18 -54
- package/abstract/curve.d.ts.map +1 -1
- package/abstract/curve.js +30 -49
- package/abstract/curve.js.map +1 -1
- package/abstract/edwards.d.ts +18 -77
- package/abstract/edwards.d.ts.map +1 -1
- package/abstract/edwards.js +68 -144
- 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 +35 -47
- package/abstract/hash-to-curve.d.ts.map +1 -1
- package/abstract/hash-to-curve.js +42 -46
- 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 +170 -169
- package/abstract/modular.js.map +1 -1
- package/abstract/montgomery.d.ts +7 -12
- package/abstract/montgomery.d.ts.map +1 -1
- package/abstract/montgomery.js +22 -29
- 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.d.ts.map +1 -1
- package/abstract/poseidon.js +26 -31
- package/abstract/poseidon.js.map +1 -1
- package/abstract/tower.d.ts.map +1 -1
- package/abstract/tower.js +43 -19
- package/abstract/tower.js.map +1 -1
- package/abstract/weierstrass.d.ts +77 -168
- package/abstract/weierstrass.d.ts.map +1 -1
- package/abstract/weierstrass.js +184 -389
- package/abstract/weierstrass.js.map +1 -1
- package/bls12-381.d.ts +5 -11
- package/bls12-381.d.ts.map +1 -1
- package/bls12-381.js +161 -181
- package/bls12-381.js.map +1 -1
- package/bn254.d.ts +59 -11
- package/bn254.d.ts.map +1 -1
- package/bn254.js +69 -97
- package/bn254.js.map +1 -1
- package/ed25519.d.ts +33 -48
- package/ed25519.d.ts.map +1 -1
- package/ed25519.js +147 -161
- package/ed25519.js.map +1 -1
- package/ed448.d.ts +27 -36
- package/ed448.d.ts.map +1 -1
- package/ed448.js +143 -164
- package/ed448.js.map +1 -1
- package/index.d.ts +1 -0
- package/index.js +20 -4
- package/index.js.map +1 -1
- package/misc.d.ts +10 -14
- package/misc.d.ts.map +1 -1
- package/misc.js +53 -62
- package/misc.js.map +1 -1
- package/nist.d.ts +31 -16
- package/nist.d.ts.map +1 -1
- package/nist.js +75 -64
- package/nist.js.map +1 -1
- package/package.json +20 -234
- package/secp256k1.d.ts +17 -30
- package/secp256k1.d.ts.map +1 -1
- package/secp256k1.js +59 -73
- package/secp256k1.js.map +1 -1
- package/src/abstract/bls.ts +207 -354
- package/src/abstract/curve.ts +25 -84
- package/src/abstract/edwards.ts +68 -193
- package/src/abstract/hash-to-curve.ts +71 -85
- package/src/abstract/modular.ts +150 -134
- package/src/abstract/montgomery.ts +28 -35
- package/src/abstract/oprf.ts +600 -0
- package/src/abstract/poseidon.ts +6 -8
- package/src/abstract/tower.ts +0 -3
- package/src/abstract/weierstrass.ts +203 -525
- package/src/bls12-381.ts +133 -139
- package/src/bn254.ts +69 -93
- package/src/ed25519.ts +106 -133
- package/src/ed448.ts +111 -138
- package/src/index.ts +19 -3
- package/src/misc.ts +68 -51
- package/src/nist.ts +77 -70
- package/src/secp256k1.ts +46 -81
- package/src/utils.ts +67 -137
- package/src/webcrypto.ts +403 -0
- package/utils.d.ts +31 -38
- package/utils.d.ts.map +1 -1
- package/utils.js +66 -185
- package/utils.js.map +1 -1
- package/webcrypto.d.ts +99 -0
- package/webcrypto.d.ts.map +1 -0
- package/webcrypto.js +256 -0
- package/webcrypto.js.map +1 -0
- package/_shortw_utils.d.ts +0 -19
- package/_shortw_utils.d.ts.map +0 -1
- package/_shortw_utils.js +0 -20
- package/_shortw_utils.js.map +0 -1
- package/abstract/utils.d.ts +0 -78
- package/abstract/utils.d.ts.map +0 -1
- package/abstract/utils.js +0 -73
- package/abstract/utils.js.map +0 -1
- 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 -243
- package/esm/abstract/edwards.d.ts.map +0 -1
- package/esm/abstract/edwards.js +0 -627
- 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 -95
- package/esm/abstract/tower.d.ts.map +0 -1
- package/esm/abstract/tower.js +0 -714
- package/esm/abstract/tower.js.map +0 -1
- package/esm/abstract/utils.d.ts +0 -78
- package/esm/abstract/utils.d.ts.map +0 -1
- package/esm/abstract/utils.js +0 -70
- package/esm/abstract/utils.js.map +0 -1
- package/esm/abstract/weierstrass.d.ts +0 -416
- package/esm/abstract/weierstrass.d.ts.map +0 -1
- package/esm/abstract/weierstrass.js +0 -1413
- 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 -705
- 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 -214
- 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 -100
- package/esm/ed448.d.ts.map +0 -1
- package/esm/ed448.js +0 -459
- 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 -294
- 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/jubjub.d.ts +0 -12
- package/jubjub.d.ts.map +0 -1
- package/jubjub.js +0 -15
- package/jubjub.js.map +0 -1
- package/p256.d.ts +0 -16
- package/p256.d.ts.map +0 -1
- package/p256.js +0 -13
- package/p256.js.map +0 -1
- package/p384.d.ts +0 -16
- package/p384.d.ts.map +0 -1
- package/p384.js +0 -13
- package/p384.js.map +0 -1
- package/p521.d.ts +0 -16
- package/p521.d.ts.map +0 -1
- package/p521.js +0 -13
- package/p521.js.map +0 -1
- package/pasta.d.ts +0 -10
- package/pasta.d.ts.map +0 -1
- package/pasta.js +0 -13
- package/pasta.js.map +0 -1
- package/src/_shortw_utils.ts +0 -21
- package/src/abstract/utils.ts +0 -80
- 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/nist.ts
CHANGED
|
@@ -5,18 +5,21 @@
|
|
|
5
5
|
*/
|
|
6
6
|
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
7
7
|
import { sha256, sha384, sha512 } from '@noble/hashes/sha2.js';
|
|
8
|
-
import { createCurve, type CurveFnWithCreate } from './_shortw_utils.ts';
|
|
9
8
|
import { createHasher, type H2CHasher } from './abstract/hash-to-curve.ts';
|
|
10
9
|
import { Field } from './abstract/modular.ts';
|
|
10
|
+
import { createORPF, type OPRF } from './abstract/oprf.ts';
|
|
11
11
|
import {
|
|
12
|
+
ecdsa,
|
|
12
13
|
mapToCurveSimpleSWU,
|
|
14
|
+
weierstrass,
|
|
15
|
+
type ECDSA,
|
|
13
16
|
type WeierstrassOpts,
|
|
14
17
|
type WeierstrassPointCons,
|
|
15
18
|
} from './abstract/weierstrass.ts';
|
|
16
19
|
|
|
17
20
|
// p = 2n**224n * (2n**32n-1n) + 2n**192n + 2n**96n - 1n
|
|
18
21
|
// a = Fp256.create(BigInt('-3'));
|
|
19
|
-
const p256_CURVE: WeierstrassOpts<bigint> = {
|
|
22
|
+
const p256_CURVE: WeierstrassOpts<bigint> = /* @__PURE__ */ (() => ({
|
|
20
23
|
p: BigInt('0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff'),
|
|
21
24
|
n: BigInt('0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551'),
|
|
22
25
|
h: BigInt(1),
|
|
@@ -24,10 +27,10 @@ const p256_CURVE: WeierstrassOpts<bigint> = {
|
|
|
24
27
|
b: BigInt('0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b'),
|
|
25
28
|
Gx: BigInt('0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296'),
|
|
26
29
|
Gy: BigInt('0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5'),
|
|
27
|
-
};
|
|
30
|
+
}))();
|
|
28
31
|
|
|
29
32
|
// p = 2n**384n - 2n**128n - 2n**96n + 2n**32n - 1n
|
|
30
|
-
const p384_CURVE: WeierstrassOpts<bigint> = {
|
|
33
|
+
const p384_CURVE: WeierstrassOpts<bigint> = /* @__PURE__ */ (() => ({
|
|
31
34
|
p: BigInt(
|
|
32
35
|
'0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff'
|
|
33
36
|
),
|
|
@@ -47,10 +50,10 @@ const p384_CURVE: WeierstrassOpts<bigint> = {
|
|
|
47
50
|
Gy: BigInt(
|
|
48
51
|
'0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f'
|
|
49
52
|
),
|
|
50
|
-
};
|
|
53
|
+
}))();
|
|
51
54
|
|
|
52
55
|
// p = 2n**521n - 1n
|
|
53
|
-
const p521_CURVE: WeierstrassOpts<bigint> = {
|
|
56
|
+
const p521_CURVE: WeierstrassOpts<bigint> = /* @__PURE__ */ (() => ({
|
|
54
57
|
p: BigInt(
|
|
55
58
|
'0x1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
|
|
56
59
|
),
|
|
@@ -70,34 +73,45 @@ const p521_CURVE: WeierstrassOpts<bigint> = {
|
|
|
70
73
|
Gy: BigInt(
|
|
71
74
|
'0x011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650'
|
|
72
75
|
),
|
|
73
|
-
};
|
|
76
|
+
}))();
|
|
74
77
|
|
|
75
|
-
const Fp256 = Field(p256_CURVE.p);
|
|
76
|
-
const Fp384 = Field(p384_CURVE.p);
|
|
77
|
-
const Fp521 = Field(p521_CURVE.p);
|
|
78
78
|
type SwuOpts = {
|
|
79
79
|
A: bigint;
|
|
80
80
|
B: bigint;
|
|
81
81
|
Z: bigint;
|
|
82
82
|
};
|
|
83
|
+
|
|
83
84
|
function createSWU(Point: WeierstrassPointCons<bigint>, opts: SwuOpts) {
|
|
84
85
|
const map = mapToCurveSimpleSWU(Point.Fp, opts);
|
|
85
86
|
return (scalars: bigint[]) => map(scalars[0]);
|
|
86
87
|
}
|
|
87
88
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
89
|
+
// NIST P256
|
|
90
|
+
const p256_Point = /* @__PURE__ */ weierstrass(p256_CURVE);
|
|
91
|
+
/**
|
|
92
|
+
* NIST P256 (aka secp256r1, prime256v1) curve, ECDSA and ECDH methods.
|
|
93
|
+
* Hashes inputs with sha256 by default.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```js
|
|
97
|
+
* import { p256 } from '@noble/curves/nist.js';
|
|
98
|
+
* const { secretKey, publicKey } = p256.keygen();
|
|
99
|
+
* // const publicKey = p256.getPublicKey(secretKey);
|
|
100
|
+
* const msg = new TextEncoder().encode('hello noble');
|
|
101
|
+
* const sig = p256.sign(msg, secretKey);
|
|
102
|
+
* const isValid = p256.verify(sig, msg, publicKey);
|
|
103
|
+
* // const sigKeccak = p256.sign(keccak256(msg), secretKey, { prehash: false });
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
export const p256: ECDSA = /* @__PURE__ */ ecdsa(p256_Point, sha256);
|
|
93
107
|
/** Hashing / encoding to p256 points / field. RFC 9380 methods. */
|
|
94
|
-
export const p256_hasher: H2CHasher<bigint
|
|
108
|
+
export const p256_hasher: H2CHasher<WeierstrassPointCons<bigint>> = /* @__PURE__ */ (() => {
|
|
95
109
|
return createHasher(
|
|
96
|
-
|
|
97
|
-
createSWU(
|
|
110
|
+
p256_Point,
|
|
111
|
+
createSWU(p256_Point, {
|
|
98
112
|
A: p256_CURVE.a,
|
|
99
113
|
B: p256_CURVE.b,
|
|
100
|
-
Z:
|
|
114
|
+
Z: p256_Point.Fp.create(BigInt('-10')),
|
|
101
115
|
}),
|
|
102
116
|
{
|
|
103
117
|
DST: 'P256_XMD:SHA-256_SSWU_RO_',
|
|
@@ -110,28 +124,28 @@ export const p256_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() => {
|
|
|
110
124
|
}
|
|
111
125
|
);
|
|
112
126
|
})();
|
|
127
|
+
/** p256 OPRF, defined in RFC 9497. */
|
|
128
|
+
export const p256_oprf: OPRF = /* @__PURE__ */ (() =>
|
|
129
|
+
createORPF({
|
|
130
|
+
name: 'P256-SHA256',
|
|
131
|
+
Point: p256_Point,
|
|
132
|
+
hash: sha256,
|
|
133
|
+
hashToGroup: p256_hasher.hashToCurve,
|
|
134
|
+
hashToScalar: p256_hasher.hashToScalar,
|
|
135
|
+
}))();
|
|
113
136
|
|
|
114
|
-
//
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
// hashToGroup: p256_hasher.hashToCurve,
|
|
119
|
-
// hashToScalar: p256_hasher.hashToScalar,
|
|
120
|
-
// });
|
|
121
|
-
|
|
122
|
-
/** NIST P384 (aka secp384r1) curve, ECDSA and ECDH methods. */
|
|
123
|
-
export const p384: CurveFnWithCreate = createCurve(
|
|
124
|
-
{ ...p384_CURVE, Fp: Fp384, lowS: false },
|
|
125
|
-
sha384
|
|
126
|
-
);
|
|
137
|
+
// NIST P384
|
|
138
|
+
const p384_Point = /* @__PURE__ */ weierstrass(p384_CURVE);
|
|
139
|
+
/** NIST P384 (aka secp384r1) curve, ECDSA and ECDH methods. Hashes inputs with sha384 by default. */
|
|
140
|
+
export const p384: ECDSA = /* @__PURE__ */ ecdsa(p384_Point, sha384);
|
|
127
141
|
/** Hashing / encoding to p384 points / field. RFC 9380 methods. */
|
|
128
|
-
export const p384_hasher: H2CHasher<bigint
|
|
142
|
+
export const p384_hasher: H2CHasher<WeierstrassPointCons<bigint>> = /* @__PURE__ */ (() => {
|
|
129
143
|
return createHasher(
|
|
130
|
-
|
|
131
|
-
createSWU(
|
|
144
|
+
p384_Point,
|
|
145
|
+
createSWU(p384_Point, {
|
|
132
146
|
A: p384_CURVE.a,
|
|
133
147
|
B: p384_CURVE.b,
|
|
134
|
-
Z:
|
|
148
|
+
Z: p384_Point.Fp.create(BigInt('-12')),
|
|
135
149
|
}),
|
|
136
150
|
{
|
|
137
151
|
DST: 'P384_XMD:SHA-384_SSWU_RO_',
|
|
@@ -144,37 +158,29 @@ export const p384_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() => {
|
|
|
144
158
|
}
|
|
145
159
|
);
|
|
146
160
|
})();
|
|
161
|
+
/** p384 OPRF, defined in RFC 9497. */
|
|
162
|
+
export const p384_oprf: OPRF = /* @__PURE__ */ (() =>
|
|
163
|
+
createORPF({
|
|
164
|
+
name: 'P384-SHA384',
|
|
165
|
+
Point: p384_Point,
|
|
166
|
+
hash: sha384,
|
|
167
|
+
hashToGroup: p384_hasher.hashToCurve,
|
|
168
|
+
hashToScalar: p384_hasher.hashToScalar,
|
|
169
|
+
}))();
|
|
147
170
|
|
|
148
|
-
//
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
// hashToScalar: p384_hasher.hashToScalar,
|
|
154
|
-
// });
|
|
155
|
-
|
|
156
|
-
// const Fn521 = Field(p521_CURVE.n, { allowedScalarLengths: [65, 66] });
|
|
157
|
-
/** NIST P521 (aka secp521r1) curve, ECDSA and ECDH methods. */
|
|
158
|
-
export const p521: CurveFnWithCreate = createCurve(
|
|
159
|
-
{ ...p521_CURVE, Fp: Fp521, lowS: false, allowedPrivateKeyLengths: [130, 131, 132] },
|
|
160
|
-
sha512
|
|
161
|
-
);
|
|
162
|
-
|
|
163
|
-
/** @deprecated use `p256` for consistency with `p256_hasher` */
|
|
164
|
-
export const secp256r1: typeof p256 = p256;
|
|
165
|
-
/** @deprecated use `p384` for consistency with `p384_hasher` */
|
|
166
|
-
export const secp384r1: typeof p384 = p384;
|
|
167
|
-
/** @deprecated use `p521` for consistency with `p521_hasher` */
|
|
168
|
-
export const secp521r1: typeof p521 = p521;
|
|
169
|
-
|
|
171
|
+
// NIST P521
|
|
172
|
+
const Fn521 = /* @__PURE__ */ (() => Field(p521_CURVE.n, { allowedLengths: [65, 66] }))();
|
|
173
|
+
const p521_Point = /* @__PURE__ */ weierstrass(p521_CURVE, { Fn: Fn521 });
|
|
174
|
+
/** NIST P521 (aka secp521r1) curve, ECDSA and ECDH methods. Hashes inputs with sha512 by default. */
|
|
175
|
+
export const p521: ECDSA = /* @__PURE__ */ ecdsa(p521_Point, sha512);
|
|
170
176
|
/** Hashing / encoding to p521 points / field. RFC 9380 methods. */
|
|
171
|
-
export const p521_hasher: H2CHasher<bigint
|
|
177
|
+
export const p521_hasher: H2CHasher<WeierstrassPointCons<bigint>> = /* @__PURE__ */ (() => {
|
|
172
178
|
return createHasher(
|
|
173
|
-
|
|
174
|
-
createSWU(
|
|
179
|
+
p521_Point,
|
|
180
|
+
createSWU(p521_Point, {
|
|
175
181
|
A: p521_CURVE.a,
|
|
176
182
|
B: p521_CURVE.b,
|
|
177
|
-
Z:
|
|
183
|
+
Z: p521_Point.Fp.create(BigInt('-4')),
|
|
178
184
|
}),
|
|
179
185
|
{
|
|
180
186
|
DST: 'P521_XMD:SHA-512_SSWU_RO_',
|
|
@@ -187,11 +193,12 @@ export const p521_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() => {
|
|
|
187
193
|
}
|
|
188
194
|
);
|
|
189
195
|
})();
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
//
|
|
196
|
+
/** p521 OPRF, defined in RFC 9497. */
|
|
197
|
+
export const p521_oprf: OPRF = /* @__PURE__ */ (() =>
|
|
198
|
+
createORPF({
|
|
199
|
+
name: 'P521-SHA512',
|
|
200
|
+
Point: p521_Point,
|
|
201
|
+
hash: sha512,
|
|
202
|
+
hashToGroup: p521_hasher.hashToCurve,
|
|
203
|
+
hashToScalar: p521_hasher.hashToScalar, // produces L=98 just like in RFC
|
|
204
|
+
}))();
|
package/src/secp256k1.ts
CHANGED
|
@@ -8,35 +8,23 @@
|
|
|
8
8
|
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
9
9
|
import { sha256 } from '@noble/hashes/sha2.js';
|
|
10
10
|
import { randomBytes } from '@noble/hashes/utils.js';
|
|
11
|
-
import {
|
|
12
|
-
import type
|
|
11
|
+
import { createKeygen, type CurveLengths } from './abstract/curve.ts';
|
|
12
|
+
import { createHasher, type H2CHasher, isogenyMap } from './abstract/hash-to-curve.ts';
|
|
13
|
+
import { Field, mapHashToField, pow2 } from './abstract/modular.ts';
|
|
13
14
|
import {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
type H2CMethod,
|
|
17
|
-
isogenyMap,
|
|
18
|
-
} from './abstract/hash-to-curve.ts';
|
|
19
|
-
import { Field, mapHashToField, mod, pow2 } from './abstract/modular.ts';
|
|
20
|
-
import {
|
|
21
|
-
_normFnElement,
|
|
15
|
+
type ECDSA,
|
|
16
|
+
ecdsa,
|
|
22
17
|
type EndomorphismOpts,
|
|
23
18
|
mapToCurveSimpleSWU,
|
|
24
19
|
type WeierstrassPoint as PointType,
|
|
20
|
+
weierstrass,
|
|
25
21
|
type WeierstrassOpts,
|
|
26
22
|
type WeierstrassPointCons,
|
|
27
23
|
} from './abstract/weierstrass.ts';
|
|
28
|
-
import
|
|
29
|
-
import {
|
|
30
|
-
bytesToNumberBE,
|
|
31
|
-
concatBytes,
|
|
32
|
-
ensureBytes,
|
|
33
|
-
inRange,
|
|
34
|
-
numberToBytesBE,
|
|
35
|
-
utf8ToBytes,
|
|
36
|
-
} from './utils.ts';
|
|
24
|
+
import { abytes, asciiToBytes, bytesToNumberBE, concatBytes } from './utils.ts';
|
|
37
25
|
|
|
38
26
|
// Seems like generator was produced from some seed:
|
|
39
|
-
// `
|
|
27
|
+
// `Pointk1.BASE.multiply(Pointk1.Fn.inv(2n, N)).toAffine().x`
|
|
40
28
|
// // gives short x 0x3b78ce563f89a0ed9414f5aa28ad0d96d6795f9c63n
|
|
41
29
|
const secp256k1_CURVE: WeierstrassOpts<bigint> = {
|
|
42
30
|
p: BigInt('0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f'),
|
|
@@ -57,7 +45,6 @@ const secp256k1_ENDO: EndomorphismOpts = {
|
|
|
57
45
|
};
|
|
58
46
|
|
|
59
47
|
const _0n = /* @__PURE__ */ BigInt(0);
|
|
60
|
-
const _1n = /* @__PURE__ */ BigInt(1);
|
|
61
48
|
const _2n = /* @__PURE__ */ BigInt(2);
|
|
62
49
|
|
|
63
50
|
/**
|
|
@@ -89,25 +76,29 @@ function sqrtMod(y: bigint): bigint {
|
|
|
89
76
|
}
|
|
90
77
|
|
|
91
78
|
const Fpk1 = Field(secp256k1_CURVE.p, { sqrt: sqrtMod });
|
|
79
|
+
const Pointk1 = /* @__PURE__ */ weierstrass(secp256k1_CURVE, {
|
|
80
|
+
Fp: Fpk1,
|
|
81
|
+
endo: secp256k1_ENDO,
|
|
82
|
+
});
|
|
92
83
|
|
|
93
84
|
/**
|
|
94
|
-
* secp256k1 curve
|
|
85
|
+
* secp256k1 curve: ECDSA and ECDH methods.
|
|
95
86
|
*
|
|
96
|
-
*
|
|
87
|
+
* Uses sha256 to hash messages. To use a different hash,
|
|
88
|
+
* pass `{ prehash: false }` to sign / verify.
|
|
97
89
|
*
|
|
98
90
|
* @example
|
|
99
91
|
* ```js
|
|
100
|
-
* import { secp256k1 } from '@noble/curves/secp256k1';
|
|
92
|
+
* import { secp256k1 } from '@noble/curves/secp256k1.js';
|
|
101
93
|
* const { secretKey, publicKey } = secp256k1.keygen();
|
|
102
|
-
* const
|
|
94
|
+
* // const publicKey = secp256k1.getPublicKey(secretKey);
|
|
95
|
+
* const msg = new TextEncoder().encode('hello noble');
|
|
103
96
|
* const sig = secp256k1.sign(msg, secretKey);
|
|
104
|
-
* const isValid = secp256k1.verify(sig, msg, publicKey)
|
|
97
|
+
* const isValid = secp256k1.verify(sig, msg, publicKey);
|
|
98
|
+
* // const sigKeccak = secp256k1.sign(keccak256(msg), secretKey, { prehash: false });
|
|
105
99
|
* ```
|
|
106
100
|
*/
|
|
107
|
-
export const secp256k1:
|
|
108
|
-
{ ...secp256k1_CURVE, Fp: Fpk1, lowS: true, endo: secp256k1_ENDO },
|
|
109
|
-
sha256
|
|
110
|
-
);
|
|
101
|
+
export const secp256k1: ECDSA = /* @__PURE__ */ ecdsa(Pointk1, sha256);
|
|
111
102
|
|
|
112
103
|
// Schnorr signatures are superior to ECDSA from above. Below is Schnorr-specific BIP0340 code.
|
|
113
104
|
// https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki
|
|
@@ -116,7 +107,7 @@ const TAGGED_HASH_PREFIXES: { [tag: string]: Uint8Array } = {};
|
|
|
116
107
|
function taggedHash(tag: string, ...messages: Uint8Array[]): Uint8Array {
|
|
117
108
|
let tagP = TAGGED_HASH_PREFIXES[tag];
|
|
118
109
|
if (tagP === undefined) {
|
|
119
|
-
const tagH = sha256(
|
|
110
|
+
const tagH = sha256(asciiToBytes(tag));
|
|
120
111
|
tagP = concatBytes(tagH, tagH);
|
|
121
112
|
TAGGED_HASH_PREFIXES[tag] = tagP;
|
|
122
113
|
}
|
|
@@ -125,13 +116,12 @@ function taggedHash(tag: string, ...messages: Uint8Array[]): Uint8Array {
|
|
|
125
116
|
|
|
126
117
|
// ECDSA compact points are 33-byte. Schnorr is 32: we strip first byte 0x02 or 0x03
|
|
127
118
|
const pointToBytes = (point: PointType<bigint>) => point.toBytes(true).slice(1);
|
|
128
|
-
const Pointk1 = /* @__PURE__ */ (() => secp256k1.Point)();
|
|
129
119
|
const hasEven = (y: bigint) => y % _2n === _0n;
|
|
130
120
|
|
|
131
121
|
// Calculate point, scalar and bytes
|
|
132
|
-
function schnorrGetExtPubKey(priv:
|
|
122
|
+
function schnorrGetExtPubKey(priv: Uint8Array) {
|
|
133
123
|
const { Fn, BASE } = Pointk1;
|
|
134
|
-
const d_ =
|
|
124
|
+
const d_ = Fn.fromBytes(priv);
|
|
135
125
|
const p = BASE.multiply(d_); // P = d'⋅G; 0 < d' < n check is done inside
|
|
136
126
|
const scalar = hasEven(p.y) ? d_ : Fn.neg(d_);
|
|
137
127
|
return { scalar, bytes: pointToBytes(p) };
|
|
@@ -164,7 +154,7 @@ function challenge(...args: Uint8Array[]): bigint {
|
|
|
164
154
|
/**
|
|
165
155
|
* Schnorr public key is just `x` coordinate of Point as per BIP340.
|
|
166
156
|
*/
|
|
167
|
-
function schnorrGetPublicKey(secretKey:
|
|
157
|
+
function schnorrGetPublicKey(secretKey: Uint8Array): Uint8Array {
|
|
168
158
|
return schnorrGetExtPubKey(secretKey).bytes; // d'=int(sk). Fail if d'=0 or d'≥n. Ret bytes(d'⋅G)
|
|
169
159
|
}
|
|
170
160
|
|
|
@@ -172,11 +162,15 @@ function schnorrGetPublicKey(secretKey: Hex): Uint8Array {
|
|
|
172
162
|
* Creates Schnorr signature as per BIP340. Verifies itself before returning anything.
|
|
173
163
|
* auxRand is optional and is not the sole source of k generation: bad CSPRNG won't be dangerous.
|
|
174
164
|
*/
|
|
175
|
-
function schnorrSign(
|
|
165
|
+
function schnorrSign(
|
|
166
|
+
message: Uint8Array,
|
|
167
|
+
secretKey: Uint8Array,
|
|
168
|
+
auxRand: Uint8Array = randomBytes(32)
|
|
169
|
+
): Uint8Array {
|
|
176
170
|
const { Fn } = Pointk1;
|
|
177
|
-
const m =
|
|
171
|
+
const m = abytes(message, undefined, 'message');
|
|
178
172
|
const { bytes: px, scalar: d } = schnorrGetExtPubKey(secretKey); // checks for isWithinCurveOrder
|
|
179
|
-
const a =
|
|
173
|
+
const a = abytes(auxRand, 32, 'auxRand'); // Auxiliary random data a: a 32-byte array
|
|
180
174
|
const t = Fn.toBytes(d ^ num(taggedHash('BIP0340/aux', a))); // Let t be the byte-wise xor of bytes(d) and hash/aux(a)
|
|
181
175
|
const rand = taggedHash('BIP0340/nonce', t, px, m); // Let rand = hash/nonce(t || bytes(P) || m)
|
|
182
176
|
// Let k' = int(rand) mod n. Fail if k' = 0. Let R = k'⋅G
|
|
@@ -194,19 +188,19 @@ function schnorrSign(message: Hex, secretKey: PrivKey, auxRand: Hex = randomByte
|
|
|
194
188
|
* Verifies Schnorr signature.
|
|
195
189
|
* Will swallow errors & return false except for initial type validation of arguments.
|
|
196
190
|
*/
|
|
197
|
-
function schnorrVerify(signature:
|
|
198
|
-
const { Fn, BASE } = Pointk1;
|
|
199
|
-
const sig =
|
|
200
|
-
const m =
|
|
201
|
-
const pub =
|
|
191
|
+
function schnorrVerify(signature: Uint8Array, message: Uint8Array, publicKey: Uint8Array): boolean {
|
|
192
|
+
const { Fp, Fn, BASE } = Pointk1;
|
|
193
|
+
const sig = abytes(signature, 64, 'signature');
|
|
194
|
+
const m = abytes(message, undefined, 'message');
|
|
195
|
+
const pub = abytes(publicKey, 32, 'publicKey');
|
|
202
196
|
try {
|
|
203
197
|
const P = lift_x(num(pub)); // P = lift_x(int(pk)); fail if that fails
|
|
204
198
|
const r = num(sig.subarray(0, 32)); // Let r = int(sig[0:32]); fail if r ≥ p.
|
|
205
|
-
if (!
|
|
199
|
+
if (!Fp.isValidNot0(r)) return false;
|
|
206
200
|
const s = num(sig.subarray(32, 64)); // Let s = int(sig[32:64]); fail if s ≥ n.
|
|
207
|
-
if (!
|
|
208
|
-
|
|
209
|
-
const e = challenge(Fn.toBytes(r), pointToBytes(P), m);
|
|
201
|
+
if (!Fn.isValidNot0(s)) return false;
|
|
202
|
+
|
|
203
|
+
const e = challenge(Fn.toBytes(r), pointToBytes(P), m); // int(challenge(bytes(r)||bytes(P)||m))%n
|
|
210
204
|
// R = s⋅G - e⋅P, where -eP == (n-e)P
|
|
211
205
|
const R = BASE.multiplyUnsafe(s).add(P.multiplyUnsafe(Fn.neg(e)));
|
|
212
206
|
const { x, y } = R.toAffine();
|
|
@@ -229,15 +223,6 @@ export type SecpSchnorr = {
|
|
|
229
223
|
pointToBytes: (point: PointType<bigint>) => Uint8Array;
|
|
230
224
|
lift_x: typeof lift_x;
|
|
231
225
|
taggedHash: typeof taggedHash;
|
|
232
|
-
|
|
233
|
-
/** @deprecated use `randomSecretKey` */
|
|
234
|
-
randomPrivateKey: (seed?: Uint8Array) => Uint8Array;
|
|
235
|
-
/** @deprecated use `utils` */
|
|
236
|
-
numberToBytesBE: typeof numberToBytesBE;
|
|
237
|
-
/** @deprecated use `utils` */
|
|
238
|
-
bytesToNumberBE: typeof bytesToNumberBE;
|
|
239
|
-
/** @deprecated use `modular` */
|
|
240
|
-
mod: typeof mod;
|
|
241
226
|
};
|
|
242
227
|
lengths: CurveLengths;
|
|
243
228
|
};
|
|
@@ -246,7 +231,7 @@ export type SecpSchnorr = {
|
|
|
246
231
|
* https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki
|
|
247
232
|
* @example
|
|
248
233
|
* ```js
|
|
249
|
-
* import { schnorr } from '@noble/curves/secp256k1';
|
|
234
|
+
* import { schnorr } from '@noble/curves/secp256k1.js';
|
|
250
235
|
* const { secretKey, publicKey } = schnorr.keygen();
|
|
251
236
|
* // const publicKey = schnorr.getPublicKey(secretKey);
|
|
252
237
|
* const msg = new TextEncoder().encode('hello');
|
|
@@ -260,29 +245,17 @@ export const schnorr: SecpSchnorr = /* @__PURE__ */ (() => {
|
|
|
260
245
|
const randomSecretKey = (seed = randomBytes(seedLength)): Uint8Array => {
|
|
261
246
|
return mapHashToField(seed, secp256k1_CURVE.n);
|
|
262
247
|
};
|
|
263
|
-
// TODO: remove
|
|
264
|
-
secp256k1.utils.randomSecretKey;
|
|
265
|
-
function keygen(seed?: Uint8Array) {
|
|
266
|
-
const secretKey = randomSecretKey(seed);
|
|
267
|
-
return { secretKey, publicKey: schnorrGetPublicKey(secretKey) };
|
|
268
|
-
}
|
|
269
248
|
return {
|
|
270
|
-
keygen,
|
|
249
|
+
keygen: createKeygen(randomSecretKey, schnorrGetPublicKey),
|
|
271
250
|
getPublicKey: schnorrGetPublicKey,
|
|
272
251
|
sign: schnorrSign,
|
|
273
252
|
verify: schnorrVerify,
|
|
274
253
|
Point: Pointk1,
|
|
275
254
|
utils: {
|
|
276
|
-
randomSecretKey
|
|
277
|
-
randomPrivateKey: randomSecretKey,
|
|
255
|
+
randomSecretKey,
|
|
278
256
|
taggedHash,
|
|
279
|
-
|
|
280
|
-
// TODO: remove
|
|
281
257
|
lift_x,
|
|
282
258
|
pointToBytes,
|
|
283
|
-
numberToBytesBE,
|
|
284
|
-
bytesToNumberBE,
|
|
285
|
-
mod,
|
|
286
259
|
},
|
|
287
260
|
lengths: {
|
|
288
261
|
secretKey: size,
|
|
@@ -335,9 +308,9 @@ const mapSWU = /* @__PURE__ */ (() =>
|
|
|
335
308
|
}))();
|
|
336
309
|
|
|
337
310
|
/** Hashing / encoding to secp256k1 points / field. RFC 9380 methods. */
|
|
338
|
-
export const secp256k1_hasher: H2CHasher<bigint
|
|
311
|
+
export const secp256k1_hasher: H2CHasher<WeierstrassPointCons<bigint>> = /* @__PURE__ */ (() =>
|
|
339
312
|
createHasher(
|
|
340
|
-
|
|
313
|
+
Pointk1,
|
|
341
314
|
(scalars: bigint[]) => {
|
|
342
315
|
const { x, y } = mapSWU(Fpk1.create(scalars[0]));
|
|
343
316
|
return isoMap(x, y);
|
|
@@ -352,11 +325,3 @@ export const secp256k1_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() =>
|
|
|
352
325
|
hash: sha256,
|
|
353
326
|
}
|
|
354
327
|
))();
|
|
355
|
-
|
|
356
|
-
/** @deprecated use `import { secp256k1_hasher } from '@noble/curves/secp256k1.js';` */
|
|
357
|
-
export const hashToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() =>
|
|
358
|
-
secp256k1_hasher.hashToCurve)();
|
|
359
|
-
|
|
360
|
-
/** @deprecated use `import { secp256k1_hasher } from '@noble/curves/secp256k1.js';` */
|
|
361
|
-
export const encodeToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() =>
|
|
362
|
-
secp256k1_hasher.encodeToCurve)();
|