forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
signature.go
57 lines (47 loc) · 1.5 KB
/
signature.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package bls
import (
"errors"
blst "github.com/supranational/blst/bindings/go"
)
const SignatureLen = blst.BLST_P2_COMPRESS_BYTES
var (
ErrFailedSignatureDecompress = errors.New("couldn't decompress signature")
errInvalidSignature = errors.New("invalid signature")
errNoSignatures = errors.New("no signatures")
errFailedSignatureAggregation = errors.New("couldn't aggregate signatures")
)
type (
Signature = blst.P2Affine
AggregateSignature = blst.P2Aggregate
)
// SignatureToBytes returns the compressed big-endian format of the signature.
func SignatureToBytes(sig *Signature) []byte {
return sig.Compress()
}
// SignatureFromBytes parses the compressed big-endian format of the signature
// into a signature.
func SignatureFromBytes(sigBytes []byte) (*Signature, error) {
sig := new(Signature).Uncompress(sigBytes)
if sig == nil {
return nil, ErrFailedSignatureDecompress
}
if !sig.SigValidate(false) {
return nil, errInvalidSignature
}
return sig, nil
}
// AggregateSignatures aggregates a non-zero number of signatures into a single
// aggregated signature.
// Invariant: all [sigs] have been validated.
func AggregateSignatures(sigs []*Signature) (*Signature, error) {
if len(sigs) == 0 {
return nil, errNoSignatures
}
var agg AggregateSignature
if !agg.Aggregate(sigs, false) {
return nil, errFailedSignatureAggregation
}
return agg.ToAffine(), nil
}