Documentation

Complexitylib.Mathlib.NatBits

Fixed-width binary encodings of natural numbers #

Big-endian, fixed-width binary encoding Nat.toBits with its exact decoder Nat.fromBits and round-trip lemmas. Values wider than the target width are truncated modulo 2 ^ w.

This file lives in Complexitylib/Mathlib/ because it extends a Mathlib type in its home (root) namespace — the sanctioned exception to the Complexity root-namespace rule. Its contents are candidates for upstreaming to Mathlib.

def Nat.toBits :
List Bool

Encode a natural number as a big-endian binary list of exactly w bits. Numbers larger than 2^w - 1 are truncated (mod 2^w).

Equations
Instances For
    theorem Nat.length_toBits (w val : ) :
    (w.toBits val).length = w

    Decode a big-endian binary list to a natural number.

    Equations
    Instances For

      Decoded values are bounded by 2 ^ length.

      theorem Nat.fromBits_toBits_mod (w val : ) :
      fromBits (w.toBits val) = val % 2 ^ w

      fromBitstoBits w reduces any input modulo 2 ^ w.

      theorem Nat.fromBits_toBits {w val : } (hv : val < 2 ^ w) :
      fromBits (w.toBits val) = val

      Nat.fromBits is a left inverse of Nat.toBits on values below 2 ^ w.