@noble/curves 1.9.5 → 2.0.0-beta.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.
Files changed (211) hide show
  1. package/README.md +267 -421
  2. package/abstract/bls.d.ts +49 -111
  3. package/abstract/bls.d.ts.map +1 -1
  4. package/abstract/bls.js +108 -152
  5. package/abstract/bls.js.map +1 -1
  6. package/abstract/curve.d.ts +7 -48
  7. package/abstract/curve.d.ts.map +1 -1
  8. package/abstract/curve.js +22 -47
  9. package/abstract/curve.js.map +1 -1
  10. package/abstract/edwards.d.ts +17 -68
  11. package/abstract/edwards.d.ts.map +1 -1
  12. package/abstract/edwards.js +98 -175
  13. package/abstract/edwards.js.map +1 -1
  14. package/abstract/fft.js +14 -27
  15. package/abstract/fft.js.map +1 -1
  16. package/abstract/hash-to-curve.d.ts +11 -24
  17. package/abstract/hash-to-curve.d.ts.map +1 -1
  18. package/abstract/hash-to-curve.js +30 -35
  19. package/abstract/hash-to-curve.js.map +1 -1
  20. package/abstract/modular.d.ts +5 -17
  21. package/abstract/modular.d.ts.map +1 -1
  22. package/abstract/modular.js +166 -167
  23. package/abstract/modular.js.map +1 -1
  24. package/abstract/montgomery.d.ts +4 -9
  25. package/abstract/montgomery.d.ts.map +1 -1
  26. package/abstract/montgomery.js +17 -20
  27. package/abstract/montgomery.js.map +1 -1
  28. package/abstract/oprf.d.ts +282 -0
  29. package/abstract/oprf.d.ts.map +1 -0
  30. package/abstract/oprf.js +297 -0
  31. package/abstract/oprf.js.map +1 -0
  32. package/abstract/poseidon.js +20 -24
  33. package/abstract/poseidon.js.map +1 -1
  34. package/abstract/tower.d.ts +9 -7
  35. package/abstract/tower.d.ts.map +1 -1
  36. package/abstract/tower.js +600 -364
  37. package/abstract/tower.js.map +1 -1
  38. package/abstract/weierstrass.d.ts +12 -145
  39. package/abstract/weierstrass.d.ts.map +1 -1
  40. package/abstract/weierstrass.js +153 -377
  41. package/abstract/weierstrass.js.map +1 -1
  42. package/bls12-381.d.ts +2 -2
  43. package/bls12-381.d.ts.map +1 -1
  44. package/bls12-381.js +174 -216
  45. package/bls12-381.js.map +1 -1
  46. package/bn254.d.ts +58 -10
  47. package/bn254.d.ts.map +1 -1
  48. package/bn254.js +70 -130
  49. package/bn254.js.map +1 -1
  50. package/ed25519.d.ts +12 -31
  51. package/ed25519.d.ts.map +1 -1
  52. package/ed25519.js +104 -146
  53. package/ed25519.js.map +1 -1
  54. package/ed448.d.ts +14 -33
  55. package/ed448.d.ts.map +1 -1
  56. package/ed448.js +105 -132
  57. package/ed448.js.map +1 -1
  58. package/index.js +1 -1
  59. package/misc.d.ts +10 -14
  60. package/misc.d.ts.map +1 -1
  61. package/misc.js +51 -60
  62. package/misc.js.map +1 -1
  63. package/nist.d.ts +11 -14
  64. package/nist.d.ts.map +1 -1
  65. package/nist.js +46 -55
  66. package/nist.js.map +1 -1
  67. package/package.json +9 -224
  68. package/secp256k1.d.ts +7 -23
  69. package/secp256k1.d.ts.map +1 -1
  70. package/secp256k1.js +72 -83
  71. package/secp256k1.js.map +1 -1
  72. package/src/abstract/bls.ts +197 -344
  73. package/src/abstract/curve.ts +10 -83
  74. package/src/abstract/edwards.ts +96 -223
  75. package/src/abstract/hash-to-curve.ts +32 -45
  76. package/src/abstract/modular.ts +144 -130
  77. package/src/abstract/montgomery.ts +21 -22
  78. package/src/abstract/oprf.ts +600 -0
  79. package/src/abstract/tower.ts +627 -382
  80. package/src/abstract/weierstrass.ts +101 -482
  81. package/src/bls12-381.ts +148 -176
  82. package/src/bn254.ts +67 -122
  83. package/src/ed25519.ts +65 -118
  84. package/src/ed448.ts +63 -113
  85. package/src/index.ts +1 -1
  86. package/src/misc.ts +66 -49
  87. package/src/nist.ts +48 -57
  88. package/src/secp256k1.ts +56 -88
  89. package/src/utils.ts +41 -61
  90. package/src/webcrypto.ts +362 -0
  91. package/utils.d.ts +28 -19
  92. package/utils.d.ts.map +1 -1
  93. package/utils.js +45 -121
  94. package/utils.js.map +1 -1
  95. package/webcrypto.d.ts +47 -0
  96. package/webcrypto.d.ts.map +1 -0
  97. package/webcrypto.js +231 -0
  98. package/webcrypto.js.map +1 -0
  99. package/esm/_shortw_utils.d.ts +0 -19
  100. package/esm/_shortw_utils.d.ts.map +0 -1
  101. package/esm/_shortw_utils.js +0 -16
  102. package/esm/_shortw_utils.js.map +0 -1
  103. package/esm/abstract/bls.d.ts +0 -190
  104. package/esm/abstract/bls.d.ts.map +0 -1
  105. package/esm/abstract/bls.js +0 -408
  106. package/esm/abstract/bls.js.map +0 -1
  107. package/esm/abstract/curve.d.ts +0 -231
  108. package/esm/abstract/curve.d.ts.map +0 -1
  109. package/esm/abstract/curve.js +0 -465
  110. package/esm/abstract/curve.js.map +0 -1
  111. package/esm/abstract/edwards.d.ts +0 -237
  112. package/esm/abstract/edwards.d.ts.map +0 -1
  113. package/esm/abstract/edwards.js +0 -632
  114. package/esm/abstract/edwards.js.map +0 -1
  115. package/esm/abstract/fft.d.ts +0 -122
  116. package/esm/abstract/fft.d.ts.map +0 -1
  117. package/esm/abstract/fft.js +0 -425
  118. package/esm/abstract/fft.js.map +0 -1
  119. package/esm/abstract/hash-to-curve.d.ts +0 -102
  120. package/esm/abstract/hash-to-curve.d.ts.map +0 -1
  121. package/esm/abstract/hash-to-curve.js +0 -203
  122. package/esm/abstract/hash-to-curve.js.map +0 -1
  123. package/esm/abstract/modular.d.ts +0 -171
  124. package/esm/abstract/modular.d.ts.map +0 -1
  125. package/esm/abstract/modular.js +0 -530
  126. package/esm/abstract/modular.js.map +0 -1
  127. package/esm/abstract/montgomery.d.ts +0 -30
  128. package/esm/abstract/montgomery.d.ts.map +0 -1
  129. package/esm/abstract/montgomery.js +0 -157
  130. package/esm/abstract/montgomery.js.map +0 -1
  131. package/esm/abstract/poseidon.d.ts +0 -68
  132. package/esm/abstract/poseidon.d.ts.map +0 -1
  133. package/esm/abstract/poseidon.js +0 -296
  134. package/esm/abstract/poseidon.js.map +0 -1
  135. package/esm/abstract/tower.d.ts +0 -93
  136. package/esm/abstract/tower.d.ts.map +0 -1
  137. package/esm/abstract/tower.js +0 -502
  138. package/esm/abstract/tower.js.map +0 -1
  139. package/esm/abstract/utils.d.ts +0 -5
  140. package/esm/abstract/utils.d.ts.map +0 -1
  141. package/esm/abstract/utils.js +0 -7
  142. package/esm/abstract/utils.js.map +0 -1
  143. package/esm/abstract/weierstrass.d.ts +0 -412
  144. package/esm/abstract/weierstrass.d.ts.map +0 -1
  145. package/esm/abstract/weierstrass.js +0 -1428
  146. package/esm/abstract/weierstrass.js.map +0 -1
  147. package/esm/bls12-381.d.ts +0 -16
  148. package/esm/bls12-381.d.ts.map +0 -1
  149. package/esm/bls12-381.js +0 -738
  150. package/esm/bls12-381.js.map +0 -1
  151. package/esm/bn254.d.ts +0 -18
  152. package/esm/bn254.d.ts.map +0 -1
  153. package/esm/bn254.js +0 -246
  154. package/esm/bn254.js.map +0 -1
  155. package/esm/ed25519.d.ts +0 -106
  156. package/esm/ed25519.d.ts.map +0 -1
  157. package/esm/ed25519.js +0 -467
  158. package/esm/ed25519.js.map +0 -1
  159. package/esm/ed448.d.ts +0 -101
  160. package/esm/ed448.d.ts.map +0 -1
  161. package/esm/ed448.js +0 -448
  162. package/esm/ed448.js.map +0 -1
  163. package/esm/index.d.ts +0 -2
  164. package/esm/index.d.ts.map +0 -1
  165. package/esm/index.js +0 -17
  166. package/esm/index.js.map +0 -1
  167. package/esm/jubjub.d.ts +0 -12
  168. package/esm/jubjub.d.ts.map +0 -1
  169. package/esm/jubjub.js +0 -12
  170. package/esm/jubjub.js.map +0 -1
  171. package/esm/misc.d.ts +0 -19
  172. package/esm/misc.d.ts.map +0 -1
  173. package/esm/misc.js +0 -109
  174. package/esm/misc.js.map +0 -1
  175. package/esm/nist.d.ts +0 -21
  176. package/esm/nist.d.ts.map +0 -1
  177. package/esm/nist.js +0 -132
  178. package/esm/nist.js.map +0 -1
  179. package/esm/p256.d.ts +0 -16
  180. package/esm/p256.d.ts.map +0 -1
  181. package/esm/p256.js +0 -16
  182. package/esm/p256.js.map +0 -1
  183. package/esm/p384.d.ts +0 -16
  184. package/esm/p384.d.ts.map +0 -1
  185. package/esm/p384.js +0 -16
  186. package/esm/p384.js.map +0 -1
  187. package/esm/p521.d.ts +0 -16
  188. package/esm/p521.d.ts.map +0 -1
  189. package/esm/p521.js +0 -16
  190. package/esm/p521.js.map +0 -1
  191. package/esm/package.json +0 -4
  192. package/esm/pasta.d.ts +0 -10
  193. package/esm/pasta.d.ts.map +0 -1
  194. package/esm/pasta.js +0 -10
  195. package/esm/pasta.js.map +0 -1
  196. package/esm/secp256k1.d.ts +0 -89
  197. package/esm/secp256k1.d.ts.map +0 -1
  198. package/esm/secp256k1.js +0 -292
  199. package/esm/secp256k1.js.map +0 -1
  200. package/esm/utils.d.ts +0 -110
  201. package/esm/utils.d.ts.map +0 -1
  202. package/esm/utils.js +0 -322
  203. package/esm/utils.js.map +0 -1
  204. package/src/_shortw_utils.ts +0 -21
  205. package/src/abstract/utils.ts +0 -7
  206. package/src/jubjub.ts +0 -12
  207. package/src/p256.ts +0 -15
  208. package/src/p384.ts +0 -15
  209. package/src/p521.ts +0 -15
  210. package/src/package.json +0 -3
  211. package/src/pasta.ts +0 -9
@@ -15,35 +15,19 @@
15
15
  * @module
16
16
  **/
17
17
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
18
- import {
19
- abytes,
20
- ensureBytes,
21
- memoized,
22
- randomBytes,
23
- type CHash,
24
- type Hex,
25
- type PrivKey,
26
- } from '../utils.ts';
27
- import { normalizeZ } from './curve.ts';
18
+ import { abytes, memoized, notImplemented, randomBytes } from '../utils.ts';
19
+ import { normalizeZ, type CurveLengths } from './curve.ts';
28
20
  import {
29
21
  createHasher,
30
22
  type H2CHasher,
31
23
  type H2CHashOpts,
24
+ type H2CMethod,
32
25
  type H2COpts,
33
- type H2CPointConstructor,
34
- type htfBasicOpts,
35
26
  type MapToCurve,
36
27
  } from './hash-to-curve.ts';
37
28
  import { getMinHashLength, mapHashToField, type IField } from './modular.ts';
38
29
  import type { Fp12, Fp12Bls, Fp2, Fp2Bls, Fp6Bls } from './tower.ts';
39
- import {
40
- _normFnElement,
41
- weierstrassPoints,
42
- type CurvePointsRes,
43
- type CurvePointsType,
44
- type WeierstrassPoint,
45
- type WeierstrassPointCons,
46
- } from './weierstrass.ts';
30
+ import { type WeierstrassPoint, type WeierstrassPointCons } from './weierstrass.ts';
47
31
 
48
32
  type Fp = bigint; // Can be different field?
49
33
 
@@ -54,19 +38,15 @@ export type TwistType = 'multiplicative' | 'divisive';
54
38
 
55
39
  export type ShortSignatureCoder<Fp> = {
56
40
  fromBytes(bytes: Uint8Array): WeierstrassPoint<Fp>;
57
- fromHex(hex: Hex): WeierstrassPoint<Fp>;
41
+ fromHex(hex: string): WeierstrassPoint<Fp>;
58
42
  toBytes(point: WeierstrassPoint<Fp>): Uint8Array;
59
- /** @deprecated use `toBytes` */
60
- toRawBytes(point: WeierstrassPoint<Fp>): Uint8Array;
61
43
  toHex(point: WeierstrassPoint<Fp>): string;
62
44
  };
63
45
 
64
- export type SignatureCoder<Fp> = {
46
+ export type LongSignatureCoder<Fp> = {
65
47
  fromBytes(bytes: Uint8Array): WeierstrassPoint<Fp>;
66
- fromHex(hex: Hex): WeierstrassPoint<Fp>;
48
+ fromHex(hex: string): WeierstrassPoint<Fp>;
67
49
  toBytes(point: WeierstrassPoint<Fp>): Uint8Array;
68
- /** @deprecated use `toBytes` */
69
- toRawBytes(point: WeierstrassPoint<Fp>): Uint8Array;
70
50
  toHex(point: WeierstrassPoint<Fp>): string;
71
51
  };
72
52
 
@@ -94,6 +74,8 @@ export type PostPrecomputeFn = (
94
74
  pointAdd: PostPrecomputePointAddFn
95
75
  ) => void;
96
76
  export type BlsPairing = {
77
+ lengths: CurveLengths;
78
+ Fr: IField<bigint>;
97
79
  Fp12: Fp12Bls;
98
80
  calcPairingPrecomputes: (p: WeierstrassPoint<Fp2>) => Precompute;
99
81
  millerLoopBatch: (pairs: [Precompute, Fp, Fp][]) => Fp12;
@@ -102,46 +84,25 @@ export type BlsPairing = {
102
84
  pairs: { g1: WeierstrassPoint<Fp>; g2: WeierstrassPoint<Fp2> }[],
103
85
  withFinalExponent?: boolean
104
86
  ) => Fp12;
87
+ randomSecretKey: (seed?: Uint8Array) => Uint8Array;
105
88
  };
106
- // TODO: replace CurveType with this? It doesn't contain r however and has postPrecompute
89
+
107
90
  export type BlsPairingParams = {
108
- // NOTE: MSB is always ignored and used as marker for length,
109
- // otherwise leading zeros will be lost.
110
- // Can be different from 'X' (seed) param!
91
+ // MSB is always ignored and used as marker for length, otherwise leading zeros will be lost.
92
+ // Can be different from `X` (seed) param.
111
93
  ateLoopSize: bigint;
112
94
  xNegative: boolean;
113
95
  twistType: TwistType; // BLS12-381: Multiplicative, BN254: Divisive
114
- // This is super ugly hack for untwist point in BN254 after miller loop
115
- postPrecompute?: PostPrecomputeFn;
96
+ randomBytes?: (len?: number) => Uint8Array;
97
+ postPrecompute?: PostPrecomputeFn; // Ugly hack to untwist point in BN254 after miller loop
116
98
  };
117
- export type CurveType = {
118
- G1: CurvePointsType<Fp> & {
119
- ShortSignature: SignatureCoder<Fp>;
120
- mapToCurve: MapToCurve<Fp>;
121
- htfDefaults: H2COpts;
122
- };
123
- G2: CurvePointsType<Fp2> & {
124
- Signature: SignatureCoder<Fp2>;
125
- mapToCurve: MapToCurve<Fp2>;
126
- htfDefaults: H2COpts;
127
- };
128
- fields: BlsFields;
129
- params: {
130
- // NOTE: MSB is always ignored and used as marker for length,
131
- // otherwise leading zeros will be lost.
132
- // Can be different from 'X' (seed) param!
133
- ateLoopSize: BlsPairingParams['ateLoopSize'];
134
- xNegative: BlsPairingParams['xNegative'];
135
- r: bigint; // TODO: remove
136
- twistType: BlsPairingParams['twistType']; // BLS12-381: Multiplicative, BN254: Divisive
137
- };
138
- htfDefaults: H2COpts;
139
- hash: CHash; // Because we need outputLen for DRBG
140
- randomBytes?: (bytesLength?: number) => Uint8Array;
141
- // This is super ugly hack for untwist point in BN254 after miller loop
142
- postPrecompute?: PostPrecomputeFn;
99
+ export type BlsHasherParams = {
100
+ mapToG1?: MapToCurve<Fp>;
101
+ mapToG2?: MapToCurve<Fp2>;
102
+ hasherOpts: H2COpts;
103
+ hasherOptsG1: H2COpts;
104
+ hasherOptsG2: H2COpts;
143
105
  };
144
-
145
106
  type PrecomputeSingle = [Fp2, Fp2, Fp2][];
146
107
  type Precompute = PrecomputeSingle[];
147
108
 
@@ -151,13 +112,12 @@ type Precompute = PrecomputeSingle[];
151
112
  * - G2 is a subgroup of ((x₁, x₂+i), (y₁, y₂+i)) E(Fq²) over y² = x³ + 4(1 + i) where i is √-1
152
113
  */
153
114
  export interface BLSCurvePair {
154
- longSignatures: BLSSigs<bigint, Fp2>;
155
- shortSignatures: BLSSigs<Fp2, bigint>;
115
+ lengths: CurveLengths;
156
116
  millerLoopBatch: BlsPairing['millerLoopBatch'];
157
117
  pairing: BlsPairing['pairing'];
158
118
  pairingBatch: BlsPairing['pairingBatch'];
159
- G1: { Point: WeierstrassPointCons<bigint> } & H2CHasher<Fp>;
160
- G2: { Point: WeierstrassPointCons<Fp2> } & H2CHasher<Fp2>;
119
+ G1: { Point: WeierstrassPointCons<Fp> };
120
+ G2: { Point: WeierstrassPointCons<Fp2> };
161
121
  fields: {
162
122
  Fp: IField<Fp>;
163
123
  Fp2: Fp2Bls;
@@ -166,92 +126,34 @@ export interface BLSCurvePair {
166
126
  Fr: IField<bigint>;
167
127
  };
168
128
  utils: {
169
- randomSecretKey: () => Uint8Array;
170
- /** @deprecated use randomSecretKey */
171
- randomPrivateKey: () => Uint8Array;
129
+ randomSecretKey: (seed?: Uint8Array) => Uint8Array;
172
130
  calcPairingPrecomputes: BlsPairing['calcPairingPrecomputes'];
173
131
  };
174
- }
175
-
176
- export type CurveFn = BLSCurvePair & {
177
- /** @deprecated use `longSignatures.getPublicKey` */
178
- getPublicKey: (secretKey: PrivKey) => Uint8Array;
179
- /** @deprecated use `shortSignatures.getPublicKey` */
180
- getPublicKeyForShortSignatures: (secretKey: PrivKey) => Uint8Array;
181
- /** @deprecated use `longSignatures.sign` */
182
- sign: {
183
- (message: Hex, secretKey: PrivKey, htfOpts?: htfBasicOpts): Uint8Array;
184
- (
185
- message: WeierstrassPoint<Fp2>,
186
- secretKey: PrivKey,
187
- htfOpts?: htfBasicOpts
188
- ): WeierstrassPoint<Fp2>;
189
- };
190
- /** @deprecated use `shortSignatures.sign` */
191
- signShortSignature: {
192
- (message: Hex, secretKey: PrivKey, htfOpts?: htfBasicOpts): Uint8Array;
193
- (
194
- message: WeierstrassPoint<Fp>,
195
- secretKey: PrivKey,
196
- htfOpts?: htfBasicOpts
197
- ): WeierstrassPoint<Fp>;
198
- };
199
- /** @deprecated use `longSignatures.verify` */
200
- verify: (
201
- signature: Hex | WeierstrassPoint<Fp2>,
202
- message: Hex | WeierstrassPoint<Fp2>,
203
- publicKey: Hex | WeierstrassPoint<Fp>,
204
- htfOpts?: htfBasicOpts
205
- ) => boolean;
206
- /** @deprecated use `shortSignatures.verify` */
207
- verifyShortSignature: (
208
- signature: Hex | WeierstrassPoint<Fp>,
209
- message: Hex | WeierstrassPoint<Fp>,
210
- publicKey: Hex | WeierstrassPoint<Fp2>,
211
- htfOpts?: htfBasicOpts
212
- ) => boolean;
213
- verifyBatch: (
214
- signature: Hex | WeierstrassPoint<Fp2>,
215
- messages: (Hex | WeierstrassPoint<Fp2>)[],
216
- publicKeys: (Hex | WeierstrassPoint<Fp>)[],
217
- htfOpts?: htfBasicOpts
218
- ) => boolean;
219
- /** @deprecated use `longSignatures.aggregatePublicKeys` */
220
- aggregatePublicKeys: {
221
- (publicKeys: Hex[]): Uint8Array;
222
- (publicKeys: WeierstrassPoint<Fp>[]): WeierstrassPoint<Fp>;
223
- };
224
- /** @deprecated use `longSignatures.aggregateSignatures` */
225
- aggregateSignatures: {
226
- (signatures: Hex[]): Uint8Array;
227
- (signatures: WeierstrassPoint<Fp2>[]): WeierstrassPoint<Fp2>;
228
- };
229
- /** @deprecated use `shortSignatures.aggregateSignatures` */
230
- aggregateShortSignatures: {
231
- (signatures: Hex[]): Uint8Array;
232
- (signatures: WeierstrassPoint<Fp>[]): WeierstrassPoint<Fp>;
233
- };
234
- G1: CurvePointsRes<Fp> & H2CHasher<Fp>;
235
- G2: CurvePointsRes<Fp2> & H2CHasher<Fp2>;
236
- /** @deprecated use `longSignatures.Signature` */
237
- Signature: SignatureCoder<Fp2>;
238
- /** @deprecated use `shortSignatures.Signature` */
239
- ShortSignature: ShortSignatureCoder<Fp>;
240
132
  params: {
241
133
  ateLoopSize: bigint;
242
- r: bigint;
243
134
  twistType: TwistType;
244
- /** @deprecated */
245
- G1b: bigint;
246
- /** @deprecated */
247
- G2b: Fp2;
248
135
  };
249
- };
136
+ }
137
+
138
+ export interface BlsCurvePairWithHashers extends BLSCurvePair {
139
+ G1: H2CHasher<WeierstrassPointCons<Fp>>;
140
+ G2: H2CHasher<WeierstrassPointCons<Fp2>>;
141
+ }
142
+
143
+ export interface BlsCurvePairWithSignatures extends BlsCurvePairWithHashers {
144
+ longSignatures: BLSSigs<bigint, Fp2>;
145
+ shortSignatures: BLSSigs<Fp2, bigint>;
146
+ }
250
147
 
251
- type BLSInput = Hex | Uint8Array;
148
+ type BLSInput = Uint8Array;
252
149
  export interface BLSSigs<P, S> {
253
- getPublicKey(secretKey: PrivKey): WeierstrassPoint<P>;
254
- sign(hashedMessage: WeierstrassPoint<S>, secretKey: PrivKey): WeierstrassPoint<S>;
150
+ lengths: CurveLengths;
151
+ keygen(seed?: Uint8Array): {
152
+ secretKey: Uint8Array;
153
+ publicKey: WeierstrassPoint<P>;
154
+ };
155
+ getPublicKey(secretKey: Uint8Array): WeierstrassPoint<P>;
156
+ sign(hashedMessage: WeierstrassPoint<S>, secretKey: Uint8Array): WeierstrassPoint<S>;
255
157
  verify(
256
158
  signature: WeierstrassPoint<S> | BLSInput,
257
159
  message: WeierstrassPoint<S>,
@@ -259,13 +161,12 @@ export interface BLSSigs<P, S> {
259
161
  ): boolean;
260
162
  verifyBatch: (
261
163
  signature: WeierstrassPoint<S> | BLSInput,
262
- messages: WeierstrassPoint<S>[],
263
- publicKeys: (WeierstrassPoint<P> | BLSInput)[]
164
+ items: { message: WeierstrassPoint<S>; publicKey: WeierstrassPoint<P> | BLSInput }[]
264
165
  ) => boolean;
265
166
  aggregatePublicKeys(publicKeys: (WeierstrassPoint<P> | BLSInput)[]): WeierstrassPoint<P>;
266
167
  aggregateSignatures(signatures: (WeierstrassPoint<S> | BLSInput)[]): WeierstrassPoint<S>;
267
168
  hash(message: Uint8Array, DST?: string | Uint8Array, hashOpts?: H2CHashOpts): WeierstrassPoint<S>;
268
- Signature: SignatureCoder<S>;
169
+ Signature: LongSignatureCoder<S>;
269
170
  }
270
171
 
271
172
  // Not used with BLS12-381 (no sequential `11` in X). Useful for other curves.
@@ -293,7 +194,7 @@ function createBlsPairing(
293
194
  G2: WeierstrassPointCons<Fp2>,
294
195
  params: BlsPairingParams
295
196
  ): BlsPairing {
296
- const { Fp2, Fp12 } = fields;
197
+ const { Fr, Fp2, Fp12 } = fields;
297
198
  const { twistType, ateLoopSize, xNegative, postPrecompute } = params;
298
199
  type G1 = typeof G1.BASE;
299
200
  type G2 = typeof G2.BASE;
@@ -420,58 +321,86 @@ function createBlsPairing(
420
321
  function pairing(Q: G1, P: G2, withFinalExponent: boolean = true): Fp12 {
421
322
  return pairingBatch([{ g1: Q, g2: P }], withFinalExponent);
422
323
  }
324
+ const lengths = {
325
+ seed: getMinHashLength(Fr.ORDER),
326
+ };
327
+ const rand = params.randomBytes || randomBytes;
328
+ const randomSecretKey = (seed = rand(lengths.seed)): Uint8Array => {
329
+ abytes(seed, lengths.seed, 'seed');
330
+ return mapHashToField(seed, Fr.ORDER);
331
+ };
423
332
  return {
333
+ lengths,
334
+ Fr,
424
335
  Fp12, // NOTE: we re-export Fp12 here because pairing results are Fp12!
425
336
  millerLoopBatch,
426
337
  pairing,
427
338
  pairingBatch,
428
339
  calcPairingPrecomputes,
340
+ randomSecretKey,
429
341
  };
430
342
  }
431
343
 
432
344
  function createBlsSig<P, S>(
433
345
  blsPairing: BlsPairing,
434
- PubCurve: CurvePointsRes<P> & H2CHasher<P>,
435
- SigCurve: CurvePointsRes<S> & H2CHasher<S>,
436
- SignatureCoder: SignatureCoder<S>,
437
- isSigG1: boolean
346
+ PubPoint: WeierstrassPointCons<P>,
347
+ SigPoint: WeierstrassPointCons<S>,
348
+ isSigG1: boolean,
349
+ hashToSigCurve: H2CMethod<WeierstrassPoint<S>>,
350
+ SignatureCoder?: LongSignatureCoder<S>
438
351
  ): BLSSigs<P, S> {
439
- const { Fp12, pairingBatch } = blsPairing;
352
+ const { Fr, Fp12, pairingBatch, randomSecretKey, lengths } = blsPairing;
353
+ if (!SignatureCoder) {
354
+ SignatureCoder = {
355
+ fromBytes: notImplemented,
356
+ fromHex: notImplemented,
357
+ toBytes: notImplemented,
358
+ toHex: notImplemented,
359
+ };
360
+ }
440
361
  type PubPoint = WeierstrassPoint<P>;
441
362
  type SigPoint = WeierstrassPoint<S>;
442
363
  function normPub(point: PubPoint | BLSInput): PubPoint {
443
- return point instanceof PubCurve.Point ? (point as PubPoint) : PubCurve.Point.fromHex(point);
364
+ return point instanceof PubPoint ? (point as PubPoint) : PubPoint.fromBytes(point);
444
365
  }
445
366
  function normSig(point: SigPoint | BLSInput): SigPoint {
446
- return point instanceof SigCurve.Point ? (point as SigPoint) : SigCurve.Point.fromHex(point);
367
+ return point instanceof SigPoint ? (point as SigPoint) : SigPoint.fromBytes(point);
447
368
  }
448
369
  function amsg(m: unknown): SigPoint {
449
- if (!(m instanceof SigCurve.Point))
370
+ if (!(m instanceof SigPoint))
450
371
  throw new Error(`expected valid message hashed to ${!isSigG1 ? 'G2' : 'G1'} curve`);
451
372
  return m as SigPoint;
452
373
  }
453
374
 
454
- type G1 = CurvePointsRes<Fp>['Point']['BASE'];
455
- type G2 = CurvePointsRes<Fp2>['Point']['BASE'];
375
+ type G1 = WeierstrassPoint<Fp>;
376
+ type G2 = WeierstrassPoint<Fp2>;
456
377
  type PairingInput = { g1: G1; g2: G2 };
457
378
  // What matters here is what point pairing API accepts as G1 or G2, not actual size or names
458
379
  const pair: (a: PubPoint, b: SigPoint) => PairingInput = !isSigG1
459
380
  ? (a: PubPoint, b: SigPoint) => ({ g1: a, g2: b }) as PairingInput
460
381
  : (a: PubPoint, b: SigPoint) => ({ g1: b, g2: a }) as PairingInput;
461
- return {
382
+ return Object.freeze({
383
+ lengths: { ...lengths, secretKey: Fr.BYTES },
384
+ keygen(seed?: Uint8Array) {
385
+ const secretKey = randomSecretKey(seed);
386
+ const publicKey = this.getPublicKey(secretKey);
387
+ return { secretKey, publicKey };
388
+ },
462
389
  // P = pk x G
463
- getPublicKey(secretKey: PrivKey): PubPoint {
464
- // TODO: replace with
465
- // const sec = PubCurve.Point.Fn.fromBytes(secretKey);
466
- const sec = _normFnElement(PubCurve.Point.Fn, secretKey);
467
- return PubCurve.Point.BASE.multiply(sec);
390
+ getPublicKey(secretKey: Uint8Array): PubPoint {
391
+ let sec;
392
+ try {
393
+ sec = PubPoint.Fn.fromBytes(secretKey);
394
+ } catch (error) {
395
+ // @ts-ignore
396
+ throw new Error('invalid private key: ' + typeof secretKey, { cause: error });
397
+ }
398
+ return PubPoint.BASE.multiply(sec);
468
399
  },
469
400
  // S = pk x H(m)
470
- sign(message: SigPoint, secretKey: PrivKey, unusedArg?: any): SigPoint {
401
+ sign(message: SigPoint, secretKey: Uint8Array, unusedArg?: any): SigPoint {
471
402
  if (unusedArg != null) throw new Error('sign() expects 2 arguments');
472
- // TODO: replace with
473
- // PubCurve.Point.Fn.fromBytes(secretKey)
474
- const sec = _normFnElement(PubCurve.Point.Fn, secretKey);
403
+ const sec = PubPoint.Fn.fromBytes(secretKey);
475
404
  amsg(message).assertValidity();
476
405
  return message.multiply(sec);
477
406
  },
@@ -488,7 +417,7 @@ function createBlsSig<P, S>(
488
417
  signature = normSig(signature);
489
418
  publicKey = normPub(publicKey);
490
419
  const P = publicKey.negate();
491
- const G = PubCurve.Point.BASE;
420
+ const G = PubPoint.BASE;
492
421
  const Hm = amsg(message);
493
422
  const S = signature;
494
423
  // This code was changed in 1.9.x:
@@ -503,15 +432,12 @@ function createBlsSig<P, S>(
503
432
  // TODO: maybe `{message: G2Hex, publicKey: G1Hex}[]` instead?
504
433
  verifyBatch(
505
434
  signature: SigPoint | BLSInput,
506
- messages: SigPoint[],
507
- publicKeys: (PubPoint | BLSInput)[]
435
+ items: { message: SigPoint; publicKey: PubPoint | BLSInput }[]
508
436
  ): boolean {
509
- aNonEmpty(messages);
510
- if (publicKeys.length !== messages.length)
511
- throw new Error('amount of public keys and messages should be equal');
437
+ aNonEmpty(items);
512
438
  const sig = normSig(signature);
513
- const nMessages = messages;
514
- const nPublicKeys = publicKeys.map(normPub);
439
+ const nMessages = items.map((i) => i.message);
440
+ const nPublicKeys = items.map((i) => normPub(i.publicKey));
515
441
  // NOTE: this works only for exact same object
516
442
  const messagePubKeyMap = new Map<SigPoint, PubPoint[]>();
517
443
  for (let i = 0; i < nPublicKeys.length; i++) {
@@ -525,7 +451,7 @@ function createBlsSig<P, S>(
525
451
  keys.push(pub);
526
452
  }
527
453
  const paired = [];
528
- const G = PubCurve.Point.BASE;
454
+ const G = PubPoint.BASE;
529
455
  try {
530
456
  for (const [msg, keys] of messagePubKeyMap) {
531
457
  const groupPublicKey = keys.reduce((acc, msg) => acc.add(msg));
@@ -542,7 +468,7 @@ function createBlsSig<P, S>(
542
468
  aggregatePublicKeys(publicKeys: (PubPoint | BLSInput)[]): PubPoint {
543
469
  aNonEmpty(publicKeys);
544
470
  publicKeys = publicKeys.map((pub) => normPub(pub));
545
- const agg = (publicKeys as PubPoint[]).reduce((sum, p) => sum.add(p), PubCurve.Point.ZERO);
471
+ const agg = (publicKeys as PubPoint[]).reduce((sum, p) => sum.add(p), PubPoint.ZERO);
546
472
  agg.assertValidity();
547
473
  return agg;
548
474
  },
@@ -552,7 +478,7 @@ function createBlsSig<P, S>(
552
478
  aggregateSignatures(signatures: (SigPoint | BLSInput)[]): SigPoint {
553
479
  aNonEmpty(signatures);
554
480
  signatures = signatures.map((sig) => normSig(sig));
555
- const agg = (signatures as SigPoint[]).reduce((sum, s) => sum.add(s), SigCurve.Point.ZERO);
481
+ const agg = (signatures as SigPoint[]).reduce((sum, s) => sum.add(s), SigPoint.ZERO);
556
482
  agg.assertValidity();
557
483
  return agg;
558
484
  },
@@ -560,188 +486,115 @@ function createBlsSig<P, S>(
560
486
  hash(messageBytes: Uint8Array, DST?: string | Uint8Array): SigPoint {
561
487
  abytes(messageBytes);
562
488
  const opts = DST ? { DST } : undefined;
563
- return SigCurve.hashToCurve(messageBytes, opts) as SigPoint;
489
+ return hashToSigCurve(messageBytes, opts);
564
490
  },
565
491
  Signature: SignatureCoder,
566
- };
492
+ }) /*satisfies Signer */;
567
493
  }
568
494
 
569
- // G1_Point: ProjConstructor<bigint>, G2_Point: ProjConstructor<Fp2>,
570
- export function bls(CURVE: CurveType): CurveFn {
495
+ type BlsSignatureCoders = Partial<{
496
+ LongSignature: LongSignatureCoder<Fp2>;
497
+ ShortSignature: ShortSignatureCoder<Fp>;
498
+ }>;
499
+
500
+ // NOTE: separate function instead of function override, so we don't depend on hasher in bn254.
501
+ export function blsBasic(
502
+ fields: BlsFields,
503
+ G1_Point: WeierstrassPointCons<Fp>,
504
+ G2_Point: WeierstrassPointCons<Fp2>,
505
+ params: BlsPairingParams
506
+ ): BLSCurvePair {
571
507
  // Fields are specific for curve, so for now we'll need to pass them with opts
572
- const { Fp, Fr, Fp2, Fp6, Fp12 } = CURVE.fields;
508
+ const { Fp, Fr, Fp2, Fp6, Fp12 } = fields;
573
509
  // Point on G1 curve: (x, y)
574
- const G1_ = weierstrassPoints(CURVE.G1);
575
- const G1 = Object.assign(
576
- G1_,
577
- createHasher(G1_.Point, CURVE.G1.mapToCurve, {
578
- ...CURVE.htfDefaults,
579
- ...CURVE.G1.htfDefaults,
580
- })
581
- );
510
+ // const G1_Point = weierstrass(CURVE.G1, { Fn: Fr });
511
+ const G1 = { Point: G1_Point };
582
512
  // Point on G2 curve (complex numbers): (x₁, x₂+i), (y₁, y₂+i)
583
- const G2_ = weierstrassPoints(CURVE.G2);
584
- const G2 = Object.assign(
585
- G2_,
586
- createHasher(G2_.Point as H2CPointConstructor<Fp2>, CURVE.G2.mapToCurve, {
587
- ...CURVE.htfDefaults,
588
- ...CURVE.G2.htfDefaults,
589
- })
590
- );
591
- type G1 = typeof G1.Point.BASE;
592
- type G2 = typeof G2.Point.BASE;
593
-
594
- const pairingRes = createBlsPairing(CURVE.fields, G1.Point, G2.Point, {
595
- ...CURVE.params,
596
- postPrecompute: CURVE.postPrecompute,
597
- });
513
+ const G2 = { Point: G2_Point };
598
514
 
599
- const { millerLoopBatch, pairing, pairingBatch, calcPairingPrecomputes } = pairingRes;
600
- const longSignatures = createBlsSig(pairingRes, G1, G2, CURVE.G2.Signature, false);
601
- const shortSignatures = createBlsSig(pairingRes, G2, G1, CURVE.G1.ShortSignature, true);
602
-
603
- const rand = CURVE.randomBytes || randomBytes;
604
- const randomSecretKey = (): Uint8Array => {
605
- const length = getMinHashLength(Fr.ORDER);
606
- return mapHashToField(rand(length), Fr.ORDER);
607
- };
608
- const utils = {
609
- randomSecretKey,
610
- randomPrivateKey: randomSecretKey,
515
+ const pairingRes = createBlsPairing(fields, G1_Point, G2_Point, params);
516
+ const {
517
+ millerLoopBatch,
518
+ pairing,
519
+ pairingBatch,
611
520
  calcPairingPrecomputes,
612
- };
613
-
614
- // LEGACY code
615
- type G1Hex = Hex | G1;
616
- type G2Hex = Hex | G2;
617
-
618
- const { ShortSignature } = CURVE.G1;
619
- const { Signature } = CURVE.G2;
620
-
621
- function normP1Hash(point: G1Hex, htfOpts?: htfBasicOpts): G1 {
622
- return point instanceof G1.Point
623
- ? point
624
- : shortSignatures.hash(ensureBytes('point', point), htfOpts?.DST);
625
- }
626
- function normP2Hash(point: G2Hex, htfOpts?: htfBasicOpts): G2 {
627
- return point instanceof G2.Point
628
- ? point
629
- : longSignatures.hash(ensureBytes('point', point), htfOpts?.DST);
630
- }
631
-
632
- function getPublicKey(privateKey: PrivKey): Uint8Array {
633
- return longSignatures.getPublicKey(privateKey).toBytes(true);
634
- }
635
- function getPublicKeyForShortSignatures(privateKey: PrivKey): Uint8Array {
636
- return shortSignatures.getPublicKey(privateKey).toBytes(true);
637
- }
638
- function sign(message: Hex, privateKey: PrivKey, htfOpts?: htfBasicOpts): Uint8Array;
639
- function sign(message: G2, privateKey: PrivKey, htfOpts?: htfBasicOpts): G2;
640
- function sign(message: G2Hex, privateKey: PrivKey, htfOpts?: htfBasicOpts): Uint8Array | G2 {
641
- const Hm = normP2Hash(message, htfOpts);
642
- const S = longSignatures.sign(Hm, privateKey);
643
- return message instanceof G2.Point ? S : Signature.toBytes(S);
644
- }
645
- function signShortSignature(
646
- message: Hex,
647
- privateKey: PrivKey,
648
- htfOpts?: htfBasicOpts
649
- ): Uint8Array;
650
- function signShortSignature(message: G1, privateKey: PrivKey, htfOpts?: htfBasicOpts): G1;
651
- function signShortSignature(
652
- message: G1Hex,
653
- privateKey: PrivKey,
654
- htfOpts?: htfBasicOpts
655
- ): Uint8Array | G1 {
656
- const Hm = normP1Hash(message, htfOpts);
657
- const S = shortSignatures.sign(Hm, privateKey);
658
- return message instanceof G1.Point ? S : ShortSignature.toBytes(S);
659
- }
660
- function verify(
661
- signature: G2Hex,
662
- message: G2Hex,
663
- publicKey: G1Hex,
664
- htfOpts?: htfBasicOpts
665
- ): boolean {
666
- const Hm = normP2Hash(message, htfOpts);
667
- return longSignatures.verify(signature, Hm, publicKey);
668
- }
669
- function verifyShortSignature(
670
- signature: G1Hex,
671
- message: G1Hex,
672
- publicKey: G2Hex,
673
- htfOpts?: htfBasicOpts
674
- ): boolean {
675
- const Hm = normP1Hash(message, htfOpts);
676
- return shortSignatures.verify(signature, Hm, publicKey);
677
- }
678
- function aggregatePublicKeys(publicKeys: Hex[]): Uint8Array;
679
- function aggregatePublicKeys(publicKeys: G1[]): G1;
680
- function aggregatePublicKeys(publicKeys: G1Hex[]): Uint8Array | G1 {
681
- const agg = longSignatures.aggregatePublicKeys(publicKeys);
682
- return publicKeys[0] instanceof G1.Point ? agg : agg.toBytes(true);
683
- }
684
- function aggregateSignatures(signatures: Hex[]): Uint8Array;
685
- function aggregateSignatures(signatures: G2[]): G2;
686
- function aggregateSignatures(signatures: G2Hex[]): Uint8Array | G2 {
687
- const agg = longSignatures.aggregateSignatures(signatures);
688
- return signatures[0] instanceof G2.Point ? agg : Signature.toBytes(agg);
689
- }
690
- function aggregateShortSignatures(signatures: Hex[]): Uint8Array;
691
- function aggregateShortSignatures(signatures: G1[]): G1;
692
- function aggregateShortSignatures(signatures: G1Hex[]): Uint8Array | G1 {
693
- const agg = shortSignatures.aggregateSignatures(signatures);
694
- return signatures[0] instanceof G1.Point ? agg : ShortSignature.toBytes(agg);
695
- }
696
- function verifyBatch(
697
- signature: G2Hex,
698
- messages: G2Hex[],
699
- publicKeys: G1Hex[],
700
- htfOpts?: htfBasicOpts
701
- ): boolean {
702
- const Hm = messages.map((m) => normP2Hash(m, htfOpts));
703
- return longSignatures.verifyBatch(signature, Hm, publicKeys);
704
- }
521
+ randomSecretKey,
522
+ lengths,
523
+ } = pairingRes;
705
524
 
706
525
  G1.Point.BASE.precompute(4);
707
-
708
- return {
709
- longSignatures,
710
- shortSignatures,
526
+ return Object.freeze({
527
+ lengths,
711
528
  millerLoopBatch,
712
529
  pairing,
713
530
  pairingBatch,
714
- verifyBatch,
715
- fields: {
716
- Fr,
717
- Fp,
718
- Fp2,
719
- Fp6,
720
- Fp12,
721
- },
722
- params: {
723
- ateLoopSize: CURVE.params.ateLoopSize,
724
- twistType: CURVE.params.twistType,
725
- // deprecated
726
- r: CURVE.params.r,
727
- G1b: CURVE.G1.b,
728
- G2b: CURVE.G2.b,
729
- },
730
- utils,
731
-
732
- // deprecated
733
- getPublicKey,
734
- getPublicKeyForShortSignatures,
735
- sign,
736
- signShortSignature,
737
- verify,
738
- verifyShortSignature,
739
- aggregatePublicKeys,
740
- aggregateSignatures,
741
- aggregateShortSignatures,
742
531
  G1,
743
532
  G2,
744
- Signature,
745
- ShortSignature,
533
+ fields: { Fr, Fp, Fp2, Fp6, Fp12 },
534
+ params: {
535
+ ateLoopSize: params.ateLoopSize,
536
+ twistType: params.twistType,
537
+ },
538
+ utils: {
539
+ randomSecretKey,
540
+ calcPairingPrecomputes,
541
+ },
542
+ });
543
+ }
544
+
545
+ // We can export this too, but seems there is not much reasons for now? If user wants hasher, they can just create hasher.
546
+ function blsHashers(
547
+ fields: BlsFields,
548
+ G1_Point: WeierstrassPointCons<Fp>,
549
+ G2_Point: WeierstrassPointCons<Fp2>,
550
+ params: BlsPairingParams,
551
+ hasherParams: BlsHasherParams
552
+ ): BlsCurvePairWithHashers {
553
+ const base = blsBasic(fields, G1_Point, G2_Point, params);
554
+ const G1Hasher = createHasher(G1_Point, hasherParams.mapToG1 || notImplemented, {
555
+ ...hasherParams.hasherOpts,
556
+ ...hasherParams.hasherOptsG1,
557
+ });
558
+ const G2Hasher = createHasher(G2_Point, hasherParams.mapToG2 || notImplemented, {
559
+ ...hasherParams.hasherOpts,
560
+ ...hasherParams.hasherOptsG2,
561
+ });
562
+ return Object.freeze({ ...base, G1: G1Hasher, G2: G2Hasher });
563
+ }
564
+
565
+ // G1_Point: ProjConstructor<bigint>, G2_Point: ProjConstructor<Fp2>,
566
+ // Rename to blsSignatures?
567
+ export function bls(
568
+ fields: BlsFields,
569
+ G1_Point: WeierstrassPointCons<Fp>,
570
+ G2_Point: WeierstrassPointCons<Fp2>,
571
+ params: BlsPairingParams,
572
+ hasherParams: BlsHasherParams,
573
+ signatureCoders: BlsSignatureCoders
574
+ ): BlsCurvePairWithSignatures {
575
+ const base = blsHashers(fields, G1_Point, G2_Point, params, hasherParams);
576
+ const pairingRes: BlsPairing = {
577
+ ...base,
578
+ Fr: base.fields.Fr,
579
+ Fp12: base.fields.Fp12,
580
+ calcPairingPrecomputes: base.utils.calcPairingPrecomputes,
581
+ randomSecretKey: base.utils.randomSecretKey,
746
582
  };
583
+ const longSignatures = createBlsSig(
584
+ pairingRes,
585
+ G1_Point,
586
+ G2_Point,
587
+ false,
588
+ base.G2.hashToCurve,
589
+ signatureCoders?.LongSignature
590
+ );
591
+ const shortSignatures = createBlsSig(
592
+ pairingRes,
593
+ G2_Point,
594
+ G1_Point,
595
+ true,
596
+ base.G1.hashToCurve,
597
+ signatureCoders?.ShortSignature
598
+ );
599
+ return Object.freeze({ ...base, longSignatures, shortSignatures });
747
600
  }