Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

sanityCheck: only necessarily calculate siblingHash if len(proof.SideNodes > 0 but also return early and unnest #56

Closed
odeke-em opened this issue Aug 15, 2021 · 0 comments · Fixed by #57

Comments

@odeke-em
Copy link
Contributor

If we look at the code in here

smt/proofs.go

Lines 44 to 54 in a99c0f5

// Check that the sibling data hashes to the first side node if not nil
if proof.SiblingData != nil {
siblingHash := th.digest(proof.SiblingData)
if proof.SideNodes != nil && len(proof.SideNodes) > 0 {
if !bytes.Equal(proof.SideNodes[0], siblingHash) {
return false
}
}
}
return true

Notice

  • We are calculating a cryptographic hash even if len(proof.SideNodes) conditions could cause a return

    smt/proofs.go

    Lines 46 to 48 in a99c0f5

    siblingHash := th.digest(proof.SiblingData)
    if proof.SideNodes != nil && len(proof.SideNodes) > 0 {
    if !bytes.Equal(proof.SideNodes[0], siblingHash) {
  • Those nested conditions could be further simplified from

Before

smt/proofs.go

Lines 45 to 52 in a99c0f5

if proof.SiblingData != nil {
siblingHash := th.digest(proof.SiblingData)
if proof.SideNodes != nil && len(proof.SideNodes) > 0 {
if !bytes.Equal(proof.SideNodes[0], siblingHash) {
return false
}
}
}

After

        // Check that the sibling data hashes to the first side node if not nil
        if proof.SiblingData == nil {
                return true
        }
        
        if len(proof.SideNodes) == 0 {
                return true
        }
        
        siblingHash := th.digest(proof.SiblingData)
        return bytes.Equal(proof.SideNodes[0], siblingHash)
odeke-em added a commit to orijtech/smt that referenced this issue Aug 15, 2021
…duce nesting

Only calculate siblingHash to avoid wasting cycles when unneeded.
Also while here, reverse conditional the code to reduce nesting and
to make reading much simpler.

Fixes celestiaorg#56
odeke-em added a commit to orijtech/smt that referenced this issue Aug 15, 2021
…duce nesting

Only calculate siblingHash to avoid wasting cycles when unneeded.
Also while here, reverse conditional the code to reduce nesting and
to make reading much simpler.

Fixes celestiaorg#56
adlerjohn pushed a commit that referenced this issue Aug 15, 2021
…duce nesting (#57)

Only calculate siblingHash to avoid wasting cycles when unneeded.
Also while here, reverse conditional the code to reduce nesting and
to make reading much simpler.

Fixes #56
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant