Skip to content

Commit bf98e91

Browse files
MegaRedHandkariy
authored andcommitted
Add missing methods to lambdaworks implementation (lambdaclass#1290)
* 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
1 parent 0682872 commit bf98e91

File tree

4 files changed

+63
-36
lines changed

4 files changed

+63
-36
lines changed

CHANGELOG.md

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

33
#### Upcoming Changes
44

5+
* fix: add `to_bytes_be` to the felt when `lambdaworks-felt` feature is active [#1290](https://github.com/lambdaclass/cairo-vm/pull/1290)
6+
7+
* chore: mark `modpow` and `to_signed_bytes_le` as *deprecated* [#1290](https://github.com/lambdaclass/cairo-vm/pull/1290)
8+
59
* fix: bump *lambdaworks-math* to latest version, that fixes no-std support [#1293](https://github.com/lambdaclass/cairo-vm/pull/1293)
610

711
* build: remove dependecy to `thiserror` (use `thiserror-no-std/std` instead)
@@ -13,8 +17,8 @@
1317
* feat: Add feature `lambdaworks-felt` to `felt` & `cairo-vm` crates [#1281](https://github.com/lambdaclass/cairo-rs/pull/1281)
1418

1519
Changes under this feature:
16-
* `Felt252` now uses _lambdaworks_' `FieldElement` internally
17-
* BREAKING: some methods of `Felt252` were removed, namely: `modpow` and `to_bytes_be`
20+
* `Felt252` now uses *LambdaWorks*' `FieldElement` internally
21+
* BREAKING: some methods of `Felt252` were removed, namely: `modpow` and `to_signed_bytes_le`
1822

1923
#### [0.7.0] - 2023-6-26
2024

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

felt/src/lib_bigint_felt.rs

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -43,40 +43,10 @@ pub(crate) trait FeltOps {
4343
#[cfg(any(feature = "std", feature = "alloc"))]
4444
fn to_str_radix(&self, radix: u32) -> String;
4545

46-
/// Converts [`Felt252`] into a [`BigInt`] number in the range: `(- FIELD / 2, FIELD / 2)`.
47-
///
48-
/// # Examples
49-
///
50-
/// ```
51-
/// # use crate::cairo_felt::Felt252;
52-
/// # use num_bigint::BigInt;
53-
/// # use num_traits::Bounded;
54-
/// let positive = Felt252::new(5);
55-
/// assert_eq!(positive.to_signed_felt(), Into::<num_bigint::BigInt>::into(5));
56-
///
57-
/// let negative = Felt252::max_value();
58-
/// assert_eq!(negative.to_signed_felt(), Into::<num_bigint::BigInt>::into(-1));
59-
/// ```
6046
fn to_signed_felt(&self) -> BigInt;
6147

62-
// Converts [`Felt252`]'s representation directly into a [`BigInt`].
63-
// Equivalent to doing felt.to_biguint().to_bigint().
6448
fn to_bigint(&self) -> BigInt;
6549

66-
/// Converts [`Felt252`] into a [`BigUint`] number.
67-
///
68-
/// # Examples
69-
///
70-
/// ```
71-
/// # use crate::cairo_felt::Felt252;
72-
/// # use num_bigint::BigUint;
73-
/// # use num_traits::{Num, Bounded};
74-
/// let positive = Felt252::new(5);
75-
/// assert_eq!(positive.to_biguint(), Into::<num_bigint::BigUint>::into(5_u32));
76-
///
77-
/// let negative = Felt252::max_value();
78-
/// assert_eq!(negative.to_biguint(), BigUint::from_str_radix("800000000000011000000000000000000000000000000000000000000000000", 16).unwrap());
79-
/// ```
8050
fn to_biguint(&self) -> BigUint;
8151

8252
fn bits(&self) -> u64;
@@ -142,11 +112,14 @@ impl Felt252 {
142112
pub fn new<T: Into<Felt252>>(value: T) -> Self {
143113
value.into()
144114
}
115+
116+
#[deprecated]
145117
pub fn modpow(&self, exponent: &Felt252, modulus: &Felt252) -> Self {
146118
Self {
147119
value: self.value.modpow(&exponent.value, &modulus.value),
148120
}
149121
}
122+
150123
pub fn iter_u64_digits(&self) -> U64Digits {
151124
self.value.iter_u64_digits()
152125
}
@@ -184,7 +157,9 @@ impl Felt252 {
184157
}
185158

186159
#[cfg(any(feature = "std", feature = "alloc"))]
160+
#[deprecated]
187161
pub fn to_signed_bytes_le(&self) -> Vec<u8> {
162+
// NOTE: this is unsigned
188163
self.value.to_signed_bytes_le()
189164
}
190165
#[cfg(any(feature = "std", feature = "alloc"))]
@@ -207,16 +182,46 @@ impl Felt252 {
207182
self.value.to_str_radix(radix)
208183
}
209184

185+
/// Converts [`Felt252`] into a [`BigInt`] number in the range: `(- FIELD / 2, FIELD / 2)`.
186+
///
187+
/// # Examples
188+
///
189+
/// ```
190+
/// # use crate::cairo_felt::Felt252;
191+
/// # use num_bigint::BigInt;
192+
/// # use num_traits::Bounded;
193+
/// let positive = Felt252::new(5);
194+
/// assert_eq!(positive.to_signed_felt(), Into::<num_bigint::BigInt>::into(5));
195+
///
196+
/// let negative = Felt252::max_value();
197+
/// assert_eq!(negative.to_signed_felt(), Into::<num_bigint::BigInt>::into(-1));
198+
/// ```
210199
pub fn to_signed_felt(&self) -> BigInt {
211200
#[allow(deprecated)]
212201
self.value.to_signed_felt()
213202
}
214203

204+
// Converts [`Felt252`]'s representation directly into a [`BigInt`].
205+
// Equivalent to doing felt.to_biguint().to_bigint().
215206
pub fn to_bigint(&self) -> BigInt {
216207
#[allow(deprecated)]
217208
self.value.to_bigint()
218209
}
219210

211+
/// Converts [`Felt252`] into a [`BigUint`] number.
212+
///
213+
/// # Examples
214+
///
215+
/// ```
216+
/// # use crate::cairo_felt::Felt252;
217+
/// # use num_bigint::BigUint;
218+
/// # use num_traits::{Num, Bounded};
219+
/// let positive = Felt252::new(5);
220+
/// assert_eq!(positive.to_biguint(), Into::<num_bigint::BigUint>::into(5_u32));
221+
///
222+
/// let negative = Felt252::max_value();
223+
/// assert_eq!(negative.to_biguint(), BigUint::from_str_radix("800000000000011000000000000000000000000000000000000000000000000", 16).unwrap());
224+
/// ```
220225
pub fn to_biguint(&self) -> BigUint {
221226
#[allow(deprecated)]
222227
self.value.to_biguint()

felt/src/lib_lambdaworks.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ impl From<bool> for Felt252 {
148148
}
149149
}
150150

151-
// TODO: bury BigUint?
152151
impl From<BigUint> for Felt252 {
153152
fn from(mut value: BigUint) -> Self {
154153
if value >= *CAIRO_PRIME_BIGUINT {
@@ -163,7 +162,21 @@ impl From<BigUint> for Felt252 {
163162
}
164163
}
165164

166-
// TODO: bury BigInt?
165+
impl From<&BigUint> for Felt252 {
166+
fn from(value: &BigUint) -> Self {
167+
if value >= &CAIRO_PRIME_BIGUINT {
168+
Self::from(value.clone())
169+
} else {
170+
let mut limbs = [0; 4];
171+
for (i, l) in (0..4).rev().zip(value.iter_u64_digits()) {
172+
limbs[i] = l;
173+
}
174+
let value = FieldElement::new(UnsignedInteger::from_limbs(limbs));
175+
Self { value }
176+
}
177+
}
178+
}
179+
167180
// NOTE: used for deserialization
168181
impl From<BigInt> for Felt252 {
169182
fn from(value: BigInt) -> Self {
@@ -193,6 +206,11 @@ impl Felt252 {
193206
self.value.representative().limbs.into_iter().rev()
194207
}
195208

209+
#[cfg(any(feature = "std", feature = "alloc"))]
210+
pub fn to_bytes_be(&self) -> Vec<u8> {
211+
self.to_be_bytes().to_vec()
212+
}
213+
196214
pub fn to_le_bytes(&self) -> [u8; 32] {
197215
// TODO: upstream should return array
198216
let mut bytes = [0; 32];

0 commit comments

Comments
 (0)