(* KE EP TOGETHER> *)
unfold d_i. rewrite Z_from_minusw. rewrite s'_i_eq. rewrite Zmod_minus_distr_l.
rewrite <- Zmod_minus_distr_l with (b := x' mod 2 ^ p). rewrite <- Zmod_minus_distr_l.
rewrite <- Zdiv_a_b_b_Zmod. elim y_i_eq; intro H. left. rewrite H. rewrite <- Zmod_minus_distr_r.
rewrite Zminus_Zdiv_Zmult. apply modred. apply Zmod_le_0_z. omega. apply Zlt_le_trans with M.
apply Zmod_lt_z_m. omega. auto with a. omega. auto with a. right. rewrite H.
rewrite <- Zmod_minus_distr_r.
replace (x' / 2 ^ p * 2 ^ p - (x' / 2 ^ p * 2 ^ p / M * M - M))
with (x' / 2 ^ p * 2 ^ p - x' / 2 ^ p * 2 ^ p / M * M + M). rewrite Zminus_Zdiv_Zmult. apply modred.
fold (0 + 0). apply Zplus_le_compat. apply Zmod_le_0_z. omega. omega. apply Zlt_le_trans with (M + M).
apply Zplus_lt_le_compat. apply Zmod_lt_z_m. omega. omega. rewrite Zpow_2_w_is_2Zpow_2_wm1. omega. omega.
ring. auto with a. auto with a. auto with a. auto with a. auto with a. Qed.

(* <KE EP TOGETHER - r_i_eq *)
Lemma r_i_eq : _Z r_i = (_Z d_i + x' mod 2 ^ p) mod 2 ^ w.
unfold r_i. unfold d_i. rewrite Z_from_minusw. rewrite Z_from_minusw. fold l. rewrite s'_i_eq.