Skip to content

Commit

Permalink
better tungsten test
Browse files Browse the repository at this point in the history
  • Loading branch information
ladnir committed Jan 23, 2024
1 parent f86a58d commit d34a114
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 110 deletions.
2 changes: 2 additions & 0 deletions libOTe/Tools/TungstenCode/TungstenCode.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ namespace osuCrypto {
auto xi = x + i;
auto xs = x + ((i + 1) % size);
ctx.plus(*xs, *xs, *xi);
ctx.mulConst(*xs, *xs);

for (u64 p = 0; p < Table::data[0].size(); ++p)
{
Expand Down Expand Up @@ -254,6 +255,7 @@ namespace osuCrypto {
ctx.plus(*x1, *x1, *xi);
ctx.plus(*x2, *x2, *xi);
ctx.plus(*x3, *x3, *xi);
ctx.mulConst(*xs, *xs);

}
}
Expand Down
228 changes: 118 additions & 110 deletions libOTe_Tests/TungstenCode_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,171 +11,179 @@ namespace tests_libOTe

template<
typename Table,
typename T>
typename T,
typename Ctx
>
void accumulateBlock(
span<T> x,
u64 i)
u64 i,
Ctx ctx)
{
auto table = Table::data;
for (u64 j = 0; j < Table::data.size(); ++j, ++i)
{
if (i == x.size())
return;

x[(i + 1) % x.size()] ^= x[i];
auto idx = (i + 1) % x.size();
ctx.plus(x[idx], x[idx], x[i]);
ctx.mulConst(x[idx], x[idx]);

for (u64 k = 0; k < table[j].size(); ++k)
{
auto d = (i + table[j][k]) % x.size();
if (d != i)
x[d] ^= x[i];
ctx.plus(x[d], x[d], x[i]);

}
}
}

void TungstenCode_encode_test(const oc::CLP& cmd)
template<typename F, typename Ctx>
void TungstenCode_encode_impl(u64 k, double r)
{
Ctx ctx;
u64 n = k * r;
TungstenCode encoder;

auto K = cmd.getManyOr<u64>("k", { 256, 3328, 152336 });
auto R = cmd.getManyOr<double>("R", { 2.0 });
encoder.config(k, n);

for (auto k : K) for (auto r : R)
{

using F = block;
using Ctx = CoeffCtxGF128;
PRNG prng(CCBlock);

Ctx ctx;
u64 n = k * r;
TungstenCode encoder;
std::vector<F> x(n);
prng.get<F>(x);

encoder.config(k, n);

//std::iota(encoder.mPerm.mPerm.begin(), encoder.mPerm.mPerm.end(), 0);
//std::swap(encoder.mPerm.mPerm[0], encoder.mPerm.mPerm[1]);

PRNG prng(CCBlock);

std::vector<F> x(n);
prng.get<F>(x);

auto z = x;
{

std::vector<F> in(roundUpTo(n - k, encoder.ChunkSize));
std::vector<F> out(roundUpTo(n - k, encoder.ChunkSize));
auto i = n - in.size();
std::copy(x.begin() + i, x.end(), in.begin());

u64 tableSize = TableTungsten1024x4::data.size();

auto z = x;
for (u64 j = 0; j <= encoder.mNumIter; ++j)
{

std::vector<F> in(roundUpTo(n - k, encoder.ChunkSize));
std::vector<F> out(roundUpTo(n - k, encoder.ChunkSize));
auto i = n - in.size();
std::copy(x.begin() + i, x.end(), in.begin());

u64 tableSize = TableTungsten1024x4::data.size();

for (u64 j = 0; j <= encoder.mNumIter; ++j)
auto in2 = in;
auto in3 = in;
for (u64 i = 0; i < in.size(); i += tableSize)
{
auto in2 = in;
auto in3 = in;
for (u64 i = 0; i < in.size(); i += tableSize)
{
accumulateBlock<TableTungsten1024x4, block>(in, i);
}
accumulateBlock<TableTungsten1024x4, F>(in, i, ctx);
}

TungstenNoop noop;
encoder.accumulate<F, TungstenNoop, Ctx, F*>(in2.data(), nullptr, in.size(), noop, ctx);
TungstenNoop noop;
encoder.accumulate<F, TungstenNoop, Ctx, F*>(in2.data(), nullptr, in.size(), noop, ctx);

if (in2 != in)
throw RTE_LOC;
if (in2 != in)
throw RTE_LOC;

if (j < encoder.mNumIter)
if (j < encoder.mNumIter)
{
auto perm = encoder.mPerm.mPerm;
std::vector<u8> flags(perm.size());
for (u64 p = 0; p < perm.size(); ++p)
{
auto perm = encoder.mPerm.mPerm;
std::vector<u8> flags(perm.size());
for (u64 p = 0; p < perm.size(); ++p)
{
if (std::exchange(flags[perm[p]], 1))
throw RTE_LOC;

for (u64 k = 0; k < encoder.ChunkSize; ++k)
out[perm[p] * encoder.ChunkSize + k] = in[p * encoder.ChunkSize + k];
}


//for (u64 p = 0; p < std::min<u64>(10, z.size()); ++p)
//{
// std::cout << p << " ti " << j << ": " << in3[p] << std::endl;
//}
//std::cout << "\n";
encoder.mPerm.reset();
std::vector<block> out2(out.size());
encoder.accumulate<F>(in3.data(), out2.data(), in.size(), encoder.mPerm, ctx);
if (std::exchange(flags[perm[p]], 1))
throw RTE_LOC;

for (u64 k = 0; k < encoder.ChunkSize; ++k)
out[perm[p] * encoder.ChunkSize + k] = in[p * encoder.ChunkSize + k];
}

if (in3 != in)
throw RTE_LOC;

//for (u64 p = 0; p < std::min<u64>(10, z.size()); ++p)
//{
// std::cout << p << " to " <<j <<": " << out2[p] << " in " << in3[p] << std::endl;
//}
//std::cout << "\n\n";
//for (u64 p = 0; p < std::min<u64>(10, z.size()); ++p)
//{
// std::cout << p << " ti " << j << ": " << in3[p] << std::endl;
//}
//std::cout << "\n";
encoder.mPerm.reset();
std::vector<F> out2(out.size());
encoder.accumulate<F, TungstenPerm<8>, Ctx, F*>(in3.data(), out2.data(), in.size(), encoder.mPerm, ctx);

if (out2 != out)
{
for (u64 p = 0; p < out.size(); ++p)
{
std::cout << p << " " << (p/encoder.ChunkSize) << " " << out[p] << " " << out2[p] << (out[p] != out2[p] ? " <<<<<" : "") << std::endl;
}

throw RTE_LOC;
}
if (in3 != in)
throw RTE_LOC;

std::swap(in, out);
//for (u64 p = 0; p < std::min<u64>(10, z.size()); ++p)
//{
// std::cout << p << " to " <<j <<": " << out2[p] << " in " << in3[p] << std::endl;
//}
//std::cout << "\n\n";

}
else
if (out2 != out)
{
for (u64 i = 0; i < k; ++i)
z[i] ^= in[i];
z.resize(k);

auto w = x;
//for (u64 j = 0; j < std::min<u64>(20, z.size()); ++j)
//for (u64 p = 0; p < out.size(); ++p)
//{
// std::cout << j << " ft : " << in3[j] << " " << w[j] << std::endl;
// std::cout << p << " " << (p / encoder.ChunkSize) << " " << out[p] << " " << out2[p] << (out[p] != out2[p] ? " <<<<<" : "") << std::endl;
//}
//std::cout << "\n\n";

TungstenAdder<TungstenCode::ChunkSize> adder;
encoder.accumulate<F>(in3.data(), w.data(), in.size(), adder, ctx);
w.resize(k);
if (w != z)
throw RTE_LOC;

throw RTE_LOC;
}
}
}

auto y = x;
encoder.dualEncode<F>(y.data(), ctx);
y.resize(k);
std::swap(in, out);

if (z != y)
{
auto m = std::min<u64>(40, z.size());
for (u64 p = 0; p < m; ++p)
{
std::cout << p << " " << (p / encoder.ChunkSize) << " " << z[p] << " " << y[p] << (z[p] != y[p] ? " <<<<<" : "") << std::endl;
}
for (u64 p = m; p < z.size(); ++p)
else
{
if(z[p] != y[p])
std::cout << p << " " << (p / encoder.ChunkSize) << " " << z[p] << " " << y[p] << (z[p] != y[p] ? " <<<<<" : "") << std::endl;
for (u64 i = 0; i < k; ++i)
ctx.plus(z[i], z[i], in[i]);

z.resize(k);

auto w = x;
//for (u64 j = 0; j < std::min<u64>(20, z.size()); ++j)
//{
// std::cout << j << " ft : " << in3[j] << " " << w[j] << std::endl;
//}
//std::cout << "\n\n";

TungstenAdder<TungstenCode::ChunkSize> adder;
encoder.accumulate<F>(in3.data(), w.data(), in.size(), adder, ctx);
w.resize(k);
if (w != z)
throw RTE_LOC;

}
throw RTE_LOC;
}
}

auto y = x;
encoder.dualEncode<F>(y.data(), ctx);
y.resize(k);

if (z != y)
{
//auto m = std::min<u64>(40, z.size());
//for (u64 p = 0; p < m; ++p)
//{
// std::cout << p << " " << (p / encoder.ChunkSize) << " " << z[p] << " " << y[p] << (z[p] != y[p] ? " <<<<<" : "") << std::endl;
//}
//for (u64 p = m; p < z.size(); ++p)
//{
// if (z[p] != y[p])
// std::cout << p << " " << (p / encoder.ChunkSize) << " " << z[p] << " " << y[p] << (z[p] != y[p] ? " <<<<<" : "") << std::endl;
//}
throw RTE_LOC;
}
}

void TungstenCode_encode_test(const oc::CLP& cmd)
{

auto K = cmd.getManyOr<u64>("k", { 256, 3328, 152336 });
auto R = cmd.getManyOr<double>("R", { 2.0 });

for (auto k : K) for (auto r : R)
{
TungstenCode_encode_impl<u32, CoeffCtxInteger>(k, r);
TungstenCode_encode_impl<u8, CoeffCtxInteger>(k,r);
TungstenCode_encode_impl<block, CoeffCtxGF128>(k, r);
TungstenCode_encode_impl<std::array<u8, 4>, CoeffCtxArray<u8, 4>>(k, r);
}

}
}

0 comments on commit d34a114

Please sign in to comment.