Skip to content
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

bug: miscompilation in constant folding #4306

Closed
digama0 opened this issue May 30, 2024 · 0 comments · Fixed by #4311
Closed

bug: miscompilation in constant folding #4306

digama0 opened this issue May 30, 2024 · 0 comments · Fixed by #4311
Assignees
Labels
bug Something isn't working

Comments

@digama0
Copy link
Collaborator

digama0 commented May 30, 2024

This code is incorrect:

def getNumLit : Expr → Option Nat
| Expr.lit (Literal.natVal n) => some n
| Expr.app (Expr.const fn _) a => if isOfNat fn then getNumLit a else none
| _ => none

def foldToNat (_ : Bool) (a : Expr) : Option Expr := do
let n ← getNumLit a
return mkRawNatLit n
def uintFoldToNatFns : List (Name × UnFoldFn) :=
numScalarTypes.foldl (fun r info => (info.toNatFn, foldToNat) :: r) []

It performs constant folding for UIntN.toNat using getNumLit, but getNumLit recognizes expressions like UIntN.ofNat M where M is larger than the bit size without performing modular reduction.

This results in a miscompilation / native_reduce proof of false:

theorem false : False := by
  have : (123456789012345678901).toUInt64.toNat = 123456789012345678901 := by native_decide
  simp [Nat.toUInt64] at this

Reported on Zulip.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants