Datasets:

Modalities:
Text
Languages:
English
Libraries:
Datasets
License:
File size: 9,199 Bytes
4365a98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
(* ========================================================================= *)
(* curve25519, in its plain Montgomery form.                                 *)
(* ========================================================================= *)

needs "EC/montgomery.ml";;
needs "EC/excluderoots.ml";;
needs "EC/computegroup.ml";;

add_curve montgomery_curve;;
add_curveneg montgomery_neg;;
add_curveadd montgomery_add;;

(* ------------------------------------------------------------------------- *)
(* Parameters for curve25519. Here  n_25519 is the large prime factor of the *)
(* group order, the full group order being 8 * n_25519. Likewise C_25519 is  *)
(* the generator of the prime order subgroup and CC_25519 is a generator for *)
(* the full group.                                                           *)
(* ------------------------------------------------------------------------- *)

let p_25519 = define `p_25519 = 57896044618658097711785492504343953926634992332820282019728792003956564819949`;;
let n_25519 = define `n_25519 = 7237005577332262213973186563042994240857116359379907606001950938285454250989`;;
let A_25519 = define `A_25519 = 486662`;;
let B_25519 = define `B_25519 = 1`;;
let C_25519 = define `C_25519 = SOME(&0x09:int,&0x20ae19a1b8a086b4e01edd2c7748d14c923d4d7e6d7c61b229e9c5a27eced3d9:int)`;;
let CC_25519 = define `CC_25519 = SOME(&6911272784993971428141625084124731891523734454433518466500745240824540625972:int,&35197529511187359173101698576797651179158701633820552795916138355302448607023:int)`;;

(* ------------------------------------------------------------------------- *)
(* Primality of the field characteristic and (sub)group order.               *)
(* ------------------------------------------------------------------------- *)

let P_25519 = prove
 (`p_25519 = 2 EXP 255 - 19`,
  REWRITE_TAC[p_25519] THEN ARITH_TAC);;

let N_25519 = prove
 (`n_25519 = 2 EXP 252 + 27742317777372353535851937790883648493`,
  REWRITE_TAC[n_25519] THEN ARITH_TAC);;

let PRIME_P25519 = prove
 (`prime p_25519`,
  REWRITE_TAC[p_25519] THEN (CONV_TAC o PRIME_RULE)
   ["2"; "3"; "5"; "7"; "11"; "13"; "17"; "19"; "23"; "29"; "31"; "37"; "41";
    "43"; "47"; "53"; "59"; "83"; "97"; "103"; "107"; "127"; "131"; "173";
    "223"; "239"; "353"; "419"; "479"; "487"; "991"; "1723"; "2437"; "3727";
    "4153"; "9463"; "32573"; "37853"; "57467"; "65147"; "75707"; "132049";
    "430751"; "569003"; "1923133"; "8574133"; "2773320623"; "72106336199";
    "1919519569386763"; "31757755568855353";
    "75445702479781427272750846543864801";
    "74058212732561358302231226437062788676166966415465897661863160754340907";
    "57896044618658097711785492504343953926634992332820282019728792003956564819949"]);;

let PRIME_N25519 = prove
 (`prime n_25519`,
  REWRITE_TAC[n_25519] THEN (CONV_TAC o PRIME_RULE)
  ["2"; "3"; "5"; "7"; "11"; "13"; "17"; "19"; "23"; "41"; "43"; "47"; "67";
   "73"; "79"; "113"; "269"; "307"; "1361"; "1723"; "2551"; "2851"; "2939";
   "3797"; "5879"; "17231"; "22111"; "30703"; "34123"; "41081"; "82163";
   "132667"; "137849"; "409477"; "531581"; "1224481"; "14741173"; "58964693";
   "292386187"; "213441916511"; "1257559732178653"; "4434155615661930479";
   "3044861653679985063343"; "172054593956031949258510691";
   "198211423230930754013084525763697";
   "19757330305831588566944191468367130476339";
   "276602624281642239937218680557139826668747";
   "7237005577332262213973186563042994240857116359379907606001950938285454250989"]);;

(* ------------------------------------------------------------------------- *)
(* Definition of the curve group and proof of its key properties.            *)
(* ------------------------------------------------------------------------- *)

let curve25519_group = define
 `curve25519_group =
    montgomery_group(integer_mod_ring p_25519,&A_25519,&1)`;;

let CURVE25519_GROUP = prove
 (`group_carrier curve25519_group =
     montgomery_curve(integer_mod_ring p_25519,&A_25519,&1) /\
   group_id curve25519_group =
     NONE /\
   group_inv curve25519_group =
     montgomery_neg(integer_mod_ring p_25519,&A_25519,&1) /\
   group_mul curve25519_group =
     montgomery_add(integer_mod_ring p_25519,&A_25519,&1)`,
  REWRITE_TAC[curve25519_group] THEN MATCH_MP_TAC MONTGOMERY_GROUP THEN
  REWRITE_TAC[FIELD_INTEGER_MOD_RING; INTEGER_MOD_RING_CHAR; PRIME_P25519] THEN
  REWRITE_TAC[A_25519; B_25519; p_25519; montgomery_nonsingular] THEN
  SIMP_TAC[INTEGER_MOD_RING_CLAUSES; ARITH; IN_ELIM_THM] THEN
  CONV_TAC INT_REDUCE_CONV);;

add_ecgroup [A_25519; B_25519; p_25519] CURVE25519_GROUP;;

let FINITE_GROUP_CARRIER_CURVE25519 = prove
 (`FINITE(group_carrier curve25519_group)`,
  REWRITE_TAC[CURVE25519_GROUP] THEN MATCH_MP_TAC FINITE_MONTGOMERY_CURVE THEN
  REWRITE_TAC[FINITE_INTEGER_MOD_RING;
              FIELD_INTEGER_MOD_RING; PRIME_P25519] THEN
  REWRITE_TAC[p_25519] THEN CONV_TAC NUM_REDUCE_CONV);;

let GENERATOR_IN_GROUP_CARRIER_CURVE25519 = prove
 (`C_25519 IN group_carrier curve25519_group`,
  REWRITE_TAC[C_25519] THEN CONV_TAC ECGROUP_CARRIER_CONV);;

let GROUP_ELEMENT_ORDER_CURVE25519_C25519 = prove
 (`group_element_order curve25519_group C_25519 = n_25519`,
  SIMP_TAC[GROUP_ELEMENT_ORDER_UNIQUE_PRIME;
           GENERATOR_IN_GROUP_CARRIER_CURVE25519; PRIME_N25519] THEN
  REWRITE_TAC[C_25519; el 1 (CONJUNCTS CURVE25519_GROUP);
              option_DISTINCT] THEN
  REWRITE_TAC[n_25519] THEN CONV_TAC(LAND_CONV ECGROUP_POW_CONV) THEN
  REFL_TAC);;

let FULLGENERATOR_IN_GROUP_CARRIER_CURVE25519 = prove
 (`CC_25519 IN group_carrier curve25519_group`,
  REWRITE_TAC[CC_25519] THEN CONV_TAC ECGROUP_CARRIER_CONV);;

let GROUP_ELEMENT_ORDER_CURVE25519_CC25519 = prove
 (`group_element_order curve25519_group CC_25519 = 8 * n_25519`,
  ABBREV_TAC
   `h = SOME
     (&39382357235489614581723060781553021112529911719440698176882885853963445705823,
      &10506421237558716435988711236408671798265365380393424752549290025458740468278)
    :(int#int)option` THEN
  SUBGOAL_THEN
   `h IN group_carrier curve25519_group /\
    group_element_order curve25519_group h = 8`
  STRIP_ASSUME_TAC THENL
   [EXPAND_TAC "h" THEN
    MATCH_MP_TAC(TAUT `p /\ (p ==> q) ==> p /\ q`) THEN CONJ_TAC THENL
     [CONV_TAC ECGROUP_CARRIER_CONV;
      SIMP_TAC[GROUP_ELEMENT_ORDER_UNIQUE_ALT; ARITH]] THEN
    DISCH_TAC THEN REWRITE_TAC[CURVE25519_GROUP] THEN CONJ_TAC THENL
     [CONV_TAC(LAND_CONV ECGROUP_POW_CONV) THEN REFL_TAC; ALL_TAC] THEN
    REWRITE_TAC[IMP_CONJ_ALT] THEN CONV_TAC EXPAND_CASES_CONV THEN
    CONV_TAC NUM_REDUCE_CONV THEN REPEAT CONJ_TAC THEN
    CONV_TAC(RAND_CONV(LAND_CONV ECGROUP_POW_CONV)) THEN
    REWRITE_TAC[option_DISTINCT];
    ALL_TAC] THEN
  SUBGOAL_THEN `CC_25519 = group_mul curve25519_group h C_25519` SUBST1_TAC THENL
   [EXPAND_TAC "h" THEN REWRITE_TAC[C_25519; CC_25519] THEN
    CONV_TAC(RAND_CONV ECGROUP_MUL_CONV) THEN REFL_TAC;
    ALL_TAC] THEN
  W(MP_TAC o PART_MATCH (lhand o rand) GROUP_ELEMENT_ORDER_MUL_EQ o
    lhand o snd) THEN
  ASM_REWRITE_TAC[GROUP_ELEMENT_ORDER_CURVE25519_C25519] THEN
  DISCH_THEN MATCH_MP_TAC THEN
  REWRITE_TAC[GENERATOR_IN_GROUP_CARRIER_CURVE25519] THEN CONJ_TAC THENL
   [EXPAND_TAC "h" THEN REWRITE_TAC[C_25519] THEN
    CONV_TAC(BINOP_CONV ECGROUP_MUL_CONV) THEN REFL_TAC;
    REWRITE_TAC[n_25519] THEN CONV_TAC COPRIME_CONV]);;

let SIZE_CURVE25519_GROUP = prove
 (`group_carrier curve25519_group HAS_SIZE (8 * n_25519)`,
  REWRITE_TAC[HAS_SIZE; FINITE_GROUP_CARRIER_CURVE25519] THEN
  MP_TAC(ISPECL [`curve25519_group`; `CC_25519`]
    GROUP_ELEMENT_ORDER_DIVIDES_GROUP_ORDER) THEN
  REWRITE_TAC[FINITE_GROUP_CARRIER_CURVE25519;
              FULLGENERATOR_IN_GROUP_CARRIER_CURVE25519] THEN
  REWRITE_TAC[divides; LEFT_IMP_EXISTS_THM;
               GROUP_ELEMENT_ORDER_CURVE25519_CC25519] THEN
  X_GEN_TAC `d:num` THEN REPEAT_TCL DISJ_CASES_THEN ASSUME_TAC
   (ARITH_RULE `d = 0 \/ d = 1 \/ 2 <= d`) THEN
  ASM_SIMP_TAC[CARD_EQ_0; FINITE_GROUP_CARRIER_CURVE25519;
               MULT_CLAUSES; GROUP_CARRIER_NONEMPTY] THEN
  MATCH_MP_TAC(ARITH_RULE
   `s < 16 * n /\ 2 * n <= d * n ==> s = (8 * n) * d ==> x = 8 * n`) THEN
  REWRITE_TAC[LE_MULT_RCANCEL; n_25519; ARITH_EQ] THEN
  ASM_REWRITE_TAC[GSYM n_25519; CURVE25519_GROUP] THEN
  W(MP_TAC o PART_MATCH (lhand o rand) CARD_BOUND_MONTGOMERY_CURVE o
    lhand o snd) THEN
  REWRITE_TAC[FIELD_INTEGER_MOD_RING; PRIME_P25519] THEN
  SIMP_TAC[FINITE_INTEGER_MOD_RING; CARD_INTEGER_MOD_RING;
           IN_INTEGER_MOD_RING_CARRIER; n_25519; p_25519;
           INTEGER_MOD_RING; A_25519; INT_OF_NUM_CLAUSES; ARITH_EQ] THEN
  ARITH_TAC);;

let GENERATED_CURVE25519_GROUP = prove
 (`subgroup_generated curve25519_group {CC_25519} = curve25519_group`,
  SIMP_TAC[SUBGROUP_GENERATED_ELEMENT_ORDER;
           FULLGENERATOR_IN_GROUP_CARRIER_CURVE25519;
           FINITE_GROUP_CARRIER_CURVE25519] THEN
  REWRITE_TAC[GROUP_ELEMENT_ORDER_CURVE25519_CC25519;
              REWRITE_RULE[HAS_SIZE] SIZE_CURVE25519_GROUP]);;

let CYCLIC_CURVE25519_GROUP = prove
 (`cyclic_group curve25519_group`,
  MESON_TAC[CYCLIC_GROUP_ALT; GENERATED_CURVE25519_GROUP]);;

let ABELIAN_CURVE25519_GROUP = prove
 (`abelian_group curve25519_group`,
  MESON_TAC[CYCLIC_CURVE25519_GROUP; CYCLIC_IMP_ABELIAN_GROUP]);;