-
Notifications
You must be signed in to change notification settings - Fork 659
/
signature.go
166 lines (146 loc) · 4.37 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package warp
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 {
// NumSigners is the number of [bls.PublicKeys] that participated in the
// [Signature]. This is exposed because users of these signatures typically
// impose a verification fee that is a function of the number of
// signers.
NumSigners() (int, error)
// 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,
networkID uint32,
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) NumSigners() (int, error) {
// Parse signer bit vector
//
// We assert that the length of [signerIndices.Bytes()] is equal
// to [len(s.Signers)] to ensure that [s.Signers] does not have
// any unnecessary zero-padding to represent the [set.Bits].
signerIndices := set.BitsFromBytes(s.Signers)
if len(signerIndices.Bytes()) != len(s.Signers) {
return 0, ErrInvalidBitSet
}
return signerIndices.Len(), nil
}
func (s *BitSetSignature) Verify(
ctx context.Context,
msg *UnsignedMessage,
networkID uint32,
pChainState validators.State,
pChainHeight uint64,
quorumNum uint64,
quorumDen uint64,
) error {
if msg.NetworkID != networkID {
return ErrWrongNetworkID
}
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
//
// We assert that the length of [signerIndices.Bytes()] is equal
// to [len(s.Signers)] to ensure that [s.Signers] does not have
// any unnecessary zero-padding to represent the [set.Bits].
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: %w", 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
}