Skip to content

Commit

Permalink
[Misc] Integration fixes after the v0.11.0 release (#49)
Browse files Browse the repository at this point in the history
- Exposing `NewTrieSpec` publically
- Adding back `Sum` function to the `MerkleRoot` type
- Adding a new `Count` function to the `MerkleRoot` type
- Adding back `PathHasherSize`

---

Signed-off-by: Daniel Olshansky <[email protected]>
Co-authored-by: Redouane Lakrache <[email protected]>
  • Loading branch information
Olshansk and red-0ne committed Jun 14, 2024
1 parent 0f26ad0 commit ea585c6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
39 changes: 39 additions & 0 deletions root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package smt

import "encoding/binary"

const (
// These are intentionally exposed to allow for for testing and custom
// implementations of downstream applications.
SmtRootSizeBytes = 32
SmstRootSizeBytes = SmtRootSizeBytes + sumSizeBytes + countSizeBytes
)

// Sum returns the uint64 sum of the merkle root, it checks the length of the
// merkle root and if it is no the same as the size of the SMST's expected
// root hash it will panic.
func (r MerkleRoot) Sum() uint64 {
if len(r)%SmtRootSizeBytes == 0 {
panic("root#sum: not a merkle sum trie")
}

firstSumByteIdx, firstCountByteIdx := getFirstMetaByteIdx([]byte(r))

var sumBz [sumSizeBytes]byte
copy(sumBz[:], []byte(r)[firstSumByteIdx:firstCountByteIdx])
return binary.BigEndian.Uint64(sumBz[:])
}

// Count returns the uint64 count of the merkle root, a cryptographically secure
// count of the number of non-empty leafs in the tree.
func (r MerkleRoot) Count() uint64 {
if len(r)%SmtRootSizeBytes == 0 {
panic("root#sum: not a merkle sum trie")
}

_, firstCountByteIdx := getFirstMetaByteIdx([]byte(r))

var countBz [countSizeBytes]byte
copy(countBz[:], []byte(r)[firstCountByteIdx:])
return binary.BigEndian.Uint64(countBz[:])
}
2 changes: 1 addition & 1 deletion smst.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewSparseMerkleSumTrie(
hasher hash.Hash,
options ...TrieSpecOption,
) *SMST {
trieSpec := newTrieSpec(hasher, true)
trieSpec := NewTrieSpec(hasher, true)
for _, option := range options {
option(&trieSpec)
}
Expand Down
2 changes: 1 addition & 1 deletion smt.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func NewSparseMerkleTrie(
options ...TrieSpecOption,
) *SMT {
smt := SMT{
TrieSpec: newTrieSpec(hasher, false),
TrieSpec: NewTrieSpec(hasher, false),
nodes: nodes,
}
for _, option := range options {
Expand Down
17 changes: 15 additions & 2 deletions trie_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,21 @@ type TrieSpec struct {
sumTrie bool
}

// newTrieSpec returns a new TrieSpec with the given hasher and sumTrie flag
func newTrieSpec(hasher hash.Hash, sumTrie bool) TrieSpec {
// NewTrieSpec returns a new TrieSpec with the given hasher and sumTrie flag
func NewTrieSpec(
hasher hash.Hash,
sumTrie bool,
opts ...TrieSpecOption,
) TrieSpec {
spec := TrieSpec{th: *NewTrieHasher(hasher)}
spec.ph = &pathHasher{spec.th}
spec.vh = &valueHasher{spec.th}
spec.sumTrie = sumTrie

for _, opt := range opts {
opt(&spec)
}

return spec
}

Expand All @@ -28,6 +37,10 @@ func (spec *TrieSpec) Spec() *TrieSpec {
return spec
}

// PathHasherSize returns the length (in bytes) of digests produced by the
// path hasher
func (spec *TrieSpec) PathHasherSize() int { return spec.ph.PathSize() }

// placeholder returns the default placeholder value depending on the trie type
func (spec *TrieSpec) placeholder() []byte {
if spec.sumTrie {
Expand Down

0 comments on commit ea585c6

Please sign in to comment.