2020-07-03 07:38:13 +00:00
|
|
|
package bls
|
|
|
|
|
2022-05-04 04:47:53 +00:00
|
|
|
import "github.com/pkg/errors"
|
|
|
|
|
2021-11-23 16:57:06 +00:00
|
|
|
// SignatureBatch refers to the defined set of
|
2020-07-03 07:38:13 +00:00
|
|
|
// signatures and its respective public keys and
|
|
|
|
// messages required to verify it.
|
2021-11-23 16:57:06 +00:00
|
|
|
type SignatureBatch struct {
|
2020-09-16 13:28:28 +00:00
|
|
|
Signatures [][]byte
|
2020-07-03 07:38:13 +00:00
|
|
|
PublicKeys []PublicKey
|
|
|
|
Messages [][32]byte
|
|
|
|
}
|
|
|
|
|
2021-11-23 16:57:06 +00:00
|
|
|
// NewSet constructs an empty signature batch object.
|
|
|
|
func NewSet() *SignatureBatch {
|
|
|
|
return &SignatureBatch{
|
2020-09-16 13:28:28 +00:00
|
|
|
Signatures: [][]byte{},
|
2020-07-06 03:09:29 +00:00
|
|
|
PublicKeys: []PublicKey{},
|
|
|
|
Messages: [][32]byte{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-23 16:57:06 +00:00
|
|
|
// Join merges the provided signature batch to out current one.
|
|
|
|
func (s *SignatureBatch) Join(set *SignatureBatch) *SignatureBatch {
|
2020-07-03 07:38:13 +00:00
|
|
|
s.Signatures = append(s.Signatures, set.Signatures...)
|
|
|
|
s.PublicKeys = append(s.PublicKeys, set.PublicKeys...)
|
|
|
|
s.Messages = append(s.Messages, set.Messages...)
|
2020-07-06 03:09:29 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2021-11-23 16:57:06 +00:00
|
|
|
// Verify the current signature batch using the batch verify algorithm.
|
|
|
|
func (s *SignatureBatch) Verify() (bool, error) {
|
2020-07-06 03:09:29 +00:00
|
|
|
return VerifyMultipleSignatures(s.Signatures, s.Messages, s.PublicKeys)
|
2020-07-03 07:38:13 +00:00
|
|
|
}
|
2021-09-15 05:49:50 +00:00
|
|
|
|
2021-11-23 16:57:06 +00:00
|
|
|
// Copy the attached signature batch and return it
|
2021-09-15 05:49:50 +00:00
|
|
|
// to the caller.
|
2021-11-23 16:57:06 +00:00
|
|
|
func (s *SignatureBatch) Copy() *SignatureBatch {
|
2021-09-15 05:49:50 +00:00
|
|
|
signatures := make([][]byte, len(s.Signatures))
|
|
|
|
pubkeys := make([]PublicKey, len(s.PublicKeys))
|
|
|
|
messages := make([][32]byte, len(s.Messages))
|
|
|
|
for i := range s.Signatures {
|
|
|
|
sig := make([]byte, len(s.Signatures[i]))
|
|
|
|
copy(sig, s.Signatures[i])
|
|
|
|
signatures[i] = sig
|
|
|
|
}
|
|
|
|
for i := range s.PublicKeys {
|
|
|
|
pubkeys[i] = s.PublicKeys[i].Copy()
|
|
|
|
}
|
|
|
|
for i := range s.Messages {
|
|
|
|
copy(messages[i][:], s.Messages[i][:])
|
|
|
|
}
|
2021-11-23 16:57:06 +00:00
|
|
|
return &SignatureBatch{
|
2021-09-15 05:49:50 +00:00
|
|
|
Signatures: signatures,
|
|
|
|
PublicKeys: pubkeys,
|
|
|
|
Messages: messages,
|
|
|
|
}
|
|
|
|
}
|
2022-05-04 04:47:53 +00:00
|
|
|
|
|
|
|
// RemoveDuplicates removes duplicate signature sets from the signature batch.
|
|
|
|
func (s *SignatureBatch) RemoveDuplicates() (int, *SignatureBatch, error) {
|
|
|
|
if len(s.Signatures) == 0 || len(s.PublicKeys) == 0 || len(s.Messages) == 0 {
|
|
|
|
return 0, s, nil
|
|
|
|
}
|
|
|
|
if len(s.Signatures) != len(s.PublicKeys) || len(s.Signatures) != len(s.Messages) {
|
|
|
|
return 0, s, errors.Errorf("mismatch number of signatures, publickeys and messages in signature batch. "+
|
|
|
|
"Signatures %d, Public Keys %d , Messages %d", s.Signatures, s.PublicKeys, s.Messages)
|
|
|
|
}
|
|
|
|
sigMap := make(map[string]int)
|
|
|
|
duplicateSet := make(map[int]bool)
|
|
|
|
for i := 0; i < len(s.Signatures); i++ {
|
|
|
|
if sigIdx, ok := sigMap[string(s.Signatures[i])]; ok {
|
|
|
|
if s.PublicKeys[sigIdx].Equals(s.PublicKeys[i]) &&
|
|
|
|
s.Messages[sigIdx] == s.Messages[i] {
|
|
|
|
duplicateSet[i] = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sigMap[string(s.Signatures[i])] = i
|
|
|
|
}
|
|
|
|
|
|
|
|
sigs := s.Signatures[:0]
|
|
|
|
pubs := s.PublicKeys[:0]
|
|
|
|
msgs := s.Messages[:0]
|
|
|
|
|
|
|
|
for i := 0; i < len(s.Signatures); i++ {
|
|
|
|
if duplicateSet[i] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
sigs = append(sigs, s.Signatures[i])
|
|
|
|
pubs = append(pubs, s.PublicKeys[i])
|
|
|
|
msgs = append(msgs, s.Messages[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Signatures = sigs
|
|
|
|
s.PublicKeys = pubs
|
|
|
|
s.Messages = msgs
|
|
|
|
|
|
|
|
return len(duplicateSet), s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AggregateBatch aggregates common messages in the provided batch to
|
|
|
|
// reduce the number of pairings required when we finally verify the
|
|
|
|
// whole batch.
|
|
|
|
func (s *SignatureBatch) AggregateBatch() (*SignatureBatch, error) {
|
|
|
|
if len(s.Signatures) == 0 || len(s.PublicKeys) == 0 || len(s.Messages) == 0 {
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
if len(s.Signatures) != len(s.PublicKeys) || len(s.Signatures) != len(s.Messages) {
|
|
|
|
return s, errors.Errorf("mismatch number of signatures, publickeys and messages in signature batch. "+
|
|
|
|
"Signatures %d, Public Keys %d , Messages %d", s.Signatures, s.PublicKeys, s.Messages)
|
|
|
|
}
|
|
|
|
msgMap := make(map[[32]byte]*SignatureBatch)
|
|
|
|
|
|
|
|
for i := 0; i < len(s.Messages); i++ {
|
|
|
|
currMsg := s.Messages[i]
|
|
|
|
currBatch, ok := msgMap[currMsg]
|
|
|
|
if ok {
|
|
|
|
currBatch.Signatures = append(currBatch.Signatures, s.Signatures[i])
|
|
|
|
currBatch.Messages = append(currBatch.Messages, s.Messages[i])
|
|
|
|
currBatch.PublicKeys = append(currBatch.PublicKeys, s.PublicKeys[i])
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
currBatch = &SignatureBatch{
|
|
|
|
Signatures: [][]byte{s.Signatures[i]},
|
|
|
|
Messages: [][32]byte{s.Messages[i]},
|
|
|
|
PublicKeys: []PublicKey{s.PublicKeys[i]},
|
|
|
|
}
|
|
|
|
msgMap[currMsg] = currBatch
|
|
|
|
}
|
|
|
|
newSt := NewSet()
|
|
|
|
for rt, b := range msgMap {
|
|
|
|
if len(b.PublicKeys) > 1 {
|
|
|
|
aggPub := AggregateMultiplePubkeys(b.PublicKeys)
|
|
|
|
aggSig, err := AggregateCompressedSignatures(b.Signatures)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
copiedRt := rt
|
|
|
|
b.PublicKeys = []PublicKey{aggPub}
|
|
|
|
b.Signatures = [][]byte{aggSig.Marshal()}
|
|
|
|
b.Messages = [][32]byte{copiedRt}
|
|
|
|
}
|
|
|
|
newObj := *b
|
|
|
|
newSt = newSt.Join(&newObj)
|
|
|
|
}
|
|
|
|
return newSt, nil
|
|
|
|
}
|