diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f81fe4e13..b141e2de08 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ #### Upcoming Changes +* feat(BREAKING): Remove unecessary conversion functions between `Felt` & `BigUint`/`BigInt` [#1562](https://github.com/lambdaclass/cairo-vm/pull/1562) + * Remove the following functions: + * felt_from_biguint + * felt_from_bigint + * felt_to_biguint + * felt_to_bigint + * perf: optimize instruction cache allocations by using `VirtualMachine::load_data` [#1441](https://github.com/lambdaclass/cairo-vm/pull/1441) * feat: Add `print_output` flag to `cairo-1` crate [#1575] (https://github.com/lambdaclass/cairo-vm/pull/1575) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 002f42129c..35e7932706 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -42,7 +42,6 @@ use cairo_vm::serde::deserialize_program::BuiltinName; use cairo_vm::serde::deserialize_program::{ApTracking, FlowTrackingData, HintParams}; use cairo_vm::types::errors::program_errors::ProgramError; use cairo_vm::types::relocatable::Relocatable; -use cairo_vm::utils::bigint_to_felt; use cairo_vm::vm::decoding::decoder::decode_instruction; use cairo_vm::vm::errors::cairo_run_errors::CairoRunError; use cairo_vm::vm::errors::memory_errors::MemoryError; @@ -326,7 +325,7 @@ fn run(args: impl Iterator) -> Result, Error> { let data: Vec = instructions .flat_map(|inst| inst.assemble().encode()) - .map(|x| bigint_to_felt(&x).unwrap_or_default()) + .map(|x| Felt252::from(&x)) .map(MaybeRelocatable::from) .collect(); diff --git a/vm/src/hint_processor/builtin_hint_processor/ec_utils.rs b/vm/src/hint_processor/builtin_hint_processor/ec_utils.rs index 08f211b5a8..e3f29091e8 100644 --- a/vm/src/hint_processor/builtin_hint_processor/ec_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/ec_utils.rs @@ -1,5 +1,5 @@ use crate::stdlib::{borrow::Cow, boxed::Box, collections::HashMap, prelude::*}; -use crate::utils::{bigint_to_felt, biguint_to_felt, felt_to_biguint, CAIRO_PRIME}; +use crate::utils::CAIRO_PRIME; use crate::Felt252; use crate::{ hint_processor::{ @@ -144,10 +144,10 @@ pub fn recover_y_hint( let p_x = get_integer_from_var_name("x", vm, ids_data, ap_tracking)?.into_owned(); let p_addr = get_relocatable_from_var_name("p", vm, ids_data, ap_tracking)?; vm.insert_value(p_addr, p_x)?; - let p_y = biguint_to_felt( - &recover_y(&felt_to_biguint(p_x)) + let p_y = Felt252::from( + &recover_y(&p_x.to_biguint()) .ok_or_else(|| HintError::RecoverYPointNotOnCurve(Box::new(p_x)))?, - )?; + ); vm.insert_value((p_addr + 1)?, p_y)?; Ok(()) } @@ -174,8 +174,8 @@ fn random_ec_point_seeded(seed_bytes: Vec) -> Result<(Felt252, Felt252), Hin if let Some(y) = y { // Conversion from BigUint to BigInt doesnt fail return Ok(( - biguint_to_felt(&x)?, - bigint_to_felt(&(y.to_bigint().unwrap() * y_coef))?, + Felt252::from(&x), + Felt252::from(&(y.to_bigint().unwrap() * y_coef)), )); } } @@ -188,7 +188,7 @@ lazy_static! { 10 ) .unwrap(); - static ref FELT_MAX_HALVED: BigUint = felt_to_biguint(Felt252::MAX) / 2_u32; + static ref FELT_MAX_HALVED: BigUint = Felt252::MAX.to_biguint() / 2_u32; } // Recovers the corresponding y coordinate on the elliptic curve @@ -198,7 +198,7 @@ lazy_static! { fn recover_y(x: &BigUint) -> Option { let y_squared: BigUint = x.modpow(&BigUint::from(3_u32), &CAIRO_PRIME) + ALPHA * x + &*BETA; if is_quad_residue(&y_squared) { - Some(felt_to_biguint(biguint_to_felt(&y_squared).ok()?.sqrt()?)) + Some(Felt252::from(&y_squared).sqrt()?.to_biguint()) } else { None } diff --git a/vm/src/hint_processor/builtin_hint_processor/math_utils.rs b/vm/src/hint_processor/builtin_hint_processor/math_utils.rs index 1265c8ed8f..80c326394b 100644 --- a/vm/src/hint_processor/builtin_hint_processor/math_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/math_utils.rs @@ -3,7 +3,6 @@ use crate::{ math_utils::signed_felt, stdlib::{boxed::Box, collections::HashMap, prelude::*}, types::errors::math_errors::MathError, - utils::{bigint_to_felt, biguint_to_felt, felt_to_bigint, felt_to_biguint}, }; use lazy_static::lazy_static; use num_traits::{Signed, Zero}; @@ -112,8 +111,8 @@ pub fn assert_le_felt( let prime_over_2_high = constants .get(PRIME_OVER_2_HIGH) .ok_or_else(|| HintError::MissingConstant(Box::new(PRIME_OVER_2_HIGH)))?; - let a = felt_to_biguint(*get_integer_from_var_name("a", vm, ids_data, ap_tracking)?); - let b = felt_to_biguint(*get_integer_from_var_name("b", vm, ids_data, ap_tracking)?); + let a = get_integer_from_var_name("a", vm, ids_data, ap_tracking)?.to_biguint(); + let b = get_integer_from_var_name("b", vm, ids_data, ap_tracking)?.to_biguint(); let range_check_ptr = get_ptr_from_var_name("range_check_ptr", vm, ids_data, ap_tracking)?; // TODO: use UnsignedInteger for this @@ -122,8 +121,8 @@ pub fn assert_le_felt( if a > b { return Err(HintError::NonLeFelt252(Box::new(( - biguint_to_felt(&a)?, - biguint_to_felt(&b)?, + Felt252::from(&a), + Felt252::from(&b), )))); } @@ -134,23 +133,23 @@ pub fn assert_le_felt( // TODO: I believe this check can be removed if lengths_and_indices[0].0 > &prime_div3 || lengths_and_indices[1].0 > &prime_div2 { return Err(HintError::ArcTooBig(Box::new(( - biguint_to_felt(&lengths_and_indices[0].0.clone())?, - biguint_to_felt(&prime_div2)?, - biguint_to_felt(&lengths_and_indices[1].0.clone())?, - biguint_to_felt(&prime_div3)?, + Felt252::from(&lengths_and_indices[0].0.clone()), + Felt252::from(&prime_div2), + Felt252::from(&lengths_and_indices[1].0.clone()), + Felt252::from(&prime_div3), )))); } let excluded = lengths_and_indices[2].1; exec_scopes.assign_or_update_variable("excluded", any_box!(Felt252::from(excluded))); - let (q_0, r_0) = (lengths_and_indices[0].0).div_mod_floor(&felt_to_biguint(*prime_over_3_high)); - let (q_1, r_1) = (lengths_and_indices[1].0).div_mod_floor(&felt_to_biguint(*prime_over_2_high)); + let (q_0, r_0) = (lengths_and_indices[0].0).div_mod_floor(&prime_over_3_high.to_biguint()); + let (q_1, r_1) = (lengths_and_indices[1].0).div_mod_floor(&prime_over_2_high.to_biguint()); - vm.insert_value(range_check_ptr, biguint_to_felt(&r_0)?)?; - vm.insert_value((range_check_ptr + 1_i32)?, biguint_to_felt(&q_0)?)?; - vm.insert_value((range_check_ptr + 2_i32)?, biguint_to_felt(&r_1)?)?; - vm.insert_value((range_check_ptr + 3_i32)?, biguint_to_felt(&q_1)?)?; + vm.insert_value(range_check_ptr, Felt252::from(&r_0))?; + vm.insert_value((range_check_ptr + 1_i32)?, Felt252::from(&q_0))?; + vm.insert_value((range_check_ptr + 2_i32)?, Felt252::from(&r_1))?; + vm.insert_value((range_check_ptr + 3_i32)?, Felt252::from(&q_1))?; Ok(()) } @@ -383,7 +382,7 @@ pub fn is_positive( let (sign, abs_value) = value_as_int.into_parts(); //Main logic (assert a is positive) match &range_check_builtin._bound { - Some(bound) if abs_value > felt_to_biguint(*bound) => { + Some(bound) if abs_value > bound.to_biguint() => { return Err(HintError::ValueOutsideValidRange(Box::new( value.into_owned(), ))) @@ -457,7 +456,7 @@ pub fn sqrt( #[allow(deprecated)] insert_value_from_var_name( "root", - biguint_to_felt(&isqrt(&felt_to_biguint(*mod_value))?)?, + Felt252::from(&isqrt(&mod_value.to_biguint())?), vm, ids_data, ap_tracking, @@ -491,22 +490,22 @@ pub fn signed_div_rem( } let int_value = signed_felt(*value); - let int_div = felt_to_bigint(*div); - let int_bound = felt_to_bigint(*bound); + let int_div = div.to_bigint(); + let int_bound = bound.to_bigint(); let (q, r) = int_value.div_mod_floor(&int_div); if int_bound.abs() < q.abs() { return Err(HintError::OutOfValidRange(Box::new(( - bigint_to_felt(&q)?, + Felt252::from(&q), bound.into_owned(), )))); } let biased_q = q + int_bound; - insert_value_from_var_name("r", bigint_to_felt(&r)?, vm, ids_data, ap_tracking)?; + insert_value_from_var_name("r", Felt252::from(&r), vm, ids_data, ap_tracking)?; insert_value_from_var_name( "biased_q", - bigint_to_felt(&biased_q)?, + Felt252::from(&biased_q), vm, ids_data, ap_tracking, @@ -576,12 +575,12 @@ pub fn assert_250_bit( let shift = constants .get(SHIFT) .map_or_else(|| get_constant_from_var_name("SHIFT", constants), Ok)?; - let value = bigint_to_felt(&signed_felt(*get_integer_from_var_name( + let value = Felt252::from(&signed_felt(*get_integer_from_var_name( "value", vm, ids_data, ap_tracking, - )?))?; + )?)); //Main logic if &value > upper_bound { return Err(HintError::ValueOutside250BitRange(Box::new(value))); @@ -625,11 +624,10 @@ pub fn is_addr_bounded( ) -> Result<(), HintError> { let addr = get_integer_from_var_name("addr", vm, ids_data, ap_tracking)?; - let addr_bound = felt_to_biguint( - *constants - .get(ADDR_BOUND) - .ok_or_else(|| HintError::MissingConstant(Box::new(ADDR_BOUND)))?, - ); + let addr_bound = constants + .get(ADDR_BOUND) + .ok_or_else(|| HintError::MissingConstant(Box::new(ADDR_BOUND)))? + .to_biguint(); let lower_bound = BigUint::one() << 250_usize; let upper_bound = BigUint::one() << 251_usize; @@ -650,7 +648,7 @@ pub fn is_addr_bounded( } // Main logic: ids.is_small = 1 if ids.addr < ADDR_BOUND else 0 - let is_small = Felt252::from((addr.as_ref() < &biguint_to_felt(&addr_bound)?) as u8); + let is_small = Felt252::from((addr.as_ref() < &Felt252::from(&addr_bound)) as u8); insert_value_from_var_name("is_small", is_small, vm, ids_data, ap_tracking) } @@ -714,9 +712,8 @@ pub fn is_quad_residue( fn div_prime_by_bound(bound: Felt252) -> Result { let prime: &BigUint = &CAIRO_PRIME; - #[allow(deprecated)] - let limit = prime / felt_to_biguint(bound); - Ok(biguint_to_felt(&limit)?) + let limit = prime / bound.to_biguint(); + Ok(Felt252::from(&limit)) } fn prime_div_constant(bound: u32) -> Result { @@ -780,7 +777,7 @@ pub fn split_xx( ) -> Result<(), HintError> { let xx = Uint256::from_var_name("xx", vm, ids_data, ap_tracking)?; let x_addr = get_relocatable_from_var_name("x", vm, ids_data, ap_tracking)?; - let xx: BigUint = felt_to_biguint(*xx.low) + felt_to_biguint(*xx.high * pow2_const(128)); + let xx: BigUint = xx.low.to_biguint() + (*xx.high * pow2_const(128)).to_biguint(); let mut x = xx.modpow( &(&*SPLIT_XX_PRIME + 3_u32).div_floor(&BigUint::from(8_u32)), &SPLIT_XX_PRIME, @@ -794,9 +791,9 @@ pub fn split_xx( vm.insert_value( x_addr, - biguint_to_felt(&(&x & &BigUint::from(u128::max_value())))?, + Felt252::from(&(&x & &BigUint::from(u128::max_value()))), )?; - vm.insert_value((x_addr + 1)?, biguint_to_felt(&(x >> 128_u32))?)?; + vm.insert_value((x_addr + 1)?, Felt252::from(&(x >> 128_u32)))?; Ok(()) } diff --git a/vm/src/hint_processor/builtin_hint_processor/print.rs b/vm/src/hint_processor/builtin_hint_processor/print.rs index a73f8e8950..12a3e2c1df 100644 --- a/vm/src/hint_processor/builtin_hint_processor/print.rs +++ b/vm/src/hint_processor/builtin_hint_processor/print.rs @@ -12,7 +12,6 @@ use crate::stdlib::collections::HashMap; use crate::types::exec_scope::ExecutionScopes; use crate::types::relocatable::MaybeRelocatable; -use crate::utils::felt_to_bigint; use crate::vm::errors::hint_errors::HintError; use crate::{ hint_processor::hint_processor_definition::HintReference, vm::vm_core::VirtualMachine, @@ -34,7 +33,7 @@ fn print_name( ap_tracking: &ApTracking, ) -> Result<(), HintError> { let name = get_integer_from_var_name("name", vm, ids_data, ap_tracking)?; - let name = String::from_utf8(felt_to_bigint(*name.as_ref()).to_signed_bytes_be()) + let name = String::from_utf8(name.as_ref().to_bigint().to_signed_bytes_be()) .map_err(|err| HintError::CustomHint(err.to_string().into_boxed_str()))?; println!("{name}"); Ok(()) diff --git a/vm/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs b/vm/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs index c0e4a6d07a..5b32f4e691 100644 --- a/vm/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs @@ -3,8 +3,6 @@ use core::ops::Shl; use crate::hint_processor::builtin_hint_processor::uint_utils::{pack, split}; use crate::math_utils::signed_felt; use crate::stdlib::{borrow::Cow, boxed::Box, collections::HashMap, prelude::*}; -use crate::types::errors::math_errors::MathError; -use crate::utils::biguint_to_felt; use crate::Felt252; use crate::{ hint_processor::{ @@ -130,8 +128,8 @@ pub fn nondet_bigint3( .ok_or(HintError::BigIntToBigUintFail)?; let arg: Vec = bigint3_split(&value)? .into_iter() - .map(|ref n| biguint_to_felt(n).map(MaybeRelocatable::from)) - .collect::, MathError>>()?; + .map(|ref n| Felt252::from(n).into()) + .collect::>(); vm.write_arg(res_reloc, &arg).map_err(HintError::Memory)?; Ok(()) } diff --git a/vm/src/hint_processor/builtin_hint_processor/secp/ec_utils.rs b/vm/src/hint_processor/builtin_hint_processor/secp/ec_utils.rs index 700c5778ea..d4dbc3b910 100644 --- a/vm/src/hint_processor/builtin_hint_processor/secp/ec_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/secp/ec_utils.rs @@ -1,4 +1,3 @@ -use crate::utils::{biguint_to_felt, felt_to_biguint}; use crate::Felt252; use crate::{ hint_processor::{ @@ -503,7 +502,7 @@ pub fn n_pair_bits( return Err(HintError::NPairBitsTooLowM); } - let (scalar_v, scalar_u) = (felt_to_biguint(*scalar_v), felt_to_biguint(*scalar_u)); + let (scalar_v, scalar_u) = (scalar_v.to_biguint(), scalar_u.to_biguint()); // Each step, fetches the bits in mth position for v and u, // and appends them to the accumulator. i.e: @@ -512,7 +511,7 @@ pub fn n_pair_bits( // 1010101__ -> 101010110 let get_bit = |x: &BigUint, i| m.checked_sub(i).map(|i| x.bit(i.into())).unwrap_or(false) as u32; - let res: Felt252 = biguint_to_felt( + let res = Felt252::from( &(0..number_of_pairs) .map(|i| { // This code is definitely verbose, but it's the only way I found to avoid a `panic` @@ -527,7 +526,7 @@ pub fn n_pair_bits( acc += x; acc }), - )?; + ); /* ids.quad_bit = ( 8 * ((ids.scalar_v >> ids.m) & 1) diff --git a/vm/src/hint_processor/builtin_hint_processor/secp/field_utils.rs b/vm/src/hint_processor/builtin_hint_processor/secp/field_utils.rs index 8f34b1bead..fb620f6eab 100644 --- a/vm/src/hint_processor/builtin_hint_processor/secp/field_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/secp/field_utils.rs @@ -1,4 +1,3 @@ -use crate::utils::bigint_to_felt; use crate::Felt252; use crate::{ hint_processor::{ @@ -42,7 +41,7 @@ pub fn verify_zero( return Err(HintError::SecpVerifyZero(Box::new(val))); } - insert_value_from_var_name("q", bigint_to_felt(&q)?, vm, ids_data, ap_tracking) + insert_value_from_var_name("q", Felt252::from(&q), vm, ids_data, ap_tracking) } /* @@ -68,7 +67,7 @@ pub fn verify_zero_with_external_const( return Err(HintError::SecpVerifyZero(Box::new(val))); } - insert_value_from_var_name("q", bigint_to_felt(&q)?, vm, ids_data, ap_tracking) + insert_value_from_var_name("q", Felt252::from(&q), vm, ids_data, ap_tracking) } /* diff --git a/vm/src/hint_processor/builtin_hint_processor/secp/signature.rs b/vm/src/hint_processor/builtin_hint_processor/secp/signature.rs index f8000d465b..aee4450cc9 100644 --- a/vm/src/hint_processor/builtin_hint_processor/secp/signature.rs +++ b/vm/src/hint_processor/builtin_hint_processor/secp/signature.rs @@ -1,4 +1,3 @@ -use crate::utils::{felt_to_bigint, felt_to_biguint}; use crate::Felt252; use crate::{ any_box, @@ -111,11 +110,10 @@ pub fn get_point_from_x( ) -> Result<(), HintError> { exec_scopes.insert_value("SECP_P", SECP_P.clone()); #[allow(deprecated)] - let beta = felt_to_bigint( - *constants - .get(BETA) - .ok_or_else(|| HintError::MissingConstant(Box::new(BETA)))?, - ); + let beta = constants + .get(BETA) + .ok_or_else(|| HintError::MissingConstant(Box::new(BETA)))? + .to_bigint(); let x_cube_int = Uint384::from_var_name("x_cube", vm, ids_data, ap_tracking)? .pack86() @@ -125,7 +123,7 @@ pub fn get_point_from_x( let mut y = y_cube_int.modpow(&(&*SECP_P + 1_u32).shr(2_u32), &SECP_P); #[allow(deprecated)] - let v = felt_to_biguint(*get_integer_from_var_name("v", vm, ids_data, ap_tracking)?); + let v = get_integer_from_var_name("v", vm, ids_data, ap_tracking)?.to_bigint(); if v.is_even() != y.is_even() { y = &*SECP_P - y; } diff --git a/vm/src/hint_processor/builtin_hint_processor/uint256_utils.rs b/vm/src/hint_processor/builtin_hint_processor/uint256_utils.rs index 1416fec21d..30fc0d6396 100644 --- a/vm/src/hint_processor/builtin_hint_processor/uint256_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/uint256_utils.rs @@ -1,4 +1,3 @@ -use crate::utils::{biguint_to_felt, felt_to_biguint}; use crate::Felt252; use crate::{ hint_processor::builtin_hint_processor::hint_utils::{ @@ -76,13 +75,13 @@ impl<'a> Uint256<'a> { } pub(crate) fn pack(self) -> BigUint { - (felt_to_biguint(*self.high) << 128) + felt_to_biguint(*self.low) + (self.high.to_biguint() << 128) + self.low.to_biguint() } pub(crate) fn split(num: &BigUint) -> Self { let mask_low: BigUint = u128::MAX.into(); - let low = biguint_to_felt(&(num & mask_low)).unwrap(); - let high = biguint_to_felt(&(num >> 128)).unwrap(); + let low = Felt252::from(&(num & mask_low)); + let high = Felt252::from(&(num >> 128)); Self::from_values(low, high) } } @@ -292,7 +291,7 @@ pub fn uint256_sqrt( )); } - let root = biguint_to_felt(&root)?; + let root = Felt252::from(&root); if only_low { insert_value_from_var_name("root", root, vm, ids_data, ap_tracking)?; @@ -394,8 +393,8 @@ pub fn uint256_offseted_unsigned_div_rem( //ids.remainder.low = remainder & ((1 << 128) - 1) //ids.remainder.high = remainder >> 128 - let a = (felt_to_biguint(*a_high) << 128_u32) + felt_to_biguint(*a_low); - let div = (felt_to_biguint(*div_high) << 128_u32) + felt_to_biguint(*div_low); + let a = (a_high.to_biguint() << 128_u32) + a_low.to_biguint(); + let div = (div_high.to_biguint() << 128_u32) + div_low.to_biguint(); //a and div will always be positive numbers //Then, Rust div_rem equals Python divmod let (quotient, remainder) = div_rem(a, div); @@ -453,9 +452,9 @@ pub fn uint256_mul_div_mod( let div_high = div_high.as_ref(); // Main Logic - let a = felt_to_biguint(*a_high).shl(128_usize) + felt_to_biguint(*a_low); - let b = felt_to_biguint(*b_high).shl(128_usize) + felt_to_biguint(*b_low); - let div = felt_to_biguint(*div_high).shl(128_usize) + felt_to_biguint(*div_low); + let a = a_high.to_biguint().shl(128_usize) + a_low.to_biguint(); + let b = b_high.to_biguint().shl(128_usize) + b_low.to_biguint(); + let div = div_high.to_biguint().shl(128_usize) + div_low.to_biguint(); if div.is_zero() { return Err(MathError::DividedByZero.into()); } @@ -464,32 +463,32 @@ pub fn uint256_mul_div_mod( // ids.quotient_low.low vm.insert_value( quotient_low_addr, - biguint_to_felt(&("ient & &BigUint::from(u128::MAX)))?, + Felt252::from(&("ient & &BigUint::from(u128::MAX))), )?; // ids.quotient_low.high vm.insert_value( (quotient_low_addr + 1)?, - biguint_to_felt(&(("ient).shr(128_u32) & &BigUint::from(u128::MAX)))?, + Felt252::from(&(("ient).shr(128_u32) & &BigUint::from(u128::MAX))), )?; // ids.quotient_high.low vm.insert_value( quotient_high_addr, - biguint_to_felt(&(("ient).shr(256_u32) & &BigUint::from(u128::MAX)))?, + Felt252::from(&(("ient).shr(256_u32) & &BigUint::from(u128::MAX))), )?; // ids.quotient_high.high vm.insert_value( (quotient_high_addr + 1)?, - biguint_to_felt(&(("ient).shr(384_u32)))?, + Felt252::from(&(("ient).shr(384_u32))), )?; //ids.remainder.low vm.insert_value( remainder_addr, - biguint_to_felt(&(&remainder & &BigUint::from(u128::MAX)))?, + Felt252::from(&(&remainder & &BigUint::from(u128::MAX))), )?; //ids.remainder.high vm.insert_value( (remainder_addr + 1)?, - biguint_to_felt(&remainder.shr(128_u32))?, + Felt252::from(&remainder.shr(128_u32)), )?; Ok(()) diff --git a/vm/src/hint_processor/builtin_hint_processor/uint384.rs b/vm/src/hint_processor/builtin_hint_processor/uint384.rs index c7d6ba3465..c3a9f35683 100644 --- a/vm/src/hint_processor/builtin_hint_processor/uint384.rs +++ b/vm/src/hint_processor/builtin_hint_processor/uint384.rs @@ -1,5 +1,5 @@ -use crate::utils::felt_to_biguint; use crate::Felt252; +use num_bigint::BigUint; use num_integer::Integer; use num_traits::Zero; @@ -107,21 +107,31 @@ pub fn add_no_uint384_check( let a = Uint384::from_var_name("a", vm, ids_data, ap_tracking)?; let b = Uint384::from_var_name("b", vm, ids_data, ap_tracking)?; // This hint is not from the cairo commonlib, and its lib can be found under different paths, so we cant rely on a full path name - let shift = felt_to_biguint(*get_constant_from_var_name("SHIFT", constants)?); - - let sum_d0 = felt_to_biguint(*a.limbs[0].as_ref()) + felt_to_biguint(*b.limbs[0].as_ref()); - let carry_d0 = Felt252::from((sum_d0 >= shift) as usize); - let sum_d1 = felt_to_biguint(*a.limbs[1].as_ref()) - + felt_to_biguint(*b.limbs[1].as_ref()) - + felt_to_biguint(carry_d0); - let carry_d1 = Felt252::from((sum_d1 >= shift) as usize); - let sum_d2 = felt_to_biguint(*a.limbs[2].as_ref()) - + felt_to_biguint(*b.limbs[2].as_ref()) - + felt_to_biguint(carry_d1); + let shift = get_constant_from_var_name("SHIFT", constants)?.to_biguint(); + + let sum_d0 = (a.limbs[0].as_ref().to_biguint()) + (b.limbs[0].as_ref().to_biguint()); + let carry_d0 = BigUint::from((sum_d0 >= shift) as usize); + let sum_d1 = + (a.limbs[1].as_ref().to_biguint()) + (b.limbs[1].as_ref().to_biguint()) + &carry_d0; + let carry_d1 = BigUint::from((sum_d1 >= shift) as usize); + let sum_d2 = + (a.limbs[2].as_ref().to_biguint()) + (b.limbs[2].as_ref().to_biguint()) + &carry_d1; let carry_d2 = Felt252::from((sum_d2 >= shift) as usize); - insert_value_from_var_name("carry_d0", carry_d0, vm, ids_data, ap_tracking)?; - insert_value_from_var_name("carry_d1", carry_d1, vm, ids_data, ap_tracking)?; + insert_value_from_var_name( + "carry_d0", + Felt252::from(&carry_d0), + vm, + ids_data, + ap_tracking, + )?; + insert_value_from_var_name( + "carry_d1", + Felt252::from(&carry_d1), + vm, + ids_data, + ap_tracking, + )?; insert_value_from_var_name("carry_d2", carry_d2, vm, ids_data, ap_tracking) } diff --git a/vm/src/hint_processor/builtin_hint_processor/uint_utils.rs b/vm/src/hint_processor/builtin_hint_processor/uint_utils.rs index 7db0b95593..ac2018bd3a 100644 --- a/vm/src/hint_processor/builtin_hint_processor/uint_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/uint_utils.rs @@ -1,7 +1,4 @@ -use crate::{ - utils::{biguint_to_felt, felt_to_biguint}, - Felt252, -}; +use crate::Felt252; use num_bigint::BigUint; use num_traits::One; @@ -11,7 +8,7 @@ pub(crate) fn split(num: &BigUint, num_bits_shift: u32) -> [Felt [0; N].map(|_| { let a = &num & bitmask; num >>= num_bits_shift; - biguint_to_felt(&a).unwrap() + Felt252::from(&a) }) } @@ -22,6 +19,6 @@ pub(crate) fn pack( limbs .into_iter() .enumerate() - .map(|(i, limb)| felt_to_biguint(*limb.as_ref()) << (i * num_bits_shift)) + .map(|(i, limb)| limb.as_ref().to_biguint() << (i * num_bits_shift)) .sum() } diff --git a/vm/src/hint_processor/builtin_hint_processor/vrf/inv_mod_p_uint512.rs b/vm/src/hint_processor/builtin_hint_processor/vrf/inv_mod_p_uint512.rs index 8854c15a34..ef8748a1bc 100644 --- a/vm/src/hint_processor/builtin_hint_processor/vrf/inv_mod_p_uint512.rs +++ b/vm/src/hint_processor/builtin_hint_processor/vrf/inv_mod_p_uint512.rs @@ -1,7 +1,7 @@ use crate::hint_processor::builtin_hint_processor::secp::bigint_utils::Uint512; use crate::hint_processor::builtin_hint_processor::uint256_utils::Uint256; use crate::stdlib::prelude::String; -use crate::utils::bigint_to_felt; +use crate::Felt252; use crate::{ hint_processor::hint_processor_definition::HintReference, math_utils::div_mod, serde::deserialize_program::ApTracking, stdlib::collections::HashMap, @@ -38,11 +38,11 @@ pub fn inv_mod_p_uint512( let p = Uint256::from_var_name("p", vm, ids_data, ap_tracking)?.pack(); - let x_inverse_mod_p = bigint_to_felt(&div_mod( + let x_inverse_mod_p = Felt252::from(&div_mod( &BigInt::one(), &BigInt::from(x), &BigInt::from(p), - )?)?; + )?); let x_inverse_mod_p = Uint256::from(x_inverse_mod_p); x_inverse_mod_p.insert_from_var_name("x_inverse_mod_p", vm, ids_data, ap_tracking)?; diff --git a/vm/src/hint_processor/cairo_1_hint_processor/hint_processor.rs b/vm/src/hint_processor/cairo_1_hint_processor/hint_processor.rs index b97107f865..577c467724 100644 --- a/vm/src/hint_processor/cairo_1_hint_processor/hint_processor.rs +++ b/vm/src/hint_processor/cairo_1_hint_processor/hint_processor.rs @@ -5,8 +5,6 @@ use crate::hint_processor::cairo_1_hint_processor::dict_manager::DictSquashExecS use crate::hint_processor::hint_processor_definition::HintReference; use crate::stdlib::{boxed::Box, collections::HashMap, prelude::*}; use crate::types::relocatable::Relocatable; -use crate::utils::biguint_to_felt; -use crate::utils::felt_to_biguint; use crate::vm::runners::cairo_runner::ResourceTracker; use crate::vm::runners::cairo_runner::RunResources; use crate::Felt252; @@ -278,9 +276,9 @@ impl Cairo1HintProcessor { value: &ResOperand, dst: &CellRef, ) -> Result<(), HintError> { - let value = felt_to_biguint(res_operand_get_val(vm, value)?); + let value = res_operand_get_val(vm, value)?.to_biguint(); let result = value.sqrt(); - vm.insert_value(cell_ref_to_relocatable(dst, vm)?, biguint_to_felt(&result)?) + vm.insert_value(cell_ref_to_relocatable(dst, vm)?, Felt252::from(&result)) .map_err(HintError::from) } @@ -324,19 +322,19 @@ impl Cairo1HintProcessor { let range_check_ptr = get_ptr(vm, range_check_base, &range_check_offset)?; vm.insert_value( range_check_ptr, - biguint_to_felt(&(felt_to_biguint(lengths_and_indices[0].0) % prime_over_3_high))?, + Felt252::from(&(lengths_and_indices[0].0.to_biguint() % prime_over_3_high)), )?; vm.insert_value( (range_check_ptr + 1)?, - biguint_to_felt(&(felt_to_biguint(lengths_and_indices[0].0) / prime_over_3_high))?, + Felt252::from(&(lengths_and_indices[0].0.to_biguint() / prime_over_3_high)), )?; vm.insert_value( (range_check_ptr + 2)?, - biguint_to_felt(&(felt_to_biguint(lengths_and_indices[1].0) % prime_over_2_high))?, + Felt252::from(&(lengths_and_indices[1].0.to_biguint() % prime_over_2_high)), )?; vm.insert_value( (range_check_ptr + 3)?, - biguint_to_felt(&(felt_to_biguint(lengths_and_indices[1].0) / prime_over_2_high))?, + Felt252::from(&(lengths_and_indices[1].0.to_biguint() / prime_over_2_high)), ) .map_err(HintError::from) } @@ -371,10 +369,10 @@ impl Cairo1HintProcessor { quotient: &CellRef, remainder: &CellRef, ) -> Result<(), HintError> { - let lhs_value = felt_to_biguint(res_operand_get_val(vm, lhs)?); - let rhs_value = felt_to_biguint(res_operand_get_val(vm, rhs)?); - let quotient_value = biguint_to_felt(&(&lhs_value / &rhs_value))?; - let remainder_value = biguint_to_felt(&(lhs_value % rhs_value))?; + let lhs_value = res_operand_get_val(vm, lhs)?.to_biguint(); + let rhs_value = res_operand_get_val(vm, rhs)?.to_biguint(); + let quotient_value = Felt252::from(&(&lhs_value / &rhs_value)); + let remainder_value = Felt252::from(&(lhs_value % rhs_value)); vm.insert_value(cell_ref_to_relocatable(quotient, vm)?, quotient_value)?; vm.insert_value(cell_ref_to_relocatable(remainder, vm)?, remainder_value) .map_err(HintError::from) @@ -412,30 +410,30 @@ impl Cairo1HintProcessor { remainder1: &CellRef, ) -> Result<(), HintError> { let pow_2_128 = BigUint::from(u128::MAX) + 1u32; - let dividend0 = felt_to_biguint(get_val(vm, dividend0)?); - let dividend1 = felt_to_biguint(get_val(vm, dividend1)?); - let divisor0 = felt_to_biguint(get_val(vm, divisor0)?); - let divisor1 = felt_to_biguint(get_val(vm, divisor1)?); + let dividend0 = get_val(vm, dividend0)?.to_biguint(); + let dividend1 = get_val(vm, dividend1)?.to_biguint(); + let divisor0 = get_val(vm, divisor0)?.to_biguint(); + let divisor1 = get_val(vm, divisor1)?.to_biguint(); let dividend: BigUint = dividend0 + dividend1.shl(128); let divisor = divisor0 + divisor1.shl(128); let (quotient, remainder) = dividend.div_rem(&divisor); let (limb1, limb0) = quotient.div_rem(&pow_2_128); vm.insert_value( cell_ref_to_relocatable(quotient0, vm)?, - biguint_to_felt(&limb0)?, + Felt252::from(&limb0), )?; vm.insert_value( cell_ref_to_relocatable(quotient1, vm)?, - biguint_to_felt(&limb1)?, + Felt252::from(&limb1), )?; let (limb1, limb0) = remainder.div_rem(&pow_2_128); vm.insert_value( cell_ref_to_relocatable(remainder0, vm)?, - biguint_to_felt(&limb0)?, + Felt252::from(&limb0), )?; vm.insert_value( cell_ref_to_relocatable(remainder1, vm)?, - biguint_to_felt(&limb1)?, + Felt252::from(&limb1), )?; Ok(()) @@ -462,15 +460,15 @@ impl Cairo1HintProcessor { x: &CellRef, y: &CellRef, ) -> Result<(), HintError> { - let value = felt_to_biguint(res_operand_get_val(vm, value)?); - let scalar = felt_to_biguint(res_operand_get_val(vm, scalar)?); - let max_x = felt_to_biguint(res_operand_get_val(vm, max_x)?); + let value = res_operand_get_val(vm, value)?.to_biguint(); + let scalar = res_operand_get_val(vm, scalar)?.to_biguint(); + let max_x = res_operand_get_val(vm, max_x)?.to_biguint(); let x_value = (&value / &scalar).min(max_x); let y_value = value - &x_value * &scalar; - vm.insert_value(cell_ref_to_relocatable(x, vm)?, biguint_to_felt(&x_value)?) + vm.insert_value(cell_ref_to_relocatable(x, vm)?, Felt252::from(&x_value)) .map_err(HintError::from)?; - vm.insert_value(cell_ref_to_relocatable(y, vm)?, biguint_to_felt(&y_value)?) + vm.insert_value(cell_ref_to_relocatable(y, vm)?, Felt252::from(&y_value)) .map_err(HintError::from)?; Ok(()) @@ -482,7 +480,7 @@ impl Cairo1HintProcessor { x: &CellRef, y: &CellRef, ) -> Result<(), HintError> { - let beta = Fq::from(felt_to_biguint(get_beta())); + let beta = Fq::from(get_beta().to_biguint()); let mut rng = ark_std::test_rng(); let (random_x, random_y_squared) = loop { @@ -502,8 +500,8 @@ impl Cairo1HintProcessor { .into_bigint() .into(); - vm.insert_value(cell_ref_to_relocatable(x, vm)?, biguint_to_felt(&x_bigint)?)?; - vm.insert_value(cell_ref_to_relocatable(y, vm)?, biguint_to_felt(&y_bigint)?)?; + vm.insert_value(cell_ref_to_relocatable(x, vm)?, Felt252::from(&x_bigint))?; + vm.insert_value(cell_ref_to_relocatable(y, vm)?, Felt252::from(&y_bigint))?; Ok(()) } @@ -621,12 +619,12 @@ impl Cairo1HintProcessor { remainder1: &CellRef, ) -> Result<(), HintError> { let pow_2_128 = BigUint::from(u128::MAX) + 1u32; - let dividend0 = felt_to_biguint(res_operand_get_val(vm, dividend0)?); - let dividend1 = felt_to_biguint(res_operand_get_val(vm, dividend1)?); - let dividend2 = felt_to_biguint(res_operand_get_val(vm, dividend2)?); - let dividend3 = felt_to_biguint(res_operand_get_val(vm, dividend3)?); - let divisor0 = felt_to_biguint(res_operand_get_val(vm, divisor0)?); - let divisor1 = felt_to_biguint(res_operand_get_val(vm, divisor1)?); + let dividend0 = res_operand_get_val(vm, dividend0)?.to_biguint(); + let dividend1 = res_operand_get_val(vm, dividend1)?.to_biguint(); + let dividend2 = res_operand_get_val(vm, dividend2)?.to_biguint(); + let dividend3 = res_operand_get_val(vm, dividend3)?.to_biguint(); + let divisor0 = res_operand_get_val(vm, divisor0)?.to_biguint(); + let divisor1 = res_operand_get_val(vm, divisor1)?.to_biguint(); let dividend: BigUint = dividend0 + dividend1.shl(128) + dividend2.shl(256) + dividend3.shl(384); let divisor = divisor0 + divisor1.shl(128); @@ -635,33 +633,33 @@ impl Cairo1HintProcessor { vm.insert_value( cell_ref_to_relocatable(quotient0, vm)?, - biguint_to_felt(&limb0)?, + Felt252::from(&limb0), )?; let (quotient, limb1) = quotient.div_rem(&pow_2_128); vm.insert_value( cell_ref_to_relocatable(quotient1, vm)?, - biguint_to_felt(&limb1)?, + Felt252::from(&limb1), )?; let (limb3, limb2) = quotient.div_rem(&pow_2_128); vm.insert_value( cell_ref_to_relocatable(quotient2, vm)?, - biguint_to_felt(&limb2)?, + Felt252::from(&limb2), )?; vm.insert_value( cell_ref_to_relocatable(quotient3, vm)?, - biguint_to_felt(&limb3)?, + Felt252::from(&limb3), )?; let (limb1, limb0) = remainder.div_rem(&pow_2_128); vm.insert_value( cell_ref_to_relocatable(remainder0, vm)?, - biguint_to_felt(&limb0)?, + Felt252::from(&limb0), )?; vm.insert_value( cell_ref_to_relocatable(remainder1, vm)?, - biguint_to_felt(&limb1)?, + Felt252::from(&limb1), )?; Ok(()) @@ -681,8 +679,8 @@ impl Cairo1HintProcessor { ) -> Result<(), HintError> { let pow_2_128 = BigUint::from(u128::MAX) + 1u32; let pow_2_64 = BigUint::from(u64::MAX) + 1u32; - let value_low = felt_to_biguint(res_operand_get_val(vm, value_low)?); - let value_high = felt_to_biguint(res_operand_get_val(vm, value_high)?); + let value_low = res_operand_get_val(vm, value_low)?.to_biguint(); + let value_high = res_operand_get_val(vm, value_high)?.to_biguint(); let value = value_low + value_high * pow_2_128.clone(); let sqrt = value.sqrt(); let remainder = value - sqrt.clone() * sqrt.clone(); @@ -693,22 +691,22 @@ impl Cairo1HintProcessor { let (sqrt1_val, sqrt0_val) = sqrt.div_rem(&pow_2_64); vm.insert_value( cell_ref_to_relocatable(sqrt0, vm)?, - biguint_to_felt(&sqrt0_val)?, + Felt252::from(&sqrt0_val), )?; vm.insert_value( cell_ref_to_relocatable(sqrt1, vm)?, - biguint_to_felt(&sqrt1_val)?, + Felt252::from(&sqrt1_val), )?; let (remainder_high_val, remainder_low_val) = remainder.div_rem(&pow_2_128); vm.insert_value( cell_ref_to_relocatable(remainder_low, vm)?, - biguint_to_felt(&remainder_low_val)?, + Felt252::from(&remainder_low_val), )?; vm.insert_value( cell_ref_to_relocatable(remainder_high, vm)?, - biguint_to_felt(&remainder_high_val)?, + Felt252::from(&remainder_high_val), )?; vm.insert_value( cell_ref_to_relocatable(sqrt_mul_2_minus_remainder_ge_u128, vm)?, @@ -1024,7 +1022,7 @@ impl Cairo1HintProcessor { val: &ResOperand, sqrt: &CellRef, ) -> Result<(), HintError> { - let value = Fq::from(felt_to_biguint(res_operand_get_val(vm, val)?)); + let value = Fq::from(res_operand_get_val(vm, val)?.to_biguint()); let three_fq = Fq::from(BigUint::from(3_u32)); let res = if value.legendre().is_qr() { @@ -1036,7 +1034,7 @@ impl Cairo1HintProcessor { if let Some(root) = res.sqrt() { let root0: BigUint = root.into_bigint().into(); let root1: BigUint = (-root).into_bigint().into(); - let root = biguint_to_felt(&core::cmp::min(root0, root1))?; + let root = Felt252::from(&core::cmp::min(root0, root1)); vm.insert_value(cell_ref_to_relocatable(sqrt, vm)?, root) .map_err(HintError::from) } else { @@ -1055,16 +1053,16 @@ impl Cairo1HintProcessor { low: &CellRef, ) -> Result<(), HintError> { let mask128 = BigUint::from(u128::MAX); - let lhs_val = felt_to_biguint(res_operand_get_val(vm, lhs)?); - let rhs_val = felt_to_biguint(res_operand_get_val(vm, rhs)?); + let lhs_val = res_operand_get_val(vm, lhs)?.to_biguint(); + let rhs_val = res_operand_get_val(vm, rhs)?.to_biguint(); let prod = lhs_val * rhs_val; vm.insert_value( cell_ref_to_relocatable(high, vm)?, - biguint_to_felt(&(prod.clone() >> 128))?, + Felt252::from(&(prod.clone() >> 128)), )?; vm.insert_value( cell_ref_to_relocatable(low, vm)?, - biguint_to_felt(&(prod & mask128))?, + Felt252::from(&(prod & mask128)), ) .map_err(HintError::from) } diff --git a/vm/src/hint_processor/cairo_1_hint_processor/hint_processor_utils.rs b/vm/src/hint_processor/cairo_1_hint_processor/hint_processor_utils.rs index f549fc23a3..02a96a5560 100644 --- a/vm/src/hint_processor/cairo_1_hint_processor/hint_processor_utils.rs +++ b/vm/src/hint_processor/cairo_1_hint_processor/hint_processor_utils.rs @@ -1,6 +1,5 @@ use crate::stdlib::prelude::*; use crate::types::{errors::math_errors::MathError, relocatable::Relocatable}; -use crate::utils::bigint_to_felt; use crate::vm::errors::{hint_errors::HintError, vm_errors::VirtualMachineError}; use crate::vm::vm_core::VirtualMachine; use crate::Felt252; @@ -12,7 +11,7 @@ pub(crate) fn extract_buffer(buffer: &ResOperand) -> Result<(&CellRef, Felt252), ResOperand::Deref(cell) => (cell, 0.into()), ResOperand::BinOp(bin_op) => { if let DerefOrImmediate::Immediate(val) = &bin_op.b { - (&bin_op.a, bigint_to_felt(&val.value)?) + (&bin_op.a, Felt252::from(&val.value)) } else { return Err(HintError::CustomHint( "Failed to extract buffer, expected ResOperand of BinOp type to have Inmediate b value".to_owned().into_boxed_str() @@ -40,12 +39,12 @@ pub(crate) fn get_val( ResOperand::DoubleDeref(cell, offset) => { get_double_deref_val(vm, cell, &Felt252::from(*offset as i32)) } - ResOperand::Immediate(x) => Ok(bigint_to_felt(&x.value)?), + ResOperand::Immediate(x) => Ok(Felt252::from(&x.value)), ResOperand::BinOp(op) => { let a = get_cell_val(vm, &op.a)?; let b = match &op.b { DerefOrImmediate::Deref(cell) => get_cell_val(vm, cell)?, - DerefOrImmediate::Immediate(x) => bigint_to_felt(&x.value)?, + DerefOrImmediate::Immediate(x) => Felt252::from(&x.value), }; match op.op { Operation::Add => Ok(a + b), @@ -108,12 +107,12 @@ pub(crate) fn res_operand_get_val( ResOperand::DoubleDeref(cell, offset) => { get_double_deref_val(vm, cell, &Felt252::from(*offset as i32)) } - ResOperand::Immediate(x) => Ok(bigint_to_felt(&x.value)?), + ResOperand::Immediate(x) => Ok(Felt252::from(&x.value)), ResOperand::BinOp(op) => { let a = get_cell_val(vm, &op.a)?; let b = match &op.b { DerefOrImmediate::Deref(cell) => get_cell_val(vm, cell)?, - DerefOrImmediate::Immediate(x) => bigint_to_felt(&x.value)?, + DerefOrImmediate::Immediate(x) => Felt252::from(&x.value), }; match op.op { Operation::Add => Ok(a + b), diff --git a/vm/src/math_utils/mod.rs b/vm/src/math_utils/mod.rs index 8381869321..c464cb7228 100644 --- a/vm/src/math_utils/mod.rs +++ b/vm/src/math_utils/mod.rs @@ -6,7 +6,7 @@ use core::cmp::min; use crate::stdlib::{boxed::Box, ops::Shr, prelude::Vec}; use crate::types::errors::math_errors::MathError; -use crate::utils::{felt_to_biguint, CAIRO_PRIME}; +use crate::utils::CAIRO_PRIME; use crate::Felt252; use lazy_static::lazy_static; use num_bigint::{BigInt, BigUint, RandBigInt, ToBigInt}; @@ -58,7 +58,7 @@ pub fn pow2_const_nz(n: u32) -> &'static NonZeroFelt { /// ``` pub fn signed_felt(felt: Felt252) -> BigInt { - let biguint = felt_to_biguint(felt); + let biguint = felt.to_biguint(); if biguint > *SIGNED_FELT_MAX { BigInt::from_biguint(num_bigint::Sign::Minus, &*CAIRO_PRIME - &biguint) } else { diff --git a/vm/src/types/program.rs b/vm/src/types/program.rs index 9601aee445..80747d80cf 100644 --- a/vm/src/types/program.rs +++ b/vm/src/types/program.rs @@ -13,8 +13,6 @@ use crate::{ #[cfg(feature = "cairo-1-hints")] use crate::serde::deserialize_program::{ApTracking, FlowTrackingData}; -#[cfg(feature = "cairo-1-hints")] -use crate::utils::biguint_to_felt; use crate::utils::PRIME_STR; use crate::Felt252; use crate::{ @@ -406,7 +404,7 @@ impl TryFrom for Program { let data = value .bytecode .iter() - .map(|x| MaybeRelocatable::from(biguint_to_felt(&x.value).unwrap_or_default())) + .map(|x| MaybeRelocatable::from(Felt252::from(&x.value))) .collect(); //Hint data is going to be hosted processor-side, hints field will only store the pc where hints are located. // Only one pc will be stored, so the hint processor will be responsible for executing all hints for a given pc diff --git a/vm/src/utils.rs b/vm/src/utils.rs index 7627779754..eeeec33894 100644 --- a/vm/src/utils.rs +++ b/vm/src/utils.rs @@ -1,9 +1,7 @@ +use crate::stdlib::prelude::*; use crate::types::relocatable::Relocatable; -use crate::{stdlib::prelude::*, types::errors::math_errors::MathError}; -use core::ops::Neg; use lazy_static::lazy_static; -use num_bigint::{BigInt, BigUint, Sign, ToBigInt}; -use num_integer::Integer; +use num_bigint::BigUint; use num_traits::Num; pub const PRIME_STR: &str = "0x800000000000011000000000000000000000000000000000000000000000001"; @@ -51,36 +49,6 @@ pub fn from_relocatable_to_indexes(relocatable: Relocatable) -> (usize, usize) { } } -pub fn felt_to_biguint(felt: crate::Felt252) -> BigUint { - let big_digits = felt - .to_le_digits() - .into_iter() - .flat_map(|limb| [limb as u32, (limb >> 32) as u32]) - .collect(); - BigUint::new(big_digits) -} - -pub fn felt_to_bigint(felt: crate::Felt252) -> BigInt { - felt_to_biguint(felt).to_bigint().unwrap() -} - -pub fn biguint_to_felt(biguint: &BigUint) -> Result { - // TODO This funtions should return a Felt252 instead of a Result - Ok(crate::Felt252::from_bytes_le_slice(&biguint.to_bytes_le())) -} - -pub fn bigint_to_felt(bigint: &BigInt) -> Result { - let (sign, bytes) = bigint - .mod_floor(&CAIRO_PRIME.to_bigint().unwrap()) - .to_bytes_le(); - let felt = crate::Felt252::from_bytes_le_slice(&bytes); - if sign == Sign::Minus { - Ok(felt.neg()) - } else { - Ok(felt) - } -} - #[cfg(test)] #[macro_use] pub mod test_utils {