Skip to content

Commit

Permalink
test: gen proof with only absent keys
Browse files Browse the repository at this point in the history
Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
  • Loading branch information
jsign committed Oct 15, 2023
1 parent 0a4e93e commit 0d744bd
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1649,3 +1649,47 @@ 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")
}
}

0 comments on commit 0d744bd

Please sign in to comment.