@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.
- package/README.md +267 -421
- package/abstract/bls.d.ts +49 -111
- package/abstract/bls.d.ts.map +1 -1
- package/abstract/bls.js +108 -152
- package/abstract/bls.js.map +1 -1
- package/abstract/curve.d.ts +4 -45
- package/abstract/curve.d.ts.map +1 -1
- package/abstract/curve.js +22 -47
- package/abstract/curve.js.map +1 -1
- package/abstract/edwards.d.ts +11 -68
- package/abstract/edwards.d.ts.map +1 -1
- package/abstract/edwards.js +62 -134
- package/abstract/edwards.js.map +1 -1
- package/abstract/fft.js +14 -27
- package/abstract/fft.js.map +1 -1
- package/abstract/hash-to-curve.d.ts +11 -24
- package/abstract/hash-to-curve.d.ts.map +1 -1
- package/abstract/hash-to-curve.js +30 -35
- package/abstract/hash-to-curve.js.map +1 -1
- package/abstract/modular.d.ts +5 -17
- package/abstract/modular.d.ts.map +1 -1
- package/abstract/modular.js +166 -167
- package/abstract/modular.js.map +1 -1
- package/abstract/montgomery.d.ts +4 -9
- package/abstract/montgomery.d.ts.map +1 -1
- package/abstract/montgomery.js +15 -18
- package/abstract/montgomery.js.map +1 -1
- package/abstract/oprf.d.ts +282 -0
- package/abstract/oprf.d.ts.map +1 -0
- package/abstract/oprf.js +297 -0
- package/abstract/oprf.js.map +1 -0
- package/abstract/poseidon.js +20 -24
- package/abstract/poseidon.js.map +1 -1
- package/abstract/tower.d.ts.map +1 -1
- package/abstract/tower.js +43 -19
- package/abstract/tower.js.map +1 -1
- package/abstract/weierstrass.d.ts +11 -145
- package/abstract/weierstrass.d.ts.map +1 -1
- package/abstract/weierstrass.js +122 -331
- package/abstract/weierstrass.js.map +1 -1
- package/bls12-381.d.ts +2 -2
- package/bls12-381.d.ts.map +1 -1
- package/bls12-381.js +171 -180
- package/bls12-381.js.map +1 -1
- package/bn254.d.ts +58 -10
- package/bn254.d.ts.map +1 -1
- package/bn254.js +69 -97
- package/bn254.js.map +1 -1
- package/ed25519.d.ts +12 -31
- package/ed25519.d.ts.map +1 -1
- package/ed25519.js +95 -137
- package/ed25519.js.map +1 -1
- package/ed448.d.ts +11 -29
- package/ed448.d.ts.map +1 -1
- package/ed448.js +85 -123
- package/ed448.js.map +1 -1
- package/index.js +1 -1
- package/misc.d.ts +10 -14
- package/misc.d.ts.map +1 -1
- package/misc.js +51 -60
- package/misc.js.map +1 -1
- package/nist.d.ts +11 -14
- package/nist.d.ts.map +1 -1
- package/nist.js +46 -55
- package/nist.js.map +1 -1
- package/package.json +8 -223
- package/secp256k1.d.ts +7 -23
- package/secp256k1.d.ts.map +1 -1
- package/secp256k1.js +47 -60
- package/secp256k1.js.map +1 -1
- package/src/abstract/bls.ts +197 -344
- package/src/abstract/curve.ts +7 -80
- package/src/abstract/edwards.ts +49 -171
- package/src/abstract/hash-to-curve.ts +32 -45
- package/src/abstract/modular.ts +144 -130
- package/src/abstract/montgomery.ts +19 -20
- package/src/abstract/oprf.ts +600 -0
- package/src/abstract/tower.ts +0 -3
- package/src/abstract/weierstrass.ts +69 -438
- package/src/bls12-381.ts +143 -138
- package/src/bn254.ts +65 -89
- package/src/ed25519.ts +53 -105
- package/src/ed448.ts +32 -77
- package/src/index.ts +1 -1
- package/src/misc.ts +66 -49
- package/src/nist.ts +48 -57
- package/src/secp256k1.ts +32 -65
- package/src/utils.ts +41 -61
- package/src/webcrypto.ts +362 -0
- package/utils.d.ts +28 -19
- package/utils.d.ts.map +1 -1
- package/utils.js +45 -121
- package/utils.js.map +1 -1
- package/webcrypto.d.ts +47 -0
- package/webcrypto.d.ts.map +1 -0
- package/webcrypto.js +231 -0
- package/webcrypto.js.map +1 -0
- package/esm/_shortw_utils.d.ts +0 -19
- package/esm/_shortw_utils.d.ts.map +0 -1
- package/esm/_shortw_utils.js +0 -16
- package/esm/_shortw_utils.js.map +0 -1
- package/esm/abstract/bls.d.ts +0 -190
- package/esm/abstract/bls.d.ts.map +0 -1
- package/esm/abstract/bls.js +0 -408
- package/esm/abstract/bls.js.map +0 -1
- package/esm/abstract/curve.d.ts +0 -231
- package/esm/abstract/curve.d.ts.map +0 -1
- package/esm/abstract/curve.js +0 -465
- package/esm/abstract/curve.js.map +0 -1
- package/esm/abstract/edwards.d.ts +0 -243
- package/esm/abstract/edwards.d.ts.map +0 -1
- package/esm/abstract/edwards.js +0 -627
- package/esm/abstract/edwards.js.map +0 -1
- package/esm/abstract/fft.d.ts +0 -122
- package/esm/abstract/fft.d.ts.map +0 -1
- package/esm/abstract/fft.js +0 -425
- package/esm/abstract/fft.js.map +0 -1
- package/esm/abstract/hash-to-curve.d.ts +0 -102
- package/esm/abstract/hash-to-curve.d.ts.map +0 -1
- package/esm/abstract/hash-to-curve.js +0 -203
- package/esm/abstract/hash-to-curve.js.map +0 -1
- package/esm/abstract/modular.d.ts +0 -171
- package/esm/abstract/modular.d.ts.map +0 -1
- package/esm/abstract/modular.js +0 -530
- package/esm/abstract/modular.js.map +0 -1
- package/esm/abstract/montgomery.d.ts +0 -30
- package/esm/abstract/montgomery.d.ts.map +0 -1
- package/esm/abstract/montgomery.js +0 -157
- package/esm/abstract/montgomery.js.map +0 -1
- package/esm/abstract/poseidon.d.ts +0 -68
- package/esm/abstract/poseidon.d.ts.map +0 -1
- package/esm/abstract/poseidon.js +0 -296
- package/esm/abstract/poseidon.js.map +0 -1
- package/esm/abstract/tower.d.ts +0 -95
- package/esm/abstract/tower.d.ts.map +0 -1
- package/esm/abstract/tower.js +0 -714
- package/esm/abstract/tower.js.map +0 -1
- package/esm/abstract/utils.d.ts +0 -5
- package/esm/abstract/utils.d.ts.map +0 -1
- package/esm/abstract/utils.js +0 -7
- package/esm/abstract/utils.js.map +0 -1
- package/esm/abstract/weierstrass.d.ts +0 -413
- package/esm/abstract/weierstrass.d.ts.map +0 -1
- package/esm/abstract/weierstrass.js +0 -1413
- package/esm/abstract/weierstrass.js.map +0 -1
- package/esm/bls12-381.d.ts +0 -16
- package/esm/bls12-381.d.ts.map +0 -1
- package/esm/bls12-381.js +0 -705
- package/esm/bls12-381.js.map +0 -1
- package/esm/bn254.d.ts +0 -18
- package/esm/bn254.d.ts.map +0 -1
- package/esm/bn254.js +0 -214
- package/esm/bn254.js.map +0 -1
- package/esm/ed25519.d.ts +0 -106
- package/esm/ed25519.d.ts.map +0 -1
- package/esm/ed25519.js +0 -467
- package/esm/ed25519.js.map +0 -1
- package/esm/ed448.d.ts +0 -100
- package/esm/ed448.d.ts.map +0 -1
- package/esm/ed448.js +0 -459
- package/esm/ed448.js.map +0 -1
- package/esm/index.d.ts +0 -2
- package/esm/index.d.ts.map +0 -1
- package/esm/index.js +0 -17
- package/esm/index.js.map +0 -1
- package/esm/jubjub.d.ts +0 -12
- package/esm/jubjub.d.ts.map +0 -1
- package/esm/jubjub.js +0 -12
- package/esm/jubjub.js.map +0 -1
- package/esm/misc.d.ts +0 -19
- package/esm/misc.d.ts.map +0 -1
- package/esm/misc.js +0 -109
- package/esm/misc.js.map +0 -1
- package/esm/nist.d.ts +0 -21
- package/esm/nist.d.ts.map +0 -1
- package/esm/nist.js +0 -132
- package/esm/nist.js.map +0 -1
- package/esm/p256.d.ts +0 -16
- package/esm/p256.d.ts.map +0 -1
- package/esm/p256.js +0 -16
- package/esm/p256.js.map +0 -1
- package/esm/p384.d.ts +0 -16
- package/esm/p384.d.ts.map +0 -1
- package/esm/p384.js +0 -16
- package/esm/p384.js.map +0 -1
- package/esm/p521.d.ts +0 -16
- package/esm/p521.d.ts.map +0 -1
- package/esm/p521.js +0 -16
- package/esm/p521.js.map +0 -1
- package/esm/package.json +0 -4
- package/esm/pasta.d.ts +0 -10
- package/esm/pasta.d.ts.map +0 -1
- package/esm/pasta.js +0 -10
- package/esm/pasta.js.map +0 -1
- package/esm/secp256k1.d.ts +0 -89
- package/esm/secp256k1.d.ts.map +0 -1
- package/esm/secp256k1.js +0 -294
- package/esm/secp256k1.js.map +0 -1
- package/esm/utils.d.ts +0 -110
- package/esm/utils.d.ts.map +0 -1
- package/esm/utils.js +0 -322
- package/esm/utils.js.map +0 -1
- package/src/_shortw_utils.ts +0 -21
- package/src/abstract/utils.ts +0 -7
- package/src/jubjub.ts +0 -12
- package/src/p256.ts +0 -15
- package/src/p384.ts +0 -15
- package/src/p521.ts +0 -15
- package/src/package.json +0 -3
- package/src/pasta.ts +0 -9
package/abstract/bls.d.ts
CHANGED
|
@@ -1,42 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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';
|
|
1
|
+
import { type CurveLengths } from './curve.ts';
|
|
2
|
+
import { type H2CHasher, type H2CHashOpts, type H2COpts, type MapToCurve } from './hash-to-curve.ts';
|
|
20
3
|
import { type IField } from './modular.ts';
|
|
21
4
|
import type { Fp12, Fp12Bls, Fp2, Fp2Bls, Fp6Bls } from './tower.ts';
|
|
22
|
-
import { type
|
|
5
|
+
import { type WeierstrassPoint, type WeierstrassPointCons } from './weierstrass.ts';
|
|
23
6
|
type Fp = bigint;
|
|
24
7
|
export type TwistType = 'multiplicative' | 'divisive';
|
|
25
8
|
export type ShortSignatureCoder<Fp> = {
|
|
26
9
|
fromBytes(bytes: Uint8Array): WeierstrassPoint<Fp>;
|
|
27
|
-
fromHex(hex:
|
|
10
|
+
fromHex(hex: string): WeierstrassPoint<Fp>;
|
|
28
11
|
toBytes(point: WeierstrassPoint<Fp>): Uint8Array;
|
|
29
12
|
toHex(point: WeierstrassPoint<Fp>): string;
|
|
30
|
-
/** @deprecated use `toBytes` */
|
|
31
|
-
toRawBytes(point: WeierstrassPoint<Fp>): Uint8Array;
|
|
32
13
|
};
|
|
33
|
-
export type
|
|
14
|
+
export type LongSignatureCoder<Fp> = {
|
|
34
15
|
fromBytes(bytes: Uint8Array): WeierstrassPoint<Fp>;
|
|
35
|
-
fromHex(hex:
|
|
16
|
+
fromHex(hex: string): WeierstrassPoint<Fp>;
|
|
36
17
|
toBytes(point: WeierstrassPoint<Fp>): Uint8Array;
|
|
37
18
|
toHex(point: WeierstrassPoint<Fp>): string;
|
|
38
|
-
/** @deprecated use `toBytes` */
|
|
39
|
-
toRawBytes(point: WeierstrassPoint<Fp>): Uint8Array;
|
|
40
19
|
};
|
|
41
20
|
export type BlsFields = {
|
|
42
21
|
Fp: IField<Fp>;
|
|
@@ -52,6 +31,8 @@ export type PostPrecomputePointAddFn = (Rx: Fp2, Ry: Fp2, Rz: Fp2, Qx: Fp2, Qy:
|
|
|
52
31
|
};
|
|
53
32
|
export type PostPrecomputeFn = (Rx: Fp2, Ry: Fp2, Rz: Fp2, Qx: Fp2, Qy: Fp2, pointAdd: PostPrecomputePointAddFn) => void;
|
|
54
33
|
export type BlsPairing = {
|
|
34
|
+
lengths: CurveLengths;
|
|
35
|
+
Fr: IField<bigint>;
|
|
55
36
|
Fp12: Fp12Bls;
|
|
56
37
|
calcPairingPrecomputes: (p: WeierstrassPoint<Fp2>) => Precompute;
|
|
57
38
|
millerLoopBatch: (pairs: [Precompute, Fp, Fp][]) => Fp12;
|
|
@@ -60,35 +41,21 @@ export type BlsPairing = {
|
|
|
60
41
|
g1: WeierstrassPoint<Fp>;
|
|
61
42
|
g2: WeierstrassPoint<Fp2>;
|
|
62
43
|
}[], withFinalExponent?: boolean) => Fp12;
|
|
44
|
+
randomSecretKey: (seed?: Uint8Array) => Uint8Array;
|
|
63
45
|
};
|
|
64
46
|
export type BlsPairingParams = {
|
|
65
47
|
ateLoopSize: bigint;
|
|
66
48
|
xNegative: boolean;
|
|
67
49
|
twistType: TwistType;
|
|
50
|
+
randomBytes?: (len?: number) => Uint8Array;
|
|
68
51
|
postPrecompute?: PostPrecomputeFn;
|
|
69
52
|
};
|
|
70
|
-
export type
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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;
|
|
53
|
+
export type BlsHasherParams = {
|
|
54
|
+
mapToG1?: MapToCurve<Fp>;
|
|
55
|
+
mapToG2?: MapToCurve<Fp2>;
|
|
56
|
+
hasherOpts: H2COpts;
|
|
57
|
+
hasherOptsG1: H2COpts;
|
|
58
|
+
hasherOptsG2: H2COpts;
|
|
92
59
|
};
|
|
93
60
|
type PrecomputeSingle = [Fp2, Fp2, Fp2][];
|
|
94
61
|
type Precompute = PrecomputeSingle[];
|
|
@@ -98,17 +65,16 @@ type Precompute = PrecomputeSingle[];
|
|
|
98
65
|
* - G2 is a subgroup of ((x₁, x₂+i), (y₁, y₂+i)) E(Fq²) over y² = x³ + 4(1 + i) where i is √-1
|
|
99
66
|
*/
|
|
100
67
|
export interface BLSCurvePair {
|
|
101
|
-
|
|
102
|
-
shortSignatures: BLSSigs<Fp2, bigint>;
|
|
68
|
+
lengths: CurveLengths;
|
|
103
69
|
millerLoopBatch: BlsPairing['millerLoopBatch'];
|
|
104
70
|
pairing: BlsPairing['pairing'];
|
|
105
71
|
pairingBatch: BlsPairing['pairingBatch'];
|
|
106
72
|
G1: {
|
|
107
|
-
Point: WeierstrassPointCons<
|
|
108
|
-
}
|
|
73
|
+
Point: WeierstrassPointCons<Fp>;
|
|
74
|
+
};
|
|
109
75
|
G2: {
|
|
110
76
|
Point: WeierstrassPointCons<Fp2>;
|
|
111
|
-
}
|
|
77
|
+
};
|
|
112
78
|
fields: {
|
|
113
79
|
Fp: IField<Fp>;
|
|
114
80
|
Fp2: Fp2Bls;
|
|
@@ -117,74 +83,46 @@ export interface BLSCurvePair {
|
|
|
117
83
|
Fr: IField<bigint>;
|
|
118
84
|
};
|
|
119
85
|
utils: {
|
|
120
|
-
randomSecretKey: () => Uint8Array;
|
|
121
|
-
/** @deprecated use randomSecretKey */
|
|
122
|
-
randomPrivateKey: () => Uint8Array;
|
|
86
|
+
randomSecretKey: (seed?: Uint8Array) => Uint8Array;
|
|
123
87
|
calcPairingPrecomputes: BlsPairing['calcPairingPrecomputes'];
|
|
124
88
|
};
|
|
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
89
|
params: {
|
|
168
90
|
ateLoopSize: bigint;
|
|
169
|
-
r: bigint;
|
|
170
91
|
twistType: TwistType;
|
|
171
|
-
/** @deprecated */
|
|
172
|
-
G1b: bigint;
|
|
173
|
-
/** @deprecated */
|
|
174
|
-
G2b: Fp2;
|
|
175
92
|
};
|
|
176
|
-
}
|
|
177
|
-
|
|
93
|
+
}
|
|
94
|
+
export interface BlsCurvePairWithHashers extends BLSCurvePair {
|
|
95
|
+
G1: H2CHasher<WeierstrassPointCons<Fp>>;
|
|
96
|
+
G2: H2CHasher<WeierstrassPointCons<Fp2>>;
|
|
97
|
+
}
|
|
98
|
+
export interface BlsCurvePairWithSignatures extends BlsCurvePairWithHashers {
|
|
99
|
+
longSignatures: BLSSigs<bigint, Fp2>;
|
|
100
|
+
shortSignatures: BLSSigs<Fp2, bigint>;
|
|
101
|
+
}
|
|
102
|
+
type BLSInput = Uint8Array;
|
|
178
103
|
export interface BLSSigs<P, S> {
|
|
179
|
-
|
|
180
|
-
|
|
104
|
+
lengths: CurveLengths;
|
|
105
|
+
keygen(seed?: Uint8Array): {
|
|
106
|
+
secretKey: Uint8Array;
|
|
107
|
+
publicKey: WeierstrassPoint<P>;
|
|
108
|
+
};
|
|
109
|
+
getPublicKey(secretKey: Uint8Array): WeierstrassPoint<P>;
|
|
110
|
+
sign(hashedMessage: WeierstrassPoint<S>, secretKey: Uint8Array): WeierstrassPoint<S>;
|
|
181
111
|
verify(signature: WeierstrassPoint<S> | BLSInput, message: WeierstrassPoint<S>, publicKey: WeierstrassPoint<P> | BLSInput): boolean;
|
|
182
|
-
verifyBatch: (signature: WeierstrassPoint<S> | BLSInput,
|
|
112
|
+
verifyBatch: (signature: WeierstrassPoint<S> | BLSInput, items: {
|
|
113
|
+
message: WeierstrassPoint<S>;
|
|
114
|
+
publicKey: WeierstrassPoint<P> | BLSInput;
|
|
115
|
+
}[]) => boolean;
|
|
183
116
|
aggregatePublicKeys(publicKeys: (WeierstrassPoint<P> | BLSInput)[]): WeierstrassPoint<P>;
|
|
184
117
|
aggregateSignatures(signatures: (WeierstrassPoint<S> | BLSInput)[]): WeierstrassPoint<S>;
|
|
185
118
|
hash(message: Uint8Array, DST?: string | Uint8Array, hashOpts?: H2CHashOpts): WeierstrassPoint<S>;
|
|
186
|
-
Signature:
|
|
119
|
+
Signature: LongSignatureCoder<S>;
|
|
187
120
|
}
|
|
188
|
-
|
|
121
|
+
type BlsSignatureCoders = Partial<{
|
|
122
|
+
LongSignature: LongSignatureCoder<Fp2>;
|
|
123
|
+
ShortSignature: ShortSignatureCoder<Fp>;
|
|
124
|
+
}>;
|
|
125
|
+
export declare function blsBasic(fields: BlsFields, G1_Point: WeierstrassPointCons<Fp>, G2_Point: WeierstrassPointCons<Fp2>, params: BlsPairingParams): BLSCurvePair;
|
|
126
|
+
export declare function bls(fields: BlsFields, G1_Point: WeierstrassPointCons<Fp>, G2_Point: WeierstrassPointCons<Fp2>, params: BlsPairingParams, hasherParams: BlsHasherParams, signatureCoders: BlsSignatureCoders): BlsCurvePairWithSignatures;
|
|
189
127
|
export {};
|
|
190
128
|
//# sourceMappingURL=bls.d.ts.map
|
package/abstract/bls.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bls.d.ts","sourceRoot":"","sources":["../src/abstract/bls.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"bls.d.ts","sourceRoot":"","sources":["../src/abstract/bls.ts"],"names":[],"mappings":"AAkBA,OAAO,EAAc,KAAK,YAAY,EAAE,MAAM,YAAY,CAAC;AAC3D,OAAO,EAEL,KAAK,SAAS,EACd,KAAK,WAAW,EAEhB,KAAK,OAAO,EACZ,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,EAAE,KAAK,gBAAgB,EAAE,KAAK,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAEpF,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,MAAM,GAAG,gBAAgB,CAAC,EAAE,CAAC,CAAC;IAC3C,OAAO,CAAC,KAAK,EAAE,gBAAgB,CAAC,EAAE,CAAC,GAAG,UAAU,CAAC;IACjD,KAAK,CAAC,KAAK,EAAE,gBAAgB,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC;CAC5C,CAAC;AAEF,MAAM,MAAM,kBAAkB,CAAC,EAAE,IA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AChC,aAAa,EAAE,kBAAkB,CAAC,GAAG,CAAC,CAAC;IACvC,cAAc,EAAE,mBAAmB,CAAC,EAAE,CAAC,CAAC;CACzC,CAAC,CAAC;AAGH,wBAAgB,QAAQ,CACtB,MAAM,EAAE,SAAS,EACjB,QAAQ,EAAE,oBAAoB,CAAC,EAAE,CAAC,EAClC,QAAQ,EAAE,oBAAoB,CAAC,GAAG,CAAC,EACnC,MAAM,EAAE,gBAAgB,GACvB,YAAY,CAqCd;AAwBD,wBAAgB,GAAG,CACjB,MAAM,EAAE,SAAS,EACjB,QAAQ,EAAE,oBAAoB,CAAC,EAAE,CAAC,EAClC,QAAQ,EAAE,oBAAoB,CAAC,GAAG,CAAC,EACnC,MAAM,EAAE,gBAAgB,EACxB,YAAY,EAAE,eAAe,EAC7B,eAAe,EAAE,kBAAkB,GAClC,0BAA0B,CA0B5B"}
|
package/abstract/bls.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.bls = bls;
|
|
4
1
|
/**
|
|
5
2
|
* BLS != BLS.
|
|
6
3
|
* The file implements BLS (Boneh-Lynn-Shacham) signatures.
|
|
@@ -18,11 +15,11 @@ exports.bls = bls;
|
|
|
18
15
|
* @module
|
|
19
16
|
**/
|
|
20
17
|
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
18
|
+
import { abytes, memoized, notImplemented, 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 {} from "./weierstrass.js";
|
|
26
23
|
// prettier-ignore
|
|
27
24
|
const _0n = BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3);
|
|
28
25
|
// Not used with BLS12-381 (no sequential `11` in X). Useful for other curves.
|
|
@@ -47,7 +44,7 @@ function aNonEmpty(arr) {
|
|
|
47
44
|
}
|
|
48
45
|
// This should be enough for bn254, no need to export full stuff?
|
|
49
46
|
function createBlsPairing(fields, G1, G2, params) {
|
|
50
|
-
const { Fp2, Fp12 } = fields;
|
|
47
|
+
const { Fr, Fp2, Fp12 } = fields;
|
|
51
48
|
const { twistType, ateLoopSize, xNegative, postPrecompute } = params;
|
|
52
49
|
// Applies sparse multiplication as line function
|
|
53
50
|
let lineFunction;
|
|
@@ -99,7 +96,7 @@ function createBlsPairing(fields, G1, G2, params) {
|
|
|
99
96
|
// pointAdd happens only if bit set, so wNAF is reasonable. Unfortunately we cannot combine
|
|
100
97
|
// add + double in windowed precomputes here, otherwise it would be single op (since X is static)
|
|
101
98
|
const ATE_NAF = NAfDecomposition(ateLoopSize);
|
|
102
|
-
const calcPairingPrecomputes =
|
|
99
|
+
const calcPairingPrecomputes = memoized((point) => {
|
|
103
100
|
const p = point;
|
|
104
101
|
const { x, y } = p.toAffine();
|
|
105
102
|
// prettier-ignore
|
|
@@ -142,8 +139,8 @@ function createBlsPairing(fields, G1, G2, params) {
|
|
|
142
139
|
function pairingBatch(pairs, withFinalExponent = true) {
|
|
143
140
|
const res = [];
|
|
144
141
|
// Cache precomputed toAffine for all points
|
|
145
|
-
|
|
146
|
-
|
|
142
|
+
normalizeZ(G1, pairs.map(({ g1 }) => g1));
|
|
143
|
+
normalizeZ(G2, pairs.map(({ g2 }) => g2));
|
|
147
144
|
for (const { g1, g2 } of pairs) {
|
|
148
145
|
if (g1.is0() || g2.is0())
|
|
149
146
|
throw new Error('pairing is not available for ZERO point');
|
|
@@ -159,24 +156,43 @@ function createBlsPairing(fields, G1, G2, params) {
|
|
|
159
156
|
function pairing(Q, P, withFinalExponent = true) {
|
|
160
157
|
return pairingBatch([{ g1: Q, g2: P }], withFinalExponent);
|
|
161
158
|
}
|
|
159
|
+
const lengths = {
|
|
160
|
+
seed: getMinHashLength(Fr.ORDER),
|
|
161
|
+
};
|
|
162
|
+
const rand = params.randomBytes || randomBytes;
|
|
163
|
+
const randomSecretKey = (seed = rand(lengths.seed)) => {
|
|
164
|
+
abytes(seed, lengths.seed, 'seed');
|
|
165
|
+
return mapHashToField(seed, Fr.ORDER);
|
|
166
|
+
};
|
|
162
167
|
return {
|
|
168
|
+
lengths,
|
|
169
|
+
Fr,
|
|
163
170
|
Fp12, // NOTE: we re-export Fp12 here because pairing results are Fp12!
|
|
164
171
|
millerLoopBatch,
|
|
165
172
|
pairing,
|
|
166
173
|
pairingBatch,
|
|
167
174
|
calcPairingPrecomputes,
|
|
175
|
+
randomSecretKey,
|
|
168
176
|
};
|
|
169
177
|
}
|
|
170
|
-
function createBlsSig(blsPairing,
|
|
171
|
-
const { Fp12, pairingBatch } = blsPairing;
|
|
178
|
+
function createBlsSig(blsPairing, PubPoint, SigPoint, isSigG1, hashToSigCurve, SignatureCoder) {
|
|
179
|
+
const { Fr, Fp12, pairingBatch, randomSecretKey, lengths } = blsPairing;
|
|
180
|
+
if (!SignatureCoder) {
|
|
181
|
+
SignatureCoder = {
|
|
182
|
+
fromBytes: notImplemented,
|
|
183
|
+
fromHex: notImplemented,
|
|
184
|
+
toBytes: notImplemented,
|
|
185
|
+
toHex: notImplemented,
|
|
186
|
+
};
|
|
187
|
+
}
|
|
172
188
|
function normPub(point) {
|
|
173
|
-
return point instanceof
|
|
189
|
+
return point instanceof PubPoint ? point : PubPoint.fromBytes(point);
|
|
174
190
|
}
|
|
175
191
|
function normSig(point) {
|
|
176
|
-
return point instanceof
|
|
192
|
+
return point instanceof SigPoint ? point : SigPoint.fromBytes(point);
|
|
177
193
|
}
|
|
178
194
|
function amsg(m) {
|
|
179
|
-
if (!(m instanceof
|
|
195
|
+
if (!(m instanceof SigPoint))
|
|
180
196
|
throw new Error(`expected valid message hashed to ${!isSigG1 ? 'G2' : 'G1'} curve`);
|
|
181
197
|
return m;
|
|
182
198
|
}
|
|
@@ -184,21 +200,30 @@ function createBlsSig(blsPairing, PubCurve, SigCurve, SignatureCoder, isSigG1) {
|
|
|
184
200
|
const pair = !isSigG1
|
|
185
201
|
? (a, b) => ({ g1: a, g2: b })
|
|
186
202
|
: (a, b) => ({ g1: b, g2: a });
|
|
187
|
-
return {
|
|
203
|
+
return Object.freeze({
|
|
204
|
+
lengths: { ...lengths, secretKey: Fr.BYTES },
|
|
205
|
+
keygen(seed) {
|
|
206
|
+
const secretKey = randomSecretKey(seed);
|
|
207
|
+
const publicKey = this.getPublicKey(secretKey);
|
|
208
|
+
return { secretKey, publicKey };
|
|
209
|
+
},
|
|
188
210
|
// P = pk x G
|
|
189
211
|
getPublicKey(secretKey) {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
212
|
+
let sec;
|
|
213
|
+
try {
|
|
214
|
+
sec = PubPoint.Fn.fromBytes(secretKey);
|
|
215
|
+
}
|
|
216
|
+
catch (error) {
|
|
217
|
+
// @ts-ignore
|
|
218
|
+
throw new Error('invalid private key: ' + typeof secretKey, { cause: error });
|
|
219
|
+
}
|
|
220
|
+
return PubPoint.BASE.multiply(sec);
|
|
194
221
|
},
|
|
195
222
|
// S = pk x H(m)
|
|
196
223
|
sign(message, secretKey, unusedArg) {
|
|
197
224
|
if (unusedArg != null)
|
|
198
225
|
throw new Error('sign() expects 2 arguments');
|
|
199
|
-
|
|
200
|
-
// PubCurve.Point.Fn.fromBytes(secretKey)
|
|
201
|
-
const sec = (0, weierstrass_ts_1._normFnElement)(PubCurve.Point.Fn, secretKey);
|
|
226
|
+
const sec = PubPoint.Fn.fromBytes(secretKey);
|
|
202
227
|
amsg(message).assertValidity();
|
|
203
228
|
return message.multiply(sec);
|
|
204
229
|
},
|
|
@@ -211,7 +236,7 @@ function createBlsSig(blsPairing, PubCurve, SigCurve, SignatureCoder, isSigG1) {
|
|
|
211
236
|
signature = normSig(signature);
|
|
212
237
|
publicKey = normPub(publicKey);
|
|
213
238
|
const P = publicKey.negate();
|
|
214
|
-
const G =
|
|
239
|
+
const G = PubPoint.BASE;
|
|
215
240
|
const Hm = amsg(message);
|
|
216
241
|
const S = signature;
|
|
217
242
|
// This code was changed in 1.9.x:
|
|
@@ -224,13 +249,11 @@ function createBlsSig(blsPairing, PubCurve, SigCurve, SignatureCoder, isSigG1) {
|
|
|
224
249
|
// https://ethresear.ch/t/fast-verification-of-multiple-bls-signatures/5407
|
|
225
250
|
// e(G, S) = e(G, SUM(n)(Si)) = MUL(n)(e(G, Si))
|
|
226
251
|
// TODO: maybe `{message: G2Hex, publicKey: G1Hex}[]` instead?
|
|
227
|
-
verifyBatch(signature,
|
|
228
|
-
aNonEmpty(
|
|
229
|
-
if (publicKeys.length !== messages.length)
|
|
230
|
-
throw new Error('amount of public keys and messages should be equal');
|
|
252
|
+
verifyBatch(signature, items) {
|
|
253
|
+
aNonEmpty(items);
|
|
231
254
|
const sig = normSig(signature);
|
|
232
|
-
const nMessages =
|
|
233
|
-
const nPublicKeys =
|
|
255
|
+
const nMessages = items.map((i) => i.message);
|
|
256
|
+
const nPublicKeys = items.map((i) => normPub(i.publicKey));
|
|
234
257
|
// NOTE: this works only for exact same object
|
|
235
258
|
const messagePubKeyMap = new Map();
|
|
236
259
|
for (let i = 0; i < nPublicKeys.length; i++) {
|
|
@@ -244,7 +267,7 @@ function createBlsSig(blsPairing, PubCurve, SigCurve, SignatureCoder, isSigG1) {
|
|
|
244
267
|
keys.push(pub);
|
|
245
268
|
}
|
|
246
269
|
const paired = [];
|
|
247
|
-
const G =
|
|
270
|
+
const G = PubPoint.BASE;
|
|
248
271
|
try {
|
|
249
272
|
for (const [msg, keys] of messagePubKeyMap) {
|
|
250
273
|
const groupPublicKey = keys.reduce((acc, msg) => acc.add(msg));
|
|
@@ -262,7 +285,7 @@ function createBlsSig(blsPairing, PubCurve, SigCurve, SignatureCoder, isSigG1) {
|
|
|
262
285
|
aggregatePublicKeys(publicKeys) {
|
|
263
286
|
aNonEmpty(publicKeys);
|
|
264
287
|
publicKeys = publicKeys.map((pub) => normPub(pub));
|
|
265
|
-
const agg = publicKeys.reduce((sum, p) => sum.add(p),
|
|
288
|
+
const agg = publicKeys.reduce((sum, p) => sum.add(p), PubPoint.ZERO);
|
|
266
289
|
agg.assertValidity();
|
|
267
290
|
return agg;
|
|
268
291
|
},
|
|
@@ -271,141 +294,74 @@ function createBlsSig(blsPairing, PubCurve, SigCurve, SignatureCoder, isSigG1) {
|
|
|
271
294
|
aggregateSignatures(signatures) {
|
|
272
295
|
aNonEmpty(signatures);
|
|
273
296
|
signatures = signatures.map((sig) => normSig(sig));
|
|
274
|
-
const agg = signatures.reduce((sum, s) => sum.add(s),
|
|
297
|
+
const agg = signatures.reduce((sum, s) => sum.add(s), SigPoint.ZERO);
|
|
275
298
|
agg.assertValidity();
|
|
276
299
|
return agg;
|
|
277
300
|
},
|
|
278
301
|
hash(messageBytes, DST) {
|
|
279
|
-
|
|
302
|
+
abytes(messageBytes);
|
|
280
303
|
const opts = DST ? { DST } : undefined;
|
|
281
|
-
return
|
|
304
|
+
return hashToSigCurve(messageBytes, opts);
|
|
282
305
|
},
|
|
283
306
|
Signature: SignatureCoder,
|
|
284
|
-
}
|
|
307
|
+
}) /*satisfies Signer */;
|
|
285
308
|
}
|
|
286
|
-
//
|
|
287
|
-
function
|
|
309
|
+
// NOTE: separate function instead of function override, so we don't depend on hasher in bn254.
|
|
310
|
+
export function blsBasic(fields, G1_Point, G2_Point, params) {
|
|
288
311
|
// Fields are specific for curve, so for now we'll need to pass them with opts
|
|
289
|
-
const { Fp, Fr, Fp2, Fp6, Fp12 } =
|
|
312
|
+
const { Fp, Fr, Fp2, Fp6, Fp12 } = fields;
|
|
290
313
|
// Point on G1 curve: (x, y)
|
|
291
|
-
const
|
|
292
|
-
const G1 =
|
|
293
|
-
...CURVE.htfDefaults,
|
|
294
|
-
...CURVE.G1.htfDefaults,
|
|
295
|
-
}));
|
|
314
|
+
// const G1_Point = weierstrass(CURVE.G1, { Fn: Fr });
|
|
315
|
+
const G1 = { Point: G1_Point };
|
|
296
316
|
// Point on G2 curve (complex numbers): (x₁, x₂+i), (y₁, y₂+i)
|
|
297
|
-
const
|
|
298
|
-
const
|
|
299
|
-
|
|
300
|
-
...CURVE.G2.htfDefaults,
|
|
301
|
-
}));
|
|
302
|
-
const pairingRes = createBlsPairing(CURVE.fields, G1.Point, G2.Point, {
|
|
303
|
-
...CURVE.params,
|
|
304
|
-
postPrecompute: CURVE.postPrecompute,
|
|
305
|
-
});
|
|
306
|
-
const { millerLoopBatch, pairing, pairingBatch, calcPairingPrecomputes } = pairingRes;
|
|
307
|
-
const longSignatures = createBlsSig(pairingRes, G1, G2, CURVE.G2.Signature, false);
|
|
308
|
-
const shortSignatures = createBlsSig(pairingRes, G2, G1, CURVE.G1.ShortSignature, true);
|
|
309
|
-
const rand = CURVE.randomBytes || utils_ts_1.randomBytes;
|
|
310
|
-
const randomSecretKey = () => {
|
|
311
|
-
const length = (0, modular_ts_1.getMinHashLength)(Fr.ORDER);
|
|
312
|
-
return (0, modular_ts_1.mapHashToField)(rand(length), Fr.ORDER);
|
|
313
|
-
};
|
|
314
|
-
const utils = {
|
|
315
|
-
randomSecretKey,
|
|
316
|
-
randomPrivateKey: randomSecretKey,
|
|
317
|
-
calcPairingPrecomputes,
|
|
318
|
-
};
|
|
319
|
-
const { ShortSignature } = CURVE.G1;
|
|
320
|
-
const { Signature } = CURVE.G2;
|
|
321
|
-
function normP1Hash(point, htfOpts) {
|
|
322
|
-
return point instanceof G1.Point
|
|
323
|
-
? point
|
|
324
|
-
: shortSignatures.hash((0, utils_ts_1.ensureBytes)('point', point), htfOpts?.DST);
|
|
325
|
-
}
|
|
326
|
-
function normP2Hash(point, htfOpts) {
|
|
327
|
-
return point instanceof G2.Point
|
|
328
|
-
? point
|
|
329
|
-
: longSignatures.hash((0, utils_ts_1.ensureBytes)('point', point), htfOpts?.DST);
|
|
330
|
-
}
|
|
331
|
-
function getPublicKey(privateKey) {
|
|
332
|
-
return longSignatures.getPublicKey(privateKey).toBytes(true);
|
|
333
|
-
}
|
|
334
|
-
function getPublicKeyForShortSignatures(privateKey) {
|
|
335
|
-
return shortSignatures.getPublicKey(privateKey).toBytes(true);
|
|
336
|
-
}
|
|
337
|
-
function sign(message, privateKey, htfOpts) {
|
|
338
|
-
const Hm = normP2Hash(message, htfOpts);
|
|
339
|
-
const S = longSignatures.sign(Hm, privateKey);
|
|
340
|
-
return message instanceof G2.Point ? S : Signature.toBytes(S);
|
|
341
|
-
}
|
|
342
|
-
function signShortSignature(message, privateKey, htfOpts) {
|
|
343
|
-
const Hm = normP1Hash(message, htfOpts);
|
|
344
|
-
const S = shortSignatures.sign(Hm, privateKey);
|
|
345
|
-
return message instanceof G1.Point ? S : ShortSignature.toBytes(S);
|
|
346
|
-
}
|
|
347
|
-
function verify(signature, message, publicKey, htfOpts) {
|
|
348
|
-
const Hm = normP2Hash(message, htfOpts);
|
|
349
|
-
return longSignatures.verify(signature, Hm, publicKey);
|
|
350
|
-
}
|
|
351
|
-
function verifyShortSignature(signature, message, publicKey, htfOpts) {
|
|
352
|
-
const Hm = normP1Hash(message, htfOpts);
|
|
353
|
-
return shortSignatures.verify(signature, Hm, publicKey);
|
|
354
|
-
}
|
|
355
|
-
function aggregatePublicKeys(publicKeys) {
|
|
356
|
-
const agg = longSignatures.aggregatePublicKeys(publicKeys);
|
|
357
|
-
return publicKeys[0] instanceof G1.Point ? agg : agg.toBytes(true);
|
|
358
|
-
}
|
|
359
|
-
function aggregateSignatures(signatures) {
|
|
360
|
-
const agg = longSignatures.aggregateSignatures(signatures);
|
|
361
|
-
return signatures[0] instanceof G2.Point ? agg : Signature.toBytes(agg);
|
|
362
|
-
}
|
|
363
|
-
function aggregateShortSignatures(signatures) {
|
|
364
|
-
const agg = shortSignatures.aggregateSignatures(signatures);
|
|
365
|
-
return signatures[0] instanceof G1.Point ? agg : ShortSignature.toBytes(agg);
|
|
366
|
-
}
|
|
367
|
-
function verifyBatch(signature, messages, publicKeys, htfOpts) {
|
|
368
|
-
const Hm = messages.map((m) => normP2Hash(m, htfOpts));
|
|
369
|
-
return longSignatures.verifyBatch(signature, Hm, publicKeys);
|
|
370
|
-
}
|
|
317
|
+
const G2 = { Point: G2_Point };
|
|
318
|
+
const pairingRes = createBlsPairing(fields, G1_Point, G2_Point, params);
|
|
319
|
+
const { millerLoopBatch, pairing, pairingBatch, calcPairingPrecomputes, randomSecretKey, lengths, } = pairingRes;
|
|
371
320
|
G1.Point.BASE.precompute(4);
|
|
372
|
-
return {
|
|
373
|
-
|
|
374
|
-
shortSignatures,
|
|
321
|
+
return Object.freeze({
|
|
322
|
+
lengths,
|
|
375
323
|
millerLoopBatch,
|
|
376
324
|
pairing,
|
|
377
325
|
pairingBatch,
|
|
378
|
-
verifyBatch,
|
|
379
|
-
fields: {
|
|
380
|
-
Fr,
|
|
381
|
-
Fp,
|
|
382
|
-
Fp2,
|
|
383
|
-
Fp6,
|
|
384
|
-
Fp12,
|
|
385
|
-
},
|
|
386
|
-
params: {
|
|
387
|
-
ateLoopSize: CURVE.params.ateLoopSize,
|
|
388
|
-
twistType: CURVE.params.twistType,
|
|
389
|
-
// deprecated
|
|
390
|
-
r: CURVE.params.r,
|
|
391
|
-
G1b: CURVE.G1.b,
|
|
392
|
-
G2b: CURVE.G2.b,
|
|
393
|
-
},
|
|
394
|
-
utils,
|
|
395
|
-
// deprecated
|
|
396
|
-
getPublicKey,
|
|
397
|
-
getPublicKeyForShortSignatures,
|
|
398
|
-
sign,
|
|
399
|
-
signShortSignature,
|
|
400
|
-
verify,
|
|
401
|
-
verifyShortSignature,
|
|
402
|
-
aggregatePublicKeys,
|
|
403
|
-
aggregateSignatures,
|
|
404
|
-
aggregateShortSignatures,
|
|
405
326
|
G1,
|
|
406
327
|
G2,
|
|
407
|
-
|
|
408
|
-
|
|
328
|
+
fields: { Fr, Fp, Fp2, Fp6, Fp12 },
|
|
329
|
+
params: {
|
|
330
|
+
ateLoopSize: params.ateLoopSize,
|
|
331
|
+
twistType: params.twistType,
|
|
332
|
+
},
|
|
333
|
+
utils: {
|
|
334
|
+
randomSecretKey,
|
|
335
|
+
calcPairingPrecomputes,
|
|
336
|
+
},
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
// We can export this too, but seems there is not much reasons for now? If user wants hasher, they can just create hasher.
|
|
340
|
+
function blsHashers(fields, G1_Point, G2_Point, params, hasherParams) {
|
|
341
|
+
const base = blsBasic(fields, G1_Point, G2_Point, params);
|
|
342
|
+
const G1Hasher = createHasher(G1_Point, hasherParams.mapToG1 || notImplemented, {
|
|
343
|
+
...hasherParams.hasherOpts,
|
|
344
|
+
...hasherParams.hasherOptsG1,
|
|
345
|
+
});
|
|
346
|
+
const G2Hasher = createHasher(G2_Point, hasherParams.mapToG2 || notImplemented, {
|
|
347
|
+
...hasherParams.hasherOpts,
|
|
348
|
+
...hasherParams.hasherOptsG2,
|
|
349
|
+
});
|
|
350
|
+
return Object.freeze({ ...base, G1: G1Hasher, G2: G2Hasher });
|
|
351
|
+
}
|
|
352
|
+
// G1_Point: ProjConstructor<bigint>, G2_Point: ProjConstructor<Fp2>,
|
|
353
|
+
// Rename to blsSignatures?
|
|
354
|
+
export function bls(fields, G1_Point, G2_Point, params, hasherParams, signatureCoders) {
|
|
355
|
+
const base = blsHashers(fields, G1_Point, G2_Point, params, hasherParams);
|
|
356
|
+
const pairingRes = {
|
|
357
|
+
...base,
|
|
358
|
+
Fr: base.fields.Fr,
|
|
359
|
+
Fp12: base.fields.Fp12,
|
|
360
|
+
calcPairingPrecomputes: base.utils.calcPairingPrecomputes,
|
|
361
|
+
randomSecretKey: base.utils.randomSecretKey,
|
|
409
362
|
};
|
|
363
|
+
const longSignatures = createBlsSig(pairingRes, G1_Point, G2_Point, false, base.G2.hashToCurve, signatureCoders?.LongSignature);
|
|
364
|
+
const shortSignatures = createBlsSig(pairingRes, G2_Point, G1_Point, true, base.G1.hashToCurve, signatureCoders?.ShortSignature);
|
|
365
|
+
return Object.freeze({ ...base, longSignatures, shortSignatures });
|
|
410
366
|
}
|
|
411
367
|
//# sourceMappingURL=bls.js.map
|