proposition: Lemma wordToZ_ZToWord': forall sz w, exists k, wordToZ (ZToWord sz w) = (w - k * Z.of_N (Npow2 sz))%Z.
proof: Proof. intros. destruct sz. - simpl. exists w%Z. rewrite ZToWord_sz0. rewrite wordToZ_wzero. lia. - exists ((w + Z.of_nat (pow2 sz)) / Z.of_N (Npow2 (S sz)))%Z. erewrite <- ZToWord_Npow2_sub_z. rewrite wordToZ_ZToWord. + reflexivity. + replace w with ((- Z.of_nat (pow2 sz)) + (w + Z.of_nat (pow2 sz)))%Z at 1 3 by lia. rewrite <- Z.add_sub_assoc. replace (Z.of_N (Npow2 (S sz))) with (2 * Z.of_nat (pow2 sz))%Z. * remember (Z.of_nat (pow2 sz)) as M. assert (M > 0)%Z. { subst. destruct (pow2 sz) eqn: E. - exfalso. eapply pow2_ne_zero. exact E. - simpl. constructor. } rewrite <- Zdiv.Zmod_eq_full by lia. pose proof (Zdiv.Z_mod_lt (w + M) (2 * M)). lia. * rewrite <- Npow2_nat. rewrite N_nat_Z. rewrite Npow2_S. rewrite N2Z.inj_add. lia. Qed.
imports: Coq.Classes.Morphisms Coq.Classes.Morphisms_Prop Coq.Classes.Equivalence Coq.Arith.PeanoNat Coq.ZArith.BinInt  Coq.Arith.Arith Coq.NArith.NArith Coq.Bool.Bool Coq.ZArith.ZArith Coq.Logic.Eqdep_dec Coq.Logic.EqdepFacts Coq.Program.Tactics Coq.Program.Equality Coq.setoid_ring.Ring Coq.setoid_ring.Ring_polynom bbv.Nomega bbv.ZLib bbv.NatLib bbv.NLib bbv.N_Z_nat_conversions bbv.DepEq bbv.DepEqNat bbv.ReservedNotations Coq.micromega.Lia