webb-tools / zero-knowledge-gadgets

Zero-knowledge gadgets for Webb's cross-chain blockchain applications.
Apache License 2.0
89 stars 29 forks source link

[TASK] Anchor API #172

Closed lazovicff closed 2 years ago

lazovicff commented 2 years ago

To make the API more configurable and more stable, I propose the following struct:

struct AnchorR1CSProver<E: PairingEngine, H: FieldHasher<E::Fr>, const HEIGHT: usize, const BRIDGE_SIZE: usize> {
    engine: PhantomData<E>,
    hasher: PhantomData<H>,
}

Which will be defined as:

type AnchorR1CSProver_Bn254_Poseidon_30_2 = AnchorR1CSProver<Bn254, Poseidon<Bn254Fr>, 30, 2>;

With the name convention: [prover name]_[ec]_[hasher]_[tree height]_[bridge size]

With interface:

trait AnchorProver<
    F: PrimeField,
    H: HasherGadget,
    const HEIGHT: usize,
    const BRIDGE_SIZE: usize
> {
    // For creating leaves where we supply the secret and the nullifier.
    // If we want to generate fresh one, we should pass None
    pub fn create_leaf_with_privates<R: RngCore>(
        chain_id: u128,
        secret: Option<Vec<u8>>,
        nullifier: Option<Vec<u8>>,
    rng: &mut R,
    ) -> Result<Leaf, Error>;
    // For making proofs
    pub fn create_proof<R: RngCore + CryptoRng>(
        curve: Curve,
        chain_id: u128,
        secret: Vec<u8>,
        nullifier: Vec<u8>,
        leaves: Vec<Vec<u8>>,
        index: u64,
        roots: [Vec<u8>; M],
        recipient: Vec<u8>,
        relayer: Vec<u8>,
        commitment: Vec<u8>,
        fee: u128,
        refund: u128,
        pk: Vec<u8>,
        rng: &mut R,
    ) -> Result<AnchorProof, Error>;
}

NOTE: This interface can be implemented for both R1CS and PLONK systems