Skip to content
This repository was archived by the owner on Jul 15, 2018. It is now read-only.

Bring in merkle and tmhash from tmlibs#105

Merged
ebuchman merged 6 commits intodevelopfrom
bucky/merkle
May 30, 2018
Merged

Bring in merkle and tmhash from tmlibs#105
ebuchman merged 6 commits intodevelopfrom
bucky/merkle

Conversation

@ebuchman
Copy link
Contributor

@ebuchman ebuchman requested a review from jaekwon as a code owner May 22, 2018 16:39
@ebuchman ebuchman mentioned this pull request May 22, 2018
@ebuchman
Copy link
Contributor Author

Also replaces tendermint/tmlibs#211

@liamsi
Copy link
Contributor

liamsi commented May 22, 2018

referencing discussion on hashing inner & leaf nodes differently; as well as the removed encoding (as both might or might not have security implications):
tendermint/tmlibs#211 (comment) (and following)

)

// Hash(left | right). The basic operation of the Merkle tree.
func SimpleHashFromTwoHashes(left, right []byte) []byte {
Copy link
Contributor

Choose a reason for hiding this comment

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

If the size of left and right is now fixed, can we use the size in the args:
SimpleHashFromTwoHashes(left, right [Size]byte)
see also: tendermint/tmlibs#211 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

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

Making this explicit via left, right [tmhash.Size]byte makes it necessary to change a lot of places. I propose to keep the encoding (called length prefix here: tendermint/tmlibs#211) and potentially refactor this at a later point.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

damn ok. my motivation was to simplify the tree description by removing unecessary length prefixing. but i agree that if we do so, we should introduce some better type safety, and doing so will require ALOT of changes ... so maybe its not that important right now ? Just feels like now is a good time to make wanted breaking changes to the data structures :P

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also having length prefixes doesnt solve the 2nd pre-image attack right ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Nope. It just mitigates similar but simpler collisions (e.g., I guess where you just append 2 byte slices together and yield the same root hash as with two leafs ?). Let's re-add the encoding (which allows for variable sized byte slices to be hashed) and add proper prefixes in a separate issue.

Copy link
Contributor

@liamsi liamsi May 24, 2018

Choose a reason for hiding this comment

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

so maybe its not that important right now ?

it's definitely easier to simply re-introduce the encoding. I've already started adding fixed lengths everywhere in another branch. There seems to be sth wrong though (didn't investigate but I pushed it now: https://github.com/Liamsi/go-crypto/compare/bucky_fixed...Liamsi:fixed_size?expand=1)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sth wrong how ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

also if we go through with tendermint/tmlibs#213 we'll need the length prefix in here.

I guess so long as we use []byte, we should use a length prefix, at least so people don't use it wrong.

Copy link
Contributor

Choose a reason for hiding this comment

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

sth wrong how ?

TestSimpleProof fails on every index

I guess so long as we use []byte, we should use a length prefix, at least so people don't use it wrong.

Totally.

/ \ / \ / \
h0 h1 h2 h3 h4 h5

*/
Copy link
Contributor

@liamsi liamsi May 22, 2018

Choose a reason for hiding this comment

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

Can we add a warning here about pre-image attacks, or, more general a warning on how to use all this safely (in our context, but also in general). See for instance: https://github.com/bitcoin/bitcoin/blob/5961b23898ee7c0af2626c46d5d70e80136578d3/src/consensus/merkle.cpp#L9-L43

Copy link
Contributor

Choose a reason for hiding this comment

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

I can take care of this later too (I would open an issue though referencing relevant comments)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would love it if you could pick this up! Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

@ebuchman
Copy link
Contributor Author

ebuchman commented May 24, 2018

@liamsi also worth noting how we use these functions from github.com/tendermint/types - especially that we duplicate the implementation to avoid allocating/populating large slices.

See PR to update tendermint dependent on this in tendermint/tendermint#1606

@liamsi
Copy link
Contributor

liamsi commented May 24, 2018

Thanks for the pointer! If you like, we can fix the test-vectors here and merge this (so you can reference the development branch there instead: https://github.com/tendermint/tendermint/pull/1606/files#diff-bd247e83efc3c45ae9e8c47233249f18R261).
I'll keep track and take care of the comments (documenting safety and JP's comments).

@liamsi liamsi mentioned this pull request May 24, 2018
3 tasks
type strHasher string

func (str strHasher) Hash() []byte {
return SimpleHashFromBytes([]byte(str))
Copy link
Contributor

@liamsi liamsi May 24, 2018

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

meh - trying to simplify the exposed interface and it got replaced with tmhash.Sum

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, makes sense

@ebuchman
Copy link
Contributor Author

Cool I'm going to squash some commits and then use amino for the encodeByteSlice (didn't want to do that before to avoid an amino dependency in tmlibs)

@ebuchman ebuchman merged commit 251041c into develop May 30, 2018
@ebuchman ebuchman deleted the bucky/merkle branch May 30, 2018 22:08
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants