Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*use ring::digest::{Algorithm, Digest};*/
- use std::collections::HashMap;
- /*type Index = u32;
- type Value = u32;
- type HashValue = u32;
- type Level = u8;
- */
- use ring::digest::{Digest,Context, Algorithm, SHA384};
- pub struct SparseMerkleTree{
- //Max seize = 256 <=> max elements = 2^256
- pub size: usize,
- //Mapping for Nodes: (level,parentIndex) -> (leftChild,rightChild)
- //For Leaves: (0,leafIndex) -> (value(=leafIndex))
- pub parenthoods: HashMap<(usize,usize),(usize,usize)>,
- pub algo: Algorithm,
- }
- pub trait Hashable {
- /// Update the given `context` with `self`.
- ///
- /// See `ring::digest::Context::update` for more information.
- fn update_context(&self, context: &mut Context);
- }
- impl Hashable for usize {
- fn update_context(&self, context: &mut Context) {
- context.update(&self.to_be_bytes());
- }
- }
- pub trait HashUtils {
- fn hash_nodes(&'static self, left: &usize, right: &usize) -> Digest;
- }
- impl HashUtils for Algorithm {
- fn hash_nodes(&'static self, left: &usize, right: &usize) -> Digest
- {
- let mut ctx = Context::new(self);
- ctx.update(&[0x01]);
- left.update_context(&mut ctx);
- right.update_context(&mut ctx);
- ctx.finish()
- }
- }
- impl SparseMerkleTree {
- pub fn new (size:usize, algo:Algorithm) -> Self {
- SparseMerkleTree{size: size, parenthoods: HashMap::new(), algo}
- }
- /*ub fn hash (&self, a:usize, b:usize) {
- }*/
- pub fn add_value (&mut self,value:usize) {
- let mut parent_value;
- let mut sibling_value;
- let mut current_value = value;
- let mut index = value;
- //Inserting the leaf in the hasmap
- self.parenthoods.insert((0,value),(0,0));
- //First round of the loop, similar but different
- if index%2 == 0 {
- match self.parenthoods.get(&(0,index+1)) {
- None => {
- self.parenthoods.insert((1,index/2),(index,index+1));
- parent_value = self.algo.hash_nodes(¤t_value,&0);
- }
- Some(_k) => {
- self.parenthoods.insert((1,index/2),(index,index+1));
- parent_value = self.algo.hash_nodes(¤t_value,&0);
- }
- }
- }
- else {
- match self.parenthoods.get(&(0,index+1)) {
- None => {
- self.parenthoods.insert((1,index/2),(index,index+1));
- parent_value = self.algo.hash_nodes(¤t_value,&0);
- }
- Some(_k) => {
- self.parenthoods.insert((1,index/2),(index,index+1));
- parent_value = self.algo.hash_nodes(¤t_value,&0);
- }
- }
- }
- //Rounds where only nodes are treated until the root
- for i in 1..self.size {
- //Index update, one level up.
- index /= 2;
- current_value = parent_value.as_ref();
- if index%2 == 0 {
- match self.parenthoods.get(&(i,index+1)) {
- None => {
- self.parenthoods.insert((1,index/2),(¤t_value,&0));
- parent_value = self.algo.hash_nodes(¤t_value,&0);
- }
- Some(niblings) => {
- sibling_value = self.algo.hash_nodes(&niblings.0,&niblings.1);
- self.parenthoods.insert((1,index/2),(¤t_value,&sibling_value));
- parent_value = self.algo.hash_nodes(¤t_value,&sibling_value);
- }
- }
- }
- else {
- match self.parenthoods.get(&(i,index-1)) {
- None => {
- self.parenthoods.insert((1,index/2),(&0,¤t_value));
- parent_value = self.algo.hash_nodes(&0,¤t_value);
- }
- Some(niblings) => {
- sibling_value = self.algo.hash_nodes(&niblings.0,&niblings.1);
- self.parenthoods.insert((1,index/2),(&sibling_value,¤t_value));
- parent_value = self.algo.hash_nodes(&sibling_value,¤t_value);
- }
- }
- }
- }
- }
- }
- fn main (){
- let mut tree: SparseMerkleTree = SparseMerkleTree::new(32,SHA384);
- tree.add_value(54);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement