@noble/curves 0.5.2 → 0.6.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 (59) hide show
  1. package/README.md +49 -5
  2. package/lib/_shortw_utils.d.ts +10 -21
  3. package/lib/abstract/bls.d.ts +39 -32
  4. package/lib/abstract/bls.js +74 -73
  5. package/lib/abstract/{group.d.ts → curve.d.ts} +31 -1
  6. package/lib/abstract/{group.js → curve.js} +39 -2
  7. package/lib/abstract/edwards.d.ts +30 -72
  8. package/lib/abstract/edwards.js +197 -375
  9. package/lib/abstract/hash-to-curve.d.ts +25 -6
  10. package/lib/abstract/hash-to-curve.js +40 -12
  11. package/lib/abstract/modular.d.ts +20 -7
  12. package/lib/abstract/modular.js +61 -35
  13. package/lib/abstract/montgomery.js +4 -5
  14. package/lib/abstract/poseidon.d.ts +29 -0
  15. package/lib/abstract/poseidon.js +115 -0
  16. package/lib/abstract/utils.d.ts +5 -36
  17. package/lib/abstract/utils.js +23 -71
  18. package/lib/abstract/weierstrass.d.ts +51 -74
  19. package/lib/abstract/weierstrass.js +455 -628
  20. package/lib/bls12-381.js +63 -58
  21. package/lib/bn.js +1 -1
  22. package/lib/ed25519.d.ts +7 -5
  23. package/lib/ed25519.js +82 -79
  24. package/lib/ed448.d.ts +3 -0
  25. package/lib/ed448.js +86 -83
  26. package/lib/esm/abstract/bls.js +75 -74
  27. package/lib/esm/abstract/{group.js → curve.js} +37 -1
  28. package/lib/esm/abstract/edwards.js +196 -374
  29. package/lib/esm/abstract/hash-to-curve.js +38 -11
  30. package/lib/esm/abstract/modular.js +58 -34
  31. package/lib/esm/abstract/montgomery.js +5 -6
  32. package/lib/esm/abstract/poseidon.js +109 -0
  33. package/lib/esm/abstract/utils.js +21 -66
  34. package/lib/esm/abstract/weierstrass.js +454 -627
  35. package/lib/esm/bls12-381.js +75 -70
  36. package/lib/esm/bn.js +1 -1
  37. package/lib/esm/ed25519.js +80 -78
  38. package/lib/esm/ed448.js +84 -82
  39. package/lib/esm/jubjub.js +1 -1
  40. package/lib/esm/p256.js +11 -9
  41. package/lib/esm/p384.js +11 -9
  42. package/lib/esm/p521.js +13 -12
  43. package/lib/esm/secp256k1.js +115 -151
  44. package/lib/esm/stark.js +104 -40
  45. package/lib/jubjub.d.ts +2 -2
  46. package/lib/jubjub.js +1 -1
  47. package/lib/p192.d.ts +20 -42
  48. package/lib/p224.d.ts +20 -42
  49. package/lib/p256.d.ts +23 -42
  50. package/lib/p256.js +13 -10
  51. package/lib/p384.d.ts +23 -42
  52. package/lib/p384.js +13 -10
  53. package/lib/p521.d.ts +23 -42
  54. package/lib/p521.js +15 -13
  55. package/lib/secp256k1.d.ts +25 -37
  56. package/lib/secp256k1.js +115 -151
  57. package/lib/stark.d.ts +36 -19
  58. package/lib/stark.js +107 -40
  59. package/package.json +13 -8
@@ -1,11 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.wNAF = void 0;
3
+ exports.validateAbsOpts = exports.wNAF = void 0;
4
4
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
5
5
  // Abelian group utilities
6
+ const modular_js_1 = require("./modular.js");
6
7
  const _0n = BigInt(0);
7
8
  const _1n = BigInt(1);
8
- // Not big, but pretty complex and it is easy to break stuff. To avoid too much copy paste
9
+ // Elliptic curve multiplication of Point by scalar. Complicated and fragile. Uses wNAF method.
10
+ // Windowed method is 10% faster, but takes 2x longer to generate & consumes 2x memory.
9
11
  function wNAF(c, bits) {
10
12
  const constTimeNegate = (condition, item) => {
11
13
  const neg = item.negate();
@@ -107,6 +109,41 @@ function wNAF(c, bits) {
107
109
  // which makes it less const-time: around 1 bigint multiply.
108
110
  return { p, f };
109
111
  },
112
+ wNAFCached(P, precomputesMap, n, transform) {
113
+ // @ts-ignore
114
+ const W = P._WINDOW_SIZE || 1;
115
+ // Calculate precomputes on a first run, reuse them after
116
+ let comp = precomputesMap.get(P);
117
+ if (!comp) {
118
+ comp = this.precomputeWindow(P, W);
119
+ if (W !== 1) {
120
+ precomputesMap.set(P, transform(comp));
121
+ }
122
+ }
123
+ return this.wNAF(W, comp, n);
124
+ },
110
125
  };
111
126
  }
112
127
  exports.wNAF = wNAF;
128
+ function validateAbsOpts(curve) {
129
+ (0, modular_js_1.validateField)(curve.Fp);
130
+ for (const i of ['n', 'h']) {
131
+ const val = curve[i];
132
+ if (typeof val !== 'bigint')
133
+ throw new Error(`Invalid curve param ${i}=${val} (${typeof val})`);
134
+ }
135
+ if (!curve.Fp.isValid(curve.Gx))
136
+ throw new Error('Invalid generator X coordinate Fp element');
137
+ if (!curve.Fp.isValid(curve.Gy))
138
+ throw new Error('Invalid generator Y coordinate Fp element');
139
+ for (const i of ['nBitLength', 'nByteLength']) {
140
+ const val = curve[i];
141
+ if (val === undefined)
142
+ continue; // Optional
143
+ if (!Number.isSafeInteger(val))
144
+ throw new Error(`Invalid param ${i}=${val} (${typeof val})`);
145
+ }
146
+ // Set defaults
147
+ return Object.freeze({ ...(0, modular_js_1.nLength)(curve.n, curve.nBitLength), ...curve });
148
+ }
149
+ exports.validateAbsOpts = validateAbsOpts;
@@ -1,13 +1,9 @@
1
- /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
2
- import * as mod from './modular.js';
3
- import * as ut from './utils.js';
4
- import { Hex, PrivKey } from './utils.js';
5
- import { Group, GroupConstructor } from './group.js';
6
- import { htfOpts } from './hash-to-curve.js';
7
- export declare type CurveType = ut.BasicCurve<bigint> & {
1
+ import { FHash, Hex } from './utils.js';
2
+ import { Group, GroupConstructor, AbstractCurve, AffinePoint } from './curve.js';
3
+ export declare type CurveType = AbstractCurve<bigint> & {
8
4
  a: bigint;
9
5
  d: bigint;
10
- hash: ut.CHash;
6
+ hash: FHash;
11
7
  randomBytes: (bytesLength?: number) => Uint8Array;
12
8
  adjustScalarBytes?: (bytes: Uint8Array) => Uint8Array;
13
9
  domain?: (data: Uint8Array, ctx: Uint8Array, phflag: boolean) => Uint8Array;
@@ -15,17 +11,13 @@ export declare type CurveType = ut.BasicCurve<bigint> & {
15
11
  isValid: boolean;
16
12
  value: bigint;
17
13
  };
18
- preHash?: ut.CHash;
19
- htfDefaults?: htfOpts;
20
- mapToCurve?: (scalar: bigint[]) => {
21
- x: bigint;
22
- y: bigint;
23
- };
14
+ preHash?: FHash;
15
+ mapToCurve?: (scalar: bigint[]) => AffinePoint<bigint>;
24
16
  };
25
17
  declare function validateOpts(curve: CurveType): Readonly<{
26
18
  readonly nBitLength: number;
27
19
  readonly nByteLength: number;
28
- readonly Fp: mod.Field<bigint>;
20
+ readonly Fp: import("./modular.js").Field<bigint>;
29
21
  readonly n: bigint;
30
22
  readonly h: bigint;
31
23
  readonly hEff?: bigint | undefined;
@@ -35,7 +27,7 @@ declare function validateOpts(curve: CurveType): Readonly<{
35
27
  readonly allowInfinityPoint?: boolean | undefined;
36
28
  readonly a: bigint;
37
29
  readonly d: bigint;
38
- readonly hash: ut.CHash;
30
+ readonly hash: FHash;
39
31
  readonly randomBytes: (bytesLength?: number | undefined) => Uint8Array;
40
32
  readonly adjustScalarBytes?: ((bytes: Uint8Array) => Uint8Array) | undefined;
41
33
  readonly domain?: ((data: Uint8Array, ctx: Uint8Array, phflag: boolean) => Uint8Array) | undefined;
@@ -43,75 +35,41 @@ declare function validateOpts(curve: CurveType): Readonly<{
43
35
  isValid: boolean;
44
36
  value: bigint;
45
37
  }) | undefined;
46
- readonly preHash?: ut.CHash | undefined;
47
- readonly htfDefaults?: htfOpts | undefined;
48
- readonly mapToCurve?: ((scalar: bigint[]) => {
49
- x: bigint;
50
- y: bigint;
51
- }) | undefined;
38
+ readonly preHash?: FHash | undefined;
39
+ readonly mapToCurve?: ((scalar: bigint[]) => AffinePoint<bigint>) | undefined;
52
40
  }>;
53
- export interface SignatureType {
54
- readonly r: PointType;
55
- readonly s: bigint;
56
- assertValidity(): SignatureType;
57
- toRawBytes(): Uint8Array;
58
- toHex(): string;
59
- }
60
- export declare type SignatureConstructor = {
61
- new (r: PointType, s: bigint): SignatureType;
62
- fromHex(hex: Hex): SignatureType;
63
- };
64
- export interface ExtendedPointType extends Group<ExtendedPointType> {
65
- readonly x: bigint;
66
- readonly y: bigint;
67
- readonly z: bigint;
68
- readonly t: bigint;
69
- multiply(scalar: number | bigint, affinePoint?: PointType): ExtendedPointType;
70
- multiplyUnsafe(scalar: number | bigint): ExtendedPointType;
41
+ export interface ExtPointType extends Group<ExtPointType> {
42
+ readonly ex: bigint;
43
+ readonly ey: bigint;
44
+ readonly ez: bigint;
45
+ readonly et: bigint;
46
+ assertValidity(): void;
47
+ multiply(scalar: bigint): ExtPointType;
48
+ multiplyUnsafe(scalar: bigint): ExtPointType;
71
49
  isSmallOrder(): boolean;
72
50
  isTorsionFree(): boolean;
73
- toAffine(invZ?: bigint): PointType;
74
- clearCofactor(): ExtendedPointType;
75
- }
76
- export interface ExtendedPointConstructor extends GroupConstructor<ExtendedPointType> {
77
- new (x: bigint, y: bigint, z: bigint, t: bigint): ExtendedPointType;
78
- fromAffine(p: PointType): ExtendedPointType;
79
- toAffineBatch(points: ExtendedPointType[]): PointType[];
80
- normalizeZ(points: ExtendedPointType[]): ExtendedPointType[];
81
- }
82
- export interface PointType extends Group<PointType> {
83
- readonly x: bigint;
84
- readonly y: bigint;
85
- _setWindowSize(windowSize: number): void;
86
- toRawBytes(isCompressed?: boolean): Uint8Array;
87
- toHex(isCompressed?: boolean): string;
88
- isTorsionFree(): boolean;
89
- clearCofactor(): PointType;
51
+ clearCofactor(): ExtPointType;
52
+ toAffine(iz?: bigint): AffinePoint<bigint>;
90
53
  }
91
- export interface PointConstructor extends GroupConstructor<PointType> {
92
- new (x: bigint, y: bigint): PointType;
93
- fromHex(hex: Hex): PointType;
94
- fromPrivateKey(privateKey: PrivKey): PointType;
95
- hashToCurve(msg: Hex, options?: Partial<htfOpts>): PointType;
96
- encodeToCurve(msg: Hex, options?: Partial<htfOpts>): PointType;
54
+ export interface ExtPointConstructor extends GroupConstructor<ExtPointType> {
55
+ new (x: bigint, y: bigint, z: bigint, t: bigint): ExtPointType;
56
+ fromAffine(p: AffinePoint<bigint>): ExtPointType;
57
+ fromHex(hex: Hex): ExtPointType;
58
+ fromPrivateKey(privateKey: Hex): ExtPointType;
97
59
  }
98
- export declare type PubKey = Hex | PointType;
99
- export declare type SigType = Hex | SignatureType;
100
60
  export declare type CurveFn = {
101
61
  CURVE: ReturnType<typeof validateOpts>;
102
- getPublicKey: (privateKey: PrivKey, isCompressed?: boolean) => Uint8Array;
62
+ getPublicKey: (privateKey: Hex) => Uint8Array;
103
63
  sign: (message: Hex, privateKey: Hex) => Uint8Array;
104
- verify: (sig: SigType, message: Hex, publicKey: PubKey) => boolean;
105
- Point: PointConstructor;
106
- ExtendedPoint: ExtendedPointConstructor;
107
- Signature: SignatureConstructor;
64
+ verify: (sig: Hex, message: Hex, publicKey: Hex) => boolean;
65
+ ExtendedPoint: ExtPointConstructor;
108
66
  utils: {
109
67
  randomPrivateKey: () => Uint8Array;
110
- getExtendedPublicKey: (key: PrivKey) => {
68
+ getExtendedPublicKey: (key: Hex) => {
111
69
  head: Uint8Array;
112
70
  prefix: Uint8Array;
113
71
  scalar: bigint;
114
- point: PointType;
72
+ point: ExtPointType;
115
73
  pointBytes: Uint8Array;
116
74
  };
117
75
  };