forked from gcash/bchd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
musig_test.go
91 lines (79 loc) · 1.73 KB
/
musig_test.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
package bchec
import (
"crypto/sha256"
"math/big"
"math/rand"
"testing"
)
func TestMuSession(t *testing.T) {
m := sha256.Sum256([]byte("hello world"))
for i := 0; i < 32; i++ {
r := rand.Intn(9) + 1
sessions := make([]*Session, r)
privkeys := make([]*PrivateKey, r)
pubkeys := make([]*PublicKey, r)
commitments := make([][]byte, r)
nonces := make([]*PublicKey, r)
svals := make([]*big.Int, r)
for x := 0; x < r; x++ {
priv, err := NewPrivateKey(S256())
if err != nil {
t.Fatal(err)
}
privkeys[x] = priv
pubkeys[x] = priv.PubKey()
}
aggPubkey, err := AggregatePublicKeys(pubkeys...)
if err != nil {
t.Fatal(err)
}
for x := 0; x < r; x++ {
var b [32]byte
rand.Read(b[:])
sess, err := NewMuSession(pubkeys, privkeys[x], b)
if err != nil {
t.Fatal(err)
}
sessions[x] = sess
commitments[x] = sess.NonceCommitment(m[:])
}
for x := 0; x < r; x++ {
sessions[x].SetNonceCommitments(commitments...)
nonces[x], err = sessions[x].Nonce()
if err != nil {
t.Fatal(err)
}
}
for x := 0; x < r; x++ {
sessions[x].SetNonces(nonces...)
svals[x], err = sessions[x].Sign(m[:])
if err != nil {
t.Fatal(err)
}
}
for x := 0; x < r; x++ {
sig := sessions[x].AggregateSignature(svals...)
valid := sig.Verify(m[:], aggPubkey)
if !valid {
t.Fatal("invalid signature")
}
}
}
}
func BenchmarkAggregatePublicKeys(b *testing.B) {
priv1, err := NewPrivateKey(S256())
if err != nil {
b.Fatal(err)
}
priv2, err := NewPrivateKey(S256())
if err != nil {
b.Fatal(err)
}
priv3, err := NewPrivateKey(S256())
if err != nil {
b.Fatal(err)
}
for i := 0; i < b.N; i++ {
AggregatePublicKeys(priv1.PubKey(), priv2.PubKey(), priv3.PubKey())
}
}