Datasets:

Modalities:
Text
Languages:
English
Libraries:
Datasets
License:
File size: 16,433 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
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
(* ========================================================================= *)
(* Montgomery curves in general, B * y^2 = x^3 + A * x^2 + x.                *)
(* ========================================================================= *)

needs "EC/misc.ml";;

(* ------------------------------------------------------------------------- *)
(* Basic definitions and naive cardinality bounds.                           *)
(* ------------------------------------------------------------------------- *)

let montgomery_point = define
 `(montgomery_point f NONE <=> T) /\
  (montgomery_point f (SOME(x:A,y)) <=>
        x IN ring_carrier f /\ y IN ring_carrier f)`;;

let montgomery_curve = define
 `(montgomery_curve(f:A ring,a:A,b:A) NONE <=> T) /\
  (montgomery_curve(f:A ring,a:A,b:A) (SOME(x,y)) <=>
        x IN ring_carrier f /\ y IN ring_carrier f /\
        ring_mul f b (ring_pow f y 2) =
        ring_add f (ring_pow f x 3)
                   (ring_add f (ring_mul f a (ring_pow f x 2)) x))`;;

let montgomery_nonsingular = define
 `montgomery_nonsingular (f:A ring,a:A,b:A) <=>
        ~(b = ring_0 f) /\ ~(ring_pow f a 2 = ring_of_num f 4)`;;

let montgomery_neg = define
 `(montgomery_neg (f:A ring,a:A,b:A) NONE = NONE) /\
  (montgomery_neg (f:A ring,a:A,b:A) (SOME(x:A,y)) = SOME(x,ring_neg f y))`;;

let montgomery_add = define
 `(!y. montgomery_add (f:A ring,a:A,b:A) NONE y = y) /\
  (!x. montgomery_add (f:A ring,a:A,b:A) x NONE = x) /\
  (!x1 y1 x2 y2.
        montgomery_add (f:A ring,a:A,b:A) (SOME(x1,y1)) (SOME(x2,y2)) =
        if x1 = x2 then
          if y1 = y2 /\ ~(y1 = ring_0 f) then
            let l = ring_div f
                (ring_add f (ring_mul f (ring_of_num f 3) (ring_pow f x1 2))
                            (ring_add f (ring_mul f (ring_of_num f 2)
                                                    (ring_mul f a x1))
                                        (ring_of_num f 1)))
                (ring_mul f (ring_of_num f 2) (ring_mul f b y1)) in
            let x3 = ring_sub f (ring_sub f (ring_mul f b (ring_pow f l 2)) a)
                                (ring_mul f (ring_of_num f 2) x1) in
            let y3 = ring_sub f (ring_mul f l (ring_sub f x1 x3)) y1 in
            SOME(x3,y3)
          else NONE
        else
          let l = ring_div f (ring_sub f y2 y1) (ring_sub f x2 x1) in
          let x3 = ring_sub f (ring_sub f (ring_mul f b (ring_pow f l 2)) a)
                              (ring_add f x1 x2) in
          let y3 = ring_sub f (ring_mul f l (ring_sub f x1 x3)) y1 in
          SOME(x3,y3))`;;

let montgomery_group = define
 `montgomery_group (f:A ring,a:A,b:A) =
        group(montgomery_curve(f,a,b),
              NONE,
              montgomery_neg(f,a,b),
              montgomery_add(f,a,b))`;;

let FINITE_MONTGOMERY_CURVE = prove
 (`!f a b:A. field f /\ FINITE(ring_carrier f)
             ==> FINITE(montgomery_curve(f,a,b))`,
  REPEAT STRIP_TAC THEN
  MATCH_MP_TAC FINITE_SUBSET THEN EXISTS_TAC
   `IMAGE SOME {(x,y) | (x:A) IN ring_carrier f /\ y IN ring_carrier f} UNION
    {NONE}` THEN
  ASM_SIMP_TAC[FINITE_UNION; FINITE_IMAGE; FINITE_SING; FINITE_PRODUCT] THEN
  REWRITE_TAC[montgomery_curve; SUBSET; FORALL_OPTION_THM;
              IN_UNION; IN_SING; IN_IMAGE; option_INJ; FORALL_PAIR_THM;
              IN_ELIM_PAIR_THM; UNWIND_THM1] THEN
  SIMP_TAC[montgomery_curve; IN]);;

let CARD_BOUND_MONTGOMERY_CURVE = prove
 (`!f a b:A. field f /\ FINITE(ring_carrier f) /\
             a IN ring_carrier f /\ b IN ring_carrier f /\ ~(b = ring_0 f)
             ==> CARD(montgomery_curve(f,a,b))
                   <= 2 * CARD(ring_carrier f) + 1`,
  REPEAT STRIP_TAC THEN
  MATCH_MP_TAC(MESON[FINITE_SUBSET; CARD_SUBSET; LE_TRANS]
   `!s. t SUBSET s /\ FINITE s /\ CARD s <= n ==> CARD t <= n`) THEN
  EXISTS_TAC
   `IMAGE SOME
     {(x,y) | (x:A) IN ring_carrier f /\ y IN ring_carrier f /\
              ring_pow f y 2 =
              ring_div f (ring_add f (ring_pow f x 3)
                           (ring_add f (ring_mul f a (ring_pow f x 2)) x)) b}
    UNION {NONE}` THEN
  CONJ_TAC THENL
   [REWRITE_TAC[montgomery_curve; SUBSET; FORALL_OPTION_THM;
      IN_UNION; IN_SING; IN_IMAGE; option_INJ; FORALL_PAIR_THM;
      IN_ELIM_PAIR_THM; UNWIND_THM1] THEN
    REWRITE_TAC[option_DISTINCT; IN_CROSS] THEN
    REPEAT GEN_TAC THEN GEN_REWRITE_TAC LAND_CONV [IN] THEN
    ASM_REWRITE_TAC[montgomery_curve] THEN
    STRIP_TAC THEN ASM_REWRITE_TAC[] THEN FIELD_TAC;
    MATCH_MP_TAC FINITE_CARD_LE_UNION] THEN
  REWRITE_TAC[FINITE_SING; CARD_SING; LE_REFL] THEN
  MATCH_MP_TAC FINITE_CARD_LE_IMAGE THEN
  MATCH_MP_TAC FINITE_QUADRATIC_CURVE THEN
  ASM_SIMP_TAC[FIELD_IMP_INTEGRAL_DOMAIN]);;

(* ------------------------------------------------------------------------- *)
(* A stronger form of nonsingularity, implying not only that the core cubic  *)
(* doesn't have repeated roots but has no roots at all except for (0,0).     *)
(* ------------------------------------------------------------------------- *)

let montgomery_strongly_nonsingular = define
  `montgomery_strongly_nonsingular(f,a:A,b:A) <=>
      ~(b = ring_0 f) /\
      ~(?z. z IN ring_carrier f /\
            ring_pow f z 2 = ring_sub f (ring_pow f a 2) (ring_of_num f 4))`;;

let MONTGOMERY_STRONGLY_NONSINGULAR = prove
 (`!f a b:A.
        field f /\ ~(ring_char f = 2) /\
        a IN ring_carrier f /\ b IN ring_carrier f
        ==> (montgomery_strongly_nonsingular(f,a,b) <=>
             !x y. montgomery_curve(f,a,b) (SOME(x,y))
                   ==> (y = ring_0 f <=> x = ring_0 f))`,
  REWRITE_TAC[FIELD_CHAR_NOT2] THEN
  REPEAT STRIP_TAC THEN REWRITE_TAC[montgomery_strongly_nonsingular] THEN
  REWRITE_TAC[FORALL_AND_THM; TAUT
   `p ==> (q <=> r) <=> (r ==> p ==> q) /\ (q ==> p ==> r)`] THEN
  REWRITE_TAC[RIGHT_FORALL_IMP_THM; FORALL_UNWIND_THM2] THEN
  REWRITE_TAC[montgomery_curve] THEN BINOP_TAC THENL
   [MATCH_MP_TAC(TAUT `(~p ==> q) /\ (p ==> ~q) ==> (~p <=> q)`) THEN
    CONJ_TAC THENL
     [REPEAT STRIP_TAC THEN TRY RING_CARRIER_TAC THEN FIELD_TAC;
      DISCH_THEN SUBST1_TAC THEN DISCH_THEN(MP_TAC o SPEC `ring_1 f:A`) THEN
      REWRITE_TAC[NOT_IMP] THEN
      REPEAT STRIP_TAC THEN TRY RING_CARRIER_TAC THEN FIELD_TAC];
    REWRITE_TAC[GSYM NOT_EXISTS_THM; montgomery_curve;
                TAUT `p ==> q <=> ~(p /\ ~q)`] THEN
    AP_TERM_TAC THEN EQ_TAC THENL
     [DISCH_THEN(X_CHOOSE_THEN `z:A` STRIP_ASSUME_TAC) THEN
      EXISTS_TAC `ring_div f (ring_sub f z a) (ring_of_num f 2):A`;
      DISCH_THEN(X_CHOOSE_THEN `x:A` STRIP_ASSUME_TAC) THEN
      EXISTS_TAC `ring_add f (ring_mul f (ring_of_num f 2) x) a:A`] THEN
    REPEAT STRIP_TAC THEN TRY RING_CARRIER_TAC THEN FIELD_TAC THEN
    NOT_RING_CHAR_DIVIDES_TAC]);;

let MONTGOMERY_STRONGLY_NONSINGULAR_IMP_NONSINGULAR = prove
 (`!f a b:A.
        a IN ring_carrier f /\ b IN ring_carrier f /\
        montgomery_strongly_nonsingular(f,a,b)
        ==> montgomery_nonsingular(f,a,b)`,
  REWRITE_TAC[montgomery_strongly_nonsingular; montgomery_nonsingular] THEN
  REPEAT GEN_TAC THEN REWRITE_TAC[NOT_EXISTS_THM] THEN STRIP_TAC THEN
  FIRST_X_ASSUM(MP_TAC o SPEC `ring_0 f:A`) THEN
  ASM_REWRITE_TAC[RING_0] THEN RING_TAC);;

(* ------------------------------------------------------------------------- *)
(* Proof of the group properties. This is just done by algebraic brute       *)
(* force except for the use of ASSOCIATIVITY_LEMMA to reduce the explosion   *)
(* of case distinctions.                                                     *)
(* ------------------------------------------------------------------------- *)

let MONTGOMERY_CURVE_IMP_POINT = prove
 (`!f a b p. montgomery_curve(f,a,b) p ==> montgomery_point f p`,
  REWRITE_TAC[FORALL_OPTION_THM; FORALL_PAIR_THM] THEN
  SIMP_TAC[montgomery_curve; montgomery_point]);;

let MONTGOMERY_POINT_NEG = prove
 (`!(f:A ring) a b p.
        montgomery_point f p
        ==> montgomery_point f (montgomery_neg (f,a,b) p)`,
  REWRITE_TAC[FORALL_OPTION_THM; FORALL_PAIR_THM] THEN
  SIMP_TAC[montgomery_neg; montgomery_point; RING_NEG]);;

let MONTGOMERY_POINT_ADD = prove
 (`!(f:A ring) a b p q.
        a IN ring_carrier f /\ b IN ring_carrier f /\
        montgomery_point f p /\ montgomery_point f q
        ==> montgomery_point f (montgomery_add (f,a,b) p q)`,
  REWRITE_TAC[FORALL_OPTION_THM; FORALL_PAIR_THM] THEN
  SIMP_TAC[montgomery_add; montgomery_point; LET_DEF; LET_END_DEF] THEN
  REPEAT STRIP_TAC THEN
  REPEAT(COND_CASES_TAC THEN ASM_REWRITE_TAC[montgomery_point]) THEN
  REPEAT STRIP_TAC THEN RING_CARRIER_TAC);;

let MONTGOMERY_CURVE_0 = prove
 (`!f a b:A. montgomery_curve(f,a,b) NONE`,
  REWRITE_TAC[montgomery_curve]);;

let MONTGOMERY_CURVE_NEG = prove
 (`!f a (b:A) p.
        integral_domain f /\ a IN ring_carrier f /\ b IN ring_carrier f /\
        montgomery_curve(f,a,b) p
        ==> montgomery_curve(f,a,b) (montgomery_neg (f,a,b) p)`,
  SIMP_TAC[FORALL_OPTION_THM; FORALL_PAIR_THM; RING_NEG;
              montgomery_curve; montgomery_neg] THEN
  REPEAT GEN_TAC THEN CONV_TAC INTEGRAL_DOMAIN_RULE);;

let MONTGOMERY_CURVE_ADD = prove
 (`!f a (b:A) p q.
        field f /\ ~(ring_char f = 2) /\
        montgomery_nonsingular(f,a,b) /\
        a IN ring_carrier f /\ b IN ring_carrier f /\
        montgomery_curve(f,a,b) p /\ montgomery_curve(f,a,b) q
        ==> montgomery_curve(f,a,b) (montgomery_add (f,a,b) p q)`,
  REWRITE_TAC[FIELD_CHAR_NOT2] THEN REPLICATE_TAC 3 GEN_TAC THEN
  SIMP_TAC[FORALL_OPTION_THM; FORALL_PAIR_THM; RING_ADD;
           montgomery_nonsingular; montgomery_curve; montgomery_add] THEN
  REWRITE_TAC[GSYM DE_MORGAN_THM] THEN
  MAP_EVERY X_GEN_TAC [`x1:A`; `y1:A`; `x2:A`; `y2:A`] THEN
  REPEAT(COND_CASES_TAC THEN ASM_REWRITE_TAC[]) THEN
  RULE_ASSUM_TAC(REWRITE_RULE[DE_MORGAN_THM]) THEN
  REPEAT(FIRST_X_ASSUM(DISJ_CASES_THEN ASSUME_TAC)) THEN
  REPEAT(FIRST_X_ASSUM(CONJUNCTS_THEN ASSUME_TAC)) THEN
  REPEAT(FIRST_X_ASSUM SUBST_ALL_TAC) THEN
  REPEAT LET_TAC THEN REWRITE_TAC[montgomery_curve] THEN
  REPEAT STRIP_TAC THEN TRY RING_CARRIER_TAC THEN
  FIELD_TAC THEN NOT_RING_CHAR_DIVIDES_TAC);;

let MONTGOMERY_ADD_LNEG = prove
 (`!f a (b:A) p.
        field f /\ ~(ring_char f = 2) /\
        a IN ring_carrier f /\ b IN ring_carrier f /\
        montgomery_curve(f,a,b) p
        ==> montgomery_add(f,a,b) (montgomery_neg (f,a,b) p) p = NONE`,
  REWRITE_TAC[FIELD_CHAR_NOT2] THEN REPLICATE_TAC 3 GEN_TAC THEN
  SIMP_TAC[FORALL_OPTION_THM; FORALL_PAIR_THM; RING_ADD;
              montgomery_curve; montgomery_neg; montgomery_add] THEN
  MAP_EVERY X_GEN_TAC [`x:A`; `y:A`] THEN
  REPEAT(COND_CASES_TAC THEN ASM_REWRITE_TAC[]) THEN
  RULE_ASSUM_TAC(REWRITE_RULE[DE_MORGAN_THM]) THEN
  REPEAT(FIRST_X_ASSUM(DISJ_CASES_THEN ASSUME_TAC)) THEN
  REPEAT(FIRST_X_ASSUM(CONJUNCTS_THEN ASSUME_TAC)) THEN
  REPEAT LET_TAC THEN REWRITE_TAC[option_DISTINCT] THEN
  REPEAT STRIP_TAC THEN TRY RING_CARRIER_TAC THEN
  FIELD_TAC);;

let MONTGOMERY_ADD_SYM = prove
 (`!f a (b:A) p q.
        field f /\
        a IN ring_carrier f /\ b IN ring_carrier f /\
        montgomery_curve(f,a,b) p /\ montgomery_curve(f,a,b) q
        ==> montgomery_add (f,a,b) p q = montgomery_add (f,a,b) q p`,
  REPLICATE_TAC 3 GEN_TAC THEN
  SIMP_TAC[FORALL_OPTION_THM; FORALL_PAIR_THM; RING_ADD;
              montgomery_curve; montgomery_add] THEN
  MAP_EVERY X_GEN_TAC [`x1:A`; `y1:A`; `x2:A`; `y2:A`] THEN
  REPEAT(COND_CASES_TAC THEN ASM_REWRITE_TAC[]) THEN
  RULE_ASSUM_TAC(REWRITE_RULE[DE_MORGAN_THM]) THEN
  REPEAT(FIRST_X_ASSUM(DISJ_CASES_THEN ASSUME_TAC)) THEN
  REPEAT(FIRST_X_ASSUM(CONJUNCTS_THEN ASSUME_TAC)) THEN
  REPEAT(FIRST_X_ASSUM SUBST_ALL_TAC) THEN
  REPEAT LET_TAC THEN
  REWRITE_TAC[option_DISTINCT; option_INJ; PAIR_EQ] THEN
  REPEAT STRIP_TAC THEN TRY RING_CARRIER_TAC THEN
  FIELD_TAC);;

let MONTGOMERY_ADD_ASSOC = prove
 (`!f a (b:A) p q r.
        field f /\ ~(ring_char f = 2) /\
        montgomery_nonsingular(f,a,b) /\
        a IN ring_carrier f /\ b IN ring_carrier f /\
        montgomery_curve(f,a,b) p /\
        montgomery_curve(f,a,b) q /\
        montgomery_curve(f,a,b) r
        ==> montgomery_add (f,a,b) p (montgomery_add (f,a,b) q r) =
            montgomery_add (f,a,b) (montgomery_add (f,a,b) p q) r`,
  let assoclemma = prove
   (`!f (a:A) b x1 y1 x2 y2.
          field f /\
          montgomery_nonsingular(f,a,b) /\
          a IN ring_carrier f /\ b IN ring_carrier f /\
          montgomery_curve(f,a,b) (SOME(x1,y1)) /\
          montgomery_curve(f,a,b) (SOME(x2,y2))
          ==> (~(SOME(x2,y2) = SOME(x1,y1)) /\
               ~(SOME(x2,y2) = montgomery_neg (f,a,b) (SOME(x1,y1))) <=>
               ~(x1 = x2))`,
    REWRITE_TAC[montgomery_nonsingular] THEN
    REWRITE_TAC[montgomery_curve; montgomery_neg; option_INJ; PAIR_EQ] THEN
    FIELD_TAC) in
  REWRITE_TAC[FIELD_CHAR_NOT2] THEN
  REWRITE_TAC[IMP_CONJ; RIGHT_FORALL_IMP_THM] THEN
  X_GEN_TAC `f:A ring` THEN REPEAT DISCH_TAC THEN
  MAP_EVERY X_GEN_TAC [`a:A`; `b:A`] THEN REPEAT DISCH_TAC THEN
  REWRITE_TAC[IMP_IMP; GSYM CONJ_ASSOC; RIGHT_IMP_FORALL_THM] THEN
  MATCH_MP_TAC ASSOCIATIVITY_LEMMA THEN
  MAP_EVERY EXISTS_TAC [`montgomery_neg(f,a:A,b)`; `NONE:(A#A)option`] THEN
  REWRITE_TAC[FORALL_OPTION_THM; FORALL_PAIR_THM] THEN
  REWRITE_TAC(CONJUNCT1 montgomery_curve :: CONJUNCT1 montgomery_neg ::
              fst(chop_list 2 (CONJUNCTS montgomery_add))) THEN
  REWRITE_TAC[CONJ_ASSOC] THEN CONJ_TAC THEN REWRITE_TAC[GSYM CONJ_ASSOC] THENL
   [REPEAT CONJ_TAC THEN
    MAP_EVERY X_GEN_TAC [`x1:A`; `y1:A`] THEN REPEAT CONJ_TAC THEN
    TRY(MAP_EVERY X_GEN_TAC [`x2:A`; `y2:A`]) THEN REPEAT CONJ_TAC THEN
    TRY(MAP_EVERY X_GEN_TAC [`x3:A`; `y3:A`]) THEN REPEAT CONJ_TAC;
    MAP_EVERY X_GEN_TAC [`x1:A`; `y1:A`; `x2:A`; `y2:A`; `x3:A`; `y3:A`] THEN
    ASM_SIMP_TAC[assoclemma; DE_MORGAN_THM] THEN
    REWRITE_TAC[option_DISTINCT] THEN
    REPEAT GEN_TAC THEN REWRITE_TAC[montgomery_curve] THEN
    STRIP_TAC THEN STRIP_TAC THEN
    ASM_REWRITE_TAC[montgomery_add]] THEN
  REWRITE_TAC[montgomery_curve; montgomery_add; montgomery_neg] THEN
  REPEAT(GEN_TAC ORELSE CONJ_TAC) THEN
  REPEAT(COND_CASES_TAC THEN ASM_REWRITE_TAC[]) THEN
  REPEAT LET_TAC THEN
  REWRITE_TAC[option_DISTINCT; option_INJ; PAIR_EQ] THEN
  REPEAT STRIP_TAC THEN
  REWRITE_TAC[montgomery_curve; montgomery_add; montgomery_neg] THEN
  REPEAT(GEN_TAC ORELSE CONJ_TAC) THEN
  REPEAT(COND_CASES_TAC THEN ASM_REWRITE_TAC[]) THEN
  REPEAT LET_TAC THEN
  REWRITE_TAC[option_DISTINCT; option_INJ; PAIR_EQ] THEN
  REPEAT STRIP_TAC THEN
  REPEAT(FIRST_X_ASSUM(CONJUNCTS_THEN ASSUME_TAC)) THEN
  REPEAT(FIRST_X_ASSUM(SUBST_ALL_TAC o SYM o check
    (fun th -> is_eq(concl th) && is_var(lhand(concl th)) &&
               is_var(rand(concl th))))) THEN
  TRY RING_CARRIER_TAC THEN
  RULE_ASSUM_TAC(REWRITE_RULE[montgomery_nonsingular]) THEN FIELD_TAC THEN
  NOT_RING_CHAR_DIVIDES_TAC);;

let MONTGOMERY_GROUP = prove
 (`!f a (b:A).
      field f /\ ~(ring_char f = 2) /\
      a IN ring_carrier f /\ b IN ring_carrier f /\
      montgomery_nonsingular(f,a,b)
      ==> group_carrier(montgomery_group(f,a,b)) = montgomery_curve(f,a,b) /\
          group_id(montgomery_group(f,a,b)) = NONE /\
          group_inv(montgomery_group(f,a,b)) = montgomery_neg(f,a,b) /\
          group_mul(montgomery_group(f,a,b)) = montgomery_add(f,a,b)`,
  REPEAT GEN_TAC THEN STRIP_TAC THEN
  REWRITE_TAC[group_carrier; group_id; group_inv; group_mul; GSYM PAIR_EQ] THEN
  REWRITE_TAC[montgomery_group; GSYM(CONJUNCT2 group_tybij)] THEN
  REPEAT CONJ_TAC THENL
   [REWRITE_TAC[IN; montgomery_curve];
    REWRITE_TAC[IN] THEN
    ASM_SIMP_TAC[MONTGOMERY_CURVE_NEG; FIELD_IMP_INTEGRAL_DOMAIN];
    REWRITE_TAC[IN] THEN ASM_SIMP_TAC[MONTGOMERY_CURVE_ADD];
    REWRITE_TAC[IN] THEN ASM_SIMP_TAC[MONTGOMERY_ADD_ASSOC];
    REWRITE_TAC[FORALL_OPTION_THM; FORALL_PAIR_THM; montgomery_add];
    REWRITE_TAC[IN] THEN GEN_TAC THEN STRIP_TAC THEN MATCH_MP_TAC(MESON[]
     `x = a /\ x = y ==> x = a /\ y = a`) THEN
    CONJ_TAC THENL
     [ASM_SIMP_TAC[MONTGOMERY_ADD_LNEG];
      MATCH_MP_TAC MONTGOMERY_ADD_SYM THEN
      ASM_SIMP_TAC[MONTGOMERY_CURVE_NEG; FIELD_IMP_INTEGRAL_DOMAIN]]]);;

let ABELIAN_MONTGOMERY_GROUP = prove
 (`!f a (b:A).
      field f /\ ~(ring_char f = 2) /\
      a IN ring_carrier f /\ b IN ring_carrier f /\
      montgomery_nonsingular(f,a,b)
      ==> abelian_group(montgomery_group(f,a,b))`,
  REPEAT STRIP_TAC THEN ASM_SIMP_TAC[abelian_group; MONTGOMERY_GROUP] THEN
  REWRITE_TAC[IN] THEN ASM_MESON_TAC[MONTGOMERY_ADD_SYM]);;