/Patika-MerkleTree-Assignment

Merkle Tree Root Calculator Implementation Assignment for Patika.dev Rust & Solana Practicum

Primary LanguageRust

Patika.dev Community Gaming Solana & Rust Practicum

Hands on Task: Merkle Tree Root Calculator on Rust

Firstly, we should understand what is Merkle Tree and how it works well. In order to do this, please research about merkle tree and watch the video carefully.

Our main purpose is that take inputs from txt files, hash them and calculate the root. Example txt file looks like:

3
patika
izmir
denizli
adana
mersin
deniz
gemi
ucak

The first line is n which is $2^n$ where the total number of leaves bottom of the tree. Then, if n is 3 like above we have 8 naked leaves in our tree. All of them is non hashed and they are written to text file one under the other. Merkle Tree

Instructions

  1. Fork or clone this repository
  2. Add dependencies to Cargo.toml which you would like to use
  3. Read data from txt file and use it as is required so, store the n as u32 and store splitted String inputs to a Vector of Strings
  4. Hash all inputs with SHA256 from sha-2 crate. It should be hex encoded then for the sake of this assignment it can be hashed like this:
fn hash_single_input(a: &str) -> String {
    let mut hasher = Sha256::new();
    let input = a;
    hasher.update(input);
    let hash = hasher.finalize();
    let hex = hex::encode(&hash);

    return hex.to_string();
}

Note: But also writing macro rule for hashing would be great!

Also you can check your output from here

Moreover, if you want to hash two hash it you can concat two strings and hash it with the function above.

And also you can check your output from here adding inputs to another without any spaces. For example 'izmir' and 'denizli':

izmirdenizli
  1. Then, from naked inputs you are expected to calculate root of merkle tree. Return the root as a String output.
  2. You can use helper function templates or just remove them.
  3. You can run tests like below:
cargo run test
  1. Pass all tests :)