@noble/curves 1.9.0 → 1.9.2
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 +78 -34
- package/_shortw_utils.d.ts +7 -5
- package/_shortw_utils.d.ts.map +1 -1
- package/_shortw_utils.js +2 -8
- package/_shortw_utils.js.map +1 -1
- package/abstract/bls.d.ts +60 -24
- package/abstract/bls.d.ts.map +1 -1
- package/abstract/bls.js +158 -109
- package/abstract/bls.js.map +1 -1
- package/abstract/curve.d.ts +44 -9
- package/abstract/curve.d.ts.map +1 -1
- package/abstract/curve.js +99 -11
- package/abstract/curve.js.map +1 -1
- package/abstract/edwards.d.ts +112 -25
- package/abstract/edwards.d.ts.map +1 -1
- package/abstract/edwards.js +141 -92
- package/abstract/edwards.js.map +1 -1
- package/abstract/fft.d.ts +122 -0
- package/abstract/fft.d.ts.map +1 -0
- package/abstract/fft.js +438 -0
- package/abstract/fft.js.map +1 -0
- package/abstract/hash-to-curve.d.ts +25 -11
- package/abstract/hash-to-curve.d.ts.map +1 -1
- package/abstract/hash-to-curve.js +17 -14
- package/abstract/hash-to-curve.js.map +1 -1
- package/abstract/modular.d.ts +28 -17
- package/abstract/modular.d.ts.map +1 -1
- package/abstract/modular.js +156 -139
- package/abstract/modular.js.map +1 -1
- package/abstract/montgomery.d.ts +3 -8
- package/abstract/montgomery.d.ts.map +1 -1
- package/abstract/montgomery.js +73 -93
- package/abstract/montgomery.js.map +1 -1
- package/abstract/poseidon.d.ts +5 -13
- package/abstract/poseidon.d.ts.map +1 -1
- package/abstract/poseidon.js +12 -7
- package/abstract/poseidon.js.map +1 -1
- package/abstract/tower.d.ts +20 -46
- package/abstract/tower.d.ts.map +1 -1
- package/abstract/tower.js +10 -4
- package/abstract/tower.js.map +1 -1
- package/abstract/utils.d.ts +1 -115
- package/abstract/utils.d.ts.map +1 -1
- package/abstract/utils.js +17 -371
- package/abstract/utils.js.map +1 -1
- package/abstract/weierstrass.d.ts +152 -73
- package/abstract/weierstrass.d.ts.map +1 -1
- package/abstract/weierstrass.js +487 -404
- package/abstract/weierstrass.js.map +1 -1
- package/bls12-381.d.ts +2 -0
- package/bls12-381.d.ts.map +1 -1
- package/bls12-381.js +504 -480
- package/bls12-381.js.map +1 -1
- package/bn254.d.ts +2 -0
- package/bn254.d.ts.map +1 -1
- package/bn254.js +44 -32
- package/bn254.js.map +1 -1
- package/ed25519.d.ts +25 -9
- package/ed25519.d.ts.map +1 -1
- package/ed25519.js +89 -65
- package/ed25519.js.map +1 -1
- package/ed448.d.ts +29 -10
- package/ed448.d.ts.map +1 -1
- package/ed448.js +116 -81
- package/ed448.js.map +1 -1
- package/esm/_shortw_utils.d.ts +7 -5
- package/esm/_shortw_utils.d.ts.map +1 -1
- package/esm/_shortw_utils.js +2 -8
- package/esm/_shortw_utils.js.map +1 -1
- package/esm/abstract/bls.d.ts +60 -24
- package/esm/abstract/bls.d.ts.map +1 -1
- package/esm/abstract/bls.js +158 -109
- package/esm/abstract/bls.js.map +1 -1
- package/esm/abstract/curve.d.ts +44 -9
- package/esm/abstract/curve.d.ts.map +1 -1
- package/esm/abstract/curve.js +96 -12
- package/esm/abstract/curve.js.map +1 -1
- package/esm/abstract/edwards.d.ts +112 -25
- package/esm/abstract/edwards.d.ts.map +1 -1
- package/esm/abstract/edwards.js +141 -94
- package/esm/abstract/edwards.js.map +1 -1
- package/esm/abstract/fft.d.ts +122 -0
- package/esm/abstract/fft.d.ts.map +1 -0
- package/esm/abstract/fft.js +425 -0
- package/esm/abstract/fft.js.map +1 -0
- package/esm/abstract/hash-to-curve.d.ts +25 -11
- package/esm/abstract/hash-to-curve.d.ts.map +1 -1
- package/esm/abstract/hash-to-curve.js +17 -14
- package/esm/abstract/hash-to-curve.js.map +1 -1
- package/esm/abstract/modular.d.ts +28 -17
- package/esm/abstract/modular.d.ts.map +1 -1
- package/esm/abstract/modular.js +155 -138
- package/esm/abstract/modular.js.map +1 -1
- package/esm/abstract/montgomery.d.ts +3 -8
- package/esm/abstract/montgomery.d.ts.map +1 -1
- package/esm/abstract/montgomery.js +74 -94
- package/esm/abstract/montgomery.js.map +1 -1
- package/esm/abstract/poseidon.d.ts +5 -13
- package/esm/abstract/poseidon.d.ts.map +1 -1
- package/esm/abstract/poseidon.js +12 -7
- package/esm/abstract/poseidon.js.map +1 -1
- package/esm/abstract/tower.d.ts +20 -46
- package/esm/abstract/tower.d.ts.map +1 -1
- package/esm/abstract/tower.js +10 -4
- package/esm/abstract/tower.js.map +1 -1
- package/esm/abstract/utils.d.ts +1 -115
- package/esm/abstract/utils.d.ts.map +1 -1
- package/esm/abstract/utils.js +3 -344
- package/esm/abstract/utils.js.map +1 -1
- package/esm/abstract/weierstrass.d.ts +152 -73
- package/esm/abstract/weierstrass.d.ts.map +1 -1
- package/esm/abstract/weierstrass.js +485 -406
- package/esm/abstract/weierstrass.js.map +1 -1
- package/esm/bls12-381.d.ts +2 -0
- package/esm/bls12-381.d.ts.map +1 -1
- package/esm/bls12-381.js +503 -479
- package/esm/bls12-381.js.map +1 -1
- package/esm/bn254.d.ts +2 -0
- package/esm/bn254.d.ts.map +1 -1
- package/esm/bn254.js +41 -29
- package/esm/bn254.js.map +1 -1
- package/esm/ed25519.d.ts +25 -9
- package/esm/ed25519.d.ts.map +1 -1
- package/esm/ed25519.js +84 -60
- package/esm/ed25519.js.map +1 -1
- package/esm/ed448.d.ts +29 -10
- package/esm/ed448.d.ts.map +1 -1
- package/esm/ed448.js +113 -78
- package/esm/ed448.js.map +1 -1
- package/esm/jubjub.d.ts +4 -0
- package/esm/jubjub.d.ts.map +1 -1
- package/esm/jubjub.js +4 -0
- package/esm/jubjub.js.map +1 -1
- package/esm/misc.d.ts.map +1 -1
- package/esm/misc.js +31 -26
- package/esm/misc.js.map +1 -1
- package/esm/nist.d.ts +8 -16
- package/esm/nist.d.ts.map +1 -1
- package/esm/nist.js +87 -97
- package/esm/nist.js.map +1 -1
- package/esm/p256.d.ts +3 -3
- package/esm/p384.d.ts +3 -3
- package/esm/p521.d.ts +3 -3
- package/esm/pasta.d.ts +4 -0
- package/esm/pasta.d.ts.map +1 -1
- package/esm/pasta.js +4 -0
- package/esm/pasta.js.map +1 -1
- package/esm/secp256k1.d.ts +6 -6
- package/esm/secp256k1.d.ts.map +1 -1
- package/esm/secp256k1.js +44 -41
- package/esm/secp256k1.js.map +1 -1
- package/esm/utils.d.ts +96 -0
- package/esm/utils.d.ts.map +1 -0
- package/esm/utils.js +279 -0
- package/esm/utils.js.map +1 -0
- package/jubjub.d.ts +4 -0
- package/jubjub.d.ts.map +1 -1
- package/jubjub.js +4 -0
- package/jubjub.js.map +1 -1
- package/misc.d.ts.map +1 -1
- package/misc.js +35 -30
- package/misc.js.map +1 -1
- package/nist.d.ts +8 -16
- package/nist.d.ts.map +1 -1
- package/nist.js +87 -97
- package/nist.js.map +1 -1
- package/p256.d.ts +3 -3
- package/p384.d.ts +3 -3
- package/p521.d.ts +3 -3
- package/package.json +26 -8
- package/pasta.d.ts +4 -0
- package/pasta.d.ts.map +1 -1
- package/pasta.js +4 -0
- package/pasta.js.map +1 -1
- package/secp256k1.d.ts +6 -6
- package/secp256k1.d.ts.map +1 -1
- package/secp256k1.js +47 -44
- package/secp256k1.js.map +1 -1
- package/src/_shortw_utils.ts +5 -15
- package/src/abstract/bls.ts +260 -145
- package/src/abstract/curve.ts +125 -18
- package/src/abstract/edwards.ts +282 -127
- package/src/abstract/fft.ts +519 -0
- package/src/abstract/hash-to-curve.ts +51 -27
- package/src/abstract/modular.ts +156 -143
- package/src/abstract/montgomery.ts +81 -111
- package/src/abstract/poseidon.ts +22 -18
- package/src/abstract/tower.ts +37 -68
- package/src/abstract/utils.ts +3 -378
- package/src/abstract/weierstrass.ts +752 -461
- package/src/bls12-381.ts +542 -507
- package/src/bn254.ts +47 -35
- package/src/ed25519.ts +104 -76
- package/src/ed448.ts +156 -105
- package/src/jubjub.ts +4 -0
- package/src/misc.ts +39 -34
- package/src/nist.ts +138 -126
- package/src/p256.ts +3 -3
- package/src/p384.ts +3 -3
- package/src/p521.ts +3 -3
- package/src/pasta.ts +5 -1
- package/src/secp256k1.ts +59 -47
- package/src/utils.ts +328 -0
- package/utils.d.ts +96 -0
- package/utils.d.ts.map +1 -0
- package/utils.js +313 -0
- package/utils.js.map +1 -0
package/esm/abstract/bls.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* BLS (Barreto-Lynn-Scott) family of pairing-friendly curves.
|
|
3
2
|
* BLS != BLS.
|
|
4
3
|
* The file implements BLS (Boneh-Lynn-Shacham) signatures.
|
|
5
4
|
* Used in both BLS (Barreto-Lynn-Scott) and BN (Barreto-Naehrig)
|
|
@@ -10,26 +9,32 @@
|
|
|
10
9
|
* - Gt, created by bilinear (ate) pairing e(G1, G2), consists of p-th roots of unity in
|
|
11
10
|
* Fq^k where k is embedding degree. Only degree 12 is currently supported, 24 is not.
|
|
12
11
|
* Pairing is used to aggregate and verify signatures.
|
|
13
|
-
* There are two
|
|
14
|
-
*
|
|
15
|
-
*
|
|
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).
|
|
16
15
|
* @module
|
|
17
16
|
**/
|
|
18
17
|
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
19
|
-
import { type
|
|
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
20
|
import { type IField } from './modular.ts';
|
|
21
|
-
import type { Fp12, Fp12Bls, Fp2, Fp2Bls,
|
|
22
|
-
import { type
|
|
23
|
-
import { type CurvePointsRes, type CurvePointsType, type ProjPointType } from './weierstrass.ts';
|
|
21
|
+
import type { Fp12, Fp12Bls, Fp2, Fp2Bls, Fp6Bls } from './tower.ts';
|
|
22
|
+
import { type CurvePointsRes, type CurvePointsType, type ProjConstructor, type ProjPointType } from './weierstrass.ts';
|
|
24
23
|
type Fp = bigint;
|
|
25
24
|
export type TwistType = 'multiplicative' | 'divisive';
|
|
26
25
|
export type ShortSignatureCoder<Fp> = {
|
|
26
|
+
fromBytes(bytes: Uint8Array): ProjPointType<Fp>;
|
|
27
27
|
fromHex(hex: Hex): ProjPointType<Fp>;
|
|
28
|
+
toBytes(point: ProjPointType<Fp>): Uint8Array;
|
|
29
|
+
/** @deprecated use `toBytes` */
|
|
28
30
|
toRawBytes(point: ProjPointType<Fp>): Uint8Array;
|
|
29
31
|
toHex(point: ProjPointType<Fp>): string;
|
|
30
32
|
};
|
|
31
33
|
export type SignatureCoder<Fp> = {
|
|
34
|
+
fromBytes(bytes: Uint8Array): ProjPointType<Fp>;
|
|
32
35
|
fromHex(hex: Hex): ProjPointType<Fp>;
|
|
36
|
+
toBytes(point: ProjPointType<Fp>): Uint8Array;
|
|
37
|
+
/** @deprecated use `toBytes` */
|
|
33
38
|
toRawBytes(point: ProjPointType<Fp>): Uint8Array;
|
|
34
39
|
toHex(point: ProjPointType<Fp>): string;
|
|
35
40
|
};
|
|
@@ -40,21 +45,21 @@ export type PostPrecomputePointAddFn = (Rx: Fp2, Ry: Fp2, Rz: Fp2, Qx: Fp2, Qy:
|
|
|
40
45
|
};
|
|
41
46
|
export type PostPrecomputeFn = (Rx: Fp2, Ry: Fp2, Rz: Fp2, Qx: Fp2, Qy: Fp2, pointAdd: PostPrecomputePointAddFn) => void;
|
|
42
47
|
export type CurveType = {
|
|
43
|
-
G1:
|
|
48
|
+
G1: CurvePointsType<Fp> & {
|
|
44
49
|
ShortSignature: SignatureCoder<Fp>;
|
|
45
50
|
mapToCurve: MapToCurve<Fp>;
|
|
46
|
-
htfDefaults:
|
|
51
|
+
htfDefaults: H2COpts;
|
|
47
52
|
};
|
|
48
|
-
G2:
|
|
53
|
+
G2: CurvePointsType<Fp2> & {
|
|
49
54
|
Signature: SignatureCoder<Fp2>;
|
|
50
55
|
mapToCurve: MapToCurve<Fp2>;
|
|
51
|
-
htfDefaults:
|
|
56
|
+
htfDefaults: H2COpts;
|
|
52
57
|
};
|
|
53
58
|
fields: {
|
|
54
59
|
Fp: IField<Fp>;
|
|
55
60
|
Fr: IField<bigint>;
|
|
56
61
|
Fp2: Fp2Bls;
|
|
57
|
-
Fp6:
|
|
62
|
+
Fp6: Fp6Bls;
|
|
58
63
|
Fp12: Fp12Bls;
|
|
59
64
|
};
|
|
60
65
|
params: {
|
|
@@ -63,59 +68,80 @@ export type CurveType = {
|
|
|
63
68
|
r: bigint;
|
|
64
69
|
twistType: TwistType;
|
|
65
70
|
};
|
|
66
|
-
htfDefaults:
|
|
71
|
+
htfDefaults: H2COpts;
|
|
67
72
|
hash: CHash;
|
|
68
|
-
randomBytes
|
|
73
|
+
randomBytes?: (bytesLength?: number) => Uint8Array;
|
|
69
74
|
postPrecompute?: PostPrecomputeFn;
|
|
70
75
|
};
|
|
71
76
|
type PrecomputeSingle = [Fp2, Fp2, Fp2][];
|
|
72
77
|
type Precompute = PrecomputeSingle[];
|
|
73
78
|
export type CurveFn = {
|
|
79
|
+
longSignatures: BLSSigs<bigint, Fp2>;
|
|
80
|
+
shortSignatures: BLSSigs<Fp2, bigint>;
|
|
81
|
+
millerLoopBatch: (pairs: [Precompute, Fp, Fp][]) => Fp12;
|
|
82
|
+
pairing: (P: ProjPointType<Fp>, Q: ProjPointType<Fp2>, withFinalExponent?: boolean) => Fp12;
|
|
83
|
+
pairingBatch: (pairs: {
|
|
84
|
+
g1: ProjPointType<Fp>;
|
|
85
|
+
g2: ProjPointType<Fp2>;
|
|
86
|
+
}[], withFinalExponent?: boolean) => Fp12;
|
|
87
|
+
/** @deprecated use `longSignatures.getPublicKey` */
|
|
74
88
|
getPublicKey: (privateKey: PrivKey) => Uint8Array;
|
|
89
|
+
/** @deprecated use `shortSignatures.getPublicKey` */
|
|
75
90
|
getPublicKeyForShortSignatures: (privateKey: PrivKey) => Uint8Array;
|
|
91
|
+
/** @deprecated use `longSignatures.sign` */
|
|
76
92
|
sign: {
|
|
77
93
|
(message: Hex, privateKey: PrivKey, htfOpts?: htfBasicOpts): Uint8Array;
|
|
78
94
|
(message: ProjPointType<Fp2>, privateKey: PrivKey, htfOpts?: htfBasicOpts): ProjPointType<Fp2>;
|
|
79
95
|
};
|
|
96
|
+
/** @deprecated use `shortSignatures.sign` */
|
|
80
97
|
signShortSignature: {
|
|
81
98
|
(message: Hex, privateKey: PrivKey, htfOpts?: htfBasicOpts): Uint8Array;
|
|
82
99
|
(message: ProjPointType<Fp>, privateKey: PrivKey, htfOpts?: htfBasicOpts): ProjPointType<Fp>;
|
|
83
100
|
};
|
|
101
|
+
/** @deprecated use `longSignatures.verify` */
|
|
84
102
|
verify: (signature: Hex | ProjPointType<Fp2>, message: Hex | ProjPointType<Fp2>, publicKey: Hex | ProjPointType<Fp>, htfOpts?: htfBasicOpts) => boolean;
|
|
103
|
+
/** @deprecated use `shortSignatures.verify` */
|
|
85
104
|
verifyShortSignature: (signature: Hex | ProjPointType<Fp>, message: Hex | ProjPointType<Fp>, publicKey: Hex | ProjPointType<Fp2>, htfOpts?: htfBasicOpts) => boolean;
|
|
86
105
|
verifyBatch: (signature: Hex | ProjPointType<Fp2>, messages: (Hex | ProjPointType<Fp2>)[], publicKeys: (Hex | ProjPointType<Fp>)[], htfOpts?: htfBasicOpts) => boolean;
|
|
106
|
+
/** @deprecated use `longSignatures.aggregatePublicKeys` */
|
|
87
107
|
aggregatePublicKeys: {
|
|
88
108
|
(publicKeys: Hex[]): Uint8Array;
|
|
89
109
|
(publicKeys: ProjPointType<Fp>[]): ProjPointType<Fp>;
|
|
90
110
|
};
|
|
111
|
+
/** @deprecated use `longSignatures.aggregateSignatures` */
|
|
91
112
|
aggregateSignatures: {
|
|
92
113
|
(signatures: Hex[]): Uint8Array;
|
|
93
114
|
(signatures: ProjPointType<Fp2>[]): ProjPointType<Fp2>;
|
|
94
115
|
};
|
|
116
|
+
/** @deprecated use `shortSignatures.aggregateSignatures` */
|
|
95
117
|
aggregateShortSignatures: {
|
|
96
118
|
(signatures: Hex[]): Uint8Array;
|
|
97
119
|
(signatures: ProjPointType<Fp>[]): ProjPointType<Fp>;
|
|
98
120
|
};
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
g2: ProjPointType<Fp2>;
|
|
104
|
-
}[], withFinalExponent?: boolean) => Fp12;
|
|
105
|
-
G1: CurvePointsRes<Fp> & ReturnType<typeof createHasher<Fp>>;
|
|
106
|
-
G2: CurvePointsRes<Fp2> & ReturnType<typeof createHasher<Fp2>>;
|
|
121
|
+
/** @deprecated use `curves.G1` and `curves.G2` */
|
|
122
|
+
G1: CurvePointsRes<Fp> & H2CHasher<Fp>;
|
|
123
|
+
G2: CurvePointsRes<Fp2> & H2CHasher<Fp2>;
|
|
124
|
+
/** @deprecated use `longSignatures.Signature` */
|
|
107
125
|
Signature: SignatureCoder<Fp2>;
|
|
126
|
+
/** @deprecated use `shortSignatures.Signature` */
|
|
108
127
|
ShortSignature: ShortSignatureCoder<Fp>;
|
|
109
128
|
params: {
|
|
110
129
|
ateLoopSize: bigint;
|
|
111
130
|
r: bigint;
|
|
131
|
+
twistType: TwistType;
|
|
132
|
+
/** @deprecated */
|
|
112
133
|
G1b: bigint;
|
|
134
|
+
/** @deprecated */
|
|
113
135
|
G2b: Fp2;
|
|
114
136
|
};
|
|
137
|
+
curves: {
|
|
138
|
+
G1: ProjConstructor<bigint>;
|
|
139
|
+
G2: ProjConstructor<Fp2>;
|
|
140
|
+
};
|
|
115
141
|
fields: {
|
|
116
142
|
Fp: IField<Fp>;
|
|
117
143
|
Fp2: Fp2Bls;
|
|
118
|
-
Fp6:
|
|
144
|
+
Fp6: Fp6Bls;
|
|
119
145
|
Fp12: Fp12Bls;
|
|
120
146
|
Fr: IField<bigint>;
|
|
121
147
|
};
|
|
@@ -124,6 +150,16 @@ export type CurveFn = {
|
|
|
124
150
|
calcPairingPrecomputes: (p: ProjPointType<Fp2>) => Precompute;
|
|
125
151
|
};
|
|
126
152
|
};
|
|
153
|
+
type BLSInput = Hex | Uint8Array;
|
|
154
|
+
export interface BLSSigs<P, S> {
|
|
155
|
+
getPublicKey(privateKey: PrivKey): ProjPointType<P>;
|
|
156
|
+
sign(hashedMessage: ProjPointType<S>, privateKey: PrivKey): ProjPointType<S>;
|
|
157
|
+
verify(signature: ProjPointType<S> | BLSInput, message: ProjPointType<S>, publicKey: ProjPointType<P> | BLSInput): boolean;
|
|
158
|
+
aggregatePublicKeys(publicKeys: (ProjPointType<P> | BLSInput)[]): ProjPointType<P>;
|
|
159
|
+
aggregateSignatures(signatures: (ProjPointType<S> | BLSInput)[]): ProjPointType<S>;
|
|
160
|
+
hash(message: Uint8Array, DST?: string | Uint8Array, hashOpts?: H2CHashOpts): ProjPointType<S>;
|
|
161
|
+
Signature: SignatureCoder<S>;
|
|
162
|
+
}
|
|
127
163
|
export declare function bls(CURVE: CurveType): CurveFn;
|
|
128
164
|
export {};
|
|
129
165
|
//# sourceMappingURL=bls.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bls.d.ts","sourceRoot":"","sources":["../../src/abstract/bls.ts"],"names":[],"mappings":"AAAA
|
|
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;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
|
|
1
1
|
/**
|
|
2
|
-
* BLS (Barreto-Lynn-Scott) family of pairing-friendly curves.
|
|
3
2
|
* BLS != BLS.
|
|
4
3
|
* The file implements BLS (Boneh-Lynn-Shacham) signatures.
|
|
5
4
|
* Used in both BLS (Barreto-Lynn-Scott) and BN (Barreto-Naehrig)
|
|
@@ -10,16 +9,16 @@
|
|
|
10
9
|
* - Gt, created by bilinear (ate) pairing e(G1, G2), consists of p-th roots of unity in
|
|
11
10
|
* Fq^k where k is embedding degree. Only degree 12 is currently supported, 24 is not.
|
|
12
11
|
* Pairing is used to aggregate and verify signatures.
|
|
13
|
-
* There are two
|
|
14
|
-
*
|
|
15
|
-
*
|
|
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).
|
|
16
15
|
* @module
|
|
17
16
|
**/
|
|
18
17
|
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
19
|
-
|
|
18
|
+
import { abytes, ensureBytes, memoized, randomBytes, } from "../utils.js";
|
|
19
|
+
import { normalizeZ } from "./curve.js";
|
|
20
20
|
import { createHasher, } from "./hash-to-curve.js";
|
|
21
21
|
import { getMinHashLength, mapHashToField } from "./modular.js";
|
|
22
|
-
import { ensureBytes, memoized } from "./utils.js";
|
|
23
22
|
import { weierstrassPoints, } from "./weierstrass.js";
|
|
24
23
|
// prettier-ignore
|
|
25
24
|
const _0n = BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3);
|
|
@@ -39,20 +38,21 @@ function NAfDecomposition(a) {
|
|
|
39
38
|
}
|
|
40
39
|
return res;
|
|
41
40
|
}
|
|
41
|
+
// G1_Point: ProjConstructor<bigint>, G2_Point: ProjConstructor<Fp2>,
|
|
42
42
|
export function bls(CURVE) {
|
|
43
43
|
// Fields are specific for curve, so for now we'll need to pass them with opts
|
|
44
44
|
const { Fp, Fr, Fp2, Fp6, Fp12 } = CURVE.fields;
|
|
45
45
|
const BLS_X_IS_NEGATIVE = CURVE.params.xNegative;
|
|
46
46
|
const TWIST = CURVE.params.twistType;
|
|
47
47
|
// Point on G1 curve: (x, y)
|
|
48
|
-
const G1_ = weierstrassPoints(
|
|
49
|
-
const G1 = Object.assign(G1_, createHasher(G1_.
|
|
48
|
+
const G1_ = weierstrassPoints(CURVE.G1);
|
|
49
|
+
const G1 = Object.assign(G1_, createHasher(G1_.Point, CURVE.G1.mapToCurve, {
|
|
50
50
|
...CURVE.htfDefaults,
|
|
51
51
|
...CURVE.G1.htfDefaults,
|
|
52
52
|
}));
|
|
53
53
|
// Point on G2 curve (complex numbers): (x₁, x₂+i), (y₁, y₂+i)
|
|
54
|
-
const G2_ = weierstrassPoints(
|
|
55
|
-
const G2 = Object.assign(G2_, createHasher(G2_.
|
|
54
|
+
const G2_ = weierstrassPoints(CURVE.G2);
|
|
55
|
+
const G2 = Object.assign(G2_, createHasher(G2_.Point, CURVE.G2.mapToCurve, {
|
|
56
56
|
...CURVE.htfDefaults,
|
|
57
57
|
...CURVE.G2.htfDefaults,
|
|
58
58
|
}));
|
|
@@ -149,10 +149,10 @@ export function bls(CURVE) {
|
|
|
149
149
|
function pairingBatch(pairs, withFinalExponent = true) {
|
|
150
150
|
const res = [];
|
|
151
151
|
// Cache precomputed toAffine for all points
|
|
152
|
-
G1.
|
|
153
|
-
G2.
|
|
152
|
+
normalizeZ(G1.Point, 'pz', pairs.map(({ g1 }) => g1));
|
|
153
|
+
normalizeZ(G2.Point, 'pz', pairs.map(({ g2 }) => g2));
|
|
154
154
|
for (const { g1, g2 } of pairs) {
|
|
155
|
-
if (g1.
|
|
155
|
+
if (g1.is0() || g2.is0())
|
|
156
156
|
throw new Error('pairing is not available for ZERO point');
|
|
157
157
|
// This uses toAffine inside
|
|
158
158
|
g1.assertValidity();
|
|
@@ -166,123 +166,162 @@ export function bls(CURVE) {
|
|
|
166
166
|
function pairing(Q, P, withFinalExponent = true) {
|
|
167
167
|
return pairingBatch([{ g1: Q, g2: P }], withFinalExponent);
|
|
168
168
|
}
|
|
169
|
+
const rand = CURVE.randomBytes || randomBytes;
|
|
169
170
|
const utils = {
|
|
170
171
|
randomPrivateKey: () => {
|
|
171
172
|
const length = getMinHashLength(Fr.ORDER);
|
|
172
|
-
return mapHashToField(
|
|
173
|
+
return mapHashToField(rand(length), Fr.ORDER);
|
|
173
174
|
},
|
|
174
175
|
calcPairingPrecomputes,
|
|
175
176
|
};
|
|
176
|
-
|
|
177
|
-
|
|
177
|
+
function aNonEmpty(arr) {
|
|
178
|
+
if (!Array.isArray(arr) || arr.length === 0)
|
|
179
|
+
throw new Error('expected non-empty array');
|
|
180
|
+
}
|
|
178
181
|
function normP1(point) {
|
|
179
|
-
return point instanceof G1.
|
|
182
|
+
return point instanceof G1.Point ? point : G1.Point.fromHex(point);
|
|
183
|
+
}
|
|
184
|
+
function normP2(point) {
|
|
185
|
+
return point instanceof G2.Point ? point : Signature.fromHex(point);
|
|
180
186
|
}
|
|
187
|
+
// TODO: add verifyBatch, fix types, Export Signature property,
|
|
188
|
+
// actually expose the generated APIs
|
|
189
|
+
function createBls(PubCurve, SigCurve) {
|
|
190
|
+
function normPub(point) {
|
|
191
|
+
return point instanceof PubCurve.Point ? point : PubCurve.Point.fromHex(point);
|
|
192
|
+
}
|
|
193
|
+
function normSig(point) {
|
|
194
|
+
return point instanceof SigCurve.Point ? point : SigCurve.Point.fromHex(point);
|
|
195
|
+
}
|
|
196
|
+
function amsg(m) {
|
|
197
|
+
if (!(m instanceof SigCurve.Point))
|
|
198
|
+
throw new Error(`expected valid message hashed to ${isLongSigs ? 'G2' : 'G1'} curve`);
|
|
199
|
+
return m;
|
|
200
|
+
}
|
|
201
|
+
// TODO: is this always ok?
|
|
202
|
+
const isLongSigs = SigCurve.Point.Fp.BYTES > PubCurve.Point.Fp.BYTES;
|
|
203
|
+
return {
|
|
204
|
+
// P = pk x G
|
|
205
|
+
getPublicKey(privateKey) {
|
|
206
|
+
return PubCurve.Point.fromPrivateKey(privateKey);
|
|
207
|
+
},
|
|
208
|
+
// S = pk x H(m)
|
|
209
|
+
sign(message, privateKey, unusedArg) {
|
|
210
|
+
if (unusedArg != null)
|
|
211
|
+
throw new Error('sign() expects 2 arguments');
|
|
212
|
+
amsg(message).assertValidity();
|
|
213
|
+
return message.multiply(PubCurve.normPrivateKeyToScalar(privateKey));
|
|
214
|
+
},
|
|
215
|
+
// Checks if pairing of public key & hash is equal to pairing of generator & signature.
|
|
216
|
+
// e(P, H(m)) == e(G, S)
|
|
217
|
+
// e(S, G) == e(H(m), P)
|
|
218
|
+
verify(signature, message, publicKey, unusedArg) {
|
|
219
|
+
if (unusedArg != null)
|
|
220
|
+
throw new Error('verify() expects 3 arguments');
|
|
221
|
+
signature = normSig(signature);
|
|
222
|
+
publicKey = normPub(publicKey);
|
|
223
|
+
const P = publicKey.negate();
|
|
224
|
+
const G = PubCurve.Point.BASE;
|
|
225
|
+
const Hm = amsg(message);
|
|
226
|
+
const S = signature;
|
|
227
|
+
// This code was changed in 1.9.x:
|
|
228
|
+
// Before it was G.negate() in G2, now it's always pubKey.negate
|
|
229
|
+
// TODO: understand if this is OK?
|
|
230
|
+
// prettier-ignore
|
|
231
|
+
const exp_ = isLongSigs ? [
|
|
232
|
+
{ g1: P, g2: Hm },
|
|
233
|
+
{ g1: G, g2: S }
|
|
234
|
+
] : [
|
|
235
|
+
{ g1: Hm, g2: P },
|
|
236
|
+
{ g1: S, g2: G }
|
|
237
|
+
];
|
|
238
|
+
// TODO
|
|
239
|
+
// @ts-ignore
|
|
240
|
+
const exp = pairingBatch(exp_);
|
|
241
|
+
return Fp12.eql(exp, Fp12.ONE);
|
|
242
|
+
},
|
|
243
|
+
// Adds a bunch of public key points together.
|
|
244
|
+
// pk1 + pk2 + pk3 = pkA
|
|
245
|
+
aggregatePublicKeys(publicKeys) {
|
|
246
|
+
aNonEmpty(publicKeys);
|
|
247
|
+
publicKeys = publicKeys.map((pub) => normPub(pub));
|
|
248
|
+
const agg = publicKeys.reduce((sum, p) => sum.add(p), PubCurve.Point.ZERO);
|
|
249
|
+
agg.assertValidity();
|
|
250
|
+
return agg;
|
|
251
|
+
},
|
|
252
|
+
// Adds a bunch of signature points together.
|
|
253
|
+
// pk1 + pk2 + pk3 = pkA
|
|
254
|
+
aggregateSignatures(signatures) {
|
|
255
|
+
aNonEmpty(signatures);
|
|
256
|
+
signatures = signatures.map((sig) => normSig(sig));
|
|
257
|
+
const agg = signatures.reduce((sum, s) => sum.add(s), SigCurve.Point.ZERO);
|
|
258
|
+
agg.assertValidity();
|
|
259
|
+
return agg;
|
|
260
|
+
},
|
|
261
|
+
hash(messageBytes, DST) {
|
|
262
|
+
abytes(messageBytes);
|
|
263
|
+
const opts = DST ? { DST } : undefined;
|
|
264
|
+
return SigCurve.hashToCurve(messageBytes, opts);
|
|
265
|
+
},
|
|
266
|
+
// @ts-ignore
|
|
267
|
+
Signature: isLongSigs ? CURVE.G2.Signature : CURVE.G1.ShortSignature,
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
const longSignatures = createBls(G1, G2);
|
|
271
|
+
const shortSignatures = createBls(G2, G1);
|
|
272
|
+
// LEGACY code
|
|
273
|
+
const { ShortSignature } = CURVE.G1;
|
|
274
|
+
const { Signature } = CURVE.G2;
|
|
181
275
|
function normP1Hash(point, htfOpts) {
|
|
182
|
-
return point instanceof G1.
|
|
276
|
+
return point instanceof G1.Point
|
|
183
277
|
? point
|
|
184
|
-
:
|
|
185
|
-
}
|
|
186
|
-
function normP2(point) {
|
|
187
|
-
return point instanceof G2.ProjectivePoint ? point : Signature.fromHex(point);
|
|
278
|
+
: shortSignatures.hash(ensureBytes('point', point), htfOpts?.DST);
|
|
188
279
|
}
|
|
189
280
|
function normP2Hash(point, htfOpts) {
|
|
190
|
-
return point instanceof G2.
|
|
281
|
+
return point instanceof G2.Point
|
|
191
282
|
? point
|
|
192
|
-
:
|
|
283
|
+
: longSignatures.hash(ensureBytes('point', point), htfOpts?.DST);
|
|
193
284
|
}
|
|
194
|
-
// Multiplies generator (G1) by private key.
|
|
195
|
-
// P = pk x G
|
|
196
285
|
function getPublicKey(privateKey) {
|
|
197
|
-
return
|
|
286
|
+
return longSignatures.getPublicKey(privateKey).toBytes(true);
|
|
198
287
|
}
|
|
199
|
-
// Multiplies generator (G2) by private key.
|
|
200
|
-
// P = pk x G
|
|
201
288
|
function getPublicKeyForShortSignatures(privateKey) {
|
|
202
|
-
return
|
|
289
|
+
return shortSignatures.getPublicKey(privateKey).toBytes(true);
|
|
203
290
|
}
|
|
204
291
|
function sign(message, privateKey, htfOpts) {
|
|
205
|
-
const
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
if (message instanceof G2.ProjectivePoint)
|
|
209
|
-
return sigPoint;
|
|
210
|
-
return Signature.toRawBytes(sigPoint);
|
|
292
|
+
const Hm = normP2Hash(message, htfOpts);
|
|
293
|
+
const S = longSignatures.sign(Hm, privateKey);
|
|
294
|
+
return message instanceof G2.Point ? S : Signature.toBytes(S);
|
|
211
295
|
}
|
|
212
296
|
function signShortSignature(message, privateKey, htfOpts) {
|
|
213
|
-
const
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
if (message instanceof G1.ProjectivePoint)
|
|
217
|
-
return sigPoint;
|
|
218
|
-
return ShortSignature.toRawBytes(sigPoint);
|
|
297
|
+
const Hm = normP1Hash(message, htfOpts);
|
|
298
|
+
const S = shortSignatures.sign(Hm, privateKey);
|
|
299
|
+
return message instanceof G1.Point ? S : ShortSignature.toBytes(S);
|
|
219
300
|
}
|
|
220
|
-
// Checks if pairing of public key & hash is equal to pairing of generator & signature.
|
|
221
|
-
// e(P, H(m)) == e(G, S)
|
|
222
301
|
function verify(signature, message, publicKey, htfOpts) {
|
|
223
|
-
const P = normP1(publicKey);
|
|
224
302
|
const Hm = normP2Hash(message, htfOpts);
|
|
225
|
-
|
|
226
|
-
const S = normP2(signature);
|
|
227
|
-
const exp = pairingBatch([
|
|
228
|
-
{ g1: P.negate(), g2: Hm }, // ePHM = pairing(P.negate(), Hm, false);
|
|
229
|
-
{ g1: G, g2: S }, // eGS = pairing(G, S, false);
|
|
230
|
-
]);
|
|
231
|
-
return Fp12.eql(exp, Fp12.ONE);
|
|
303
|
+
return longSignatures.verify(signature, Hm, publicKey);
|
|
232
304
|
}
|
|
233
|
-
// Checks if pairing of public key & hash is equal to pairing of generator & signature.
|
|
234
|
-
// e(S, G) == e(H(m), P)
|
|
235
305
|
function verifyShortSignature(signature, message, publicKey, htfOpts) {
|
|
236
|
-
const P = normP2(publicKey);
|
|
237
306
|
const Hm = normP1Hash(message, htfOpts);
|
|
238
|
-
|
|
239
|
-
const S = normP1(signature);
|
|
240
|
-
const exp = pairingBatch([
|
|
241
|
-
{ g1: Hm, g2: P }, // eHmP = pairing(Hm, P, false);
|
|
242
|
-
{ g1: S, g2: G.negate() }, // eSG = pairing(S, G.negate(), false);
|
|
243
|
-
]);
|
|
244
|
-
return Fp12.eql(exp, Fp12.ONE);
|
|
245
|
-
}
|
|
246
|
-
function aNonEmpty(arr) {
|
|
247
|
-
if (!Array.isArray(arr) || arr.length === 0)
|
|
248
|
-
throw new Error('expected non-empty array');
|
|
307
|
+
return shortSignatures.verify(signature, Hm, publicKey);
|
|
249
308
|
}
|
|
250
309
|
function aggregatePublicKeys(publicKeys) {
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
const aggAffine = agg; //.toAffine();
|
|
254
|
-
if (publicKeys[0] instanceof G1.ProjectivePoint) {
|
|
255
|
-
aggAffine.assertValidity();
|
|
256
|
-
return aggAffine;
|
|
257
|
-
}
|
|
258
|
-
// toRawBytes ensures point validity
|
|
259
|
-
return aggAffine.toRawBytes(true);
|
|
310
|
+
const agg = longSignatures.aggregatePublicKeys(publicKeys);
|
|
311
|
+
return publicKeys[0] instanceof G1.Point ? agg : agg.toBytes(true);
|
|
260
312
|
}
|
|
261
313
|
function aggregateSignatures(signatures) {
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
const aggAffine = agg; //.toAffine();
|
|
265
|
-
if (signatures[0] instanceof G2.ProjectivePoint) {
|
|
266
|
-
aggAffine.assertValidity();
|
|
267
|
-
return aggAffine;
|
|
268
|
-
}
|
|
269
|
-
return Signature.toRawBytes(aggAffine);
|
|
314
|
+
const agg = longSignatures.aggregateSignatures(signatures);
|
|
315
|
+
return signatures[0] instanceof G2.Point ? agg : Signature.toBytes(agg);
|
|
270
316
|
}
|
|
271
317
|
function aggregateShortSignatures(signatures) {
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
const aggAffine = agg; //.toAffine();
|
|
275
|
-
if (signatures[0] instanceof G1.ProjectivePoint) {
|
|
276
|
-
aggAffine.assertValidity();
|
|
277
|
-
return aggAffine;
|
|
278
|
-
}
|
|
279
|
-
return ShortSignature.toRawBytes(aggAffine);
|
|
318
|
+
const agg = shortSignatures.aggregateSignatures(signatures);
|
|
319
|
+
return signatures[0] instanceof G1.Point ? agg : ShortSignature.toBytes(agg);
|
|
280
320
|
}
|
|
281
321
|
// https://ethresear.ch/t/fast-verification-of-multiple-bls-signatures/5407
|
|
282
322
|
// e(G, S) = e(G, SUM(n)(Si)) = MUL(n)(e(G, Si))
|
|
283
|
-
function verifyBatch(signature,
|
|
284
323
|
// TODO: maybe `{message: G2Hex, publicKey: G1Hex}[]` instead?
|
|
285
|
-
messages, publicKeys, htfOpts) {
|
|
324
|
+
function verifyBatch(signature, messages, publicKeys, htfOpts) {
|
|
286
325
|
aNonEmpty(messages);
|
|
287
326
|
if (publicKeys.length !== messages.length)
|
|
288
327
|
throw new Error('amount of public keys and messages should be equal');
|
|
@@ -307,32 +346,26 @@ export function bls(CURVE) {
|
|
|
307
346
|
const groupPublicKey = keys.reduce((acc, msg) => acc.add(msg));
|
|
308
347
|
paired.push({ g1: groupPublicKey, g2: msg });
|
|
309
348
|
}
|
|
310
|
-
paired.push({ g1: G1.
|
|
349
|
+
paired.push({ g1: G1.Point.BASE.negate(), g2: sig });
|
|
311
350
|
return Fp12.eql(pairingBatch(paired), Fp12.ONE);
|
|
312
351
|
}
|
|
313
352
|
catch {
|
|
314
353
|
return false;
|
|
315
354
|
}
|
|
316
355
|
}
|
|
317
|
-
G1.
|
|
356
|
+
G1.Point.BASE.precompute(4);
|
|
318
357
|
return {
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
sign,
|
|
322
|
-
signShortSignature,
|
|
323
|
-
verify,
|
|
324
|
-
verifyBatch,
|
|
325
|
-
verifyShortSignature,
|
|
326
|
-
aggregatePublicKeys,
|
|
327
|
-
aggregateSignatures,
|
|
328
|
-
aggregateShortSignatures,
|
|
358
|
+
longSignatures,
|
|
359
|
+
shortSignatures,
|
|
329
360
|
millerLoopBatch,
|
|
330
361
|
pairing,
|
|
331
362
|
pairingBatch,
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
363
|
+
// TODO!!!
|
|
364
|
+
verifyBatch,
|
|
365
|
+
curves: {
|
|
366
|
+
G1: G1_.Point,
|
|
367
|
+
G2: G2_.Point,
|
|
368
|
+
},
|
|
336
369
|
fields: {
|
|
337
370
|
Fr,
|
|
338
371
|
Fp,
|
|
@@ -342,11 +375,27 @@ export function bls(CURVE) {
|
|
|
342
375
|
},
|
|
343
376
|
params: {
|
|
344
377
|
ateLoopSize: CURVE.params.ateLoopSize,
|
|
378
|
+
twistType: CURVE.params.twistType,
|
|
379
|
+
// deprecated
|
|
345
380
|
r: CURVE.params.r,
|
|
346
381
|
G1b: CURVE.G1.b,
|
|
347
382
|
G2b: CURVE.G2.b,
|
|
348
383
|
},
|
|
349
384
|
utils,
|
|
385
|
+
// deprecated
|
|
386
|
+
getPublicKey,
|
|
387
|
+
getPublicKeyForShortSignatures,
|
|
388
|
+
sign,
|
|
389
|
+
signShortSignature,
|
|
390
|
+
verify,
|
|
391
|
+
verifyShortSignature,
|
|
392
|
+
aggregatePublicKeys,
|
|
393
|
+
aggregateSignatures,
|
|
394
|
+
aggregateShortSignatures,
|
|
395
|
+
G1,
|
|
396
|
+
G2,
|
|
397
|
+
Signature,
|
|
398
|
+
ShortSignature,
|
|
350
399
|
};
|
|
351
400
|
}
|
|
352
401
|
//# sourceMappingURL=bls.js.map
|