Skip to content

Commit 72065eb

Browse files
authored
Use to_bigint to output representation as bigint (#1100)
* WIP to_bigint * use to_signed_felt where necessary * correct remaining to_biguint().to_bigint() pattern * address comments
1 parent c17aa23 commit 72065eb

File tree

9 files changed

+30
-42
lines changed

9 files changed

+30
-42
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#### Upcoming Changes
44

5+
* 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)
6+
57
* Implement hint on field_arithmetic lib [#1090](https://github.com/lambdaclass/cairo-rs/pull/1090)
68

79
`BuiltinHintProcessor` now supports the following hints:

felt/src/bigint_felt.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,18 @@ impl FeltOps for FeltBigInt<FIELD_HIGH, FIELD_LOW> {
182182
self.val.to_str_radix(radix)
183183
}
184184

185-
fn to_bigint(&self) -> BigInt {
185+
fn to_signed_felt(&self) -> BigInt {
186186
if self.is_negative() {
187187
BigInt::from_biguint(num_bigint::Sign::Minus, &*CAIRO_PRIME_BIGUINT - &self.val)
188188
} else {
189189
self.val.clone().into()
190190
}
191191
}
192192

193+
fn to_bigint(&self) -> BigInt {
194+
self.val.clone().into()
195+
}
196+
193197
fn to_biguint(&self) -> BigUint {
194198
self.val.clone()
195199
}

felt/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ pub(crate) trait FeltOps {
6565
/// let negative = Felt252::max_value();
6666
/// assert_eq!(negative.to_bigint(), Into::<num_bigint::BigInt>::into(-1));
6767
/// ```
68+
fn to_signed_felt(&self) -> BigInt;
69+
70+
// Converts [`Felt252`]'s representation directly into a [`BigInt`].
71+
// Equivalent to doing felt.to_biguint().to_bigint().
6872
fn to_bigint(&self) -> BigInt;
6973

7074
/// Converts [`Felt252`] into a [`BigUint`] number.
@@ -194,10 +198,17 @@ impl Felt252 {
194198
pub fn to_str_radix(&self, radix: u32) -> String {
195199
self.value.to_str_radix(radix)
196200
}
201+
202+
pub fn to_signed_felt(&self) -> BigInt {
203+
#[allow(deprecated)]
204+
self.value.to_signed_felt()
205+
}
206+
197207
pub fn to_bigint(&self) -> BigInt {
198208
#[allow(deprecated)]
199209
self.value.to_bigint()
200210
}
211+
201212
pub fn to_biguint(&self) -> BigUint {
202213
#[allow(deprecated)]
203214
self.value.to_biguint()

src/hint_processor/builtin_hint_processor/bigint.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ pub fn bigint_pack_div_mod_hint(
5050
d2: x_bigint5.d2,
5151
};
5252
let x_lower = x_lower.pack86().to_bigint().unwrap_or_default();
53-
let d3 = x_bigint5.d3.as_ref().to_biguint().to_bigint().unwrap();
54-
let d4 = x_bigint5.d4.as_ref().to_biguint().to_bigint().unwrap();
53+
let d3 = x_bigint5.d3.as_ref().to_bigint();
54+
let d4 = x_bigint5.d4.as_ref().to_bigint();
5555
x_lower + d3 * BigInt::from(BASE.pow(3)) + d4 * BigInt::from(BASE.pow(4))
5656
};
5757
let y: BigInt = BigInt3::from_var_name("y", vm, ids_data, ap_tracking)?

src/hint_processor/builtin_hint_processor/math_utils.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -478,12 +478,9 @@ pub fn signed_div_rem(
478478
_ => {}
479479
}
480480

481-
#[allow(deprecated)]
482-
let int_value = value.to_bigint();
483-
#[allow(deprecated)]
484-
let int_div = div.to_bigint();
485-
#[allow(deprecated)]
486-
let int_bound = bound.to_bigint();
481+
let int_value = value.to_signed_felt();
482+
let int_div = div.to_signed_felt();
483+
let int_bound = bound.to_signed_felt();
487484
let (q, r) = int_value.div_mod_floor(&int_div);
488485

489486
if int_bound.abs() < q.abs() {

src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl BigInt3<'_> {
9393
limbs
9494
.into_iter()
9595
.enumerate()
96-
.map(|(idx, value)| value.to_bigint().shl(idx * 86))
96+
.map(|(idx, value)| value.to_signed_felt().shl(idx * 86))
9797
.sum()
9898
}
9999

src/hint_processor/builtin_hint_processor/secp/secp_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ pub(crate) fn bigint3_pack(num: Uint384) -> num_bigint::BigInt {
100100
limbs
101101
.into_iter()
102102
.enumerate()
103-
.map(|(idx, value)| value.to_bigint().shl(idx * 86))
103+
.map(|(idx, value)| value.to_signed_felt().shl(idx * 86))
104104
.sum()
105105
}
106106

src/vm/runners/builtin_runner/ec_op.rs

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::vm::errors::runner_errors::RunnerError;
1010
use crate::vm::vm_memory::memory::Memory;
1111
use crate::vm::vm_memory::memory_segments::MemorySegmentManager;
1212
use felt::Felt252;
13-
use num_bigint::{BigInt, ToBigInt};
13+
use num_bigint::BigInt;
1414
use num_integer::{div_ceil, Integer};
1515
use num_traits::{Num, One, Pow, Zero};
1616

@@ -65,35 +65,9 @@ impl EcOpBuiltinRunner {
6565
prime: &BigInt,
6666
height: u32,
6767
) -> Result<(BigInt, BigInt), RunnerError> {
68-
let mut slope = m
69-
.clone()
70-
.to_biguint()
71-
.to_bigint()
72-
.ok_or(RunnerError::FoundNonInt)?;
73-
let mut partial_sum_b = (
74-
partial_sum
75-
.0
76-
.to_biguint()
77-
.to_bigint()
78-
.ok_or(RunnerError::FoundNonInt)?,
79-
partial_sum
80-
.1
81-
.to_biguint()
82-
.to_bigint()
83-
.ok_or(RunnerError::FoundNonInt)?,
84-
);
85-
let mut doubled_point_b = (
86-
doubled_point
87-
.0
88-
.to_biguint()
89-
.to_bigint()
90-
.ok_or(RunnerError::FoundNonInt)?,
91-
doubled_point
92-
.1
93-
.to_biguint()
94-
.to_bigint()
95-
.ok_or(RunnerError::FoundNonInt)?,
96-
);
68+
let mut slope = m.to_bigint();
69+
let mut partial_sum_b = (partial_sum.0.to_bigint(), partial_sum.1.to_bigint());
70+
let mut doubled_point_b = (doubled_point.0.to_bigint(), doubled_point.1.to_bigint());
9771
for _ in 0..height {
9872
if (doubled_point_b.0.clone() - partial_sum_b.0.clone()).is_zero() {
9973
#[allow(deprecated)]

src/vm/vm_core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,7 @@ impl VirtualMachine {
937937
.get(&Relocatable::from((segment_index as isize, i)))
938938
{
939939
Some(val) => match val.as_ref() {
940-
MaybeRelocatable::Int(num) => format!("{}", num.to_bigint()),
940+
MaybeRelocatable::Int(num) => format!("{}", num.to_signed_felt()),
941941
MaybeRelocatable::RelocatableValue(rel) => format!("{}", rel),
942942
},
943943
_ => "<missing>".to_string(),

0 commit comments

Comments
 (0)