diff --git a/go/ics23.go b/go/ics23.go index db12975..51f2323 100644 --- a/go/ics23.go +++ b/go/ics23.go @@ -23,25 +23,23 @@ and determine neighbors */ package ics23 -import ( - "bytes" -) - // CommitmentRoot is a byte slice that represents the merkle root of a tree that can be used to validate proofs type CommitmentRoot []byte // VerifyMembership returns true iff -// proof is (contains) an ExistenceProof for the given key and value AND -// calculating the root for the ExistenceProof matches the provided CommitmentRoot +// proof is an ExistenceProof for the given key and value AND +// calculating the root for the ExistenceProof matches the provided CommitmentRoot. func VerifyMembership(spec *ProofSpec, root CommitmentRoot, proof *CommitmentProof, key []byte, value []byte) bool { - // decompress it before running code (no-op if not compressed) - proof = Decompress(proof) - ep := getExistProofForKey(proof, key) + if proof == nil { + return false + } + + ep := proof.GetExist() if ep == nil { return false } - err := ep.Verify(spec, root, key, value) - return err == nil + + return ep.Verify(spec, root, key, value) == nil } // VerifyNonMembership returns true iff @@ -50,58 +48,14 @@ func VerifyMembership(spec *ProofSpec, root CommitmentRoot, proof *CommitmentPro // left and right proofs are neighbors (or left/right most if one is nil) // provided key is between the keys of the two proofs func VerifyNonMembership(spec *ProofSpec, root CommitmentRoot, proof *CommitmentProof, key []byte) bool { - // decompress it before running code (no-op if not compressed) - proof = Decompress(proof) - np := getNonExistProofForKey(spec, proof, key) - if np == nil { - return false - } - err := np.Verify(spec, root, key) - return err == nil -} - -func getExistProofForKey(proof *CommitmentProof, key []byte) *ExistenceProof { if proof == nil { - return nil - } - - switch p := proof.Proof.(type) { - case *CommitmentProof_Exist: - ep := p.Exist - if bytes.Equal(ep.Key, key) { - return ep - } - case *CommitmentProof_Batch: - for _, sub := range p.Batch.Entries { - if ep := sub.GetExist(); ep != nil && bytes.Equal(ep.Key, key) { - return ep - } - } + return false } - return nil -} -func getNonExistProofForKey(spec *ProofSpec, proof *CommitmentProof, key []byte) *NonExistenceProof { - switch p := proof.Proof.(type) { - case *CommitmentProof_Nonexist: - np := p.Nonexist - if isLeft(spec, np.Left, key) && isRight(spec, np.Right, key) { - return np - } - case *CommitmentProof_Batch: - for _, sub := range p.Batch.Entries { - if np := sub.GetNonexist(); np != nil && isLeft(spec, np.Left, key) && isRight(spec, np.Right, key) { - return np - } - } + np := proof.GetNonexist() + if np == nil { + return false } - return nil -} - -func isLeft(spec *ProofSpec, left *ExistenceProof, key []byte) bool { - return left == nil || bytes.Compare(keyForComparison(spec, left.Key), keyForComparison(spec, key)) < 0 -} -func isRight(spec *ProofSpec, right *ExistenceProof, key []byte) bool { - return right == nil || bytes.Compare(keyForComparison(spec, right.Key), keyForComparison(spec, key)) > 0 + return np.Verify(spec, root, key) == nil } diff --git a/go/vectors_test.go b/go/vectors_test.go index 9b6282a..425f9b5 100644 --- a/go/vectors_test.go +++ b/go/vectors_test.go @@ -13,6 +13,7 @@ func TestVectors(t *testing.T) { name := fmt.Sprintf("%s/%s", tc.Dir, tc.Filename) t.Run(name, func(t *testing.T) { proof, ref := LoadFile(t, tc.Dir, tc.Filename) + // Test Calculate method calculatedRoot, err := proof.Calculate() if err != nil { @@ -26,12 +27,12 @@ func TestVectors(t *testing.T) { // non-existence valid := VerifyNonMembership(tc.Spec, ref.RootHash, proof, ref.Key) if !valid { - t.Fatal("Invalid proof") + t.Fatalf("Invalid proof: %v", err) } } else { valid := VerifyMembership(tc.Spec, ref.RootHash, proof, ref.Key, ref.Value) if !valid { - t.Fatal("Invalid proof") + t.Fatalf("Invalid proof: %v", err) } } })