-
Notifications
You must be signed in to change notification settings - Fork 1
/
Primisc.v
1811 lines (1690 loc) · 49.2 KB
/
Primisc.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(* experiments with primes... *)
Set Nested Proofs Allowed.
Require Import Utf8 Arith.
Import List List.ListNotations.
Require Import Misc Primes.
Require Import Totient QuadRes.
Theorem prime_pow_φ : ∀ p, prime p →
∀ k, k ≠ 0 → φ (p ^ k) = p ^ (k - 1) * φ p.
Proof.
intros * Hp * Hk.
rewrite (prime_φ p); [ | easy ].
destruct (Nat.eq_dec p 0) as [Hpz| Hpz]; [ now subst p | ].
unfold φ.
unfold coprimes.
rewrite (filter_ext_in _ (λ d, negb (d mod p =? 0))). 2: {
intros a Ha.
apply in_seq in Ha.
rewrite Nat.add_comm, Nat.sub_add in Ha. 2: {
apply Nat.neq_0_lt_0.
now apply Nat.pow_nonzero.
}
remember (a mod p) as r eqn:Hr; symmetry in Hr.
destruct r. {
apply Nat.eqb_neq.
apply Nat.mod_divides in Hr; [ | easy ].
destruct Hr as (d, Hd).
rewrite Hd.
destruct k; [ easy | cbn ].
rewrite Nat.gcd_mul_mono_l.
intros H.
apply Nat.eq_mul_1 in H.
destruct H as (H, _).
now subst p.
} {
apply Nat.eqb_eq.
assert (Hg : Nat.gcd p a = 1). {
rewrite <- Nat.gcd_mod; [ | easy ].
rewrite Nat.gcd_comm.
apply eq_gcd_prime_small_1; [ easy | ].
split; [ rewrite Hr; flia | ].
now apply Nat.mod_upper_bound.
}
clear - Hg.
induction k; [ easy | ].
now apply Nat_gcd_1_mul_l.
}
}
clear Hp.
replace k with (k - 1 + 1) at 1 by flia Hk.
rewrite Nat.pow_add_r, Nat.pow_1_r.
remember (p ^ (k - 1)) as a eqn:Ha.
clear k Hk Ha Hpz.
induction a; [ easy | ].
cbn.
destruct (Nat.eq_dec p 0) as [Hpz| Hpz]. {
subst p; cbn.
now rewrite Nat.mul_0_r.
}
destruct (Nat.eq_dec a 0) as [Haz| Haz]. {
subst a; cbn.
do 2 rewrite Nat.add_0_r.
rewrite (filter_ext_in _ (λ d, true)). 2: {
intros a Ha.
apply in_seq in Ha.
rewrite Nat.mod_small; [ | flia Ha ].
destruct a; [ flia Ha | easy ].
}
clear.
destruct p; [ easy | ].
rewrite Nat.sub_succ, Nat.sub_0_r.
induction p; [ easy | ].
rewrite <- (Nat.add_1_r p).
rewrite seq_app, filter_app, app_length.
now rewrite IHp.
}
rewrite <- Nat.add_sub_assoc. 2: {
apply Nat.neq_0_lt_0.
now apply Nat.neq_mul_0.
}
rewrite Nat.add_comm.
rewrite seq_app, filter_app, app_length.
rewrite IHa, Nat.add_comm; f_equal.
rewrite Nat.add_comm, Nat.sub_add. 2: {
apply Nat.neq_0_lt_0.
now apply Nat.neq_mul_0.
}
replace p with (1 + (p - 1)) at 2 by flia Hpz.
rewrite seq_app, filter_app, app_length.
cbn.
rewrite Nat.mod_mul; [ | easy ]; cbn.
rewrite (filter_ext_in _ (λ d, true)). 2: {
intros b Hb.
remember (b mod p) as c eqn:Hc; symmetry in Hc.
destruct c; [ | easy ].
apply Nat.mod_divide in Hc; [ | easy ].
destruct Hc as (c, Hc).
subst b.
apply in_seq in Hb.
destruct Hb as (Hb1, Hb2).
clear - Hb1 Hb2; exfalso.
revert p a Hb1 Hb2.
induction c; intros; [ flia Hb1 | ].
cbn in Hb1, Hb2.
destruct (Nat.eq_dec a 0) as [Haz| Haz]. {
subst a.
cbn in Hb1, Hb2.
destruct p; [ flia Hb1 | ].
rewrite Nat.sub_succ, Nat.sub_0_r in Hb2.
flia Hb2.
}
destruct (Nat.eq_dec p 0) as [Hpz| Hpz]. {
subst p; flia Hb1.
}
specialize (IHc p (a - 1)) as H1.
assert (H : (a - 1) * p + 1 ≤ c * p). {
rewrite Nat.mul_sub_distr_r, Nat.mul_1_l.
rewrite <- Nat.add_sub_swap. 2: {
destruct a; [ easy | ].
cbn; flia.
}
flia Hb1 Haz Hpz.
}
specialize (H1 H); clear H.
apply H1.
apply (Nat.add_lt_mono_l _ _ p).
eapply lt_le_trans; [ apply Hb2 | ].
ring_simplify.
do 2 apply Nat.add_le_mono_r.
rewrite Nat.mul_sub_distr_l, Nat.mul_1_r.
rewrite Nat.sub_add. 2: {
destruct a; [ easy | rewrite Nat.mul_succ_r; flia ].
}
now rewrite Nat.mul_comm.
}
clear.
remember (a * p + 1) as b; clear a Heqb.
destruct p; [ easy | ].
rewrite Nat.sub_succ, Nat.sub_0_r.
revert b.
induction p; intros; [ easy | ].
rewrite <- Nat.add_1_r.
rewrite seq_app, filter_app, app_length.
now rewrite IHp.
Qed.
Theorem divide_add_div_le : ∀ m p q,
2 ≤ p
→ 2 ≤ q
→ Nat.divide p m
→ Nat.divide q m
→ m / p + m / q ≤ m.
Proof.
intros * H2p H2q Hpm Hqm.
destruct Hpm as (kp, Hkp).
destruct Hqm as (kq, Hkq).
destruct (Nat.eq_dec p 0) as [Hpz| Hpz]; [ flia Hpz H2p | ].
destruct (Nat.eq_dec q 0) as [Hqz| Hqz]; [ flia Hqz H2q | ].
rewrite Hkq at 2.
rewrite Nat.div_mul; [ | easy ].
rewrite Hkp at 1.
rewrite Nat.div_mul; [ | easy ].
apply (Nat.mul_le_mono_pos_r _ _ (p * q)). {
destruct p; [ easy | ].
destruct q; [ easy | cbn; flia ].
}
rewrite Nat.mul_add_distr_r.
rewrite Nat.mul_assoc, <- Hkp.
rewrite Nat.mul_assoc, Nat.mul_shuffle0, <- Hkq.
rewrite <- Nat.mul_add_distr_l.
apply Nat.mul_le_mono_l.
rewrite Nat.add_comm.
apply Nat.add_le_mul. {
destruct p; [ easy | ].
destruct p; [ easy | flia ].
} {
destruct q; [ easy | ].
destruct q; [ easy | flia ].
}
Qed.
Theorem length_filter_mod_seq : ∀ a b,
a mod b ≠ 0
→ length (filter (λ d, negb (d mod b =? 0)) (seq a b)) = b - 1.
Proof.
intros a b Hab1.
destruct (Nat.eq_dec b 0) as [Hbz| Hbz]; [ now subst b | ].
specialize (Nat.div_mod a b Hbz) as H1.
remember (a / b) as q eqn:Hq.
remember (a mod b) as r eqn:Hr.
move q after r; move Hq after Hr.
replace b with (b - r + r) at 1. 2: {
apply Nat.sub_add.
now rewrite Hr; apply Nat.lt_le_incl, Nat.mod_upper_bound.
}
rewrite seq_app, filter_app, app_length.
rewrite List_filter_all_true. 2: {
intros c Hc.
apply Bool.negb_true_iff, Nat.eqb_neq.
apply in_seq in Hc.
intros Hcon.
specialize (Nat.div_mod c b Hbz) as H2.
rewrite Hcon, Nat.add_0_r in H2.
remember (c / b) as s eqn:Hs.
subst a c.
clear Hcon.
destruct Hc as (Hc1, Hc2).
rewrite Nat.add_sub_assoc in Hc2. 2: {
rewrite Hr.
now apply Nat.lt_le_incl, Nat.mod_upper_bound.
}
rewrite Nat.add_sub_swap in Hc2; [ | flia ].
rewrite Nat.add_sub in Hc2.
replace b with (b * 1) in Hc2 at 3 by flia.
rewrite <- Nat.mul_add_distr_l in Hc2.
apply Nat.mul_lt_mono_pos_l in Hc2; [ | flia Hbz ].
rewrite Nat.add_1_r in Hc2.
apply Nat.succ_le_mono in Hc2.
apply Nat.nlt_ge in Hc1.
apply Hc1; clear Hc1.
apply (le_lt_trans _ (b * q)); [ | flia Hab1 ].
now apply Nat.mul_le_mono_l.
}
rewrite seq_length.
replace r with (1 + (r - 1)) at 3 by flia Hab1.
rewrite seq_app, filter_app, app_length; cbn.
rewrite H1 at 1.
rewrite Nat.add_sub_assoc. 2: {
rewrite Hr.
now apply Nat.lt_le_incl, Nat.mod_upper_bound.
}
rewrite Nat.add_sub_swap; [ | flia ].
rewrite Nat.add_sub.
rewrite Nat_mod_add_l_mul_l; [ | easy ].
rewrite Nat.mod_same; [ cbn | easy ].
rewrite List_filter_all_true. 2: {
intros c Hc.
apply Bool.negb_true_iff, Nat.eqb_neq.
apply in_seq in Hc.
intros Hcon.
specialize (Nat.div_mod c b Hbz) as H2.
rewrite Hcon, Nat.add_0_r in H2.
remember (c / b) as s eqn:Hs.
subst a c.
clear Hcon.
destruct Hc as (Hc1, Hc2).
rewrite Nat.add_sub_assoc in Hc2. 2: {
rewrite Hr.
rewrite Nat_mod_add_l_mul_l; [ | easy ].
rewrite Nat.mod_small; [ flia Hab1 | ].
rewrite Hr.
now apply Nat.mod_upper_bound.
}
rewrite Nat.add_sub_swap in Hc2; [ | flia ].
rewrite Nat.add_sub in Hc2.
rewrite Nat.add_sub_assoc in Hc2. 2: {
rewrite Hr.
now apply Nat.lt_le_incl, Nat.mod_upper_bound.
}
rewrite Nat.sub_add in Hc2; [ | flia ].
rewrite Nat.add_sub_assoc in Hc1. 2: {
rewrite Hr.
now apply Nat.lt_le_incl, Nat.mod_upper_bound.
}
rewrite Nat.add_sub_swap in Hc1; [ | flia ].
rewrite Nat.add_sub in Hc1.
rewrite Nat.add_shuffle0 in Hc2.
apply Nat.nlt_ge in Hc1; apply Hc1; clear Hc1.
rewrite Nat.add_1_r.
apply -> Nat.succ_le_mono.
replace b with (b * 1) at 3 by flia.
rewrite <- Nat.mul_add_distr_l.
apply Nat.mul_le_mono_l.
replace b with (b * 1) in Hc2 at 3 by flia.
rewrite <- Nat.mul_add_distr_l in Hc2.
apply Nat.nlt_ge; intros Hc1.
replace s with ((q + 1) + S (s - (q + 2))) in Hc2 by flia Hc1.
rewrite Nat.mul_add_distr_l in Hc2.
apply Nat.add_lt_mono_l in Hc2.
apply Nat.nle_gt in Hc2; apply Hc2; clear Hc2.
rewrite Nat.mul_comm; cbn.
transitivity b; [ | flia Hc1 ].
rewrite Hr.
now apply Nat.lt_le_incl, Nat.mod_upper_bound.
}
rewrite seq_length.
rewrite Nat.add_sub_assoc; [ | flia Hab1 ].
rewrite Nat.sub_add; [ easy | ].
rewrite Hr.
now apply Nat.lt_le_incl, Nat.mod_upper_bound.
Qed.
Theorem gcd_1_div_mul_exact : ∀ m p q kp kq,
q ≠ 0
→ Nat.gcd p q = 1
→ m = kp * p
→ m = kq * q
→ kp = q * (kp / q).
Proof.
intros * Hqz Hg Hkp Hkq.
rewrite <- Nat.divide_div_mul_exact; [ | easy | ]. 2: {
apply (Nat.gauss _ p). {
rewrite Nat.mul_comm, <- Hkp, Hkq.
now exists kq.
} {
now rewrite Nat.gcd_comm.
}
}
now rewrite Nat.mul_comm, Nat.div_mul.
Qed.
Theorem Nat_gcd_1_mul_divide : ∀ m p q,
Nat.gcd p q = 1
→ Nat.divide p m
→ Nat.divide q m
→ Nat.divide (p * q) m.
Proof.
intros * Hg Hpm Hqm.
destruct (Nat.eq_dec m 0) as [Hmz| Hmz]. {
subst m; cbn.
now exists 0.
}
assert (Hpz : p ≠ 0). {
destruct Hpm as (k, Hk).
now intros H; rewrite H, Nat.mul_0_r in Hk.
}
assert (Hqz : q ≠ 0). {
destruct Hqm as (k, Hk).
now intros H; rewrite H, Nat.mul_0_r in Hk.
}
destruct Hpm as (kp, Hkp).
destruct Hqm as (kq, Hkq).
exists (kp * kq / m).
rewrite Nat.mul_comm.
rewrite Hkp at 2.
rewrite Nat.div_mul_cancel_l; [ | easy | ]. 2: {
intros H; subst kp.
rewrite Hkp in Hkq; cbn in Hkq.
symmetry in Hkq.
apply Nat.eq_mul_0 in Hkq.
destruct Hkq as [H| H]; [ | now subst q ].
now subst kq.
}
rewrite (Nat.mul_comm p), <- Nat.mul_assoc.
rewrite <- Nat.divide_div_mul_exact; [ | easy | ]. 2: {
exists (kq / p).
rewrite Nat.mul_comm.
rewrite Nat.gcd_comm in Hg.
now apply (gcd_1_div_mul_exact m q p kq kp).
}
rewrite (Nat.mul_comm p).
rewrite Nat.div_mul; [ | easy ].
now rewrite Nat.mul_comm.
Qed.
Theorem prime_divisors_decomp : ∀ n a,
a ∈ prime_divisors n ↔ a ∈ prime_decomp n.
Proof.
intros.
split; intros Ha. {
apply filter_In in Ha.
destruct Ha as (Ha, H).
apply Bool.andb_true_iff in H.
destruct H as (Hpa, Hna).
apply Nat.eqb_eq in Hna.
apply in_seq in Ha.
apply Nat.mod_divide in Hna; [ | flia Ha ].
apply prime_decomp_in_iff.
split; [ | split ]; [ flia Ha | easy | easy ].
} {
apply filter_In.
apply prime_decomp_in_iff in Ha.
destruct Ha as (Hnz & Ha & Han).
split. {
apply in_seq.
split. {
transitivity 2; [ flia | ].
now apply prime_ge_2.
} {
destruct Han as (k, Hk); subst n.
destruct k; [ easy | flia ].
}
}
apply Bool.andb_true_iff.
split; [ easy | ].
apply Nat.eqb_eq.
apply Nat.mod_divide in Han; [ easy | ].
now intros H1; subst a.
}
Qed.
Theorem prime_divisors_nil_iff: ∀ n, prime_divisors n = [] ↔ n = 0 ∨ n = 1.
Proof.
intros.
split; intros Hn. {
apply prime_decomp_nil_iff.
remember (prime_decomp n) as l eqn:Hl; symmetry in Hl.
destruct l as [| a l]; [ easy | ].
specialize (proj2 (prime_divisors_decomp n a)) as H1.
rewrite Hl, Hn in H1.
now exfalso; apply H1; left.
} {
now destruct Hn; subst n.
}
Qed.
(* primitive roots *)
Fixpoint prim_root_cycle_loop n g gr it :=
match it with
| 0 => []
| S it' =>
let gr' := (g * gr) mod n in
if gr' =? g then [gr]
else gr :: prim_root_cycle_loop n g gr' it'
end.
Definition prim_root_cycle n g := prim_root_cycle_loop n g g (n - 1).
Definition is_prim_root n g :=
match Nat.gcd g n with
| 1 => length (prim_root_cycle n g) =? φ n
| _ => false
end.
Definition prim_roots n := filter (is_prim_root n) (seq 1 (n - 1)).
Compute (prim_roots 14, φ (φ 14)).
Compute (prim_root_cycle 14 5).
Compute (sort Nat.leb (map (λ i, Nat_pow_mod 5 i 14) (seq 1 14))).
Fixpoint in_list_nat n l :=
match l with
| [] => false
| a :: l => if n =? a then true else in_list_nat n l
end.
Definition is_quad_res p n := in_list_nat n (quad_res p).
Theorem fold_φ : ∀ n, length (coprimes n) = φ n.
Proof. easy. Qed.
Theorem φ_interv : ∀ n, 2 ≤ n → 1 ≤ φ n < n.
Proof.
intros * H2n.
unfold φ.
unfold coprimes.
split. {
destruct n; [ easy | ].
rewrite Nat.sub_succ, Nat.sub_0_r.
destruct n; [ flia H2n | ].
remember (S (S n)) as ssn.
cbn; rewrite Nat.gcd_1_r; cbn; flia.
} {
rewrite List_length_filter_negb; [ | apply seq_NoDup ].
rewrite seq_length.
flia H2n.
}
Qed.
(* multiplicative order modulo *)
Fixpoint ord_mod_aux it n a i :=
match it with
| 0 => 0
| S it' =>
if Nat.eq_dec (Nat_pow_mod a i n) 1 then i
else ord_mod_aux it' n a (i + 1)
end.
Definition ord_mod n a := ord_mod_aux n n a 1.
Theorem List_seq_eq_nil : ∀ a b, seq a b = [] → b = 0.
Proof.
intros * Hs.
now destruct b.
Qed.
Lemma ord_mod_aux_prop : ∀ it n a i,
n + 1 ≤ it + i
→ (∀ j, 1 ≤ j < i → (a ^ j) mod n ≠ 1)
→ 2 ≤ n
→ Nat.gcd a n = 1
→ a ^ ord_mod_aux it n a i mod n = 1 ∧
∀ m, 0 < m < ord_mod_aux it n a i → (a ^ m) mod n ≠ 1.
Proof.
intros * Hnit Hj H2n Hg.
assert (Hnz : n ≠ 0) by flia H2n.
destruct (Nat.eq_dec a 0) as [Haz| Haz]. {
subst a.
rewrite Nat.gcd_0_l in Hg; flia H2n Hg.
}
revert i Hnit Hj.
induction it; intros. {
cbn; cbn in Hnit.
specialize (euler_fermat_little n a Hnz Haz Hg) as H1.
rewrite (Nat.mod_small 1) in H1; [ | easy ].
specialize (Hj (φ n)) as H2.
assert (H : 1 ≤ φ n < i). {
specialize (φ_interv n H2n) as H3.
split; [ easy | ].
transitivity n; [ easy | flia Hnit ].
}
now specialize (H2 H).
}
cbn.
rewrite Nat_pow_mod_is_pow_mod; [ | easy ].
destruct (Nat.eq_dec (a ^ i mod n) 1) as [Ha1| Ha1]. {
split; [ easy | ].
intros m Hm.
now apply Hj.
}
apply IHit; [ flia Hnit | ].
intros k Hk.
destruct (Nat.eq_dec k i) as [Hki| Hki]; [ now subst k | ].
apply Hj; flia Hk Hki.
Qed.
Theorem ord_mod_prop : ∀ n a,
2 ≤ n
→ Nat.gcd a n = 1
→ (a ^ ord_mod n a) mod n = 1 ∧
∀ m, 0 < m < ord_mod n a → (a ^ m) mod n ≠ 1.
Proof.
intros * H2n Hg.
apply ord_mod_aux_prop; [ easy | | easy | easy ].
intros j Hj.
flia Hj.
Qed.
Theorem ord_mod_1_r : ∀ n, 2 ≤ n → ord_mod n 1 = 1.
Proof.
intros * H2n.
destruct n; [ easy | ].
cbn - [ Nat_pow_mod ].
rewrite Nat_pow_mod_is_pow_mod; [ | easy ].
rewrite Nat.pow_1_r.
rewrite Nat.mod_1_l; [ easy | flia H2n ].
Qed.
Lemma eq_ord_mod_aux_0 : ∀ it n a i,
n ≠ 0
→ i ≠ 0
→ ord_mod_aux it n a i = 0
→ ∀ j, i ≤ j < i + it → a ^ j mod n ≠ 1.
Proof.
intros * Hnz Hiz Ho * Hj.
revert i Hiz Ho Hj.
induction it; intros; [ flia Hj | ].
cbn in Ho.
rewrite Nat_pow_mod_is_pow_mod in Ho; [ | easy ].
destruct (Nat.eq_dec (a ^ i mod n) 1) as [Hai| Hai]; [ easy | ].
destruct (Nat.eq_dec i j) as [Hij| Hij]; [ now subst i | ].
apply (IHit (i + 1)); [ flia | easy | ].
split; [ flia Hj Hij | flia Hj ].
Qed.
Theorem ord_mod_neq_0 : ∀ n a, 2 ≤ n → Nat.gcd a n = 1 → ord_mod n a ≠ 0.
Proof.
intros * H2n Hg Ho.
destruct (Nat.eq_dec n 0) as [Hnz| Hnz]; [ now subst n | ].
destruct (Nat.eq_dec a 0) as [Haz| Haz]. {
subst a.
rewrite Nat.gcd_0_l in Hg.
flia Hg H2n.
}
unfold ord_mod in Ho.
specialize (eq_ord_mod_aux_0 n n a 1 Hnz (Nat.neq_succ_0 _) Ho) as H1.
specialize (euler_fermat_little n a Hnz Haz Hg) as H2.
rewrite (Nat.mod_small 1) in H2; [ | easy ].
revert H2; apply H1.
specialize (φ_interv n H2n) as H2.
flia H2.
Qed.
Theorem ord_mod_divisor : ∀ n a b,
Nat.gcd a n = 1
→ a ^ b mod n = 1
→ Nat.divide (ord_mod n a) b.
Proof.
intros * Hg Habn.
destruct (lt_dec n 2) as [H2n| H2n]. {
destruct n; [ easy | ].
destruct n; [ easy | flia H2n ].
}
apply Nat.nlt_ge in H2n.
specialize (ord_mod_prop n a H2n Hg) as H1.
destruct H1 as (Han, Ham).
destruct (Nat.eq_dec b 0) as [Hbz| Hbz]. {
now subst b; exists 0.
}
destruct (lt_dec b (ord_mod n a)) as [Hbo| Hbo]. {
apply Nat.neq_0_lt_0 in Hbz.
now specialize (Ham b (conj Hbz Hbo)) as H1.
}
apply Nat.nlt_ge in Hbo.
destruct (Nat.eq_dec (ord_mod n a) 0) as [Hoz| Hoz]. {
now apply ord_mod_neq_0 in Hoz.
}
specialize (Nat.div_mod b (ord_mod n a) Hoz) as H1.
destruct (Nat.eq_dec (b mod ord_mod n a) 0) as [Hmz| Hmz]. {
rewrite Hmz, Nat.add_0_r in H1.
exists (b / ord_mod n a).
now rewrite Nat.mul_comm.
}
exfalso; apply Hmz; clear Hmz.
assert (H2 : a ^ (b mod ord_mod n a) ≡ 1 mod n). {
rewrite H1 in Habn.
rewrite Nat.pow_add_r in Habn.
rewrite Nat.pow_mul_r in Habn.
rewrite <- Nat.mul_mod_idemp_l in Habn; [ | flia H2n ].
rewrite <- Nat_mod_pow_mod in Habn.
rewrite Han in Habn.
rewrite Nat.pow_1_l in Habn.
rewrite Nat.mod_1_l in Habn; [ | easy ].
rewrite Nat.mul_1_l in Habn.
now rewrite <- (Nat.mod_1_l n) in Habn.
}
rewrite Nat.mod_1_l in H2; [ | easy ].
specialize (Ham (b mod ord_mod n a)) as H3.
remember (ord_mod n a) as x eqn:Hx.
remember (b mod x) as r eqn:Hr.
destruct (Nat.eq_dec r 0) as [Hrz| Hzr]; [ easy | exfalso ].
assert (H : 0 < r < x). {
split; [ flia Hzr | ].
rewrite Hr.
now apply Nat.mod_upper_bound.
}
now specialize (H3 H).
Qed.
(* https://wstein.org/edu/2007/spring/ent/ent-html/node29.html *)
Theorem ord_mod_mul_divide : ∀ n a b r s,
Nat.gcd a n = 1
→ Nat.gcd b n = 1
→ Nat.gcd r s = 1
→ ord_mod n a = r
→ ord_mod n b = s
→ Nat.divide (ord_mod n (a * b)) (r * s).
Proof.
intros * Han Hbn Hg Hoa Hob.
destruct (lt_dec n 2) as [H2n| H2n]. {
destruct n. {
cbn in Hoa, Hob; cbn.
now subst r s.
}
destruct n; [ | flia H2n ].
cbn in Hoa, Hob.
now subst r s.
}
apply Nat.nlt_ge in H2n.
destruct (lt_dec a 2) as [H2a| H2a]. {
destruct a. {
rewrite Nat.gcd_0_l in Han; flia Han H2n.
}
destruct a; [ | flia H2a ].
rewrite ord_mod_1_r in Hoa; [ | easy ].
subst r.
do 2 rewrite Nat.mul_1_l.
now rewrite Hob.
}
apply Nat.nlt_ge in H2a.
destruct (lt_dec b 2) as [H2b| H2b]. {
destruct b. {
rewrite Nat.gcd_0_l in Hbn; flia Hbn H2n.
}
destruct b; [ | flia H2b ].
rewrite ord_mod_1_r in Hob; [ | easy ].
subst s.
do 2 rewrite Nat.mul_1_r.
now rewrite Hoa.
}
apply Nat.nlt_ge in H2b.
assert (H2 : (a * b) ^ (r * s) = a ^ (r * s) * b ^ (r * s)). {
apply Nat.pow_mul_l.
}
specialize (ord_mod_prop n a H2n Han) as Har.
rewrite Hoa in Har.
destruct Har as (Har, Harn).
specialize (ord_mod_prop n b H2n Hbn) as Hbs.
rewrite Hob in Hbs.
destruct Hbs as (Hbs, Hbsn).
apply (f_equal (λ x, x mod n)) in H2.
rewrite (Nat.pow_mul_r a) in H2.
rewrite (Nat.mul_comm r s) in H2.
rewrite (Nat.pow_mul_r b) in H2.
rewrite Nat.mul_mod in H2; [ | flia H2n ].
rewrite <- (Nat_mod_pow_mod (a ^ r)) in H2.
rewrite <- (Nat_mod_pow_mod (b ^ s)) in H2.
rewrite Har, Hbs in H2.
do 2 rewrite Nat.pow_1_l in H2.
rewrite (Nat.mod_small 1) in H2; [ | easy ].
rewrite Nat.mul_1_l in H2.
rewrite (Nat.mod_small 1) in H2; [ | easy ].
rewrite (Nat.mul_comm s r) in H2.
move H2 at bottom.
apply ord_mod_divisor; [ | easy ].
now apply Nat_gcd_1_mul_l.
Qed.
Theorem order_multiplicative : ∀ n a b r s,
Nat.gcd a n = 1
→ Nat.gcd b n = 1
→ ord_mod n a = r
→ ord_mod n b = s
→ Nat.gcd r s = 1
→ ord_mod n (a * b) = r * s.
Proof.
intros * Han Hbn Hoa Hob Hg.
destruct (lt_dec n 2) as [H2n| H2n]. {
destruct n. {
cbn in Hoa; cbn.
now subst r.
}
destruct n; [ | flia H2n ].
cbn in Hoa; cbn.
now subst r.
}
apply Nat.nlt_ge in H2n.
specialize (ord_mod_mul_divide n a b r s Han Hbn Hg Hoa Hob) as H1.
(* https://wstein.org/edu/2007/spring/ent/ent-html/node29.html *)
remember (ord_mod n (a * b)) as d eqn:Hd.
specialize (Nat.divide_mul_split d r s) as H2.
assert (Habn : Nat.gcd (a * b) n = 1) by now apply Nat_gcd_1_mul_l.
assert (H : d ≠ 0). {
rewrite Hd.
now apply ord_mod_neq_0.
}
specialize (H2 H H1); clear H.
destruct H2 as (r1 & s1 & Hrs & Hr & Hs).
specialize (ord_mod_prop n a H2n Han) as (Hao & Ham).
rewrite Hoa in Hao, Ham.
specialize (ord_mod_prop n b H2n Hbn) as (Hbo & Hbm).
rewrite Hob in Hbo, Hbm.
specialize (ord_mod_prop n (a * b) H2n Habn) as (Habo & Habm).
rewrite <- Hd in Habo, Habm.
rewrite Hrs in Habo.
assert (Hrr : r1 = r). {
apply (f_equal (λ x, x ^ (s / s1))) in Habo.
rewrite Nat.pow_1_l in Habo.
apply (f_equal (λ x, x mod n)) in Habo.
rewrite Nat_mod_pow_mod in Habo.
rewrite Nat.mod_1_l in Habo; [ | easy ].
rewrite <- Nat.pow_mul_r in Habo.
rewrite <- Nat.mul_assoc in Habo.
assert (Hs1z : s1 ≠ 0). {
intros H; subst s1.
destruct Hs as (k, Hk).
rewrite Nat.mul_0_r in Hk.
rewrite Hk in Hob.
now apply ord_mod_neq_0 in Hob.
}
rewrite <- Nat.divide_div_mul_exact in Habo; [ | easy | easy ].
rewrite (Nat.mul_comm s1), Nat.div_mul in Habo; [ | easy ].
rewrite (Nat.mul_comm r1) in Habo.
rewrite Nat.pow_mul_r in Habo.
rewrite Nat.pow_mul_l in Habo.
rewrite <- Nat_mod_pow_mod in Habo.
rewrite <- Nat.mul_mod_idemp_r in Habo; [ | flia H2n ].
rewrite Hbo, Nat.mul_1_r in Habo.
rewrite Nat_mod_pow_mod in Habo.
rewrite <- Nat.pow_mul_r in Habo.
assert (H2 : Nat.divide r (s * r1)). {
rewrite <- Hoa.
now apply ord_mod_divisor.
}
apply Nat.gauss in H2; [ | easy ].
move Hr at bottom.
now apply Nat.divide_antisym.
}
assert (Hss : s1 = s). {
clear Hrr.
apply (f_equal (λ x, x ^ (r / r1))) in Habo.
rewrite Nat.pow_1_l in Habo.
apply (f_equal (λ x, x mod n)) in Habo.
rewrite Nat_mod_pow_mod in Habo.
rewrite Nat.mod_1_l in Habo; [ | easy ].
rewrite <- Nat.pow_mul_r in Habo.
rewrite Nat.mul_shuffle0 in Habo.
assert (Hr1z : r1 ≠ 0). {
intros H; subst r1.
destruct Hr as (k, Hk).
rewrite Nat.mul_0_r in Hk.
rewrite Hk in Hoa.
now apply ord_mod_neq_0 in Hoa.
}
rewrite <- Nat.divide_div_mul_exact in Habo; [ | easy | easy ].
rewrite (Nat.mul_comm r1), Nat.div_mul in Habo; [ | easy ].
rewrite Nat.pow_mul_r in Habo.
rewrite Nat.pow_mul_l in Habo.
rewrite <- Nat_mod_pow_mod in Habo.
rewrite <- Nat.mul_mod_idemp_l in Habo; [ | flia H2n ].
rewrite Hao, Nat.mul_1_l in Habo.
rewrite Nat_mod_pow_mod in Habo.
rewrite <- Nat.pow_mul_r in Habo.
assert (H2 : Nat.divide s (r * s1)). {
rewrite <- Hob.
now apply ord_mod_divisor.
}
apply Nat.gauss in H2; [ | now rewrite Nat.gcd_comm ].
move Hs at bottom.
now apply Nat.divide_antisym.
}
now subst r1 s1.
Qed.
Fixpoint list_with_occ l :=
match l with
| [] => []
| x :: l' =>
match list_with_occ l' with
| [] => [(x, 1)]
| (y, c) :: l' =>
if Nat.eq_dec x y then (x, c + 1) :: l'
else (x, 1) :: (y, c) :: l'
end
end.
Definition prime_decomp_pow p := list_with_occ (prime_decomp p).
(* roots of equation x^n ≡ 1 mod p *)
Definition nth_roots_of_unity_modulo n p :=
filter (λ x, Nat_pow_mod x n p =? 1) (seq 1 (p - 1)).
Compute (let '(n, p) := (2, 13) in nth_roots_of_unity_modulo n p).
Compute (let '(n, p) := (4, 13) in nth_roots_of_unity_modulo n p).
Compute (let '(n, p) := (3, 13) in nth_roots_of_unity_modulo n p).
Theorem Couteau : ∀ a b n, Nat.gcd a n = 1 → a ^ (b mod φ n) ≡ a ^ b mod n.
Proof.
intros * Hg.
destruct (lt_dec n 2) as [H2n| H2n]. {
destruct n; [ easy | ].
destruct n; [ easy | flia H2n ].
}
apply Nat.nlt_ge in H2n.
destruct (Nat.eq_dec a 0) as [Haz| Haz]. {
subst a; cbn in Hg; flia Hg H2n.
}
specialize (Nat.div_mod b (φ n)) as H1.
assert (H : φ n ≠ 0). {
specialize (φ_interv n H2n) as H2.
flia H2.
}
specialize (H1 H); clear H.
apply (f_equal (λ x, a ^ x mod n)) in H1.
rewrite Nat.pow_add_r in H1.
rewrite Nat.pow_mul_r in H1.
rewrite <- Nat.mul_mod_idemp_l in H1; [ | flia H2n ].
rewrite <- (Nat_mod_pow_mod (a ^ φ n)) in H1.
rewrite euler_fermat_little in H1; [ | flia H2n | easy | easy ].
rewrite Nat.mod_1_l in H1; [ | easy ].
rewrite Nat.pow_1_l in H1.
rewrite Nat.mod_1_l in H1; [ | easy ].
now rewrite Nat.mul_1_l in H1.
Qed.
Definition prim_roots' p :=
let l := prime_decomp_pow (p - 1) in
let l'' :=
map
(λ '(d, q),
let l1 := nth_roots_of_unity_modulo (d ^ q) p in
let l2 := nth_roots_of_unity_modulo (d ^ (q - 1)) p in
fold_left (λ l x2, remove Nat.eq_dec x2 l) l2 l1)
l
in
fold_left (λ l1 l2, map (λ '(x, y), x * y mod p) (list_prod l1 l2))
l'' [1].
Compute (let p := 31 in (sort Nat.leb (prim_roots' p), (prim_roots p))).
Compute (let p := 31 in combine (sort Nat.leb (prim_roots' p)) (prim_roots p)).
(* j'aimerais bien prouver que prim_roots'(p) donne les racines primitives
modulo p et/ou qu'il en existe au moins une (prim_roots'(p)≠[]) *)
(* c'est censé marcher pour 1,2,4,p^α,2p^α mais ça a pas l'air : ça ne
marche que pour les nombres premiers *)
(* mmm... tout dépend de ce qu'on appelle "racine primitive": pour les
nombres composés, il s'agit de générateurs uniquement des nombres
premiers avec eux. Pour 14, ils ne génèrent que 1,3,5,9,11,13 *)
(* donc, mes définitions prim_roots et prim_roots' ne vont pas *)
Compute (prim_roots 29, prim_roots' 29).
Compute (prim_roots 14, prim_roots' 14).
Compute (sort Nat.leb (map (λ i, Nat_pow_mod 5 i 14) (seq 1 14))).
Compute (let n := 9 in (prim_roots n, length (prim_roots n), φ (φ n))).
Compute (let n := 9 in (prim_roots' n, length (prim_roots' n), φ (φ n))).
Compute (nth_roots_of_unity_modulo 2 27, nth_roots_of_unity_modulo 1 27).
Compute (nth_roots_of_unity_modulo 13 27, nth_roots_of_unity_modulo 13 27).
(* prim_root(n) ≠ [] ↔ n=2,4,p^α,2p^α *)
(* prim_roots seem to work but not prim_roots' (that works only on primes) *)
Compute (let n := 26 in (prim_roots n, sort Nat.leb (prim_roots' n))).
Compute (let n := 6 in sort Nat.leb (map (λ i, Nat_pow_mod 5 i n) (seq 1 (n - 1)))).
Require Import Ring2 Rpolynomial2 Rsummation.
Section In_ring_A.
Context {A : Type}.
Context {rng : ring A}.
Theorem xpow_0 : (ⓧ^0 = 1)%pol.
Proof. now apply eq_poly_eq. Qed.
Theorem lap_convol_mul_succ_r : ∀ la lb i len,
lap_convol_mul la lb i (S len) =
(Σ (j = 0, i), nth j la 0 * nth (i - j) lb 0)%Rng ::
lap_convol_mul la lb (S i) len.
Proof. easy. Qed.
(* http://math.univ-lille1.fr/~fricain/M1-ARITHMETIQUE/chap2.pdf *)
Theorem lt_unique : ∀ a b (lt_ab1 lt_ab2 : a < b), lt_ab1 = lt_ab2.
Proof.
intros.
apply (le_unique (S a) b lt_ab1 lt_ab2).
Qed.
(* representing a natural greater or equal to 2 *)
Class nat2 := mknat2 { number_minus_2 : nat }.
Definition n2 {n : nat2} := number_minus_2 + 2.
Definition mkn n := mknat2 (n - 2).
Class Zn (n : nat2) := mkZn { zn : nat; zn_prop : zn < n2 }.
Arguments zn {_} Zn%nat.
Theorem eq_Zn_eq n : ∀ a b : Zn n, zn a = zn b ↔ a = b.
Proof.
intros (a, apr) (b, bpr).
split; intros Hab; [ | now rewrite Hab ].
cbn in Hab.
subst b.
specialize (lt_unique a n2 apr bpr) as H1.
now subst bpr.
Qed.
Theorem Zn_prop {n : nat2} : ∀ op, op mod n2 < n2.
Proof.
intros.
apply Nat.mod_upper_bound, Nat.neq_0_lt_0.
apply (lt_le_trans _ 2); [ apply Nat.lt_0_succ | ].
unfold n2; flia.
Qed.
Definition Zn_x (n : nat2) x := mkZn n (x mod n2) (Zn_prop x).
Definition Zn_zero {n : nat2} := Zn_x n 0.
Definition Zn_one {n : nat2} := Zn_x n 1.
Definition Zn_add {n : nat2} (a b : Zn n) := Zn_x n (zn a + zn b).
Definition Zn_mul {n : nat2} (a b : Zn n) := Zn_x n (zn a * zn b).
Definition Zn_opp {n : nat2} (a : Zn n) := Zn_x n (n2 - zn a).
Theorem Zn_add_comm n : ∀ a b : Zn n, Zn_add a b = Zn_add b a.
Proof.
intros.
unfold Zn_add.
now rewrite Nat.add_comm.
Qed.
Theorem Zn_add_assoc n : ∀ a b c : Zn n,
Zn_add a (Zn_add b c) = Zn_add (Zn_add a b) c.
Proof.
intros.
destruct (Nat.eq_dec n2 0) as [Hnz| Hnz]. {
now unfold n2 in Hnz; rewrite Nat.add_comm in Hnz.
}
apply eq_Zn_eq; cbn.
rewrite Nat.add_mod_idemp_r; [ | easy ].
rewrite Nat.add_mod_idemp_l; [ | easy ].
now rewrite Nat.add_assoc.
Qed.
Theorem Zn_add_0_l n : ∀ a : Zn n, Zn_add Zn_zero a = a.
Proof.
intros.
destruct (Nat.eq_dec n2 0) as [Hnz| Hnz]. {
now unfold n2 in Hnz; rewrite Nat.add_comm in Hnz.
}
apply eq_Zn_eq; cbn.
rewrite Nat.mod_0_l; [ | easy ].
rewrite Nat.add_0_l.
apply Nat.mod_small.
now destruct a.
Qed.
Theorem Zn_add_opp_l n : ∀ a : Zn n, Zn_add (Zn_opp a) a = Zn_zero.
Proof.
intros.
destruct (Nat.eq_dec n2 0) as [Hnz| Hnz]. {