emp-toolkit
circuit_file.h
Go to the documentation of this file.
1 #ifndef CIRCUIT_FILE
2 #define CIRCUIT_FILE
3 
4 #define AND_GATE 0
5 #define XOR_GATE 1
6 #define NOT_GATE 2
7 #include "garble_circuit.h"
8 #include "backend.h"
9 #include <stdio.h>
10 #include "block.h"
11 #include "bit.h"
12 class CircuitFile { public:
14  int *gates;
16  int tmp, tmp2;
17  CircuitFile(const char * file) {
18  FILE * f = fopen(file, "r");
19  tmp2=fscanf(f, "%d%d\n", &num_gate, &num_wire);
20  tmp2=fscanf(f, "%d%d%d\n", &n1, &n2, &n3);
21  tmp2=fscanf(f, "\n");
22  char str[10];
23  gates = new int[num_gate*4];
24  wires = new block[num_wire];
25  for(int i = 0; i < num_gate; ++i) {
26  tmp2=fscanf(f, "%d", &tmp);
27  if (tmp == 2) {
28  tmp2=fscanf(f, "%d%d%d%d%s", &tmp, &gates[4*i], &gates[4*i+1], &gates[4*i+2], str);
29  if (str[0] == 'A') gates[4*i+3] = AND_GATE;
30  else if (str[0] == 'X') gates[4*i+3] = XOR_GATE;
31  }
32  else if (tmp == 1) {
33  tmp2=fscanf(f, "%d%d%d%s", &tmp, &gates[4*i], &gates[4*i+2], str);
34  gates[4*i+3] = NOT_GATE;
35  }
36  }
37  fclose(f);
38  }
39 
40  CircuitFile(const CircuitFile& cf) {
41  num_gate = cf.num_gate;
42  num_wire = cf.num_wire;
43  n1 = cf.n1;
44  n2 = cf.n2;
45  n3 = cf.n3;
46  gates = new int[num_gate*4];
47  wires = new block[num_wire];
48  memcpy(gates, cf.gates, num_gate*4*sizeof(int));
49  memcpy(wires, cf.wires, num_wire*sizeof(block));
50  }
52  delete[] gates;
53  delete[] wires;
54  }
55  int table_size() const{
56  return num_gate*4;
57  }
58 
59  void compute(block * out, block * in1, block * in2) {
60  memcpy(wires, in1, n1*sizeof(block));
61  memcpy(wires+n1, in2, n2*sizeof(block));
62  for(int i = 0; i < num_gate; ++i) {
63  if(gates[4*i+3] == AND_GATE) {
64  wires[gates[4*i+2]] = local_gc->gc_and(
65 wires[gates[4*i]], wires[gates[4*i+1]]);
66  }
67  else if (gates[4*i+3] == XOR_GATE) {
68  wires[gates[4*i+2]] = local_gc->gc_xor(
69 wires[gates[4*i]], wires[gates[4*i+1]]);
70  }
71  else
72  wires[gates[4*i+2]] = local_gc->gc_not(wires[gates[4*i]]);
73  }
74  memcpy(out, &wires[num_wire-n3], n3*sizeof(block));
75  }
76 };
77 #endif// CIRCUIT_FILE
int * gates
Definition: circuit_file.h:14
int num_gate
Definition: circuit_file.h:13
int tmp
Definition: circuit_file.h:16
CircuitFile(const CircuitFile &cf)
Definition: circuit_file.h:40
int table_size() const
Definition: circuit_file.h:55
void compute(block *out, block *in1, block *in2)
Definition: circuit_file.h:59
__m128i block
Definition: block.h:8
block gc_xor(const block &a, const block &b)
Definition: garble_circuit.h:24
#define NOT_GATE
Definition: circuit_file.h:6
int num_wire
Definition: circuit_file.h:13
int n3
Definition: circuit_file.h:13
int n2
Definition: circuit_file.h:13
int n1
Definition: circuit_file.h:13
block * wires
Definition: circuit_file.h:15
~CircuitFile()
Definition: circuit_file.h:51
block gc_not(const block &a)
Definition: garble_circuit.h:27
block gc_and(const block &a, const block &b)
Definition: garble_circuit.h:21
#define AND_GATE
Definition: circuit_file.h:4
int tmp2
Definition: circuit_file.h:16
CircuitFile(const char *file)
Definition: circuit_file.h:17
#define XOR_GATE
Definition: circuit_file.h:5
GarbleCircuit * local_gc
Definition: backend.cpp:8
Definition: circuit_file.h:12