diff --git a/CHANGELOG.md b/CHANGELOG.md index f75ea196f7..b2629c92fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ #### Upcoming Changes +* fix: add `to_bytes_be` to the felt when `lambdaworks-felt` feature is active [#1290](https://github.com/lambdaclass/cairo-vm/pull/1290) + +* chore: mark `modpow` and `to_signed_bytes_le` as *deprecated* [#1290](https://github.com/lambdaclass/cairo-vm/pull/1290) + * fix: bump *lambdaworks-math* to latest version, that fixes no-std support [#1293](https://github.com/lambdaclass/cairo-vm/pull/1293) * build: remove dependecy to `thiserror` (use `thiserror-no-std/std` instead) @@ -13,8 +17,8 @@ * feat: Add feature `lambdaworks-felt` to `felt` & `cairo-vm` crates [#1281](https://github.com/lambdaclass/cairo-rs/pull/1281) Changes under this feature: - * `Felt252` now uses _lambdaworks_' `FieldElement` internally - * BREAKING: some methods of `Felt252` were removed, namely: `modpow` and `to_bytes_be` + * `Felt252` now uses *LambdaWorks*' `FieldElement` internally + * BREAKING: some methods of `Felt252` were removed, namely: `modpow` and `to_signed_bytes_le` #### [0.7.0] - 2023-6-26 diff --git a/Cargo.lock b/Cargo.lock index 0b582070a0..dd1907d201 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2981,9 +2981,9 @@ dependencies = [ [[package]] name = "windows-targets" -version = "0.48.0" +version = "0.48.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b1eb6f0cd7c80c79759c929114ef071b87354ce476d9d94271031c0497adfd5" +checksum = "05d4b17490f70499f20b9e791dcf6a299785ce8af4d709018206dc5b4953e95f" dependencies = [ "windows_aarch64_gnullvm", "windows_aarch64_msvc", diff --git a/felt/src/lib_bigint_felt.rs b/felt/src/lib_bigint_felt.rs index 18df387924..f505cb4e04 100644 --- a/felt/src/lib_bigint_felt.rs +++ b/felt/src/lib_bigint_felt.rs @@ -43,40 +43,10 @@ pub(crate) trait FeltOps { #[cfg(any(feature = "std", feature = "alloc"))] fn to_str_radix(&self, radix: u32) -> String; - /// Converts [`Felt252`] into a [`BigInt`] number in the range: `(- FIELD / 2, FIELD / 2)`. - /// - /// # Examples - /// - /// ``` - /// # use crate::cairo_felt::Felt252; - /// # use num_bigint::BigInt; - /// # use num_traits::Bounded; - /// let positive = Felt252::new(5); - /// assert_eq!(positive.to_signed_felt(), Into::::into(5)); - /// - /// let negative = Felt252::max_value(); - /// assert_eq!(negative.to_signed_felt(), 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. - /// - /// # Examples - /// - /// ``` - /// # use crate::cairo_felt::Felt252; - /// # use num_bigint::BigUint; - /// # use num_traits::{Num, Bounded}; - /// let positive = Felt252::new(5); - /// assert_eq!(positive.to_biguint(), Into::::into(5_u32)); - /// - /// let negative = Felt252::max_value(); - /// assert_eq!(negative.to_biguint(), BigUint::from_str_radix("800000000000011000000000000000000000000000000000000000000000000", 16).unwrap()); - /// ``` fn to_biguint(&self) -> BigUint; fn bits(&self) -> u64; @@ -142,11 +112,14 @@ impl Felt252 { pub fn new>(value: T) -> Self { value.into() } + + #[deprecated] pub fn modpow(&self, exponent: &Felt252, modulus: &Felt252) -> Self { Self { value: self.value.modpow(&exponent.value, &modulus.value), } } + pub fn iter_u64_digits(&self) -> U64Digits { self.value.iter_u64_digits() } @@ -184,7 +157,9 @@ impl Felt252 { } #[cfg(any(feature = "std", feature = "alloc"))] + #[deprecated] pub fn to_signed_bytes_le(&self) -> Vec { + // NOTE: this is unsigned self.value.to_signed_bytes_le() } #[cfg(any(feature = "std", feature = "alloc"))] @@ -207,16 +182,46 @@ impl Felt252 { self.value.to_str_radix(radix) } + /// Converts [`Felt252`] into a [`BigInt`] number in the range: `(- FIELD / 2, FIELD / 2)`. + /// + /// # Examples + /// + /// ``` + /// # use crate::cairo_felt::Felt252; + /// # use num_bigint::BigInt; + /// # use num_traits::Bounded; + /// let positive = Felt252::new(5); + /// assert_eq!(positive.to_signed_felt(), Into::::into(5)); + /// + /// let negative = Felt252::max_value(); + /// assert_eq!(negative.to_signed_felt(), Into::::into(-1)); + /// ``` pub fn to_signed_felt(&self) -> BigInt { #[allow(deprecated)] self.value.to_signed_felt() } + // Converts [`Felt252`]'s representation directly into a [`BigInt`]. + // Equivalent to doing felt.to_biguint().to_bigint(). pub fn to_bigint(&self) -> BigInt { #[allow(deprecated)] self.value.to_bigint() } + /// Converts [`Felt252`] into a [`BigUint`] number. + /// + /// # Examples + /// + /// ``` + /// # use crate::cairo_felt::Felt252; + /// # use num_bigint::BigUint; + /// # use num_traits::{Num, Bounded}; + /// let positive = Felt252::new(5); + /// assert_eq!(positive.to_biguint(), Into::::into(5_u32)); + /// + /// let negative = Felt252::max_value(); + /// assert_eq!(negative.to_biguint(), BigUint::from_str_radix("800000000000011000000000000000000000000000000000000000000000000", 16).unwrap()); + /// ``` pub fn to_biguint(&self) -> BigUint { #[allow(deprecated)] self.value.to_biguint() diff --git a/felt/src/lib_lambdaworks.rs b/felt/src/lib_lambdaworks.rs index 1f5ee04534..a16cc548a8 100644 --- a/felt/src/lib_lambdaworks.rs +++ b/felt/src/lib_lambdaworks.rs @@ -148,7 +148,6 @@ impl From for Felt252 { } } -// TODO: bury BigUint? impl From for Felt252 { fn from(mut value: BigUint) -> Self { if value >= *CAIRO_PRIME_BIGUINT { @@ -163,7 +162,21 @@ impl From for Felt252 { } } -// TODO: bury BigInt? +impl From<&BigUint> for Felt252 { + fn from(value: &BigUint) -> Self { + if value >= &CAIRO_PRIME_BIGUINT { + Self::from(value.clone()) + } else { + let mut limbs = [0; 4]; + for (i, l) in (0..4).rev().zip(value.iter_u64_digits()) { + limbs[i] = l; + } + let value = FieldElement::new(UnsignedInteger::from_limbs(limbs)); + Self { value } + } + } +} + // NOTE: used for deserialization impl From for Felt252 { fn from(value: BigInt) -> Self { @@ -193,6 +206,11 @@ impl Felt252 { self.value.representative().limbs.into_iter().rev() } + #[cfg(any(feature = "std", feature = "alloc"))] + pub fn to_bytes_be(&self) -> Vec { + self.to_be_bytes().to_vec() + } + pub fn to_le_bytes(&self) -> [u8; 32] { // TODO: upstream should return array let mut bytes = [0; 32];