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

Implement Felt from boolean #24

Merged
merged 15 commits into from
Dec 20, 2023
17 changes: 17 additions & 0 deletions crates/starknet-types-core/src/felt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,15 @@ impl From<i128> for Felt {
}
}

impl From<bool> for Felt {
fn from(value: bool) -> Felt {
match value {
true => Felt::ONE,
false => Felt::ZERO,
}
}
}

impl From<&BigInt> for Felt {
fn from(bigint: &BigInt) -> Felt {
let (sign, bytes) = bigint.mod_floor(&CAIRO_PRIME_BIGINT).to_bytes_le();
Expand Down Expand Up @@ -1715,4 +1724,12 @@ mod test {
);
}
}

#[test]
fn bool_into_felt() {
let zero: Felt = false.into();
let one: Felt = true.into();
assert_eq!(one, Felt::ONE);
assert_eq!(zero, Felt::ZERO);
}
}