Debian Codegraph @

src/Spec/Curve25519.v

raw
1 From Coq Require Import BinPosDef.
2 Require Import Spec.ModularArithmetic.
3 Local Open Scope positive_scope.
4
5 Notation p := (2^255-19).
6 Notation l := (2^252 + 27742317777372353535851937790883648493).
7 Notation l2 := (2^253 - 55484635554744707071703875581767296995).
8 Notation order := (8*l).
9 Notation twist_order := (4*l2).
10
11 Lemma orders_match : (2*(p + 1) - order = twist_order)%Z. Proof. exact eq_refl. Qed.
12
13 From Coq Require Import Znumtheory List. Import ListNotations.
14 From Coqprime.PrimalityTest Require Import Pocklington PocklingtonCertificat.
15
16 Lemma prime_p : prime p.
17 Proof.
18 simple refine (Pocklington_refl (Pock_certif _ 2
19 [(74058212732561358302231226437062788676166966415465897661863160754340907, 1); (2, 2)] 1)
20 [ Pock_certif 74058212732561358302231226437062788676166966415465897661863160754340907 2
21 [(31757755568855353, 1); (57467, 1); (353, 1); (2, 1)] 2028478494862525422475606;
22 Pock_certif 31757755568855353 10 [(223, 1); (107, 1); (3, 1); (2, 3)] 539560;
23 Pock_certif 57467 2 [(59, 1); (2, 1)] 14;
24 Pock_certif 353 3 [(2, 5)] 1;
25 Pock_certif 223 3 [(3, 1); (2, 1)] 1;
26 Pock_certif 107 2 [(53, 1); (2, 1)] 1;
27 Pock_certif 59 2 [(29, 1); (2, 1)] 1;
28 Pock_certif 53 2 [(2, 2)] 4;
29 Pock_certif 29 2 [(2, 2)] 1;
30 Proof_certif 3 prime_3;
31 Proof_certif 2 prime_2
32 ] _).
33 native_cast_no_check (@eq_refl bool true).
34 Qed. (* 1s *)
35
36 Lemma prime_l : prime l.
37 Proof.
38 simple refine (Pocklington_simple_computational l 276602624281642239937218680557139826668747 2
39 (Pocklington_refl(Pock_certif _ 2 [(19757330305831588566944191468367130476339, 1); (2, 1)] 1)
40 [Pock_certif 19757330305831588566944191468367130476339 2 [(213441916511, 1); (269, 1); (2, 1)] 150181583887270;
41 Pock_certif 213441916511 13 [(292386187, 1); (2, 1)] 1;
42 Pock_certif 292386187 2 [(307, 1); (2, 1)] 961;
43 Pock_certif 307 5 [(3, 1); (2, 1)] 1;
44 Pock_certif 269 2 [(67, 1); (2, 2)] 1; Pock_certif 67 2 [(3, 1); (2, 1)] 1;
45 Proof_certif 3 prime_3; Proof_certif 2 prime_2] _)
46 _ _ _ _ _ _ _ _).
47 1: native_cast_no_check (@eq_refl bool true).
48 all : native_compute; exact eq_refl.
49 Qed. (* 1.3s *)
50
51 Lemma prime_l2 : prime l2.
52 Proof.
53 simple refine (Pocklington_refl (Pock_certif _ 2
54 [(27413359092552162435694767700453926735143482401279781, 1); (2, 2)] 1)
55 [Pock_certif 27413359092552162435694767700453926735143482401279781 2
56 [(104719073621178708975837602950775180438320278101, 1); (2, 2)] 1;
57 Pock_certif 104719073621178708975837602950775180438320278101 2
58 [(203852586375664218368381551393371968928013, 1); (2, 2)] 1;
59 Pock_certif 203852586375664218368381551393371968928013 2
60 [(1013266244677, 1); (743104567, 1); (2, 2)] 1;
61 Pock_certif 1013266244677 2 [(383, 1); (7, 1); (2, 2)] 7406;
62 Pock_certif 743104567 3 [(3637, 1); (2, 1)] 322;
63 Pock_certif 3637 2 [(3, 1); (2, 2)] 11;
64 Pock_certif 383 5 [(191, 1); (2, 1)] 1;
65 Pock_certif 191 7 [(5, 1); (2, 1)] 1; Pock_certif 7 3 [(2, 1)] 1;
66 Pock_certif 5 2 [(2, 2)] 1;
67 Proof_certif 3 prime_3;
68 Proof_certif 2 prime_2] _).
69 native_cast_no_check (@eq_refl bool true).
70 Time Qed. (* 1s *)
71
72 Local Notation F := (F p).
73
74 Require PrimeFieldTheorems.
75
76 Lemma field : @Hierarchy.field F eq F.zero F.one F.opp F.add F.sub F.mul F.inv F.div.
77 Proof. apply PrimeFieldTheorems.F.field_modulo, prime_p. Qed.
78 Lemma char_ge_3 : @Ring.char_ge F eq F.zero F.one F.opp F.add F.sub F.mul 3.
79 Proof. eapply Hierarchy.char_ge_weaken; try apply ModularArithmeticTheorems.F.char_gt; Decidable.vm_decide. Qed.
80
81 Require Import Spec.CompleteEdwardsCurve.
82 Module E.
83 Definition a : F := F.opp 1.
84 Definition d : F := F.div (F.opp (F.of_Z _ 121665)) (F.of_Z _ 121666).
85
86 Lemma nonzero_a : a <> F.zero. Proof. Decidable.vm_decide. Qed.
87 Lemma square_a : exists sqrt_a, F.mul sqrt_a sqrt_a = a.
88 Proof. epose (@PrimeFieldTheorems.F.Decidable_square p prime_p eq_refl); Decidable.vm_decide. Qed.
89 Lemma nonsquare_d : forall x, F.mul x x <> d.
90 Proof. epose (@PrimeFieldTheorems.F.Decidable_square p prime_p eq_refl); Decidable.vm_decide. Qed.
91
92 Definition point := @E.point F eq F.one F.add F.mul a d.
93 Definition add := E.add(field:=field)(char_ge_3:=char_ge_3)(a:=a)(d:=d)
94 (nonzero_a:=nonzero_a)(square_a:=square_a)(nonsquare_d:=nonsquare_d).
95 Definition zero := E.zero(field:=field)(a:=a)(d:=d)(nonzero_a:=nonzero_a).
96 Definition B : E.point.
97 refine (
98 exist _ (F.of_Z _ 15112221349535400772501151409588531511454012693041857206046113283949847762202, F.div (F.of_Z _ 4) (F.of_Z _ 5)) _).
99 Decidable.vm_decide.
100 Defined.
101 End E.
102
103 Require Import Spec.MontgomeryCurve.
104 Module M.
105 Definition a : F := F.of_Z _ 486662.
106 Definition b : F := F.one.
107 Definition a24 : F := ((a - F.of_Z _ 2) / F.of_Z _ 4)%F.
108 Definition point := @M.point F eq F.add F.mul a b.
109 Definition B : point :=
110 exist _ (inl (F.of_Z _ 9, F.of_Z _ 14781619447589544791020593568409986887264606134616475288964881837755586237401)) eq_refl.
111
112 Lemma a2m4_nonzero : F.sub (F.mul a a) (F.of_Z _ 4) <> F.zero. Decidable.vm_decide. Qed.
113 Lemma a2m4_nonsq : ~(exists r, F.mul r r = F.sub (F.mul a a) (F.of_Z _ 4)).
114 Proof. epose (@PrimeFieldTheorems.F.Decidable_square p prime_p eq_refl); Decidable.vm_decide. Qed.
115 Lemma b_nonzero : b <> F.zero. Decidable.vm_decide. Qed.
116
117 Definition add := (M.add(field:=field)(char_ge_3:=char_ge_3)(a:=a)(b_nonzero:=b_nonzero)).
118 Definition opp := (M.opp(field:=field)(a:=a)(b_nonzero:=b_nonzero)).
119 Definition X0 := (M.X0(Feq:=eq)(Fzero:=F.zero)(Fadd:=F.add)(Fmul:=F.mul)(a:=a)(b:=b)).
120 Definition scalarmult := (@ScalarMult.scalarmult_ref _ add M.zero opp).
121 End M.
122
123 Definition clamp k := let s := k/8 mod 2^251 in 8*(2^251 + s).