You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

733 lines
25 KiB

4 years ago
  1. #[macro_use]
  2. extern crate arrayref;
  3. extern crate hex;
  4. extern crate rustc_hex;
  5. extern crate tiny_keccak;
  6. use rustc_hex::ToHex;
  7. pub mod constants;
  8. pub mod db;
  9. pub mod node;
  10. pub mod utils;
  11. type Result<T> = std::result::Result<T, String>;
  12. pub struct TestValue {
  13. pub bytes: Vec<u8>,
  14. pub index_length: u32,
  15. }
  16. pub trait Value {
  17. fn bytes(&self) -> &Vec<u8>;
  18. fn index_length(&self) -> u32;
  19. fn hi(&self) -> [u8; 32];
  20. fn ht(&self) -> [u8; 32];
  21. }
  22. impl Value for TestValue {
  23. fn bytes(&self) -> &Vec<u8> {
  24. &self.bytes
  25. }
  26. fn index_length(&self) -> u32 {
  27. self.index_length
  28. }
  29. fn hi(&self) -> [u8; 32] {
  30. utils::hash_vec(
  31. self.bytes()
  32. .to_vec()
  33. .split_at(self.index_length() as usize)
  34. .0
  35. .to_vec(),
  36. )
  37. }
  38. fn ht(&self) -> [u8; 32] {
  39. utils::hash_vec(self.bytes().to_vec())
  40. }
  41. }
  42. pub struct MerkleTree<'a> {
  43. root: [u8; 32],
  44. num_levels: u32,
  45. sto: &'a mut db::Db,
  46. }
  47. impl<'a> MerkleTree<'a> {
  48. pub fn new(database: &'a mut db::Db, num_levels: u32) -> MerkleTree<'a> {
  49. MerkleTree {
  50. root: constants::EMPTYNODEVALUE,
  51. num_levels,
  52. sto: database,
  53. }
  54. }
  55. pub fn get_root(&mut self) -> [u8; 32] {
  56. self.root
  57. }
  58. pub fn get_num_levels(&mut self) -> u32 {
  59. self.num_levels
  60. }
  61. pub fn add(&mut self, v: &TestValue) -> Result<()> {
  62. // add the leaf that we are adding
  63. self.sto.insert(
  64. v.ht(),
  65. constants::TYPENODEVALUE,
  66. v.index_length(),
  67. v.bytes().to_vec(),
  68. );
  69. let hi = v.hi();
  70. let path = utils::get_path(self.num_levels, hi);
  71. let mut siblings: Vec<[u8; 32]> = Vec::new();
  72. let mut node_hash = self.root;
  73. for i in (0..=self.num_levels - 2).rev() {
  74. // get node
  75. let (t, il, node_bytes) = self.sto.get(&node_hash);
  76. if t == constants::TYPENODEFINAL {
  77. let hi_child =
  78. utils::hash_vec(node_bytes.to_vec().split_at(il as usize).0.to_vec());
  79. let path_child = utils::get_path(self.num_levels, hi_child);
  80. let pos_diff = utils::compare_paths(&path_child, &path);
  81. if pos_diff == -1 {
  82. return Err("node already exists".to_owned());
  83. }
  84. let final_node_1_hash = utils::calc_hash_from_leaf_and_level(
  85. pos_diff as u32,
  86. &path_child,
  87. utils::hash_vec(node_bytes.to_vec()),
  88. );
  89. self.sto.insert(
  90. final_node_1_hash,
  91. constants::TYPENODEFINAL,
  92. il,
  93. node_bytes.to_vec(),
  94. );
  95. let final_node_2_hash =
  96. utils::calc_hash_from_leaf_and_level(pos_diff as u32, &path, v.ht());
  97. self.sto.insert(
  98. final_node_2_hash,
  99. constants::TYPENODEFINAL,
  100. v.index_length(),
  101. v.bytes().to_vec(),
  102. );
  103. // parent node
  104. let parent_node: node::TreeNode;
  105. if path[pos_diff as usize] {
  106. parent_node = node::TreeNode {
  107. child_l: final_node_1_hash,
  108. child_r: final_node_2_hash,
  109. }
  110. } else {
  111. parent_node = node::TreeNode {
  112. child_l: final_node_2_hash,
  113. child_r: final_node_1_hash,
  114. }
  115. }
  116. let empties = utils::get_empties_between_i_and_pos(i, pos_diff as u32 + 1);
  117. for empty in &empties {
  118. siblings.push(*empty);
  119. }
  120. let path_from_pos_diff = utils::cut_path(&path, (pos_diff + 1) as usize);
  121. self.root = self.replace_leaf(
  122. path_from_pos_diff,
  123. &siblings,
  124. parent_node.ht(),
  125. constants::TYPENODENORMAL,
  126. 0,
  127. parent_node.bytes().to_vec(),
  128. );
  129. return Ok(());
  130. }
  131. let node = node::parse_node_bytes(node_bytes);
  132. let sibling: [u8; 32];
  133. if !path[i as usize] {
  134. node_hash = node.child_l;
  135. sibling = node.child_r;
  136. } else {
  137. sibling = node.child_l;
  138. node_hash = node.child_r;
  139. }
  140. siblings.push(*array_ref!(sibling, 0, 32));
  141. if node_hash == constants::EMPTYNODEVALUE {
  142. if i == self.num_levels - 2
  143. && siblings[siblings.len() - 1] == constants::EMPTYNODEVALUE
  144. {
  145. let final_node_hash =
  146. utils::calc_hash_from_leaf_and_level(i + 1, &path, v.ht());
  147. self.sto.insert(
  148. final_node_hash,
  149. constants::TYPENODEFINAL,
  150. v.index_length(),
  151. v.bytes().to_vec(),
  152. );
  153. self.root = final_node_hash;
  154. return Ok(());
  155. }
  156. let final_node_hash = utils::calc_hash_from_leaf_and_level(i, &path, v.ht());
  157. let path_from_i = utils::cut_path(&path, i as usize);
  158. self.root = self.replace_leaf(
  159. path_from_i,
  160. &siblings,
  161. final_node_hash,
  162. constants::TYPENODEFINAL,
  163. v.index_length(),
  164. v.bytes().to_vec(),
  165. );
  166. return Ok(());
  167. }
  168. }
  169. self.root = self.replace_leaf(
  170. path,
  171. &siblings,
  172. v.ht(),
  173. constants::TYPENODEVALUE,
  174. v.index_length(),
  175. v.bytes().to_vec(),
  176. );
  177. return Ok(());
  178. }
  179. pub fn replace_leaf(
  180. &mut self,
  181. path: Vec<bool>,
  182. siblings: &Vec<[u8; 32]>,
  183. leaf_hash: [u8; 32],
  184. node_type: u8,
  185. index_length: u32,
  186. leaf_value: Vec<u8>,
  187. ) -> [u8; 32] {
  188. self.sto
  189. .insert(leaf_hash, node_type, index_length, leaf_value);
  190. let mut curr_node = leaf_hash;
  191. for i in 0..siblings.len() {
  192. if !path[i as usize] {
  193. let node = node::TreeNode {
  194. child_l: curr_node,
  195. child_r: siblings[siblings.len() - 1 - i],
  196. };
  197. self.sto
  198. .insert(node.ht(), constants::TYPENODENORMAL, 0, node.bytes());
  199. curr_node = node.ht();
  200. } else {
  201. let node = node::TreeNode {
  202. child_l: siblings[siblings.len() - 1 - i],
  203. child_r: curr_node,
  204. };
  205. self.sto
  206. .insert(node.ht(), constants::TYPENODENORMAL, 0, node.bytes());
  207. curr_node = node.ht();
  208. }
  209. }
  210. curr_node
  211. }
  212. pub fn get_value_in_pos(&mut self, hi: [u8; 32]) -> Vec<u8> {
  213. let path = utils::get_path(self.num_levels, hi);
  214. let mut node_hash = self.root;
  215. for i in (0..=self.num_levels - 2).rev() {
  216. let (t, il, node_bytes) = self.sto.get(&node_hash);
  217. if t == constants::TYPENODEFINAL {
  218. let hi_node = utils::hash_vec(node_bytes.to_vec().split_at(il as usize).0.to_vec());
  219. let path_node = utils::get_path(self.num_levels, hi_node);
  220. let pos_diff = utils::compare_paths(&path_node, &path);
  221. if pos_diff != -1 {
  222. return constants::EMPTYNODEVALUE.to_vec();
  223. }
  224. return node_bytes;
  225. }
  226. let node = node::parse_node_bytes(node_bytes);
  227. if !path[i as usize] {
  228. node_hash = node.child_l;
  229. } else {
  230. node_hash = node.child_r;
  231. }
  232. }
  233. let (_t, _il, node_bytes) = self.sto.get(&node_hash);
  234. node_bytes
  235. }
  236. pub fn generate_proof(&mut self, hi: [u8; 32]) -> Vec<u8> {
  237. let mut mp: Vec<u8> = Vec::new();
  238. let mut empties: [u8; 32] = [0; 32];
  239. let path = utils::get_path(self.num_levels, hi);
  240. let mut siblings: Vec<[u8; 32]> = Vec::new();
  241. let mut node_hash = self.root;
  242. for i in 0..self.num_levels {
  243. let (t, il, node_bytes) = self.sto.get(&node_hash);
  244. if t == constants::TYPENODEFINAL {
  245. let real_value_in_pos = self.get_value_in_pos(hi);
  246. if real_value_in_pos == constants::EMPTYNODEVALUE {
  247. let leaf_hi =
  248. utils::hash_vec(node_bytes.to_vec().split_at(il as usize).0.to_vec());
  249. let path_child = utils::get_path(self.num_levels, leaf_hi);
  250. let pos_diff = utils::compare_paths(&path_child, &path);
  251. if pos_diff as u32 == self.num_levels {
  252. return mp;
  253. }
  254. if pos_diff as u32 != self.num_levels - 1 - i {
  255. let sibling = utils::calc_hash_from_leaf_and_level(
  256. pos_diff as u32,
  257. &path_child,
  258. utils::hash_vec(node_bytes.to_vec()),
  259. );
  260. let mut new_siblings: Vec<[u8; 32]> = Vec::new();
  261. new_siblings.push(sibling);
  262. new_siblings.extend(siblings);
  263. siblings = new_siblings;
  264. // set empties bit
  265. let bit_pos = self.num_levels - 2 - pos_diff as u32;
  266. empties[(empties.len() as isize + (bit_pos as isize / 8 - 1) as isize)
  267. as usize] |= 1 << (bit_pos % 8);
  268. }
  269. }
  270. break;
  271. }
  272. let node = node::parse_node_bytes(node_bytes);
  273. let sibling: [u8; 32];
  274. if !path[self.num_levels as usize - i as usize - 2] {
  275. node_hash = node.child_l;
  276. sibling = node.child_r;
  277. } else {
  278. sibling = node.child_l;
  279. node_hash = node.child_r;
  280. }
  281. if sibling != constants::EMPTYNODEVALUE {
  282. // set empties bit
  283. empties[(empties.len() as isize + (i as isize / 8 - 1) as isize) as usize] |=
  284. 1 << (i % 8);
  285. let mut new_siblings: Vec<[u8; 32]> = Vec::new();
  286. new_siblings.push(sibling);
  287. new_siblings.extend(siblings);
  288. siblings = new_siblings;
  289. }
  290. }
  291. mp.append(&mut empties[..].to_vec());
  292. for s in siblings {
  293. mp.append(&mut s.to_vec());
  294. }
  295. mp
  296. }
  297. pub fn print_level(&mut self, parent: [u8; 32], mut lvl: u32, max_level: u32) {
  298. let mut line: String = "".to_string();
  299. for _ in 0..lvl {
  300. line += &format!(" ");
  301. }
  302. line += &format!("lvl {}", lvl);
  303. line += &format!(" - '{}' = ", parent.to_hex());
  304. let (t, _, node_bytes) = self.sto.get(&parent);
  305. let mut node = node::TreeNode {
  306. child_l: constants::EMPTYNODEVALUE,
  307. child_r: constants::EMPTYNODEVALUE,
  308. };
  309. if t == constants::TYPENODENORMAL {
  310. node = node::parse_node_bytes(node_bytes);
  311. line += &format!("'{}' - '{}'", node.child_l.to_hex(), node.child_r.to_hex());
  312. } else if t == constants::TYPENODEVALUE {
  313. //
  314. } else if t == constants::TYPENODEFINAL {
  315. let hash_node_bytes = utils::hash_vec(node_bytes);
  316. line += &format!("[final] final tree node: {} \n", hash_node_bytes.to_hex());
  317. let (_, _, leaf_node_bytes) = self.sto.get(&hash_node_bytes);
  318. for _ in 0..lvl {
  319. line += " ";
  320. }
  321. let leaf_node_string = String::from_utf8_lossy(&leaf_node_bytes);
  322. line += &format!("leaf value: {}", leaf_node_string);
  323. } else {
  324. line += "[EMPTY Branch]"
  325. }
  326. println!("{}", line);
  327. lvl += 1;
  328. if node.child_r.len() > 0
  329. && lvl < max_level
  330. && t != constants::TYPENODEEMPTY
  331. && t != constants::TYPENODEFINAL
  332. {
  333. self.print_level(node.child_l, lvl, max_level);
  334. self.print_level(node.child_r, lvl, max_level);
  335. }
  336. }
  337. pub fn print_full_tree(&mut self) {
  338. let root = self.root.clone();
  339. let num_levels = self.num_levels.clone();
  340. self.print_level(root, 0, num_levels - 1);
  341. println!("root {:?}", &self.root.to_hex());
  342. }
  343. pub fn print_levels_tree(&mut self, max_level: u32) {
  344. let root = self.root.clone();
  345. let num_levels = self.num_levels.clone();
  346. self.print_level(root, 0, num_levels - 1 - max_level);
  347. println!("root {:?}", self.root.to_hex());
  348. }
  349. }
  350. pub fn verify_proof(
  351. root: [u8; 32],
  352. mp: &Vec<u8>,
  353. hi: [u8; 32],
  354. ht: [u8; 32],
  355. num_levels: u32,
  356. ) -> bool {
  357. let empties: Vec<u8>;
  358. empties = mp.split_at(32).0.to_vec();
  359. let mut siblings: Vec<[u8; 32]> = Vec::new();
  360. for i in (empties.len()..mp.len()).step_by(constants::EMPTYNODEVALUE.len()) {
  361. let mut sibling: [u8; 32] = [0; 32];
  362. sibling.copy_from_slice(&mp[i..i + constants::EMPTYNODEVALUE.len()]);
  363. siblings.push(sibling);
  364. }
  365. let path = utils::get_path(num_levels, hi);
  366. let mut node_hash = ht;
  367. let mut sibling_used_pos = 0;
  368. for i in (0..=num_levels - 2).rev() {
  369. let sibling: [u8; 32];
  370. if (empties[empties.len() - i as usize / 8 - 1] & (1 << (i % 8))) > 0 {
  371. sibling = siblings[sibling_used_pos];
  372. sibling_used_pos += 1;
  373. } else {
  374. sibling = constants::EMPTYNODEVALUE;
  375. }
  376. let n: node::TreeNode;
  377. if path[num_levels as usize - i as usize - 2] {
  378. n = node::TreeNode {
  379. child_l: sibling,
  380. child_r: node_hash,
  381. }
  382. } else {
  383. n = node::TreeNode {
  384. child_l: node_hash,
  385. child_r: sibling,
  386. }
  387. }
  388. if node_hash == constants::EMPTYNODEVALUE && sibling == constants::EMPTYNODEVALUE {
  389. node_hash = constants::EMPTYNODEVALUE;
  390. } else {
  391. node_hash = n.ht();
  392. }
  393. }
  394. if node_hash == root {
  395. return true;
  396. }
  397. false
  398. }
  399. #[cfg(test)]
  400. mod tests {
  401. use super::*;
  402. use rustc_hex::ToHex;
  403. #[test]
  404. fn test_hash_vec() {
  405. let a: Vec<u8> = From::from("test".to_string());
  406. let h = utils::hash_vec(a);
  407. assert_eq!(
  408. "9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658",
  409. h.to_hex()
  410. );
  411. }
  412. #[test]
  413. fn test_new_mt() {
  414. let mut sto = db::Db::new("test".to_string(), true);
  415. let mt = MerkleTree::new(&mut sto, 140);
  416. assert_eq!(140, mt.num_levels);
  417. assert_eq!(
  418. "0000000000000000000000000000000000000000000000000000000000000000",
  419. mt.root.to_hex()
  420. );
  421. let (_t, _il, b) = mt.sto.get(&[0; 32]);
  422. assert_eq!(mt.root.to_vec(), b);
  423. }
  424. #[test]
  425. fn test_tree_node() {
  426. let n = node::TreeNode {
  427. child_l: [1; 32],
  428. child_r: [2; 32],
  429. };
  430. assert_eq!("01010101010101010101010101010101010101010101010101010101010101010202020202020202020202020202020202020202020202020202020202020202",
  431. n.bytes().to_hex());
  432. assert_eq!(
  433. "346d8c96a2454213fcc0daff3c96ad0398148181b9fa6488f7ae2c0af5b20aa0",
  434. n.ht().to_hex()
  435. );
  436. }
  437. #[test]
  438. fn test_add() {
  439. let mut sto = db::Db::new("test".to_string(), true);
  440. let mut mt = MerkleTree::new(&mut sto, 140);
  441. assert_eq!(
  442. "0000000000000000000000000000000000000000000000000000000000000000",
  443. mt.root.to_hex()
  444. );
  445. let val = TestValue {
  446. bytes: vec![1, 2, 3, 4, 5],
  447. index_length: 3,
  448. };
  449. mt.add(&val).unwrap();
  450. let (_t, _il, b) = mt.sto.get(&val.ht());
  451. assert_eq!(*val.bytes(), b);
  452. assert_eq!(
  453. "a0e72cc948119fcb71b413cf5ada12b2b825d5133299b20a6d9325ffc3e2fbf1",
  454. mt.root.to_hex()
  455. );
  456. }
  457. #[test]
  458. fn test_add_2() {
  459. let mut sto = db::Db::new("test".to_string(), true);
  460. let mut mt = MerkleTree::new(&mut sto, 140);
  461. let val = TestValue {
  462. bytes: "this is a test leaf".as_bytes().to_vec(),
  463. index_length: 15,
  464. };
  465. assert_eq!(
  466. "0000000000000000000000000000000000000000000000000000000000000000",
  467. mt.root.to_hex()
  468. );
  469. mt.add(&val).unwrap();
  470. let (_t, _il, b) = mt.sto.get(&val.ht());
  471. assert_eq!(*val.bytes(), b);
  472. assert_eq!(
  473. "b4fdf8a653198f0e179ccb3af7e4fc09d76247f479d6cfc95cd92d6fda589f27",
  474. mt.root.to_hex()
  475. );
  476. let val2 = TestValue {
  477. bytes: "this is a second test leaf".as_bytes().to_vec(),
  478. index_length: 15,
  479. };
  480. mt.add(&val2).unwrap();
  481. let (_t, _il, b) = mt.sto.get(&val2.ht());
  482. assert_eq!(*val2.bytes(), b);
  483. assert_eq!(
  484. "8ac95e9c8a6fbd40bb21de7895ee35f9c8f30ca029dbb0972c02344f49462e82",
  485. mt.root.to_hex()
  486. );
  487. }
  488. #[test]
  489. fn test_generate_proof_and_verify_proof() {
  490. let mut sto = db::Db::new("test".to_string(), true);
  491. let mut mt = MerkleTree::new(&mut sto, 140);
  492. let val = TestValue {
  493. bytes: "this is a test leaf".as_bytes().to_vec(),
  494. index_length: 15,
  495. };
  496. assert_eq!(
  497. "0000000000000000000000000000000000000000000000000000000000000000",
  498. mt.root.to_hex()
  499. );
  500. mt.add(&val).unwrap();
  501. let (_t, _il, b) = mt.sto.get(&val.ht());
  502. assert_eq!(*val.bytes(), b);
  503. assert_eq!(
  504. "b4fdf8a653198f0e179ccb3af7e4fc09d76247f479d6cfc95cd92d6fda589f27",
  505. mt.root.to_hex()
  506. );
  507. let val2 = TestValue {
  508. bytes: "this is a second test leaf".as_bytes().to_vec(),
  509. index_length: 15,
  510. };
  511. mt.add(&val2).unwrap();
  512. let (_t, _il, b) = mt.sto.get(&val2.ht());
  513. assert_eq!(*val2.bytes(), b);
  514. assert_eq!(
  515. "8ac95e9c8a6fbd40bb21de7895ee35f9c8f30ca029dbb0972c02344f49462e82",
  516. mt.root.to_hex()
  517. );
  518. let mp = mt.generate_proof(val2.hi());
  519. assert_eq!("0000000000000000000000000000000000000000000000000000000000000001fd8e1a60cdb23c0c7b2cf8462c99fafd905054dccb0ed75e7c8a7d6806749b6b", mp.to_hex());
  520. // verify
  521. let v = verify_proof(mt.root, &mp, val2.hi(), val2.ht(), mt.num_levels);
  522. assert_eq!(true, v);
  523. }
  524. #[test]
  525. fn test_generate_proof_empty_leaf_and_verify_proof() {
  526. let mut sto = db::Db::new("test".to_string(), true);
  527. let mut mt = MerkleTree::new(&mut sto, 140);
  528. let val = TestValue {
  529. bytes: "this is a test leaf".as_bytes().to_vec(),
  530. index_length: 15,
  531. };
  532. mt.add(&val).unwrap();
  533. let val2 = TestValue {
  534. bytes: "this is a second test leaf".as_bytes().to_vec(),
  535. index_length: 15,
  536. };
  537. mt.add(&val2).unwrap();
  538. assert_eq!(
  539. "8ac95e9c8a6fbd40bb21de7895ee35f9c8f30ca029dbb0972c02344f49462e82",
  540. mt.root.to_hex()
  541. );
  542. // proof of empty leaf
  543. let val3 = TestValue {
  544. bytes: "this is a third test leaf".as_bytes().to_vec(),
  545. index_length: 15,
  546. };
  547. let mp = mt.generate_proof(val3.hi());
  548. assert_eq!("000000000000000000000000000000000000000000000000000000000000000389741fa23da77c259781ad8f4331a5a7d793eef1db7e5200ddfc8e5f5ca7ce2bfd8e1a60cdb23c0c7b2cf8462c99fafd905054dccb0ed75e7c8a7d6806749b6b", mp.to_hex());
  549. // verify that is a proof of an empty leaf (constants::EMPTYNODEVALUE)
  550. let v = verify_proof(
  551. mt.root,
  552. &mp,
  553. val3.hi(),
  554. constants::EMPTYNODEVALUE,
  555. mt.num_levels,
  556. );
  557. assert_eq!(true, v);
  558. }
  559. #[test]
  560. fn test_harcoded_proofs_of_existing_leaf() {
  561. // check proof of value in leaf
  562. let mut root: [u8; 32] = [0; 32];
  563. root.copy_from_slice(
  564. &hex::decode("7d7c5e8f4b3bf434f3d9d223359c4415e2764dd38de2e025fbf986e976a7ed3d")
  565. .unwrap(),
  566. );
  567. let mp = hex::decode("0000000000000000000000000000000000000000000000000000000000000002d45aada6eec346222eaa6b5d3a9260e08c9b62fcf63c72bc05df284de07e6a52").unwrap();
  568. let mut hi: [u8; 32] = [0; 32];
  569. hi.copy_from_slice(
  570. &hex::decode("786677808ba77bdd9090a969f1ef2cbd1ac5aecd9e654f340500159219106878")
  571. .unwrap(),
  572. );
  573. let mut ht: [u8; 32] = [0; 32];
  574. ht.copy_from_slice(
  575. &hex::decode("786677808ba77bdd9090a969f1ef2cbd1ac5aecd9e654f340500159219106878")
  576. .unwrap(),
  577. );
  578. let v = verify_proof(root, &mp, hi, ht, 140);
  579. assert_eq!(true, v);
  580. }
  581. #[test]
  582. fn test_harcoded_proofs_of_empty_leaf() {
  583. // check proof of value in leaf
  584. let mut root: [u8; 32] = [0; 32];
  585. root.copy_from_slice(
  586. &hex::decode("8f021d00c39dcd768974ddfe0d21f5d13f7215bea28db1f1cb29842b111332e7")
  587. .unwrap(),
  588. );
  589. let mp = hex::decode("0000000000000000000000000000000000000000000000000000000000000004bf8e980d2ed328ae97f65c30c25520aeb53ff837579e392ea1464934c7c1feb9").unwrap();
  590. let mut hi: [u8; 32] = [0; 32];
  591. hi.copy_from_slice(
  592. &hex::decode("a69792a4cff51f40b7a1f7ae596c6ded4aba241646a47538898f17f2a8dff647")
  593. .unwrap(),
  594. );
  595. let v = verify_proof(root, &mp, hi, constants::EMPTYNODEVALUE, 140);
  596. assert_eq!(true, v);
  597. }
  598. #[test]
  599. fn test_add_leafs_different_order() {
  600. let mut sto1 = db::Db::new("test".to_string(), true);
  601. let mut mt1 = MerkleTree::new(&mut sto1, 140);
  602. mt1.add(&TestValue {
  603. bytes: "0 this is a test leaf".as_bytes().to_vec(),
  604. index_length: 15,
  605. })
  606. .unwrap();
  607. mt1.add(&TestValue {
  608. bytes: "1 this is a test leaf".as_bytes().to_vec(),
  609. index_length: 15,
  610. })
  611. .unwrap();
  612. mt1.add(&TestValue {
  613. bytes: "2 this is a test leaf".as_bytes().to_vec(),
  614. index_length: 15,
  615. })
  616. .unwrap();
  617. mt1.add(&TestValue {
  618. bytes: "3 this is a test leaf".as_bytes().to_vec(),
  619. index_length: 15,
  620. })
  621. .unwrap();
  622. mt1.add(&TestValue {
  623. bytes: "4 this is a test leaf".as_bytes().to_vec(),
  624. index_length: 15,
  625. })
  626. .unwrap();
  627. mt1.add(&TestValue {
  628. bytes: "5 this is a test leaf".as_bytes().to_vec(),
  629. index_length: 15,
  630. })
  631. .unwrap();
  632. // mt1.print_full_tree();
  633. let mut sto2 = db::Db::new("test".to_string(), true);
  634. let mut mt2 = MerkleTree::new(&mut sto2, 140);
  635. mt2.add(&TestValue {
  636. bytes: "2 this is a test leaf".as_bytes().to_vec(),
  637. index_length: 15,
  638. })
  639. .unwrap();
  640. mt2.add(&TestValue {
  641. bytes: "1 this is a test leaf".as_bytes().to_vec(),
  642. index_length: 15,
  643. })
  644. .unwrap();
  645. mt2.add(&TestValue {
  646. bytes: "0 this is a test leaf".as_bytes().to_vec(),
  647. index_length: 15,
  648. })
  649. .unwrap();
  650. mt2.add(&TestValue {
  651. bytes: "5 this is a test leaf".as_bytes().to_vec(),
  652. index_length: 15,
  653. })
  654. .unwrap();
  655. mt2.add(&TestValue {
  656. bytes: "3 this is a test leaf".as_bytes().to_vec(),
  657. index_length: 15,
  658. })
  659. .unwrap();
  660. mt2.add(&TestValue {
  661. bytes: "4 this is a test leaf".as_bytes().to_vec(),
  662. index_length: 15,
  663. })
  664. .unwrap();
  665. // mt2.print_full_tree();
  666. assert_eq!(mt1.root, mt2.root);
  667. assert_eq!(
  668. &mt1.root.to_hex(),
  669. "264397f84da141b3134dcde1d7540d27a2bf0d787bbe8365d9ad5c9c18d3c621"
  670. );
  671. }
  672. #[test]
  673. fn test_add_1000_leafs() {
  674. let mut sto = db::Db::new("test".to_string(), true);
  675. let mut mt = MerkleTree::new(&mut sto, 140);
  676. for i in 0..1000 {
  677. mt.add(&TestValue {
  678. bytes: (i.to_string() + " this is a test leaf").as_bytes().to_vec(),
  679. index_length: 15,
  680. })
  681. .unwrap();
  682. }
  683. assert_eq!(
  684. mt.root.to_hex(),
  685. "6e2da580b2920cd78ed8d4e4bf41e209dfc99ef28bc19560042f0ac803e0d6f7"
  686. );
  687. }
  688. }