Skip to content

Commit 23faab4

Browse files
committed
Verify: only check post root if provided
Signed-off-by: Guillaume Ballet <[email protected]>
1 parent 9d9754f commit 23faab4

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

proof_ipa.go

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -638,41 +638,44 @@ func Verify(vp *VerkleProof, preStateRoot []byte, postStateRoot []byte, statedif
638638
if err != nil {
639639
return fmt.Errorf("error rebuilding the pre-tree from proof: %w", err)
640640
}
641-
// TODO this should not be necessary, remove it
642-
// after the new proof generation code has stabilized.
643-
for _, stemdiff := range statediff {
644-
for _, suffixdiff := range stemdiff.SuffixDiffs {
645-
var key [32]byte
646-
copy(key[:31], stemdiff.Stem[:])
647-
key[31] = suffixdiff.Suffix
648641

649-
val, err := pretree.Get(key[:], nil)
650-
if err != nil {
651-
return fmt.Errorf("could not find key %x in tree rebuilt from proof: %w", key, err)
652-
}
653-
if len(val) > 0 {
654-
if !bytes.Equal(val, suffixdiff.CurrentValue[:]) {
655-
return fmt.Errorf("could not find correct value at %x in tree rebuilt from proof: %x != %x", key, val, *suffixdiff.CurrentValue)
642+
if len(postStateRoot) != 0 {
643+
// TODO this should not be necessary, remove it
644+
// after the new proof generation code has stabilized.
645+
for _, stemdiff := range statediff {
646+
for _, suffixdiff := range stemdiff.SuffixDiffs {
647+
var key [32]byte
648+
copy(key[:31], stemdiff.Stem[:])
649+
key[31] = suffixdiff.Suffix
650+
651+
val, err := pretree.Get(key[:], nil)
652+
if err != nil {
653+
return fmt.Errorf("could not find key %x in tree rebuilt from proof: %w", key, err)
656654
}
657-
} else {
658-
if suffixdiff.CurrentValue != nil && len(suffixdiff.CurrentValue) != 0 {
659-
return fmt.Errorf("could not find correct value at %x in tree rebuilt from proof: %x != %x", key, val, *suffixdiff.CurrentValue)
655+
if len(val) > 0 {
656+
if !bytes.Equal(val, suffixdiff.CurrentValue[:]) {
657+
return fmt.Errorf("could not find correct value at %x in tree rebuilt from proof: %x != %x", key, val, *suffixdiff.CurrentValue)
658+
}
659+
} else {
660+
if suffixdiff.CurrentValue != nil && len(suffixdiff.CurrentValue) != 0 {
661+
return fmt.Errorf("could not find correct value at %x in tree rebuilt from proof: %x != %x", key, val, *suffixdiff.CurrentValue)
662+
}
660663
}
661664
}
662665
}
663-
}
664666

665-
// TODO: this is necessary to verify that the post-values are the correct ones.
666-
// But all this can be avoided with a even faster way. The EVM block execution can
667-
// keep track of the written keys, and compare that list with this post-values list.
668-
// This can avoid regenerating the post-tree which is somewhat expensive.
669-
posttree, err := PostStateTreeFromStateDiff(pretree, statediff)
670-
if err != nil {
671-
return fmt.Errorf("error rebuilding the post-tree from proof: %w", err)
672-
}
673-
regeneratedPostTreeRoot := posttree.Commitment().Bytes()
674-
if !bytes.Equal(regeneratedPostTreeRoot[:], postStateRoot) {
675-
return fmt.Errorf("post tree root mismatch: %x != %x", regeneratedPostTreeRoot, postStateRoot)
667+
// TODO: this is necessary to verify that the post-values are the correct ones.
668+
// But all this can be avoided with a even faster way. The EVM block execution can
669+
// keep track of the written keys, and compare that list with this post-values list.
670+
// This can avoid regenerating the post-tree which is somewhat expensive.
671+
posttree, err := PostStateTreeFromStateDiff(pretree, statediff)
672+
if err != nil {
673+
return fmt.Errorf("error rebuilding the post-tree from proof: %w", err)
674+
}
675+
regeneratedPostTreeRoot := posttree.Commitment().Bytes()
676+
if !bytes.Equal(regeneratedPostTreeRoot[:], postStateRoot) {
677+
return fmt.Errorf("post tree root mismatch: %x != %x", regeneratedPostTreeRoot, postStateRoot)
678+
}
676679
}
677680

678681
return verifyVerkleProofWithPreState(proof, pretree)

0 commit comments

Comments
 (0)