1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the snarkVM library.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
mod helpers;
pub use helpers::*;
mod path;
pub use path::*;
#[cfg(test)]
mod tests;
use snarkvm_console_types::prelude::*;
use aleo_std::prelude::*;
use std::collections::BTreeMap;
#[cfg(not(feature = "serial"))]
use rayon::prelude::*;
#[derive(Clone)]
pub struct MerkleTree<E: Environment, LH: LeafHash<Hash = PH::Hash>, PH: PathHash<Hash = Field<E>>, const DEPTH: u8> {
/// The leaf hasher for the Merkle tree.
leaf_hasher: LH,
/// The path hasher for the Merkle tree.
path_hasher: PH,
/// The computed root of the full Merkle tree.
root: PH::Hash,
/// The internal hashes, from root to hashed leaves, of the full Merkle tree.
tree: Vec<PH::Hash>,
/// The canonical empty hash.
empty_hash: Field<E>,
/// The number of hashed leaves in the tree.
number_of_leaves: usize,
}
impl<E: Environment, LH: LeafHash<Hash = PH::Hash>, PH: PathHash<Hash = Field<E>>, const DEPTH: u8>
MerkleTree<E, LH, PH, DEPTH>
{
#[inline]
/// Initializes a new Merkle tree with the given leaves.
pub fn new(leaf_hasher: &LH, path_hasher: &PH, leaves: &[LH::Leaf]) -> Result<Self> {
let timer = timer!("MerkleTree::new");
// Ensure the Merkle tree depth is greater than 0.
ensure!(DEPTH > 0, "Merkle tree depth must be greater than 0");
// Ensure the Merkle tree depth is less than or equal to 64.
ensure!(DEPTH <= 64u8, "Merkle tree depth must be less than or equal to 64");
// Compute the maximum number of leaves.
let max_leaves = match leaves.len().checked_next_power_of_two() {
Some(num_leaves) => num_leaves,
None => bail!("Integer overflow when computing the maximum number of leaves in the Merkle tree"),
};
// Compute the number of nodes.
let num_nodes = max_leaves - 1;
// Compute the tree size as the maximum number of leaves plus the number of nodes.
let tree_size = max_leaves + num_nodes;
// Compute the number of levels in the Merkle tree (i.e. log2(tree_size)).
let tree_depth = tree_depth::<DEPTH>(tree_size)?;
// Compute the number of padded levels.
let padding_depth = DEPTH - tree_depth;
// Compute the empty hash.
let empty_hash = path_hasher.hash_empty()?;
// Initialize the Merkle tree.
let mut tree = vec![empty_hash; tree_size];
// Compute and store each leaf hash.
tree[num_nodes..num_nodes + leaves.len()].copy_from_slice(&leaf_hasher.hash_leaves(leaves)?);
lap!(timer, "Hashed {} leaves", leaves.len());
// Compute and store the hashes for each level, iterating from the penultimate level to the root level.
let mut start_index = num_nodes;
// Compute the start index of the current level.
while let Some(start) = parent(start_index) {
// Compute the end index of the current level.
let end = left_child(start);
// Construct the children for each node in the current level.
let tuples = (start..end).map(|i| (tree[left_child(i)], tree[right_child(i)])).collect::<Vec<_>>();
// Compute and store the hashes for each node in the current level.
tree[start..end].copy_from_slice(&path_hasher.hash_all_children(&tuples)?);
// Update the start index for the next level.
start_index = start;
}
lap!(timer, "Hashed {} levels", tree_depth);
// Compute the root hash, by iterating from the root level up to `DEPTH`.
let mut root_hash = tree[0];
for _ in 0..padding_depth {
// Update the root hash, by hashing the current root hash with the empty hash.
root_hash = path_hasher.hash_children(&root_hash, &empty_hash)?;
}
lap!(timer, "Hashed {} padding levels", padding_depth);
finish!(timer);
Ok(Self {
leaf_hasher: leaf_hasher.clone(),
path_hasher: path_hasher.clone(),
root: root_hash,
tree,
empty_hash,
number_of_leaves: leaves.len(),
})
}
#[inline]
/// Returns a new Merkle tree with the given new leaves appended to it.
pub fn prepare_append(&self, new_leaves: &[LH::Leaf]) -> Result<Self> {
let timer = timer!("MerkleTree::prepare_append");
// Compute the maximum number of leaves.
let max_leaves = match (self.number_of_leaves + new_leaves.len()).checked_next_power_of_two() {
Some(num_leaves) => num_leaves,
None => bail!("Integer overflow when computing the maximum number of leaves in the Merkle tree"),
};
// Compute the number of nodes.
let num_nodes = max_leaves - 1;
// Compute the tree size as the maximum number of leaves plus the number of nodes.
let tree_size = num_nodes + max_leaves;
// Compute the number of levels in the Merkle tree (i.e. log2(tree_size)).
let tree_depth = tree_depth::<DEPTH>(tree_size)?;
// Compute the number of padded levels.
let padding_depth = DEPTH - tree_depth;
// Initialize the Merkle tree.
let mut tree = vec![self.empty_hash; num_nodes];
// Extend the new Merkle tree with the existing leaf hashes.
tree.extend(self.leaf_hashes()?);
// Extend the new Merkle tree with the new leaf hashes.
tree.extend(&self.leaf_hasher.hash_leaves(new_leaves)?);
// Resize the new Merkle tree with empty hashes to pad up to `tree_size`.
tree.resize(tree_size, self.empty_hash);
lap!(timer, "Hashed {} new leaves", new_leaves.len());
// Initialize a start index to track the starting index of the current level.
let start_index = num_nodes;
// Initialize a middle index to separate the precomputed indices from the new indices that need to be computed.
let middle_index = num_nodes + self.number_of_leaves;
// Initialize a precompute index to track the starting index of each precomputed level.
let start_precompute_index = match self.number_of_leaves.checked_next_power_of_two() {
Some(num_leaves) => num_leaves - 1,
None => bail!("Integer overflow when computing the Merkle tree precompute index"),
};
// Initialize a precompute index to track the middle index of each precomputed level.
let middle_precompute_index = match num_nodes == start_precompute_index {
// If the old tree and new tree are of the same size, then we can copy over the right half of the old tree.
true => Some(start_precompute_index + self.number_of_leaves + new_leaves.len() + 1),
// Otherwise, we need to compute the right half of the new tree.
false => None,
};
// Compute and store the hashes for each level, iterating from the penultimate level to the root level.
self.compute_updated_tree(
&mut tree,
start_index,
middle_index,
start_precompute_index,
middle_precompute_index,
)?;
// Compute the root hash, by iterating from the root level up to `DEPTH`.
let mut root_hash = tree[0];
for _ in 0..padding_depth {
// Update the root hash, by hashing the current root hash with the empty hash.
root_hash = self.path_hasher.hash_children(&root_hash, &self.empty_hash)?;
}
lap!(timer, "Hashed {} padding levels", padding_depth);
finish!(timer);
Ok(Self {
leaf_hasher: self.leaf_hasher.clone(),
path_hasher: self.path_hasher.clone(),
root: root_hash,
tree,
empty_hash: self.empty_hash,
number_of_leaves: self.number_of_leaves + new_leaves.len(),
})
}
#[inline]
/// Updates the Merkle tree with the given new leaves appended to it.
pub fn append(&mut self, new_leaves: &[LH::Leaf]) -> Result<()> {
let timer = timer!("MerkleTree::append");
// Compute the updated Merkle tree with the new leaves.
let updated_tree = self.prepare_append(new_leaves)?;
// Update the tree at the very end, so the original tree is not altered in case of failure.
*self = updated_tree;
finish!(timer);
Ok(())
}
#[inline]
/// Updates the Merkle tree at the location of the given leaf index with the new leaf.
pub fn update(&mut self, leaf_index: usize, new_leaf: &LH::Leaf) -> Result<()> {
let timer = timer!("MerkleTree::update");
// Compute the updated Merkle tree with the new leaves.
let updated_tree = self.prepare_update(leaf_index, new_leaf)?;
// Update the tree at the very end, so the original tree is not altered in case of failure.
*self = updated_tree;
finish!(timer);
Ok(())
}
#[inline]
/// Returns a new Merkle tree with updates at the location of the given leaf index with the new leaf.
pub fn prepare_update(&self, leaf_index: usize, new_leaf: &LH::Leaf) -> Result<Self> {
let timer = timer!("MerkleTree::prepare_update");
// Check that the leaf index is within the bounds of the Merkle tree.
ensure!(
leaf_index < self.number_of_leaves,
"Leaf index must be less than the number of leaves in the Merkle tree {leaf_index} , {}",
self.number_of_leaves
);
// Allocate a vector to store the path hashes.
let mut path_hashes = Vec::with_capacity(DEPTH as usize);
// Compute and add the new leaf hash to the path hashes.
path_hashes.push(self.leaf_hasher.hash_leaf(new_leaf)?);
lap!(timer, "Hashed 1 new leaf");
// Compute the start index (on the left) for the leaf hashes level in the Merkle tree.
let start = match self.number_of_leaves.checked_next_power_of_two() {
Some(num_leaves) => num_leaves - 1,
None => bail!("Integer overflow when computing the Merkle tree start index"),
};
// Compute the new hashes for the path from the leaf to the root.
let mut index = start + leaf_index;
while let Some(parent) = parent(index) {
// Get the left and right child hashes of the parent.
let (left, right) = match is_left_child(index) {
true => (path_hashes.last().unwrap(), &self.tree[right_child(parent)]),
false => (&self.tree[left_child(parent)], path_hashes.last().unwrap()),
};
// Compute and add the new parent hash to the path hashes.
path_hashes.push(self.path_hasher.hash_children(left, right)?);
// Update the index to the parent.
index = parent;
}
// Compute the number of levels in the Merkle tree (i.e. log2(tree_size)).
let tree_depth = tree_depth::<DEPTH>(self.tree.len())?;
// Compute the padding depth.
let padding_depth = DEPTH - tree_depth;
// Update the root hash.
// This unwrap is safe, as the path hashes vector is guaranteed to have at least one element.
let mut root_hash = *path_hashes.last().unwrap();
for _ in 0..padding_depth {
// Update the root hash, by hashing the current root hash with the empty hash.
root_hash = self.path_hasher.hash_children(&root_hash, &self.empty_hash)?;
}
lap!(timer, "Hashed {} padding levels", padding_depth);
// Initialize the Merkle tree.
let mut tree = Vec::with_capacity(self.tree.len());
// Extend the new Merkle tree with the existing leaf hashes.
tree.extend(&self.tree);
// Update the rest of the tree with the new path hashes.
let mut index = Some(start + leaf_index);
for path_hash in path_hashes {
tree[index.unwrap()] = path_hash;
index = parent(index.unwrap());
}
finish!(timer);
Ok(Self {
leaf_hasher: self.leaf_hasher.clone(),
path_hasher: self.path_hasher.clone(),
root: root_hash,
tree,
empty_hash: self.empty_hash,
number_of_leaves: self.number_of_leaves,
})
}
#[inline]
/// Updates the Merkle tree at the location of the given leaf indices with the new leaves.
pub fn update_many(&mut self, updates: &BTreeMap<usize, LH::Leaf>) -> Result<()> {
let timer = timer!("MerkleTree::update_many");
// Check that there are updates to perform.
ensure!(!updates.is_empty(), "There must be at least one leaf to update in the Merkle tree");
// Check that the latest leaf index is less than number of leaves in the Merkle tree.
// Note: This unwrap is safe since updates is guaranteed to be non-empty.
ensure!(
*updates.last_key_value().unwrap().0 < self.number_of_leaves,
"Leaf index must be less than the number of leaves in the Merkle tree"
);
// Compute the start index (on the left) for the leaf hashes level in the Merkle tree.
let start = match self.number_of_leaves.checked_next_power_of_two() {
Some(num_leaves) => num_leaves - 1,
None => bail!("Integer overflow when computing the Merkle tree start index"),
};
// A helper to compute the leaf hash.
let hash_update = |(leaf_index, leaf): &(&usize, &LH::Leaf)| {
self.leaf_hasher.hash_leaf(leaf).map(|hash| (start + **leaf_index, hash))
};
// Hash the leaves and add them to the updated hashes.
let leaf_hashes: Vec<(usize, LH::Hash)> = match updates.len() {
0..=100 => updates.iter().map(|update| hash_update(&update)).collect::<Result<Vec<_>>>()?,
_ => cfg_iter!(updates).map(|update| hash_update(&update)).collect::<Result<Vec<_>>>()?,
};
lap!(timer, "Hashed {} new leaves", leaf_hashes.len());
// Store the updated hashes by level.
let mut updated_hashes = vec![leaf_hashes];
// A helper function to compute the path hashes for a given level.
type Update<PH> = (usize, (<PH as PathHash>::Hash, <PH as PathHash>::Hash));
let compute_path_hashes = |inputs: &[Update<PH>]| match inputs.len() {
0..=100 => inputs
.iter()
.map(|(index, (left, right))| self.path_hasher.hash_children(left, right).map(|hash| (*index, hash)))
.collect::<Result<Vec<_>>>(),
_ => cfg_iter!(inputs)
.map(|(index, (left, right))| self.path_hasher.hash_children(left, right).map(|hash| (*index, hash)))
.collect::<Result<Vec<_>>>(),
};
// Compute the depth of the tree. This corresponds to the number of levels of hashes in the tree.
let tree_depth = tree_depth::<DEPTH>(self.tree.len())?;
// Allocate a vector to store the inputs to the path hasher.
let mut inputs = Vec::with_capacity(updated_hashes[0].len());
// For each level in the tree, compute the path hashes.
// In the first iteration, we compute the path hashes for the updated leaf hashes.
// In the subsequent iterations, we compute the path hashes for the updated path hashes, until we reach the root.
for level in 0..tree_depth as usize {
let mut current = 0;
while current < updated_hashes[level].len() {
let (current_leaf_index, current_leaf_hash) = updated_hashes[level][current];
// Get the sibling of the current leaf.
let sibling_leaf_index = match sibling(current_leaf_index) {
Some(sibling_index) => sibling_index,
// If there is no sibling, then we have reached the root.
None => break,
};
// Check if the sibling hash is the next hash in the vector.
let sibling_is_next_hash = match current + 1 < updated_hashes[level].len() {
true => updated_hashes[level][current + 1].0 == sibling_leaf_index,
false => false,
};
// Get the sibling hash.
// Note: This algorithm assumes that the sibling hash is either the next hash in the vector,
// or in the original Merkle tree. Consequently, updates need to be provided in sequential order.
// This is enforced by the type of `updates: `BTreeMap<usize, LH::Leaf>`.
// If this assumption is violated, then the algorithm will compute incorrect path hashes in the Merkle tree.
let sibling_leaf_hash = match sibling_is_next_hash {
true => updated_hashes[level][current + 1].1,
false => self.tree[sibling_leaf_index],
};
// Order the current and sibling hashes.
let (left, right) = match is_left_child(current_leaf_index) {
true => (current_leaf_hash, sibling_leaf_hash),
false => (sibling_leaf_hash, current_leaf_hash),
};
// Compute the parent index.
// Note that this unwrap is safe, since we check that the `current_leaf_index` is not the root.
let parent_index = parent(current_leaf_index).unwrap();
// Add the parent hash to the updated hashes.
inputs.push((parent_index, (left, right)));
// Update the current index.
match sibling_is_next_hash {
true => current += 2,
false => current += 1,
}
}
// Compute the path hashes for the current level.
let path_hashes = compute_path_hashes(&inputs)?;
// Add the path hashes to the updated hashes.
updated_hashes.push(path_hashes);
// Clear the inputs.
inputs.clear();
}
// Compute the padding depth.
let padding_depth = DEPTH - tree_depth;
// Update the root hash.
// This unwrap is safe, as the updated hashes is guaranteed to have at least one element.
let mut root_hash = updated_hashes.last().unwrap()[0].1;
for _ in 0..padding_depth {
// Update the root hash, by hashing the current root hash with the empty hash.
root_hash = self.path_hasher.hash_children(&root_hash, &self.empty_hash)?;
}
lap!(timer, "Hashed {} padding levels", padding_depth);
// Update the root hash.
self.root = root_hash;
// Update the rest of the tree with the updated hashes.
for (index, hash) in updated_hashes.into_iter().flatten() {
self.tree[index] = hash;
}
finish!(timer);
Ok(())
}
#[inline]
/// Returns a new Merkle tree with the last 'n' leaves removed from it.
pub fn prepare_remove_last_n(&self, n: usize) -> Result<Self> {
let timer = timer!("MerkleTree::prepare_remove_last_n");
ensure!(n > 0, "Cannot remove zero leaves from the Merkle tree");
// Determine the updated number of leaves, after removing the last 'n' leaves.
let updated_number_of_leaves = self.number_of_leaves.checked_sub(n).ok_or_else(|| {
anyhow!("Failed to remove '{n}' leaves from the Merkle tree, as it only contains {}", self.number_of_leaves)
})?;
// Compute the maximum number of leaves.
let max_leaves = match (updated_number_of_leaves).checked_next_power_of_two() {
Some(num_leaves) => num_leaves,
None => bail!("Integer overflow when computing the maximum number of leaves in the Merkle tree"),
};
// Compute the number of nodes.
let num_nodes = max_leaves - 1;
// Compute the tree size as the maximum number of leaves plus the number of nodes.
let tree_size = num_nodes + max_leaves;
// Compute the number of levels in the Merkle tree (i.e. log2(tree_size)).
let tree_depth = tree_depth::<DEPTH>(tree_size)?;
// Compute the number of padded levels.
let padding_depth = DEPTH - tree_depth;
// Initialize the Merkle tree.
let mut tree = vec![self.empty_hash; num_nodes];
// Extend the new Merkle tree with the existing leaf hashes, excluding the last 'n' leaves.
tree.extend(&self.leaf_hashes()?[..updated_number_of_leaves]);
// Resize the new Merkle tree with empty hashes to pad up to `tree_size`.
tree.resize(tree_size, self.empty_hash);
lap!(timer, "Resizing to {} leaves", updated_number_of_leaves);
// Initialize a start index to track the starting index of the current level.
let start_index = num_nodes;
// Initialize a middle index to separate the precomputed indices from the new indices that need to be computed.
let middle_index = num_nodes + updated_number_of_leaves;
// Initialize a precompute index to track the starting index of each precomputed level.
let start_precompute_index = match self.number_of_leaves.checked_next_power_of_two() {
Some(num_leaves) => num_leaves - 1,
None => bail!("Integer overflow when computing the Merkle tree precompute index"),
};
// Initialize a precompute index to track the middle index of each precomputed level.
let middle_precompute_index = match num_nodes == start_precompute_index {
// If the old tree and new tree are of the same size, then we can copy over the right half of the old tree.
true => Some(start_precompute_index + self.number_of_leaves + 1),
// true => None,
// Otherwise, do nothing, since shrinking the tree is already free.
false => None,
};
// Compute and store the hashes for each level, iterating from the penultimate level to the root level.
self.compute_updated_tree(
&mut tree,
start_index,
middle_index,
start_precompute_index,
middle_precompute_index,
)?;
// Compute the root hash, by iterating from the root level up to `DEPTH`.
let mut root_hash = tree[0];
for _ in 0..padding_depth {
// Update the root hash, by hashing the current root hash with the empty hash.
root_hash = self.path_hasher.hash_children(&root_hash, &self.empty_hash)?;
}
lap!(timer, "Hashed {} padding levels", padding_depth);
finish!(timer);
Ok(Self {
leaf_hasher: self.leaf_hasher.clone(),
path_hasher: self.path_hasher.clone(),
root: root_hash,
tree,
empty_hash: self.empty_hash,
number_of_leaves: updated_number_of_leaves,
})
}
#[inline]
/// Updates the Merkle tree with the last 'n' leaves removed from it.
pub fn remove_last_n(&mut self, n: usize) -> Result<()> {
let timer = timer!("MerkleTree::remove_last_n");
// Compute the updated Merkle tree with the last 'n' leaves removed.
let updated_tree = self.prepare_remove_last_n(n)?;
// Update the tree at the very end, so the original tree is not altered in case of failure.
*self = updated_tree;
finish!(timer);
Ok(())
}
#[inline]
/// Returns the Merkle path for the given leaf index and leaf.
pub fn prove(&self, leaf_index: usize, leaf: &LH::Leaf) -> Result<MerklePath<E, DEPTH>> {
// Ensure the leaf index is valid.
ensure!(leaf_index < self.number_of_leaves, "The given Merkle leaf index is out of bounds");
// Compute the leaf hash.
let leaf_hash = self.leaf_hasher.hash_leaf(leaf)?;
// Compute the start index (on the left) for the leaf hashes level in the Merkle tree.
let start = match self.number_of_leaves.checked_next_power_of_two() {
Some(num_leaves) => num_leaves - 1,
None => bail!("Integer overflow when computing the Merkle tree start index"),
};
// Compute the absolute index of the leaf in the Merkle tree.
let mut index = start + leaf_index;
// Ensure the leaf index is valid.
ensure!(index < self.tree.len(), "The given Merkle leaf index is out of bounds");
// Ensure the leaf hash matches the one in the tree.
ensure!(self.tree[index] == leaf_hash, "The given Merkle leaf does not match the one in the Merkle tree");
// Initialize a vector for the Merkle path.
let mut path = Vec::with_capacity(DEPTH as usize);
// Iterate from the leaf hash to the root level, storing the sibling hashes along the path.
for _ in 0..DEPTH {
// Compute the index of the sibling hash, if it exists.
if let Some(sibling) = sibling(index) {
// Append the sibling hash to the path.
path.push(self.tree[sibling]);
// Compute the index of the parent hash, if it exists.
match parent(index) {
// Update the index to the parent index.
Some(parent) => index = parent,
// If the parent does not exist, the path is complete.
None => break,
}
}
}
// If the Merkle path length is not equal to `DEPTH`, pad the path with the empty hash.
path.resize(DEPTH as usize, self.empty_hash);
// Return the Merkle path.
MerklePath::try_from((U64::new(leaf_index as u64), path))
}
/// Returns `true` if the given Merkle path is valid for the given root and leaf.
pub fn verify(&self, path: &MerklePath<E, DEPTH>, root: &PH::Hash, leaf: &LH::Leaf) -> bool {
path.verify(&self.leaf_hasher, &self.path_hasher, root, leaf)
}
/// Returns the Merkle root of the tree.
pub const fn root(&self) -> &PH::Hash {
&self.root
}
/// Returns the Merkle tree (excluding the hashes of the leaves).
pub fn tree(&self) -> &[PH::Hash] {
&self.tree
}
/// Returns the empty hash.
pub const fn empty_hash(&self) -> &PH::Hash {
&self.empty_hash
}
/// Returns the leaf hashes from the Merkle tree.
pub fn leaf_hashes(&self) -> Result<&[LH::Hash]> {
// Compute the start index (on the left) for the leaf hashes level in the Merkle tree.
let start = match self.number_of_leaves.checked_next_power_of_two() {
Some(num_leaves) => num_leaves - 1,
None => bail!("Integer overflow when computing the Merkle tree start index"),
};
// Compute the end index (on the right) for the leaf hashes level in the Merkle tree.
let end = start + self.number_of_leaves;
// Return the leaf hashes.
Ok(&self.tree[start..end])
}
/// Returns the number of leaves in the Merkle tree.
pub const fn number_of_leaves(&self) -> usize {
self.number_of_leaves
}
/// Compute and store the hashes for each level, iterating from the penultimate level to the root level.
///
/// ```ignore
/// start_index middle_index end_index
/// start_precompute_index middle_precompute_index end_index
/// ```
#[inline]
fn compute_updated_tree(
&self,
tree: &mut [Field<E>],
mut start_index: usize,
mut middle_index: usize,
mut start_precompute_index: usize,
mut middle_precompute_index: Option<usize>,
) -> Result<()> {
// Initialize a timer for the while loop.
let timer = timer!("MerkleTree::compute_updated_tree");
// Compute and store the hashes for each level, iterating from the penultimate level to the root level.
while let (Some(start), Some(middle)) = (parent(start_index), parent(middle_index)) {
// Compute the end index of the current level.
let end = left_child(start);
// If the current level has precomputed indices, copy them instead of recomputing them.
if let Some(start_precompute) = parent(start_precompute_index) {
// Compute the end index of the precomputed level.
let end_precompute = start_precompute + (middle - start);
// Copy the hashes for each node in the current level.
tree[start..middle].copy_from_slice(&self.tree[start_precompute..end_precompute]);
// Update the precompute index for the next level.
start_precompute_index = start_precompute;
} else {
// Ensure the start index is equal to the middle index, as all precomputed indices have been processed.
ensure!(start == middle, "Failed to process all left precomputed indices in the Merkle tree");
}
lap!(timer, "Precompute (Left): {start} -> {middle}");
// If the current level has precomputed indices, copy them instead of recomputing them.
// Note: This logic works because the old tree and new tree are the same power of two.
if let Some(middle_precompute) = middle_precompute_index {
if let Some(middle_precompute) = parent(middle_precompute) {
// Construct the children for the new indices in the current level.
let tuples = (middle..middle_precompute)
.map(|i| (tree[left_child(i)], tree[right_child(i)]))
.collect::<Vec<_>>();
// Process the indices that need to be computed for the current level.
// If any level requires computing more than 100 nodes, borrow the tree for performance.
match tuples.len() >= 100 {
// Option 1: Borrow the tree to compute and store the hashes for the new indices in the current level.
true => cfg_iter_mut!(tree[middle..middle_precompute]).zip_eq(cfg_iter!(tuples)).try_for_each(
|(node, (left, right))| {
*node = self.path_hasher.hash_children(left, right)?;
Ok::<_, Error>(())
},
)?,
// Option 2: Compute and store the hashes for the new indices in the current level.
false => tree[middle..middle_precompute].iter_mut().zip_eq(&tuples).try_for_each(
|(node, (left, right))| {
*node = self.path_hasher.hash_children(left, right)?;
Ok::<_, Error>(())
},
)?,
}
lap!(timer, "Compute: {middle} -> {middle_precompute}");
// Copy the hashes for each node in the current level.
tree[middle_precompute..end].copy_from_slice(&self.tree[middle_precompute..end]);
// Update the precompute index for the next level.
middle_precompute_index = Some(middle_precompute + 1);
lap!(timer, "Precompute (Right): {middle_precompute} -> {end}");
} else {
// Ensure the middle precompute index is equal to the end index, as all precomputed indices have been processed.
ensure!(
middle_precompute == end,
"Failed to process all right precomputed indices in the Merkle tree"
);
}
} else {
// Construct the children for the new indices in the current level.
let tuples = (middle..end).map(|i| (tree[left_child(i)], tree[right_child(i)])).collect::<Vec<_>>();
// Process the indices that need to be computed for the current level.
// If any level requires computing more than 100 nodes, borrow the tree for performance.
match tuples.len() >= 100 {
// Option 1: Borrow the tree to compute and store the hashes for the new indices in the current level.
true => cfg_iter_mut!(tree[middle..end]).zip_eq(cfg_iter!(tuples)).try_for_each(
|(node, (left, right))| {
*node = self.path_hasher.hash_children(left, right)?;
Ok::<_, Error>(())
},
)?,
// Option 2: Compute and store the hashes for the new indices in the current level.
false => tree[middle..end].iter_mut().zip_eq(&tuples).try_for_each(|(node, (left, right))| {
*node = self.path_hasher.hash_children(left, right)?;
Ok::<_, Error>(())
})?,
}
lap!(timer, "Compute: {middle} -> {end}");
}
// Update the start index for the next level.
start_index = start;
// Update the middle index for the next level.
middle_index = middle;
}
// End the timer for the while loop.
finish!(timer);
Ok(())
}
}
/// Returns the depth of the tree, given the size of the tree.
#[inline]
#[allow(clippy::cast_possible_truncation)]
fn tree_depth<const DEPTH: u8>(tree_size: usize) -> Result<u8> {
let tree_size = u64::try_from(tree_size)?;
// Ensure the tree size is less than 2^52 (for casting to an f64).
let tree_depth = match tree_size < 4503599627370496_u64 {
// Compute the log2 of the tree size.
true => (tree_size as f64).log2(),
false => bail!("Tree size must be less than 2^52"),
};
// Ensure the tree depth is within a u8 range.
match tree_depth <= u8::MAX as f64 {
true => {
// Convert the tree depth to a u8.
let tree_depth = tree_depth as u8;
// Ensure the tree depth is within the depth bound.
match tree_depth <= DEPTH {
// Return the tree depth.
true => Ok(tree_depth),
false => bail!("Merkle tree cannot exceed depth {DEPTH}: attempted to reach depth {tree_depth}"),
}
}
false => bail!("Merkle tree depth ({tree_depth}) exceeds maximum size ({})", u8::MAX),
}
}
/// Returns the index of the left child, given an index.
#[inline]
const fn left_child(index: usize) -> usize {
2 * index + 1
}
/// Returns the index of the right child, given an index.
#[inline]
const fn right_child(index: usize) -> usize {
2 * index + 2
}
/// Returns the index of the sibling, given an index.
#[inline]
const fn sibling(index: usize) -> Option<usize> {
if is_root(index) {
None
} else if is_left_child(index) {
Some(index + 1)
} else {
Some(index - 1)
}
}
/// Returns true iff the index represents the root.
#[inline]
const fn is_root(index: usize) -> bool {
index == 0
}
/// Returns true iff the given index represents a left child.
#[inline]
const fn is_left_child(index: usize) -> bool {
index % 2 == 1
}
/// Returns the index of the parent, given the index of a child.
#[inline]
const fn parent(index: usize) -> Option<usize> {
if index > 0 { Some((index - 1) >> 1) } else { None }
}