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

merkle tree structure needed for FRI #9

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions cryptography/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "cryptography"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rs_merkle = "1.4.1"
ark-poly = "0.4.2"
ark-ff = "0.4.2"
ark-test-curves = "0.4.2"
sha2 = "0.10.8"
rand = { version = "0.8", features = [ "std", "std_rng" ] }
sha3 = "0.10"
thiserror = "1.0.38"
serde = { version = "1.0", features = ["derive"] }
rayon = { version = "1.8.0", optional = true }
1 change: 1 addition & 0 deletions cryptography/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod merkle;
32 changes: 32 additions & 0 deletions cryptography/src/merkle/common_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

use super::tree::checkForValidMerkleTree;

pub fn parent_idx (node_idx: usize) -> usize {
if node_idx % 2 == 0{
(node_idx - 1) / 2
}
else {
node_idx / 2
}
}

pub fn sibling_idx (node_idx: usize) -> usize {
if node_idx % 2 == 0 {
node_idx - 1
}
else {
node_idx + 1
}
}

pub fn fill_untill_pow_two <T:Clone>(values: &mut Vec<T>) -> Vec<T> {
while !is_power_of_two(values.len()){
values.push(values[values.len() - 1].clone())
}

values.to_vec()
}

pub fn is_power_of_two(x: usize) -> bool {
(x != 0) && ((x & (x-1)) == 0)
}
42 changes: 42 additions & 0 deletions cryptography/src/merkle/hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::{marker::PhantomData, fmt::Debug};

use ark_ff::PrimeField;
use rs_merkle::{algorithms::Sha256, Hasher};

pub trait Hash<F: PrimeField> {
type HashedVal: Clone + PartialEq + Debug + Copy;
fn hash_value(data: F) -> Self::HashedVal;
fn hash_left_right_child(lchild: Self::HashedVal, rchild: Self::HashedVal) -> Self::HashedVal;
}

#[derive(Clone, Debug)]
pub struct Sha256_ <F: PrimeField> {
_marker: PhantomData<F>,
}

pub fn usize_slice_to_u8_slice(usizes: &[usize]) -> Vec<u8> {
let mut bytes = Vec::new();
for &usize_val in usizes {
let usize_bytes = usize_val.to_le_bytes();
bytes.extend_from_slice(&usize_bytes);
}
bytes
}

impl <F: PrimeField> Hash<F> for Sha256_<F>{
type HashedVal = F;

fn hash_value(data: F) -> Self::HashedVal {
let vals: Vec<usize> = data.to_string().into() as Vec<usize>;
F::from_le_bytes_mod_order(&Sha256::hash(usize_slice_to_u8_slice(&vals)))
}

fn hash_left_right_child(lchild: Self::HashedVal, rchild: Self::HashedVal) -> Self::HashedVal {
let mut d_left : Vec<usize> = lchild.to_string().into() as Vec<usize>;
let mut d_right: Vec<usize> = rchild.to_string().into() as Vec<usize>;

d_left.append(&mut d_right);
let hashed_children: [u8; 32] = Sha256::hash(&d_left);
F::from_le_bytes_mod_order(&hashed_children)
}
}
3 changes: 3 additions & 0 deletions cryptography/src/merkle/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod common_utils;
pub mod tree;
pub mod hash;
16 changes: 16 additions & 0 deletions cryptography/src/merkle/tree.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use ark_ff::PrimeField;
use super::hash::Hash;


//One stop checking trait that would import the tree content of a Valid Merkle Tree
pub trait checkForValidMerkleTree: Default {
type Node: PartialEq + Eq + Clone + Sync + Send;
type Value: Sync + Send;

fn convert_to_node(leaf: &Self::Value) -> Self::Node;

fn hash_leaves(unhashed_leaves: &[Self::Value]) -> Vec<Self::Node> {

}

}