-
Notifications
You must be signed in to change notification settings - Fork 199
/
multiSigMock.go
190 lines (150 loc) · 5.57 KB
/
multiSigMock.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package mock
import (
"github.com/ElrondNetwork/elrond-go/crypto"
)
// BelNevMock is used to mock belare neven multisignature scheme
type BelNevMock struct {
aggSig []byte
commSecret []byte
commHash []byte
commitments [][]byte
sigs [][]byte
pubkeys []string
selfId uint16
VerifyMock func(msg []byte, bitmap []byte) error
CommitmentHashMock func(index uint16) ([]byte, error)
CreateCommitmentMock func() ([]byte, []byte)
AggregateCommitmentsMock func(bitmap []byte) error
CreateSignatureShareMock func(msg []byte, bitmap []byte) ([]byte, error)
VerifySignatureShareMock func(index uint16, sig []byte, msg []byte, bitmap []byte) error
AggregateSigsMock func(bitmap []byte) ([]byte, error)
SignatureShareMock func(index uint16) ([]byte, error)
StoreCommitmentMock func(index uint16, value []byte) error
StoreCommitmentHashMock func(uint16, []byte) error
CommitmentMock func(uint16) ([]byte, error)
CreateCalled func(pubKeys []string, index uint16) (crypto.MultiSigner, error)
ResetCalled func(pubKeys []string, index uint16) error
}
// NewMultiSigner -
func NewMultiSigner() *BelNevMock {
multisigner := &BelNevMock{}
multisigner.commitments = make([][]byte, 21)
multisigner.sigs = make([][]byte, 21)
multisigner.pubkeys = make([]string, 21)
return multisigner
}
// Create creates a multiSigner using receiver as template and initializes corresponding fields with the given params
func (bnm *BelNevMock) Create(pubKeys []string, index uint16) (crypto.MultiSigner, error) {
if bnm.CreateCalled != nil {
return bnm.CreateCalled(pubKeys, index)
}
multiSig := NewMultiSigner()
multiSig.selfId = index
multiSig.pubkeys = pubKeys
return multiSig, nil
}
// Reset -
func (bnm *BelNevMock) Reset(pubKeys []string, index uint16) error {
if bnm.ResetCalled != nil {
return bnm.ResetCalled(pubKeys, index)
}
bnm.commitments = make([][]byte, 21)
bnm.sigs = make([][]byte, 21)
bnm.pubkeys = make([]string, 21)
bnm.selfId = index
bnm.pubkeys = pubKeys
return nil
}
// SetAggregatedSig sets the aggregated signature according to the given byte array
func (bnm *BelNevMock) SetAggregatedSig(aggSig []byte) error {
bnm.aggSig = aggSig
return nil
}
// Verify returns nil if the aggregateed signature is verified for the given public keys
func (bnm *BelNevMock) Verify(msg []byte, bitmap []byte) error {
return bnm.VerifyMock(msg, bitmap)
}
// CreateCommitment creates a secret commitment and the corresponding public commitment point
func (bnm *BelNevMock) CreateCommitment() (commSecret []byte, commitment []byte) {
commSecret, comm := bnm.CreateCommitmentMock()
bnm.commSecret = commSecret
bnm.commitments[bnm.selfId] = comm
return commSecret, comm
}
// StoreCommitmentHash adds a commitment hash to the list on the specified position
func (bnm *BelNevMock) StoreCommitmentHash(index uint16, commHash []byte) error {
if bnm.StoreCommitmentHashMock == nil {
bnm.commHash = commHash
return nil
}
return bnm.StoreCommitmentHashMock(index, commHash)
}
// CommitmentHash returns the commitment hash from the list on the specified position
func (bnm *BelNevMock) CommitmentHash(index uint16) ([]byte, error) {
if bnm.CommitmentHashMock == nil {
return bnm.commHash, nil
}
return bnm.CommitmentHashMock(index)
}
// StoreCommitment adds a commitment to the list on the specified position
func (bnm *BelNevMock) StoreCommitment(index uint16, value []byte) error {
if bnm.StoreCommitmentMock == nil {
if index >= uint16(len(bnm.commitments)) {
return crypto.ErrIndexOutOfBounds
}
bnm.commitments[index] = value
return nil
}
return bnm.StoreCommitmentMock(index, value)
}
// Commitment returns the commitment from the list with the specified position
func (bnm *BelNevMock) Commitment(index uint16) ([]byte, error) {
if bnm.CommitmentMock == nil {
if index >= uint16(len(bnm.commitments)) {
return nil, crypto.ErrIndexOutOfBounds
}
return bnm.commitments[index], nil
}
return bnm.CommitmentMock(index)
}
// AggregateCommitments aggregates the list of commitments
func (bnm *BelNevMock) AggregateCommitments(bitmap []byte) error {
return bnm.AggregateCommitmentsMock(bitmap)
}
// CreateSignatureShare creates a partial signature
func (bnm *BelNevMock) CreateSignatureShare(msg []byte, bitmap []byte) ([]byte, error) {
if bnm.CreateSignatureShareMock == nil {
return bnm.CreateSignatureShareMock(msg, bitmap)
}
return bnm.CreateSignatureShareMock(msg, bitmap)
}
// StoreSignatureShare adds the partial signature of the signer with specified position
func (bnm *BelNevMock) StoreSignatureShare(index uint16, sig []byte) error {
if index >= uint16(len(bnm.pubkeys)) {
return crypto.ErrIndexOutOfBounds
}
bnm.sigs[index] = sig
return nil
}
// VerifySignatureShare verifies the partial signature of the signer with specified position
func (bnm *BelNevMock) VerifySignatureShare(index uint16, sig []byte, msg []byte, bitmap []byte) error {
return bnm.VerifySignatureShareMock(index, sig, msg, bitmap)
}
// AggregateSigs aggregates all collected partial signatures
func (bnm *BelNevMock) AggregateSigs(bitmap []byte) ([]byte, error) {
return bnm.AggregateSigsMock(bitmap)
}
// SignatureShare -
func (bnm *BelNevMock) SignatureShare(index uint16) ([]byte, error) {
if bnm.SignatureShareMock == nil {
if index >= uint16(len(bnm.sigs)) {
return nil, crypto.ErrIndexOutOfBounds
}
return bnm.sigs[index], nil
}
return bnm.SignatureShareMock(index)
}
// IsInterfaceNil returns true if there is no value under the interface
func (bnm *BelNevMock) IsInterfaceNil() bool {
return bnm == nil
}