-
Notifications
You must be signed in to change notification settings - Fork 1
/
inversemod.dfy
98 lines (98 loc) · 1.91 KB
/
inversemod.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
method inversemod(a: nat, n: nat) returns (t: nat, flag: bool)
requires a>0 && n>0;
ensures (!flag ==> t == 1) || (flag ==> (a*t)%n==1)
{
var x := 0;
while x<=a && (1+n*x)%a!=0
invariant 0<=x<=a+1
decreases (a-x) {
x := x + 1;
}
if (x == a+1){
flag := false; t := 1;
}
else{
flag := true; t := (1+n*x)/a;
}
}
function power(b: nat, e: nat): nat
decreases e
{
if e == 0 then 1 else b * power(b,e-1)
}
method expoBySquare(x:nat,n:nat) returns (p:nat)
requires n>=0
ensures p == power(x,n)
decreases *
{
var N,q := n,x;
p := 1;
while N>=0
decreases *
{
p := if N%2 == 0 then p else p*q;
q := q * q;
N := if N%2 == 0 then N/2 else (N-1)/2;
}
}
function fact (n:nat) : nat
requires n >= 0
decreases n
{
if n <= 0 then 1 else n*fact(n-1)
}
method Factorial(n: nat) returns (p: nat)
requires n>=0
ensures p == fact(n)
{
var i : nat;
i, p := 1,1;
while i <= n
invariant i<= n+1
invariant p == fact(i-1)
decreases n + 1 - i
{
p := p * i;
i := i + 1;
}
}
class primeRandomNumber{
var xk: nat;
constructor()
ensures xk == 4;
{
xk := 4;
}
method primePRNG() returns (r: nat)
modifies this
ensures xk == (12*old(xk))%40
ensures 40 < r < 1602
{
xk := (12*xk)%40;
r := xk*xk + xk + 41;
}
}
method RSAencrypt (m: nat) returns (c: nat, flag: bool)
requires m>=0
decreases *
{
var m1: nat; var f2: bool;
var h := new primeRandomNumber();
var p := h.primePRNG();
var q := h.primePRNG();
var n := p * q;
var ph :=(p-1)*(q-1);
var e := h.primePRNG();
var d, f1 := inversemod(e,ph);
if (f1){
c := expoBySquare(m,e);
c := c % n;
m1 := expoBySquare(c,d);
m1 := m1 % n;
f2 := (m==m1);
flag := f2 && f1;
}else{
flag := true;
c := 9999;
}
}