plonky2/src/merkle_proofs.rs

137 lines
4.4 KiB
Rust
Raw Normal View History

use anyhow::{ensure, Result};
2021-04-12 10:38:07 +02:00
use crate::circuit_builder::CircuitBuilder;
use crate::field::extension_field::Extendable;
2021-04-12 10:38:07 +02:00
use crate::field::field::Field;
use crate::gates::gmimc::GMiMCGate;
2021-04-26 18:24:57 +02:00
use crate::hash::GMIMC_ROUNDS;
2021-04-21 22:31:45 +02:00
use crate::hash::{compress, hash_or_noop};
2021-04-12 10:38:07 +02:00
use crate::proof::{Hash, HashTarget};
use crate::target::Target;
use crate::wire::Wire;
2021-04-21 22:31:45 +02:00
#[derive(Clone, Debug)]
2021-04-12 10:38:07 +02:00
pub struct MerkleProof<F: Field> {
/// The Merkle digest of each sibling subtree, staying from the bottommost layer.
pub siblings: Vec<Hash<F>>,
}
pub struct MerkleProofTarget {
/// The Merkle digest of each sibling subtree, staying from the bottommost layer.
pub siblings: Vec<HashTarget>,
}
/// Verifies that the given leaf data is present at the given index in the Merkle tree with the
/// given root.
pub(crate) fn verify_merkle_proof<F: Field>(
leaf_data: Vec<F>,
leaf_index: usize,
merkle_root: Hash<F>,
2021-04-21 22:31:45 +02:00
proof: &MerkleProof<F>,
reverse_bits: bool,
2021-04-22 09:27:59 +02:00
) -> Result<()> {
2021-04-29 08:18:31 +02:00
ensure!(
leaf_index >> proof.siblings.len() == 0,
"Merkle leaf index is too large."
);
2021-04-21 22:31:45 +02:00
let index = if reverse_bits {
crate::util::reverse_bits(leaf_index, proof.siblings.len())
} else {
leaf_index
};
2021-04-12 10:38:07 +02:00
let mut current_digest = hash_or_noop(leaf_data);
2021-04-21 22:31:45 +02:00
for (i, &sibling_digest) in proof.siblings.iter().enumerate() {
let bit = (index >> i & 1) == 1;
2021-04-12 10:38:07 +02:00
current_digest = if bit {
compress(sibling_digest, current_digest)
} else {
compress(current_digest, sibling_digest)
}
}
2021-04-22 09:27:59 +02:00
ensure!(current_digest == merkle_root, "Invalid Merkle proof.");
Ok(())
2021-04-12 10:38:07 +02:00
}
impl<F: Extendable<D>, const D: usize> CircuitBuilder<F, D> {
2021-04-12 10:38:07 +02:00
/// Verifies that the given leaf data is present at the given index in the Merkle tree with the
/// given root.
pub(crate) fn verify_merkle_proof(
&mut self,
leaf_data: Vec<Target>,
leaf_index: Target,
merkle_root: HashTarget,
proof: MerkleProofTarget,
) {
let zero = self.zero();
let height = proof.siblings.len();
let purported_index_bits = self.split_le_virtual(leaf_index, height);
let mut state: HashTarget = self.hash_or_noop(leaf_data);
let mut acc_leaf_index = zero;
for (bit, sibling) in purported_index_bits.into_iter().zip(proof.siblings) {
2021-04-21 22:31:45 +02:00
let gate = self
.add_gate_no_constants(GMiMCGate::<F, D, GMIMC_ROUNDS>::with_automatic_constants());
2021-04-12 10:38:07 +02:00
let swap_wire = GMiMCGate::<F, D, GMIMC_ROUNDS>::WIRE_SWAP;
2021-04-21 22:31:45 +02:00
let swap_wire = Target::Wire(Wire {
gate,
input: swap_wire,
});
2021-04-12 10:38:07 +02:00
self.generate_copy(bit, swap_wire);
let old_acc_wire = GMiMCGate::<F, D, GMIMC_ROUNDS>::WIRE_INDEX_ACCUMULATOR_OLD;
2021-04-21 22:31:45 +02:00
let old_acc_wire = Target::Wire(Wire {
gate,
input: old_acc_wire,
});
2021-04-12 10:38:07 +02:00
self.route(acc_leaf_index, old_acc_wire);
let new_acc_wire = GMiMCGate::<F, D, GMIMC_ROUNDS>::WIRE_INDEX_ACCUMULATOR_NEW;
2021-04-21 22:31:45 +02:00
let new_acc_wire = Target::Wire(Wire {
gate,
input: new_acc_wire,
});
2021-04-12 10:38:07 +02:00
acc_leaf_index = new_acc_wire;
let input_wires = (0..12)
2021-04-21 22:31:45 +02:00
.map(|i| {
Target::Wire(Wire {
gate,
input: GMiMCGate::<F, D, GMIMC_ROUNDS>::wire_input(i),
2021-04-21 22:31:45 +02:00
})
})
2021-04-12 10:38:07 +02:00
.collect::<Vec<_>>();
for i in 0..4 {
self.route(state.elements[i], input_wires[i]);
self.route(sibling.elements[i], input_wires[4 + i]);
self.route(zero, input_wires[8 + i]);
}
2021-04-21 22:31:45 +02:00
state = HashTarget::from_vec(
(0..4)
.map(|i| {
Target::Wire(Wire {
gate,
input: GMiMCGate::<F, D, GMIMC_ROUNDS>::wire_output(i),
2021-04-21 22:31:45 +02:00
})
})
.collect(),
)
2021-04-12 10:38:07 +02:00
}
self.assert_equal(acc_leaf_index, leaf_index);
self.assert_hashes_equal(state, merkle_root)
}
pub(crate) fn assert_hashes_equal(&mut self, x: HashTarget, y: HashTarget) {
for i in 0..4 {
self.assert_equal(x.elements[i], y.elements[i]);
}
}
}