forked from prysmaticlabs/prysm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
kzg.go
219 lines (199 loc) · 5.91 KB
/
kzg.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package blocks
import (
"github.com/pkg/errors"
"github.com/prysmaticlabs/gohashtree"
field_params "github.com/stratisproject/prysm-stratis/config/fieldparams"
"github.com/stratisproject/prysm-stratis/config/params"
"github.com/stratisproject/prysm-stratis/consensus-types/interfaces"
"github.com/stratisproject/prysm-stratis/container/trie"
"github.com/stratisproject/prysm-stratis/encoding/ssz"
"github.com/stratisproject/prysm-stratis/runtime/version"
)
const (
bodyLength = 12 // The number of elements in the BeaconBlockBody Container
logBodyLength = 4 // The log 2 of bodyLength
kzgPosition = 11 // The index of the KZG commitment list in the Body
KZGOffset = 54 * field_params.MaxBlobCommitmentsPerBlock
)
var (
errInvalidIndex = errors.New("index out of bounds")
errInvalidBodyRoot = errors.New("invalid Beacon Block Body root")
errInvalidInclusionProof = errors.New("invalid KZG commitment inclusion proof")
)
// VerifyKZGInclusionProof verifies the Merkle proof in a Blob sidecar against
// the beacon block body root.
func VerifyKZGInclusionProof(blob ROBlob) error {
if blob.SignedBlockHeader == nil {
return errNilBlockHeader
}
if blob.SignedBlockHeader.Header == nil {
return errNilBlockHeader
}
root := blob.SignedBlockHeader.Header.BodyRoot
if len(root) != field_params.RootLength {
return errInvalidBodyRoot
}
chunks := make([][32]byte, 2)
copy(chunks[0][:], blob.KzgCommitment)
copy(chunks[1][:], blob.KzgCommitment[field_params.RootLength:])
gohashtree.HashChunks(chunks, chunks)
verified := trie.VerifyMerkleProof(root, chunks[0][:], blob.Index+KZGOffset, blob.CommitmentInclusionProof)
if !verified {
return errInvalidInclusionProof
}
return nil
}
// MerkleProofKZGCommitment constructs a Merkle proof of inclusion of the KZG
// commitment of index `index` into the Beacon Block with the given `body`
func MerkleProofKZGCommitment(body interfaces.ReadOnlyBeaconBlockBody, index int) ([][]byte, error) {
bodyVersion := body.Version()
if bodyVersion < version.Deneb {
return nil, errUnsupportedBeaconBlockBody
}
commitments, err := body.BlobKzgCommitments()
if err != nil {
return nil, err
}
proof, err := bodyProof(commitments, index)
if err != nil {
return nil, err
}
membersRoots, err := topLevelRoots(body)
if err != nil {
return nil, err
}
sparse, err := trie.GenerateTrieFromItems(membersRoots, logBodyLength)
if err != nil {
return nil, err
}
topProof, err := sparse.MerkleProof(kzgPosition)
if err != nil {
return nil, err
}
// sparse.MerkleProof always includes the length of the slice this is
// why we remove the last element that is not needed in topProof
proof = append(proof, topProof[:len(topProof)-1]...)
return proof, nil
}
// leavesFromCommitments hashes each commitment to construct a slice of roots
func leavesFromCommitments(commitments [][]byte) [][]byte {
leaves := make([][]byte, len(commitments))
for i, kzg := range commitments {
chunk := make([][32]byte, 2)
copy(chunk[0][:], kzg)
copy(chunk[1][:], kzg[field_params.RootLength:])
gohashtree.HashChunks(chunk, chunk)
leaves[i] = chunk[0][:]
}
return leaves
}
// bodyProof returns the Merkle proof of the subtree up to the root of the KZG
// commitment list.
func bodyProof(commitments [][]byte, index int) ([][]byte, error) {
if index < 0 || index >= len(commitments) {
return nil, errInvalidIndex
}
leaves := leavesFromCommitments(commitments)
sparse, err := trie.GenerateTrieFromItems(leaves, field_params.LogMaxBlobCommitments)
if err != nil {
return nil, err
}
proof, err := sparse.MerkleProof(index)
if err != nil {
return nil, err
}
return proof, err
}
// topLevelRoots computes the slice with the roots of each element in the
// BeaconBlockBody. Notice that the KZG commitments root is not needed for the
// proof computation thus it's omitted
func topLevelRoots(body interfaces.ReadOnlyBeaconBlockBody) ([][]byte, error) {
layer := make([][]byte, bodyLength)
for i := range layer {
layer[i] = make([]byte, 32)
}
// Randao Reveal
randao := body.RandaoReveal()
root, err := ssz.MerkleizeByteSliceSSZ(randao[:])
if err != nil {
return nil, err
}
copy(layer[0], root[:])
// eth1_data
eth1 := body.Eth1Data()
root, err = eth1.HashTreeRoot()
if err != nil {
return nil, err
}
copy(layer[1], root[:])
// graffiti
root = body.Graffiti()
copy(layer[2], root[:])
// Proposer slashings
ps := body.ProposerSlashings()
root, err = ssz.MerkleizeListSSZ(ps, params.BeaconConfig().MaxProposerSlashings)
if err != nil {
return nil, err
}
copy(layer[3], root[:])
// Attester slashings
as := body.AttesterSlashings()
root, err = ssz.MerkleizeListSSZ(as, params.BeaconConfig().MaxAttesterSlashings)
if err != nil {
return nil, err
}
copy(layer[4], root[:])
// Attestations
att := body.Attestations()
root, err = ssz.MerkleizeListSSZ(att, params.BeaconConfig().MaxAttestations)
if err != nil {
return nil, err
}
copy(layer[5], root[:])
// Deposits
dep := body.Deposits()
root, err = ssz.MerkleizeListSSZ(dep, params.BeaconConfig().MaxDeposits)
if err != nil {
return nil, err
}
copy(layer[6], root[:])
// Voluntary Exits
ve := body.VoluntaryExits()
root, err = ssz.MerkleizeListSSZ(ve, params.BeaconConfig().MaxVoluntaryExits)
if err != nil {
return nil, err
}
copy(layer[7], root[:])
// Sync Aggregate
sa, err := body.SyncAggregate()
if err != nil {
return nil, err
}
root, err = sa.HashTreeRoot()
if err != nil {
return nil, err
}
copy(layer[8], root[:])
// Execution Payload
ep, err := body.Execution()
if err != nil {
return nil, err
}
root, err = ep.HashTreeRoot()
if err != nil {
return nil, err
}
copy(layer[9], root[:])
// BLS Changes
bls, err := body.BLSToExecutionChanges()
if err != nil {
return nil, err
}
root, err = ssz.MerkleizeListSSZ(bls, params.BeaconConfig().MaxBlsToExecutionChanges)
if err != nil {
return nil, err
}
copy(layer[10], root[:])
// KZG commitments is not needed
return layer, nil
}