Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 62 additions & 16 deletions libOTe/TwoChooseOne/Silent/SilentOtExtReceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,53 @@ namespace osuCrypto
throw RTE_LOC;
}


#ifdef OC_ENABLE_VAES
namespace
{
// Each YMM register holds two received blocks. Extract their choice bits,
// clear those bits and compute AES(x) ^ x before writing either message.
OC_FORCEINLINE void hashReceiverVaes(const AES& aes, const block* input,
block* output, u8* choices, u64 count)
{
alignas(32) __m256i key[11];
for (u64 k = 0; k < 11; ++k)
key[k] = _mm256_broadcastsi128_si256(aes.mRoundKey[k].mData);
const auto mask = _mm256_set_epi64x(-1, -2, -1, -2);

for (u64 i = 0; i < count; i += 16)
{
__m256i x0, x1, x2, x3, x4, x5, x6, x7;
__m256i a0, a1, a2, a3, a4, a5, a6, a7;
u32 bits = 0;
#define OC_RECV_LANES(OP) OP(0); OP(1); OP(2); OP(3); OP(4); OP(5); OP(6); OP(7)
#define OC_RECV_LOAD(j) do { \
const auto raw = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(input + i + 2*j)); \
const auto signs = _mm256_movemask_pd(_mm256_castsi256_pd(_mm256_slli_epi64(raw, 63))); \
bits |= u32((signs & 1) | ((signs >> 1) & 2)) << (2*j); \
x##j = _mm256_and_si256(raw, mask); \
a##j = _mm256_xor_si256(x##j, key[0]); \
} while (false)
#define OC_RECV_ROUND(j) a##j = _mm256_aesenc_epi128(a##j, key[k])
#define OC_RECV_STORE(j) _mm256_storeu_si256(reinterpret_cast<__m256i*>(output + i + 2*j), \
_mm256_xor_si256(_mm256_aesenclast_epi128(a##j, key[10]), x##j))
OC_RECV_LANES(OC_RECV_LOAD);
choices[i / 8] = u8(bits);
choices[i / 8 + 1] = u8(bits >> 8);
for (u64 k = 1; k < 10; ++k)
{
OC_RECV_LANES(OC_RECV_ROUND);
}
OC_RECV_LANES(OC_RECV_STORE);
#undef OC_RECV_STORE
#undef OC_RECV_ROUND
#undef OC_RECV_LOAD
#undef OC_RECV_LANES
}
}
}
#endif

// Hashes the OT messages for security and extracts choice bits
void SilentOtExtReceiver::hash(
BitVector& choices,
Expand All @@ -676,7 +723,7 @@ namespace osuCrypto
if ((u64)mA.size() != mRequestNumOts)
throw std::logic_error("Silent OT receiver hash state is not ready. " LOCATION);

auto cIter = choices.begin();
u64 i = 0;
auto n8 = mRequestNumOts / 8 * 8;
auto m = messages.data();
auto r = mA.data();
Expand All @@ -686,8 +733,17 @@ namespace osuCrypto
// Mask to clear the least significant bit (used for choice bit)
block mask = OneBlock ^ AllOneBlock;

#ifdef OC_ENABLE_VAES
i = mRequestNumOts / 16 * 16;
if (i)
{
hashReceiverVaes(mAesFixedKey, r, m, choices.data(), i);
m += i;
r += i;
}
#endif
// Process in blocks of 8 for efficiency
for (u64 i = 0; i < n8; i += 8)
for (; i < n8; i += 8)
{
// Extract the choice bits from the LSB of each block
u32 b0 = r[0].testc(OneBlock);
Expand Down Expand Up @@ -728,21 +784,11 @@ namespace osuCrypto
}

// Process any remaining messages
cIter = cIter + n8;
for (u64 i = n8; i < (u64)messages.size(); ++i)
for (; i < mRequestNumOts; ++i)
{
auto m = &messages[i];
auto r = &mA[i];

// Clear the LSB which contained the choice bit
m[0] = r[0] & mask;

// Hash the message for security
m[0] = mAesFixedKey.hashBlock(m[0]);

// Extract the choice bit
*cIter = r[0].testc(OneBlock);
++cIter;
const block raw = mA[i];
choices[i] = raw.testc(OneBlock);
messages[i] = mAesFixedKey.hashBlock(raw & mask);
}
}
else
Expand Down
38 changes: 38 additions & 0 deletions libOTe_Tests/SilentOT_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,44 @@ namespace {
// Silent OT Extension tests
//=================================================================================================

void OtExt_Silent_receiverHash_Test(const CLP&)
{
#ifdef ENABLE_SILENTOT
PRNG prng(block(42, 19));
SilentOtExtReceiver receiver;
const block mask = AllOneBlock ^ OneBlock;
std::array<block, 50> raw;
prng.get(raw.data(), raw.size());

// Cross three 16-OT batches and cover every 8-OT and scalar tail.
for (u64 n = 0; n <= raw.size(); ++n)
{
receiver.mRequestNumOts = n;
receiver.mA.resize(n);
alignas(32) std::array<block, 54> storage;
for (u64 layout = 0; layout < 3; ++layout)
{
std::copy_n(raw.begin(), n, receiver.mA.begin());
storage.fill(AllOneBlock);
// Test 32-byte alignment, 16-byte-only alignment, and in-place output.
block* out = layout == 2 ? receiver.mA.data() : storage.data() + 2 + layout;
BitVector choices(n);
receiver.hash(choices, span<block>(out, n), ChoiceBitPacking::True);
for (u64 i = 0; i < n; ++i)
{
if (choices[i] != raw[i].testc(OneBlock) ||
out[i] != mAesFixedKey.hashBlock(raw[i] & mask))
throw RTE_LOC;
}
if (layout != 2 && (out[-1] != AllOneBlock || out[n] != AllOneBlock))
throw RTE_LOC;
}
}
#else
throw UnitTestSkipped("Silent OT disabled");
#endif
}

void OtExt_Silent_random_Test(const CLP& cmd)
{
#ifdef ENABLE_SILENTOT
Expand Down
1 change: 1 addition & 0 deletions libOTe_Tests/SilentOT_Tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <cryptoTools/Common/CLP.h>


void OtExt_Silent_receiverHash_Test(const oc::CLP& cmd);
void OtExt_Silent_random_Test(const oc::CLP& cmd);
void OtExt_Silent_correlated_Test(const oc::CLP& cmd);
void OtExt_Silent_inplace_Test(const oc::CLP& cmd);
Expand Down
1 change: 1 addition & 0 deletions libOTe_Tests/UnitTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ namespace tests_libOTe
tc.add("DotExt_Kos_Check_Test ", DotExt_Kos_Check_Test);
tc.add("DotExt_Kos_SplitDelta_Test ", DotExt_Kos_SplitDelta_Test);
tc.add("DotExt_Kos_MapReuse_Test ", DotExt_Kos_MapReuse_Test);
tc.add("OtExt_Silent_receiverHash_Test ", OtExt_Silent_receiverHash_Test);
tc.add("OtExt_Silent_random_Test ", OtExt_Silent_random_Test);
tc.add("OtExt_Silent_correlated_Test ", OtExt_Silent_correlated_Test);
tc.add("OtExt_Silent_inplace_Test ", OtExt_Silent_inplace_Test);
Expand Down
Loading