Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cell index to the Push method of Tree #20

Merged
merged 6 commits into from
Mar 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions datasquare.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ func (ds *dataSquare) RowRoot(x uint) []byte {
}

tree := ds.createTreeFn()
for _, d := range ds.Row(x) {
tree.Push(d)
for i, d := range ds.Row(x) {
tree.Push(d, SquareIndex{Cell: uint(i), Axis: x})
}

return tree.Root()
Expand All @@ -190,8 +190,8 @@ func (ds *dataSquare) ColRoot(y uint) []byte {
}

tree := ds.createTreeFn()
for _, d := range ds.Column(y) {
tree.Push(d)
for i, d := range ds.Column(y) {
tree.Push(d, SquareIndex{Axis: y, Cell: uint(i)})
}

return tree.Root()
Expand All @@ -202,7 +202,7 @@ func (ds *dataSquare) computeRowProof(x uint, y uint) ([]byte, [][]byte, uint, u
data := ds.Row(x)

for i := uint(0); i < ds.width; i++ {
tree.Push(data[i])
tree.Push(data[i], SquareIndex{Axis: y, Cell: uint(i)})
}

merkleRoot, proof, proofIndex, numLeaves := tree.Prove(int(y))
Expand All @@ -214,7 +214,7 @@ func (ds *dataSquare) computeColumnProof(x uint, y uint) ([]byte, [][]byte, uint
data := ds.Column(y)

for i := uint(0); i < ds.width; i++ {
tree.Push(data[i])
tree.Push(data[i], SquareIndex{Axis: y, Cell: uint(i)})
}
// TODO(ismail): check for overflow when casting from uint -> int
merkleRoot, proof, proofIndex, numLeaves := tree.Prove(int(x))
Expand Down
12 changes: 10 additions & 2 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ import (
// TreeConstructorFn creates a fresh Tree instance to be used as the Merkle inside of rsmt2d.
type TreeConstructorFn = func() Tree

// SquareIndex contains all information needed to identify the cell that is being
// pushed
type SquareIndex struct {
Axis, Cell uint
}

// Tree wraps merkle tree implementations to work with rsmt2d
type Tree interface {
Push(data []byte)
Push(data []byte, idx SquareIndex)
// TODO(ismail): is this general enough?
Prove(idx int) (merkleRoot []byte, proofSet [][]byte, proofIndex uint64, numLeaves uint64)
Root() []byte
Expand All @@ -32,7 +39,8 @@ func NewDefaultTree() Tree {
}
}

func (d *DefaultTree) Push(data []byte) {
func (d *DefaultTree) Push(data []byte, _idx SquareIndex) {
// ignore the idx, as this implementation doesn't need that info
d.leaves = append(d.leaves, data)
}

Expand Down