-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEval.dfy
294 lines (271 loc) · 8.57 KB
/
Eval.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
module Eval {
import opened Circ
import opened Utils
import opened MapFunction
function BinaryOp(a: bool, b: bool, nk: CNodeKind): bool
requires CNodeKindIsBinary(nk)
{
match nk {
case CAnd =>
a && b
case COr =>
a || b
case CXor =>
Xor(a, b)
}
}
function UnaryOp(a: bool, nk: CNodeKind): bool
requires CNodeKindIsUnary(nk)
{
match nk {
case CIden =>
a
case CInv =>
!a
}
}
ghost predicate EvaluateConstRequirements(c: Circuit, fik: FIKeys)
{
&& c.Valid()
&& FICircuitValid(c, fik)
}
ghost predicate EvaluatePathRequirements(c: Circuit, path: seq<NP>)
{
&& c.Valid()
&& (|path| > 0)
&& PathValid(c, path)
&& Seq.HasNoDuplicates(path)
}
ghost predicate EvaluateINPInnerRequirements(c: Circuit, path: seq<NP>, fik: FIKeys)
{
&& EvaluateConstRequirements(c, fik)
&& EvaluatePathRequirements(c, path)
&& INPValid(c, Seq.Last(path))
}
ghost predicate EvaluateONPUnaryBinaryRequirements(c: Circuit, path: seq<NP>, fik: FIKeys)
{
&& EvaluateConstRequirements(c, fik)
&& EvaluatePathRequirements(c, path)
&& ONPValid(c, path[|path|-1])
&& (Seq.Last(path).n !in fik.state)
&& var nk := c.NodeKind[Seq.Last(path).n];
&& (nk.CXor? || nk.CAnd? || nk.COr? || nk.CInv? || nk.CIden?)
}
ghost predicate EvaluateONPBinaryRequirements(c: Circuit, path: seq<NP>, fik: FIKeys)
{
&& EvaluateConstRequirements(c, fik)
&& EvaluatePathRequirements(c, path)
&& ONPValid(c, Seq.Last(path))
&& (Seq.Last(path).n !in fik.state)
&& var nk := c.NodeKind[Seq.Last(path).n];
&& (nk.CXor? || nk.CAnd? || nk.COr?)
}
ghost predicate EvaluateONPUnaryRequirements(c: Circuit, path: seq<NP>, fik: FIKeys)
{
&& EvaluateConstRequirements(c, fik)
&& EvaluatePathRequirements(c, path)
&& ONPValid(c, Seq.Last(path))
&& (Seq.Last(path).n !in fik.state)
&& var nk := c.NodeKind[Seq.Last(path).n];
&& (nk.CInv? || nk.CIden?)
}
ghost predicate EvaluateONPInnerRequirements(c: Circuit, path: seq<NP>, fik: FIKeys)
{
&& EvaluateConstRequirements(c, fik)
&& EvaluatePathRequirements(c, path)
&& ONPValid(c, Seq.Last(path))
}
function EvaluateINPInner(c: Circuit, path: seq<NP>, fi: FI): EvalResult
requires EvaluateINPInnerRequirements(c, path, FItoKeys(fi))
decreases |NodesNotInPath(c, path)|, 2
{
var head := path[|path|-1];
var tail := path[..|path|-1];
if head in fi.inputs then
EvalOk(fi.inputs[head])
else
if head in c.PortSource then
var onp := c.PortSource[head];
if onp in path then
EvalError({}, {GetLoop(path, onp)})
else
reveal Circuit.Valid();
NodesNotInPathDecreases(c, path, onp);
StillHasNoDuplicates(path, onp);
AppendPathValid(c, path, onp);
EvaluateONPInner(c, path + [onp], fi)
else
EvalError({head}, {})
}
lemma NodesNotInPathDecreases(c: Circuit, p: seq<NP>, np: NP)
requires c.Valid()
requires PathValid(c, p)
requires NPValid(c, np)
requires
reveal PathValid();
|p| > 0 ==> NPsConnected(c, Seq.Last(p), np)
requires Seq.HasNoDuplicates(p)
requires np !in p
ensures
var new_p := p + [np];
PathValid(c, new_p) &&
(|NodesNotInPath(c, new_p)| < |NodesNotInPath(c, p)|)
{
reveal PathValid();
var new_p := p + [np];
var all_np := AllNP(c);
var nodes_in_path := Seq.ToSet(p);
var new_nodes_in_path := Seq.ToSet(new_p);
reveal Seq.ToSet();
assert new_nodes_in_path == nodes_in_path + {np};
}
function GetLoop(path: seq<NP>, np: NP): (loop: seq<NP>)
requires Seq.HasNoDuplicates(path)
requires np in path
{
var index := Seq.IndexOf(path, np);
path[index..] + [np]
}
function EvaluateONPBinary(c: Circuit, path: seq<NP>, fi: FI): EvalResult
requires EvaluateONPBinaryRequirements(c, path, FItoKeys(fi))
decreases |NodesNotInPath(c, path)|, 3
{
var nk := c.NodeKind[path[|path|-1].n];
var head := Seq.Last(path);
assert NodeValid(c, head.n);
var inp_0 := NP(head.n, INPUT_0);
var inp_1 := NP(head.n, INPUT_1);
if inp_0 in path then
var loop := GetLoop(path, inp_0);
EvalError({}, {loop})
else if inp_1 in path then
var loop := GetLoop(path, inp_1);
EvalError({}, {loop})
else
NodesNotInPathDecreases(c, path, inp_0);
NodesNotInPathDecreases(c, path, inp_1);
var new_path_0 := path + [inp_0];
var new_path_1 := path + [inp_1];
assert PathValid(c, new_path_0);
assert PathValid(c, new_path_1);
assert |NodesNotInPath(c, path + [inp_0])| < |NodesNotInPath(c, path)|;
StillHasNoDuplicates(path, inp_0);
StillHasNoDuplicates(path, inp_1);
var iv_0 := EvaluateINPInner(c, path + [inp_0], fi);
var iv_1 := EvaluateINPInner(c, path + [inp_1], fi);
match iv_0
case EvalError(missing_0, loops_0) => (
match iv_1
case EvalError(missing_1, loops_1) =>
EvalError(missing_0 + missing_1, loops_0 + loops_1)
case EvalOk(b1) =>
EvalError(missing_0, loops_0)
)
case EvalOk(b0) => (
match iv_1
case EvalError(missing_1, loops_1) =>
EvalError(missing_1, loops_1)
case EvalOk(b1) =>
EvalOk(BinaryOp(b0, b1, nk))
//if nk.CXor? then
// EvalOk(Xor(b0, b1))
//else if nk.CAnd? then
// EvalOk(b0 && b1)
//else
// assert nk.COr?;
// EvalOk(b0 || b1)
)
}
function EvaluateONPUnary(c: Circuit, path: seq<NP>, fi: FI): EvalResult
requires EvaluateONPUnaryRequirements(c, path, FItoKeys(fi))
requires
var nk := c.NodeKind[Seq.Last(path).n];
nk.CInv? || nk.CIden?
decreases |NodesNotInPath(c, path)|, 3
{
var head := Seq.Last(path);
var inp_0 := NP(head.n, INPUT_0);
if inp_0 in path then
EvalError({}, {GetLoop(path, inp_0)})
else
var new_path := path + [inp_0];
AppendPathValid(c, path, inp_0);
assert PathValid(c, new_path);
NodesNotInPathDecreases(c, path, inp_0);
StillHasNoDuplicates(path, inp_0);
var iv_0 := EvaluateINPInner(c, new_path, fi);
match iv_0
case EvalError(missing_0, loops_0) =>
EvalError(missing_0, loops_0)
case EvalOk(b0) =>
var nk := c.NodeKind[head.n];
EvalOk(UnaryOp(b0, nk))
//match nk
// case CInv => EvalOk(!b0)
// case CIden => EvalOk(b0)
}
opaque predicate FICircuitValid(c: Circuit, fik: FIKeys)
{
// It's possible a np to be in fi.inputs and a Seq sink.
&& (forall np :: np in fik.inputs ==> INPValid(c, np))
&& (forall n :: n in fik.state ==> n in c.NodeKind && c.NodeKind[n].CSeq?)
}
function EvaluateONPInner(c: Circuit, path: seq<NP>, fi: FI): EvalResult
requires EvaluateONPInnerRequirements(c, path, FItoKeys(fi))
decreases |NodesNotInPath(c, path)|, 4
{
var head := path[|path|-1];
var nk := c.NodeKind[head.n];
if head.n in fi.state then
assert nk.CSeq? by {
reveal FICircuitValid();
}
match nk
case CSeq() => EvalOk(fi.state[head.n])
else
match nk
case CXor() => EvaluateONPBinary(c, path, fi)
case CAnd() => EvaluateONPBinary(c, path, fi)
case COr() => EvaluateONPBinary(c, path, fi)
case CInv() => EvaluateONPUnary(c, path, fi)
case CIden() => EvaluateONPUnary(c, path, fi)
case CConst(b) => EvalOk(b)
case CSeq() => EvalError({head}, {})
}
lemma LengthOneNoDuplicates<X>(s: seq<X>)
requires |s| == 1
ensures Seq.HasNoDuplicates(s)
{
reveal Seq.HasNoDuplicates();
}
function EvaluateONP(c: Circuit, np: NP, fi: FI): EvalResult
requires c.Valid()
requires FICircuitValid(c, FItoKeys(fi))
requires ONPValid(c, np)
{
var path := [np];
LengthOneNoDuplicates(path);
reveal PathValid();
EvaluateONPInner(c, path, fi)
}
function EvaluateINP(c: Circuit, np: NP, fi: FI): EvalResult
requires c.Valid()
requires FICircuitValid(c, FItoKeys(fi))
requires INPValid(c, np)
{
var path := [np];
LengthOneNoDuplicates(path);
reveal PathValid();
EvaluateINPInner(c, path, fi)
}
function Evaluate(c: Circuit, np: NP, fi: FI): EvalResult
requires c.Valid()
requires FICircuitValid(c, FItoKeys(fi))
requires ONPValid(c, np) || INPValid(c, np)
{
if ONPValid(c, np) then
EvaluateONP(c, np, fi)
else
EvaluateINP(c, np, fi)
}
}