-
Notifications
You must be signed in to change notification settings - Fork 26
/
legacy_recovery_test.go
140 lines (132 loc) · 3.86 KB
/
legacy_recovery_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
package kzg
import (
"fmt"
"github.com/protolambda/go-kzg/bls"
"math/rand"
"testing"
)
func TestErasureCodeRecoverSimple(t *testing.T) {
// Create some random data, with padding...
fs := NewFFTSettings(5)
poly := make([]bls.Fr, fs.MaxWidth, fs.MaxWidth)
for i := uint64(0); i < fs.MaxWidth/2; i++ {
bls.AsFr(&poly[i], i)
}
for i := fs.MaxWidth / 2; i < fs.MaxWidth; i++ {
poly[i] = bls.ZERO
}
debugFrs("poly", poly)
// Get data for polynomial SLOW_INDICES
data, err := fs.FFT(poly, false)
if err != nil {
t.Fatal(err)
}
debugFrs("data", data)
// copy over the 2nd half, leave the first half as nils
subset := make([]*bls.Fr, fs.MaxWidth, fs.MaxWidth)
half := fs.MaxWidth / 2
for i := half; i < fs.MaxWidth; i++ {
subset[i] = &data[i]
}
debugFrPtrs("subset", subset)
recovered, err := fs.ErasureCodeRecover(subset)
if err != nil {
t.Fatal(err)
}
debugFrs("recovered", recovered)
for i := range recovered {
if got := &recovered[i]; !bls.EqualFr(got, &data[i]) {
t.Errorf("recovery at index %d got %s but expected %s", i, bls.FrStr(got), bls.FrStr(&data[i]))
}
}
// And recover the original coeffs for good measure
back, err := fs.FFT(recovered, true)
if err != nil {
t.Fatal(err)
}
debugFrs("back", back)
for i := uint64(0); i < half; i++ {
if got := &back[i]; !bls.EqualFr(got, &poly[i]) {
t.Errorf("coeff at index %d got %s but expected %s", i, bls.FrStr(got), bls.FrStr(&poly[i]))
}
}
for i := half; i < fs.MaxWidth; i++ {
if got := &back[i]; !bls.EqualZero(got) {
t.Errorf("expected zero padding in index %d", i)
}
}
}
func TestErasureCodeRecover(t *testing.T) {
// Create some random poly, with padding so we get redundant data
fs := NewFFTSettings(7)
poly := make([]bls.Fr, fs.MaxWidth, fs.MaxWidth)
for i := uint64(0); i < fs.MaxWidth/2; i++ {
bls.AsFr(&poly[i], i)
}
for i := fs.MaxWidth / 2; i < fs.MaxWidth; i++ {
poly[i] = bls.ZERO
}
debugFrs("poly", poly)
// Get coefficients for polynomial SLOW_INDICES
data, err := fs.FFT(poly, false)
if err != nil {
t.Fatal(err)
}
debugFrs("data", data)
// Util to pick a random subnet of the values
randomSubset := func(known uint64, rngSeed uint64) []*bls.Fr {
withMissingValues := make([]*bls.Fr, fs.MaxWidth, fs.MaxWidth)
for i := range data {
withMissingValues[i] = &data[i]
}
rng := rand.New(rand.NewSource(int64(rngSeed)))
missing := fs.MaxWidth - known
pruned := rng.Perm(int(fs.MaxWidth))[:missing]
for _, i := range pruned {
withMissingValues[i] = nil
}
return withMissingValues
}
// Try different amounts of known indices, and try it in multiple random ways
var lastKnown uint64 = 0
for knownRatio := 0.7; knownRatio < 1.0; knownRatio += 0.05 {
known := uint64(float64(fs.MaxWidth) * knownRatio)
if known == lastKnown {
continue
}
lastKnown = known
for i := 0; i < 3; i++ {
t.Run(fmt.Sprintf("random_subset_%d_known_%d", i, known), func(t *testing.T) {
subset := randomSubset(known, uint64(i))
debugFrPtrs("subset", subset)
recovered, err := fs.ErasureCodeRecover(subset)
if err != nil {
t.Fatal(err)
}
debugFrs("recovered", recovered)
for i := range recovered {
if got := &recovered[i]; !bls.EqualFr(got, &data[i]) {
t.Errorf("recovery at index %d got %s but expected %s", i, bls.FrStr(got), bls.FrStr(&data[i]))
}
}
// And recover the original coeffs for good measure
back, err := fs.FFT(recovered, true)
if err != nil {
t.Fatal(err)
}
debugFrs("back", back)
half := uint64(len(back)) / 2
for i := uint64(0); i < half; i++ {
if got := &back[i]; !bls.EqualFr(got, &poly[i]) {
t.Errorf("coeff at index %d got %s but expected %s", i, bls.FrStr(got), bls.FrStr(&poly[i]))
}
}
for i := half; i < fs.MaxWidth; i++ {
if got := &back[i]; !bls.EqualZero(got) {
t.Errorf("expected zero padding in index %d", i)
}
}
})
}
}
}