|
| 1 | +use crate::{ |
| 2 | + circuits::{ |
| 3 | + merkle_insertion::prove_insertion, merkle_update::prove_update, InsertMerkleProofCircuit, |
| 4 | + ProofVariantCircuit, UpdateMerkleProofCircuit, |
| 5 | + }, |
| 6 | + tree::Digest, |
| 7 | + utils::create_and_verify_snark, |
| 8 | +}; |
| 9 | +use anyhow::Result; |
| 10 | +use bellman::{groth16, Circuit, ConstraintSystem, SynthesisError}; |
| 11 | +use bls12_381::{Bls12, Scalar}; |
| 12 | +use indexed_merkle_tree::tree::Proof; |
| 13 | + |
| 14 | +/// BatchMerkleProofCircuit represents a circuit for proving a batch of merkle proof circuits. |
| 15 | +#[derive(Clone)] |
| 16 | +pub struct BatchMerkleProofCircuit { |
| 17 | + pub old_commitment: Scalar, |
| 18 | + pub new_commitment: Scalar, |
| 19 | + pub proofs: Vec<ProofVariantCircuit>, |
| 20 | +} |
| 21 | + |
| 22 | +impl BatchMerkleProofCircuit { |
| 23 | + pub fn new( |
| 24 | + old_commitment: &Digest, |
| 25 | + new_commitment: &Digest, |
| 26 | + proofs: Vec<Proof>, |
| 27 | + ) -> Result<BatchMerkleProofCircuit> { |
| 28 | + let parsed_old_commitment: Scalar = (*old_commitment).try_into()?; |
| 29 | + let parsed_new_commitment: Scalar = (*new_commitment).try_into()?; |
| 30 | + let mut proof_circuit_array: Vec<ProofVariantCircuit> = vec![]; |
| 31 | + for proof in proofs { |
| 32 | + match proof { |
| 33 | + Proof::Update(update_proof) => { |
| 34 | + proof_circuit_array.push(ProofVariantCircuit::Update( |
| 35 | + UpdateMerkleProofCircuit::new(&update_proof)?, |
| 36 | + )); |
| 37 | + } |
| 38 | + Proof::Insert(insertion_proof) => { |
| 39 | + proof_circuit_array.push(ProofVariantCircuit::Insert( |
| 40 | + InsertMerkleProofCircuit::new(&insertion_proof)?, |
| 41 | + )); |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + Ok(BatchMerkleProofCircuit { |
| 46 | + old_commitment: parsed_old_commitment, |
| 47 | + new_commitment: parsed_new_commitment, |
| 48 | + proofs: proof_circuit_array, |
| 49 | + }) |
| 50 | + } |
| 51 | + |
| 52 | + pub fn create_and_verify_snark( |
| 53 | + &self, |
| 54 | + ) -> Result<(groth16::Proof<Bls12>, groth16::VerifyingKey<Bls12>)> { |
| 55 | + let scalars: Vec<Scalar> = vec![self.old_commitment, self.new_commitment]; |
| 56 | + |
| 57 | + create_and_verify_snark(ProofVariantCircuit::Batch(self.clone()), scalars) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +impl Circuit<Scalar> for BatchMerkleProofCircuit { |
| 62 | + fn synthesize<CS: ConstraintSystem<Scalar>>(self, cs: &mut CS) -> Result<(), SynthesisError> { |
| 63 | + // If the proofs are empty, we just verify that the commitments are equal |
| 64 | + if self.proofs.is_empty() { |
| 65 | + let provided_old_commitment = |
| 66 | + cs.alloc_input(|| "provided old commitment", || Ok(self.old_commitment))?; |
| 67 | + let provided_new_commitment = |
| 68 | + cs.alloc_input(|| "provided new commitment", || Ok(self.new_commitment))?; |
| 69 | + |
| 70 | + // provided_old_commitment * (1) = provided_new_commitment |
| 71 | + cs.enforce( |
| 72 | + || "old commitment check", |
| 73 | + |lc| lc + provided_old_commitment, |
| 74 | + |lc| lc + CS::one(), |
| 75 | + |lc| lc + provided_new_commitment, |
| 76 | + ); |
| 77 | + |
| 78 | + return Ok(()); |
| 79 | + } |
| 80 | + |
| 81 | + // before the calculations make sure that the old root is that of the first proof |
| 82 | + let old_root = match &self.proofs[0] { |
| 83 | + ProofVariantCircuit::Update(update_proof_circuit) => update_proof_circuit.old_root, |
| 84 | + ProofVariantCircuit::Insert(insert_proof_circuit) => { |
| 85 | + insert_proof_circuit.pre_insertion_root |
| 86 | + } |
| 87 | + ProofVariantCircuit::Batch(batch_proof_circuit) => batch_proof_circuit.old_commitment, |
| 88 | + }; |
| 89 | + |
| 90 | + let provided_old_commitment = |
| 91 | + cs.alloc_input(|| "provided old commitment", || Ok(self.old_commitment))?; |
| 92 | + let old_commitment_from_proofs = |
| 93 | + cs.alloc(|| "old commitment from proofs", || Ok(old_root))?; |
| 94 | + |
| 95 | + // old_commitment_from_proofs * (1) = provided_old_commitment |
| 96 | + cs.enforce( |
| 97 | + || "old commitment check", |
| 98 | + |lc| lc + old_commitment_from_proofs, |
| 99 | + |lc| lc + CS::one(), |
| 100 | + |lc| lc + provided_old_commitment, |
| 101 | + ); |
| 102 | + |
| 103 | + let mut new_commitment: Scalar = Scalar::zero(); |
| 104 | + for proof in self.proofs { |
| 105 | + // update the new_commitment for every proof, applying the constraints of the circuit each time |
| 106 | + match proof { |
| 107 | + ProofVariantCircuit::Update(update_proof_circuit) => { |
| 108 | + new_commitment = prove_update( |
| 109 | + cs, |
| 110 | + update_proof_circuit.old_root, |
| 111 | + &update_proof_circuit.old_path, |
| 112 | + update_proof_circuit.updated_root, |
| 113 | + &update_proof_circuit.updated_path, |
| 114 | + )?; |
| 115 | + } |
| 116 | + ProofVariantCircuit::Insert(insert_proof_circuit) => { |
| 117 | + new_commitment = prove_insertion( |
| 118 | + cs, |
| 119 | + insert_proof_circuit.pre_insertion_root, |
| 120 | + &insert_proof_circuit.insertion_path, |
| 121 | + insert_proof_circuit.new_leaf_node, |
| 122 | + insert_proof_circuit.existing_leaf_update, |
| 123 | + insert_proof_circuit.new_leaf_activation, |
| 124 | + )?; |
| 125 | + } |
| 126 | + ProofVariantCircuit::Batch(_) => { |
| 127 | + // Batches cannot be recursively constructed |
| 128 | + // TODO: Should they be able to? |
| 129 | + return Err(SynthesisError::Unsatisfiable); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + let provided_new_commitment = |
| 135 | + cs.alloc_input(|| "provided commitment", || Ok(self.new_commitment))?; |
| 136 | + let recalculated_new_commitment = |
| 137 | + cs.alloc(|| "recalculated commitment", || Ok(new_commitment))?; |
| 138 | + |
| 139 | + // recalculated_commitment * (1) = provided_commitment |
| 140 | + cs.enforce( |
| 141 | + || "new commitment check", |
| 142 | + |lc| lc + recalculated_new_commitment, |
| 143 | + |lc| lc + CS::one(), |
| 144 | + |lc| lc + provided_new_commitment, |
| 145 | + ); |
| 146 | + |
| 147 | + Ok(()) |
| 148 | + } |
| 149 | +} |
0 commit comments