From Coq Require Import Bool. From Coq Require Import ZArith. From Coq Require Import Lia. Require Import Crypto.Util.ZUtil.Definitions. Require Import Crypto.Util.ZUtil.Pow. Require Import Crypto.Util.ZUtil.Testbit. Require Import Crypto.Util.ZUtil.Modulo. Require Import Crypto.Util.ZUtil.Lor. Require Import Crypto.Util.ZUtil.Notations. Require Import Crypto.Util.ZUtil.Tactics.PullPush. Require Import Crypto.Util.ZUtil.Tactics.SolveTestbit. Require Import Crypto.Util.Tactics.BreakMatch. Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. Require Import Crypto.Util.Decidable. Import Notations. Local Open Scope Z_scope. Module Z. Local Ltac destruct_ltb a b := let E := fresh in destruct (a a mod 2 ^ m = b mod 2 ^ m /\ - 2 ^ (m - 1) <= b < 2 ^ (m - 1). Proof. unfold Z.twos_complement. assert (0 < 2 ^ m) by (apply Z.pow_pos_nonneg; lia). assert (0 <= a mod 2 ^ m < 2 ^ m) by (apply Z.mod_pos_bound; lia). assert (2 ^ (m - 1) <= 2 ^ m) by (apply Z.pow_le_mono_r; lia). assert (2 * 2 ^ (m - 1) = 2 ^ m) by (rewrite Z.pow_mul_base by lia; apply f_equal2; lia). split; destruct_ltb (a mod 2 ^ m) (2 ^ (m - 1)); intros H4; subst; try split; repeat (pull_Zmod; push_Zmod; rewrite ?Z.mod_0_l, ?Z.sub_0_r); try lia. - destruct H4. rewrite H4 in *. rewrite Z.mod_small. lia. destruct (dec (b < 0)); [|lia]. pose proof Z.mod_neg_small b (2 ^ m) ltac:(nia). rewrite Z.mod_small in H6; lia. - destruct H4. rewrite H4 in *. rewrite Z.mod_neg_small. lia. destruct (dec (0 <= b)); [|lia]. rewrite Z.mod_small in H3; lia. Qed. Lemma twos_complement_testbit_spec_full m a i (Hm : 0 < m) : Z.testbit (Z.twos_complement m a) i = if (i