-
Notifications
You must be signed in to change notification settings - Fork 0
/
musig2.go
57 lines (45 loc) · 1.23 KB
/
musig2.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) 2023 Yawning Angel
//
// SPDX-License-Identifier: SSPL-1.0
// Package musig2 implements the MuSig2 multi-signature algorithm as
// specified in BIP-0327.
package musig2
import (
"crypto/sha256"
"encoding"
"hash"
"gitlab.com/yawning/secp256k1-voi"
)
// Dumping ground for various helpers.
var cIdentityBytes = make([]byte, secp256k1.CompressedPointSize)
func newTaggedHash(tag string) hash.Hash {
hashedTag := sha256.Sum256([]byte(tag))
h := sha256.New()
_, _ = h.Write(hashedTag[:])
_, _ = h.Write(hashedTag[:])
return h
}
func taggedHash(tag string, vals ...[]byte) []byte {
h := newTaggedHash(tag)
for _, v := range vals {
_, _ = h.Write(v)
}
return h.Sum(nil)
}
//nolint:forcetypeassert
func cloneHash(h hash.Hash) hash.Hash {
// This is so stupid, but it appears to be the only way to copy
// hash state, and it's and improvement to do this, if only for
// readability reasons.
m := h.(encoding.BinaryMarshaler)
st, err := m.MarshalBinary()
if err != nil {
panic("musig2: failed to serialize hash: " + err.Error())
}
nh := sha256.New()
um := nh.(encoding.BinaryUnmarshaler)
if err = um.UnmarshalBinary(st); err != nil {
panic("musig2: failed to deserialize hash: " + err.Error())
}
return nh
}