-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmatInverse.cpp
113 lines (109 loc) · 1.9 KB
/
matInverse.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
int** identi(int s){
int i,j;
int** a = (int**)malloc(s * sizeof(int*));
for(i = 0; i < s; i++)
{
a[i] = (int*)malloc(s * sizeof(int));
for(j = 0; j < s; j++)
{
if(i == j)
a[i][j] = 1;
else
a[i][j] = 0;
}
}
return(a);
}
int** cat(int** A, int** ident, int size)
{
int i, j, k, m;
int** n = (int**)malloc(size * sizeof(int*));
for(i = 0; i < size; i++)
{
n[i] = (int*)malloc(2*size * sizeof(int));
for(j = 0; j < size; j++)
{
n[i][j] = A[i][j];
}
for(j = size; j < 2*size; j++)
{
n[i][j] = ident[i][j-size];
}
}
return(n);
}
int** zeros(int size)
{
int i,j;
int** a = (int**)malloc(size * sizeof(int*));
for(i = 0; i < size; i++)
{
a[i] = (int*)malloc(size * sizeof(int));
for(j = 0; j < size; j++)
{
a[i][j] = 0;
}
}
return(a);
}
int** decryptKey(int** key, int size){
int ex,wy;
int n = size;
int** id = identi(size);
int** X = cat(key, id, n); //conjoin 2 matrices next to each other
// deallocate(id);
int** B;
int i,b,x, ii, j, a;
for(i = 0; i < n; i++)
{
x = X[i][i];
ii = i;
while(x == 0)
{
if(ii == (n-1))
{
B = zeros(n);
return(B);
}
ii = ii + 1;
x = ((X[ii][i] + X[i][i]) % 2);//added -1 to account for n
}
if(ii != i)
{
for(b = 0; b < 2*n; b++)
{
X[i][b] = ((X[i][b] + X[ii][b]) % 2);
}
}
for(j = 0; j < n; j++)
{
if(j != i)
{
x = X[j][i];
for(b = 0; b < 2*n; b++)
{
X[j][b] = ((X[j][b] - x * X[i][b]) % 2);
}
}
}
}
B = (int**)malloc(size * sizeof(int*));
for(b = 0; b < n; b++)
{
B[b] = (int*)malloc(size * sizeof(int));
for(a = n; a < 2*n; a++)
{
B[b][a-n] = X[b][a];
}
}
for(a = 0; a < size; a++)
{
free(X[a]);
}
free(X);
return(B);
}