@noble/curves 1.9.6 → 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 (210) 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 +4 -45
  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 +11 -68
  11. package/abstract/edwards.d.ts.map +1 -1
  12. package/abstract/edwards.js +62 -134
  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 +15 -18
  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.map +1 -1
  35. package/abstract/tower.js +43 -19
  36. package/abstract/tower.js.map +1 -1
  37. package/abstract/weierstrass.d.ts +11 -145
  38. package/abstract/weierstrass.d.ts.map +1 -1
  39. package/abstract/weierstrass.js +122 -331
  40. package/abstract/weierstrass.js.map +1 -1
  41. package/bls12-381.d.ts +2 -2
  42. package/bls12-381.d.ts.map +1 -1
  43. package/bls12-381.js +171 -180
  44. package/bls12-381.js.map +1 -1
  45. package/bn254.d.ts +58 -10
  46. package/bn254.d.ts.map +1 -1
  47. package/bn254.js +69 -97
  48. package/bn254.js.map +1 -1
  49. package/ed25519.d.ts +12 -31
  50. package/ed25519.d.ts.map +1 -1
  51. package/ed25519.js +95 -137
  52. package/ed25519.js.map +1 -1
  53. package/ed448.d.ts +11 -29
  54. package/ed448.d.ts.map +1 -1
  55. package/ed448.js +85 -123
  56. package/ed448.js.map +1 -1
  57. package/index.js +1 -1
  58. package/misc.d.ts +10 -14
  59. package/misc.d.ts.map +1 -1
  60. package/misc.js +51 -60
  61. package/misc.js.map +1 -1
  62. package/nist.d.ts +11 -14
  63. package/nist.d.ts.map +1 -1
  64. package/nist.js +46 -55
  65. package/nist.js.map +1 -1
  66. package/package.json +8 -223
  67. package/secp256k1.d.ts +7 -23
  68. package/secp256k1.d.ts.map +1 -1
  69. package/secp256k1.js +47 -60
  70. package/secp256k1.js.map +1 -1
  71. package/src/abstract/bls.ts +197 -344
  72. package/src/abstract/curve.ts +7 -80
  73. package/src/abstract/edwards.ts +49 -171
  74. package/src/abstract/hash-to-curve.ts +32 -45
  75. package/src/abstract/modular.ts +144 -130
  76. package/src/abstract/montgomery.ts +19 -20
  77. package/src/abstract/oprf.ts +600 -0
  78. package/src/abstract/tower.ts +0 -3
  79. package/src/abstract/weierstrass.ts +69 -438
  80. package/src/bls12-381.ts +143 -138
  81. package/src/bn254.ts +65 -89
  82. package/src/ed25519.ts +53 -105
  83. package/src/ed448.ts +32 -77
  84. package/src/index.ts +1 -1
  85. package/src/misc.ts +66 -49
  86. package/src/nist.ts +48 -57
  87. package/src/secp256k1.ts +32 -65
  88. package/src/utils.ts +41 -61
  89. package/src/webcrypto.ts +362 -0
  90. package/utils.d.ts +28 -19
  91. package/utils.d.ts.map +1 -1
  92. package/utils.js +45 -121
  93. package/utils.js.map +1 -1
  94. package/webcrypto.d.ts +47 -0
  95. package/webcrypto.d.ts.map +1 -0
  96. package/webcrypto.js +231 -0
  97. package/webcrypto.js.map +1 -0
  98. package/esm/_shortw_utils.d.ts +0 -19
  99. package/esm/_shortw_utils.d.ts.map +0 -1
  100. package/esm/_shortw_utils.js +0 -16
  101. package/esm/_shortw_utils.js.map +0 -1
  102. package/esm/abstract/bls.d.ts +0 -190
  103. package/esm/abstract/bls.d.ts.map +0 -1
  104. package/esm/abstract/bls.js +0 -408
  105. package/esm/abstract/bls.js.map +0 -1
  106. package/esm/abstract/curve.d.ts +0 -231
  107. package/esm/abstract/curve.d.ts.map +0 -1
  108. package/esm/abstract/curve.js +0 -465
  109. package/esm/abstract/curve.js.map +0 -1
  110. package/esm/abstract/edwards.d.ts +0 -243
  111. package/esm/abstract/edwards.d.ts.map +0 -1
  112. package/esm/abstract/edwards.js +0 -627
  113. package/esm/abstract/edwards.js.map +0 -1
  114. package/esm/abstract/fft.d.ts +0 -122
  115. package/esm/abstract/fft.d.ts.map +0 -1
  116. package/esm/abstract/fft.js +0 -425
  117. package/esm/abstract/fft.js.map +0 -1
  118. package/esm/abstract/hash-to-curve.d.ts +0 -102
  119. package/esm/abstract/hash-to-curve.d.ts.map +0 -1
  120. package/esm/abstract/hash-to-curve.js +0 -203
  121. package/esm/abstract/hash-to-curve.js.map +0 -1
  122. package/esm/abstract/modular.d.ts +0 -171
  123. package/esm/abstract/modular.d.ts.map +0 -1
  124. package/esm/abstract/modular.js +0 -530
  125. package/esm/abstract/modular.js.map +0 -1
  126. package/esm/abstract/montgomery.d.ts +0 -30
  127. package/esm/abstract/montgomery.d.ts.map +0 -1
  128. package/esm/abstract/montgomery.js +0 -157
  129. package/esm/abstract/montgomery.js.map +0 -1
  130. package/esm/abstract/poseidon.d.ts +0 -68
  131. package/esm/abstract/poseidon.d.ts.map +0 -1
  132. package/esm/abstract/poseidon.js +0 -296
  133. package/esm/abstract/poseidon.js.map +0 -1
  134. package/esm/abstract/tower.d.ts +0 -95
  135. package/esm/abstract/tower.d.ts.map +0 -1
  136. package/esm/abstract/tower.js +0 -714
  137. package/esm/abstract/tower.js.map +0 -1
  138. package/esm/abstract/utils.d.ts +0 -5
  139. package/esm/abstract/utils.d.ts.map +0 -1
  140. package/esm/abstract/utils.js +0 -7
  141. package/esm/abstract/utils.js.map +0 -1
  142. package/esm/abstract/weierstrass.d.ts +0 -413
  143. package/esm/abstract/weierstrass.d.ts.map +0 -1
  144. package/esm/abstract/weierstrass.js +0 -1413
  145. package/esm/abstract/weierstrass.js.map +0 -1
  146. package/esm/bls12-381.d.ts +0 -16
  147. package/esm/bls12-381.d.ts.map +0 -1
  148. package/esm/bls12-381.js +0 -705
  149. package/esm/bls12-381.js.map +0 -1
  150. package/esm/bn254.d.ts +0 -18
  151. package/esm/bn254.d.ts.map +0 -1
  152. package/esm/bn254.js +0 -214
  153. package/esm/bn254.js.map +0 -1
  154. package/esm/ed25519.d.ts +0 -106
  155. package/esm/ed25519.d.ts.map +0 -1
  156. package/esm/ed25519.js +0 -467
  157. package/esm/ed25519.js.map +0 -1
  158. package/esm/ed448.d.ts +0 -100
  159. package/esm/ed448.d.ts.map +0 -1
  160. package/esm/ed448.js +0 -459
  161. package/esm/ed448.js.map +0 -1
  162. package/esm/index.d.ts +0 -2
  163. package/esm/index.d.ts.map +0 -1
  164. package/esm/index.js +0 -17
  165. package/esm/index.js.map +0 -1
  166. package/esm/jubjub.d.ts +0 -12
  167. package/esm/jubjub.d.ts.map +0 -1
  168. package/esm/jubjub.js +0 -12
  169. package/esm/jubjub.js.map +0 -1
  170. package/esm/misc.d.ts +0 -19
  171. package/esm/misc.d.ts.map +0 -1
  172. package/esm/misc.js +0 -109
  173. package/esm/misc.js.map +0 -1
  174. package/esm/nist.d.ts +0 -21
  175. package/esm/nist.d.ts.map +0 -1
  176. package/esm/nist.js +0 -132
  177. package/esm/nist.js.map +0 -1
  178. package/esm/p256.d.ts +0 -16
  179. package/esm/p256.d.ts.map +0 -1
  180. package/esm/p256.js +0 -16
  181. package/esm/p256.js.map +0 -1
  182. package/esm/p384.d.ts +0 -16
  183. package/esm/p384.d.ts.map +0 -1
  184. package/esm/p384.js +0 -16
  185. package/esm/p384.js.map +0 -1
  186. package/esm/p521.d.ts +0 -16
  187. package/esm/p521.d.ts.map +0 -1
  188. package/esm/p521.js +0 -16
  189. package/esm/p521.js.map +0 -1
  190. package/esm/package.json +0 -4
  191. package/esm/pasta.d.ts +0 -10
  192. package/esm/pasta.d.ts.map +0 -1
  193. package/esm/pasta.js +0 -10
  194. package/esm/pasta.js.map +0 -1
  195. package/esm/secp256k1.d.ts +0 -89
  196. package/esm/secp256k1.d.ts.map +0 -1
  197. package/esm/secp256k1.js +0 -294
  198. package/esm/secp256k1.js.map +0 -1
  199. package/esm/utils.d.ts +0 -110
  200. package/esm/utils.d.ts.map +0 -1
  201. package/esm/utils.js +0 -322
  202. package/esm/utils.js.map +0 -1
  203. package/src/_shortw_utils.ts +0 -21
  204. package/src/abstract/utils.ts +0 -7
  205. package/src/jubjub.ts +0 -12
  206. package/src/p256.ts +0 -15
  207. package/src/p384.ts +0 -15
  208. package/src/p521.ts +0 -15
  209. package/src/package.json +0 -3
  210. package/src/pasta.ts +0 -9
@@ -1,19 +0,0 @@
1
- /**
2
- * Utilities for short weierstrass curves, combined with noble-hashes.
3
- * @module
4
- */
5
- /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
6
- import { type CurveFn, type CurveType } from './abstract/weierstrass.ts';
7
- import type { CHash } from './utils.ts';
8
- /** connects noble-curves to noble-hashes */
9
- export declare function getHash(hash: CHash): {
10
- hash: CHash;
11
- };
12
- /** Same API as @noble/hashes, with ability to create curve with custom hash */
13
- export type CurveDef = Readonly<Omit<CurveType, 'hash'>>;
14
- export type CurveFnWithCreate = CurveFn & {
15
- create: (hash: CHash) => CurveFn;
16
- };
17
- /** @deprecated use new `weierstrass()` and `ecdsa()` methods */
18
- export declare function createCurve(curveDef: CurveDef, defHash: CHash): CurveFnWithCreate;
19
- //# sourceMappingURL=_shortw_utils.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"_shortw_utils.d.ts","sourceRoot":"","sources":["../src/_shortw_utils.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,sEAAsE;AACtE,OAAO,EAAE,KAAK,OAAO,EAAE,KAAK,SAAS,EAAe,MAAM,2BAA2B,CAAC;AACtF,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAExC,4CAA4C;AAC5C,wBAAgB,OAAO,CAAC,IAAI,EAAE,KAAK,GAAG;IAAE,IAAI,EAAE,KAAK,CAAA;CAAE,CAEpD;AACD,+EAA+E;AAC/E,MAAM,MAAM,QAAQ,GAAG,QAAQ,CAAC,IAAI,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC,CAAC;AACzD,MAAM,MAAM,iBAAiB,GAAG,OAAO,GAAG;IAAE,MAAM,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,OAAO,CAAA;CAAE,CAAC;AAE/E,gEAAgE;AAChE,wBAAgB,WAAW,CAAC,QAAQ,EAAE,QAAQ,EAAE,OAAO,EAAE,KAAK,GAAG,iBAAiB,CAGjF"}
@@ -1,16 +0,0 @@
1
- /**
2
- * Utilities for short weierstrass curves, combined with noble-hashes.
3
- * @module
4
- */
5
- /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
6
- import { weierstrass } from "./abstract/weierstrass.js";
7
- /** connects noble-curves to noble-hashes */
8
- export function getHash(hash) {
9
- return { hash };
10
- }
11
- /** @deprecated use new `weierstrass()` and `ecdsa()` methods */
12
- export function createCurve(curveDef, defHash) {
13
- const create = (hash) => weierstrass({ ...curveDef, hash: hash });
14
- return { ...create(defHash), create };
15
- }
16
- //# sourceMappingURL=_shortw_utils.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"_shortw_utils.js","sourceRoot":"","sources":["../src/_shortw_utils.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,sEAAsE;AACtE,OAAO,EAAgC,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAGtF,4CAA4C;AAC5C,MAAM,UAAU,OAAO,CAAC,IAAW;IACjC,OAAO,EAAE,IAAI,EAAE,CAAC;AAClB,CAAC;AAKD,gEAAgE;AAChE,MAAM,UAAU,WAAW,CAAC,QAAkB,EAAE,OAAc;IAC5D,MAAM,MAAM,GAAG,CAAC,IAAW,EAAW,EAAE,CAAC,WAAW,CAAC,EAAE,GAAG,QAAQ,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;IAClF,OAAO,EAAE,GAAG,MAAM,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;AACxC,CAAC"}
@@ -1,190 +0,0 @@
1
- /**
2
- * BLS != BLS.
3
- * The file implements BLS (Boneh-Lynn-Shacham) signatures.
4
- * Used in both BLS (Barreto-Lynn-Scott) and BN (Barreto-Naehrig)
5
- * families of pairing-friendly curves.
6
- * Consists of two curves: G1 and G2:
7
- * - G1 is a subgroup of (x, y) E(Fq) over y² = x³ + 4.
8
- * - G2 is a subgroup of ((x₁, x₂+i), (y₁, y₂+i)) E(Fq²) over y² = x³ + 4(1 + i) where i is √-1
9
- * - Gt, created by bilinear (ate) pairing e(G1, G2), consists of p-th roots of unity in
10
- * Fq^k where k is embedding degree. Only degree 12 is currently supported, 24 is not.
11
- * Pairing is used to aggregate and verify signatures.
12
- * There are two modes of operation:
13
- * - Long signatures: X-byte keys + 2X-byte sigs (G1 keys + G2 sigs).
14
- * - Short signatures: 2X-byte keys + X-byte sigs (G2 keys + G1 sigs).
15
- * @module
16
- **/
17
- /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
18
- import { type CHash, type Hex, type PrivKey } from '../utils.ts';
19
- import { type H2CHasher, type H2CHashOpts, type H2COpts, type htfBasicOpts, type MapToCurve } from './hash-to-curve.ts';
20
- import { type IField } from './modular.ts';
21
- import type { Fp12, Fp12Bls, Fp2, Fp2Bls, Fp6Bls } from './tower.ts';
22
- import { type CurvePointsRes, type CurvePointsType, type WeierstrassPoint, type WeierstrassPointCons } from './weierstrass.ts';
23
- type Fp = bigint;
24
- export type TwistType = 'multiplicative' | 'divisive';
25
- export type ShortSignatureCoder<Fp> = {
26
- fromBytes(bytes: Uint8Array): WeierstrassPoint<Fp>;
27
- fromHex(hex: Hex): WeierstrassPoint<Fp>;
28
- toBytes(point: WeierstrassPoint<Fp>): Uint8Array;
29
- toHex(point: WeierstrassPoint<Fp>): string;
30
- /** @deprecated use `toBytes` */
31
- toRawBytes(point: WeierstrassPoint<Fp>): Uint8Array;
32
- };
33
- export type SignatureCoder<Fp> = {
34
- fromBytes(bytes: Uint8Array): WeierstrassPoint<Fp>;
35
- fromHex(hex: Hex): WeierstrassPoint<Fp>;
36
- toBytes(point: WeierstrassPoint<Fp>): Uint8Array;
37
- toHex(point: WeierstrassPoint<Fp>): string;
38
- /** @deprecated use `toBytes` */
39
- toRawBytes(point: WeierstrassPoint<Fp>): Uint8Array;
40
- };
41
- export type BlsFields = {
42
- Fp: IField<Fp>;
43
- Fr: IField<bigint>;
44
- Fp2: Fp2Bls;
45
- Fp6: Fp6Bls;
46
- Fp12: Fp12Bls;
47
- };
48
- export type PostPrecomputePointAddFn = (Rx: Fp2, Ry: Fp2, Rz: Fp2, Qx: Fp2, Qy: Fp2) => {
49
- Rx: Fp2;
50
- Ry: Fp2;
51
- Rz: Fp2;
52
- };
53
- export type PostPrecomputeFn = (Rx: Fp2, Ry: Fp2, Rz: Fp2, Qx: Fp2, Qy: Fp2, pointAdd: PostPrecomputePointAddFn) => void;
54
- export type BlsPairing = {
55
- Fp12: Fp12Bls;
56
- calcPairingPrecomputes: (p: WeierstrassPoint<Fp2>) => Precompute;
57
- millerLoopBatch: (pairs: [Precompute, Fp, Fp][]) => Fp12;
58
- pairing: (P: WeierstrassPoint<Fp>, Q: WeierstrassPoint<Fp2>, withFinalExponent?: boolean) => Fp12;
59
- pairingBatch: (pairs: {
60
- g1: WeierstrassPoint<Fp>;
61
- g2: WeierstrassPoint<Fp2>;
62
- }[], withFinalExponent?: boolean) => Fp12;
63
- };
64
- export type BlsPairingParams = {
65
- ateLoopSize: bigint;
66
- xNegative: boolean;
67
- twistType: TwistType;
68
- postPrecompute?: PostPrecomputeFn;
69
- };
70
- export type CurveType = {
71
- G1: CurvePointsType<Fp> & {
72
- ShortSignature: SignatureCoder<Fp>;
73
- mapToCurve: MapToCurve<Fp>;
74
- htfDefaults: H2COpts;
75
- };
76
- G2: CurvePointsType<Fp2> & {
77
- Signature: SignatureCoder<Fp2>;
78
- mapToCurve: MapToCurve<Fp2>;
79
- htfDefaults: H2COpts;
80
- };
81
- fields: BlsFields;
82
- params: {
83
- ateLoopSize: BlsPairingParams['ateLoopSize'];
84
- xNegative: BlsPairingParams['xNegative'];
85
- r: bigint;
86
- twistType: BlsPairingParams['twistType'];
87
- };
88
- htfDefaults: H2COpts;
89
- hash: CHash;
90
- randomBytes?: (bytesLength?: number) => Uint8Array;
91
- postPrecompute?: PostPrecomputeFn;
92
- };
93
- type PrecomputeSingle = [Fp2, Fp2, Fp2][];
94
- type Precompute = PrecomputeSingle[];
95
- /**
96
- * BLS consists of two curves: G1 and G2:
97
- * - G1 is a subgroup of (x, y) E(Fq) over y² = x³ + 4.
98
- * - G2 is a subgroup of ((x₁, x₂+i), (y₁, y₂+i)) E(Fq²) over y² = x³ + 4(1 + i) where i is √-1
99
- */
100
- export interface BLSCurvePair {
101
- longSignatures: BLSSigs<bigint, Fp2>;
102
- shortSignatures: BLSSigs<Fp2, bigint>;
103
- millerLoopBatch: BlsPairing['millerLoopBatch'];
104
- pairing: BlsPairing['pairing'];
105
- pairingBatch: BlsPairing['pairingBatch'];
106
- G1: {
107
- Point: WeierstrassPointCons<bigint>;
108
- } & H2CHasher<Fp>;
109
- G2: {
110
- Point: WeierstrassPointCons<Fp2>;
111
- } & H2CHasher<Fp2>;
112
- fields: {
113
- Fp: IField<Fp>;
114
- Fp2: Fp2Bls;
115
- Fp6: Fp6Bls;
116
- Fp12: Fp12Bls;
117
- Fr: IField<bigint>;
118
- };
119
- utils: {
120
- randomSecretKey: () => Uint8Array;
121
- /** @deprecated use randomSecretKey */
122
- randomPrivateKey: () => Uint8Array;
123
- calcPairingPrecomputes: BlsPairing['calcPairingPrecomputes'];
124
- };
125
- }
126
- export type CurveFn = BLSCurvePair & {
127
- /** @deprecated use `longSignatures.getPublicKey` */
128
- getPublicKey: (secretKey: PrivKey) => Uint8Array;
129
- /** @deprecated use `shortSignatures.getPublicKey` */
130
- getPublicKeyForShortSignatures: (secretKey: PrivKey) => Uint8Array;
131
- /** @deprecated use `longSignatures.sign` */
132
- sign: {
133
- (message: Hex, secretKey: PrivKey, htfOpts?: htfBasicOpts): Uint8Array;
134
- (message: WeierstrassPoint<Fp2>, secretKey: PrivKey, htfOpts?: htfBasicOpts): WeierstrassPoint<Fp2>;
135
- };
136
- /** @deprecated use `shortSignatures.sign` */
137
- signShortSignature: {
138
- (message: Hex, secretKey: PrivKey, htfOpts?: htfBasicOpts): Uint8Array;
139
- (message: WeierstrassPoint<Fp>, secretKey: PrivKey, htfOpts?: htfBasicOpts): WeierstrassPoint<Fp>;
140
- };
141
- /** @deprecated use `longSignatures.verify` */
142
- verify: (signature: Hex | WeierstrassPoint<Fp2>, message: Hex | WeierstrassPoint<Fp2>, publicKey: Hex | WeierstrassPoint<Fp>, htfOpts?: htfBasicOpts) => boolean;
143
- /** @deprecated use `shortSignatures.verify` */
144
- verifyShortSignature: (signature: Hex | WeierstrassPoint<Fp>, message: Hex | WeierstrassPoint<Fp>, publicKey: Hex | WeierstrassPoint<Fp2>, htfOpts?: htfBasicOpts) => boolean;
145
- verifyBatch: (signature: Hex | WeierstrassPoint<Fp2>, messages: (Hex | WeierstrassPoint<Fp2>)[], publicKeys: (Hex | WeierstrassPoint<Fp>)[], htfOpts?: htfBasicOpts) => boolean;
146
- /** @deprecated use `longSignatures.aggregatePublicKeys` */
147
- aggregatePublicKeys: {
148
- (publicKeys: Hex[]): Uint8Array;
149
- (publicKeys: WeierstrassPoint<Fp>[]): WeierstrassPoint<Fp>;
150
- };
151
- /** @deprecated use `longSignatures.aggregateSignatures` */
152
- aggregateSignatures: {
153
- (signatures: Hex[]): Uint8Array;
154
- (signatures: WeierstrassPoint<Fp2>[]): WeierstrassPoint<Fp2>;
155
- };
156
- /** @deprecated use `shortSignatures.aggregateSignatures` */
157
- aggregateShortSignatures: {
158
- (signatures: Hex[]): Uint8Array;
159
- (signatures: WeierstrassPoint<Fp>[]): WeierstrassPoint<Fp>;
160
- };
161
- G1: CurvePointsRes<Fp> & H2CHasher<Fp>;
162
- G2: CurvePointsRes<Fp2> & H2CHasher<Fp2>;
163
- /** @deprecated use `longSignatures.Signature` */
164
- Signature: SignatureCoder<Fp2>;
165
- /** @deprecated use `shortSignatures.Signature` */
166
- ShortSignature: ShortSignatureCoder<Fp>;
167
- params: {
168
- ateLoopSize: bigint;
169
- r: bigint;
170
- twistType: TwistType;
171
- /** @deprecated */
172
- G1b: bigint;
173
- /** @deprecated */
174
- G2b: Fp2;
175
- };
176
- };
177
- type BLSInput = Hex | Uint8Array;
178
- export interface BLSSigs<P, S> {
179
- getPublicKey(secretKey: PrivKey): WeierstrassPoint<P>;
180
- sign(hashedMessage: WeierstrassPoint<S>, secretKey: PrivKey): WeierstrassPoint<S>;
181
- verify(signature: WeierstrassPoint<S> | BLSInput, message: WeierstrassPoint<S>, publicKey: WeierstrassPoint<P> | BLSInput): boolean;
182
- verifyBatch: (signature: WeierstrassPoint<S> | BLSInput, messages: WeierstrassPoint<S>[], publicKeys: (WeierstrassPoint<P> | BLSInput)[]) => boolean;
183
- aggregatePublicKeys(publicKeys: (WeierstrassPoint<P> | BLSInput)[]): WeierstrassPoint<P>;
184
- aggregateSignatures(signatures: (WeierstrassPoint<S> | BLSInput)[]): WeierstrassPoint<S>;
185
- hash(message: Uint8Array, DST?: string | Uint8Array, hashOpts?: H2CHashOpts): WeierstrassPoint<S>;
186
- Signature: SignatureCoder<S>;
187
- }
188
- export declare function bls(CURVE: CurveType): CurveFn;
189
- export {};
190
- //# sourceMappingURL=bls.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"bls.d.ts","sourceRoot":"","sources":["../../src/abstract/bls.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;IAeI;AACJ,sEAAsE;AACtE,OAAO,EAKL,KAAK,KAAK,EACV,KAAK,GAAG,EACR,KAAK,OAAO,EACb,MAAM,aAAa,CAAC;AAErB,OAAO,EAEL,KAAK,SAAS,EACd,KAAK,WAAW,EAChB,KAAK,OAAO,EAEZ,KAAK,YAAY,EACjB,KAAK,UAAU,EAChB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAoC,KAAK,MAAM,EAAE,MAAM,cAAc,CAAC;AAC7E,OAAO,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AACrE,OAAO,EAGL,KAAK,cAAc,EACnB,KAAK,eAAe,EACpB,KAAK,gBAAgB,EACrB,KAAK,oBAAoB,EAC1B,MAAM,kBAAkB,CAAC;AAE1B,KAAK,EAAE,GAAG,MAAM,CAAC;AAKjB,MAAM,MAAM,SAAS,GAAG,gBAAgB,GAAG,UAAU,CAAC;AAEtD,MAAM,MAAM,mBAAmB,CAAC,EAAE,IAAI;IACpC,SAAS,CAAC,KAAK,EAAE,UAAU,GAAG,gBAAgB,CAAC,EAAE,CAAC,CAAC;IACnD,OAAO,CAAC,GAAG,EAAE,GAAG,GAAG,gBAAgB,CAAC,EAAE,CAAC,CAAC;IACxC,OAAO,CAAC,KAAK,EAAE,gBAAgB,CAAC,EAAE,CAAC,GAAG,UAAU,CAAC;IACjD,KAAK,CAAC,KAAK,EAAE,gBAAgB,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC;IAC3C,gCAAgC;IAChC,UAAU,CAAC,KAAK,EAAE,gBAAgB,CAAC,EAAE,CAAC,GAAG,UAAU,CAAC;CAC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CAAC;IACzF,IAAI,CAAC,OAAO,EAAE,UAAU,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,UAAU,EAAE,QAAQ,CAAC,EAAE,WAAW,GAAG,gBAAgB,CAAC,CAAC,CAAC,CAAC;IAClG,SAAS,EAAE,cAAc,CAAC,CAAC,CAAC,CAAC;CAC9B;AA6SD,wBAAgB,GAAG,CAAC,KAAK,EAAE,SAAS,GAAG,OAAO,CAiL7C"}
@@ -1,408 +0,0 @@
1
- /**
2
- * BLS != BLS.
3
- * The file implements BLS (Boneh-Lynn-Shacham) signatures.
4
- * Used in both BLS (Barreto-Lynn-Scott) and BN (Barreto-Naehrig)
5
- * families of pairing-friendly curves.
6
- * Consists of two curves: G1 and G2:
7
- * - G1 is a subgroup of (x, y) E(Fq) over y² = x³ + 4.
8
- * - G2 is a subgroup of ((x₁, x₂+i), (y₁, y₂+i)) E(Fq²) over y² = x³ + 4(1 + i) where i is √-1
9
- * - Gt, created by bilinear (ate) pairing e(G1, G2), consists of p-th roots of unity in
10
- * Fq^k where k is embedding degree. Only degree 12 is currently supported, 24 is not.
11
- * Pairing is used to aggregate and verify signatures.
12
- * There are two modes of operation:
13
- * - Long signatures: X-byte keys + 2X-byte sigs (G1 keys + G2 sigs).
14
- * - Short signatures: 2X-byte keys + X-byte sigs (G2 keys + G1 sigs).
15
- * @module
16
- **/
17
- /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
18
- import { abytes, ensureBytes, memoized, randomBytes, } from "../utils.js";
19
- import { normalizeZ } from "./curve.js";
20
- import { createHasher, } from "./hash-to-curve.js";
21
- import { getMinHashLength, mapHashToField } from "./modular.js";
22
- import { _normFnElement, weierstrassPoints, } from "./weierstrass.js";
23
- // prettier-ignore
24
- const _0n = BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3);
25
- // Not used with BLS12-381 (no sequential `11` in X). Useful for other curves.
26
- function NAfDecomposition(a) {
27
- const res = [];
28
- // a>1 because of marker bit
29
- for (; a > _1n; a >>= _1n) {
30
- if ((a & _1n) === _0n)
31
- res.unshift(0);
32
- else if ((a & _3n) === _3n) {
33
- res.unshift(-1);
34
- a += _1n;
35
- }
36
- else
37
- res.unshift(1);
38
- }
39
- return res;
40
- }
41
- function aNonEmpty(arr) {
42
- if (!Array.isArray(arr) || arr.length === 0)
43
- throw new Error('expected non-empty array');
44
- }
45
- // This should be enough for bn254, no need to export full stuff?
46
- function createBlsPairing(fields, G1, G2, params) {
47
- const { Fp2, Fp12 } = fields;
48
- const { twistType, ateLoopSize, xNegative, postPrecompute } = params;
49
- // Applies sparse multiplication as line function
50
- let lineFunction;
51
- if (twistType === 'multiplicative') {
52
- lineFunction = (c0, c1, c2, f, Px, Py) => Fp12.mul014(f, c0, Fp2.mul(c1, Px), Fp2.mul(c2, Py));
53
- }
54
- else if (twistType === 'divisive') {
55
- // NOTE: it should be [c0, c1, c2], but we use different order here to reduce complexity of
56
- // precompute calculations.
57
- lineFunction = (c0, c1, c2, f, Px, Py) => Fp12.mul034(f, Fp2.mul(c2, Py), Fp2.mul(c1, Px), c0);
58
- }
59
- else
60
- throw new Error('bls: unknown twist type');
61
- const Fp2div2 = Fp2.div(Fp2.ONE, Fp2.mul(Fp2.ONE, _2n));
62
- function pointDouble(ell, Rx, Ry, Rz) {
63
- const t0 = Fp2.sqr(Ry); // Ry²
64
- const t1 = Fp2.sqr(Rz); // Rz²
65
- const t2 = Fp2.mulByB(Fp2.mul(t1, _3n)); // 3 * T1 * B
66
- const t3 = Fp2.mul(t2, _3n); // 3 * T2
67
- const t4 = Fp2.sub(Fp2.sub(Fp2.sqr(Fp2.add(Ry, Rz)), t1), t0); // (Ry + Rz)² - T1 - T0
68
- const c0 = Fp2.sub(t2, t0); // T2 - T0 (i)
69
- const c1 = Fp2.mul(Fp2.sqr(Rx), _3n); // 3 * Rx²
70
- const c2 = Fp2.neg(t4); // -T4 (-h)
71
- ell.push([c0, c1, c2]);
72
- Rx = Fp2.mul(Fp2.mul(Fp2.mul(Fp2.sub(t0, t3), Rx), Ry), Fp2div2); // ((T0 - T3) * Rx * Ry) / 2
73
- Ry = Fp2.sub(Fp2.sqr(Fp2.mul(Fp2.add(t0, t3), Fp2div2)), Fp2.mul(Fp2.sqr(t2), _3n)); // ((T0 + T3) / 2)² - 3 * T2²
74
- Rz = Fp2.mul(t0, t4); // T0 * T4
75
- return { Rx, Ry, Rz };
76
- }
77
- function pointAdd(ell, Rx, Ry, Rz, Qx, Qy) {
78
- // Addition
79
- const t0 = Fp2.sub(Ry, Fp2.mul(Qy, Rz)); // Ry - Qy * Rz
80
- const t1 = Fp2.sub(Rx, Fp2.mul(Qx, Rz)); // Rx - Qx * Rz
81
- const c0 = Fp2.sub(Fp2.mul(t0, Qx), Fp2.mul(t1, Qy)); // T0 * Qx - T1 * Qy == Ry * Qx - Rx * Qy
82
- const c1 = Fp2.neg(t0); // -T0 == Qy * Rz - Ry
83
- const c2 = t1; // == Rx - Qx * Rz
84
- ell.push([c0, c1, c2]);
85
- const t2 = Fp2.sqr(t1); // T1²
86
- const t3 = Fp2.mul(t2, t1); // T2 * T1
87
- const t4 = Fp2.mul(t2, Rx); // T2 * Rx
88
- const t5 = Fp2.add(Fp2.sub(t3, Fp2.mul(t4, _2n)), Fp2.mul(Fp2.sqr(t0), Rz)); // T3 - 2 * T4 + T0² * Rz
89
- Rx = Fp2.mul(t1, t5); // T1 * T5
90
- Ry = Fp2.sub(Fp2.mul(Fp2.sub(t4, t5), t0), Fp2.mul(t3, Ry)); // (T4 - T5) * T0 - T3 * Ry
91
- Rz = Fp2.mul(Rz, t3); // Rz * T3
92
- return { Rx, Ry, Rz };
93
- }
94
- // Pre-compute coefficients for sparse multiplication
95
- // Point addition and point double calculations is reused for coefficients
96
- // pointAdd happens only if bit set, so wNAF is reasonable. Unfortunately we cannot combine
97
- // add + double in windowed precomputes here, otherwise it would be single op (since X is static)
98
- const ATE_NAF = NAfDecomposition(ateLoopSize);
99
- const calcPairingPrecomputes = memoized((point) => {
100
- const p = point;
101
- const { x, y } = p.toAffine();
102
- // prettier-ignore
103
- const Qx = x, Qy = y, negQy = Fp2.neg(y);
104
- // prettier-ignore
105
- let Rx = Qx, Ry = Qy, Rz = Fp2.ONE;
106
- const ell = [];
107
- for (const bit of ATE_NAF) {
108
- const cur = [];
109
- ({ Rx, Ry, Rz } = pointDouble(cur, Rx, Ry, Rz));
110
- if (bit)
111
- ({ Rx, Ry, Rz } = pointAdd(cur, Rx, Ry, Rz, Qx, bit === -1 ? negQy : Qy));
112
- ell.push(cur);
113
- }
114
- if (postPrecompute) {
115
- const last = ell[ell.length - 1];
116
- postPrecompute(Rx, Ry, Rz, Qx, Qy, pointAdd.bind(null, last));
117
- }
118
- return ell;
119
- });
120
- function millerLoopBatch(pairs, withFinalExponent = false) {
121
- let f12 = Fp12.ONE;
122
- if (pairs.length) {
123
- const ellLen = pairs[0][0].length;
124
- for (let i = 0; i < ellLen; i++) {
125
- f12 = Fp12.sqr(f12); // This allows us to do sqr only one time for all pairings
126
- // NOTE: we apply multiple pairings in parallel here
127
- for (const [ell, Px, Py] of pairs) {
128
- for (const [c0, c1, c2] of ell[i])
129
- f12 = lineFunction(c0, c1, c2, f12, Px, Py);
130
- }
131
- }
132
- }
133
- if (xNegative)
134
- f12 = Fp12.conjugate(f12);
135
- return withFinalExponent ? Fp12.finalExponentiate(f12) : f12;
136
- }
137
- // Calculates product of multiple pairings
138
- // This up to x2 faster than just `map(({g1, g2})=>pairing({g1,g2}))`
139
- function pairingBatch(pairs, withFinalExponent = true) {
140
- const res = [];
141
- // Cache precomputed toAffine for all points
142
- normalizeZ(G1, pairs.map(({ g1 }) => g1));
143
- normalizeZ(G2, pairs.map(({ g2 }) => g2));
144
- for (const { g1, g2 } of pairs) {
145
- if (g1.is0() || g2.is0())
146
- throw new Error('pairing is not available for ZERO point');
147
- // This uses toAffine inside
148
- g1.assertValidity();
149
- g2.assertValidity();
150
- const Qa = g1.toAffine();
151
- res.push([calcPairingPrecomputes(g2), Qa.x, Qa.y]);
152
- }
153
- return millerLoopBatch(res, withFinalExponent);
154
- }
155
- // Calculates bilinear pairing
156
- function pairing(Q, P, withFinalExponent = true) {
157
- return pairingBatch([{ g1: Q, g2: P }], withFinalExponent);
158
- }
159
- return {
160
- Fp12, // NOTE: we re-export Fp12 here because pairing results are Fp12!
161
- millerLoopBatch,
162
- pairing,
163
- pairingBatch,
164
- calcPairingPrecomputes,
165
- };
166
- }
167
- function createBlsSig(blsPairing, PubCurve, SigCurve, SignatureCoder, isSigG1) {
168
- const { Fp12, pairingBatch } = blsPairing;
169
- function normPub(point) {
170
- return point instanceof PubCurve.Point ? point : PubCurve.Point.fromHex(point);
171
- }
172
- function normSig(point) {
173
- return point instanceof SigCurve.Point ? point : SigCurve.Point.fromHex(point);
174
- }
175
- function amsg(m) {
176
- if (!(m instanceof SigCurve.Point))
177
- throw new Error(`expected valid message hashed to ${!isSigG1 ? 'G2' : 'G1'} curve`);
178
- return m;
179
- }
180
- // What matters here is what point pairing API accepts as G1 or G2, not actual size or names
181
- const pair = !isSigG1
182
- ? (a, b) => ({ g1: a, g2: b })
183
- : (a, b) => ({ g1: b, g2: a });
184
- return {
185
- // P = pk x G
186
- getPublicKey(secretKey) {
187
- // TODO: replace with
188
- // const sec = PubCurve.Point.Fn.fromBytes(secretKey);
189
- const sec = _normFnElement(PubCurve.Point.Fn, secretKey);
190
- return PubCurve.Point.BASE.multiply(sec);
191
- },
192
- // S = pk x H(m)
193
- sign(message, secretKey, unusedArg) {
194
- if (unusedArg != null)
195
- throw new Error('sign() expects 2 arguments');
196
- // TODO: replace with
197
- // PubCurve.Point.Fn.fromBytes(secretKey)
198
- const sec = _normFnElement(PubCurve.Point.Fn, secretKey);
199
- amsg(message).assertValidity();
200
- return message.multiply(sec);
201
- },
202
- // Checks if pairing of public key & hash is equal to pairing of generator & signature.
203
- // e(P, H(m)) == e(G, S)
204
- // e(S, G) == e(H(m), P)
205
- verify(signature, message, publicKey, unusedArg) {
206
- if (unusedArg != null)
207
- throw new Error('verify() expects 3 arguments');
208
- signature = normSig(signature);
209
- publicKey = normPub(publicKey);
210
- const P = publicKey.negate();
211
- const G = PubCurve.Point.BASE;
212
- const Hm = amsg(message);
213
- const S = signature;
214
- // This code was changed in 1.9.x:
215
- // Before it was G.negate() in G2, now it's always pubKey.negate
216
- // e(P, -Q)===e(-P, Q)==e(P, Q)^-1. Negate can be done anywhere (as long it is done once per pair).
217
- // We just moving sign, but since pairing is multiplicative, we doing X * X^-1 = 1
218
- const exp = pairingBatch([pair(P, Hm), pair(G, S)]);
219
- return Fp12.eql(exp, Fp12.ONE);
220
- },
221
- // https://ethresear.ch/t/fast-verification-of-multiple-bls-signatures/5407
222
- // e(G, S) = e(G, SUM(n)(Si)) = MUL(n)(e(G, Si))
223
- // TODO: maybe `{message: G2Hex, publicKey: G1Hex}[]` instead?
224
- verifyBatch(signature, messages, publicKeys) {
225
- aNonEmpty(messages);
226
- if (publicKeys.length !== messages.length)
227
- throw new Error('amount of public keys and messages should be equal');
228
- const sig = normSig(signature);
229
- const nMessages = messages;
230
- const nPublicKeys = publicKeys.map(normPub);
231
- // NOTE: this works only for exact same object
232
- const messagePubKeyMap = new Map();
233
- for (let i = 0; i < nPublicKeys.length; i++) {
234
- const pub = nPublicKeys[i];
235
- const msg = nMessages[i];
236
- let keys = messagePubKeyMap.get(msg);
237
- if (keys === undefined) {
238
- keys = [];
239
- messagePubKeyMap.set(msg, keys);
240
- }
241
- keys.push(pub);
242
- }
243
- const paired = [];
244
- const G = PubCurve.Point.BASE;
245
- try {
246
- for (const [msg, keys] of messagePubKeyMap) {
247
- const groupPublicKey = keys.reduce((acc, msg) => acc.add(msg));
248
- paired.push(pair(groupPublicKey, msg));
249
- }
250
- paired.push(pair(G.negate(), sig));
251
- return Fp12.eql(pairingBatch(paired), Fp12.ONE);
252
- }
253
- catch {
254
- return false;
255
- }
256
- },
257
- // Adds a bunch of public key points together.
258
- // pk1 + pk2 + pk3 = pkA
259
- aggregatePublicKeys(publicKeys) {
260
- aNonEmpty(publicKeys);
261
- publicKeys = publicKeys.map((pub) => normPub(pub));
262
- const agg = publicKeys.reduce((sum, p) => sum.add(p), PubCurve.Point.ZERO);
263
- agg.assertValidity();
264
- return agg;
265
- },
266
- // Adds a bunch of signature points together.
267
- // pk1 + pk2 + pk3 = pkA
268
- aggregateSignatures(signatures) {
269
- aNonEmpty(signatures);
270
- signatures = signatures.map((sig) => normSig(sig));
271
- const agg = signatures.reduce((sum, s) => sum.add(s), SigCurve.Point.ZERO);
272
- agg.assertValidity();
273
- return agg;
274
- },
275
- hash(messageBytes, DST) {
276
- abytes(messageBytes);
277
- const opts = DST ? { DST } : undefined;
278
- return SigCurve.hashToCurve(messageBytes, opts);
279
- },
280
- Signature: SignatureCoder,
281
- };
282
- }
283
- // G1_Point: ProjConstructor<bigint>, G2_Point: ProjConstructor<Fp2>,
284
- export function bls(CURVE) {
285
- // Fields are specific for curve, so for now we'll need to pass them with opts
286
- const { Fp, Fr, Fp2, Fp6, Fp12 } = CURVE.fields;
287
- // Point on G1 curve: (x, y)
288
- const G1_ = weierstrassPoints(CURVE.G1);
289
- const G1 = Object.assign(G1_, createHasher(G1_.Point, CURVE.G1.mapToCurve, {
290
- ...CURVE.htfDefaults,
291
- ...CURVE.G1.htfDefaults,
292
- }));
293
- // Point on G2 curve (complex numbers): (x₁, x₂+i), (y₁, y₂+i)
294
- const G2_ = weierstrassPoints(CURVE.G2);
295
- const G2 = Object.assign(G2_, createHasher(G2_.Point, CURVE.G2.mapToCurve, {
296
- ...CURVE.htfDefaults,
297
- ...CURVE.G2.htfDefaults,
298
- }));
299
- const pairingRes = createBlsPairing(CURVE.fields, G1.Point, G2.Point, {
300
- ...CURVE.params,
301
- postPrecompute: CURVE.postPrecompute,
302
- });
303
- const { millerLoopBatch, pairing, pairingBatch, calcPairingPrecomputes } = pairingRes;
304
- const longSignatures = createBlsSig(pairingRes, G1, G2, CURVE.G2.Signature, false);
305
- const shortSignatures = createBlsSig(pairingRes, G2, G1, CURVE.G1.ShortSignature, true);
306
- const rand = CURVE.randomBytes || randomBytes;
307
- const randomSecretKey = () => {
308
- const length = getMinHashLength(Fr.ORDER);
309
- return mapHashToField(rand(length), Fr.ORDER);
310
- };
311
- const utils = {
312
- randomSecretKey,
313
- randomPrivateKey: randomSecretKey,
314
- calcPairingPrecomputes,
315
- };
316
- const { ShortSignature } = CURVE.G1;
317
- const { Signature } = CURVE.G2;
318
- function normP1Hash(point, htfOpts) {
319
- return point instanceof G1.Point
320
- ? point
321
- : shortSignatures.hash(ensureBytes('point', point), htfOpts?.DST);
322
- }
323
- function normP2Hash(point, htfOpts) {
324
- return point instanceof G2.Point
325
- ? point
326
- : longSignatures.hash(ensureBytes('point', point), htfOpts?.DST);
327
- }
328
- function getPublicKey(privateKey) {
329
- return longSignatures.getPublicKey(privateKey).toBytes(true);
330
- }
331
- function getPublicKeyForShortSignatures(privateKey) {
332
- return shortSignatures.getPublicKey(privateKey).toBytes(true);
333
- }
334
- function sign(message, privateKey, htfOpts) {
335
- const Hm = normP2Hash(message, htfOpts);
336
- const S = longSignatures.sign(Hm, privateKey);
337
- return message instanceof G2.Point ? S : Signature.toBytes(S);
338
- }
339
- function signShortSignature(message, privateKey, htfOpts) {
340
- const Hm = normP1Hash(message, htfOpts);
341
- const S = shortSignatures.sign(Hm, privateKey);
342
- return message instanceof G1.Point ? S : ShortSignature.toBytes(S);
343
- }
344
- function verify(signature, message, publicKey, htfOpts) {
345
- const Hm = normP2Hash(message, htfOpts);
346
- return longSignatures.verify(signature, Hm, publicKey);
347
- }
348
- function verifyShortSignature(signature, message, publicKey, htfOpts) {
349
- const Hm = normP1Hash(message, htfOpts);
350
- return shortSignatures.verify(signature, Hm, publicKey);
351
- }
352
- function aggregatePublicKeys(publicKeys) {
353
- const agg = longSignatures.aggregatePublicKeys(publicKeys);
354
- return publicKeys[0] instanceof G1.Point ? agg : agg.toBytes(true);
355
- }
356
- function aggregateSignatures(signatures) {
357
- const agg = longSignatures.aggregateSignatures(signatures);
358
- return signatures[0] instanceof G2.Point ? agg : Signature.toBytes(agg);
359
- }
360
- function aggregateShortSignatures(signatures) {
361
- const agg = shortSignatures.aggregateSignatures(signatures);
362
- return signatures[0] instanceof G1.Point ? agg : ShortSignature.toBytes(agg);
363
- }
364
- function verifyBatch(signature, messages, publicKeys, htfOpts) {
365
- const Hm = messages.map((m) => normP2Hash(m, htfOpts));
366
- return longSignatures.verifyBatch(signature, Hm, publicKeys);
367
- }
368
- G1.Point.BASE.precompute(4);
369
- return {
370
- longSignatures,
371
- shortSignatures,
372
- millerLoopBatch,
373
- pairing,
374
- pairingBatch,
375
- verifyBatch,
376
- fields: {
377
- Fr,
378
- Fp,
379
- Fp2,
380
- Fp6,
381
- Fp12,
382
- },
383
- params: {
384
- ateLoopSize: CURVE.params.ateLoopSize,
385
- twistType: CURVE.params.twistType,
386
- // deprecated
387
- r: CURVE.params.r,
388
- G1b: CURVE.G1.b,
389
- G2b: CURVE.G2.b,
390
- },
391
- utils,
392
- // deprecated
393
- getPublicKey,
394
- getPublicKeyForShortSignatures,
395
- sign,
396
- signShortSignature,
397
- verify,
398
- verifyShortSignature,
399
- aggregatePublicKeys,
400
- aggregateSignatures,
401
- aggregateShortSignatures,
402
- G1,
403
- G2,
404
- Signature,
405
- ShortSignature,
406
- };
407
- }
408
- //# sourceMappingURL=bls.js.map