-
Notifications
You must be signed in to change notification settings - Fork 257
/
proof.go
178 lines (156 loc) · 5.18 KB
/
proof.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
package proof
import (
"bytes"
"errors"
"fmt"
"github.com/celestiaorg/rsmt2d"
"github.com/celestiaorg/celestia-app/pkg/appconsts"
"github.com/celestiaorg/celestia-app/pkg/da"
appns "github.com/celestiaorg/celestia-app/pkg/namespace"
"github.com/celestiaorg/celestia-app/pkg/shares"
"github.com/celestiaorg/celestia-app/pkg/square"
"github.com/celestiaorg/celestia-app/pkg/wrapper"
"github.com/tendermint/tendermint/crypto/merkle"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/tendermint/tendermint/types"
)
// NewTxInclusionProof returns a new share inclusion proof for the given
// transaction index.
func NewTxInclusionProof(txs [][]byte, txIndex, appVersion uint64) (types.ShareProof, error) {
if txIndex >= uint64(len(txs)) {
return types.ShareProof{}, fmt.Errorf("txIndex %d out of bounds", txIndex)
}
builder, err := square.NewBuilder(appconsts.SquareSizeUpperBound(appVersion), appVersion, txs...)
if err != nil {
return types.ShareProof{}, err
}
dataSquare, err := builder.Export()
if err != nil {
return types.ShareProof{}, err
}
shareRange, err := builder.FindTxShareRange(int(txIndex))
if err != nil {
return types.ShareProof{}, err
}
namespace := getTxNamespace(txs[txIndex])
return NewShareInclusionProof(dataSquare, namespace, shareRange)
}
func getTxNamespace(tx []byte) (ns appns.Namespace) {
_, isBlobTx := types.UnmarshalBlobTx(tx)
if isBlobTx {
return appns.PayForBlobNamespace
}
return appns.TxNamespace
}
// NewShareInclusionProof takes an ODS, extends it, then
// returns an NMT inclusion proof for a set of shares
// belonging to the same namespace to the data root.
// Expects the share range to be pre-validated.
func NewShareInclusionProof(
dataSquare square.Square,
namespace appns.Namespace,
shareRange shares.Range,
) (types.ShareProof, error) {
eds, err := da.ExtendShares(shares.ToBytes(dataSquare))
if err != nil {
return types.ShareProof{}, err
}
return NewShareInclusionProofFromEDS(eds, namespace, shareRange)
}
// NewShareInclusionProofFromEDS takes an extended data square,
// and returns an NMT inclusion proof for a set of shares
// belonging to the same namespace to the data root.
// Expects the share range to be pre-validated.
func NewShareInclusionProofFromEDS(
eds *rsmt2d.ExtendedDataSquare,
namespace appns.Namespace,
shareRange shares.Range,
) (types.ShareProof, error) {
squareSize := square.Size(len(eds.FlattenedODS()))
startRow := shareRange.Start / squareSize
endRow := (shareRange.End - 1) / squareSize
startLeaf := shareRange.Start % squareSize
endLeaf := (shareRange.End - 1) % squareSize
edsRowRoots, err := eds.RowRoots()
if err != nil {
return types.ShareProof{}, err
}
edsColRoots, err := eds.ColRoots()
if err != nil {
return types.ShareProof{}, err
}
// create the binary merkle inclusion proof for all the square rows to the data root
_, allProofs := merkle.ProofsFromByteSlices(append(edsRowRoots, edsColRoots...))
rowProofs := make([]*merkle.Proof, endRow-startRow+1)
rowRoots := make([]tmbytes.HexBytes, endRow-startRow+1)
for i := startRow; i <= endRow; i++ {
rowProofs[i-startRow] = allProofs[i]
rowRoots[i-startRow] = edsRowRoots[i]
}
// get the extended rows containing the shares.
rows := make([][]shares.Share, endRow-startRow+1)
for i := startRow; i <= endRow; i++ {
shares, err := shares.FromBytes(eds.Row(uint(i)))
if err != nil {
return types.ShareProof{}, err
}
rows[i-startRow] = shares
}
var shareProofs []*tmproto.NMTProof //nolint:prealloc
var rawShares [][]byte
for i, row := range rows {
// create an nmt to generate a proof.
// we have to re-create the tree as the eds one is not accessible.
tree := wrapper.NewErasuredNamespacedMerkleTree(uint64(squareSize), uint(i))
for _, share := range row {
err := tree.Push(
share.ToBytes(),
)
if err != nil {
return types.ShareProof{}, err
}
}
// make sure that the generated root is the same as the eds row root.
root, err := tree.Root()
if err != nil {
return types.ShareProof{}, err
}
if !bytes.Equal(rowRoots[i].Bytes(), root) {
return types.ShareProof{}, errors.New("eds row root is different than tree root")
}
startLeafPos := startLeaf
endLeafPos := endLeaf
// if this is not the first row, then start with the first leaf
if i > 0 {
startLeafPos = 0
}
// if this is not the last row, then select for the rest of the row
if i != (len(rows) - 1) {
endLeafPos = squareSize - 1
}
rawShares = append(rawShares, shares.ToBytes(row[startLeafPos:endLeafPos+1])...)
proof, err := tree.ProveRange(int(startLeafPos), int(endLeafPos+1))
if err != nil {
return types.ShareProof{}, err
}
shareProofs = append(shareProofs, &tmproto.NMTProof{
Start: int32(proof.Start()),
End: int32(proof.End()),
Nodes: proof.Nodes(),
LeafHash: proof.LeafHash(),
})
}
return types.ShareProof{
RowProof: types.RowProof{
RowRoots: rowRoots,
Proofs: rowProofs,
StartRow: uint32(startRow),
EndRow: uint32(endRow),
},
Data: rawShares,
ShareProofs: shareProofs,
NamespaceID: namespace.ID,
NamespaceVersion: uint32(namespace.Version),
}, nil
}