3 const Bit * carryIn,
int size) {
4 Bit carry, bxc, axc, t;
8 if(carryIn && carryOut)
17 skipLast = (carryOut ==
nullptr);
18 while(size-->skipLast) {
21 dest[i] = op1[i] ^ bxc;
26 if(carryOut !=
nullptr)
29 dest[i] = carry ^ op2[i] ^ op1[i];
32 const Bit * borrowIn,
int size) {
34 int skipLast;
int i = 0;
36 if(borrowIn && borrowOut)
37 *borrowOut = *borrowIn;
45 skipLast = (borrowOut ==
nullptr);
46 while(size-- > skipLast) {
47 bxa = op1[i] ^ op2[i];
48 bxc = borrow ^ op2[i];
49 dest[i] = bxa ^ borrow;
54 if(borrowOut !=
nullptr) {
58 dest[i] = op1[i] ^ op2[i] ^ borrow;
63 Bit * temp =
new Bit[size];
64 for(
int i = 0; i < size; ++i)sum[i]=
false;
65 for(
int i=0;i<size;++i) {
66 for (
int k = 0; k < size-i; ++k)
67 temp[k] = op1[k] & op2[i];
68 add_full(sum+i,
nullptr, sum+i, temp,
nullptr, size-i);
70 memcpy(dest, sum,
sizeof(
Bit)*size);
80 x = tsrc[i] ^ fsrc[i];
82 dest[i] = a ^ fsrc[i];
89 for(i=0; i < size-1; ++i) {
90 dest[i] = src[i] ^ cond;
95 dest[i] = cond ^ c ^ src[i];
100 Bit * overflow =
new Bit[size];
101 Bit * temp =
new Bit[size];
102 Bit * rem =
new Bit[size];
103 Bit * quot =
new Bit[size];
105 memcpy(rem, op1, size*
sizeof(
Bit));
107 for(
int i = 1; i < size;++i)
108 overflow[i] = overflow[i-1] | op2[size-i];
110 for(
int i = size-1; i >= 0; --i) {
111 sub_full(temp, &b, rem+i, op2,
nullptr, size-i);
116 if(vrem !=
nullptr) memcpy(vrem, rem, size*
sizeof(
Bit));
117 if(vquot !=
nullptr) memcpy(vquot, quot, size*
sizeof(
Bit));
130 for(
int i = 0; i < length; ++i)
131 bbits[i] = b[i] ? one : zero;
144 bool* b =
new bool[len];
152 :
Integer(len,
std::to_string(input), party) {
157 for(
int i = 0; i <
size(); ++i)
171 inline string Integer::reveal<string>(
int party)
const {
172 bool * b =
new bool[
length];
175 for(
int i =
length-1; i >= 0; --i)
176 bin += (b[i]?
'1':
'0');
182 inline int Integer::reveal<int>(
int party)
const {
183 string s = reveal<string>(
party);
187 inline uint32_t Integer::reveal<uint32_t>(
int party)
const {
196 inline long long Integer::reveal<long long>(
int party)
const {
197 string s = reveal<string>(
party);
210 for(
int i = 0; i <
size(); ++i)
212 return ( (*
this) + res) ^ res;
219 Bit extended_bit = old[
length-1] & signed_extend;
220 for(
int i = min(len,
size()); i < len; ++i)
221 bits[i] = extended_bit;
230 for(
int i = 0; i <
size(); ++i)
237 for(
int i = 0; i <
size(); ++i)
244 for(
int i = 0; i <
size(); ++i)
245 res.bits[i] = res.bits[i] & rhs.
bits[i];
252 for(
int i = 0; i <
size(); ++i)
256 for(
int i =
size()-1; i >= shamt; --i)
258 for(
int i = shamt-1; i>=0; --i)
267 for(
int i = 0; i <
size(); ++i)
271 for(
int i = shamt; i <
size(); ++i)
273 for(
int i = size()-shamt; i <
size(); ++i)
281 for(
int i = 0; i < min(
int(ceil(log2(
size()))) , shamt.
size()-1); ++i)
282 res = res.
select(shamt[i], res<<(1<<i));
288 for(
int i = 0; i <min(
int(ceil(log2(
size()))) , shamt.
size()-1); ++i)
289 res = res.
select(shamt[i], res>>(1<<i));
302 return !tmp[tmp.
size()-1];
307 for(
int i = 0; i <
size(); ++i)
308 res = res & (
bits[i] == rhs[i]);
359 for(
int i =
size() - 2; i>=0; --i)
360 res[i] = res[i+1] | res[i];
362 for(
int i = 0; i < res.
size(); ++i)
369 for(
int i = 0; i <
size(); i++) {
375 while(vec.size() > 1) {
377 for(
size_t i = 0; i < vec.size()-1; i+=2) {
378 vec[j++] = vec[i]+vec[i+1];
380 if(vec.size()%2 == 1) {
381 vec[j++] = vec[vec.size()-1];
383 for(
int i = 0; i < j; ++i)
385 int vec_size = vec.
size();
386 for(
int i = j; i < vec_size; ++i)
395 for(
int i = 0; i < p.
size(); ++i) {
396 Integer tmp = (res * base) % q;
398 base = (base*base) % q;
Bit equal(const Integer &rhs) const
Definition: integer.hpp:305
__m128i block
Definition: block.h:8
void div_full(Bit *vquot, Bit *vrem, const Bit *op1, const Bit *op2, int size)
Definition: integer.hpp:98
Integer operator+(const Integer &rhs) const
Definition: integer.hpp:314
Bit geq(const Integer &rhs) const
Definition: integer.hpp:294
block public_label(bool b)
Definition: garble_circuit.h:18
void sub_full(Bit *dest, Bit *borrowOut, const Bit *op1, const Bit *op2, const Bit *borrowIn, int size)
Definition: integer.hpp:31
Integer operator/(const Integer &rhs) const
Definition: integer.hpp:333
Integer operator*(const Integer &rhs) const
Definition: integer.hpp:327
void Reveal(bool *out, int party, const block *lbls, int nel)
Definition: backend.h:15
Integer & resize(int length, bool signed_extend=true)
Definition: integer.hpp:215
string bin_to_dec(const string &bin2)
Definition: utils.h:82
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
void Feed(block *lbls, int party, const bool *b, int nel)
Definition: backend.h:12
Integer()
Definition: integer.h:40
int size() const
Definition: integer.hpp:203
void ifThenElse(Bit *dest, const Bit *tsrc, const Bit *fsrc, int size, Bit cond)
Definition: integer.hpp:75
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
Integer hamming_weight() const
Definition: integer.hpp:367
Backend * local_backend
Definition: backend.cpp:7
int length
Definition: integer.h:15
Integer operator%(const Integer &rhs) const
Definition: integer.hpp:342
O reveal(int party=PUBLIC) const
void add_full(Bit *dest, Bit *carryOut, const Bit *op1, const Bit *op2, const Bit *carryIn, int size)
Definition: integer.hpp:2
Integer select(const Bit &sel, const Integer &rhs) const
Definition: integer.hpp:155
void condNeg(Bit cond, Bit *dest, const Bit *src, int size)
Definition: integer.hpp:86
Bit * bits
Definition: integer.h:16
GarbleCircuit * local_gc
Definition: backend.cpp:8
void mul_full(Bit *dest, const Bit *op1, const Bit *op2, int size)
Definition: integer.hpp:60
Integer operator-() const
Definition: integer.hpp:352
void init(Bit *bits, const bool *b, int length, int party)
Definition: integer.hpp:125