Skip to content

Commit

Permalink
feat: export hasher sizes from the trie spec and enforce path length …
Browse files Browse the repository at this point in the history
…in closest proof method and verification
  • Loading branch information
h5law committed Mar 20, 2024
1 parent 6f0f1e6 commit cbdf0b9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
5 changes: 5 additions & 0 deletions proofs.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ func (proof *SparseMerkleClosestProof) GetValueHash(spec *TrieSpec) []byte {
}

func (proof *SparseMerkleClosestProof) validateBasic(spec *TrieSpec) error {
// ensure the path used in the proof is within the path size
if len(proof.Path) > spec.ph.PathSize() {
return fmt.Errorf("invalid path length: got %d, max is %d", len(proof.Path), spec.ph.PathSize())
}

// ensure the depth of the leaf node being proven is within the path size
if proof.Depth < 0 || proof.Depth > spec.ph.PathSize()*8 {
return fmt.Errorf("invalid depth: got %d, outside of [0, %d]", proof.Depth, spec.ph.PathSize()*8)
Expand Down
9 changes: 9 additions & 0 deletions smt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package smt

import (
"bytes"
"fmt"
"hash"

"github.com/pokt-network/smt/kvstore"
Expand Down Expand Up @@ -424,6 +425,14 @@ func (smt *SMT) ProveClosest(path []byte) (
proof *SparseMerkleClosestProof, // proof of the key-value pair found
err error, // the error value encountered
) {
// ensure the path used in the proof is within the path size
if len(proof.Path) > smt.Spec().PathHasherSize() {
return nil, fmt.Errorf(
"invalid path length: got %d, max is %d",
len(proof.Path), smt.Spec().PathHasherSize(),
)
}

workingPath := make([]byte, len(path))
copy(workingPath, path)
var siblings []trieNode
Expand Down
13 changes: 11 additions & 2 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ type SparseMerkleSumTrie interface {
// leaf paths and stored values, and the corresponding maximum trie depth.
type TrieSpec struct {
th trieHasher
ph PathHasher
vh ValueHasher
ph *pathHasher
vh *valueHasher
sumTrie bool
}

Expand All @@ -98,6 +98,15 @@ func newTrieSpec(hasher hash.Hash, sumTrie bool) TrieSpec {
// Spec returns the TrieSpec associated with the given trie
func (spec *TrieSpec) Spec() *TrieSpec { return spec }

// PathHasherSize returns the size of the path hasher
func (spec *TrieSpec) PathHasherSize() int { return spec.ph.PathSize() }

// ValueHasherSize returns the size of the value hasher
func (spec *TrieSpec) ValueHasherSize() int { return spec.th.hashSize() }

// TrieHasherSize returns the size of the trie hasher
func (spec *TrieSpec) TrieHasherSize() int { return spec.th.hasher.Size() }

func (spec *TrieSpec) depth() int { return spec.ph.PathSize() * 8 }
func (spec *TrieSpec) digestValue(data []byte) []byte {
if spec.vh == nil {
Expand Down

0 comments on commit cbdf0b9

Please sign in to comment.