-
Notifications
You must be signed in to change notification settings - Fork 646
/
signature.go
137 lines (120 loc) · 3.39 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright (C) 2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package teleporter
import (
"context"
"errors"
"fmt"
"math/big"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
"github.com/ava-labs/avalanchego/utils/set"
)
var (
_ Signature = (*BitSetSignature)(nil)
ErrInvalidBitSet = errors.New("bitset is invalid")
ErrInsufficientWeight = errors.New("signature weight is insufficient")
ErrInvalidSignature = errors.New("signature is invalid")
ErrParseSignature = errors.New("failed to parse signature")
)
type Signature interface {
// Verify that this signature was signed by at least [quorumNum]/[quorumDen]
// of the validators of [msg.SourceChainID] at [pChainHeight].
//
// Invariant: [msg] is correctly initialized.
Verify(
ctx context.Context,
msg *UnsignedMessage,
pChainState validators.State,
pChainHeight uint64,
quorumNum uint64,
quorumDen uint64,
) error
}
type BitSetSignature struct {
// Signers is a big-endian byte slice encoding which validators signed this
// message.
Signers []byte `serialize:"true"`
Signature [bls.SignatureLen]byte `serialize:"true"`
}
func (s *BitSetSignature) Verify(
ctx context.Context,
msg *UnsignedMessage,
pChainState validators.State,
pChainHeight uint64,
quorumNum uint64,
quorumDen uint64,
) error {
subnetID, err := pChainState.GetSubnetID(ctx, msg.SourceChainID)
if err != nil {
return err
}
vdrs, totalWeight, err := GetCanonicalValidatorSet(ctx, pChainState, pChainHeight, subnetID)
if err != nil {
return err
}
// Parse signer bit vector
signerIndices := set.BitsFromBytes(s.Signers)
if len(signerIndices.Bytes()) != len(s.Signers) {
return ErrInvalidBitSet
}
// Get the validators that (allegedly) signed the message.
signers, err := FilterValidators(signerIndices, vdrs)
if err != nil {
return err
}
// Because [signers] is a subset of [vdrs], this can never error.
sigWeight, _ := SumWeight(signers)
// Make sure the signature's weight is sufficient.
err = VerifyWeight(
sigWeight,
totalWeight,
quorumNum,
quorumDen,
)
if err != nil {
return err
}
// Parse the aggregate signature
aggSig, err := bls.SignatureFromBytes(s.Signature[:])
if err != nil {
return fmt.Errorf("%w: %s", ErrParseSignature, err)
}
// Create the aggregate public key
aggPubKey, err := AggregatePublicKeys(signers)
if err != nil {
return err
}
// Verify the signature
unsignedBytes := msg.Bytes()
if !bls.Verify(aggPubKey, aggSig, unsignedBytes) {
return ErrInvalidSignature
}
return nil
}
// VerifyWeight returns [nil] if [sigWeight] is at least [quorumNum]/[quorumDen]
// of [totalWeight].
// If [sigWeight >= totalWeight * quorumNum / quorumDen] then return [nil]
func VerifyWeight(
sigWeight uint64,
totalWeight uint64,
quorumNum uint64,
quorumDen uint64,
) error {
// Verifies that quorumNum * totalWeight <= quorumDen * sigWeight
scaledTotalWeight := new(big.Int).SetUint64(totalWeight)
scaledTotalWeight.Mul(scaledTotalWeight, new(big.Int).SetUint64(quorumNum))
scaledSigWeight := new(big.Int).SetUint64(sigWeight)
scaledSigWeight.Mul(scaledSigWeight, new(big.Int).SetUint64(quorumDen))
if scaledTotalWeight.Cmp(scaledSigWeight) == 1 {
return fmt.Errorf(
"%w: %d*%d > %d*%d",
ErrInsufficientWeight,
quorumNum,
totalWeight,
quorumDen,
sigWeight,
)
}
return nil
}