Skip to content

Commit

Permalink
refactor: expose CreateShareToRowRootProofs for downstream use (#3739)
Browse files Browse the repository at this point in the history
<!--
Please read and fill out this form before submitting your PR.

Please make sure you have reviewed our contributors guide before
submitting your
first PR.
-->

## Overview

Exposes that functionality so that we don't end up copy-pasting that
code in Celestia-node.

(cherry picked from commit 8536727)

# Conflicts:
#	pkg/proof/proof.go
  • Loading branch information
rach-id authored and mergify[bot] committed Jul 26, 2024
1 parent 01f0adc commit 71f03b9
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions pkg/proof/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func NewShareInclusionProofFromEDS(
rows[i-startRow] = shares
}

<<<<<<< HEAD

Check failure on line 116 in pkg/proof/proof.go

View workflow job for this annotation

GitHub Actions / test / test

syntax error: unexpected <<, expected }

Check failure on line 116 in pkg/proof/proof.go

View workflow job for this annotation

GitHub Actions / test / test-short

syntax error: unexpected <<, expected }

Check failure on line 116 in pkg/proof/proof.go

View workflow job for this annotation

GitHub Actions / test / test-coverage

syntax error: unexpected <<, expected }

Check failure on line 116 in pkg/proof/proof.go

View workflow job for this annotation

GitHub Actions / test / test-race

syntax error: unexpected <<, expected }

Check failure on line 116 in pkg/proof/proof.go

View workflow job for this annotation

GitHub Actions / lint / golangci-lint

syntax error: unexpected <<, expected }

Check failure on line 116 in pkg/proof/proof.go

View workflow job for this annotation

GitHub Actions / lint / golangci-lint

expected statement, found '<<' (typecheck)
var shareProofs []*tmproto.NMTProof //nolint:prealloc
var rawShares [][]byte
for i, row := range rows {

Check failure on line 119 in pkg/proof/proof.go

View workflow job for this annotation

GitHub Actions / test / test

syntax error: non-declaration statement outside function body

Check failure on line 119 in pkg/proof/proof.go

View workflow job for this annotation

GitHub Actions / test / test-short

syntax error: non-declaration statement outside function body

Check failure on line 119 in pkg/proof/proof.go

View workflow job for this annotation

GitHub Actions / test / test-coverage

syntax error: non-declaration statement outside function body

Check failure on line 119 in pkg/proof/proof.go

View workflow job for this annotation

GitHub Actions / test / test-race

syntax error: non-declaration statement outside function body

Check failure on line 119 in pkg/proof/proof.go

View workflow job for this annotation

GitHub Actions / lint / golangci-lint

syntax error: non-declaration statement outside function body
Expand Down Expand Up @@ -165,6 +166,14 @@ func NewShareInclusionProofFromEDS(

return types.ShareProof{
RowProof: types.RowProof{
=======

Check failure on line 169 in pkg/proof/proof.go

View workflow job for this annotation

GitHub Actions / lint / golangci-lint

expected operand, found '==' (typecheck)
shareProofs, rawShares, err := CreateShareToRowRootProofs(squareSize, rows, rowRoots, startLeaf, endLeaf)
if err != nil {

Check failure on line 171 in pkg/proof/proof.go

View workflow job for this annotation

GitHub Actions / lint / golangci-lint

missing ',' in composite literal (typecheck)
return ShareProof{}, err

Check failure on line 172 in pkg/proof/proof.go

View workflow job for this annotation

GitHub Actions / lint / golangci-lint

expected operand, found 'return' (typecheck)
}
return ShareProof{
RowProof: &RowProof{
>>>>>>> 8536727a (refactor: expose CreateShareToRowRootProofs for downstream use (#3739))

Check failure on line 176 in pkg/proof/proof.go

View workflow job for this annotation

GitHub Actions / test / test

invalid character U+0023 '#'

Check failure on line 176 in pkg/proof/proof.go

View workflow job for this annotation

GitHub Actions / test / test-short

invalid character U+0023 '#'

Check failure on line 176 in pkg/proof/proof.go

View workflow job for this annotation

GitHub Actions / test / test-coverage

invalid character U+0023 '#'

Check failure on line 176 in pkg/proof/proof.go

View workflow job for this annotation

GitHub Actions / test / test-race

invalid character U+0023 '#'

Check failure on line 176 in pkg/proof/proof.go

View workflow job for this annotation

GitHub Actions / lint / golangci-lint

invalid character U+0023 '#'
RowRoots: rowRoots,
Proofs: rowProofs,
StartRow: uint32(startRow),
Expand All @@ -176,3 +185,68 @@ func NewShareInclusionProofFromEDS(
NamespaceVersion: uint32(namespace.Version),
}, nil
}
<<<<<<< HEAD
=======

func safeConvertUint64ToInt(val uint64) (int, error) {
if val > math.MaxInt {
return 0, fmt.Errorf("value %d is too large to convert to int", val)
}
return int(val), nil
}

// CreateShareToRowRootProofs takes a set of shares and their corresponding row roots, and generates
// an NMT inclusion proof of a set of shares, defined by startLeaf and endLeaf, to their corresponding row roots.
func CreateShareToRowRootProofs(squareSize int, rowShares [][]shares.Share, rowRoots [][]byte, startLeaf, endLeaf int) ([]*NMTProof, [][]byte, error) {
shareProofs := make([]*NMTProof, 0, len(rowRoots))
var rawShares [][]byte
for i, row := range rowShares {
// 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 nil, nil, err
}
}

// make sure that the generated root is the same as the eds row root.
root, err := tree.Root()
if err != nil {
return nil, nil, err
}
if !bytes.Equal(rowRoots[i], root) {
return nil, nil, 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(rowShares) - 1) {
endLeafPos = squareSize - 1
}

rawShares = append(rawShares, shares.ToBytes(row[startLeafPos:endLeafPos+1])...)
proof, err := tree.ProveRange(startLeafPos, endLeafPos+1)
if err != nil {
return nil, nil, err
}

shareProofs = append(shareProofs, &NMTProof{
Start: int32(proof.Start()),
End: int32(proof.End()),
Nodes: proof.Nodes(),
LeafHash: proof.LeafHash(),
})
}
return shareProofs, rawShares, nil
}
>>>>>>> 8536727a (refactor: expose CreateShareToRowRootProofs for downstream use (#3739))

Check failure on line 252 in pkg/proof/proof.go

View workflow job for this annotation

GitHub Actions / test / test

syntax error: non-declaration statement outside function body

Check failure on line 252 in pkg/proof/proof.go

View workflow job for this annotation

GitHub Actions / test / test

invalid character U+0023 '#'

Check failure on line 252 in pkg/proof/proof.go

View workflow job for this annotation

GitHub Actions / test / test-short

syntax error: non-declaration statement outside function body

Check failure on line 252 in pkg/proof/proof.go

View workflow job for this annotation

GitHub Actions / test / test-short

invalid character U+0023 '#'

Check failure on line 252 in pkg/proof/proof.go

View workflow job for this annotation

GitHub Actions / test / test-coverage

syntax error: non-declaration statement outside function body

Check failure on line 252 in pkg/proof/proof.go

View workflow job for this annotation

GitHub Actions / test / test-coverage

invalid character U+0023 '#'

Check failure on line 252 in pkg/proof/proof.go

View workflow job for this annotation

GitHub Actions / test / test-race

syntax error: non-declaration statement outside function body

Check failure on line 252 in pkg/proof/proof.go

View workflow job for this annotation

GitHub Actions / test / test-race

invalid character U+0023 '#'

Check failure on line 252 in pkg/proof/proof.go

View workflow job for this annotation

GitHub Actions / lint / golangci-lint

syntax error: non-declaration statement outside function body

Check failure on line 252 in pkg/proof/proof.go

View workflow job for this annotation

GitHub Actions / lint / golangci-lint

invalid character U+0023 '#' (typecheck)

0 comments on commit 71f03b9

Please sign in to comment.