@noble/curves 0.6.0 → 0.6.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.
@@ -3,7 +3,7 @@ import { sha256 } from '@noble/hashes/sha256';
3
3
  import { Fp as Field, mod, pow2 } from './abstract/modular.js';
4
4
  import { createCurve } from './_shortw_utils.js';
5
5
  import { mapToCurveSimpleSWU } from './abstract/weierstrass.js';
6
- import { ensureBytes, concatBytes, bytesToNumberBE as bytesToNum, numberToBytesBE, } from './abstract/utils.js';
6
+ import { ensureBytes, concatBytes, bytesToNumberBE as bytesToInt, numberToBytesBE, } from './abstract/utils.js';
7
7
  import { randomBytes } from '@noble/hashes/utils';
8
8
  import * as htf from './abstract/hash-to-curve.js';
9
9
  /**
@@ -112,20 +112,17 @@ function taggedHash(tag, ...messages) {
112
112
  }
113
113
  return sha256(concatBytes(tagP, ...messages));
114
114
  }
115
- const toRawX = (point) => point.toRawBytes(true).slice(1);
115
+ const pointToBytes = (point) => point.toRawBytes(true).slice(1);
116
116
  const numTo32b = (n) => numberToBytesBE(n, 32);
117
117
  const modN = (x) => mod(x, secp256k1N);
118
- const _Point = secp256k1.ProjectivePoint;
119
- const Gmul = (priv) => _Point.fromPrivateKey(priv);
120
- const GmulAdd = (Q, a, b) => _Point.BASE.multiplyAndAddUnsafe(Q, a, b);
121
- function schnorrGetScalar(priv) {
122
- // Let d' = int(sk)
123
- // Fail if d' = 0 or d' n
124
- // Let P = d'⋅G
125
- // Let d = d' if has_even_y(P), otherwise let d = n - d' .
126
- const point = Gmul(priv);
127
- const scalar = point.hasEvenY() ? priv : modN(-priv);
128
- return { point, scalar, x: toRawX(point) };
118
+ const Point = secp256k1.ProjectivePoint;
119
+ const GmulAdd = (Q, a, b) => Point.BASE.multiplyAndAddUnsafe(Q, a, b);
120
+ const hex32ToInt = (key) => bytesToInt(ensureBytes(key, 32));
121
+ function schnorrGetExtPubKey(priv) {
122
+ let d = typeof priv === 'bigint' ? priv : hex32ToInt(priv);
123
+ const point = Point.fromPrivateKey(d); // P = d'⋅G; 0 < d' < n check is done inside
124
+ const scalar = point.hasEvenY() ? d : modN(-d); // d = d' if has_even_y(P), otherwise d = n-d'
125
+ return { point, scalar, bytes: pointToBytes(point) };
129
126
  }
130
127
  function lift_x(x) {
131
128
  if (!fe(x))
@@ -134,37 +131,31 @@ function lift_x(x) {
134
131
  let y = sqrtMod(c); // Let y = c^(p+1)/4 mod p.
135
132
  if (y % 2n !== 0n)
136
133
  y = mod(-y, secp256k1P); // Return the unique point P such that x(P) = x and
137
- const p = new _Point(x, y, _1n); // y(P) = y if y mod 2 = 0 or y(P) = p-y otherwise.
134
+ const p = new Point(x, y, _1n); // y(P) = y if y mod 2 = 0 or y(P) = p-y otherwise.
138
135
  p.assertValidity();
139
136
  return p;
140
137
  }
141
138
  function challenge(...args) {
142
- return modN(bytesToNum(taggedHash(TAGS.challenge, ...args)));
139
+ return modN(bytesToInt(taggedHash(TAGS.challenge, ...args)));
143
140
  }
141
+ // Schnorr's pubkey is just `x` of Point (BIP340)
144
142
  function schnorrGetPublicKey(privateKey) {
145
- return toRawX(Gmul(privateKey)); // Let d' = int(sk). Fail if d' = 0 or d' n. Return bytes(d'⋅G)
143
+ return schnorrGetExtPubKey(privateKey).bytes; // d'=int(sk). Fail if d'=0 or d'≥n. Ret bytes(d'⋅G)
146
144
  }
147
- /**
148
- * Synchronously creates Schnorr signature. Improved security: verifies itself before
149
- * producing an output.
150
- * @param msg message (not message hash)
151
- * @param privateKey private key
152
- * @param auxRand random bytes that would be added to k. Bad RNG won't break it.
153
- */
145
+ // Creates Schnorr signature as per BIP340. Verifies itself before returning anything.
146
+ // auxRand is optional and is not the sole source of k generation: bad CSPRNG won't be dangerous
154
147
  function schnorrSign(message, privateKey, auxRand = randomBytes(32)) {
155
148
  if (message == null)
156
149
  throw new Error(`sign: Expected valid message, not "${message}"`);
157
- const m = ensureBytes(message);
158
- // checks for isWithinCurveOrder
159
- const { x: px, scalar: d } = schnorrGetScalar(bytesToNum(ensureBytes(privateKey, 32)));
150
+ const m = ensureBytes(message); // checks for isWithinCurveOrder
151
+ const { bytes: px, scalar: d } = schnorrGetExtPubKey(privateKey);
160
152
  const a = ensureBytes(auxRand, 32); // Auxiliary random data a: a 32-byte array
161
- // TODO: replace with proper xor?
162
- const t = numTo32b(d ^ bytesToNum(taggedHash(TAGS.aux, a))); // Let t be the byte-wise xor of bytes(d) and hash/aux(a)
153
+ const t = numTo32b(d ^ bytesToInt(taggedHash(TAGS.aux, a))); // Let t be the byte-wise xor of bytes(d) and hash/aux(a)
163
154
  const rand = taggedHash(TAGS.nonce, t, px, m); // Let rand = hash/nonce(t || bytes(P) || m)
164
- const k_ = modN(bytesToNum(rand)); // Let k' = int(rand) mod n
155
+ const k_ = modN(bytesToInt(rand)); // Let k' = int(rand) mod n
165
156
  if (k_ === _0n)
166
157
  throw new Error('sign failed: k is zero'); // Fail if k' = 0.
167
- const { point: R, x: rx, scalar: k } = schnorrGetScalar(k_); // Let R = k'⋅G.
158
+ const { point: R, bytes: rx, scalar: k } = schnorrGetExtPubKey(k_); // Let R = k'⋅G.
168
159
  const e = challenge(rx, px, m); // Let e = int(hash/challenge(bytes(R) || bytes(P) || m)) mod n.
169
160
  const sig = new Uint8Array(64); // Let sig = bytes(R) || bytes((k + ed) mod n).
170
161
  sig.set(numTo32b(R.px), 0);
@@ -179,16 +170,16 @@ function schnorrSign(message, privateKey, auxRand = randomBytes(32)) {
179
170
  */
180
171
  function schnorrVerify(signature, message, publicKey) {
181
172
  try {
182
- const P = lift_x(bytesToNum(ensureBytes(publicKey, 32))); // P = lift_x(int(pk)); fail if that fails
173
+ const P = lift_x(hex32ToInt(publicKey)); // P = lift_x(int(pk)); fail if that fails
183
174
  const sig = ensureBytes(signature, 64);
184
- const r = bytesToNum(sig.subarray(0, 32)); // Let r = int(sig[0:32]); fail if r ≥ p.
175
+ const r = bytesToInt(sig.subarray(0, 32)); // Let r = int(sig[0:32]); fail if r ≥ p.
185
176
  if (!fe(r))
186
177
  return false;
187
- const s = bytesToNum(sig.subarray(32, 64)); // Let s = int(sig[32:64]); fail if s ≥ n.
178
+ const s = bytesToInt(sig.subarray(32, 64)); // Let s = int(sig[32:64]); fail if s ≥ n.
188
179
  if (!ge(s))
189
180
  return false;
190
181
  const m = ensureBytes(message);
191
- const e = challenge(numTo32b(r), toRawX(P), m); // int(challenge(bytes(r)||bytes(P)||m)) mod n
182
+ const e = challenge(numTo32b(r), pointToBytes(P), m); // int(challenge(bytes(r)||bytes(P)||m)) mod n
192
183
  const R = GmulAdd(P, s, modN(-e)); // R = s⋅G - e⋅P
193
184
  if (!R || !R.hasEvenY() || R.toAffine().x !== r)
194
185
  return false; // -eP == (n-e)P
@@ -199,11 +190,18 @@ function schnorrVerify(signature, message, publicKey) {
199
190
  }
200
191
  }
201
192
  export const schnorr = {
202
- // Schnorr's pubkey is just `x` of Point (BIP340)
203
193
  getPublicKey: schnorrGetPublicKey,
204
194
  sign: schnorrSign,
205
195
  verify: schnorrVerify,
206
- utils: { lift_x, int: bytesToNum, taggedHash },
196
+ utils: {
197
+ getExtendedPublicKey: schnorrGetExtPubKey,
198
+ lift_x,
199
+ pointToBytes,
200
+ numberToBytesBE,
201
+ bytesToNumberBE: bytesToInt,
202
+ taggedHash,
203
+ mod,
204
+ },
207
205
  };
208
206
  const isoMap = htf.isogenyMap(Fp, [
209
207
  // xNum
package/lib/esm/stark.js CHANGED
@@ -95,7 +95,7 @@ function ensureBytes0x(hex) {
95
95
  function normalizePrivateKey(privKey) {
96
96
  return cutils.bytesToHex(ensureBytes0x(privKey)).padStart(64, '0');
97
97
  }
98
- function getPublicKey0x(privKey, isCompressed) {
98
+ function getPublicKey0x(privKey, isCompressed = false) {
99
99
  return starkCurve.getPublicKey(normalizePrivateKey(privKey), isCompressed);
100
100
  }
101
101
  function getSharedSecret0x(privKeyA, pubKeyB) {
package/lib/p192.d.ts CHANGED
@@ -9,11 +9,11 @@ export declare const P192: Readonly<{
9
9
  readonly hEff?: bigint | undefined;
10
10
  readonly Gx: bigint;
11
11
  readonly Gy: bigint;
12
- readonly wrapPrivateKey?: boolean | undefined;
13
12
  readonly allowInfinityPoint?: boolean | undefined;
14
13
  readonly a: bigint;
15
14
  readonly b: bigint;
16
- readonly normalizePrivateKey?: ((key: import("./abstract/utils.js").PrivKey) => import("./abstract/utils.js").PrivKey) | undefined;
15
+ readonly allowedPrivateKeyLengths?: readonly number[] | undefined;
16
+ readonly wrapPrivateKey?: boolean | undefined;
17
17
  readonly endo?: {
18
18
  beta: bigint;
19
19
  splitScalar: (k: bigint) => {
@@ -25,10 +25,10 @@ export declare const P192: Readonly<{
25
25
  } | undefined;
26
26
  readonly isTorsionFree?: ((c: import("./abstract/weierstrass.js").ProjConstructor<bigint>, point: import("./abstract/weierstrass.js").ProjPointType<bigint>) => boolean) | undefined;
27
27
  readonly clearCofactor?: ((c: import("./abstract/weierstrass.js").ProjConstructor<bigint>, point: import("./abstract/weierstrass.js").ProjPointType<bigint>) => import("./abstract/weierstrass.js").ProjPointType<bigint>) | undefined;
28
- lowS: boolean;
29
28
  readonly hash: import("./abstract/utils.js").CHash;
30
29
  readonly hmac: (key: Uint8Array, ...messages: Uint8Array[]) => Uint8Array;
31
30
  readonly randomBytes: (bytesLength?: number | undefined) => Uint8Array;
31
+ lowS: boolean;
32
32
  readonly bits2int?: ((bytes: Uint8Array) => bigint) | undefined;
33
33
  readonly bits2int_modN?: ((bytes: Uint8Array) => bigint) | undefined;
34
34
  }>;
@@ -59,11 +59,11 @@ export declare const secp192r1: Readonly<{
59
59
  readonly hEff?: bigint | undefined;
60
60
  readonly Gx: bigint;
61
61
  readonly Gy: bigint;
62
- readonly wrapPrivateKey?: boolean | undefined;
63
62
  readonly allowInfinityPoint?: boolean | undefined;
64
63
  readonly a: bigint;
65
64
  readonly b: bigint;
66
- readonly normalizePrivateKey?: ((key: import("./abstract/utils.js").PrivKey) => import("./abstract/utils.js").PrivKey) | undefined;
65
+ readonly allowedPrivateKeyLengths?: readonly number[] | undefined;
66
+ readonly wrapPrivateKey?: boolean | undefined;
67
67
  readonly endo?: {
68
68
  beta: bigint;
69
69
  splitScalar: (k: bigint) => {
@@ -75,10 +75,10 @@ export declare const secp192r1: Readonly<{
75
75
  } | undefined;
76
76
  readonly isTorsionFree?: ((c: import("./abstract/weierstrass.js").ProjConstructor<bigint>, point: import("./abstract/weierstrass.js").ProjPointType<bigint>) => boolean) | undefined;
77
77
  readonly clearCofactor?: ((c: import("./abstract/weierstrass.js").ProjConstructor<bigint>, point: import("./abstract/weierstrass.js").ProjPointType<bigint>) => import("./abstract/weierstrass.js").ProjPointType<bigint>) | undefined;
78
- lowS: boolean;
79
78
  readonly hash: import("./abstract/utils.js").CHash;
80
79
  readonly hmac: (key: Uint8Array, ...messages: Uint8Array[]) => Uint8Array;
81
80
  readonly randomBytes: (bytesLength?: number | undefined) => Uint8Array;
81
+ lowS: boolean;
82
82
  readonly bits2int?: ((bytes: Uint8Array) => bigint) | undefined;
83
83
  readonly bits2int_modN?: ((bytes: Uint8Array) => bigint) | undefined;
84
84
  }>;
package/lib/p224.d.ts CHANGED
@@ -9,11 +9,11 @@ export declare const P224: Readonly<{
9
9
  readonly hEff?: bigint | undefined;
10
10
  readonly Gx: bigint;
11
11
  readonly Gy: bigint;
12
- readonly wrapPrivateKey?: boolean | undefined;
13
12
  readonly allowInfinityPoint?: boolean | undefined;
14
13
  readonly a: bigint;
15
14
  readonly b: bigint;
16
- readonly normalizePrivateKey?: ((key: import("./abstract/utils.js").PrivKey) => import("./abstract/utils.js").PrivKey) | undefined;
15
+ readonly allowedPrivateKeyLengths?: readonly number[] | undefined;
16
+ readonly wrapPrivateKey?: boolean | undefined;
17
17
  readonly endo?: {
18
18
  beta: bigint;
19
19
  splitScalar: (k: bigint) => {
@@ -25,10 +25,10 @@ export declare const P224: Readonly<{
25
25
  } | undefined;
26
26
  readonly isTorsionFree?: ((c: import("./abstract/weierstrass.js").ProjConstructor<bigint>, point: import("./abstract/weierstrass.js").ProjPointType<bigint>) => boolean) | undefined;
27
27
  readonly clearCofactor?: ((c: import("./abstract/weierstrass.js").ProjConstructor<bigint>, point: import("./abstract/weierstrass.js").ProjPointType<bigint>) => import("./abstract/weierstrass.js").ProjPointType<bigint>) | undefined;
28
- lowS: boolean;
29
28
  readonly hash: import("./abstract/utils.js").CHash;
30
29
  readonly hmac: (key: Uint8Array, ...messages: Uint8Array[]) => Uint8Array;
31
30
  readonly randomBytes: (bytesLength?: number | undefined) => Uint8Array;
31
+ lowS: boolean;
32
32
  readonly bits2int?: ((bytes: Uint8Array) => bigint) | undefined;
33
33
  readonly bits2int_modN?: ((bytes: Uint8Array) => bigint) | undefined;
34
34
  }>;
@@ -59,11 +59,11 @@ export declare const secp224r1: Readonly<{
59
59
  readonly hEff?: bigint | undefined;
60
60
  readonly Gx: bigint;
61
61
  readonly Gy: bigint;
62
- readonly wrapPrivateKey?: boolean | undefined;
63
62
  readonly allowInfinityPoint?: boolean | undefined;
64
63
  readonly a: bigint;
65
64
  readonly b: bigint;
66
- readonly normalizePrivateKey?: ((key: import("./abstract/utils.js").PrivKey) => import("./abstract/utils.js").PrivKey) | undefined;
65
+ readonly allowedPrivateKeyLengths?: readonly number[] | undefined;
66
+ readonly wrapPrivateKey?: boolean | undefined;
67
67
  readonly endo?: {
68
68
  beta: bigint;
69
69
  splitScalar: (k: bigint) => {
@@ -75,10 +75,10 @@ export declare const secp224r1: Readonly<{
75
75
  } | undefined;
76
76
  readonly isTorsionFree?: ((c: import("./abstract/weierstrass.js").ProjConstructor<bigint>, point: import("./abstract/weierstrass.js").ProjPointType<bigint>) => boolean) | undefined;
77
77
  readonly clearCofactor?: ((c: import("./abstract/weierstrass.js").ProjConstructor<bigint>, point: import("./abstract/weierstrass.js").ProjPointType<bigint>) => import("./abstract/weierstrass.js").ProjPointType<bigint>) | undefined;
78
- lowS: boolean;
79
78
  readonly hash: import("./abstract/utils.js").CHash;
80
79
  readonly hmac: (key: Uint8Array, ...messages: Uint8Array[]) => Uint8Array;
81
80
  readonly randomBytes: (bytesLength?: number | undefined) => Uint8Array;
81
+ lowS: boolean;
82
82
  readonly bits2int?: ((bytes: Uint8Array) => bigint) | undefined;
83
83
  readonly bits2int_modN?: ((bytes: Uint8Array) => bigint) | undefined;
84
84
  }>;
package/lib/p224.js CHANGED
@@ -11,7 +11,7 @@ exports.P224 = (0, _shortw_utils_js_1.createCurve)({
11
11
  // Params: a, b
12
12
  a: BigInt('0xfffffffffffffffffffffffffffffffefffffffffffffffffffffffe'),
13
13
  b: BigInt('0xb4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4'),
14
- // Field over which we'll do calculations; 2n**224n - 2n**96n + 1n
14
+ // Field over which we'll do calculations;
15
15
  Fp: (0, modular_js_1.Fp)(BigInt('0xffffffffffffffffffffffffffffffff000000000000000000000001')),
16
16
  // Curve order, total count of valid points in the field
17
17
  n: BigInt('0xffffffffffffffffffffffffffff16a2e0b8f03e13dd29455c5c2a3d'),
package/lib/p256.d.ts CHANGED
@@ -10,11 +10,11 @@ export declare const P256: Readonly<{
10
10
  readonly hEff?: bigint | undefined;
11
11
  readonly Gx: bigint;
12
12
  readonly Gy: bigint;
13
- readonly wrapPrivateKey?: boolean | undefined;
14
13
  readonly allowInfinityPoint?: boolean | undefined;
15
14
  readonly a: bigint;
16
15
  readonly b: bigint;
17
- readonly normalizePrivateKey?: ((key: import("./abstract/utils.js").PrivKey) => import("./abstract/utils.js").PrivKey) | undefined;
16
+ readonly allowedPrivateKeyLengths?: readonly number[] | undefined;
17
+ readonly wrapPrivateKey?: boolean | undefined;
18
18
  readonly endo?: {
19
19
  beta: bigint;
20
20
  splitScalar: (k: bigint) => {
@@ -26,10 +26,10 @@ export declare const P256: Readonly<{
26
26
  } | undefined;
27
27
  readonly isTorsionFree?: ((c: import("./abstract/weierstrass.js").ProjConstructor<bigint>, point: import("./abstract/weierstrass.js").ProjPointType<bigint>) => boolean) | undefined;
28
28
  readonly clearCofactor?: ((c: import("./abstract/weierstrass.js").ProjConstructor<bigint>, point: import("./abstract/weierstrass.js").ProjPointType<bigint>) => import("./abstract/weierstrass.js").ProjPointType<bigint>) | undefined;
29
- lowS: boolean;
30
29
  readonly hash: import("./abstract/utils.js").CHash;
31
30
  readonly hmac: (key: Uint8Array, ...messages: Uint8Array[]) => Uint8Array;
32
31
  readonly randomBytes: (bytesLength?: number | undefined) => Uint8Array;
32
+ lowS: boolean;
33
33
  readonly bits2int?: ((bytes: Uint8Array) => bigint) | undefined;
34
34
  readonly bits2int_modN?: ((bytes: Uint8Array) => bigint) | undefined;
35
35
  }>;
@@ -60,11 +60,11 @@ export declare const secp256r1: Readonly<{
60
60
  readonly hEff?: bigint | undefined;
61
61
  readonly Gx: bigint;
62
62
  readonly Gy: bigint;
63
- readonly wrapPrivateKey?: boolean | undefined;
64
63
  readonly allowInfinityPoint?: boolean | undefined;
65
64
  readonly a: bigint;
66
65
  readonly b: bigint;
67
- readonly normalizePrivateKey?: ((key: import("./abstract/utils.js").PrivKey) => import("./abstract/utils.js").PrivKey) | undefined;
66
+ readonly allowedPrivateKeyLengths?: readonly number[] | undefined;
67
+ readonly wrapPrivateKey?: boolean | undefined;
68
68
  readonly endo?: {
69
69
  beta: bigint;
70
70
  splitScalar: (k: bigint) => {
@@ -76,10 +76,10 @@ export declare const secp256r1: Readonly<{
76
76
  } | undefined;
77
77
  readonly isTorsionFree?: ((c: import("./abstract/weierstrass.js").ProjConstructor<bigint>, point: import("./abstract/weierstrass.js").ProjPointType<bigint>) => boolean) | undefined;
78
78
  readonly clearCofactor?: ((c: import("./abstract/weierstrass.js").ProjConstructor<bigint>, point: import("./abstract/weierstrass.js").ProjPointType<bigint>) => import("./abstract/weierstrass.js").ProjPointType<bigint>) | undefined;
79
- lowS: boolean;
80
79
  readonly hash: import("./abstract/utils.js").CHash;
81
80
  readonly hmac: (key: Uint8Array, ...messages: Uint8Array[]) => Uint8Array;
82
81
  readonly randomBytes: (bytesLength?: number | undefined) => Uint8Array;
82
+ lowS: boolean;
83
83
  readonly bits2int?: ((bytes: Uint8Array) => bigint) | undefined;
84
84
  readonly bits2int_modN?: ((bytes: Uint8Array) => bigint) | undefined;
85
85
  }>;
package/lib/p384.d.ts CHANGED
@@ -10,11 +10,11 @@ export declare const P384: Readonly<{
10
10
  readonly hEff?: bigint | undefined;
11
11
  readonly Gx: bigint;
12
12
  readonly Gy: bigint;
13
- readonly wrapPrivateKey?: boolean | undefined;
14
13
  readonly allowInfinityPoint?: boolean | undefined;
15
14
  readonly a: bigint;
16
15
  readonly b: bigint;
17
- readonly normalizePrivateKey?: ((key: import("./abstract/utils.js").PrivKey) => import("./abstract/utils.js").PrivKey) | undefined;
16
+ readonly allowedPrivateKeyLengths?: readonly number[] | undefined;
17
+ readonly wrapPrivateKey?: boolean | undefined;
18
18
  readonly endo?: {
19
19
  beta: bigint;
20
20
  splitScalar: (k: bigint) => {
@@ -26,10 +26,10 @@ export declare const P384: Readonly<{
26
26
  } | undefined;
27
27
  readonly isTorsionFree?: ((c: import("./abstract/weierstrass.js").ProjConstructor<bigint>, point: import("./abstract/weierstrass.js").ProjPointType<bigint>) => boolean) | undefined;
28
28
  readonly clearCofactor?: ((c: import("./abstract/weierstrass.js").ProjConstructor<bigint>, point: import("./abstract/weierstrass.js").ProjPointType<bigint>) => import("./abstract/weierstrass.js").ProjPointType<bigint>) | undefined;
29
- lowS: boolean;
30
29
  readonly hash: import("./abstract/utils.js").CHash;
31
30
  readonly hmac: (key: Uint8Array, ...messages: Uint8Array[]) => Uint8Array;
32
31
  readonly randomBytes: (bytesLength?: number | undefined) => Uint8Array;
32
+ lowS: boolean;
33
33
  readonly bits2int?: ((bytes: Uint8Array) => bigint) | undefined;
34
34
  readonly bits2int_modN?: ((bytes: Uint8Array) => bigint) | undefined;
35
35
  }>;
@@ -60,11 +60,11 @@ export declare const secp384r1: Readonly<{
60
60
  readonly hEff?: bigint | undefined;
61
61
  readonly Gx: bigint;
62
62
  readonly Gy: bigint;
63
- readonly wrapPrivateKey?: boolean | undefined;
64
63
  readonly allowInfinityPoint?: boolean | undefined;
65
64
  readonly a: bigint;
66
65
  readonly b: bigint;
67
- readonly normalizePrivateKey?: ((key: import("./abstract/utils.js").PrivKey) => import("./abstract/utils.js").PrivKey) | undefined;
66
+ readonly allowedPrivateKeyLengths?: readonly number[] | undefined;
67
+ readonly wrapPrivateKey?: boolean | undefined;
68
68
  readonly endo?: {
69
69
  beta: bigint;
70
70
  splitScalar: (k: bigint) => {
@@ -76,10 +76,10 @@ export declare const secp384r1: Readonly<{
76
76
  } | undefined;
77
77
  readonly isTorsionFree?: ((c: import("./abstract/weierstrass.js").ProjConstructor<bigint>, point: import("./abstract/weierstrass.js").ProjPointType<bigint>) => boolean) | undefined;
78
78
  readonly clearCofactor?: ((c: import("./abstract/weierstrass.js").ProjConstructor<bigint>, point: import("./abstract/weierstrass.js").ProjPointType<bigint>) => import("./abstract/weierstrass.js").ProjPointType<bigint>) | undefined;
79
- lowS: boolean;
80
79
  readonly hash: import("./abstract/utils.js").CHash;
81
80
  readonly hmac: (key: Uint8Array, ...messages: Uint8Array[]) => Uint8Array;
82
81
  readonly randomBytes: (bytesLength?: number | undefined) => Uint8Array;
82
+ lowS: boolean;
83
83
  readonly bits2int?: ((bytes: Uint8Array) => bigint) | undefined;
84
84
  readonly bits2int_modN?: ((bytes: Uint8Array) => bigint) | undefined;
85
85
  }>;
package/lib/p521.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- import { PrivKey } from './abstract/utils.js';
2
1
  import * as htf from './abstract/hash-to-curve.js';
3
2
  export declare const P521: Readonly<{
4
3
  create: (hash: import("./abstract/utils.js").CHash) => import("./abstract/weierstrass.js").CurveFn;
@@ -11,11 +10,11 @@ export declare const P521: Readonly<{
11
10
  readonly hEff?: bigint | undefined;
12
11
  readonly Gx: bigint;
13
12
  readonly Gy: bigint;
14
- readonly wrapPrivateKey?: boolean | undefined;
15
13
  readonly allowInfinityPoint?: boolean | undefined;
16
14
  readonly a: bigint;
17
15
  readonly b: bigint;
18
- readonly normalizePrivateKey?: ((key: PrivKey) => PrivKey) | undefined;
16
+ readonly allowedPrivateKeyLengths?: readonly number[] | undefined;
17
+ readonly wrapPrivateKey?: boolean | undefined;
19
18
  readonly endo?: {
20
19
  beta: bigint;
21
20
  splitScalar: (k: bigint) => {
@@ -27,16 +26,16 @@ export declare const P521: Readonly<{
27
26
  } | undefined;
28
27
  readonly isTorsionFree?: ((c: import("./abstract/weierstrass.js").ProjConstructor<bigint>, point: import("./abstract/weierstrass.js").ProjPointType<bigint>) => boolean) | undefined;
29
28
  readonly clearCofactor?: ((c: import("./abstract/weierstrass.js").ProjConstructor<bigint>, point: import("./abstract/weierstrass.js").ProjPointType<bigint>) => import("./abstract/weierstrass.js").ProjPointType<bigint>) | undefined;
30
- lowS: boolean;
31
29
  readonly hash: import("./abstract/utils.js").CHash;
32
30
  readonly hmac: (key: Uint8Array, ...messages: Uint8Array[]) => Uint8Array;
33
31
  readonly randomBytes: (bytesLength?: number | undefined) => Uint8Array;
32
+ lowS: boolean;
34
33
  readonly bits2int?: ((bytes: Uint8Array) => bigint) | undefined;
35
34
  readonly bits2int_modN?: ((bytes: Uint8Array) => bigint) | undefined;
36
35
  }>;
37
- getPublicKey: (privateKey: PrivKey, isCompressed?: boolean | undefined) => Uint8Array;
38
- getSharedSecret: (privateA: PrivKey, publicB: import("./abstract/utils.js").Hex, isCompressed?: boolean | undefined) => Uint8Array;
39
- sign: (msgHash: import("./abstract/utils.js").Hex, privKey: PrivKey, opts?: import("./abstract/weierstrass.js").SignOpts | undefined) => import("./abstract/weierstrass.js").SignatureType;
36
+ getPublicKey: (privateKey: import("./abstract/utils.js").PrivKey, isCompressed?: boolean | undefined) => Uint8Array;
37
+ getSharedSecret: (privateA: import("./abstract/utils.js").PrivKey, publicB: import("./abstract/utils.js").Hex, isCompressed?: boolean | undefined) => Uint8Array;
38
+ sign: (msgHash: import("./abstract/utils.js").Hex, privKey: import("./abstract/utils.js").PrivKey, opts?: import("./abstract/weierstrass.js").SignOpts | undefined) => import("./abstract/weierstrass.js").SignatureType;
40
39
  verify: (signature: import("./abstract/utils.js").Hex | {
41
40
  r: bigint;
42
41
  s: bigint;
@@ -44,8 +43,8 @@ export declare const P521: Readonly<{
44
43
  ProjectivePoint: import("./abstract/weierstrass.js").ProjConstructor<bigint>;
45
44
  Signature: import("./abstract/weierstrass.js").SignatureConstructor;
46
45
  utils: {
47
- _normalizePrivateKey: (key: PrivKey) => bigint;
48
- isValidPrivateKey(privateKey: PrivKey): boolean;
46
+ _normalizePrivateKey: (key: import("./abstract/utils.js").PrivKey) => bigint;
47
+ isValidPrivateKey(privateKey: import("./abstract/utils.js").PrivKey): boolean;
49
48
  hashToPrivateKey: (hash: import("./abstract/utils.js").Hex) => Uint8Array;
50
49
  randomPrivateKey: () => Uint8Array;
51
50
  };
@@ -61,11 +60,11 @@ export declare const secp521r1: Readonly<{
61
60
  readonly hEff?: bigint | undefined;
62
61
  readonly Gx: bigint;
63
62
  readonly Gy: bigint;
64
- readonly wrapPrivateKey?: boolean | undefined;
65
63
  readonly allowInfinityPoint?: boolean | undefined;
66
64
  readonly a: bigint;
67
65
  readonly b: bigint;
68
- readonly normalizePrivateKey?: ((key: PrivKey) => PrivKey) | undefined;
66
+ readonly allowedPrivateKeyLengths?: readonly number[] | undefined;
67
+ readonly wrapPrivateKey?: boolean | undefined;
69
68
  readonly endo?: {
70
69
  beta: bigint;
71
70
  splitScalar: (k: bigint) => {
@@ -77,16 +76,16 @@ export declare const secp521r1: Readonly<{
77
76
  } | undefined;
78
77
  readonly isTorsionFree?: ((c: import("./abstract/weierstrass.js").ProjConstructor<bigint>, point: import("./abstract/weierstrass.js").ProjPointType<bigint>) => boolean) | undefined;
79
78
  readonly clearCofactor?: ((c: import("./abstract/weierstrass.js").ProjConstructor<bigint>, point: import("./abstract/weierstrass.js").ProjPointType<bigint>) => import("./abstract/weierstrass.js").ProjPointType<bigint>) | undefined;
80
- lowS: boolean;
81
79
  readonly hash: import("./abstract/utils.js").CHash;
82
80
  readonly hmac: (key: Uint8Array, ...messages: Uint8Array[]) => Uint8Array;
83
81
  readonly randomBytes: (bytesLength?: number | undefined) => Uint8Array;
82
+ lowS: boolean;
84
83
  readonly bits2int?: ((bytes: Uint8Array) => bigint) | undefined;
85
84
  readonly bits2int_modN?: ((bytes: Uint8Array) => bigint) | undefined;
86
85
  }>;
87
- getPublicKey: (privateKey: PrivKey, isCompressed?: boolean | undefined) => Uint8Array;
88
- getSharedSecret: (privateA: PrivKey, publicB: import("./abstract/utils.js").Hex, isCompressed?: boolean | undefined) => Uint8Array;
89
- sign: (msgHash: import("./abstract/utils.js").Hex, privKey: PrivKey, opts?: import("./abstract/weierstrass.js").SignOpts | undefined) => import("./abstract/weierstrass.js").SignatureType;
86
+ getPublicKey: (privateKey: import("./abstract/utils.js").PrivKey, isCompressed?: boolean | undefined) => Uint8Array;
87
+ getSharedSecret: (privateA: import("./abstract/utils.js").PrivKey, publicB: import("./abstract/utils.js").Hex, isCompressed?: boolean | undefined) => Uint8Array;
88
+ sign: (msgHash: import("./abstract/utils.js").Hex, privKey: import("./abstract/utils.js").PrivKey, opts?: import("./abstract/weierstrass.js").SignOpts | undefined) => import("./abstract/weierstrass.js").SignatureType;
90
89
  verify: (signature: import("./abstract/utils.js").Hex | {
91
90
  r: bigint;
92
91
  s: bigint;
@@ -94,8 +93,8 @@ export declare const secp521r1: Readonly<{
94
93
  ProjectivePoint: import("./abstract/weierstrass.js").ProjConstructor<bigint>;
95
94
  Signature: import("./abstract/weierstrass.js").SignatureConstructor;
96
95
  utils: {
97
- _normalizePrivateKey: (key: PrivKey) => bigint;
98
- isValidPrivateKey(privateKey: PrivKey): boolean;
96
+ _normalizePrivateKey: (key: import("./abstract/utils.js").PrivKey) => bigint;
97
+ isValidPrivateKey(privateKey: import("./abstract/utils.js").PrivKey): boolean;
99
98
  hashToPrivateKey: (hash: import("./abstract/utils.js").Hex) => Uint8Array;
100
99
  randomPrivateKey: () => Uint8Array;
101
100
  };
package/lib/p521.js CHANGED
@@ -4,7 +4,6 @@ exports.encodeToCurve = exports.hashToCurve = exports.secp521r1 = exports.P521 =
4
4
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
5
5
  const _shortw_utils_js_1 = require("./_shortw_utils.js");
6
6
  const sha512_1 = require("@noble/hashes/sha512");
7
- const utils_js_1 = require("./abstract/utils.js");
8
7
  const modular_js_1 = require("./abstract/modular.js");
9
8
  const weierstrass_js_1 = require("./abstract/weierstrass.js");
10
9
  const htf = require("./abstract/hash-to-curve.js");
@@ -36,18 +35,7 @@ exports.P521 = (0, _shortw_utils_js_1.createCurve)({
36
35
  Gy: BigInt('0x011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650'),
37
36
  h: BigInt(1),
38
37
  lowS: false,
39
- // P521 keys could be 130, 131, 132 bytes. We normalize to 132 bytes.
40
- // Does not replace validation; invalid keys would still be rejected.
41
- normalizePrivateKey(key) {
42
- if (typeof key === 'bigint')
43
- return key;
44
- if (key instanceof Uint8Array)
45
- key = (0, utils_js_1.bytesToHex)(key);
46
- if (typeof key !== 'string' || !([130, 131, 132].includes(key.length))) {
47
- throw new Error('Invalid key');
48
- }
49
- return key.padStart(66 * 2, '0'); // ensure it's always 132 bytes
50
- },
38
+ allowedPrivateKeyLengths: [130, 131, 132] // P521 keys are variable-length. Normalize to 132b
51
39
  }, sha512_1.sha512);
52
40
  exports.secp521r1 = exports.P521;
53
41
  const { hashToCurve, encodeToCurve } = htf.hashToCurve(exports.secp521r1.ProjectivePoint, (scalars) => mapSWU(scalars[0]), {
@@ -1,5 +1,6 @@
1
+ import { mod } from './abstract/modular.js';
1
2
  import { ProjPointType as PointType } from './abstract/weierstrass.js';
2
- import { Hex, bytesToNumberBE as bytesToNum, PrivKey } from './abstract/utils.js';
3
+ import { Hex, bytesToNumberBE as bytesToInt, PrivKey } from './abstract/utils.js';
3
4
  import * as htf from './abstract/hash-to-curve.js';
4
5
  export declare const secp256k1: Readonly<{
5
6
  create: (hash: import("./abstract/utils.js").CHash) => import("./abstract/weierstrass.js").CurveFn;
@@ -12,11 +13,11 @@ export declare const secp256k1: Readonly<{
12
13
  readonly hEff?: bigint | undefined;
13
14
  readonly Gx: bigint;
14
15
  readonly Gy: bigint;
15
- readonly wrapPrivateKey?: boolean | undefined;
16
16
  readonly allowInfinityPoint?: boolean | undefined;
17
17
  readonly a: bigint;
18
18
  readonly b: bigint;
19
- readonly normalizePrivateKey?: ((key: PrivKey) => PrivKey) | undefined;
19
+ readonly allowedPrivateKeyLengths?: readonly number[] | undefined;
20
+ readonly wrapPrivateKey?: boolean | undefined;
20
21
  readonly endo?: {
21
22
  beta: bigint;
22
23
  splitScalar: (k: bigint) => {
@@ -28,10 +29,10 @@ export declare const secp256k1: Readonly<{
28
29
  } | undefined;
29
30
  readonly isTorsionFree?: ((c: import("./abstract/weierstrass.js").ProjConstructor<bigint>, point: PointType<bigint>) => boolean) | undefined;
30
31
  readonly clearCofactor?: ((c: import("./abstract/weierstrass.js").ProjConstructor<bigint>, point: PointType<bigint>) => PointType<bigint>) | undefined;
31
- lowS: boolean;
32
32
  readonly hash: import("./abstract/utils.js").CHash;
33
33
  readonly hmac: (key: Uint8Array, ...messages: Uint8Array[]) => Uint8Array;
34
34
  readonly randomBytes: (bytesLength?: number | undefined) => Uint8Array;
35
+ lowS: boolean;
35
36
  readonly bits2int?: ((bytes: Uint8Array) => bigint) | undefined;
36
37
  readonly bits2int_modN?: ((bytes: Uint8Array) => bigint) | undefined;
37
38
  }>;
@@ -52,16 +53,14 @@ export declare const secp256k1: Readonly<{
52
53
  };
53
54
  }>;
54
55
  declare function taggedHash(tag: string, ...messages: Uint8Array[]): Uint8Array;
56
+ declare function schnorrGetExtPubKey(priv: PrivKey): {
57
+ point: PointType<bigint>;
58
+ scalar: bigint;
59
+ bytes: Uint8Array;
60
+ };
55
61
  declare function lift_x(x: bigint): PointType<bigint>;
56
- declare function schnorrGetPublicKey(privateKey: PrivKey): Uint8Array;
57
- /**
58
- * Synchronously creates Schnorr signature. Improved security: verifies itself before
59
- * producing an output.
60
- * @param msg message (not message hash)
61
- * @param privateKey private key
62
- * @param auxRand random bytes that would be added to k. Bad RNG won't break it.
63
- */
64
- declare function schnorrSign(message: Hex, privateKey: Hex, auxRand?: Hex): Uint8Array;
62
+ declare function schnorrGetPublicKey(privateKey: Hex): Uint8Array;
63
+ declare function schnorrSign(message: Hex, privateKey: PrivKey, auxRand?: Hex): Uint8Array;
65
64
  /**
66
65
  * Verifies Schnorr signature synchronously.
67
66
  */
@@ -71,9 +70,13 @@ export declare const schnorr: {
71
70
  sign: typeof schnorrSign;
72
71
  verify: typeof schnorrVerify;
73
72
  utils: {
73
+ getExtendedPublicKey: typeof schnorrGetExtPubKey;
74
74
  lift_x: typeof lift_x;
75
- int: typeof bytesToNum;
75
+ pointToBytes: (point: PointType<bigint>) => Uint8Array;
76
+ numberToBytesBE: (n: bigint, len: number) => Uint8Array;
77
+ bytesToNumberBE: typeof bytesToInt;
76
78
  taggedHash: typeof taggedHash;
79
+ mod: typeof mod;
77
80
  };
78
81
  };
79
82
  declare const hashToCurve: (msg: Hex, options?: htf.htfBasicOpts | undefined) => htf.H2CPoint<bigint>, encodeToCurve: (msg: Hex, options?: htf.htfBasicOpts | undefined) => htf.H2CPoint<bigint>;