@decryption/curves 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +75 -0
- package/abstract/bls.d.ts +410 -0
- package/abstract/bls.js +433 -0
- package/abstract/curve.d.ts +441 -0
- package/abstract/curve.js +622 -0
- package/abstract/edwards.d.ts +319 -0
- package/abstract/edwards.js +697 -0
- package/abstract/fft.d.ts +438 -0
- package/abstract/fft.js +580 -0
- package/abstract/frost.d.ts +292 -0
- package/abstract/frost.js +705 -0
- package/abstract/hash-to-curve.d.ts +238 -0
- package/abstract/hash-to-curve.js +353 -0
- package/abstract/modular.d.ts +550 -0
- package/abstract/modular.js +872 -0
- package/abstract/montgomery.d.ts +95 -0
- package/abstract/montgomery.js +180 -0
- package/abstract/oprf.d.ts +354 -0
- package/abstract/oprf.js +358 -0
- package/abstract/poseidon.d.ts +198 -0
- package/abstract/poseidon.js +471 -0
- package/abstract/tower.d.ts +213 -0
- package/abstract/tower.js +963 -0
- package/abstract/weierstrass.d.ts +587 -0
- package/abstract/weierstrass.js +1421 -0
- package/bls12-381.d.ts +23 -0
- package/bls12-381.js +680 -0
- package/bn254.d.ts +74 -0
- package/bn254.js +248 -0
- package/ed25519.d.ts +211 -0
- package/ed25519.js +626 -0
- package/ed448.d.ts +184 -0
- package/ed448.js +596 -0
- package/esm/abstract/bls.js +429 -0
- package/esm/abstract/curve.js +610 -0
- package/esm/abstract/edwards.js +691 -0
- package/esm/abstract/fft.js +567 -0
- package/esm/abstract/frost.js +702 -0
- package/esm/abstract/hash-to-curve.js +345 -0
- package/esm/abstract/modular.js +849 -0
- package/esm/abstract/montgomery.js +177 -0
- package/esm/abstract/oprf.js +355 -0
- package/esm/abstract/poseidon.js +462 -0
- package/esm/abstract/tower.js +925 -0
- package/esm/abstract/weierstrass.js +1411 -0
- package/esm/bls12-381.js +677 -0
- package/esm/bn254.js +244 -0
- package/esm/ed25519.js +622 -0
- package/esm/ed448.js +593 -0
- package/esm/index.js +38 -0
- package/esm/misc.js +211 -0
- package/esm/nist.js +266 -0
- package/esm/secp256k1.js +465 -0
- package/esm/utils.js +597 -0
- package/esm/webcrypto.js +362 -0
- package/index.d.ts +0 -0
- package/index.js +38 -0
- package/misc.d.ts +113 -0
- package/misc.js +216 -0
- package/nist.d.ts +136 -0
- package/nist.js +269 -0
- package/package.json +46 -0
- package/secp256k1.d.ts +152 -0
- package/secp256k1.js +468 -0
- package/utils.d.ts +535 -0
- package/utils.js +626 -0
- package/webcrypto.d.ts +240 -0
- package/webcrypto.js +365 -0
package/esm/misc.js
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Miscellaneous, rarely used curves.
|
|
3
|
+
* jubjub, babyjubjub, pallas, vesta.
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
7
|
+
import { blake512 } from '@decryption/hashes/blake1';
|
|
8
|
+
import { blake2s } from '@decryption/hashes/blake2';
|
|
9
|
+
import { sha256, sha384, sha512 } from '@decryption/hashes/sha2';
|
|
10
|
+
import { abytes, concatBytes } from '@decryption/hashes/utils';
|
|
11
|
+
import { eddsa, edwards, } from './abstract/edwards';
|
|
12
|
+
import { ecdsa, weierstrass } from './abstract/weierstrass';
|
|
13
|
+
import { asciiToBytes } from './utils';
|
|
14
|
+
// Jubjub curves have 𝔽p over scalar fields of other curves. They are friendly to ZK proofs.
|
|
15
|
+
// Zcash Protocol Specification "Jubjub" parameters:
|
|
16
|
+
// q = BLS12-381 Fr, r, h = 8, a = -1, d = -10240/10241.
|
|
17
|
+
// Gx/Gy keep the canonical Jubjub base point used by Zcash implementations.
|
|
18
|
+
const jubjub_CURVE = /* @__PURE__ */ (() => ({
|
|
19
|
+
p: BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001'),
|
|
20
|
+
n: BigInt('0xe7db4ea6533afa906673b0101343b00a6682093ccc81082d0970e5ed6f72cb7'),
|
|
21
|
+
h: BigInt(8),
|
|
22
|
+
a: BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000000'),
|
|
23
|
+
d: BigInt('0x2a9318e74bfa2b48f5fd9207e6bd7fd4292d7f6d37579d2601065fd6d6343eb1'),
|
|
24
|
+
Gx: BigInt('0x11dafe5d23e1218086a365b99fbf3d3be72f6afd7d1f72623e6b071492d1122b'),
|
|
25
|
+
Gy: BigInt('0x1d523cf1ddab1a1793132e78c866c0c33e26ba5cc220fed7cc3f870e59d292aa'),
|
|
26
|
+
}))();
|
|
27
|
+
/**
|
|
28
|
+
* Generic EdDSA-over-Jubjub convenience wrapper with `sha512`.
|
|
29
|
+
* This is not the Zcash RedJubjub / Sapling signature scheme.
|
|
30
|
+
* @example
|
|
31
|
+
* Generate one Jubjub keypair, sign a message, and verify it.
|
|
32
|
+
*
|
|
33
|
+
* ```ts
|
|
34
|
+
* const { secretKey, publicKey } = jubjub.keygen();
|
|
35
|
+
* const msg = new TextEncoder().encode('hello noble');
|
|
36
|
+
* const sig = jubjub.sign(msg, secretKey);
|
|
37
|
+
* const isValid = jubjub.verify(sig, msg, publicKey);
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export const jubjub = /* @__PURE__ */ (() => eddsa(edwards(jubjub_CURVE), sha512))();
|
|
41
|
+
// BabyJubJub over bn254 Fr. EIP-2494 explicitly defines both the full-group generator G and the
|
|
42
|
+
// prime-order subgroup base point B = 8*G.
|
|
43
|
+
// noble's Edwards abstraction expects Point.BASE / curve.n to describe the prime-order subgroup, so
|
|
44
|
+
// use the EIP base point B here.
|
|
45
|
+
// Historical noble incorrectly used the EIP generator G as Point.BASE, which mismatched the
|
|
46
|
+
// abstraction and leaked the wrong order into consumers.
|
|
47
|
+
// Historical noble used G instead:
|
|
48
|
+
// Gx = 995203441582195749578291179787384436505546430278305826713579947235728471134
|
|
49
|
+
// Gy = 5472060717959818805561601436314318772137091100104008585924551046643952123905
|
|
50
|
+
const babyjubjub_CURVE = /* @__PURE__ */ (() => ({
|
|
51
|
+
p: BigInt('0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001'),
|
|
52
|
+
n: BigInt('0x060c89ce5c263405370a08b6d0302b0bab3eedb83920ee0a677297dc392126f1'),
|
|
53
|
+
h: BigInt(8),
|
|
54
|
+
a: BigInt('168700'),
|
|
55
|
+
d: BigInt('168696'),
|
|
56
|
+
Gx: BigInt('0xbb77a6ad63e739b4eacb2e09d6277c12ab8d8010534e0b62893f3f6bb957051'),
|
|
57
|
+
Gy: BigInt('0x25797203f7a0b24925572e1cd16bf9edfce0051fb9e133774b3c257a872d7d8b'),
|
|
58
|
+
}))();
|
|
59
|
+
/**
|
|
60
|
+
* Curve over scalar field of bn254. babyjubjub Fp = bn254 n
|
|
61
|
+
* This is a working generic EdDSA-over-BabyJubJub wrapper that uses `blake512` for the 64-byte
|
|
62
|
+
* secret expansion required by the shared EdDSA helper.
|
|
63
|
+
* It is not the BabyJubJub stack used by iden3/circomlib, `babyjubjub-rs`, or
|
|
64
|
+
* `@zk-kit/eddsa-poseidon`: those pair the subgroup base B/B8 with Blake-style secret expansion
|
|
65
|
+
* plus dedicated Poseidon / MiMC / Pedersen transcript hashing. This wrapper stays generic and is
|
|
66
|
+
* not meant as an interoperability target for those BabyJubJub signing stacks.
|
|
67
|
+
* @example
|
|
68
|
+
* Access the BabyJubJub base point and round-trip it through the point codec.
|
|
69
|
+
*
|
|
70
|
+
* ```ts
|
|
71
|
+
* import { babyjubjub } from './misc';
|
|
72
|
+
* const base = babyjubjub.Point.BASE;
|
|
73
|
+
* const encoded = base.toBytes();
|
|
74
|
+
* const decoded = babyjubjub.Point.fromBytes(encoded);
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export const babyjubjub = /* @__PURE__ */ (() => eddsa(edwards(babyjubjub_CURVE), blake512))();
|
|
78
|
+
// Sapling URS randomness beacon from the Zcash protocol. This stays as the 64-byte ASCII
|
|
79
|
+
// lowercase-hex string used for the first Blake2s block, not 32 raw bytes.
|
|
80
|
+
const jubjub_gh_first_block = /* @__PURE__ */ asciiToBytes('096b36a5804bfacef1691e173c366a47ff5ba84a44f26ddd7e8d9f79d5b42df0');
|
|
81
|
+
/**
|
|
82
|
+
* @param tag - Hash input.
|
|
83
|
+
* @param personalization - BLAKE2 personalization bytes.
|
|
84
|
+
* @returns Prime-order Jubjub point.
|
|
85
|
+
* @throws If the digest does not decode to a Jubjub point, or if the
|
|
86
|
+
* cofactor-cleared point has small order. {@link Error}
|
|
87
|
+
* @example
|
|
88
|
+
* Hash a tag into a prime-order Jubjub point.
|
|
89
|
+
*
|
|
90
|
+
* ```ts
|
|
91
|
+
* import { jubjub_groupHash } from './misc';
|
|
92
|
+
* import { asciiToBytes } from './utils';
|
|
93
|
+
* const tag = Uint8Array.of(2);
|
|
94
|
+
* const personalization = asciiToBytes('Zcash_G_');
|
|
95
|
+
* const point = jubjub_groupHash(tag, personalization);
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
export function jubjub_groupHash(tag, personalization) {
|
|
99
|
+
const h = blake2s.create({ personalization, dkLen: 32 });
|
|
100
|
+
h.update(jubjub_gh_first_block);
|
|
101
|
+
h.update(tag);
|
|
102
|
+
// NOTE: returns EdwardsPoint, in case it will be multiplied later
|
|
103
|
+
let p = jubjub.Point.fromBytes(h.digest());
|
|
104
|
+
// NOTE: cannot replace with isSmallOrder, returns Point*8
|
|
105
|
+
p = p.multiply(jubjub_CURVE.h);
|
|
106
|
+
if (p.equals(jubjub.Point.ZERO))
|
|
107
|
+
throw new Error('Point has small order');
|
|
108
|
+
return p;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* No secret data is leaked here at all.
|
|
112
|
+
* It operates over public data.
|
|
113
|
+
* @param m - Message prefix.
|
|
114
|
+
* @param personalization - 8-byte BLAKE2 personalization bytes.
|
|
115
|
+
* @returns First non-zero group hash.
|
|
116
|
+
* @throws If the personalization is invalid, or if no non-zero Jubjub group
|
|
117
|
+
* hash can be found. {@link Error}
|
|
118
|
+
* @example
|
|
119
|
+
* Derive the first non-zero Jubjub group hash for one personalization tag.
|
|
120
|
+
*
|
|
121
|
+
* ```ts
|
|
122
|
+
* import { jubjub_findGroupHash } from './misc';
|
|
123
|
+
* import { asciiToBytes } from './utils';
|
|
124
|
+
* const msg = Uint8Array.of();
|
|
125
|
+
* const personalization = asciiToBytes('Zcash_G_');
|
|
126
|
+
* const point = jubjub_findGroupHash(msg, personalization);
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
export function jubjub_findGroupHash(m, personalization) {
|
|
130
|
+
// Validate BLAKE2s personalization once up front; otherwise the retry loop swallows the real
|
|
131
|
+
// input error and turns it into a misleading "tag overflow".
|
|
132
|
+
abytes(personalization, 8, 'personalization');
|
|
133
|
+
const tag = concatBytes(m, Uint8Array.of(0));
|
|
134
|
+
const hashes = [];
|
|
135
|
+
for (let i = 0; i < 256; i++) {
|
|
136
|
+
tag[tag.length - 1] = i;
|
|
137
|
+
try {
|
|
138
|
+
hashes.push(jubjub_groupHash(tag, personalization));
|
|
139
|
+
}
|
|
140
|
+
catch (e) { }
|
|
141
|
+
}
|
|
142
|
+
if (!hashes.length)
|
|
143
|
+
throw new Error('findGroupHash tag overflow');
|
|
144
|
+
return hashes[0];
|
|
145
|
+
}
|
|
146
|
+
const brainpoolP256r1_CURVE = /* @__PURE__ */ (() => ({
|
|
147
|
+
p: BigInt('0xa9fb57dba1eea9bc3e660a909d838d726e3bf623d52620282013481d1f6e5377'),
|
|
148
|
+
a: BigInt('0x7d5a0975fc2c3057eef67530417affe7fb8055c126dc5c6ce94a4b44f330b5d9'),
|
|
149
|
+
b: BigInt('0x26dc5c6ce94a4b44f330b5d9bbd77cbf958416295cf7e1ce6bccdc18ff8c07b6'),
|
|
150
|
+
n: BigInt('0xa9fb57dba1eea9bc3e660a909d838d718c397aa3b561a6f7901e0e82974856a7'),
|
|
151
|
+
Gx: BigInt('0x8bd2aeb9cb7e57cb2c4b482ffc81b7afb9de27e1e3bd23c23a4453bd9ace3262'),
|
|
152
|
+
Gy: BigInt('0x547ef835c3dac4fd97f8461a14611dc9c27745132ded8e545c1d54c72f046997'),
|
|
153
|
+
h: BigInt(1),
|
|
154
|
+
}))();
|
|
155
|
+
/**
|
|
156
|
+
* Brainpool P256r1 with sha256, from RFC 5639.
|
|
157
|
+
* @example
|
|
158
|
+
* Generate one Brainpool P256r1 keypair, sign a message, and verify it.
|
|
159
|
+
*
|
|
160
|
+
* ```ts
|
|
161
|
+
* const { secretKey, publicKey } = brainpoolP256r1.keygen();
|
|
162
|
+
* const msg = new TextEncoder().encode('hello noble');
|
|
163
|
+
* const sig = brainpoolP256r1.sign(msg, secretKey);
|
|
164
|
+
* const isValid = brainpoolP256r1.verify(sig, msg, publicKey);
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
export const brainpoolP256r1 = /* @__PURE__ */ (() => ecdsa(weierstrass(brainpoolP256r1_CURVE), sha256))();
|
|
168
|
+
const brainpoolP384r1_CURVE = /* @__PURE__ */ (() => ({
|
|
169
|
+
p: BigInt('0x8cb91e82a3386d280f5d6f7e50e641df152f7109ed5456b412b1da197fb71123acd3a729901d1a71874700133107ec53'),
|
|
170
|
+
a: BigInt('0x7bc382c63d8c150c3c72080ace05afa0c2bea28e4fb22787139165efba91f90f8aa5814a503ad4eb04a8c7dd22ce2826'),
|
|
171
|
+
b: BigInt('0x04a8c7dd22ce28268b39b55416f0447c2fb77de107dcd2a62e880ea53eeb62d57cb4390295dbc9943ab78696fa504c11'),
|
|
172
|
+
n: BigInt('0x8cb91e82a3386d280f5d6f7e50e641df152f7109ed5456b31f166e6cac0425a7cf3ab6af6b7fc3103b883202e9046565'),
|
|
173
|
+
Gx: BigInt('0x1d1c64f068cf45ffa2a63a81b7c13f6b8847a3e77ef14fe3db7fcafe0cbd10e8e826e03436d646aaef87b2e247d4af1e'),
|
|
174
|
+
Gy: BigInt('0x8abe1d7520f9c2a45cb1eb8e95cfd55262b70b29feec5864e19c054ff99129280e4646217791811142820341263c5315'),
|
|
175
|
+
h: BigInt(1),
|
|
176
|
+
}))();
|
|
177
|
+
/**
|
|
178
|
+
* Brainpool P384r1 with sha384, from RFC 5639.
|
|
179
|
+
* @example
|
|
180
|
+
* Generate one Brainpool P384r1 keypair, sign a message, and verify it.
|
|
181
|
+
*
|
|
182
|
+
* ```ts
|
|
183
|
+
* const { secretKey, publicKey } = brainpoolP384r1.keygen();
|
|
184
|
+
* const msg = new TextEncoder().encode('hello noble');
|
|
185
|
+
* const sig = brainpoolP384r1.sign(msg, secretKey);
|
|
186
|
+
* const isValid = brainpoolP384r1.verify(sig, msg, publicKey);
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
export const brainpoolP384r1 = /* @__PURE__ */ (() => ecdsa(weierstrass(brainpoolP384r1_CURVE), sha384))();
|
|
190
|
+
const brainpoolP512r1_CURVE = /* @__PURE__ */ (() => ({
|
|
191
|
+
p: BigInt('0xaadd9db8dbe9c48b3fd4e6ae33c9fc07cb308db3b3c9d20ed6639cca703308717d4d9b009bc66842aecda12ae6a380e62881ff2f2d82c68528aa6056583a48f3'),
|
|
192
|
+
a: BigInt('0x7830a3318b603b89e2327145ac234cc594cbdd8d3df91610a83441caea9863bc2ded5d5aa8253aa10a2ef1c98b9ac8b57f1117a72bf2c7b9e7c1ac4d77fc94ca'),
|
|
193
|
+
b: BigInt('0x3df91610a83441caea9863bc2ded5d5aa8253aa10a2ef1c98b9ac8b57f1117a72bf2c7b9e7c1ac4d77fc94cadc083e67984050b75ebae5dd2809bd638016f723'),
|
|
194
|
+
n: BigInt('0xaadd9db8dbe9c48b3fd4e6ae33c9fc07cb308db3b3c9d20ed6639cca70330870553e5c414ca92619418661197fac10471db1d381085ddaddb58796829ca90069'),
|
|
195
|
+
Gx: BigInt('0x81aee4bdd82ed9645a21322e9c4c6a9385ed9f70b5d916c1b43b62eef4d0098eff3b1f78e2d0d48d50d1687b93b97d5f7c6d5047406a5e688b352209bcb9f822'),
|
|
196
|
+
Gy: BigInt('0x7dde385d566332ecc0eabfa9cf7822fdf209f70024a57b1aa000c55b881f8111b2dcde494a5f485e5bca4bd88a2763aed1ca2b2fa8f0540678cd1e0f3ad80892'),
|
|
197
|
+
h: BigInt(1),
|
|
198
|
+
}))();
|
|
199
|
+
/**
|
|
200
|
+
* Brainpool P512r1 with sha512, from RFC 5639.
|
|
201
|
+
* @example
|
|
202
|
+
* Generate one Brainpool P512r1 keypair, sign a message, and verify it.
|
|
203
|
+
*
|
|
204
|
+
* ```ts
|
|
205
|
+
* const { secretKey, publicKey } = brainpoolP512r1.keygen();
|
|
206
|
+
* const msg = new TextEncoder().encode('hello noble');
|
|
207
|
+
* const sig = brainpoolP512r1.sign(msg, secretKey);
|
|
208
|
+
* const isValid = brainpoolP512r1.verify(sig, msg, publicKey);
|
|
209
|
+
* ```
|
|
210
|
+
*/
|
|
211
|
+
export const brainpoolP512r1 = /* @__PURE__ */ (() => ecdsa(weierstrass(brainpoolP512r1_CURVE), sha512))();
|
package/esm/nist.js
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal module for NIST P256, P384, P521 curves.
|
|
3
|
+
* Do not use for now.
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
7
|
+
import { sha256, sha384, sha512 } from '@decryption/hashes/sha2';
|
|
8
|
+
import { createFROST } from './abstract/frost';
|
|
9
|
+
import { createHasher } from './abstract/hash-to-curve';
|
|
10
|
+
import { createOPRF } from './abstract/oprf';
|
|
11
|
+
import { ecdsa, mapToCurveSimpleSWU, weierstrass, } from './abstract/weierstrass';
|
|
12
|
+
// p = 2n**224n * (2n**32n-1n) + 2n**192n + 2n**96n - 1n
|
|
13
|
+
// a = Fp256.create(BigInt('-3'));
|
|
14
|
+
const p256_CURVE = /* @__PURE__ */ (() => ({
|
|
15
|
+
p: BigInt('0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff'),
|
|
16
|
+
n: BigInt('0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551'),
|
|
17
|
+
h: BigInt(1),
|
|
18
|
+
a: BigInt('0xffffffff00000001000000000000000000000000fffffffffffffffffffffffc'),
|
|
19
|
+
b: BigInt('0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b'),
|
|
20
|
+
Gx: BigInt('0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296'),
|
|
21
|
+
Gy: BigInt('0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5'),
|
|
22
|
+
}))();
|
|
23
|
+
// p = 2n**384n - 2n**128n - 2n**96n + 2n**32n - 1n
|
|
24
|
+
const p384_CURVE = /* @__PURE__ */ (() => ({
|
|
25
|
+
p: BigInt('0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff'),
|
|
26
|
+
n: BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973'),
|
|
27
|
+
h: BigInt(1),
|
|
28
|
+
a: BigInt('0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000fffffffc'),
|
|
29
|
+
b: BigInt('0xb3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef'),
|
|
30
|
+
Gx: BigInt('0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7'),
|
|
31
|
+
Gy: BigInt('0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f'),
|
|
32
|
+
}))();
|
|
33
|
+
// p = 2n**521n - 1n
|
|
34
|
+
const p521_CURVE = /* @__PURE__ */ (() => ({
|
|
35
|
+
p: BigInt('0x1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'),
|
|
36
|
+
n: BigInt('0x01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409'),
|
|
37
|
+
h: BigInt(1),
|
|
38
|
+
a: BigInt('0x1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc'),
|
|
39
|
+
b: BigInt('0x0051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00'),
|
|
40
|
+
Gx: BigInt('0x00c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66'),
|
|
41
|
+
Gy: BigInt('0x011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650'),
|
|
42
|
+
}))();
|
|
43
|
+
function createSWU(Point, opts) {
|
|
44
|
+
let map;
|
|
45
|
+
// RFC 9380's NIST suites here all use m = 1, so createHasher passes one field element per map.
|
|
46
|
+
// Building the SWU sqrt-ratio helper eagerly adds noticeable `nist.js` import cost, so defer it
|
|
47
|
+
// to first use; after that the cached mapper is reused directly.
|
|
48
|
+
return (scalars) => (map || (map = mapToCurveSimpleSWU(Point.Fp, opts)))(scalars[0]);
|
|
49
|
+
}
|
|
50
|
+
// NIST P256
|
|
51
|
+
const p256_Point = /* @__PURE__ */ weierstrass(p256_CURVE);
|
|
52
|
+
/**
|
|
53
|
+
* NIST P256 (aka secp256r1, prime256v1) curve, ECDSA and ECDH methods.
|
|
54
|
+
* Hashes inputs with sha256 by default.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* Generate one P-256 keypair, sign a message, and verify it.
|
|
58
|
+
*
|
|
59
|
+
* ```js
|
|
60
|
+
* import { p256 } from './nist';
|
|
61
|
+
* const { secretKey, publicKey } = p256.keygen();
|
|
62
|
+
* // const publicKey = p256.getPublicKey(secretKey);
|
|
63
|
+
* const msg = new TextEncoder().encode('hello noble');
|
|
64
|
+
* const sig = p256.sign(msg, secretKey);
|
|
65
|
+
* const isValid = p256.verify(sig, msg, publicKey);
|
|
66
|
+
* // const sigKeccak = p256.sign(keccak256(msg), secretKey, { prehash: false });
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export const p256 = /* @__PURE__ */ ecdsa(p256_Point, sha256);
|
|
70
|
+
/**
|
|
71
|
+
* Hashing / encoding to p256 points / field. RFC 9380 methods.
|
|
72
|
+
* @example
|
|
73
|
+
* Hash one message onto the P-256 curve.
|
|
74
|
+
*
|
|
75
|
+
* ```ts
|
|
76
|
+
* const point = p256_hasher.hashToCurve(new TextEncoder().encode('hello noble'));
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export const p256_hasher = /* @__PURE__ */ (() => {
|
|
80
|
+
return createHasher(p256_Point, createSWU(p256_Point, {
|
|
81
|
+
A: p256_CURVE.a,
|
|
82
|
+
B: p256_CURVE.b,
|
|
83
|
+
Z: p256_Point.Fp.create(BigInt('-10')),
|
|
84
|
+
}), {
|
|
85
|
+
DST: 'P256_XMD:SHA-256_SSWU_RO_',
|
|
86
|
+
encodeDST: 'P256_XMD:SHA-256_SSWU_NU_',
|
|
87
|
+
p: p256_CURVE.p,
|
|
88
|
+
m: 1,
|
|
89
|
+
k: 128,
|
|
90
|
+
expand: 'xmd',
|
|
91
|
+
hash: sha256,
|
|
92
|
+
});
|
|
93
|
+
})();
|
|
94
|
+
/**
|
|
95
|
+
* p256 OPRF, defined in RFC 9497.
|
|
96
|
+
* @example
|
|
97
|
+
* Run one blind/evaluate/finalize OPRF round over P-256.
|
|
98
|
+
*
|
|
99
|
+
* ```ts
|
|
100
|
+
* const input = new TextEncoder().encode('hello noble');
|
|
101
|
+
* const keys = p256_oprf.oprf.generateKeyPair();
|
|
102
|
+
* const blind = p256_oprf.oprf.blind(input);
|
|
103
|
+
* const evaluated = p256_oprf.oprf.blindEvaluate(keys.secretKey, blind.blinded);
|
|
104
|
+
* const output = p256_oprf.oprf.finalize(input, blind.blind, evaluated);
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
export const p256_oprf = /* @__PURE__ */ (() => createOPRF({
|
|
108
|
+
name: 'P256-SHA256',
|
|
109
|
+
Point: p256_Point,
|
|
110
|
+
hash: sha256,
|
|
111
|
+
hashToGroup: p256_hasher.hashToCurve,
|
|
112
|
+
hashToScalar: p256_hasher.hashToScalar,
|
|
113
|
+
}))();
|
|
114
|
+
/**
|
|
115
|
+
* FROST threshold signatures over p256. RFC 9591.
|
|
116
|
+
* @example
|
|
117
|
+
* Create one trusted-dealer package for 2-of-3 p256 signing.
|
|
118
|
+
*
|
|
119
|
+
* ```ts
|
|
120
|
+
* const alice = p256_FROST.Identifier.derive('alice@example.com');
|
|
121
|
+
* const bob = p256_FROST.Identifier.derive('bob@example.com');
|
|
122
|
+
* const carol = p256_FROST.Identifier.derive('carol@example.com');
|
|
123
|
+
* const deal = p256_FROST.trustedDealer({ min: 2, max: 3 }, [alice, bob, carol]);
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
export const p256_FROST = /* @__PURE__ */ (() => createFROST({
|
|
127
|
+
name: 'FROST-P256-SHA256-v1',
|
|
128
|
+
Point: p256_Point,
|
|
129
|
+
hashToScalar: p256_hasher.hashToScalar,
|
|
130
|
+
hash: sha256,
|
|
131
|
+
}))();
|
|
132
|
+
// NIST P384
|
|
133
|
+
const p384_Point = /* @__PURE__ */ weierstrass(p384_CURVE);
|
|
134
|
+
/**
|
|
135
|
+
* NIST P384 (aka secp384r1) curve, ECDSA and ECDH methods. Hashes inputs with sha384 by default.
|
|
136
|
+
* @example
|
|
137
|
+
* Generate one P-384 keypair, sign a message, and verify it.
|
|
138
|
+
*
|
|
139
|
+
* ```ts
|
|
140
|
+
* const { secretKey, publicKey } = p384.keygen();
|
|
141
|
+
* const msg = new TextEncoder().encode('hello noble');
|
|
142
|
+
* const sig = p384.sign(msg, secretKey);
|
|
143
|
+
* const isValid = p384.verify(sig, msg, publicKey);
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
export const p384 = /* @__PURE__ */ ecdsa(p384_Point, sha384);
|
|
147
|
+
/**
|
|
148
|
+
* Hashing / encoding to p384 points / field. RFC 9380 methods.
|
|
149
|
+
* @example
|
|
150
|
+
* Hash one message onto the P-384 curve.
|
|
151
|
+
*
|
|
152
|
+
* ```ts
|
|
153
|
+
* const point = p384_hasher.hashToCurve(new TextEncoder().encode('hello noble'));
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
export const p384_hasher = /* @__PURE__ */ (() => {
|
|
157
|
+
return createHasher(p384_Point, createSWU(p384_Point, {
|
|
158
|
+
A: p384_CURVE.a,
|
|
159
|
+
B: p384_CURVE.b,
|
|
160
|
+
Z: p384_Point.Fp.create(BigInt('-12')),
|
|
161
|
+
}), {
|
|
162
|
+
DST: 'P384_XMD:SHA-384_SSWU_RO_',
|
|
163
|
+
encodeDST: 'P384_XMD:SHA-384_SSWU_NU_',
|
|
164
|
+
p: p384_CURVE.p,
|
|
165
|
+
m: 1,
|
|
166
|
+
k: 192,
|
|
167
|
+
expand: 'xmd',
|
|
168
|
+
hash: sha384,
|
|
169
|
+
});
|
|
170
|
+
})();
|
|
171
|
+
/**
|
|
172
|
+
* p384 OPRF, defined in RFC 9497.
|
|
173
|
+
* @example
|
|
174
|
+
* Run one blind/evaluate/finalize OPRF round over P-384.
|
|
175
|
+
*
|
|
176
|
+
* ```ts
|
|
177
|
+
* const input = new TextEncoder().encode('hello noble');
|
|
178
|
+
* const keys = p384_oprf.oprf.generateKeyPair();
|
|
179
|
+
* const blind = p384_oprf.oprf.blind(input);
|
|
180
|
+
* const evaluated = p384_oprf.oprf.blindEvaluate(keys.secretKey, blind.blinded);
|
|
181
|
+
* const output = p384_oprf.oprf.finalize(input, blind.blind, evaluated);
|
|
182
|
+
* ```
|
|
183
|
+
*/
|
|
184
|
+
export const p384_oprf = /* @__PURE__ */ (() => createOPRF({
|
|
185
|
+
name: 'P384-SHA384',
|
|
186
|
+
Point: p384_Point,
|
|
187
|
+
hash: sha384,
|
|
188
|
+
hashToGroup: p384_hasher.hashToCurve,
|
|
189
|
+
hashToScalar: p384_hasher.hashToScalar,
|
|
190
|
+
}))();
|
|
191
|
+
// NIST P521
|
|
192
|
+
// RFC 7518 fixes the canonical JWK/JOSE width at 66 bytes:
|
|
193
|
+
// - Section 3.4 says ECDSA octet strings must not omit leading zero octets
|
|
194
|
+
// - Sections 6.2.1.2/6.2.1.3 say P-521 coordinates "x"/"y" must be 66 octets
|
|
195
|
+
// - Section 6.2.2.1 says private scalar "d" must be ceil(log2(n)/8) octets, i.e. 66 for P-521
|
|
196
|
+
// NIST FIPS 186-5 Appendix A.3.3 also routes deterministic ECDSA private keys through Appendix
|
|
197
|
+
// B.2.3, whose Integer-to-Octet-String output has explicit fixed length L; for P-521 that is the
|
|
198
|
+
// same 66-byte order width.
|
|
199
|
+
// RFC 6979 matches that width too: private key x is an integer, while `int2octets(x)` uses
|
|
200
|
+
// rlen = 8 * ceil(qlen/8); for P-521, qlen = 521 so the canonical octet width is 66 bytes.
|
|
201
|
+
// Wycheproof ECDH stores private values as integers, not fixed-width scalar bytes, so it does not
|
|
202
|
+
// require a dedicated 65-byte parser path; the repo tests now normalize those integer fixtures to
|
|
203
|
+
// the canonical 66-byte width before use. There is no good standards or oracle reason to accept
|
|
204
|
+
// exactly 65 bytes here: the coherent choices are canonical 66 only, or a broader integer-style
|
|
205
|
+
// parser across many widths. Since this field parser is fixed-width, keep it canonical and use the
|
|
206
|
+
// default exact-66-byte scalar field path.
|
|
207
|
+
const p521_Point = /* @__PURE__ */ weierstrass(p521_CURVE);
|
|
208
|
+
/**
|
|
209
|
+
* NIST P521 (aka secp521r1) curve, ECDSA and ECDH methods. Hashes inputs with sha512 by default.
|
|
210
|
+
* Deterministic `keygen(seed)` expects 99 seed bytes here because the generic scalar-derivation
|
|
211
|
+
* helper uses `getMinHashLength(n)`, not the 66-byte canonical secret-key width.
|
|
212
|
+
* @example
|
|
213
|
+
* Generate one P-521 keypair, sign a message, and verify it.
|
|
214
|
+
*
|
|
215
|
+
* ```ts
|
|
216
|
+
* const { secretKey, publicKey } = p521.keygen();
|
|
217
|
+
* const msg = new TextEncoder().encode('hello noble');
|
|
218
|
+
* const sig = p521.sign(msg, secretKey);
|
|
219
|
+
* const isValid = p521.verify(sig, msg, publicKey);
|
|
220
|
+
* ```
|
|
221
|
+
*/
|
|
222
|
+
export const p521 = /* @__PURE__ */ ecdsa(p521_Point, sha512);
|
|
223
|
+
/**
|
|
224
|
+
* Hashing / encoding to p521 points / field. RFC 9380 methods.
|
|
225
|
+
* @example
|
|
226
|
+
* Hash one message onto the P-521 curve.
|
|
227
|
+
*
|
|
228
|
+
* ```ts
|
|
229
|
+
* const point = p521_hasher.hashToCurve(new TextEncoder().encode('hello noble'));
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
232
|
+
export const p521_hasher = /* @__PURE__ */ (() => {
|
|
233
|
+
return createHasher(p521_Point, createSWU(p521_Point, {
|
|
234
|
+
A: p521_CURVE.a,
|
|
235
|
+
B: p521_CURVE.b,
|
|
236
|
+
Z: p521_Point.Fp.create(BigInt('-4')),
|
|
237
|
+
}), {
|
|
238
|
+
DST: 'P521_XMD:SHA-512_SSWU_RO_',
|
|
239
|
+
encodeDST: 'P521_XMD:SHA-512_SSWU_NU_',
|
|
240
|
+
p: p521_CURVE.p,
|
|
241
|
+
m: 1,
|
|
242
|
+
k: 256,
|
|
243
|
+
expand: 'xmd',
|
|
244
|
+
hash: sha512,
|
|
245
|
+
});
|
|
246
|
+
})();
|
|
247
|
+
/**
|
|
248
|
+
* p521 OPRF, defined in RFC 9497.
|
|
249
|
+
* @example
|
|
250
|
+
* Run one blind/evaluate/finalize OPRF round over P-521.
|
|
251
|
+
*
|
|
252
|
+
* ```ts
|
|
253
|
+
* const input = new TextEncoder().encode('hello noble');
|
|
254
|
+
* const keys = p521_oprf.oprf.generateKeyPair();
|
|
255
|
+
* const blind = p521_oprf.oprf.blind(input);
|
|
256
|
+
* const evaluated = p521_oprf.oprf.blindEvaluate(keys.secretKey, blind.blinded);
|
|
257
|
+
* const output = p521_oprf.oprf.finalize(input, blind.blind, evaluated);
|
|
258
|
+
* ```
|
|
259
|
+
*/
|
|
260
|
+
export const p521_oprf = /* @__PURE__ */ (() => createOPRF({
|
|
261
|
+
name: 'P521-SHA512',
|
|
262
|
+
Point: p521_Point,
|
|
263
|
+
hash: sha512,
|
|
264
|
+
hashToGroup: p521_hasher.hashToCurve,
|
|
265
|
+
hashToScalar: p521_hasher.hashToScalar, // produces L=98 just like in RFC
|
|
266
|
+
}))();
|