Felt Construction From Untrusted Numeric Inputs
Rule
Felt::new(x) is checked and returns Result, rejecting values greater than or equal to Felt::ORDER. Use it when a u64 input may exceed the field modulus.
Use one of:
Felt::from_u8(x),Felt::from_u16(x)orFelt::from_u32(x)foru32-or-smaller inputs. They are infallible andconst, so prefer them to define constants.Felt::from(x)foru32-or-smaller inputs in non-constcode (infallible).Felt::new(x)orFelt::try_from(x)foru64inputs; both returnResultand check the bound.Felt::new_unchecked(x)only whenx < Felt::ORDERhas already been proved.
Why
The field modulus sits just below 2^64, so out-of-range inputs occupy a narrow band that tests can miss. Checked construction makes those inputs explicit errors; new_unchecked skips that protection.
Examples
// Good: const constructor for a small constant
const NUM_SLOTS: Felt = Felt::from_u8(4);
// Good: u32 input, infallible conversion
let f = Felt::from(slot_index as u32);
// Good: untrusted u64 input, checked conversion returning Result
let f = Felt::new(user_value).map_err(|_| Error::FeltOverflow)?;
// Good: unchecked construction only after proving the canonical bound
assert!(bounded_value < Felt::ORDER);
let f = Felt::new_unchecked(bounded_value);
// Bad: unchecked construction on an untrusted value
let f = Felt::new_unchecked(user_value);
// Bad: new_unchecked for a constant that a const from_u* constructor covers
const NUM_SLOTS: Felt = Felt::new_unchecked(4);