Skip to content

Commit

Permalink
test(felt): add tests for new functions
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLucqs committed Feb 7, 2024
1 parent 48986c0 commit fc7a087
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 10 deletions.
1 change: 1 addition & 0 deletions crates/starknet-types-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ papyrus-serialization = []

[dev-dependencies]
proptest = "1.1.0"
regex = "1.10.3"
serde_test = "1.0.1"
160 changes: 150 additions & 10 deletions crates/starknet-types-core/src/felt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,51 @@ use arbitrary::{self, Arbitrary, Unstructured};
pub struct Felt(pub(crate) FieldElement<Stark252PrimeField>);

/// A non-zero [Felt].
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord)]
pub struct NonZeroFelt(FieldElement<Stark252PrimeField>);

impl NonZeroFelt {
/// Create a [NonZeroFelt] as a constant.
/// # Safety
/// If the value is zero will panic.
pub const fn from_raw(value: [u64; 4]) -> Self {
let mut i = 0;
let mut zeros_nb = 0;
while i < value.len() {
if value[i] == 0 {
zeros_nb += 1;
}
i += 1;
}
assert!(zeros_nb < value.len(), "Felt is zero");
assert!(
value[0] != 0 || value[1] != 0 || value[2] != 0 || value[3] != 0,
"Felt is zero"
);
let value = Felt::from_raw(value);
Self(value.0)
}

/// [Felt] constant that's equal to 1.
pub const ONE: Self = Self::from_felt_unchecked(Felt(
FieldElement::<Stark252PrimeField>::from_hex_unchecked("1"),
));

/// [Felt] constant that's equal to 2.
pub const TWO: Self = Self::from_felt_unchecked(Felt(
FieldElement::<Stark252PrimeField>::from_hex_unchecked("2"),
));

/// [Felt] constant that's equal to 3.
pub const THREE: Self = Self::from_felt_unchecked(Felt(
FieldElement::<Stark252PrimeField>::from_hex_unchecked("3"),
));

/// Maximum value of [Felt]. Equals to 2^251 + 17 * 2^192.
pub const MAX: Self =
Self::from_felt_unchecked(Felt(FieldElement::<Stark252PrimeField>::const_from_raw(
UnsignedInteger::from_limbs([544, 0, 0, 32]),
)));

/// 2 ** 251
pub const ELEMENT_UPPER_BOUND: Felt = Felt::from_raw([
576459263475450960,
18446744073709255680,
160989183,
18446743986131435553,
]);

/// Create a [NonZeroFelt] without checking it. If the [Felt] is indeed [Felt::ZERO]
/// this can lead to undefined behaviour and big security issue.
/// You should always use the [TryFrom] implementation
Expand Down Expand Up @@ -1074,6 +1098,7 @@ mod test {
use super::*;
use core::ops::Shl;
use proptest::prelude::*;
use regex::Regex;
#[cfg(feature = "serde")]
use serde_test::{assert_de_tokens, assert_ser_tokens, Configure, Token};

Expand Down Expand Up @@ -1331,6 +1356,13 @@ mod test {
prop_assert!(x.mul_mod(&y, &nzp) < p);
}

#[test]
fn to_raw_reversed(mut x in any::<[u64; 4]>()) {
let felt = Felt::from_raw(x);
x.reverse();
prop_assert_eq!(felt.to_raw_reversed(), x);
}

#[test]
fn non_zero_felt_new_is_ok_when_not_zero(x in nonzero_felt()) {
prop_assert!(NonZeroFelt::try_from(x).is_ok());
Expand All @@ -1342,6 +1374,13 @@ mod test {
prop_assert_eq!([a, b, c].iter().sum::<Felt>(), a + b + c);
prop_assert_eq!([a, b, c].iter().map(Clone::clone).sum::<Felt>(), a + b + c);
}

#[test]
fn felt_to_fixed_hex_string(a in any::<Felt>()) {
prop_assert_eq!(a.to_fixed_hex_string().len(), 66);
let re = Regex::new(r"^0x([0-9a-fA-F]{64})$").unwrap();
prop_assert!(re.is_match(&a.to_fixed_hex_string()));
}
}

#[test]
Expand Down Expand Up @@ -1381,6 +1420,107 @@ mod test {
assert_eq!(Felt::MAX.to_bytes_be(), max_bytes);
}

#[test]
fn prime() {
assert_eq!(Felt::prime(), CAIRO_PRIME_BIGINT.to_biguint().unwrap());
}

#[test]
fn felt_from_raw() {
let zero_bytes = [0; 4];
assert_eq!(Felt::from_raw(zero_bytes), Felt::ZERO);
let one_raw = [
576460752303422960,
18446744073709551615,
18446744073709551615,
18446744073709551585,
];
assert_eq!(Felt::from_raw(one_raw), Felt::ONE);
let nineteen_raw = [
576460752303413168,
18446744073709551615,
18446744073709551615,
18446744073709551009,
];
assert_eq!(Felt::from_raw(nineteen_raw), Felt::from(19));
}

#[test]
fn felt_from_hex_unchecked() {
assert_eq!(Felt::from_hex_unchecked("0"), Felt::from(0));
assert_eq!(Felt::from_hex_unchecked("1"), Felt::from(1));
assert_eq!(Felt::from_hex_unchecked("0x2"), Felt::from(2));
assert_eq!(Felt::from_hex_unchecked("0x0000000003"), Felt::from(3));
assert_eq!(Felt::from_hex_unchecked("000004"), Felt::from(4));
assert_eq!(Felt::from_hex_unchecked("0x05b"), Felt::from(91));
assert_eq!(Felt::from_hex_unchecked("A"), Felt::from(10));
}
#[test]
fn nonzerofelt_from_raw() {
let one_raw = [
576460752303422960,
18446744073709551615,
18446744073709551615,
18446744073709551585,
];
assert_eq!(NonZeroFelt::from_raw(one_raw), NonZeroFelt::ONE);
let two_raw = [
576460752303422416,
18446744073709551615,
18446744073709551615,
18446744073709551553,
];
assert_eq!(NonZeroFelt::from_raw(two_raw), NonZeroFelt::TWO);
let nineteen_raw = [
576460752303413168,
18446744073709551615,
18446744073709551615,
18446744073709551009,
];
assert_eq!(
NonZeroFelt::from_raw(nineteen_raw),
NonZeroFelt::try_from(Felt::from(19)).unwrap()
);
}

#[test]
fn nonzerofelt_from_felt_unchecked() {
assert_eq!(
NonZeroFelt::from_felt_unchecked(Felt::from_hex_unchecked("9028392")),
NonZeroFelt::try_from(Felt::from(0x9028392)).unwrap()
);
assert_eq!(
NonZeroFelt::from_felt_unchecked(Felt::from_hex_unchecked("1")),
NonZeroFelt::try_from(Felt::from(1)).unwrap()
);
assert_eq!(
NonZeroFelt::from_felt_unchecked(Felt::from_hex_unchecked("0x2")),
NonZeroFelt::try_from(Felt::from(2)).unwrap()
);
assert_eq!(
NonZeroFelt::from_felt_unchecked(Felt::from_hex_unchecked("0x0000000003")),
NonZeroFelt::try_from(Felt::from(3)).unwrap()
);
assert_eq!(
NonZeroFelt::from_felt_unchecked(Felt::from_hex_unchecked("000004")),
NonZeroFelt::try_from(Felt::from(4)).unwrap()
);
assert_eq!(
NonZeroFelt::from_felt_unchecked(Felt::from_hex_unchecked("0x05b")),
NonZeroFelt::try_from(Felt::from(91)).unwrap()
);
assert_eq!(
NonZeroFelt::from_felt_unchecked(Felt::from_hex_unchecked("A")),
NonZeroFelt::try_from(Felt::from(10)).unwrap()
);
}

#[test]
#[should_panic(expected = "Felt is zero")]
fn nonzerofelt_is_zero_from_raw() {
NonZeroFelt::from_raw([0; 4]);
}

#[test]
fn non_zero_felt_from_zero_should_fail() {
assert!(NonZeroFelt::try_from(Felt::ZERO).is_err());
Expand Down

0 comments on commit fc7a087

Please sign in to comment.