Skip to content
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

Apply pattern matching to R1CS duplicate functions. #12

Closed
CPerezz opened this issue Aug 26, 2019 · 3 comments
Closed

Apply pattern matching to R1CS duplicate functions. #12

CPerezz opened this issue Aug 26, 2019 · 3 comments
Assignees

Comments

@CPerezz
Copy link
Contributor

CPerezz commented Aug 26, 2019

On the code we find examples of duplicated code on which we maybe can apply pattern-matching techniques:

fn input_full<T>(&self, data: &Vec<T>) -> bool {
        data.len() == self.t
    }
    pub fn width_left<T>(&self, data: &Vec<T>) -> usize {
        self.t - data.len()
    }
    pub fn input_bytes(&mut self, bytes: &[u8]) -> Result<(), PermError> {
        // Map arbitrary bytes to group using elligator2
        let scalar = Scalar::hash_from_bytes::<Sha512>(bytes);
        self.input(scalar)
    }
    pub fn input(&mut self, scalar: Scalar) -> Result<(), PermError> {
        if self.input_full(&self.data) {
            return Err(PermError::InputFull);
        }
        self.data.push(scalar);
        Ok(())
    }

or:

fn pad(&mut self) {
        let pad_amount = self.width_left(&self.data);
        let zero = Scalar::zero();
        let zeroes = vec![zero; pad_amount];

        self.data.extend(zeroes);
    }
    fn pad_lc(&mut self) {
        let pad_amount = self.width_left(&self.data_lc);
        let zero_lc: LinearCombination = Scalar::zero().into();
        let zeroes = vec![zero_lc; pad_amount];

        self.data_lc.extend(zeroes);
    }

The idea is to have only one function that operates depending on the parameter passed.
This will reduce the code complexity and provide an easier usage for the end user.

@CPerezz CPerezz self-assigned this Aug 26, 2019
@kevaundray kevaundray changed the title Apply patter matching to R1CS duplicate functions. Apply pattern matching to R1CS duplicate functions. Aug 26, 2019
@kevaundray
Copy link
Contributor

I think the example we discussed over the call, was due to derive macros.

Would be interesting to see the code complexity if these were implemented.

@CPerezz
Copy link
Contributor Author

CPerezz commented Aug 29, 2019

As the functions like result() and pad() have variadic arguments depending if we are working with R1CS or not, the only way to optimize the code (macros are inline) and also reduce the duplicate code is to use mentioned macros. Since type matching is quite ugly to apply.

Another option (that will prioritize readability) will be to implement these functions passing the extra parameters (if we work with R1CS) with Options.

Something like:

pub fn constrain_result(
        &mut self,
        cs: Option<&mut dyn ConstraintSystem>,
    ) -> Result<Vec<T>, PermError> { 
        match cs {} // Check if we have a cs or we are not in the R1CS case.
        // Code....
}

@CPerezz
Copy link
Contributor Author

CPerezz commented Sep 30, 2019

This does not make sense anymore since the changes done in #23

@CPerezz CPerezz closed this as completed Sep 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants