From 8213a4a3b2d15214610f046597c427c1a9789bf0 Mon Sep 17 00:00:00 2001 From: evan-forbes Date: Fri, 26 Mar 2021 12:29:11 -0500 Subject: [PATCH 1/6] add cell index to the Push method of Tree --- datasquare.go | 12 ++++++------ tree.go | 18 ++++++++++++++++-- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/datasquare.go b/datasquare.go index f4d65f2..dd2ed20 100644 --- a/datasquare.go +++ b/datasquare.go @@ -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, CellIndex{Axis: Row, CellIndex: uint(i), AxisIndex: x}) } return tree.Root() @@ -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, CellIndex{Axis: Col, CellIndex: uint(i), AxisIndex: y}) } return tree.Root() @@ -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], CellIndex{Axis: Row, CellIndex: uint(i), AxisIndex: y}) } merkleRoot, proof, proofIndex, numLeaves := tree.Prove(int(y)) @@ -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], CellIndex{Axis: Col, CellIndex: uint(i), AxisIndex: y}) } // TODO(ismail): check for overflow when casting from uint -> int merkleRoot, proof, proofIndex, numLeaves := tree.Prove(int(x)) diff --git a/tree.go b/tree.go index 36ba865..22a233a 100644 --- a/tree.go +++ b/tree.go @@ -10,8 +10,21 @@ import ( // TreeConstructorFn creates a fresh Tree instance to be used as the Merkle inside of rsmt2d. type TreeConstructorFn = func() Tree +type Axis bool + +const ( + Row Axis = true + Col Axis = false +) + +type CellIndex struct { + AxisIndex uint + CellIndex uint + Axis Axis +} + type Tree interface { - Push(data []byte) + Push(data []byte, idx CellIndex) // TODO(ismail): is this general enough? Prove(idx int) (merkleRoot []byte, proofSet [][]byte, proofIndex uint64, numLeaves uint64) Root() []byte @@ -32,7 +45,8 @@ func NewDefaultTree() Tree { } } -func (d *DefaultTree) Push(data []byte) { +func (d *DefaultTree) Push(data []byte, idx CellIndex) { + // ignore the idx, as this implementation doesn't need know d.leaves = append(d.leaves, data) } From a666394c92c69f6c3d57b5f99e7c6d47294d4991 Mon Sep 17 00:00:00 2001 From: evan-forbes Date: Fri, 26 Mar 2021 12:40:13 -0500 Subject: [PATCH 2/6] small doc change --- tree.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tree.go b/tree.go index 22a233a..fd36d07 100644 --- a/tree.go +++ b/tree.go @@ -46,7 +46,7 @@ func NewDefaultTree() Tree { } func (d *DefaultTree) Push(data []byte, idx CellIndex) { - // ignore the idx, as this implementation doesn't need know + // ignore the idx, as this implementation doesn't need that info d.leaves = append(d.leaves, data) } From 3b5ae818a3aac5ed27466830f623167f852b2b3d Mon Sep 17 00:00:00 2001 From: evan-forbes Date: Fri, 26 Mar 2021 12:48:09 -0500 Subject: [PATCH 3/6] add docs --- tree.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tree.go b/tree.go index fd36d07..5e01489 100644 --- a/tree.go +++ b/tree.go @@ -10,19 +10,26 @@ import ( // TreeConstructorFn creates a fresh Tree instance to be used as the Merkle inside of rsmt2d. type TreeConstructorFn = func() Tree +// Axis indicates whether a Column or Row is being used type Axis bool const ( + // Row indicates the use of the x axis of a data square Row Axis = true + + // Col indicates the use of the y axis of a data square Col Axis = false ) +// CellIndex contains all information needed to identify the cell that is being +// pushed type CellIndex struct { AxisIndex uint CellIndex uint Axis Axis } +// Tree wraps merkle tree implementations to work with rsmt2d type Tree interface { Push(data []byte, idx CellIndex) // TODO(ismail): is this general enough? From 640c3685641ca248a5e411a7e2f624b13f17a442 Mon Sep 17 00:00:00 2001 From: evan-forbes Date: Fri, 26 Mar 2021 14:00:18 -0500 Subject: [PATCH 4/6] remove axis as it's not needed --- datasquare.go | 8 ++++---- tree.go | 12 ------------ 2 files changed, 4 insertions(+), 16 deletions(-) diff --git a/datasquare.go b/datasquare.go index dd2ed20..7d9096d 100644 --- a/datasquare.go +++ b/datasquare.go @@ -167,7 +167,7 @@ func (ds *dataSquare) RowRoot(x uint) []byte { tree := ds.createTreeFn() for i, d := range ds.Row(x) { - tree.Push(d, CellIndex{Axis: Row, CellIndex: uint(i), AxisIndex: x}) + tree.Push(d, CellIndex{CellIndex: uint(i), AxisIndex: x}) } return tree.Root() @@ -191,7 +191,7 @@ func (ds *dataSquare) ColRoot(y uint) []byte { tree := ds.createTreeFn() for i, d := range ds.Column(y) { - tree.Push(d, CellIndex{Axis: Col, CellIndex: uint(i), AxisIndex: y}) + tree.Push(d, CellIndex{CellIndex: uint(i), AxisIndex: y}) } return tree.Root() @@ -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], CellIndex{Axis: Row, CellIndex: uint(i), AxisIndex: y}) + tree.Push(data[i], CellIndex{CellIndex: uint(i), AxisIndex: y}) } merkleRoot, proof, proofIndex, numLeaves := tree.Prove(int(y)) @@ -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], CellIndex{Axis: Col, CellIndex: uint(i), AxisIndex: y}) + tree.Push(data[i], CellIndex{CellIndex: uint(i), AxisIndex: y}) } // TODO(ismail): check for overflow when casting from uint -> int merkleRoot, proof, proofIndex, numLeaves := tree.Prove(int(x)) diff --git a/tree.go b/tree.go index 5e01489..67a1df0 100644 --- a/tree.go +++ b/tree.go @@ -10,23 +10,11 @@ import ( // TreeConstructorFn creates a fresh Tree instance to be used as the Merkle inside of rsmt2d. type TreeConstructorFn = func() Tree -// Axis indicates whether a Column or Row is being used -type Axis bool - -const ( - // Row indicates the use of the x axis of a data square - Row Axis = true - - // Col indicates the use of the y axis of a data square - Col Axis = false -) - // CellIndex contains all information needed to identify the cell that is being // pushed type CellIndex struct { AxisIndex uint CellIndex uint - Axis Axis } // Tree wraps merkle tree implementations to work with rsmt2d From ca5dd8e154f9d56ad9de440efa89578fe9061027 Mon Sep 17 00:00:00 2001 From: Evan Forbes <42654277+evan-forbes@users.noreply.github.com> Date: Fri, 26 Mar 2021 19:15:12 -0500 Subject: [PATCH 5/6] add prefix to indicate arg is unused Co-authored-by: Ismail Khoffi --- tree.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tree.go b/tree.go index 67a1df0..6d4476d 100644 --- a/tree.go +++ b/tree.go @@ -40,7 +40,7 @@ func NewDefaultTree() Tree { } } -func (d *DefaultTree) Push(data []byte, idx CellIndex) { +func (d *DefaultTree) Push(data []byte, _idx CellIndex) { // ignore the idx, as this implementation doesn't need that info d.leaves = append(d.leaves, data) } From 1f35c00fc7dbd9b0e5dbecb4001d9acefdf9f18c Mon Sep 17 00:00:00 2001 From: evan-forbes Date: Fri, 26 Mar 2021 19:30:02 -0500 Subject: [PATCH 6/6] change name of CellIndex to SquareIndex to avoid using the same name --- datasquare.go | 8 ++++---- tree.go | 11 +++++------ 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/datasquare.go b/datasquare.go index 7d9096d..c27f7cb 100644 --- a/datasquare.go +++ b/datasquare.go @@ -167,7 +167,7 @@ func (ds *dataSquare) RowRoot(x uint) []byte { tree := ds.createTreeFn() for i, d := range ds.Row(x) { - tree.Push(d, CellIndex{CellIndex: uint(i), AxisIndex: x}) + tree.Push(d, SquareIndex{Cell: uint(i), Axis: x}) } return tree.Root() @@ -191,7 +191,7 @@ func (ds *dataSquare) ColRoot(y uint) []byte { tree := ds.createTreeFn() for i, d := range ds.Column(y) { - tree.Push(d, CellIndex{CellIndex: uint(i), AxisIndex: y}) + tree.Push(d, SquareIndex{Axis: y, Cell: uint(i)}) } return tree.Root() @@ -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], CellIndex{CellIndex: uint(i), AxisIndex: y}) + tree.Push(data[i], SquareIndex{Axis: y, Cell: uint(i)}) } merkleRoot, proof, proofIndex, numLeaves := tree.Prove(int(y)) @@ -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], CellIndex{CellIndex: uint(i), AxisIndex: y}) + 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)) diff --git a/tree.go b/tree.go index 6d4476d..78542bd 100644 --- a/tree.go +++ b/tree.go @@ -10,16 +10,15 @@ import ( // TreeConstructorFn creates a fresh Tree instance to be used as the Merkle inside of rsmt2d. type TreeConstructorFn = func() Tree -// CellIndex contains all information needed to identify the cell that is being +// SquareIndex contains all information needed to identify the cell that is being // pushed -type CellIndex struct { - AxisIndex uint - CellIndex uint +type SquareIndex struct { + Axis, Cell uint } // Tree wraps merkle tree implementations to work with rsmt2d type Tree interface { - Push(data []byte, idx CellIndex) + Push(data []byte, idx SquareIndex) // TODO(ismail): is this general enough? Prove(idx int) (merkleRoot []byte, proofSet [][]byte, proofIndex uint64, numLeaves uint64) Root() []byte @@ -40,7 +39,7 @@ func NewDefaultTree() Tree { } } -func (d *DefaultTree) Push(data []byte, _idx CellIndex) { +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) }