forked from starkware-libs/formal-proofs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathec_spec.lean
1506 lines (1400 loc) · 65.4 KB
/
ec_spec.lean
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/-
Specifications file for ec_spec.cairo
Do not modify the constant definitions, structure definitions, or automatic specifications.
Do not change the name or arguments of the user specifications and soundness theorems.
You may freely move the definitions around in the file.
You may add definitions and theorems wherever you wish in this file.
-/
import starkware.cairo.lean.semantics.soundness.prelude
import starkware.cairo.common.cairo_secp.bigint_spec
import starkware.cairo.common.cairo_secp.field_spec
import starkware.cairo.common.cairo_secp.constants_spec
import starkware.cairo.common.cairo_secp.bigint3_spec
import starkware.cairo.common.cairo_secp.ec_point_spec
-- JDA: Additional import.
import starkware.cairo.common.cairo_secp.elliptic_curves
open starkware.cairo.common.cairo_secp.bigint3
open starkware.cairo.common.cairo_secp.ec_point
open starkware.cairo.common.cairo_secp.bigint
open starkware.cairo.common.cairo_secp.field
open starkware.cairo.common.cairo_secp.constants
namespace starkware.cairo.common.cairo_secp.ec
variables {F : Type} [field F] [decidable_eq F] [prelude_hyps F]
-- End of automatically generated prelude.
-- Main scope definitions.
-- End of main scope definitions.
/-
-- Data for writing the specifications.
-/
structure BddECPointData {mem : F → F} (secpF : Type*) [field secpF] (pt : EcPoint mem) :=
(ix iy : bigint3)
(ixbdd : ix.bounded (3 * BASE - 1))
(iybdd : iy.bounded (3 * BASE - 1))
(ptxeq : pt.x = ix.toBigInt3)
(ptyeq : pt.y = iy.toBigInt3)
(onEC : pt.x = ⟨0, 0, 0⟩ ∨ (iy.val : secpF)^2 = (ix.val : secpF)^3 + 7)
theorem BddECPointData.onEC' {mem : F → F} {secpF : Type*} [field secpF] {pt : EcPoint mem}
(h : BddECPointData secpF pt) (h' : pt.x ≠ ⟨0, 0, 0⟩) :
(h.iy.val : secpF)^2 = (h.ix.val : secpF)^3 + 7 :=
or.resolve_left h.onEC h'
section secpF
variables
{secpF : Type} -- in practice, zmod SECP_PRIME
[field secpF] -- in practice, @zmod.field _ ⟨prime_secp⟩
[char_p secpF SECP_PRIME]
[decidable_eq secpF]
def BddECPointData.toECPoint {mem : F → F} {pt : EcPoint mem} (h : BddECPointData secpF pt) :
ECPoint secpF :=
if h' : pt.x = ⟨0, 0, 0⟩ then
ECPoint.ZeroPoint
else
ECPoint.AffinePoint ⟨h.ix.val, h.iy.val, h.onEC' h'⟩
def BddECPointData.zero {mem : F → F} : BddECPointData secpF (⟨⟨0, 0, 0⟩, ⟨0, 0, 0⟩⟩ : EcPoint mem) :=
{ ix := ⟨0, 0, 0⟩,
iy := ⟨0, 0, 0⟩,
ixbdd := by simp [bigint3.bounded]; norm_num1,
iybdd := by simp [bigint3.bounded]; norm_num1,
ptxeq := by simp [bigint3.toBigInt3],
ptyeq := by simp [bigint3.toBigInt3],
onEC := or.inl rfl }
end secpF
def SECP_LOG2_BOUND := 100
class secp_field (secpF : Type*) extends ec_field secpF, char_p secpF SECP_PRIME :=
(seven_not_square : ∀ y : secpF, y^2 ≠ 7)
(neg_seven_not_cube : ∀ x : secpF, x^3 ≠ -7)
(order_large : ∀ {pt : ECPoint secpF}, pt ≠ 0 →
∀ {n : ℕ}, n ≠ 0 → n < 2^(SECP_LOG2_BOUND + 1) + 2 → n • pt ≠ 0)
theorem secp_field.order_large_corr {secpF : Type*} [secp_field secpF]
{pt : ECPoint secpF} (ptnz : pt ≠ 0) :
∀ {n : ℕ}, n < 2^SECP_LOG2_BOUND → ¬ ((n * 2) • pt = pt ∨ (n * 2) • pt = -pt) :=
begin
rintros n nlt (h | h),
{ cases n with npred,
{ rw [zero_mul, zero_smul] at h, exact ptnz h.symm},
have : (npred * 2 + 1) • pt = 0,
{ rw [nat.succ_eq_add_one, add_mul, one_mul] at h,
have : (npred * 2 + 1 + 1) • pt = pt := h,
rw [add_smul, one_smul, ←sub_eq_zero, add_sub_cancel] at this,
exact this },
revert this, apply secp_field.order_large ptnz,
{ apply nat.succ_ne_zero },
apply lt_of_lt_of_le,
apply add_lt_add_right,
apply lt_of_le_of_lt,
apply mul_le_mul_of_nonneg_right,
apply le_of_lt,
apply lt_trans (nat.lt_succ_self _) nlt,
norm_num,
apply nat.lt_succ_self,
norm_num [SECP_LOG2_BOUND],
},
rw [eq_neg_iff_add_eq_zero, ← one_smul _ pt, smul_smul (n * 2) 1, ← add_smul] at h,
revert h,
apply secp_field.order_large ptnz,
{ apply nat.succ_ne_zero },
rw [mul_assoc, mul_one],
apply lt_trans,
change (_ < 2^SECP_LOG2_BOUND * 2 + 1),
apply add_lt_add_right,
apply zero_lt.mul_lt_mul_right' nlt,
norm_num, norm_num [SECP_LOG2_BOUND]
end
theorem secp_field.y_ne_zero_of_on_ec {secpF : Type*} [secp_field secpF] {x y : secpF}
(h : on_ec (x, y)) : y ≠ 0 :=
by { contrapose! h, simp [on_ec, h, ←sub_eq_iff_eq_add],
apply ne.symm, apply secp_field.neg_seven_not_cube }
theorem secp_field.x_ne_zero_of_on_ec {secpF : Type*} [secp_field secpF] {x y : secpF}
(h : on_ec (x, y)) : x ≠ 0 :=
by { contrapose! h, simp [on_ec, h, secp_field.seven_not_square] }
theorem secp_field.eq_zero_of_double_eq_zero {secpF : Type*} [secp_field secpF]
{x : ECPoint secpF} (h : 2 • x = 0) : x = 0 :=
begin
cases x, { refl },
simp [two_smul, ECPoint.add_def, ECPoint.add] at h,
have : x.y ≠ -x.y,
{ intro h',
have h_on_ec := x.h, dsimp [on_ec] at h_on_ec,
have : 2 * x.y = 0,
{ rwa [two_mul, ←eq_neg_iff_add_eq_zero] },
rw mul_eq_zero at this,
simp [or.resolve_left this ec_field.two_ne_zero] at h_on_ec,
apply secp_field.neg_seven_not_cube x.x,
exact eq_neg_iff_add_eq_zero.mpr h_on_ec.symm },
rw dif_neg this at h,
contradiction
end
namespace BddECPointData
theorem toECPoint_zero {mem : F → F} (secpF : Type) [secp_field secpF] :
(BddECPointData.zero : BddECPointData secpF (⟨⟨0, 0, 0⟩, ⟨0, 0, 0⟩⟩ : EcPoint mem)).toECPoint =
0 :=
by { simp [toECPoint], refl }
theorem pt_zero_iff' {mem : F → F} {secpF : Type} [secp_field secpF]
{pt : EcPoint mem} (h : BddECPointData secpF pt) :
pt.x = ⟨0, 0, 0⟩ ↔ h.ix.val = 0 :=
begin
split,
{ rw [h.ptxeq],
intro heq,
rw toBigInt3_eq_zero_of_bounded_3BASE heq h.ixbdd,
simp [bigint3.val] },
intro heq,
cases h.onEC with h1 h1, { exact h1 },
exfalso,
have : on_ec ((h.ix.val : secpF), (h.iy.val : secpF)):= h1,
apply secp_field.x_ne_zero_of_on_ec this,
simp [heq]
end
theorem pt_zero_iff {mem : F → F} {secpF : Type} [secp_field secpF]
{pt : EcPoint mem} (h : BddECPointData secpF pt) :
pt.x = ⟨0, 0, 0⟩ ↔ (h.ix.val : secpF) = 0 :=
begin
split,
{ rw [h.ptxeq],
intro heq,
rw toBigInt3_eq_zero_of_bounded_3BASE heq h.ixbdd,
simp [bigint3.val] },
intro heq,
cases h.onEC with h1 h1, { exact h1 },
exfalso,
have : on_ec ((h.ix.val : secpF), (h.iy.val : secpF)):= h1,
apply secp_field.x_ne_zero_of_on_ec this,
simp [heq]
end
theorem toECPoint_eq_zero_iff {mem : F → F} {secpF : Type} [secp_field secpF] {pt : EcPoint mem} (h : BddECPointData secpF pt) :
h.toECPoint = 0 ↔ pt.x = ⟨0, 0, 0⟩ :=
by { by_cases h : pt.x = ⟨0, 0, 0⟩; simp [BddECPointData.toECPoint, h, ECPoint.zero_def] }
theorem toECPoint_eq_of_eq_of_ne {mem : F → F} {secpF : Type} [secp_field secpF] {pt0 pt1 : EcPoint mem}
{h0 : BddECPointData secpF pt0}
{h1 : BddECPointData secpF pt1}
(hxeq : (h0.ix.val : secpF) = (h1.ix.val : secpF))
(hyne : (h0.iy.val : secpF) ≠ -(h1.iy.val : secpF)) :
h0.toECPoint = h1.toECPoint :=
begin
have aux: pt0.x = ⟨0, 0, 0⟩ ↔ pt1.x = ⟨0, 0, 0⟩,
{ rw [h1.pt_zero_iff, ←hxeq, ←h0.pt_zero_iff] },
by_cases h: pt0.x = ⟨0, 0, 0⟩,
{ have : pt1.x = ⟨0, 0, 0⟩, by rwa ←aux,
rw [h0.toECPoint_eq_zero_iff.mpr h, h1.toECPoint_eq_zero_iff.mpr this] },
have : ¬ (pt1.x = ⟨0, 0, 0⟩), by rwa ←aux,
rw [BddECPointData.toECPoint, BddECPointData.toECPoint, dif_neg h, dif_neg this],
congr' 1, ext, { exact hxeq }, dsimp,
have : (h0.iy.val : secpF)^2 = (h1.iy.val : secpF)^2,
{ rw [h0.onEC' h, h1.onEC' this, hxeq] },
have := eq_or_eq_neg_of_sq_eq_sq _ _ this,
exact or.resolve_right this hyne
end
end BddECPointData
theorem double_Affine {secpF : Type} [secp_field secpF] {x1 y1 x2 y2 : secpF}
(h1 : on_ec (x1, y1)) (h2 : on_ec (x2, y2))
(heq : ec_double (x1, y1) = (x2, y2)) :
2 • ECPoint.AffinePoint ⟨x1, y1, h1⟩ = ECPoint.AffinePoint ⟨x2, y2, h2⟩ :=
begin
have : y1 ≠ -y1,
{ intro heq,
apply secp_field.y_ne_zero_of_on_ec h1,
have : 2 * y1 = 0,
{ rwa [two_mul, add_eq_zero_iff_eq_neg] },
rw mul_eq_zero at this,
exact this.resolve_left ec_field.two_ne_zero },
rw two_smul,
simp [ECPoint.add_def, ECPoint.add], dsimp,
rw [dif_neg this], congr; simp [heq]
end
theorem Affine_add_Affine {secpF : Type} [secp_field secpF] {x1 y1 x2 y2 x3 y3 : secpF}
(h1 : on_ec (x1, y1)) (h2 : on_ec (x2, y2)) (h3 : on_ec (x3, y3)) (hne : x1 ≠ x2)
(heq : ec_add (x1, y1) (x2, y2) = (x3, y3)) :
ECPoint.AffinePoint ⟨x1, y1, h1⟩ + ECPoint.AffinePoint ⟨x2, y2, h2⟩ =
ECPoint.AffinePoint ⟨x3, y3, h3⟩ :=
begin
simp [ECPoint.add_def, ECPoint.add], dsimp,
rw [dif_neg hne], congr; simp [heq]
end
/-
-- Function: ec_negate
-/
/- ec_negate autogenerated specification -/
-- Do not change this definition.
def auto_spec_ec_negate (mem : F → F) (κ : ℕ) (range_check_ptr : F) (point : EcPoint mem) (ρ_range_check_ptr : F) (ρ_point : EcPoint mem) : Prop :=
∃ (κ₁ : ℕ) (range_check_ptr₁ : F) (minus_y : BigInt3 mem), spec_nondet_bigint3 mem κ₁ range_check_ptr range_check_ptr₁ minus_y ∧
∃ (κ₂ : ℕ) (range_check_ptr₂ : F), spec_verify_zero mem κ₂ range_check_ptr₁ {
d0 := minus_y.d0 + point.y.d0,
d1 := minus_y.d1 + point.y.d1,
d2 := minus_y.d2 + point.y.d2
} range_check_ptr₂ ∧
κ₁ + κ₂ + 14 ≤ κ ∧
ρ_range_check_ptr = range_check_ptr₂ ∧
ρ_point = {
x := point.x,
y := minus_y
}
-- You may change anything in this definition except the name and arguments.
def spec_ec_negate (mem : F → F) (κ : ℕ) (range_check_ptr : F) (point : EcPoint mem) (ρ_range_check_ptr : F) (ρ_point : EcPoint mem) : Prop :=
∀ ix iy : bigint3,
ix.bounded (3 * BASE - 1) →
iy.bounded (3 * BASE - 1) →
point.x = ix.toBigInt3 →
point.y = iy.toBigInt3 →
∃ ineg_y : bigint3,
ineg_y.bounded (3 * (BASE - 1)) ∧
ρ_point = { x := ix.toBigInt3, y := ineg_y.toBigInt3 } ∧
ineg_y.val ≡ - iy.val [ZMOD SECP_PRIME]
/- ec_negate soundness theorem -/
-- Do not change the statement of this theorem. You may change the proof.
theorem sound_ec_negate
{mem : F → F}
(κ : ℕ)
(range_check_ptr : F) (point : EcPoint mem) (ρ_range_check_ptr : F) (ρ_point : EcPoint mem)
(h_auto : auto_spec_ec_negate mem κ range_check_ptr point ρ_range_check_ptr ρ_point) :
spec_ec_negate mem κ range_check_ptr point ρ_range_check_ptr ρ_point :=
begin
intros ix iy ixbdd iybdd ptxeq ptyeq,
rcases h_auto with ⟨_, _, neg_y, hneg_y, _, _, hverify_zero, _, _, ρ_point_eq⟩,
rcases nondet_bigint3_corr hneg_y with ⟨ineg_y, neg_y_eq, ineg_y_bdd⟩,
use [ineg_y, ineg_y_bdd],
split, { rw [ρ_point_eq, ptxeq, neg_y_eq] },
rw [int.modeq_iff_sub_mod_eq_zero, sub_neg_eq_add, ←bigint3.add_val],
apply hverify_zero,
{ simp [ptyeq, neg_y_eq, bigint3.add, bigint3.toUnreducedBigInt3, bigint3.toBigInt3] },
apply bigint3.bounded_of_bounded_of_le,
apply bigint3.bounded_add ineg_y_bdd iybdd,
dsimp only [BASE, SECP_REM], simp_int_casts, norm_num1
end
/- Better specification. -/
def spec_ec_negate' {mem : F → F} ( pt : EcPoint mem ) ( ret : EcPoint mem )
(secpF : Type) [secp_field secpF] : Prop :=
∀ h : BddECPointData secpF pt,
∃ hret : BddECPointData secpF ret,
hret.toECPoint = -h.toECPoint
theorem spec_ec_negate'_of_spec_ec_negate
{mem : F → F} {κ : ℕ} {range_check_ptr : F} {pt : EcPoint mem} {ret0 : F} {ret : EcPoint mem}
(h : spec_ec_negate mem κ range_check_ptr pt ret0 ret)
(secpF : Type) [secp_field secpF] :
spec_ec_negate' pt ret secpF :=
begin
intro hpt,
rcases h hpt.ix hpt.iy hpt.ixbdd hpt.iybdd hpt.ptxeq hpt.ptyeq with ⟨ineg_y, ineg_y_bdd, reteq, hineg_y⟩,
have inegyvaleq : (ineg_y.val : secpF) = - (hpt.iy.val : secpF),
{ rw [←int.cast_neg, char_p.int_coe_eq_int_coe_iff secpF SECP_PRIME],
exact hineg_y },
rw EcPoint.ext_iff at reteq,
have retx_eq_ptx : ret.x = pt.x := reteq.1.trans (hpt.ptxeq.symm),
let hret : BddECPointData secpF ret :=
{ ix := hpt.ix,
iy := ineg_y,
ixbdd := hpt.ixbdd,
iybdd := bigint3.bounded_of_bounded_of_le ineg_y_bdd bound_slack,
ptxeq := reteq.1,
ptyeq := reteq.2,
onEC :=
begin
cases hpt.onEC with h' h',
{ exact or.inl (retx_eq_ptx.trans h') },
right, rw [inegyvaleq, neg_sq, h']
end },
use hret,
simp [BddECPointData.toECPoint],
by_cases h' : pt.x = ⟨0, 0, 0⟩,
{ rw [dif_pos h', dif_pos (retx_eq_ptx.trans h')],
refl },
rw [dif_neg h', dif_neg (ne_of_eq_of_ne retx_eq_ptx h')],
simp [ECPoint.neg_def, ECPoint.neg, inegyvaleq]
end
/-
-- Function: compute_doubling_slope
-/
/- compute_doubling_slope autogenerated specification -/
-- Do not change this definition.
def auto_spec_compute_doubling_slope (mem : F → F) (κ : ℕ) (range_check_ptr : F) (point : EcPoint mem) (ρ_range_check_ptr : F) (ρ_slope : BigInt3 mem) : Prop :=
∃ (κ₁ : ℕ) (range_check_ptr₁ : F) (slope : BigInt3 mem), spec_nondet_bigint3 mem κ₁ range_check_ptr range_check_ptr₁ slope ∧
∃ (κ₂ : ℕ) (x_sqr : UnreducedBigInt3 mem), spec_unreduced_sqr mem κ₂ point.x x_sqr ∧
∃ (κ₃ : ℕ) (slope_y : UnreducedBigInt3 mem), spec_unreduced_mul mem κ₃ slope point.y slope_y ∧
∃ (κ₄ : ℕ) (range_check_ptr₂ : F), spec_verify_zero mem κ₄ range_check_ptr₁ {
d0 := 3 * x_sqr.d0 - 2 * slope_y.d0,
d1 := 3 * x_sqr.d1 - 2 * slope_y.d1,
d2 := 3 * x_sqr.d2 - 2 * slope_y.d2
} range_check_ptr₂ ∧
κ₁ + κ₂ + κ₃ + κ₄ + 34 ≤ κ ∧
ρ_range_check_ptr = range_check_ptr₂ ∧
ρ_slope = slope
-- You may change anything in this definition except the name and arguments.
def spec_compute_doubling_slope (mem : F → F) (κ : ℕ) (range_check_ptr : F) (point : EcPoint mem) (ρ_range_check_ptr : F) (ρ_slope : BigInt3 mem) : Prop :=
∀ ix iy : bigint3,
ix.bounded (3 * BASE - 1) →
iy.bounded (3 * BASE - 1) →
point.x = ix.toBigInt3 →
point.y = iy.toBigInt3 →
∃ is : bigint3,
is.bounded (3 * (BASE - 1)) ∧
ρ_slope = is.toBigInt3 ∧
3 * ix.val^2 ≡ 2 * iy.val * is.val [ZMOD SECP_PRIME]
/- compute_doubling_slope soundness theorem -/
-- Do not change the statement of this theorem. You may change the proof.
theorem sound_compute_doubling_slope
{mem : F → F}
(κ : ℕ)
(range_check_ptr : F) (point : EcPoint mem) (ρ_range_check_ptr : F) (ρ_slope : BigInt3 mem)
(h_auto : auto_spec_compute_doubling_slope mem κ range_check_ptr point ρ_range_check_ptr ρ_slope) :
spec_compute_doubling_slope mem κ range_check_ptr point ρ_range_check_ptr ρ_slope :=
begin
rcases h_auto with ⟨_, _, slope, valid_slope, _, x_sqr, hx, _, slope_y, hslope_y, _, _, vz, _, _, ret1eq⟩,
rcases nondet_bigint3_corr valid_slope with ⟨is, slopeeq, isbdd⟩,
intros ix iy ixbdd iybdd ptxeq ptyeq,
have x_sqr_eq := hx _ ptxeq,
have slope_y_eq := hslope_y _ _ slopeeq ptyeq,
refine ⟨_, isbdd, ret1eq.trans slopeeq, _⟩,
set diff : bigint3 := (ix.sqr.cmul 3).sub ((iy.mul is).cmul 2) with diff_eq,
have diff_bdd : diff.bounded (5 * ((3 * BASE - 1)^2 * (8 * SECP_REM + 1))),
{ rw [diff_eq, (show (5 : ℤ) = 3 + 2, by norm_num), add_mul],
apply bigint3.bounded_sub,
apply bigint3.bounded_cmul' (show (0 : ℤ) ≤ 3, by norm_num1),
apply bigint3.bounded_sqr ixbdd,
apply bigint3.bounded_cmul' (show (0 : ℤ) ≤ 2, by norm_num1),
apply bigint3.bounded_mul iybdd,
apply bigint3.bounded_of_bounded_of_le isbdd bound_slack },
have : diff.val % SECP_PRIME = 0,
{ apply vz,
{ simp only [diff_eq, x_sqr_eq, slope_y_eq],
dsimp [bigint3.toUnreducedBigInt3, bigint3.sqr, bigint3.mul, bigint3.cmul, bigint3.sub],
simp_int_casts,
split, ring,
split, ring, ring },
apply bigint3.bounded_of_bounded_of_le diff_bdd,
dsimp only [BASE, SECP_REM], simp_int_casts, norm_num1 },
symmetry,
rw [int.modeq_iff_dvd, int.dvd_iff_mod_eq_zero, ←this, diff_eq, bigint3.sub_val,
bigint3.cmul_val, bigint3.cmul_val],
apply int.modeq.sub,
apply int.modeq.mul_left,
apply int.modeq.symm,
apply bigint3.sqr_val,
rw mul_assoc,
apply int.modeq.mul_left,
apply int.modeq.symm,
apply bigint3.mul_val
end
/-
-- Function: compute_slope
-/
/- compute_slope autogenerated specification -/
-- Do not change this definition.
def auto_spec_compute_slope (mem : F → F) (κ : ℕ) (range_check_ptr : F) (point0 point1 : EcPoint mem) (ρ_range_check_ptr : F) (ρ_slope : BigInt3 mem) : Prop :=
∃ (κ₁ : ℕ) (range_check_ptr₁ : F) (slope : BigInt3 mem), spec_nondet_bigint3 mem κ₁ range_check_ptr range_check_ptr₁ slope ∧
∃ x_diff : BigInt3 mem, x_diff = {
d0 := point0.x.d0 - point1.x.d0,
d1 := point0.x.d1 - point1.x.d1,
d2 := point0.x.d2 - point1.x.d2
} ∧
∃ (κ₂ : ℕ) (x_diff_slope : UnreducedBigInt3 mem), spec_unreduced_mul mem κ₂ x_diff slope x_diff_slope ∧
∃ (κ₃ : ℕ) (range_check_ptr₂ : F), spec_verify_zero mem κ₃ range_check_ptr₁ {
d0 := x_diff_slope.d0 - point0.y.d0 + point1.y.d0,
d1 := x_diff_slope.d1 - point0.y.d1 + point1.y.d1,
d2 := x_diff_slope.d2 - point0.y.d2 + point1.y.d2
} range_check_ptr₂ ∧
κ₁ + κ₂ + κ₃ + 21 ≤ κ ∧
ρ_range_check_ptr = range_check_ptr₂ ∧
ρ_slope = slope
-- You may change anything in this definition except the name and arguments.
def spec_compute_slope (mem : F → F) (κ : ℕ) (range_check_ptr : F) (point0 point1 : EcPoint mem) (ρ_range_check_ptr : F) (ρ_slope : BigInt3 mem) : Prop :=
∀ ix0 iy0 ix1 iy1 : bigint3,
ix0.bounded (3 * BASE - 1) →
iy0.bounded (3 * BASE - 1) →
ix1.bounded (3 * BASE - 1) →
iy1.bounded (3 * BASE - 1) →
point0.x = ix0.toBigInt3 →
point0.y = iy0.toBigInt3 →
point1.x = ix1.toBigInt3 →
point1.y = iy1.toBigInt3 →
∃ is : bigint3,
is.bounded (3 * (BASE - 1)) ∧
ρ_slope = is.toBigInt3 ∧
(ix0.val - ix1.val) * is.val ≡ (iy0.val - iy1.val) [ZMOD SECP_PRIME]
/- compute_slope soundness theorem -/
-- Do not change the statement of this theorem. You may change the proof.
theorem sound_compute_slope
{mem : F → F}
(κ : ℕ)
(range_check_ptr : F) (point0 point1 : EcPoint mem) (ρ_range_check_ptr : F) (ρ_slope : BigInt3 mem)
(h_auto : auto_spec_compute_slope mem κ range_check_ptr point0 point1 ρ_range_check_ptr ρ_slope) :
spec_compute_slope mem κ range_check_ptr point0 point1 ρ_range_check_ptr ρ_slope :=
begin
rcases h_auto with ⟨_, _, slope, valid_slope, x_diff, x_diff_eq,
_, x_diff_slope, h_x_diff_slope, _, _, vz, _, _, ret1eq⟩,
rcases nondet_bigint3_corr valid_slope with ⟨is, slope_eq, isbdd⟩,
intros ix0 iy0 ix1 iy1 ix0bdd iy0bdd ix1bdd iy1bdd pt0xeq pt0yeq pt1xeq pt1yeq,
set ix_diff := ix0.sub ix1 with ix_diff_eq,
have x_diff_eq' : x_diff = ix_diff.toBigInt3,
{ rw [ix_diff_eq, x_diff_eq, bigint3.toBigInt3_sub, BigInt3.sub, pt0xeq, pt1xeq] },
have x_diff_slope_eq := h_x_diff_slope _ _ x_diff_eq' slope_eq,
refine ⟨_, isbdd, ret1eq.trans slope_eq, _⟩,
set diff : bigint3 := (ix_diff.mul is).sub (iy0.sub iy1) with diff_eq,
have diff_bdd : diff.bounded
(((3 * BASE - 1) + (3 * BASE - 1))^2 * (8 * SECP_REM + 1) + ((3 * BASE - 1) + (3 * BASE - 1))),
{ rw [diff_eq],
apply bigint3.bounded_sub,
apply bigint3.bounded_mul,
apply bigint3.bounded_sub ix0bdd ix1bdd,
apply bigint3.bounded_of_bounded_of_le isbdd,
unfold BASE, simp_int_casts, norm_num1,
apply bigint3.bounded_sub iy0bdd iy1bdd },
have : diff.val % SECP_PRIME = 0,
{ apply vz,
{ simp only [diff_eq, x_diff_slope_eq, ix_diff_eq, pt0xeq, pt1xeq, pt0yeq, pt1yeq],
dsimp [bigint3.toUnreducedBigInt3, bigint3.toBigInt3, bigint3.mul, bigint3.sub], simp [←sub_add] },
apply bigint3.bounded_of_bounded_of_le diff_bdd,
dsimp only [BASE, SECP_REM], simp_int_casts, norm_num1 },
symmetry,
rw [int.modeq_iff_dvd, int.dvd_iff_mod_eq_zero, ←this, diff_eq, bigint3.sub_val,
bigint3.sub_val],
apply int.modeq.sub,
apply int.modeq.symm,
apply int.modeq.trans,
apply bigint3.mul_val,
apply int.modeq.mul_right,
rw [ix_diff_eq, bigint3.sub_val],
apply int.modeq.refl
end
/-
-- Function: ec_double
-/
/- ec_double autogenerated specification -/
-- Do not change this definition.
def auto_spec_ec_double_block5 (mem : F → F) (κ : ℕ) (range_check_ptr : F) (point : EcPoint mem) (ρ_range_check_ptr : F) (ρ_res : EcPoint mem) : Prop :=
∃ (κ₁ : ℕ) (range_check_ptr₁ : F) (slope : BigInt3 mem), spec_compute_doubling_slope mem κ₁ range_check_ptr point range_check_ptr₁ slope ∧
∃ (κ₂ : ℕ) (slope_sqr : UnreducedBigInt3 mem), spec_unreduced_sqr mem κ₂ slope slope_sqr ∧
∃ (κ₃ : ℕ) (range_check_ptr₂ : F) (new_x : BigInt3 mem), spec_nondet_bigint3 mem κ₃ range_check_ptr₁ range_check_ptr₂ new_x ∧
∃ (κ₄ : ℕ) (range_check_ptr₃ : F) (new_y : BigInt3 mem), spec_nondet_bigint3 mem κ₄ range_check_ptr₂ range_check_ptr₃ new_y ∧
∃ (κ₅ : ℕ) (range_check_ptr₄ : F), spec_verify_zero mem κ₅ range_check_ptr₃ {
d0 := slope_sqr.d0 - new_x.d0 - 2 * point.x.d0,
d1 := slope_sqr.d1 - new_x.d1 - 2 * point.x.d1,
d2 := slope_sqr.d2 - new_x.d2 - 2 * point.x.d2
} range_check_ptr₄ ∧
∃ (κ₆ : ℕ) (x_diff_slope : UnreducedBigInt3 mem), spec_unreduced_mul mem κ₆ {
d0 := point.x.d0 - new_x.d0,
d1 := point.x.d1 - new_x.d1,
d2 := point.x.d2 - new_x.d2
} slope x_diff_slope ∧
∃ (κ₇ : ℕ) (range_check_ptr₅ : F), spec_verify_zero mem κ₇ range_check_ptr₄ {
d0 := x_diff_slope.d0 - point.y.d0 - new_y.d0,
d1 := x_diff_slope.d1 - point.y.d1 - new_y.d1,
d2 := x_diff_slope.d2 - point.y.d2 - new_y.d2
} range_check_ptr₅ ∧
κ₁ + κ₂ + κ₃ + κ₄ + κ₅ + κ₆ + κ₇ + 49 ≤ κ ∧
ρ_range_check_ptr = range_check_ptr₅ ∧
ρ_res = {
x := new_x,
y := new_y
}
def auto_spec_ec_double (mem : F → F) (κ : ℕ) (range_check_ptr : F) (point : EcPoint mem) (ρ_range_check_ptr : F) (ρ_res : EcPoint mem) : Prop :=
((point.x.d0 = 0 ∧
((point.x.d1 = 0 ∧
((point.x.d2 = 0 ∧
11 ≤ κ ∧
ρ_range_check_ptr = range_check_ptr ∧
ρ_res = point) ∨
(point.x.d2 ≠ 0 ∧
∃ (κ₁ : ℕ), auto_spec_ec_double_block5 mem κ₁ range_check_ptr point ρ_range_check_ptr ρ_res ∧
κ₁ + 3 ≤ κ))) ∨
(point.x.d1 ≠ 0 ∧
∃ (κ₁ : ℕ), auto_spec_ec_double_block5 mem κ₁ range_check_ptr point ρ_range_check_ptr ρ_res ∧
κ₁ + 2 ≤ κ))) ∨
(point.x.d0 ≠ 0 ∧
∃ (κ₁ : ℕ), auto_spec_ec_double_block5 mem κ₁ range_check_ptr point ρ_range_check_ptr ρ_res ∧
κ₁ + 1 ≤ κ))
-- Added manually
theorem auto_spec_ec_double_better {mem : F → F} {κ : ℕ}{range_check_ptr : F} {point : EcPoint mem} {ρ_range_check_ptr : F} {ρ_res : EcPoint mem} (h : auto_spec_ec_double mem κ range_check_ptr point ρ_range_check_ptr ρ_res ) :
(point.x.d0 = 0 ∧ point.x.d1 = 0 ∧ point.x.d2 = 0 ∧
ρ_range_check_ptr = range_check_ptr ∧ ρ_res = point) ∨
((point.x.d2 ≠ 0 ∨ point.x.d1 ≠ 0 ∨ point.x.d0 ≠ 0) ∧
∃ (κ₁ : ℕ), auto_spec_ec_double_block5 mem κ₁ range_check_ptr point ρ_range_check_ptr ρ_res) :=
begin
rcases h with (⟨ptx0z, (⟨ptx1z, ⟨ptx2z, h2⟩ | ⟨ptx2nz, ⟨h31, h32, _⟩⟩⟩ | ⟨ptx1nz, ⟨h41, h42, _⟩⟩)⟩ | ⟨ptx0nz, ⟨h51, h52, _⟩⟩),
{ left, use [ptx0z, ptx1z, ptx2z, h2.2] },
{ right, split, left, assumption, exact ⟨h31, h32⟩ },
{ right, split, right, left, assumption, exact ⟨h41, h42⟩ },
right, split, right, right, assumption, exact ⟨h51, h52⟩,
end
-- You may change anything in this definition except the name and arguments.
def spec_ec_double (mem : F → F) (κ : ℕ) (range_check_ptr : F) (point : EcPoint mem) (ρ_range_check_ptr : F) (ρ_res : EcPoint mem) : Prop :=
(point.x = ⟨0, 0, 0⟩ ∧ ρ_res = point) ∨
(point.x ≠ ⟨0, 0, 0⟩ ∧
∀ ix iy : bigint3,
ix.bounded (3 * BASE - 1) →
iy.bounded (3 * BASE - 1) →
point.x = ix.toBigInt3 →
point.y = iy.toBigInt3 →
∃ is inew_x inew_y : bigint3,
is.bounded (3 * (BASE - 1)) ∧
inew_x.bounded (3 * (BASE - 1)) ∧
inew_y.bounded (3 * (↑BASE - 1)) ∧
ρ_res = { x := inew_x.toBigInt3, y := inew_y.toBigInt3 } ∧
3 * ix.val^2 ≡ 2 * iy.val * is.val [ZMOD SECP_PRIME] ∧
inew_x.val ≡ is.val^2 - 2 * ix.val [ZMOD SECP_PRIME] ∧
inew_y.val ≡ (ix.val - inew_x.val) * is.val - iy.val [ZMOD SECP_PRIME])
/- ec_double soundness theorem -/
-- Do not change the statement of this theorem. You may change the proof.
theorem sound_ec_double
{mem : F → F}
(κ : ℕ)
(range_check_ptr : F) (point : EcPoint mem) (ρ_range_check_ptr : F) (ρ_res : EcPoint mem)
(h_auto : auto_spec_ec_double mem κ range_check_ptr point ρ_range_check_ptr ρ_res) :
spec_ec_double mem κ range_check_ptr point ρ_range_check_ptr ρ_res :=
begin
rcases auto_spec_ec_double_better h_auto with
⟨ptx0z, ptx1z, ptx2z, _, reseq⟩ | ⟨nz, _, main_case⟩,
{ left, simp [BigInt3.ext_iff, ptx0z, ptx1z, ptx2z, reseq] },
rcases main_case with ⟨_, _, slope, h_doubling_slope,
_, islope_sqr, h_slope_square,
_, _, new_x, vv_new_x, _, _, new_y, vv_new_y, _, _, vz,
_, x_diff_slope, h_x_diff_slope,
_, _, vz', _, _, ret1_eq⟩,
right, split,
{ simp [BigInt3.ext_iff], tauto },
intros ix iy ixbdd iybdd ptxeq ptyeq,
rcases h_doubling_slope ix iy ixbdd iybdd ptxeq ptyeq with ⟨is, isbdd, slope_eq, slope_congr⟩,
have islope_sqr_eq := h_slope_square _ slope_eq,
rcases nondet_bigint3_corr vv_new_x with ⟨inew_x, inew_x_eq, inew_x_bdd⟩,
rcases nondet_bigint3_corr vv_new_y with ⟨inew_y, inew_y_eq, inew_y_bdd⟩,
set diff1 := ((is.mul is).sub inew_x).sub (ix.cmul 2) with diff1_eq,
have diff1_bdd : diff1.bounded
((3 * (BASE - 1))^2 * (8 * SECP_REM + 1) + 3 * (BASE - 1) + 2 * (3 * BASE - 1)),
{ rw [diff1_eq],
apply bigint3.bounded_sub,
apply bigint3.bounded_sub _ inew_x_bdd,
apply bigint3.bounded_mul isbdd isbdd,
apply bigint3.bounded_cmul ixbdd },
have diff1_equiv : diff1.val % SECP_PRIME = 0,
{ apply vz,
{ simp only [diff1_eq, islope_sqr_eq, ptxeq, inew_x_eq],
dsimp [bigint3.toUnreducedBigInt3, bigint3.toBigInt3, bigint3.mul, bigint3.sub, bigint3.cmul,
bigint3.sqr],
simp only [int.cast_sub, int.cast_mul 2], simp_int_casts,
split, refl, split, refl, refl },
apply bigint3.bounded_of_bounded_of_le diff1_bdd,
dsimp only [BASE, SECP_REM], simp_int_casts, norm_num1 },
have : (ix.sub inew_x).toBigInt3 =
{ d0 := point.x.d0 - new_x.d0, d1 := point.x.d1 - new_x.d1, d2 := point.x.d2 - new_x.d2 },
{ rw [bigint3.toBigInt3_sub, ←ptxeq, ←inew_x_eq], refl },
have x_diff_slope_eq := h_x_diff_slope _ _ this.symm slope_eq,
set diff2 := (((ix.sub inew_x).mul is).sub iy).sub inew_y with diff2_eq,
have diff2_bdd : diff2.bounded
(((3 * BASE - 1) + (3 * (BASE - 1)))^2 * (8 * SECP_REM + 1) + (3 * BASE - 1) + 3 * (BASE - 1)),
{ rw [diff2_eq],
apply bigint3.bounded_sub _ inew_y_bdd,
apply bigint3.bounded_sub _ iybdd,
apply bigint3.bounded_mul,
apply bigint3.bounded_sub ixbdd inew_x_bdd,
apply bigint3.bounded_of_bounded_of_le isbdd,
apply le_add_of_nonneg_left,
dsimp only [BASE, SECP_REM], simp_int_casts, norm_num1 },
have diff2_equiv : diff2.val % SECP_PRIME = 0,
{ apply vz',
{ simp only [diff2_eq, x_diff_slope_eq, inew_y_eq, bigint3.toBigInt3,
bigint3.toUnreducedBigInt3, bigint3.mul, bigint3.sub, ptyeq, int.cast_sub],
split, refl, split, refl, split },
apply bigint3.bounded_of_bounded_of_le diff2_bdd,
dsimp only [BASE, SECP_REM], simp_int_casts, norm_num1 },
use [is, inew_x, inew_y, isbdd, inew_x_bdd, inew_y_bdd],
split, rw [ret1_eq, inew_x_eq, inew_y_eq],
use slope_congr,
split,
{ rw [int.modeq_iff_dvd, int.dvd_iff_mod_eq_zero, ←diff1_equiv, diff1_eq, bigint3.sub_val,
bigint3.sub_val, bigint3.cmul_val, sub_sub, add_comm, ←sub_sub, pow_two],
apply int.modeq.sub_right,
apply int.modeq.sub_right,
symmetry,
apply bigint3.mul_val },
rw [int.modeq_iff_dvd, int.dvd_iff_mod_eq_zero, ←diff2_equiv, diff2_eq, bigint3.sub_val,
bigint3.sub_val],
apply int.modeq.sub_right,
apply int.modeq.sub_right,
symmetry,
transitivity,
apply bigint3.mul_val,
rw [bigint3.sub_val]
end
/- Better specification -/
def spec_ec_double' {mem : F → F} ( pt : EcPoint mem ) ( ret1 : EcPoint mem )
(secpF : Type) [secp_field secpF] : Prop :=
∀ h : BddECPointData secpF pt,
∃ hret : BddECPointData secpF ret1,
hret.toECPoint = 2 • h.toECPoint
theorem spec_ec_double'_of_spec_ec_double
{mem : F → F} {κ : ℕ} {range_check_ptr : F} {pt : EcPoint mem} {ret0 : F} {ret1 : EcPoint mem}
(h : spec_ec_double mem κ range_check_ptr pt ret0 ret1)
(secpF : Type) [secp_field secpF] :
spec_ec_double' pt ret1 secpF :=
begin
intros hpt,
rcases h with ⟨ptx0, reteq⟩ | ⟨ptxnz, h⟩,
{ rw reteq, use hpt,
rw [hpt.toECPoint_eq_zero_iff.mpr ptx0, smul_zero] },
rcases h hpt.ix hpt.iy hpt.ixbdd hpt.iybdd hpt.ptxeq hpt.ptyeq with
⟨is, inew_x, inew_y, is_bdd, inew_x_bdd, inew_y_bdd, ret1eq, mod1eq, mod2eq, mod3eq⟩,
have onec_pt := hpt.onEC' ptxnz,
have hptynez : (hpt.iy.val : secpF) ≠ 0 := secp_field.y_ne_zero_of_on_ec onec_pt,
have eq1 : 3 * (hpt.ix.val : secpF)^2 = 2 * hpt.iy.val * is.val,
{ norm_cast, rw @eq_iff_modeq_int secpF _ SECP_PRIME,
apply mod1eq },
have eq2 : (inew_x.val : secpF) = is.val ^ 2 - 2 * hpt.ix.val,
{ norm_cast, rw @eq_iff_modeq_int secpF _ SECP_PRIME,
apply mod2eq },
have eq3: (inew_y.val : secpF) = (hpt.ix.val - inew_x.val) * is.val - hpt.iy.val,
{ norm_cast, rw @eq_iff_modeq_int secpF _ SECP_PRIME,
apply mod3eq },
have eq1a : (is.val : secpF) = 3 * (hpt.ix.val : secpF)^2 / (2 * hpt.iy.val),
{ field_simp [ec_field.two_ne_zero], rw [mul_comm, eq1] },
have ecdoubleeq : ec_double ((hpt.ix.val : secpF), hpt.iy.val) = (inew_x.val, inew_y.val),
{ simp [ec_double, ec_double_slope],
split, rw [eq2, eq1a, div_pow],
rw [eq3, eq1a], congr, rw [eq2, eq1a, div_pow] },
have onec_new: on_ec (↑(inew_x.val), ↑(inew_y.val)),
{ have := @on_ec_ec_double secpF _ (↑(hpt.ix.val), ↑(hpt.iy.val)) onec_pt hptynez,
rw ecdoubleeq at this, exact this },
have hhret: ¬ (inew_x.i0 = 0 ∧ inew_x.i1 = 0 ∧ inew_x.i2 = 0),
{ contrapose! onec_new,
rw [on_ec], dsimp,
conv { congr, to_rhs, simp [bigint3.val, onec_new.1, onec_new.2.1, onec_new.2.2] },
apply secp_field.seven_not_square },
let hret : BddECPointData secpF ret1 :=
{ ix := inew_x,
iy := inew_y,
ixbdd := by apply bigint3.bounded_of_bounded_of_le inew_x_bdd; norm_num [BASE],
iybdd := by apply bigint3.bounded_of_bounded_of_le inew_y_bdd; norm_num [BASE],
ptxeq := by { rw ret1eq },
ptyeq := by { rw ret1eq },
onEC := or.inr onec_new },
use hret,
have : ret1.x ≠ ⟨0, 0, 0⟩,
{ rw [ret1eq], dsimp,
have h' := secp_field.x_ne_zero_of_on_ec onec_new,
contrapose! h',
have : inew_x = ⟨0, 0, 0⟩ := toBigInt3_eq_zero_of_bounded_3BASE h'
(bigint3.bounded_of_bounded_of_le inew_x_bdd bound_slack),
simp [this, bigint3.val] },
dsimp [BddECPointData.toECPoint], simp [dif_neg ptxnz, dif_neg this],
symmetry,
apply double_Affine _ _ ecdoubleeq
end
/-
-- Function: fast_ec_add
-/
/- fast_ec_add autogenerated specification -/
-- Do not change this definition.
def auto_spec_fast_ec_add_block9 (mem : F → F) (κ : ℕ) (range_check_ptr : F) (point0 point1 : EcPoint mem) (ρ_range_check_ptr : F) (ρ_res : EcPoint mem) : Prop :=
∃ (κ₁ : ℕ) (range_check_ptr₁ : F) (slope : BigInt3 mem), spec_compute_slope mem κ₁ range_check_ptr point0 point1 range_check_ptr₁ slope ∧
∃ (κ₂ : ℕ) (slope_sqr : UnreducedBigInt3 mem), spec_unreduced_sqr mem κ₂ slope slope_sqr ∧
∃ (κ₃ : ℕ) (range_check_ptr₂ : F) (new_x : BigInt3 mem), spec_nondet_bigint3 mem κ₃ range_check_ptr₁ range_check_ptr₂ new_x ∧
∃ (κ₄ : ℕ) (range_check_ptr₃ : F) (new_y : BigInt3 mem), spec_nondet_bigint3 mem κ₄ range_check_ptr₂ range_check_ptr₃ new_y ∧
∃ (κ₅ : ℕ) (range_check_ptr₄ : F), spec_verify_zero mem κ₅ range_check_ptr₃ {
d0 := slope_sqr.d0 - new_x.d0 - point0.x.d0 - point1.x.d0,
d1 := slope_sqr.d1 - new_x.d1 - point0.x.d1 - point1.x.d1,
d2 := slope_sqr.d2 - new_x.d2 - point0.x.d2 - point1.x.d2
} range_check_ptr₄ ∧
∃ (κ₆ : ℕ) (x_diff_slope : UnreducedBigInt3 mem), spec_unreduced_mul mem κ₆ {
d0 := point0.x.d0 - new_x.d0,
d1 := point0.x.d1 - new_x.d1,
d2 := point0.x.d2 - new_x.d2
} slope x_diff_slope ∧
∃ (κ₇ : ℕ) (range_check_ptr₅ : F), spec_verify_zero mem κ₇ range_check_ptr₄ {
d0 := x_diff_slope.d0 - point0.y.d0 - new_y.d0,
d1 := x_diff_slope.d1 - point0.y.d1 - new_y.d1,
d2 := x_diff_slope.d2 - point0.y.d2 - new_y.d2
} range_check_ptr₅ ∧
κ₁ + κ₂ + κ₃ + κ₄ + κ₅ + κ₆ + κ₇ + 52 ≤ κ ∧
ρ_range_check_ptr = range_check_ptr₅ ∧
ρ_res = {
x := new_x,
y := new_y
}
def auto_spec_fast_ec_add_block5 (mem : F → F) (κ : ℕ) (range_check_ptr : F) (point0 point1 : EcPoint mem) (ρ_range_check_ptr : F) (ρ_res : EcPoint mem) : Prop :=
((point1.x.d0 = 0 ∧
((point1.x.d1 = 0 ∧
((point1.x.d2 = 0 ∧
11 ≤ κ ∧
ρ_range_check_ptr = range_check_ptr ∧
ρ_res = point0) ∨
(point1.x.d2 ≠ 0 ∧
∃ (κ₁ : ℕ), auto_spec_fast_ec_add_block9 mem κ₁ range_check_ptr point0 point1 ρ_range_check_ptr ρ_res ∧
κ₁ + 3 ≤ κ))) ∨
(point1.x.d1 ≠ 0 ∧
∃ (κ₁ : ℕ), auto_spec_fast_ec_add_block9 mem κ₁ range_check_ptr point0 point1 ρ_range_check_ptr ρ_res ∧
κ₁ + 2 ≤ κ))) ∨
(point1.x.d0 ≠ 0 ∧
∃ (κ₁ : ℕ), auto_spec_fast_ec_add_block9 mem κ₁ range_check_ptr point0 point1 ρ_range_check_ptr ρ_res ∧
κ₁ + 1 ≤ κ))
def auto_spec_fast_ec_add (mem : F → F) (κ : ℕ) (range_check_ptr : F) (point0 point1 : EcPoint mem) (ρ_range_check_ptr : F) (ρ_res : EcPoint mem) : Prop :=
((point0.x.d0 = 0 ∧
((point0.x.d1 = 0 ∧
((point0.x.d2 = 0 ∧
11 ≤ κ ∧
ρ_range_check_ptr = range_check_ptr ∧
ρ_res = point1) ∨
(point0.x.d2 ≠ 0 ∧
∃ (κ₁ : ℕ), auto_spec_fast_ec_add_block5 mem κ₁ range_check_ptr point0 point1 ρ_range_check_ptr ρ_res ∧
κ₁ + 3 ≤ κ))) ∨
(point0.x.d1 ≠ 0 ∧
∃ (κ₁ : ℕ), auto_spec_fast_ec_add_block5 mem κ₁ range_check_ptr point0 point1 ρ_range_check_ptr ρ_res ∧
κ₁ + 2 ≤ κ))) ∨
(point0.x.d0 ≠ 0 ∧
∃ (κ₁ : ℕ), auto_spec_fast_ec_add_block5 mem κ₁ range_check_ptr point0 point1 ρ_range_check_ptr ρ_res ∧
κ₁ + 1 ≤ κ))
-- You may change anything in this definition except the name and arguments.
def spec_fast_ec_add (mem : F → F) (κ : ℕ) (range_check_ptr : F) (point0 point1 : EcPoint mem) (ρ_range_check_ptr : F) (ρ_res : EcPoint mem) : Prop :=
∀ ix0 iy0 ix1 iy1 : bigint3,
ix0.bounded (3 * BASE - 1) →
iy0.bounded (3 * BASE - 1) →
ix1.bounded (3 * BASE - 1) →
iy1.bounded (3 * BASE - 1) →
point0.x = ix0.toBigInt3 →
point0.y = iy0.toBigInt3 →
point1.x = ix1.toBigInt3 →
point1.y = iy1.toBigInt3 →
(point0.x.d0 = 0 ∧ point0.x.d1 = 0 ∧ point0.x.d2 = 0 ∧ ρ_res = point1) ∨
(point1.x.d0 = 0 ∧ point1.x.d1 = 0 ∧ point1.x.d2 = 0 ∧ ρ_res = point0) ∨
¬ (point0.x.d0 = 0 ∧ point0.x.d1 = 0 ∧ point0.x.d2 = 0) ∧
¬ (point1.x.d0 = 0 ∧ point1.x.d1 = 0 ∧ point1.x.d2 = 0) ∧
∃ is inew_x inew_y : bigint3,
is.bounded (3 * (BASE - 1)) ∧
inew_x.bounded (3 * (BASE - 1)) ∧
inew_y.bounded (3 * (BASE - 1)) ∧
ρ_res = { x := inew_x.toBigInt3, y := inew_y.toBigInt3 } ∧
(ix0.val - ix1.val) * is.val ≡ (iy0.val - iy1.val) [ZMOD SECP_PRIME] ∧
inew_x.val ≡ is.val^2 - ix0.val - ix1.val [ZMOD SECP_PRIME] ∧
inew_y.val ≡ (ix0.val - inew_x.val) * is.val - iy0.val [ZMOD SECP_PRIME]
/- fast_ec_add soundness theorem -/
theorem fast_ec_add_block5_spec_better {mem : F → F} {κ : ℕ}{range_check_ptr : F} {pt0 pt1 : EcPoint mem} {ret0 : F} {ret1 : EcPoint mem}
(h_auto : auto_spec_fast_ec_add_block5 mem κ range_check_ptr pt0 pt1 ret0 ret1) :
(pt1.x.d0 = 0 ∧ pt1.x.d1 = 0 ∧ pt1.x.d2 = 0 ∧
ret0 = range_check_ptr ∧ ret1 = pt0) ∨
(¬ (pt1.x.d0 = 0 ∧ pt1.x.d1 = 0 ∧ pt1.x.d2 = 0) ∧
∃ κ₁, auto_spec_fast_ec_add_block9 mem κ₁ range_check_ptr pt0 pt1 ret0 ret1) :=
begin
rcases h_auto with
⟨pt1x0z, ⟨pt1x1z, ⟨pt1x2z, ⟨_, ret0, ret1⟩⟩ | ⟨pt1x2nz, ⟨κ₁, hblock9⟩⟩⟩ |
⟨pt1x1nz, ⟨κ₁, hblock9⟩⟩⟩ | ⟨pt1x0nz, ⟨κ₁, hblock9⟩⟩,
{ left, use [pt1x0z, pt1x1z, pt1x2z, ret0, ret1] },
{ right, split, intro h, apply pt1x2nz h.2.2,
exact ⟨κ₁, hblock9.1⟩ },
{ right, split, intro h, apply pt1x1nz h.2.1,
exact ⟨κ₁, hblock9.1⟩ },
{ right, split, intro h, apply pt1x0nz h.1,
exact ⟨κ₁, hblock9.1⟩ }
end
theorem ec_add_mainb_spec_better {mem : F → F} {κ : ℕ} {range_check_ptr : F} {pt0 pt1 : EcPoint mem} {ret0 : F} {ret1 : EcPoint mem}
(h_auto : auto_spec_fast_ec_add mem κ range_check_ptr pt0 pt1 ret0 ret1) :
(pt0.x.d0 = 0 ∧ pt0.x.d1 = 0 ∧ pt0.x.d2 = 0 ∧
ret0 = range_check_ptr ∧ ret1 = pt1) ∨
(pt1.x.d0 = 0 ∧ pt1.x.d1 = 0 ∧ pt1.x.d2 = 0 ∧
ret0 = range_check_ptr ∧ ret1 = pt0) ∨
(¬(pt0.x.d0 = 0 ∧ pt0.x.d1 = 0 ∧ pt0.x.d2 = 0) ∧
¬ (pt1.x.d0 = 0 ∧ pt1.x.d1 = 0 ∧ pt1.x.d2 = 0) ∧
∃ κ₁, auto_spec_fast_ec_add_block9 mem κ₁ range_check_ptr pt0 pt1 ret0 ret1) :=
begin
rcases h_auto with
⟨pt1x0z, ⟨pt1x1z, ⟨pt1x2z, ⟨_, ret0, ret1⟩⟩ | ⟨pt1x2nz, κ₁, hblock9⟩⟩ |
⟨pt1x1nz, κ₁, hblock9⟩⟩ | ⟨pt1x0nz, κ₁, hblock9⟩,
{ left, use [pt1x0z, pt1x1z, pt1x2z, ret0, ret1] },
{ right,
cases fast_ec_add_block5_spec_better hblock9.1 with h' h',
{ left, exact h' },
right, split,
{ intro h, exact pt1x2nz h.2.2 },
exact h' },
{ right,
cases fast_ec_add_block5_spec_better hblock9.1 with h' h',
{ left, exact h' },
right, split,
{ intro h, exact pt1x1nz h.2.1 },
exact h' },
{ right,
cases fast_ec_add_block5_spec_better hblock9.1 with h' h',
{ left, exact h' },
right, split,
{ intro h, exact pt1x0nz h.1 },
exact h' },
end
-- Do not change the statement of this theorem. You may change the proof.
theorem sound_fast_ec_add
{mem : F → F}
(κ : ℕ)
(range_check_ptr : F) (point0 point1 : EcPoint mem) (ρ_range_check_ptr : F) (ρ_res : EcPoint mem)
(h_auto : auto_spec_fast_ec_add mem κ range_check_ptr point0 point1 ρ_range_check_ptr ρ_res) :
spec_fast_ec_add mem κ range_check_ptr point0 point1 ρ_range_check_ptr ρ_res :=
begin
intros ix0 iy0 ix1 iy1 ix0bdd iy0bdd ix1bdd iy1bdd pt0xeq pt0yeq pt1xeq pt1yeq,
rcases ec_add_mainb_spec_better h_auto with
⟨pt0x0z, pt0x1z, pt0x2z, _, ret1eq⟩ |
⟨pt1x0z, pt1x1z, pt1x2z, _, ret1eq⟩ |
⟨pt0nz, pt1nz, _, _, _, slope, h_compute_slope,
_, islope_sqr, h_slope_sqr,
_, _, new_x, vv_new_x, _, _, new_y, vv_new_y, _, _, vz,
_, x_diff_slope, h_x_diff_slope,
_, _, vz', _, _, ret1_eq⟩,
{ left, use [pt0x0z, pt0x1z, pt0x2z, ret1eq] },
{ right, left, use [pt1x0z, pt1x1z, pt1x2z, ret1eq] },
rcases h_compute_slope ix0 iy0 ix1 iy1 ix0bdd iy0bdd ix1bdd iy1bdd pt0xeq pt0yeq pt1xeq pt1yeq
with ⟨is, isbdd, slope_eq, slope_congr⟩,
have islope_sqr_eq := h_slope_sqr _ slope_eq,
rcases nondet_bigint3_corr vv_new_x with ⟨inew_x, inew_x_eq, inew_x_bdd⟩,
rcases nondet_bigint3_corr vv_new_y with ⟨inew_y, inew_y_eq, inew_y_bdd⟩,
set diff1 := (((is.mul is).sub inew_x).sub ix0).sub ix1 with diff1_eq,
have diff1_bdd : diff1.bounded
((3 * (BASE - 1))^2 * (8 * SECP_REM + 1) + 3 * (BASE - 1) + (3 * BASE - 1) + (3 * BASE - 1)),
{ rw [diff1_eq],
apply bigint3.bounded_sub _ ix1bdd,
apply bigint3.bounded_sub _ ix0bdd,
apply bigint3.bounded_sub _ inew_x_bdd,
apply bigint3.bounded_mul isbdd isbdd },
have diff1_equiv : diff1.val % SECP_PRIME = 0,
{ apply vz,
{ simp only [diff1_eq, islope_sqr_eq, pt0xeq, pt1xeq, inew_x_eq],
dsimp [bigint3.toBigInt3, bigint3.toUnreducedBigInt3, bigint3.mul, bigint3.sub, bigint3.cmul,
bigint3.sqr],
simp only [int.cast_sub, int.cast_mul 2], simp_int_casts,
split, refl, split, refl, refl },
apply bigint3.bounded_of_bounded_of_le diff1_bdd,
dsimp only [BASE, SECP_REM], simp_int_casts, norm_num1 },
have : (ix0.sub inew_x).toBigInt3 =
{ d0 := point0.x.d0 - new_x.d0, d1 := point0.x.d1 - new_x.d1, d2 := point0.x.d2 - new_x.d2 },
{ rw [bigint3.toBigInt3_sub, ←pt0xeq, ←inew_x_eq], refl },
have x_diff_slope_eq := h_x_diff_slope _ _ this.symm slope_eq,
set diff2 := (((ix0.sub inew_x).mul is).sub iy0).sub inew_y with diff2_eq,
have diff2_bdd : diff2.bounded
(((3 * BASE - 1) + (3 * (BASE - 1)))^2 * (8 * SECP_REM + 1) + (3 * BASE - 1) + 3 * (BASE - 1)),
{ rw [diff2_eq],
apply bigint3.bounded_sub _ inew_y_bdd,
apply bigint3.bounded_sub _ iy0bdd,
apply bigint3.bounded_mul,
apply bigint3.bounded_sub ix0bdd inew_x_bdd,
apply bigint3.bounded_of_bounded_of_le isbdd,
apply le_add_of_nonneg_left,
dsimp only [BASE, SECP_REM], simp_int_casts, norm_num1 },
have diff2_equiv : diff2.val % SECP_PRIME = 0,
{ apply vz',
{ simp only [diff2_eq, x_diff_slope_eq, inew_y_eq, bigint3.toBigInt3,
bigint3.toUnreducedBigInt3, bigint3.mul, bigint3.sub, pt0yeq, int.cast_sub],
split, refl, split, refl, split },
apply bigint3.bounded_of_bounded_of_le diff2_bdd,
dsimp only [BASE, SECP_REM], simp_int_casts, norm_num1 },
right, right,
use [pt0nz, pt1nz, is, inew_x, inew_y, isbdd, inew_x_bdd, inew_y_bdd],
split, rw [ret1_eq, inew_x_eq, inew_y_eq],
use slope_congr,
split,
{ rw [int.modeq_iff_dvd, int.dvd_iff_mod_eq_zero, ←diff1_equiv, diff1_eq, bigint3.sub_val,
bigint3.sub_val, bigint3.sub_val, sub_sub, sub_sub, pow_two, ←add_assoc,
add_comm _ inew_x.val, sub_sub, ←sub_sub],
apply int.modeq.sub_right,
apply int.modeq.sub_right,
symmetry,
apply bigint3.mul_val },
rw [int.modeq_iff_dvd, int.dvd_iff_mod_eq_zero, ←diff2_equiv, diff2_eq, bigint3.sub_val,
bigint3.sub_val],
apply int.modeq.sub_right,
apply int.modeq.sub_right,
symmetry,
transitivity,
apply bigint3.mul_val,
rw [bigint3.sub_val]
end
/-
Better specification of fast_ec_add
-/
def spec_fast_ec_add' {mem : F → F} ( pt0 pt1 : EcPoint mem ) ( ret1 : EcPoint mem )
(secpF : Type) [secp_field secpF] : Prop :=
∀ h0 : BddECPointData secpF pt0,
∀ h1 : BddECPointData secpF pt1,
(↑(h0.ix.val) : secpF) ≠ ↑(h1.ix.val) →
∃ hret : BddECPointData secpF ret1,
hret.toECPoint = h0.toECPoint + h1.toECPoint
theorem spec_fast_ec_add'_of_spec_ec_add
{mem : F → F} {κ : ℕ} {range_check_ptr : F} {pt0 pt1 : EcPoint mem} {ret0 : F} {ret1 : EcPoint mem}
(h : spec_fast_ec_add mem κ range_check_ptr pt0 pt1 ret0 ret1)
(secpF : Type) [secp_field secpF] :
spec_fast_ec_add' pt0 pt1 ret1 secpF :=
begin
intros h0 h1 hne,
rcases h h0.ix h0.iy h1.ix h1.iy h0.ixbdd h0.iybdd h1.ixbdd h1.iybdd h0.ptxeq h0.ptyeq h1.ptxeq h1.ptyeq with
h' | h' | h',
{ rcases h' with ⟨pt0x0eq, pt0x1eq, pt0x2eq, rfl⟩,