emp-toolkit
integer.h
Go to the documentation of this file.
1 #ifndef INTEGER_H__
2 #define INTEGER_H__
3 
4 #include "bit.h"
5 #include "number.h"
6 #include "comparable.h"
7 #include "swappable.h"
8 #include <vector>
9 #include <algorithm>
10 #include <math.h>
11 using std::vector;
12 using std::min;
13 
14 class Integer : public Swappable<Integer>, public Comparable<Integer> { public:
15  int length = 0;
16  Bit* bits = nullptr;
17  Integer(Integer&& in) : length(in.length) {
18  bits = in.bits;
19  in.bits = nullptr;
20  }
21  Integer(const Integer& in): length(in.length) {
22  bits = new Bit[length];
23  memcpy(bits, in.bits, sizeof(Bit)*length);
24  }
26  length = rhs.length;
27  std::swap(bits, rhs.bits);
28  return *this;
29  }
30  Integer(int len, const void * b) : length(len) {
31  bits = new Bit[len];
32  memcpy(bits, b, sizeof(Bit)*len);
33  }
35  if (bits!=nullptr) delete[] bits;
36  }
37 
38  Integer(int length, const string& str, int party = PUBLIC);
39  Integer(int length, long long input, int party = PUBLIC);
40  Integer() :length(0),bits(nullptr){ }
41 
42 //Comparable
43  Bit geq(const Integer & rhs) const;
44  Bit equal(const Integer & rhs) const;
45 
46 //Swappable
47  Integer select(const Bit & sel, const Integer & rhs) const;
48  Integer operator^(const Integer& rhs) const;
49 
50  int size() const;
51  template<typename O>
52  O reveal(int party=PUBLIC) const;
53 
54  Integer abs() const;
55  Integer& resize(int length, bool signed_extend = true);
57  Integer leading_zeros() const;
58  Integer hamming_weight() const;
59 
60  Integer operator<<(int shamt)const;
61  Integer operator>>(int shamt)const;
62  Integer operator<<(const Integer& shamt)const;
63  Integer operator>>(const Integer& shamt)const;
64 
65  Integer operator+(const Integer& rhs)const;
66  Integer operator-(const Integer& rhs)const;
67  Integer operator-()const;
68  Integer operator*(const Integer& rhs)const;
69  Integer operator/(const Integer& rhs)const;
70  Integer operator%(const Integer& rhs)const;
71  Integer operator&(const Integer& rhs)const;
72  Integer operator|(const Integer& rhs)const;
73 
74  Bit& operator[](int index);
75  const Bit & operator[](int index) const;
76 
77 //batcher
78  template<typename... Args>
79  static size_t bool_size(size_t size, Args... args) {
80  return size;
81  }
82  static void bool_data(bool* data, size_t len, long long num) {
83  bool_data(data, len, std::to_string(num));
84  }
85  static void bool_data(bool* data, size_t len, string str) {
86  string bin = dec_to_bin(str);
87  std::reverse(bin.begin(), bin.end());
88 // cout << "convert " <<str<<" "<<bin<<endl;
89  int l = (bin.size() > (size_t)len ? len : bin.size());
90  for(int i = 0; i < l; ++i)
91  data[i] = (bin[i] == '1');
92  for (size_t i = l; i < len; ++i)
93  data[i] = data[l-1];
94  }
95 };
96 
97 void init(Bit * bits, const bool* b, int length, int party = PUBLIC);
98 #include "integer.hpp"
99 #endif// INTEGER_H__
Integer(int len, const void *b)
Definition: integer.h:30
Bit equal(const Integer &rhs) const
Definition: integer.hpp:305
string dec_to_bin(const string &dec)
Definition: utils.h:68
Integer operator+(const Integer &rhs) const
Definition: integer.hpp:314
static void bool_data(bool *data, size_t len, string str)
Definition: integer.h:85
Bit geq(const Integer &rhs) const
Definition: integer.hpp:294
void init(Bit *bits, const bool *b, int length, int party=PUBLIC)
Definition: integer.hpp:125
Integer(const Integer &in)
Definition: integer.h:21
Definition: comparable.h:5
Integer operator/(const Integer &rhs) const
Definition: integer.hpp:333
Integer operator*(const Integer &rhs) const
Definition: integer.hpp:327
Integer & operator=(Integer rhs)
Definition: integer.h:25
Integer & resize(int length, bool signed_extend=true)
Definition: integer.hpp:215
Integer operator|(const Integer &rhs) const
Definition: integer.hpp:235
Bit & operator[](int index)
Definition: integer.hpp:162
#define PUBLIC
Definition: utils.h:14
static void bool_data(bool *data, size_t len, long long num)
Definition: integer.h:82
Integer operator>>(int shamt) const
Definition: integer.hpp:264
Integer abs() const
Definition: integer.hpp:208
Integer operator^(const Integer &rhs) const
Definition: integer.hpp:228
~Integer()
Definition: integer.h:34
Integer()
Definition: integer.h:40
int size() const
Definition: integer.hpp:203
Definition: swappable.h:7
void swap(const Bit &swap, T &o1, T &o2)
Definition: swappable.h:18
Definition: integer.h:14
Integer operator &(const Integer &rhs) const
Integer operator<<(int shamt) const
Definition: integer.hpp:249
Integer modExp(Integer p, Integer q)
Definition: integer.hpp:391
Integer leading_zeros() const
Definition: integer.hpp:357
Definition: bit.h:8
Integer hamming_weight() const
Definition: integer.hpp:367
static size_t bool_size(size_t size, Args... args)
Definition: integer.h:79
int length
Definition: integer.h:15
Integer operator%(const Integer &rhs) const
Definition: integer.hpp:342
O reveal(int party=PUBLIC) const
Integer select(const Bit &sel, const Integer &rhs) const
Definition: integer.hpp:155
int party
Definition: input-check-malicious.cpp:12
Bit * bits
Definition: integer.h:16
Integer(Integer &&in)
Definition: integer.h:17
Integer operator-() const
Definition: integer.hpp:352