2020-07-03 07:38:13 +00:00
|
|
|
package bls
|
|
|
|
|
2022-12-20 10:41:47 +00:00
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AggregatedSignature represents aggregated signature produced by AggregateBatch()
|
|
|
|
const AggregatedSignature = "bls aggregated signature"
|
2022-05-04 04:47:53 +00:00
|
|
|
|
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 {
|
2022-12-20 10:41:47 +00:00
|
|
|
Signatures [][]byte
|
|
|
|
PublicKeys []PublicKey
|
|
|
|
Messages [][32]byte
|
|
|
|
Descriptions []string
|
2020-07-03 07:38:13 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 16:57:06 +00:00
|
|
|
// NewSet constructs an empty signature batch object.
|
|
|
|
func NewSet() *SignatureBatch {
|
|
|
|
return &SignatureBatch{
|
2022-12-20 10:41:47 +00:00
|
|
|
Signatures: [][]byte{},
|
|
|
|
PublicKeys: []PublicKey{},
|
|
|
|
Messages: [][32]byte{},
|
|
|
|
Descriptions: []string{},
|
2020-07-06 03:09:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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...)
|
2022-12-20 10:41:47 +00:00
|
|
|
s.Descriptions = append(s.Descriptions, set.Descriptions...)
|
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
|
|
|
|
2022-12-20 10:41:47 +00:00
|
|
|
// VerifyVerbosely verifies signatures as a whole at first, if fails, fallback
|
|
|
|
// to verify each single signature to identify invalid ones.
|
|
|
|
func (s *SignatureBatch) VerifyVerbosely() (bool, error) {
|
|
|
|
valid, err := s.Verify()
|
|
|
|
if err != nil || valid {
|
|
|
|
return valid, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// if signature batch is invalid, we then verify signatures one by one.
|
|
|
|
|
|
|
|
errmsg := "some signatures are invalid. details:"
|
|
|
|
for i := 0; i < len(s.Signatures); i++ {
|
|
|
|
sig := s.Signatures[i]
|
|
|
|
msg := s.Messages[i]
|
|
|
|
pubKey := s.PublicKeys[i]
|
|
|
|
|
|
|
|
valid, err := VerifySignature(sig, msg, pubKey)
|
|
|
|
if !valid {
|
|
|
|
desc := s.Descriptions[i]
|
|
|
|
if err != nil {
|
|
|
|
errmsg += fmt.Sprintf("\nsignature '%s' is invalid."+
|
|
|
|
" signature: 0x%s, public key: 0x%s, message: 0x%v, error: %v",
|
|
|
|
desc, hex.EncodeToString(sig), hex.EncodeToString(pubKey.Marshal()),
|
|
|
|
hex.EncodeToString(msg[:]), err)
|
|
|
|
} else {
|
|
|
|
errmsg += fmt.Sprintf("\nsignature '%s' is invalid."+
|
|
|
|
" signature: 0x%s, public key: 0x%s, message: 0x%v",
|
|
|
|
desc, hex.EncodeToString(sig), hex.EncodeToString(pubKey.Marshal()),
|
|
|
|
hex.EncodeToString(msg[:]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, errors.Errorf(errmsg)
|
|
|
|
}
|
|
|
|
|
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))
|
2022-12-20 10:41:47 +00:00
|
|
|
descriptions := make([]string, len(s.Descriptions))
|
2021-09-15 05:49:50 +00:00
|
|
|
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][:])
|
|
|
|
}
|
2022-12-20 10:41:47 +00:00
|
|
|
copy(descriptions, s.Descriptions)
|
2021-11-23 16:57:06 +00:00
|
|
|
return &SignatureBatch{
|
2022-12-20 10:41:47 +00:00
|
|
|
Signatures: signatures,
|
|
|
|
PublicKeys: pubkeys,
|
|
|
|
Messages: messages,
|
|
|
|
Descriptions: descriptions,
|
2021-09-15 05:49:50 +00:00
|
|
|
}
|
|
|
|
}
|
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]
|
2022-12-20 10:41:47 +00:00
|
|
|
descs := s.Descriptions[:0]
|
2022-05-04 04:47:53 +00:00
|
|
|
|
|
|
|
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])
|
2022-12-20 10:41:47 +00:00
|
|
|
descs = append(descs, s.Descriptions[i])
|
2022-05-04 04:47:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.Signatures = sigs
|
|
|
|
s.PublicKeys = pubs
|
|
|
|
s.Messages = msgs
|
2022-12-20 10:41:47 +00:00
|
|
|
s.Descriptions = descs
|
2022-05-04 04:47:53 +00:00
|
|
|
|
|
|
|
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) {
|
2022-12-20 10:41:47 +00:00
|
|
|
if len(s.Signatures) != len(s.PublicKeys) || len(s.Signatures) != len(s.Messages) || len(s.Signatures) != len(s.Descriptions) {
|
|
|
|
return s, errors.Errorf("mismatch number of signatures, publickeys, messages and descriptions in signature batch. "+
|
|
|
|
"Signatures %d, Public Keys %d , Messages %d, Descriptions %d", len(s.Signatures), len(s.PublicKeys), len(s.Messages), len(s.Descriptions))
|
2022-05-04 04:47:53 +00:00
|
|
|
}
|
2022-12-20 10:41:47 +00:00
|
|
|
if len(s.Signatures) == 0 {
|
|
|
|
return s, nil
|
2022-05-04 04:47:53 +00:00
|
|
|
}
|
|
|
|
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])
|
2022-12-20 10:41:47 +00:00
|
|
|
currBatch.Descriptions = append(currBatch.Descriptions, s.Descriptions[i])
|
2022-05-04 04:47:53 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
currBatch = &SignatureBatch{
|
2022-12-20 10:41:47 +00:00
|
|
|
Signatures: [][]byte{s.Signatures[i]},
|
|
|
|
Messages: [][32]byte{s.Messages[i]},
|
|
|
|
PublicKeys: []PublicKey{s.PublicKeys[i]},
|
|
|
|
Descriptions: []string{s.Descriptions[i]},
|
2022-05-04 04:47:53 +00:00
|
|
|
}
|
|
|
|
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}
|
2022-12-20 10:41:47 +00:00
|
|
|
b.Descriptions = []string{AggregatedSignature}
|
2022-05-04 04:47:53 +00:00
|
|
|
}
|
|
|
|
newObj := *b
|
|
|
|
newSt = newSt.Join(&newObj)
|
|
|
|
}
|
|
|
|
return newSt, nil
|
|
|
|
}
|