Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .github/workflows/kani.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ jobs:
# - unicode::unicode_data::conversions::to_* are not listed for now: since the
# Unicode tables were regenerated (nightly-2026-09-25) they exceed the 10m
# timeout on macOS and the ubuntu runner died while verifying them.
# - isize::carrying_mul is not listed: the CarryingMulAdd fallback in
# intrinsics/fallback.rs widens isize to the unsigned UDoubleSize, so the
# multiplication and additions overflow for negative operands
# (https://github.com/rust-lang/rust/issues/166858).
- name: Run Kani Verification
if: github.event_name == 'merge_group' || github.event_name == 'workflow_dispatch' || inputs.autoharness
run: |
Expand All @@ -139,6 +143,17 @@ jobs:
--include-pattern char::convert::from_u32_unchecked \
--include-pattern core_arch::x86::__m128d::as_f64x2 \
--include-pattern "convert::num::<impl.convert::From<num::nonzero::NonZero<" \
--include-pattern "num::<impl.i8>::carrying_mul$" \
--include-pattern "num::<impl.i16>::carrying_mul$" \
--include-pattern "num::<impl.i32>::carrying_mul$" \
--include-pattern "num::<impl.i64>::carrying_mul$" \
--include-pattern "num::<impl.i128>::carrying_mul$" \
--include-pattern "num::<impl.u8>::carrying_mul$" \
--include-pattern "num::<impl.u16>::carrying_mul$" \
--include-pattern "num::<impl.u32>::carrying_mul$" \
--include-pattern "num::<impl.u64>::carrying_mul$" \
--include-pattern "num::<impl.u128>::carrying_mul$" \
--include-pattern "num::<impl.usize>::carrying_mul$" \
--include-pattern "num::<impl.i8>::unchecked_add" \
--include-pattern "num::<impl.i16>::unchecked_add" \
--include-pattern "num::<impl.i32>::unchecked_add" \
Expand Down
84 changes: 0 additions & 84 deletions library/core/src/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1941,50 +1941,6 @@ mod verify {
}
}

/// A macro to generate Kani proof harnesses for the `carrying_mul` method,
///
/// The macro creates multiple harnesses for different ranges of input values,
/// allowing testing of both small and large inputs.
///
/// # Parameters:
/// - `$type`: The integer type (e.g., u8, u16) for which the `carrying_mul` function is being tested.
/// - `$wide_type`: A wider type to simulate the multiplication (e.g., u16 for u8, u32 for u16).
/// - `$harness_name`: The name of the Kani harness to be generated.
/// - `$min`: The minimum value for the range of inputs for `lhs`, `rhs`, and `carry_in`.
/// - `$max`: The maximum value for the range of inputs for `lhs`, `rhs`, and `carry_in`.
macro_rules! generate_carrying_mul_intervals {
($type:ty, $wide_type:ty, $($harness_name:ident, $min:expr, $max:expr),+) => {
$(
#[kani::proof]
#[kani::solver(kissat)]
pub fn $harness_name() {
let lhs: $type = kani::any::<$type>();
let rhs: $type = kani::any::<$type>();
let carry_in: $type = kani::any::<$type>();

kani::assume(lhs >= $min && lhs <= $max);
kani::assume(rhs >= $min && rhs <= $max);
kani::assume(carry_in >= $min && carry_in <= $max);

// Perform the carrying multiplication
let (result, carry_out) = lhs.carrying_mul(rhs, carry_in);

// Manually compute the expected result and carry using wider type
let wide_result = (lhs as $wide_type)
.wrapping_mul(rhs as $wide_type)
.wrapping_add(carry_in as $wide_type);

let expected_result = wide_result as $type;
let expected_carry = (wide_result >> <$type>::BITS) as $type;

// Assert the result and carry are correct
assert_eq!(result, expected_result);
assert_eq!(carry_out, expected_carry);
}
)+
}
}

// Part 2 : Nested unsafe functions Generation Macros --> https://github.com/verify-rust-std/blob/main/doc/src/challenges/0011-floats-ints.md

// Verify `widening_mul`, which internally uses `unchecked_mul`
Expand Down Expand Up @@ -2195,46 +2151,6 @@ mod verify {
usize::MAX
);

// Part_2 `carrying_mul` proofs
//
// ====================== u8 Harnesses ======================
/// Kani proof harness for `carrying_mul` on `u8` type with full range of values.
generate_carrying_mul_intervals!(u8, u16, carrying_mul_u8_full_range, 0u8, u8::MAX);

// ====================== u16 Harnesses ======================
/// Kani proof harness for `carrying_mul` on `u16` type with full range of values.
generate_carrying_mul_intervals!(u16, u32, carrying_mul_u16_full_range, 0u16, u16::MAX);

// ====================== u32 Harnesses ======================
generate_carrying_mul_intervals!(
u32,
u64,
carrying_mul_u32_small,
0u32,
10u32,
carrying_mul_u32_large,
u32::MAX - 10u32,
u32::MAX,
carrying_mul_u32_mid_edge,
(u32::MAX / 2) - 10u32,
(u32::MAX / 2) + 10u32
);

// ====================== u64 Harnesses ======================
generate_carrying_mul_intervals!(
u64,
u128,
carrying_mul_u64_small,
0u64,
10u64,
carrying_mul_u64_large,
u64::MAX - 10u64,
u64::MAX,
carrying_mul_u64_mid_edge,
(u64::MAX / 2) - 10u64,
(u64::MAX / 2) + 10u64
);

// Part_2 `widening_mul` proofs

// ====================== u8 Harnesses ======================
Expand Down
Loading