From f97b7cad87f23b5dc8d234a4af0795296a8406b9 Mon Sep 17 00:00:00 2001 From: miguelis Date: Wed, 25 Sep 2024 18:28:56 +0200 Subject: [PATCH] Complement function depends on the number of bits of the prime number --- circom_algebra/src/algebra.rs | 4 ++-- circom_algebra/src/modular_arithmetic.rs | 13 ++++++------- constraint_generation/src/execute.rs | 2 +- mkdocs/docs/circom-language/basic-operators.md | 8 ++++---- 4 files changed, 13 insertions(+), 14 deletions(-) diff --git a/circom_algebra/src/algebra.rs b/circom_algebra/src/algebra.rs index bb3a47b46..235b7e951 100644 --- a/circom_algebra/src/algebra.rs +++ b/circom_algebra/src/algebra.rs @@ -554,13 +554,13 @@ impl ArithmeticExpression { } // Bit operations - pub fn complement_254( + pub fn complement( elem: &ArithmeticExpression, field: &BigInt, ) -> ArithmeticExpression { use ArithmeticExpression::*; if let Number { value } = elem { - Number { value: modular_arithmetic::complement_254(value, field) } + Number { value: modular_arithmetic::complement(value, field) } } else { NonQuadratic } diff --git a/circom_algebra/src/modular_arithmetic.rs b/circom_algebra/src/modular_arithmetic.rs index f1d5790f2..60c386c05 100644 --- a/circom_algebra/src/modular_arithmetic.rs +++ b/circom_algebra/src/modular_arithmetic.rs @@ -91,15 +91,14 @@ pub fn multi_inv(values: &Vec, field: &BigInt) -> Vec{ } //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 { @@ -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); } diff --git a/constraint_generation/src/execute.rs b/constraint_generation/src/execute.rs index 887d52c38..1a4a3061a 100644 --- a/constraint_generation/src/execute.rs +++ b/constraint_generation/src/execute.rs @@ -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) } diff --git a/mkdocs/docs/circom-language/basic-operators.md b/mkdocs/docs/circom-language/basic-operators.md index 7ff89ffec..c2940bc7f 100644 --- a/mkdocs/docs/circom-language/basic-operators.md +++ b/mkdocs/docs/circom-language/basic-operators.md @@ -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 | | >> | a >> 4 | Right shift operator | | << | a << 4 | Left shift operator | @@ -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 | | >>= | a >>= 4 | Right shift operator and assignment | | <<= | a <<= 4 | Left shift operator and assignment |