emp-toolkit
utils.hpp
Go to the documentation of this file.
1 static const char* hex_char_to_bin(char c)
2 {
3  switch(toupper(c))
4  {
5  case '0': return "0000";
6  case '1': return "0001";
7  case '2': return "0010";
8  case '3': return "0011";
9  case '4': return "0100";
10  case '5': return "0101";
11  case '6': return "0110";
12  case '7': return "0111";
13  case '8': return "1000";
14  case '9': return "1001";
15  case 'A': return "1010";
16  case 'B': return "1011";
17  case 'C': return "1100";
18  case 'D': return "1101";
19  case 'E': return "1110";
20  case 'F': return "1111";
21  default: return "0";
22  }
23 }
24 
25 inline std::string hex_to_binary(std::string hex) {
26  std::string bin;
27  for(unsigned i = 0; i != hex.length(); ++i)
28  bin += hex_char_to_bin(hex[i]);
29  return bin;
30 }
31 inline void parse_party_and_port(char ** arg, int * party, int * port) {
32  *party = atoi (arg[1]);
33  *port = atoi (arg[2]);
34 }
35 
36 inline std::string Party(int p) {
37  if (p == ALICE)
38  return "ALICE";
39  else if (p == BOB)
40  return "BOB";
41  else return "PUBLIC";
42 }
43 
44 inline uint64_t timeStamp() {
45  struct timespec t;
46  clock_gettime(CLOCK_REALTIME,&t);
47  return (t.tv_sec*1000*1000+t.tv_nsec/1000);
48 }
49 inline double wallClock() {
50  struct timespec t;
51  clock_gettime(CLOCK_REALTIME,&t);
52  return t.tv_sec+1e-9*t.tv_nsec;
53 }
54 
55 template<typename t>
56 t bool_to_int(const bool * data, size_t len) {
57  if (len != 0) len = (len > sizeof(t)*8 ? sizeof(t)*8 : len);
58  else len = sizeof(t)*8;
59  t res = 0;
60  for(size_t i = 0; i < len-1; ++i) {
61  if(data[i])
62  res |= (1LL<<i);
63  }
64  if(data[len-1]) return -1*res;
65  else return res;
66 }
67 
68 inline uint64_t bool_to64(const bool * data) {
69  uint64_t res = 0;
70  for(int i = 0; i < 64; ++i) {
71  if(data[i])
72  res |= (1LL<<i);
73  }
74  return res;
75 }
76 inline block bool_to128(const bool * data) {
77  return makeBlock(bool_to64(data+64), bool_to64(data));
78 }
79 
80 inline void int64_to_bool(bool * data, uint64_t input, int length) {
81  for (int i = 0; i < length; ++i) {
82  data[i] = (input & 1)==1;
83  input >>= 1;
84  }
85 }
void int64_to_bool(bool *data, uint64_t input, int length)
Definition: utils.hpp:80
block bool_to128(const bool *data)
Definition: utils.hpp:76
__m128i block
Definition: block.h:8
#define BOB
Definition: utils.h:16
#define makeBlock(X, Y)
Definition: block.h:69
std::string Party(int p)
Definition: utils.hpp:36
void parse_party_and_port(char **arg, int *party, int *port)
Definition: utils.hpp:31
std::string hex_to_binary(std::string hex)
Definition: utils.hpp:25
int port
Definition: input-check-malicious.cpp:12
#define ALICE
Definition: utils.h:15
uint64_t bool_to64(const bool *data)
Definition: utils.hpp:68
uint64_t timeStamp()
Definition: utils.hpp:44
t bool_to_int(const bool *data, size_t len)
Definition: utils.hpp:56
int party
Definition: input-check-malicious.cpp:12
double wallClock()
Definition: utils.hpp:49