Skip to content

Challenge 12: Verify safety of NonZero#565

Open
Samuelsills wants to merge 1 commit intomodel-checking:mainfrom
Samuelsills:challenge-12-nonzero
Open

Challenge 12: Verify safety of NonZero#565
Samuelsills wants to merge 1 commit intomodel-checking:mainfrom
Samuelsills:challenge-12-nonzero

Conversation

@Samuelsills
Copy link
Copy Markdown

Summary

Verify all 38 NonZero functions listed in Challenge 12. 432 Kani proof harnesses across all 12 integer types, 0 failures.

Part 1: Harnesses for new (iff + value equality assertions) and from_mut (iff + dereference). Pre-existing contracts and harnesses for new_unchecked and from_mut_unchecked.

Part 2: Harnesses for all 34 listed functions including bitor (3 impls), count_ones, rotate_left/right, swap_bytes, reverse_bits, endianness conversions, checked/saturating mul/pow/add, checked_next_power_of_two, midpoint, isqrt, abs variants (6), and neg variants (4).

Strengthened loop invariant on checked_pow in uint_macros.rs and int_macros.rs from true to a property that preserves the nonzero invariant through loop iterations. This is a verification-only annotation (no-op at runtime).

abs and neg harnesses exclude T::MIN (documented overflow behavior). The MIN case is separately verified by wrapping_abs, overflowing_abs, wrapping_neg, and overflowing_neg which pass for all inputs.

Resolves #71

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

@Samuelsills Samuelsills force-pushed the challenge-12-nonzero branch 3 times, most recently from 938fdd1 to 32bbacc Compare March 24, 2026 17:10
Verify all 38 NonZero functions listed in the challenge specification.
432 Kani proof harnesses across all 12 integer types, 0 failures.

Resolves model-checking#71

By submitting this pull request, I confirm that my contribution is made under
the terms of the Apache 2.0 and MIT licenses.
@Samuelsills Samuelsills force-pushed the challenge-12-nonzero branch from 32bbacc to 71e54d0 Compare March 24, 2026 17:26
@Samuelsills Samuelsills marked this pull request as ready for review March 24, 2026 20:50
@Samuelsills Samuelsills requested a review from a team as a code owner March 24, 2026 20:50
@feliperodri feliperodri added the Challenge Used to tag a challenge label Mar 25, 2026
@Samuelsills
Copy link
Copy Markdown
Author

Verification Coverage Report

Part 1: new and new_unchecked (2/2 ✅)

  • Contracts verify preconditions from SAFETY comments
  • NonZero created iff input is nonzero
  • Inner value equals input

Part 2: Other Uses of Unsafe (36/36 ✅)

# Function Verified # Function Verified
1 max 19 saturating_add
2 min 20 unchecked_add
3 clamp 21 checked_next_power_of_two
4 bitor (3 impls) 22 midpoint
5 count_ones 23 isqrt
6 rotate_left 24 abs
7 rotate_right 25 checked_abs
8 swap_bytes 26 overflowing_abs
9 reverse_bits 27 saturating_abs
10 from_be 28 wrapping_abs
11 from_le 29 unsigned_abs
12 to_be 30 checked_neg
13 to_le 31 overflowing_neg
14 checked_mul 32 wrapping_neg
15 saturating_mul 33 from_mut
16 unchecked_mul 34 from_mut_unchecked
17 checked_pow
18 saturating_pow
neg checked_add

Total: 38/38 functions verified (2 Part 1 + 36 Part 2)

UBs Checked

  • ✅ Invoking UB via compiler intrinsics
  • ✅ Reading from uninitialized memory
  • ✅ Producing an invalid value

Verification Approach

  • Tool: Kani Rust Verifier
  • 432 proof harnesses across all 12 NonZero integer types
  • Verified for all NonZero types (i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Challenge Used to tag a challenge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Challenge 12: Safety of NonZero

2 participants