Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. /*use ring::digest::{Algorithm, Digest};*/
  2. use std::collections::HashMap;
  3.  
  4. /*type Index = u32;
  5. type Value = u32;
  6. type HashValue = u32;
  7. type Level = u8;
  8. */
  9. use ring::digest::{Digest,Context, Algorithm, SHA384};
  10.  
  11. pub struct SparseMerkleTree{
  12. //Max seize = 256 <=> max elements = 2^256
  13. pub size: usize,
  14. //Mapping for Nodes: (level,parentIndex) -> (leftChild,rightChild)
  15. //For Leaves: (0,leafIndex) -> (value(=leafIndex))
  16. pub parenthoods: HashMap<(usize,usize),(usize,usize)>,
  17. pub algo: Algorithm,
  18. }
  19.  
  20.  
  21.  
  22. pub trait Hashable {
  23. /// Update the given `context` with `self`.
  24. ///
  25. /// See `ring::digest::Context::update` for more information.
  26. fn update_context(&self, context: &mut Context);
  27. }
  28.  
  29. impl Hashable for usize {
  30. fn update_context(&self, context: &mut Context) {
  31. context.update(&self.to_be_bytes());
  32. }
  33. }
  34.  
  35. pub trait HashUtils {
  36.  
  37. fn hash_nodes(&'static self, left: &usize, right: &usize) -> Digest;
  38.  
  39. }
  40.  
  41. impl HashUtils for Algorithm {
  42.  
  43. fn hash_nodes(&'static self, left: &usize, right: &usize) -> Digest
  44. {
  45. let mut ctx = Context::new(self);
  46. ctx.update(&[0x01]);
  47. left.update_context(&mut ctx);
  48. right.update_context(&mut ctx);
  49. ctx.finish()
  50. }
  51. }
  52.  
  53. impl SparseMerkleTree {
  54.  
  55. pub fn new (size:usize, algo:Algorithm) -> Self {
  56. SparseMerkleTree{size: size, parenthoods: HashMap::new(), algo}
  57. }
  58.  
  59. /*ub fn hash (&self, a:usize, b:usize) {
  60.  
  61. }*/
  62.  
  63. pub fn add_value (&mut self,value:usize) {
  64.  
  65. let mut parent_value;
  66. let mut sibling_value;
  67. let mut current_value = value;
  68. let mut index = value;
  69. //Inserting the leaf in the hasmap
  70. self.parenthoods.insert((0,value),(0,0));
  71.  
  72. //First round of the loop, similar but different
  73. if index%2 == 0 {
  74. match self.parenthoods.get(&(0,index+1)) {
  75. None => {
  76. self.parenthoods.insert((1,index/2),(index,index+1));
  77. parent_value = self.algo.hash_nodes(&current_value,&0);
  78.  
  79. }
  80. Some(_k) => {
  81. self.parenthoods.insert((1,index/2),(index,index+1));
  82. parent_value = self.algo.hash_nodes(&current_value,&0);
  83. }
  84. }
  85. }
  86. else {
  87. match self.parenthoods.get(&(0,index+1)) {
  88. None => {
  89. self.parenthoods.insert((1,index/2),(index,index+1));
  90. parent_value = self.algo.hash_nodes(&current_value,&0);
  91. }
  92. Some(_k) => {
  93. self.parenthoods.insert((1,index/2),(index,index+1));
  94. parent_value = self.algo.hash_nodes(&current_value,&0);
  95. }
  96. }
  97. }
  98.  
  99. //Rounds where only nodes are treated until the root
  100. for i in 1..self.size {
  101. //Index update, one level up.
  102. index /= 2;
  103. current_value = parent_value.as_ref();
  104. if index%2 == 0 {
  105. match self.parenthoods.get(&(i,index+1)) {
  106. None => {
  107. self.parenthoods.insert((1,index/2),(&current_value,&0));
  108. parent_value = self.algo.hash_nodes(&current_value,&0);
  109. }
  110. Some(niblings) => {
  111. sibling_value = self.algo.hash_nodes(&niblings.0,&niblings.1);
  112. self.parenthoods.insert((1,index/2),(&current_value,&sibling_value));
  113. parent_value = self.algo.hash_nodes(&current_value,&sibling_value);
  114. }
  115. }
  116. }
  117. else {
  118. match self.parenthoods.get(&(i,index-1)) {
  119. None => {
  120. self.parenthoods.insert((1,index/2),(&0,&current_value));
  121. parent_value = self.algo.hash_nodes(&0,&current_value);
  122.  
  123. }
  124. Some(niblings) => {
  125. sibling_value = self.algo.hash_nodes(&niblings.0,&niblings.1);
  126. self.parenthoods.insert((1,index/2),(&sibling_value,&current_value));
  127. parent_value = self.algo.hash_nodes(&sibling_value,&current_value);
  128. }
  129. }
  130. }
  131. }
  132. }
  133. }
  134.  
  135. fn main (){
  136. let mut tree: SparseMerkleTree = SparseMerkleTree::new(32,SHA384);
  137.  
  138. tree.add_value(54);
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement