Documentation

Mathlib.Data.Bitvec.Core

Basic operations on bitvectors #

This is a work-in-progress, and contains additions to other theories.

This file was moved to mathlib from core Lean in the switch to Lean 3.20.0c. It is not fully in compliance with mathlib style standards.

def Bitvec (n : ) :

Bitvec n is a Vector of Bool with length n.

Equations
def Bitvec.zero (n : ) :

Create a zero bitvector

Equations
def Bitvec.one (n : ) :

Create a bitvector of length n whose n-1st entry is 1 and other entries are 0.

Equations
def Bitvec.cong {a : } {b : } (h : a = b) :
Bitvec aBitvec b

Create a bitvector from another with a provably equal length.

Equations
def Bitvec.append {m : } {n : } :
Bitvec mBitvec nBitvec (m + n)

Bitvec specific version of Vector.append

Equations
  • Bitvec.append = Vector.append

Shift operations #

def Bitvec.shl {n : } (x : Bitvec n) (i : ) :

shl x i is the bitvector obtained by left-shifting x i times and padding with false. If x.length < i then this will return the all-falses bitvector.

Equations
def Bitvec.fillShr {n : } (x : Bitvec n) (i : ) (fill : Bool) :

fill_shr x i fill is the bitvector obtained by right-shifting x i times and then padding with fill : Bool. If x.length < i then this will return the constant fill bitvector.

Equations
def Bitvec.ushr {n : } (x : Bitvec n) (i : ) :

unsigned shift right

Equations
def Bitvec.sshr {m : } :
Bitvec mBitvec m

signed shift right

Equations

Bitwise operations #

def Bitvec.not {n : } (bv : Bitvec n) :

bitwise not

Equations
def Bitvec.and {n : } :
Bitvec nBitvec nBitvec n

bitwise and

Equations
def Bitvec.or {n : } :
Bitvec nBitvec nBitvec n

bitwise or

Equations
def Bitvec.xor {n : } :
Bitvec nBitvec nBitvec n

bitwise xor

Equations

Arithmetic operators #

def Bitvec.xor3 (x : Bool) (y : Bool) (c : Bool) :

xor3 x y c is ((x XOR y) XOR c).

Equations
def Bitvec.carry (x : Bool) (y : Bool) (c : Bool) :

carry x y c is x && y || x && c || y && c.

Equations
def Bitvec.neg {n : } (x : Bitvec n) :

neg x is the two's complement of x.

Equations
def Bitvec.adc {n : } (x : Bitvec n) (y : Bitvec n) (c : Bool) :
Bitvec (n + 1)

Add with carry (no overflow)

Equations
def Bitvec.add {n : } (x : Bitvec n) (y : Bitvec n) :

The sum of two bitvectors

Equations
def Bitvec.sbb {n : } (x : Bitvec n) (y : Bitvec n) (b : Bool) :

Subtract with borrow

Equations
def Bitvec.sub {n : } (x : Bitvec n) (y : Bitvec n) :

The difference of two bitvectors

Equations
instance Bitvec.instZeroBitvec {n : } :
Equations
instance Bitvec.instOneBitvec {n : } :
Equations
instance Bitvec.instAddBitvec {n : } :
Equations
  • Bitvec.instAddBitvec = { add := Bitvec.add }
instance Bitvec.instSubBitvec {n : } :
Equations
  • Bitvec.instSubBitvec = { sub := Bitvec.sub }
instance Bitvec.instNegBitvec {n : } :
Equations
  • Bitvec.instNegBitvec = { neg := Bitvec.neg }
def Bitvec.mul {n : } (x : Bitvec n) (y : Bitvec n) :

The product of two bitvectors

Equations
instance Bitvec.instMulBitvec {n : } :
Equations
  • Bitvec.instMulBitvec = { mul := Bitvec.mul }

Comparison operators #

def Bitvec.uborrow {n : } (x : Bitvec n) (y : Bitvec n) :

uborrow x y returns true iff the "subtract with borrow" operation on x, y and false required a borrow.

Equations
def Bitvec.Ult {n : } (x : Bitvec n) (y : Bitvec n) :

unsigned less-than proposition

Equations
def Bitvec.Ugt {n : } (x : Bitvec n) (y : Bitvec n) :

unsigned greater-than proposition

Equations
def Bitvec.Ule {n : } (x : Bitvec n) (y : Bitvec n) :

unsigned less-than-or-equal-to proposition

Equations
def Bitvec.Uge {n : } (x : Bitvec n) (y : Bitvec n) :

unsigned greater-than-or-equal-to proposition

Equations
def Bitvec.sborrow {n : } :
Bitvec nBitvec nBool

sborrow x y returns true iff x < y as two's complement integers

Equations
  • One or more equations did not get rendered due to their size.
def Bitvec.Slt {n : } (x : Bitvec n) (y : Bitvec n) :

signed less-than proposition

Equations
def Bitvec.Sgt {n : } (x : Bitvec n) (y : Bitvec n) :

signed greater-than proposition

Equations
def Bitvec.Sle {n : } (x : Bitvec n) (y : Bitvec n) :

signed less-than-or-equal-to proposition

Equations
def Bitvec.Sge {n : } (x : Bitvec n) (y : Bitvec n) :

signed greater-than-or-equal-to proposition

Equations

Conversion to nat and int #

def Bitvec.ofNat (n : ) :
Bitvec n

Create a bitvector from a nat

Equations
def Bitvec.ofInt (n : ) :

Create a bitvector in the two's complement representation from an int

Equations
def Bitvec.addLsb (r : ) (b : Bool) :

add_lsb r b is r + r + 1 if b is true and r + r otherwise.

Equations

Given a List of Bools, return the nat they represent as a list of binary digits.

Equations
def Bitvec.toNat {n : } (v : Bitvec n) :

Return the natural number encoded by the input bitvector

Equations
theorem Bitvec.toNat_append {m : } (xs : Bitvec m) (b : Bool) :
Bitvec.toNat (Vector.append xs (b ::ᵥ Vector.nil)) = Bitvec.toNat xs * 2 + Bitvec.toNat (b ::ᵥ Vector.nil)
theorem Bitvec.bits_toNat_decide (n : ) :
Bitvec.toNat (decide (n % 2 = 1) ::ᵥ Vector.nil) = n % 2
theorem Bitvec.ofNat_succ {k : } {n : } :
Bitvec.ofNat (Nat.succ k) n = Vector.append (Bitvec.ofNat k (n / 2)) (decide (n % 2 = 1) ::ᵥ Vector.nil)
theorem Bitvec.toNat_ofNat {k : } {n : } :
def Bitvec.toInt {n : } :
Bitvec n

Return the integer encoded by the input bitvector

Equations
  • One or more equations did not get rendered due to their size.

Miscellaneous instances #

instance Bitvec.instReprBitvec (n : ) :
Equations
instance instDecidableUlt {n : } {x : Bitvec n} {y : Bitvec n} :
Equations
instance instDecidableUgt {n : } {x : Bitvec n} {y : Bitvec n} :
Equations