Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

[golang] Merkle verifier #1161

Merged
merged 8 commits into from
Jun 21, 2016
Merged

Conversation

AlCutter
Copy link
Member

Lays groundwork for #1159
cc/ @gdbelvin

h.hasher.Write([]byte{LeafPrefix})
return h.hasher.Sum(leaf)

}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't you do something like this in Go instead? Wouldn't that be more Go-like?

func (h hash.Hash) HashLeaf(leaf []byte) []byte {
    h.Reset()
    h.Write([]byte{LeafPrefix})
    return h.Sum(leaf)
}

And just delete DigestSize. And inline the two-line implementation of HashEmpty entirely at its one call site?

Or if it's weird to add methods (which it kind of is for these methods in particular, from my perception of how this interface is meant to be used), HashLeafand HashChildren could just be plain functions that take a hash.Hash?

Copy link
Member Author

Choose a reason for hiding this comment

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

Nope, that's pretty Objective-C, though :)
AFAIK in go you can't add methods to types in other packages.

@pphaneuf
Copy link
Contributor

Also:

--- FAIL: TestVerifyInclusionProof (0.00s)
    merkle_verifier_test.go:337: failed to verify proof: insuficient number of proof components (1) for treeSize 8
--- FAIL: TestVerifyConsistencyProof (0.00s)
    merkle_verifier_test.go:354: Failed to verify proof: invalid empty root1
--- FAIL: TestCollisionDifferentLeaves (0.00s)
    tree_hasher_test.go:93: Got unexpected leaf1 digest size 37, expected 32
FAIL
FAIL    github.com/google/certificate-transparency/go/merkletree    0.025s

@pphaneuf
Copy link
Contributor

pphaneuf commented Apr 26, 2016

See cc5c322 for a suggestion trimming a chunk out. Also fails the tests, in the same way, I didn't fix it up, just flensed the fat away. 😉

func (h TreeHasher) HashLeaf(leaf []byte) []byte {
h.hasher.Reset()
h.hasher.Write([]byte{LeafPrefix})
return h.hasher.Sum(leaf)
Copy link
Contributor

@RJPercival RJPercival Apr 29, 2016

Choose a reason for hiding this comment

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

I don't think this does what you think it does... https://golang.org/pkg/hash/ (or in fact, what anyone would expect it to do!)

Copy link
Member Author

Choose a reason for hiding this comment

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

That is a really good point o_O
Thanks!

@gdbelvin
Copy link
Contributor

gdbelvin commented May 3, 2016

Very happy to see progress on this!

@AlCutter
Copy link
Member Author

PTAL :)

}

// VerifyInclusionProof verifies the correctness of the passed in proof given the passed in information about the tree and leaf.
func (m MerkleVerifier) VerifyInclusionProof(leafIndex, treeSize int64, proof [][]byte, root []byte, data []byte) error {
Copy link
Contributor

@gdbelvin gdbelvin May 25, 2016

Choose a reason for hiding this comment

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

In Go, multiple arguments with the same type can be rewritten as follows:

func (m MerkleVerifier) VerifyInclusionProof(leafIndex, treeSize int64, proof [][]byte, root, data []byte)

Copy link
Member Author

Choose a reason for hiding this comment

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

Like leafIndex and treeSize already are, you mean? :)
I didn't collapse the later arg types as it looks a bit weird to me, though I don't feel strongly about it.

Copy link
Contributor

Choose a reason for hiding this comment

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

It isn't clear what data is. Perhaps rename it to leaf or something like that?

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@gdbelvin
Copy link
Contributor

Will this be integrated with the client?

@AlCutter
Copy link
Member Author

AlCutter commented Jun 9, 2016

@RJPercival prod prod

"fmt"
)

// MerkleVerifier is a class which knows how to verify merkle inclusion and consistency proofs.
Copy link
Contributor

Choose a reason for hiding this comment

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

The RFC refers to inclusion proofs as "audit proofs". Do we consistently use the term "inclusion proof" instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah - "audit proof" is kinda vague, so we settled on using "inclusion-" and "consistency-proof" for clarity.

@AlCutter
Copy link
Member Author

PTAL

@AlCutter
Copy link
Member Author

ping

@pphaneuf
Copy link
Contributor

This one could stand a rebase, being on top of a commit from back in April! 😉

@AlCutter AlCutter force-pushed the go_merkle_verifier branch from afdfb71 to 394f5b4 Compare June 17, 2016 13:28
}

// RootFromInclusionProof calculates the expected tree root given the proof and leaf.
func (m MerkleVerifier) RootFromInclusionProof(leafIndex, treeSize int64, proof [][]byte, data []byte) ([]byte, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Rename data to leaf here as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@AlCutter
Copy link
Member Author

PTAL

sha256EmptyTreeHash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
)

func verifierCheck(v *MerkleVerifier, leafIndex, treeSize int64, proof [][]byte, root []byte, data []byte) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

data should be renamed to leaf here too.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

return errors.New("incorrectly verified against swapped roots")
}

// Wrong inclusion proofs
Copy link
Contributor

Choose a reason for hiding this comment

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

s/inclusion/consistency/

t.Fatalf("Empty leaf digest has length %d, but expected %d", got, want)
}

if bytes.Compare(leaf1Digest, leaf2Digest) == 0 {
Copy link
Contributor

@RJPercival RJPercival Jun 21, 2016

Choose a reason for hiding this comment

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

nit: bytes.Equal is preferable to bytes.Compare. This applies in the next function as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup, surprised I wrote it using Compare tbh. Done, anyway.

@RJPercival
Copy link
Contributor

LGTM, bar a few nits.

@AlCutter AlCutter merged commit dad64d1 into google:master Jun 21, 2016
@AlCutter AlCutter deleted the go_merkle_verifier branch June 21, 2016 10:19
gdbelvin added a commit to gdbelvin/certificate-transparency that referenced this pull request Jul 14, 2016
* upstream/master: (23 commits)
  Check for HTTP status 200 in fetchAndParse.
  Run everything through gofmt.
  Tweak the imports to make goimports happier.
  Initialize client if passed nil (google#1265)
  Have the caller supply the *http.Client used in logclient.go.
  Review fixes
  Use ASN1 lib to transform TLS encoded list into a Octet String
  Move SCTListLen Check to beginning
  Rework patch
  Add support for serializing SCT Lists
  Get rid of the Java MerkleLeafType enum.
  Add a missing entry to the .gitignore file.
  Get rid of the TLSSerializer class. Make the low-level serialization functions stand-alone ones in a namespace rather than members of a class.
  6962-bis: Implement OID parsing/serialization.
  Do not use std::move in "return" statements.
  Improve stats comments and add additional inc.
  Add additonal stats to outer FixAndLog wrapper.
  [golang] Merkle verifier (google#1161)
  Ignore clang warnings due to unused stuff caused by glog (google#1253)
  Remove a few unnecessary uses of std::move.
  ...
gdbelvin pushed a commit to gdbelvin/certificate-transparency that referenced this pull request Jul 18, 2016
gdbelvin added a commit to gdbelvin/certificate-transparency that referenced this pull request Jul 18, 2016
* upstream/master: (25 commits)
  Check for HTTP status 200 in fetchAndParse.
  Run everything through gofmt.
  Tweak the imports to make goimports happier.
  Initialize client if passed nil (google#1265)
  Have the caller supply the *http.Client used in logclient.go.
  Review fixes
  Use ASN1 lib to transform TLS encoded list into a Octet String
  Move SCTListLen Check to beginning
  Rework patch
  Add support for serializing SCT Lists
  Get rid of the Java MerkleLeafType enum.
  Add a missing entry to the .gitignore file.
  Get rid of the TLSSerializer class. Make the low-level serialization functions stand-alone ones in a namespace rather than members of a class.
  6962-bis: Implement OID parsing/serialization.
  Do not use std::move in "return" statements.
  Improve stats comments and add additional inc.
  Add additonal stats to outer FixAndLog wrapper.
  [golang] Merkle verifier (google#1161)
  Ignore clang warnings due to unused stuff caused by glog (google#1253)
  Remove a few unnecessary uses of std::move.
  ...
gdbelvin added a commit to gdbelvin/certificate-transparency that referenced this pull request Jul 18, 2016
* upstream/master: (25 commits)
  Check for HTTP status 200 in fetchAndParse.
  Run everything through gofmt.
  Tweak the imports to make goimports happier.
  Initialize client if passed nil (google#1265)
  Have the caller supply the *http.Client used in logclient.go.
  Review fixes
  Use ASN1 lib to transform TLS encoded list into a Octet String
  Move SCTListLen Check to beginning
  Rework patch
  Add support for serializing SCT Lists
  Get rid of the Java MerkleLeafType enum.
  Add a missing entry to the .gitignore file.
  Get rid of the TLSSerializer class. Make the low-level serialization functions stand-alone ones in a namespace rather than members of a class.
  6962-bis: Implement OID parsing/serialization.
  Do not use std::move in "return" statements.
  Improve stats comments and add additional inc.
  Add additonal stats to outer FixAndLog wrapper.
  [golang] Merkle verifier (google#1161)
  Ignore clang warnings due to unused stuff caused by glog (google#1253)
  Remove a few unnecessary uses of std::move.
  ...
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants