(* Copyright 2012-2015 by Adam Petcher.				* * Use of this source code is governed by the license described	* * in the LICENSE file at the root of the source tree.		*)
(* Definitions and theory of natural numbers that is useful in cryptographi proofs. *)
Set Implicit Arguments.
Require Export Arith.
Require Export Omega.
Require Export Arith.Div2.
Require Export Coq.Numbers.Natural.Peano.NPeano.
induction n1; destruct n2; intuition; simpl in *.
remember (n2 * n3) as x.
omega.
remember (n1 * n3) as x.
omega.
f_equal.
eapply IHn1; eauto.
eapply plus_reg_l. eauto.
Qed.
intuition.
eapply mult_same_r; eauto.
rewrite mult_comm.
rewrite (mult_comm n2 n3).
trivial.
Qed.
destruct n1; intuition; simpl in *.
remember (n1 * n2) as x.
omega.
Qed.
intuition.
Qed.
intuition.
Qed.
intuition.
Qed.
intuition.
Qed.
intuition.
Qed.
Class nz (a : nat) := {
agz : a > 0
}.
Instance nz_nat : forall (n : nat), nz (S n).
intuition.
econstructor.
omega.
Defined.
Definition posnat := {n : nat | n > 0}.
Definition posnatToNat(p : posnat) :=
match p with
| exist _ n _ => n
end.
Inductive posnatEq : posnat -> posnat -> Prop :=
| posnatEq_intro :
forall (n1 n2 : nat) pf1 pf2,
n1 = n2 ->
posnatEq (exist _ n1 pf1) (exist _ n2 pf2).
Definition posnatMult(p1 p2 : posnat) : posnat :=
match (p1, p2) with
| (exist _ n1 pf1, exist _ n2 pf2) =>
(exist (fun n => n > 0) (n1 * n2) (mult_gt_0 pf1 pf2))
end.
intuition.
unfold posnatMult.
destruct p1; destruct p2.
econstructor.
apply mult_comm.
Qed.
Coercion posnatToNat : posnat >-> nat.
intuition.
destruct p.
unfold posnatToNat.
trivial.
Qed.
Instance nz_posnat : forall (p : posnat),
nz p.
intuition.
econstructor.
eapply posnat_pos.
Qed.
Definition natToPosnat(n : nat)(pf : nz n) :=
(exist (fun x => x > 0) n agz).
Notation "'pos' x" := (@natToPosnat x _) (at level 40).
Fixpoint expnat n1 n2 :=
match n2 with
| 0 => 1
| S n2' =>
n1 * (expnat n1 n2')
end.
induction n; intuition; simpl in *.
remember (x * expnat x n) as y.
assert (y <> 0); try omega.
intuition; subst.
apply mult_is_O in H1.
destruct H1; omega.
Qed.
intuition.
eapply PeanoNat.Nat.div2_decr.
omega.
Qed.
intuition.
destruct (Even.even_odd_dec n).
rewrite (even_double n) at 1.
unfold double.
omega.
trivial.
rewrite (odd_double n) at 1.
unfold double.
omega.
trivial.
Qed.
Local Open Scope N_scope.
Definition modNat (n : nat)(p : posnat) : nat :=
N.to_nat ((N.of_nat n) mod (N.of_nat p)).
intros []; discriminate.
Qed.
unfold modNat.
intuition.
rewrite Nnat.Nat2N.inj_add.
rewrite <- N.add_mod_idemp_l.
f_equal.
rewrite <- (Nnat.Nat2N.id n2) at 2.
rewrite Nnat.Nat2N.inj_add.
repeat rewrite Nnat.N2Nat.id.
trivial.
destruct p.
simpl.
destruct x;
simpl.
omega.
apply Npos_nz.
Qed.
intuition.
unfold modNat.
rewrite N.mod_same.
trivial.
unfold N.of_nat, posnatToNat.
destruct p.
destruct x.
omega.
apply Npos_nz.
Qed.
intuition.
unfold N.of_nat.
destruct n.
intuition.
simpl.
unfold N.le.
case_eq ((0 ?= N.pos (Pos.of_succ_nat n))); intuition;
try discriminate.
Qed.
intuition.
unfold N.of_nat, posnatToNat.
destruct p.
destruct x.
omega.
destruct x; intuition; simpl in *.
case_eq (N.compare 0 1)%N; intuition.
inversion H.
inversion H.
case_eq (N.compare 0 (N.pos (Pos.succ (Pos.of_succ_nat x))))%N; intuition.
inversion H.
inversion H.
Qed.
intuition.
unfold modNat.
assert (N.of_nat x mod N.of_nat p < N.of_nat p)%N.
apply N.mod_bound_pos.
apply of_nat_ge_0.
apply of_posnat_gt_0.
specialize (Nnat.N2Nat.inj_compare); intros H0.
rewrite <- (Nnat.Nat2N.id p) at 2.
apply nat_compare_lt.
rewrite <- H0.
apply N.compare_lt_iff.
trivial.
Qed.

Lemma modNat_eq : forall (n : posnat) x, (x < n -> modNat x n = x)%nat.