@@ -327,7 +327,12 @@ func (sh *signingHandler) AggregateSigs(bitmap []byte, epoch uint32) ([]byte, er
327327// AggregateSigsWithKeys aggregates the provided signature shares over the provided group,
328328// without touching the per-round state; bitmap indices are aligned with pubKeys and sigShares
329329func (sh * signingHandler ) AggregateSigsWithKeys (pubKeys []string , bitmap []byte , sigShares [][]byte , epoch uint32 ) ([]byte , error ) {
330- signatures , pubKeysSigners , err := sh .selectByBitmap (pubKeys , bitmap , sigShares )
330+ pubKeysSigners , err := sh .selectPubKeysByBitmap (pubKeys , bitmap )
331+ if err != nil {
332+ return nil , err
333+ }
334+
335+ signatures , err := selectSigSharesByBitmap (pubKeys , bitmap , sigShares )
331336 if err != nil {
332337 return nil , err
333338 }
@@ -343,7 +348,7 @@ func (sh *signingHandler) AggregateSigsWithKeys(pubKeys []string, bitmap []byte,
343348// VerifyAggregatedSigWithKeys verifies the aggregated signature over the provided group,
344349// without touching the per-round state
345350func (sh * signingHandler ) VerifyAggregatedSigWithKeys (pubKeys []string , bitmap []byte , message []byte , aggSig []byte , epoch uint32 ) error {
346- _ , pubKeysSigners , err := sh .selectByBitmap (pubKeys , bitmap , nil )
351+ pubKeysSigners , err := sh .selectPubKeysByBitmap (pubKeys , bitmap )
347352 if err != nil {
348353 return err
349354 }
@@ -376,41 +381,63 @@ func (sh *signingHandler) VerifySigShareWithKey(pubKey []byte, sigShare []byte,
376381 return multiSigner .VerifySignatureShareV2 (pk , message , sigShare )
377382}
378383
379- // selectByBitmap returns the signatures and public keys whose index is set in the bitmap;
380- // sigShares may be nil when only the public keys are needed
381- func (sh * signingHandler ) selectByBitmap (pubKeys []string , bitmap []byte , sigShares [][]byte ) ([][]byte , []crypto.PublicKey , error ) {
384+ func validateBitmap (pubKeys []string , bitmap []byte ) error {
382385 if bitmap == nil {
383- return nil , nil , ErrNilBitmap
386+ return ErrNilBitmap
384387 }
385388 if len (bitmap )* 8 < len (pubKeys ) {
386- return nil , nil , ErrBitmapMismatch
389+ return ErrBitmapMismatch
387390 }
388- if sigShares != nil && len (sigShares ) != len (pubKeys ) {
389- return nil , nil , ErrIndexOutOfBounds
391+
392+ return nil
393+ }
394+
395+ // selectPubKeysByBitmap returns the public keys whose index is set in the bitmap
396+ func (sh * signingHandler ) selectPubKeysByBitmap (pubKeys []string , bitmap []byte ) ([]crypto.PublicKey , error ) {
397+ err := validateBitmap (pubKeys , bitmap )
398+ if err != nil {
399+ return nil , err
390400 }
391401
392- signatures := make ([][]byte , 0 , len (pubKeys ))
393402 pubKeysSigners := make ([]crypto.PublicKey , 0 , len (pubKeys ))
394403 for i , pubKeyStr := range pubKeys {
395404 if bitmap [i / 8 ]& (1 << (uint16 (i )% 8 )) == 0 {
396405 continue
397406 }
398407
399- if sigShares != nil {
400- if len (sigShares [i ]) == 0 {
401- return nil , nil , ErrNilElement
402- }
403- signatures = append (signatures , sigShares [i ])
404- }
405-
406408 pubKey , err := sh .getPubKeyFromBytes ([]byte (pubKeyStr ))
407409 if err != nil {
408- return nil , nil , err
410+ return nil , err
409411 }
410412 pubKeysSigners = append (pubKeysSigners , pubKey )
411413 }
412414
413- return signatures , pubKeysSigners , nil
415+ return pubKeysSigners , nil
416+ }
417+
418+ // selectSigSharesByBitmap returns the signature shares whose index is set in the bitmap
419+ func selectSigSharesByBitmap (pubKeys []string , bitmap []byte , sigShares [][]byte ) ([][]byte , error ) {
420+ err := validateBitmap (pubKeys , bitmap )
421+ if err != nil {
422+ return nil , err
423+ }
424+ if len (sigShares ) != len (pubKeys ) {
425+ return nil , ErrIndexOutOfBounds
426+ }
427+
428+ signatures := make ([][]byte , 0 , len (pubKeys ))
429+ for i := range sigShares {
430+ if bitmap [i / 8 ]& (1 << (uint16 (i )% 8 )) == 0 {
431+ continue
432+ }
433+
434+ if len (sigShares [i ]) == 0 {
435+ return nil , ErrNilElement
436+ }
437+ signatures = append (signatures , sigShares [i ])
438+ }
439+
440+ return signatures , nil
414441}
415442
416443func (sh * signingHandler ) getPubKeyFromBytes (
0 commit comments