Section parm_sec.
Variable M_in_Z_ : Z_ (2 ^ w).
Variable ptM'cM'' : Z_ w * Z_ w * Z_ (2 ^ w) * Z_ (2 ^ w) * Z_ (2 ^ w).
Variable d_Sir_Si : Z_ (2 ^ w) * Z_ (2 ^ w).
Variable l_in_Z_ : Z_ (2 ^ w).
Variable x_i_in_Z_ : Z_ (2 ^ w).
Variable h : Z.
Let p_in_Z_w := fst (fst (fst (fst ptM'cM''))).
Let t_in_Z_w := snd (fst (fst (fst ptM'cM''))).
Let M'_in_Z_ := snd (fst (fst ptM'cM'')).
Let c_in_Z_ := snd (fst ptM'cM'').
Let M''_in_Z_ := snd ptM'cM''.
Let M := _Z M_in_Z_.
Let p := _Z p_in_Z_w.
Let t := _Z t_in_Z_w.
Let c := _Z c_in_Z_.
Let M'' := _Z M''_in_Z_.
Let M' := _Z M'_in_Z_.
Let l := _Z l_in_Z_.
Let x_i := _Z x_i_in_Z_.
Definition d_Si := fst d_Sir_Si.
Definition r_Si := snd d_Sir_Si.
Hypothesis d_Si_eq_r_Si_eq : P M p d_Si r_Si h l_in_Z_.
Hypothesis le_1_M : 1 <= M.
Hypothesis le_M_max : M <= 2 ^ (w - 1).
Hypothesis p_M : p = Zlog_sup M.
Hypothesis t_M : t = w - p \/ M = 1.
Hypothesis c_M : c = 2 ^ p.
Hypothesis M''_M : M'' = (2 ^ t * M) mod 2 ^ w.
Hypothesis M'_M : M' = 2 ^ (p + w) / M mod 2 ^ w.
unfold p. apply le_0__Z. Qed.
apply Zlt_le_weak. exact lt02w. Qed.
Local Hint Resolve le_0_w lt02w le02w : a.
Local Hint Resolve le_0_p : a.
clear x_i_in_Z_ x_i.
rewrite p_M. generalize le_1_M. case M. intro. absurd (2 <= 0); omega. intros q H.
elim (Zlog_sup_correct2 (Zpos q)). omega. omega. intros q H. absurd (2 <= Zneg q). apply Zlt_not_le.
unfold Z.lt. trivial. assumption. Qed.
clear x_i_in_Z_ x_i.
rewrite p_M. elim (Zle_lt_or_eq 1 M). intro H. left. apply Z.le_trans with (Zlog_sup 2). simpl. omega.
apply Zlog_sup_seq. omega. intro H. right. symmetry. trivial. assumption. Qed.
clear x_i_in_Z_ x_i.
elim le_1_p; intro H; omega. Qed.
clear x_i_in_Z_ x_i.
elim lt_0_p; intro H; omega. Qed.
Local Hint Resolve le_M_2pp le_1_p lt_0_p le_0_pm1 : a.
clear x_i_in_Z_ x_i.
elim (Zle_lt_or_eq 1 M). intro H. rewrite p_M. generalize le_1_M. case M. intro. absurd (2 <= 0); omega.
intros q H0. elim (Zlog_sup_correct2 (Zpos q)). omega. omega. intros q H0. absurd (1 <= Zneg q).
apply Zlt_not_le. unfold Z.lt. trivial. assumption. omega. assumption. Qed.
Local Hint Resolve lt_2pm1_M : a.
clear x_i_in_Z_ x_i.
elim (Zle_lt_or_eq 1 M). intro H. replace (2 ^ p) with (1 * M + (2 ^ p - M)). apply Zdiv_mult_plus. omega.
cut (M <= 2 ^ p). omega. auto with a. cut (2 ^ p < M + M). omega.
apply Z.lt_le_trans with (2 ^ (p - 1) + 1 + (2 ^ (p - 1) + 1)).
replace (2 ^ (p - 1) + 1 + (2 ^ (p - 1) + 1)) with (2 ^ (p - 1) + 2 ^ (p - 1) + 2). rewrite Z_pow_plus.
replace (p - 1 + 1) with p. omega. ring. elim le_0_pm1. omega. intro H0. absurd (1 < M). omega.
assumption. ring. cut (2 ^ (p - 1) < M). omega. elim lt_2pm1_M. omega. intro H0. absurd (1 < M). omega.
assumption. ring. intro H0. rewrite p_M. rewrite <- H0. trivial. assumption. Qed.
transitivity (2 ^ (p + w) / M / 2 ^ w * 2 ^ w + (2 ^ (p + w) / M) mod 2 ^ w).
apply f_equal2 with (f := Zplus). replace (2 ^ (p + w) / M / 2 ^ w) with 1. ring.
rewrite Zdivdivdiv. rewrite Zmult_comm. rewrite <- Zdivdivdiv. rewrite Zpower_exp; auto with a.
rewrite Z_div_mult. symmetry. exact div_M_1. apply Z.lt_gt. auto with a. apply Z.le_ge. auto with a.
apply Z.le_ge. auto with a. auto with a. omega. omega. auto with a. trivial. apply Zdivmod_split.
auto with a. Qed.
(* <KEEP TOGETHER - multired_equations *)
Let r'_i := ltw d_Si M_in_Z_ r_Si (minusw r_Si M_in_Z_).
Let s_i := ursh x_i_in_Z_ p_in_Z_w.
Let s'_i := multp2 s_i p_in_Z_w.
Let s''_i := ltw r'_i c_in_Z_ s_i (minusw s_i M''_in_Z_).
Let h_i := multp2 r'_i t_in_Z_w.
Let h'_i := plusw h_i s''_i.
Let q_i := uhwm h'_i M'_in_Z_.
Let q'_i := plusw q_i h'_i.
Let y_i := multw q'_i M_in_Z_.
Definition d_i := minusw s'_i y_i.
Definition r_i := minusw x_i_in_Z_ y_i.
(* KEEP TOGETHER> *)
Let d0 := d_Si. (* notation *)
Let r0 := r_Si. (* notation *)
(* <KEEP TOGETHER - multired_final *)
Let r'0 := ltw d0 M_in_Z_ r0 (minusw r0 M_in_Z_). (* notation *)
Let r''0 := ltw r'0 c_in_Z_ r'0 (minusw r'0 M_in_Z_). (* only needed after the loop *)
Definition r'''0 := ltw r''0 M_in_Z_ r''0 (minusw r''0 M_in_Z_). (* only needed after the loop *)
(* KEEP TOGETHER> *)
Let h_i' := multp2 r''0 t_in_Z_w.
Let h'_i' := plusw h_i' s_i.
Definition g' := r'''0. (* only needed after the loop *)
Let x := h * 2 ^ w + l.
Let x' := r''0 * 2 ^ w + x_i.
unfold x'. rewrite Zmod_km_plus. symmetry. unfold x_i. apply modred. apply le_0__Z. apply lt_z__Z. exact lt02w. Qed.
apply lt_0_Zpow. fold (0 + 0). apply Zplus_le_compat; auto with a. Qed.
apply lt_0_Zpow. auto with a. Qed.
Local Hint Resolve lt_0_2pp : a.
clear. cut (0 < w). omega. exact lt_0_w. Qed.
Local Hint Resolve le_0_wm1 lt_0_w : a.
clear x_i_in_Z_ x_i s_i s'_i s''_i h'_i q_i q'_i y_i h'_i h'_i' x'.
rewrite p_M. apply Z.le_lt_trans with (w - 1). rewrite <- Zlog_sup_pow. apply Zlog_sup_seq.
assumption. auto with a. omega. Qed.
apply Zlt_le_weak. exact lt_p_w. Qed.
cut (p <= w). omega. exact le_p_w. Qed.
Local Hint Resolve lt_p_w le_p_w le_0_wmp : a.
unfold M. apply lt_z__Z. Qed.
apply Zlt_le_weak. exact lt_M_2pw. Qed.
Hint Resolve lt_M_2pw le_M_2pw : a.
unfold P in d_Si_eq_r_Si_eq. tauto. Qed.
unfold P in d_Si_eq_r_Si_eq. tauto. Qed.
clear x_i_in_Z_ x_i s_i s'_i s''_i h'_i q_i q'_i y_i h'_i' x'.
replace (h mod M * 2 ^ w) with (0 + h mod M * 2 ^ (w - p + p)). rewrite Zpower_exp. rewrite Zmult_assoc.
rewrite Zmod_plus_distr_l with (m := 2 ^ p). rewrite Z_mod_plus. rewrite modred with (x := 0).
replace (0 + l) with l. exact r_Si_eq_lit. ring. omega. apply lt_0_Zpow. auto with a.
apply Z.lt_gt. apply Z.lt_le_trans with M. omega. auto with a. apply lt_0_Zpow. auto with a. cut (p < w). omega. auto with a.
apply Z.le_ge. auto with a. replace (w - p + p) with w. ring. ring. Qed.
clear x_i_in_Z_ x_i s_i s'_i s''_i h'_i q_i q'_i y_i h'_i h'_i' x'.
replace ((h mod M + M) * 2 ^ w) with (0 + (h mod M + M) * 2 ^ (w - p + p)). rewrite Zpower_exp. rewrite Zmult_assoc.
rewrite Zmod_plus_distr_l with (m := 2 ^ p). rewrite Z_mod_plus. rewrite modred with (x := 0).
replace (0 + l) with l. exact r_Si_eq_lit. ring. omega. apply lt_0_Zpow. auto with a.
apply Z.lt_gt. apply Z.lt_le_trans with M. omega. auto with a. apply lt_0_Zpow. auto with a. cut (p < w). omega. auto with a.
apply Z.le_ge. auto with a. replace (w - p + p) with w. ring. ring. Qed.
clear x_i_in_Z_ x_i s_i s'_i s''_i h'_i q_i q'_i y_i h'_i h'_i' x'.
rewrite <- Zpower_exp. replace (w - p + p) with w. trivial. ring. cut (p < w). omega. auto with a. apply Z.le_ge. auto with a.
Qed.
clear x_i_in_Z_ x_i s_i s'_i s''_i h'_i q_i q'_i y_i h'_i h'_i' x'.
intros a b c'. replace w with (w - p + p). rewrite Zpower_exp. do 2 rewrite Zmult_assoc. do 2 rewrite Zplus_comm with (m := b).
rewrite Z_mod_plus. rewrite Z_mod_plus. trivial. apply Z.lt_gt. apply lt_0_Zpow. auto with a. apply Z.lt_gt. apply lt_0_Zpow.
auto with a. cut (p < w). omega. auto with a. apply Z.le_ge. auto with a. ring. Qed.
(* KE EP TOGETHER> *)
clear x_i_in_Z_ x_i s_i s'_i s''_i h'_i q_i q'_i y_i h'_i h'_i' x'.
unfold r'_i. elim d_Si_eq; intro H. rewrite ltw_true. rewrite r_Si_eq_a. rewrite H. apply modred. apply Zplus_le_0_compat.
apply Zmod_le_0_z. omega. apply Zmod_le_0_z. apply lt_0_Zpow. auto with a. apply Z.lt_le_trans with (M + 2 ^ p).
apply Zplus_lt_le_compat. apply Zmod_lt_z_m. omega. apply Zlt_le_weak. apply Zmod_lt_z_m. apply lt_0_Zpow. auto with a.
rewrite Zpow_2_w_is_2Zpow_2_wm1. apply Zplus_le_compat. assumption. apply Zle_pow_le. auto with a. cut (p < w). omega.
auto with a. rewrite H. fold M. apply Zmod_lt_z_m. omega. elim H; intro H0. rewrite ltw_false. simpl. rewrite r_Si_eq_a. fold M.
rewrite H0. rewrite <- Zmod_minus_distr_l.
replace (((h mod M * 2 ^ w + l) / 2 ^ p * 2 ^ p) mod M + M + (h mod M * 2 ^ w + l) mod 2 ^ p - M)
with (((h mod M * 2 ^ w + l) / 2 ^ p * 2 ^ p) mod M + (h mod M * 2 ^ w + l) mod 2 ^ p).
apply modred. apply Zplus_le_0_compat. apply Zmod_le_0_z. omega. apply Zmod_le_0_z. apply lt_0_Zpow. auto with a.
apply Z.lt_le_trans with (M + 2 ^ p). apply Zplus_lt_le_compat. apply Zmod_lt_z_m. omega. apply Zlt_le_weak. apply Zmod_lt_z_m.
apply lt_0_Zpow. auto with a. rewrite Zpow_2_w_is_2Zpow_2_wm1. apply Zplus_le_compat. assumption. apply Zle_pow_le. auto with a.
cut (p < w). omega. auto with a. ring. exact lt02w. rewrite H0. fold M.
cut (0 <= ((h mod M * 2 ^ w + l) / 2 ^ p * 2 ^ p) mod M). omega. apply Zmod_le_0_z. omega. elim H0; intro H1.
rewrite ltw_true. rewrite r_Si_eq_b. rewrite H1. rewrite p2wp. rewrite Zmult_assoc. rewrite mod_over_div.
rewrite Zmod_mult_distr_l with (a := h mod M + M). replace (h mod M + M) with (h mod M + 1 * M). rewrite Z_mod_plus.
rewrite <- Zmod_mult_distr_l. rewrite <- mod_over_div. rewrite <- Zmult_assoc. rewrite <- Zmult_assoc. rewrite <- Zpower_exp.
replace (w - p + p) with w. rewrite mod2p with (c' := h mod M). apply modred. apply Zplus_le_0_compat.
apply Zmod_le_0_z. omega. apply Zmod_le_0_z. apply lt_0_Zpow. auto with a. apply Z.lt_le_trans with (M + 2 ^ p).
apply Zplus_lt_le_compat. apply Zmod_lt_z_m. omega. apply Zlt_le_weak. apply Zmod_lt_z_m. apply lt_0_Zpow. auto with a.
rewrite Zpow_2_w_is_2Zpow_2_wm1. apply Zplus_le_compat. assumption. apply Zle_pow_le. auto with a. cut (p < w). omega.
auto with a. ring. cut (p < w). omega. auto with a. apply Z.le_ge. auto with a. apply lt_0_Zpow. auto with a. omega. omega.
omega. ring. omega. apply lt_0_Zpow. auto with a. omega. rewrite H1. fold M. apply Zmod_lt_z_m. omega.
rewrite ltw_false. simpl. rewrite r_Si_eq_b. rewrite H1.
(* rewrite p2wp. *) (* does not work: Cannot solve a second-order unification problem *)
(* replace ((h mod M + M) * 2 ^ w) with ((h mod M + M) * (2 ^ (w - p) * 2 ^ p)). *) (* again, does not work *)
transitivity ((((((h mod M + M) * (2 ^ (w - p) * 2 ^ p) + l) / 2 ^ p * 2 ^ p) mod M + M +
((h mod M + M) * (2 ^ (w - p) * 2 ^ p) + l) mod 2 ^ p) mod
(2 ^ (w - p) * 2 ^ p) - M) mod (2 ^ (w - p) * 2 ^ p)). (* workaround *)
rewrite <- p2wp. trivial. (* this works *)
rewrite Zmult_assoc. rewrite mod_over_div.
rewrite Zmod_mult_distr_l with (a := h mod M + M). replace (h mod M + M) with (h mod M + 1 * M). rewrite Z_mod_plus.
rewrite <- Zmod_mult_distr_l. rewrite <- mod_over_div. rewrite <- Zmult_assoc. rewrite <- Zmult_assoc. rewrite <- Zpower_exp.
replace (w - p + p) with w. rewrite mod2p with (c' := h mod M). rewrite <- Zmod_minus_distr_l. fold M.
replace (((h mod M * 2 ^ w + l) / 2 ^ p * 2 ^ p) mod M + M + (h mod M * 2 ^ w + l) mod 2 ^ p - M)
with (((h mod M * 2 ^ w + l) / 2 ^ p * 2 ^ p) mod M + (h mod M * 2 ^ w + l) mod 2 ^ p). apply modred.
apply Zplus_le_0_compat. apply Zmod_le_0_z. omega. apply Zmod_le_0_z. apply lt_0_Zpow. auto with a.
apply Z.lt_le_trans with (M + 2 ^ p). apply Zplus_lt_le_compat. apply Zmod_lt_z_m. omega. apply Zlt_le_weak. apply Zmod_lt_z_m.
apply lt_0_Zpow. auto with a. rewrite Zpow_2_w_is_2Zpow_2_wm1. apply Zplus_le_compat. assumption. apply Zle_pow_le. auto with a.
cut (p < w). omega. auto with a. ring. auto with a. ring. cut (p < w). omega. auto with a. apply Z.le_ge. auto with a.
apply lt_0_Zpow. auto with a. omega. omega. omega. ring. omega. apply lt_0_Zpow. auto with a. omega. rewrite H1. fold M.
cut (0 <= (((h mod M + M) * 2 ^ w + l) / 2 ^ p * 2 ^ p) mod M). omega. apply Zmod_le_0_z. omega. Qed.
Let x'' := h mod M * 2 ^ w + l.
clear x_i_in_Z_ x_i s_i s'_i s''_i h'_i q_i q'_i y_i h'_i h'_i' x'.
intros a b. cut (0 <= (a mod M + b mod M) / M). cut ((a mod M + b mod M) / M <= 1). omega.
apply Z.le_trans with ((M - 1 + M) / M). apply Zdiv_le. omega. apply Zplus_le_compat. cut (a mod M < M). omega.
apply Zmod_lt_z_m. omega. cut (b mod M < M). omega. apply Zmod_lt_z_m. omega. replace (M - 1 + M) with (2 * M - 1).
rewrite Zdiv_mult_minus. omega. omega. omega. omega. ring. apply Zle_0_div. fold (0 + 0). apply Zplus_le_compat.
apply Zmod_le_0_z. omega. apply Zmod_le_0_z. omega. omega. Qed.
clear x_i_in_Z_ x_i s_i s'_i s''_i h'_i q_i q'_i y_i h'_i h'_i' x'.
intro a. cut (0 <= a mod 2 ^ p / M). cut (a mod 2 ^ p / M <= 1). omega. apply Z.le_trans with (2 ^ p / M). apply Zdiv_le. omega.
cut (a mod 2 ^ p < 2 ^ p). omega. apply Zmod_lt_z_m. apply lt_0_Zpow. auto with a. rewrite div_M_1. omega. apply Zle_0_div.
apply Zmod_le_0_z. apply lt_0_Zpow. auto with a. omega. Qed.
clear x_i_in_Z_ x_i s_i s'_i s''_i h'_i q_i q'_i y_i h'_i h'_i' x'.
rewrite r'_i_eq. fold x''. rewrite Z_div_mod_eq with (a := x'' mod 2 ^ p) (b := M).
rewrite Zplus_comm with (m := (x'' mod 2 ^ p) mod M). rewrite Zplus_assoc.
rewrite Z_div_mod_eq with (a := (x'' / 2 ^ p * 2 ^ p) mod M + (x'' mod 2 ^ p) mod M) (b := M). rewrite <- Zmod_plus_distr_l.
rewrite Zplus_comm with (n := x'' / 2 ^ p * 2 ^ p). rewrite <- Zmod_plus_distr_l. rewrite Zplus_comm with (n := x'' mod 2 ^ p).
rewrite Zmult_comm with (m := 2 ^ p). rewrite <- Z_div_mod_eq. replace (x'' mod M) with (x mod M).
elim (sum_r'_i (2 ^ p * (x'' / 2 ^ p)) (x'' mod 2 ^ p)); intro H; elim (div_r'_i x''); intro H0; rewrite H; rewrite H0. left. ring.
right. left. ring. right. left. ring. right. right. ring. unfold x''. rewrite Zmod_plus_distr_l. rewrite <- Zmod_mult_distr_l.
rewrite <- Zmod_plus_distr_l. trivial. omega. omega. omega. apply Z.lt_gt. apply lt_0_Zpow. auto with a. omega. omega. omega.
omega. Qed.
(* KE EP TOGETHER> *)
simpl. fold l. fold p. fold x_i. rewrite x_i_eq. trivial. Qed.
(* KE EP TOGETHER> *)
unfold s'_i. rewrite Z_from_multp2. fold p. rewrite s_i_eq. rewrite Zmult_comm. rewrite modred.
replace (x' mod 2 ^ p) with (x' mod 2 ^ w mod 2 ^ p). apply Zdiv_a_b_b_Zmod. auto with a.
replace (2 ^ w) with (2 ^ (w - p) * 2 ^ p). apply Zmod_prod_Zmod. apply lt_0_Zpow. auto with a.
auto with a. rewrite <- Zpower_exp. apply f_equal2 with (f := Zpower). trivial. ring. apply Z.le_ge.
auto with a. apply Z.le_ge. auto with a. fold (0 * 0). apply Zmult_le_compat. apply Zle_0_div.
apply Zmod_le_0_z. auto with a. auto with a. apply Zlt_le_weak. auto with a. omega. omega.
apply Z.le_lt_trans with (x' mod 2 ^ w). rewrite Zmult_comm. apply Z_mult_div_ge. apply Z.lt_gt.
auto with a. apply Zmod_lt_z_m. auto with a. Qed.
unfold x'. fold (0 + 0). apply Zplus_le_compat; auto with a. apply Zmult_le_0_compat. apply le_0__Z.
auto with a. unfold x_i. apply le_0__Z. Qed.
Local Hint Resolve le_0_x' : a.
unfold r''0.
unfold r'0. unfold d0. unfold r0. fold r'_i. (* too long *)
elim (Zle_or_lt c r'_i); intro H. rewrite ltw_false. rewrite Z_from_minusw. fold M. rewrite modred. rewrite r'_i_eq.
cut (((h mod M * 2 ^ w + l) / 2 ^ p * 2 ^ p) mod M < M). cut ((h mod M * 2 ^ w + l) mod 2 ^ p < 2 ^ p). omega.
apply Zmod_lt_z_m. apply lt_0_Zpow. auto with a. apply Zmod_lt_z_m. omega. cut (M <= r'_i). omega.
apply Z.le_trans with c. rewrite c_M. exact le_M_2pp. assumption. cut (0 <= M). cut (r'_i < 2 ^ w). omega. apply lt_z__Z.
omega. assumption. rewrite ltw_true. rewrite <- c_M. assumption. assumption. Qed.
(* KE EP TOGETHER> *)
clear x_i_in_Z_ x_i s_i s'_i s''_i h'_i q_i q'_i y_i h'_i h'_i' x'.
unfold r''0.
unfold r'0. unfold d0. unfold r0. fold r'_i. (* too long *) elim (Zle_or_lt c r'_i); intro H. rewrite ltw_false. simpl. fold M.
rewrite modred. elim r'_i_eq'_i; intro H0. absurd (c <= r'_i). apply Zlt_not_le. rewrite H0. apply Z.lt_le_trans with M.
apply Zmod_lt_z_m. omega. rewrite c_M. exact le_M_2pp. assumption. elim H0; intro H1. left. omega. right. omega.
cut (M <= 2 ^ p). omega. exact le_M_2pp. cut (r'_i < 2 ^ w). cut (0 < M). omega. omega. apply lt_z__Z. assumption.
rewrite ltw_true. elim r'_i_eq'_i. intro H0. tauto. intro H0. elim H0; intro H1. tauto. absurd (r'_i < c). apply Zle_not_lt.
rewrite H1. apply Z.le_trans with (M + M). rewrite c_M. elim (Zle_lt_or_eq 1 M). intro H2. elim lt_2pm1_M; intro H3.
replace p with (p - 1 + 1). rewrite Zpower_exp. simpl. unfold Zpower_pos. simpl. omega. cut (1 <= p). omega.
replace 1 with (Zlog_sup 2). rewrite p_M. apply Zlog_sup_seq. omega. trivial. omega. ring. rewrite p_M. rewrite H3. omega.
intro H2. rewrite p_M. rewrite <- H2. simpl. omega. omega. cut (0 <= x mod M). omega. apply Zmod_le_0_z. omega. assumption.
assumption. Qed.
(* KE EP TOGETHER> *)
clear x_i_in_Z_ x_i s_i s'_i s''_i h'_i q_i q'_i y_i h'_i h'_i' x'.
unfold r'''0. elim r''0_eq; intro H. rewrite ltw_true. trivial. rewrite H. apply Zmod_lt_z_m. fold M. omega. rewrite ltw_false.
simpl. rewrite H. fold M. replace (x mod M + M - M) with (x mod M). apply modred.
apply Zmod_le_0_z. omega. apply Z.lt_trans with M. apply Zmod_lt_z_m. omega. unfold M. apply lt_z__Z. ring. rewrite H. fold M.
cut (0 <= x mod M). omega. apply Zmod_le_0_z. omega. Qed.
unfold x'. apply Z.le_lt_trans with ((2 ^ p - 1) * 2 ^ w + (2 ^ w - 1)). apply Zplus_le_compat.
apply Zmult_le_compat. auto with a. auto with a. cut (r''0 < 2 ^ p). omega. apply r''0_small. omega. auto with a.
apply le_0__Z. auto with a. cut (x_i < 2 ^ w). omega. unfold x_i. apply lt_z__Z. rewrite Zmult_minus_distr_r.
rewrite <- Zpower_exp. omega. auto with a. apply Z.le_ge. auto with a. apply Z.le_ge. auto with a. Qed.
cut (x' < 2 ^ (p + w)). omega. exact lt_x'_x'max. Qed.
Local Hint Resolve lt_x'_x'max le_x'_x'max : a.
intro H. cut (r''0 < 2 ^ p). rewrite p_M. rewrite H. simpl. cut (0 <= r''0). omega. apply le_0__Z. exact r''0_small. Qed.
(* KE EP TOGETHER> *)
unfold x'. rewrite Zdiv_times_plus. rewrite Zdiv_small_0. simpl. fold t. replace (r''0 + 0) with (_Z r''0). elim t_M; intro H.
rewrite H. rewrite Zmult_comm. apply modred. fold (0 * 0). apply Zmult_le_compat. apply le_0__Z. apply Zlt_le_weak.
apply lt_0_Zpow. auto with a. omega.
omega. apply Z.lt_le_trans with (2 ^ p * 2 ^ (w - p)). apply Zmult_lt_compat_r. auto with a.
apply lt_0_Zpow. auto with a. apply r''0_small. rewrite <- Zpower_exp. replace (p + (w - p)) with w. omega.
ring. apply Z.le_ge. auto with a. apply Z.le_ge. auto with a. rewrite Meq_ir''0. rewrite Zmult_comm. simpl. apply modred. omega.
auto with a. assumption. ring. unfold x_i. apply le_0__Z. unfold x_i. apply lt_z__Z. auto with a. Qed.
(* KE EP TOGETHER> *)
unfold h'_i'. rewrite Z_from_plusw. rewrite h_i'_eq. simpl. fold x_i p. rewrite x_i_eq. rewrite <- Zdiv_times_plus.
rewrite <- Zmult_assoc. rewrite <- Zpower_exp. replace (w - p + p) with w. rewrite Zdivmod_split.
apply modred. apply Zle_0_div. auto with a. auto with a. apply Z.lt_le_trans with (2 ^ (p + w) / 2 ^ p).
apply Z.le_lt_trans with ((2 ^ (p + w) - 1) / 2 ^ p). apply Zdiv_le. auto with a. cut (x' < 2 ^ (p + w)).
omega. auto with a. rewrite Zpower_exp. rewrite Zmult_comm. rewrite Zdiv_mult_minus. rewrite Z_div_mult.
omega. apply Z.lt_gt. auto with a. auto with a. omega. cut (0 < 2 ^ p). omega. auto with a. apply Z.le_ge.
auto with a. apply Z.le_ge. auto with a. rewrite Zpower_exp. rewrite Zmult_comm. rewrite Z_div_mult.
omega. apply Z.lt_gt. auto with a. apply Z.le_ge. auto with a. apply Z.le_ge. auto with a. auto with a.
ring. apply Z.le_ge. auto with a. apply Z.le_ge. auto with a. auto with a. Qed.
unfold h'_i'. unfold h'_i. simpl. unfold r''0.
unfold r'0. unfold d0. unfold r0. fold r'_i. (* too long *)
unfold s''_i. fold t. fold p. elim (Zle_or_lt c r'_i); intro H. rewrite ltw_false. rewrite ltw_false. simpl. fold M''.
rewrite M''_M. fold M. fold x_i. rewrite Zmult_comm. rewrite <- Zmod_mult_distr_l. rewrite <- Zmod_plus_distr_l.
rewrite <- Zmod_minus_distr_r. rewrite <- Zmod_plus_distr_l. rewrite Zplus_comm with (n := 2 ^ t * r'_i).
rewrite <- Zmod_plus_distr_l. fold p. apply f_equal2 with (f := Zmod). ring. trivial. exact lt02w. exact lt02w. exact lt02w.
exact lt02w. exact lt02w. assumption. assumption. rewrite ltw_true. rewrite ltw_true. trivial. assumption. assumption. Qed.
(* KE EP TOGETHER> *)
unfold q_i. rewrite uhwm_eq. rewrite <- h'_i'h'_i_eq. rewrite h'_i'_eq. fold M'. rewrite M'_M. trivial. Qed.
unfold q'_i. rewrite Z_from_plusw. rewrite q_i_eq. rewrite <- h'_i'h'_i_eq. rewrite h'_i'_eq. rewrite Zplus_comm.
rewrite <- Zdiv_times_plus. rewrite <- Zmult_plus_distr_r. rewrite large_M'_eq. trivial. auto with a. Qed.
intros z a b m H H0. elim H0; intro H1; rewrite H1; tauto. Qed.

(* <KE EP TOGETHER - q'_i_eq *)
Lemma q'_i_eq :
_Z q'_i = (x' / 2 ^ p * 2 ^ p / M) mod 2 ^ w \/
_Z q'_i = (x' / 2 ^ p * 2 ^ p / M - 1) mod 2 ^ w.