From d34a114575b83209ebc6bdd714e658e0a57a20d0 Mon Sep 17 00:00:00 2001 From: Peter Rindal Date: Tue, 23 Jan 2024 10:26:19 -0800 Subject: [PATCH] better tungsten test --- libOTe/Tools/TungstenCode/TungstenCode.h | 2 + libOTe_Tests/TungstenCode_Tests.cpp | 228 ++++++++++++----------- 2 files changed, 120 insertions(+), 110 deletions(-) diff --git a/libOTe/Tools/TungstenCode/TungstenCode.h b/libOTe/Tools/TungstenCode/TungstenCode.h index 8044edb..76d174d 100644 --- a/libOTe/Tools/TungstenCode/TungstenCode.h +++ b/libOTe/Tools/TungstenCode/TungstenCode.h @@ -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) { @@ -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); } } diff --git a/libOTe_Tests/TungstenCode_Tests.cpp b/libOTe_Tests/TungstenCode_Tests.cpp index 12608cf..080be8a 100644 --- a/libOTe_Tests/TungstenCode_Tests.cpp +++ b/libOTe_Tests/TungstenCode_Tests.cpp @@ -11,10 +11,13 @@ namespace tests_libOTe template< typename Table, - typename T> + typename T, + typename Ctx + > void accumulateBlock( span x, - u64 i) + u64 i, + Ctx ctx) { auto table = Table::data; for (u64 j = 0; j < Table::data.size(); ++j, ++i) @@ -22,160 +25,165 @@ namespace tests_libOTe 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 + void TungstenCode_encode_impl(u64 k, double r) { + Ctx ctx; + u64 n = k * r; + TungstenCode encoder; - auto K = cmd.getManyOr("k", { 256, 3328, 152336 }); - auto R = cmd.getManyOr("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 x(n); + prng.get(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 x(n); - prng.get(x); + auto z = x; + { + std::vector in(roundUpTo(n - k, encoder.ChunkSize)); + std::vector 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 in(roundUpTo(n - k, encoder.ChunkSize)); - std::vector 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(in, i); - } + accumulateBlock(in, i, ctx); + } - TungstenNoop noop; - encoder.accumulate(in2.data(), nullptr, in.size(), noop, ctx); + TungstenNoop noop; + encoder.accumulate(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 flags(perm.size()); + for (u64 p = 0; p < perm.size(); ++p) { - auto perm = encoder.mPerm.mPerm; - std::vector 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(10, z.size()); ++p) - //{ - // std::cout << p << " ti " << j << ": " << in3[p] << std::endl; - //} - //std::cout << "\n"; - encoder.mPerm.reset(); - std::vector out2(out.size()); - encoder.accumulate(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(10, z.size()); ++p) - //{ - // std::cout << p << " to " <(10, z.size()); ++p) + //{ + // std::cout << p << " ti " << j << ": " << in3[p] << std::endl; + //} + //std::cout << "\n"; + encoder.mPerm.reset(); + std::vector out2(out.size()); + encoder.accumulate, 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(10, z.size()); ++p) + //{ + // std::cout << p << " to " <(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 adder; - encoder.accumulate(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(y.data(), ctx); - y.resize(k); + std::swap(in, out); - if (z != y) - { - auto m = std::min(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(20, z.size()); ++j) + //{ + // std::cout << j << " ft : " << in3[j] << " " << w[j] << std::endl; + //} + //std::cout << "\n\n"; + + TungstenAdder adder; + encoder.accumulate(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(y.data(), ctx); + y.resize(k); + + if (z != y) + { + //auto m = std::min(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("k", { 256, 3328, 152336 }); + auto R = cmd.getManyOr("R", { 2.0 }); + + for (auto k : K) for (auto r : R) + { + TungstenCode_encode_impl(k, r); + TungstenCode_encode_impl(k,r); + TungstenCode_encode_impl(k, r); + TungstenCode_encode_impl, CoeffCtxArray>(k, r); + } + } } \ No newline at end of file