diff --git a/tests/Paxos_Tests.cpp b/tests/Paxos_Tests.cpp index 87ddadb..7ba77c2 100644 --- a/tests/Paxos_Tests.cpp +++ b/tests/Paxos_Tests.cpp @@ -2151,6 +2151,166 @@ void Baxos_idxtype_sentinel_and_params_Test(const oc::CLP&) } } +void Paxos_bounds_and_gf128Inv_Test(const oc::CLP&) +{ + auto expectThrows = [](auto&& fn) + { + bool threw = false; + try + { + fn(); + } + catch (const std::runtime_error&) + { + threw = true; + } + if (!threw) + throw RTE_LOC; + }; + + // 1. gf128Inv must pivot on any non-zero GF(2^128) element (not only + // OneBlock) and must still report a singular matrix as empty. + { + Matrix mtx(2, 2); + mtx(0, 0) = ZeroBlock; + mtx(0, 1) = block(0, 3); + mtx(1, 0) = block(0, 2); + mtx(1, 1) = block(0, 5); + + auto inv = gf128Inv(mtx); + if (inv.rows() != 2 || inv.cols() != 2) + throw RTE_LOC; + auto prod = gf128Mul(mtx, inv); + if (prod(0, 0) != oc::OneBlock || prod(0, 1) != ZeroBlock || + prod(1, 0) != ZeroBlock || prod(1, 1) != oc::OneBlock) + throw RTE_LOC; + + Matrix singular(2, 2); + singular(0, 1) = block(0, 3); + singular(1, 1) = block(0, 5); + if (gf128Inv(singular).size() != 0) + throw RTE_LOC; + } + + // 2. Paxos MatrixView encode/decode when the row width is a multiple + // of sizeof(block). 16 bytes per row reduces to the span overload and 32 + // bytes per row to MatrixView with two blocks per row; in both + // cases the output has Paxos::size() rows rather than numItems rows. + for (u64 cols : { u64(16), u64(32) }) + { + const u64 n = 128; + Paxos paxos; + paxos.init(n, 3, 40, PaxosParam::GF128, block(1, 2)); + PRNG prng(block(3, 4)); + std::vector items(n); + prng.get(items.data(), items.size()); + Matrix values(n, cols), decoded(n, cols), p(paxos.size(), cols); + Matrix shortValues(n - 1, cols), shortP(paxos.size() - 1, cols); + prng.get(values.data(), values.size()); + + paxos.setInput(items); + expectThrows([&]() { paxos.encode(shortValues, p); }); + expectThrows([&]() { paxos.encode(values, shortP); }); + paxos.encode(values, p); + + expectThrows([&]() { paxos.decode(items, shortValues, p); }); + expectThrows([&]() { paxos.decode(items, decoded, shortP); }); + paxos.decode(items, decoded, p); + if (decoded != values) + throw RTE_LOC; + } + + // 3. The span and MatrixView overloads of Paxos::encode, + // Paxos::decode, Baxos::solve, and Baxos::decode (and direct + // Baxos::implParSolve) reject inputs, values, and tables whose sizes do + // not match, and still round-trip otherwise. + { + const u64 n = 64; + PRNG prng(block(7, 8)); + std::vector items(n), values(n), decoded(n); + prng.get(items.data(), items.size()); + prng.get(values.data(), values.size()); + std::vector shortItems(items.begin(), items.end() - 1); + std::vector shortValues(n - 1), longValues(n + 1); + + Paxos paxos; + paxos.init(n, 3, 40, PaxosParam::GF128, block(5, 6)); + std::vector p(paxos.size()); + std::vector shortP(paxos.size() - 1), longP(paxos.size() + 1); + + paxos.setInput(items); + expectThrows([&]() { paxos.encode(shortValues, p); }); + expectThrows([&]() { paxos.encode(longValues, p); }); + expectThrows([&]() { paxos.encode(values, shortP); }); + expectThrows([&]() { paxos.encode(values, longP); }); + paxos.encode(values, p); + + expectThrows([&]() { paxos.decode(items, shortValues, p); }); + expectThrows([&]() { paxos.decode(items, longValues, p); }); + expectThrows([&]() { paxos.decode(items, decoded, shortP); }); + expectThrows([&]() { paxos.decode(items, decoded, longP); }); + paxos.decode(items, decoded, p); + if (decoded != values) + throw RTE_LOC; + + // binSize 32 gives two bins; binSize 64 takes the single-bin path. + for (u64 binSize : { u64(32), u64(64) }) + { + Baxos baxos; + baxos.init(n, binSize, 3, 40, PaxosParam::GF128, block(9, 10)); + if (baxos.mNumBins != n / binSize) + throw RTE_LOC; + std::vector bp(baxos.size()); + std::vector shortBp(baxos.size() - 1), longBp(baxos.size() + 1); + + expectThrows([&]() { baxos.solve(shortItems, values, bp); }); + expectThrows([&]() { baxos.solve(items, shortValues, bp); }); + expectThrows([&]() { baxos.solve(items, longValues, bp); }); + expectThrows([&]() { baxos.solve(items, values, shortBp); }); + expectThrows([&]() { baxos.solve(items, values, longBp); }); + baxos.solve(items, values, bp); + + PxVector pxShortVals(shortValues), pxVals(values); + PxVector pxOut(bp); + auto h = pxOut.defaultHelper(); + expectThrows([&]() { baxos.implParSolve(shortItems, pxVals, pxOut, nullptr, 1, h); }); + expectThrows([&]() { baxos.implParSolve(items, pxShortVals, pxOut, nullptr, 1, h); }); + + expectThrows([&]() { baxos.decode(shortItems, values, bp); }); + expectThrows([&]() { baxos.decode(items, shortValues, bp); }); + expectThrows([&]() { baxos.decode(items, longValues, bp); }); + expectThrows([&]() { baxos.decode(items, decoded, shortBp); }); + expectThrows([&]() { baxos.decode(items, decoded, longBp); }); + std::fill(decoded.begin(), decoded.end(), ZeroBlock); + baxos.decode(items, decoded, bp); + if (decoded != values) + throw RTE_LOC; + + // Two blocks per row: sizes are counted in rows, not elements. + const u64 cols = 2; + Matrix mValues(n, cols), mDecoded(n, cols); + Matrix mTable(baxos.size(), cols); + Matrix mShortValues(n - 1, cols); + Matrix mShortTable(baxos.size() - 1, cols); + prng.get(mValues.data(), mValues.size()); + MatrixView mValuesView(mValues.data(), n, cols); + MatrixView mTableView(mTable.data(), baxos.size(), cols); + MatrixView mShortValuesView(mShortValues.data(), n - 1, cols); + MatrixView mShortTableView(mShortTable.data(), baxos.size() - 1, cols); + + expectThrows([&]() { baxos.solve(items, mShortValuesView, MatrixView(mTable)); }); + expectThrows([&]() { baxos.solve(items, mValuesView, MatrixView(mShortTable)); }); + baxos.solve(items, mValuesView, MatrixView(mTable)); + + expectThrows([&]() { baxos.decode(items, MatrixView(mShortValues), mTableView); }); + expectThrows([&]() { baxos.decode(items, MatrixView(mDecoded), mShortTableView); }); + baxos.decode(items, MatrixView(mDecoded), mTableView); + if (mDecoded != mValues) + throw RTE_LOC; + } + } +} + void Paxos_dense_kernel_Test(const oc::CLP&) diff --git a/tests/Paxos_Tests.h b/tests/Paxos_Tests.h index f1bb922..cddafb2 100644 --- a/tests/Paxos_Tests.h +++ b/tests/Paxos_Tests.h @@ -28,6 +28,7 @@ void Baxos_bin_overflow_Test(const oc::CLP& cmd); void Baxos_bin_size_Test(const oc::CLP& cmd); void Baxos_decode_weight_overflow_Test(const oc::CLP& cmd); void Baxos_idxtype_sentinel_and_params_Test(const oc::CLP& cmd); +void Paxos_bounds_and_gf128Inv_Test(const oc::CLP& cmd); diff --git a/tests/UnitTests.cpp b/tests/UnitTests.cpp index 3f8d585..00ed1cc 100644 --- a/tests/UnitTests.cpp +++ b/tests/UnitTests.cpp @@ -42,6 +42,7 @@ namespace volePSI_Tests t.add("Baxos_bin_size_Test", Baxos_bin_size_Test); t.add("Baxos_decode_weight_overflow_Test", Baxos_decode_weight_overflow_Test); t.add("Baxos_idxtype_sentinel_and_params_Test", Baxos_idxtype_sentinel_and_params_Test); + t.add("Paxos_bounds_and_gf128Inv_Test", Paxos_bounds_and_gf128Inv_Test); #ifdef VOLE_PSI_ENABLE_GMW t.add("SilentTripleGen_test ", SilentTripleGen_test); diff --git a/volePSI/Paxos.h b/volePSI/Paxos.h index 635d068..02367ac 100644 --- a/volePSI/Paxos.h +++ b/volePSI/Paxos.h @@ -204,7 +204,7 @@ namespace volePSI encode( MatrixView((block*)values.data(), n, m), - MatrixView((block*)output.data(), n, m), + MatrixView((block*)output.data(), output.rows(), m), prng); } else diff --git a/volePSI/PaxosImpl.h b/volePSI/PaxosImpl.h index 07f57fd..cbfb932 100644 --- a/volePSI/PaxosImpl.h +++ b/volePSI/PaxosImpl.h @@ -88,7 +88,7 @@ namespace volePSI // is non-zero for (u64 j = i + 1; j < n; ++j) { - if (mtx(j, i) == oc::OneBlock) + if (mtx(j, i) != oc::ZeroBlock) { // found one, swap the rows and break for (u64 k = 0; k < n; ++k) @@ -1014,10 +1014,9 @@ namespace volePSI setTimePoint("decode begin"); validate(); - if (!mHasher.isConfiguredFor(mWeight, mSparseSize) || values.size() < inputs.size()) - throw RTE_LOC; - - if (PP.size() != size()) + if (!mHasher.isConfiguredFor(mWeight, mSparseSize) || + static_cast(values.size()) != inputs.size() || + static_cast(PP.size()) != size()) throw RTE_LOC; auto main = inputs.size() / gPaxosBuildRowSize * gPaxosBuildRowSize; @@ -2792,8 +2791,9 @@ namespace volePSI { mPaxosParam.validate(); if (mWeight < 2 || mWeight > maxWeight || mPaxosParam.mWeight != mWeight || - !mNumBins || inputs.size() > mNumItems || V.size() < inputs.size() || - P.size() != size()) + !mNumBins || inputs.size() != mNumItems || + static_cast(V.size()) != mNumItems || + static_cast(P.size()) != size()) throw RTE_LOC; // Select the smallest IdxType whose non-sentinel range fits all column indices [0, size()). @@ -2821,6 +2821,12 @@ namespace volePSI u64 numThreads, Helper& h) { + mPaxosParam.validate(); + if (mWeight < 2 || mWeight > maxWeight || mPaxosParam.mWeight != mWeight || + !mNumBins || inputs_.size() != mNumItems || + static_cast(vals_.size()) != mNumItems || + static_cast(p_.size()) != size()) + throw RTE_LOC; #ifndef NDEBUG { std::unordered_set inputSet; @@ -2830,8 +2836,6 @@ namespace volePSI } } #endif - if (p_.size() != size()) - throw RTE_LOC; if (mNumBins == 1) { @@ -3412,7 +3416,8 @@ namespace volePSI { mPaxosParam.validate(); if (mWeight < 2 || mWeight > maxWeight || mPaxosParam.mWeight != mWeight || - !mNumBins || pp.size() != size() || values.size() < inputs.size()) + !mNumBins || static_cast(values.size()) != inputs.size() || + static_cast(pp.size()) != size()) throw RTE_LOC; if (inputs.empty()) return;