@noble/curves 1.9.0 → 1.9.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 +78 -34
- package/_shortw_utils.d.ts +7 -5
- package/_shortw_utils.d.ts.map +1 -1
- package/_shortw_utils.js +2 -8
- package/_shortw_utils.js.map +1 -1
- package/abstract/bls.d.ts +60 -24
- package/abstract/bls.d.ts.map +1 -1
- package/abstract/bls.js +158 -109
- package/abstract/bls.js.map +1 -1
- package/abstract/curve.d.ts +44 -9
- package/abstract/curve.d.ts.map +1 -1
- package/abstract/curve.js +99 -11
- package/abstract/curve.js.map +1 -1
- package/abstract/edwards.d.ts +112 -25
- package/abstract/edwards.d.ts.map +1 -1
- package/abstract/edwards.js +141 -92
- package/abstract/edwards.js.map +1 -1
- package/abstract/fft.d.ts +122 -0
- package/abstract/fft.d.ts.map +1 -0
- package/abstract/fft.js +438 -0
- package/abstract/fft.js.map +1 -0
- package/abstract/hash-to-curve.d.ts +25 -11
- package/abstract/hash-to-curve.d.ts.map +1 -1
- package/abstract/hash-to-curve.js +17 -14
- package/abstract/hash-to-curve.js.map +1 -1
- package/abstract/modular.d.ts +28 -17
- package/abstract/modular.d.ts.map +1 -1
- package/abstract/modular.js +156 -139
- package/abstract/modular.js.map +1 -1
- package/abstract/montgomery.d.ts +3 -8
- package/abstract/montgomery.d.ts.map +1 -1
- package/abstract/montgomery.js +73 -93
- package/abstract/montgomery.js.map +1 -1
- package/abstract/poseidon.d.ts +5 -13
- package/abstract/poseidon.d.ts.map +1 -1
- package/abstract/poseidon.js +12 -7
- package/abstract/poseidon.js.map +1 -1
- package/abstract/tower.d.ts +20 -46
- package/abstract/tower.d.ts.map +1 -1
- package/abstract/tower.js +10 -4
- package/abstract/tower.js.map +1 -1
- package/abstract/utils.d.ts +1 -115
- package/abstract/utils.d.ts.map +1 -1
- package/abstract/utils.js +17 -371
- package/abstract/utils.js.map +1 -1
- package/abstract/weierstrass.d.ts +152 -73
- package/abstract/weierstrass.d.ts.map +1 -1
- package/abstract/weierstrass.js +487 -404
- package/abstract/weierstrass.js.map +1 -1
- package/bls12-381.d.ts +2 -0
- package/bls12-381.d.ts.map +1 -1
- package/bls12-381.js +504 -480
- package/bls12-381.js.map +1 -1
- package/bn254.d.ts +2 -0
- package/bn254.d.ts.map +1 -1
- package/bn254.js +44 -32
- package/bn254.js.map +1 -1
- package/ed25519.d.ts +25 -9
- package/ed25519.d.ts.map +1 -1
- package/ed25519.js +89 -65
- package/ed25519.js.map +1 -1
- package/ed448.d.ts +29 -10
- package/ed448.d.ts.map +1 -1
- package/ed448.js +116 -81
- package/ed448.js.map +1 -1
- package/esm/_shortw_utils.d.ts +7 -5
- package/esm/_shortw_utils.d.ts.map +1 -1
- package/esm/_shortw_utils.js +2 -8
- package/esm/_shortw_utils.js.map +1 -1
- package/esm/abstract/bls.d.ts +60 -24
- package/esm/abstract/bls.d.ts.map +1 -1
- package/esm/abstract/bls.js +158 -109
- package/esm/abstract/bls.js.map +1 -1
- package/esm/abstract/curve.d.ts +44 -9
- package/esm/abstract/curve.d.ts.map +1 -1
- package/esm/abstract/curve.js +96 -12
- package/esm/abstract/curve.js.map +1 -1
- package/esm/abstract/edwards.d.ts +112 -25
- package/esm/abstract/edwards.d.ts.map +1 -1
- package/esm/abstract/edwards.js +141 -94
- package/esm/abstract/edwards.js.map +1 -1
- package/esm/abstract/fft.d.ts +122 -0
- package/esm/abstract/fft.d.ts.map +1 -0
- package/esm/abstract/fft.js +425 -0
- package/esm/abstract/fft.js.map +1 -0
- package/esm/abstract/hash-to-curve.d.ts +25 -11
- package/esm/abstract/hash-to-curve.d.ts.map +1 -1
- package/esm/abstract/hash-to-curve.js +17 -14
- package/esm/abstract/hash-to-curve.js.map +1 -1
- package/esm/abstract/modular.d.ts +28 -17
- package/esm/abstract/modular.d.ts.map +1 -1
- package/esm/abstract/modular.js +155 -138
- package/esm/abstract/modular.js.map +1 -1
- package/esm/abstract/montgomery.d.ts +3 -8
- package/esm/abstract/montgomery.d.ts.map +1 -1
- package/esm/abstract/montgomery.js +74 -94
- package/esm/abstract/montgomery.js.map +1 -1
- package/esm/abstract/poseidon.d.ts +5 -13
- package/esm/abstract/poseidon.d.ts.map +1 -1
- package/esm/abstract/poseidon.js +12 -7
- package/esm/abstract/poseidon.js.map +1 -1
- package/esm/abstract/tower.d.ts +20 -46
- package/esm/abstract/tower.d.ts.map +1 -1
- package/esm/abstract/tower.js +10 -4
- package/esm/abstract/tower.js.map +1 -1
- package/esm/abstract/utils.d.ts +1 -115
- package/esm/abstract/utils.d.ts.map +1 -1
- package/esm/abstract/utils.js +3 -344
- package/esm/abstract/utils.js.map +1 -1
- package/esm/abstract/weierstrass.d.ts +152 -73
- package/esm/abstract/weierstrass.d.ts.map +1 -1
- package/esm/abstract/weierstrass.js +485 -406
- package/esm/abstract/weierstrass.js.map +1 -1
- package/esm/bls12-381.d.ts +2 -0
- package/esm/bls12-381.d.ts.map +1 -1
- package/esm/bls12-381.js +503 -479
- package/esm/bls12-381.js.map +1 -1
- package/esm/bn254.d.ts +2 -0
- package/esm/bn254.d.ts.map +1 -1
- package/esm/bn254.js +41 -29
- package/esm/bn254.js.map +1 -1
- package/esm/ed25519.d.ts +25 -9
- package/esm/ed25519.d.ts.map +1 -1
- package/esm/ed25519.js +84 -60
- package/esm/ed25519.js.map +1 -1
- package/esm/ed448.d.ts +29 -10
- package/esm/ed448.d.ts.map +1 -1
- package/esm/ed448.js +113 -78
- package/esm/ed448.js.map +1 -1
- package/esm/jubjub.d.ts +4 -0
- package/esm/jubjub.d.ts.map +1 -1
- package/esm/jubjub.js +4 -0
- package/esm/jubjub.js.map +1 -1
- package/esm/misc.d.ts.map +1 -1
- package/esm/misc.js +31 -26
- package/esm/misc.js.map +1 -1
- package/esm/nist.d.ts +8 -16
- package/esm/nist.d.ts.map +1 -1
- package/esm/nist.js +87 -97
- package/esm/nist.js.map +1 -1
- package/esm/p256.d.ts +3 -3
- package/esm/p384.d.ts +3 -3
- package/esm/p521.d.ts +3 -3
- package/esm/pasta.d.ts +4 -0
- package/esm/pasta.d.ts.map +1 -1
- package/esm/pasta.js +4 -0
- package/esm/pasta.js.map +1 -1
- package/esm/secp256k1.d.ts +6 -6
- package/esm/secp256k1.d.ts.map +1 -1
- package/esm/secp256k1.js +44 -41
- package/esm/secp256k1.js.map +1 -1
- package/esm/utils.d.ts +96 -0
- package/esm/utils.d.ts.map +1 -0
- package/esm/utils.js +279 -0
- package/esm/utils.js.map +1 -0
- package/jubjub.d.ts +4 -0
- package/jubjub.d.ts.map +1 -1
- package/jubjub.js +4 -0
- package/jubjub.js.map +1 -1
- package/misc.d.ts.map +1 -1
- package/misc.js +35 -30
- package/misc.js.map +1 -1
- package/nist.d.ts +8 -16
- package/nist.d.ts.map +1 -1
- package/nist.js +87 -97
- package/nist.js.map +1 -1
- package/p256.d.ts +3 -3
- package/p384.d.ts +3 -3
- package/p521.d.ts +3 -3
- package/package.json +26 -8
- package/pasta.d.ts +4 -0
- package/pasta.d.ts.map +1 -1
- package/pasta.js +4 -0
- package/pasta.js.map +1 -1
- package/secp256k1.d.ts +6 -6
- package/secp256k1.d.ts.map +1 -1
- package/secp256k1.js +47 -44
- package/secp256k1.js.map +1 -1
- package/src/_shortw_utils.ts +5 -15
- package/src/abstract/bls.ts +260 -145
- package/src/abstract/curve.ts +125 -18
- package/src/abstract/edwards.ts +282 -127
- package/src/abstract/fft.ts +519 -0
- package/src/abstract/hash-to-curve.ts +51 -27
- package/src/abstract/modular.ts +156 -143
- package/src/abstract/montgomery.ts +81 -111
- package/src/abstract/poseidon.ts +22 -18
- package/src/abstract/tower.ts +37 -68
- package/src/abstract/utils.ts +3 -378
- package/src/abstract/weierstrass.ts +752 -461
- package/src/bls12-381.ts +542 -507
- package/src/bn254.ts +47 -35
- package/src/ed25519.ts +104 -76
- package/src/ed448.ts +156 -105
- package/src/jubjub.ts +4 -0
- package/src/misc.ts +39 -34
- package/src/nist.ts +138 -126
- package/src/p256.ts +3 -3
- package/src/p384.ts +3 -3
- package/src/p521.ts +3 -3
- package/src/pasta.ts +5 -1
- package/src/secp256k1.ts +59 -47
- package/src/utils.ts +328 -0
- package/utils.d.ts +96 -0
- package/utils.d.ts.map +1 -0
- package/utils.js +313 -0
- package/utils.js.map +1 -0
package/src/nist.ts
CHANGED
|
@@ -4,151 +4,163 @@
|
|
|
4
4
|
* @module
|
|
5
5
|
*/
|
|
6
6
|
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
7
|
-
import { sha256, sha384, sha512 } from '@noble/hashes/sha2';
|
|
7
|
+
import { sha256, sha384, sha512 } from '@noble/hashes/sha2.js';
|
|
8
8
|
import { createCurve, type CurveFnWithCreate } from './_shortw_utils.ts';
|
|
9
|
-
import { createHasher, type
|
|
10
|
-
import { Field } from './abstract/modular.ts';
|
|
11
|
-
import { mapToCurveSimpleSWU } from './abstract/weierstrass.ts';
|
|
9
|
+
import { createHasher, type H2CHasher } from './abstract/hash-to-curve.ts';
|
|
10
|
+
import { Field, type IField } from './abstract/modular.ts';
|
|
11
|
+
import { mapToCurveSimpleSWU, type WeierstrassOpts } from './abstract/weierstrass.ts';
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* secp256r1 curve, ECDSA and ECDH methods.
|
|
19
|
-
* Field: `2n**224n * (2n**32n-1n) + 2n**192n + 2n**96n-1n`
|
|
20
|
-
*/
|
|
21
|
-
// prettier-ignore
|
|
22
|
-
export const p256: CurveFnWithCreate = createCurve({
|
|
23
|
-
a: p256_a,
|
|
24
|
-
b: p256_b,
|
|
25
|
-
Fp: Fp256,
|
|
13
|
+
// p = 2n**224n * (2n**32n-1n) + 2n**192n + 2n**96n - 1n
|
|
14
|
+
// a = Fp256.create(BigInt('-3'));
|
|
15
|
+
const p256_CURVE: WeierstrassOpts<bigint> = {
|
|
16
|
+
p: BigInt('0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff'),
|
|
26
17
|
n: BigInt('0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551'),
|
|
18
|
+
h: BigInt(1),
|
|
19
|
+
a: BigInt('0xffffffff00000001000000000000000000000000fffffffffffffffffffffffc'),
|
|
20
|
+
b: BigInt('0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b'),
|
|
27
21
|
Gx: BigInt('0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296'),
|
|
28
22
|
Gy: BigInt('0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5'),
|
|
29
|
-
|
|
30
|
-
lowS: false
|
|
31
|
-
} as const, sha256);
|
|
32
|
-
/** Alias to p256. */
|
|
33
|
-
export const secp256r1: CurveFnWithCreate = p256;
|
|
23
|
+
};
|
|
34
24
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
B: p256_b,
|
|
39
|
-
Z: Fp256.create(BigInt('-10')),
|
|
40
|
-
}))();
|
|
41
|
-
|
|
42
|
-
/** Hashing / encoding to p256 points / field. RFC 9380 methods. */
|
|
43
|
-
export const p256_hasher: Hasher<bigint> = /* @__PURE__ */ (() =>
|
|
44
|
-
createHasher(secp256r1.ProjectivePoint, (scalars: bigint[]) => p256_mapSWU(scalars[0]), {
|
|
45
|
-
DST: 'P256_XMD:SHA-256_SSWU_RO_',
|
|
46
|
-
encodeDST: 'P256_XMD:SHA-256_SSWU_NU_',
|
|
47
|
-
p: Fp256.ORDER,
|
|
48
|
-
m: 1,
|
|
49
|
-
k: 128,
|
|
50
|
-
expand: 'xmd',
|
|
51
|
-
hash: sha256,
|
|
52
|
-
}))();
|
|
53
|
-
|
|
54
|
-
// Field over which we'll do calculations.
|
|
55
|
-
const Fp384 = Field(
|
|
56
|
-
BigInt(
|
|
25
|
+
// p = 2n**384n - 2n**128n - 2n**96n + 2n**32n - 1n
|
|
26
|
+
const p384_CURVE: WeierstrassOpts<bigint> = {
|
|
27
|
+
p: BigInt(
|
|
57
28
|
'0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff'
|
|
58
|
-
)
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const p384_b = BigInt('0xb3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef');
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* secp384r1 curve, ECDSA and ECDH methods.
|
|
66
|
-
* Field: `2n**384n - 2n**128n - 2n**96n + 2n**32n - 1n`.
|
|
67
|
-
* */
|
|
68
|
-
// prettier-ignore
|
|
69
|
-
export const p384: CurveFnWithCreate = createCurve({
|
|
70
|
-
a: p384_a,
|
|
71
|
-
b: p384_b,
|
|
72
|
-
Fp: Fp384,
|
|
73
|
-
n: BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973'),
|
|
74
|
-
Gx: BigInt('0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7'),
|
|
75
|
-
Gy: BigInt('0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f'),
|
|
29
|
+
),
|
|
30
|
+
n: BigInt(
|
|
31
|
+
'0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973'
|
|
32
|
+
),
|
|
76
33
|
h: BigInt(1),
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
export const p384_hasher: Hasher<bigint> = /* @__PURE__ */ (() =>
|
|
91
|
-
createHasher(secp384r1.ProjectivePoint, (scalars: bigint[]) => p384_mapSWU(scalars[0]), {
|
|
92
|
-
DST: 'P384_XMD:SHA-384_SSWU_RO_',
|
|
93
|
-
encodeDST: 'P384_XMD:SHA-384_SSWU_NU_',
|
|
94
|
-
p: Fp384.ORDER,
|
|
95
|
-
m: 1,
|
|
96
|
-
k: 192,
|
|
97
|
-
expand: 'xmd',
|
|
98
|
-
hash: sha384,
|
|
99
|
-
}))();
|
|
34
|
+
a: BigInt(
|
|
35
|
+
'0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000fffffffc'
|
|
36
|
+
),
|
|
37
|
+
b: BigInt(
|
|
38
|
+
'0xb3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef'
|
|
39
|
+
),
|
|
40
|
+
Gx: BigInt(
|
|
41
|
+
'0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7'
|
|
42
|
+
),
|
|
43
|
+
Gy: BigInt(
|
|
44
|
+
'0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f'
|
|
45
|
+
),
|
|
46
|
+
};
|
|
100
47
|
|
|
101
|
-
//
|
|
102
|
-
const
|
|
103
|
-
BigInt(
|
|
48
|
+
// p = 2n**521n - 1n
|
|
49
|
+
const p521_CURVE: WeierstrassOpts<bigint> = {
|
|
50
|
+
p: BigInt(
|
|
104
51
|
'0x1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
|
|
105
|
-
)
|
|
106
|
-
);
|
|
107
|
-
|
|
108
|
-
const p521_a = Fp521.create(BigInt('-3'));
|
|
109
|
-
const p521_b = BigInt(
|
|
110
|
-
'0x0051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00'
|
|
111
|
-
);
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* NIST secp521r1 aka p521 curve, ECDSA and ECDH methods.
|
|
115
|
-
* Field: `2n**521n - 1n`.
|
|
116
|
-
*/
|
|
117
|
-
// prettier-ignore
|
|
118
|
-
export const p521: CurveFnWithCreate = createCurve({
|
|
119
|
-
a: p521_a,
|
|
120
|
-
b: p521_b,
|
|
121
|
-
Fp: Fp521,
|
|
52
|
+
),
|
|
122
53
|
n: BigInt(
|
|
123
54
|
'0x01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409'
|
|
124
55
|
),
|
|
56
|
+
h: BigInt(1),
|
|
57
|
+
a: BigInt(
|
|
58
|
+
'0x1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc'
|
|
59
|
+
),
|
|
60
|
+
b: BigInt(
|
|
61
|
+
'0x0051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00'
|
|
62
|
+
),
|
|
125
63
|
Gx: BigInt(
|
|
126
64
|
'0x00c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66'
|
|
127
65
|
),
|
|
128
66
|
Gy: BigInt(
|
|
129
67
|
'0x011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650'
|
|
130
68
|
),
|
|
131
|
-
|
|
132
|
-
lowS: false,
|
|
133
|
-
allowedPrivateKeyLengths: [130, 131, 132] // P521 keys are variable-length. Normalize to 132b
|
|
134
|
-
} as const, sha512);
|
|
135
|
-
export const secp521r1: CurveFnWithCreate = p521;
|
|
69
|
+
};
|
|
136
70
|
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
71
|
+
const Fp256 = Field(p256_CURVE.p);
|
|
72
|
+
const Fp384 = Field(p384_CURVE.p);
|
|
73
|
+
const Fp521 = Field(p521_CURVE.p);
|
|
74
|
+
type SwuOpts = {
|
|
75
|
+
A: bigint;
|
|
76
|
+
B: bigint;
|
|
77
|
+
Z: bigint;
|
|
78
|
+
};
|
|
79
|
+
function createSWU(field: IField<bigint>, opts: SwuOpts) {
|
|
80
|
+
const map = mapToCurveSimpleSWU(field, opts);
|
|
81
|
+
return (scalars: bigint[]) => map(scalars[0]);
|
|
82
|
+
}
|
|
143
83
|
|
|
84
|
+
/** NIST P256 (aka secp256r1, prime256v1) curve, ECDSA and ECDH methods. */
|
|
85
|
+
export const p256: CurveFnWithCreate = createCurve(
|
|
86
|
+
{ ...p256_CURVE, Fp: Fp256, lowS: false },
|
|
87
|
+
sha256
|
|
88
|
+
);
|
|
89
|
+
/** Alias to p256. */
|
|
90
|
+
export const secp256r1: CurveFnWithCreate = p256;
|
|
91
|
+
/** Hashing / encoding to p256 points / field. RFC 9380 methods. */
|
|
92
|
+
export const p256_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() => {
|
|
93
|
+
return createHasher(
|
|
94
|
+
p256.Point,
|
|
95
|
+
createSWU(Fp256, {
|
|
96
|
+
A: p256_CURVE.a,
|
|
97
|
+
B: p256_CURVE.b,
|
|
98
|
+
Z: Fp256.create(BigInt('-10')),
|
|
99
|
+
}),
|
|
100
|
+
{
|
|
101
|
+
DST: 'P256_XMD:SHA-256_SSWU_RO_',
|
|
102
|
+
encodeDST: 'P256_XMD:SHA-256_SSWU_NU_',
|
|
103
|
+
p: p256_CURVE.p,
|
|
104
|
+
m: 1,
|
|
105
|
+
k: 128,
|
|
106
|
+
expand: 'xmd',
|
|
107
|
+
hash: sha256,
|
|
108
|
+
}
|
|
109
|
+
);
|
|
110
|
+
})();
|
|
111
|
+
|
|
112
|
+
/** NIST P384 (aka secp384r1) curve, ECDSA and ECDH methods. */
|
|
113
|
+
export const p384: CurveFnWithCreate = createCurve(
|
|
114
|
+
{ ...p384_CURVE, Fp: Fp384, lowS: false },
|
|
115
|
+
sha384
|
|
116
|
+
);
|
|
117
|
+
/** Alias to p384. */
|
|
118
|
+
export const secp384r1: CurveFnWithCreate = p384;
|
|
119
|
+
/** Hashing / encoding to p384 points / field. RFC 9380 methods. */
|
|
120
|
+
export const p384_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() => {
|
|
121
|
+
return createHasher(
|
|
122
|
+
p384.Point,
|
|
123
|
+
createSWU(Fp384, {
|
|
124
|
+
A: p384_CURVE.a,
|
|
125
|
+
B: p384_CURVE.b,
|
|
126
|
+
Z: Fp384.create(BigInt('-12')),
|
|
127
|
+
}),
|
|
128
|
+
{
|
|
129
|
+
DST: 'P384_XMD:SHA-384_SSWU_RO_',
|
|
130
|
+
encodeDST: 'P384_XMD:SHA-384_SSWU_NU_',
|
|
131
|
+
p: p384_CURVE.p,
|
|
132
|
+
m: 1,
|
|
133
|
+
k: 192,
|
|
134
|
+
expand: 'xmd',
|
|
135
|
+
hash: sha384,
|
|
136
|
+
}
|
|
137
|
+
);
|
|
138
|
+
})();
|
|
139
|
+
|
|
140
|
+
/** NIST P521 (aka secp521r1) curve, ECDSA and ECDH methods. */
|
|
141
|
+
export const p521: CurveFnWithCreate = createCurve(
|
|
142
|
+
{ ...p521_CURVE, Fp: Fp521, lowS: false, allowedPrivateKeyLengths: [130, 131, 132] },
|
|
143
|
+
sha512
|
|
144
|
+
);
|
|
145
|
+
/** Alias to p521. */
|
|
146
|
+
export const secp521r1: CurveFnWithCreate = p521;
|
|
144
147
|
/** Hashing / encoding to p521 points / field. RFC 9380 methods. */
|
|
145
|
-
export const p521_hasher:
|
|
146
|
-
createHasher(
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
148
|
+
export const p521_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() => {
|
|
149
|
+
return createHasher(
|
|
150
|
+
p521.Point,
|
|
151
|
+
createSWU(Fp521, {
|
|
152
|
+
A: p521_CURVE.a,
|
|
153
|
+
B: p521_CURVE.b,
|
|
154
|
+
Z: Fp521.create(BigInt('-4')),
|
|
155
|
+
}),
|
|
156
|
+
{
|
|
157
|
+
DST: 'P521_XMD:SHA-512_SSWU_RO_',
|
|
158
|
+
encodeDST: 'P521_XMD:SHA-512_SSWU_NU_',
|
|
159
|
+
p: p521_CURVE.p,
|
|
160
|
+
m: 1,
|
|
161
|
+
k: 256,
|
|
162
|
+
expand: 'xmd',
|
|
163
|
+
hash: sha512,
|
|
164
|
+
}
|
|
165
|
+
);
|
|
166
|
+
})();
|
package/src/p256.ts
CHANGED
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
* @module
|
|
4
4
|
*/
|
|
5
5
|
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
6
|
-
import { type
|
|
6
|
+
import { type H2CMethod } from './abstract/hash-to-curve.ts';
|
|
7
7
|
import { p256_hasher, p256 as p256n } from './nist.ts';
|
|
8
8
|
export const p256: typeof p256n = p256n;
|
|
9
9
|
export const secp256r1: typeof p256n = p256n;
|
|
10
|
-
export const hashToCurve:
|
|
11
|
-
export const encodeToCurve:
|
|
10
|
+
export const hashToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => p256_hasher.hashToCurve)();
|
|
11
|
+
export const encodeToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => p256_hasher.encodeToCurve)();
|
package/src/p384.ts
CHANGED
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
* @module
|
|
4
4
|
*/
|
|
5
5
|
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
6
|
-
import { type
|
|
6
|
+
import { type H2CMethod } from './abstract/hash-to-curve.ts';
|
|
7
7
|
import { p384_hasher, p384 as p384n } from './nist.ts';
|
|
8
8
|
export const p384: typeof p384n = p384n;
|
|
9
9
|
export const secp384r1: typeof p384n = p384n;
|
|
10
|
-
export const hashToCurve:
|
|
11
|
-
export const encodeToCurve:
|
|
10
|
+
export const hashToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => p384_hasher.hashToCurve)();
|
|
11
|
+
export const encodeToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => p384_hasher.encodeToCurve)();
|
|
12
12
|
|
|
13
13
|
/** @deprecated Use `import { p384_hasher } from "@noble/curves/nist"` module. */
|
package/src/p521.ts
CHANGED
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
* @module
|
|
4
4
|
*/
|
|
5
5
|
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
6
|
-
import { type
|
|
6
|
+
import { type H2CMethod } from './abstract/hash-to-curve.ts';
|
|
7
7
|
import { p521_hasher, p521 as p521n } from './nist.ts';
|
|
8
8
|
export const p521: typeof p521n = p521n;
|
|
9
9
|
export const secp521r1: typeof p521n = p521n;
|
|
10
|
-
export const hashToCurve:
|
|
11
|
-
export const encodeToCurve:
|
|
10
|
+
export const hashToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => p521_hasher.hashToCurve)();
|
|
11
|
+
export const encodeToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => p521_hasher.encodeToCurve)();
|
package/src/pasta.ts
CHANGED
package/src/secp256k1.ts
CHANGED
|
@@ -1,22 +1,28 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* SECG secp256k1. See [pdf](https://www.secg.org/sec2-v2.pdf).
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* secp256k1 belongs to Koblitz curves: it has efficiently computable endomorphism.
|
|
8
|
-
* Endomorphism uses 2x less RAM, speeds up precomputation by 2x and ECDH / key recovery by 20%.
|
|
9
|
-
* For precomputed wNAF it trades off 1/2 init time & 1/3 ram for 20% perf hit.
|
|
10
|
-
* [See explanation](https://gist.github.com/paulmillr/eb670806793e84df628a7c434a873066).
|
|
4
|
+
* Belongs to Koblitz curves: it has efficiently-computable GLV endomorphism ψ,
|
|
5
|
+
* check out {@link EndomorphismOpts}. Seems to be rigid (not backdoored).
|
|
11
6
|
* @module
|
|
12
7
|
*/
|
|
13
8
|
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
14
|
-
import { sha256 } from '@noble/hashes/sha2';
|
|
15
|
-
import { randomBytes } from '@noble/hashes/utils';
|
|
9
|
+
import { sha256 } from '@noble/hashes/sha2.js';
|
|
10
|
+
import { randomBytes } from '@noble/hashes/utils.js';
|
|
16
11
|
import { createCurve, type CurveFnWithCreate } from './_shortw_utils.ts';
|
|
17
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
createHasher,
|
|
14
|
+
type H2CHasher,
|
|
15
|
+
type H2CMethod,
|
|
16
|
+
isogenyMap,
|
|
17
|
+
} from './abstract/hash-to-curve.ts';
|
|
18
18
|
import { Field, mod, pow2 } from './abstract/modular.ts';
|
|
19
|
-
import
|
|
19
|
+
import {
|
|
20
|
+
type EndomorphismOpts,
|
|
21
|
+
mapToCurveSimpleSWU,
|
|
22
|
+
type ProjPointType as PointType,
|
|
23
|
+
type WeierstrassOpts,
|
|
24
|
+
} from './abstract/weierstrass.ts';
|
|
25
|
+
import type { Hex, PrivKey } from './utils.ts';
|
|
20
26
|
import {
|
|
21
27
|
aInRange,
|
|
22
28
|
bytesToNumberBE,
|
|
@@ -24,11 +30,21 @@ import {
|
|
|
24
30
|
ensureBytes,
|
|
25
31
|
inRange,
|
|
26
32
|
numberToBytesBE,
|
|
27
|
-
} from './
|
|
28
|
-
import { mapToCurveSimpleSWU, type ProjPointType as PointType } from './abstract/weierstrass.ts';
|
|
33
|
+
} from './utils.ts';
|
|
29
34
|
|
|
30
|
-
|
|
31
|
-
|
|
35
|
+
// Seems like generator was produced from some seed:
|
|
36
|
+
// `Point.BASE.multiply(Point.Fn.inv(2n, N)).toAffine().x`
|
|
37
|
+
// // gives short x 0x3b78ce563f89a0ed9414f5aa28ad0d96d6795f9c63n
|
|
38
|
+
const secp256k1_CURVE: WeierstrassOpts<bigint> = {
|
|
39
|
+
p: BigInt('0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f'),
|
|
40
|
+
n: BigInt('0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141'),
|
|
41
|
+
h: BigInt(1),
|
|
42
|
+
a: BigInt(0),
|
|
43
|
+
b: BigInt(7),
|
|
44
|
+
Gx: BigInt('0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'),
|
|
45
|
+
Gy: BigInt('0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8'),
|
|
46
|
+
};
|
|
47
|
+
const _0n = BigInt(0);
|
|
32
48
|
const _1n = BigInt(1);
|
|
33
49
|
const _2n = BigInt(2);
|
|
34
50
|
const divNearest = (a: bigint, b: bigint) => (a + b / _2n) / b;
|
|
@@ -38,7 +54,7 @@ const divNearest = (a: bigint, b: bigint) => (a + b / _2n) / b;
|
|
|
38
54
|
* (P+1n/4n).toString(2) would produce bits [223x 1, 0, 22x 1, 4x 0, 11, 00]
|
|
39
55
|
*/
|
|
40
56
|
function sqrtMod(y: bigint): bigint {
|
|
41
|
-
const P =
|
|
57
|
+
const P = secp256k1_CURVE.p;
|
|
42
58
|
// prettier-ignore
|
|
43
59
|
const _3n = BigInt(3), _6n = BigInt(6), _11n = BigInt(11), _22n = BigInt(22);
|
|
44
60
|
// prettier-ignore
|
|
@@ -61,7 +77,7 @@ function sqrtMod(y: bigint): bigint {
|
|
|
61
77
|
return root;
|
|
62
78
|
}
|
|
63
79
|
|
|
64
|
-
const Fpk1 = Field(
|
|
80
|
+
const Fpk1 = Field(secp256k1_CURVE.p, undefined, undefined, { sqrt: sqrtMod });
|
|
65
81
|
|
|
66
82
|
/**
|
|
67
83
|
* secp256k1 curve, ECDSA and ECDH methods.
|
|
@@ -80,19 +96,14 @@ const Fpk1 = Field(secp256k1P, undefined, undefined, { sqrt: sqrtMod });
|
|
|
80
96
|
*/
|
|
81
97
|
export const secp256k1: CurveFnWithCreate = createCurve(
|
|
82
98
|
{
|
|
83
|
-
|
|
84
|
-
b: BigInt(7),
|
|
99
|
+
...secp256k1_CURVE,
|
|
85
100
|
Fp: Fpk1,
|
|
86
|
-
n: secp256k1N,
|
|
87
|
-
Gx: BigInt('55066263022277343669578718895168534326250603453777594175500187360389116729240'),
|
|
88
|
-
Gy: BigInt('32670510020758816978083085130507043184471273380659243275938904335757337482424'),
|
|
89
|
-
h: BigInt(1),
|
|
90
101
|
lowS: true, // Allow only low-S signatures by default in sign() and verify()
|
|
91
102
|
endo: {
|
|
92
103
|
// Endomorphism, see above
|
|
93
104
|
beta: BigInt('0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee'),
|
|
94
105
|
splitScalar: (k: bigint) => {
|
|
95
|
-
const n =
|
|
106
|
+
const n = secp256k1_CURVE.n;
|
|
96
107
|
const a1 = BigInt('0x3086d221a7d46bcde86c90e49284eb15');
|
|
97
108
|
const b1 = -_1n * BigInt('0xe4437ed6010e88286f547fa90abfe4c3');
|
|
98
109
|
const a2 = BigInt('0x114ca50f7a8e2f3f657c1108d9d44cfd8');
|
|
@@ -112,14 +123,13 @@ export const secp256k1: CurveFnWithCreate = createCurve(
|
|
|
112
123
|
}
|
|
113
124
|
return { k1neg, k1, k2neg, k2 };
|
|
114
125
|
},
|
|
115
|
-
},
|
|
126
|
+
} satisfies EndomorphismOpts,
|
|
116
127
|
},
|
|
117
128
|
sha256
|
|
118
129
|
);
|
|
119
130
|
|
|
120
131
|
// Schnorr signatures are superior to ECDSA from above. Below is Schnorr-specific BIP0340 code.
|
|
121
132
|
// https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki
|
|
122
|
-
const _0n = BigInt(0);
|
|
123
133
|
/** An object mapping tags to their tagged hash prefix of [SHA256(tag) | SHA256(tag)] */
|
|
124
134
|
const TAGGED_HASH_PREFIXES: { [tag: string]: Uint8Array } = {};
|
|
125
135
|
function taggedHash(tag: string, ...messages: Uint8Array[]): Uint8Array {
|
|
@@ -133,19 +143,18 @@ function taggedHash(tag: string, ...messages: Uint8Array[]): Uint8Array {
|
|
|
133
143
|
}
|
|
134
144
|
|
|
135
145
|
// ECDSA compact points are 33-byte. Schnorr is 32: we strip first byte 0x02 or 0x03
|
|
136
|
-
const pointToBytes = (point: PointType<bigint>) => point.
|
|
146
|
+
const pointToBytes = (point: PointType<bigint>) => point.toBytes(true).slice(1);
|
|
137
147
|
const numTo32b = (n: bigint) => numberToBytesBE(n, 32);
|
|
138
|
-
const modP = (x: bigint) => mod(x,
|
|
139
|
-
const modN = (x: bigint) => mod(x,
|
|
140
|
-
const Point = secp256k1.
|
|
141
|
-
const
|
|
142
|
-
Point.BASE.multiplyAndAddUnsafe(Q, a, b);
|
|
148
|
+
const modP = (x: bigint) => mod(x, secp256k1_CURVE.p);
|
|
149
|
+
const modN = (x: bigint) => mod(x, secp256k1_CURVE.n);
|
|
150
|
+
const Point = /* @__PURE__ */ (() => secp256k1.Point)();
|
|
151
|
+
const hasEven = (y: bigint) => y % _2n === _0n;
|
|
143
152
|
|
|
144
153
|
// Calculate point, scalar and bytes
|
|
145
154
|
function schnorrGetExtPubKey(priv: PrivKey) {
|
|
146
155
|
let d_ = secp256k1.utils.normPrivateKeyToScalar(priv); // same method executed in fromPrivateKey
|
|
147
156
|
let p = Point.fromPrivateKey(d_); // P = d'⋅G; 0 < d' < n check is done inside
|
|
148
|
-
const scalar = p.
|
|
157
|
+
const scalar = hasEven(p.y) ? d_ : modN(-d_);
|
|
149
158
|
return { scalar: scalar, bytes: pointToBytes(p) };
|
|
150
159
|
}
|
|
151
160
|
/**
|
|
@@ -153,12 +162,12 @@ function schnorrGetExtPubKey(priv: PrivKey) {
|
|
|
153
162
|
* @returns valid point checked for being on-curve
|
|
154
163
|
*/
|
|
155
164
|
function lift_x(x: bigint): PointType<bigint> {
|
|
156
|
-
aInRange('x', x, _1n,
|
|
165
|
+
aInRange('x', x, _1n, secp256k1_CURVE.p); // Fail if x ≥ p.
|
|
157
166
|
const xx = modP(x * x);
|
|
158
167
|
const c = modP(xx * x + BigInt(7)); // Let c = x³ + 7 mod p.
|
|
159
168
|
let y = sqrtMod(c); // Let y = c^(p+1)/4 mod p.
|
|
160
|
-
if (y
|
|
161
|
-
const p =
|
|
169
|
+
if (!hasEven(y)) y = modP(-y); // Return the unique point P such that x(P) = x and
|
|
170
|
+
const p = Point.fromAffine({ x, y }); // y(P) = y if y mod 2 = 0 or y(P) = p-y otherwise.
|
|
162
171
|
p.assertValidity();
|
|
163
172
|
return p;
|
|
164
173
|
}
|
|
@@ -214,13 +223,16 @@ function schnorrVerify(signature: Hex, message: Hex, publicKey: Hex): boolean {
|
|
|
214
223
|
try {
|
|
215
224
|
const P = lift_x(num(pub)); // P = lift_x(int(pk)); fail if that fails
|
|
216
225
|
const r = num(sig.subarray(0, 32)); // Let r = int(sig[0:32]); fail if r ≥ p.
|
|
217
|
-
if (!inRange(r, _1n,
|
|
226
|
+
if (!inRange(r, _1n, secp256k1_CURVE.p)) return false;
|
|
218
227
|
const s = num(sig.subarray(32, 64)); // Let s = int(sig[32:64]); fail if s ≥ n.
|
|
219
|
-
if (!inRange(s, _1n,
|
|
228
|
+
if (!inRange(s, _1n, secp256k1_CURVE.n)) return false;
|
|
220
229
|
const e = challenge(numTo32b(r), pointToBytes(P), m); // int(challenge(bytes(r)||bytes(P)||m))%n
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
230
|
+
// R = s⋅G - e⋅P, where -eP == (n-e)P
|
|
231
|
+
const R = Point.BASE.multiplyUnsafe(s).add(P.multiplyUnsafe(modN(-e)));
|
|
232
|
+
const { x, y } = R.toAffine();
|
|
233
|
+
// Fail if is_infinite(R) / not has_even_y(R) / x(R) ≠ r.
|
|
234
|
+
if (R.is0() || !hasEven(y) || x !== r) return false;
|
|
235
|
+
return true;
|
|
224
236
|
} catch (error) {
|
|
225
237
|
return false;
|
|
226
238
|
}
|
|
@@ -308,9 +320,9 @@ const mapSWU = /* @__PURE__ */ (() =>
|
|
|
308
320
|
Z: Fpk1.create(BigInt('-11')),
|
|
309
321
|
}))();
|
|
310
322
|
/** Hashing / encoding to secp256k1 points / field. RFC 9380 methods. */
|
|
311
|
-
export const secp256k1_hasher:
|
|
323
|
+
export const secp256k1_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() =>
|
|
312
324
|
createHasher(
|
|
313
|
-
secp256k1.
|
|
325
|
+
secp256k1.Point,
|
|
314
326
|
(scalars: bigint[]) => {
|
|
315
327
|
const { x, y } = mapSWU(Fpk1.create(scalars[0]));
|
|
316
328
|
return isoMap(x, y);
|
|
@@ -323,11 +335,11 @@ export const secp256k1_hasher: Hasher<bigint> = /* @__PURE__ */ (() =>
|
|
|
323
335
|
k: 128,
|
|
324
336
|
expand: 'xmd',
|
|
325
337
|
hash: sha256,
|
|
326
|
-
}
|
|
338
|
+
}
|
|
327
339
|
))();
|
|
328
340
|
|
|
329
|
-
export const hashToCurve:
|
|
341
|
+
export const hashToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() =>
|
|
330
342
|
secp256k1_hasher.hashToCurve)();
|
|
331
343
|
|
|
332
|
-
export const encodeToCurve:
|
|
344
|
+
export const encodeToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() =>
|
|
333
345
|
secp256k1_hasher.encodeToCurve)();
|