diff --git a/MERKLE_ROOT.md b/MERKLE_ROOT.md new file mode 100644 index 0000000..1a2fbd7 --- /dev/null +++ b/MERKLE_ROOT.md @@ -0,0 +1,59 @@ +Python DRAFT + +√ +``` +>>> from keccaky import hash_it_bytes +>>> +>>> +>>> +>>> leaves = [ +... [ +... 58, 194, 37, 22, 141, 245, 66, 18, 162, 92, 28, 1, 253, 53, 190, 191, 234, 64, 143, +... 218, 194, 227, 29, 221, 111, 128, 164, 187, 249, 165, 241, 203, +... ], +... [ +... 181, 85, 61, 227, 21, 224, 237, 245, 4, 217, 21, 10, 248, 45, 175, 165, 196, 102, +... 127, 166, 24, 237, 10, 111, 25, 198, 155, 65, 22, 108, 85, 16, +... ], +... [ +... 11, 66, 182, 57, 60, 31, 83, 6, 15, 227, 221, 191, 205, 122, 173, 204, 168, 148, +... 70, 90, 90, 67, 143, 105, 200, 125, 121, 11, 34, 153, 185, 178, +... ], +... [ +... 241, 145, 142, 133, 98, 35, 110, 177, 122, 220, 133, 2, 51, 47, 76, 156, 130, 188, +... 20, 225, 155, 252, 10, 161, 10, 182, 116, 255, 117, 179, 210, 243, +... ], +... ]; +>>> +>>> +>>> node1 = hash_it_bytes(leaves[0] + leaves[1]) +>>> list(node1) +[128, 91, 33, 216, 70, 177, 137, 239, 174, 176, 55, 125, 107, 176, 210, 1, 179, 135, 42, 54, 62, 96, 124, 37, 8, 143, 2, 91, 12, 106, 225, 248] +>>> +>>> +>>> +>>> node2 = hash_it_bytes(leaves[2] + leaves[3]) +>>> list(node2) +[210, 83, 165, 45, 76, 176, 13, 226, 137, 94, 133, 242, 82, 158, 41, 118, 230, 170, 170, 92, 24, 16, 107, 104, 171, 102, 129, 62, 20, 65, 86, 105] +>>> +>>> +>>> +>>> root = hash_it_bytes(node1+node2) +>>> list(root) +[104, 32, 63, 144, 233, 208, 125, 197, 133, 146, 89, 215, 83, 110, 135, 166, 186, 157, 52, 95, 37, 82, 181, 185, 222, 41, 153, 221, 206, 156, 225, 191] +>>> +>>> +>>> +>>> proof_leaf_0 = [leaves[0], leaves[1], list(node2)] +>>> +>>> result = hash_it_bytes(list(hash_it_bytes(proof_leaf_0[0] + proof_leaf_0[1])) + proof_leaf_0[2]) +>>> +>>> list(result) +>>> [104, 32, 63, 144, 233, 208, 125, 197, 133, 146, 89, 215, 83, 110, 135, 166, 186, 157, 52, 95, 37, 82, 181, 185, 222, 41, 153, 221, 206, 156, 225, 191] +>>> +>>> list(root) +>>> [104, 32, 63, 144, 233, 208, 125, 197, 133, 146, 89, 215, 83, 110, 135, 166, 186, 157, 52, 95, 37, 82, 181, 185, 222, 41, 153, 221, 206, 156, 225, 191] +>>> +>>> result == root +>>> True +``` \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 7c4c2c8..e2131d2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,5 @@ +pub mod merkle_proof; +pub mod merkle_proof_check; +pub mod merkle_root; pub mod merkletree; pub mod utils; diff --git a/src/merkle_proof.rs b/src/merkle_proof.rs index ee32a7a..45b809d 100644 --- a/src/merkle_proof.rs +++ b/src/merkle_proof.rs @@ -1,4 +1,4 @@ -use crate::{merkle_root::merkle_root, utils2::Node}; +use crate::{merkle_root::merkle_root, utils::Node}; pub fn merkle_proof(leaves: &[[u8; 32]], leaf: [u8; 32]) -> Vec { let mut proof: Vec = Vec::new(); @@ -84,7 +84,8 @@ mod tests { mod merkle_proof_leaves_odd {} mod merkle_proof_leaves_base_2 { - use crate::utils2::{Node, Side}; + + use crate::utils::{Node, Side}; use super::merkle_proof; diff --git a/src/merkle_proof_check.rs b/src/merkle_proof_check.rs index 463fc91..284f47c 100644 --- a/src/merkle_proof_check.rs +++ b/src/merkle_proof_check.rs @@ -1,4 +1,4 @@ -use crate::utils2::{hash_function, Node}; +use crate::utils::{hash_function, Node}; pub fn merkle_proof_check(proof: Vec, leaf: [u8; 32]) -> [u8; 32] { let mut current_hash = leaf; @@ -27,7 +27,8 @@ mod tests { mod merkle_proof_check_leaves_odd {} mod merkle_proof_check_leaves_base_2 { - use crate::utils2::{Node, Side}; + + use crate::utils::{Node, Side}; use super::merkle_proof_check; diff --git a/src/merkle_root.rs b/src/merkle_root.rs index 6e88cd1..ff1c0b4 100644 --- a/src/merkle_root.rs +++ b/src/merkle_root.rs @@ -1,4 +1,4 @@ -use crate::utils2::hash_function; +use crate::utils::hash_function; pub fn merkle_root(leaves: &[[u8; 32]]) -> [u8; 32] { let mut node = [0u8; 32]; diff --git a/src/merkletree/tree.rs b/src/merkletree/tree.rs index a5b0c9b..75c0a71 100644 --- a/src/merkletree/tree.rs +++ b/src/merkletree/tree.rs @@ -87,8 +87,9 @@ impl MerkleTree { impl MerkleTree { pub fn proof(self, leaf: &str) -> Vec { - let mut proof = MerkleTree::make_proof(self.leafs, to_keccak256(leaf.to_string()), &mut vec![]); + let mut proof = + MerkleTree::make_proof(self.leafs, to_keccak256(leaf.to_string()), &mut vec![]); proof.reverse(); - return proof + return proof; } } diff --git a/src/utils.rs b/src/utils.rs index cc177c7..54abcf5 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,4 +1,5 @@ use super::merkletree::Leaf; +use tiny_keccak::{Hasher, Keccak}; /// # Verify if `x: int` is power of 2 /// - params `x: int` @@ -47,8 +48,6 @@ pub fn is_power_2(number: u32) -> bool { /// ); /// ``` pub fn to_keccak256(message: String) -> Leaf { - use tiny_keccak::{Hasher, Keccak}; - let mut k256 = Keccak::v256(); let mut result = [0; 32]; @@ -82,3 +81,39 @@ pub fn half(arr: &Vec) -> (Vec, Vec) { (left.to_vec(), right.to_vec()) } + +#[derive(PartialEq, Debug, Clone)] +pub enum Side { + LEFT = 0, + RIGHT = 1, +} +impl From for Side { + fn from(num: u8) -> Self { + match num { + 0 => Side::LEFT, + 1 => Side::RIGHT, + _ => panic!("Invalid value for Side enum, must be either `0` or `1`"), + } + } +} + +#[derive(PartialEq, Debug, Clone)] +pub struct Node { + pub data: [u8; 32], + pub side: Side, +} + +pub fn hash_it(data: &[u8], buffer: &mut [u8; 32]) { + let mut k256 = Keccak::v256(); + + k256.update(data); + k256.finalize(buffer); +} + +pub fn hash_function(left: &[u8; 32], right: &[u8; 32], buffer: &mut [u8; 32]) { + let mut concat = [0u8; 64]; + concat[..32].copy_from_slice(left); + concat[32..].copy_from_slice(right); + + hash_it(&concat, buffer) +}