-
Notifications
You must be signed in to change notification settings - Fork 4
/
shuffle_integrity_test.go
155 lines (141 loc) · 3.83 KB
/
shuffle_integrity_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
package crypto
import (
"github.com/herumi/bls-eth-go-binary/bls"
"github.com/stretchr/testify/require"
"testing"
)
// will generate a shared secret via dkg, then re-distributes the shares and checks
// that shares from the original DKG can't be combined with the shares from the re-distribution to
// re-construct the secret.
func TestShuffleIntegrity(t *testing.T) {
InitBLS()
///
/// Step 1 - first DKG
///
poly1 := &Polynomial{
Degree: 2,
Coefficients: []bls.Fr{
frFromInt(6), // free coefficient
frFromInt(0), // x^1
frFromInt(1), // x^2
},
}
poly2 := &Polynomial{
Degree: 2,
Coefficients: []bls.Fr{
frFromInt(3), // free coefficient
frFromInt(0), // x^1
frFromInt(1), // x^2
},
}
poly3 := &Polynomial{
Degree: 2,
Coefficients: []bls.Fr{
frFromInt(9), // free coefficient
frFromInt(0), // x^1
frFromInt(1), // x^2
},
}
// shared secret is 18
dkg := DKG{polynomials:map[uint32]*Polynomial{
1: poly1,
2: poly2,
3: poly3,
}, degree:2}
// dkg shares
sks,err := dkg.GroupSecrets([]uint32{
1,
2,
3,
})
require.NoError(t, err)
///
/// Step 2 - redistribute shares
///
distro1,err := NewRedistribuition(2, sks[1])
require.NoError(t,err)
distro2,err := NewRedistribuition(2, sks[2])
require.NoError(t,err)
distro3,err := NewRedistribuition(2, sks[3])
require.NoError(t,err)
// generate shares to everyone
sharesFrom1,err := distro1.GenerateShares([]uint32{1,2,3})
require.NoError(t,err)
sharesFrom2,err := distro2.GenerateShares([]uint32{1,2,3})
require.NoError(t,err)
sharesFrom3,err := distro3.GenerateShares([]uint32{1,2,3})
require.NoError(t,err)
shares1 := [][]bls.Fr {
{frFromInt(1), *sharesFrom1[1]},
{frFromInt(2), *sharesFrom2[1]},
{frFromInt(3), *sharesFrom3[1]},
}
shares2 := [][]bls.Fr {
{frFromInt(1), *sharesFrom1[2]},
{frFromInt(2), *sharesFrom2[2]},
{frFromInt(3), *sharesFrom3[2]},
}
shares3 := [][]bls.Fr {
{frFromInt(1), *sharesFrom1[3]},
{frFromInt(2), *sharesFrom2[3]},
{frFromInt(3), *sharesFrom3[3]},
}
// reconstruct individual group sk for 1,2,3
p1 := NewLagrangeInterpolation(shares1)
sk1Interpolated,err := p1.Interpolate()
require.NoError(t,err)
p2 := NewLagrangeInterpolation(shares2)
sk2Interpolated,err := p2.Interpolate()
require.NoError(t,err)
p3 := NewLagrangeInterpolation(shares3)
sk3Interpolated,err := p3.Interpolate()
require.NoError(t,err)
///
/// Step 3 - verify redistribute and original shares reconstruct to the same secreet
///
group := [][]bls.Fr {
{frFromInt(1), *sk1Interpolated},
{frFromInt(2), *sk2Interpolated},
{frFromInt(3), *sk3Interpolated},
}
p := NewLagrangeInterpolation(group)
redistribuitedGroupSk, err := p.Interpolate()
require.NoError(t,err)
require.Equal(t, "18", redistribuitedGroupSk.GetString(10))
///
/// Step 4 - try to re-construct the secreet by combining shares from the 2 groups
///
t.Run("shuffled 1", func(t *testing.T) {
group = [][]bls.Fr {
{frFromInt(1), *sk1Interpolated},
{frFromInt(2), *sks[2]},
{frFromInt(3), *sk3Interpolated},
}
p = NewLagrangeInterpolation(group)
shuffledGroupSk, err := p.Interpolate()
require.NoError(t,err)
require.NotEqual(t, "18", shuffledGroupSk.GetString(10))
})
t.Run("shuffled 2", func(t *testing.T) {
group = [][]bls.Fr {
{frFromInt(1), *sk1Interpolated},
{frFromInt(2), *sks[2]},
{frFromInt(3), *sks[3]},
}
p = NewLagrangeInterpolation(group)
shuffledGroupSk, err := p.Interpolate()
require.NoError(t,err)
require.NotEqual(t, "18", shuffledGroupSk.GetString(10))
})
t.Run("shuffled 2", func(t *testing.T) {
group = [][]bls.Fr {
{frFromInt(1), *sk1Interpolated},
{frFromInt(2), *sk2Interpolated},
{frFromInt(3), *sks[3]},
}
p = NewLagrangeInterpolation(group)
shuffledGroupSk, err := p.Interpolate()
require.NoError(t,err)
require.NotEqual(t, "18", shuffledGroupSk.GetString(10))
})
}