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

complex proof (binary tree hash). #81

Open
wants to merge 77 commits into
base: master
Choose a base branch
from

Conversation

cheme
Copy link
Contributor

@cheme cheme commented Feb 26, 2020

This draft PR tracks some experiments with binary tree hash replacing the hash of children for
a binary tree hash of childrens in branch nodes.

Current state is tests passing (but not tested in depth, just existing test minus 2 that acts differently).
All test runs on this new proof (see COMPLEX_HASH associated constant definition in test-support/reference-trie/src/lib.rs).
Code and interface change needs rework (lots of TODOs and duplicated code but initial purpose of writing this was to pass tests in order to check limitations and possibly unexpected blockers).

Performance was not really checked thoroughly (binary hasher trait is incredibly awkward at this point and is instantiated for every binary hash, and there is some skippable memcopy), on iter_build it looked like a ~40% perf dropdown.

Proof build from Recorder are not trimmed (recorder record all stored data and cannot really skip some child hash).
Proof compacted with trie_codec and 'generate_proof' do skip child hash not needed for binary tree hash. The format is (inline node are never skipped for simplicity):
'standard encoded branch without children' ++ '2 byte bitmap in proof elements' ++ 'inline nodes' ++ 'additional hashes'
The bitmap is needed to know which children are defined as input for the proof (it is the skipped hash from compact proof ++ inline children).
Inline nodes are here even when not needed in the proof, this could be optimized but would change the proof compaction to go into inline node (that is the case for 'generate_proof' but I do not think it is worth it to have something different from the general case).
Additional hashes are the hashes needed to check proof in order (on a 4 size tree with one node in the proof it will be two elements: the sibling input hash and the hash of the other two siblings).

The binary hash applied is running over a sized binary tree with size defined as consecutive existing children of a branch (since we got a bitmap of their position in the header we can calculate the binary proof on a contiguous sequence).
So for a branch with one child the binary hash is the hash of the child, for two hash it is hash(hash1 ++ hash2) for three hash it is hash(hash(hash1 ++ hash2) ++ hash3) and so on.

ahead logic is based on the first one which makes thing a bit hard to
read).
cheme added a commit to cheme/trie that referenced this pull request May 18, 2020
@cheme cheme marked this pull request as ready for review May 18, 2020 12:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant