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

smt: minor improvements #376

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion prover/crypto/state-management/smt/smt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func TestBuildFromScratch(t *testing.T) {
leaves[i] = Bytes32(leavesFr[i].Bytes())
}

// And generate the
// And generate the tree with those leaves
tree := smt.BuildComplete(leaves, config.HashFunc)

// Test-Merkle tests the merkle proof point by point
Expand Down
12 changes: 9 additions & 3 deletions prover/crypto/state-management/smt/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Tree struct {
// OccupiedLeaves continuously list of the occupied leaves. For the toy
// implementation we track all the leaves.
OccupiedLeaves []types.Bytes32
// Occupied not stores all the node with a non-trivial value in the tree.
// OccupiedNodes stores all the nodes with a non-trivial value in the tree.
//
// Does not include the root. (So there are 39 levels and not 40).
// Returns a node at a given level:
Expand All @@ -39,7 +39,7 @@ type Tree struct {
// It does not include the "empty root" nor the empty leaf
// so the first position contains the empty node for the level one.
// So there are 39, and not 40 levels. That way, the indexing stays
// consistent with "OccupiedNode"
// consistent with "OccupiedNodes"
EmptyNodes []types.Bytes32
}

Expand Down Expand Up @@ -117,6 +117,9 @@ func (t *Tree) MustGetLeaf(pos int) types.Bytes32 {
//
// (for config.Depth == 40)
func (t *Tree) getNode(level, posInLevel int) types.Bytes32 {
if posInLevel < 0 {
utils.Panic("negative position in level: %v", posInLevel)
}
switch {
case level == t.Config.Depth:
// The only logical posInLevels value is zero in this case
Expand Down Expand Up @@ -164,6 +167,9 @@ func (t *Tree) getNode(level, posInLevel int) types.Bytes32 {
//
// (for config.Depth == 40)
func (t *Tree) updateNode(level, posInLevel int, newVal types.Bytes32) {
if posInLevel < 0 {
utils.Panic("negative position in level: %v", posInLevel)
}
switch {
case level == t.Config.Depth:
// The only logical posInLevels value is zero in this case
Expand Down Expand Up @@ -268,7 +274,7 @@ func BuildComplete(leaves []types.Bytes32, hashFunc func() hashtypes.Hasher) *Tr
numLeaves := len(leaves)

// Sanity check : there should be a power of two number of leaves
if !utils.IsPowerOfTwo(numLeaves) || numLeaves == 0 {
if !utils.IsPowerOfTwo(numLeaves) {
AlexandreBelling marked this conversation as resolved.
Show resolved Hide resolved
utils.Panic("expected power of two number of leaves, got %v", numLeaves)
}

Expand Down
2 changes: 1 addition & 1 deletion prover/crypto/state-management/smt/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func (t *Tree) Update(pos int, newVal types.Bytes32) {
current := newVal
idx := pos

if pos >= 1<<depth {
if pos <= 0 || pos >= 1<<depth {
jsign marked this conversation as resolved.
Show resolved Hide resolved
utils.Panic("out of bound %v", pos)
}

Expand Down
Loading