@noble/curves 0.8.3 → 0.9.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.
Files changed (102) hide show
  1. package/README.md +87 -62
  2. package/_shortw_utils.d.ts +2 -1
  3. package/_shortw_utils.d.ts.map +1 -1
  4. package/abstract/bls.d.ts +11 -11
  5. package/abstract/bls.d.ts.map +1 -1
  6. package/abstract/bls.js.map +1 -1
  7. package/abstract/curve.d.ts +5 -3
  8. package/abstract/curve.d.ts.map +1 -1
  9. package/abstract/curve.js +5 -1
  10. package/abstract/curve.js.map +1 -1
  11. package/abstract/edwards.d.ts +2 -1
  12. package/abstract/edwards.d.ts.map +1 -1
  13. package/abstract/hash-to-curve.d.ts +2 -2
  14. package/abstract/hash-to-curve.d.ts.map +1 -1
  15. package/abstract/hash-to-curve.js.map +1 -1
  16. package/abstract/modular.d.ts +24 -12
  17. package/abstract/modular.d.ts.map +1 -1
  18. package/abstract/modular.js +16 -3
  19. package/abstract/modular.js.map +1 -1
  20. package/abstract/poseidon.d.ts +3 -3
  21. package/abstract/poseidon.d.ts.map +1 -1
  22. package/abstract/poseidon.js.map +1 -1
  23. package/abstract/weierstrass.d.ts +27 -5
  24. package/abstract/weierstrass.d.ts.map +1 -1
  25. package/abstract/weierstrass.js +20 -6
  26. package/abstract/weierstrass.js.map +1 -1
  27. package/bls12-381.d.ts +4 -4
  28. package/bls12-381.d.ts.map +1 -1
  29. package/bls12-381.js +2 -2
  30. package/bls12-381.js.map +1 -1
  31. package/bn.js +1 -1
  32. package/bn.js.map +1 -1
  33. package/ed25519.js +1 -1
  34. package/ed25519.js.map +1 -1
  35. package/ed448.js +1 -1
  36. package/ed448.js.map +1 -1
  37. package/esm/abstract/bls.js.map +1 -1
  38. package/esm/abstract/curve.js +5 -1
  39. package/esm/abstract/curve.js.map +1 -1
  40. package/esm/abstract/hash-to-curve.js.map +1 -1
  41. package/esm/abstract/modular.js +14 -1
  42. package/esm/abstract/modular.js.map +1 -1
  43. package/esm/abstract/poseidon.js.map +1 -1
  44. package/esm/abstract/weierstrass.js +20 -6
  45. package/esm/abstract/weierstrass.js.map +1 -1
  46. package/esm/bls12-381.js +2 -2
  47. package/esm/bls12-381.js.map +1 -1
  48. package/esm/bn.js +2 -2
  49. package/esm/bn.js.map +1 -1
  50. package/esm/ed25519.js +1 -1
  51. package/esm/ed25519.js.map +1 -1
  52. package/esm/ed448.js +1 -1
  53. package/esm/ed448.js.map +1 -1
  54. package/esm/jubjub.js +2 -2
  55. package/esm/jubjub.js.map +1 -1
  56. package/esm/p256.js +1 -1
  57. package/esm/p256.js.map +1 -1
  58. package/esm/p384.js +1 -1
  59. package/esm/p384.js.map +1 -1
  60. package/esm/p521.js +1 -1
  61. package/esm/p521.js.map +1 -1
  62. package/esm/pasta.js +2 -2
  63. package/esm/pasta.js.map +1 -1
  64. package/esm/secp256k1.js +1 -1
  65. package/esm/secp256k1.js.map +1 -1
  66. package/jubjub.js +1 -1
  67. package/jubjub.js.map +1 -1
  68. package/p256.d.ts +4 -2
  69. package/p256.d.ts.map +1 -1
  70. package/p256.js +1 -1
  71. package/p256.js.map +1 -1
  72. package/p384.d.ts +4 -2
  73. package/p384.d.ts.map +1 -1
  74. package/p384.js +1 -1
  75. package/p384.js.map +1 -1
  76. package/p521.d.ts +4 -2
  77. package/p521.d.ts.map +1 -1
  78. package/p521.js +1 -1
  79. package/p521.js.map +1 -1
  80. package/package.json +5 -5
  81. package/pasta.js +2 -2
  82. package/pasta.js.map +1 -1
  83. package/secp256k1.d.ts +2 -1
  84. package/secp256k1.d.ts.map +1 -1
  85. package/secp256k1.js +1 -1
  86. package/secp256k1.js.map +1 -1
  87. package/src/abstract/bls.ts +11 -11
  88. package/src/abstract/curve.ts +7 -3
  89. package/src/abstract/hash-to-curve.ts +2 -2
  90. package/src/abstract/modular.ts +28 -19
  91. package/src/abstract/poseidon.ts +2 -2
  92. package/src/abstract/weierstrass.ts +27 -11
  93. package/src/bls12-381.ts +5 -5
  94. package/src/bn.ts +2 -2
  95. package/src/ed25519.ts +1 -1
  96. package/src/ed448.ts +1 -1
  97. package/src/jubjub.ts +2 -2
  98. package/src/p256.ts +1 -1
  99. package/src/p384.ts +1 -1
  100. package/src/p521.ts +1 -1
  101. package/src/pasta.ts +2 -2
  102. package/src/secp256k1.ts +2 -3
@@ -1,10 +1,10 @@
1
1
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
2
2
  // Poseidon Hash: https://eprint.iacr.org/2019/458.pdf, https://www.poseidon-hash.info
3
- import { Field, FpPow, validateField } from './modular.js';
3
+ import { IField, FpPow, validateField } from './modular.js';
4
4
  // We don't provide any constants, since different implementations use different constants.
5
5
  // For reference constants see './test/poseidon.test.js'.
6
6
  export type PoseidonOpts = {
7
- Fp: Field<bigint>;
7
+ Fp: IField<bigint>;
8
8
  t: number;
9
9
  roundsFull: number;
10
10
  roundsPartial: number;
@@ -82,8 +82,8 @@ export interface ProjConstructor<T> extends GroupConstructor<ProjPointType<T>> {
82
82
 
83
83
  export type CurvePointsType<T> = BasicWCurve<T> & {
84
84
  // Bytes
85
- fromBytes: (bytes: Uint8Array) => AffinePoint<T>;
86
- toBytes: (c: ProjConstructor<T>, point: ProjPointType<T>, compressed: boolean) => Uint8Array;
85
+ fromBytes?: (bytes: Uint8Array) => AffinePoint<T>;
86
+ toBytes?: (c: ProjConstructor<T>, point: ProjPointType<T>, isCompressed: boolean) => Uint8Array;
87
87
  };
88
88
 
89
89
  function validatePointOpts<T>(curve: CurvePointsType<T>) {
@@ -93,8 +93,6 @@ function validatePointOpts<T>(curve: CurvePointsType<T>) {
93
93
  {
94
94
  a: 'field',
95
95
  b: 'field',
96
- fromBytes: 'function',
97
- toBytes: 'function',
98
96
  },
99
97
  {
100
98
  allowedPrivateKeyLengths: 'array',
@@ -102,6 +100,8 @@ function validatePointOpts<T>(curve: CurvePointsType<T>) {
102
100
  isTorsionFree: 'function',
103
101
  clearCofactor: 'function',
104
102
  allowInfinityPoint: 'boolean',
103
+ fromBytes: 'function',
104
+ toBytes: 'function',
105
105
  }
106
106
  );
107
107
  const { endo, Fp, a } = opts;
@@ -184,6 +184,23 @@ export function weierstrassPoints<T>(opts: CurvePointsType<T>) {
184
184
  const CURVE = validatePointOpts(opts);
185
185
  const { Fp } = CURVE; // All curves has same field / group length as for now, but they can differ
186
186
 
187
+ const toBytes =
188
+ CURVE.toBytes ||
189
+ ((c: ProjConstructor<T>, point: ProjPointType<T>, isCompressed: boolean) => {
190
+ const a = point.toAffine();
191
+ return ut.concatBytes(Uint8Array.from([0x04]), Fp.toBytes(a.x), Fp.toBytes(a.y));
192
+ });
193
+ const fromBytes =
194
+ CURVE.fromBytes ||
195
+ ((bytes: Uint8Array) => {
196
+ // const head = bytes[0];
197
+ const tail = bytes.subarray(1);
198
+ // if (head !== 0x04) throw new Error('Only non-compressed encoding is supported');
199
+ const x = Fp.fromBytes(tail.subarray(0, Fp.BYTES));
200
+ const y = Fp.fromBytes(tail.subarray(Fp.BYTES, 2 * Fp.BYTES));
201
+ return { x, y };
202
+ });
203
+
187
204
  /**
188
205
  * y² = x³ + ax + b: Short weierstrass curve formula
189
206
  * @returns y²
@@ -280,7 +297,7 @@ export function weierstrassPoints<T>(opts: CurvePointsType<T>) {
280
297
  * @param hex short/long ECDSA hex
281
298
  */
282
299
  static fromHex(hex: Hex): Point {
283
- const P = Point.fromAffine(CURVE.fromBytes(ensureBytes('pointHex', hex)));
300
+ const P = Point.fromAffine(fromBytes(ensureBytes('pointHex', hex)));
284
301
  P.assertValidity();
285
302
  return P;
286
303
  }
@@ -563,7 +580,7 @@ export function weierstrassPoints<T>(opts: CurvePointsType<T>) {
563
580
 
564
581
  toRawBytes(isCompressed = true): Uint8Array {
565
582
  this.assertValidity();
566
- return CURVE.toBytes(Point, this, isCompressed);
583
+ return toBytes(Point, this, isCompressed);
567
584
  }
568
585
 
569
586
  toHex(isCompressed = true): string {
@@ -574,6 +591,7 @@ export function weierstrassPoints<T>(opts: CurvePointsType<T>) {
574
591
  const wnaf = wNAF(Point, CURVE.endo ? Math.ceil(_bits / 2) : _bits);
575
592
 
576
593
  return {
594
+ CURVE,
577
595
  ProjectivePoint: Point as ProjConstructor<T>,
578
596
  normPrivateKeyToScalar,
579
597
  weierstrassEquation,
@@ -652,8 +670,7 @@ export type CurveFn = {
652
670
 
653
671
  export function weierstrass(curveDef: CurveType): CurveFn {
654
672
  const CURVE = validateOpts(curveDef) as ReturnType<typeof validateOpts>;
655
- const CURVE_ORDER = CURVE.n;
656
- const Fp = CURVE.Fp;
673
+ const { Fp, n: CURVE_ORDER } = CURVE;
657
674
  const compressedLen = Fp.BYTES + 1; // e.g. 33 for 32
658
675
  const uncompressedLen = 2 * Fp.BYTES + 1; // e.g. 65 for 32
659
676
 
@@ -1055,11 +1072,10 @@ export function weierstrass(curveDef: CurveType): CurveFn {
1055
1072
  }
1056
1073
 
1057
1074
  // Implementation of the Shallue and van de Woestijne method for any Weierstrass curve
1058
-
1059
1075
  // TODO: check if there is a way to merge this with uvRatio in Edwards && move to modular?
1060
1076
  // b = True and y = sqrt(u / v) if (u / v) is square in F, and
1061
1077
  // b = False and y = sqrt(Z * (u / v)) otherwise.
1062
- export function SWUFpSqrtRatio<T>(Fp: mod.Field<T>, Z: T) {
1078
+ export function SWUFpSqrtRatio<T>(Fp: mod.IField<T>, Z: T) {
1063
1079
  // Generic implementation
1064
1080
  const q = Fp.ORDER;
1065
1081
  let l = 0n;
@@ -1124,7 +1140,7 @@ export function SWUFpSqrtRatio<T>(Fp: mod.Field<T>, Z: T) {
1124
1140
  }
1125
1141
  // From draft-irtf-cfrg-hash-to-curve-16
1126
1142
  export function mapToCurveSimpleSWU<T>(
1127
- Fp: mod.Field<T>,
1143
+ Fp: mod.IField<T>,
1128
1144
  opts: {
1129
1145
  A: T;
1130
1146
  B: T;
package/src/bls12-381.ts CHANGED
@@ -72,13 +72,13 @@ import { isogenyMap } from './abstract/hash-to-curve.js';
72
72
  // CURVE FIELDS
73
73
  // Finite field over p.
74
74
  const Fp =
75
- mod.Fp(
75
+ mod.Field(
76
76
  0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaabn
77
77
  );
78
78
  type Fp = bigint;
79
79
  // Finite field over r.
80
80
  // This particular field is not used anywhere in bls12-381, but it is still useful.
81
- const Fr = mod.Fp(0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001n);
81
+ const Fr = mod.Field(0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001n);
82
82
 
83
83
  // Fp₂ over complex plane
84
84
  type BigintTuple = [bigint, bigint];
@@ -124,7 +124,7 @@ const FP2_ORDER =
124
124
  0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaabn **
125
125
  2n;
126
126
 
127
- const Fp2: mod.Field<Fp2> & Fp2Utils = {
127
+ const Fp2: mod.IField<Fp2> & Fp2Utils = {
128
128
  ORDER: FP2_ORDER,
129
129
  BITS: bitLen(FP2_ORDER),
130
130
  BYTES: Math.ceil(bitLen(FP2_ORDER) / 8),
@@ -333,7 +333,7 @@ type Fp6Utils = {
333
333
  multiplyByFp2(lhs: Fp6, rhs: Fp2): Fp6;
334
334
  };
335
335
 
336
- const Fp6: mod.Field<Fp6> & Fp6Utils = {
336
+ const Fp6: mod.IField<Fp6> & Fp6Utils = {
337
337
  ORDER: Fp2.ORDER, // TODO: unused, but need to verify
338
338
  BITS: 3 * Fp2.BITS,
339
339
  BYTES: 3 * Fp2.BYTES,
@@ -545,7 +545,7 @@ type Fp12Utils = {
545
545
  _cyclotomicExp(num: Fp12, n: bigint): Fp12;
546
546
  };
547
547
 
548
- const Fp12: mod.Field<Fp12> & Fp12Utils = {
548
+ const Fp12: mod.IField<Fp12> & Fp12Utils = {
549
549
  ORDER: Fp2.ORDER, // TODO: unused, but need to verify
550
550
  BITS: 2 * Fp2.BITS,
551
551
  BYTES: 2 * Fp2.BYTES,
package/src/bn.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  import { sha256 } from '@noble/hashes/sha256';
3
3
  import { weierstrass } from './abstract/weierstrass.js';
4
4
  import { getHash } from './_shortw_utils.js';
5
- import { Fp } from './abstract/modular.js';
5
+ import { Field } from './abstract/modular.js';
6
6
  /**
7
7
  * bn254 pairing-friendly curve.
8
8
  * Previously known as alt_bn_128, when it had 128-bit security.
@@ -12,7 +12,7 @@ import { Fp } from './abstract/modular.js';
12
12
  export const bn254 = weierstrass({
13
13
  a: BigInt(0),
14
14
  b: BigInt(3),
15
- Fp: Fp(BigInt('0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47')),
15
+ Fp: Field(BigInt('0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47')),
16
16
  n: BigInt('0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001'),
17
17
  Gx: BigInt(1),
18
18
  Gy: BigInt(2),
package/src/ed25519.ts CHANGED
@@ -3,7 +3,7 @@ import { sha512 } from '@noble/hashes/sha512';
3
3
  import { concatBytes, randomBytes, utf8ToBytes } from '@noble/hashes/utils';
4
4
  import { twistedEdwards, ExtPointType } from './abstract/edwards.js';
5
5
  import { montgomery } from './abstract/montgomery.js';
6
- import { mod, pow2, isNegativeLE, Fp as Field, FpSqrtEven } from './abstract/modular.js';
6
+ import { mod, pow2, isNegativeLE, Field, FpSqrtEven } from './abstract/modular.js';
7
7
  import {
8
8
  equalBytes,
9
9
  bytesToHex,
package/src/ed448.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  import { shake256 } from '@noble/hashes/sha3';
3
3
  import { concatBytes, randomBytes, utf8ToBytes, wrapConstructor } from '@noble/hashes/utils';
4
4
  import { twistedEdwards } from './abstract/edwards.js';
5
- import { mod, pow2, Fp as Field } from './abstract/modular.js';
5
+ import { mod, pow2, Field } from './abstract/modular.js';
6
6
  import { montgomery } from './abstract/montgomery.js';
7
7
  import * as htf from './abstract/hash-to-curve.js';
8
8
 
package/src/jubjub.ts CHANGED
@@ -3,7 +3,7 @@ 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';
6
- import { Fp } from './abstract/modular.js';
6
+ import { Field } from './abstract/modular.js';
7
7
 
8
8
  /**
9
9
  * jubjub Twisted Edwards curve.
@@ -17,7 +17,7 @@ export const jubjub = twistedEdwards({
17
17
  d: BigInt('0x2a9318e74bfa2b48f5fd9207e6bd7fd4292d7f6d37579d2601065fd6d6343eb1'),
18
18
  // Finite field 𝔽p over which we'll do calculations
19
19
  // Same value as bls12-381 Fr (not Fp)
20
- Fp: Fp(BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001')),
20
+ Fp: Field(BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001')),
21
21
  // Subgroup order: how many points curve has
22
22
  n: BigInt('0xe7db4ea6533afa906673b0101343b00a6682093ccc81082d0970e5ed6f72cb7'),
23
23
  // Cofactor
package/src/p256.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
2
2
  import { createCurve } from './_shortw_utils.js';
3
3
  import { sha256 } from '@noble/hashes/sha256';
4
- import { Fp as Field } from './abstract/modular.js';
4
+ import { Field } from './abstract/modular.js';
5
5
  import { mapToCurveSimpleSWU } from './abstract/weierstrass.js';
6
6
  import * as htf from './abstract/hash-to-curve.js';
7
7
 
package/src/p384.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
2
2
  import { createCurve } from './_shortw_utils.js';
3
3
  import { sha384 } from '@noble/hashes/sha512';
4
- import { Fp as Field } from './abstract/modular.js';
4
+ import { Field } from './abstract/modular.js';
5
5
  import { mapToCurveSimpleSWU } from './abstract/weierstrass.js';
6
6
  import * as htf from './abstract/hash-to-curve.js';
7
7
 
package/src/p521.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
2
2
  import { createCurve } from './_shortw_utils.js';
3
3
  import { sha512 } from '@noble/hashes/sha512';
4
- import { Fp as Field } from './abstract/modular.js';
4
+ import { Field } from './abstract/modular.js';
5
5
  import { mapToCurveSimpleSWU } from './abstract/weierstrass.js';
6
6
  import * as htf from './abstract/hash-to-curve.js';
7
7
 
package/src/pasta.ts CHANGED
@@ -11,7 +11,7 @@ export const q = BigInt('0x40000000000000000000000000000000224698fc0994a8dd8c46e
11
11
  export const pallas = weierstrass({
12
12
  a: BigInt(0),
13
13
  b: BigInt(5),
14
- Fp: mod.Fp(p),
14
+ Fp: mod.Field(p),
15
15
  n: q,
16
16
  Gx: mod.mod(BigInt(-1), p),
17
17
  Gy: BigInt(2),
@@ -22,7 +22,7 @@ export const pallas = weierstrass({
22
22
  export const vesta = weierstrass({
23
23
  a: BigInt(0),
24
24
  b: BigInt(5),
25
- Fp: mod.Fp(q),
25
+ Fp: mod.Field(q),
26
26
  n: p,
27
27
  Gx: mod.mod(BigInt(-1), q),
28
28
  Gy: BigInt(2),
package/src/secp256k1.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
2
2
  import { sha256 } from '@noble/hashes/sha256';
3
3
  import { randomBytes } from '@noble/hashes/utils';
4
- import { Fp as Field, mod, pow2 } from './abstract/modular.js';
4
+ import { Field, mod, pow2 } from './abstract/modular.js';
5
5
  import { ProjPointType as PointType, mapToCurveSimpleSWU } from './abstract/weierstrass.js';
6
6
  import type { Hex, PrivKey } from './abstract/utils.js';
7
7
  import { bytesToNumberBE, concatBytes, ensureBytes, numberToBytesBE } from './abstract/utils.js';
@@ -43,7 +43,6 @@ function sqrtMod(y: bigint): bigint {
43
43
  }
44
44
 
45
45
  const Fp = Field(secp256k1P, undefined, undefined, { sqrt: sqrtMod });
46
- type Fp = bigint;
47
46
 
48
47
  export const secp256k1 = createCurve(
49
48
  {
@@ -245,7 +244,7 @@ const isoMap = htf.isogenyMap(
245
244
  '0x6484aa716545ca2cf3a70c3fa8fe337e0a3d21162f0d6299a7bf8192bfd2a76f',
246
245
  '0x0000000000000000000000000000000000000000000000000000000000000001', // LAST 1
247
246
  ],
248
- ].map((i) => i.map((j) => BigInt(j))) as [Fp[], Fp[], Fp[], Fp[]]
247
+ ].map((i) => i.map((j) => BigInt(j))) as [bigint[], bigint[], bigint[], bigint[]]
249
248
  );
250
249
  const mapSWU = mapToCurveSimpleSWU(Fp, {
251
250
  A: BigInt('0x3f8731abdd661adca08a5558f0f5d272e953d363cb6f0e5d405447c01a444533'),