diff --git a/proof_ipa.go b/proof_ipa.go index 15d4e8a..a4a27b0 100644 --- a/proof_ipa.go +++ b/proof_ipa.go @@ -638,41 +638,44 @@ func Verify(vp *VerkleProof, preStateRoot []byte, postStateRoot []byte, statedif if err != nil { return fmt.Errorf("error rebuilding the pre-tree from proof: %w", err) } - // TODO this should not be necessary, remove it - // after the new proof generation code has stabilized. - for _, stemdiff := range statediff { - for _, suffixdiff := range stemdiff.SuffixDiffs { - var key [32]byte - copy(key[:31], stemdiff.Stem[:]) - key[31] = suffixdiff.Suffix - val, err := pretree.Get(key[:], nil) - if err != nil { - return fmt.Errorf("could not find key %x in tree rebuilt from proof: %w", key, err) - } - if len(val) > 0 { - if !bytes.Equal(val, suffixdiff.CurrentValue[:]) { - return fmt.Errorf("could not find correct value at %x in tree rebuilt from proof: %x != %x", key, val, *suffixdiff.CurrentValue) + if len(postStateRoot) != 0 { + // TODO this should not be necessary, remove it + // after the new proof generation code has stabilized. + for _, stemdiff := range statediff { + for _, suffixdiff := range stemdiff.SuffixDiffs { + var key [32]byte + copy(key[:31], stemdiff.Stem[:]) + key[31] = suffixdiff.Suffix + + val, err := pretree.Get(key[:], nil) + if err != nil { + return fmt.Errorf("could not find key %x in tree rebuilt from proof: %w", key, err) } - } else { - if suffixdiff.CurrentValue != nil && len(suffixdiff.CurrentValue) != 0 { - return fmt.Errorf("could not find correct value at %x in tree rebuilt from proof: %x != %x", key, val, *suffixdiff.CurrentValue) + if len(val) > 0 { + if !bytes.Equal(val, suffixdiff.CurrentValue[:]) { + return fmt.Errorf("could not find correct value at %x in tree rebuilt from proof: %x != %x", key, val, *suffixdiff.CurrentValue) + } + } else { + if suffixdiff.CurrentValue != nil && len(suffixdiff.CurrentValue) != 0 { + return fmt.Errorf("could not find correct value at %x in tree rebuilt from proof: %x != %x", key, val, *suffixdiff.CurrentValue) + } } } } - } - // TODO: this is necessary to verify that the post-values are the correct ones. - // But all this can be avoided with a even faster way. The EVM block execution can - // keep track of the written keys, and compare that list with this post-values list. - // This can avoid regenerating the post-tree which is somewhat expensive. - posttree, err := PostStateTreeFromStateDiff(pretree, statediff) - if err != nil { - return fmt.Errorf("error rebuilding the post-tree from proof: %w", err) - } - regeneratedPostTreeRoot := posttree.Commitment().Bytes() - if !bytes.Equal(regeneratedPostTreeRoot[:], postStateRoot) { - return fmt.Errorf("post tree root mismatch: %x != %x", regeneratedPostTreeRoot, postStateRoot) + // TODO: this is necessary to verify that the post-values are the correct ones. + // But all this can be avoided with a even faster way. The EVM block execution can + // keep track of the written keys, and compare that list with this post-values list. + // This can avoid regenerating the post-tree which is somewhat expensive. + posttree, err := PostStateTreeFromStateDiff(pretree, statediff) + if err != nil { + return fmt.Errorf("error rebuilding the post-tree from proof: %w", err) + } + regeneratedPostTreeRoot := posttree.Commitment().Bytes() + if !bytes.Equal(regeneratedPostTreeRoot[:], postStateRoot) { + return fmt.Errorf("post tree root mismatch: %x != %x", regeneratedPostTreeRoot, postStateRoot) + } } return verifyVerkleProofWithPreState(proof, pretree)