fix: do not evaluate bit-vector literals of width 2^32 or more This PR fixes process aborts with `INTERNAL PANIC: Nat.pow exponent is too big` in `simp`, `grind`, `cbv`, `bv_decide` and `bv_normalize`. They happened on goals containing a `BitVec` literal of width `2^32` or more, such as `example (x : BitVec 5000000000) : 1#5000000000 = x := by simp`, and on powers with an exponent of `2^32` or more when `exponentiation.threshold` was raised above `2^32`. These terms are now left unevaluated. The examples from #15786 now pass. The runtime implementation of `Nat.pow` aborts the process when the base is greater than `1` and the exponent does not fit in 32 bits, and most bit-vector operations compute `2^w`. The new function `isSafeBitVecWidth` is checked by the bit-vector literal decoders (`getBitVecValue?`, `Sym.getBitVecValue?`, `getLitValueModulus?`), by the evaluators of operations that build a new width (`setWidth`, `append`, `replicate`, `extractLsb`, `ofNat`, `allOnes`, and similar) in `simp`, `grind`, `Sym` and the `bv_decide` normalizer, and by `bv_decide` when it selects bit-vector atoms. `checkExponent` now rejects exponents that do not fit in 32 bits regardless of the threshold, as `checkShiftLeft` does. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
diff --git a/src/Lean/Meta/LitValues.lean b/src/Lean/Meta/LitValues.lean index 5d9156a..f0d9858 100644 --- a/src/Lean/Meta/LitValues.lean +++ b/src/Lean/Meta/LitValues.lean
@@ -7,6 +7,7 @@ prelude public import Lean.Meta.Basic import Init.While +import Lean.Util.SafeExponentiation public section namespace Lean.Meta /-! @@ -145,20 +146,25 @@ - a `BitVec.ofNat` application - a `BitVec.ofNatLT` application that encode a `BitVec n` with value `v`. + +Returns `none` if the width `n` is not safe to evaluate (see `isSafeBitVecWidth`). -/ def getBitVecValue? (e : Expr) : MetaM (Option ((n : Nat) × BitVec n)) := OptionT.run do match_expr e with | BitVec.ofNat nExpr vExpr => let n ← getNatValue? nExpr + guard (isSafeBitVecWidth n) let v ← getNatValue? vExpr return ⟨n, BitVec.ofNat n v⟩ | BitVec.ofNatLT nExpr vExpr _ => let n ← getNatValue? nExpr + guard (isSafeBitVecWidth n) let v ← getNatValue? vExpr return ⟨n, BitVec.ofNat n v⟩ | _ => let (v, type) ← getOfNatValue? e ``BitVec let n ← getNatValue? (← whnfD type.appArg!) + guard (isSafeBitVecWidth n) return ⟨n, BitVec.ofNat n v⟩ /-- @@ -171,6 +177,7 @@ | Fin n => getNatValue? n | BitVec w => let some w ← getNatValue? w | return none + unless isSafeBitVecWidth w do return none return some (2 ^ w) | UInt8 => return some (2 ^ 8) | UInt16 => return some (2 ^ 16)
diff --git a/src/Lean/Meta/Sym/DSimp/EvalGround.lean b/src/Lean/Meta/Sym/DSimp/EvalGround.lean index 321b817..2f48136 100644 --- a/src/Lean/Meta/Sym/DSimp/EvalGround.lean +++ b/src/Lean/Meta/Sym/DSimp/EvalGround.lean
@@ -410,18 +410,21 @@ abbrev evalBitVecExtend (op : {n : Nat} → (v : Nat) → BitVec n → BitVec v) (v a : Expr) : DSimpM Result := do let some v := getNatValue? v | return .rfl let some a := getBitVecValue? a | return .rfl + unless isSafeBitVecWidth v do return .rfl let e ← share <| toExpr <| op v a.val return .step e (done := true) def evalBitVecAppend (a b : Expr) : DSimpM Result := do let some a := getBitVecValue? a | return .rfl let some b := getBitVecValue? b | return .rfl + unless isSafeBitVecWidth (a.n + b.n) do return .rfl let e ← share <| toExpr (a.val ++ b.val) return .step e (done := true) def evalBitVecCast (m a : Expr) : DSimpM Result := do let some m := getNatValue? m | return .rfl let some a := getBitVecValue? a | return .rfl + unless isSafeBitVecWidth m do return .rfl let e ← share <| toExpr <| BitVec.ofNat m a.val.toNat return .step e (done := true) @@ -429,6 +432,7 @@ let some a := getBitVecValue? a | return .rfl let some m := getNatValue? m | return .rfl unless isSafeShiftLeft a.val.toNat m maxShiftLeft do return .rfl + unless isSafeBitVecWidth (a.n + m) do return .rfl let e ← share <| toExpr <|a.val.shiftLeftZeroExtend m return .step e (done := true) @@ -436,17 +440,20 @@ let some start := getNatValue? start | return .rfl let some len := getNatValue? len | return .rfl let some a := getBitVecValue? a | return .rfl + unless isSafeBitVecWidth len do return .rfl let e ← share <| toExpr <| a.val.extractLsb' start len return .step e (done := true) def evalBitVecReplicate (i a : Expr) : DSimpM Result := do let some i := getNatValue? i | return .rfl let some a := getBitVecValue? a | return .rfl + unless isSafeBitVecWidth (a.n * i) do return .rfl let e ← share <| toExpr <| a.val.replicate i return .step e (done := true) def evalBitVecAllOnes (n : Expr) : DSimpM Result := do let some n := getNatValue? n | return .rfl + unless isSafeBitVecWidth n do return .rfl let e ← share <| toExpr <| BitVec.allOnes n return .step e (done := true) @@ -463,6 +470,7 @@ def evalBitVecOfInt (n i : Expr) : DSimpM Result := do let some n := getNatValue? n | return .rfl let some i := getIntValue? i | return .rfl + unless isSafeBitVecWidth n do return .rfl let e ← share <| toExpr <| BitVec.ofInt n i return .step e (done := true) @@ -474,6 +482,7 @@ def evalBitVecOfFin (n a : Expr) : DSimpM Result := do let some n := getNatValue? n | return .rfl let some a := getFinValue? a | return .rfl + unless isSafeBitVecWidth n do return .rfl let e ← share <| toExpr <| BitVec.ofNat n a.val.val return .step e (done := true) @@ -481,12 +490,14 @@ let some a := getNatValue? a | return .rfl if Sym.isNatValue n then return .rfl -- already in normal form let some n ← evalNat n |>.run | return .rfl + unless isSafeBitVecWidth n do return .rfl let e ← share <| toExpr (BitVec.ofNat n a) return .step e (done := true) def evalBitVecOfNatClamp (n a : Expr) : DSimpM Result := do let some n := getNatValue? n | return .rfl let some a := getNatValue? a | return .rfl + unless isSafeBitVecWidth n do return .rfl let e ← share <| toExpr (BitVec.ofNatClamp n a) return .step e (done := true)
diff --git a/src/Lean/Meta/Sym/LitValues.lean b/src/Lean/Meta/Sym/LitValues.lean index cef2587..dbaace3 100644 --- a/src/Lean/Meta/Sym/LitValues.lean +++ b/src/Lean/Meta/Sym/LitValues.lean
@@ -7,6 +7,7 @@ prelude public import Lean.Expr public import Init.Data.Rat +import Lean.Util.SafeExponentiation public section namespace Lean.Meta.Sym /-! @@ -40,19 +41,23 @@ n : Nat val : BitVec n +/-- Returns `none` if the width is not safe to evaluate (see `isSafeBitVecWidth`). -/ def getBitVecValue? (e : Expr) : OptionT Id BitVecValue := match_expr e with | BitVec.ofNat nExpr vExpr => do let n ← getNatValue? nExpr + guard (isSafeBitVecWidth n) let v ← getNatValue? vExpr return ⟨n, BitVec.ofNat n v⟩ | BitVec.ofNatLT nExpr vExpr _ => do let n ← getNatValue? nExpr + guard (isSafeBitVecWidth n) let v ← getNatValue? vExpr return ⟨n, BitVec.ofNat n v⟩ | OfNat.ofNat α v _ => do let_expr BitVec n := α | failure let n ← getNatValue? n + guard (isSafeBitVecWidth n) let .lit (.natVal v) := v | failure return ⟨n, BitVec.ofNat n v⟩ | _ => failure
diff --git a/src/Lean/Meta/Sym/Simp/EvalGround.lean b/src/Lean/Meta/Sym/Simp/EvalGround.lean index d7c3093..2afb413 100644 --- a/src/Lean/Meta/Sym/Simp/EvalGround.lean +++ b/src/Lean/Meta/Sym/Simp/EvalGround.lean
@@ -672,6 +672,7 @@ -- `BitVec.ofNat` with a literal width is the normal form only when `bitVecOfNat := true` if Sym.isNatValue n && (← read).bitVecOfNat then return .rfl if let some nv ← evalNat n |>.run then -- TODO: consider using dsimp + unless isSafeBitVecWidth nv do return .rfl let r ← mkBitVecLit (BitVec.ofNat nv a) return .step r (mkRflBitVec r nv) (done := true) else @@ -689,6 +690,7 @@ def evalBitVecOfNatClamp (n a : Expr) : EvalM Result := do let some nv := getNatValue? n | return .rfl let some av := getNatValue? a | return .rfl + unless isSafeBitVecWidth nv do return .rfl let r ← mkBitVecLit (BitVec.ofNatClamp nv av) return .step r (mkRflBitVec r nv) (done := true) @@ -721,18 +723,21 @@ def evalBitVecAppend (a b : Expr) : EvalM Result := do let some a := getBitVecValue? a | return .rfl let some b := getBitVecValue? b | return .rfl + unless isSafeBitVecWidth (a.n + b.n) do return .rfl let e ← mkBitVecLit (a.val ++ b.val) return .step e (mkRflBitVec e (a.n + b.n)) (done := true) def evalBitVecCast (m a : Expr) : EvalM Result := do let some a := getBitVecValue? a | return .rfl let some m ← evalNat m |>.run | return .rfl + unless isSafeBitVecWidth m do return .rfl let e ← mkBitVecLit (BitVec.ofNat m a.val.toNat) return .step e (mkRflBitVec e m) (done := true) def evalBitVecExtend (op : {n : Nat} → (v : Nat) → BitVec n → BitVec v) (v a : Expr) : EvalM Result := do let some a := getBitVecValue? a | return .rfl let some v ← evalNat v |>.run | return .rfl + unless isSafeBitVecWidth v do return .rfl let e ← mkBitVecLit (op v a.val) return .step e (mkRflBitVec e v) (done := true) @@ -740,12 +745,14 @@ let some a := getBitVecValue? a | return .rfl let some start ← evalNat start |>.run | return .rfl let some len ← evalNat len |>.run | return .rfl + unless isSafeBitVecWidth len do return .rfl let e ← mkBitVecLit (a.val.extractLsb' start len) return .step e (mkRflBitVec e len) (done := true) def evalBitVecReplicate (i a : Expr) : EvalM Result := do let some a := getBitVecValue? a | return .rfl let some i ← evalNat i |>.run | return .rfl + unless isSafeBitVecWidth (a.n * i) do return .rfl let e ← mkBitVecLit (a.val.replicate i) return .step e (mkRflBitVec e (a.n * i)) (done := true) @@ -753,17 +760,20 @@ let some a := getBitVecValue? a | return .rfl let some m ← evalNat m |>.run | return .rfl unless isSafeShiftLeft a.val.toNat m (← read).maxShiftLeft do return .rfl + unless isSafeBitVecWidth (a.n + m) do return .rfl let e ← mkBitVecLit (a.val.shiftLeftZeroExtend m) return .step e (mkRflBitVec e (a.n + m)) (done := true) def evalBitVecAllOnes (n : Expr) : EvalM Result := do let some n ← evalNat n |>.run | return .rfl + unless isSafeBitVecWidth n do return .rfl let e ← mkBitVecLit (BitVec.allOnes n) return .step e (mkRflBitVec e n) (done := true) def evalBitVecOfInt (n i : Expr) : EvalM Result := do let some n ← evalNat n |>.run | return .rfl let some i := getIntValue? i | return .rfl + unless isSafeBitVecWidth n do return .rfl let e ← mkBitVecLit (BitVec.ofInt n i) return .step e (mkRflBitVec e n) (done := true) @@ -777,6 +787,7 @@ def evalBitVecOfFin (n a : Expr) : EvalM Result := do let some n ← evalNat n |>.run | return .rfl let some a := getFinValue? a | return .rfl + unless isSafeBitVecWidth n do return .rfl let e ← mkBitVecLit (BitVec.ofNat n a.val.val) return .step e (mkRflBitVec e n) (done := true)
diff --git a/src/Lean/Meta/Tactic/BVDecide/Normalize/Simproc.lean b/src/Lean/Meta/Tactic/BVDecide/Normalize/Simproc.lean index 97aee46..2f0d11b 100644 --- a/src/Lean/Meta/Tactic/BVDecide/Normalize/Simproc.lean +++ b/src/Lean/Meta/Tactic/BVDecide/Normalize/Simproc.lean
@@ -104,6 +104,15 @@ let h := mkExpectedPropHint h decEqTrue mkApp3 (mkConst ``of_decide_eq_true) p inst h +/-- +Returns the width `w` if `wExpr` is a literal and bit-vectors of width `w` are safe to evaluate +(see `isSafeBitVecWidth`). +-/ +def getWidth? (wExpr : Expr) : Option Nat := do + let w ← Sym.getNatValue? wExpr + guard (isSafeBitVecWidth w) + return w + @[inline] def mkLit [ToExpr α] (a : α) : Sym.SymM Expr := Sym.share <| toExpr a @@ -270,7 +279,7 @@ countRule `bvAnd.andSelf return .step lhs (mkApp2 (mkConst ``BitVec.and_self) wExpr lhs) else - let some w := Sym.getNatValue? wExpr | return .rfl + let some w := getWidth? wExpr | return .rfl let notAnd : SimprocM (Option Sym.Simp.Result) := tryRule `bvAnd.notAnd do let_expr Complement.complement _ _ lhs := lhs | return none if lhs != rhs then return none @@ -305,7 +314,7 @@ def bvAdd (ty lhs rhs : Expr) : SimprocM (Sym.Simp.Result) := do let_expr BitVec wExpr := ty | return .rfl - let some w := Sym.getNatValue? wExpr | return .rfl + let some w := getWidth? wExpr | return .rfl if lhs == rhs then let expr ← BitVec.mkMul lhs (← mkLit 2#w) wExpr countRule `bvAdd.addSame @@ -436,7 +445,7 @@ def bvShiftRight (α lhs rhs : Expr) : SimprocM (Sym.Simp.Result) := do let_expr BitVec wExpr := α | return .rfl - let some w := Sym.getNatValue? wExpr | return .rfl + let some w := getWidth? wExpr | return .rfl if lhs == rhs then let proof := mkApp2 (mkConst ``Std.Tactic.BVDecide.Normalize.BitVec.ushiftRight_self) wExpr lhs countRule `bvShiftRight.ushiftRightSelf @@ -445,6 +454,7 @@ let_expr BitVec.ofNat nExpr kExpr := rhs | return .rfl let some n := Sym.getNatValue? nExpr | return .rfl let some k := Sym.getNatValue? kExpr | return .rfl + unless isSafeBitVecWidth n do return .rfl let expr ← BitVec.mkNatShiftRight lhs (← mkLit (k % 2 ^ n)) wExpr let proof := mkApp4 (mkConst ``BitVec.ushiftRight_ofNat_eq) wExpr lhs nExpr kExpr countRule `bvShiftRight.ushiftRightOfNat @@ -452,7 +462,7 @@ def extractLsb' (wExpr startExpr lenExpr targetExpr : Expr) : SimprocM (Sym.Simp.Result) := do let tryExtractFull : SimprocM Sym.Simp.Result := do - let some w := Sym.getNatValue? wExpr | return .rfl + let some w := getWidth? wExpr | return .rfl let some start := Sym.getNatValue? startExpr | return .rfl let some len := Sym.getNatValue? lenExpr | return .rfl if start != 0 then return .rfl @@ -497,7 +507,7 @@ | HAdd.hAdd _ _ _ _ lhs rhs => let some start := Sym.getNatValue? startExpr | return .rfl let some len := Sym.getNatValue? lenExpr | return .rfl - let some width := Sym.getNatValue? wExpr | return .rfl + let some width := getWidth? wExpr | return .rfl if !(start == 0 && len ≤ width) then return .rfl let lhs' ← BitVec.mkExtractLsb' wExpr startExpr lenExpr lhs let rhs' ← BitVec.mkExtractLsb' wExpr startExpr lenExpr rhs @@ -515,7 +525,7 @@ | HMul.hMul _ _ _ _ lhs rhs => let some start := Sym.getNatValue? startExpr | return .rfl let some len := Sym.getNatValue? lenExpr | return .rfl - let some width := Sym.getNatValue? wExpr | return .rfl + let some width := getWidth? wExpr | return .rfl if !(start == 0 && len ≤ width) then return .rfl let lhs' ← BitVec.mkExtractLsb' wExpr startExpr lenExpr lhs let rhs' ← BitVec.mkExtractLsb' wExpr startExpr lenExpr rhs @@ -569,7 +579,7 @@ return .rfl | Complement.complement _ _ inner => unless isVarValueExtract inner do return .rfl - let some initialWidthVal := Sym.getNatValue? wExpr | return .rfl + let some initialWidthVal := getWidth? wExpr | return .rfl let some startVal := Sym.getNatValue? startExpr | return .rfl let some lenVal := Sym.getNatValue? lenExpr | return .rfl if !(startVal + lenVal) < initialWidthVal then return .rfl @@ -608,6 +618,7 @@ let_expr BitVec.ofNat nExpr kExpr := rhs | return .rfl let some n := Sym.getNatValue? nExpr | return .rfl let some k := Sym.getNatValue? kExpr | return .rfl + unless isSafeBitVecWidth n do return .rfl let expr ← BitVec.mkNatShiftLeft lhs (← mkLit (k % 2 ^ n)) wExpr let proof := mkApp4 (mkConst ``BitVec.shiftLeft_ofNat_eq) wExpr lhs nExpr kExpr countRule `bvShiftLeft @@ -692,7 +703,7 @@ countRule `bvBeq.beqSelf return .step (← mkLit true) proof else - let some w := Sym.getNatValue? wExpr | return .rfl + let some w := getWidth? wExpr | return .rfl let addInj : SimprocM (Option Sym.Simp.Result) := tryRule `bvBeq.addInj do let_expr HAdd.hAdd _ _ _ _ llhs lrhs := lhs | return none let_expr HAdd.hAdd _ _ _ _ rlhs rrhs := rhs | return none @@ -1182,7 +1193,7 @@ -- A specialised version of BitVec.neg_eq_not_add so it doesn't trigger on -constant def bvNeg (α val : Expr) : SimprocM (Sym.Simp.Result) := do let_expr BitVec wExpr := α | return .rfl - let some w := Sym.getNatValue? wExpr | return .rfl + let some w := getWidth? wExpr | return .rfl match Sym.getBitVecValue? val with | some _ => return .rfl | none => @@ -1291,7 +1302,7 @@ def bvShiftRightNat (α lhsExpr rhsExpr : Expr) : SimprocM (Sym.Simp.Result) := do let_expr BitVec wExpr := α | return .rfl - let some w := Sym.getNatValue? wExpr | return .rfl + let some w := getWidth? wExpr | return .rfl match Sym.getNatValue? rhsExpr with | some rhs => if rhs < w then @@ -1323,7 +1334,7 @@ def bvShiftLeftNat (α lhsExpr rhsExpr : Expr) : SimprocM (Sym.Simp.Result) := do let_expr BitVec wExpr := α | return .rfl - let some w := Sym.getNatValue? wExpr | return .rfl + let some w := getWidth? wExpr | return .rfl match Sym.getNatValue? rhsExpr with | some rhs => @@ -1359,7 +1370,7 @@ if Sym.isNatValue rhsExpr then return .rfl -- Handled by other rewrite rules if isBitVecToNat rhsExpr then return .rfl - let some w := Sym.getNatValue? wExpr | return .rfl + let some w := getWidth? wExpr | return .rfl let (vExpr, clamped, h) ← clampShiftAmount wExpr rhsExpr w let expr ← BitVec.mkSshiftRight' lhsExpr clamped wExpr vExpr let proof := mkApp5 (mkConst ``BitVec.sshiftRight_eq_sshiftRight'_ofNatClamp)
diff --git a/src/Lean/Meta/Tactic/BVDecide/Reflect/Reify.lean b/src/Lean/Meta/Tactic/BVDecide/Reflect/Reify.lean index aaca401..937237d 100644 --- a/src/Lean/Meta/Tactic/BVDecide/Reflect/Reify.lean +++ b/src/Lean/Meta/Tactic/BVDecide/Reflect/Reify.lean
@@ -11,6 +11,7 @@ import Lean.Meta.Tactic.BVDecide.Reflect.ReifiedBVPred import Lean.Meta.Tactic.BVDecide.Reflect.ReifiedBVLogical import Lean.Meta.Sym.LitValues +import Lean.Util.SafeExponentiation import Lean.Meta.AppBuilder import Std.Tactic.BVDecide.Reflect import Lean.Meta.Sym.InferType @@ -29,7 +30,9 @@ public def isValidBitVecAtom (e : Expr) : ReifyM (Option Nat) := do let t ← Sym.instantiateMVarsS (← Sym.inferType e) let_expr BitVec widthExpr := t | return none - return Sym.getNatValue? widthExpr + let some width := Sym.getNatValue? widthExpr | return none + unless isSafeBitVecWidth width do return none + return some width public def isValidBoolAtom (e : Expr) : ReifyM Bool := do let ty ← Sym.inferType e
diff --git a/src/Lean/Meta/Tactic/Grind/BitVec.lean b/src/Lean/Meta/Tactic/Grind/BitVec.lean index 7bf5d66..4427478 100644 --- a/src/Lean/Meta/Tactic/Grind/BitVec.lean +++ b/src/Lean/Meta/Tactic/Grind/BitVec.lean
@@ -87,20 +87,21 @@ (op : {n : Nat} → (m : Nat) → BitVec n → BitVec m) (e : Expr) : GoalM Unit := do unless e.isAppOfArity declName 3 do return () let some m ← getNatValue? (e.getArg! 1) | return () -- occurs in the result type + unless isSafeBitVecWidth m do return () unaryOp e fun r => do let some ⟨_, v⟩ ← getBitVecValue? r | return none some <$> mkBVLit m (op m v) -/-- Table entry for extract-like `op : (i j : Nat) → BitVec n → BitVec m`. -/ -@[inline] private def extractBV (declName : Name) - (op : {n : Nat} → (i j : Nat) → BitVec n → (m : Nat) × BitVec m) (e : Expr) : GoalM Unit := do +/-- Table entry for extract-like `op : (i j : Nat) → BitVec n → BitVec (width i j)`. -/ +@[inline] private def extractBV (declName : Name) (width : Nat → Nat → Nat) + (op : {n : Nat} → (i j : Nat) → BitVec n → BitVec (width i j)) (e : Expr) : GoalM Unit := do unless e.isAppOfArity declName 4 do return () let some i ← getNatValue? (e.getArg! 1) | return () -- occurs in the result type let some j ← getNatValue? (e.getArg! 2) | return () + unless isSafeBitVecWidth (width i j) do return () unaryOp e fun r => do let some ⟨_, v⟩ ← getBitVecValue? r | return none - let ⟨m, w⟩ := op i j v - some <$> mkBVLit m w + some <$> mkBVLit (width i j) (op i j v) /-- Table entry for `op : BitVec n → BitVec n → BitVec n` (e.g. `&&&`) with a `BitVec` type guard. -/ @[inline] private def binBV (declName : Name) (arity : Nat) @@ -165,6 +166,7 @@ builtin_grind_propagator propagateBVOfNat ↑BitVec.ofNat := fun e => do unless e.isAppOfArity ``BitVec.ofNat 2 do return () let some w ← getNatValue? (e.getArg! 0) | return () -- occurs in the result type + unless isSafeBitVecWidth w do return () unaryOp e fun r => do let some n ← getNatValue? r | return none some <$> mkBVLit w (BitVec.ofNat w n) @@ -172,6 +174,7 @@ builtin_grind_propagator propagateBVOfInt ↑BitVec.ofInt := fun e => do unless e.isAppOfArity ``BitVec.ofInt 2 do return () let some w ← getNatValue? (e.getArg! 0) | return () -- occurs in the result type + unless isSafeBitVecWidth w do return () unaryOp e fun r => do let some i ← getIntValue? r | return none some <$> mkBVLit w (BitVec.ofInt w i) @@ -180,15 +183,16 @@ builtin_grind_propagator propagateBVSignExtend ↑BitVec.signExtend := extendBV ``BitVec.signExtend BitVec.signExtend builtin_grind_propagator propagateBVExtractLsb' ↑BitVec.extractLsb' := - extractBV ``BitVec.extractLsb' fun start len v => ⟨len, v.extractLsb' start len⟩ + extractBV ``BitVec.extractLsb' (fun _ len => len) fun start len v => v.extractLsb' start len builtin_grind_propagator propagateBVExtractLsb ↑BitVec.extractLsb := - extractBV ``BitVec.extractLsb fun hi lo v => ⟨hi - lo + 1, v.extractLsb hi lo⟩ + extractBV ``BitVec.extractLsb (fun hi lo => hi - lo + 1) fun hi lo v => v.extractLsb hi lo builtin_grind_propagator propagateBVReplicate ↑BitVec.replicate := fun e => do unless e.isAppOfArity ``BitVec.replicate 3 do return () let some i ← getNatValue? (e.getArg! 1) | return () -- occurs in the result type unaryOp e fun r => do let some ⟨n, v⟩ ← getBitVecValue? r | return none + unless isSafeBitVecWidth (n * i) do return none some <$> mkBVLit (n * i) (v.replicate i) builtin_grind_propagator propagateBVAnd ↑HAnd.hAnd := binBV ``HAnd.hAnd 6 (· &&& ·) @@ -201,6 +205,7 @@ binOp e fun r₁ r₂ => do let some ⟨n₁, v₁⟩ ← getBitVecValue? r₁ | return none let some ⟨n₂, v₂⟩ ← getBitVecValue? r₂ | return none + unless isSafeBitVecWidth (n₁ + n₂) do return none some <$> mkBVLit (n₁ + n₂) (v₁ ++ v₂) builtin_grind_propagator propagateBVShiftLeft ↑BitVec.shiftLeft :=
diff --git a/src/Lean/Meta/Tactic/Simp/BuiltinSimprocs/BitVec.lean b/src/Lean/Meta/Tactic/Simp/BuiltinSimprocs/BitVec.lean index 39085ea..e760cdc 100644 --- a/src/Lean/Meta/Tactic/Simp/BuiltinSimprocs/BitVec.lean +++ b/src/Lean/Meta/Tactic/Simp/BuiltinSimprocs/BitVec.lean
@@ -65,6 +65,7 @@ unless e.isAppOfArity declName 3 do return .continue let some v ← fromExpr? e.appArg! | return .continue let some n ← Nat.fromExpr? e.appFn!.appArg! | return .continue + unless isSafeBitVecWidth n do return .continue return .done <| (← toExpr' (op n v.value)) /-- @@ -255,6 +256,7 @@ let_expr HAppend.hAppend _ _ _ _ a b ← e | return .continue let some v₁ ← fromExpr? a | return .continue let some v₂ ← fromExpr? b | return .continue + unless isSafeBitVecWidth (v₁.n + v₂.n) do return .continue return .done <| (← toExpr' (v₁.value ++ v₂.value)) set_option linter.coreInternal.internalModule false in -- User-facing builtin simprocs are fine @@ -263,6 +265,7 @@ let_expr cast _ m _ v ← e | return .continue let some v ← fromExpr? v | return .continue let some m ← Nat.fromExpr? m | return .continue + unless isSafeBitVecWidth m do return .continue return .done <| (← toExpr' (BitVec.ofNat m v.value.toNat)) set_option linter.coreInternal.internalModule false in -- User-facing builtin simprocs are fine @@ -285,6 +288,7 @@ let_expr BitVec.ofInt n i ← e | return .continue let some n ← Nat.fromExpr? n | return .continue let some i ← Int.fromExpr? i | return .continue + unless isSafeBitVecWidth n do return .continue return .done <| (← toExpr' (BitVec.ofInt n i)) set_option linter.coreInternal.internalModule false in -- User-facing builtin simprocs are fine @@ -293,6 +297,7 @@ let_expr BitVec.ofNatClamp w n ← e | return .continue let some w ← Nat.fromExpr? w | return .continue let some n ← Nat.fromExpr? n | return .continue + unless isSafeBitVecWidth w do return .continue return .done <| (← toExpr' (BitVec.ofNatClamp w n)) set_option linter.coreInternal.internalModule false in -- User-facing builtin simprocs are fine @@ -301,6 +306,7 @@ let_expr BitVec.ofNat n v ← e | return .continue let some v ← Nat.fromExpr? v | return .continue if let some n ← Nat.fromExpr? n then + unless isSafeBitVecWidth n do return .continue let bv := BitVec.ofNat n v -- `BitVec.ofNat` is the normal form only when `bitVecOfNat := true`; otherwise -- literals must be rewritten to the `OfNat.ofNat` form produced by `toExpr'`. @@ -382,6 +388,7 @@ let some v ← fromExpr? v | return .continue let some w ← Nat.fromExpr? w | return .continue if h : v.n ≤ w then + unless isSafeBitVecWidth w do return .continue return .done <| (← toExpr' (v.value.setWidth' h)) else return .continue @@ -394,6 +401,7 @@ let some m ← Nat.fromExpr? m | return .continue let warning := (← Simp.getConfig).warnExponents unless (← checkShiftLeft v.value.toNat m (warning := warning)) do return .continue + unless isSafeBitVecWidth (v.n + m) do return .continue return .done <| (← toExpr' (v.value.shiftLeftZeroExtend m)) set_option linter.coreInternal.internalModule false in -- User-facing builtin simprocs are fine @@ -403,6 +411,7 @@ let some v ← fromExpr? v | return .continue let some start ← Nat.fromExpr? start | return .continue let some len ← Nat.fromExpr? len | return .continue + unless isSafeBitVecWidth len do return .continue return .done <| (← toExpr' (v.value.extractLsb' start len)) set_option linter.coreInternal.internalModule false in -- User-facing builtin simprocs are fine @@ -412,6 +421,7 @@ let some v ← fromExpr? v | return .continue let some hi ← Nat.fromExpr? hi | return .continue let some lo ← Nat.fromExpr? lo | return .continue + unless isSafeBitVecWidth (hi - lo + 1) do return .continue return .done <| (← toExpr' (v.value.extractLsb hi lo)) set_option linter.coreInternal.internalModule false in -- User-facing builtin simprocs are fine @@ -420,6 +430,7 @@ let_expr replicate _ i v ← e | return .continue let some v ← fromExpr? v | return .continue let some i ← Nat.fromExpr? i | return .continue + unless isSafeBitVecWidth (v.n * i) do return .continue return .done <| (← toExpr' (v.value.replicate i)) set_option linter.coreInternal.internalModule false in -- User-facing builtin simprocs are fine @@ -439,6 +450,7 @@ builtin_dsimproc [simp, seval] reduceAllOnes (allOnes _) := fun e => do let_expr allOnes n ← e | return .continue let some n ← Nat.fromExpr? n | return .continue + unless isSafeBitVecWidth n do return .continue return .done <| (← toExpr' (allOnes n)) set_option linter.coreInternal.internalModule false in -- User-facing builtin simprocs are fine @@ -446,6 +458,7 @@ let_expr BitVec.ofFin w v ← e | return .continue let some w ← evalNat w |>.run | return .continue let some ⟨_, v⟩ ← getFinValue? v | return .continue + unless isSafeBitVecWidth w do return .continue return .done <| (← toExpr' (BitVec.ofNat w v.val)) set_option linter.coreInternal.internalModule false in -- User-facing builtin simprocs are fine
diff --git a/src/Lean/Util/SafeExponentiation.lean b/src/Lean/Util/SafeExponentiation.lean index 275e4df..66b310d 100644 --- a/src/Lean/Util/SafeExponentiation.lean +++ b/src/Lean/Util/SafeExponentiation.lean
@@ -22,9 +22,12 @@ } /-- -Returns `true` if `n` is `≤ exponentiation.threshold`. Otherwise, +Returns `true` if `n` is `≤ exponentiation.threshold` and fits in 32 bits. Otherwise, reports a warning and returns `false`. This method ensures there is at most one warning message of this kind in the message log. + +The runtime implementation of `Nat.pow` aborts the process when the exponent does not fit in +32 bits, even if the user increased `exponentiation.threshold`. -/ def checkExponent (n : Nat) (warning := true) : CoreM Bool := do let threshold ← getRecordedOption exponentiation.threshold @@ -32,6 +35,10 @@ if (← pure warning <&&> logMessageKind `unsafe.exponentiation) then logWarning s!"exponent {n} exceeds the threshold {threshold}, exponentiation operation was not evaluated, use `set_option {exponentiation.threshold.name} <num>` to set a new threshold" return false + else if n ≥ UInt32.size then + if (← pure warning <&&> logMessageKind `unsafe.exponentiation) then + logWarning s!"exponent {n} does not fit in 32 bits, exponentiation operation was not evaluated" + return false else return true @@ -75,4 +82,16 @@ else return true +/-- +Returns `true` if bit-vectors of width `w` are safe to evaluate. + +Most bit-vector operations compute `2^w`. The bound `2^32` comes from the runtime, not the kernel: +`lean_nat_pow` (`src/runtime/object.cpp`) aborts the process when the base is greater than `1` and +the exponent is greater than `UINT_MAX`, because `mpz::pow` takes the exponent as an +`unsigned int`. Thus, procedures that evaluate bit-vector operations on literals must not build a +`BitVec w` value unless this function returns `true`. +-/ +def isSafeBitVecWidth (w : Nat) : Bool := + w < UInt32.size + end Lean
diff --git a/tests/elab/bitvec_huge_width.lean b/tests/elab/bitvec_huge_width.lean new file mode 100644 index 0000000..f137f27 --- /dev/null +++ b/tests/elab/bitvec_huge_width.lean
@@ -0,0 +1,324 @@ +/-! +Bit-vector literals of width `2^32` or more are not evaluated by `simp` and `cbv`. +Evaluating them requires computing `2^w`, and the runtime implementation of `Nat.pow` aborts the +process when the exponent does not fit in 32 bits. +-/ + +-- `simp` + +/-- +error: `simp` made no progress +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : 1#4294967296 = x := by simp + +/-- +error: unsolved goals +x : BitVec 4294967296 +n : Nat +⊢ 5#4294967296 = x +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (5 : BitVec 4294967296) = x := by simp + +/-- +error: `simp` made no progress +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : 1#4294967296 + 1#4294967296 = x := by simp + +/-- +error: `simp` made no progress +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : -1#4294967296 = x := by simp + +/-- +error: `simp` made no progress +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : ~~~1#4294967296 = x := by simp + +/-- +error: unsolved goals +x : BitVec 4294967296 +n : Nat +⊢ 3#4294967296 = x +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : 1#4294967296 * 3#4294967296 = x := by simp + +/-- +warning: exponent 4294967296 exceeds the threshold 256, exponentiation operation was not evaluated, use `set_option exponentiation.threshold <num>` to set a new threshold +--- +error: unsolved goals +x : BitVec 4294967296 +n : Nat +⊢ 1 = n +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (1#4294967296).toNat = n := by simp + +/-- +warning: exponent 4294967296 exceeds the threshold 256, exponentiation operation was not evaluated, use `set_option exponentiation.threshold <num>` to set a new threshold +--- +error: unsolved goals +x : BitVec 4294967296 +n : Nat +⊢ 1 = n +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (BitVec.setWidth 4294967296 1#8).toNat = n := by simp + +/-- +error: `simp` made no progress +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (BitVec.signExtend 4294967296 255#8).toNat = n := by simp + +/-- +warning: exponent 4294967296 exceeds the threshold 256, exponentiation operation was not evaluated, use `set_option exponentiation.threshold <num>` to set a new threshold +--- +error: unsolved goals +x : BitVec 4294967296 +n : Nat +⊢ (-1 % 2 ^ 4294967296).toNat = n +--- +error: (kernel) the kernel refused to evaluate `Nat.pow` because its second argument does not fit in a 32-bit unsigned integer +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (BitVec.ofInt 4294967296 (-1)).toNat = n := by simp + +/-- +warning: exponent 4294967296 exceeds the threshold 256, exponentiation operation was not evaluated, use `set_option exponentiation.threshold <num>` to set a new threshold +--- +error: unsolved goals +x : BitVec 4294967296 +n : Nat +⊢ 2 ^ 4294967296 - 1 = n +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (BitVec.allOnes 4294967296).toNat = n := by simp + +/-- +error: Tactic `simp` failed with a nested error: +maximum recursion depth has been reached +use `set_option maxRecDepth <num>` to increase limit +use `set_option diagnostics true` to get diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (BitVec.replicate 4294967296 1#1).toNat = n := by simp + +/-- +warning: exponent 4294967296 exceeds the threshold 256, exponentiation operation was not evaluated, use `set_option exponentiation.threshold <num>` to set a new threshold +--- +error: unsolved goals +x : BitVec 4294967296 +n : Nat +⊢ 1 = n +--- +error: (kernel) the kernel refused to evaluate `Nat.pow` because its second argument does not fit in a 32-bit unsigned integer +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (BitVec.extractLsb' 0 4294967296 1#8).toNat = n := by simp + +/-- +warning: exponent 4294967296 exceeds the threshold 256, exponentiation operation was not evaluated, use `set_option exponentiation.threshold <num>` to set a new threshold +--- +error: unsolved goals +x : BitVec 4294967296 +n : Nat +⊢ 0 = n +--- +error: (kernel) the kernel refused to evaluate `Nat.pow` because its second argument does not fit in a 32-bit unsigned integer +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (BitVec.extractLsb 4294967296 1 1#8).toNat = n := by simp + +/-- +error: `simp` made no progress +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (BitVec.ofNatClamp 4294967296 5).toNat = n := by simp + +/-- +warning: exponent 4294967296 exceeds the threshold 256, exponentiation operation was not evaluated, use `set_option exponentiation.threshold <num>` to set a new threshold +--- +error: unsolved goals +x : BitVec 4294967296 +n : Nat +⊢ 5 % 2 ^ 4294967296 = n +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (BitVec.ofNat 4294967296 (2 + 3)).toNat = n := by simp + +/-- +error: unsolved goals +x : BitVec 4294967296 +n : Nat +⊢ 1 = n +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (BitVec.setWidth 8 1#4294967296).toNat = n := by simp + +/-- +warning: exponent 4294967296 exceeds the threshold 256, exponentiation operation was not evaluated, use `set_option exponentiation.threshold <num>` to set a new threshold +--- +error: unsolved goals +⊢ 1 < 2 % 2 ^ 4294967296 +-/ +#guard_msgs in +example : 1#4294967296 < 2#4294967296 := by simp + +-- `cbv` + +/-- +error: maximum recursion depth has been reached +use `set_option maxRecDepth <num>` to increase limit +use `set_option diagnostics true` to get diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : 1#4294967296 = x := by cbv + +/-- +warning: exponent 4294967296 exceeds the threshold 256, exponentiation operation was not evaluated, use `set_option exponentiation.threshold <num>` to set a new threshold +--- +error: maximum recursion depth has been reached +use `set_option maxRecDepth <num>` to increase limit +use `set_option diagnostics true` to get diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (5 : BitVec 4294967296) = x := by cbv + +/-- +error: maximum recursion depth has been reached +use `set_option maxRecDepth <num>` to increase limit +use `set_option diagnostics true` to get diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : 1#4294967296 + 1#4294967296 = x := by cbv + +/-- +error: maximum recursion depth has been reached +use `set_option maxRecDepth <num>` to increase limit +use `set_option diagnostics true` to get diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : -1#4294967296 = x := by cbv + +/-- +error: maximum recursion depth has been reached +use `set_option maxRecDepth <num>` to increase limit +use `set_option diagnostics true` to get diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : ~~~1#4294967296 = x := by cbv + +/-- +error: maximum recursion depth has been reached +use `set_option maxRecDepth <num>` to increase limit +use `set_option diagnostics true` to get diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : 1#4294967296 * 3#4294967296 = x := by cbv + +/-- +error: maximum recursion depth has been reached +use `set_option maxRecDepth <num>` to increase limit +use `set_option diagnostics true` to get diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (1#4294967296).toNat = n := by cbv + +/-- +error: unsolved goals +x : BitVec 4294967296 +n : Nat +⊢ 1 = n +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (BitVec.setWidth 4294967296 1#8).toNat = n := by cbv + +/-- +error: maximum recursion depth has been reached +use `set_option maxRecDepth <num>` to increase limit +use `set_option diagnostics true` to get diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (BitVec.signExtend 4294967296 255#8).toNat = n := by cbv + +/-- +error: maximum recursion depth has been reached +use `set_option maxRecDepth <num>` to increase limit +use `set_option diagnostics true` to get diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (BitVec.ofInt 4294967296 (-1)).toNat = n := by cbv + +/-- +error: maximum recursion depth has been reached +use `set_option maxRecDepth <num>` to increase limit +use `set_option diagnostics true` to get diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (BitVec.allOnes 4294967296).toNat = n := by cbv + +/-- +error: maximum recursion depth has been reached +use `set_option maxRecDepth <num>` to increase limit +use `set_option diagnostics true` to get diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (BitVec.extractLsb' 0 4294967296 1#8).toNat = n := by cbv + +/-- +error: maximum recursion depth has been reached +use `set_option maxRecDepth <num>` to increase limit +use `set_option diagnostics true` to get diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (BitVec.extractLsb 4294967296 1 1#8).toNat = n := by cbv + +/-- +error: maximum recursion depth has been reached +use `set_option maxRecDepth <num>` to increase limit +use `set_option diagnostics true` to get diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (BitVec.ofNatClamp 4294967296 5).toNat = n := by cbv + +/-- +error: maximum recursion depth has been reached +use `set_option maxRecDepth <num>` to increase limit +use `set_option diagnostics true` to get diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (BitVec.ofNat 4294967296 (2 + 3)).toNat = n := by cbv + +/-- +error: maximum recursion depth has been reached +use `set_option maxRecDepth <num>` to increase limit +use `set_option diagnostics true` to get diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (BitVec.setWidth 8 1#4294967296).toNat = n := by cbv + +-- Bit-vectors of regular widths are still evaluated. + +#guard_msgs in +example : 255#8 + 1#8 = 0#8 := by simp + +#guard_msgs in +example : 255#8 + 1#8 = 0#8 := by cbv + +#guard_msgs in +example : BitVec.setWidth 16 255#8 = 255#16 := by simp + +#guard_msgs in +example : BitVec.setWidth 16 255#8 = 255#16 := by cbv + +#guard_msgs in +example : BitVec.allOnes 8 = 255#8 := by simp + +#guard_msgs in +example : BitVec.replicate 2 1#4 = 17#8 := by cbv
diff --git a/tests/elab/bv_decide_huge_width.lean b/tests/elab/bv_decide_huge_width.lean new file mode 100644 index 0000000..3dc7e30 --- /dev/null +++ b/tests/elab/bv_decide_huge_width.lean
@@ -0,0 +1,67 @@ +import Std.Tactic.BVDecide + +/-! +The `bv_decide` normalizer does not evaluate bit-vector literals of width `2^32` or more. +Evaluating them requires computing `2^w`, and the runtime implementation of `Nat.pow` aborts the +process when the exponent does not fit in 32 bits. +For the same reason, `bv_decide` does not treat terms of such widths as bit-vector atoms. +-/ + +/-- +error: unsolved goals +x : BitVec 4294967296 +h✝ : x = 1#4294967296 +x✝¹ : ¬x + x = 2#4294967296 +h : (x == 1#4294967296) = true +x✝ : (!x + x == 2#4294967296) = true +⊢ False +-/ +#guard_msgs in +example (x : BitVec 4294967296) (h : x = 1#4294967296) : x + x = 2#4294967296 := by + bv_normalize + +/-- +error: unsolved goals +x : BitVec 4294967296 +x✝¹ : ¬-1#4294967296 + x = x - 1#4294967296 +x✝ : (!-1#4294967296 + x == x + -1#4294967296) = true +⊢ False +-/ +#guard_msgs in +example (x : BitVec 4294967296) : (-1#4294967296) + x = x - 1#4294967296 := by + bv_normalize + +/-- +error: unsolved goals +x : BitVec 8 +h✝ : x = 1#8 +x✝¹ : ¬x + x = 2#8 +h : (x == 1#8) = true +x✝ : (!BitVec.extractLsb' 0 7 x ++ 0#1 == 2#8) = true +⊢ False +-/ +#guard_msgs in +example (x : BitVec 8) (h : x = 1#8) : x + x = 2#8 := by + bv_normalize + +/-- +error: The prover found a potentially spurious counterexample: +- It abstracted the following unsupported expressions as opaque variables: + - 1#8 ++ 0#4294967296 == x +Consider the following assignment: +1#8 ++ 0#4294967296 == x = false +-/ +#guard_msgs in +example (x : BitVec (8 + 4294967296)) : 1#8 ++ 0#4294967296 = x := by + bv_decide + +/-- +error: The prover found a potentially spurious counterexample: +- It abstracted the following unsupported expressions as opaque variables: + - x == y +Consider the following assignment: +x == y = false +-/ +#guard_msgs in +example (x y : BitVec 4294967296) : x = y := by + bv_decide
diff --git a/tests/elab/grind_bitvec_huge_width.lean b/tests/elab/grind_bitvec_huge_width.lean new file mode 100644 index 0000000..423757e --- /dev/null +++ b/tests/elab/grind_bitvec_huge_width.lean
@@ -0,0 +1,133 @@ +/-! +`grind` does not evaluate bit-vector literals of width `2^32` or more (#15786). +Evaluating them requires computing `2^w`, and the runtime implementation of `Nat.pow` aborts the +process when the exponent does not fit in 32 bits. +-/ + +-- #15786 + +#guard_msgs in +example (x y : BitVec 4294967296) (h : x = y) : y = x := by + grind + +#guard_msgs in +example (x : BitVec 4294967296) (h : x = 5) : x = 5 := by + grind + +#guard_msgs in +example (x : BitVec (2 ^ 32)) (h : x = 5) : x = 5 := by + grind + +#guard_msgs in +example (x : BitVec 4294967296) (h : x = 1#4294967296) : x = 1#4294967296 := by + grind + +/-- +error: `grind` has failed, use `+verbose` to obtain diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : 1#4294967296 = x := by grind + +/-- +error: `grind` has failed, use `+verbose` to obtain diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (5 : BitVec 4294967296) = x := by grind + +/-- +error: `grind` has failed, use `+verbose` to obtain diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : 1#4294967296 + 1#4294967296 = x := by grind + +/-- +error: `grind` has failed, use `+verbose` to obtain diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : -1#4294967296 = x := by grind + +/-- +error: `grind` has failed, use `+verbose` to obtain diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : ~~~1#4294967296 = x := by grind + +/-- +error: `grind` has failed, use `+verbose` to obtain diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : 1#4294967296 * 3#4294967296 = x := by grind + +/-- +error: `grind` has failed, use `+verbose` to obtain diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (1#4294967296).toNat = n := by grind + +/-- +error: `grind` has failed, use `+verbose` to obtain diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (BitVec.setWidth 4294967296 1#8).toNat = n := by grind + +/-- +warning: exponent 4294967296 exceeds the threshold 256, exponentiation operation was not evaluated, use `set_option exponentiation.threshold <num>` to set a new threshold +--- +error: `grind` has failed, use `+verbose` to obtain diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (BitVec.signExtend 4294967296 255#8).toNat = n := by grind + +/-- +error: `grind` has failed, use `+verbose` to obtain diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (BitVec.ofInt 4294967296 (-1)).toNat = n := by grind + +/-- +error: `grind` has failed, use `+verbose` to obtain diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (BitVec.allOnes 4294967296).toNat = n := by grind + +/-- +error: `grind` has failed, use `+verbose` to obtain diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (BitVec.replicate 4294967296 1#1).toNat = n := by grind + +/-- +error: `grind` has failed, use `+verbose` to obtain diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (BitVec.extractLsb' 0 4294967296 1#8).toNat = n := by grind + +/-- +error: `grind` has failed, use `+verbose` to obtain diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (BitVec.extractLsb 4294967296 1 1#8).toNat = n := by grind + +/-- +error: `grind` has failed, use `+verbose` to obtain diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (BitVec.ofNatClamp 4294967296 5).toNat = n := by grind + +/-- +error: `grind` has failed, use `+verbose` to obtain diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (BitVec.ofNat 4294967296 (2 + 3)).toNat = n := by grind + +/-- +error: `grind` has failed, use `+verbose` to obtain diagnostic information +-/ +#guard_msgs in +example (x : BitVec 4294967296) (n : Nat) : (BitVec.setWidth 8 1#4294967296).toNat = n := by grind + +#guard_msgs in +example (x : BitVec 8) (h : x = 255#8) : x + 1#8 = 0#8 := by grind + +#guard_msgs in +example (x : BitVec 8) (h : x = 255#8) : BitVec.setWidth 16 x = 255#16 := by grind
diff --git a/tests/elab/safeExpCap.lean b/tests/elab/safeExpCap.lean new file mode 100644 index 0000000..4b2cb4f --- /dev/null +++ b/tests/elab/safeExpCap.lean
@@ -0,0 +1,78 @@ +/-! +Exponents that do not fit in 32 bits are not evaluated, even if `exponentiation.threshold` is +increased. The runtime implementation of `Nat.pow` aborts the process on them. +-/ + +/-- +warning: exponent 4294967296 does not fit in 32 bits, exponentiation operation was not evaluated +--- +error: `simp` made no progress +-/ +#guard_msgs in +set_option exponentiation.threshold 5000000000 in +example (x : Nat) : 2 ^ 4294967296 = x := by simp + +/-- +warning: exponent 4294967296 does not fit in 32 bits, exponentiation operation was not evaluated +--- +error: `simp` made no progress +-/ +#guard_msgs in +set_option exponentiation.threshold 5000000000 in +example (x : Int) : (2 : Int) ^ 4294967296 = x := by simp + +/-- +warning: exponent 4294967296 does not fit in 32 bits, exponentiation operation was not evaluated +--- +error: `simp` made no progress +-/ +#guard_msgs in +set_option exponentiation.threshold 5000000000 in +example (x : Int) : (-2 : Int) ^ 4294967296 = x := by simp + +/-- +warning: exponent 4294967296 does not fit in 32 bits, exponentiation operation was not evaluated +--- +error: `simp` made no progress +-/ +#guard_msgs in +set_option exponentiation.threshold 5000000000 in +example (x : Nat) : Nat.powMod 2 4294967296 0 = x := by simp + +/-- +warning: exponent 4294967296 does not fit in 32 bits, exponentiation operation was not evaluated +--- +error: `simp` made no progress +-/ +#guard_msgs in +set_option exponentiation.threshold 5000000000 in +example (x : Nat) : x % 2 ^ 4294967296 = x := by simp + +/-- +error: `grind` has failed, use `+verbose` to obtain diagnostic information +-/ +#guard_msgs in +set_option exponentiation.threshold 5000000000 in +example (x : Nat) : 2 ^ 4294967296 = x := by grind + +/-- +error: `lia` has failed, use `+verbose` to obtain diagnostic information +-/ +#guard_msgs in +set_option exponentiation.threshold 5000000000 in +example (x : Nat) : x % 2 ^ 4294967296 = x := by lia + +/-- +error: `grind` has failed, use `+verbose` to obtain diagnostic information +-/ +#guard_msgs in +set_option exponentiation.threshold 5000000000 in +example (x : Rat) : (2 : Rat) ^ (-4294967296 : Int) = x := by grind + +#guard_msgs in +set_option exponentiation.threshold 5000 in +example : 2 ^ 5000 % 2 = 0 := by simp + +#guard_msgs in +set_option exponentiation.threshold 5000 in +example : 2 ^ 5000 % 2 = 0 := by rfl