Skip to content
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

feat: Add prehash_compare_key to allow proving nonexistence in sparse trees #136

Merged
merged 14 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions go/ics23.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func VerifyMembership(spec *ProofSpec, root CommitmentRoot, proof *CommitmentPro
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(proof, key)
np := getNonExistProofForKey(spec, proof, key)
if np == nil {
return false
}
Expand Down Expand Up @@ -148,27 +148,27 @@ func getExistProofForKey(proof *CommitmentProof, key []byte) *ExistenceProof {
return nil
}

func getNonExistProofForKey(proof *CommitmentProof, key []byte) *NonExistenceProof {
func getNonExistProofForKey(spec *ProofSpec, proof *CommitmentProof, key []byte) *NonExistenceProof {
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
switch p := proof.Proof.(type) {
case *CommitmentProof_Nonexist:
np := p.Nonexist
if isLeft(np.Left, key) && isRight(np.Right, key) {
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(np.Left, key) && isRight(np.Right, key) {
if np := sub.GetNonexist(); np != nil && isLeft(spec, np.Left, key) && isRight(spec, np.Right, key) {
return np
}
}
}
return nil
}

func isLeft(left *ExistenceProof, key []byte) bool {
return left == nil || bytes.Compare(left.Key, key) < 0
func isLeft(spec *ProofSpec, left *ExistenceProof, key []byte) bool {
return left == nil || bytes.Compare(keyForComparison(spec, left.Key), keyForComparison(spec, key)) < 0
}

func isRight(right *ExistenceProof, key []byte) bool {
return right == nil || bytes.Compare(right.Key, key) > 0
func isRight(spec *ProofSpec, right *ExistenceProof, key []byte) bool {
return right == nil || bytes.Compare(keyForComparison(spec, right.Key), keyForComparison(spec, key)) > 0
}
12 changes: 10 additions & 2 deletions go/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ func (p *ExistenceProof) CheckAgainstSpec(spec *ProofSpec) error {
return nil
}

func keyForComparison(spec *ProofSpec, key []byte) []byte {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also will have to use this function on getExistProofForKey and getNonexistProofForKey correct?

473d9a5#diff-39a55415fc38a90b85c05989f293fda3b7ee126010cfe63838fd9b8441e47ed1R39

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good catch

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but only on getNonexistProof, because the lexical comparison is only relevant for non-existence proofs

plaidfinch marked this conversation as resolved.
Show resolved Hide resolved
if !spec.PrehashCompareKey {
return key
}
hash, _ := doHashOrNoop(spec.LeafSpec.PrehashKey, key)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is using the PrehashKey defined in the leafspec and the key in the existenceProof is supposed to now be unhashed for SMT proofs, I think the SMT spec needs to be changed to have PrehashKey=SHA_256 instead of NO_HASH

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right so the SMT proof spec in this repo need to be changed

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code that generates the proofs will also need to be updated following the spec change

return hash
}

// Verify does all checks to ensure the proof has valid non-existence proofs,
// and they ensure the given key is not in the CommitmentState
func (p *NonExistenceProof) Verify(spec *ProofSpec, root CommitmentRoot, key []byte) error {
Expand All @@ -229,13 +237,13 @@ func (p *NonExistenceProof) Verify(spec *ProofSpec, root CommitmentRoot, key []b

// Ensure in valid range
if rightKey != nil {
if bytes.Compare(key, rightKey) >= 0 {
if bytes.Compare(keyForComparison(spec, key), keyForComparison(spec, rightKey)) >= 0 {
return errors.New("key is not left of right proof")
}
}

if leftKey != nil {
if bytes.Compare(key, leftKey) <= 0 {
if bytes.Compare(keyForComparison(spec, key), keyForComparison(spec, leftKey)) <= 0 {
return errors.New("key is not right of left proof")
}
}
Expand Down
173 changes: 109 additions & 64 deletions go/proofs.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions js/src/generated/codecimpl.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,9 @@ export namespace ics23 {

/** ProofSpec minDepth */
minDepth?: number | null;

/** ProofSpec prehashComparedKey */
prehashComparedKey?: boolean | null;
}

/**
Expand Down Expand Up @@ -736,6 +739,9 @@ export namespace ics23 {
/** ProofSpec minDepth. */
public minDepth: number;

/** ProofSpec prehashComparedKey. */
public prehashComparedKey: boolean;

/**
* Creates a new ProofSpec instance using the specified properties.
* @param [properties] Properties to set
Expand Down
Loading