File size: 7,101 Bytes
e1191aa | 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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | #include "Common/Defines.h"
#include "Crypto/Commit.h"
#include "Common/BitVector.h"
//#include "Common/Timer.h"
#include <boost/math/special_functions/binomial.hpp>
#include <boost/multiprecision/cpp_bin_float.hpp>
namespace bOPRF {
Timer gTimer;
const block ZeroBlock = _mm_set_epi64x(0, 0);
const block OneBlock = _mm_set_epi64x(0, 1);
const block AllOneBlock = _mm_set_epi64x(u64(-1), u64(-1));
const block CCBlock = ([]() {block cc; memset(&cc, 0xcc, sizeof(block)); return cc; })();
std::ostream& operator<<(std::ostream& out, const block& blk)
{
out << std::hex;
u64* data = (u64*)&blk;
out << std::setw(16) << std::setfill('0') << data[0]
<< std::setw(16) << std::setfill('0') << data[1];
out << std::dec << std::setw(0);
return out;
}
std::ostream& operator<<(std::ostream& out, const blockBop& blk)
{
out << std::hex;
u64* data = (u64*)&blk;
out << std::setw(16) << std::setfill('0') << data[0] << "..."
//<< std::setw(16) << std::setfill('0') << data[1]
//<< std::setw(16) << std::setfill('0') << data[2]
//<< std::setw(16) << std::setfill('0') << data[3]
//<< std::setw(16) << std::setfill('0') << data[4]
//<< std::setw(16) << std::setfill('0') << data[5]
<< std::setw(16) << std::setfill('0') << data[6];
out << std::dec << std::setw(0);
return out;
}
std::ostream* gOut = &std::cout;
std::ostream& operator<<(std::ostream& out, const Commit& comm)
{
out << std::hex;
u32* data = (u32*)comm.data();
out << std::setw(8) << std::setfill('0') << data[0]
<< std::setw(8) << std::setfill('0') << data[1]
<< std::setw(8) << std::setfill('0') << data[2]
<< std::setw(8) << std::setfill('0') << data[3]
<< std::setw(8) << std::setfill('0') << data[4];
out << std::dec << std::setw(0);
return out;
}
block PRF(const block& b, u64 i)
{
//TODO("REMOVE THIS!!");
//return b;
block ret, tweak = _mm_set1_epi64x(i), enc;
ret = b ^ tweak;
mAesFixedKey.ecbEncBlock(ret, enc);
ret = ret ^ enc; // H( a0 )
return ret;
}
void split(const std::string &s, char delim, std::vector<std::string> &elems) {
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
}
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, elems);
return elems;
}
const int tab64[64] = {
63, 0, 58, 1, 59, 47, 53, 2,
60, 39, 48, 27, 54, 33, 42, 3,
61, 51, 37, 40, 49, 18, 28, 20,
55, 30, 34, 11, 43, 14, 22, 4,
62, 57, 46, 52, 38, 26, 32, 41,
50, 36, 17, 19, 29, 10, 13, 21,
56, 45, 25, 31, 35, 16, 9, 12,
44, 24, 15, 8, 23, 7, 6, 5 };
u64 log2floor(u64 value)
{
value |= value >> 1;
value |= value >> 2;
value |= value >> 4;
value |= value >> 8;
value |= value >> 16;
value |= value >> 32;
return tab64[((uint64_t)((value - (value >> 1)) * 0x07EDD5E59A4E28C2)) >> 58];
}
u64 log2ceil(u64 value)
{
return u64(std::ceil(std::log2(value)));
}
u64 get_stash_size(u64 neles) {
if (neles >= (1 << 24))
return 2;
if (neles >= (1 << 20))
return 3;
if (neles >= (1 << 16))
return 4;
if (neles >= (1 << 12))
return 6;
if (neles >= (1 << 8))
return 12;
return 12; //other
throw std::runtime_error("get_stash_size: rt error at " LOCATION);
}
//template<unsigned int N = 16>
double getBinOverflowProb(u64 numBins, u64 numBalls, u64 binSize, double epsilon = 0.0001)
{
if (numBalls <= binSize)
return std::numeric_limits<double>::max();
if (numBalls > unsigned(-1))
{
auto msg = ("boost::math::binomial_coefficient(...) only supports " + std::to_string(sizeof(unsigned) * 8) + " bit inputs which was exceeded." LOCATION);
std::cout << msg << std::endl;
throw std::runtime_error(msg);
}
//try
//{
//std::cout << numBalls << " " << numBins << " " << binSize << std::endl;
typedef boost::multiprecision::number<boost::multiprecision::backends::cpp_bin_float<16>> T;
T sum = 0.0;
T sec = 0.0;// minSec + 1;
T diff = 1;
u64 i = binSize + 1;
while (diff > T(epsilon) && numBalls >= i /*&& sec > minSec*/)
{
sum += numBins * boost::math::binomial_coefficient<T>(numBalls, i)
* boost::multiprecision::pow(T(1.0) / numBins, i) * boost::multiprecision::pow(1 - T(1.0) / numBins, numBalls - i);
//std::cout << "sum[" << i << "] " << sum << std::endl;
T sec2 = boost::multiprecision::log2(sum);
diff = boost::multiprecision::abs(sec - sec2);
//std::cout << diff << std::endl;
sec = sec2;
i++;
}
return std::max<double>(0, (double)-sec);
//}
//catch (std::exception& e)
//{
// if (N == 16)
// {
// std::cout << "percision failure at " << LOCATION << "\n tring again with high percision (performance penalty)" << std::endl;
// // try again with higher percition
// return getBinOverflowProb<128>(numBins, numBalls, binSize);
// }
//
// std::cout << "retry percision failure at " << LOCATION << "\n" << e.what() << std::endl;
// throw;
//}
}
u64 get_bin_size(u64 numBins, u64 numBalls, u64 statSecParam)
{
auto B = std::max<u64>(1, numBalls / numBins);
double currentProb = 0;
u64 step = 1;
bool doubling = true;
while (currentProb < statSecParam || step > 1)
{
if (!step)
throw std::runtime_error(LOCATION);
if (statSecParam > currentProb)
{
if (doubling) step = std::max<u64>(1, step * 2);
else step = std::max<u64>(1, step / 2);
B += step;
}
else
{
doubling = false;
step = std::max<u64>(1, step / 2);
B -= step;
}
currentProb = getBinOverflowProb(numBins, numBalls, B);
}
return B;
//if (simpleSize <= cuckooSize)
//{
// if (simpleSize >= (1 << 24))
// return 28;
// if (simpleSize >= (1 << 20))
// return 27;
// if (simpleSize >= (1 << 16))
// return 26;
// if (simpleSize >= (1 << 12))
// return 25;
// if (simpleSize >= (1 << 8))
// return 24;
// //return 30; //other
//}
//else
//{
// return simpleSize / cuckooSize + 16 + 4 * std::sqrt(simpleSize * std::log2(cuckooSize) / cuckooSize);
//}
//throw std::runtime_error("get_bin_size: rt error at " LOCATION);
}
u64 get_codeword_size(u64 neles) {
if (neles >= (1 << 24))
return 448 / 8; // in byte
if (neles >= (1 << 20))
return 448 / 8;
if (neles >= (1 << 16))
return 440 / 8;
if (neles >= (1 << 12))
return 432 / 8;
if (neles >= (1 << 8))
return 424 / 8;
return 424 / 8;
//throw std::runtime_error("get_codeword_size: rt error at " LOCATION);
}
u64 get_mask_size(u64 neles, u64 othersize, u64 statSecParam) {
return (statSecParam + log2(neles * othersize) + 7 ) / 8;
//if (neles >= (1 << 24))
// return 88 / 8; // in byte
//if (neles >= (1 << 20))
// return 80 / 8;
//if (neles >= (1 << 16))
// return 72 / 8;
//if (neles >= (1 << 12))
// return 64 / 8;
//if (neles >= (1 << 8))
// return 56 / 8;
//return 56 / 8;
////return (40 + 2 * log(neles)) / 8;
// throw std::runtime_error("get_codeword_size: rt error at " LOCATION);
}
}
|