emp-toolkit
xor_tree.h
Go to the documentation of this file.
1 #ifndef XORTREE_H__
2 #define XORTREE_H__
3 #include "bit.h"
8 //TODO: hardcoded with ssp=40, should at least support a range a ssp's
9 template<int N=232, int M=232>
10 class XorTree{public:
11  int n, ssp;
12  bool matrix[N][M];
13  block blockM[N][M];
14  XorTree(int n, int ssp = 40) {
15  this->n=n;
16  this->ssp = ssp;
17  uint8_t bM[N][M];
18  PRG prg(fix_key);
19  prg.random_data(bM, N*M);
20  for(int i = 0; i < N; ++i)
21  for(int j = 0; j < M; ++j) {
22  matrix[i][j] = (bM[i][j]%2 == 1);
23  blockM[i][j] = matrix[i][j] ? one_block(): zero_block();
24  }
25  }
26  void circuitN(block* out, block * in, int dim = N) {
27  assert(dim <= N);
28 
29  for(int i = 0; i < dim; ++i) {
30  block res = zero_block();
31  for(int j = 0; j < M; ++j) {
32  res = xorBlocks(res, andBlocks(in[j], blockM[i][j]));
33  }
34  out[i] = xorBlocks(res, in[i+M]);
35  }
36  }
37 
38  void circuit(block * out, block * in) {
39  int i ;
40  for(i = 0; i < n/N; ++i) {
41  circuitN(out+(i*N), in+(N+M)*i, N);
42  }
43  if(n%N != 0) {
44  circuitN(out+(i*N) , in+(N+M)*i, n%N);
45  }
46  }
47 
48  void genN(bool* out, bool * in, PRG * prg, int dim = N) {
49  assert(dim <= N);
50  uint8_t tmp[M];
51  prg->random_data(tmp, M);
52  for(int i = 0; i < M; ++i)
53  out[i] = (tmp[i]%2==1);
54 
55  for(int i = 0; i < dim; ++i) {
56  bool res = false;
57  for(int j = 0; j < M; ++j) {
58  if( matrix[i][j] == 1)
59  res = res ^ (out[j]);
60  }
61  out[M+i] = res ^ in[i];
62  }
63  }
64  int output_size() {
65  if(n%N == 0)
66  return (M+N)*n/N;
67  else return (M+N)*(n/N+1);
68  }
69  int input_size() {
70  return n;
71  }
72 
73  void gen(bool * out, bool * in) {
74  PRG prg;int i;
75  for(i = 0; i < n/N; ++i) {
76  genN(out+(i*(N+M)), in+N*i, &prg, N);
77  }
78  if(n%N != 0) {
79  genN(out+(i*(N+M)) , in+N*i, &prg, n%N);
80  }
81  }
82 };
84 #endif
85 
block blockM[N][M]
Definition: xor_tree.h:13
void genN(bool *out, bool *in, PRG *prg, int dim=N)
Definition: xor_tree.h:48
__m128i block
Definition: block.h:8
int input_size()
Definition: xor_tree.h:69
bool matrix[N][M]
Definition: xor_tree.h:12
block xorBlocks(block x, block y)
Definition: block.h:35
XorTree(int n, int ssp=40)
Definition: xor_tree.h:14
void circuit(block *out, block *in)
Definition: xor_tree.h:38
#define zero_block()
Definition: block.h:66
void gen(bool *out, bool *in)
Definition: xor_tree.h:73
void circuitN(block *out, block *in, int dim=N)
Definition: xor_tree.h:26
Definition: xor_tree.h:10
const char fix_key[]
Definition: block.h:130
Definition: prg.h:16
void random_data(void *data, int nbytes)
Definition: prg.h:49
int ssp
Definition: xor_tree.h:11
int n
Definition: xor_tree.h:11
#define one_block()
Definition: block.h:67
int output_size()
Definition: xor_tree.h:64
block andBlocks(block x, block y)
Definition: block.h:36