-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerator.java
More file actions
293 lines (208 loc) · 8.45 KB
/
Generator.java
File metadata and controls
293 lines (208 loc) · 8.45 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package com.applied_crypto;
import com.applied_crypto.Util;
import org.apache.commons.codec.binary.Hex;
import java.util.*;
import org.json.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
public class Generator {
private BufferedReader br;
private String current_line = "";
private String whole_file = "";
private JSONObject circuit;
private JSONObject gates;
private JSONObject wires;
private ArrayList<Gate> gates_list = new ArrayList<Gate>();
private HashMap input_hm = new HashMap();
private JSONObject inputs;
public String fileName = "";
private HashMap wires_0_1= new HashMap();
private Set <String> inp_out = new HashSet<String>();
private HashMap wireKeys = new HashMap();
public Generator(String FILENAME) throws Exception {
br = new BufferedReader( new FileReader(FILENAME));
fileName = FILENAME;
System.out.println("reading circuit...");
while ( (current_line = br.readLine()) != null) {
whole_file = whole_file + current_line;
}
circuit = new JSONObject(whole_file);
}
public void parse_circuit() throws Exception {
circuit = new JSONObject(whole_file);
gates = (JSONObject) circuit.get("gates");
for (Iterator iterator = gates.keys(); iterator.hasNext();) {
String key = (String) iterator.next();
JSONObject gate = (JSONObject) gates.get(key);
ArrayList<String> inputs = new ArrayList<String>();
ArrayList<String> outputs = new ArrayList<String>();
ArrayList<Integer> table = new ArrayList<Integer>();
JSONArray jsonArray = (JSONArray) gate.get("inp");
if (jsonArray.length() != 2) {
throw new Exception("Inputs of Gate: " + key + " are wrong." );
}
for (int i = 0; i < jsonArray.length(); i++) {
inputs.add(jsonArray.get(i).toString());
//adding input to union set
inp_out.add(jsonArray.get(i).toString());
}
jsonArray = (JSONArray) gate.get("out");
if (jsonArray.length() != 1) {
throw new Exception("Output of Gate: " + key + " are wrong." );
}
for (int i = 0; i < jsonArray.length(); i++) {
outputs.add(jsonArray.get(i).toString());
//adding input to union set
inp_out.add(jsonArray.get(i).toString());
}
String k = gate.get("type").toString();
String catchType = k;
jsonArray = (JSONArray) gate.get("table");
if (jsonArray.length() != 4) {
throw new Exception("Table length of Gate: " + key + " is wrong." );
}
for (int i = 0; i < jsonArray.length(); i++) {
table.add((int)jsonArray.get(i));
}
Gate sg = new Gate(inputs.get(0), inputs.get(1), outputs.get(0));
sg.fill_table(table);
sg.set_Type(catchType);
gates_list.add(sg);
}
inputs = (JSONObject) circuit.get("inputs");
for (Iterator iterator_2 = inputs.keys(); iterator_2.hasNext();) {
String key = (String) iterator_2.next();
Integer value = (Integer) inputs.get(key);
input_hm.put(key, value);
}
// System.out.println(inp_out);
wires = new JSONObject();
for (String s: inp_out){
ArrayList<byte[]> temp_key_holder = new ArrayList<byte[]>(2);
JSONObject k = new JSONObject();
byte[] key0 = Util.GetRandom();
byte[] key1 = Util.GetRandom();
k.put("0", Hex.encodeHexString(key0) );
k.put("1", Hex.encodeHexString(key1) );
//wires is a global JSONObject
wires.put(s, k);
}
for (int i = 0; i<gates_list.size(); i++){
String key = "g" + i;
Gate temp_gate = gates_list.get(i);
String[] inps = temp_gate.get_inputs();
String[] out = temp_gate.get_outputs();
ArrayList<byte[]> wire1_keys = new ArrayList<byte[]>();
ArrayList<byte[]> wire2_keys = new ArrayList<byte[]>();
ArrayList<byte[]> outWire_keys = new ArrayList<byte[]>();
//creating temp for easier access to stuff in wires just below
JSONObject temp = (JSONObject) wires.get(inps[0]);
wire1_keys.add( Hex.decodeHex( temp.get("0").toString().toCharArray() ) );
wire1_keys.add( Hex.decodeHex( temp.get("1").toString().toCharArray() ) );
temp = (JSONObject) wires.get(inps[1]);
wire2_keys.add( Hex.decodeHex( temp.get("0").toString().toCharArray() ) );
wire2_keys.add( Hex.decodeHex( temp.get("1").toString().toCharArray() ) );
temp = (JSONObject) wires.get(out[0]);
outWire_keys.add( Hex.decodeHex( temp.get("0").toString().toCharArray() ) );
outWire_keys.add( Hex.decodeHex( temp.get("1").toString().toCharArray() ) );
ArrayList<Integer> temp_table = temp_gate.get_table();
ArrayList<String> encodedTable = new ArrayList<String>();
// ALL THE COMBINATIONS OF ENCODING OVER KEY "w1", "w2" and "w3"
if (temp_table.get(0)==0){
byte[] midEnc = Util.specialEncryption( wire1_keys.get(0), outWire_keys.get(0));
byte[] t = Util.specialEncryption( wire2_keys.get(0), midEnc );
encodedTable.add(Hex.encodeHexString(t));
}
else {
byte[] midEnc = Util.specialEncryption( wire1_keys.get(0), outWire_keys.get(1));
byte[] t = Util.specialEncryption( wire2_keys.get(0), midEnc );
encodedTable.add(Hex.encodeHexString(t));
}
if (temp_table.get(1)==0){
byte[] midEnc = Util.specialEncryption( wire1_keys.get(0), outWire_keys.get(0));
byte[] t = Util.specialEncryption( wire2_keys.get(1), midEnc );
encodedTable.add(Hex.encodeHexString(t));
}
else {
byte[] midEnc = Util.specialEncryption( wire1_keys.get(0), outWire_keys.get(1));
byte[] t = Util.specialEncryption( wire2_keys.get(1), midEnc );
encodedTable.add(Hex.encodeHexString(t));
}
if (temp_table.get(2)==0){
byte[] midEnc = Util.specialEncryption( wire1_keys.get(1), outWire_keys.get(0));
byte[] t = Util.specialEncryption( wire2_keys.get(0), midEnc );
encodedTable.add(Hex.encodeHexString(t));
}
else {
byte[] midEnc = Util.specialEncryption( wire1_keys.get(1), outWire_keys.get(1));
byte[] t = Util.specialEncryption( wire2_keys.get(0), midEnc );
encodedTable.add(Hex.encodeHexString(t));
}
if (temp_table.get(3)==0){
byte[] midEnc = Util.specialEncryption( wire1_keys.get(1), outWire_keys.get(0));
byte[] t = Util.specialEncryption( wire2_keys.get(1), midEnc );
encodedTable.add(Hex.encodeHexString(t));
}
else {
byte[] midEnc = Util.specialEncryption( wire1_keys.get(1), outWire_keys.get(1));
byte[] t = Util.specialEncryption( wire2_keys.get(1), midEnc );
encodedTable.add(Hex.encodeHexString(t));
}
JSONObject final_table = new JSONObject();
final_table.put("table", encodedTable);
gates_list.get(i).set_table(encodedTable);
}
//big JSONObject that will finally be written to file
JSONObject garbledCircuit = new JSONObject();
//allGates is the JSONObject that holds all the gates as
//the name suggests
JSONObject allGates = new JSONObject();
//looping over all the gates in the storage
for (int i =0; i<gates_list.size(); i++){
String gateName = "g" + (i+1);
//creating a JSONObject in which all the specific
//gate information is added
JSONObject master = new JSONObject();
String[] inputs = gates_list.get(i).get_inputs();
String[] outputs = gates_list.get(i).get_outputs();
String retType = gates_list.get(i).get_Type();
ArrayList<String> hexTable = gates_list.get(i).get_hex_table();
master.put("inp", inputs);
master.put("out", outputs);
master.put("table", hexTable);
master.put("type", retType);
allGates.put(gateName, master);
}
//writing the key "gates" and all it's inputs
garbledCircuit.put("gates", allGates);
JSONObject garbleInputs = new JSONObject();
Set<String> input_set = new HashSet<String>();
input_set = input_hm.keySet();
for (String key: input_set){
Integer a = (Integer) input_hm.get(key);
String b = Integer.toString(a);
// System.out.println(input_hm.get(key).getClass().getName());
JSONObject temp = (JSONObject) wires.get(key);
String garbleVal = temp.get(b).toString();
garbleInputs.put(key, garbleVal);
}
//writing the key "inputs" and it's values
garbledCircuit.put("inputs", garbleInputs);
//writing the key "wires" and it's values
garbledCircuit.put("wires", wires);
//writing the final garbledCircuit to file
String segs[] = fileName.split("/");
String finalFileName = "gar_"+ segs[segs.length - 1];
try (FileWriter file = new FileWriter(finalFileName)) {
file.write(garbledCircuit.toString(4));
}
}
public static void main(String[] args) throws Exception {
String FILENAME = args[0];
Generator generator = new Generator(FILENAME);
generator.parse_circuit();
}
}