@@ -5,7 +5,8 @@ const HASH_OR_ENCODE = true;
5
5
const DST = "BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_" ;
6
6
const RAND_BITS = 64 ;
7
7
8
- class ErrorBLST extends Error {
8
+ export { BLST_ERROR } ;
9
+ export class ErrorBLST extends Error {
9
10
constructor ( blstError : BLST_ERROR ) {
10
11
super ( BLST_ERROR [ blstError ] ) ;
11
12
}
@@ -45,6 +46,11 @@ export class SecretKey {
45
46
return new SecretKey ( sk ) ;
46
47
}
47
48
49
+ toAggregatePublicKey ( ) : AggregatePublicKey {
50
+ const pk = new PkConstructor ( this . value ) ;
51
+ return new AggregatePublicKey ( pk ) ;
52
+ }
53
+
48
54
toPublicKey ( ) : PublicKey {
49
55
const pk = new PkConstructor ( this . value ) ;
50
56
return new PublicKey ( pk . to_affine ( ) ) ;
@@ -98,14 +104,6 @@ export class Signature extends SerializeAffine<SigAffine> {
98
104
}
99
105
}
100
106
101
- function aggregate < P extends Pn_Affine < any , any > > ( points : { value : P } [ ] ) {
102
- const agg = points [ 0 ] . value . to_jacobian ( ) ;
103
- for ( const pk of points . slice ( 1 ) ) {
104
- agg . aggregate ( pk . value ) ;
105
- }
106
- return agg ;
107
- }
108
-
109
107
export class AggregatePublicKey {
110
108
value : Pk ;
111
109
@@ -117,7 +115,9 @@ export class AggregatePublicKey {
117
115
return new AggregatePublicKey ( pk . value . to_jacobian ( ) ) ;
118
116
}
119
117
static fromPublicKeys ( pks : PublicKey [ ] ) : AggregatePublicKey {
120
- return new AggregatePublicKey ( aggregate ( pks ) ) ;
118
+ return aggregatePubkeys (
119
+ pks . map ( ( pk ) => AggregatePublicKey . fromPublicKey ( pk ) )
120
+ ) ;
121
121
}
122
122
static fromPublicKeysBytes ( pks : Uint8Array [ ] ) : AggregatePublicKey {
123
123
return AggregatePublicKey . fromPublicKeys ( pks . map ( PublicKey . fromBytes ) ) ;
@@ -145,7 +145,9 @@ export class AggregateSignature {
145
145
return new AggregateSignature ( sig . value . to_jacobian ( ) ) ;
146
146
}
147
147
static fromSignatures ( sigs : Signature [ ] ) : AggregateSignature {
148
- return new AggregateSignature ( aggregate ( sigs ) ) ;
148
+ return aggregateSignatures (
149
+ sigs . map ( ( sig ) => AggregateSignature . fromSignature ( sig ) )
150
+ ) ;
149
151
}
150
152
static fromSignaturesBytes ( sigs : Uint8Array [ ] ) : AggregateSignature {
151
153
return AggregateSignature . fromSignatures ( sigs . map ( Signature . fromBytes ) ) ;
@@ -162,6 +164,34 @@ export class AggregateSignature {
162
164
}
163
165
}
164
166
167
+ export function aggregatePubkeys (
168
+ pks : AggregatePublicKey [ ]
169
+ ) : AggregatePublicKey {
170
+ if ( pks . length === 0 ) {
171
+ throw new ErrorBLST ( BLST_ERROR . EMPTY_AGGREGATE_ARRAY ) ;
172
+ }
173
+
174
+ const agg = pks
175
+ . map ( ( pk ) => pk . value )
176
+ . reduce ( ( _agg , pk ) => blst . P1 . add ( _agg , pk ) ) ;
177
+
178
+ return new AggregatePublicKey ( agg ) ;
179
+ }
180
+
181
+ export function aggregateSignatures (
182
+ sigs : AggregateSignature [ ]
183
+ ) : AggregateSignature {
184
+ if ( sigs . length === 0 ) {
185
+ throw new ErrorBLST ( BLST_ERROR . EMPTY_AGGREGATE_ARRAY ) ;
186
+ }
187
+
188
+ const agg = sigs
189
+ . map ( ( sig ) => sig . value )
190
+ . reduce ( ( _agg , sig ) => blst . P2 . add ( _agg , sig ) ) ;
191
+
192
+ return new AggregateSignature ( agg ) ;
193
+ }
194
+
165
195
export function verify (
166
196
msg : Uint8Array ,
167
197
pk : PublicKey ,
@@ -172,10 +202,10 @@ export function verify(
172
202
173
203
export function fastAggregateVerify (
174
204
msg : Uint8Array ,
175
- pks : PublicKey [ ] ,
205
+ pks : AggregatePublicKey [ ] ,
176
206
sig : Signature
177
207
) : boolean {
178
- const aggPk = AggregatePublicKey . fromPublicKeys ( pks ) ;
208
+ const aggPk = aggregatePubkeys ( pks ) ;
179
209
const pk = aggPk . toPublicKey ( ) ;
180
210
return aggregateVerify ( [ msg ] , [ pk ] , sig ) ;
181
211
}
0 commit comments