Skip to content

Commit

Permalink
improved check and minor fixed to frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
ladnir committed Feb 6, 2024
1 parent 540f72c commit 17b1f9b
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 42 deletions.
6 changes: 4 additions & 2 deletions frontend/ExampleTwoChooseOne.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "libOTe/TwoChooseOne/Silent/SilentOtExtSender.h"
#include "util.h"
#include "coproto/Socket/AsioSocket.h"
#include "cryptoTools/Common/BitVector.h"
#include "cryptoTools/Crypto/PRNG.h"

namespace osuCrypto
{
Expand Down Expand Up @@ -203,8 +205,6 @@ namespace osuCrypto
}
}

// make sure all messages have been sent.
cp::sync_wait(chl.flush());
}
else
{
Expand Down Expand Up @@ -302,6 +302,8 @@ namespace osuCrypto
}


// make sure all messages have been sent.
cp::sync_wait(chl.flush());

auto e = timer.setTimePoint("finish");
auto milli = std::chrono::duration_cast<std::chrono::milliseconds>(e - s).count();
Expand Down
46 changes: 35 additions & 11 deletions libOTe/Tools/ExConvCode/ExConvChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ namespace osuCrypto
u64 awEnd = cmd.getOr("awEnd", 20);
u64 bwBeing = cmd.getOr("bw", 3);
u64 bwEnd = cmd.getOr("bwEnd", 11);
auto x2 = cmd.isSet("x2");

for (u64 aw = awBeing; aw < awEnd; aw += 2)
{
Expand All @@ -137,8 +138,8 @@ namespace osuCrypto
u64 avg = 0;
u64 gMin = n;
std::mutex mtx;
u64 ticks = n * trials;
std::atomic<u64> done = 0;
u64 ticks = x2 ? k * k * trials : n * trials;
std::atomic<u64> done = 0;
auto routine = [&](u64 i) {
for (u64 j = i; j < trials; j += nt)
{
Expand All @@ -147,11 +148,25 @@ namespace osuCrypto
encoder.config(k, n, bw, aw, sys, reg, block(21341234, j));
encoder.mAccTwice = accTwice;

//auto g = getGenerator(encoder);
//auto g2 = compress(g);
//auto G = getCompressedGenerator(encoder);
//if(std::equal(G.begin(), G.end(), g2.begin()) == false)
// throw RTE_LOC;

u64 min = 0;
if (cmd.isSet("x2"))
min = getGeneratorWeightx2(encoder, verbose);
if (x2)
{
min = getGeneratorWeightx2<ExConvCode, std::atomic<u64>&>(encoder, verbose, done);

}
else
min = getGeneratorWeight<ExConvCode, std::atomic<u64>&>(encoder, verbose, done);
{
//min = getGeneratorWeight<ExConvCode, std::atomic<u64>&>(encoder, verbose, done);
min = getGeneratorWeight2<ExConvCode, std::atomic<u64>&>(encoder, verbose, done);
//if(min != min2)
// throw RTE_LOC;
}

std::lock_guard<std::mutex> lock(mtx);
gMin = std::min(gMin, min);
Expand All @@ -166,24 +181,33 @@ namespace osuCrypto
}
//routine(nt - 1);
u64 sleep = 1;
auto start = std::chrono::high_resolution_clock::now();
while (done != ticks)
{
std::this_thread::sleep_for(std::chrono::milliseconds(sleep));
sleep = std::min<u64>(1000, sleep * 2);
u64 curDone = done;
auto end = std::chrono::high_resolution_clock::now();
auto dur = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
auto ticksPerSec = double(curDone) / dur * 1000;

auto f = double(curDone) / ticks;
u64 g = f * 40;
u64 p = f * 100;

u64 d = double(done) * 40 / ticks;
u64 sec = p > 2 ? (ticks - curDone) / ticksPerSec : 0;

std::cout << "[" << std::string(d, '|') << std::string(40 - d, ' ') << "] " << double(done) * 100 / ticks << "%\r" << std::flush;
std::cout << "[" << std::string(g, '|') << std::string(40 - g, ' ') << "] " << p << "% "<< sec <<"s\r" << std::flush;
}
std::cout <<std::string(50, ' ') << "\r" << std::flush;
std::cout << std::string(60, ' ') << "\r" << std::flush;

for (u64 i = 0; i < thrds.size(); ++i)
{
thrds[i].join();
}
std::cout << "aw " << aw << " bw " << bw <<
": min " << double(gMin) / n <<
", avg " << double(avg) / n / trials << std::endl;
std::cout << "aw " << aw << " bw " << bw <<
" min " << double(gMin) / n <<
" avg " << double(avg) / n / trials << std::endl;
}
}

Expand Down
166 changes: 137 additions & 29 deletions libOTe/Tools/ExConvCode/ExConvChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ namespace osuCrypto

// the (psuedo) minimum distance finder for expand convolute codes.
void ExConvChecker(const CLP& cmd);
namespace detail
{
struct GetGeneratorBatch
{
std::array<block, 8> mVal;

GetGeneratorBatch operator^(const GetGeneratorBatch& b) const
{
GetGeneratorBatch ret;
for (u64 i = 0; i < mVal.size(); ++i)
ret.mVal[i] = mVal[i] ^ b.mVal[i];
return ret;
}
};
}


template<typename Code>
Expand All @@ -40,6 +55,39 @@ namespace osuCrypto
return g;
}

template<typename Code>
Matrix<block> getCompressedGenerator(Code& encoder)
{
auto k = encoder.mMessageSize;
auto n = encoder.mCodeSize;;
Matrix<block> g(k, divCeil(n, 128));

u64 batchSize = sizeof(detail::GetGeneratorBatch) * 8;
std::vector<detail::GetGeneratorBatch> x(n);
for (u64 i = 0; i < n; i += batchSize)
{
memset(x.data(), 0, sizeof(x[0]) * x.size());

for (u64 p = 0; p < batchSize; ++p)
{
*oc::BitIterator((u8*)&x[i + p], p) = 1;
}

// encode a batch of batchSize=1024 unit vectors...
encoder.template dualEncode<detail::GetGeneratorBatch, CoeffCtxGF2>(x.data(), {});

u64 mk = divCeil(std::min<u64>(batchSize, n - i), 8);
auto i128 = i / 128;

// x[j,p] is the (i+p)-th bit of the j-th codeword.
// We want g[j, i+p] = x[j,p]
for (u64 j = 0; j < k; ++j)
{
memcpy(g.data(j) + i128, &x[j], mk);
}
}
return g;
}
inline Matrix<block> compress(Matrix<u8> g)
{
Matrix <block> G(g.rows(), divCeil(g.cols(), 128));
Expand Down Expand Up @@ -73,20 +121,21 @@ namespace osuCrypto
return G;
}

template<typename Code>
u64 getGeneratorWeightx2(Code& encoder, bool verbose)
template<typename Code, typename Count = u64>
u64 getGeneratorWeightx2(Code& encoder, bool verbose, Count c = {})
{
auto k = encoder.mMessageSize;
auto n = encoder.mCodeSize;
auto g = getGenerator(encoder);
auto G = compress(g);
auto G = getCompressedGenerator(encoder);
//auto G = compress(g);

u64 min = n;
auto N = G.cols();
for (u64 i = 0; i < k; ++i)
{
for (u64 i2 = 0; i2 < k; ++i2)
{
{
++c;
auto gg = G.data(i);
u64 weight = 0;
if (i == i2)
Expand All @@ -111,33 +160,33 @@ namespace osuCrypto
}
}

if (verbose)
{
std::cout << i << " \n";
for (u64 j = 0; j < n; ++j)
{
if (g(i, j))
std::cout << Color::Green << "1" << Color::Default;
else
std::cout << "0";
}
std::cout << "\n";
//if (verbose)
//{
// std::cout << i << " \n";
// for (u64 j = 0; j < n; ++j)
// {
// if (g(i, j))
// std::cout << Color::Green << "1" << Color::Default;
// else
// std::cout << "0";
// }
// std::cout << "\n";

if (i != i2)
{
// if (i != i2)
// {

std::cout << i2 << " \n";
for (u64 j = 0; j < n; ++j)
{
if (g(i2, j))
std::cout << Color::Green << "1" << Color::Default;
else
std::cout << "0";
}
std::cout << "\n";
}
// std::cout << i2 << " \n";
// for (u64 j = 0; j < n; ++j)
// {
// if (g(i2, j))
// std::cout << Color::Green << "1" << Color::Default;
// else
// std::cout << "0";
// }
// std::cout << "\n";
// }

}
//}
min = std::min<u64>(min, weight);
}
}
Expand Down Expand Up @@ -167,4 +216,63 @@ namespace osuCrypto
return *std::min_element(weights.begin(), weights.end());
}


template<typename Code, typename Count = u64>
u64 getGeneratorWeight2(Code& encoder, bool verbose, Count c = {})
{
auto k = encoder.mMessageSize;
auto n = encoder.mCodeSize;
//auto g = getGenerator(encoder);

std::vector<u64> weights(k);
//for (u64 i = 0; i < n; ++i)
//{
// std::vector<u8> x(n);
// x[i] = 1;
// encoder.template dualEncode<u8, CoeffCtxGF2>(x.data(), {});

// for (u64 j = 0; j < k; ++j)
// {
// weights[j] += x[j];
// }
// ++c;
//}
//Matrix<block> g(k, divCeil(n, 128));

u64 batchSize = sizeof(detail::GetGeneratorBatch) * 8;
std::vector<detail::GetGeneratorBatch> x(n);
for (u64 i = 0; i < n; i += batchSize)
{
memset(x.data(), 0, sizeof(x[0]) * x.size());
u64 min = std::min<u64>(batchSize, n - 1);

for (u64 p = 0; p < min; ++p)
{
*oc::BitIterator((u8*)&x[i + p], p) = 1;
}

// encode a batch of batchSize=1024 unit vectors...
encoder.template dualEncode<detail::GetGeneratorBatch, CoeffCtxGF2>(x.data(), {});

u64 mk = divCeil(min, 8);
auto i128 = i / 128;

// x[j,p] is the (i+p)-th bit of the j-th codeword.
// We want g[j, i+p] = x[j,p]
for (u64 j = 0; j < k; ++j)
{
for (u64 b = 0; b < x[j].mVal.size(); ++b)
{
weights[j] +=
popcount(x[j].mVal[b].get<u64>(0)) +
popcount(x[j].mVal[b].get<u64>(1));
}
}

c += min;
}

return *std::min_element(weights.begin(), weights.end());
}

}

0 comments on commit 17b1f9b

Please sign in to comment.