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

[Misc] Integration fixes after the v0.11.0 release #49

Merged
merged 7 commits into from
Jun 14, 2024
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
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
Loading