Skip to content

Commit

Permalink
fix(boojum): added inf point handling in add/sub mixed for sw_projective
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaMasych committed Oct 2, 2024
1 parent 31d01c1 commit 88349a3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
24 changes: 22 additions & 2 deletions crates/boojum/src/gadgets/curves/sw_projective/extended.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,15 +482,35 @@ where
cs: &mut CS,
other_xy: &mut (NN, NN),
) -> Self {
self.add_sub_mixed_impl(cs, other_xy, false)
let (x, y) = other_xy;

let x_is_zero = x.is_zero(cs);
let y_is_zero = y.is_zero(cs);

// Even though it is not algebraically correct, de-facto it is true.
let is_point2_inf = x_is_zero.and(cs, y_is_zero);

let result = self.add_sub_mixed_impl(cs, other_xy, false);

Self::conditionally_select(cs, is_point2_inf, self, &result)
}

pub fn sub_mixed<CS: ConstraintSystem<F>>(
&mut self,
cs: &mut CS,
other_xy: &mut (NN, NN),
) -> Self {
self.add_sub_mixed_impl(cs, other_xy, true)
let (x, y) = other_xy;

let x_is_zero = x.is_zero(cs);
let y_is_zero = y.is_zero(cs);

// Even though it is not algebraically correct, de-facto it is true.
let is_point2_inf = x_is_zero.and(cs, y_is_zero);

let result = self.add_sub_mixed_impl(cs, other_xy, true);

Self::conditionally_select(cs, is_point2_inf, self, &result)
}

pub fn convert_to_affine_or_default<CS: ConstraintSystem<F>>(
Expand Down
22 changes: 20 additions & 2 deletions crates/boojum/src/gadgets/curves/sw_projective/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,15 +478,33 @@ where
cs: &mut CS,
other_xy: &mut (NN, NN),
) -> Self {
self.add_sub_mixed_impl(cs, other_xy, false)
let (x, y) = other_xy;

let x_is_zero = x.is_zero(cs);
let y_is_zero = y.is_zero(cs);

let is_point2_inf = x_is_zero.and(cs, y_is_zero);

let result = self.add_sub_mixed_impl(cs, other_xy, false);

Self::conditionally_select(cs, is_point2_inf, self, &result)
}

pub fn sub_mixed<CS: ConstraintSystem<F>>(
&mut self,
cs: &mut CS,
other_xy: &mut (NN, NN),
) -> Self {
self.add_sub_mixed_impl(cs, other_xy, true)
let (x, y) = other_xy;

let x_is_zero = x.is_zero(cs);
let y_is_zero = y.is_zero(cs);

let is_point2_inf = x_is_zero.and(cs, y_is_zero);

let result = self.add_sub_mixed_impl(cs, other_xy, true);

Self::conditionally_select(cs, is_point2_inf, self, &result)
}

pub fn convert_to_affine_or_default<CS: ConstraintSystem<F>>(
Expand Down

0 comments on commit 88349a3

Please sign in to comment.