forked from benaich/JavaDataStructures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
259 lines (227 loc) · 7.43 KB
/
Matrix.java
File metadata and controls
259 lines (227 loc) · 7.43 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
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
package ECC;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Matrix {
private final String[][] data;
private final int rows;
private final int columns;
public Matrix(String[][] data) {
this.data = data;
this.rows = data.length;
this.columns = data[0].length;
}
public int countRows() {
return rows;
}
public int countColumns() {
return columns;
}
public String[][] getData() {
return data;
}
public String getData(int r, int c) {
return data[r][c];
}
public static Matrix make(String[][] data) {
return new Matrix(data);
}
public static Matrix make(int[] array) {
int keyLength = 2 * ECC.PAD;
Integer[] data = new Integer[(array.length - keyLength)];
// get the matrix
for (int i = 0; i < data.length; i++) {
data[i] = array[i + keyLength];
}
Matrix M = Helpers.listToMatrix(Arrays.asList(data));
return M;
}
public void scramble(boolean type) {
int n, m, row1, row2, col1, col2, x1, x2, op;
n = data[0].length;
m = data.length;
row1 = ECC.getRandom().nextInt(m);
row2 = Helpers.getNotEqualTo(row1, m);
col1 = ECC.getRandom().nextInt(n);
col2 = Helpers.getNotEqualTo(col1, m);
op = new BigInteger(2, ECC.getRandom()).intValue(); //operations:
if (type) {
x1 = Integer.min(col1, col2); // first index
x2 = Integer.max(col1, col2); // last index
rowTrasformation(row1, x1, x2, op);
rowTrasformation(row2, x1, x2, op);
log("R", op, row1, row2, x1, x2);
} else {
x1 = Integer.min(row1, row2);
x2 = Integer.max(row1, row2);
columnTrasformation(col1, x1, x2, op);
columnTrasformation(col2, x1, x2, op);
log("C", op, col1, col2, x1, x2);
}
}
public void rowTrasformation(int r1, int x1, int x2, int op) {
if (op == 0) {
circularLeftShift(r1, x1, x2);
} else if (op == 1) {
circularRightShift(r1, x1, x2);
} else {
reverseRow(r1, x1, x2);
}
}
public void columnTrasformation(int c1, int x1, int x2, int op) {
if (op == 0) {
circularUpwardShift(c1, x1, x2);
} else if (op == 1) {
circularDownwardShift(c1, x1, x2);
} else {
reverseColumn(c1, x1, x2);
}
}
public void reverseRowTrasformation(int r1, int x1, int x2, int op) {
if (op == 0) {
op = 1;
} else if (op == 1) {
op = 0;
}
rowTrasformation(r1, x1, x2, op);
}
public void reverseColumnTrasformation(int c1, int x1, int x2, int op) {
if (op == 0) {
op = 1;
} else if (op == 1) {
op = 0;
}
columnTrasformation(c1, x1, x2, op);
}
public void circularLeftShift(int row, int c1, int c2) {
String tmp = data[row][c1];
for (int i = c1; i < c2; i++) {
data[row][i] = data[row][i + 1];
}
data[row][c2] = tmp;
}
public void circularRightShift(int row, int c1, int c2) {
String tmp = data[row][c2];
for (int i = c2; i > c1; i--) {
data[row][i] = data[row][i - 1];
}
data[row][c1] = tmp;
}
public void reverseRow(int row, int c1, int c2) {
String tmp;
while (c1 < c2) {
tmp = data[row][c1];
data[row][c1] = data[row][c2];
data[row][c2] = tmp;
c1++;
c2--;
}
}
public void circularUpwardShift(int col, int r1, int r2) {
String tmp = data[r1][col];
for (int i = r1; i < r2; i++) {
data[i][col] = data[i + 1][col];
}
data[r2][col] = tmp;
}
public void circularDownwardShift(int col, int r1, int r2) {
String tmp = data[r2][col];
for (int i = r2; i > r1; i--) {
data[i][col] = data[i - 1][col];
}
data[r1][col] = tmp;
}
public void reverseColumn(int col, int r1, int r2) {
String tmp;
while (r1 < r2) {
tmp = data[r1][col];
data[r1][col] = data[r2][col];
data[r2][col] = tmp;
r1++;
r2--;
}
}
public static String[][] additionCode = new String[][]{
{"01", "10", "11", "00"},
{"10", "11", "00", "01"},
{"11", "00", "01", "10"},
{"00", "01", "10", "11"}
};
public static String[][] subsractionCode = new String[][]{
{"11", "10", "01", "00"},
{"00", "11", "10", "01"},
{"01", "00", "11", "10"},
{"10", "01", "00", "11"}
};
public void performAddition(String[] key) {
performOperation(key, true);
}
public void performSubstraction(String[] key) {
performOperation(key, false);
}
public void performOperation(String[] key, boolean op) {
int r, c;
String[][] operationCode = (op) ? additionCode : subsractionCode;
for (int i = 0; i < countColumns(); i++) {
for (int j = 0; j < countRows(); j++) {
r = Integer.parseInt(data[j][i], 2);
c = Integer.parseInt(key[j], 2);
data[j][i] = operationCode[r][c];
}
}
}
public int[] toArray(String[] key) {
// return array of key + data;
int k, keySize = key.length * 2;
int[] array = new int[countColumns() * countRows() * 2 + keySize];
//append the key first
for (k = 0; k < keySize; k = k + 2) {
String str = key[k / 2];
array[k] = Integer.parseInt(str.charAt(0) + "");
array[k + 1] = Integer.parseInt(str.charAt(1) + "");
}
// then add the data
for (int i = 0; i < countColumns(); i++) {
for (int j = 0; j < countRows(); j++) {
array[k] = Integer.parseInt(data[j][i].charAt(0) + "");
array[k + 1] = Integer.parseInt(data[j][i].charAt(1) + "");
k = k + 2;
}
}
return array;
}
public List<Point> toPoints() {
List<Point> list = new ArrayList<>();
for (int i = 0; i < countColumns(); i++) {
String pointCode = "";
for (int j = 0; j < countRows(); j++) {
pointCode += data[j][i];
}
int x = Integer.parseInt(pointCode.substring(0, pointCode.length() / 2), 2);
int y = Integer.parseInt(pointCode.substring(pointCode.length() / 2), 2);
if (x == 0 && y == 0) {
list.add(Point.getInfinity());
} else {
list.add(new Point(x, y));
}
}
return list;
}
@Override
public String toString() {
StringBuilder sbResult = new StringBuilder();
for (int i = 0; i < countRows(); i++) {
for (int j = 0; j < countColumns(); j++) {
sbResult.append(data[i][j]);
sbResult.append("\t");
}
sbResult.append("\n");
}
return sbResult.toString();
}
public void log(String t, int op, int a, int b, int c, int d) {
System.out.println("subkey : " + t + "/" + op + "/" + a + "/" + b + "/" + c + "/" + d);
Helpers.saveSubKey(t, op, a, b, c, d);
}
}