-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompactTest.fs
More file actions
225 lines (186 loc) · 4.91 KB
/
CompactTest.fs
File metadata and controls
225 lines (186 loc) · 4.91 KB
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
module CompactTest
open Xunit
open FsUnitTyped
open LanguageServices.Compact
open AST
open Parser
open TypeChecker
let counter =
"""
pragma language_version 0.15;
import CompactStandardLibrary;
enum State { unset, set }
// public state
export ledger round: Counter;
// transition function changing public state
export circuit increment(): [] {
round.increment(1);
}"""
let stateSetter =
"""
import CompactStandardLibrary;
enum State { unset, set }
export ledger authority: Bytes<32>;
export ledger value: Uint<64>;
export ledger state: State;
export ledger round: Counter;
constructor(sk: Bytes<32>, v: Uint<64>) {
authority = publicKey(round, sk);
value = v;
state = State.set;
}
circuit publicKey(round: Field, sk: Bytes<32>): Bytes<32> {
return persistentHash<Vector<3, Bytes<32>>>(
[pad(32, "midnight:examples:lock:pk"),
round as Bytes<32>,
sk]);
}
export circuit get(): Uint<64> {
assert(state == State.set, "Attempted to get uninitialized value");
return value;
}
witness secretKey(): Bytes<32>;
export circuit set(v: Uint<64>): [] {
assert(state == State.unset, "Attempted to set initialized value");
const sk = secretKey();
const pk = publicKey(round, sk);
authority = pk;
value = v;
state = State.set;
}
export circuit clear(): [] {
assert(state == State.set, "Attempted to clear uninitialized value");
const sk = secretKey();
const pk = publicKey(round, sk);
assert(authority == pk, "Attempted to clear without authorization");
state = State.unset;
round.increment(1);
}"""
let extractWithEmptyEnv code =
let env =
{ enums = Map.empty
functions = Map.empty
variables = Map.empty }
code |> SemanticRules.extractSemanticInfo env
[<Fact>]
let ``parse counter`` () =
let topLevel = LanguageServices.Compact.Parser.parse counter
let expected =
[ Pragma([ "language_version" ], Version [ 0; 15 ])
Import [ [ "CompactStandardLibrary" ] ]
Enum(false, [ "State" ], [ [ "unset" ]; [ "set" ] ])
Ledger(
true,
{ paramName = [ "round" ]
paramType = NamedType([ "Counter" ], []) }
)
Circuit(
true,
[ "increment" ],
{ args = []; returnType = Void },
[ CallStatement(Call([ "round"; "increment" ], [], [ Lit(Int 1) ])) ]
) ]
topLevel |> shouldEqual expected
[<Fact>]
let ``parse large example`` () =
let topLevel = LanguageServices.Compact.Parser.parse stateSetter
topLevel.Length |> shouldBeGreaterThan 0
[<Fact>]
let ``typecheck simple arithmetic in constructor`` () =
let src =
"""
constructor() {
const x = 1 + 2;
}
"""
let prog = parse src
check prog
[<Fact>]
let ``typecheck assignment type mismatch`` () =
let src =
"""
constructor() {
const x = 1;
x = true;
}
"""
let prog = parse src
shouldFail<TypeError> (fun () -> check prog)
[<Fact>]
let ``typecheck return type mismatch`` () =
let src =
"""
circuit foo(): int {
return true;
}
"""
let prog = parse src
Assert.Throws<TypeError>(fun () -> check prog) |> ignore
[<Fact>]
let ``extract semantic info`` () =
let mkParam name cType =
{ paramName = [ name ]
paramType = cType }
let bytes32 = NamedType([ "Bytes" ], [ TypeParamInt 32 ])
let roundDotIncrement =
[ "round"; "increment" ],
{ args = [ mkParam "n" compactInt ]
returnType = Void }
let persistentHash =
[ "persistentHash" ],
{ args =
[ { paramName = [ "xs" ]
paramType = NamedType(compactVector, [ TypeParamInt 3; CompactTypeParam bytes32 ]) } ]
returnType = bytes32 }
let pad =
[ "pad" ],
{ args = [ mkParam "n" compactInt; mkParam "s" compactString ]
returnType = bytes32 }
let compactAssert =
[ "assert" ],
{ args = [ mkParam "cond" compactBool; mkParam "msg" compactString ]
returnType = Void }
let envFunctions =
Map.ofList [ roundDotIncrement; persistentHash; pad; compactAssert ]
let env =
{ enums = Map.empty
functions = envFunctions
variables = Map.empty }
stateSetter
|> SemanticRules.extractSemanticInfo env
|> _.Count
|> shouldBeGreaterThan 0
[<Fact>]
let ``validCalc demo 0`` () =
let validCalc =
"""
circuit validCalc(): Uint<64> {
const a = 18;
const b = 1;
return a/b;
}
"""
let obligations = extractWithEmptyEnv validCalc
let firstObligation = obligations["validCalc"][0] |> string
// proof {lemma firstObligation}
// |> inspect
// |> summary
// |> print
//
// let a, b, eighteen = mkIntVar "a", mkIntVar "b", Integer 18
// proof {
// lemma firstObligation
// a = one <&&> (b = eighteen) ==> (b != zero)
// ``⇒`` { weakening }
// b = eighteen ==> (b != zero)
// ``≡`` { ``Leibniz as axiom`` ((!=) zero) b eighteen }
// b = eighteen ==> (eighteen != zero)
// ``≡`` {}
// b = eighteen ==> True
// ``≡`` {}
// True
// }
// |> inspect
// |> summary
// |> print
firstObligation |> shouldEqual "a = 18 ∧ b = 1 ⇒ b ≠ 0"