diff --git a/proof_test.go b/proof_test.go index a9a88ce6..8860b620 100644 --- a/proof_test.go +++ b/proof_test.go @@ -942,3 +942,52 @@ func TestProofOfAbsenceBorderCase(t *testing.T) { t.Fatal("differing commitment for child #0") } } + +func TestGenerateProofWithOnlyAbsentKeys(t *testing.T) { + t.Parallel() + + // Create a tree with only one key. + root := New() + presentKey, _ := hex.DecodeString("4000000000000000000000000000000000000000000000000000000000000000") + if err := root.Insert(presentKey, zeroKeyTest, nil); err != nil { + t.Fatalf("inserting into the original failed: %v", err) + } + root.Commit() + + // Create a proof with a key with the same first byte, but different second byte (i.e: absent). + absentKey, _ := hex.DecodeString("4010000000000000000000000000000000000000000000000000000000000000") + proof, cis, zis, yis, err := MakeVerkleMultiProof(root, nil, keylist{absentKey}, nil) + if err != nil { + t.Fatal(err) + } + + // It must pass. + if ok, err := VerifyVerkleProof(proof, cis, zis, yis, cfg); !ok || err != nil { + t.Fatalf("original proof didn't verify: %v", err) + } + + // Serialize + Deserialize + build tree from proof. + vp, statediff, err := SerializeProof(proof) + if err != nil { + t.Fatal(err) + } + dproof, err := DeserializeProof(vp, statediff) + if err != nil { + t.Fatal(err) + } + droot, err := PreStateTreeFromProof(dproof, root.Commit()) + if err != nil { + t.Fatal(err) + } + + // From the rebuilt tree, validate the proof. + pe, _, _, err := GetCommitmentsForMultiproof(droot, keylist{absentKey}, nil) + if err != nil { + t.Fatal(err) + } + + // It must pass. + if ok, err := VerifyVerkleProof(dproof, pe.Cis, pe.Zis, pe.Yis, cfg); !ok || err != nil { + t.Fatalf("reconstructed proof didn't verify: %v", err) + } +} diff --git a/tree_test.go b/tree_test.go index 0536e541..2cab2826 100644 --- a/tree_test.go +++ b/tree_test.go @@ -1649,47 +1649,3 @@ func TestLeafNodeInsert(t *testing.T) { t.Fatalf("hash should not be nil") } } - -func TestGenerateProofWithOnlyAbsentKeys(t *testing.T) { - t.Parallel() - - // Create a tree with only one key. - root := New() - presentKey, _ := hex.DecodeString("4000000000000000000000000000000000000000000000000000000000000000") - if err := root.Insert(presentKey, zeroKeyTest, nil); err != nil { - t.Fatalf("inserting into the original failed: %v", err) - } - root.Commit() - - // Create a proof with a key with the same first byte, but different second byte (i.e: absent). - absentKey, _ := hex.DecodeString("4010000000000000000000000000000000000000000000000000000000000000") - proof, _, _, _, err := MakeVerkleMultiProof(root, nil, keylist{absentKey}, nil) - if err != nil { - t.Fatal(err) - } - - // Serialize + Deserialize + build tree from proof. - vp, statediff, err := SerializeProof(proof) - if err != nil { - t.Fatal(err) - } - dproof, err := DeserializeProof(vp, statediff) - if err != nil { - t.Fatal(err) - } - droot, err := PreStateTreeFromProof(dproof, root.Commit()) - if err != nil { - t.Fatal(err) - } - - // From the rebuilt tree, validate the proof. - pe, _, _, err := GetCommitmentsForMultiproof(droot, keylist{absentKey}, nil) - if err != nil { - t.Fatal(err) - } - - // It must pass. - if ok, err := VerifyVerkleProof(proof, pe.Cis, pe.Zis, pe.Yis, cfg); !ok || err != nil { - t.Fatal("proof didn't verify") - } -}