Skip to content

Commit

Permalink
Fix oldLeafKeyFull size at method tree.down
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaucube committed Oct 5, 2021
1 parent 30d8b42 commit e8404e1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
12 changes: 5 additions & 7 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,12 +405,10 @@ func (t *Tree) down(rTx db.ReadTx, newKey, currKey []byte, siblings [][]byte,
return nil, nil, nil, ErrKeyAlreadyExists
}

oldLeafKeyFull := make([]byte, t.hashFunction.Len())
// if len(oldLeafKey) > t.hashFunction.Len() { // WIP
// return nil, nil, nil,
// fmt.Errorf("len(oldLeafKey) > hashFunction.Len()")
// }
copy(oldLeafKeyFull[:], oldLeafKey)
oldLeafKeyFull, err := keyPathFromKey(t.maxLevels, oldLeafKey)
if err != nil {
return nil, nil, nil, err
}

// if currKey is already used, go down until paths diverge
oldPath := getPath(t.maxLevels, oldLeafKeyFull)
Expand Down Expand Up @@ -841,7 +839,7 @@ func CheckProof(hashFunc HashFunction, k, v, root, packedSiblings []byte) (bool,
return false, err
}

keyPath := make([]byte, len(siblings))
keyPath := make([]byte, int(math.Ceil(float64(len(siblings))/float64(8)))) //nolint:gomnd
copy(keyPath[:], k)

key, _, err := newLeafValue(hashFunc, k, v)
Expand Down
24 changes: 24 additions & 0 deletions tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,30 @@ func TestKeyLen(t *testing.T) {
c.Assert(verif, qt.IsFalse)
}

func TestKeyLenBiggerThan32(t *testing.T) {
c := qt.New(t)
maxLevels := 264
database, err := badgerdb.New(badgerdb.Options{Path: c.TempDir()})
c.Assert(err, qt.IsNil)
tree, err := NewTree(database, maxLevels, HashFunctionBlake2b)
c.Assert(err, qt.IsNil)

bLen := 33
err = tree.Add(
randomBytes(bLen),
randomBytes(bLen))
c.Assert(err, qt.IsNil)

// 2nd key that we add, will find a node with len(key)==32 (due the
// hash output size, expect that next Add does not give any error, as
// internally it will use a keyPath of size corresponent to the
// maxLevels size of the tree
err = tree.Add(
randomBytes(bLen),
randomBytes(bLen))
c.Assert(err, qt.IsNil)
}

func BenchmarkAdd(b *testing.B) {
bLen := 32 // for both Poseidon & Sha256
// prepare inputs
Expand Down

0 comments on commit e8404e1

Please sign in to comment.