Skip to content

Commit

Permalink
Revert "Add a generic DigestAlgorithmGadget to MerkleTreeGadget instead"
Browse files Browse the repository at this point in the history
This reverts commit 4e3a09c.
  • Loading branch information
tessico committed Dec 23, 2022
1 parent c051dbb commit 108fc1c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
23 changes: 17 additions & 6 deletions primitives/src/circuit/merkle_tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
//! Trait definitions for a Merkle tree gadget.
use crate::merkle_tree::MerkleTreeScheme;
use jf_relation::{errors::CircuitError, BoolVar, Variable};
use ark_ff::PrimeField;
use jf_relation::{errors::CircuitError, BoolVar, PlonkCircuit, Variable};

mod rescue_merkle_tree;

Expand Down Expand Up @@ -36,10 +37,10 @@ mod rescue_merkle_tree;
/// circuit.enforce_merkle_proof(leaf_var, path_vars, root_var).unwrap();
/// assert!(circuit.check_circuit_satisfiability(&[]).is_ok());
/// ```
pub trait MerkleTreeGadget<M, D>
pub trait MerkleTreeGadget<M, F>
where
M: MerkleTreeScheme,
D: DigestAlgorithmGadget<M> + ?Sized,
F: PrimeField,
{
/// Type to represent the leaf element of the concrete MT instantiation.
type LeafVar;
Expand All @@ -49,6 +50,9 @@ where
/// Merkle path.
type MerklePathVar;

/// Gadget for the digest algorithm.
type DigestGadget: DigestAlgorithmGadget<F>;

/// Allocate a variable for the leaf element.
fn create_leaf_variable(
&mut self,
Expand Down Expand Up @@ -85,10 +89,17 @@ where
}

/// Circuit counterpart to DigestAlgorithm
pub trait DigestAlgorithmGadget<M: MerkleTreeScheme> {
pub trait DigestAlgorithmGadget<F>
where
F: PrimeField,
{
/// Digest a list of variables
fn digest(&mut self, data: &[Variable]) -> Result<Variable, CircuitError>;
fn digest(circuit: &mut PlonkCircuit<F>, data: &[Variable]) -> Result<Variable, CircuitError>;

/// Digest an indexed element
fn digest_leaf(&mut self, pos: usize, elem: Variable) -> Result<Variable, CircuitError>;
fn digest_leaf(
circuit: &mut PlonkCircuit<F>,
pos: usize,
elem: Variable,
) -> Result<Variable, CircuitError>;
}
30 changes: 17 additions & 13 deletions primitives/src/circuit/merkle_tree/rescue_merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,30 @@ pub struct StandardLeafVar {
pub elem: Variable,
}

impl<F: RescueParameter> DigestAlgorithmGadget<RescueMerkleTree<F>> for PlonkCircuit<F> {
fn digest(&mut self, data: &[Variable]) -> Result<Variable, CircuitError> {
Ok(RescueNativeGadget::<F>::rescue_sponge_no_padding(self, &data, 1)?[0])
pub struct RescueDigestGadget {}

impl<F: RescueParameter> DigestAlgorithmGadget<F> for RescueDigestGadget {
fn digest(circuit: &mut PlonkCircuit<F>, data: &[Variable]) -> Result<Variable, CircuitError> {
Ok(RescueNativeGadget::<F>::rescue_sponge_no_padding(circuit, &data, 1)?[0])
}

fn digest_leaf(&mut self, pos: usize, elem: Variable) -> Result<Variable, CircuitError> {
let zero = self.zero();
Ok(RescueNativeGadget::<F>::rescue_sponge_no_padding(self, &[zero, pos, elem], 1)?[0])
fn digest_leaf(
circuit: &mut PlonkCircuit<F>,
pos: usize,
elem: Variable,
) -> Result<Variable, CircuitError> {
let zero = circuit.zero();
Ok(RescueNativeGadget::<F>::rescue_sponge_no_padding(circuit, &[zero, pos, elem], 1)?[0])
}
}

impl<F> MerkleTreeGadget<RescueMerkleTree<F>, dyn DigestAlgorithmGadget<RescueMerkleTree<F>>>
for PlonkCircuit<F>
impl<F> MerkleTreeGadget<RescueMerkleTree<F>, F> for PlonkCircuit<F>
where
F: RescueParameter,
{
type LeafVar = StandardLeafVar;
type MerklePathVar = Rescue3AryMerklePathVar;
type DigestGadget = RescueDigestGadget;

fn create_leaf_variable(
&mut self,
Expand Down Expand Up @@ -178,9 +184,7 @@ pub struct Rescue3AryNodeVar {
pub is_right_child: BoolVar,
}

trait MerkleTreeHelperGadget<F: RescueParameter>:
MerkleTreeGadget<RescueMerkleTree<F>, dyn DigestAlgorithmGadget<RescueMerkleTree<F>>>
{
trait MerkleTreeHelperGadget<F: RescueParameter>: MerkleTreeGadget<RescueMerkleTree<F>, F> {
/// Produces an ordered list of variables based on the relative position of
/// a node and its siblings.
/// * `node` - node to be inserted in the final list.
Expand Down Expand Up @@ -279,7 +283,7 @@ impl<F: RescueParameter> MerkleTreeHelperGadget<F> for PlonkCircuit<F> {
elem: StandardLeafVar,
path_vars: &Rescue3AryMerklePathVar,
) -> Result<Variable, CircuitError> {
let mut cur_label = Self::digest_leaf(self, elem.uid, elem.elem)?;
let mut cur_label = Self::DigestGadget::digest_leaf(self, elem.uid, elem.elem)?;
for cur_node in path_vars.nodes.iter() {
let input_labels = self.permute(
cur_label,
Expand All @@ -290,7 +294,7 @@ impl<F: RescueParameter> MerkleTreeHelperGadget<F> for PlonkCircuit<F> {
)?;
// check that the left child's label is non-zero
self.non_zero_gate(input_labels[0])?;
cur_label = Self::digest(self, &input_labels)?;
cur_label = Self::DigestGadget::digest(self, &input_labels)?;
}
Ok(cur_label)
}
Expand Down

0 comments on commit 108fc1c

Please sign in to comment.