Repository navigation
New challenge: Safety and correctness of integer logarithms #729
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
Open
Kieran-Hulsman
wants to merge
3
commits into
model-checking:main
Choose a base branch
from
Kieran-Hulsman:kieran/ilog-challenge-proposal
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+69
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # Challenge 30: Safety and Correctness of Integer Logarithms | ||
|
|
||
| - **Status:** *Open* | ||
| - **Solution:** *Option field to point to the PR that solved this challenge.* | ||
| - **Tracking Issue:** [#728](https://github.com/model-checking/verify-rust-std/issues/728) | ||
| - **Start date:** *2026/11/02* | ||
| - **Reward:** *TBD* | ||
|
|
||
| ------------------- | ||
|
|
||
| ## Goal | ||
|
|
||
| Verify the safety and correctness of `ilog10`, `ilog2`, and their checked variants in `core::num`, | ||
| including `NonZero<T>::ilog2` and `NonZero<T>::ilog10` for unsigned types and the internal | ||
| `int_log10` helpers they rely on. | ||
|
|
||
| ## Motivation | ||
|
|
||
| Decimal logarithms make unsafe promises about arithmetic result bounds; binary logarithms | ||
| call intrinsics that require nonzero inputs; the `usize` decimal-logarithm wrapper creates a | ||
| `NonZero` value without checking it. Violating any of these requirements is undefined | ||
| behavior, even though the public APIs are safe. | ||
|
|
||
| ## Description | ||
|
|
||
| | Function | Integer Types | Location | | ||
| |---------|---------------|----------| | ||
| | `ilog10`, `checked_ilog10`, `ilog2`, `checked_ilog2` | All signed and unsigned integer types | [int_macros.rs](https://github.com/model-checking/verify-rust-std/blob/main/library/core/src/num/int_macros.rs), [uint_macros.rs](https://github.com/model-checking/verify-rust-std/blob/main/library/core/src/num/uint_macros.rs) | | ||
| | `NonZero<T>::ilog2`, `NonZero<T>::ilog10` | All unsigned integer types | [nonzero.rs](https://github.com/model-checking/verify-rust-std/blob/main/library/core/src/num/nonzero.rs) | | ||
| | `u8_impl`, `u16_impl`, `u32_impl`, `u64_impl`, `u128_impl`, `less_than_5` | Each helper's documented input domain | [int_log10.rs](https://github.com/model-checking/verify-rust-std/blob/main/library/core/src/num/imp/int_log10.rs) | | ||
| | Internal wrappers `u8`, `u16`, `u32`, `u64`, `u128`, `usize`, `i8`, `i16`, `i32`, `i64`, `i128` | Corresponding integer types | [int_log10.rs](https://github.com/model-checking/verify-rust-std/blob/main/library/core/src/num/imp/int_log10.rs) | | ||
|
feliperodri marked this conversation as resolved.
Resolved
|
||
|
|
||
| - **Signed:** `i8`, `i16`, `i32`, `i64`, `i128`, `isize`. | ||
| - **Unsigned:** `u8`, `u16`, `u32`, `u64`, `u128`, `usize`. | ||
|
|
||
| Arbitrary-base `ilog` is excluded: its general calculation contains no direct unsafe | ||
| operations, and its unsafe dependencies overlap with the implementations above. | ||
|
|
||
| This extends [Challenge 11](0011-floats-ints.md) to logarithm methods, which are not | ||
| in that challenge's target list. NonZero constructor verification belongs to | ||
| [Challenge 12](0012-nonzero.md) and is not repeated here. | ||
|
|
||
| ### Assumptions | ||
|
|
||
| - Existing verified NonZero contracts and documented layout guarantees may be reused. | ||
| - The repository's CI runs only 64-bit targets, so proofs need only run on the 64-bit | ||
| configuration of pointer-width-dependent code: the `usize` wrapper and the `isize` methods. | ||
| For the 16- and 32-bit configurations, it suffices to verify the `u16`/`u32` wrappers that | ||
| `usize` selects and the `i16`/`i32` wrappers that `isize` uses, and to argue that the | ||
| `NonZero::new_unchecked` conversion in the `usize` wrapper is safe for each pointer width. | ||
|
|
||
| ### Success Criteria | ||
|
|
||
| Cover every input to the public methods and each internal helper's documented input domain: | ||
|
|
||
| - Verify the safety conditions of all unsafe operations, including result-bound assertions, nonzero-input requirements, and the `NonZero::new_unchecked` conversion in the `usize` wrapper. | ||
| - For positive input `n`, prove the result is the greatest nonnegative integer `r` with `b^r <= n`, where `b` is 2 or 10 and powers are interpreted mathematically. | ||
|
feliperodri marked this conversation as resolved.
Resolved
|
||
| - Prove checked methods return `None` for zero or negative inputs, and `ilog2` and `ilog10` panic on those inputs. | ||
| - Prove the calculations do not overflow or underflow. | ||
|
|
||
| All proofs must automatically ensure the absence of the following | ||
| [undefined behaviors](https://github.com/rust-lang/reference/blob/142b2ed77d33f37a9973772bd95e6144ed9dce43/src/behavior-considered-undefined.md): | ||
|
|
||
| - Invoking undefined behavior through compiler intrinsics. | ||
| - Producing an invalid value. | ||
|
feliperodri marked this conversation as resolved.
Resolved
|
||
|
|
||
| Note: All solutions to verification challenges need to satisfy the criteria established in the | ||
| [challenge book](../general-rules.md) in addition to the ones listed above. | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.