Documentation

CslibFrontier.Foundations.Data.Nat.Fields

Packing numbers into fields #

A low field x < m and a high field y are packed into the number x + m * y. Reducing modulo m recovers x (Nat.add_mul_mod_of_lt), and dividing by m recovers y (Nat.add_mul_div_left_of_lt). For fields of a and b bits, the packed number has a + b bits (Nat.add_two_pow_mul_lt). Packing a packed number again as the high field gives any fixed number of fields, which these facts decode one at a time.

Main results #

theorem Nat.add_mul_mod_of_lt {x m : ℕ} (y : ℕ) (hx : x < m) :
(x + m * y) % m = x

Reducing the packed number x + m * y modulo m gives the low field x < m.

theorem Nat.add_mul_div_left_of_lt {x m : ℕ} (y : ℕ) (hx : x < m) :
(x + m * y) / m = y

Dividing the packed number x + m * y by m gives the high field y, for x < m.

theorem Nat.add_two_pow_mul_lt {a b x y : ℕ} (hx : x < 2 ^ a) (hy : y < 2 ^ b) :
x + 2 ^ a * y < 2 ^ (a + b)

Fields x < 2 ^ a and y < 2 ^ b pack into a number x + 2 ^ a * y of a + b bits.