Skip to content

Commit

Permalink
Refactor complement_254 function to handle sign correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelis committed Aug 22, 2024
1 parent 9a4215b commit b1f795d
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion circom_algebra/src/modular_arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ pub fn multi_inv(values: &Vec<BigInt>, field: &BigInt) -> Vec<BigInt>{
// 254 bit complement
pub fn complement_254(elem: &BigInt, field: &BigInt) -> BigInt {
let (sign, mut bit_repr) = bit_representation(elem);
let new_sign = if elem == &BigInt::from(0) { Sign::Plus } else { sign};
while bit_repr.len() > 254 {
bit_repr.pop();
}
Expand All @@ -104,7 +105,7 @@ pub fn complement_254(elem: &BigInt, field: &BigInt) -> BigInt {
for bit in &mut bit_repr {
*bit = if *bit == 0 { 1 } else { 0 };
}
let cp = BigInt::from_radix_le(sign, &bit_repr, 2).unwrap();
let cp = BigInt::from_radix_le(new_sign, &bit_repr, 2).unwrap();
modulus(&cp, field)
}

Expand Down

0 comments on commit b1f795d

Please sign in to comment.