emp-toolkit
float.hpp
Go to the documentation of this file.
1 inline Float::Float(int value_length, int expnt_length, double input, int party) {
2  double abs = input > 0 ? input:-1*input;
3  double lo = pow(2, value_length-2);
4  double up = pow(2, value_length-1);
5  int p = 0;
6  while(abs < lo) {abs*=2;--p;}
7  while(abs >= up) {abs/=2;++p;}
8  expnt = Integer(expnt_length, p, party);
9  value = Integer(value_length, (long long)(abs*(input > 0 ? 1: -1)), party);
10 }
11 
12 inline Float Float::If(const Bit & select, const Float& d) {
13  Float res(*this);
14  res.value = value.If(select, d.value);
15  res.expnt = expnt.If(select, d.expnt);
16  return res;
17 }
18 
19 template<>
20 inline string Float::reveal<string>(int party) const {
21  double val = value.reveal<long long>(party);
22  double exp = expnt.reveal<long long>(party);
23  return to_string(val*pow(2.0, exp));
24 }
25 
26 template<>
27 inline double Float::reveal<double>(int party) const {
28  double val = value.reveal<long long>(party);
29  double exp = expnt.reveal<long long>(party);
30  return val*pow(2.0, exp);
31 }
32 
33 inline string Float::detail(int party) const {
34  string res = reveal<string>(party);
35  res += (" v: " + value.reveal<string>(party));
36  res += (" p: " + expnt.reveal<string>(party));
37  return res;
38 }
39 
40 inline int Float::size() const {
41  return value.size()+expnt.size();
42 }
43 
44 inline Float Float::abs() const {
45  Float res(*this);
46  res.value = value.abs();
47  return res;
48 }
49 
50 inline void Float::normalize(int value_length, int to_add_to_expnt) {
51  Integer value_before_normalize = value;
52  Integer bits_to_shift(value_before_normalize.size(), 0, PUBLIC);
53  for(int i = bits_to_shift.size()-1; i>0; --i)
54  bits_to_shift[i] = value_before_normalize[i] ^ value_before_normalize[i-1];
55  bits_to_shift[0] = true;
56 
57  Integer shift_amount = bits_to_shift.leading_zeros();
58  value_before_normalize = value_before_normalize << shift_amount;
59  expnt = expnt - shift_amount;
60 
61  value = value_before_normalize;
62  value = value >> (value_before_normalize.size() - value_length);
63  value.resize(value_length);
64  expnt = expnt + Integer(expnt.size(), (value_before_normalize.size() + to_add_to_expnt - value_length), PUBLIC);
65 }
66 
67 inline Float Float::operator+(const Float& rhs) const{
68  Float res(*this);
69  Integer diff = expnt - rhs.expnt;
70  Integer diff_abs = diff.abs();
71  Bit to_swap = diff[diff.size()-1];
72  res.expnt = expnt.If(!to_swap, rhs.expnt);
73 
74  Integer v1 = value, v2 = rhs.value;
75  swap(to_swap, v1, v2);
76 
77  v1.resize(2*value.size(), true);
78  v1 = v1 << diff_abs;
79  v2.resize(2*value.size(), true);
80  res.value = v1 + v2;
81  res.normalize(rhs.value.size(), 0);
82  return res;
83 }
84 
85 inline Float Float::operator-(const Float& rhs) const {
86  return (*this) + (-rhs);
87 }
88 
89 inline Float Float::operator-() const {
90  Float res = *this;
91  res.value = -res.value;
92  return res;
93 }
94 
95 inline Float Float::operator*(const Float& rhs) const {
96  Float res(*this);
97  Integer v1 = value;
98  Integer v2 = rhs.value;
99  v1.resize(value.size()*2);
100  v2.resize(value.size()*2);
101  v1 = v1*v2;
102  Bit extra_move = v1[v1.size()-1] == v1[v1.size()-2];
103  v1 = v1.If(extra_move, v1 << 1);
104 
105  res.value = v1 >> (v1.size() - rhs.value.size());
106  res.value.resize(rhs.value.size());
107  res.expnt = res.expnt + rhs.expnt;
108 
109  res.expnt = res.expnt.If(extra_move, res.expnt - Integer(res.expnt.size(), 1));
110  res.expnt = res.expnt + Integer(res.expnt.size(), (v1.size()-rhs.value.size()), PUBLIC);
111  return res;
112 }
113 
114 inline Integer divide_frac(const Integer& lhs, const Integer& rhs) {
115  Integer i1 = lhs, i2 = rhs;
116  i1.resize(rhs.size()*2+1, false);
117  i1=i1<<rhs.size();
118  Integer res(2*rhs.size(), 0, 0);
119  for(int i = 0; i <= rhs.size(); ++i) {
120  Integer tmp = i1 >> (rhs.size()-i);
121  tmp.resize(i1.size() - rhs.size());
122  Integer diff = tmp - i2;
123  res[rhs.size()-i] = !diff[diff.size()-1];
124  tmp = tmp.If(!diff[diff.size()-1], diff);
125  for(int j = 0; j < tmp.size(); ++j)
126  i1[rhs.size()+j-i] = tmp[j];
127  }
128  return res;
129 }
130 
131 
132 //to optimize
133 inline Float Float::operator/(const Float& rhs) const {
134  Float res(*this);
135  Bit sign = rhs.value[rhs.value.size()-1] != value[value.size()-1];
136  Integer v_tmp = value.abs();
137  Integer rhs_v_tmp = rhs.value.abs();
138  res.value = divide_frac(v_tmp, rhs_v_tmp);
139  res.value = res.value.If(sign, -value);
140  res.expnt = res.expnt - rhs.expnt;
141  res.normalize(rhs.value.size(), -1*rhs.value.size());
142  return res;
143 }
144 
145 inline Float Float::operator|(const Float& rhs) const{
146  Float res(*this);
147  res.expnt = expnt | rhs.expnt;
148  res.value = value | rhs.value;
149  return res;
150 }
151 
152 inline Float Float::operator^(const Float& rhs) const{
153  Float res(*this);
154  res.expnt = expnt ^ rhs.expnt;
155  res.value = value ^ rhs.value;
156  return res;
157 }
158 
159 inline Float Float::operator&(const Float& rhs) const{
160  Float res(*this);
161  res.expnt = expnt & rhs.expnt;
162  res.value = value & rhs.value;
163  return res;
164 }
165 
166 inline Bit Float::greater(const Float& rhs) const {
167  Float tmp = (*this) - rhs;
168  return !tmp.value[tmp.value.size()-1];
169 }
170 inline Bit Float::equal(const Float& rhs) const {
171  return false;
172 }
Float operator &(const Float &rhs) const
Float operator/(const Float &rhs) const
Definition: float.hpp:133
Float abs() const
Definition: float.hpp:44
Float operator+(const Float &rhs) const
Definition: float.hpp:67
Bit equal(const Float &rhs) const
Definition: float.hpp:170
Float operator-() const
Definition: float.hpp:89
Integer & resize(int length, bool signed_extend=true)
Definition: integer.hpp:215
Float If(const Bit &select, const Float &d)
Definition: float.hpp:12
#define PUBLIC
Definition: utils.h:14
Integer value
Definition: float.h:11
Integer abs() const
Definition: integer.hpp:208
Float operator^(const Float &rhs) const
Definition: float.hpp:152
string detail(int party) const
Definition: float.hpp:33
Float(Float &&in)
Definition: float.h:13
int size() const
Definition: float.hpp:40
int size() const
Definition: integer.hpp:203
void swap(const Bit &swap, T &o1, T &o2)
Definition: swappable.h:18
Definition: integer.h:14
Integer divide_frac(const Integer &lhs, const Integer &rhs)
Definition: float.hpp:114
Bit greater(const Float &rhs) const
Definition: float.hpp:166
Integer leading_zeros() const
Definition: integer.hpp:357
void normalize(int value_length, int to_add_to_expnt)
Definition: float.hpp:50
Definition: bit.h:8
Integer expnt
Definition: float.h:12
Float operator|(const Float &rhs) const
Definition: float.hpp:145
Definition: float.h:10
Float operator*(const Float &rhs) const
Definition: float.hpp:95
O reveal(int party=PUBLIC) const
int party
Definition: input-check-malicious.cpp:12
T If(const Bit &sel, const T &rhs) const
Definition: swappable.h:8