@noble/curves 0.5.1 → 0.5.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.
@@ -1,4 +1,14 @@
1
1
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
2
+ // The pairing-friendly Barreto-Lynn-Scott elliptic curve construction allows to:
3
+ // - Construct zk-SNARKs at the 128-bit security
4
+ // - Use threshold signatures, which allows a user to sign lots of messages with one signature and verify them swiftly in a batch, using Boneh-Lynn-Shacham signature scheme.
5
+ // Differences from @noble/bls12-381 1.4:
6
+ // - PointG1 -> G1.Point
7
+ // - PointG2 -> G2.Point
8
+ // - PointG2.fromSignature -> Signature.decode
9
+ // - PointG2.toSignature -> Signature.encode
10
+ // - Fixed Fp2 ORDER
11
+ // - Points now have only two coordinates
2
12
  import { sha256 } from '@noble/hashes/sha256';
3
13
  import { randomBytes } from '@noble/hashes/utils';
4
14
  import { bls } from './abstract/bls.js';
@@ -7,13 +17,6 @@ import { concatBytes, ensureBytes, numberToBytesBE, bytesToNumberBE, bitLen, bit
7
17
  // Types
8
18
  import { mapToCurveSimpleSWU, } from './abstract/weierstrass.js';
9
19
  import { isogenyMap } from './abstract/hash-to-curve.js';
10
- // Differences from bls12-381:
11
- // - PointG1 -> G1.Point
12
- // - PointG2 -> G2.Point
13
- // - PointG2.fromSignature -> Signature.decode
14
- // - PointG2.toSignature -> Signature.encode
15
- // - Fixed Fp2 ORDER
16
- // Points now have only two coordinates
17
20
  // CURVE FIELDS
18
21
  // Finite field over p.
19
22
  const Fp = mod.Fp(0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaabn);
@@ -97,6 +100,8 @@ const Fp2 = {
97
100
  return { c0: Fp.mul(factor, Fp.create(a)), c1: Fp.mul(factor, Fp.create(-b)) };
98
101
  },
99
102
  sqrt: (num) => {
103
+ if (Fp2.equals(num, Fp2.ZERO))
104
+ return Fp2.ZERO; // Algo doesn't handles this case
100
105
  // TODO: Optimize this line. It's extremely slow.
101
106
  // Speeding this up would boost aggregateSignatures.
102
107
  // https://eprint.iacr.org/2012/685.pdf applicable?
@@ -222,13 +222,13 @@ const D_MINUS_ONE_SQ = BigInt('4044083434630853685810104246932319082624839914623
222
222
  // Calculates 1/√(number)
223
223
  const invertSqrt = (number) => uvRatio(_1n, number);
224
224
  const MAX_255B = BigInt('0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff');
225
- const bytes255ToNumberLE = (bytes) => ed25519.utils.mod(bytesToNumberLE(bytes) & MAX_255B);
225
+ const bytes255ToNumberLE = (bytes) => ed25519.CURVE.Fp.create(bytesToNumberLE(bytes) & MAX_255B);
226
226
  // Computes Elligator map for Ristretto
227
227
  // https://ristretto.group/formulas/elligator.html
228
228
  function calcElligatorRistrettoMap(r0) {
229
229
  const { d } = ed25519.CURVE;
230
230
  const P = ed25519.CURVE.Fp.ORDER;
231
- const { mod } = ed25519.utils;
231
+ const mod = ed25519.CURVE.Fp.create;
232
232
  const r = mod(SQRT_M1 * r0 * r0); // 1
233
233
  const Ns = mod((r + _1n) * ONE_MINUS_D_SQ); // 2
234
234
  let c = BigInt(-1); // 3
@@ -286,7 +286,7 @@ export class RistrettoPoint {
286
286
  hex = ensureBytes(hex, 32);
287
287
  const { a, d } = ed25519.CURVE;
288
288
  const P = ed25519.CURVE.Fp.ORDER;
289
- const { mod } = ed25519.utils;
289
+ const mod = ed25519.CURVE.Fp.create;
290
290
  const emsg = 'RistrettoPoint.fromHex: the hex is not valid encoding of RistrettoPoint';
291
291
  const s = bytes255ToNumberLE(hex);
292
292
  // 1. Check that s_bytes is the canonical encoding of a field element, or else abort.
@@ -318,7 +318,7 @@ export class RistrettoPoint {
318
318
  toRawBytes() {
319
319
  let { x, y, z, t } = this.ep;
320
320
  const P = ed25519.CURVE.Fp.ORDER;
321
- const { mod } = ed25519.utils;
321
+ const mod = ed25519.CURVE.Fp.create;
322
322
  const u1 = mod(mod(z + y) * mod(z - y)); // 1
323
323
  const u2 = mod(x * y); // 2
324
324
  // Square root always exists
@@ -356,7 +356,7 @@ export class RistrettoPoint {
356
356
  assertRstPoint(other);
357
357
  const a = this.ep;
358
358
  const b = other.ep;
359
- const { mod } = ed25519.utils;
359
+ const mod = ed25519.CURVE.Fp.create;
360
360
  // (x1 * y2 == y1 * x2) | (y1 * y2 == x1 * x2)
361
361
  const one = mod(a.x * b.y) === mod(a.y * b.x);
362
362
  const two = mod(a.y * b.y) === mod(a.x * b.x);
package/lib/esm/ed448.js CHANGED
@@ -126,7 +126,8 @@ const ED448_DEF = {
126
126
  d: BigInt('726838724295606890549323807888004534353641360687318060281490199180612328166730772686396383698676545930088884461843637361053498018326358'),
127
127
  // Finite field 𝔽p over which we'll do calculations; 2n ** 448n - 2n ** 224n - 1n
128
128
  Fp,
129
- // Subgroup order: how many points ed448 has; 2n**446n - 13818066809895115352007386748515426880336692474882178609894547503885n
129
+ // Subgroup order: how many points curve has;
130
+ // 2n**446n - 13818066809895115352007386748515426880336692474882178609894547503885n
130
131
  n: BigInt('181709681073901722637330951972001133588410340171829515070372549795146003961539585716195755291692375963310293709091662304773755859649779'),
131
132
  nBitLength: 456,
132
133
  // Cofactor
package/lib/esm/jubjub.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
2
- import { sha256 } from '@noble/hashes/sha256';
2
+ import { sha512 } from '@noble/hashes/sha512';
3
3
  import { concatBytes, randomBytes, utf8ToBytes } from '@noble/hashes/utils';
4
4
  import { twistedEdwards } from './abstract/edwards.js';
5
5
  import { blake2s } from '@noble/hashes/blake2s';
@@ -7,22 +7,23 @@ import { Fp } from './abstract/modular.js';
7
7
  /**
8
8
  * jubjub Twisted Edwards curve.
9
9
  * https://neuromancer.sk/std/other/JubJub
10
+ * jubjub does not use EdDSA, so `hash`/sha512 params are passed because interface expects them.
10
11
  */
11
12
  export const jubjub = twistedEdwards({
12
13
  // Params: a, d
13
14
  a: BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000000'),
14
15
  d: BigInt('0x2a9318e74bfa2b48f5fd9207e6bd7fd4292d7f6d37579d2601065fd6d6343eb1'),
15
16
  // Finite field 𝔽p over which we'll do calculations
17
+ // Same value as bls12-381 Fr (not Fp)
16
18
  Fp: Fp(BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001')),
17
- // Subgroup order: how many points ed25519 has
18
- // 2n ** 252n + 27742317777372353535851937790883648493n;
19
+ // Subgroup order: how many points curve has
19
20
  n: BigInt('0xe7db4ea6533afa906673b0101343b00a6682093ccc81082d0970e5ed6f72cb7'),
20
21
  // Cofactor
21
22
  h: BigInt(8),
22
23
  // Base point (x, y) aka generator point
23
24
  Gx: BigInt('0x11dafe5d23e1218086a365b99fbf3d3be72f6afd7d1f72623e6b071492d1122b'),
24
25
  Gy: BigInt('0x1d523cf1ddab1a1793132e78c866c0c33e26ba5cc220fed7cc3f870e59d292aa'),
25
- hash: sha256,
26
+ hash: sha512,
26
27
  randomBytes,
27
28
  });
28
29
  const GH_FIRST_BLOCK = utf8ToBytes('096b36a5804bfacef1691e173c366a47ff5ba84a44f26ddd7e8d9f79d5b42df0');
@@ -7,10 +7,8 @@ import { ensureBytes, concatBytes, hexToBytes, bytesToNumberBE, } from './abstra
7
7
  import { randomBytes } from '@noble/hashes/utils';
8
8
  import { isogenyMap } from './abstract/hash-to-curve.js';
9
9
  /**
10
- * secp256k1 belongs to Koblitz curves: it has
11
- * efficiently computable Frobenius endomorphism.
12
- * Endomorphism improves efficiency:
13
- * Uses 2x less RAM, speeds up precomputation by 2x and ECDH / sign key recovery by 20%.
10
+ * secp256k1 belongs to Koblitz curves: it has efficiently computable endomorphism.
11
+ * Endomorphism uses 2x less RAM, speeds up precomputation by 2x and ECDH / key recovery by 20%.
14
12
  * Should always be used for Projective's double-and-add multiplication.
15
13
  * For affines cached multiplication, it trades off 1/2 init time & 1/3 ram for 20% perf hit.
16
14
  * https://gist.github.com/paulmillr/eb670806793e84df628a7c434a873066
@@ -111,7 +109,7 @@ export const secp256k1 = createCurve({
111
109
  const b1 = -_1n * BigInt('0xe4437ed6010e88286f547fa90abfe4c3');
112
110
  const a2 = BigInt('0x114ca50f7a8e2f3f657c1108d9d44cfd8');
113
111
  const b2 = a1;
114
- const POW_2_128 = BigInt('0x100000000000000000000000000000000');
112
+ const POW_2_128 = BigInt('0x100000000000000000000000000000000'); // (2n**128n).toString(16)
115
113
  const c1 = divNearest(b2 * k, n);
116
114
  const c2 = divNearest(-b1 * k, n);
117
115
  let k1 = mod(k - c1 * a1 - c2 * a2, n);
@@ -155,22 +153,20 @@ function normalizePublicKey(publicKey) {
155
153
  else {
156
154
  const bytes = ensureBytes(publicKey);
157
155
  // Schnorr is 32 bytes
158
- if (bytes.length === 32) {
159
- const x = bytesToNumberBE(bytes);
160
- if (!isValidFieldElement(x))
161
- throw new Error('Point is not on curve');
162
- const y2 = secp256k1.utils._weierstrassEquation(x); // = x³ + ax + b
163
- let y = sqrtMod(y2); // y = ^ (p+1)/4
164
- const isYOdd = (y & _1n) === _1n;
165
- // Schnorr
166
- if (isYOdd)
167
- y = secp256k1.CURVE.Fp.negate(y);
168
- const point = new secp256k1.Point(x, y);
169
- point.assertValidity();
170
- return point;
171
- }
172
- // Do we need that in schnorr at all?
173
- return secp256k1.Point.fromHex(publicKey);
156
+ if (bytes.length !== 32)
157
+ throw new Error('Schnorr pubkeys must be 32 bytes');
158
+ const x = bytesToNumberBE(bytes);
159
+ if (!isValidFieldElement(x))
160
+ throw new Error('Point is not on curve');
161
+ const y2 = secp256k1.utils._weierstrassEquation(x); // y² = + ax + b
162
+ let y = sqrtMod(y2); // y = ^ (p+1)/4
163
+ const isYOdd = (y & _1n) === _1n;
164
+ // Schnorr
165
+ if (isYOdd)
166
+ y = secp256k1.CURVE.Fp.negate(y);
167
+ const point = new secp256k1.Point(x, y);
168
+ point.assertValidity();
169
+ return point;
174
170
  }
175
171
  }
176
172
  const isWithinCurveOrder = secp256k1.utils._isWithinCurveOrder;
@@ -206,10 +202,11 @@ class SchnorrSignature {
206
202
  }
207
203
  static fromHex(hex) {
208
204
  const bytes = ensureBytes(hex);
209
- if (bytes.length !== 64)
210
- throw new TypeError(`SchnorrSignature.fromHex: expected 64 bytes, not ${bytes.length}`);
211
- const r = bytesToNumberBE(bytes.subarray(0, 32));
212
- const s = bytesToNumberBE(bytes.subarray(32, 64));
205
+ const len = 32; // group length
206
+ if (bytes.length !== 2 * len)
207
+ throw new TypeError(`SchnorrSignature.fromHex: expected ${2 * len} bytes, not ${bytes.length}`);
208
+ const r = bytesToNumberBE(bytes.subarray(0, len));
209
+ const s = bytesToNumberBE(bytes.subarray(len, 2 * len));
213
210
  return new SchnorrSignature(r, s);
214
211
  }
215
212
  assertValidity() {
package/lib/esm/stark.js CHANGED
@@ -121,12 +121,13 @@ function hashKeyWithIndex(key, index) {
121
121
  export function grindKey(seed) {
122
122
  const _seed = ensureBytes0x(seed);
123
123
  const sha256mask = 2n ** 256n;
124
- const limit = sha256mask - starkCurve.utils.mod(sha256mask, starkCurve.CURVE.n);
124
+ const Fn = Fp(CURVE.n);
125
+ const limit = sha256mask - Fn.create(sha256mask);
125
126
  for (let i = 0;; i++) {
126
127
  const key = hashKeyWithIndex(_seed, i);
127
128
  // key should be in [0, limit)
128
129
  if (key < limit)
129
- return starkCurve.utils.mod(key, starkCurve.CURVE.n).toString(16);
130
+ return Fn.create(key).toString(16);
130
131
  }
131
132
  }
132
133
  export function getStarkKey(privateKey) {
package/lib/jubjub.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * jubjub Twisted Edwards curve.
3
3
  * https://neuromancer.sk/std/other/JubJub
4
+ * jubjub does not use EdDSA, so `hash`/sha512 params are passed because interface expects them.
4
5
  */
5
6
  export declare const jubjub: import("./abstract/edwards.js").CurveFn;
6
7
  export declare function groupHash(tag: Uint8Array, personalization: Uint8Array): import("./abstract/edwards.js").ExtendedPointType;
package/lib/jubjub.js CHANGED
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.findGroupHash = exports.groupHash = exports.jubjub = void 0;
4
4
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
5
- const sha256_1 = require("@noble/hashes/sha256");
5
+ const sha512_1 = require("@noble/hashes/sha512");
6
6
  const utils_1 = require("@noble/hashes/utils");
7
7
  const edwards_js_1 = require("./abstract/edwards.js");
8
8
  const blake2s_1 = require("@noble/hashes/blake2s");
@@ -10,22 +10,23 @@ const modular_js_1 = require("./abstract/modular.js");
10
10
  /**
11
11
  * jubjub Twisted Edwards curve.
12
12
  * https://neuromancer.sk/std/other/JubJub
13
+ * jubjub does not use EdDSA, so `hash`/sha512 params are passed because interface expects them.
13
14
  */
14
15
  exports.jubjub = (0, edwards_js_1.twistedEdwards)({
15
16
  // Params: a, d
16
17
  a: BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000000'),
17
18
  d: BigInt('0x2a9318e74bfa2b48f5fd9207e6bd7fd4292d7f6d37579d2601065fd6d6343eb1'),
18
19
  // Finite field 𝔽p over which we'll do calculations
20
+ // Same value as bls12-381 Fr (not Fp)
19
21
  Fp: (0, modular_js_1.Fp)(BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001')),
20
- // Subgroup order: how many points ed25519 has
21
- // 2n ** 252n + 27742317777372353535851937790883648493n;
22
+ // Subgroup order: how many points curve has
22
23
  n: BigInt('0xe7db4ea6533afa906673b0101343b00a6682093ccc81082d0970e5ed6f72cb7'),
23
24
  // Cofactor
24
25
  h: BigInt(8),
25
26
  // Base point (x, y) aka generator point
26
27
  Gx: BigInt('0x11dafe5d23e1218086a365b99fbf3d3be72f6afd7d1f72623e6b071492d1122b'),
27
28
  Gy: BigInt('0x1d523cf1ddab1a1793132e78c866c0c33e26ba5cc220fed7cc3f870e59d292aa'),
28
- hash: sha256_1.sha256,
29
+ hash: sha512_1.sha512,
29
30
  randomBytes: utils_1.randomBytes,
30
31
  });
31
32
  const GH_FIRST_BLOCK = (0, utils_1.utf8ToBytes)('096b36a5804bfacef1691e173c366a47ff5ba84a44f26ddd7e8d9f79d5b42df0');
package/lib/p192.d.ts CHANGED
@@ -38,10 +38,8 @@ export declare const P192: Readonly<{
38
38
  }>;
39
39
  getPublicKey: (privateKey: import("./abstract/utils.js").PrivKey, isCompressed?: boolean | undefined) => Uint8Array;
40
40
  getSharedSecret: (privateA: import("./abstract/utils.js").PrivKey, publicB: import("./abstract/weierstrass.js").PubKey, isCompressed?: boolean | undefined) => Uint8Array;
41
- sign: (msgHash: import("./abstract/utils.js").Hex, privKey: import("./abstract/utils.js").PrivKey, opts?: {
42
- lowS?: boolean | undefined;
43
- extraEntropy?: (true | import("./abstract/utils.js").Hex) | undefined;
44
- } | undefined) => import("./abstract/weierstrass.js").SignatureType;
41
+ 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;
42
+ signUnhashed: (msg: Uint8Array, privKey: import("./abstract/utils.js").PrivKey, opts?: import("./abstract/weierstrass.js").SignOpts | undefined) => import("./abstract/weierstrass.js").SignatureType;
45
43
  verify: (signature: import("./abstract/utils.js").Hex | import("./abstract/weierstrass.js").SignatureType, msgHash: import("./abstract/utils.js").Hex, publicKey: import("./abstract/weierstrass.js").PubKey, opts?: {
46
44
  lowS?: boolean | undefined;
47
45
  } | undefined) => boolean;
@@ -49,8 +47,6 @@ export declare const P192: Readonly<{
49
47
  ProjectivePoint: import("./abstract/weierstrass.js").ProjectiveConstructor<bigint>;
50
48
  Signature: import("./abstract/weierstrass.js").SignatureConstructor;
51
49
  utils: {
52
- mod: (a: bigint, b?: bigint | undefined) => bigint;
53
- invert: (number: bigint, modulo?: bigint | undefined) => bigint;
54
50
  _bigintToBytes: (num: bigint) => Uint8Array;
55
51
  _bigintToString: (num: bigint) => string;
56
52
  _normalizePrivateKey: (key: import("./abstract/utils.js").PrivKey) => bigint;
@@ -103,10 +99,8 @@ export declare const secp192r1: Readonly<{
103
99
  }>;
104
100
  getPublicKey: (privateKey: import("./abstract/utils.js").PrivKey, isCompressed?: boolean | undefined) => Uint8Array;
105
101
  getSharedSecret: (privateA: import("./abstract/utils.js").PrivKey, publicB: import("./abstract/weierstrass.js").PubKey, isCompressed?: boolean | undefined) => Uint8Array;
106
- sign: (msgHash: import("./abstract/utils.js").Hex, privKey: import("./abstract/utils.js").PrivKey, opts?: {
107
- lowS?: boolean | undefined;
108
- extraEntropy?: (true | import("./abstract/utils.js").Hex) | undefined;
109
- } | undefined) => import("./abstract/weierstrass.js").SignatureType;
102
+ 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;
103
+ signUnhashed: (msg: Uint8Array, privKey: import("./abstract/utils.js").PrivKey, opts?: import("./abstract/weierstrass.js").SignOpts | undefined) => import("./abstract/weierstrass.js").SignatureType;
110
104
  verify: (signature: import("./abstract/utils.js").Hex | import("./abstract/weierstrass.js").SignatureType, msgHash: import("./abstract/utils.js").Hex, publicKey: import("./abstract/weierstrass.js").PubKey, opts?: {
111
105
  lowS?: boolean | undefined;
112
106
  } | undefined) => boolean;
@@ -114,8 +108,6 @@ export declare const secp192r1: Readonly<{
114
108
  ProjectivePoint: import("./abstract/weierstrass.js").ProjectiveConstructor<bigint>;
115
109
  Signature: import("./abstract/weierstrass.js").SignatureConstructor;
116
110
  utils: {
117
- mod: (a: bigint, b?: bigint | undefined) => bigint;
118
- invert: (number: bigint, modulo?: bigint | undefined) => bigint;
119
111
  _bigintToBytes: (num: bigint) => Uint8Array;
120
112
  _bigintToString: (num: bigint) => string;
121
113
  _normalizePrivateKey: (key: import("./abstract/utils.js").PrivKey) => bigint;
package/lib/p224.d.ts CHANGED
@@ -38,10 +38,8 @@ export declare const P224: Readonly<{
38
38
  }>;
39
39
  getPublicKey: (privateKey: import("./abstract/utils.js").PrivKey, isCompressed?: boolean | undefined) => Uint8Array;
40
40
  getSharedSecret: (privateA: import("./abstract/utils.js").PrivKey, publicB: import("./abstract/weierstrass.js").PubKey, isCompressed?: boolean | undefined) => Uint8Array;
41
- sign: (msgHash: import("./abstract/utils.js").Hex, privKey: import("./abstract/utils.js").PrivKey, opts?: {
42
- lowS?: boolean | undefined;
43
- extraEntropy?: (true | import("./abstract/utils.js").Hex) | undefined;
44
- } | undefined) => import("./abstract/weierstrass.js").SignatureType;
41
+ 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;
42
+ signUnhashed: (msg: Uint8Array, privKey: import("./abstract/utils.js").PrivKey, opts?: import("./abstract/weierstrass.js").SignOpts | undefined) => import("./abstract/weierstrass.js").SignatureType;
45
43
  verify: (signature: import("./abstract/utils.js").Hex | import("./abstract/weierstrass.js").SignatureType, msgHash: import("./abstract/utils.js").Hex, publicKey: import("./abstract/weierstrass.js").PubKey, opts?: {
46
44
  lowS?: boolean | undefined;
47
45
  } | undefined) => boolean;
@@ -49,8 +47,6 @@ export declare const P224: Readonly<{
49
47
  ProjectivePoint: import("./abstract/weierstrass.js").ProjectiveConstructor<bigint>;
50
48
  Signature: import("./abstract/weierstrass.js").SignatureConstructor;
51
49
  utils: {
52
- mod: (a: bigint, b?: bigint | undefined) => bigint;
53
- invert: (number: bigint, modulo?: bigint | undefined) => bigint;
54
50
  _bigintToBytes: (num: bigint) => Uint8Array;
55
51
  _bigintToString: (num: bigint) => string;
56
52
  _normalizePrivateKey: (key: import("./abstract/utils.js").PrivKey) => bigint;
@@ -103,10 +99,8 @@ export declare const secp224r1: Readonly<{
103
99
  }>;
104
100
  getPublicKey: (privateKey: import("./abstract/utils.js").PrivKey, isCompressed?: boolean | undefined) => Uint8Array;
105
101
  getSharedSecret: (privateA: import("./abstract/utils.js").PrivKey, publicB: import("./abstract/weierstrass.js").PubKey, isCompressed?: boolean | undefined) => Uint8Array;
106
- sign: (msgHash: import("./abstract/utils.js").Hex, privKey: import("./abstract/utils.js").PrivKey, opts?: {
107
- lowS?: boolean | undefined;
108
- extraEntropy?: (true | import("./abstract/utils.js").Hex) | undefined;
109
- } | undefined) => import("./abstract/weierstrass.js").SignatureType;
102
+ 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;
103
+ signUnhashed: (msg: Uint8Array, privKey: import("./abstract/utils.js").PrivKey, opts?: import("./abstract/weierstrass.js").SignOpts | undefined) => import("./abstract/weierstrass.js").SignatureType;
110
104
  verify: (signature: import("./abstract/utils.js").Hex | import("./abstract/weierstrass.js").SignatureType, msgHash: import("./abstract/utils.js").Hex, publicKey: import("./abstract/weierstrass.js").PubKey, opts?: {
111
105
  lowS?: boolean | undefined;
112
106
  } | undefined) => boolean;
@@ -114,8 +108,6 @@ export declare const secp224r1: Readonly<{
114
108
  ProjectivePoint: import("./abstract/weierstrass.js").ProjectiveConstructor<bigint>;
115
109
  Signature: import("./abstract/weierstrass.js").SignatureConstructor;
116
110
  utils: {
117
- mod: (a: bigint, b?: bigint | undefined) => bigint;
118
- invert: (number: bigint, modulo?: bigint | undefined) => bigint;
119
111
  _bigintToBytes: (num: bigint) => Uint8Array;
120
112
  _bigintToString: (num: bigint) => string;
121
113
  _normalizePrivateKey: (key: import("./abstract/utils.js").PrivKey) => bigint;
package/lib/p256.d.ts CHANGED
@@ -38,10 +38,8 @@ export declare const P256: Readonly<{
38
38
  }>;
39
39
  getPublicKey: (privateKey: import("./abstract/utils.js").PrivKey, isCompressed?: boolean | undefined) => Uint8Array;
40
40
  getSharedSecret: (privateA: import("./abstract/utils.js").PrivKey, publicB: import("./abstract/weierstrass.js").PubKey, isCompressed?: boolean | undefined) => Uint8Array;
41
- sign: (msgHash: import("./abstract/utils.js").Hex, privKey: import("./abstract/utils.js").PrivKey, opts?: {
42
- lowS?: boolean | undefined;
43
- extraEntropy?: (true | import("./abstract/utils.js").Hex) | undefined;
44
- } | undefined) => import("./abstract/weierstrass.js").SignatureType;
41
+ 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;
42
+ signUnhashed: (msg: Uint8Array, privKey: import("./abstract/utils.js").PrivKey, opts?: import("./abstract/weierstrass.js").SignOpts | undefined) => import("./abstract/weierstrass.js").SignatureType;
45
43
  verify: (signature: import("./abstract/utils.js").Hex | import("./abstract/weierstrass.js").SignatureType, msgHash: import("./abstract/utils.js").Hex, publicKey: import("./abstract/weierstrass.js").PubKey, opts?: {
46
44
  lowS?: boolean | undefined;
47
45
  } | undefined) => boolean;
@@ -49,8 +47,6 @@ export declare const P256: Readonly<{
49
47
  ProjectivePoint: import("./abstract/weierstrass.js").ProjectiveConstructor<bigint>;
50
48
  Signature: import("./abstract/weierstrass.js").SignatureConstructor;
51
49
  utils: {
52
- mod: (a: bigint, b?: bigint | undefined) => bigint;
53
- invert: (number: bigint, modulo?: bigint | undefined) => bigint;
54
50
  _bigintToBytes: (num: bigint) => Uint8Array;
55
51
  _bigintToString: (num: bigint) => string;
56
52
  _normalizePrivateKey: (key: import("./abstract/utils.js").PrivKey) => bigint;
@@ -103,10 +99,8 @@ export declare const secp256r1: Readonly<{
103
99
  }>;
104
100
  getPublicKey: (privateKey: import("./abstract/utils.js").PrivKey, isCompressed?: boolean | undefined) => Uint8Array;
105
101
  getSharedSecret: (privateA: import("./abstract/utils.js").PrivKey, publicB: import("./abstract/weierstrass.js").PubKey, isCompressed?: boolean | undefined) => Uint8Array;
106
- sign: (msgHash: import("./abstract/utils.js").Hex, privKey: import("./abstract/utils.js").PrivKey, opts?: {
107
- lowS?: boolean | undefined;
108
- extraEntropy?: (true | import("./abstract/utils.js").Hex) | undefined;
109
- } | undefined) => import("./abstract/weierstrass.js").SignatureType;
102
+ 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;
103
+ signUnhashed: (msg: Uint8Array, privKey: import("./abstract/utils.js").PrivKey, opts?: import("./abstract/weierstrass.js").SignOpts | undefined) => import("./abstract/weierstrass.js").SignatureType;
110
104
  verify: (signature: import("./abstract/utils.js").Hex | import("./abstract/weierstrass.js").SignatureType, msgHash: import("./abstract/utils.js").Hex, publicKey: import("./abstract/weierstrass.js").PubKey, opts?: {
111
105
  lowS?: boolean | undefined;
112
106
  } | undefined) => boolean;
@@ -114,8 +108,6 @@ export declare const secp256r1: Readonly<{
114
108
  ProjectivePoint: import("./abstract/weierstrass.js").ProjectiveConstructor<bigint>;
115
109
  Signature: import("./abstract/weierstrass.js").SignatureConstructor;
116
110
  utils: {
117
- mod: (a: bigint, b?: bigint | undefined) => bigint;
118
- invert: (number: bigint, modulo?: bigint | undefined) => bigint;
119
111
  _bigintToBytes: (num: bigint) => Uint8Array;
120
112
  _bigintToString: (num: bigint) => string;
121
113
  _normalizePrivateKey: (key: import("./abstract/utils.js").PrivKey) => bigint;
package/lib/p384.d.ts CHANGED
@@ -38,10 +38,8 @@ export declare const P384: Readonly<{
38
38
  }>;
39
39
  getPublicKey: (privateKey: import("./abstract/utils.js").PrivKey, isCompressed?: boolean | undefined) => Uint8Array;
40
40
  getSharedSecret: (privateA: import("./abstract/utils.js").PrivKey, publicB: import("./abstract/weierstrass.js").PubKey, isCompressed?: boolean | undefined) => Uint8Array;
41
- sign: (msgHash: import("./abstract/utils.js").Hex, privKey: import("./abstract/utils.js").PrivKey, opts?: {
42
- lowS?: boolean | undefined;
43
- extraEntropy?: (true | import("./abstract/utils.js").Hex) | undefined;
44
- } | undefined) => import("./abstract/weierstrass.js").SignatureType;
41
+ 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;
42
+ signUnhashed: (msg: Uint8Array, privKey: import("./abstract/utils.js").PrivKey, opts?: import("./abstract/weierstrass.js").SignOpts | undefined) => import("./abstract/weierstrass.js").SignatureType;
45
43
  verify: (signature: import("./abstract/utils.js").Hex | import("./abstract/weierstrass.js").SignatureType, msgHash: import("./abstract/utils.js").Hex, publicKey: import("./abstract/weierstrass.js").PubKey, opts?: {
46
44
  lowS?: boolean | undefined;
47
45
  } | undefined) => boolean;
@@ -49,8 +47,6 @@ export declare const P384: Readonly<{
49
47
  ProjectivePoint: import("./abstract/weierstrass.js").ProjectiveConstructor<bigint>;
50
48
  Signature: import("./abstract/weierstrass.js").SignatureConstructor;
51
49
  utils: {
52
- mod: (a: bigint, b?: bigint | undefined) => bigint;
53
- invert: (number: bigint, modulo?: bigint | undefined) => bigint;
54
50
  _bigintToBytes: (num: bigint) => Uint8Array;
55
51
  _bigintToString: (num: bigint) => string;
56
52
  _normalizePrivateKey: (key: import("./abstract/utils.js").PrivKey) => bigint;
@@ -103,10 +99,8 @@ export declare const secp384r1: Readonly<{
103
99
  }>;
104
100
  getPublicKey: (privateKey: import("./abstract/utils.js").PrivKey, isCompressed?: boolean | undefined) => Uint8Array;
105
101
  getSharedSecret: (privateA: import("./abstract/utils.js").PrivKey, publicB: import("./abstract/weierstrass.js").PubKey, isCompressed?: boolean | undefined) => Uint8Array;
106
- sign: (msgHash: import("./abstract/utils.js").Hex, privKey: import("./abstract/utils.js").PrivKey, opts?: {
107
- lowS?: boolean | undefined;
108
- extraEntropy?: (true | import("./abstract/utils.js").Hex) | undefined;
109
- } | undefined) => import("./abstract/weierstrass.js").SignatureType;
102
+ 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;
103
+ signUnhashed: (msg: Uint8Array, privKey: import("./abstract/utils.js").PrivKey, opts?: import("./abstract/weierstrass.js").SignOpts | undefined) => import("./abstract/weierstrass.js").SignatureType;
110
104
  verify: (signature: import("./abstract/utils.js").Hex | import("./abstract/weierstrass.js").SignatureType, msgHash: import("./abstract/utils.js").Hex, publicKey: import("./abstract/weierstrass.js").PubKey, opts?: {
111
105
  lowS?: boolean | undefined;
112
106
  } | undefined) => boolean;
@@ -114,8 +108,6 @@ export declare const secp384r1: Readonly<{
114
108
  ProjectivePoint: import("./abstract/weierstrass.js").ProjectiveConstructor<bigint>;
115
109
  Signature: import("./abstract/weierstrass.js").SignatureConstructor;
116
110
  utils: {
117
- mod: (a: bigint, b?: bigint | undefined) => bigint;
118
- invert: (number: bigint, modulo?: bigint | undefined) => bigint;
119
111
  _bigintToBytes: (num: bigint) => Uint8Array;
120
112
  _bigintToString: (num: bigint) => string;
121
113
  _normalizePrivateKey: (key: import("./abstract/utils.js").PrivKey) => bigint;
package/lib/p521.d.ts CHANGED
@@ -39,10 +39,8 @@ export declare const P521: Readonly<{
39
39
  }>;
40
40
  getPublicKey: (privateKey: PrivKey, isCompressed?: boolean | undefined) => Uint8Array;
41
41
  getSharedSecret: (privateA: PrivKey, publicB: import("./abstract/weierstrass.js").PubKey, isCompressed?: boolean | undefined) => Uint8Array;
42
- sign: (msgHash: import("./abstract/utils.js").Hex, privKey: PrivKey, opts?: {
43
- lowS?: boolean | undefined;
44
- extraEntropy?: (true | import("./abstract/utils.js").Hex) | undefined;
45
- } | undefined) => import("./abstract/weierstrass.js").SignatureType;
42
+ sign: (msgHash: import("./abstract/utils.js").Hex, privKey: PrivKey, opts?: import("./abstract/weierstrass.js").SignOpts | undefined) => import("./abstract/weierstrass.js").SignatureType;
43
+ signUnhashed: (msg: Uint8Array, privKey: PrivKey, opts?: import("./abstract/weierstrass.js").SignOpts | undefined) => import("./abstract/weierstrass.js").SignatureType;
46
44
  verify: (signature: import("./abstract/utils.js").Hex | import("./abstract/weierstrass.js").SignatureType, msgHash: import("./abstract/utils.js").Hex, publicKey: import("./abstract/weierstrass.js").PubKey, opts?: {
47
45
  lowS?: boolean | undefined;
48
46
  } | undefined) => boolean;
@@ -50,8 +48,6 @@ export declare const P521: Readonly<{
50
48
  ProjectivePoint: import("./abstract/weierstrass.js").ProjectiveConstructor<bigint>;
51
49
  Signature: import("./abstract/weierstrass.js").SignatureConstructor;
52
50
  utils: {
53
- mod: (a: bigint, b?: bigint | undefined) => bigint;
54
- invert: (number: bigint, modulo?: bigint | undefined) => bigint;
55
51
  _bigintToBytes: (num: bigint) => Uint8Array;
56
52
  _bigintToString: (num: bigint) => string;
57
53
  _normalizePrivateKey: (key: PrivKey) => bigint;
@@ -104,10 +100,8 @@ export declare const secp521r1: Readonly<{
104
100
  }>;
105
101
  getPublicKey: (privateKey: PrivKey, isCompressed?: boolean | undefined) => Uint8Array;
106
102
  getSharedSecret: (privateA: PrivKey, publicB: import("./abstract/weierstrass.js").PubKey, isCompressed?: boolean | undefined) => Uint8Array;
107
- sign: (msgHash: import("./abstract/utils.js").Hex, privKey: PrivKey, opts?: {
108
- lowS?: boolean | undefined;
109
- extraEntropy?: (true | import("./abstract/utils.js").Hex) | undefined;
110
- } | undefined) => import("./abstract/weierstrass.js").SignatureType;
103
+ sign: (msgHash: import("./abstract/utils.js").Hex, privKey: PrivKey, opts?: import("./abstract/weierstrass.js").SignOpts | undefined) => import("./abstract/weierstrass.js").SignatureType;
104
+ signUnhashed: (msg: Uint8Array, privKey: PrivKey, opts?: import("./abstract/weierstrass.js").SignOpts | undefined) => import("./abstract/weierstrass.js").SignatureType;
111
105
  verify: (signature: import("./abstract/utils.js").Hex | import("./abstract/weierstrass.js").SignatureType, msgHash: import("./abstract/utils.js").Hex, publicKey: import("./abstract/weierstrass.js").PubKey, opts?: {
112
106
  lowS?: boolean | undefined;
113
107
  } | undefined) => boolean;
@@ -115,8 +109,6 @@ export declare const secp521r1: Readonly<{
115
109
  ProjectivePoint: import("./abstract/weierstrass.js").ProjectiveConstructor<bigint>;
116
110
  Signature: import("./abstract/weierstrass.js").SignatureConstructor;
117
111
  utils: {
118
- mod: (a: bigint, b?: bigint | undefined) => bigint;
119
- invert: (number: bigint, modulo?: bigint | undefined) => bigint;
120
112
  _bigintToBytes: (num: bigint) => Uint8Array;
121
113
  _bigintToString: (num: bigint) => string;
122
114
  _normalizePrivateKey: (key: PrivKey) => bigint;
@@ -40,10 +40,8 @@ export declare const secp256k1: Readonly<{
40
40
  }>;
41
41
  getPublicKey: (privateKey: PrivKey, isCompressed?: boolean | undefined) => Uint8Array;
42
42
  getSharedSecret: (privateA: PrivKey, publicB: import("./abstract/weierstrass.js").PubKey, isCompressed?: boolean | undefined) => Uint8Array;
43
- sign: (msgHash: Hex, privKey: PrivKey, opts?: {
44
- lowS?: boolean | undefined;
45
- extraEntropy?: (true | Hex) | undefined;
46
- } | undefined) => import("./abstract/weierstrass.js").SignatureType;
43
+ sign: (msgHash: Hex, privKey: PrivKey, opts?: import("./abstract/weierstrass.js").SignOpts | undefined) => import("./abstract/weierstrass.js").SignatureType;
44
+ signUnhashed: (msg: Uint8Array, privKey: PrivKey, opts?: import("./abstract/weierstrass.js").SignOpts | undefined) => import("./abstract/weierstrass.js").SignatureType;
47
45
  verify: (signature: Hex | import("./abstract/weierstrass.js").SignatureType, msgHash: Hex, publicKey: import("./abstract/weierstrass.js").PubKey, opts?: {
48
46
  lowS?: boolean | undefined;
49
47
  } | undefined) => boolean;
@@ -51,8 +49,6 @@ export declare const secp256k1: Readonly<{
51
49
  ProjectivePoint: import("./abstract/weierstrass.js").ProjectiveConstructor<bigint>;
52
50
  Signature: import("./abstract/weierstrass.js").SignatureConstructor;
53
51
  utils: {
54
- mod: (a: bigint, b?: bigint | undefined) => bigint;
55
- invert: (number: bigint, modulo?: bigint | undefined) => bigint;
56
52
  _bigintToBytes: (num: bigint) => Uint8Array;
57
53
  _bigintToString: (num: bigint) => string;
58
54
  _normalizePrivateKey: (key: PrivKey) => bigint;
package/lib/secp256k1.js CHANGED
@@ -10,10 +10,8 @@ const utils_js_1 = require("./abstract/utils.js");
10
10
  const utils_1 = require("@noble/hashes/utils");
11
11
  const hash_to_curve_js_1 = require("./abstract/hash-to-curve.js");
12
12
  /**
13
- * secp256k1 belongs to Koblitz curves: it has
14
- * efficiently computable Frobenius endomorphism.
15
- * Endomorphism improves efficiency:
16
- * Uses 2x less RAM, speeds up precomputation by 2x and ECDH / sign key recovery by 20%.
13
+ * secp256k1 belongs to Koblitz curves: it has efficiently computable endomorphism.
14
+ * Endomorphism uses 2x less RAM, speeds up precomputation by 2x and ECDH / key recovery by 20%.
17
15
  * Should always be used for Projective's double-and-add multiplication.
18
16
  * For affines cached multiplication, it trades off 1/2 init time & 1/3 ram for 20% perf hit.
19
17
  * https://gist.github.com/paulmillr/eb670806793e84df628a7c434a873066
@@ -114,7 +112,7 @@ exports.secp256k1 = (0, _shortw_utils_js_1.createCurve)({
114
112
  const b1 = -_1n * BigInt('0xe4437ed6010e88286f547fa90abfe4c3');
115
113
  const a2 = BigInt('0x114ca50f7a8e2f3f657c1108d9d44cfd8');
116
114
  const b2 = a1;
117
- const POW_2_128 = BigInt('0x100000000000000000000000000000000');
115
+ const POW_2_128 = BigInt('0x100000000000000000000000000000000'); // (2n**128n).toString(16)
118
116
  const c1 = divNearest(b2 * k, n);
119
117
  const c2 = divNearest(-b1 * k, n);
120
118
  let k1 = (0, modular_js_1.mod)(k - c1 * a1 - c2 * a2, n);
@@ -158,22 +156,20 @@ function normalizePublicKey(publicKey) {
158
156
  else {
159
157
  const bytes = (0, utils_js_1.ensureBytes)(publicKey);
160
158
  // Schnorr is 32 bytes
161
- if (bytes.length === 32) {
162
- const x = (0, utils_js_1.bytesToNumberBE)(bytes);
163
- if (!isValidFieldElement(x))
164
- throw new Error('Point is not on curve');
165
- const y2 = exports.secp256k1.utils._weierstrassEquation(x); // = x³ + ax + b
166
- let y = sqrtMod(y2); // y = ^ (p+1)/4
167
- const isYOdd = (y & _1n) === _1n;
168
- // Schnorr
169
- if (isYOdd)
170
- y = exports.secp256k1.CURVE.Fp.negate(y);
171
- const point = new exports.secp256k1.Point(x, y);
172
- point.assertValidity();
173
- return point;
174
- }
175
- // Do we need that in schnorr at all?
176
- return exports.secp256k1.Point.fromHex(publicKey);
159
+ if (bytes.length !== 32)
160
+ throw new Error('Schnorr pubkeys must be 32 bytes');
161
+ const x = (0, utils_js_1.bytesToNumberBE)(bytes);
162
+ if (!isValidFieldElement(x))
163
+ throw new Error('Point is not on curve');
164
+ const y2 = exports.secp256k1.utils._weierstrassEquation(x); // y² = + ax + b
165
+ let y = sqrtMod(y2); // y = ^ (p+1)/4
166
+ const isYOdd = (y & _1n) === _1n;
167
+ // Schnorr
168
+ if (isYOdd)
169
+ y = exports.secp256k1.CURVE.Fp.negate(y);
170
+ const point = new exports.secp256k1.Point(x, y);
171
+ point.assertValidity();
172
+ return point;
177
173
  }
178
174
  }
179
175
  const isWithinCurveOrder = exports.secp256k1.utils._isWithinCurveOrder;
@@ -210,10 +206,11 @@ class SchnorrSignature {
210
206
  }
211
207
  static fromHex(hex) {
212
208
  const bytes = (0, utils_js_1.ensureBytes)(hex);
213
- if (bytes.length !== 64)
214
- throw new TypeError(`SchnorrSignature.fromHex: expected 64 bytes, not ${bytes.length}`);
215
- const r = (0, utils_js_1.bytesToNumberBE)(bytes.subarray(0, 32));
216
- const s = (0, utils_js_1.bytesToNumberBE)(bytes.subarray(32, 64));
209
+ const len = 32; // group length
210
+ if (bytes.length !== 2 * len)
211
+ throw new TypeError(`SchnorrSignature.fromHex: expected ${2 * len} bytes, not ${bytes.length}`);
212
+ const r = (0, utils_js_1.bytesToNumberBE)(bytes.subarray(0, len));
213
+ const s = (0, utils_js_1.bytesToNumberBE)(bytes.subarray(len, 2 * len));
217
214
  return new SchnorrSignature(r, s);
218
215
  }
219
216
  assertValidity() {
package/lib/stark.d.ts CHANGED
@@ -43,8 +43,6 @@ declare const CURVE: Readonly<{
43
43
  readonly truncateHash?: ((hash: Uint8Array, truncateOnly?: boolean | undefined) => bigint) | undefined;
44
44
  }>, Point: import("./abstract/weierstrass.js").PointConstructor<bigint>, ProjectivePoint: import("./abstract/weierstrass.js").ProjectiveConstructor<bigint>, Signature: import("./abstract/weierstrass.js").SignatureConstructor;
45
45
  export declare const utils: {
46
- mod: (a: bigint, b?: bigint | undefined) => bigint;
47
- invert: (number: bigint, modulo?: bigint | undefined) => bigint;
48
46
  _bigintToBytes: (num: bigint) => Uint8Array;
49
47
  _bigintToString: (num: bigint) => string;
50
48
  _normalizePrivateKey: (key: cutils.PrivKey) => bigint;
package/lib/stark.js CHANGED
@@ -134,12 +134,13 @@ function hashKeyWithIndex(key, index) {
134
134
  function grindKey(seed) {
135
135
  const _seed = ensureBytes0x(seed);
136
136
  const sha256mask = 2n ** 256n;
137
- const limit = sha256mask - exports.starkCurve.utils.mod(sha256mask, exports.starkCurve.CURVE.n);
137
+ const Fn = (0, modular_js_1.Fp)(CURVE.n);
138
+ const limit = sha256mask - Fn.create(sha256mask);
138
139
  for (let i = 0;; i++) {
139
140
  const key = hashKeyWithIndex(_seed, i);
140
141
  // key should be in [0, limit)
141
142
  if (key < limit)
142
- return exports.starkCurve.utils.mod(key, exports.starkCurve.CURVE.n).toString(16);
143
+ return Fn.create(key).toString(16);
143
144
  }
144
145
  }
145
146
  exports.grindKey = grindKey;