Skip to content

Commit

Permalink
Add missing methods to lambdaworks implementation (lambdaclass#1290)
Browse files Browse the repository at this point in the history
* Add missing methods to lambdaworks impl

* Remove `modpow` and `to_signed_bytes_le` from lw-felt

* Add `From<&Big(U)Int>` to lw-felt
* Move comments out of `FeltOps` and remove trait

* Fix build

* Update changelog

* Uncomment deprecated tags
  • Loading branch information
MegaRedHand authored and kariy committed Jul 25, 2023
1 parent 0682872 commit bf98e91
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 36 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Expand Up @@ -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)
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 35 additions & 30 deletions felt/src/lib_bigint_felt.rs
Expand Up @@ -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::<num_bigint::BigInt>::into(5));
///
/// let negative = Felt252::max_value();
/// assert_eq!(negative.to_signed_felt(), 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.
///
/// # 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::<num_bigint::BigUint>::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;
Expand Down Expand Up @@ -142,11 +112,14 @@ impl Felt252 {
pub fn new<T: Into<Felt252>>(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()
}
Expand Down Expand Up @@ -184,7 +157,9 @@ impl Felt252 {
}

#[cfg(any(feature = "std", feature = "alloc"))]
#[deprecated]
pub fn to_signed_bytes_le(&self) -> Vec<u8> {
// NOTE: this is unsigned
self.value.to_signed_bytes_le()
}
#[cfg(any(feature = "std", feature = "alloc"))]
Expand All @@ -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::<num_bigint::BigInt>::into(5));
///
/// let negative = Felt252::max_value();
/// assert_eq!(negative.to_signed_felt(), Into::<num_bigint::BigInt>::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::<num_bigint::BigUint>::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()
Expand Down
22 changes: 20 additions & 2 deletions felt/src/lib_lambdaworks.rs
Expand Up @@ -148,7 +148,6 @@ impl From<bool> for Felt252 {
}
}

// TODO: bury BigUint?
impl From<BigUint> for Felt252 {
fn from(mut value: BigUint) -> Self {
if value >= *CAIRO_PRIME_BIGUINT {
Expand All @@ -163,7 +162,21 @@ impl From<BigUint> 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<BigInt> for Felt252 {
fn from(value: BigInt) -> Self {
Expand Down Expand Up @@ -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<u8> {
self.to_be_bytes().to_vec()
}

pub fn to_le_bytes(&self) -> [u8; 32] {
// TODO: upstream should return array
let mut bytes = [0; 32];
Expand Down

0 comments on commit bf98e91

Please sign in to comment.