Skip to content

Kernel accepts theorem with oversized projection index due to unsigned truncation #13602

Closed as duplicate

Description

@trackoor

Prerequisites

Description

Lean accepts a checked theorem declaration containing an oversized projection index. The projection index is a Nat, but the kernel paths convert it to C++ unsigned after checking only is_small(). This lets an invalid projection reduce as if it were a valid field projection.

The reproducer proves, without axioms:

{ first := 0, second := 1 }.4294967298 = 1

The same file confirms that the raw natural-number equality 4294967297 = 1 is rejected, so the issue is specific to projection handling.

Context

This appears to affect multiple kernel paths, not just one projection function:

  • src/kernel/type_checker.cpp:221 (infer_proj)
  • src/kernel/type_checker.cpp:377 (reduce_proj)
  • src/kernel/type_checker.cpp:1008 (lazy_delta_proj_reduction)

Each path checks is_small() and then calls get_small_value() into an unsigned.

Steps to Reproduce

  1. Save this file as ProjectionOverflowFieldEquality.lean:
import Lean

open Lean

namespace ProjectionOverflowFieldEquality

def hugeIdx : Nat := 4294967297

structure PairBox where
  first : Nat
  second : Nat

run_elab
  let boxValue :=
    mkApp2 (mkConst ``PairBox.mk [])
      (mkNatLit 0)
      (mkNatLit 1)
  let hugeProjection := mkProj ``PairBox hugeIdx boxValue
  let eqType :=
    mkApp3 (mkConst ``Eq [levelOne])
      (mkConst ``Nat)
      hugeProjection
      (mkNatLit 1)
  let eqProof :=
    mkApp2 (mkConst ``Eq.refl [levelOne])
      (mkConst ``Nat)
      (mkNatLit 1)
  let decl := Declaration.thmDecl {
    name := `ProjectionOverflowFieldEquality.hugeProjection_eq_one
    levelParams := []
    type := eqType
    value := eqProof
  }
  match (← getEnv).addDeclCore (doCheck := true) 0 decl none with
  | .ok env => modifyEnv fun _ => env
  | .error ex => throwError "{ex.toMessageData {}}"

run_elab
  let natEqType :=
    mkApp3 (mkConst ``Eq [levelZero])
      (mkConst ``Nat)
      (mkNatLit hugeIdx)
      (mkNatLit 1)
  let bogusProof :=
    mkApp2 (mkConst ``Eq.refl [levelZero])
      (mkConst ``Nat)
      (mkNatLit 1)
  let decl := Declaration.thmDecl {
    name := `ProjectionOverflowFieldEquality.hugeIndex_eq_one
    levelParams := []
    type := natEqType
    value := bogusProof
  }
  match (← getEnv).addDeclCore (doCheck := true) 0 decl none with
  | .ok _ => throwError "unexpectedly accepted Nat index equality"
  | .error _ => logInfo "checked Nat index equality rejected"

#check ProjectionOverflowFieldEquality.hugeProjection_eq_one
#print axioms ProjectionOverflowFieldEquality.hugeProjection_eq_one

end ProjectionOverflowFieldEquality
  1. Run:
lean --trust=0 ProjectionOverflowFieldEquality.lean

Expected behavior: The theorem declaration using .4294967298 should be rejected because the projection index is not a valid field index for PairBox.

Actual behavior: Lean accepts the theorem and reports no axioms:

checked Nat index equality rejected
ProjectionOverflowFieldEquality.hugeProjection_eq_one :
  { first := 0, second := 1 }.4294967298 = 1
'ProjectionOverflowFieldEquality.hugeProjection_eq_one' does not depend on any axioms

Versions

Lean (version 4.31.0-pre, arm64-apple-darwin25.3.0, commit ref: refs/heads/master, Release)

Tested commit:

659249a7df544a0507137f5869d44b23f1951f78

OS:

macOS arm64

Additional Information

Suggested fix: use one shared checked conversion for projection indices in all kernel projection paths. It should reject indices outside the C++ unsigned range and reject indices outside the actual field range before they are used by type inference, reduction, or defeq shortcuts.

Impact

This is a checked-kernel correctness issue: a theorem declaration is accepted even though its type contains an invalid projection index that should not be reducible as a valid field projection.

Activity

  1. added
    bug
    on May 2, 2026
  2. nomeata commented on May 2, 2026

    @nomeata
    Collaborator

    Duplicate of #12746, right?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions