| 1 |
From Coq Require Import BinIntDef. |
| 2 |
From Coq Require Import BinNatDef. |
| 3 |
From Coq Require Import BinPosDef. |
| 4 |
|
| 5 |
From Coq Require Znumtheory BinNums. |
| 6 |
|
| 7 |
Require Crypto.Arithmetic.ModularArithmeticPre. |
| 8 |
|
| 9 |
Delimit Scope positive_scope with positive. |
| 10 |
Bind Scope positive_scope with BinPos.positive. |
| 11 |
Infix "+" := BinPos.Pos.add : positive_scope. |
| 12 |
Infix "*" := BinPos.Pos.mul : positive_scope. |
| 13 |
Infix "-" := BinPos.Pos.sub : positive_scope. |
| 14 |
Infix "^" := BinPos.Pos.pow : positive_scope. |
| 15 |
|
| 16 |
Delimit Scope N_scope with N. |
| 17 |
Bind Scope N_scope with BinNums.N. |
| 18 |
Infix "+" := BinNat.N.add : N_scope. |
| 19 |
Infix "*" := BinNat.N.mul : N_scope. |
| 20 |
Infix "-" := BinNat.N.sub : N_scope. |
| 21 |
Infix "/" := BinNat.N.div : N_scope. |
| 22 |
Infix "^" := BinNat.N.pow : N_scope. |
| 23 |
|
| 24 |
Delimit Scope Z_scope with Z. |
| 25 |
Bind Scope Z_scope with BinInt.Z. |
| 26 |
Infix "+" := BinInt.Z.add : Z_scope. |
| 27 |
Infix "*" := BinInt.Z.mul : Z_scope. |
| 28 |
Infix "-" := BinInt.Z.sub : Z_scope. |
| 29 |
Infix "/" := BinInt.Z.div : Z_scope. |
| 30 |
Infix "^" := BinInt.Z.pow : Z_scope. |
| 31 |
Infix "mod" := BinInt.Z.modulo (at level 40, no associativity) : Z_scope. |
| 32 |
|
| 33 |
Local Open Scope Z_scope. |
| 34 |
Global Coercion BinInt.Z.pos : BinPos.positive >-> BinInt.Z. |
| 35 |
Global Coercion BinInt.Z.of_N : BinNums.N >-> BinInt.Z. |
| 36 |
Global Set Printing Coercions. |
| 37 |
|
| 38 |
Module F. |
| 39 |
Definition F (m : BinPos.positive) := { z : BinInt.Z | z = z mod m }. |
| 40 |
Local Obligation Tactic := cbv beta; auto using ModularArithmeticPre.Z_mod_mod. |
| 41 |
Program Definition of_Z m (a:BinNums.Z) : F m := a mod m. |
| 42 |
Definition to_Z {m} (a:F m) : BinNums.Z := proj1_sig a. |
| 43 |
|
| 44 |
Section FieldOperations. |
| 45 |
Context {m : BinPos.positive}. |
| 46 |
Definition zero : F m := of_Z m 0. |
| 47 |
Definition one : F m := of_Z m 1. |
| 48 |
|
| 49 |
Definition add (a b:F m) : F m := of_Z m (to_Z a + to_Z b). |
| 50 |
Definition mul (a b:F m) : F m := of_Z m (to_Z a * to_Z b). |
| 51 |
Definition opp (a : F m) : F m := of_Z m (0 - to_Z a). |
| 52 |
Definition sub (a b:F m) : F m := add a (opp b). |
| 53 |
|
| 54 |
Definition inv_with_spec : { inv : F m -> F m |
| 55 |
| inv zero = zero |
| 56 |
/\ ( Znumtheory.prime m -> |
| 57 |
forall a, a <> zero -> mul (inv a) a = one ) |
| 58 |
} := ModularArithmeticPre.inv_impl. |
| 59 |
Definition inv : F m -> F m := Eval hnf in proj1_sig inv_with_spec. |
| 60 |
Definition div (a b:F m) : F m := mul a (inv b). |
| 61 |
|
| 62 |
Definition pow_with_spec : { pow : F m -> BinNums.N -> F m |
| 63 |
| forall a, pow a 0%N = one |
| 64 |
/\ forall x, pow a (1 + x)%N = mul a (pow a x) |
| 65 |
} := ModularArithmeticPre.pow_impl. |
| 66 |
Definition pow : F m -> BinNums.N -> F m := Eval hnf in proj1_sig pow_with_spec. |
| 67 |
End FieldOperations. |
| 68 |
|
| 69 |
Definition of_nat m (n:nat) := F.of_Z m (BinInt.Z.of_nat n). |
| 70 |
Definition to_nat {m} (x:F m) := BinInt.Z.to_nat (F.to_Z x). |
| 71 |
Notation nat_mod := of_nat (only parsing). |
| 72 |
|
| 73 |
Definition of_N m n := F.of_Z m (BinInt.Z.of_N n). |
| 74 |
Definition to_N {m} (x:F m) := BinInt.Z.to_N (F.to_Z x). |
| 75 |
Notation N_mod := of_N (only parsing). |
| 76 |
|
| 77 |
Notation Z_mod := of_Z (only parsing). |
| 78 |
End F. |
| 79 |
|
| 80 |
Notation F := F.F. |
| 81 |
Declare Scope F_scope. |
| 82 |
Delimit Scope F_scope with F. |
| 83 |
Bind Scope F_scope with F.F. |
| 84 |
Infix "+" := F.add : F_scope. |
| 85 |
Infix "*" := F.mul : F_scope. |
| 86 |
Infix "-" := F.sub : F_scope. |
| 87 |
Infix "/" := F.div : F_scope. |
| 88 |
Infix "^" := F.pow : F_scope. |
| 89 |
Notation "0" := F.zero : F_scope. |
| 90 |
Notation "1" := F.one : F_scope. |