/
helpers.go
187 lines (166 loc) · 5.15 KB
/
helpers.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 fuzz
import (
"math/rand"
"testing"
gokzg "github.com/crate-crypto/go-kzg-4844"
ckzg "github.com/ethereum/c-kzg-4844/bindings/go"
"github.com/holiman/uint256"
)
type Action int64
const (
actionRandom Action = 0
actionValid Action = 1
actionMutated Action = 2
)
func Mutate(data []byte, seed int64) []byte {
rand.Seed(seed)
/* Mutate at most 1% of the bits */
count := int(float32(rand.Intn(len(data)*8)) * 0.01)
for i := 0; i < count; i++ {
index := rand.Intn(len(data))
bitPosition := uint(rand.Intn(8))
data[index] ^= 1 << bitPosition
}
return data
}
func GetRandFieldElement(t *testing.T, seed int64) (ckzg.Bytes32, [32]byte, bool) {
t.Helper()
rand.Seed(seed)
fieldElementBytes := make([]byte, ckzg.BytesPerFieldElement)
_, err := rand.Read(fieldElementBytes)
if err != nil {
return ckzg.Bytes32{}, [32]byte{}, false
}
var cKzgFieldElement ckzg.Bytes32
action := Action(seed % 3)
if action == actionRandom {
// Provide a completely random field element.
copy(cKzgFieldElement[:], fieldElementBytes[:])
return cKzgFieldElement, cKzgFieldElement, true
} else {
// Provide a valid/canonical field element.
var BlsModulus = new(uint256.Int)
BlsModulus.SetBytes(gokzg.BlsModulus[:])
field := new(uint256.Int).SetBytes(fieldElementBytes[:])
field = field.Mod(field, BlsModulus)
canonicalFieldElementBytes := field.Bytes32()
// Mutate the data, which may make it invalid.
if action == actionMutated {
mutateSeed := rand.Int63()
mutated := Mutate(canonicalFieldElementBytes[:], mutateSeed)
copy(canonicalFieldElementBytes[:], mutated)
}
copy(cKzgFieldElement[:], canonicalFieldElementBytes[:])
return cKzgFieldElement, canonicalFieldElementBytes, true
}
}
func GetRandCanonicalFieldElement(t *testing.T, seed int64) (ckzg.Bytes32, [32]byte, bool) {
t.Helper()
rand.Seed(seed)
fieldElementBytes := make([]byte, ckzg.BytesPerFieldElement)
_, err := rand.Read(fieldElementBytes)
if err != nil {
return ckzg.Bytes32{}, [32]byte{}, false
}
var BlsModulus = new(uint256.Int)
BlsModulus.SetBytes(gokzg.BlsModulus[:])
field := new(uint256.Int).SetBytes(fieldElementBytes[:])
field = field.Mod(field, BlsModulus)
canonicalFieldElementBytes := field.Bytes32()
var cKzgFieldElement ckzg.Bytes32
copy(cKzgFieldElement[:], canonicalFieldElementBytes[:])
return cKzgFieldElement, canonicalFieldElementBytes, true
}
func GetRandBlob(t *testing.T, seed int64) (ckzg.Blob, gokzg.Blob, bool) {
t.Helper()
rand.Seed(seed)
action := Action(seed % 3)
if action == actionRandom {
// Provide a completely random blob.
randomBytes := make([]byte, ckzg.BytesPerBlob)
_, err := rand.Read(randomBytes)
if err != nil {
return ckzg.Blob{}, gokzg.Blob{}, false
}
var cKzgBlob ckzg.Blob
copy(cKzgBlob[:], randomBytes)
var goKzgBlob gokzg.Blob
copy(goKzgBlob[:], randomBytes)
return cKzgBlob, goKzgBlob, true
} else {
// Provide a valid/canonical blob.
var cKzgBlob ckzg.Blob
var goKzgBlob gokzg.Blob
for i := 0; i < ckzg.BytesPerBlob; i += ckzg.BytesPerFieldElement {
newSeed := rand.Int63()
_, canonicalFieldElementBytes, ok := GetRandCanonicalFieldElement(t, newSeed)
if !ok {
return ckzg.Blob{}, gokzg.Blob{}, false
}
// Mutate the data, which may make it invalid.
if action == actionMutated {
mutateSeed := rand.Int63()
mutated := Mutate(canonicalFieldElementBytes[:], mutateSeed)
copy(canonicalFieldElementBytes[:], mutated)
}
copy(cKzgBlob[i:i+ckzg.BytesPerFieldElement], canonicalFieldElementBytes[:])
copy(goKzgBlob[i:i+ckzg.BytesPerFieldElement], canonicalFieldElementBytes[:])
}
return cKzgBlob, goKzgBlob, true
}
}
func GetRandG1(t *testing.T, seed int64) ([]byte, bool) {
t.Helper()
rand.Seed(seed)
action := Action(seed % 3)
if action == actionRandom {
// Provide a completely random g1 point.
randomBytes := make([]byte, 48)
_, err := rand.Read(randomBytes)
if err != nil {
return []byte{}, false
}
return randomBytes, true
} else {
// Provide a valid/canonical g1 point.
blob, _, ok := GetRandBlob(t, seed)
if ok != false {
return []byte{}, false
}
commitment, err := ckzg.BlobToKZGCommitment(blob)
// Mutate the data, which may make it invalid.
if action == actionMutated {
mutateSeed := rand.Int63()
mutated := Mutate(commitment[:], mutateSeed)
copy(commitment[:], mutated)
}
if err != nil {
return []byte{}, false
}
return commitment[:], true
}
}
func GetRandCommitment(t *testing.T, seed int64) (ckzg.Bytes48, gokzg.KZGCommitment, bool) {
t.Helper()
commitmentBytes, ok := GetRandG1(t, seed)
if !ok {
return ckzg.Bytes48{}, gokzg.KZGCommitment{}, false
}
var cKzgCommitment ckzg.Bytes48
copy(cKzgCommitment[:], commitmentBytes)
var goKzgCommitment gokzg.KZGCommitment
copy(goKzgCommitment[:], commitmentBytes)
return cKzgCommitment, goKzgCommitment, true
}
func GetRandProof(t *testing.T, seed int64) (ckzg.Bytes48, gokzg.KZGProof, bool) {
t.Helper()
proofBytes, ok := GetRandG1(t, seed)
if !ok {
return ckzg.Bytes48{}, gokzg.KZGProof{}, false
}
var cKzgProof ckzg.Bytes48
copy(cKzgProof[:], proofBytes)
var goKzgProof gokzg.KZGProof
copy(goKzgProof[:], proofBytes)
return cKzgProof, goKzgProof, true
}