From af1876539add48377f9be976bd522f66baebe8de Mon Sep 17 00:00:00 2001 From: Peter Rindal Date: Tue, 24 Nov 2026 15:36:17 -0800 Subject: [PATCH 01/22] Allow caller-owned full SPIN maps and worker-specific banks --- libOTe/Tools/Spin/SpinCode.h | 59 +++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 17 deletions(-) diff --git a/libOTe/Tools/Spin/SpinCode.h b/libOTe/Tools/Spin/SpinCode.h index 14f14161..e1ac9c2c 100644 --- a/libOTe/Tools/Spin/SpinCode.h +++ b/libOTe/Tools/Spin/SpinCode.h @@ -8,6 +8,7 @@ #include #include #include +#include #include namespace osuCrypto { @@ -32,30 +33,46 @@ class SpinCode { return k; } - SpinCode(u64 requested, block seed, SdNoiseDistribution noise) - : mEncoder({ messageSize(requested), parameters, seed.get(0), seed.get(1) }, setup(noise)) - , mBlocks(mEncoder.make_workspace()) + SpinCode(u64 requested, block seed, SdNoiseDistribution noise, + u64 bankSeed = spin::SetupOptions{}.bank_seed) + : mEncoder(std::in_place_type, + spin::CodeSpec{ messageSize(requested), parameters, seed.get(0), seed.get(1) }, + spin::SetupOptions{ setup(noise).mode, bankSeed }) + , mBlocks(encoder().make_workspace()) + {} + + // Share a regular map across parties or workers. Each handle owns its scratch. + explicit SpinCode(std::shared_ptr full) + : mEncoder(std::move(full)), mBlocks(checkedFull().make_workspace()) {} - // Reseeds `code` when its size and noise distribution match (no allocation - // for stationary noise; a full rebuild of the map for regular noise with a - // new seed); otherwise constructs it. static void prepare(std::optional& code, u64 requested, block seed, SdNoiseDistribution noise) { + const spin::CodeSeed next{ seed.get(0), seed.get(1) }; if (code && code->messageSize() == messageSize(requested) && - code->mEncoder.setup_options().mode == setup(noise).mode) - code->mEncoder.setCodeSeed({ seed.get(0), seed.get(1) }); - else - code.emplace(requested, seed, noise); + code->encoder().setup_options().mode == setup(noise).mode) { + if (auto* owned = std::get_if(&code->mEncoder)) { + owned->setCodeSeed(next); + return; + } + const auto spec = code->encoder().specification(); + if (spec.route_seed == next.route && spec.inner_seed == next.inner) + return; + } + code.emplace(requested, seed, noise); } - u64 messageSize() const { return mEncoder.message_size(); } - u64 codeSize() const { return mEncoder.code_size(); } - const spin::PreparedEncoder& encoder() const { return mEncoder; } + u64 messageSize() const { return encoder().message_size(); } + u64 codeSize() const { return encoder().code_size(); } + const spin::PreparedEncoder& encoder() const { + if (const auto* owned = std::get_if(&mEncoder)) + return *owned; + return *std::get>(mEncoder); + } // Transposed encoding in place: x holds 2K blocks; the first K receive the // result and the rest are unchanged. void transpose(span x) { - mEncoder.transpose_inplace(std::span(x.data(), x.size()), mBlocks); + encoder().transpose_inplace(std::span(x.data(), x.size()), mBlocks); } // The same map on 2K choice bytes, each 0 or 1 (correlated OT's separate @@ -66,7 +83,7 @@ class SpinCode { if (x.size() != n) throw std::invalid_argument("SPIN choice transpose needs 2K bytes"); if (!mBits) { - mBits.emplace(mEncoder.make_bit_workspace()); + mBits.emplace(encoder().make_bit_workspace()); mBitsIn.resize(n / 64); mBitsOut.resize(k / 64); } @@ -84,7 +101,7 @@ class SpinCode { } if (high) throw std::invalid_argument("SPIN choice bytes must be 0 or 1"); - mEncoder.transpose_bits(std::span(mBitsIn.data(), mBitsIn.size()), + encoder().transpose_bits(std::span(mBitsIn.data(), mBitsIn.size()), std::span(mBitsOut.data(), mBitsOut.size()), *mBits); for (u64 i = 0; i < k; i += 8) { const u64 bits = (mBitsOut[i / 64] >> (i % 64)) & 255; @@ -101,7 +118,15 @@ class SpinCode { return { noise == SdNoiseDistribution::Stationary ? spin::SetupMode::BankedHeuristic : spin::SetupMode::Full }; } - spin::PreparedEncoder mEncoder; + const spin::PreparedEncoder& checkedFull() const { + const auto& full = std::get>(mEncoder); + if (!full || full->setup_options().mode != spin::SetupMode::Full || + full->specification().parameters != parameters) + throw std::invalid_argument("SPIN requires a full Turbo encoder"); + return *full; + } + + std::variant> mEncoder; spin::PreparedEncoder::Workspace mBlocks; std::optional mBits; AlignedUnVector mBitsIn, mBitsOut; From 3a107da9b305c4c36c5ed84b52a7005185d584e8 Mon Sep 17 00:00:00 2001 From: Peter Rindal Date: Tue, 24 Nov 2026 22:12:36 -0800 Subject: [PATCH 02/22] Fuse two-bit SmallFieldVole sender hashing and reduction on AVX-512 VAES --- libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp | 64 ++++++++++++++++++++- libOTe_Tests/SoftSpoken_Tests.cpp | 33 +++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) diff --git a/libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp b/libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp index 7f8a1be5..e6fdd165 100644 --- a/libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp +++ b/libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp @@ -98,6 +98,60 @@ namespace osuCrypto } + +#if defined(OC_ENABLE_VAES) && defined(__AVX512F__) && defined(__VAES__) + // Each ZMM holds the four 128-bit hashes for one two-bit VOLE. + static OC_FORCEINLINE __m512i storeTwoBitV(const __m512i a, const __m512i b, block* outV) + { + // For each VOLE, v[0] = h1 ^ h3 and v[1] = h2 ^ h3. + const auto middle = _mm512_shuffle_i32x4(a, b, _MM_SHUFFLE(2, 1, 2, 1)); + const auto last = _mm512_shuffle_i32x4(a, b, _MM_SHUFFLE(3, 3, 3, 3)); + _mm512_storeu_si512(outV, _mm512_xor_si512(middle, last)); + + // Keep h0 ^ h1 and h2 ^ h3 for the final reduction to u. + const auto even = _mm512_shuffle_i32x4(a, b, _MM_SHUFFLE(2, 0, 2, 0)); + const auto odd = _mm512_shuffle_i32x4(a, b, _MM_SHUFFLE(3, 1, 3, 1)); + return _mm512_xor_si512(even, odd); + } + + static OC_FORCEINLINE void generateEightTwoBitSenders( + const block* seeds, block index, const AES& aes, block* outU, block* outV) + { + const auto tweak = _mm512_broadcast_i32x4(index.mData); + const auto first = _mm512_xor_si512(tweak, _mm512_broadcast_i32x4(aes.mRoundKey[0].mData)); + __m512i raw[8], state[8]; +#pragma GCC unroll 8 + for (unsigned i = 0; i < 8; ++i) + { + raw[i] = _mm512_loadu_si512(seeds + 4 * i); + state[i] = _mm512_xor_si512(raw[i], first); + } +#pragma GCC unroll 9 + for (unsigned round = 1; round < 10; ++round) + { + const auto key = _mm512_broadcast_i32x4(aes.mRoundKey[round].mData); +#pragma GCC unroll 8 + for (unsigned i = 0; i < 8; ++i) + state[i] = _mm512_aesenc_epi128(state[i], key); + } + // Each output XORs two or four hashes. Both the final round key and + // the block index in the feed-forward cancel. Use the last round's + // XOR for the per-lane seed that remains. +#pragma GCC unroll 8 + for (unsigned i = 0; i < 8; ++i) + state[i] = _mm512_aesenclast_epi128(state[i], raw[i]); +#pragma GCC unroll 2 + for (unsigned i = 0; i < 8; i += 4) + { + const auto ab = storeTwoBitV(state[i], state[i + 1], outV + 2 * i); + const auto cd = storeTwoBitV(state[i + 2], state[i + 3], outV + 2 * i + 4); + const auto even = _mm512_shuffle_i32x4(ab, cd, _MM_SHUFFLE(2, 0, 2, 0)); + const auto odd = _mm512_shuffle_i32x4(ab, cd, _MM_SHUFFLE(3, 1, 3, 1)); + _mm512_storeu_si512(outU + i, _mm512_xor_si512(even, odd)); + } + } +#endif + template OC_FORCEINLINE void generateSenderImpl( const SmallFieldVoleSender& This, @@ -120,7 +174,15 @@ namespace osuCrypto UNREACHABLE(); const u64 volePerSuperBlk = superBlkSize / fieldSize; - for (u64 nVole = 0; nVole < This.mNumVoles; nVole += volePerSuperBlk) + u64 nVole = 0; +#if defined(OC_ENABLE_VAES) && defined(__AVX512F__) && defined(__VAES__) + if constexpr (fieldBitsConst == 2) + { + for (; nVole + 8 <= This.mNumVoles; nVole += 8, seeds += 32, outU += 8, outV += 16) + generateEightTwoBitSenders(seeds, blockIdxBlock, aes, outU, outV); + } +#endif + for (; nVole < This.mNumVoles; nVole += volePerSuperBlk) { // TODO: Try combining the block index into the AES key. It gives the same PRG, but // might be faster. diff --git a/libOTe_Tests/SoftSpoken_Tests.cpp b/libOTe_Tests/SoftSpoken_Tests.cpp index 1131c127..3201a5b6 100644 --- a/libOTe_Tests/SoftSpoken_Tests.cpp +++ b/libOTe_Tests/SoftSpoken_Tests.cpp @@ -30,11 +30,44 @@ namespace tests_libOTe +#ifdef ENABLE_SOFTSPOKEN_OT + static void twoBitSenderHashTest() + { + PRNG prng(block(184, 731)); + for (u64 count : {1, 2, 7, 8, 9, 15, 16, 17, 63, 64, 65}) + { + SmallFieldVoleSender sender; + sender.init(2, count, false); + std::vector seeds(4 * count); + prng.get(seeds.data(), seeds.size()); + sender.setSeed(seeds); + // Offset both outputs by a block to exercise unaligned SIMD stores. + AlignedUnVector u(sender.uPadded() + 1), v(sender.vPadded() + 1); + for (u64 index : std::array{0, 1, 1ull << 53, ~0ull}) + { + const AES aes(prng.get()); + sender.generate(index, aes, u.data() + 1, v.data() + 1); + for (u64 i = 0; i < count; ++i) + { + std::array h; + for (u64 j = 0; j < 4; ++j) + h[j] = aes.hashBlock(seeds[4 * i + j] ^ toBlock(index)); + if (u[i + 1] != (h[0] ^ h[1] ^ h[2] ^ h[3]) || + v[2 * i + 1] != (h[1] ^ h[3]) || + v[2 * i + 2] != (h[2] ^ h[3])) + throw UnitTestFail("Two-bit VOLE sender hash mismatch"); + } + } + } + } +#endif + void Vole_SoftSpokenSmall_Test(const oc::CLP& cmd) { #ifdef ENABLE_SOFTSPOKEN_OT //throw RTE_LOC; tests::xorReduction(); + twoBitSenderHashTest(); const bool print = cmd.isSet("v"); From 80d8ffb3d48a1ccc98d56df60e88367548853f95 Mon Sep 17 00:00:00 2001 From: Peter Rindal Date: Tue, 24 Nov 2026 22:30:06 -0800 Subject: [PATCH 03/22] Fuse two-bit VOLE receiver hashing, reduction and correction on AVX-512 VAES --- libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp | 135 +++++++++++++++++--- libOTe_Tests/SoftSpoken_Tests.cpp | 40 ++++++ 2 files changed, 160 insertions(+), 15 deletions(-) diff --git a/libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp b/libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp index e6fdd165..f1476092 100644 --- a/libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp +++ b/libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp @@ -114,32 +114,43 @@ namespace osuCrypto return _mm512_xor_si512(even, odd); } - static OC_FORCEINLINE void generateEightTwoBitSenders( - const block* seeds, block index, const AES& aes, block* outU, block* outV) + // All two-bit outputs XOR an even number of Davies-Meyer hashes. The + // common final-round key and index cancel. AESENCLAST folds in each seed. + template + static OC_FORCEINLINE void hashTwoBitSeeds(const block* seeds, block index, + const AES& aes, __m512i (&state)[lanes]) { + static_assert(stride == 3 || stride == 4); const auto tweak = _mm512_broadcast_i32x4(index.mData); const auto first = _mm512_xor_si512(tweak, _mm512_broadcast_i32x4(aes.mRoundKey[0].mData)); - __m512i raw[8], state[8]; -#pragma GCC unroll 8 - for (unsigned i = 0; i < 8; ++i) + __m512i raw[lanes]; +#pragma GCC unroll 9 + for (unsigned i = 0; i < lanes; ++i) { - raw[i] = _mm512_loadu_si512(seeds + 4 * i); + if constexpr (stride == 4) + raw[i] = _mm512_loadu_si512(seeds + 4 * i); + else + raw[i] = _mm512_maskz_loadu_epi64(0x3f, seeds + 3 * i); state[i] = _mm512_xor_si512(raw[i], first); } #pragma GCC unroll 9 for (unsigned round = 1; round < 10; ++round) { const auto key = _mm512_broadcast_i32x4(aes.mRoundKey[round].mData); -#pragma GCC unroll 8 - for (unsigned i = 0; i < 8; ++i) +#pragma GCC unroll 9 + for (unsigned i = 0; i < lanes; ++i) state[i] = _mm512_aesenc_epi128(state[i], key); } - // Each output XORs two or four hashes. Both the final round key and - // the block index in the feed-forward cancel. Use the last round's - // XOR for the per-lane seed that remains. -#pragma GCC unroll 8 - for (unsigned i = 0; i < 8; ++i) +#pragma GCC unroll 9 + for (unsigned i = 0; i < lanes; ++i) state[i] = _mm512_aesenclast_epi128(state[i], raw[i]); + } + + static OC_FORCEINLINE void generateEightTwoBitSenders( + const block* seeds, block index, const AES& aes, block* outU, block* outV) + { + __m512i state[8]; + hashTwoBitSeeds<4>(seeds, index, aes, state); #pragma GCC unroll 2 for (unsigned i = 0; i < 8; i += 4) { @@ -226,6 +237,67 @@ namespace osuCrypto } } +#if defined(OC_ENABLE_VAES) && defined(__AVX512F__) && defined(__VAES__) + // Three registers hold the seed triples of four consecutive VOLEs: + // [a0 a1 a2 b0], [b1 b2 c0 c1], [c2 d0 d1 d2]. + template + static OC_FORCEINLINE void storeFourTwoBitW(__m512i x, __m512i y, __m512i z, + const u8* delta, const block* correction, block* out) + { + const auto ab = _mm512_permutex2var_epi64(x, _mm512_setr_epi64(0, 1, 2, 3, 6, 7, 8, 9), y); + const auto cd = _mm512_shuffle_i32x4(y, z, _MM_SHUFFLE(2, 1, 3, 2)); + auto wAB = _mm512_xor_si512(ab, _mm512_shuffle_i32x4(x, y, _MM_SHUFFLE(1, 1, 2, 2))); + auto wCD = _mm512_xor_si512(cd, _mm512_shuffle_i32x4(z, z, _MM_SHUFFLE(3, 3, 0, 0))); + if constexpr (corrected) + { + // Each delta bit selects both 64-bit halves of one output block. + const auto bytes = _mm_loadl_epi64(reinterpret_cast(delta)); + const unsigned mask = _mm_movemask_epi8(_mm_unpacklo_epi8(bytes, bytes)); + const auto c = _mm512_loadu_si512(correction); + wAB = _mm512_mask_xor_epi64(wAB, __mmask8(mask), wAB, + _mm512_shuffle_i32x4(c, c, _MM_SHUFFLE(1, 1, 0, 0))); + wCD = _mm512_mask_xor_epi64(wCD, __mmask8(mask >> 8), wCD, + _mm512_shuffle_i32x4(c, c, _MM_SHUFFLE(3, 3, 2, 2))); + } + _mm512_storeu_si512(out, wAB); + _mm512_storeu_si512(out + 4, wCD); + } + + template + static OC_FORCEINLINE void generateTwelveTwoBitReceivers(const block* seeds, block index, + const AES& aes, const u8* delta, const block* correction, block* out) + { + __m512i state[9]; + hashTwoBitSeeds<4>(seeds, index, aes, state); +#pragma GCC unroll 3 + for (unsigned i = 0; i < 3; ++i) + storeFourTwoBitW(state[3 * i], state[3 * i + 1], state[3 * i + 2], + corrected ? delta + 8 * i : nullptr, corrected ? correction + 4 * i : nullptr, out + 8 * i); + } + + // The existing receiver generates complete groups of three, including its + // final padded outputs. Load exactly three seeds per VOLE in the remainder. + static OC_FORCEINLINE void generateThreeTwoBitReceivers(const block* seeds, block index, + const AES& aes, const u8* delta, const block* correction, block* out) + { + __m512i state[3]; + hashTwoBitSeeds<3>(seeds, index, aes, state); +#pragma GCC unroll 3 + for (unsigned i = 0; i < 3; ++i) + { + const auto h2 = _mm512_shuffle_i32x4(state[i], state[i], _MM_SHUFFLE(2, 2, 2, 2)); + auto w = _mm512_castsi512_si256(_mm512_xor_si512(state[i], h2)); + if (correction) + { + const auto masks = _mm256_set_m128i(_mm_set1_epi8(delta[2 * i + 1]), _mm_set1_epi8(delta[2 * i])); + const auto c = _mm256_broadcastsi128_si256(correction[i].mData); + w = _mm256_xor_si256(w, _mm256_and_si256(masks, c)); + } + _mm256_storeu_si256(reinterpret_cast<__m256i*>(out + 2 * i), w); + } + } +#endif + template OC_FORCEINLINE void generateReceiverImpl( const SmallFieldVoleReceiver& This, @@ -263,8 +335,34 @@ namespace osuCrypto constexpr u64 aesPerSuperBlk = aesPerVole * volePerSuperBlk; constexpr u64 fieldsPerSuperBlk = volePerSuperBlk << fieldBits_; - for (u64 nVole = 0; nVole < This.mNumVoles; nVole += volePerSuperBlk, - deltaPtr += fieldBits * volePerSuperBlk) + u64 nVole = 0; +#if defined(OC_ENABLE_VAES) && defined(__AVX512F__) && defined(__VAES__) + if constexpr (fieldBitsConst == 2) + { + for (; nVole + 12 <= This.mNumVoles; nVole += 12, seeds += 36, outW += 24) + { + if (correctionPresent) + { + generateTwelveTwoBitReceivers(seeds, blockIdxBlock, aes, deltaPtr, correction, outW); + correction += 12; + deltaPtr += 24; + } + else + generateTwelveTwoBitReceivers(seeds, blockIdxBlock, aes, deltaPtr, nullptr, outW); + } + for (; nVole < This.mNumVoles; nVole += 3, seeds += 9, outW += 6) + { + generateThreeTwoBitReceivers(seeds, blockIdxBlock, aes, deltaPtr, correction, outW); + if (correctionPresent) + { + correction += 3; + deltaPtr += 6; + } + } + return; + } +#endif + for (; nVole < This.mNumVoles; nVole += volePerSuperBlk) { block input[aesPerSuperBlk], hashes[aesPerSuperBlk], xorHashes[fieldsPerSuperBlk]; for (u64 i = 0; i < aesPerSuperBlk; ++i, ++seeds) @@ -291,7 +389,10 @@ namespace osuCrypto *outW = xorHashes[i * fieldSize + j + 1]; if (correctionPresent) + { correction += volePerSuperBlk; + deltaPtr += fieldBits * volePerSuperBlk; + } } } else @@ -404,6 +505,7 @@ namespace osuCrypto mDeltaUnpacked.resize(wPadded()); for (u64 i = 0; i < mDelta.size(); ++i) mDeltaUnpacked[i] = -(u8)mDelta[i]; + std::fill(mDeltaUnpacked.begin() + mDelta.size(), mDeltaUnpacked.end(), u8{0}); } void SmallFieldVoleReceiver::setSeeds(span seeds_) @@ -415,6 +517,9 @@ namespace osuCrypto assert(mNumVoles <= mNumVolesPadded); mSeeds.resize(mNumVolesPadded * (fieldSize() - 1)); std::copy(seeds_.begin(), seeds_.end(), mSeeds.data()); + // Packed generation reads complete seed groups, including a partial final group. + if (mFieldBits <= superBlkShift) + std::fill(mSeeds.begin() + numSeeds, mSeeds.end(), ZeroBlock); mConsistencyFailed = false; } diff --git a/libOTe_Tests/SoftSpoken_Tests.cpp b/libOTe_Tests/SoftSpoken_Tests.cpp index 3201a5b6..3252166b 100644 --- a/libOTe_Tests/SoftSpoken_Tests.cpp +++ b/libOTe_Tests/SoftSpoken_Tests.cpp @@ -60,6 +60,45 @@ namespace tests_libOTe } } } + static void twoBitReceiverHashTest() + { + PRNG prng(block(137, 54)); + for (u64 count : {1, 3, 11, 12, 13, 23, 24, 25, 63, 64, 65}) + { + SmallFieldVoleReceiver receiver; + receiver.init(2, count, false); + std::vector base(2 * count), seeds(3 * count); + BitVector delta(2 * count); + delta.randomize(prng); + prng.get(base.data(), base.size()); + prng.get(seeds.data(), seeds.size()); + AlignedUnVector w(receiver.wPadded() + 1), c(receiver.uPadded() + 1); + prng.get(c.data(), c.size()); + for (bool corrected : {false, true}) + { + if (corrected) + receiver.setBaseOts(base, delta); + receiver.setSeeds(seeds); + for (u64 index : std::array{0, 1, 1ull << 53, ~0ull}) + { + const AES aes(prng.get()); + receiver.generate(index, aes, w.data() + 1, corrected ? c.data() + 1 : nullptr); + for (u64 i = 0; i < count; ++i) + { + const auto h2 = aes.hashBlock(seeds[3 * i + 2] ^ toBlock(index)); + for (u64 j = 0; j < 2; ++j) + { + auto expected = aes.hashBlock(seeds[3 * i + j] ^ toBlock(index)) ^ h2; + if (corrected && delta[2 * i + j]) + expected ^= c[i + 1]; + if (expected != w[2 * i + j + 1]) + throw UnitTestFail("Two-bit VOLE receiver hash mismatch"); + } + } + } + } + } + } #endif void Vole_SoftSpokenSmall_Test(const oc::CLP& cmd) @@ -68,6 +107,7 @@ namespace tests_libOTe //throw RTE_LOC; tests::xorReduction(); twoBitSenderHashTest(); + twoBitReceiverHashTest(); const bool print = cmd.isSet("v"); From fd2170aad8d19a91224f22ac1db6f250221d20ab Mon Sep 17 00:00:00 2001 From: Peter Rindal Date: Tue, 24 Nov 2026 22:48:52 -0800 Subject: [PATCH 04/22] Pack live regular PPRF sender leaves and mask block messages directly --- libOTe/Tools/Pprf/RegularPprf.h | 46 ++++++++++++++++++++++++++++----- libOTe_Tests/Pprf_Tests.cpp | 3 +++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/libOTe/Tools/Pprf/RegularPprf.h b/libOTe/Tools/Pprf/RegularPprf.h index 24da969f..5ce06768 100644 --- a/libOTe/Tools/Pprf/RegularPprf.h +++ b/libOTe/Tools/Pprf/RegularPprf.h @@ -11,6 +11,7 @@ #include "cryptoTools/Crypto/PRNG.h" #include "libOTe/Tools/Coproto.h" #include +#include #include "libOTe/Tools/CoeffCtx.h" #include "PprfUtil.h" @@ -744,7 +745,9 @@ namespace osuCrypto { auto parents = levels.back(); auto generateGeneric = [&] { - std::array hashed; + alignas(64) std::array hashed; + const u64 fullLanes = mDomain / subtreeDomain; + const u64 partialSize = mDomain % subtreeDomain; for (u64 parentIdx = 0; parentIdx < parents.size(); ++parentIdx) { for (u64 side = 0; side < 2; ++side) @@ -752,11 +755,23 @@ namespace osuCrypto gGgmAes[side].hashBlocks<8>( parents[parentIdx].data(), hashed.data()); const auto localLeaf = 2 * parentIdx + side; - REGULAR_PPRF_SIMD8(lane, { - addLeaf( - side, hashed[lane], - lane * subtreeDomain + localLeaf); - }); + if constexpr (std::is_same_v && Format == PprfOutputFormat::ByPhysicalIndex) + { + // Physical order visits one local leaf across eight subtrees. + // All hashes enter the sum, but only the live prefix is emitted. + auto& sum = leafSums[side]; + REGULAR_PPRF_SIMD8(lane, { ctx.plus(sum, sum, hashed[lane]); }); + const u64 live = fullLanes + (localLeaf < partialSize); + for (u64 lane = 0; lane < live; ++lane) + ctx.copy(output[treeIdx * mDomain + nativeLeaf + lane], hashed[lane]); + nativeLeaf += live; + } + else + { + REGULAR_PPRF_SIMD8(lane, { + addLeaf(side, hashed[lane], lane * subtreeDomain + localLeaf); + }); + } } } }; @@ -842,6 +857,25 @@ namespace osuCrypto 0, count * ctx.template byteSize()); leafMsgs = leafMsgs.subspan(msg.size()); ctx.serialize(leafOts.begin(), leafOts.begin() + count, msg.begin()); + if (msg.size() == sizeof(block) || msg.size() == 2 * sizeof(block)) + { + // These are the first one or two counter blocks of the OT masker's stream. + const AES aes(mBaseOTs(treeIdx, 0)[1 ^ choice]); + const block counters[2]{block(0, 0), block(0, 1)}; + block pads[2]; + if (msg.size() == 2 * sizeof(block)) + aes.ecbEncBlocks<2>(counters, pads); + else + pads[0] = aes.ecbEncBlock(counters[0]); + for (u64 i = 0; i < msg.size() / sizeof(block); ++i) + { + block value; + std::memcpy(&value, msg.data() + i * sizeof(block), sizeof(block)); + value ^= pads[i]; + std::memcpy(msg.data() + i * sizeof(block), &value, sizeof(block)); + } + continue; + } otMasker.SetSeed( mBaseOTs(treeIdx, 0)[1 ^ choice], divCeil(msg.size(), sizeof(block))); diff --git a/libOTe_Tests/Pprf_Tests.cpp b/libOTe_Tests/Pprf_Tests.cpp index 0ebdd045..c228489b 100644 --- a/libOTe_Tests/Pprf_Tests.cpp +++ b/libOTe_Tests/Pprf_Tests.cpp @@ -715,6 +715,9 @@ void RegularPprf_PhysicalIndex_test(const CLP& cmd) { auto f = PprfOutputFormat::ByPhysicalIndex; auto v = cmd.isSet("v"); + // The physical block path also supports coefficient addition other than XOR. + for (bool program : {false, true}) + RegularPprf_test_impl(210, 9, program, f, false, v); for (auto d : { 32,3242 }) for (auto n : { 8, 128 }) for (auto p : { true, false }) for (auto e : { true, false }) { RegularPprf_test_impl(d, n, p, f, e, v); From 675f9cccfeb6c6866b1c67cedf37bec7204e2454 Mon Sep 17 00:00:00 2001 From: Peter Rindal Date: Tue, 24 Nov 2026 23:01:41 -0800 Subject: [PATCH 05/22] Stream compact regular PPRF leaves and unmask receiver corrections directly --- libOTe/Tools/Pprf/RegularPprf.h | 126 ++++++++++++++++++++------------ 1 file changed, 78 insertions(+), 48 deletions(-) diff --git a/libOTe/Tools/Pprf/RegularPprf.h b/libOTe/Tools/Pprf/RegularPprf.h index 5ce06768..23075ba9 100644 --- a/libOTe/Tools/Pprf/RegularPprf.h +++ b/libOTe/Tools/Pprf/RegularPprf.h @@ -401,6 +401,64 @@ namespace osuCrypto #undef REGULAR_PPRF_STORE_HASH } + + template + OC_FORCEINLINE u64 hashPhysicalBlockLeaves(span> parents, + u64 domain, Iter output, Sums& leafSums, CoeffCtx& ctx) + { + const u64 subtreeDomain = 2 * parents.size(); + const u64 fullLanes = domain / subtreeDomain; + const u64 partialSize = domain % subtreeDomain; + alignas(64) std::array hashed; + u64 written = 0; + for (u64 p = 0; p < parents.size(); ++p) + { + for (u64 side = 0; side < 2; ++side) + { + gGgmAes[side].hashBlocks<8>(parents[p].data(), hashed.data()); + // All eight subtrees enter the correction sum. Only the live + // prefix of this row belongs in the compact physical output. + auto& sum = leafSums[side]; + REGULAR_PPRF_SIMD8(lane, { ctx.plus(sum, sum, hashed[lane]); }); + const u64 live = fullLanes + (2 * p + side < partialSize); + // The domain fills more than half of its padded tree, so each + // row has at least four live lanes. Keep these short stores inline. + assert(live >= 4 && live <= 8); + REGULAR_PPRF_SIMD8(lane, { + if (lane < 4 || lane < live) + ctx.copy(output[written + lane], hashed[lane]); + }); + written += live; + } + } + return written; + } + + OC_FORCEINLINE void xorLeafMask(span msg, block key, PRNG& otMasker) + { + if (msg.size() == sizeof(block) || msg.size() == 2 * sizeof(block)) + { + // These are the first one or two counter blocks of the OT masker's stream. + const AES aes(key); + const block counters[2]{block(0, 0), block(0, 1)}; + block pads[2]; + if (msg.size() == 2 * sizeof(block)) + aes.ecbEncBlocks<2>(counters, pads); + else + pads[0] = aes.ecbEncBlock(counters[0]); + for (u64 i = 0; i < msg.size() / sizeof(block); ++i) + { + block value; + std::memcpy(&value, msg.data() + i * sizeof(block), sizeof(block)); + value ^= pads[i]; + std::memcpy(msg.data() + i * sizeof(block), &value, sizeof(block)); + } + return; + } + otMasker.SetSeed(key, divCeil(msg.size(), sizeof(block))); + for (u64 i = 0; i < msg.size(); ++i) + msg[i] ^= otMasker.get(); + } } @@ -745,9 +803,13 @@ namespace osuCrypto { auto parents = levels.back(); auto generateGeneric = [&] { + if constexpr (std::is_same_v && Format == PprfOutputFormat::ByPhysicalIndex) + { + nativeLeaf = pprf::hashPhysicalBlockLeaves(parents, mDomain, + output.begin() + treeIdx * mDomain, leafSums, ctx); + return; + } alignas(64) std::array hashed; - const u64 fullLanes = mDomain / subtreeDomain; - const u64 partialSize = mDomain % subtreeDomain; for (u64 parentIdx = 0; parentIdx < parents.size(); ++parentIdx) { for (u64 side = 0; side < 2; ++side) @@ -755,23 +817,11 @@ namespace osuCrypto gGgmAes[side].hashBlocks<8>( parents[parentIdx].data(), hashed.data()); const auto localLeaf = 2 * parentIdx + side; - if constexpr (std::is_same_v && Format == PprfOutputFormat::ByPhysicalIndex) - { - // Physical order visits one local leaf across eight subtrees. - // All hashes enter the sum, but only the live prefix is emitted. - auto& sum = leafSums[side]; - REGULAR_PPRF_SIMD8(lane, { ctx.plus(sum, sum, hashed[lane]); }); - const u64 live = fullLanes + (localLeaf < partialSize); - for (u64 lane = 0; lane < live; ++lane) - ctx.copy(output[treeIdx * mDomain + nativeLeaf + lane], hashed[lane]); - nativeLeaf += live; - } - else - { - REGULAR_PPRF_SIMD8(lane, { - addLeaf(side, hashed[lane], lane * subtreeDomain + localLeaf); - }); - } + REGULAR_PPRF_SIMD8(lane, { + addLeaf( + side, hashed[lane], + lane * subtreeDomain + localLeaf); + }); } } }; @@ -857,30 +907,7 @@ namespace osuCrypto 0, count * ctx.template byteSize()); leafMsgs = leafMsgs.subspan(msg.size()); ctx.serialize(leafOts.begin(), leafOts.begin() + count, msg.begin()); - if (msg.size() == sizeof(block) || msg.size() == 2 * sizeof(block)) - { - // These are the first one or two counter blocks of the OT masker's stream. - const AES aes(mBaseOTs(treeIdx, 0)[1 ^ choice]); - const block counters[2]{block(0, 0), block(0, 1)}; - block pads[2]; - if (msg.size() == 2 * sizeof(block)) - aes.ecbEncBlocks<2>(counters, pads); - else - pads[0] = aes.ecbEncBlock(counters[0]); - for (u64 i = 0; i < msg.size() / sizeof(block); ++i) - { - block value; - std::memcpy(&value, msg.data() + i * sizeof(block), sizeof(block)); - value ^= pads[i]; - std::memcpy(msg.data() + i * sizeof(block), &value, sizeof(block)); - } - continue; - } - otMasker.SetSeed( - mBaseOTs(treeIdx, 0)[1 ^ choice], - divCeil(msg.size(), sizeof(block))); - for (u64 i = 0; i < msg.size(); ++i) - msg[i] ^= otMasker.get(); + pprf::xorLeafMask(msg, mBaseOTs(treeIdx, 0)[1 ^ choice], otMasker); } assert(leafMsgs.empty()); } @@ -1355,6 +1382,12 @@ namespace osuCrypto { auto parents = levels.back(); auto generateGeneric = [&] { + if constexpr (std::is_same_v && Format == PprfOutputFormat::ByPhysicalIndex) + { + nativeLeaf = pprf::hashPhysicalBlockLeaves(parents, mDomain, + output.begin() + treeIdx * mDomain, leafSums, ctx); + return; + } std::array hashed; for (u64 parentIdx = 0; parentIdx < parents.size(); ++parentIdx) { @@ -1432,11 +1465,8 @@ namespace osuCrypto const auto valueCount = 1 + u64(programPuncturedPoint); const auto valueBytes = valueCount * ctx.template byteSize(); auto selected = leafMsgs.subspan(inactiveSide * valueBytes, valueBytes); - PRNG otMasker( - mBaseOTs(treeIdx, 0), - divCeil(selected.size(), sizeof(block))); - for (u64 i = 0; i < selected.size(); ++i) - selected[i] ^= otMasker.get(); + PRNG otMasker; + pprf::xorLeafMask(selected, mBaseOTs(treeIdx, 0), otMasker); auto leafOts = ctx.template makeVec(valueCount); ctx.deserialize(selected.begin(), selected.end(), leafOts.begin()); From dc53c8a1b19853b2aca080250c2316e4a12a367f Mon Sep 17 00:00:00 2001 From: Peter Rindal Date: Tue, 24 Nov 2026 23:20:23 -0800 Subject: [PATCH 06/22] Interleave compact XOR PPRF leaf hashes with AVX-512 VAES --- libOTe/Tools/Pprf/RegularPprf.h | 80 ++++++++++++++++++++++++++++++--- libOTe_Tests/Pprf_Tests.cpp | 39 ++++++++++++++++ 2 files changed, 113 insertions(+), 6 deletions(-) diff --git a/libOTe/Tools/Pprf/RegularPprf.h b/libOTe/Tools/Pprf/RegularPprf.h index 23075ba9..9ce19339 100644 --- a/libOTe/Tools/Pprf/RegularPprf.h +++ b/libOTe/Tools/Pprf/RegularPprf.h @@ -12,6 +12,8 @@ #include "libOTe/Tools/Coproto.h" #include #include +#include +#include #include "libOTe/Tools/CoeffCtx.h" #include "PprfUtil.h" @@ -199,15 +201,13 @@ namespace osuCrypto _mm512_storeu_si512(sum1, s1[0]); _mm512_storeu_si512(sum1 + 4, s1[1]); } - template - OC_FORCEINLINE void leafStep(const ZmmKey& k0, const ZmmKey& k1, const block* in, block* o, - __m512i (&s0)[2], __m512i (&s1)[2]) + // Each input is four independent parents. Advance both leaf keys together. + template + OC_FORCEINLINE void hashLeafChains(const ZmmKey& k0, const ZmmKey& k1, + const __m512i (&x)[C], __m512i (&a)[C], __m512i (&b)[C]) { - constexpr u64 C = 2 * NP; - __m512i x[C], a[C], b[C]; for (u64 c = 0; c < C; ++c) { - x[c] = _mm512_loadu_si512(in + 4 * c); a[c] = _mm512_xor_si512(x[c], k0.k[0]); b[c] = _mm512_xor_si512(x[c], k1.k[0]); } @@ -221,6 +221,20 @@ namespace osuCrypto { a[c] = _mm512_xor_si512(_mm512_aesenclast_epi128(a[c], k0.k[10]), x[c]); b[c] = _mm512_xor_si512(_mm512_aesenclast_epi128(b[c], k1.k[10]), x[c]); + } + } + + template + OC_FORCEINLINE void leafStep(const ZmmKey& k0, const ZmmKey& k1, const block* in, block* o, + __m512i (&s0)[2], __m512i (&s1)[2]) + { + constexpr u64 C = 2 * NP; + __m512i x[C], a[C], b[C]; + for (u64 c = 0; c < C; ++c) + x[c] = _mm512_loadu_si512(in + 4 * c); + hashLeafChains(k0, k1, x, a, b); + for (u64 c = 0; c < C; ++c) + { const u64 q = c / 2, v = c % 2; _mm512_storeu_si512(o + 16 * q + 4 * v, a[c]); _mm512_storeu_si512(o + 16 * q + 8 + 4 * v, b[c]); @@ -243,6 +257,40 @@ namespace osuCrypto _mm512_storeu_si512(sum1, s1[0]); _mm512_storeu_si512(sum1 + 4, s1[1]); } + // Both children of a parent have the same live width because the domain is even. + // Hash all eight lanes for each correction, then store only the live leaves. + template + OC_FORCEINLINE void compactLeafStep(const ZmmKey& k0, const ZmmKey& k1, + const AlignedArray* parents, block*& out, u64 firstParent, + u64 fullLanes, u64 extraParents, __m512i& sum0, __m512i& sum1) + { + __m512i x[2 * NP], left[2 * NP], right[2 * NP]; + for (u64 c = 0; c < 2 * NP; ++c) + x[c] = _mm512_loadu_si512(parents[c / 2].data() + 4 * (c % 2)); + hashLeafChains(k0, k1, x, left, right); + for (u64 q = 0; q < NP; ++q) + { + const u64 live = fullLanes + (firstParent + q < extraParents); + assert(live >= 4 && live <= 8); + const __mmask8 mask = static_cast<__mmask8>((1u << (2 * (live - 4))) - 1); + sum0 = _mm512_ternarylogic_epi64(sum0, left[2 * q], left[2 * q + 1], 0x96); + sum1 = _mm512_ternarylogic_epi64(sum1, right[2 * q], right[2 * q + 1], 0x96); + _mm512_storeu_si512(out, left[2 * q]); + _mm512_mask_storeu_epi64(out + 4, mask, left[2 * q + 1]); + out += live; + _mm512_storeu_si512(out, right[2 * q]); + _mm512_mask_storeu_epi64(out + 4, mask, right[2 * q + 1]); + out += live; + } + } + + OC_FORCEINLINE block fold4(__m512i sum) + { + alignas(64) block lanes[4]; + _mm512_store_si512(lanes, sum); + return (lanes[0] ^ lanes[1]) ^ (lanes[2] ^ lanes[3]); + } + using Key = ZmmKey; #else using Key = YmmKey; @@ -409,6 +457,26 @@ namespace osuCrypto const u64 subtreeDomain = 2 * parents.size(); const u64 fullLanes = domain / subtreeDomain; const u64 partialSize = domain % subtreeDomain; +#if defined(OC_ENABLE_VAES) && defined(ENABLE_AVX512) + // A context can opt in when block copy is assignment and addition is bitwise XOR. + if constexpr (std::contiguous_iterator && + requires { requires CoeffCtx::blockCopyIsTrivialAndAdditionIsXor; }) + { + const pprfVaes::ZmmKey k0(gGgmAes[0]), k1(gGgmAes[1]); + block* dest = std::to_address(output); + __m512i sum0 = _mm512_setzero_si512(), sum1 = _mm512_setzero_si512(); + u64 p = 0; + for (; p + 2 <= parents.size(); p += 2) + pprfVaes::compactLeafStep<2>(k0, k1, parents.data() + p, dest, + p, fullLanes, partialSize / 2, sum0, sum1); + if (p < parents.size()) + pprfVaes::compactLeafStep<1>(k0, k1, parents.data() + p, dest, + p, fullLanes, partialSize / 2, sum0, sum1); + leafSums[0] ^= pprfVaes::fold4(sum0); + leafSums[1] ^= pprfVaes::fold4(sum1); + return dest - std::to_address(output); + } +#endif alignas(64) std::array hashed; u64 written = 0; for (u64 p = 0; p < parents.size(); ++p) diff --git a/libOTe_Tests/Pprf_Tests.cpp b/libOTe_Tests/Pprf_Tests.cpp index c228489b..a9f0b246 100644 --- a/libOTe_Tests/Pprf_Tests.cpp +++ b/libOTe_Tests/Pprf_Tests.cpp @@ -711,8 +711,47 @@ void HalfTreePprf_test(const oc::CLP& cmd) // verbose); //} +namespace +{ + struct RawXorTestCtx : CoeffCtxGF2 + { + static constexpr bool blockCopyIsTrivialAndAdditionIsXor = true; + }; + + void checkCompactXorLeaves() + { + PRNG prng(CCBlock); + RawXorTestCtx ctx; + for (u64 domain = 10; domain <= 256; domain += 2) + { + AlignedUnVector> parents(pprf::paddedDomain(domain) / 16); + std::vector output(domain + 2, CCBlock); + std::array sums{prng.get(), prng.get()}; + auto expectedSums = sums; + std::vector expected = output; + u64 next = 1; + for (u64 p = 0; p < parents.size(); ++p) + { + prng.get(parents[p].data(), 8); + for (u64 side = 0; side < 2; ++side) + for (u64 lane = 0; lane < 8; ++lane) + { + const block h = gGgmAes[side].hashBlock(parents[p][lane]); + expectedSums[side] ^= h; + if (lane * (2 * parents.size()) + 2 * p + side < domain) + expected[next++] = h; + } + } + const auto written = pprf::hashPhysicalBlockLeaves(parents, domain, output.data() + 1, sums, ctx); + if (written != domain || sums != expectedSums || output != expected) + throw std::runtime_error("Compact XOR leaves disagree with the scalar hashes"); + } + } +} + void RegularPprf_PhysicalIndex_test(const CLP& cmd) { + checkCompactXorLeaves(); auto f = PprfOutputFormat::ByPhysicalIndex; auto v = cmd.isSet("v"); // The physical block path also supports coefficient addition other than XOR. From 1b2d2402250dc06f8da000af9c33be185519b079 Mon Sep 17 00:00:00 2001 From: Peter Rindal Date: Wed, 25 Nov 2026 08:29:52 -0800 Subject: [PATCH 07/22] Recover the regular PPRF receiver root directly and keep block leaf scratch local --- libOTe/Tools/Pprf/RegularPprf.h | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/libOTe/Tools/Pprf/RegularPprf.h b/libOTe/Tools/Pprf/RegularPprf.h index 9ce19339..bdf062b5 100644 --- a/libOTe/Tools/Pprf/RegularPprf.h +++ b/libOTe/Tools/Pprf/RegularPprf.h @@ -1309,8 +1309,17 @@ namespace osuCrypto zeroAes.add_epi64(ZeroBlock) }; + // The root is the missing parent. Its two generated children cancel + // the inactive offsets, leaving the OT correction as the sibling seed. + if (mDepth > 1) + { + const auto sibling = (point >> (mDepth - 1)) ^ 1; + current[sibling] = (*theirSumsIter)[sibling] ^ mBaseOTs(treeIdx, mDepth - 1); + ++theirSumsIter; + } + const auto topInternalDepth = std::min(3, mDepth - 1); - for (u64 d = 0; d < topInternalDepth; ++d) + for (u64 d = 1; d < topInternalDepth; ++d) { block leftAccumulator = inactiveInternal[0]; block rightAccumulator = inactiveInternal[1]; @@ -1397,8 +1406,18 @@ namespace osuCrypto } assert(theirSumsIter == theirSums.end()); - auto leafSums = ctx.template makeVec(2); - auto zero = ctx.template makeVec(1); + // Standard block vectors can use fixed, aligned storage for these + // one- and two-element buffers. Preserve context-owned storage otherwise. + auto leafBuffer = [&](span storage) { + if constexpr (std::is_same_v && std::is_same_v>) + return storage; + else + return ctx.template makeVec(storage.size()); + }; + AlignedArray sumStorage, otStorage; + AlignedArray zeroStorage; + auto leafSums = leafBuffer(sumStorage); + auto zero = leafBuffer(zeroStorage); ctx.zero(zero.begin(), zero.end()); for (u64 side = 0; side < 2; ++side) { @@ -1536,7 +1555,7 @@ namespace osuCrypto PRNG otMasker; pprf::xorLeafMask(selected, mBaseOTs(treeIdx, 0), otMasker); - auto leafOts = ctx.template makeVec(valueCount); + auto leafOts = leafBuffer({ otStorage.data(), valueCount }); ctx.deserialize(selected.begin(), selected.end(), leafOts.begin()); if (programPuncturedPoint) { From f7e893c662ddf14db876e0a5b5b077a936a868fc Mon Sep 17 00:00:00 2001 From: Peter Rindal Date: Wed, 25 Nov 2026 09:04:13 -0800 Subject: [PATCH 08/22] Pair regular PPRF sender correction masks with VAES --- libOTe/Tools/Pprf/RegularPprf.h | 138 ++++++++++++++++++++++++++------ libOTe_Tests/Pprf_Tests.cpp | 20 +++++ 2 files changed, 134 insertions(+), 24 deletions(-) diff --git a/libOTe/Tools/Pprf/RegularPprf.h b/libOTe/Tools/Pprf/RegularPprf.h index bdf062b5..01f20e33 100644 --- a/libOTe/Tools/Pprf/RegularPprf.h +++ b/libOTe/Tools/Pprf/RegularPprf.h @@ -10,6 +10,7 @@ #include "cryptoTools/Common/Range.h" #include "cryptoTools/Crypto/PRNG.h" #include "libOTe/Tools/Coproto.h" +#include "libOTe/Tools/CompilerHints.h" #include #include #include @@ -291,11 +292,82 @@ namespace osuCrypto return (lanes[0] ^ lanes[1]) ^ (lanes[2] ^ lanes[3]); } + template + OC_FORCEINLINE decltype(auto) compactLeafKey() + { + if constexpr (Cache) + { + static const ZmmKey key(gGgmAes[Side]); + return (key); + } + else + return ZmmKey(gGgmAes[Side]); + } + using Key = ZmmKey; #else using Key = YmmKey; #endif + template + OC_FORCEINLINE void leafMaskRound(int rc, __m256i& key, __m256i& pad0, __m256i& pad1) + { + // Repeat RotWord(w3) in every column. ShiftRows then preserves + // the repeated word while AESENCLAST applies the AES S-box. + const auto word = _mm256_shuffle_epi8(key, _mm256_set1_epi32(0x0c0f0e0d)); + const auto subWord = _mm256_aesenclast_epi128(word, _mm256_set1_epi32(rc)); + // Inclusive XOR prefix of the four key words, then add the schedule word. + key = _mm256_xor_si256(key, _mm256_slli_si256(key, 4)); + key = _mm256_xor_si256(subWord, _mm256_xor_si256(key, _mm256_slli_si256(key, 8))); + if constexpr (Last) + { + pad0 = _mm256_aesenclast_epi128(pad0, key); + if constexpr (TwoBlocks) + pad1 = _mm256_aesenclast_epi128(pad1, key); + } + else + { + pad0 = _mm256_aesenc_epi128(pad0, key); + if constexpr (TwoBlocks) + pad1 = _mm256_aesenc_epi128(pad1, key); + } + } + + // One OT mask key per 128-bit lane. Encrypt counters 0 and, for a + // two-block message, 1 while advancing both AES-128 key schedules. + template + OC_FORCEINLINE void xorLeafMaskPair(u8* messages, const std::array& keys) + { + auto key = _mm256_loadu_si256(reinterpret_cast(keys.data())); + auto pad0 = key; + auto pad1 = _mm256_xor_si256(key, _mm256_setr_epi64x(1, 0, 1, 0)); + leafMaskRound(0x01, key, pad0, pad1); + leafMaskRound(0x02, key, pad0, pad1); + leafMaskRound(0x04, key, pad0, pad1); + leafMaskRound(0x08, key, pad0, pad1); + leafMaskRound(0x10, key, pad0, pad1); + leafMaskRound(0x20, key, pad0, pad1); + leafMaskRound(0x40, key, pad0, pad1); + leafMaskRound(0x80, key, pad0, pad1); + leafMaskRound(0x1b, key, pad0, pad1); + leafMaskRound(0x36, key, pad0, pad1); + + // Choice zero uses key one. Store counters in order within each choice. + auto dest = reinterpret_cast<__m256i*>(messages); + if constexpr (TwoBlocks) + { + const auto first = _mm256_permute2x128_si256(pad0, pad1, 0x31); + const auto second = _mm256_permute2x128_si256(pad0, pad1, 0x20); + _mm256_storeu_si256(dest, _mm256_xor_si256(_mm256_loadu_si256(dest), first)); + _mm256_storeu_si256(dest + 1, _mm256_xor_si256(_mm256_loadu_si256(dest + 1), second)); + } + else + { + const auto pads = _mm256_permute2x128_si256(pad0, pad0, 0x01); + _mm256_storeu_si256(dest, _mm256_xor_si256(_mm256_loadu_si256(dest), pads)); + } + } + // The kernels keep one sum per lane (the 8 subtrees of one tree); the // tree's level sum is their XOR. OC_FORCEINLINE block fold8(const block* s) @@ -450,7 +522,7 @@ namespace osuCrypto #undef REGULAR_PPRF_STORE_HASH } - template + template OC_FORCEINLINE u64 hashPhysicalBlockLeaves(span> parents, u64 domain, Iter output, Sums& leafSums, CoeffCtx& ctx) { @@ -462,7 +534,8 @@ namespace osuCrypto if constexpr (std::contiguous_iterator && requires { requires CoeffCtx::blockCopyIsTrivialAndAdditionIsXor; }) { - const pprfVaes::ZmmKey k0(gGgmAes[0]), k1(gGgmAes[1]); + const auto& k0 = pprfVaes::compactLeafKey<0, CacheLeafKeys>(); + const auto& k1 = pprfVaes::compactLeafKey<1, CacheLeafKeys>(); block* dest = std::to_address(output); __m512i sum0 = _mm512_setzero_si512(), sum1 = _mm512_setzero_si512(); u64 p = 0; @@ -502,6 +575,16 @@ namespace osuCrypto return written; } + template + OC_FORCEINLINE auto leafBuffer(CoeffCtx& ctx, span storage) + { + using VecF = typename CoeffCtx::template Vec; + if constexpr (std::is_same_v && std::is_same_v>) + return storage; + else + return ctx.template makeVec(storage.size()); + } + OC_FORCEINLINE void xorLeafMask(span msg, block key, PRNG& otMasker) { if (msg.size() == sizeof(block) || msg.size() == 2 * sizeof(block)) @@ -527,6 +610,20 @@ namespace osuCrypto for (u64 i = 0; i < msg.size(); ++i) msg[i] ^= otMasker.get(); } + + OC_FORCEINLINE void xorSenderLeafMasks(span msgs, u64 messageBytes, const std::array& keys) + { + assert(msgs.size() == 2 * messageBytes); +#ifdef OC_ENABLE_VAES + if (messageBytes == sizeof(block)) + return pprfVaes::xorLeafMaskPair(msgs.data(), keys); + if (messageBytes == 2 * sizeof(block)) + return pprfVaes::xorLeafMaskPair(msgs.data(), keys); +#endif + PRNG otMasker; + xorLeafMask(msgs.subspan(0, messageBytes), keys[1], otMasker); + xorLeafMask(msgs.subspan(messageBytes, messageBytes), keys[0], otMasker); + } } @@ -739,8 +836,9 @@ namespace osuCrypto mPntCount = 0; } + // Keep the aligned tree and leaf scratch out of the calling coroutine. template - void expandOne( + OC_NOINLINE void expandOne( block root, u64 treeIdx, bool programPuncturedPoint, @@ -829,7 +927,8 @@ namespace osuCrypto } assert(encSumIter == encSums.end()); - auto leafSums = ctx.template makeVec(2); + AlignedArray sumStorage; + auto leafSums = pprf::leafBuffer(ctx, sumStorage); ctx.zero(leafSums.begin(), leafSums.end()); const auto padded = pprf::paddedDomain(mDomain); const auto subtreeDomain = mDepth > 3 ? padded / 8 : padded; @@ -873,7 +972,8 @@ namespace osuCrypto auto generateGeneric = [&] { if constexpr (std::is_same_v && Format == PprfOutputFormat::ByPhysicalIndex) { - nativeLeaf = pprf::hashPhysicalBlockLeaves(parents, mDomain, + // Reuse the public broadcast schedules across sender trees. + nativeLeaf = pprf::hashPhysicalBlockLeaves(parents, mDomain, output.begin() + treeIdx * mDomain, leafSums, ctx); return; } @@ -948,8 +1048,10 @@ namespace osuCrypto } assert(nativeLeaf == mDomain); - auto leafOts = ctx.template makeVec(2); - PRNG otMasker; + AlignedArray otStorage; + auto leafOts = pprf::leafBuffer(ctx, otStorage); + const auto count = 1 + u64(programPuncturedPoint); + const auto messageBytes = count * ctx.template byteSize(); for (u64 choice = 0; choice < 2; ++choice) { if (programPuncturedPoint) @@ -970,14 +1072,10 @@ namespace osuCrypto ctx.copy(leafOts[0], leafSums[choice]); } - const auto count = 1 + u64(programPuncturedPoint); - auto msg = leafMsgs.subspan( - 0, count * ctx.template byteSize()); - leafMsgs = leafMsgs.subspan(msg.size()); + auto msg = leafMsgs.subspan(choice * messageBytes, messageBytes); ctx.serialize(leafOts.begin(), leafOts.begin() + count, msg.begin()); - pprf::xorLeafMask(msg, mBaseOTs(treeIdx, 0)[1 ^ choice], otMasker); } - assert(leafMsgs.empty()); + pprf::xorSenderLeafMasks(leafMsgs, messageBytes, mBaseOTs(treeIdx, 0)); } @@ -1406,18 +1504,10 @@ namespace osuCrypto } assert(theirSumsIter == theirSums.end()); - // Standard block vectors can use fixed, aligned storage for these - // one- and two-element buffers. Preserve context-owned storage otherwise. - auto leafBuffer = [&](span storage) { - if constexpr (std::is_same_v && std::is_same_v>) - return storage; - else - return ctx.template makeVec(storage.size()); - }; AlignedArray sumStorage, otStorage; AlignedArray zeroStorage; - auto leafSums = leafBuffer(sumStorage); - auto zero = leafBuffer(zeroStorage); + auto leafSums = pprf::leafBuffer(ctx, sumStorage); + auto zero = pprf::leafBuffer(ctx, zeroStorage); ctx.zero(zero.begin(), zero.end()); for (u64 side = 0; side < 2; ++side) { @@ -1555,7 +1645,7 @@ namespace osuCrypto PRNG otMasker; pprf::xorLeafMask(selected, mBaseOTs(treeIdx, 0), otMasker); - auto leafOts = leafBuffer({ otStorage.data(), valueCount }); + auto leafOts = pprf::leafBuffer(ctx, { otStorage.data(), valueCount }); ctx.deserialize(selected.begin(), selected.end(), leafOts.begin()); if (programPuncturedPoint) { diff --git a/libOTe_Tests/Pprf_Tests.cpp b/libOTe_Tests/Pprf_Tests.cpp index a9f0b246..699b46a3 100644 --- a/libOTe_Tests/Pprf_Tests.cpp +++ b/libOTe_Tests/Pprf_Tests.cpp @@ -74,6 +74,26 @@ void RegularPprf_expandOne_test(const oc::CLP& cmd) #if defined(ENABLE_SILENTOT) || defined(ENABLE_SILENT_VOLE) + // Check both serialized choices, including the one the receiver never opens. + PRNG prng(CCBlock); + for (const u64 bytes : { 16, 32 }) + { + for (const u64 offset : { 0, 1, 15 }) + { + std::array keys; + prng.get(keys.data(), keys.size()); + std::array actual; + prng.get(actual.data(), actual.size()); + auto expected = actual; + PRNG masker; + for (u64 choice = 0; choice < 2; ++choice) + pprf::xorLeafMask({ expected.data() + offset + choice * bytes, bytes }, keys[1 ^ choice], masker); + pprf::xorSenderLeafMasks({ actual.data() + offset, 2 * bytes }, bytes, keys); + if (actual != expected) + throw RTE_LOC; + } + } + for (u64 domain : { 2, 128, 4522}) for (bool program : {true, false}) { From fca9ddbfe4c49a9aebef2d73e698b00a2cf0a898 Mon Sep 17 00:00:00 2001 From: Peter Rindal Date: Wed, 25 Nov 2026 09:25:19 -0800 Subject: [PATCH 09/22] Fold PPRF corrections in SIMD registers and index internal nodes by row --- libOTe/Tools/Pprf/RegularPprf.h | 45 ++++++++++++++++++-------------- libOTe_Tests/Pprf_Tests.cpp | 46 +++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 19 deletions(-) diff --git a/libOTe/Tools/Pprf/RegularPprf.h b/libOTe/Tools/Pprf/RegularPprf.h index 01f20e33..17d04d28 100644 --- a/libOTe/Tools/Pprf/RegularPprf.h +++ b/libOTe/Tools/Pprf/RegularPprf.h @@ -67,15 +67,17 @@ namespace osuCrypto // The level sums stay in registers; sum0/sum1 (8 blocks each) are // read on entry and written on exit. OC_FORCEINLINE void internalLevel(const YmmKey& key, - const block* parents, block* children, u64 width, block* sum0, block* sum1) + const AlignedArray* parents, AlignedArray* children, + u64 width, block* sum0, block* sum1) { #define OC_PPRF_SUM_IN(j) __m256i s0##j = OC_PPRF_LD(sum0, j), s1##j = OC_PPRF_LD(sum1, j) OC_PPRF_LANES(OC_PPRF_SUM_IN); #undef OC_PPRF_SUM_IN for (u64 p = 0; p < width; ++p) { - const block* in = parents + 8 * p; - block* ch = children + 16 * p; + const block* in = parents[p].data(); + block* left = children[2 * p].data(); + block* right = children[2 * p + 1].data(); #define OC_PPRF_IN(j) const __m256i x##j = OC_PPRF_LD(in, j); __m256i a##j = _mm256_xor_si256(x##j, key.k[0]) OC_PPRF_LANES(OC_PPRF_IN); #undef OC_PPRF_IN @@ -90,8 +92,8 @@ namespace osuCrypto { \ const __m256i c0 = _mm256_aesenc_epi128(a##j, x##j); \ const __m256i c1 = _mm256_add_epi64(a##j, x##j); \ - OC_PPRF_ST(ch, j, c0); \ - OC_PPRF_ST(ch + 8, j, c1); \ + OC_PPRF_ST(left, j, c0); \ + OC_PPRF_ST(right, j, c1); \ s0##j = _mm256_xor_si256(s0##j, c0); \ s1##j = _mm256_xor_si256(s1##j, c1); \ } @@ -162,14 +164,15 @@ namespace osuCrypto }; template - OC_FORCEINLINE void internalStep(const ZmmKey& key, const block* in, block* ch, + OC_FORCEINLINE void internalStep(const ZmmKey& key, + const AlignedArray* parents, AlignedArray* children, __m512i (&s0)[2], __m512i (&s1)[2]) { constexpr u64 C = 2 * NP; __m512i x[C], a[C]; for (u64 c = 0; c < C; ++c) { - x[c] = _mm512_loadu_si512(in + 4 * c); + x[c] = _mm512_loadu_si512(parents[c / 2].data() + 4 * (c % 2)); a[c] = _mm512_xor_si512(x[c], key.k[0]); } for (u64 r = 1; r < 10; ++r) @@ -181,23 +184,24 @@ namespace osuCrypto const __m512i c0 = _mm512_aesenc_epi128(a[c], x[c]); const __m512i c1 = _mm512_add_epi64(a[c], x[c]); const u64 q = c / 2, v = c % 2; // parent q, lanes 4v..4v+3 - _mm512_storeu_si512(ch + 16 * q + 4 * v, c0); - _mm512_storeu_si512(ch + 16 * q + 8 + 4 * v, c1); + _mm512_storeu_si512(children[2 * q].data() + 4 * v, c0); + _mm512_storeu_si512(children[2 * q + 1].data() + 4 * v, c1); s0[v] = _mm512_xor_si512(s0[v], c0); s1[v] = _mm512_xor_si512(s1[v], c1); } } OC_FORCEINLINE void internalLevel(const ZmmKey& key, - const block* parents, block* children, u64 width, block* sum0, block* sum1) + const AlignedArray* parents, AlignedArray* children, + u64 width, block* sum0, block* sum1) { __m512i s0[2] = { _mm512_loadu_si512(sum0), _mm512_loadu_si512(sum0 + 4) }; __m512i s1[2] = { _mm512_loadu_si512(sum1), _mm512_loadu_si512(sum1 + 4) }; u64 p = 0; for (; p + 2 <= width; p += 2) - internalStep<2>(key, parents + 8 * p, children + 16 * p, s0, s1); + internalStep<2>(key, parents + p, children + 2 * p, s0, s1); if (p < width) - internalStep<1>(key, parents + 8 * p, children + 16 * p, s0, s1); + internalStep<1>(key, parents + p, children + 2 * p, s0, s1); _mm512_storeu_si512(sum0, s0[0]); _mm512_storeu_si512(sum0 + 4, s0[1]); _mm512_storeu_si512(sum1, s1[0]); _mm512_storeu_si512(sum1 + 4, s1[1]); } @@ -287,9 +291,8 @@ namespace osuCrypto OC_FORCEINLINE block fold4(__m512i sum) { - alignas(64) block lanes[4]; - _mm512_store_si512(lanes, sum); - return (lanes[0] ^ lanes[1]) ^ (lanes[2] ^ lanes[3]); + const auto pair = _mm256_xor_si256(_mm512_castsi512_si256(sum), _mm512_extracti64x4_epi64(sum, 1)); + return _mm_xor_si128(_mm256_castsi256_si128(pair), _mm256_extracti128_si256(pair, 1)); } template @@ -372,7 +375,11 @@ namespace osuCrypto // tree's level sum is their XOR. OC_FORCEINLINE block fold8(const block* s) { +#ifdef ENABLE_AVX512 + return fold4(_mm512_xor_si512(_mm512_loadu_si512(s), _mm512_loadu_si512(s + 4))); +#else return ((s[0] ^ s[1]) ^ (s[2] ^ s[3])) ^ ((s[4] ^ s[5]) ^ (s[6] ^ s[7])); +#endif } } #endif @@ -900,8 +907,8 @@ namespace osuCrypto #ifdef OC_ENABLE_VAES { alignas(64) std::array laneSums0{}, laneSums1{}; - pprfVaes::internalLevel(internalKey, parents.data()->data(), - children.data()->data(), width, laneSums0.data(), laneSums1.data()); + pprfVaes::internalLevel(internalKey, parents.data(), children.data(), + width, laneSums0.data(), laneSums1.data()); leftAccumulator = leftAccumulator ^ pprfVaes::fold8(laneSums0.data()); rightAccumulator = rightAccumulator ^ pprfVaes::fold8(laneSums1.data()); } @@ -1467,8 +1474,8 @@ namespace osuCrypto #ifdef OC_ENABLE_VAES { alignas(64) std::array laneSums0{}, laneSums1{}; - pprfVaes::internalLevel(internalKey, parents.data()->data(), - children.data()->data(), width, laneSums0.data(), laneSums1.data()); + pprfVaes::internalLevel(internalKey, parents.data(), children.data(), + width, laneSums0.data(), laneSums1.data()); leftAccumulator = leftAccumulator ^ pprfVaes::fold8(laneSums0.data()); rightAccumulator = rightAccumulator ^ pprfVaes::fold8(laneSums1.data()); } diff --git a/libOTe_Tests/Pprf_Tests.cpp b/libOTe_Tests/Pprf_Tests.cpp index 699b46a3..84636c48 100644 --- a/libOTe_Tests/Pprf_Tests.cpp +++ b/libOTe_Tests/Pprf_Tests.cpp @@ -69,10 +69,56 @@ void RegularPprf_expandOne_test_impl(u64 domain, bool program) } } +#ifdef OC_ENABLE_VAES +static void checkRegularInternalLevel(const AES& aes, const auto& key) +{ + using Row = AlignedArray; + PRNG prng(CCBlock); + for (u64 width : { 1, 2, 3, 4, 8 }) + { + std::vector parents(width), children(2 * width); + Row left, right; + prng.get(left.data(), left.size()); + prng.get(right.data(), right.size()); + Row expectedLeft = left, expectedRight = right; + for (auto& row : parents) + prng.get(row.data(), row.size()); + pprfVaes::internalLevel(key, parents.data(), children.data(), width, left.data(), right.data()); + for (u64 p = 0; p < width; ++p) + for (u64 lane = 0; lane < 8; ++lane) + { + const auto parent = parents[p][lane]; + const auto encrypted = aes.ecbEncBlock(parent); + const auto l = AES::roundEnc(encrypted, parent); + const auto r = encrypted.add_epi64(parent); + if (children[2 * p][lane] != l || children[2 * p + 1][lane] != r) + throw UnitTestFail("regular PPRF internal child mismatch"); + expectedLeft[lane] ^= l; + expectedRight[lane] ^= r; + } + block leftSum = ZeroBlock, rightSum = ZeroBlock; + for (u64 lane = 0; lane < 8; ++lane) + { + leftSum ^= expectedLeft[lane]; + rightSum ^= expectedRight[lane]; + } + if (left != expectedLeft || right != expectedRight || + pprfVaes::fold8(left.data()) != leftSum || pprfVaes::fold8(right.data()) != rightSum) + throw UnitTestFail("regular PPRF internal correction mismatch"); + } +} +#endif + void RegularPprf_expandOne_test(const oc::CLP& cmd) { #if defined(ENABLE_SILENTOT) || defined(ENABLE_SILENT_VOLE) +#ifdef OC_ENABLE_VAES + checkRegularInternalLevel(mAesFixedKey, pprfVaes::YmmKey(mAesFixedKey)); +#ifdef ENABLE_AVX512 + checkRegularInternalLevel(mAesFixedKey, pprfVaes::ZmmKey(mAesFixedKey)); +#endif +#endif // Check both serialized choices, including the one the receiver never opens. PRNG prng(CCBlock); From ec75427220fc8ecfc0d9ff4a898cb62a2497dd17 Mon Sep 17 00:00:00 2001 From: Peter Rindal Date: Wed, 25 Nov 2026 10:08:47 -0800 Subject: [PATCH 10/22] Index full PPRF leaves by row and reuse the public VAES keys --- libOTe/Tools/Pprf/RegularPprf.h | 72 +++++++++++++++++---------------- libOTe_Tests/Pprf_Tests.cpp | 37 +++++++++++++++++ 2 files changed, 74 insertions(+), 35 deletions(-) diff --git a/libOTe/Tools/Pprf/RegularPprf.h b/libOTe/Tools/Pprf/RegularPprf.h index 17d04d28..2b984518 100644 --- a/libOTe/Tools/Pprf/RegularPprf.h +++ b/libOTe/Tools/Pprf/RegularPprf.h @@ -110,14 +110,14 @@ namespace osuCrypto // gGgmAes[keep].hashBlocks<8>. Both keys run interleaved so 8 YMM AES // chains are in flight. sum0/sum1 (8 blocks each) are XOR-accumulated. OC_FORCEINLINE void leafLevel(const YmmKey& k0, const YmmKey& k1, - const block* parents, block* out, u64 width, block* sum0, block* sum1) + const AlignedArray* parents, block* out, u64 width, block* sum0, block* sum1) { #define OC_PPRF_SUM_IN(j) __m256i s0##j = OC_PPRF_LD(sum0, j), s1##j = OC_PPRF_LD(sum1, j) OC_PPRF_LANES(OC_PPRF_SUM_IN); #undef OC_PPRF_SUM_IN for (u64 p = 0; p < width; ++p) { - const block* in = parents + 8 * p; + const block* in = parents[p].data(); block* o = out + 16 * p; #define OC_PPRF_IN(j) \ const __m256i x##j = OC_PPRF_LD(in, j); \ @@ -230,13 +230,14 @@ namespace osuCrypto } template - OC_FORCEINLINE void leafStep(const ZmmKey& k0, const ZmmKey& k1, const block* in, block* o, + OC_FORCEINLINE void leafStep(const ZmmKey& k0, const ZmmKey& k1, + const AlignedArray* parents, block* o, __m512i (&s0)[2], __m512i (&s1)[2]) { constexpr u64 C = 2 * NP; __m512i x[C], a[C], b[C]; for (u64 c = 0; c < C; ++c) - x[c] = _mm512_loadu_si512(in + 4 * c); + x[c] = _mm512_loadu_si512(parents[c / 2].data() + 4 * (c % 2)); hashLeafChains(k0, k1, x, a, b); for (u64 c = 0; c < C; ++c) { @@ -249,15 +250,15 @@ namespace osuCrypto } OC_FORCEINLINE void leafLevel(const ZmmKey& k0, const ZmmKey& k1, - const block* parents, block* out, u64 width, block* sum0, block* sum1) + const AlignedArray* parents, block* out, u64 width, block* sum0, block* sum1) { __m512i s0[2] = { _mm512_loadu_si512(sum0), _mm512_loadu_si512(sum0 + 4) }; __m512i s1[2] = { _mm512_loadu_si512(sum1), _mm512_loadu_si512(sum1 + 4) }; u64 p = 0; for (; p + 2 <= width; p += 2) - leafStep<2>(k0, k1, parents + 8 * p, out + 16 * p, s0, s1); + leafStep<2>(k0, k1, parents + p, out + 16 * p, s0, s1); if (p < width) - leafStep<1>(k0, k1, parents + 8 * p, out + 16 * p, s0, s1); + leafStep<1>(k0, k1, parents + p, out + 16 * p, s0, s1); _mm512_storeu_si512(sum0, s0[0]); _mm512_storeu_si512(sum0 + 4, s0[1]); _mm512_storeu_si512(sum1, s1[0]); _mm512_storeu_si512(sum1 + 4, s1[1]); } @@ -295,23 +296,24 @@ namespace osuCrypto return _mm_xor_si128(_mm256_castsi256_si128(pair), _mm256_extracti128_si256(pair, 1)); } + using Key = ZmmKey; +#else + using Key = YmmKey; +#endif + + // Broadcast the two public leaf keys once on paths that reuse their schedules. template - OC_FORCEINLINE decltype(auto) compactLeafKey() + OC_FORCEINLINE decltype(auto) leafKey() { if constexpr (Cache) { - static const ZmmKey key(gGgmAes[Side]); + static const Key key(gGgmAes[Side]); return (key); } else - return ZmmKey(gGgmAes[Side]); + return Key(gGgmAes[Side]); } - using Key = ZmmKey; -#else - using Key = YmmKey; -#endif - template OC_FORCEINLINE void leafMaskRound(int rc, __m256i& key, __m256i& pad0, __m256i& pad1) { @@ -381,6 +383,16 @@ namespace osuCrypto return ((s[0] ^ s[1]) ^ (s[2] ^ s[3])) ^ ((s[4] ^ s[5]) ^ (s[6] ^ s[7])); #endif } + // Keep the full leaf kernel out of the sender and receiver expansion loops. + OC_NOINLINE inline std::array fullLeafLevel( + span> parents, block* dest) + { + alignas(64) std::array left{}, right{}; + leafLevel(leafKey<0, true>(), leafKey<1, true>(), parents.data(), dest, + parents.size(), left.data(), right.data()); + return { fold8(left.data()), fold8(right.data()) }; + } + } #endif @@ -541,8 +553,8 @@ namespace osuCrypto if constexpr (std::contiguous_iterator && requires { requires CoeffCtx::blockCopyIsTrivialAndAdditionIsXor; }) { - const auto& k0 = pprfVaes::compactLeafKey<0, CacheLeafKeys>(); - const auto& k1 = pprfVaes::compactLeafKey<1, CacheLeafKeys>(); + const auto& k0 = pprfVaes::leafKey<0, CacheLeafKeys>(); + const auto& k1 = pprfVaes::leafKey<1, CacheLeafKeys>(); block* dest = std::to_address(output); __m512i sum0 = _mm512_setzero_si512(), sum1 = _mm512_setzero_si512(); u64 p = 0; @@ -1018,15 +1030,10 @@ namespace osuCrypto block leftAccumulator = ZeroBlock; block rightAccumulator = ZeroBlock; #ifdef OC_ENABLE_VAES - { - alignas(64) std::array laneSums0{}, laneSums1{}; - pprfVaes::leafLevel(pprfVaes::Key(gGgmAes[0]), pprfVaes::Key(gGgmAes[1]), - parents.data()->data(), dest, parents.size(), - laneSums0.data(), laneSums1.data()); - nativeLeaf += 16 * parents.size(); - leftAccumulator = leftAccumulator ^ pprfVaes::fold8(laneSums0.data()); - rightAccumulator = rightAccumulator ^ pprfVaes::fold8(laneSums1.data()); - } + const auto sums = pprfVaes::fullLeafLevel(parents, dest); + leftAccumulator ^= sums[0]; + rightAccumulator ^= sums[1]; + nativeLeaf += 16 * parents.size(); #else for (u64 parentIdx = 0; parentIdx < parents.size(); ++parentIdx) { @@ -1606,15 +1613,10 @@ namespace osuCrypto block leftAccumulator = gGgmAes[0].hashBlock(ZeroBlock); block rightAccumulator = gGgmAes[1].hashBlock(ZeroBlock); #ifdef OC_ENABLE_VAES - { - alignas(64) std::array laneSums0{}, laneSums1{}; - pprfVaes::leafLevel(pprfVaes::Key(gGgmAes[0]), pprfVaes::Key(gGgmAes[1]), - parents.data()->data(), dest, parents.size(), - laneSums0.data(), laneSums1.data()); - nativeLeaf += 16 * parents.size(); - leftAccumulator = leftAccumulator ^ pprfVaes::fold8(laneSums0.data()); - rightAccumulator = rightAccumulator ^ pprfVaes::fold8(laneSums1.data()); - } + const auto sums = pprfVaes::fullLeafLevel(parents, dest); + leftAccumulator ^= sums[0]; + rightAccumulator ^= sums[1]; + nativeLeaf += 16 * parents.size(); #else for (u64 parentIdx = 0; parentIdx < parents.size(); ++parentIdx) { diff --git a/libOTe_Tests/Pprf_Tests.cpp b/libOTe_Tests/Pprf_Tests.cpp index 84636c48..a6c6284c 100644 --- a/libOTe_Tests/Pprf_Tests.cpp +++ b/libOTe_Tests/Pprf_Tests.cpp @@ -107,6 +107,41 @@ static void checkRegularInternalLevel(const AES& aes, const auto& key) throw UnitTestFail("regular PPRF internal correction mismatch"); } } + +template +static void checkRegularLeafLevel() +{ + using Row = AlignedArray; + PRNG prng(CCBlock); + const Key k0(gGgmAes[0]), k1(gGgmAes[1]); + for (u64 width : { 0, 1, 2, 3, 8 }) + { + std::vector parents(width); + for (auto& row : parents) + prng.get(row.data(), row.size()); + // Offset the destination by one block and guard both ends. + std::vector leaves(16 * width + 2, AllOneBlock); + auto expected = leaves; + std::array sums; + for (auto& row : sums) + prng.get(row.data(), row.size()); + auto expectedSums = sums; + for (u64 p = 0; p < width; ++p) + for (u64 side = 0; side < 2; ++side) + for (u64 lane = 0; lane < 8; ++lane) + { + const auto parent = parents[p][lane]; + const auto leaf = gGgmAes[side].ecbEncBlock(parent) ^ parent; + expected[1 + 16 * p + 8 * side + lane] = leaf; + expectedSums[side][lane] ^= leaf; + } + + pprfVaes::leafLevel(k0, k1, parents.data(), leaves.data() + 1, width, + sums[0].data(), sums[1].data()); + if (leaves != expected || sums != expectedSums) + throw UnitTestFail("regular PPRF leaf or correction mismatch"); + } +} #endif void RegularPprf_expandOne_test(const oc::CLP& cmd) @@ -115,8 +150,10 @@ void RegularPprf_expandOne_test(const oc::CLP& cmd) #ifdef OC_ENABLE_VAES checkRegularInternalLevel(mAesFixedKey, pprfVaes::YmmKey(mAesFixedKey)); + checkRegularLeafLevel(); #ifdef ENABLE_AVX512 checkRegularInternalLevel(mAesFixedKey, pprfVaes::ZmmKey(mAesFixedKey)); + checkRegularLeafLevel(); #endif #endif From c2e04234eb7c1906b250c241a6ae49a0fa5a7c82 Mon Sep 17 00:00:00 2001 From: Peter Rindal Date: Wed, 25 Nov 2026 11:50:37 -0800 Subject: [PATCH 11/22] Hash compact XOR PPRF leaves with 256-bit VAES --- libOTe/Tools/Pprf/RegularPprf.h | 66 ++++++++++++++++++++++++++++++++- libOTe_Tests/Pprf_Tests.cpp | 6 ++- 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/libOTe/Tools/Pprf/RegularPprf.h b/libOTe/Tools/Pprf/RegularPprf.h index 2b984518..26df21e4 100644 --- a/libOTe/Tools/Pprf/RegularPprf.h +++ b/libOTe/Tools/Pprf/RegularPprf.h @@ -299,6 +299,61 @@ namespace osuCrypto using Key = ZmmKey; #else using Key = YmmKey; + + OC_FORCEINLINE void storeCompactRow(block* out, u64 live, const __m256i (&row)[4]) + { + assert(live >= 4 && live <= 8); + _mm256_storeu_si256(reinterpret_cast<__m256i*>(out), row[0]); + _mm256_storeu_si256(reinterpret_cast<__m256i*>(out + 2), row[1]); + if (live >= 6) + { + _mm256_storeu_si256(reinterpret_cast<__m256i*>(out + 4), row[2]); + if (live == 8) + _mm256_storeu_si256(reinterpret_cast<__m256i*>(out + 6), row[3]); + else if (live == 7) + _mm_storeu_si128(reinterpret_cast<__m128i*>(out + 6), _mm256_castsi256_si128(row[3])); + } + else if (live == 5) + _mm_storeu_si128(reinterpret_cast<__m128i*>(out + 4), _mm256_castsi256_si128(row[2])); + } + + // Eight independent AES chains cover both children of one eight-lane row. + // All lanes contribute to the correction, including those outside the domain. + OC_FORCEINLINE void compactLeafStep(const YmmKey& k0, const YmmKey& k1, + const block* parents, block*& out, u64 live, __m256i& sum0, __m256i& sum1) + { + __m256i x[4], left[4], right[4]; + for (u64 c = 0; c < 4; ++c) + { + x[c] = _mm256_loadu_si256(reinterpret_cast(parents + 2 * c)); + left[c] = _mm256_xor_si256(x[c], k0.k[0]); + right[c] = _mm256_xor_si256(x[c], k1.k[0]); + } + for (u64 r = 1; r < 10; ++r) + for (u64 c = 0; c < 4; ++c) + { + left[c] = _mm256_aesenc_epi128(left[c], k0.k[r]); + right[c] = _mm256_aesenc_epi128(right[c], k1.k[r]); + } + for (u64 c = 0; c < 4; ++c) + { + left[c] = _mm256_xor_si256(_mm256_aesenclast_epi128(left[c], k0.k[10]), x[c]); + right[c] = _mm256_xor_si256(_mm256_aesenclast_epi128(right[c], k1.k[10]), x[c]); + } + auto fold = [](const __m256i (&v)[4]) { + return _mm256_xor_si256(_mm256_xor_si256(v[0], v[1]), _mm256_xor_si256(v[2], v[3])); + }; + sum0 = _mm256_xor_si256(sum0, fold(left)); + sum1 = _mm256_xor_si256(sum1, fold(right)); + storeCompactRow(out, live, left); + storeCompactRow(out + live, live, right); + out += 2 * live; + } + + OC_FORCEINLINE block fold2(__m256i sum) + { + return _mm_xor_si128(_mm256_castsi256_si128(sum), _mm256_extracti128_si256(sum, 1)); + } #endif // Broadcast the two public leaf keys once on paths that reuse their schedules. @@ -548,7 +603,7 @@ namespace osuCrypto const u64 subtreeDomain = 2 * parents.size(); const u64 fullLanes = domain / subtreeDomain; const u64 partialSize = domain % subtreeDomain; -#if defined(OC_ENABLE_VAES) && defined(ENABLE_AVX512) +#ifdef OC_ENABLE_VAES // A context can opt in when block copy is assignment and addition is bitwise XOR. if constexpr (std::contiguous_iterator && requires { requires CoeffCtx::blockCopyIsTrivialAndAdditionIsXor; }) @@ -556,6 +611,7 @@ namespace osuCrypto const auto& k0 = pprfVaes::leafKey<0, CacheLeafKeys>(); const auto& k1 = pprfVaes::leafKey<1, CacheLeafKeys>(); block* dest = std::to_address(output); +#ifdef ENABLE_AVX512 __m512i sum0 = _mm512_setzero_si512(), sum1 = _mm512_setzero_si512(); u64 p = 0; for (; p + 2 <= parents.size(); p += 2) @@ -566,6 +622,14 @@ namespace osuCrypto p, fullLanes, partialSize / 2, sum0, sum1); leafSums[0] ^= pprfVaes::fold4(sum0); leafSums[1] ^= pprfVaes::fold4(sum1); +#else + __m256i sum0 = _mm256_setzero_si256(), sum1 = _mm256_setzero_si256(); + for (u64 p = 0; p < parents.size(); ++p) + pprfVaes::compactLeafStep(k0, k1, parents[p].data(), dest, + fullLanes + (p < partialSize / 2), sum0, sum1); + leafSums[0] ^= pprfVaes::fold2(sum0); + leafSums[1] ^= pprfVaes::fold2(sum1); +#endif return dest - std::to_address(output); } #endif diff --git a/libOTe_Tests/Pprf_Tests.cpp b/libOTe_Tests/Pprf_Tests.cpp index a6c6284c..8530a23e 100644 --- a/libOTe_Tests/Pprf_Tests.cpp +++ b/libOTe_Tests/Pprf_Tests.cpp @@ -821,6 +821,7 @@ namespace static constexpr bool blockCopyIsTrivialAndAdditionIsXor = true; }; + template void checkCompactXorLeaves() { PRNG prng(CCBlock); @@ -845,7 +846,7 @@ namespace expected[next++] = h; } } - const auto written = pprf::hashPhysicalBlockLeaves(parents, domain, output.data() + 1, sums, ctx); + const auto written = pprf::hashPhysicalBlockLeaves(parents, domain, output.data() + 1, sums, ctx); if (written != domain || sums != expectedSums || output != expected) throw std::runtime_error("Compact XOR leaves disagree with the scalar hashes"); } @@ -854,7 +855,8 @@ namespace void RegularPprf_PhysicalIndex_test(const CLP& cmd) { - checkCompactXorLeaves(); + checkCompactXorLeaves(); + checkCompactXorLeaves(); auto f = PprfOutputFormat::ByPhysicalIndex; auto v = cmd.isSet("v"); // The physical block path also supports coefficient addition other than XOR. From b2c7216632e73104ad069e4accba62ec195a0b46 Mon Sep 17 00:00:00 2001 From: Peter Rindal Date: Wed, 25 Nov 2026 13:14:18 -0800 Subject: [PATCH 12/22] Batch the first three physical PPRF sender levels across eight trees --- libOTe/Tools/Pprf/RegularPprf.h | 72 ++++++++++++++++++++++++++++++++- libOTe_Tests/Pprf_Tests.cpp | 3 +- 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/libOTe/Tools/Pprf/RegularPprf.h b/libOTe/Tools/Pprf/RegularPprf.h index 26df21e4..092a712e 100644 --- a/libOTe/Tools/Pprf/RegularPprf.h +++ b/libOTe/Tools/Pprf/RegularPprf.h @@ -840,7 +840,15 @@ namespace osuCrypto encPerTree, count, programPuncturedPoint, buff, encSums, leafMsgs, ctx); - for (u64 j = 0; j < count; ++j) + u64 j = 0; + if (oFormat == PprfOutputFormat::ByPhysicalIndex && mDepth > 3) + { + for (; j + 8 <= count; j += 8) + expandPhysicalEight(seed, batch + j, programPuncturedPoint, levels, + output, callbackLeaves, encSums.subspan(j * encPerTree, 8 * encPerTree), + leafMsgs.subspan(j * leafPerTree, 8 * leafPerTree), ctx); + } + for (; j < count; ++j) { const auto tree = batch + j; const auto root = mAesFixedKey.ecbEncBlock(seed ^ block(tree)); @@ -963,6 +971,68 @@ namespace osuCrypto current = next; } + expandFromTop(current.data(), treeIdx, programPuncturedPoint, levels, + output, callbackLeaves, encSums.subspan(topInternalDepth), leafMsgs, ctx); + } + + private: + // Each row is one tree. Encrypt eight independent parents at a time, + // preserving both the child order and the separate correction for each row. + template + void expandTopLevel(u64 firstTree, const block* parents, block* children, + span> encSums) const + { + constexpr u64 width = u64{1} << Depth; + constexpr u64 rows = 8 / width; + for (u64 t = 0; t < 8; t += rows) + { + block encrypted[8]; + mAesFixedKey.ecbEncBlocks<8>(parents + t * width, encrypted); + for (u64 row = 0; row < rows; ++row) + { + block leftSum = ZeroBlock, rightSum = ZeroBlock; + for (u64 k = 0; k < width; ++k) + { + const u64 i = (t + row) * width + k; + const auto aes = encrypted[row * width + k]; + const auto left = AES::roundEnc(aes, parents[i]); + const auto right = aes.add_epi64(parents[i]); + children[2 * i] = left; + children[2 * i + 1] = right; + leftSum ^= left; + rightSum ^= right; + } + const auto& ots = mBaseOTs(firstTree + t + row, mDepth - 1 - Depth); + encSums[(t + row) * (mDepth - 1) + Depth] = {leftSum ^ ots[1], rightSum ^ ots[0]}; + } + } + } + + OC_NOINLINE void expandPhysicalEight(block seed, u64 firstTree, bool program, + span>> levels, VecF& output, VecF& callbackLeaves, + span> encSums, span leafMsgs, CoeffCtx& ctx) + { + block seeds[8], roots[8], pairs[16], quads[32], tops[64]; + for (u64 t = 0; t < 8; ++t) + seeds[t] = seed ^ block(firstTree + t); + mAesFixedKey.ecbEncBlocks<8>(seeds, roots); + expandTopLevel<0>(firstTree, roots, pairs, encSums); + expandTopLevel<1>(firstTree, pairs, quads, encSums); + expandTopLevel<2>(firstTree, quads, tops, encSums); + const u64 sumsPerTree = mDepth - 1, bytesPerTree = leafMsgs.size() / 8; + for (u64 t = 0; t < 8; ++t) + expandFromTop(tops + 8 * t, firstTree + t, + program, levels, output, callbackLeaves, + encSums.subspan(t * sumsPerTree + 3, sumsPerTree - 3), + leafMsgs.subspan(t * bytesPerTree, bytesPerTree), ctx); + } + + template + OC_NOINLINE void expandFromTop(const block* current, u64 treeIdx, bool programPuncturedPoint, + span>> levels, VecF& output, VecF& callbackLeaves, + span> encSums, span leafMsgs, CoeffCtx& ctx) + { + auto encSumIter = encSums.begin(); if (mDepth > 3) { assert(levels.size() == mDepth - 3); diff --git a/libOTe_Tests/Pprf_Tests.cpp b/libOTe_Tests/Pprf_Tests.cpp index 8530a23e..de3140e1 100644 --- a/libOTe_Tests/Pprf_Tests.cpp +++ b/libOTe_Tests/Pprf_Tests.cpp @@ -861,7 +861,8 @@ void RegularPprf_PhysicalIndex_test(const CLP& cmd) auto v = cmd.isSet("v"); // The physical block path also supports coefficient addition other than XOR. for (bool program : {false, true}) - RegularPprf_test_impl(210, 9, program, f, false, v); + for (bool eager : {false, true}) + RegularPprf_test_impl(210, 17, program, f, eager, v); for (auto d : { 32,3242 }) for (auto n : { 8, 128 }) for (auto p : { true, false }) for (auto e : { true, false }) { RegularPprf_test_impl(d, n, p, f, e, v); From c49ecaffe530e6be8bf08e0ff33a3f339361b869 Mon Sep 17 00:00:00 2001 From: Peter Rindal Date: Wed, 25 Nov 2026 13:36:23 -0800 Subject: [PATCH 13/22] Reduce internal PPRF corrections across two AVX2 rows --- libOTe/Tools/Pprf/RegularPprf.h | 125 ++++++++++++++++++++------------ libOTe_Tests/Pprf_Tests.cpp | 11 ++- 2 files changed, 89 insertions(+), 47 deletions(-) diff --git a/libOTe/Tools/Pprf/RegularPprf.h b/libOTe/Tools/Pprf/RegularPprf.h index 092a712e..11c51f09 100644 --- a/libOTe/Tools/Pprf/RegularPprf.h +++ b/libOTe/Tools/Pprf/RegularPprf.h @@ -60,49 +60,56 @@ namespace osuCrypto #define OC_PPRF_LD(p, j) _mm256_loadu_si256(reinterpret_cast(p) + (j)) #define OC_PPRF_ST(p, j, v) _mm256_storeu_si256(reinterpret_cast<__m256i*>(p) + (j), v) - // One internal GGM level, 8 lanes per parent (4 YMM): parent p gives - // children 16p..16p+7 (left) and 16p+8..16p+15 (right). - // Same values as the scalar loop: - // a = AES(x), child0 = AESENC(a, x) = R(a) ^ x, child1 = a + x (64-bit lanes). - // The level sums stay in registers; sum0/sum1 (8 blocks each) are - // read on entry and written on exit. + // Expand four 256-bit AES chains per row. Two rows keep eight chains in + // flight. The caller chooses per-lane sums or one correction per side. + template + OC_FORCEINLINE void internalYmmStep(const YmmKey& key, + const AlignedArray* parents, AlignedArray* children, + Accumulate&& accumulate) + { + static_assert(Rows == 1 || Rows == 2); + constexpr unsigned chains = 4 * Rows; + __m256i aes[chains], left[chains], right[chains]; + for (unsigned c = 0; c < chains; ++c) + aes[c] = _mm256_xor_si256(OC_PPRF_LD(parents[c / 4].data(), c % 4), key.k[0]); + for (unsigned r = 1; r < 10; ++r) + for (unsigned c = 0; c < chains; ++c) + aes[c] = _mm256_aesenc_epi128(aes[c], key.k[r]); + for (unsigned c = 0; c < chains; ++c) + { + const auto x = OC_PPRF_LD(parents[c / 4].data(), c % 4); + const auto a = _mm256_aesenclast_epi128(aes[c], key.k[10]); + left[c] = _mm256_aesenc_epi128(a, x); + right[c] = _mm256_add_epi64(a, x); + OC_PPRF_ST(children[2 * (c / 4)].data(), c % 4, left[c]); + OC_PPRF_ST(children[2 * (c / 4) + 1].data(), c % 4, right[c]); + } + accumulate(left, right); + } + OC_FORCEINLINE void internalLevel(const YmmKey& key, const AlignedArray* parents, AlignedArray* children, u64 width, block* sum0, block* sum1) { -#define OC_PPRF_SUM_IN(j) __m256i s0##j = OC_PPRF_LD(sum0, j), s1##j = OC_PPRF_LD(sum1, j) - OC_PPRF_LANES(OC_PPRF_SUM_IN); -#undef OC_PPRF_SUM_IN + __m256i left[4], right[4]; + for (unsigned c = 0; c < 4; ++c) + { + left[c] = OC_PPRF_LD(sum0, c); + right[c] = OC_PPRF_LD(sum1, c); + } for (u64 p = 0; p < width; ++p) + internalYmmStep<1>(key, parents + p, children + 2 * p, [&](auto& l, auto& r) { + for (unsigned c = 0; c < 4; ++c) + { + left[c] = _mm256_xor_si256(left[c], l[c]); + right[c] = _mm256_xor_si256(right[c], r[c]); + } + }); + for (unsigned c = 0; c < 4; ++c) { - const block* in = parents[p].data(); - block* left = children[2 * p].data(); - block* right = children[2 * p + 1].data(); -#define OC_PPRF_IN(j) const __m256i x##j = OC_PPRF_LD(in, j); __m256i a##j = _mm256_xor_si256(x##j, key.k[0]) - OC_PPRF_LANES(OC_PPRF_IN); -#undef OC_PPRF_IN - for (u64 r = 1; r < 10; ++r) - { -#define OC_PPRF_RND(j) a##j = _mm256_aesenc_epi128(a##j, key.k[r]) - OC_PPRF_LANES(OC_PPRF_RND); -#undef OC_PPRF_RND - } -#define OC_PPRF_OUT(j) \ - a##j = _mm256_aesenclast_epi128(a##j, key.k[10]); \ - { \ - const __m256i c0 = _mm256_aesenc_epi128(a##j, x##j); \ - const __m256i c1 = _mm256_add_epi64(a##j, x##j); \ - OC_PPRF_ST(left, j, c0); \ - OC_PPRF_ST(right, j, c1); \ - s0##j = _mm256_xor_si256(s0##j, c0); \ - s1##j = _mm256_xor_si256(s1##j, c1); \ - } - OC_PPRF_LANES(OC_PPRF_OUT); -#undef OC_PPRF_OUT + OC_PPRF_ST(sum0, c, left[c]); + OC_PPRF_ST(sum1, c, right[c]); } -#define OC_PPRF_SUM_OUT(j) OC_PPRF_ST(sum0, j, s0##j); OC_PPRF_ST(sum1, j, s1##j) - OC_PPRF_LANES(OC_PPRF_SUM_OUT); -#undef OC_PPRF_SUM_OUT } // The leaf GGM level, 8 lanes per parent: out[16p + 8*keep + t] = @@ -438,6 +445,36 @@ namespace osuCrypto return ((s[0] ^ s[1]) ^ (s[2] ^ s[3])) ^ ((s[4] ^ s[5]) ^ (s[6] ^ s[7])); #endif } + OC_FORCEINLINE std::array internalLevelSums(const Key& key, + const AlignedArray* parents, AlignedArray* children, u64 width) + { +#ifdef ENABLE_AVX512 + alignas(64) std::array left{}, right{}; + internalLevel(key, parents, children, width, left.data(), right.data()); + return {fold8(left.data()), fold8(right.data())}; +#else + // A tree correction needs only the two totals. Reducing each batch + // leaves two live YMM sums across iterations instead of eight. + auto left = _mm256_setzero_si256(), right = _mm256_setzero_si256(); + auto accumulate = [&](__m256i (&l)[N], __m256i (&r)[N]) { + for (unsigned stride = N / 2; stride; stride /= 2) + for (unsigned c = 0; c < stride; ++c) + { + l[c] = _mm256_xor_si256(l[c], l[c + stride]); + r[c] = _mm256_xor_si256(r[c], r[c + stride]); + } + left = _mm256_xor_si256(left, l[0]); + right = _mm256_xor_si256(right, r[0]); + }; + u64 p = 0; + for (; width - p >= 2; p += 2) + internalYmmStep<2>(key, parents + p, children + 2 * p, accumulate); + if (p < width) + internalYmmStep<1>(key, parents + p, children + 2 * p, accumulate); + return {fold2(left), fold2(right)}; +#endif + } + // Keep the full leaf kernel out of the sender and receiver expansion loops. OC_NOINLINE inline std::array fullLeafLevel( span> parents, block* dest) @@ -1052,11 +1089,9 @@ namespace osuCrypto const auto width = u64{ 1 } << localDepth; #ifdef OC_ENABLE_VAES { - alignas(64) std::array laneSums0{}, laneSums1{}; - pprfVaes::internalLevel(internalKey, parents.data(), children.data(), - width, laneSums0.data(), laneSums1.data()); - leftAccumulator = leftAccumulator ^ pprfVaes::fold8(laneSums0.data()); - rightAccumulator = rightAccumulator ^ pprfVaes::fold8(laneSums1.data()); + const auto sums = pprfVaes::internalLevelSums(internalKey, parents.data(), children.data(), width); + leftAccumulator ^= sums[0]; + rightAccumulator ^= sums[1]; } #else for (u64 parentIdx = 0; parentIdx < width; ++parentIdx) @@ -1614,11 +1649,9 @@ namespace osuCrypto const auto width = u64{ 1 } << localDepth; #ifdef OC_ENABLE_VAES { - alignas(64) std::array laneSums0{}, laneSums1{}; - pprfVaes::internalLevel(internalKey, parents.data(), children.data(), - width, laneSums0.data(), laneSums1.data()); - leftAccumulator = leftAccumulator ^ pprfVaes::fold8(laneSums0.data()); - rightAccumulator = rightAccumulator ^ pprfVaes::fold8(laneSums1.data()); + const auto sums = pprfVaes::internalLevelSums(internalKey, parents.data(), children.data(), width); + leftAccumulator ^= sums[0]; + rightAccumulator ^= sums[1]; } #else for (u64 parentIdx = 0; parentIdx < width; ++parentIdx) diff --git a/libOTe_Tests/Pprf_Tests.cpp b/libOTe_Tests/Pprf_Tests.cpp index de3140e1..90724a52 100644 --- a/libOTe_Tests/Pprf_Tests.cpp +++ b/libOTe_Tests/Pprf_Tests.cpp @@ -74,13 +74,15 @@ static void checkRegularInternalLevel(const AES& aes, const auto& key) { using Row = AlignedArray; PRNG prng(CCBlock); - for (u64 width : { 1, 2, 3, 4, 8 }) + for (u64 width : { 0, 1, 2, 3, 4, 8, 17 }) { std::vector parents(width), children(2 * width); Row left, right; prng.get(left.data(), left.size()); prng.get(right.data(), right.size()); Row expectedLeft = left, expectedRight = right; + const block initialLeft = pprfVaes::fold8(left.data()); + const block initialRight = pprfVaes::fold8(right.data()); for (auto& row : parents) prng.get(row.data(), row.size()); pprfVaes::internalLevel(key, parents.data(), children.data(), width, left.data(), right.data()); @@ -105,6 +107,13 @@ static void checkRegularInternalLevel(const AES& aes, const auto& key) if (left != expectedLeft || right != expectedRight || pprfVaes::fold8(left.data()) != leftSum || pprfVaes::fold8(right.data()) != rightSum) throw UnitTestFail("regular PPRF internal correction mismatch"); + if constexpr (std::is_same_v, pprfVaes::Key>) + { + std::vector actual(children.size()); + const auto sums = pprfVaes::internalLevelSums(key, parents.data(), actual.data(), width); + if (actual != children || sums[0] != (initialLeft ^ leftSum) || sums[1] != (initialRight ^ rightSum)) + throw UnitTestFail("regular PPRF reduced level mismatch"); + } } } From b0795e86d69e0c4a383d8aca5c561ad11a20e74b Mon Sep 17 00:00:00 2001 From: Peter Rindal Date: Wed, 25 Nov 2026 13:49:27 -0800 Subject: [PATCH 14/22] Hash SoftSpoken sender pairs directly into their output --- libOTe/Tools/OtPairHashVaes.h | 56 +++++++++++++++++++ .../TwoChooseOne/Silent/SilentOtExtSender.cpp | 43 +------------- .../SoftSpokenOT/SoftSpokenShOtExt.cpp | 16 ++++++ .../SoftSpokenOT/SoftSpokenShOtExt.h | 16 ++++++ libOTe_Tests/SoftSpoken_Tests.cpp | 22 ++++++++ 5 files changed, 113 insertions(+), 40 deletions(-) create mode 100644 libOTe/Tools/OtPairHashVaes.h diff --git a/libOTe/Tools/OtPairHashVaes.h b/libOTe/Tools/OtPairHashVaes.h new file mode 100644 index 00000000..6c0c7f9c --- /dev/null +++ b/libOTe/Tools/OtPairHashVaes.h @@ -0,0 +1,56 @@ +#pragma once +#include "cryptoTools/Crypto/AES.h" +#include + +#ifdef OC_ENABLE_VAES +namespace osuCrypto::details +{ + // Each YMM lane holds one sender message. Keep both messages in registers + // through the Davies-Meyer feed-forward and store directly to the output. + struct OtPairHashVaes + { + __m256i key[11]; + __m256i dpair; + + OC_FORCEINLINE OtPairHashVaes(const osuCrypto::AES& aes, block delta) + : dpair(_mm256_set_m128i(delta.mData, _mm_setzero_si128())) + { + for (u64 k = 0; k < 11; ++k) + key[k] = _mm256_broadcastsi128_si256(aes.mRoundKey[k].mData); + } + + template + OC_FORCEINLINE void hashEight(const block* r, std::array* m) const + { + const auto mask = _mm256_set_epi64x(-1, -2, -1, -2); + __m256i x0, x1, x2, x3, x4, x5, x6, x7; + __m256i a0, a1, a2, a3, a4, a5, a6, a7; +#define OC_HASH_LANES(OP) OP(0); OP(1); OP(2); OP(3); OP(4); OP(5); OP(6); OP(7) +#define OC_HASH_LOAD(j) \ + x##j = _mm256_xor_si256(_mm256_broadcastsi128_si256(_mm_loadu_si128(&r[j].mData)), dpair); \ + if constexpr (clearChoiceBit) x##j = _mm256_and_si256(x##j, mask); \ + a##j = _mm256_xor_si256(x##j, key[0]) +#define OC_HASH_ROUND(j) a##j = _mm256_aesenc_epi128(a##j, key[k]) +#define OC_HASH_STORE(j) \ + a##j = _mm256_xor_si256(_mm256_aesenclast_epi128(a##j, key[10]), x##j); \ + if constexpr (streaming && aligned32) \ + _mm256_stream_si256(reinterpret_cast<__m256i*>(&m[j]), a##j); \ + else if constexpr (streaming) { \ + _mm_stream_si128(&m[j][0].mData, _mm256_castsi256_si128(a##j)); \ + _mm_stream_si128(&m[j][1].mData, _mm256_extracti128_si256(a##j, 1)); \ + } else \ + _mm256_storeu_si256(reinterpret_cast<__m256i*>(&m[j]), a##j) + OC_HASH_LANES(OC_HASH_LOAD); + for (u64 k = 1; k < 10; ++k) + { + OC_HASH_LANES(OC_HASH_ROUND); + } + OC_HASH_LANES(OC_HASH_STORE); +#undef OC_HASH_STORE +#undef OC_HASH_ROUND +#undef OC_HASH_LOAD +#undef OC_HASH_LANES + } + }; +} +#endif diff --git a/libOTe/TwoChooseOne/Silent/SilentOtExtSender.cpp b/libOTe/TwoChooseOne/Silent/SilentOtExtSender.cpp index 310aab06..54ab9c3e 100644 --- a/libOTe/TwoChooseOne/Silent/SilentOtExtSender.cpp +++ b/libOTe/TwoChooseOne/Silent/SilentOtExtSender.cpp @@ -4,6 +4,7 @@ #ifdef ENABLE_SILENTOT #include "libOTe/Tools/Tools.h" +#include "libOTe/Tools/OtPairHashVaes.h" #include "libOTe/TwoChooseOne/Silent/SilentOtExtReceiver.h" #include #include "cryptoTools/Common/Log.h" @@ -399,51 +400,13 @@ namespace osuCrypto namespace { #ifdef OC_ENABLE_VAES - // Hashes OTs [0, n8), n8 a multiple of 8: m[i] = (H(r_i & mask), H((r_i ^ d) & mask)) - // with H(x) = AES(x) ^ x. Each (x0, x1) pair is built in one YMM register - // (broadcast r, xor (0 || delta), and the mask) and the hashed pair is - // stored straight to the output, so no stack batch or copy is needed. - // Not a coroutine: the aligned key array stays in an ordinary frame. template OC_FORCEINLINE void hashSenderVaes(const AES& aes, const block* r, std::array* m, u64 n8, block delta) { - alignas(32) __m256i key[11]; - for (u64 k = 0; k < 11; ++k) - key[k] = _mm256_broadcastsi128_si256(aes.mRoundKey[k].mData); - const __m256i mask = _mm256_set_epi64x(-1, -2, -1, -2); - const __m256i dpair = _mm256_set_m128i(delta.mData, _mm_setzero_si128()); - + const details::OtPairHashVaes hash(aes, delta); for (u64 i = 0; i < n8; i += 8) - { - __m256i x0, x1, x2, x3, x4, x5, x6, x7; - __m256i a0, a1, a2, a3, a4, a5, a6, a7; -#define OC_HASH_LANES(OP) OP(0); OP(1); OP(2); OP(3); OP(4); OP(5); OP(6); OP(7) -#define OC_HASH_LOAD(j) \ - x##j = _mm256_and_si256(_mm256_xor_si256( \ - _mm256_broadcastsi128_si256(_mm_loadu_si128(&r[i + j].mData)), dpair), mask); \ - a##j = _mm256_xor_si256(x##j, key[0]) -#define OC_HASH_ROUND(j) a##j = _mm256_aesenc_epi128(a##j, key[k]) -#define OC_HASH_STORE(j) \ - a##j = _mm256_xor_si256(_mm256_aesenclast_epi128(a##j, key[10]), x##j); \ - if constexpr (streaming && aligned32) \ - _mm256_stream_si256(reinterpret_cast<__m256i*>(&m[i + j]), a##j); \ - else if constexpr (streaming) { \ - _mm_stream_si128(&m[i + j][0].mData, _mm256_castsi256_si128(a##j)); \ - _mm_stream_si128(&m[i + j][1].mData, _mm256_extracti128_si256(a##j, 1)); \ - } else \ - _mm256_storeu_si256(reinterpret_cast<__m256i*>(&m[i + j]), a##j) - OC_HASH_LANES(OC_HASH_LOAD); - for (u64 k = 1; k < 10; ++k) - { - OC_HASH_LANES(OC_HASH_ROUND); - } - OC_HASH_LANES(OC_HASH_STORE); -#undef OC_HASH_STORE -#undef OC_HASH_ROUND -#undef OC_HASH_LOAD -#undef OC_HASH_LANES - } + hash.hashEight(r + i, m + i); } #endif } diff --git a/libOTe/TwoChooseOne/SoftSpokenOT/SoftSpokenShOtExt.cpp b/libOTe/TwoChooseOne/SoftSpokenOT/SoftSpokenShOtExt.cpp index e92071a6..1c3ff16a 100644 --- a/libOTe/TwoChooseOne/SoftSpokenOT/SoftSpokenShOtExt.cpp +++ b/libOTe/TwoChooseOne/SoftSpokenOT/SoftSpokenShOtExt.cpp @@ -2,9 +2,25 @@ #ifdef ENABLE_SOFTSPOKEN_OT #include "libOTe/Vole/SoftSpokenOT/SubspaceVoleMaliciousLeaky.h" +#include "libOTe/Tools/CompilerHints.h" +#include "libOTe/Tools/OtPairHashVaes.h" namespace osuCrypto { +#ifdef OC_ENABLE_VAES + OC_NOINLINE void details::softSpokenHashPairs(u64 count, block delta, std::array* out, + const block* in, const osuCrypto::AES& aes) + { + // Work backwards so input can occupy the first half of the output. + assert(count % 8 == 0); + const OtPairHashVaes hash(aes, delta); + while (count) + { + count -= 8; + hash.hashEight(in + count, out + count); + } + } +#endif template const u64 SoftSpokenShOtSender::commSize; diff --git a/libOTe/TwoChooseOne/SoftSpokenOT/SoftSpokenShOtExt.h b/libOTe/TwoChooseOne/SoftSpokenOT/SoftSpokenShOtExt.h index c9a54bad..2d4970db 100644 --- a/libOTe/TwoChooseOne/SoftSpokenOT/SoftSpokenShOtExt.h +++ b/libOTe/TwoChooseOne/SoftSpokenOT/SoftSpokenShOtExt.h @@ -20,6 +20,13 @@ namespace osuCrypto { +#ifdef OC_ENABLE_VAES + namespace details + { + void softSpokenHashPairs(u64 count, block delta, std::array* out, + const block* in, const osuCrypto::AES& aes); + } +#endif // Pseudorandomly generate a stream of AES round keys. struct AESStream @@ -306,6 +313,15 @@ namespace osuCrypto u64 numUsed, block deltaBlock, std::array* messagesOut, const block* messagesIn, Enc& enc) { +#ifdef OC_ENABLE_VAES + if constexpr (std::is_same_v, AES>) + { + const u64 tail = numUsed % 8; + if (numUsed >= 8) + details::softSpokenHashPairs(numUsed - tail, deltaBlock, messagesOut + tail, messagesIn + tail, enc); + numUsed = tail; + } +#endif // Loop backwards, similarly to DotSemiHonest. u64 i = numUsed; while (i >= superBlkSize / 2) diff --git a/libOTe_Tests/SoftSpoken_Tests.cpp b/libOTe_Tests/SoftSpoken_Tests.cpp index 3252166b..c184fd24 100644 --- a/libOTe_Tests/SoftSpoken_Tests.cpp +++ b/libOTe_Tests/SoftSpoken_Tests.cpp @@ -852,6 +852,28 @@ namespace tests_libOTe PRNG prng0(block(4234335, 3445235)); PRNG prng1(block(42348345, 989835)); + // Compare both sender messages to scalar AES, including partial batches + // and odd low bits that the SoftSpoken hash must preserve. + { + PRNG random(block(7, 91)); + AES aes(random.get()); + const auto delta = random.get() | OneBlock; + std::array input; + random.get(input.data(), input.size()); + for (u64 n = 0; n <= input.size(); ++n) + { + using Pair = std::array; + std::array out; + out.fill(Pair{AllOneBlock, AllOneBlock}); + auto expected = out; + for (u64 i = 0; i < n; ++i) + expected[i + 1] = {aes.hashBlock(input[i]), aes.hashBlock(input[i] ^ delta)}; + SoftSpokenShOtSender<>::xorAndHashMessages(n, delta, out.data() + 1, input.data(), aes); + if (out != expected) + throw UnitTestFail("SoftSpoken sender pair hash mismatch"); + } + } + auto nnumOTs = cmd.getManyOr("n", { { 10, 100, 9733 } }); for (auto random : { false, true }) From 7c1ecaa821805b3a2ff40f0952d8b378d964cb8f Mon Sep 17 00:00:00 2001 From: Peter Rindal Date: Wed, 25 Nov 2026 14:24:33 -0800 Subject: [PATCH 15/22] Fuse two-bit VOLE sender hashing and reduction on AVX2 VAES --- libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp | 52 +++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp b/libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp index f1476092..4fed66dc 100644 --- a/libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp +++ b/libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp @@ -161,6 +161,52 @@ namespace osuCrypto _mm512_storeu_si512(outU + i, _mm512_xor_si512(even, odd)); } } +#elif defined(OC_ENABLE_VAES) && defined(__AVX2__) && defined(__VAES__) + // Two seeds per YMM. The common last-round key and index cancel in + // the even-weight reductions, just as they do in the 512-bit kernel. + template + static OC_FORCEINLINE void hashTwoBitSeeds256(const block* seeds, block index, + const AES& aes, __m256i (&state)[lanes]) + { + const auto first = _mm256_broadcastsi128_si256((index ^ aes.mRoundKey[0]).mData); + __m256i raw[lanes]; + for (unsigned i = 0; i < lanes; ++i) + { + raw[i] = _mm256_loadu_si256(reinterpret_cast(seeds + 2 * i)); + state[i] = _mm256_xor_si256(raw[i], first); + } +#pragma GCC unroll 9 + for (unsigned round = 1; round < 10; ++round) + { + const auto key = _mm256_broadcastsi128_si256(aes.mRoundKey[round].mData); + for (auto& v : state) + v = _mm256_aesenc_epi128(v, key); + } + for (unsigned i = 0; i < lanes; ++i) + state[i] = _mm256_aesenclast_epi128(state[i], raw[i]); + } + + static OC_FORCEINLINE void generateFourTwoBitSenders( + const block* seeds, block index, const AES& aes, block* outU, block* outV) + { + __m256i state[8]; + hashTwoBitSeeds256(seeds, index, aes, state); + for (unsigned i = 0; i < 4; i += 2) + { + __m256i partial[2]; + for (unsigned j = 0; j < 2; ++j) + { + const auto a = state[2 * (i + j)], b = state[2 * (i + j) + 1]; + const auto middle = _mm256_permute2x128_si256(a, b, 0x21); + const auto last = _mm256_permute2x128_si256(b, b, 0x11); + _mm256_storeu_si256(reinterpret_cast<__m256i*>(outV + 2 * (i + j)), _mm256_xor_si256(middle, last)); + partial[j] = _mm256_xor_si256(a, b); + } + const auto even = _mm256_permute2x128_si256(partial[0], partial[1], 0x20); + const auto odd = _mm256_permute2x128_si256(partial[0], partial[1], 0x31); + _mm256_storeu_si256(reinterpret_cast<__m256i*>(outU + i), _mm256_xor_si256(even, odd)); + } + } #endif template @@ -192,6 +238,12 @@ namespace osuCrypto for (; nVole + 8 <= This.mNumVoles; nVole += 8, seeds += 32, outU += 8, outV += 16) generateEightTwoBitSenders(seeds, blockIdxBlock, aes, outU, outV); } +#elif defined(OC_ENABLE_VAES) && defined(__AVX2__) && defined(__VAES__) + if constexpr (fieldBitsConst == 2) + { + for (; This.mNumVoles - nVole >= 4; nVole += 4, seeds += 16, outU += 4, outV += 8) + generateFourTwoBitSenders(seeds, blockIdxBlock, aes, outU, outV); + } #endif for (; nVole < This.mNumVoles; nVole += volePerSuperBlk) { From 5b1ea93a330ffe2ae28ad34a90e36ebe316a4377 Mon Sep 17 00:00:00 2001 From: Peter Rindal Date: Wed, 25 Nov 2026 14:27:58 -0800 Subject: [PATCH 16/22] Fuse paired two-bit VOLE receiver reductions on AVX2 VAES --- libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp | 40 +++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp b/libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp index 4fed66dc..e96a2817 100644 --- a/libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp +++ b/libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp @@ -348,6 +348,31 @@ namespace osuCrypto _mm256_storeu_si256(reinterpret_cast<__m256i*>(out + 2 * i), w); } } +#elif defined(OC_ENABLE_VAES) && defined(__AVX2__) && defined(__VAES__) + template + static OC_FORCEINLINE void generateSixTwoBitReceivers(const block* seeds, block index, + const AES& aes, const u8* delta, const block* correction, block* out) + { + __m256i state[9]; + hashTwoBitSeeds256(seeds, index, aes, state); + for (unsigned i = 0; i < 3; ++i) + { + // Two triples occupy [a0 a1], [a2 b0], [b1 b2]. + const auto x = state[3 * i], y = state[3 * i + 1], z = state[3 * i + 2]; + auto a = _mm256_xor_si256(x, _mm256_permute2x128_si256(y, y, 0x00)); + auto b = _mm256_xor_si256(_mm256_permute2x128_si256(y, z, 0x21), + _mm256_permute2x128_si256(z, z, 0x11)); + if constexpr (corrected) + { + const auto maskA = _mm256_set_m128i(_mm_set1_epi8(delta[4 * i + 1]), _mm_set1_epi8(delta[4 * i])); + const auto maskB = _mm256_set_m128i(_mm_set1_epi8(delta[4 * i + 3]), _mm_set1_epi8(delta[4 * i + 2])); + a = _mm256_xor_si256(a, _mm256_and_si256(maskA, _mm256_broadcastsi128_si256(correction[2 * i].mData))); + b = _mm256_xor_si256(b, _mm256_and_si256(maskB, _mm256_broadcastsi128_si256(correction[2 * i + 1].mData))); + } + _mm256_storeu_si256(reinterpret_cast<__m256i*>(out + 4 * i), a); + _mm256_storeu_si256(reinterpret_cast<__m256i*>(out + 4 * i + 2), b); + } + } #endif template @@ -413,6 +438,21 @@ namespace osuCrypto } return; } +#elif defined(OC_ENABLE_VAES) && defined(__AVX2__) && defined(__VAES__) + if constexpr (fieldBitsConst == 2) + { + for (; This.mNumVoles - nVole >= 6; nVole += 6, seeds += 18, outW += 12) + { + if (correctionPresent) + { + generateSixTwoBitReceivers(seeds, blockIdxBlock, aes, deltaPtr, correction, outW); + deltaPtr += 12; + correction += 6; + } + else + generateSixTwoBitReceivers(seeds, blockIdxBlock, aes, nullptr, nullptr, outW); + } + } #endif for (; nVole < This.mNumVoles; nVole += volePerSuperBlk) { From e29e7b3f40754b089247c10d2618f57c413149f8 Mon Sep 17 00:00:00 2001 From: Peter Rindal Date: Wed, 25 Nov 2026 14:33:20 -0800 Subject: [PATCH 17/22] Allocate regular PPRF base OTs for immediate overwrite --- libOTe/Tools/Pprf/RegularPprf.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libOTe/Tools/Pprf/RegularPprf.h b/libOTe/Tools/Pprf/RegularPprf.h index 11c51f09..68546281 100644 --- a/libOTe/Tools/Pprf/RegularPprf.h +++ b/libOTe/Tools/Pprf/RegularPprf.h @@ -821,7 +821,7 @@ namespace osuCrypto if (baseOtCount() != static_cast(baseMessages.size())) throw RTE_LOC; - mBaseOTs.resize(mPntCount, mDepth); + mBaseOTs.resize(mPntCount, mDepth, AllocType::Uninitialized); for (u64 i = 0; i < static_cast(mBaseOTs.size()); ++i) mBaseOTs(i) = baseMessages[i]; } @@ -1393,7 +1393,7 @@ namespace osuCrypto if (baseOtCount() != static_cast(baseMessages.size())) throw RTE_LOC; - mBaseOTs.resize(mPntCount, mDepth); + mBaseOTs.resize(mPntCount, mDepth, AllocType::Uninitialized); memcpy(mBaseOTs.data(), baseMessages.data(), baseMessages.size() * sizeof(block)); } From 347cf4751363496dfb0419a4417396745f803735 Mon Sep 17 00:00:00 2001 From: Peter Rindal Date: Wed, 25 Nov 2026 14:42:04 -0800 Subject: [PATCH 18/22] Hash partial AVX2 two-bit VOLE sender batches with initialized padding --- libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp | 18 +++++++++++++----- libOTe_Tests/SoftSpoken_Tests.cpp | 7 ++++++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp b/libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp index e96a2817..f1320cae 100644 --- a/libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp +++ b/libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp @@ -186,12 +186,14 @@ namespace osuCrypto state[i] = _mm256_aesenclast_epi128(state[i], raw[i]); } - static OC_FORCEINLINE void generateFourTwoBitSenders( + template + static OC_FORCEINLINE void generateTwoBitSenders256( const block* seeds, block index, const AES& aes, block* outU, block* outV) { - __m256i state[8]; + static_assert(count == 2 || count == 4); + __m256i state[2 * count]; hashTwoBitSeeds256(seeds, index, aes, state); - for (unsigned i = 0; i < 4; i += 2) + for (unsigned i = 0; i < count; i += 2) { __m256i partial[2]; for (unsigned j = 0; j < 2; ++j) @@ -241,8 +243,12 @@ namespace osuCrypto #elif defined(OC_ENABLE_VAES) && defined(__AVX2__) && defined(__VAES__) if constexpr (fieldBitsConst == 2) { - for (; This.mNumVoles - nVole >= 4; nVole += 4, seeds += 16, outU += 4, outV += 8) - generateFourTwoBitSenders(seeds, blockIdxBlock, aes, outU, outV); + // The generic sender writes complete pairs, so three remaining VOLEs occupy four slots. + for (; nVole + 3 <= This.mNumVoles; nVole += 4, seeds += 16, outU += 4, outV += 8) + generateTwoBitSenders256<4>(seeds, blockIdxBlock, aes, outU, outV); + if (nVole < This.mNumVoles) + generateTwoBitSenders256<2>(seeds, blockIdxBlock, aes, outU, outV); + return; } #endif for (; nVole < This.mNumVoles; nVole += volePerSuperBlk) @@ -541,6 +547,8 @@ namespace osuCrypto assert(mNumVoles <= mNumVolesPadded); mSeeds.resize(mNumVolesPadded * fieldSize()); std::copy(seeds_.begin(), seeds_.end(), mSeeds.data()); + if (mFieldBits <= superBlkShift) + std::fill(mSeeds.begin() + numSeeds, mSeeds.end(), ZeroBlock); } u64 computeNumVolesPadded(u64 fieldBits, u64 numVoles) diff --git a/libOTe_Tests/SoftSpoken_Tests.cpp b/libOTe_Tests/SoftSpoken_Tests.cpp index c184fd24..b311b22b 100644 --- a/libOTe_Tests/SoftSpoken_Tests.cpp +++ b/libOTe_Tests/SoftSpoken_Tests.cpp @@ -34,13 +34,18 @@ namespace tests_libOTe static void twoBitSenderHashTest() { PRNG prng(block(184, 731)); - for (u64 count : {1, 2, 7, 8, 9, 15, 16, 17, 63, 64, 65}) + for (u64 count : {1, 2, 3, 4, 7, 8, 9, 15, 16, 17, 63, 64, 65}) { SmallFieldVoleSender sender; sender.init(2, count, false); std::vector seeds(4 * count); prng.get(seeds.data(), seeds.size()); + sender.mSeeds.resize(4 * sender.uPadded()); + std::fill(sender.mSeeds.begin(), sender.mSeeds.end(), AllOneBlock); sender.setSeed(seeds); + for (u64 i = seeds.size(); i < sender.mSeeds.size(); ++i) + if (sender.mSeeds[i] != ZeroBlock) + throw UnitTestFail("Uninitialized small-field VOLE sender padding"); // Offset both outputs by a block to exercise unaligned SIMD stores. AlignedUnVector u(sender.uPadded() + 1), v(sender.vPadded() + 1); for (u64 index : std::array{0, 1, 1ull << 53, ~0ull}) From d5198ad805cbe2c81c94fc9c48c9d969e2ed80e6 Mon Sep 17 00:00:00 2001 From: Peter Rindal Date: Wed, 25 Nov 2026 14:42:24 -0800 Subject: [PATCH 19/22] Reuse broadcast PPRF leaf keys on the AVX2 receiver --- libOTe/Tools/Pprf/RegularPprf.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/libOTe/Tools/Pprf/RegularPprf.h b/libOTe/Tools/Pprf/RegularPprf.h index 68546281..43fc532a 100644 --- a/libOTe/Tools/Pprf/RegularPprf.h +++ b/libOTe/Tools/Pprf/RegularPprf.h @@ -645,8 +645,10 @@ namespace osuCrypto if constexpr (std::contiguous_iterator && requires { requires CoeffCtx::blockCopyIsTrivialAndAdditionIsXor; }) { - const auto& k0 = pprfVaes::leafKey<0, CacheLeafKeys>(); - const auto& k1 = pprfVaes::leafKey<1, CacheLeafKeys>(); + // The sender and receiver use the same public YMM key schedules. + constexpr bool cache = CacheLeafKeys || std::is_same_v; + const auto& k0 = pprfVaes::leafKey<0, cache>(); + const auto& k1 = pprfVaes::leafKey<1, cache>(); block* dest = std::to_address(output); #ifdef ENABLE_AVX512 __m512i sum0 = _mm512_setzero_si512(), sum1 = _mm512_setzero_si512(); From 42fd061ad0b3701d5b2fabf864974ff7334c2acb Mon Sep 17 00:00:00 2001 From: Peter Rindal Date: Wed, 25 Nov 2026 14:45:16 -0800 Subject: [PATCH 20/22] Consume complete padded triples in the AVX2 VOLE receiver --- libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp | 3 ++- libOTe_Tests/SoftSpoken_Tests.cpp | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp b/libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp index f1320cae..0408a8f2 100644 --- a/libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp +++ b/libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp @@ -447,7 +447,8 @@ namespace osuCrypto #elif defined(OC_ENABLE_VAES) && defined(__AVX2__) && defined(__VAES__) if constexpr (fieldBitsConst == 2) { - for (; This.mNumVoles - nVole >= 6; nVole += 6, seeds += 18, outW += 12) + // Four or five remaining VOLEs occupy two complete padded triples. + for (; nVole + 4 <= This.mNumVoles; nVole += 6, seeds += 18, outW += 12) { if (correctionPresent) { diff --git a/libOTe_Tests/SoftSpoken_Tests.cpp b/libOTe_Tests/SoftSpoken_Tests.cpp index b311b22b..3c144691 100644 --- a/libOTe_Tests/SoftSpoken_Tests.cpp +++ b/libOTe_Tests/SoftSpoken_Tests.cpp @@ -68,7 +68,7 @@ namespace tests_libOTe static void twoBitReceiverHashTest() { PRNG prng(block(137, 54)); - for (u64 count : {1, 3, 11, 12, 13, 23, 24, 25, 63, 64, 65}) + for (u64 count : {1, 3, 4, 5, 6, 11, 12, 13, 23, 24, 25, 63, 64, 65}) { SmallFieldVoleReceiver receiver; receiver.init(2, count, false); From 70f56029df2b1270a5d9cbd59f104b9e53353f6e Mon Sep 17 00:00:00 2001 From: Peter Rindal Date: Wed, 25 Nov 2026 16:49:03 -0800 Subject: [PATCH 21/22] Pin SPIN to the validated Turbo transpose kernels --- thirdparty/getSpin.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/thirdparty/getSpin.cmake b/thirdparty/getSpin.cmake index ff2eb44f..245ddf67 100644 --- a/thirdparty/getSpin.cmake +++ b/thirdparty/getSpin.cmake @@ -1,5 +1,5 @@ set(GIT_REPOSITORY https://github.com/ladnir/spin_codes.git) -set(GIT_TAG "34a03e96291ab1b8ecd146f1f0507b4990758036") +set(GIT_TAG "c3a7ccc9e32513df00e839fcce3cf55ef0df5fbe") set(CLONE_DIR "${OC_THIRDPARTY_CLONE_DIR}/spin") set(BUILD_DIR "${CLONE_DIR}/out/build/${OC_CONFIG}") set(LOG_FILE "${CMAKE_CURRENT_LIST_DIR}/log-spin.txt") From 047d68e50ca28e32e121631ce6846dcb9b5bda69 Mon Sep 17 00:00:00 2001 From: Peter Rindal Date: Wed, 25 Nov 2026 16:53:05 -0800 Subject: [PATCH 22/22] Pin SPIN with the corrected artifact test fixture --- thirdparty/getSpin.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/thirdparty/getSpin.cmake b/thirdparty/getSpin.cmake index 245ddf67..edaa3220 100644 --- a/thirdparty/getSpin.cmake +++ b/thirdparty/getSpin.cmake @@ -1,5 +1,5 @@ set(GIT_REPOSITORY https://github.com/ladnir/spin_codes.git) -set(GIT_TAG "c3a7ccc9e32513df00e839fcce3cf55ef0df5fbe") +set(GIT_TAG "61a301b5ce26c08c7343f10bb0a78af1f6399254") set(CLONE_DIR "${OC_THIRDPARTY_CLONE_DIR}/spin") set(BUILD_DIR "${CLONE_DIR}/out/build/${OC_CONFIG}") set(LOG_FILE "${CMAKE_CURRENT_LIST_DIR}/log-spin.txt")