Skip to content

Commit

Permalink
implements an optimization technique
Browse files Browse the repository at this point in the history
  • Loading branch information
staheri14 committed May 10, 2024
1 parent 749d39b commit 1c5308b
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

// ErrFailedCompletenessCheck indicates that the verification of a namespace proof failed due to the lack of completeness property.
var ErrFailedCompletenessCheck = errors.New("failed completeness check")
var ErrWrongLeafHashesSize = errors.New("wrong leafHashes size")

// Proof represents a namespace proof of a namespace.ID in an NMT. In case this
// proof proves the absence of a namespace.ID in a tree it also contains the
Expand Down Expand Up @@ -260,6 +261,15 @@ func (proof Proof) VerifyLeafHashes(nth *NmtHasher, verifyCompleteness bool, nID
return false, fmt.Errorf("proof range [proof.start=%d, proof.end=%d) is not valid: %w", proof.Start(), proof.End(), ErrInvalidRange)
}

// check whether the number of leaves match the proof range i.e., end-start.
// If not, make an early return.
expectedLeafHashesCount := proof.End() - proof.Start()
if len(leafHashes) != expectedLeafHashesCount {
return false, fmt.Errorf(
"supplied leafHashes size %d, expected size %d: %w",
len(leafHashes), expectedLeafHashesCount, ErrWrongLeafHashesSize)
}

// perform some consistency checks:
if nID.Size() != nth.NamespaceSize() {
return false, fmt.Errorf("namespace ID size (%d) does not match the namespace size of the NMT hasher (%d)", nID.Size(), nth.NamespaceSize())
Expand Down Expand Up @@ -411,6 +421,13 @@ func (proof Proof) VerifyInclusion(h hash.Hash, nid namespace.ID, leavesWithoutN
return false
}

// check whether the number of leavesWithoutNamespace match the proof range i.e., end-start.
// If not, make an early return.
expectedLeavesCount := proof.End() - proof.Start()
if len(leavesWithoutNamespace) != expectedLeavesCount {
return false
}

nth := NewNmtHasher(h, nid.Size(), proof.isMaxNamespaceIDIgnored)

// perform some consistency checks:
Expand Down

0 comments on commit 1c5308b

Please sign in to comment.