Section EQ_MODULO.
Variable modul: Z.
Hypothesis modul_pos: modul > 0.
Definition eqmod (x y: Z) : Prop := exists k, x = k * modul + y.
End EQ_MODULO.
(** ** Fast normalization modulo [2^n] *)
Fixpoint P_mod_two_p (p: positive) (n: nat) {struct n} : Z :=
match n with
| O => 0
| S m =>
match p with
| xH => 1
| xO q => Z.double (P_mod_two_p q m)
| xI q => Z.succ_double (P_mod_two_p q m)
end
end.
Definition Z_mod_two_p (x: Z) (n: nat) : Z :=
match x with
| Z0 => 0
| Zpos p => P_mod_two_p p n
| Zneg p => let r := P_mod_two_p p n in if zeq r 0 then 0 else two_power_nat n - r
end.

Lemma Z_mod_two_p_range:
forall n x, 0 <= Z_mod_two_p x n < two_power_nat n.