-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpedersen_dkg_test.go
187 lines (159 loc) · 4.4 KB
/
pedersen_dkg_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
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
package main
import (
"errors"
"github.com/stretchr/testify/require"
"go.dedis.ch/kyber/v4"
"go.dedis.ch/kyber/v4/group/edwards25519"
"go.dedis.ch/kyber/v4/share"
dkg "go.dedis.ch/kyber/v4/share/dkg/pedersen"
"go.dedis.ch/kyber/v4/sign/eddsa"
"go.dedis.ch/kyber/v4/sign/schnorr"
"go.dedis.ch/kyber/v4/util/random"
"testing"
)
type TestNode struct {
Index uint32
Private kyber.Scalar
Public kyber.Point
dkg *dkg.DistKeyGenerator
res *dkg.Result
}
func NewTestNode(s dkg.Suite, index int) *TestNode {
private := s.Scalar().Pick(random.New())
public := s.Point().Mul(private, nil)
return &TestNode{
Index: uint32(index),
Private: private,
Public: public,
}
}
func GenerateTestNodes(s dkg.Suite, n int) []*TestNode {
tns := make([]*TestNode, n)
for i := 0; i < n; i++ {
tns[i] = NewTestNode(s, i)
}
return tns
}
func NodesFromTest(tns []*TestNode) []dkg.Node {
nodes := make([]dkg.Node, len(tns))
for i := 0; i < len(tns); i++ {
nodes[i] = dkg.Node{
Index: tns[i].Index,
Public: tns[i].Public,
}
}
return nodes
}
// inits the dkg structure
func SetupNodes(nodes []*TestNode, c *dkg.Config) {
nonce := dkg.GetNonce()
for _, n := range nodes {
c2 := *c
c2.Longterm = n.Private
c2.Nonce = nonce
nodeDKG, err := dkg.NewDistKeyHandler(&c2)
if err != nil {
panic(err)
}
n.dkg = nodeDKG
}
}
func RunDKG(t *testing.T, tns []*TestNode, conf dkg.Config) ([]*dkg.Result, *share.PubShare) {
SetupNodes(tns, &conf)
var deals []*dkg.DealBundle
for _, node := range tns {
d, err := node.dkg.Deals()
require.NoError(t, err)
deals = append(deals, d)
}
var respBundles []*dkg.ResponseBundle
for _, node := range tns {
resp, err := node.dkg.ProcessDeals(deals)
require.NoError(t, err)
if resp != nil {
respBundles = append(respBundles, resp)
}
}
var justifs []*dkg.JustificationBundle
var results []*dkg.Result
for _, node := range tns {
res, just, err := node.dkg.ProcessResponses(respBundles)
if !errors.Is(err, dkg.ErrEvicted) {
// there should not be any other error than eviction
require.NoError(t, err)
}
if res != nil {
results = append(results, res)
} else if just != nil {
justifs = append(justifs, just)
}
}
// Get the public
if len(justifs) == 0 {
// Get the public
publicPoly := share.NewPubPoly(conf.Suite, conf.Suite.Point().Base(), results[0].Key.Commits)
publicKey := publicPoly.Eval(0)
return results, publicKey
}
for _, node := range tns {
res, err := node.dkg.ProcessJustifications(justifs)
if errors.Is(err, dkg.ErrEvicted) {
continue
}
require.NoError(t, err)
require.NotNil(t, res)
results = append(results, res)
}
// Get the public
publicPoly := share.NewPubPoly(conf.Suite, conf.Suite.Point().Base(), results[0].Key.Commits)
publicKey := publicPoly.Eval(0)
return results, publicKey
}
func Sign(results []*dkg.Result, threshold int, msg []byte, suite dkg.Suite, n int) ([]byte, error) {
if len(results) < threshold {
return nil, errors.New("not enough nodes to recover the secret")
}
var shares []*share.PriShare
for _, res := range results {
shares = append(shares, res.Key.PriShare())
}
secretPoly, err := share.RecoverPriPoly(suite, shares, threshold, n)
if err != nil {
return nil, err
}
secret := secretPoly.Secret()
publicPoly := share.NewPubPoly(suite, suite.Point().Base(), results[0].Key.Commits)
publicKey := publicPoly.Eval(0).V
ed := eddsa.NewEdDSA(suite.RandomStream())
ed.Secret = secret
ed.Public = publicKey
signedMsg, err := ed.Sign(msg)
return signedMsg, err
}
// Test_run_dkg_and_sign_message runs DKG with 5 nodes and then tries to sign some random message using the result
// and check if the signature can successfully be verified.
func Test_run_dkg_and_sign_message(t *testing.T) {
// Settings
n := 5
thr := 4
suite := edwards25519.NewBlakeSHA256Ed25519()
tns := GenerateTestNodes(suite, n)
list := NodesFromTest(tns)
conf := dkg.Config{
Suite: suite,
NewNodes: list,
Threshold: thr,
Auth: schnorr.NewScheme(suite),
}
// Run the DKG protocol
results, publicKey := RunDKG(t, tns, conf)
// Randomly select a subset of just enough (i.e. threshold) of nodes to try and sign the message
resultsSubset := make([]*dkg.Result, thr)
for i := 0; i < thr; i++ {
resultsSubset[i] = results[i]
}
msg := []byte("Hello")
signed, err := Sign(resultsSubset, thr, msg, suite, n)
require.NoError(t, err)
err = eddsa.Verify(publicKey.V, msg, signed)
}