Skip to content

Commit

Permalink
add: utils functions and update imports
Browse files Browse the repository at this point in the history
  • Loading branch information
olivmath committed Feb 5, 2024
1 parent 824f9cf commit 8c69ec3
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 9 deletions.
59 changes: 59 additions & 0 deletions MERKLE_ROOT.md
Original file line number Diff line number Diff line change
@@ -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
```
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
pub mod merkle_proof;
pub mod merkle_proof_check;
pub mod merkle_root;
pub mod merkletree;
pub mod utils;
5 changes: 3 additions & 2 deletions src/merkle_proof.rs
Original file line number Diff line number Diff line change
@@ -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<Node> {
let mut proof: Vec<Node> = Vec::new();
Expand Down Expand Up @@ -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;

Expand Down
5 changes: 3 additions & 2 deletions src/merkle_proof_check.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils2::{hash_function, Node};
use crate::utils::{hash_function, Node};

pub fn merkle_proof_check(proof: Vec<Node>, leaf: [u8; 32]) -> [u8; 32] {
let mut current_hash = leaf;
Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/merkle_root.rs
Original file line number Diff line number Diff line change
@@ -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];
Expand Down
5 changes: 3 additions & 2 deletions src/merkletree/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ impl MerkleTree {

impl MerkleTree {
pub fn proof(self, leaf: &str) -> Vec<Node> {
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;
}
}
39 changes: 37 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::merkletree::Leaf;
use tiny_keccak::{Hasher, Keccak};

/// # Verify if `x: int` is power of 2
/// - params `x: int`
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -82,3 +81,39 @@ pub fn half<T: std::clone::Clone>(arr: &Vec<T>) -> (Vec<T>, Vec<T>) {

(left.to_vec(), right.to_vec())
}

#[derive(PartialEq, Debug, Clone)]
pub enum Side {
LEFT = 0,
RIGHT = 1,
}
impl From<u8> 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)
}

0 comments on commit 8c69ec3

Please sign in to comment.