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]: Implement a Merkle Tree #161

Merged
merged 6 commits into from
Feb 18, 2025

Conversation

beeguy74
Copy link
Contributor

@beeguy74 beeguy74 commented Feb 14, 2025

📝 [FEAT]: Implement a Merkle Tree

🛠️ Issue

📖 Description

Implementation of a Merkle Tree contract. The contract handles the creation of Merkle trees and verification of data inclusion using Merkle proofs. I provided series of unit tests to ensure that all works fine.

build_tree returns the hashes of the nodes in the Merkle tree;
get_root returns the hash root of the tree;
verify requires Merkle proof array, hash root, hash of the value being verified and its index in the original data array
generate_merkle_proof requires the index of the target value and the length of initial data array

📝 Additional Notes

I refined the Merkle tree implementation from 'Starknet by Example': I added a support for odd input data arrays so provided tree is built as follows:

  • The hashes of the transactions in the input data array are computed: hash(L1), hash(L2), hash(L3), and so on.
  • Hashes of the sum of data hashes are then calculated, for example, hash(hash(L1) + hash(L2)). Since a Merkle tree is binary, the number of elements at each iteration must be even. Therefore, if the data array contains an odd number of elements, the last one is duplicated and combined with itself: hash(hash(L3) + hash(L3)).
  • Next, the hashes of the sum of hashes are computed again. This process repeats until a single hash is obtained—the Merkle root.

@beeguy74 beeguy74 marked this pull request as ready for review February 15, 2025 20:23
@beeguy74 beeguy74 changed the title initial implemetation of MercleTree build_tree [FEAT]: Implement a Merkle Tree Feb 15, 2025
@beeguy74
Copy link
Contributor Author

@danielcdz I am ready for review, sir! 🦺

Copy link
Member

@danielcdz danielcdz left a comment

Choose a reason for hiding this comment

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

LGTM

@beeguy74
Copy link
Contributor Author

Do you have some comments on this? should i change something?

Copy link
Contributor

@coxmars coxmars left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for your effort 🫡

@coxmars coxmars merged commit 1999b55 into KaizeNodeLabs:main Feb 18, 2025
2 checks passed
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.

[FEAT]: Implement a Merkle Tree
3 participants