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

feat: add from_bytes_(l|n)e methods #1326

Merged
merged 8 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#### Upcoming Changes

* feat: add `from_bytes_le` and `from_bytes_ne` methods [#1326](https://github.com/lambdaclass/cairo-vm/pull/1326)

#### [0.8.2] - 2023-7-10

* chore: update dependencies, particularly lamdaworks 0.1.2 -> 0.1.3 [#1323](https://github.com/lambdaclass/cairo-vm/pull/1323)
Expand Down
10 changes: 5 additions & 5 deletions felt/src/bigint_felt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,11 @@ impl FeltOps for FeltBigInt<FIELD_HIGH, FIELD_LOW> {
}

fn from_bytes_be(bytes: &[u8]) -> FeltBigInt<FIELD_HIGH, FIELD_LOW> {
let mut value = BigUint::from_bytes_be(bytes);
if value >= *CAIRO_PRIME_BIGUINT {
value = value.mod_floor(&CAIRO_PRIME_BIGUINT);
}
Self::from(value)
Self::from(BigUint::from_bytes_be(bytes))
}

fn from_bytes_le(bytes: &[u8]) -> FeltBigInt<FIELD_HIGH, FIELD_LOW> {
Self::from(BigUint::from_bytes_le(bytes))
}

#[cfg(any(feature = "std", feature = "alloc"))]
Expand Down
15 changes: 15 additions & 0 deletions felt/src/lib_bigint_felt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub(crate) trait FeltOps {

fn from_bytes_be(bytes: &[u8]) -> Self;

fn from_bytes_le(bytes: &[u8]) -> Self;

#[cfg(any(feature = "std", feature = "alloc"))]
fn to_str_radix(&self, radix: u32) -> String;

Expand Down Expand Up @@ -177,6 +179,19 @@ impl Felt252 {
value: FeltBigInt::from_bytes_be(bytes),
}
}
pub fn from_bytes_le(bytes: &[u8]) -> Self {
Self {
value: FeltBigInt::from_bytes_le(bytes),
}
}
pub fn from_bytes_ne(bytes: &[u8]) -> Self {
// Call either version depending on target endianness
#[cfg(target_endian = "little")]
let res = Self::from_bytes_le(bytes);
#[cfg(target_endian = "big")]
let res = Self::from_bytes_be(bytes);
res
}
#[cfg(any(feature = "std", feature = "alloc"))]
pub fn to_str_radix(&self, radix: u32) -> String {
self.value.to_str_radix(radix)
Expand Down
15 changes: 15 additions & 0 deletions felt/src/lib_lambdaworks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,24 @@ impl Felt252 {
}

pub fn from_bytes_be(bytes: &[u8]) -> Self {
// TODO: use upstream's version when it's more lenient
Self::from(BigUint::from_bytes_be(bytes))
}

pub fn from_bytes_le(bytes: &[u8]) -> Self {
// TODO: use upstream's version when it's more lenient
Self::from(BigUint::from_bytes_le(bytes))
}

pub fn from_bytes_ne(bytes: &[u8]) -> Self {
// Call either version depending on target endianness
#[cfg(target_endian = "little")]
let res = Self::from_bytes_le(bytes);
#[cfg(target_endian = "big")]
let res = Self::from_bytes_be(bytes);
res
}

#[cfg(any(feature = "std", feature = "alloc"))]
pub fn to_str_radix(&self, radix: u32) -> String {
if radix == 16 {
Expand Down
4 changes: 2 additions & 2 deletions fuzzer/Cargo.lock

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