diff --git a/CHANGELOG.md b/CHANGELOG.md index b35b9d6bf7..b18f115316 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ #### Upcoming Changes +* Use to_signed_felt as function for felt252 as BigInt within [-P/2, P/2] range and use to_bigint as function for representation as BigInt. [#1100](https://github.com/lambdaclass/cairo-rs/pull/1100) + * Implement hint on field_arithmetic lib [#1090](https://github.com/lambdaclass/cairo-rs/pull/1090) `BuiltinHintProcessor` now supports the following hints: diff --git a/felt/src/bigint_felt.rs b/felt/src/bigint_felt.rs index 8e3ad92039..d123a7977d 100644 --- a/felt/src/bigint_felt.rs +++ b/felt/src/bigint_felt.rs @@ -182,7 +182,7 @@ impl FeltOps for FeltBigInt { self.val.to_str_radix(radix) } - fn to_bigint(&self) -> BigInt { + fn to_signed_felt(&self) -> BigInt { if self.is_negative() { BigInt::from_biguint(num_bigint::Sign::Minus, &*CAIRO_PRIME_BIGUINT - &self.val) } else { @@ -190,6 +190,10 @@ impl FeltOps for FeltBigInt { } } + fn to_bigint(&self) -> BigInt { + self.val.clone().into() + } + fn to_biguint(&self) -> BigUint { self.val.clone() } diff --git a/felt/src/lib.rs b/felt/src/lib.rs index c562faea1f..f16ac54bdd 100644 --- a/felt/src/lib.rs +++ b/felt/src/lib.rs @@ -65,6 +65,10 @@ pub(crate) trait FeltOps { /// let negative = Felt252::max_value(); /// assert_eq!(negative.to_bigint(), Into::::into(-1)); /// ``` + fn to_signed_felt(&self) -> BigInt; + + // Converts [`Felt252`]'s representation directly into a [`BigInt`]. + // Equivalent to doing felt.to_biguint().to_bigint(). fn to_bigint(&self) -> BigInt; /// Converts [`Felt252`] into a [`BigUint`] number. @@ -194,10 +198,17 @@ impl Felt252 { pub fn to_str_radix(&self, radix: u32) -> String { self.value.to_str_radix(radix) } + + pub fn to_signed_felt(&self) -> BigInt { + #[allow(deprecated)] + self.value.to_signed_felt() + } + pub fn to_bigint(&self) -> BigInt { #[allow(deprecated)] self.value.to_bigint() } + pub fn to_biguint(&self) -> BigUint { #[allow(deprecated)] self.value.to_biguint() diff --git a/src/hint_processor/builtin_hint_processor/bigint.rs b/src/hint_processor/builtin_hint_processor/bigint.rs index b9a0f83101..73e06580d7 100644 --- a/src/hint_processor/builtin_hint_processor/bigint.rs +++ b/src/hint_processor/builtin_hint_processor/bigint.rs @@ -50,8 +50,8 @@ pub fn bigint_pack_div_mod_hint( d2: x_bigint5.d2, }; let x_lower = x_lower.pack86().to_bigint().unwrap_or_default(); - let d3 = x_bigint5.d3.as_ref().to_biguint().to_bigint().unwrap(); - let d4 = x_bigint5.d4.as_ref().to_biguint().to_bigint().unwrap(); + let d3 = x_bigint5.d3.as_ref().to_bigint(); + let d4 = x_bigint5.d4.as_ref().to_bigint(); x_lower + d3 * BigInt::from(BASE.pow(3)) + d4 * BigInt::from(BASE.pow(4)) }; let y: BigInt = BigInt3::from_var_name("y", vm, ids_data, ap_tracking)? diff --git a/src/hint_processor/builtin_hint_processor/math_utils.rs b/src/hint_processor/builtin_hint_processor/math_utils.rs index 81a239f3f8..bc947f4db0 100644 --- a/src/hint_processor/builtin_hint_processor/math_utils.rs +++ b/src/hint_processor/builtin_hint_processor/math_utils.rs @@ -478,12 +478,9 @@ pub fn signed_div_rem( _ => {} } - #[allow(deprecated)] - let int_value = value.to_bigint(); - #[allow(deprecated)] - let int_div = div.to_bigint(); - #[allow(deprecated)] - let int_bound = bound.to_bigint(); + let int_value = value.to_signed_felt(); + let int_div = div.to_signed_felt(); + let int_bound = bound.to_signed_felt(); let (q, r) = int_value.div_mod_floor(&int_div); if int_bound.abs() < q.abs() { diff --git a/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs b/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs index c584a88d97..12062a5687 100644 --- a/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs +++ b/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs @@ -93,7 +93,7 @@ impl BigInt3<'_> { limbs .into_iter() .enumerate() - .map(|(idx, value)| value.to_bigint().shl(idx * 86)) + .map(|(idx, value)| value.to_signed_felt().shl(idx * 86)) .sum() } diff --git a/src/hint_processor/builtin_hint_processor/secp/secp_utils.rs b/src/hint_processor/builtin_hint_processor/secp/secp_utils.rs index 03607b8fd5..a872bb0601 100644 --- a/src/hint_processor/builtin_hint_processor/secp/secp_utils.rs +++ b/src/hint_processor/builtin_hint_processor/secp/secp_utils.rs @@ -100,7 +100,7 @@ pub(crate) fn bigint3_pack(num: Uint384) -> num_bigint::BigInt { limbs .into_iter() .enumerate() - .map(|(idx, value)| value.to_bigint().shl(idx * 86)) + .map(|(idx, value)| value.to_signed_felt().shl(idx * 86)) .sum() } diff --git a/src/vm/runners/builtin_runner/ec_op.rs b/src/vm/runners/builtin_runner/ec_op.rs index aa5099b627..89eb61ec8d 100644 --- a/src/vm/runners/builtin_runner/ec_op.rs +++ b/src/vm/runners/builtin_runner/ec_op.rs @@ -10,7 +10,7 @@ use crate::vm::errors::runner_errors::RunnerError; use crate::vm::vm_memory::memory::Memory; use crate::vm::vm_memory::memory_segments::MemorySegmentManager; use felt::Felt252; -use num_bigint::{BigInt, ToBigInt}; +use num_bigint::BigInt; use num_integer::{div_ceil, Integer}; use num_traits::{Num, One, Pow, Zero}; @@ -65,35 +65,9 @@ impl EcOpBuiltinRunner { prime: &BigInt, height: u32, ) -> Result<(BigInt, BigInt), RunnerError> { - let mut slope = m - .clone() - .to_biguint() - .to_bigint() - .ok_or(RunnerError::FoundNonInt)?; - let mut partial_sum_b = ( - partial_sum - .0 - .to_biguint() - .to_bigint() - .ok_or(RunnerError::FoundNonInt)?, - partial_sum - .1 - .to_biguint() - .to_bigint() - .ok_or(RunnerError::FoundNonInt)?, - ); - let mut doubled_point_b = ( - doubled_point - .0 - .to_biguint() - .to_bigint() - .ok_or(RunnerError::FoundNonInt)?, - doubled_point - .1 - .to_biguint() - .to_bigint() - .ok_or(RunnerError::FoundNonInt)?, - ); + let mut slope = m.to_bigint(); + let mut partial_sum_b = (partial_sum.0.to_bigint(), partial_sum.1.to_bigint()); + let mut doubled_point_b = (doubled_point.0.to_bigint(), doubled_point.1.to_bigint()); for _ in 0..height { if (doubled_point_b.0.clone() - partial_sum_b.0.clone()).is_zero() { #[allow(deprecated)] diff --git a/src/vm/vm_core.rs b/src/vm/vm_core.rs index c49591b157..84b0c9eb3e 100644 --- a/src/vm/vm_core.rs +++ b/src/vm/vm_core.rs @@ -937,7 +937,7 @@ impl VirtualMachine { .get(&Relocatable::from((segment_index as isize, i))) { Some(val) => match val.as_ref() { - MaybeRelocatable::Int(num) => format!("{}", num.to_bigint()), + MaybeRelocatable::Int(num) => format!("{}", num.to_signed_felt()), MaybeRelocatable::RelocatableValue(rel) => format!("{}", rel), }, _ => "".to_string(),