From 9f3da35a8ac3107190f8c85c8cf3ea1a0f8780a4 Mon Sep 17 00:00:00 2001 From: miguelis Date: Wed, 3 Jul 2024 19:50:10 +0200 Subject: [PATCH] Update ~-operator to use 254 bits instead of 256 --- circom_algebra/src/algebra.rs | 4 ++-- circom_algebra/src/modular_arithmetic.rs | 12 ++++++------ constraint_generation/src/execute.rs | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/circom_algebra/src/algebra.rs b/circom_algebra/src/algebra.rs index bfddd49e2..bb3a47b46 100644 --- a/circom_algebra/src/algebra.rs +++ b/circom_algebra/src/algebra.rs @@ -554,13 +554,13 @@ impl ArithmeticExpression { } // Bit operations - pub fn complement_256( + pub fn complement_254( elem: &ArithmeticExpression, field: &BigInt, ) -> ArithmeticExpression { use ArithmeticExpression::*; if let Number { value } = elem { - Number { value: modular_arithmetic::complement_256(value, field) } + Number { value: modular_arithmetic::complement_254(value, field) } } else { NonQuadratic } diff --git a/circom_algebra/src/modular_arithmetic.rs b/circom_algebra/src/modular_arithmetic.rs index 6fdfce35a..4eb9cc728 100644 --- a/circom_algebra/src/modular_arithmetic.rs +++ b/circom_algebra/src/modular_arithmetic.rs @@ -92,13 +92,13 @@ pub fn multi_inv(values: &Vec, field: &BigInt) -> Vec{ //Bit operations -// 256 bit complement -pub fn complement_256(elem: &BigInt, field: &BigInt) -> BigInt { +// 254 bit complement +pub fn complement_254(elem: &BigInt, field: &BigInt) -> BigInt { let (sign, mut bit_repr) = bit_representation(elem); - while bit_repr.len() > 256 { + while bit_repr.len() > 254 { bit_repr.pop(); } - for _i in bit_repr.len()..256 { + for _i in bit_repr.len()..254 { bit_repr.push(0); } for bit in &mut bit_repr { @@ -252,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_256(&big_num, &field); - let big_num_complement_complement = complement_256(&big_num_complement, &field); + let big_num_complement = complement_254(&big_num, &field); + let big_num_complement_complement = complement_254(&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 51704922c..887d52c38 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_256(value, field), + Complement => AExpr::complement_254(value, field), }; Result::Ok(result) }