Documentation

Complexity.Foundations.Data.BitString.List.Binary

Saturating binary decoding #

Words are read most significant bit first. Saturating at every step keeps a unary accumulator bounded on arbitrary input words; it agrees with capping the full mathematical binary value.

Main definitions #

Main results #

References #

Adapted from Probability/UniformNat.lean on the cslib crypto branch at 5ff69ca93402e7c947208dc56396d6d86203ccd8.

def Cslib.BitString.lowBits (width value : ℕ) :

The first width digits of a natural, least significant first. Unlike binaryWord, this always has the declared width: high digits are truncated and absent digits are false.

Instances For
    @[simp]
    theorem Cslib.BitString.length_lowBits (width value : ℕ) :
    (lowBits width value).length = width

    lowBits width value has width digits.

    theorem Cslib.BitString.lowBits_eq_ofFn (width value : ℕ) :
    lowBits width value = List.ofFn fun (i : Fin width) => value.testBit ↑i

    The executable digit loop agrees with the finite coordinate view.

    The natural number represented by a word, most significant bit first.

    Instances For

      Decode binary digits with a saturating accumulator.

      Instances For
        theorem Cslib.BitString.boundedBinaryValue_eq (bound : ℕ) (word : List Bool) :
        boundedBinaryValue bound word = min bound (binaryValue word)

        Saturating at each step agrees with capping the final mathematical value.

        theorem Cslib.BitString.boundedBinaryValue_le (bound : ℕ) (word : List Bool) :
        boundedBinaryValue bound word ≤ bound

        The unary result never exceeds the declared bound.

        theorem Cslib.BitString.binaryValue_snoc (word : List Bool) (bit : Bool) :
        binaryValue (word ++ [bit]) = 2 * binaryValue word + bit.toNat

        Append a low-order bit to a most-significant-first binary word.

        Reversing the little-endian digits gives the corresponding most-significant-first value.

        An all-one binary word represents one less than the corresponding power of two.

        A word of n bits represents a number below 2 ^ n.

        Leading zeros do not change the represented number.

        The word of width w representing a in binary, most significant bit first, padded with leading zeros; it has width w when a < 2 ^ w (length_binaryWord).

        Instances For
          theorem Cslib.BitString.length_binaryWord {w a : ℕ} (ha : a < 2 ^ w) :

          The word of width w representing a has w bits when a < 2 ^ w.

          @[simp]

          The word binaryWord w a represents a, whatever the width w.

          theorem Cslib.BitString.exists_binaryValue_eq {t k : ℕ} (hk : k < 2 ^ t) :
          ∃ (u : Fin t → Bool), binaryValue (List.ofFn u) = k

          Every number below 2 ^ t is represented by a word of t bits.

          Fixed-width binary words represent their values injectively.