-
Notifications
You must be signed in to change notification settings - Fork 1
/
089-encrypt.dfy
44 lines (42 loc) · 990 Bytes
/
089-encrypt.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
function rot_sym(c: char): char
// pre-conditions-start
requires 'a' <= c <= 'z'
// pre-conditions-end
// post-conditions-start
ensures 'a' <= rot_sym(c) <= 'z'
// post-conditions-end
{
// impl-start
var alph := c as int - 'a' as int;
((alph + 2 * 2) % 26 + 'a' as int) as char
// impl-end
}
method encrypt(s: string) returns (r: string)
// pre-conditions-start
requires forall i :: 0 <= i < |s| ==> 'a' <= s[i] <= 'z'
// pre-conditions-end
// post-conditions-start
ensures |r| == |s|
ensures forall i :: 0 <= i < |s| ==> r[i] == rot_sym(s[i])
// post-conditions-end
{
// impl-start
r := "";
var i := 0;
while i < |s|
// invariants-start
invariant 0 <= i <= |s|
invariant |r| == i
invariant forall j :: 0 <= j < i ==> r[j] == rot_sym(s[j])
// invariants-end
{
r := r + [rot_sym(s[i])];
i := i + 1;
}
// impl-end
}
method Main() {
var s := "asdfghjkl";
var r := encrypt(s);
assert r == "ewhjklnop";
}