Skip to content

Commit

Permalink
Complement function depends on the number of bits of the prime number
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelis committed Sep 25, 2024
1 parent c133004 commit f97b7ca
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
4 changes: 2 additions & 2 deletions circom_algebra/src/algebra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,13 +554,13 @@ impl<C: Default + Clone + Display + Hash + Eq> ArithmeticExpression<C> {
}

// Bit operations
pub fn complement_254(
pub fn complement(
elem: &ArithmeticExpression<C>,
field: &BigInt,
) -> ArithmeticExpression<C> {
use ArithmeticExpression::*;
if let Number { value } = elem {
Number { value: modular_arithmetic::complement_254(value, field) }
Number { value: modular_arithmetic::complement(value, field) }
} else {
NonQuadratic
}
Expand Down
13 changes: 6 additions & 7 deletions circom_algebra/src/modular_arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,14 @@ pub fn multi_inv(values: &Vec<BigInt>, field: &BigInt) -> Vec<BigInt>{
}

//Bit operations

// 254 bit complement
pub fn complement_254(elem: &BigInt, field: &BigInt) -> BigInt {
pub fn complement(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 {
let nbits = field.bits();
while bit_repr.len() > nbits {
bit_repr.pop();
}
for _i in bit_repr.len()..254 {
for _i in bit_repr.len()..nbits {
bit_repr.push(0);
}
for bit in &mut bit_repr {
Expand Down Expand Up @@ -253,8 +252,8 @@ mod tests {
.expect("generating the big int was not possible");
let big_num = BigInt::parse_bytes("1234".as_bytes(), 10)
.expect("generating the big int was not possible");
let big_num_complement = complement_254(&big_num, &field);
let big_num_complement_complement = complement_254(&big_num_complement, &field);
let big_num_complement = complement(&big_num, &field);
let big_num_complement_complement = complement(&big_num_complement, &field);
let big_num_modulus = modulus(&big_num, &field);
assert_eq!(big_num_complement_complement, big_num_modulus);
}
Expand Down
2 changes: 1 addition & 1 deletion constraint_generation/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2079,7 +2079,7 @@ fn execute_prefix_op(
let result = match prefix_op {
BoolNot => AExpr::not(value, field),
Sub => AExpr::prefix_sub(value, field),
Complement => AExpr::complement_254(value, field),
Complement => AExpr::complement(value, field),
};
Result::Ok(result)
}
Expand Down
8 changes: 4 additions & 4 deletions mkdocs/docs/circom-language/basic-operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ All bitwise operators are performed modulo p.
| :--- | :--- | :--- |
| & | a & b | Bitwise AND |
| \| | a \| b | Bitwise OR |
| ~ | ~a | Complement 254 bits |
| ^ | a ^ b | XOR 254 bits |
| ~ | ~a | Complement to the number of bits of the prime number |
| ^ | a ^ b | Bitwise XOR |
| &gt;&gt; | a &gt;&gt; 4 | Right shift operator |
| &lt;&lt; | a &lt;&lt; 4 | Left shift operator |

Expand All @@ -122,8 +122,8 @@ There are operators that combine bitwise operators with a final assignment.
| :--- | :--- | :--- |
| &= | a &= b | Bitwise AND and assignment |
| \|= | a \|= b | Bitwise OR and assignment |
| ~= | ~=a | Complement 254 bits and assignment |
| ^= | a ^= b | XOR 254 bits and assignment |
| ~= | ~=a | Complement to the number of bits of the prime number and assignment |
| ^= | a ^= b | Bitwise XOR and assignment |
| &gt;&gt;= | a &gt;&gt;= 4 | Right shift operator and assignment |
| &lt;&lt;= | a &lt;&lt;= 4 | Left shift operator and assignment |

Expand Down

0 comments on commit f97b7ca

Please sign in to comment.