-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelfConnect.dfy
1134 lines (1061 loc) · 40.4 KB
/
SelfConnect.dfy
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
module SelfConnect {
import opened Circ
import opened ConservedSubcircuit
import opened Scuf
import opened MapConnection
import opened MapFunction
import opened Eval
import opened Connection
import opened Utils
import opened Subcircuit
import opened Path
// A scuf is modified such that some of its outputs are connected to the inputs.
// The outputs remain unchanged, but the new input width is reduced.
// `InternalConnection` defines a mapping from the (new_inputs, outputs) to the original inputs.
datatype InternalConnection = InternalConnection(
ni_width: nat, // Width of the new inputs
i_width: nat, // Width of the original inputs
o_width: nat, // Width of the outputs.
connections: map<nat, nat>, // Maps input indices to output indices
i2ni: seq<nat>, // A map that can be used to generate new_inputs from inputs.
nio2i: seq<(bool, nat)> // A map to generate inputs from new_inputs and outputs.
) {
ghost predicate BasicValid()
{
&& (ni_width <= i_width)
&& (i_width <= ni_width + o_width)
&& (|i2ni| == ni_width)
&& (|nio2i| == i_width)
&& Seq.HasNoDuplicates(i2ni)
&& Seq.HasNoDuplicates(nio2i)
}
opaque predicate ToNIMapValid()
requires BasicValid()
{
forall ni_index: nat :: ni_index < ni_width ==> (
// For all items in the i2ni.
// When we look at where that item in the new inputs came from we get an address in the
// new inputs.
// When we look up that address in the nio2i we should see that it came from the new inputs.
var i_index: nat := i2ni[ni_index];
&& (i_index < i_width)
&& var (from_output, index) := nio2i[i_index];
&& (!from_output)
&& (index == ni_index)
)
}
opaque predicate ToIMapValid()
requires BasicValid()
{
forall i_index: nat :: i_index < i_width ==> (
// For all items in the old inputs.
// When we see where the input came from it could come from the new inputs or from the outputs.
// If it comes from the new inputs then we should be able to check that i2ni is consistent.
// If it comes from the outputs then we just check that consistent with `connections`.
var (from_output, index) := nio2i[i_index];
&& ((!from_output) ==> index < ni_width && i2ni[index] == i_index && (i_index !in connections))
&& ((from_output) ==> index < o_width && (i_index in connections) && (index == connections[i_index]))
)
}
opaque predicate ConnectionsValid()
{
&& (forall index: nat :: index in connections.Values ==> index < o_width)
&& (forall index: nat :: index in connections.Keys ==> index < i_width)
}
ghost predicate Valid() {
&& BasicValid()
&& ConnectionsValid()
&& ToNIMapValid()
&& ToIMapValid()
}
function NIO2I<T>(bni: seq<T>, bo: seq<T>): (bi: seq<T>)
requires Valid()
requires |bni| == ni_width
requires |bo| == o_width
ensures |bi| == i_width
{
reveal ToIMapValid();
seq(i_width, (i_index: int) requires 0 <= i_index < i_width =>
var (from_output, index) := nio2i[i_index];
if !from_output then
bni[index]
else
bo[index]
)
}
opaque function I2NIInternal<T>(bi: seq<T>, ni_index: nat): (bni: seq<T>)
requires Valid()
requires |bi| == i_width
requires ni_index <= ni_width
ensures |bni| == ni_index
ensures forall index: nat :: index < ni_index ==>
reveal ToNIMapValid();
bni[index] == bi[i2ni[index]]
decreases ni_index
{
if ni_index == 0 then
[]
else
reveal ToNIMapValid();
var i_index := i2ni[ni_index-1];
I2NIInternal(bi, ni_index-1) + [bi[i_index]]
}
function I2NI<T>(bi: seq<T>): (bni: seq<T>)
requires Valid()
requires |bi| == i_width
ensures |bni| == ni_width
{
reveal I2NIInternal();
I2NIInternal(bi, ni_width)
}
lemma I2NICorrect<T>(bi: seq<T>)
requires Valid()
requires |bi| == i_width
ensures
reveal ToNIMapValid();
var bni := I2NI(bi);
forall ni_index: nat :: ni_index < ni_width ==>
bni[ni_index] == bi[i2ni[ni_index]]
{
reveal I2NIInternal();
}
function MapConnectedInputs<T(==)>(bi: seq<T>): (r: set<T>)
requires Valid()
requires |bi| == i_width
{
reveal ConnectionsValid();
(set index | index in connections :: bi[index])
}
lemma I2NIProperties<T>(bi: seq<T>)
requires Valid()
requires |bi| == i_width
requires Seq.HasNoDuplicates(bi)
ensures
var bni := I2NI(bi);
&& Seq.HasNoDuplicates(bni)
&& Seq.ToSet(bni) == Seq.ToSet(bi) - MapConnectedInputs(bi)
{
reveal ToNIMapValid();
reveal ToIMapValid();
reveal ConnectionsValid();
reveal Seq.ToSet();
reveal Seq.HasNoDuplicates();
var bni := I2NI(bi);
forall i_index: nat | i_index < i_width
ensures bi[i_index] in Seq.ToSet(bni) || bi[i_index] in MapConnectedInputs(bi)
{
var (from_output, index) := nio2i[i_index];
if from_output {
assert bi[i_index] in MapConnectedInputs(bi);
} else {
reveal Seq.ToSet();
assert bi[i_index] == bni[index];
assert bi[i_index] in Seq.ToSet(bni);
}
}
}
lemma NIO2I2NI<T>(bni: seq<T>, bo: seq<T>)
requires Valid()
requires |bni| == ni_width
requires |bo| == o_width
ensures I2NI(NIO2I(bni, bo)) == bni
{
reveal ToNIMapValid();
reveal ToIMapValid();
}
lemma NI2NIO2I<T>(bi: seq<T>, bo: seq<T>)
requires Valid()
requires |bi| == i_width
requires |bo| == o_width
requires forall i_index: nat :: i_index < i_width ==> (
reveal ToIMapValid();
var (from_output, index) := nio2i[i_index];
from_output ==> bi[i_index] == bo[index]
)
ensures NIO2I(I2NI(bi), bo) == bi
{
reveal ToNIMapValid();
reveal ToIMapValid();
I2NICorrect(bi);
var bni := I2NI(bi);
assert forall ni_index: nat :: ni_index < ni_width ==>
bni[ni_index] == bi[i2ni[ni_index]];
var bi_new := NIO2I(bni, bo);
forall i_index: nat | i_index < i_width
ensures bi_new[i_index] == bi[i_index]
{
var (from_output, index) := nio2i[i_index];
if !from_output {
assert bi_new[i_index] == bni[index];
assert bi_new[i_index] == bi[i_index];
} else {
assert bi_new[i_index] == bo[index];
assert bi_new[i_index] == bi[i_index];
}
}
}
function GetConnectedInputs(mp: ScufMap): (r: set<NP>)
requires Valid()
requires MPConnectionConsistent(mp, this)
ensures r <= Seq.ToSet(mp.inputs)
{
reveal ToNIMapValid();
reveal ToIMapValid();
reveal ConnectionsValid();
reveal Seq.ToSet();
(set i_index | i_index in connections :: mp.inputs[i_index])
}
function GetConnectedOutputs(mp: ScufMap): (r: set<NP>)
requires Valid()
requires MPConnectionConsistent(mp, this)
ensures r <= Seq.ToSet(mp.outputs)
{
reveal ToNIMapValid();
reveal ToIMapValid();
reveal ConnectionsValid();
reveal Seq.ToSet();
(set o_index | o_index in connections.Values :: mp.outputs[o_index])
}
lemma GetConnectionProperties(c: Circuit, s: Scuf)
requires Valid()
requires c.Valid()
requires s.Valid(c) || s.ValidRelaxInputs(c)
requires ScufConnectionConsistent(c, s, this)
ensures
reveal ScufConnectionConsistent();
var r := GetConnection(s.mp);
&& (forall onp :: onp in r.Values ==> ONPValid(c, onp))
&& (forall inp :: inp in r ==> INPValid(c, inp))
&& MPConnectionConsistent(s.mp, this)
&& (r.Keys == GetConnectedInputs(s.mp))
&& (r.Values == GetConnectedOutputs(s.mp))
&& reveal ONPsValid();
&& !PathExistsBetweenNPSets(c, r.Values, r.Keys)
&& ConnectCircuitRequirements(c, r)
{
reveal ScufConnectionConsistent();
var np_connections := GetConnection(s.mp);
reveal Seq.ToSet();
s.SomewhatValidToRelaxInputs(c);
ScufFInputsAreValid(c, s);
ScufFOutputsAreValid(c, s);
assert ConnectCircuitRequirements(c, np_connections) by {
reveal ConnectCircuitRequirements();
}
reveal ONPsValid();
assert !PathExistsBetweenNPSets(c, np_connections.Values, np_connections.Keys) by {
assert (np_connections.Keys == GetConnectedInputs(s.mp));
assert (np_connections.Values == GetConnectedOutputs(s.mp));
assert !PathExistsBetweenNPSets(c, GetConnectedOutputs(s.mp), GetConnectedInputs(s.mp));
}
}
function GetConnection(mp: ScufMap): (r: map<NP, NP>)
requires Valid()
requires mp.Valid()
requires MPConnectionConsistent(mp, this)
ensures MPConnectionConsistent(mp, this)
ensures r.Keys == GetConnectedInputs(mp)
ensures r.Values == GetConnectedOutputs(mp)
{
reveal ToNIMapValid();
reveal ToIMapValid();
reveal ConnectionsValid();
assert |mp.inputs| == i_width;
assert (forall index: nat :: index in connections.Keys ==> index < i_width);
assert Seq.HasNoDuplicates(mp.inputs);
reveal Seq.HasNoDuplicates();
var np_connections := (map i_index | i_index in connections :: mp.inputs[i_index] := mp.outputs[connections[i_index]]);
assert np_connections.Values == GetConnectedOutputs(mp) by {
forall onp | onp in np_connections.Values
ensures onp in GetConnectedOutputs(mp)
{
var o_index: nat :| o_index < |mp.outputs| && mp.outputs[o_index] == onp;
assert o_index in connections.Values;
}
forall onp | onp in GetConnectedOutputs(mp)
ensures onp in np_connections.Values
{
var o_index: nat :| o_index < |mp.outputs| && mp.outputs[o_index] == onp;
assert o_index in connections.Values;
var i_index :| i_index in connections && connections[i_index] == o_index;
assert onp == mp.outputs[connections[i_index]];
assert np_connections[mp.inputs[i_index]] == onp;
}
}
np_connections
}
ghost predicate MPSIRequirements(mp: ScufMap, si: SI)
{
&& Valid()
&& mp.Valid()
&& MPConnectionConsistent(mp, this)
&& var new_mp := ConnectScufMap(mp, this);
&& SIValid(si, new_mp.inputs, new_mp.state)
}
ghost predicate MPFIRequirements(mp: ScufMap, fi: FI)
{
&& Valid()
&& mp.Valid()
&& MPConnectionConsistent(mp, this)
&& var new_mp := ConnectScufMap(mp, this);
&& FIValid(fi, new_mp.inputs, new_mp.state)
}
ghost predicate MPUFFIRequirements(mp: ScufMap, uf: UpdateFunction, fi: FI)
{
&& MPFIRequirements(mp, fi)
&& uf.Valid()
&& UFConnectionConsistent(uf, this)
&& ScufMapUpdateFunctionConsistent(mp, uf)
}
ghost predicate MPUFSIRequirements(mp: ScufMap, uf: UpdateFunction, si: SI)
{
&& MPSIRequirements(mp, si)
&& uf.Valid()
&& UFConnectionConsistent(uf, this)
&& ScufMapUpdateFunctionConsistent(mp, uf)
}
ghost predicate UFSIRequirements(uf: UpdateFunction, si: SI)
{
&& Valid()
&& (|si.inputs| == ni_width)
&& (|si.state| == uf.state_width)
&& uf.Valid()
&& UFConnectionConsistent(uf, this)
}
function SNI2SIFromOutputs(sni: SI, outputs: seq<bool>): (si_from_outputs: SI)
requires Valid()
requires |sni.inputs| == ni_width
requires |outputs| == o_width
ensures |si_from_outputs.inputs| == i_width
{
SI(NIO2I(sni.inputs, outputs), sni.state)
}
function SIFromOutputs(mp: ScufMap, fi: FI, outputs: seq<bool>): (si_from_outputs: SI)
requires MPFIRequirements(mp, fi)
requires |outputs| == |mp.outputs|
ensures |si_from_outputs.state| == |mp.state|
ensures |si_from_outputs.inputs| == |mp.inputs|
{
var new_mp := ConnectScufMap(mp, this);
var sni := new_mp.fi2si(fi);
SNI2SIFromOutputs(sni, outputs)
}
function SNI2SOFromOutputs(uf: UpdateFunction, sni: SI, outputs: seq<bool>): (so_from_outputs: SO)
requires UFSIRequirements(uf, sni)
requires |outputs| == uf.output_width
ensures |so_from_outputs.state| == uf.state_width
ensures |so_from_outputs.outputs| == uf.output_width
{
var si := SNI2SIFromOutputs(sni, outputs);
reveal UpdateFunction.Valid();
var so := uf.sf(si);
so
}
function SOFromOutputs(
mp: ScufMap, uf: UpdateFunction, fi: FI, outputs: seq<bool>): (so_from_outputs: SO)
requires MPUFFIRequirements(mp, uf, fi)
requires |outputs| == |mp.outputs|
ensures |so_from_outputs.state| == |mp.state|
ensures |so_from_outputs.outputs| == |mp.outputs|
{
var new_mp := ConnectScufMap(mp, this);
var sni := new_mp.fi2si(fi);
var si := SIFromOutputs(mp, fi, outputs);
SNI2SOFromOutputs(uf, sni, outputs)
}
function FIFromOutputs(mp: ScufMap, fi: FI, outputs: seq<bool>): (fi_from_outputs: FI)
requires Valid()
requires mp.Valid()
requires MPConnectionConsistent(mp, this)
requires
var new_mp := ConnectScufMap(mp, this);
FIValid(fi, new_mp.inputs, new_mp.state)
requires |outputs| == |mp.outputs|
ensures fi_from_outputs.state == fi.state
ensures FIValid(fi_from_outputs, mp.inputs, mp.state)
{
var new_mp := ConnectScufMap(mp, this);
var sni := new_mp.fi2si(fi);
var si_pass := SNI2SIFromOutputs(sni, outputs);
var fi_pass := mp.si2fi(si_pass);
assert fi_pass.state == fi.state by {
MapToSeqToMap(mp.state, fi.state);
}
fi_pass
}
function FIFirstPass(mp: ScufMap, fi: FI): (fi_first_pass: FI)
requires MPFIRequirements(mp, fi)
ensures fi_first_pass.state == fi.state
ensures FIValid(fi_first_pass, mp.inputs, mp.state)
{
var output_width := |mp.outputs|;
var fake_output := seq(output_width, (index: nat) requires index < output_width => false);
FIFromOutputs(mp, fi, fake_output)
}
function FOFirstPass(mp: ScufMap, uf: UpdateFunction, fi: FI): (fo_first_pass: FO)
requires MPUFFIRequirements(mp, uf, fi)
ensures fo_first_pass.outputs.Keys == Seq.ToSet(mp.outputs)
ensures fo_first_pass.state.Keys == Seq.ToSet(mp.state)
{
var so_first_pass := SOFirstPass(mp, uf, fi);
mp.so2fo(so_first_pass)
}
lemma FOFirstPassTOFISecondPass(mp: ScufMap, uf: UpdateFunction, fi: FI, inp: NP)
requires MPUFFIRequirements(mp, uf, fi)
requires
var conn_inputs := GetConnectedInputs(mp);
inp in conn_inputs
ensures
var fo_first_pass := FOFirstPass(mp, uf, fi);
var fi_second_pass := FISecondPass(mp, uf, fi);
var npconnections := GetConnection(mp);
var onp := npconnections[inp];
fi_second_pass.inputs[inp] == fo_first_pass.outputs[onp]
{
assert inp in mp.inputs by {
reveal Seq.ToSet();
}
var new_mp := ConnectScufMap(mp, this);
var inp_index := Seq.IndexOf(mp.inputs, inp);
var si := new_mp.fi2si(fi);
var so_first_pass := SOFirstPass(mp, uf, fi);
var fi_second_pass := FISecondPass(mp, uf, fi);
var si_second_pass := SI(NIO2I(si.inputs, so_first_pass.outputs), si.state);
assert mp.inputs[inp_index] == inp;
assert fi_second_pass == mp.si2fi(si_second_pass);
assert fi_second_pass.inputs == SeqsToMap(mp.inputs, si_second_pass.inputs);
assert si_second_pass.inputs[inp_index] == fi_second_pass.inputs[inp] by {
reveal MapMatchesSeqs();
}
assert inp_index < |nio2i| by {
}
var conn_inputs := GetConnectedInputs(mp);
assert inp_index in connections by {
assert inp in conn_inputs;
reveal ToNIMapValid();
reveal ToIMapValid();
reveal ConnectionsValid();
reveal Seq.ToSet();
reveal Seq.HasNoDuplicates();
assert conn_inputs == (set i | i in connections :: mp.inputs[i]);
}
var (from_output, onp_index) := nio2i[inp_index];
assert from_output by {
reveal ToNIMapValid();
reveal ToIMapValid();
reveal ConnectionsValid();
assert ((!from_output) ==> onp_index < ni_width && i2ni[onp_index] == inp_index && (inp_index !in connections));
assert ((from_output) ==> onp_index < o_width && (inp_index in connections) && (onp_index == connections[inp_index]));
}
var npconnections := GetConnection(mp);
var onp := npconnections[inp];
assert onp_index < |mp.outputs| by {
reveal ToNIMapValid();
reveal ToIMapValid();
reveal ConnectionsValid();
}
assert mp.outputs[onp_index] == onp by {
reveal ToNIMapValid();
reveal ToIMapValid();
reveal ConnectionsValid();
reveal Seq.HasNoDuplicates();
}
var fo_first_pass := FOFirstPass(mp, uf, fi);
assert so_first_pass.outputs[onp_index] == fo_first_pass.outputs[onp] by {
reveal MapMatchesSeqs();
}
}
function SNI2SOFirstPass(uf: UpdateFunction, sni: SI): (so_first_pass: SO)
requires UFSIRequirements(uf, sni)
ensures |so_first_pass.outputs| == uf.output_width
{
var fake_output := seq(uf.output_width, (index: nat) requires index < uf.output_width => false);
SNI2SOFromOutputs(uf, sni, fake_output)
}
function SOFirstPass(mp: ScufMap, uf: UpdateFunction, fi: FI): (so_first_pass: SO)
requires MPUFFIRequirements(mp, uf, fi)
ensures |so_first_pass.outputs| == |mp.outputs|
ensures |so_first_pass.outputs| == uf.output_width
{
var output_width := |mp.outputs|;
var fake_output := seq(output_width, (index: nat) requires index < output_width => false);
SOFromOutputs(mp, uf, fi, fake_output)
}
function FISecondPass(mp: ScufMap, uf: UpdateFunction, fi: FI): (fi_second_pass: FI)
requires MPUFFIRequirements(mp, uf, fi)
ensures FIValid(fi_second_pass, mp.inputs, mp.state)
ensures fi.state == fi_second_pass.state
{
var so := SOFirstPass(mp, uf, fi);
FIFromOutputs(mp, fi, so.outputs)
}
function SNI2SOSecondPass(uf: UpdateFunction, si: SI): (so_second_pass: SO)
requires UFSIRequirements(uf, si)
ensures uf.SOVal(so_second_pass)
{
var so := SNI2SOFirstPass(uf, si);
var si_second_pass := SNI2SIFromOutputs(si, so.outputs);
reveal UpdateFunction.Valid();
uf.sf(si_second_pass)
}
function SOSecondPass(mp: ScufMap, uf: UpdateFunction, fi: FI): (so_second_pass: SO)
requires MPUFFIRequirements(mp, uf, fi)
ensures SOValid(so_second_pass, mp.outputs, mp.state)
{
var new_mp := ConnectScufMap(mp, this);
var sni := new_mp.fi2si(fi);
SNI2SOSecondPass(uf, sni)
}
function FOSecondPass(mp: ScufMap, uf: UpdateFunction, fi: FI): (fo_second_pass: FO)
requires MPUFFIRequirements(mp, uf, fi)
ensures FOValid(fo_second_pass, mp.outputs, mp.state)
{
var so_second_pass := SOSecondPass(mp, uf, fi);
mp.so2fo(so_second_pass)
}
lemma FIFromOutputsMatchingKeyMatchingValue(mp: ScufMap, uf: UpdateFunction, fi: FI, outputs: seq<bool>)
requires MPUFFIRequirements(mp, uf, fi)
requires |outputs| == |mp.outputs|
ensures
var fi_pass := FIFromOutputs(mp, fi, outputs);
forall x :: (x in fi.inputs && x in fi_pass.inputs) ==>
(fi.inputs[x] == fi_pass.inputs[x])
{
reveal Seq.ToSet();
var new_mp := ConnectScufMap(mp, this);
var fi_pass := FIFromOutputs(mp, fi, outputs);
forall x: NP | (x in fi.inputs && x in fi_pass.inputs)
ensures fi.inputs[x] == fi_pass.inputs[x]
{
assert FIValid(fi, new_mp.inputs, new_mp.state);
assert FIValid(fi_pass, mp.inputs, mp.state);
assert x in mp.inputs;
assert x in new_mp.inputs;
var output_width := |mp.outputs|;
var sni_pass := new_mp.fi2si(fi);
var si_pass := SI(NIO2I(sni_pass.inputs, outputs), sni_pass.state);
var fi_pass := mp.si2fi(si_pass);
var ni_index := Seq.IndexOf(new_mp.inputs, x);
assert new_mp.inputs[ni_index] == x;
var i_index := Seq.IndexOf(mp.inputs, x);
assert mp.inputs[i_index] == x;
assert new_mp.inputs == I2NI(mp.inputs);
assert |i2ni| == ni_width && i2ni[ni_index] < i_width by {
reveal ToNIMapValid();
}
assert new_mp.inputs[ni_index] == mp.inputs[i2ni[ni_index]] by {
I2NICorrect(mp.inputs);
}
assert i2ni[ni_index] == i_index by {
assert mp.inputs[i2ni[ni_index]] == x;
assert mp.inputs[i_index] == x;
assert Seq.HasNoDuplicates(mp.inputs);
reveal Seq.HasNoDuplicates();
}
var value := fi.inputs[x];
new_mp.fi2siInputs(fi, x);
assert sni_pass.inputs[ni_index] == value;
NIO2I2NI(sni_pass.inputs, outputs);
assert si_pass.inputs[i2ni[ni_index]] == value;
assert si_pass.inputs[i_index] == value;
assert mp.inputs[i_index] == x;
mp.si2fi2si(si_pass);
mp.fi2siInputs(fi_pass, x);
assert fi_pass.inputs[x] == value;
}
}
lemma FIFirstPassMatchingKeyMatchingValue(mp: ScufMap, uf: UpdateFunction, fi: FI)
requires MPUFFIRequirements(mp, uf, fi)
ensures
var fi_pass := FIFirstPass(mp, fi);
forall x :: (x in fi.inputs && x in fi_pass.inputs) ==>
(fi.inputs[x] == fi_pass.inputs[x])
{
var output_width := |mp.outputs|;
var fake_output := seq(output_width, (index: nat) requires index < output_width => false);
FIFromOutputsMatchingKeyMatchingValue(mp, uf, fi, fake_output);
}
lemma FISecondPassMatchingKeyMatchingValue(mp: ScufMap, uf: UpdateFunction, fi: FI)
requires MPUFFIRequirements(mp, uf, fi)
ensures
var fi_second_pass := FISecondPass(mp, uf, fi);
forall x :: (x in fi.inputs && x in fi_second_pass.inputs) ==>
(fi.inputs[x] == fi_second_pass.inputs[x])
{
var so := SOFirstPass(mp, uf, fi);
FIFromOutputsMatchingKeyMatchingValue(mp, uf, fi, so.outputs);
}
}
function GetSubPathInOldInternal(c: Circuit, new_c: Circuit, inps: set<NP>, p: seq<NP>, index: nat): (subp: seq<NP>)
requires c.Valid()
requires new_c.Valid()
requires new_c.NodeKind == c.NodeKind
requires forall np :: np in c.PortSource ==> np in new_c.PortSource && c.PortSource[np] == new_c.PortSource[np]
requires forall np :: np in new_c.PortSource && np !in c.PortSource ==> np in inps
requires index < |p|
requires Seq.Last(p) in inps
requires index > 0 ==> p[index-1] !in inps
requires PathValid(new_c, p)
requires PathValid(c, p[..index])
ensures PathValid(c, subp)
ensures |subp| > 0
ensures Seq.Last(subp) in inps
ensures Seq.First(subp) == Seq.First(p)
decreases |p| - index
{
reveal PathValid();
if p[index] in inps then
p[..index+1]
else
assert PathValid(c, p[..(index+1)]) by {
assert PathValid(c, p[..index]);
assert NPValid(c, p[index]);
assert PathValid(new_c, p);
assert NPValid(new_c, p[index]);
if index > 0 {
assert NPValid(new_c, p[index-1]);
assert NPsConnected(new_c, p[index-1], p[index]);
assert p[index-1] !in inps;
assert NPsConnected(c, p[index-1], p[index]);
}
}
GetSubPathInOldInternal(c, new_c, inps, p, index+1)
}
function GetSubPathInOld(c: Circuit, new_c: Circuit, inps: set<NP>, p: seq<NP>): (subp: seq<NP>)
requires c.Valid()
requires new_c.Valid()
requires new_c.NodeKind == c.NodeKind
requires forall np :: np in c.PortSource ==> np in new_c.PortSource && c.PortSource[np] == new_c.PortSource[np]
requires forall np :: np in new_c.PortSource && np !in c.PortSource ==> np in inps
requires |p| > 0
requires Seq.Last(p) in inps
requires PathValid(new_c, p)
ensures PathValid(c, subp)
ensures |subp| > 0
ensures Seq.Last(subp) in inps
ensures Seq.First(subp) == Seq.First(p)
{
reveal PathValid();
GetSubPathInOldInternal(c, new_c, inps, p, 0)
}
lemma StillNoPathExistsBetweenNPSets(c: Circuit, new_c: Circuit, onps: set<NP>, inps: set<NP>)
requires c.Valid()
requires new_c.Valid()
requires ONPsValid(c, onps)
requires ONPsValid(new_c, onps)
requires new_c.NodeKind == c.NodeKind
requires forall np :: np in c.PortSource ==> np in new_c.PortSource && c.PortSource[np] == new_c.PortSource[np]
requires forall np :: np in new_c.PortSource && np !in c.PortSource ==> np in inps
requires !PathExistsBetweenNPSets(c, onps, inps)
ensures !PathExistsBetweenNPSets(new_c, onps, inps)
{
reveal PathExistsBetweenNPSets();
if PathExistsBetweenNPSets(new_c, onps, inps) {
var p :| (|p| > 0) && PathValid(new_c, p) && (Seq.First(p) in onps) && (Seq.Last(p) in inps);
var psub := GetSubPathInOld(c, new_c, inps, p);
assert PathBetweenNPSets(c, psub, onps, inps);
}
}
opaque ghost predicate ScufConnectionConsistent(c: Circuit, s: Scuf, conn: InternalConnection)
requires c.Valid()
requires s.Valid(c) || s.ValidRelaxInputs(c)
requires conn.Valid()
{
reveal InternalConnection.ConnectionsValid();
reveal ONPsValid();
s.SomewhatValidToRelaxInputs(c);
ScufFOutputsAreValid(c, s);
&& (conn.i_width == s.uf.input_width)
&& (conn.o_width == s.uf.output_width)
&& !PathExistsBetweenNPSets(c, conn.GetConnectedOutputs(s.mp), conn.GetConnectedInputs(s.mp))
&& (forall i_index :: i_index in conn.connections ==> s.mp.inputs[i_index] !in c.PortSource)
}
predicate UFConnectionConsistent(uf: UpdateFunction, conn: InternalConnection)
{
&& (conn.i_width == uf.input_width)
&& (conn.o_width == uf.output_width)
}
predicate MPConnectionConsistent(mp: ScufMap, conn: InternalConnection)
{
&& (conn.i_width == |mp.inputs|)
&& (conn.o_width == |mp.outputs|)
}
lemma ScufConsistentUFMPConsistent(c: Circuit, s: Scuf, conn: InternalConnection)
requires c.Valid()
requires s.Valid(c)
requires conn.Valid()
requires ScufConnectionConsistent(c, s, conn)
ensures UFConnectionConsistent(s.uf, conn)
ensures MPConnectionConsistent(s.mp, conn)
{
reveal ScufConnectionConsistent();
}
opaque ghost predicate InserterConnectionConsistent(z: ScufInserter, conn: InternalConnection)
requires z.Valid()
requires conn.Valid()
{
reveal ScufInserter.Valid();
&& UFConnectionConsistent(z.uf, conn)
&& forall c: Circuit :: c.Valid() ==> (
z.ValidForCircuit(c);
var (new_c, s) := z.fn(c);
assert new_c.Valid() && s.Valid(new_c) by {
reveal SimpleInsertion();
}
ScufConnectionConsistent(new_c, s, conn)
)
}
opaque function ConnectUpdateFunction(uf: UpdateFunction, conn: InternalConnection): (new_uf: UpdateFunction)
requires uf.Valid()
requires conn.Valid()
requires UFConnectionConsistent(uf, conn)
ensures new_uf.Valid()
ensures new_uf.input_width == conn.ni_width
ensures new_uf.output_width == uf.output_width
ensures new_uf.state_width == uf.state_width
{
reveal UpdateFunction.Valid();
UpdateFunction(
conn.ni_width,
uf.output_width,
uf.state_width,
(sni: SI) requires |sni.inputs| == conn.ni_width && |sni.state| == uf.state_width =>
conn.SNI2SOSecondPass(uf, sni)
)
}
lemma SelfConnectSecondPassMFLookup(s: Scuf, conn: InternalConnection, np: NP, fi: FI)
requires s.MapValid()
requires conn.Valid()
requires MPConnectionConsistent(s.mp, conn)
requires UFConnectionConsistent(s.uf, conn)
requires np in s.mp.outputs || np in StateINPs(s.mp.state)
requires
var new_s := ConnectScuf(s, conn);
FIValid(fi, new_s.mp.inputs, new_s.mp.state)
ensures
var new_s := ConnectScuf(s, conn);
var fi_second_pass := conn.FISecondPass(s.mp, s.uf, fi);
assert FIValid(fi_second_pass, s.mp.inputs, s.mp.state);
reveal Seq.ToSet();
MFLookup(s, fi_second_pass, np) == MFLookup(new_s, fi, np)
{
var new_s := ConnectScuf(s, conn);
var sni := new_s.mp.fi2si(fi);
var output_width := |s.mp.outputs|;
var fake_output := seq(output_width, (index: nat) requires index < output_width => false);
var si_first_pass := conn.SNI2SIFromOutputs( sni, fake_output);
reveal UpdateFunction.Valid();
var so_first_pass := s.uf.sf(si_first_pass);
var fo_first_pass := new_s.mp.so2fo(so_first_pass);
assert conn.FOFirstPass(s.mp, s.uf, fi) == fo_first_pass;
var si_second_pass := conn.SNI2SIFromOutputs(sni, so_first_pass.outputs);
var so_second_pass := conn.SNI2SOSecondPass(s.uf, sni);//s.uf.sf(si_second_pass);
assert new_s.uf.sf(sni) == so_second_pass by {
reveal ConnectUpdateFunction();
}
var fo := new_s.mp.so2fo(so_second_pass);
assert conn.FOSecondPass(s.mp, s.uf, fi) == fo;
reveal Seq.ToSet();
if np in s.mp.outputs {
assert MFLookup(new_s, fi, np) == fo.outputs[np];
} else {
assert MFLookup(new_s, fi, np) == fo.state[np.n];
}
s.mp.si2fi2si(si_second_pass);
var fi_second_pass := s.mp.si2fi(si_second_pass);
var fo_second_pass := s.mp.so2fo(so_second_pass);
assert fo_second_pass == conn.FOSecondPass(s.mp, s.uf, fi);
if np in s.mp.outputs {
assert MFLookup(s, fi_second_pass, np) == fo_second_pass.outputs[np];
assert fo.outputs[np] == fo_second_pass.outputs[np];
} else {
assert MFLookup(s, fi_second_pass, np) == fo_second_pass.state[np.n];
assert fo.state[np.n] == fo_second_pass.state[np.n];
}
}
function ConnectScufMap(mp: ScufMap, conn: InternalConnection): (new_mp: ScufMap)
requires mp.Valid()
requires conn.Valid()
requires MPConnectionConsistent(mp, conn)
ensures new_mp.Valid()
ensures
var connection := conn.GetConnection(mp);
&& Seq.ToSet(new_mp.inputs) + connection.Keys == Seq.ToSet(mp.inputs)
&& Seq.ToSet(new_mp.inputs) == Seq.ToSet(mp.inputs) - connection.Keys
{
var new_mp := ScufMap(
conn.I2NI(mp.inputs),
mp.outputs,
mp.state);
conn.I2NIProperties(mp.inputs);
new_mp
}
function ConnectScuf(s: Scuf, conn: InternalConnection): (new_scuf: Scuf)
requires s.MapValid()
requires conn.Valid()
requires UFConnectionConsistent(s.uf, conn)
ensures new_scuf.MapValid()
{
var new_s := Scuf(s.sc, ConnectScufMap(s.mp, conn), ConnectUpdateFunction(s.uf, conn));
assert new_s.MapValid() by {
assert new_s.mp.Valid();
assert new_s.mp.InSc(new_s.sc) by {
reveal NPsInSc();
}
assert new_s.uf.Valid();
assert ScufMapUpdateFunctionConsistent(new_s.mp, new_s.uf);
}
new_s
}
lemma ConnectCircuitConnOutputsConstant(c: Circuit, s: Scuf, conn: InternalConnection)
requires c.Valid()
requires s.ValidRelaxInputs(c) || s.Valid(c)
requires conn.Valid()
requires ScufConnectionConsistent(c, s, conn)
ensures
reveal ScufConnectionConsistent();
var connection := conn.GetConnection(s.mp);
assert ConnectCircuitRequirements(c, connection) by {
conn.GetConnectionProperties(c, s);
}
var new_c := ConnectCircuit(c, connection);
ConnOutputs(c, s.sc) == ConnOutputs(new_c, s.sc)
{
var old_co := (set np: NP | np.n !in s.sc && np in c.PortSource &&
c.PortSource[np].n in s.sc :: c.PortSource[np]);
reveal ScufConnectionConsistent();
var connection := conn.GetConnection(s.mp);
conn.GetConnectionProperties(c, s);
var new_c := ConnectCircuit(c, connection);
var new_co := (set np: NP | np.n !in s.sc && np in new_c.PortSource &&
new_c.PortSource[np].n in s.sc :: new_c.PortSource[np]);
var conn_inputs := conn.GetConnectedInputs(s.mp);
var conn_outputs := conn.GetConnectedOutputs(s.mp);
reveal Scuf.SomewhatValid();
reveal Scuf.SomewhatValidRelaxInputs();
assert AllONPs(c, s.sc) >= Seq.ToSet(s.mp.outputs);
assert Seq.ToSet(s.mp.outputs) >= ConnOutputs(c, s.sc);
reveal ConnOutputs();
reveal Seq.ToSet();
assert forall np :: np in conn_outputs ==> np in AllONPs(c, s.sc);
reveal AllONPs();
reveal AllINPs();
assert forall np :: np in conn_outputs ==> np.n in s.sc;
assert old_co <= new_co;
assert new_co <= old_co by {
forall inp: NP | inp.n !in s.sc && inp in new_c.PortSource && new_c.PortSource[inp].n in s.sc
ensures new_c.PortSource[inp] in old_co
{
assert new_c.PortSource[inp] in new_co;
if inp !in c.PortSource {
assert inp in conn_inputs;
FInputsInSc(c, s);
reveal NPsInSc();
assert false;
}
assert new_c.PortSource[inp] in old_co;
}
}
assert old_co == new_co;
}
lemma ConnectionKeysInUnconnInputs(c: Circuit, s: Scuf, conn: InternalConnection)
requires c.Valid()
requires s.ValidRelaxInputs(c) || s.Valid(c)
requires conn.Valid()
requires ScufConnectionConsistent(c, s, conn)
ensures
reveal ScufConnectionConsistent();
conn.GetConnectedInputs(s.mp) <= UnconnInputs(c, s.sc)
{
reveal UnconnInputs();
reveal ScufConnectionConsistent();
var conn_inputs := conn.GetConnectedInputs(s.mp);
assert conn_inputs <= Seq.ToSet(s.mp.inputs);
s.SomewhatValidToRelaxInputs(c);
FInputsInSc(c, s);
ScufFInputsAreValid(c, s);
reveal Seq.ToSet();
reveal NPsInSc();
}
lemma ConnectionKeysNotInConnInputs(c: Circuit, s: Scuf, conn: InternalConnection)
requires c.Valid()
requires s.ValidRelaxInputs(c) || s.Valid(c)
requires conn.Valid()
requires ScufConnectionConsistent(c, s, conn)
ensures
reveal ScufConnectionConsistent();
var connection := conn.GetConnection(s.mp);
assert ConnectCircuitRequirements(c, connection) by {
conn.GetConnectionProperties(c, s);
}
var new_c := ConnectCircuit(c, connection);
reveal ScufConnectionConsistent();
reveal ScValid();
conn.GetConnectedInputs(s.mp) !! ConnInputs(new_c, s.sc)
{
reveal ConnInputs();
reveal ScufConnectionConsistent();
var connection := conn.GetConnection(s.mp);
assert ConnectCircuitRequirements(c, connection) by {
conn.GetConnectionProperties(c, s);
}
var new_c := ConnectCircuit(c, connection);
var conn_inputs := conn.GetConnectedInputs(s.mp);
var conn_outputs := conn.GetConnectedOutputs(s.mp);
assert conn_inputs <= Seq.ToSet(s.mp.inputs);
assert conn_outputs <= Seq.ToSet(s.mp.outputs);
s.SomewhatValidToRelaxInputs(c);
FInputsInSc(c, s);
FOutputsInSc(c, s);
reveal NPsInSc();
ScufFInputsAreValid(c, s);
reveal ConnInputs();
reveal ScValid();
}
lemma ConnectCircuitAllInputsDecreases(c: Circuit, s: Scuf, conn: InternalConnection)
requires c.Valid()
requires s.Valid(c)
requires conn.Valid()
requires ScufConnectionConsistent(c, s, conn)
ensures
reveal ScufConnectionConsistent();
var connection := conn.GetConnection(s.mp);
assert ConnectCircuitRequirements(c, connection) by {
conn.GetConnectionProperties(c, s);
}
var new_c := ConnectCircuit(c, connection);
reveal ScValid();
&& AllInputs(c, s.sc) == AllInputs(new_c, s.sc) + connection.Keys
&& AllInputs(c, s.sc) - connection.Keys == AllInputs(new_c, s.sc)
{
reveal ScufConnectionConsistent();