Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 5 additions & 1 deletion felt/src/bigint_felt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,18 @@ impl FeltOps for FeltBigInt<FIELD_HIGH, FIELD_LOW> {
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 {
self.val.clone().into()
}
}

fn to_bigint(&self) -> BigInt {
self.val.clone().into()
}

fn to_biguint(&self) -> BigUint {
self.val.clone()
}
Expand Down
11 changes: 11 additions & 0 deletions felt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ pub(crate) trait FeltOps {
/// let negative = Felt252::max_value();
/// assert_eq!(negative.to_bigint(), Into::<num_bigint::BigInt>::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.
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions src/hint_processor/builtin_hint_processor/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?
Expand Down
9 changes: 3 additions & 6 deletions src/hint_processor/builtin_hint_processor/math_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
34 changes: 4 additions & 30 deletions src/vm/runners/builtin_runner/ec_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion src/vm/vm_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
_ => "<missing>".to_string(),
Expand Down