|
@ -214,39 +214,24 @@ where |
|
|
&x_diff_is_actual,
|
|
|
&x_diff_is_actual,
|
|
|
)?;
|
|
|
)?;
|
|
|
|
|
|
|
|
|
let x_diff_inv = AllocatedNum::alloc(cs.namespace(|| "x diff inverse"), || {
|
|
|
|
|
|
if *x_diff_is_actual.get_value().get()? == Fp::one() {
|
|
|
|
|
|
|
|
|
let lambda = AllocatedNum::alloc(cs.namespace(|| "lambda"), || {
|
|
|
|
|
|
let x_diff_inv = if *x_diff_is_actual.get_value().get()? == Fp::one() {
|
|
|
// Set to default
|
|
|
// Set to default
|
|
|
Ok(Fp::one())
|
|
|
|
|
|
|
|
|
Fp::one()
|
|
|
} else {
|
|
|
} else {
|
|
|
// Set to the actual inverse
|
|
|
// Set to the actual inverse
|
|
|
let inv = (*other.x.get_value().get()? - *self.x.get_value().get()?).invert();
|
|
|
|
|
|
if inv.is_some().unwrap_u8() == 1 {
|
|
|
|
|
|
Ok(inv.unwrap())
|
|
|
|
|
|
} else {
|
|
|
|
|
|
Err(SynthesisError::DivisionByZero)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
|
|
cs.enforce(
|
|
|
|
|
|
|| "Check inverse",
|
|
|
|
|
|
|lc| lc + x_diff.get_variable(),
|
|
|
|
|
|
|lc| lc + x_diff_inv.get_variable(),
|
|
|
|
|
|
|lc| lc + CS::one(),
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
(*other.x.get_value().get()? - *self.x.get_value().get()?)
|
|
|
|
|
|
.invert()
|
|
|
|
|
|
.unwrap()
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
let lambda = AllocatedNum::alloc(cs.namespace(|| "lambda"), || {
|
|
|
|
|
|
Ok(
|
|
|
|
|
|
(*other.y.get_value().get()? - *self.y.get_value().get()?)
|
|
|
|
|
|
* x_diff_inv.get_value().get()?,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
Ok((*other.y.get_value().get()? - *self.y.get_value().get()?) * x_diff_inv)
|
|
|
})?;
|
|
|
})?;
|
|
|
cs.enforce(
|
|
|
cs.enforce(
|
|
|
|| "Check that lambda is correct",
|
|
|
|| "Check that lambda is correct",
|
|
|
|lc| lc + other.y.get_variable() - self.y.get_variable(),
|
|
|
|
|
|
|lc| lc + x_diff_inv.get_variable(),
|
|
|
|
|
|
|lc| lc + lambda.get_variable(),
|
|
|
|lc| lc + lambda.get_variable(),
|
|
|
|
|
|
|lc| lc + x_diff.get_variable(),
|
|
|
|
|
|
|lc| lc + other.y.get_variable() - self.y.get_variable(),
|
|
|
);
|
|
|
);
|
|
|
|
|
|
|
|
|
//************************************************************************/
|
|
|
//************************************************************************/
|
|
@ -358,37 +343,24 @@ where |
|
|
|
|
|
|
|
|
let tmp = select_one_or_num2(cs.namespace(|| "tmp"), &tmp_actual, &self.is_infinity)?;
|
|
|
let tmp = select_one_or_num2(cs.namespace(|| "tmp"), &tmp_actual, &self.is_infinity)?;
|
|
|
|
|
|
|
|
|
// Compute inv = tmp.invert
|
|
|
|
|
|
let tmp_inv = AllocatedNum::alloc(cs.namespace(|| "tmp inverse"), || {
|
|
|
|
|
|
if *self.is_infinity.get_value().get()? == Fp::one() {
|
|
|
|
|
|
|
|
|
// Now compute lambda as (Fp::one() + Fp::one + Fp::one()) * self.x * self.x * tmp_inv
|
|
|
|
|
|
let prod_1 = AllocatedNum::alloc(cs.namespace(|| "alloc prod 1"), || {
|
|
|
|
|
|
let tmp_inv = if *self.is_infinity.get_value().get()? == Fp::one() {
|
|
|
// Return default value 1
|
|
|
// Return default value 1
|
|
|
Ok(Fp::one())
|
|
|
|
|
|
|
|
|
Fp::one()
|
|
|
} else {
|
|
|
} else {
|
|
|
// Return the actual inverse
|
|
|
// Return the actual inverse
|
|
|
let inv = (*tmp.get_value().get()?).invert();
|
|
|
|
|
|
if inv.is_some().unwrap_u8() == 1 {
|
|
|
|
|
|
Ok(inv.unwrap())
|
|
|
|
|
|
} else {
|
|
|
|
|
|
Err(SynthesisError::DivisionByZero)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
})?;
|
|
|
|
|
|
cs.enforce(
|
|
|
|
|
|
|| "Check inverse",
|
|
|
|
|
|
|lc| lc + tmp.get_variable(),
|
|
|
|
|
|
|lc| lc + tmp_inv.get_variable(),
|
|
|
|
|
|
|lc| lc + CS::one(),
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
(*tmp.get_value().get()?).invert().unwrap()
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
// Now compute lambda as (Fp::one() + Fp::one + Fp::one()) * self.x * self.x * tmp_inv
|
|
|
|
|
|
let prod_1 = AllocatedNum::alloc(cs.namespace(|| "alloc prod 1"), || {
|
|
|
|
|
|
Ok(*tmp_inv.get_value().get()? * self.x.get_value().get()?)
|
|
|
|
|
|
|
|
|
Ok(tmp_inv * self.x.get_value().get()?)
|
|
|
})?;
|
|
|
})?;
|
|
|
|
|
|
|
|
|
cs.enforce(
|
|
|
cs.enforce(
|
|
|
|| "Check prod 1",
|
|
|
|| "Check prod 1",
|
|
|
|lc| lc + self.x.get_variable(),
|
|
|
|
|
|
|lc| lc + tmp_inv.get_variable(),
|
|
|
|
|
|
|
|
|
|lc| lc + tmp.get_variable(),
|
|
|
|lc| lc + prod_1.get_variable(),
|
|
|
|lc| lc + prod_1.get_variable(),
|
|
|
|
|
|
|lc| lc + self.x.get_variable(),
|
|
|
);
|
|
|
);
|
|
|
|
|
|
|
|
|
let prod_2 = AllocatedNum::alloc(cs.namespace(|| "alloc prod 2"), || {
|
|
|
let prod_2 = AllocatedNum::alloc(cs.namespace(|| "alloc prod 2"), || {
|
|
|