Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing methods to lambdaworks implementation #1290

Merged
merged 6 commits into from
Jun 28, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@
}
}

// 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 @@
}
}

// TODO: bury BigInt?
impl From<&BigUint> for Felt252 {
fn from(value: &BigUint) -> Self {
if value >= &CAIRO_PRIME_BIGUINT {
Self::from(value.clone())

Check warning on line 168 in felt/src/lib_lambdaworks.rs

View check run for this annotation

Codecov / codecov/patch

felt/src/lib_lambdaworks.rs#L166-L168

Added lines #L166 - L168 were not covered by tests
} 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 }

Check warning on line 175 in felt/src/lib_lambdaworks.rs

View check run for this annotation

Codecov / codecov/patch

felt/src/lib_lambdaworks.rs#L170-L175

Added lines #L170 - L175 were not covered by tests
}
}

Check warning on line 177 in felt/src/lib_lambdaworks.rs

View check run for this annotation

Codecov / codecov/patch

felt/src/lib_lambdaworks.rs#L177

Added line #L177 was not covered by tests
}

// NOTE: used for deserialization
impl From<BigInt> for Felt252 {
fn from(value: BigInt) -> Self {
Expand Down Expand Up @@ -193,6 +206,11 @@
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()
}

Check warning on line 212 in felt/src/lib_lambdaworks.rs

View check run for this annotation

Codecov / codecov/patch

felt/src/lib_lambdaworks.rs#L210-L212

Added lines #L210 - L212 were not covered by tests

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