Make mul_by_inverse use one constraint (#42)

Co-authored-by: Pratyush Mishra <pratyushmishra@berkeley.edu>
This commit is contained in:
Dev Ojha
2021-01-15 12:55:26 -06:00
committed by GitHub
parent bdc9fbbf30
commit f88d7c6ea3
3 changed files with 10 additions and 31 deletions

View File

@@ -716,21 +716,6 @@ impl<F: PrimeField> FieldVar<F, F> for FpVar<F> {
}
}
/// Returns (self / denominator), but requires fewer constraints than
/// self * denominator.inverse()
/// It is up to the caller to ensure that denominator is non-zero,
/// since in that case the result is unconstrained.
#[tracing::instrument(target = "r1cs")]
fn mul_by_inverse(&self, denominator: &Self) -> Result<Self, SynthesisError> {
use FpVar::*;
match (self, denominator) {
(Constant(s), Constant(d)) => Ok(Constant(*s / *d)),
(Var(s), Constant(d)) => Ok(Var(s.mul_constant(d.inverse().get()?))),
(Constant(s), Var(d)) => Ok(Var(d.inverse()?.mul_constant(*s))),
(Var(s), Var(d)) => Ok(Var(d.inverse()?.mul(s))),
}
}
#[tracing::instrument(target = "r1cs")]
fn frobenius_map(&self, power: usize) -> Result<Self, SynthesisError> {
match self {