NumberHelperEightDigitWordAccepted

Lean checked

Statement

This explanation was written alongside the formalization to describe the statement and its reasoning.

Helper · Eight decimal bytes satisfy the word predicate

If every one of the eight little-endian bytes of a 64-bit word w lies in [48,57], then the extracted eight-digit predicate accepts w.

Lean statement

Trellis authored these statements and proofs. Lean checks the formal proofs; the assumptions used by each proof are listed below.

import Mathlib.Tactic.IntervalCases
import Tablet.num_imp_dec2flt_common_is_8digits

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option maxHeartbeats 2000000

namespace dec2flt_model

-- [TABLET NODE: NumberHelperEightDigitWordAccepted]
theorem NumberHelperEightDigitWordAccepted (w : Std.U64)
    (hd : forall i : Nat, i < 8 ->
      48 <= (w.val / (256^i)) % 256 ∧
        (w.val / (256^i)) % 256 <= 57) :
    num.imp.dec2flt.common.is_8digits w = ok true := by
Proof

Natural-language proof

The status above describes the Lean proof. Its explanatory prose may remain a sketch after that proof closes.

Explanatory prose status

Prose retained as authored. The explanatory prose is preserved as it stood when captured.

Proof

Unfold num imp dec2flt common is 8digits. For each prefix of k=1,\ldots,8 bytes, the hypothesis packs that prefix between the low 8k bits of 0x3030303030303030 and 0x3939393939393939. This follows successively from the positional identity between division by 256^{k-1} and the next byte.
Adding the low 8k bits of 0x4646464646464646 to the upper packed bound produces at most the k-byte word consisting entirely of 0x7f; in particular it is below 2^{8k-1} and no carry reaches the next byte. Subtracting the low 8k bits of 0x3030303030303030 from the lower bound is nonnegative and the largest possible difference is the k-byte word consisting entirely of 0x09, again below 2^{8k-1}. Therefore the high bit of byte k-1 is zero in both wrapping operations. This checks all eight byte boundaries, including every possible incoming carry or borrow.
The mask 0x8080808080808080 has set bits only at those eight high bit positions. At each such position both operands of the bitwise OR are zero, while every other position is cleared by the mask. Hence the masked word is zero, so the extracted equality test returns true and the predicate returns ok true.

Lean proof


  have h0 := hd 0 (by omega)
  have h1 := hd 1 (by omega)
  have hp1 : 48 <= w.val % 256 ∧ w.val % 256 <= 57 := by simpa using h0
  have hp2 : 12336 <= w.val % 65536 ∧ w.val % 65536 <= 14649 := by omega
  have h2 := hd 2 (by omega)
  have hp3 : 3158064 <= w.val % 16777216 ∧
      w.val % 16777216 <= 3750201 := by omega
  have h3 := hd 3 (by omega)
  have hp4 : 808464432 <= w.val % 4294967296 ∧
      w.val % 4294967296 <= 960051513 := by omega
  have h4 := hd 4 (by omega)
  have hp5 : 206966894640 <= w.val % 1099511627776 ∧
      w.val % 1099511627776 <= 245773187385 := by omega
  have h5 := hd 5 (by omega)
  have hp6 : 52983525027888 <= w.val % 281474976710656 ∧
      w.val % 281474976710656 <= 62917935970617 := by omega
  have h6 := hd 6 (by omega)
  have hp7 : 13563782407139376 <= w.val % 72057594037927936 ∧
      w.val % 72057594037927936 <= 16106991608478009 := by omega
  have h7 := hd 7 (by omega)
  have hw : w.val < 18446744073709551616 := by scalar_tac
  have hp8 : 3472328296227680304 <= w.val ∧
      w.val <= 4123389851770370361 := by omega
  have hmsbs : forall i : Nat, i < 8 ->
      (BitVec.extractLsb' 0 (8*(i+1))
        (w.bv + 5063812098665367110#64)).msb = false ∧
      (BitVec.extractLsb' 0 (8*(i+1))
        (w.bv - 3472328296227680304#64)).msb = false := by
    intro i hi
    have o := operationValues w i hi
    have c := constantValues i hi
    have hi_cases : i = 0 ∨ i = 1 ∨ i = 2 ∨ i = 3 ∨ i = 4 ∨
        i = 5 ∨ i = 6 ∨ i = 7 := by omega
    rcases hi_cases with rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl <;>
      constructor
    all_goals rw [BitVec.msb_eq_false_iff_two_mul_lt]
    all_goals first
      | rw [o.1, prefixValue, c.1, c.2.2.1]
      | rw [o.2, prefixValue, c.2.1, c.2.2.1]
    all_goals norm_num only at *
    all_goals omega
  unfold num.imp.dec2flt.common.is_8digits
  simp [Aeneas.Std.lift, core.num.U64.wrapping_add,
    core.num.U64.wrapping_sub, HOr.hOr, HAnd.hAnd, UScalar.or, UScalar.and]
  apply UScalar.eq_of_val_eq
  change ((((w.bv + 5063812098665367110#64) |||
    (w.bv - 3472328296227680304#64)) &&& 9259542123273814144#64).toNat = 0)
  have hb0 := hmsbs 0 (by omega)
  have hb1 := hmsbs 1 (by omega)
  have hb2 := hmsbs 2 (by omega)
  have hb3 := hmsbs 3 (by omega)
  have hb4 := hmsbs 4 (by omega)
  have hb5 := hmsbs 5 (by omega)
  have hb6 := hmsbs 6 (by omega)
  have hb7 := hmsbs 7 (by omega)
  simp only [BitVec.msb, BitVec.getMsbD_eq_getLsbD,
    BitVec.getLsbD_extractLsb'] at hb0 hb1 hb2 hb3 hb4 hb5 hb6 hb7
  norm_num at hb0 hb1 hb2 hb3 hb4 hb5 hb6 hb7
  change ((((w.bv + 5063812098665367110#64) |||
    (w.bv - 3472328296227680304#64)) &&& 9259542123273814144#64).toNat =
      (0#64).toNat)
  apply (BitVec.toNat_eq).mp
  apply BitVec.eq_of_getLsbD_eq
  intro j hj
  interval_cases j <;>
    simp_all [BitVec.getLsbD_and, BitVec.getLsbD_or, hb0, hb1, hb2, hb3,
      hb4, hb5, hb6, hb7]
where
  operationValues (w : Std.U64) (i : Nat) (hi : i < 8) :
      (BitVec.extractLsb' 0 (8*(i+1))
        (w.bv + 5063812098665367110#64)).toNat =
          ((BitVec.extractLsb' 0 (8*(i+1)) w.bv).toNat +
            (BitVec.extractLsb' 0 (8*(i+1))
              (5063812098665367110#64)).toNat) % 2^(8*(i+1)) ∧
      (BitVec.extractLsb' 0 (8*(i+1))
        (w.bv - 3472328296227680304#64)).toNat =
          ((BitVec.extractLsb' 0 (8*(i+1)) w.bv).toNat +
            (BitVec.extractLsb' 0 (8*(i+1))
              (-3472328296227680304#64)).toNat) % 2^(8*(i+1)) := by
    have hk : 8*(i+1) <= 64 := by omega
    constructor
    · rw [BitVec.extractLsb'_add hk, BitVec.toNat_add]
    · rw [sub_eq_add_neg, BitVec.extractLsb'_add hk, BitVec.toNat_add]

  prefixValue (w : Std.U64) (k : Nat) :
      (BitVec.extractLsb' 0 k w.bv).toNat = w.val % 2^k := by
    rw [BitVec.extractLsb'_toNat]
    simp [Nat.shiftRight_eq_div_pow]

  constantValues : forall i : Nat, i < 8 ->
      (BitVec.extractLsb' 0 (8*(i+1)) (5063812098665367110#64)).toNat =
        (match i with
        | 0 => 70 | 1 => 17990 | 2 => 4605510 | 3 => 1179010630
        | 4 => 301826721350 | 5 => 77267640665670
        | 6 => 19780516010411590 | _ => 5063812098665367110) ∧
      (BitVec.extractLsb' 0 (8*(i+1))
        (-3472328296227680304#64)).toNat =
        (match i with
        | 0 => 208 | 1 => 53200 | 2 => 13619152 | 3 => 3486502864
        | 4 => 892544733136 | 5 => 228491451682768
        | 6 => 58493811630788560 | _ => 14974415777481871312) ∧
      2^(8*(i+1)) =
        (match i with
        | 0 => 256 | 1 => 65536 | 2 => 16777216 | 3 => 4294967296
        | 4 => 1099511627776 | 5 => 281474976710656
        | 6 => 72057594037927936 | _ => 18446744073709551616) ∧
      2^(8*(i+1)-1) =
        (match i with
        | 0 => 128 | 1 => 32768 | 2 => 8388608 | 3 => 2147483648
        | 4 => 549755813888 | 5 => 140737488355328
        | 6 => 36028797018963968 | _ => 9223372036854775808) := by
    intro i hi
    have hi_cases : i = 0 ∨ i = 1 ∨ i = 2 ∨ i = 3 ∨ i = 4 ∨
        i = 5 ∨ i = 6 ∨ i = 7 := by omega
    rcases hi_cases with rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl <;>
      decide +kernel

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Mathlib.Tactic.IntervalCases
import Tablet.num_imp_dec2flt_common_is_8digits

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option maxHeartbeats 2000000

namespace dec2flt_model

-- [TABLET NODE: NumberHelperEightDigitWordAccepted]
theorem NumberHelperEightDigitWordAccepted (w : Std.U64)
    (hd : forall i : Nat, i < 8 ->
      48 <= (w.val / (256^i)) % 256 ∧
        (w.val / (256^i)) % 256 <= 57) :
    num.imp.dec2flt.common.is_8digits w = ok true := by
-- BODY
  have h0 := hd 0 (by omega)
  have h1 := hd 1 (by omega)
  have hp1 : 48 <= w.val % 256 ∧ w.val % 256 <= 57 := by simpa using h0
  have hp2 : 12336 <= w.val % 65536 ∧ w.val % 65536 <= 14649 := by omega
  have h2 := hd 2 (by omega)
  have hp3 : 3158064 <= w.val % 16777216 ∧
      w.val % 16777216 <= 3750201 := by omega
  have h3 := hd 3 (by omega)
  have hp4 : 808464432 <= w.val % 4294967296 ∧
      w.val % 4294967296 <= 960051513 := by omega
  have h4 := hd 4 (by omega)
  have hp5 : 206966894640 <= w.val % 1099511627776 ∧
      w.val % 1099511627776 <= 245773187385 := by omega
  have h5 := hd 5 (by omega)
  have hp6 : 52983525027888 <= w.val % 281474976710656 ∧
      w.val % 281474976710656 <= 62917935970617 := by omega
  have h6 := hd 6 (by omega)
  have hp7 : 13563782407139376 <= w.val % 72057594037927936 ∧
      w.val % 72057594037927936 <= 16106991608478009 := by omega
  have h7 := hd 7 (by omega)
  have hw : w.val < 18446744073709551616 := by scalar_tac
  have hp8 : 3472328296227680304 <= w.val ∧
      w.val <= 4123389851770370361 := by omega
  have hmsbs : forall i : Nat, i < 8 ->
      (BitVec.extractLsb' 0 (8*(i+1))
        (w.bv + 5063812098665367110#64)).msb = false ∧
      (BitVec.extractLsb' 0 (8*(i+1))
        (w.bv - 3472328296227680304#64)).msb = false := by
    intro i hi
    have o := operationValues w i hi
    have c := constantValues i hi
    have hi_cases : i = 0 ∨ i = 1 ∨ i = 2 ∨ i = 3 ∨ i = 4 ∨
        i = 5 ∨ i = 6 ∨ i = 7 := by omega
    rcases hi_cases with rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl <;>
      constructor
    all_goals rw [BitVec.msb_eq_false_iff_two_mul_lt]
    all_goals first
      | rw [o.1, prefixValue, c.1, c.2.2.1]
      | rw [o.2, prefixValue, c.2.1, c.2.2.1]
    all_goals norm_num only at *
    all_goals omega
  unfold num.imp.dec2flt.common.is_8digits
  simp [Aeneas.Std.lift, core.num.U64.wrapping_add,
    core.num.U64.wrapping_sub, HOr.hOr, HAnd.hAnd, UScalar.or, UScalar.and]
  apply UScalar.eq_of_val_eq
  change ((((w.bv + 5063812098665367110#64) |||
    (w.bv - 3472328296227680304#64)) &&& 9259542123273814144#64).toNat = 0)
  have hb0 := hmsbs 0 (by omega)
  have hb1 := hmsbs 1 (by omega)
  have hb2 := hmsbs 2 (by omega)
  have hb3 := hmsbs 3 (by omega)
  have hb4 := hmsbs 4 (by omega)
  have hb5 := hmsbs 5 (by omega)
  have hb6 := hmsbs 6 (by omega)
  have hb7 := hmsbs 7 (by omega)
  simp only [BitVec.msb, BitVec.getMsbD_eq_getLsbD,
    BitVec.getLsbD_extractLsb'] at hb0 hb1 hb2 hb3 hb4 hb5 hb6 hb7
  norm_num at hb0 hb1 hb2 hb3 hb4 hb5 hb6 hb7
  change ((((w.bv + 5063812098665367110#64) |||
    (w.bv - 3472328296227680304#64)) &&& 9259542123273814144#64).toNat =
      (0#64).toNat)
  apply (BitVec.toNat_eq).mp
  apply BitVec.eq_of_getLsbD_eq
  intro j hj
  interval_cases j <;>
    simp_all [BitVec.getLsbD_and, BitVec.getLsbD_or, hb0, hb1, hb2, hb3,
      hb4, hb5, hb6, hb7]
where
  operationValues (w : Std.U64) (i : Nat) (hi : i < 8) :
      (BitVec.extractLsb' 0 (8*(i+1))
        (w.bv + 5063812098665367110#64)).toNat =
          ((BitVec.extractLsb' 0 (8*(i+1)) w.bv).toNat +
            (BitVec.extractLsb' 0 (8*(i+1))
              (5063812098665367110#64)).toNat) % 2^(8*(i+1)) ∧
      (BitVec.extractLsb' 0 (8*(i+1))
        (w.bv - 3472328296227680304#64)).toNat =
          ((BitVec.extractLsb' 0 (8*(i+1)) w.bv).toNat +
            (BitVec.extractLsb' 0 (8*(i+1))
              (-3472328296227680304#64)).toNat) % 2^(8*(i+1)) := by
    have hk : 8*(i+1) <= 64 := by omega
    constructor
    · rw [BitVec.extractLsb'_add hk, BitVec.toNat_add]
    · rw [sub_eq_add_neg, BitVec.extractLsb'_add hk, BitVec.toNat_add]

  prefixValue (w : Std.U64) (k : Nat) :
      (BitVec.extractLsb' 0 k w.bv).toNat = w.val % 2^k := by
    rw [BitVec.extractLsb'_toNat]
    simp [Nat.shiftRight_eq_div_pow]

  constantValues : forall i : Nat, i < 8 ->
      (BitVec.extractLsb' 0 (8*(i+1)) (5063812098665367110#64)).toNat =
        (match i with
        | 0 => 70 | 1 => 17990 | 2 => 4605510 | 3 => 1179010630
        | 4 => 301826721350 | 5 => 77267640665670
        | 6 => 19780516010411590 | _ => 5063812098665367110) ∧
      (BitVec.extractLsb' 0 (8*(i+1))
        (-3472328296227680304#64)).toNat =
        (match i with
        | 0 => 208 | 1 => 53200 | 2 => 13619152 | 3 => 3486502864
        | 4 => 892544733136 | 5 => 228491451682768
        | 6 => 58493811630788560 | _ => 14974415777481871312) ∧
      2^(8*(i+1)) =
        (match i with
        | 0 => 256 | 1 => 65536 | 2 => 16777216 | 3 => 4294967296
        | 4 => 1099511627776 | 5 => 281474976710656
        | 6 => 72057594037927936 | _ => 18446744073709551616) ∧
      2^(8*(i+1)-1) =
        (match i with
        | 0 => 128 | 1 => 32768 | 2 => 8388608 | 3 => 2147483648
        | 4 => 549755813888 | 5 => 140737488355328
        | 6 => 36028797018963968 | _ => 9223372036854775808) := by
    intro i hi
    have hi_cases : i = 0 ∨ i = 1 ∨ i = 2 ∨ i = 3 ∨ i = 4 ∨
        i = 5 ∨ i = 6 ∨ i = 7 := by omega
    rcases hi_cases with rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl <;>
      decide +kernel

end dec2flt_model

Proof structure

Follow the declared dependencies to read the supporting statements and proofs.

Explore this dependency tree

Inspect related source (45)
More program dependencies (12)
Standard-library dependencies (30)

Related source and models come from the extraction records and declared dependencies.

Proof assumptions and review

Lean checks the formal proof. Trellis also reviews how the statement corresponds to the intended requirement.

Transitive assumptions

This list combines the assumptions recorded for this declaration and its dependencies. It may include assumptions used by auxiliary declarations in those modules.

Helper theorems included
Technical checking records
Local closure summary
{
  "node": "NumberHelperEightDigitWordAccepted",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "ea9eba97646c690a76a57c8604357296e11ba522835bb867afb658e4355415e9",
  "active_statement_hash": "cae6852f35feac848a3d3921c2dbbffa0a4401b353a6046876ad32e31e4bfa61",
  "kernel_axioms": [
    "Classical.choice",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "d5c04ffa333d9b33b238a527f5beaa2be82e021d3e9c0bbe01b1310280f7f55a",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-68",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "num_imp_dec2flt_common_is_8digits": "effb8a26907859a5"
  },
  "strict_definition_deps": {
    "num_imp_dec2flt_common_is_8digits": "53e9de5212d69bf5"
  }
}
Prose assessment
{
  "label": "Lean proof closed; predictive prose review no longer required",
  "assessment_status": null,
  "fingerprint_relation": "unknown",
  "assessment_fingerprint": null,
  "current_fingerprint": "sound-v2:0c62cb720681d67ed19d94faf9154078c2c956797934e7d3cf503d4e7b4990c8",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}