Felt construction

From 0xMiden/protocol · View interactive version

Felt construction is an AI agent skill from the GitHub repository 0xMiden/protocol. Use when constructing a `Felt` from a numeric value in Rust — use checked construction unless the canonical bound is already proved.

How to use this skill

Skill file

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:

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);
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.