emp-toolkit
utils.h
Go to the documentation of this file.
1 #ifndef UTILS_H__
2 #define UTILS_H__
3 #include <string>
4 #include "block.h"
5 #include <sstream>
6 #include <bitset>
7 #include <cstddef>//https://gcc.gnu.org/gcc-4.9/porting_to.html
8 #include <gmp.h>
9 #include "config.h"
10 #include "prg.h"
11 #define macro_xstr(a) macro_str(a)
12 #define macro_str(a) #a
13 
14 #define PUBLIC 0
15 #define ALICE 1
16 #define BOB 2
17 using std::string;
18 
19 template<typename T>
20 void inline delete_array_null(T * ptr){
21  if(ptr != nullptr) {
22  delete[] ptr;
23  ptr = nullptr;
24  }
25 }
26 
27 inline void error(const char * s, int line = 0, const char * file = nullptr) {
28  fprintf(stderr, s, "\n");
29  if(file != nullptr) {
30  fprintf(stderr, "at %d, %s\n", line, file);
31  }
32  exit(1);
33 }
34 template<class... Ts>
35 void run_function(void *function, const Ts&... args) {
36  reinterpret_cast<void(*)(Ts...)>(function)(args...);
37 }
38 void parse_party_and_port(char ** arg, int * party, int * port);
39 std::string Party(int p);
40 template <typename T = uint64_t>
41 std::string m128i_to_string(const __m128i var) {
42  std::stringstream sstr;
43  const T* values = (const T*) &var;
44  for (unsigned int i = 0; i < sizeof(__m128i) / sizeof(T); i++) {
45  sstr <<"0x"<<std::hex<< values[i] << " ";
46  }
47  return sstr.str();
48 }
49 double wallClock();
50 uint64_t timeStamp();
51 template<typename T>
52 T bool_to_int(const bool * data, size_t len = 0);
53 block bool_to128(const bool * data);
54 void int64_to_bool(bool * data, uint64_t input, int length);
55 std::string hex_to_binary(std::string hex);
56 
57 inline string change_base(string str, int old_base, int new_base) {
58  mpz_t tmp;
59  mpz_init_set_str (tmp, str.c_str(), old_base);
60  char * b = new char[mpz_sizeinbase(tmp, new_base) + 2];
61  mpz_get_str(b, new_base, tmp);
62  mpz_clear(tmp);
63  string res(b);
64  delete[]b;
65  return res;
66 }
67 
68 inline string dec_to_bin(const string& dec) {
69  string bin = change_base(dec, 10, 2);
70  if(dec[0] != '-')
71  return '0' + bin;
72  bin[0] = '1';
73  bool flip = false;
74  for(int i = bin.size()-1; i>=1; --i) {
75  if(flip)
76  bin[i] = (bin[i] == '1' ? '0': '1');
77  if(bin[i] == '1')
78  flip = true;
79  }
80  return bin;
81 }
82 inline string bin_to_dec(const string& bin2) {
83  if(bin2[0] == '0')
84  return change_base(bin2, 2, 10);
85  string bin = bin2;
86  bin[0] = '0';
87  bool flip = false;
88  for(int i = bin.size()-1; i>=1; --i) {
89  if(flip)
90  bin[i] = (bin[i] == '1' ? '0': '1');
91  if(bin[i] == '1')
92  flip = true;
93  }
94  return "-"+change_base(bin, 2, 10);
95 }
96 
97 #include "utils.hpp"
98 #endif// UTILS_H__
T bool_to_int(const bool *data, size_t len=0)
Definition: utils.hpp:56
std::string Party(int p)
Definition: utils.hpp:36
double wallClock()
Definition: utils.hpp:49
void run_function(void *function, const Ts &... args)
Definition: utils.h:35
std::string m128i_to_string(const __m128i var)
Definition: utils.h:41
void delete_array_null(T *ptr)
Definition: utils.h:20
__m128i block
Definition: block.h:8
string dec_to_bin(const string &dec)
Definition: utils.h:68
void error(const char *s, int line=0, const char *file=nullptr)
Definition: utils.h:27
string bin_to_dec(const string &bin2)
Definition: utils.h:82
void int64_to_bool(bool *data, uint64_t input, int length)
Definition: utils.hpp:80
std::string hex_to_binary(std::string hex)
Definition: utils.hpp:25
uint64_t timeStamp()
Definition: utils.hpp:44
int port
Definition: input-check-malicious.cpp:12
string change_base(string str, int old_base, int new_base)
Definition: utils.h:57
int party
Definition: input-check-malicious.cpp:12
block bool_to128(const bool *data)
Definition: utils.hpp:76
void parse_party_and_port(char **arg, int *party, int *port)
Definition: utils.hpp:31