-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathSource.cpp
More file actions
233 lines (196 loc) · 6.31 KB
/
Copy pathSource.cpp
File metadata and controls
233 lines (196 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <stdio.h>
#include <random>
#include <array>
#include <stdint.h>
#define Assert(x) if (!(x)) ((int*)nullptr)[0] = 0;
// how many bytes there are in the secret key. Increase for more security
const size_t c_keyBytes = 1;
//=================================================================================
void WaitForEnter ()
{
printf("Press Enter to quit");
fflush(stdin);
getchar();
}
//=================================================================================
// Replace with something crypto secure if desired
uint8_t Random_uint8_t ()
{
static std::random_device rd;
static std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, 255);
return dis(gen);
}
int RandomInt (int min, int max)
{
static std::random_device rd;
static std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(min, max);
return dis(gen);
}
//=================================================================================
template <size_t KEYBYTES>
class CFHEEncryptedBit
{
public:
// constructor for creating an unencrypted value
CFHEEncryptedBit (bool value)
{
uint8_t = value ? 0xFF : 0;
for (uint8_t &v : m_value)
v = value;
}
// logical operators
void XOR (const CFHEEncryptedBit<KEYBYTES>& rhs)
{
for (size_t i = 0; i < KEYBYTES; ++i)
m_value[i] = m_value[i] ^ rhs.m_value[i];
}
void AND (const CFHEEncryptedBit<KEYBYTES>& rhs)
{
for (size_t i = 0; i < KEYBYTES; ++i)
m_value[i] = m_value[i] & rhs.m_value[i];
}
void NOT ()
{
for (uint8_t &v : m_value)
v = ~v;
}
private:
template <size_t KEYBYTES>
friend class CFHEPrivateKey;
CFHEEncryptedBit (const std::array<uint8_t, KEYBYTES>& key, bool value)
{
// generate the starting vector
for (uint8_t &v: m_value)
v = Random_uint8_t();
// if the vector comes up with the wrong answer, flip a bit that corresponds
// to a 1 in the key, to flip our result
if (Decrypt(key) != value)
{
size_t byteIndex;
uint8_t bitMask;
RandomKeyBitTrue(key, byteIndex, bitMask);
m_value[byteIndex] = m_value[byteIndex] ^ bitMask;
}
// make sure we encrypted it correctly
Assert(Decrypt(key) == value);
}
bool Decrypt (const std::array<uint8_t, KEYBYTES>& key) const
{
// for each bit where the key and the random bits match, flip the return value
bool ret = false;
for (size_t i = 0; i < KEYBYTES; ++i)
{
uint8_t result = m_value[i] & key[i];
while (result)
{
if (result & 1)
ret = !ret;
result = result >> 1;
}
}
return ret;
}
static void RandomKeyBitTrue (const std::array<uint8_t, KEYBYTES>& key, size_t& byteIndex, uint8_t& bitMask)
{
// get a byte index that has bits set in it
byteIndex = RandomInt(0, KEYBYTES - 1);
while (key[byteIndex] == 0)
byteIndex = (byteIndex + 1) % KEYBYTES;
// get a bit index that is set
size_t bitIndex = RandomInt(0, 7);
bitMask = 1 << bitIndex;
while ((key[byteIndex] & bitMask) == 0)
{
bitIndex = (bitIndex + 1) & 7;
bitMask = 1 << bitIndex;
}
}
std::array<uint8_t, KEYBYTES> m_value;
};
//=================================================================================
template <size_t KEYBYTES>
class CFHEPrivateKey
{
public:
CFHEPrivateKey ()
{
// make sure there's at least one bit set to true
bool hasAnySet = false;
do
{
for (uint8_t &v : m_key)
{
v = Random_uint8_t();
if (v)
hasAnySet = true;
}
}
while (!hasAnySet);
// TOOD: temp!
m_key.back() |= 1;
// TODO: remove!
//m_key[0] = 15;
}
CFHEEncryptedBit<KEYBYTES> EncryptBit (bool value)
{
return CFHEEncryptedBit<KEYBYTES>(m_key, value);
}
bool DecryptBit (const CFHEEncryptedBit<KEYBYTES>& value)
{
return value.Decrypt(m_key);
}
private:
std::array<uint8_t, KEYBYTES> m_key;
};
typedef CFHEPrivateKey<c_keyBytes> TPrivateKey;
typedef CFHEEncryptedBit<c_keyBytes> TEncryptedBit;
//=================================================================================
int main (int argc, char **argv)
{
/*
TPrivateKey privateKey;
TEncryptedBit falseBit = privateKey.EncryptBit(false);
TEncryptedBit trueBit = privateKey.EncryptBit(true);
TEncryptedBit trueBit2 = privateKey.EncryptBit(true);
*/
for (int i = 0; i < 100000; ++i)
{
TPrivateKey privateKey;
TEncryptedBit falseBit = privateKey.EncryptBit(false);
TEncryptedBit trueBit = privateKey.EncryptBit(true);
TEncryptedBit trueBit2 = privateKey.EncryptBit(true);
falseBit.XOR(trueBit);
Assert(privateKey.DecryptBit(falseBit) == true);
falseBit.AND(trueBit2);
Assert(privateKey.DecryptBit(falseBit) == true);
}
WaitForEnter();
return 0;
}
/*
TODO:
* AND isn't always working, think about that a bit!
* NOT is also having a problem... wtf?
* i think maybe we need the X_i_j like described in that paper.
* make it so you have a templated encrypted object. it does sizeof(type)*8 to figure out how many encrypted bits it needs etc.
* unit tests?
* make it do useful work!
* do some profiling to show how fast it is?
* make something that uses all features
! they keep saying the key is a p-bit odd integer. maybe it needs an odd number of bits set to true?
* http://crypto.stanford.edu/craig/easy-fhe.pdf
BLOG:
* talk about xor, and, not
* talk about "unencrypted" values being used in the math too
* talk about bit rotation and bit shifting
* mention that it's turing complete
* basic examples with small keys
* mention that this is FHE but is insecure. Same general idea as first FHE paper
* link to the FHE paper, and the other stuff? or wait til doing the "real" thing?
* figure out how this is insecure (gaussian elimination?)
* probably best for part 2
* mention it on blog though
* mention how you did the random fix up, to help keep a constant operating time
*/