Comparisons and overflow/underflow on `Felt`
- Langage dominant
- TypeScript
- Étoiles
- 10
- Forks
- 50
- Merge moyen
- 22 j 23 h
- PR mergées (30 j)
- 1
Description
## Felt Comparison
https://docs.miden.xyz/builder/tutorials/rust-compiler/miden-bank/constants-constraints#safe-felt-comparisons says:
> Never use `<`, `>`, `<=`, or `>=` operators directly on `Felt` values. They produce incorrect results due to field element ordering.
Is this still accurate? It looks like `miden_field::Felt` properly uses the built-in instructions for this which shouldn't produce "incorrect" results (I'm not sure what exactly we mean by that).
## Safe Arithmetic
Similarly, the miden bank tutorial has a large section on the dangers of underflows: https://docs.miden.xyz/builder/tutorials/rust-compiler/miden-bank/asset-management#step-2-add-the-withdraw-method-skeleton.
Ideally this would be easy for users to avoid, e.g. by implementing `checked*` arithmetic for the `miden_field::Felt` type , so users can easily do things like `current_balance.checked_sub(withdraw_amount).expect("insufficient balance")`.
If correct/agreed, we'd have to open an issue in `miden-crypto`.
An even safer alternative, at least for on-chain users, would be to let `impl Add for Felt` (and all such related impls) panic instead of wrap around. We'd implement `wrapping_add` and such instead for explicit use. In other words, it'd be ideal to follow the safe design of arithmetic functionality on the Rust primitive types and have `Felt::{wrapping_add, checked_add, overflowing_add, ...}`.
Maybe we could selectively do this only for the `miden_field::Felt` type in `wasm32`, which would be ideal to not have to touch the non-wasm parts, though even that might be doable.
cc @greenhat for thoughts
Guide de contribution
Ouvrir le guide de contribution
Piste de recherche
Examinez les sections de documentation liées concernant les comparaisons de Felt et l'arithmétique sécurisée. Étudiez le type `miden_field::Felt` dans le dépôt miden-crypto pour comprendre ses opérateurs de comparaison et ses implémentations arithmétiques actuelles. Déterminez si la documentation est exacte et si des modifications du type Rust (comme l'ajout de méthodes `checked_*`) sont nécessaires, ce qui nécessiterait d'ouvrir un issue dans miden-crypto.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- rust, wasm
- Domaine
- backend, documentation
- Type d'issue
- Documentation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 35/100