ZK-Garage / plonk

A pure Rust PLONK implementation using arkworks as a backend.
https://discord.gg/XWJdhVf37F
Mozilla Public License 2.0
295 stars 76 forks source link

Minor suggestions: using ToConstraintField, using HomomorphicCommitment everywhere, and removing unused append_commitments #89

Closed ghost closed 2 years ago

ghost commented 2 years ago

Some extra cleanup that I didn't do in the last PR.

  1. Instead of using special structs, use arkworks ToConstraintField for public inputs. This does have to use (unfortunately) Boxes or other dynamic dispatch (to get trait objects) but maybe this is better.
  2. There were some places I had used PolynomialCommitment<F, DensePolynomial<F>> instead of the HomomorphicCommitment<F> trait, which is slightly inconsistent. This makes everything consistent, using PrimeField as well.
  3. Removes the unused append_commitments option, since using append for every impl CanonicalSerialize worked well.
bhgomes commented 2 years ago

@joebebel

Instead of using special structs, use arkworks ToConstraintField for public inputs. This does have to use (unfortunately) Boxes or other dynamic dispatch (to get trait objects) but maybe this is better.

I would suggest a builder pattern like the following instead of dynamic dispatch:

/// Public Input Builder
#[derive(derivative::Derivative)]
#[derivative(Default(bound = ""))]
pub struct InputBuilder<F> 
where
    F: PrimeField,
{
    input: Vec<F>,
}

impl<F> InputBuilder<F>
where
    F: PrimeField,
{
    /// Pushes a new input `item` into `self`, returning `None` if `item` could not be converted
    /// into constraint field elements.
    pub fn push<T>(self, item: &T) -> Option<Self>
    where
        T: ToConstraintField<F>,
    {
        self.extend([item])
    }

    /// Extends the input buffer by appending all the elements of `iter` converted into constraint
    /// field elements, returning `None` if any of the conversions failed.
    pub fn extend<'t, T, I>(mut self, iter: I) -> Option<Self>
    where
        T: 't + ToConstraintField<F>,
        I: IntoIterator<Item = &'t T>,
    {
        for item in iter {
            self.input.append(&mut item.to_field_elements()?);
        }
        Some(self)
    }

    /// Returns the vector of input elements.
    pub fn finish(self) -> Vec<F> {
        self.input
    }
}