| 1 |
Require Import PArith BinInt ZArith. |
| 2 |
From Coq Require Import Lia. |
| 3 |
Require Import Crypto.Arithmetic.PrimeFieldTheorems. |
| 4 |
|
| 5 |
Local Open Scope Z_scope. |
| 6 |
|
| 7 |
|
| 8 |
Section __. |
| 9 |
Context {M_pos : positive}. |
| 10 |
|
| 11 |
Lemma solve_F_equality_via_Z lhs' rhs' (lhs rhs : F M_pos) |
| 12 |
: F.to_Z lhs = lhs' mod Z.pos M_pos -> |
| 13 |
F.to_Z rhs = rhs' mod Z.pos M_pos -> |
| 14 |
lhs' = rhs' -> |
| 15 |
lhs = rhs. |
| 16 |
Proof. |
| 17 |
intros. |
| 18 |
rewrite <- (F.of_Z_to_Z lhs). |
| 19 |
rewrite <- (F.of_Z_to_Z rhs). |
| 20 |
intuition congruence. |
| 21 |
Qed. |
| 22 |
|
| 23 |
|
| 24 |
(*TODO: add remaining homomorphisms. |
| 25 |
To support additional operations, prove a lemma like the ones below |
| 26 |
and then add it to the `F_convert_to_Z` tactic. |
| 27 |
*) |
| 28 |
|
| 29 |
Lemma F_mul_to_Z a a' b b' |
| 30 |
: F.to_Z a = a' mod Z.pos M_pos -> |
| 31 |
F.to_Z b = b' mod Z.pos M_pos -> |
| 32 |
@F.to_Z M_pos (a * b) = (a' * b') mod Z.pos M_pos. |
| 33 |
Proof. |
| 34 |
intros H H0. |
| 35 |
rewrite F.to_Z_mul. |
| 36 |
rewrite H, H0. |
| 37 |
rewrite <- PullPush.Z.mul_mod_l. |
| 38 |
rewrite <- PullPush.Z.mul_mod_r. |
| 39 |
congruence. |
| 40 |
Qed. |
| 41 |
|
| 42 |
Lemma F_add_to_Z a a' b b' |
| 43 |
: F.to_Z a = a' mod Z.pos M_pos -> |
| 44 |
F.to_Z b = b' mod Z.pos M_pos -> |
| 45 |
@F.to_Z M_pos (a + b) = (a' + b') mod Z.pos M_pos. |
| 46 |
Proof. |
| 47 |
intros H H0. |
| 48 |
rewrite F.to_Z_add. |
| 49 |
rewrite H, H0. |
| 50 |
rewrite <- PullPush.Z.add_mod_l. |
| 51 |
rewrite <- PullPush.Z.add_mod_r. |
| 52 |
congruence. |
| 53 |
Qed. |
| 54 |
|
| 55 |
|
| 56 |
Lemma F_pow_to_Z a a' c |
| 57 |
: F.to_Z a = a' mod Z.pos M_pos -> |
| 58 |
@F.to_Z M_pos (a ^ c) = (a' ^ c) mod Z.pos M_pos. |
| 59 |
Proof. |
| 60 |
intros H. |
| 61 |
rewrite F.to_Z_pow. |
| 62 |
rewrite H. |
| 63 |
rewrite <- PullPush.Z.pow_mod_full. |
| 64 |
congruence. |
| 65 |
Qed. |
| 66 |
|
| 67 |
Lemma F_var_to_Z (x : F M_pos) : F.to_Z x = proj1_sig x mod Z.pos M_pos. |
| 68 |
Proof. |
| 69 |
destruct x; simpl; assumption. |
| 70 |
Qed. |
| 71 |
|
| 72 |
Lemma F_one_to_Z : @F.to_Z M_pos 1 = 1 mod Z.pos M_pos. |
| 73 |
Proof. |
| 74 |
reflexivity. |
| 75 |
Qed. |
| 76 |
|
| 77 |
|
| 78 |
Lemma F_const_to_Z c : F.to_Z (F.of_Z M_pos c) = c mod Z.pos M_pos. |
| 79 |
Proof. |
| 80 |
reflexivity. |
| 81 |
Qed. |
| 82 |
|
| 83 |
End __. |
| 84 |
|
| 85 |
|
| 86 |
Ltac F_convert_to_Z := |
| 87 |
solve [repeat |
| 88 |
let e := lazymatch goal with |- F.to_Z ?x = _ => x end in |
| 89 |
first [ simple eapply F_mul_to_Z |
| 90 |
| simple eapply F_add_to_Z |
| 91 |
| simple eapply F_one_to_Z |
| 92 |
| simple eapply F_pow_to_Z |
| 93 |
| simple eapply F_const_to_Z |
| 94 |
(* must be last *) |
| 95 |
| simple eapply F_var_to_Z]]. |
| 96 |
|
| 97 |
(*TODO: doesn't prepare hypotheses. |
| 98 |
To support working with hypotheses will require a variation on `solve_F_equality_via_Z` |
| 99 |
that goes in the other direction. |
| 100 |
Since Z->F isn't injective, it will require some thinking. |
| 101 |
*) |
| 102 |
Ltac F_zify := |
| 103 |
intros; |
| 104 |
lazymatch goal with |
| 105 |
| [|- ?lhs = ?rhs] => |
| 106 |
simple eapply solve_F_equality_via_Z; |
| 107 |
[F_convert_to_Z | F_convert_to_Z | ] |
| 108 |
end. |
| 109 |
|
| 110 |
Ltac F_lia := F_zify; (lia || fail "F_lia failed; check that all necessary homomorphisms have been added"). |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
Section Example. |
| 115 |
Context {M_pos : positive}. |
| 116 |
|
| 117 |
Goal forall (x : F M_pos), (x + F.of_Z _ 4 * x)%F = ( x * F.of_Z _ 2 + F.of_Z _ 2 * x + 1 * 1 * x)%F. |
| 118 |
Proof. |
| 119 |
F_lia. |
| 120 |
Qed. |
| 121 |
End Example. |