From 7f1111bc0e80ea1d7e70dc29c15df69384b5bd9a Mon Sep 17 00:00:00 2001 From: Bardi Harborow Date: Sat, 5 Dec 2026 05:29:12 +1100 Subject: [PATCH] Replace the `carrying_mul` harnesses with autoharness Remove the 8 hand-written `carrying_mul` harnesses and verify `carrying_mul` with autoharness instead, over all inputs, for 11 of the 12 integer types. The `$` anchor keeps the patterns from also matching `carrying_mul_add`. For `u32` and `u64`, the removed harnesses only checked operands in three 21-value intervals, and none of them covered the signed types, `u128` or `usize`. `isize` is not listed due to an upstream issue. --- .github/workflows/kani.yml | 15 +++++++ library/core/src/num/mod.rs | 84 ------------------------------------- 2 files changed, 15 insertions(+), 84 deletions(-) diff --git a/.github/workflows/kani.yml b/.github/workflows/kani.yml index d93b0ecc26b16..94e529edfeeac 100644 --- a/.github/workflows/kani.yml +++ b/.github/workflows/kani.yml @@ -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: | @@ -139,6 +143,17 @@ jobs: --include-pattern char::convert::from_u32_unchecked \ --include-pattern core_arch::x86::__m128d::as_f64x2 \ --include-pattern "convert::num::::carrying_mul$" \ + --include-pattern "num::::carrying_mul$" \ + --include-pattern "num::::carrying_mul$" \ + --include-pattern "num::::carrying_mul$" \ + --include-pattern "num::::carrying_mul$" \ + --include-pattern "num::::carrying_mul$" \ + --include-pattern "num::::carrying_mul$" \ + --include-pattern "num::::carrying_mul$" \ + --include-pattern "num::::carrying_mul$" \ + --include-pattern "num::::carrying_mul$" \ + --include-pattern "num::::carrying_mul$" \ --include-pattern "num::::unchecked_add" \ --include-pattern "num::::unchecked_add" \ --include-pattern "num::::unchecked_add" \ diff --git a/library/core/src/num/mod.rs b/library/core/src/num/mod.rs index 0d32e34e8dfb5..943e2acb2ea60 100644 --- a/library/core/src/num/mod.rs +++ b/library/core/src/num/mod.rs @@ -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` @@ -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 ======================