Skip to content

Commit

Permalink
remove epoch from GetProofItems
Browse files Browse the repository at this point in the history
  • Loading branch information
weiihann committed Oct 30, 2024
1 parent 861826a commit e558f36
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 67 deletions.
2 changes: 1 addition & 1 deletion empty.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (Empty) Commitment() *Point {
return &id
}

func (Empty) GetProofItems(keylist, StateEpoch, NodeResolverFn) (*ProofElements, []byte, []Stem, error) {
func (Empty) GetProofItems(keylist, NodeResolverFn) (*ProofElements, []byte, []Stem, error) {
return nil, nil, nil, errors.New("trying to produce a commitment for an empty subtree")
}

Expand Down
2 changes: 1 addition & 1 deletion empty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestEmptyFuncs(t *testing.T) {
t.Fatal("commitment and commit mismatch")
}

if _, _, _, err := e.GetProofItems(nil, 0, nil); err == nil {
if _, _, _, err := e.GetProofItems(nil, nil); err == nil {
t.Fatal("get proof items should error")
}

Expand Down
2 changes: 1 addition & 1 deletion expired_leaf.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (n *ExpiredLeafNode) Commitment() *Point {
return n.commitment
}

func (n *ExpiredLeafNode) GetProofItems(keys keylist, curEpoch StateEpoch, resolver NodeResolverFn) (*ProofElements, []byte, []Stem, error) {
func (n *ExpiredLeafNode) GetProofItems(keys keylist, resolver NodeResolverFn) (*ProofElements, []byte, []Stem, error) {
var (
pe = &ProofElements{
Vals: make([][]byte, len(keys)),
Expand Down
2 changes: 1 addition & 1 deletion hashednode.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (HashedNode) Commitment() *Point {
panic("can not get commitment of a hash node")
}

func (HashedNode) GetProofItems(keylist, StateEpoch, NodeResolverFn) (*ProofElements, []byte, []Stem, error) {
func (HashedNode) GetProofItems(keylist, NodeResolverFn) (*ProofElements, []byte, []Stem, error) {
return nil, nil, nil, errors.New("can not get the full path, and there is no proof of absence")
}

Expand Down
2 changes: 1 addition & 1 deletion hashednode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestHashedNodeFuncs(t *testing.T) {
if v != nil {
t.Fatal("non-nil get from a hashed node")
}
if _, _, _, err := e.GetProofItems(nil, 0, nil); err == nil {
if _, _, _, err := e.GetProofItems(nil, nil); err == nil {
t.Fatal("got nil error when getting proof items from a hashed node")
}
if _, err := e.Serialize(); err != errSerializeHashedNode {
Expand Down
12 changes: 6 additions & 6 deletions proof_ipa.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,20 +188,20 @@ func (sd StateDiff) Equal(other StateDiff) error {

func GetCommitmentsForMultiproof(root VerkleNode, keys [][]byte, resolver NodeResolverFn) (*ProofElements, []byte, []Stem, error) {
sort.Sort(keylist(keys))
return root.GetProofItems(keylist(keys), curEpoch, resolver)
return root.GetProofItems(keylist(keys), resolver)
}

// getProofElementsFromTree factors the logic that is used both in the proving and verification methods. It takes a pre-state
// tree and an optional post-state tree, extracts the proof data from them and returns all the items required to build/verify
// a proof.
func getProofElementsFromTree(preroot, postroot VerkleNode, keys [][]byte, preEpoch, postEpoch StateEpoch, resolver NodeResolverFn) (*ProofElements, []byte, []Stem, [][]byte, error) {
func getProofElementsFromTree(preroot, postroot VerkleNode, keys [][]byte, postEpoch StateEpoch, resolver NodeResolverFn) (*ProofElements, []byte, []Stem, [][]byte, error) {
// go-ipa won't accept no key as an input, catch this corner case
// and return an empty result.
if len(keys) == 0 {
return nil, nil, nil, nil, errors.New("no key provided for proof")
}

pe, es, poas, err := GetCommitmentsForMultiproof(preroot, keys, preEpoch, resolver)
pe, es, poas, err := GetCommitmentsForMultiproof(preroot, keys, resolver)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("error getting pre-state proof data: %w", err)
}
Expand All @@ -228,8 +228,8 @@ func getProofElementsFromTree(preroot, postroot VerkleNode, keys [][]byte, preEp
return pe, es, poas, postvals, nil
}

func MakeVerkleMultiProof(preroot, postroot VerkleNode, keys [][]byte, preEpoch, postEpoch StateEpoch, resolver NodeResolverFn) (*Proof, []*Point, []byte, []*Fr, error) {
pe, es, poas, postvals, err := getProofElementsFromTree(preroot, postroot, keys, preEpoch, postEpoch, resolver)
func MakeVerkleMultiProof(preroot, postroot VerkleNode, keys [][]byte, postEpoch StateEpoch, resolver NodeResolverFn) (*Proof, []*Point, []byte, []*Fr, error) {
pe, es, poas, postvals, err := getProofElementsFromTree(preroot, postroot, keys, postEpoch, resolver)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("get commitments for multiproof: %s", err)
}
Expand Down Expand Up @@ -272,7 +272,7 @@ func MakeVerkleMultiProof(preroot, postroot VerkleNode, keys [][]byte, preEpoch,

// verifyVerkleProofWithPreState takes a proof and a trusted tree root and verifies that the proof is valid.
func verifyVerkleProofWithPreState(proof *Proof, preroot VerkleNode, preEpoch StateEpoch) error {
pe, _, _, _, err := getProofElementsFromTree(preroot, nil, proof.Keys, preEpoch, 0, nil)
pe, _, _, _, err := getProofElementsFromTree(preroot, nil, proof.Keys, 0, nil)
if err != nil {
return fmt.Errorf("error getting proof elements: %w", err)
}
Expand Down
62 changes: 31 additions & 31 deletions proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestProofEmptyTree(t *testing.T) {
root := New()
root.Commit()

proof, cis, zis, yis, _ := MakeVerkleMultiProof(root, nil, [][]byte{ffx32KeyTest}, 0, 0, nil)
proof, cis, zis, yis, _ := MakeVerkleMultiProof(root, nil, [][]byte{ffx32KeyTest}, 0, nil)
cfg := GetConfig()
if ok, err := verifyVerkleProof(proof, cis, zis, yis, cfg); !ok || err != nil {
t.Fatalf("could not verify verkle proof: %s", ToDot(root))
Expand All @@ -65,7 +65,7 @@ func TestProofVerifyTwoLeaves(t *testing.T) {
}
root.Commit()

proof, cis, zis, yis, _ := MakeVerkleMultiProof(root, nil, [][]byte{ffx32KeyTest}, 0, 0, nil)
proof, cis, zis, yis, _ := MakeVerkleMultiProof(root, nil, [][]byte{ffx32KeyTest}, 0, nil)

cfg := GetConfig()
if ok, err := verifyVerkleProof(proof, cis, zis, yis, cfg); !ok || err != nil {
Expand All @@ -92,7 +92,7 @@ func TestProofVerifyMultipleLeaves(t *testing.T) {
}
root.Commit()

proof, cis, zis, yis, _ := MakeVerkleMultiProof(root, nil, [][]byte{keys[0]}, 0, 0, nil)
proof, cis, zis, yis, _ := MakeVerkleMultiProof(root, nil, [][]byte{keys[0]}, 0, nil)

cfg := GetConfig()
if ok, err := verifyVerkleProof(proof, cis, zis, yis, cfg); !ok || err != nil {
Expand All @@ -119,9 +119,9 @@ func TestMultiProofVerifyMultipleLeaves(t *testing.T) {
}
root.Commit()

proof, _, _, _, _ := MakeVerkleMultiProof(root, nil, keys[0:2], 0, 0, nil)
proof, _, _, _, _ := MakeVerkleMultiProof(root, nil, keys[0:2], 0, nil)

pe, _, _, err := GetCommitmentsForMultiproof(root, keys[0:2], 0, nil)
pe, _, _, err := GetCommitmentsForMultiproof(root, keys[0:2], nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -159,9 +159,9 @@ func TestMultiProofVerifyMultipleLeavesWithAbsentStem(t *testing.T) {
absent[3] = 1 // and the stem differs
keys = append(keys, absent)

proof, _, _, _, _ := MakeVerkleMultiProof(root, nil, keys, 0, 0, nil)
proof, _, _, _, _ := MakeVerkleMultiProof(root, nil, keys, 0, nil)

pe, _, isabsent, err := GetCommitmentsForMultiproof(root, keys, 0, nil)
pe, _, isabsent, err := GetCommitmentsForMultiproof(root, keys, nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -193,9 +193,9 @@ func TestMultiProofVerifyMultipleLeavesCommitmentRedundancy(t *testing.T) {
}
root.Commit()

proof, _, _, _, _ := MakeVerkleMultiProof(root, nil, keys, 0, 0, nil)
proof, _, _, _, _ := MakeVerkleMultiProof(root, nil, keys, 0, nil)

pe, _, _, err := GetCommitmentsForMultiproof(root, keys, 0, nil)
pe, _, _, err := GetCommitmentsForMultiproof(root, keys, nil)
if err != nil {
t.Fatal(err)
}
Expand All @@ -217,7 +217,7 @@ func TestProofOfAbsenceInternalVerify(t *testing.T) {
}
root.Commit()

proof, cis, zis, yis, _ := MakeVerkleMultiProof(root, nil, [][]byte{ffx32KeyTest}, 0, 0, nil)
proof, cis, zis, yis, _ := MakeVerkleMultiProof(root, nil, [][]byte{ffx32KeyTest}, 0, nil)

cfg := GetConfig()
if ok, err := verifyVerkleProof(proof, cis, zis, yis, cfg); !ok || err != nil {
Expand All @@ -237,7 +237,7 @@ func TestProofOfAbsenceLeafVerify(t *testing.T) {
}
root.Commit()

proof, cis, zis, yis, _ := MakeVerkleMultiProof(root, nil, [][]byte{oneKeyTest}, 0, 0, nil)
proof, cis, zis, yis, _ := MakeVerkleMultiProof(root, nil, [][]byte{oneKeyTest}, 0, nil)

cfg := GetConfig()
if ok, err := verifyVerkleProof(proof, cis, zis, yis, cfg); !ok || err != nil {
Expand All @@ -262,7 +262,7 @@ func TestProofOfAbsenceLeafVerifyOtherSuffix(t *testing.T) {
return ret
}()

proof, cis, zis, yis, _ := MakeVerkleMultiProof(root, nil, [][]byte{key}, 0, 0, nil)
proof, cis, zis, yis, _ := MakeVerkleMultiProof(root, nil, [][]byte{key}, 0, nil)

cfg := GetConfig()
if ok, err := verifyVerkleProof(proof, cis, zis, yis, cfg); !ok || err != nil {
Expand All @@ -284,7 +284,7 @@ func TestProofOfAbsenceStemVerify(t *testing.T) {
}()

root.Commit()
proof, cis, zis, yis, _ := MakeVerkleMultiProof(root, nil, [][]byte{key}, 0, 0, nil)
proof, cis, zis, yis, _ := MakeVerkleMultiProof(root, nil, [][]byte{key}, 0, nil)

cfg := GetConfig()
if ok, err := verifyVerkleProof(proof, cis, zis, yis, cfg); !ok || err != nil {
Expand All @@ -310,7 +310,7 @@ func BenchmarkProofCalculation(b *testing.B) {
b.ReportAllocs()

for i := 0; i < b.N; i++ {
if _, _, _, _, err := MakeVerkleMultiProof(root, nil, [][]byte{keys[len(keys)/2]}, 0, 0, nil); err != nil {
if _, _, _, _, err := MakeVerkleMultiProof(root, nil, [][]byte{keys[len(keys)/2]}, 0, nil); err != nil {
b.Fatal(err)
}
}
Expand All @@ -331,7 +331,7 @@ func BenchmarkProofVerification(b *testing.B) {
}

root.Commit()
proof, cis, zis, yis, _ := MakeVerkleMultiProof(root, nil, [][]byte{keys[len(keys)/2]}, 0, 0, nil)
proof, cis, zis, yis, _ := MakeVerkleMultiProof(root, nil, [][]byte{keys[len(keys)/2]}, 0, nil)

b.ResetTimer()
b.ReportAllocs()
Expand Down Expand Up @@ -362,7 +362,7 @@ func TestProofSerializationNoAbsentStem(t *testing.T) {
}
}

proof, _, _, _, _ := MakeVerkleMultiProof(root, nil, [][]byte{keys[0]}, 0, 0, nil)
proof, _, _, _, _ := MakeVerkleMultiProof(root, nil, [][]byte{keys[0]}, 0, nil)

vp, statediff, err := SerializeProof(proof)
if err != nil {
Expand Down Expand Up @@ -401,7 +401,7 @@ func TestProofSerializationWithAbsentStem(t *testing.T) {
absentkey[2] = 2
absentkey[3] = 1

proof, _, _, _, _ := MakeVerkleMultiProof(root, nil, [][]byte{absentkey[:]}, 0, 0, nil)
proof, _, _, _, _ := MakeVerkleMultiProof(root, nil, [][]byte{absentkey[:]}, 0, nil)

vp, statediff, err := SerializeProof(proof)
if err != nil {
Expand Down Expand Up @@ -442,7 +442,7 @@ func TestProofDeserialize(t *testing.T) {
absentkey[2] = 2
absentkey[3] = 1

proof, _, _, _, _ := MakeVerkleMultiProof(root, nil, [][]byte{absentkey[:]}, 0, 0, nil)
proof, _, _, _, _ := MakeVerkleMultiProof(root, nil, [][]byte{absentkey[:]}, 0, nil)

vp, statediff, err := SerializeProof(proof)
if err != nil {
Expand All @@ -455,7 +455,7 @@ func TestProofDeserialize(t *testing.T) {
}
_ = deserialized

pe, _, _, err := root.GetProofItems(keylist{absentkey[:]}, 0, nil)
pe, _, _, err := root.GetProofItems(keylist{absentkey[:]}, nil)
if err != nil {
t.Fatal(err)
}
Expand All @@ -472,7 +472,7 @@ func TestProofOfAbsenceEdgeCase(t *testing.T) {
root.Commit()

ret, _ := hex.DecodeString("0303030303030303030303030303030303030303030303030303030303030303")
proof, cs, zis, yis, _ := MakeVerkleMultiProof(root, nil, [][]byte{ret}, 0, 0, nil)
proof, cs, zis, yis, _ := MakeVerkleMultiProof(root, nil, [][]byte{ret}, 0, nil)
cfg := GetConfig()
if ok, err := verifyVerkleProof(proof, cs, zis, yis, cfg); !ok || err != nil {
t.Fatal("could not verify proof")
Expand All @@ -493,7 +493,7 @@ func TestProofOfAbsenceOtherMultipleLeaves(t *testing.T) {

ret1, _ := hex.DecodeString("0303030303030303030303030303030303030303030303030303030303030300")
ret2, _ := hex.DecodeString("0303030303030303030303030303030303030303030303030303030303030301")
proof, cs, zis, yis, _ := MakeVerkleMultiProof(root, nil, [][]byte{ret1, ret2}, 0, 0, nil)
proof, cs, zis, yis, _ := MakeVerkleMultiProof(root, nil, [][]byte{ret1, ret2}, 0, nil)
cfg := GetConfig()
if ok, err := verifyVerkleProof(proof, cs, zis, yis, cfg); !ok || err != nil {
t.Fatal("could not verify proof")
Expand Down Expand Up @@ -555,7 +555,7 @@ func TestProofOfAbsenceNoneMultipleStems(t *testing.T) {

ret1, _ := hex.DecodeString("0303030303030303030303030303030303030303030303030303030303030300")
ret2, _ := hex.DecodeString("0303030303030303030303030303030303030303030303030303030303030200")
proof, cs, zis, yis, _ := MakeVerkleMultiProof(root, nil, [][]byte{ret1, ret2}, 0, 0, nil)
proof, cs, zis, yis, _ := MakeVerkleMultiProof(root, nil, [][]byte{ret1, ret2}, 0, nil)
cfg := GetConfig()
if ok, err := verifyVerkleProof(proof, cs, zis, yis, cfg); !ok || err != nil {
t.Fatal("could not verify proof")
Expand Down Expand Up @@ -862,7 +862,7 @@ func TestProofOfExpiryOneLeaf(t *testing.T) {
t.Fatalf("expected commitment to be %x, got %x", init, comm)
}

proof, cis, zis, yis, err := MakeVerkleMultiProof(root, nil, [][]byte{zeroKeyTest}, 0, 0, nil)
proof, cis, zis, yis, err := MakeVerkleMultiProof(root, nil, [][]byte{zeroKeyTest}, 0, nil)
if err != nil {
t.Fatalf("could not make verkle proof: %v", err)
}
Expand Down Expand Up @@ -914,7 +914,7 @@ func TestProofOfExpiryMultipleLeaves(t *testing.T) {
t.Fatalf("expected commitment to be %x, got %x", init, comm)
}

proof, cis, zis, yis, err := MakeVerkleMultiProof(root, nil, [][]byte{zeroKeyTest, ffx32KeyTest}, 0, 0, nil)
proof, cis, zis, yis, err := MakeVerkleMultiProof(root, nil, [][]byte{zeroKeyTest, ffx32KeyTest}, 0, nil)
if err != nil {
t.Fatalf("could not make verkle proof: %v", err)
}
Expand Down Expand Up @@ -970,7 +970,7 @@ func testSerializeDeserializeProof(t *testing.T, insertKVs map[string][]byte, pr
proveKVs[string(key)] = value
}

proof, _, _, _, _ := MakeVerkleMultiProof(root, nil, proveKeys, 0, 0, nil)
proof, _, _, _, _ := MakeVerkleMultiProof(root, nil, proveKeys, 0, nil)

serialized, statediff, err := SerializeProof(proof)
if err != nil {
Expand Down Expand Up @@ -1119,7 +1119,7 @@ func TestProofVerificationWithPostState(t *testing.T) { // skipcq: GO-R1005
}
postroot.Commit()

proof, _, _, _, _ := MakeVerkleMultiProof(root, postroot, data.keystoprove, 0, 0, nil)
proof, _, _, _, _ := MakeVerkleMultiProof(root, postroot, data.keystoprove, 0, nil)

keys:
for i := range proof.Keys {
Expand Down Expand Up @@ -1206,7 +1206,7 @@ func TestProofVerificationPreStateExpiredPostStateResurrected(t *testing.T) {
t.Fatalf("could not insert key: %v", err)
}

proof, _, _, _, _ := MakeVerkleMultiProof(preRoot, postRoot, keylist{zeroKeyTest}, preEpoch, postEpoch, nil)
proof, _, _, _, _ := MakeVerkleMultiProof(preRoot, postRoot, keylist{zeroKeyTest}, postEpoch, nil)

p, diff, err := SerializeProof(proof)
if err != nil {
Expand Down Expand Up @@ -1259,7 +1259,7 @@ func TestGenerateProofWithOnlyAbsentKeys(t *testing.T) {

// 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}, 0, 0, nil)
proof, cis, zis, yis, err := MakeVerkleMultiProof(root, nil, keylist{absentKey}, 0, nil)
if err != nil {
t.Fatal(err)
}
Expand All @@ -1284,7 +1284,7 @@ func TestGenerateProofWithOnlyAbsentKeys(t *testing.T) {
}

// From the rebuilt tree, validate the proof.
pe, _, _, err := GetCommitmentsForMultiproof(droot, keylist{absentKey}, 0, nil)
pe, _, _, err := GetCommitmentsForMultiproof(droot, keylist{absentKey}, nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1339,7 +1339,7 @@ func TestDoubleProofOfAbsence(t *testing.T) {
// in that leaf node. i.e: two proof of absence in the same leaf node with no proof of presence.
key2, _ := hex.DecodeString("0000000000000000000000000000000000000000000000000000000000000100")
key3, _ := hex.DecodeString("0000000000000000000000000000000000000000000000000000000000000200")
proof, _, _, _, _ := MakeVerkleMultiProof(root, nil, keylist{key2, key3}, 0, 0, nil)
proof, _, _, _, _ := MakeVerkleMultiProof(root, nil, keylist{key2, key3}, 0, nil)

serialized, statediff, err := SerializeProof(proof)
if err != nil {
Expand Down Expand Up @@ -1388,7 +1388,7 @@ func TestProveAbsenceInEmptyHalf(t *testing.T) {

key2, _ := hex.DecodeString("0000000000000000000000000000000000000000000000000000000000000100")
key3, _ := hex.DecodeString("0000000000000000000000000000000000000000000000000000000000000000")
proof, _, _, _, _ := MakeVerkleMultiProof(root, nil, keylist{key2, key3}, 0, 0, nil)
proof, _, _, _, _ := MakeVerkleMultiProof(root, nil, keylist{key2, key3}, 0, nil)

serialized, statediff, err := SerializeProof(proof)
if err != nil {
Expand Down
Loading

0 comments on commit e558f36

Please sign in to comment.