arkworks-rs / snark

Interfaces for Relations and SNARKs for these relations
https://www.arkworks.rs
Apache License 2.0
776 stars 209 forks source link

Add a new trait `ToConstraintFieldGadget`? #232

Closed weikengchen closed 3 years ago

weikengchen commented 4 years ago

We should have a new trait ToConstraintFieldGadget, which is the r1cs counterpart of ToConstraintField. Both are shown below.

pub trait ToConstraintField<F: Field> {
    fn to_field_elements(&self) -> Result<Vec<F>, Error>;
}

pub trait ToConstraintFieldGadget<ConstraintF: PrimeField> {
    fn to_field_gadgets<CS: ConstraintSystem<ConstraintF>>(
        &self,
        cs: CS,
    ) -> Result<Vec<FpGadget<ConstraintF>>, SynthesisError>;
}

Reason: for algebraic hash functions.

Currently, many gadgets implement ToBytesGadget, so that they could later be used for Pedersen hash functions (or even classical SHA-256).

This is not suitable for algebraic hash functions like Poseidon, whose natural input is exactly of the field elements.

Thus, it would be great if we have a ToConstraintFieldGadget that is implemented in many applicable gadgets. I have had some local implementations. And I am wondering whether we should do so. If so, I can submit a PR after the Great Refactoring.

weikengchen commented 4 years ago

This can also make the HashChain/Sponge easier to program since they can accept any types with a trait ToConstraintFieldGadget, rather than requiring the developers to write additional lines of code to convert it first to constraint field gadgets.