-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: VerifyMembership and VerifyNonMembership api #391
Merged
colin-axner
merged 12 commits into
master
from
colin/update-verify-membership-and-nonmembership
Oct 23, 2024
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d3b71c4
refactor: remove and
colin-axner a1082ee
chore: CHANGELOG
colin-axner 300714b
lint
colin-axner ebbb3ba
chore: changelog
colin-axner fd4a2a1
refactor: remove CombineProofs
colin-axner c57484e
refactor: VerifyMembership and VerifyNonMembership api
colin-axner 9188852
refactor: reset api
colin-axner 718e9e2
Update go/ics23.go
colin-axner c08b737
refactor: remove unnecessary code
colin-axner 6896086
Merge branch 'colin/update-verify-membership-and-nonmembership' of gi…
colin-axner 28aa5da
Merge branch 'master' of github.com:cosmos/ics23 into colin/update-ve…
colin-axner 2f768be
lint: remove isLeft and isRight as they are unused now
colin-axner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. checked in |
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
checked in
ep.Verify