Skip to content

Commit

Permalink
Conversion improvements for Felt252 (#927)
Browse files Browse the repository at this point in the history
* Conversion improvements for Felt252

- New `fn to_be_bytes(&self) -> [u8; 32]` method
- New `fn to_u128(&self) -> Option<u128>` method
- Unroll `to_le_bytes` for more obvious reading
- Proptests for all three methods

* Simplify to_be_bytes

* Fix tests

* Limit range of proptest

* Restrict more the range
  • Loading branch information
Oppen committed Mar 29, 2023
1 parent d6cd373 commit eebfd61
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
4 changes: 4 additions & 0 deletions felt/src/bigint_felt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,10 @@ impl<'a, const PH: u128, const PL: u128> BitXor for &'a FeltBigInt<PH, PL> {
}

impl<const PH: u128, const PL: u128> ToPrimitive for FeltBigInt<PH, PL> {
fn to_u128(&self) -> Option<u128> {
self.val.to_u128()
}

fn to_u64(&self) -> Option<u64> {
self.val.to_u64()
}
Expand Down
63 changes: 59 additions & 4 deletions felt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,27 @@ impl Felt252 {
}

pub fn to_le_bytes(&self) -> [u8; 32] {
let mut res: [u8; 32] = [0; 32];
for (i, x) in self.iter_u64_digits().take(4).enumerate() {
res[8 * i..8 * (i + 1)].copy_from_slice(&x.to_le_bytes());
}
let mut res = [0u8; 32];
let mut iter = self.iter_u64_digits();
let (d0, d1, d2, d3) = (
iter.next().unwrap_or_default().to_le_bytes(),
iter.next().unwrap_or_default().to_le_bytes(),
iter.next().unwrap_or_default().to_le_bytes(),
iter.next().unwrap_or_default().to_le_bytes(),
);
res[..8].copy_from_slice(&d0);
res[8..16].copy_from_slice(&d1);
res[16..24].copy_from_slice(&d2);
res[24..].copy_from_slice(&d3);
res
}

pub fn to_be_bytes(&self) -> [u8; 32] {
let mut bytes = self.to_le_bytes();
bytes.reverse();
bytes
}

#[cfg(any(feature = "std", feature = "alloc"))]
pub fn to_signed_bytes_le(&self) -> Vec<u8> {
self.value.to_signed_bytes_le()
Expand Down Expand Up @@ -705,6 +719,10 @@ impl<'a> BitXor for &'a Felt252 {
}

impl ToPrimitive for Felt252 {
fn to_u128(&self) -> Option<u128> {
self.value.to_u128()
}

fn to_u64(&self) -> Option<u64> {
self.value.to_u64()
}
Expand Down Expand Up @@ -877,6 +895,43 @@ mod test {
prop_assert!(&x.to_biguint() < p);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn to_be_bytes(ref x in FELT_PATTERN) {
let x = &Felt252::from_bytes_be(x.as_bytes());
let bytes = x.to_be_bytes();
let y = &Felt252::from_bytes_be(&bytes);
prop_assert!(x == y);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn to_le_bytes(ref x in FELT_PATTERN) {
let x = &Felt252::from_bytes_be(x.as_bytes());
let mut bytes = x.to_le_bytes();
// Convert to big endian for test
bytes.reverse();
let y = &Felt252::from_bytes_be(&bytes);
prop_assert!(x == y);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn to_u128_ok(ref x in "(0|[1-9a-f][0-9a-f]{0,31})") {
let x = &Felt252::parse_bytes(x.as_bytes(), 16).unwrap();
let y = x.to_u128();
prop_assert!(y.is_some());
prop_assert!(x.to_string() == y.unwrap().to_string());
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn to_u128_out_of_range(ref x in "([1-9a-f][0-9a-f]{32,40})") {
let x = &Felt252::parse_bytes(x.as_bytes(), 16).unwrap();
let y = x.to_u128();
prop_assert!(y.is_none());
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
// Property-based test that ensures, for 100 felt values that are randomly generated each time tests are run, that a felt created using Felt252::from_bytes_be doesn't fall outside the range [0, p].
Expand Down

0 comments on commit eebfd61

Please sign in to comment.