-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.go
307 lines (252 loc) · 7.53 KB
/
key.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Copyright (c) 2023 Yawning Angel
//
// SPDX-License-Identifier: SSPL-1.0
package musig2
import (
"bytes"
"errors"
"math"
"sort"
"gitlab.com/yawning/secp256k1-voi"
"gitlab.com/yawning/secp256k1-voi/secec"
"gitlab.com/yawning/secp256k1-voi/secec/bitcoin"
)
const (
// TweakSize is the size of an aggregated public key tweak in bytes.
TweakSize = 32 // secp256k1.ScalarSize
maxPublicKeys = math.MaxUint32
tagKeyAggList = "KeyAgg list"
tagKeyAggCoefficient = "KeyAgg coefficient"
)
var (
scOne = secp256k1.NewScalar().One()
scNegOne = secp256k1.NewScalar().Negate(scOne)
errInvalidNumberOfKeys = errors.New("musig2: invalid number of public keys")
errInvalidTweak = errors.New("musig2: invalid tweak")
errPublicKeyNotInAgg = errors.New("musig2: public key not part of aggregate key")
errQIsInfinity = errors.New("musig2: Q is the point at infinity")
)
func keySort(pks []*secec.PublicKey) []*secec.PublicKey {
// INVARIANT: 0 < u < maxPublicKeys
ret := make([]*secec.PublicKey, 0, len(pks))
ret = append(ret, pks...)
// Return pk_1..u sorted in lexicographical order.
sort.SliceStable(
ret,
func(i, j int) bool {
return bytes.Compare(ret[i].CompressedBytes(), ret[j].CompressedBytes()) < 0
},
)
return ret
}
// AggregatedPublicKey is the aggregated and tweaked public key.
type AggregatedPublicKey struct {
q *secp256k1.Point // Invariant: Not infinity
tacc *secp256k1.Scalar
gacc *secp256k1.Scalar
pks []*secec.PublicKey
pk2 []byte // GetSecondKey(pk_1..u)
l []byte // HashKeys(pk_1..u)
}
// ApplyTweak applies a tweak to the aggregated public key.
func (aggPk *AggregatedPublicKey) ApplyTweak(tweak []byte, isXOnlyTweak bool) error {
if len(tweak) != TweakSize {
return errInvalidTweak
}
// Let (Q, gacc, tacc) = keyagg_ctx
// If is_xonly_t and not has_even_y(Q):
g := scOne // Else: Let g = 1
if isXOnlyTweak && aggPk.q.IsYOdd() != 0 {
// Let g = -1 mod n
g = scNegOne
}
// Let t = int(tweak); fail if t >= n
t, err := secp256k1.NewScalarFromCanonicalBytes((*[secp256k1.ScalarSize]byte)(tweak))
if err != nil {
return errors.Join(errInvalidTweak, err)
}
// Let Q' = g * Q + t * G
qP := secp256k1.NewIdentityPoint().DoubleScalarMultBasepointVartime(t, g, aggPk.q)
// Fail if is_infinite(Q')
if qP.IsIdentity() != 0 {
return errQIsInfinity
}
// Let gacc' = g * gacc mod n
aggPk.gacc.Multiply(g, aggPk.gacc)
// Let tacc' = t + g * tacc mod n
aggPk.tacc = secp256k1.NewScalar().Sum(t, secp256k1.NewScalar().Product(g, aggPk.tacc))
// Return keyagg_ctx' = (Q', gacc', tacc')
aggPk.q.Set(qP)
return nil
}
// Schnorr returns the BIP-340 Schnorr public key corresponding to the
// aggregated public key.
func (aggPk *AggregatedPublicKey) Schnorr() *bitcoin.SchnorrPublicKey {
pk, err := bitcoin.NewSchnorrPublicKey(aggPk.xBytes())
if err != nil {
panic(err)
}
return pk
}
func (aggPk *AggregatedPublicKey) xBytes() []byte {
b, err := aggPk.q.XBytes()
if err != nil {
panic(err)
}
return b
}
func (aggPk *AggregatedPublicKey) getSessionKeyAggCoeff(pk *secec.PublicKey) (*secp256k1.Scalar, error) {
// Fail if pk not in pk_1..u
var ok bool
for _, v := range aggPk.pks {
if ok = v.Equal(pk); ok {
break
}
}
if !ok {
return nil, errPublicKeyNotInAgg
}
// Return KeyAggCoeff(pk1..u, pk)
//
// Internal Algorithm KeyAggCoeff(pk_1..u, pk')
// Let pk2 = GetSecondKey(pk_1..u):
// Return KeyAggCoeffInternal(pk_1..u, pk', pk2)
if aggPk.pk2 == nil {
panic("musig2: BUG: pk2 is nil")
}
return keyAggCoeffInternal(aggPk.l, pk, aggPk.pk2), nil
}
// PublicKeyAggregator accumulates PublicKeys, before doing the final
// aggregation and generating an AggregatedPublicKey.
type PublicKeyAggregator struct {
pks []*secec.PublicKey
applyKeySort bool
}
// SetKeySort sets if the individual public keys should be sorted during
// the final aggregation step.
func (agg *PublicKeyAggregator) SetKeySort(t bool) *PublicKeyAggregator {
agg.applyKeySort = t
return agg
}
// Add adds a PublicKey to the key aggregator.
//
// WARNING: Unless the PublicKeyAggregator is configured to sort the public
// keys, the order in which PublicKeys are added matters.
func (agg *PublicKeyAggregator) Add(pk *secec.PublicKey) error {
if uint64(len(agg.pks))+1 > maxPublicKeys {
return errInvalidNumberOfKeys
}
if pk == nil {
panic("musig2: invalid public key")
}
agg.pks = append(agg.pks, pk)
return nil
}
// Aggregate produces an AggregatedPublicKey. This operation does not affect
// the state of the PublicKeyAggregator.
func (agg *PublicKeyAggregator) Aggregate() (*AggregatedPublicKey, error) {
pks := agg.pks
if u := len(pks); uint64(u) > maxPublicKeys || u == 0 {
return nil, errInvalidNumberOfKeys
}
if agg.applyKeySort {
pks = keySort(pks)
}
//
// KeyAgg - Key Aggregation
//
// Let L = HashKeys(pk1..u) (From KeyAggCoeffInternal)
L := hashKeys(pks)
// Let pk2 = GetSecondKey(pk_1..u)
pk2 := getSecondKey(pks)
// For i = 1 .. u:
Q := secp256k1.NewIdentityPoint()
for i := range pks {
pk := pks[i]
// Let P_i = cpoint(pk_i); fail if that fails and blame signer
// i for invalid individual public key.
//
// Note/yawning: Since we use sensible data-types rather
// than byte vectors for public keys, `pks` is guaranteed
// to be a vector of points on the curve.
P := pk.Point()
// Let a_i = KeyAggCoeffInternal(pk_1..u, pk_i, pk2).
a := keyAggCoeffInternal(L, pk, pk2)
Q.Add(Q, secp256k1.NewIdentityPoint().ScalarMult(a, P))
}
// Let Q = a1 * P1 + a2 * P2 + ... + au * Pu
//
// XXX/perf: Add a vartime multiscalar multiply which will outperform
// the current incremental method of calculating Q, but this operation
// should be comparatively infrequent, so who cares.
// Fail if is_infinite(Q).
if Q.IsIdentity() != 0 {
return nil, errQIsInfinity
}
// Let gacc = 1
// Let tacc = 0
// Return keyagg_ctx = (Q, gacc, tacc).
//
// Note/yawning: We opt to store extra values that are required
// as part of the signing/verification process as well so that
// further calls to getSecondKey (pk2), and HashKeys (L) can
// be omitted entirely.
return &AggregatedPublicKey{
q: Q,
gacc: secp256k1.NewScalarFrom(scOne),
tacc: secp256k1.NewScalar(),
pks: pks,
pk2: pk2,
l: L,
}, nil
}
// NewPublicKeyAggregator creates an empty PublicKeyAggregator.
func NewPublicKeyAggregator() *PublicKeyAggregator {
return new(PublicKeyAggregator)
}
// Internal Algorithm HashKeys(pk_1..u).
func hashKeys(pks []*secec.PublicKey) []byte {
// Return hashKeyAgg list(pk_1 || pk_2 || ... || pk_u)
h := newTaggedHash(tagKeyAggList)
for _, pk := range pks {
_, _ = h.Write(pk.CompressedBytes())
}
return h.Sum(nil)
}
// Internal Algorithm GetSecondKey(pk_1..u).
func getSecondKey(pks []*secec.PublicKey) []byte {
pk1 := pks[0] // The spec starts indexes from 1.
// For j = 1 .. u:
for i, pk := range pks {
if i == 0 {
continue
}
// If pk_j != pk_1:
if !pk.Equal(pk1) {
// Return pk_j
return pks[i].CompressedBytes()
}
}
// Return bytes(33, 0)
return cIdentityBytes
}
// Internal Algorithm KeyAggCoeffInternal(pk_1..u, pk', pk2).
func keyAggCoeffInternal(l []byte, pkP *secec.PublicKey, pk2 []byte) *secp256k1.Scalar {
// If pk' = pk2:
if bytes.Equal(pkP.CompressedBytes(), pk2) {
// Return 1
return scOne
}
// Let L = HashKeys(pk1..u)
if l == nil {
panic("musig2: BUG: L is nil")
}
// Return int(hashKeyAgg coefficient(L || pk')) mod n
b := taggedHash(
tagKeyAggCoefficient,
l, // L
pkP.CompressedBytes(), // pk'
)
sc, _ := secp256k1.NewScalarFromBytes((*[secp256k1.ScalarSize]byte)(b))
return sc
}