Skip to content

Commit

Permalink
simplest ot asm alignment fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ladnir committed Mar 20, 2023
1 parent 701f865 commit 7878333
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 59 deletions.
17 changes: 3 additions & 14 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,9 @@
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "RelWithDebInfo",
"CMAKE_BUILD_TYPE": "Debug",
"ENABLE_ALL_OT": true,
"LIBOTE_STD_VER": "17",
"ENABLE_KKRT": false,
"ENABLE_IKNP": true,
"ENABLE_MR": true,
"ENABLE_SIMPLESTOT": false,
"ENABLE_SIMPLESTOT_ASM": false,
"ENABLE_RELIC": false,
"ENABLE_SODIUM":true,
"ENABLE_BOOST": false,
"FETCH_AUTO": "ON",
"ENABLE_CIRCUITS": true,
"VERBOSE_FETCH": true,
"LIBOTE_STD_VER": "14",
"CMAKE_PREFIX_PATH": "${sourceDir}/../out/install",
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}"
},
Expand All @@ -42,7 +31,7 @@
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"LIBOTE_STD_VER": "17",
"LIBOTE_STD_VER": "14",
"ENABLE_ALL_OT": true,
"ENABLE_KKRT": "ON",
"ENABLE_IKNP": "ON",
Expand Down
3 changes: 3 additions & 0 deletions frontend/benchmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ namespace osuCrypto

inline void SilentOtBench(const CLP& cmd)
{
#ifdef ENABLE_SILENTOT

try
{

Expand Down Expand Up @@ -307,5 +309,6 @@ namespace osuCrypto
{
std::cout << e.what() << std::endl;
}
#endif
}
}
120 changes: 99 additions & 21 deletions libOTe/Base/SimplestOT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,18 +183,18 @@ namespace osuCrypto
return rand;
}

std::string hexPrnt(span<u8> d)
{
std::stringstream ss;
for (auto dd : d)
{
ss << std::setw(2) << std::setfill('0') << std::hex
<< int(dd);
}
return ss.str();
}

std::mutex _gmtx;
//std::string hexPrnt(span<u8> d)
//{
// std::stringstream ss;
// for (auto dd : d)
// {
// ss << std::setw(2) << std::setfill('0') << std::hex
// << int(dd);
// }
// return ss.str();
//}

//std::mutex _gmtx;

struct SendState
{
Expand All @@ -218,7 +218,7 @@ namespace osuCrypto

std::vector<u8> init(PRNG& prng)
{
std::lock_guard<std::mutex>l(_gmtx);
//std::lock_guard<std::mutex>l(_gmtx);
//std::cout << "S0 " << hash() << std::endl;
rand = makeRandSource(prng);
//std::cout << "S1 " << hash() << std::endl;
Expand All @@ -237,7 +237,7 @@ namespace osuCrypto
void gen4(u64 i, span<std::array<block, 2>> msg)
{

std::lock_guard<std::mutex>l(_gmtx);
//std::lock_guard<std::mutex>l(_gmtx);
sender_keygen(&sender, Rs_pack, keys);

auto min = std::min<u32>(4, msg.size() - i);
Expand Down Expand Up @@ -266,7 +266,6 @@ namespace osuCrypto
memset(&cs, 0, sizeof(cs));
}


RecvState(RecvState&& o) = delete;

block hash()
Expand All @@ -293,15 +292,15 @@ namespace osuCrypto

void init(PRNG& prng)
{
std::lock_guard<std::mutex>l(_gmtx);
//std::lock_guard<std::mutex>l(_gmtx);
receiver_procS(&receiver);
receiver_maketable(&receiver);
rand = makeRandSource(prng);
}

std::vector<u8> send4(u64 i, const BitVector& choices)
{
std::lock_guard<std::mutex>l(_gmtx);
//std::lock_guard<std::mutex>l(_gmtx);
auto min = std::min<u32>(4, choices.size() - i);

for (u32 j = 0; j < min; j++)
Expand All @@ -314,7 +313,7 @@ namespace osuCrypto

void gen4(u64 i, span<block> msg)
{
std::lock_guard<std::mutex>l(_gmtx);
//std::lock_guard<std::mutex>l(_gmtx);
auto min = std::min<u32>(4, msg.size() - i);

receiver_keygen(&receiver, keys);
Expand All @@ -324,10 +323,86 @@ namespace osuCrypto
}
};

template<typename State>
struct AlginedState
{
State* ptr = nullptr;
AlginedState()
{
ptr = new (AlignedAllocator<char>{}.aligned_malloc(sizeof(State), 32)) State;
}
AlginedState(AlginedState&& o)
: ptr(std::exchange(o.ptr, nullptr))
{}

~AlginedState()
{
if (ptr)
{
ptr->~State();
AlignedAllocator<char>{}.aligned_free(ptr);
}
}

State* operator->()
{
return ptr;
}
};

}


void AsmSimplestOTTest()
{

for (u64 j = 0; j < 1; ++j)
{


u64 n = 16;

BitVector choices(n);
RecvState recv;
SendState send;
std::vector<std::array<block, 2>> sMsg(n);
std::vector<block> rMsg(n);

PRNG sprng(ZeroBlock);

auto sd = send.init(sprng);
//std::cout << "send 1 " << hexPrnt(sd) << std::endl;
auto rd = recv.recvData();

if (sd.size() != rd.size())
throw RTE_LOC;
memcpy(rd.data(), sd.data(), sd.size());

// recv
PRNG rprng(ZeroBlock);
recv.init(rprng);

for (auto i = 0ull; i < sMsg.size(); i += 4)
{
sd = recv.send4(i, choices);
rd = send.recv4();


if (sd.size() != rd.size())
throw RTE_LOC;
memcpy(rd.data(), sd.data(), sd.size());

//std::cout << "send 2 " << i << std::endl;
send.gen4(i, sMsg);

//MC_AWAIT(chl.send(sd));
//std::cout << "recv 3 " << i << std::endl;
recv.gen4(i, rMsg);
//std::cout << "recv 4 " << i << std::endl;
}
}
}

//void AsmSimplestOT::receive(
// const BitVector& choices,
// span<block> msg,
Expand Down Expand Up @@ -358,12 +433,13 @@ namespace osuCrypto
{

MC_BEGIN(task<>, this, &choices, msg, &prng, &chl,
rs = std::make_unique<RecvState>(),
rs = AlginedState<RecvState>(),
i = u64{},
rd = span<u8>{},
sd = std::vector<u8>{}
);

//prng.SetSeed(ZeroBlock);
//std::cout << "recv 0" << std::endl;
rd = rs->recvData();
MC_AWAIT(chl.recv(rd));
Expand All @@ -388,16 +464,18 @@ namespace osuCrypto
Socket& chl)
{
MC_BEGIN(task<>, this, msg, &prng, &chl,
ss = std::make_unique<SendState>(),
ss = AlginedState<SendState>(),
i = u64{},
rd = span<u8>{},
sd = std::vector<u8>{}
);

//prng.SetSeed(ZeroBlock);

//std::cout << "send 0" << std::endl;
sd = ss->init(prng);
//std::cout << "send 1 " << hexPrnt(sd) << std::endl;
MC_AWAIT(chl.send(sd));
//std::cout << "send 1" << std::endl;

for (i = 0; i < msg.size(); i += 4)
{
Expand Down
44 changes: 37 additions & 7 deletions libOTe/Tools/EACode/EACode.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@

namespace osuCrypto
{

#if __cplusplus >= 201703L
#define EA_CONSTEXPR constexpr
#else
#define EA_CONSTEXPR
#endif
// THe encoder for the generator matrix G = B * A.
// B is the expander while A is the accumulator.
//
Expand Down Expand Up @@ -156,7 +160,7 @@ namespace osuCrypto
OC_FORCEINLINE typename std::enable_if<(count > 1), T>::type
expandOne(const T* __restrict ee, detail::ExpanderModd& prng)const
{
if constexpr (count >= 8)
if EA_CONSTEXPR (count >= 8)
{
u64 rr[8];
T w[8];
Expand Down Expand Up @@ -188,8 +192,11 @@ namespace osuCrypto
w[6] ^
w[7];

if constexpr (count > 8)
ww = ww ^ expandOne<T,count-8>(ee, prng);
if EA_CONSTEXPR(count > 8)
{

ww = ww ^ expandOne<T, (count < 8 ? 0 : count - 8)>(ee, prng);
}
return ww;
}
else
Expand All @@ -211,7 +218,7 @@ namespace osuCrypto
T2* __restrict y2,
detail::ExpanderModd& prng)const
{
if constexpr (count >= 8)
if EA_CONSTEXPR (count >= 8)
{
u64 rr[8];
T w1[8];
Expand Down Expand Up @@ -262,11 +269,11 @@ namespace osuCrypto
w2[6] ^
w2[7];

if constexpr (count > 8)
if EA_CONSTEXPR (count > 8)
{
T yy1;
T2 yy2;
expandOne<T, count - 8>(ee1, ee2, yy1,yy2, prng);
expandOne<T, (count < 8 ? 0 : count - 8)>(ee1, ee2, yy1,yy2, prng);
ww1 = ww1 ^ yy1;
ww2 = ww2 ^ yy2;
}
Expand Down Expand Up @@ -297,6 +304,7 @@ namespace osuCrypto
return ee[r];
}


template<typename T, typename T2, u64 count>
OC_FORCEINLINE typename std::enable_if<count == 1, T>::type
expandOne(
Expand All @@ -312,6 +320,26 @@ namespace osuCrypto
}


template<typename T, u64 count>
OC_FORCEINLINE typename std::enable_if<count == 0, T>::type
expandOne(const T* __restrict ee, detail::ExpanderModd& prng) const
{
return {};
}


template<typename T, typename T2, u64 count>
OC_FORCEINLINE typename std::enable_if<count == 0, T>::type
expandOne(
const T* __restrict ee1,
const T2* __restrict ee2,
T* __restrict y1,
T2* __restrict y2,
detail::ExpanderModd& prng) const
{
}


template<typename T>
void expand(
span<const T> e,
Expand Down Expand Up @@ -545,4 +573,6 @@ namespace osuCrypto
return A.sparse();
}
};

#undef EA_CONSTEXPR
}
6 changes: 6 additions & 0 deletions libOTe/Vole/SoftSpokenOT/SmallFieldVole.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif

#include "SmallFieldVole.h"
#ifdef ENABLE_SOFTSPOKEN_OT

Expand Down Expand Up @@ -25,6 +29,7 @@
#endif



namespace osuCrypto
{

Expand Down Expand Up @@ -90,6 +95,7 @@ namespace osuCrypto
if (!isReceiver)
*outU = path[treeDepth][0];
}

}

template<u64 fieldBitsConst>
Expand Down
Loading

0 comments on commit 7878333

Please sign in to comment.