-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbipschnorrmulti_test.go
130 lines (122 loc) · 2.48 KB
/
bipschnorrmulti_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
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
package bipschnorr_test
import (
"crypto/rand"
"math/big"
"time"
"github.com/tnakagawa/bipschnorr"
"github.com/btcsuite/btcd/btcec"
"testing"
)
func TestMultisignature(t *testing.T) {
start := time.Now()
t.Logf("Introduction / %fs", (time.Now().Sub(start)).Seconds())
u := rndi(10) + 2
m := rndbs()
users := []*bipschnorr.Muser{}
for i := 1; i <= u; i++ {
d := rndbi()
user, err := bipschnorr.NewMultiUser(i, u, d, m)
if err != nil {
t.Logf("error : %+v", err)
t.Fail()
return
}
users = append(users, user)
}
t.Logf("Step1 / %fs", (time.Now().Sub(start)).Seconds())
for i := range users {
for j := range users {
if i == j {
continue
}
users[j].SetPublicKey(i+1, users[i].PublicKey())
}
}
P, err := users[0].P()
if err != nil {
t.Logf("error : %+v", err)
t.Fail()
return
}
t.Logf("u : %d", u)
t.Logf("P : %x", P.Bytes())
t.Logf("m : %x", m)
t.Logf("Step2 / %fs", (time.Now().Sub(start)).Seconds())
for i := range users {
for j := range users {
if i == j {
continue
}
users[j].SetHash(i+1, users[i].Hash())
}
}
t.Logf("Step3 / %fs", (time.Now().Sub(start)).Seconds())
for i := range users {
for j := range users {
if i == j {
continue
}
users[j].SetRandomPoint(i+1, users[i].RandomPoint())
}
}
t.Logf("Step4 / %fs", (time.Now().Sub(start)).Seconds())
for i := range users {
err := users[i].CheckHash()
if err != nil {
t.Logf("error : %+v", err)
t.Fail()
return
}
}
for i := range users {
for j := range users {
if i == j {
continue
}
s, err := users[i].Sign()
if err != nil {
t.Logf("error : %+v", err)
t.Fail()
return
}
users[j].SetSign(i+1, s)
}
}
t.Logf("Step5 / %fs", (time.Now().Sub(start)).Seconds())
for i := range users {
err := users[i].CheckSign()
if err != nil {
t.Logf("error : %+v", err)
t.Fail()
return
}
}
t.Logf("Step6 / %fs", (time.Now().Sub(start)).Seconds())
i := rndi(u)
sig, err := users[i].Signing()
if err != nil {
t.Logf("error : %+v", err)
t.Fail()
return
}
v := bipschnorr.Verification(P, m, sig)
t.Logf("Verification:%v / %fs", v, (time.Now().Sub(start)).Seconds())
if !v {
t.Logf("fail verify : %v", v)
t.Fail()
return
}
}
func rndbs() []byte {
bs := make([]byte, 32)
rand.Read(bs)
return bs
}
func rndbi() *big.Int {
r := new(big.Int).Mod(new(big.Int).SetBytes(rndbs()), btcec.S256().N)
return r
}
func rndi(n int) int {
r := new(big.Int).Mod(rndbi(), big.NewInt(int64(n)))
return int(r.Int64())
}