-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API] Refactor Merkle tree and its gadget #88
Milestone
Comments
we should define something like |
This was referenced Aug 11, 2022
13 tasks
I feel that the following is more ergonomic: use ark_ff::PrimeField;
use jf_relation::{errors::CircuitError, BoolVar, PlonkCircuit};
use crate::{
merkle_tree::{
prelude::{RescueMerkleTree, RescueSparseMerkleTree},
MerkleTreeScheme, UniversalMerkleTreeScheme,
},
rescue::RescueParameter,
};
/// Gadgets for rescue-based merkle tree
pub trait MerkleTreeGadget<F: PrimeField + RescueParameter> {
/// given an element and its merkle proof in a `RescueMerkleTree`,
/// return `BoolVar` indicating the correctness of its membership proof
fn is_member(
&mut self,
elem: &<RescueMerkleTree<F> as MerkleTreeScheme>::Element,
merkle_proof: &<RescueMerkleTree<F> as MerkleTreeScheme>::MembershipProof,
merkle_root: &<RescueMerkleTree<F> as MerkleTreeScheme>::NodeValue,
) -> Result<BoolVar, CircuitError>;
/// Enforce correct `merkle_proof` for the `elem` against
/// `expected_merkle_root`.
fn enforce_merkle_proof(
&mut self,
elem: &<RescueMerkleTree<F> as MerkleTreeScheme>::Element,
merkle_proof: &<RescueMerkleTree<F> as MerkleTreeScheme>::MembershipProof,
expected_merkle_root: &<RescueMerkleTree<F> as MerkleTreeScheme>::NodeValue,
) -> Result<(), CircuitError>;
}
/// Gadget for sparse merkle tree and key-value map tree
pub trait SparseMerkleTreeGadget<F: PrimeField + RescueParameter> {
/// checking non-membership proof
fn is_non_member(
&mut self,
elem: &<RescueSparseMerkleTree<F, F> as MerkleTreeScheme>::Element,
nonmerkle_proof: &<RescueSparseMerkleTree<F, F> as UniversalMerkleTreeScheme>::NonMembershipProof,
merkle_root: &<RescueSparseMerkleTree<F, F> as MerkleTreeScheme>::NodeValue,
) -> Result<BoolVar, CircuitError>;
}
impl<F> MerkleTreeGadget<F> for PlonkCircuit<F>
where
F: PrimeField + RescueParameter,
{
fn is_member(
&mut self,
_elem: &<RescueMerkleTree<F> as MerkleTreeScheme>::Element,
_merkle_proof: &<RescueMerkleTree<F> as MerkleTreeScheme>::MembershipProof,
_merkle_root: &<RescueMerkleTree<F> as MerkleTreeScheme>::NodeValue,
) -> Result<BoolVar, CircuitError> {
todo!();
}
fn enforce_merkle_proof(
&mut self,
elem: &<RescueMerkleTree<F> as MerkleTreeScheme>::Element,
merkle_proof: &<RescueMerkleTree<F> as MerkleTreeScheme>::MembershipProof,
expected_merkle_root: &<RescueMerkleTree<F> as MerkleTreeScheme>::NodeValue,
) -> Result<(), CircuitError> {
let is_member = self.is_member(elem, merkle_proof, expected_merkle_root)?;
self.enforce_true(is_member.into())
}
} |
6 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Current API for MT and its gadgets need some refactoring, e.g. here's the code we use in ZPrice to generate a circuit of lots of Merkle Proof verification:
Struct::new()
and field filling to instantiate structsMerklePath
,MerkleNode
etc are slightly confusingThe text was updated successfully, but these errors were encountered: