(** Explicit formulas for carry bits during addition. Conceptually, the theory
 * here matches the bitblasting rules for integers. However, the vector of
 * carry bits is represented as a [Z] so it can be used in bitwise operations.
 * The last three lemmas about [addcarries] are the main interface, but the
 * generalization [adccarries] is provided as the same theory applies. *)

Definition adccarries (x y : Z) (c : bool) := (x + y + Z.b2z c) .^ (x .^ y).Definition addcarries (x y : Z)            := (x + y)           .^ (x .^ y).

Lemma testbit_addcarries_pos x y i (Hi : 0 < i) : Z.testbit (addcarries x y) i = (x.[i-1] && y.[i-1] || (x.[i-1]^^y.[i-1]) && (addcarries x y).[i-1])%bool.