| 1 |
Require Export Crypto.Spec.ModularArithmetic. |
| 2 |
Require Export Crypto.Arithmetic.ModularArithmeticTheorems. |
| 3 |
From Coq Require Export Ring_theory Field_theory Field_tac. |
| 4 |
|
| 5 |
From Coq Require Import Nsatz. |
| 6 |
From Coq Require Import Lia. |
| 7 |
Require Import Crypto.Arithmetic.ModularArithmeticPre. |
| 8 |
Require Import Crypto.Util.NumTheoryUtil. |
| 9 |
From Coq Require Import Morphisms Setoid. |
| 10 |
From Coq Require Import BinInt BinNat ZArith Znumtheory NArith. (* import Zdiv before Znumtheory *) |
| 11 |
From Coq Require Import Eqdep_dec. |
| 12 |
Require Import Crypto.Util.NumTheoryUtil. |
| 13 |
Require Import Crypto.Util.ZUtil.Odd. |
| 14 |
Require Import Crypto.Util.ZUtil.Modulo. |
| 15 |
Require Import Crypto.Util.ZUtil.Tactics.ZeroBounds. |
| 16 |
Require Import Crypto.Util.Tactics.SpecializeBy. |
| 17 |
Require Import Crypto.Util.Decidable. |
| 18 |
Require Export Crypto.Util.FixCoqMistakes. |
| 19 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 20 |
Require Crypto.Algebra.Hierarchy Crypto.Algebra.Field. |
| 21 |
|
| 22 |
Existing Class prime. |
| 23 |
Local Open Scope F_scope. |
| 24 |
|
| 25 |
Module F. |
| 26 |
Section Field. |
| 27 |
Context (q:positive) {prime_q:prime q}. |
| 28 |
Lemma inv_spec : F.inv 0%F = (0%F : F q) |
| 29 |
/\ (prime q -> forall x : F q, x <> 0%F -> (F.inv x * x)%F = 1%F). |
| 30 |
Proof using Type. change (@F.inv q) with (proj1_sig (@F.inv_with_spec q)); destruct (@F.inv_with_spec q); eauto. Qed. |
| 31 |
|
| 32 |
Lemma inv_0 : F.inv 0%F = F.of_Z q 0. Proof using Type. destruct inv_spec; auto. Qed. |
| 33 |
Lemma inv_nonzero (x:F q) : (x <> 0 -> F.inv x * x%F = 1)%F. Proof using Type*. destruct inv_spec; auto. Qed. |
| 34 |
|
| 35 |
Global Instance field_modulo : @Algebra.Hierarchy.field (F q) Logic.eq 0%F 1%F F.opp F.add F.sub F.mul F.inv F.div. |
| 36 |
Proof using Type*. |
| 37 |
repeat match goal with |
| 38 |
| _ => solve [ solve_proper |
| 39 |
| apply F.commutative_ring_modulo |
| 40 |
| apply inv_nonzero |
| 41 |
| cbv [F.zero F.one not]; pose proof prime_ge_2 q prime_q; |
| 42 |
rewrite F.eq_to_Z_iff, !F.to_Z_of_Z, !Zmod_small; lia ] |
| 43 |
| _ => split |
| 44 |
end. |
| 45 |
Qed. |
| 46 |
End Field. |
| 47 |
|
| 48 |
Section NumberThoery. |
| 49 |
Context {q:positive} {prime_q:prime q} {two_lt_q: 2 < q}. |
| 50 |
|
| 51 |
(* TODO: move to PrimeFieldTheorems *) |
| 52 |
Lemma to_Z_1 : @F.to_Z q 1 = 1%Z. |
| 53 |
Proof using two_lt_q. simpl. rewrite Zmod_small; lia. Qed. |
| 54 |
|
| 55 |
Lemma Fq_inv_fermat (x:F q) : F.inv x = x ^ Z.to_N (q - 2)%Z. |
| 56 |
Proof using Type*. |
| 57 |
destruct (dec (x = 0%F)) as [?|Hnz]. |
| 58 |
{ subst x; rewrite inv_0, F.pow_0_l; trivial. |
| 59 |
change (0%N) with (Z.to_N 0%Z); rewrite Z2N.inj_iff; lia. } |
| 60 |
erewrite <-Algebra.Field.inv_unique; try reflexivity. |
| 61 |
rewrite F.eq_to_Z_iff, F.to_Z_mul, F.to_Z_pow, Z2N.id, to_Z_1 by lia. |
| 62 |
apply (fermat_inv q _ (F.to_Z x)); rewrite F.mod_to_Z; eapply F.to_Z_nonzero; trivial. |
| 63 |
Qed. |
| 64 |
|
| 65 |
Lemma euler_criterion (a : F q) (a_nonzero : a <> 0) : |
| 66 |
(a ^ (Z.to_N (q / 2)) = 1) <-> (exists b, b*b = a). |
| 67 |
Proof using Type*. |
| 68 |
pose proof F.to_Z_nonzero_range a; pose proof (odd_as_div q). |
| 69 |
specialize_by (destruct (Z.prime_odd_or_2 _ prime_q); try lia; trivial). |
| 70 |
rewrite F.eq_to_Z_iff, !F.to_Z_pow, !to_Z_1, !Z2N.id by lia. |
| 71 |
rewrite F.square_iff, <-(euler_criterion (q/2)) by (trivial || lia); reflexivity. |
| 72 |
Qed. |
| 73 |
|
| 74 |
Global Instance Decidable_square : forall (x:F q), Decidable (exists y, y*y = x). |
| 75 |
Proof. |
| 76 |
intro x; destruct (dec (x = 0)). |
| 77 |
{ left. abstract (exists 0; subst; apply Ring.mul_0_l). } |
| 78 |
{ eapply Decidable_iff_to_impl; [eapply euler_criterion; assumption | exact _]. } |
| 79 |
Defined. |
| 80 |
End NumberThoery. |
| 81 |
|
| 82 |
Section SquareRootsPrime3Mod4. |
| 83 |
Context {q:positive} {prime_q: prime q} {q_3mod4 : q mod 4 = 3}. |
| 84 |
|
| 85 |
Add Field _field2 : (Algebra.Field.field_theory_for_stdlib_tactic(T:=F q)) |
| 86 |
(morphism (F.ring_morph q), |
| 87 |
constants [F.is_constant], |
| 88 |
div (F.morph_div_theory q), |
| 89 |
power_tac (F.power_theory q) [F.is_pow_constant]). |
| 90 |
|
| 91 |
Definition sqrt_3mod4 (a : F q) : F q := a ^ Z.to_N (q / 4 + 1). |
| 92 |
|
| 93 |
Global Instance Proper_sqrt_3mod4 : Proper (eq ==> eq ) sqrt_3mod4. |
| 94 |
Proof using Type. repeat intro; subst; reflexivity. Qed. |
| 95 |
|
| 96 |
Lemma two_lt_q_3mod4 : 2 < q. |
| 97 |
Proof using Type*. |
| 98 |
pose proof (prime_ge_2 q _) as two_le_q. |
| 99 |
destruct (Zle_lt_or_eq _ _ two_le_q) as [H|H]; [exact H|]. |
| 100 |
rewrite <-H in q_3mod4; discriminate. |
| 101 |
Qed. |
| 102 |
Local Hint Resolve two_lt_q_3mod4 : core. |
| 103 |
|
| 104 |
Lemma sqrt_3mod4_correct (x:F q) : |
| 105 |
((exists y, y*y = x) <-> (sqrt_3mod4 x)*(sqrt_3mod4 x) = x)%F. |
| 106 |
Proof using Type*. |
| 107 |
cbv [sqrt_3mod4]; intros. |
| 108 |
destruct (F.eq_dec x 0); |
| 109 |
repeat match goal with |
| 110 |
| |- _ => progress subst |
| 111 |
| |- _ => progress rewrite ?F.pow_0_l, <-?F.pow_add_r |
| 112 |
| |- _ => progress rewrite <-?Z2N.inj_0, <-?Z2N.inj_add by Z.zero_bounds |
| 113 |
| |- _ => rewrite <-@euler_criterion by auto |
| 114 |
| |- ?x ^ (?f _) = ?a <-> ?x ^ (?f _) = ?a => do 3 f_equiv; [ ] |
| 115 |
| |- _ => rewrite !Zmod_odd in *; repeat (break_match; break_match_hyps); lia |
| 116 |
| |- _ => rewrite Z.rem_mul_r in * by lia |
| 117 |
| |- (exists x, _) <-> ?B => assert B by field; solve [intuition eauto] |
| 118 |
| |- (?x ^ Z.to_N ?a = 1) <-> _ => |
| 119 |
transitivity (x ^ Z.to_N a * x ^ Z.to_N 1 = x); |
| 120 |
[ rewrite F.pow_1_r, Algebra.Field.mul_cancel_l_iff by auto; reflexivity | ] |
| 121 |
| |- (_ <> _)%N => rewrite Z2N.inj_iff by Z.zero_bounds |
| 122 |
| |- (?a <> 0)%Z => assert (0 < a) by Z.zero_bounds; lia |
| 123 |
| |- (_ = _)%Z => replace 4 with (2 * 2)%Z in * by ring; |
| 124 |
rewrite <-Z.div_div by Z.zero_bounds; |
| 125 |
rewrite Z.add_diag, Z.mul_add_distr_l, Z.mul_div_eq by lia |
| 126 |
end. |
| 127 |
Qed. |
| 128 |
End SquareRootsPrime3Mod4. |
| 129 |
|
| 130 |
Section SquareRootsPrime5Mod8. |
| 131 |
Context {q:positive} {prime_q: prime q} {q_5mod8 : q mod 8 = 5}. |
| 132 |
Local Open Scope F_scope. |
| 133 |
Add Field _field3 : (Algebra.Field.field_theory_for_stdlib_tactic(T:=F q)) |
| 134 |
(morphism (F.ring_morph q), |
| 135 |
constants [F.is_constant], |
| 136 |
div (F.morph_div_theory q), |
| 137 |
power_tac (F.power_theory q) [F.is_pow_constant]). |
| 138 |
|
| 139 |
(* Any nonsquare element raised to (q-1)/4 (real implementations use 2 ^ ((q-1)/4) ) |
| 140 |
would work for sqrt_minus1 *) |
| 141 |
Context (sqrt_minus1 : F q) (sqrt_minus1_valid : sqrt_minus1 * sqrt_minus1 = F.opp 1). |
| 142 |
|
| 143 |
Lemma two_lt_q_5mod8 : 2 < q. |
| 144 |
Proof using prime_q q_5mod8. |
| 145 |
pose proof (prime_ge_2 q _) as two_le_q. |
| 146 |
destruct (Zle_lt_or_eq _ _ two_le_q) as [H|H]; [exact H|]. |
| 147 |
rewrite <-H in *. discriminate. |
| 148 |
Qed. |
| 149 |
Local Hint Resolve two_lt_q_5mod8 : core. |
| 150 |
|
| 151 |
Definition sqrt_5mod8 (a : F q) : F q := |
| 152 |
let b := a ^ Z.to_N (q / 8 + 1) in |
| 153 |
if dec (b ^ 2 = a) |
| 154 |
then b |
| 155 |
else sqrt_minus1 * b. |
| 156 |
|
| 157 |
Global Instance Proper_sqrt_5mod8 : Proper (eq ==> eq ) sqrt_5mod8. |
| 158 |
Proof using Type. repeat intro; subst; reflexivity. Qed. |
| 159 |
|
| 160 |
Lemma eq_b4_a2 (x : F q) (Hex:exists y, y*y = x) : |
| 161 |
((x ^ Z.to_N (q / 8 + 1)) ^ 2) ^ 2 = x ^ 2. |
| 162 |
Proof using prime_q q_5mod8. |
| 163 |
pose proof two_lt_q_5mod8. |
| 164 |
assert (0 <= q/8)%Z by (apply Z.div_le_lower_bound; rewrite ?Z.mul_0_r; lia). |
| 165 |
assert (Z.to_N (q / 8 + 1) <> 0%N) by |
| 166 |
(intro Hbad; change (0%N) with (Z.to_N 0%Z) in Hbad; rewrite Z2N.inj_iff in Hbad; lia). |
| 167 |
destruct (dec (x = 0)); [subst; rewrite !F.pow_0_l by (trivial || lazy_decide); reflexivity|]. |
| 168 |
rewrite !F.pow_pow_l. |
| 169 |
|
| 170 |
replace (Z.to_N (q / 8 + 1) * (2*2))%N with (Z.to_N (q / 2 + 2))%N. |
| 171 |
2: { (* this is a boring but gnarly proof :/ *) |
| 172 |
change (2*2)%N with (Z.to_N 4). |
| 173 |
rewrite <- Z2N.inj_mul by Z.zero_bounds. |
| 174 |
apply Z2N.inj_iff; try Z.zero_bounds. |
| 175 |
rewrite <- Z.mul_cancel_l with (p := 2) by lia. |
| 176 |
ring_simplify. |
| 177 |
rewrite Z.mul_div_eq by lia. |
| 178 |
rewrite Z.mul_div_eq by lia. |
| 179 |
rewrite (Zmod_div_mod 2 8 q) by |
| 180 |
(try lia; apply Zmod_divide; lia || auto). |
| 181 |
rewrite q_5mod8. |
| 182 |
replace (5 mod 2)%Z with 1%Z by auto. |
| 183 |
ring. |
| 184 |
} |
| 185 |
|
| 186 |
rewrite Z2N.inj_add, F.pow_add_r by Z.zero_bounds. |
| 187 |
replace (x ^ Z.to_N (q / 2)) with (F.of_Z q 1) by |
| 188 |
(symmetry; apply @euler_criterion; eauto). |
| 189 |
change (Z.to_N 2) with 2%N; ring. |
| 190 |
Qed. |
| 191 |
|
| 192 |
Lemma mul_square_sqrt_minus1 : forall x, sqrt_minus1 * x * (sqrt_minus1 * x) = F.opp (x * x). |
| 193 |
Proof using prime_q sqrt_minus1_valid. |
| 194 |
intros x. |
| 195 |
transitivity (F.opp 1 * (x * x)); [ | field]. |
| 196 |
rewrite <-sqrt_minus1_valid. |
| 197 |
field. |
| 198 |
Qed. |
| 199 |
|
| 200 |
Lemma eq_b4_a2_iff (x : F q) : x <> 0 -> |
| 201 |
((exists y, y*y = x) <-> ((x ^ Z.to_N (q / 8 + 1)) ^ 2) ^ 2 = x ^ 2). |
| 202 |
Proof using Type*. |
| 203 |
split; try apply eq_b4_a2. |
| 204 |
intro Hyy. |
| 205 |
rewrite [email protected]_2_r in *. |
| 206 |
destruct (Field.only_two_square_roots_choice _ x (x * x) Hyy eq_refl); clear Hyy; |
| 207 |
[ eexists; eassumption | ]. |
| 208 |
match goal with H : ?a * ?a = F.opp _ |- _ => exists (sqrt_minus1 * a); |
| 209 |
rewrite mul_square_sqrt_minus1; rewrite H end. |
| 210 |
field. |
| 211 |
Qed. |
| 212 |
|
| 213 |
Lemma sqrt_5mod8_correct : forall x, |
| 214 |
((exists y, y*y = x) <-> (sqrt_5mod8 x)*(sqrt_5mod8 x) = x). |
| 215 |
Proof using Type*. |
| 216 |
cbv [sqrt_5mod8]; intros x. |
| 217 |
destruct (F.eq_dec x 0). |
| 218 |
{ |
| 219 |
repeat match goal with |
| 220 |
| |- _ => progress subst |
| 221 |
| |- _ => progress rewrite ?F.pow_0_l |
| 222 |
| |- (_ <> _)%N => rewrite <-Z2N.inj_0, Z2N.inj_iff by Z.zero_bounds |
| 223 |
| |- (?a <> 0)%Z => assert (0 < a) by Z.zero_bounds; lia |
| 224 |
| |- _ => congruence |
| 225 |
end. |
| 226 |
break_match; |
| 227 |
match goal with |- _ <-> ?G => assert G by field end; intuition eauto. |
| 228 |
} { |
| 229 |
rewrite eq_b4_a2_iff by auto. |
| 230 |
rewrite [email protected]_2_r in *. |
| 231 |
break_match. |
| 232 |
intuition (f_equal; eauto). |
| 233 |
split; intro A. { |
| 234 |
destruct (Field.only_two_square_roots_choice _ x (x * x) A eq_refl) as [B | B]; |
| 235 |
clear A; try congruence. |
| 236 |
rewrite mul_square_sqrt_minus1, B; field. |
| 237 |
} { |
| 238 |
rewrite mul_square_sqrt_minus1 in A. |
| 239 |
transitivity (F.opp x * F.opp x); [ | field ]. |
| 240 |
f_equal; rewrite <-A at 3; field. |
| 241 |
} |
| 242 |
} |
| 243 |
Qed. |
| 244 |
End SquareRootsPrime5Mod8. |
| 245 |
|
| 246 |
Module Iso. |
| 247 |
Section IsomorphicRings. |
| 248 |
Context {q:positive} {prime_q:prime q} {two_lt_q:2 < Z.pos q}. |
| 249 |
Context |
| 250 |
{H : Type} {eq : H -> H -> Prop} {zero one : H} |
| 251 |
{opp : H -> H} {add sub mul : H -> H -> H} |
| 252 |
{phi : F q -> H} {phi' : H -> F q} |
| 253 |
{phi'_phi : forall A : F q, Logic.eq (phi' (phi A)) A} |
| 254 |
{phi'_iff : forall a b : H, iff (Logic.eq (phi' a) (phi' b)) (eq a b)} |
| 255 |
{phi'_zero : Logic.eq (phi' zero) F.zero} {phi'_one : Logic.eq (phi' one) F.one} |
| 256 |
{phi'_opp : forall a : H, Logic.eq (phi' (opp a)) (F.opp (phi' a))} |
| 257 |
{phi'_add : forall a b : H, Logic.eq (phi' (add a b)) (F.add (phi' a) (phi' b))} |
| 258 |
{phi'_sub : forall a b : H, Logic.eq (phi' (sub a b)) (F.sub (phi' a) (phi' b))} |
| 259 |
{phi'_mul : forall a b : H, Logic.eq (phi' (mul a b)) (F.mul (phi' a) (phi' b))}. |
| 260 |
(* TODO: revive this once we figure out how to spec the pow impl |
| 261 |
Definition inv (x:H) := pow x (NtoP (Z.to_N (q - 2)%Z)). |
| 262 |
Definition div x y := mul (inv y) x. |
| 263 |
|
| 264 |
Lemma ring : |
| 265 |
@Algebra.Hierarchy.ring H eq zero one opp add sub mul |
| 266 |
/\ @Ring.is_homomorphism (F q) Logic.eq F.one F.add F.mul H eq one add mul phi |
| 267 |
/\ @Ring.is_homomorphism H eq one add mul (F q) Logic.eq F.one F.add F.mul phi'. |
| 268 |
Proof using phi'_add phi'_iff phi'_mul phi'_one phi'_opp phi'_phi phi'_sub phi'_zero. eapply @Ring.ring_by_isomorphism; assumption || exact _. Qed. |
| 269 |
Local Instance _iso_ring : Algebra.Hierarchy.ring := proj1 ring. |
| 270 |
Local Instance _iso_hom1 : Ring.is_homomorphism := proj1 (proj2 ring). |
| 271 |
Local Instance _iso_hom2 : Ring.is_homomorphism := proj2 (proj2 ring). |
| 272 |
|
| 273 |
Let inv_proof : forall a : H, phi' (inv a) = F.inv (phi' a). |
| 274 |
Proof. |
| 275 |
intros. |
| 276 |
cbv [inv]. rewrite (Fq_inv_fermat(q:=q)(two_lt_q:=two_lt_q)). |
| 277 |
rewrite <-Z_nat_N at 1 2. |
| 278 |
rewrite (ScalarMult.homomorphism_scalarmult(phi:=phi')(MUL_is_scalarmult:=pow_is_scalarmult)(mul_is_scalarmult:=F.pow_is_scalarmult)). |
| 279 |
reflexivity. |
| 280 |
assumption. |
| 281 |
Qed. |
| 282 |
|
| 283 |
Let div_proof : forall a b : H, phi' (mul (inv b) a) = phi' a / phi' b. |
| 284 |
Proof. |
| 285 |
intros. |
| 286 |
rewrite phi'_mul, inv_proof, Algebra.Hierarchy.field_div_definition, Algebra.Hierarchy.commutative. |
| 287 |
reflexivity. |
| 288 |
Qed. |
| 289 |
|
| 290 |
Lemma field_and_iso : |
| 291 |
@Algebra.Hierarchy.field H eq zero one opp add sub mul inv div |
| 292 |
/\ @Ring.is_homomorphism (F q) Logic.eq F.one F.add F.mul H eq one add mul phi |
| 293 |
/\ @Ring.is_homomorphism H eq one add mul (F q) Logic.eq F.one F.add F.mul phi'. |
| 294 |
Proof using Type*. eapply @Field.field_and_homomorphism_from_redundant_representation; |
| 295 |
assumption || exact _ || exact inv_proof || exact div_proof. Qed. |
| 296 |
*) |
| 297 |
End IsomorphicRings. |
| 298 |
End Iso. |
| 299 |
End F. |