@noble/curves 0.3.1 → 0.4.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 (70) hide show
  1. package/README.md +2 -1
  2. package/lib/bls.d.ts +79 -0
  3. package/lib/bls.js +304 -0
  4. package/lib/edwards.d.ts +10 -6
  5. package/lib/edwards.js +16 -11
  6. package/lib/esm/bls.js +300 -0
  7. package/lib/esm/edwards.js +17 -12
  8. package/lib/esm/group.js +2 -2
  9. package/lib/esm/hashToCurve.js +105 -0
  10. package/lib/esm/modular.js +131 -50
  11. package/lib/esm/utils.js +25 -19
  12. package/lib/esm/weierstrass.js +351 -272
  13. package/lib/group.js +2 -2
  14. package/lib/hashToCurve.d.ts +13 -0
  15. package/lib/hashToCurve.js +112 -0
  16. package/lib/modular.d.ts +37 -17
  17. package/lib/modular.js +138 -54
  18. package/lib/utils.d.ts +28 -10
  19. package/lib/utils.js +31 -22
  20. package/lib/weierstrass.d.ts +106 -69
  21. package/lib/weierstrass.js +352 -272
  22. package/package.json +23 -44
  23. package/lib/crypto.d.ts +0 -4
  24. package/lib/crypto.js +0 -8
  25. package/lib/cryptoBrowser.d.ts +0 -4
  26. package/lib/cryptoBrowser.js +0 -7
  27. package/lib/definitions/_shortw_utils.d.ts +0 -63
  28. package/lib/definitions/_shortw_utils.js +0 -18
  29. package/lib/definitions/bn.d.ts +0 -7
  30. package/lib/definitions/bn.js +0 -23
  31. package/lib/definitions/ed25519.d.ts +0 -49
  32. package/lib/definitions/ed25519.js +0 -308
  33. package/lib/definitions/ed448.d.ts +0 -3
  34. package/lib/definitions/ed448.js +0 -127
  35. package/lib/definitions/index.d.ts +0 -0
  36. package/lib/definitions/index.js +0 -2
  37. package/lib/definitions/jubjub.d.ts +0 -7
  38. package/lib/definitions/jubjub.js +0 -55
  39. package/lib/definitions/p192.d.ts +0 -112
  40. package/lib/definitions/p192.js +0 -23
  41. package/lib/definitions/p224.d.ts +0 -112
  42. package/lib/definitions/p224.js +0 -24
  43. package/lib/definitions/p256.d.ts +0 -112
  44. package/lib/definitions/p256.js +0 -23
  45. package/lib/definitions/p384.d.ts +0 -112
  46. package/lib/definitions/p384.js +0 -24
  47. package/lib/definitions/p521.d.ts +0 -113
  48. package/lib/definitions/p521.js +0 -36
  49. package/lib/definitions/pasta.d.ts +0 -2
  50. package/lib/definitions/pasta.js +0 -32
  51. package/lib/definitions/secp256k1.d.ts +0 -87
  52. package/lib/definitions/secp256k1.js +0 -245
  53. package/lib/definitions/stark.d.ts +0 -62
  54. package/lib/definitions/stark.js +0 -248
  55. package/lib/esm/crypto.js +0 -5
  56. package/lib/esm/cryptoBrowser.js +0 -4
  57. package/lib/esm/definitions/_shortw_utils.js +0 -13
  58. package/lib/esm/definitions/bn.js +0 -20
  59. package/lib/esm/definitions/ed25519.js +0 -304
  60. package/lib/esm/definitions/ed448.js +0 -124
  61. package/lib/esm/definitions/index.js +0 -2
  62. package/lib/esm/definitions/jubjub.js +0 -50
  63. package/lib/esm/definitions/p192.js +0 -20
  64. package/lib/esm/definitions/p224.js +0 -21
  65. package/lib/esm/definitions/p256.js +0 -20
  66. package/lib/esm/definitions/p384.js +0 -21
  67. package/lib/esm/definitions/p521.js +0 -33
  68. package/lib/esm/definitions/pasta.js +0 -29
  69. package/lib/esm/definitions/secp256k1.js +0 -241
  70. package/lib/esm/definitions/stark.js +0 -227
@@ -1,12 +1,9 @@
1
1
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
2
- import { BasicCurve, Hex, PrivKey, randomBytes as utilRandomBytes } from './utils.js';
2
+ import * as mod from './modular.js';
3
+ import { Hex, PrivKey } from './utils.js';
4
+ import * as utils from './utils.js';
5
+ import { htfOpts } from './hashToCurve.js';
3
6
  import { Group, GroupConstructor } from './group.js';
4
- export declare type CHash = {
5
- (message: Uint8Array | string): Uint8Array;
6
- blockLen: number;
7
- outputLen: number;
8
- create(): any;
9
- };
10
7
  declare type HmacFnSync = (key: Uint8Array, ...messages: Uint8Array[]) => Uint8Array;
11
8
  declare type EndomorphismOpts = {
12
9
  beta: bigint;
@@ -17,37 +14,19 @@ declare type EndomorphismOpts = {
17
14
  k2: bigint;
18
15
  };
19
16
  };
20
- export declare type CurveType = BasicCurve & {
21
- a: bigint;
22
- b: bigint;
23
- lowS?: boolean;
24
- hash: CHash;
25
- hmac: HmacFnSync;
26
- randomBytes?: (bytesLength?: number) => Uint8Array;
27
- truncateHash?: (hash: Uint8Array, truncateOnly?: boolean) => bigint;
28
- sqrtMod?: (n: bigint) => bigint;
17
+ export declare type BasicCurve<T> = utils.BasicCurve<T> & {
18
+ a: T;
19
+ b: T;
29
20
  normalizePrivateKey?: (key: PrivKey) => PrivKey;
30
21
  endo?: EndomorphismOpts;
22
+ isTorsionFree?: (c: JacobianConstructor<T>, point: JacobianPointType<T>) => boolean;
23
+ clearCofactor?: (c: JacobianConstructor<T>, point: JacobianPointType<T>) => JacobianPointType<T>;
24
+ htfDefaults?: htfOpts;
25
+ mapToCurve?: (scalar: bigint[]) => {
26
+ x: T;
27
+ y: T;
28
+ };
31
29
  };
32
- declare function validateOpts(curve: CurveType): Readonly<{
33
- readonly nBitLength: number;
34
- readonly nByteLength: number;
35
- readonly P: bigint;
36
- readonly n: bigint;
37
- readonly h: bigint;
38
- readonly Gx: bigint;
39
- readonly Gy: bigint;
40
- readonly a: bigint;
41
- readonly b: bigint;
42
- lowS: boolean;
43
- readonly hash: CHash;
44
- readonly hmac: HmacFnSync;
45
- randomBytes: typeof utilRandomBytes;
46
- readonly truncateHash?: ((hash: Uint8Array, truncateOnly?: boolean | undefined) => bigint) | undefined;
47
- readonly sqrtMod?: ((n: bigint) => bigint) | undefined;
48
- readonly normalizePrivateKey?: ((key: PrivKey) => PrivKey) | undefined;
49
- readonly endo?: EndomorphismOpts | undefined;
50
- }>;
51
30
  declare type Entropy = Hex | true;
52
31
  declare type SignOpts = {
53
32
  lowS?: boolean;
@@ -74,6 +53,58 @@ declare type SignOpts = {
74
53
  *
75
54
  * TODO: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html#unique-symbol
76
55
  */
56
+ export interface JacobianPointType<T> extends Group<JacobianPointType<T>> {
57
+ readonly x: T;
58
+ readonly y: T;
59
+ readonly z: T;
60
+ multiply(scalar: number | bigint, affinePoint?: PointType<T>): JacobianPointType<T>;
61
+ multiplyUnsafe(scalar: bigint): JacobianPointType<T>;
62
+ toAffine(invZ?: T): PointType<T>;
63
+ }
64
+ export interface JacobianConstructor<T> extends GroupConstructor<JacobianPointType<T>> {
65
+ new (x: T, y: T, z: T): JacobianPointType<T>;
66
+ fromAffine(p: PointType<T>): JacobianPointType<T>;
67
+ toAffineBatch(points: JacobianPointType<T>[]): PointType<T>[];
68
+ normalizeZ(points: JacobianPointType<T>[]): JacobianPointType<T>[];
69
+ }
70
+ export interface PointType<T> extends Group<PointType<T>> {
71
+ readonly x: T;
72
+ readonly y: T;
73
+ _setWindowSize(windowSize: number): void;
74
+ hasEvenY(): boolean;
75
+ toRawBytes(isCompressed?: boolean): Uint8Array;
76
+ toHex(isCompressed?: boolean): string;
77
+ assertValidity(): void;
78
+ multiplyAndAddUnsafe(Q: PointType<T>, a: bigint, b: bigint): PointType<T> | undefined;
79
+ }
80
+ export interface PointConstructor<T> extends GroupConstructor<PointType<T>> {
81
+ new (x: T, y: T): PointType<T>;
82
+ fromHex(hex: Hex): PointType<T>;
83
+ fromPrivateKey(privateKey: PrivKey): PointType<T>;
84
+ hashToCurve(msg: Hex, options?: Partial<htfOpts>): PointType<T>;
85
+ encodeToCurve(msg: Hex, options?: Partial<htfOpts>): PointType<T>;
86
+ }
87
+ export declare type CurvePointsType<T> = BasicCurve<T> & {
88
+ fromBytes: (bytes: Uint8Array) => {
89
+ x: T;
90
+ y: T;
91
+ };
92
+ toBytes: (c: PointConstructor<T>, point: PointType<T>, compressed: boolean) => Uint8Array;
93
+ };
94
+ export declare type CurvePointsRes<T> = {
95
+ Point: PointConstructor<T>;
96
+ JacobianPoint: JacobianConstructor<T>;
97
+ normalizePrivateKey: (key: PrivKey) => bigint;
98
+ weierstrassEquation: (x: T) => T;
99
+ isWithinCurveOrder: (num: bigint) => boolean;
100
+ };
101
+ export declare function weierstrassPoints<T>(opts: CurvePointsType<T>): {
102
+ Point: PointConstructor<T>;
103
+ JacobianPoint: JacobianConstructor<T>;
104
+ normalizePrivateKey: (key: PrivKey) => bigint;
105
+ weierstrassEquation: (x: T) => T;
106
+ isWithinCurveOrder: (num: bigint) => boolean;
107
+ };
77
108
  export interface SignatureType {
78
109
  readonly r: bigint;
79
110
  readonly s: bigint;
@@ -82,7 +113,7 @@ export interface SignatureType {
82
113
  copyWithRecoveryBit(recovery: number): SignatureType;
83
114
  hasHighS(): boolean;
84
115
  normalizeS(): SignatureType;
85
- recoverPublicKey(msgHash: Hex): PointType;
116
+ recoverPublicKey(msgHash: Hex): PointType<bigint>;
86
117
  toDERRawBytes(isCompressed?: boolean): Uint8Array;
87
118
  toDERHex(isCompressed?: boolean): string;
88
119
  toCompactRawBytes(): Uint8Array;
@@ -93,36 +124,42 @@ export declare type SignatureConstructor = {
93
124
  fromCompact(hex: Hex): SignatureType;
94
125
  fromDER(hex: Hex): SignatureType;
95
126
  };
96
- export interface JacobianPointType extends Group<JacobianPointType> {
97
- readonly x: bigint;
98
- readonly y: bigint;
99
- readonly z: bigint;
100
- multiply(scalar: number | bigint, affinePoint?: PointType): JacobianPointType;
101
- multiplyUnsafe(scalar: bigint): JacobianPointType;
102
- toAffine(invZ?: bigint): PointType;
103
- }
104
- export interface JacobianPointConstructor extends GroupConstructor<JacobianPointType> {
105
- new (x: bigint, y: bigint, z: bigint): JacobianPointType;
106
- fromAffine(p: PointType): JacobianPointType;
107
- toAffineBatch(points: JacobianPointType[]): PointType[];
108
- normalizeZ(points: JacobianPointType[]): JacobianPointType[];
109
- }
110
- export interface PointType extends Group<PointType> {
111
- readonly x: bigint;
112
- readonly y: bigint;
113
- _setWindowSize(windowSize: number): void;
114
- hasEvenY(): boolean;
115
- toRawBytes(isCompressed?: boolean): Uint8Array;
116
- toHex(isCompressed?: boolean): string;
117
- assertValidity(): void;
118
- multiplyAndAddUnsafe(Q: PointType, a: bigint, b: bigint): PointType | undefined;
119
- }
120
- export interface PointConstructor extends GroupConstructor<PointType> {
121
- new (x: bigint, y: bigint): PointType;
122
- fromHex(hex: Hex): PointType;
123
- fromPrivateKey(privateKey: PrivKey): PointType;
124
- }
125
- export declare type PubKey = Hex | PointType;
127
+ export declare type PubKey = Hex | PointType<bigint>;
128
+ export declare type CurveType = BasicCurve<bigint> & {
129
+ lowS?: boolean;
130
+ hash: utils.CHash;
131
+ hmac: HmacFnSync;
132
+ randomBytes: (bytesLength?: number) => Uint8Array;
133
+ truncateHash?: (hash: Uint8Array, truncateOnly?: boolean) => bigint;
134
+ };
135
+ declare function validateOpts(curve: CurveType): Readonly<{
136
+ readonly nBitLength: number;
137
+ readonly nByteLength: number;
138
+ readonly Fp: mod.Field<bigint>;
139
+ readonly n: bigint;
140
+ readonly h: bigint;
141
+ readonly hEff?: bigint | undefined;
142
+ readonly Gx: bigint;
143
+ readonly Gy: bigint;
144
+ readonly wrapPrivateKey?: boolean | undefined;
145
+ readonly allowInfinityPoint?: boolean | undefined;
146
+ readonly a: bigint;
147
+ readonly b: bigint;
148
+ readonly normalizePrivateKey?: ((key: PrivKey) => PrivKey) | undefined;
149
+ readonly endo?: EndomorphismOpts | undefined;
150
+ readonly isTorsionFree?: ((c: JacobianConstructor<bigint>, point: JacobianPointType<bigint>) => boolean) | undefined;
151
+ readonly clearCofactor?: ((c: JacobianConstructor<bigint>, point: JacobianPointType<bigint>) => JacobianPointType<bigint>) | undefined;
152
+ readonly htfDefaults?: htfOpts | undefined;
153
+ readonly mapToCurve?: ((scalar: bigint[]) => {
154
+ x: bigint;
155
+ y: bigint;
156
+ }) | undefined;
157
+ lowS: boolean;
158
+ readonly hash: utils.CHash;
159
+ readonly hmac: HmacFnSync;
160
+ readonly randomBytes: (bytesLength?: number | undefined) => Uint8Array;
161
+ readonly truncateHash?: ((hash: Uint8Array, truncateOnly?: boolean | undefined) => bigint) | undefined;
162
+ }>;
126
163
  export declare type CurveFn = {
127
164
  CURVE: ReturnType<typeof validateOpts>;
128
165
  getPublicKey: (privateKey: PrivKey, isCompressed?: boolean) => Uint8Array;
@@ -131,8 +168,8 @@ export declare type CurveFn = {
131
168
  verify: (signature: Hex | SignatureType, msgHash: Hex, publicKey: PubKey, opts?: {
132
169
  lowS?: boolean;
133
170
  }) => boolean;
134
- Point: PointConstructor;
135
- JacobianPoint: JacobianPointConstructor;
171
+ Point: PointConstructor<bigint>;
172
+ JacobianPoint: JacobianConstructor<bigint>;
136
173
  Signature: SignatureConstructor;
137
174
  utils: {
138
175
  mod: (a: bigint, b?: bigint) => bigint;
@@ -140,7 +177,7 @@ export declare type CurveFn = {
140
177
  _bigintToBytes: (num: bigint) => Uint8Array;
141
178
  _bigintToString: (num: bigint) => string;
142
179
  _normalizePrivateKey: (key: PrivKey) => bigint;
143
- _normalizePublicKey: (publicKey: PubKey) => PointType;
180
+ _normalizePublicKey: (publicKey: PubKey) => PointType<bigint>;
144
181
  _isWithinCurveOrder: (num: bigint) => boolean;
145
182
  _isValidFieldElement: (num: bigint) => boolean;
146
183
  _weierstrassEquation: (x: bigint) => bigint;