2021-05-30 13:25:53 -07:00
|
|
|
use anyhow::{ensure, Result};
|
2021-07-15 07:35:12 -07:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-05-30 13:25:53 -07:00
|
|
|
|
|
|
|
|
use crate::field::extension_field::Extendable;
|
2021-07-29 22:00:29 -07:00
|
|
|
use crate::field::field_types::Field;
|
2021-04-12 10:38:07 +02:00
|
|
|
use crate::gates::gmimc::GMiMCGate;
|
2021-07-29 22:00:29 -07:00
|
|
|
use crate::hash::hash_types::{HashOut, HashOutTarget};
|
|
|
|
|
use crate::hash::hashing::{compress, hash_or_noop, GMIMC_ROUNDS};
|
|
|
|
|
use crate::iop::target::Target;
|
|
|
|
|
use crate::iop::wire::Wire;
|
|
|
|
|
use crate::plonk::circuit_builder::CircuitBuilder;
|
2021-04-12 10:38:07 +02:00
|
|
|
|
2021-07-15 07:35:12 -07:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
|
#[serde(bound = "")]
|
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.
|
2021-07-29 22:00:29 -07:00
|
|
|
pub siblings: Vec<HashOut<F>>,
|
2021-04-12 10:38:07 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-08 17:16:26 +02:00
|
|
|
#[derive(Clone)]
|
2021-04-12 10:38:07 +02:00
|
|
|
pub struct MerkleProofTarget {
|
|
|
|
|
/// The Merkle digest of each sibling subtree, staying from the bottommost layer.
|
2021-07-29 22:00:29 -07:00
|
|
|
pub siblings: Vec<HashOutTarget>,
|
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<F: Field>(
|
|
|
|
|
leaf_data: Vec<F>,
|
|
|
|
|
leaf_index: usize,
|
2021-07-29 22:00:29 -07:00
|
|
|
merkle_root: HashOut<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
|
|
|
}
|
|
|
|
|
|
2021-05-30 13:25:53 -07: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
|
2021-07-22 15:10:55 +02:00
|
|
|
/// given root. The index is given by it's little-endian bits.
|
2021-04-12 10:38:07 +02:00
|
|
|
pub(crate) fn verify_merkle_proof(
|
|
|
|
|
&mut self,
|
|
|
|
|
leaf_data: Vec<Target>,
|
2021-07-22 15:10:55 +02:00
|
|
|
leaf_index_bits: &[Target],
|
2021-07-29 22:00:29 -07:00
|
|
|
merkle_root: HashOutTarget,
|
2021-06-04 17:36:48 +02:00
|
|
|
proof: &MerkleProofTarget,
|
2021-04-12 10:38:07 +02:00
|
|
|
) {
|
|
|
|
|
let zero = self.zero();
|
|
|
|
|
|
2021-07-29 22:00:29 -07:00
|
|
|
let mut state: HashOutTarget = self.hash_or_noop(leaf_data);
|
2021-04-12 10:38:07 +02:00
|
|
|
|
2021-07-22 15:10:55 +02:00
|
|
|
for (&bit, &sibling) in leaf_index_bits.iter().zip(&proof.siblings) {
|
2021-07-22 23:48:03 -07:00
|
|
|
let gate_type = GMiMCGate::<F, D, GMIMC_ROUNDS>::new_automatic_constants();
|
|
|
|
|
let gate = self.add_gate(gate_type, vec![]);
|
2021-04-12 10:38:07 +02:00
|
|
|
|
2021-05-30 13:25:53 -07: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 input_wires = (0..12)
|
2021-04-21 22:31:45 +02:00
|
|
|
.map(|i| {
|
|
|
|
|
Target::Wire(Wire {
|
|
|
|
|
gate,
|
2021-05-30 13:25:53 -07:00
|
|
|
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-07-29 22:00:29 -07:00
|
|
|
state = HashOutTarget::from_vec(
|
2021-04-21 22:31:45 +02:00
|
|
|
(0..4)
|
|
|
|
|
.map(|i| {
|
|
|
|
|
Target::Wire(Wire {
|
|
|
|
|
gate,
|
2021-05-30 13:25:53 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2021-07-19 12:22:18 -07:00
|
|
|
self.named_assert_hashes_equal(state, merkle_root, "check Merkle root".into())
|
2021-04-12 10:38:07 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-29 22:00:29 -07:00
|
|
|
pub(crate) fn assert_hashes_equal(&mut self, x: HashOutTarget, y: HashOutTarget) {
|
2021-04-12 10:38:07 +02:00
|
|
|
for i in 0..4 {
|
|
|
|
|
self.assert_equal(x.elements[i], y.elements[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-14 20:54:30 +02:00
|
|
|
|
2021-07-29 22:00:29 -07:00
|
|
|
pub(crate) fn named_assert_hashes_equal(
|
|
|
|
|
&mut self,
|
|
|
|
|
x: HashOutTarget,
|
|
|
|
|
y: HashOutTarget,
|
|
|
|
|
name: String,
|
|
|
|
|
) {
|
2021-07-14 20:54:30 +02:00
|
|
|
for i in 0..4 {
|
|
|
|
|
self.named_assert_equal(
|
|
|
|
|
x.elements[i],
|
|
|
|
|
y.elements[i],
|
|
|
|
|
format!("{}: {}-th hash element", name, i),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-12 10:38:07 +02:00
|
|
|
}
|
2021-07-13 09:44:35 +02:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use anyhow::Result;
|
|
|
|
|
use rand::{thread_rng, Rng};
|
|
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
use crate::field::crandall_field::CrandallField;
|
2021-07-29 22:00:29 -07:00
|
|
|
use crate::hash::merkle_tree::MerkleTree;
|
|
|
|
|
use crate::iop::witness::PartialWitness;
|
|
|
|
|
use crate::plonk::circuit_builder::CircuitBuilder;
|
|
|
|
|
use crate::plonk::circuit_data::CircuitConfig;
|
|
|
|
|
use crate::plonk::verifier::verify;
|
2021-07-13 09:44:35 +02:00
|
|
|
|
|
|
|
|
fn random_data<F: Field>(n: usize, k: usize) -> Vec<Vec<F>> {
|
|
|
|
|
(0..n).map(|_| F::rand_vec(k)).collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2021-07-13 15:20:14 +02:00
|
|
|
fn test_recursive_merkle_proof() -> Result<()> {
|
2021-07-13 09:44:35 +02:00
|
|
|
type F = CrandallField;
|
|
|
|
|
let config = CircuitConfig::large_config();
|
2021-08-09 09:58:09 +02:00
|
|
|
let mut pw = PartialWitness::new(config.num_wires);
|
2021-07-13 09:44:35 +02:00
|
|
|
let mut builder = CircuitBuilder::<F, 4>::new(config);
|
|
|
|
|
|
|
|
|
|
let log_n = 8;
|
|
|
|
|
let n = 1 << log_n;
|
|
|
|
|
let leaves = random_data::<F>(n, 7);
|
2021-07-15 10:39:57 +02:00
|
|
|
let tree = MerkleTree::new(leaves, false);
|
2021-07-13 09:44:35 +02:00
|
|
|
let i: usize = thread_rng().gen_range(0, n);
|
|
|
|
|
let proof = tree.prove(i);
|
|
|
|
|
|
|
|
|
|
let proof_t = MerkleProofTarget {
|
|
|
|
|
siblings: builder.add_virtual_hashes(proof.siblings.len()),
|
|
|
|
|
};
|
|
|
|
|
for i in 0..proof.siblings.len() {
|
|
|
|
|
pw.set_hash_target(proof_t.siblings[i], proof.siblings[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let root_t = builder.add_virtual_hash();
|
|
|
|
|
pw.set_hash_target(root_t, tree.root);
|
|
|
|
|
|
|
|
|
|
let i_c = builder.constant(F::from_canonical_usize(i));
|
2021-07-22 15:10:55 +02:00
|
|
|
let i_bits = builder.split_le(i_c, log_n);
|
2021-07-13 09:44:35 +02:00
|
|
|
|
|
|
|
|
let data = builder.add_virtual_targets(tree.leaves[i].len());
|
|
|
|
|
for j in 0..data.len() {
|
|
|
|
|
pw.set_target(data[j], tree.leaves[i][j]);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-22 15:10:55 +02:00
|
|
|
builder.verify_merkle_proof(data, &i_bits, root_t, &proof_t);
|
2021-07-13 09:44:35 +02:00
|
|
|
|
|
|
|
|
let data = builder.build();
|
2021-07-18 23:14:48 -07:00
|
|
|
let proof = data.prove(pw)?;
|
2021-07-14 20:54:30 +02:00
|
|
|
|
|
|
|
|
verify(proof, &data.verifier_only, &data.common)
|
|
|
|
|
}
|
2021-07-13 09:44:35 +02:00
|
|
|
}
|