Skip to content

Instantly share code, notes, and snippets.

@lzjluzijie
Last active September 8, 2023 02:56
Show Gist options
  • Save lzjluzijie/621c0e9043db8fd090dc03f31b1e23c1 to your computer and use it in GitHub Desktop.
Save lzjluzijie/621c0e9043db8fd090dc03f31b1e23c1 to your computer and use it in GitHub Desktop.
libOTe
#include "coproto/Socket/AsioSocket.h"
#include "cryptoTools/Common/CLP.h"
#include "libOTe/Base/BaseOT.h"
#include "libOTe/TwoChooseOne/Iknp/IknpOtExtReceiver.h"
#include "libOTe/TwoChooseOne/Iknp/IknpOtExtSender.h"
#include "libOTe/TwoChooseOne/Kos/KosOtExtReceiver.h"
#include "libOTe/TwoChooseOne/Kos/KosOtExtSender.h"
using namespace osuCrypto;
using namespace std;
const u64 totalOTs = 1024;
const bool correlated = true;
const bool fake = false;
void sender() {
auto chl = cp::asioConnect("127.0.0.1:7700", true);
PRNG prng(sysRandomSeed());
KosOtExtSender sender;
cout << "OK" << endl;
if (fake) {
PRNG commonPRNG(oc::ZeroBlock);
std::array<std::array<block, 2>, 128> sendMsgs;
commonPRNG.get(sendMsgs.data(), sendMsgs.size());
BitVector bv(128);
bv.randomize(commonPRNG);
std::array<block, 128> recvMsgs;
for (u64 i = 0; i < 128; ++i) recvMsgs[i] = sendMsgs[i][bv[i]];
sender.setBaseOts(recvMsgs, bv);
} else {
DefaultBaseOT base;
BitVector bv(128);
std::array<block, 128> baseMsg;
bv.randomize(prng);
cout << "OK" << endl;
// perform the base To, call sync_wait to block until they have completed.
cp::sync_wait(base.receive(bv, baseMsg, prng, chl));
sender.setBaseOts(baseMsg, bv);
cout << "OK" << endl;
}
if (correlated) {
vector<block> cMsgs(totalOTs);
prng.get(cMsgs.data(), cMsgs.size());
cout << "OK" << endl;
cp::sync_wait(sender.sendCorrelated(
cMsgs, [](block m0, u64 i) { return m0; }, prng, chl));
cout << "send" << cMsgs[0] << endl;
} else {
// construct a vector to stored the random send messages.
vector<array<block, 2>> sMsgs(totalOTs);
// Populate msgs with something useful...
prng.get(sMsgs.data(), sMsgs.size());
// perform the OTs. The receiver will learn one
// of the messages stored in msgs.
cp::sync_wait(sender.sendChosen(sMsgs, prng, chl));
cout << "send 0 " << sMsgs[0][0] << endl;
cout << "send 1 " << sMsgs[0][1] << endl;
}
}
void receiver() {
auto chl = cp::asioConnect("127.0.0.1:7700", false);
PRNG prng(sysRandomSeed());
KosOtExtReceiver receiver;
if (fake) {
PRNG commonPRNG(oc::ZeroBlock);
std::array<std::array<block, 2>, 128> sendMsgs;
commonPRNG.get(sendMsgs.data(), sendMsgs.size());
receiver.setBaseOts(sendMsgs);
} else {
DefaultBaseOT base;
std::array<std::array<block, 2>, 128> baseMsg;
// perform the base To, call sync_wait to block until they have completed.
cp::sync_wait(base.send(baseMsg, prng, chl));
receiver.setBaseOts(baseMsg);
}
// construct the choices that we want.
BitVector choice(totalOTs);
// in this case pick random messages.
choice.randomize(prng);
// construct a vector to stored the received messages.
std::vector<block> rMsgs(totalOTs);
if (correlated) {
cp::sync_wait(receiver.receiveCorrelated(choice, rMsgs, prng, chl));
} else {
// perform totalOTs chosen message OTs, the results will be written to
// msgs.
cp::sync_wait(receiver.receiveChosen(choice, rMsgs, prng, chl));
}
cout << "choice " << choice[0] << endl;
cout << rMsgs[0] << endl;
}
int main(int argc, char** argv) {
if (argc == 1) {
std::cout << "Usage: " << argv[0] << "0|1" << std::endl;
return 0;
}
if (argv[1][0] == '0') {
sender();
} else {
receiver();
}
}
#include <cryptoTools/Common/CLP.h>
#include <cryptoTools/Common/Log.h>
#include "libOTe/TwoChooseOne/OTExtInterface.h"
#include "libOTe/Vole/SoftSpokenOT/SmallFieldVole.h"
// #include "libOTe/TwoChooseOne/SoftSpokenOT/SoftSpokenMalLeakyDotExt.h"
#include "libOTe/TwoChooseOne/SoftSpokenOT/SoftSpokenMalOtExt.h"
#include "libOTe/TwoChooseOne/SoftSpokenOT/SoftSpokenShOtExt.h"
// #include "libOTe/TwoChooseOne/SoftSpokenOT/SoftSpokenShOtExt.h"
#include <cryptoTools/Common/BitVector.h>
#include <cryptoTools/Common/Matrix.h>
#include <random>
#include <thread>
#include <vector>
using namespace osuCrypto;
using namespace std;
int main() {
cout << (block(16, 16).gf128Mul(block(0, 256))) << endl;
auto sockets = cp::LocalAsyncSocket::makePair();
PRNG prng0(block(4234385, 3445235));
PRNG prng1(block(42348395, 989835));
u64 numVoles = 16;
size_t fieldBits = 8;
const size_t nBaseOTs = SmallFieldVoleBase::baseOtCount(fieldBits, numVoles);
cout << "nBaseOTs: " << nBaseOTs << endl;
std::vector<std::array<block, 2>> baseSend(nBaseOTs);
std::vector<block> baseRecv(nBaseOTs);
BitVector baseChoice(nBaseOTs);
baseChoice.randomize(prng0);
prng0.get(baseSend.data(), baseSend.size());
for (u64 i = 0; i < nBaseOTs; ++i) baseRecv[i] = baseSend[i][baseChoice[i]];
SmallFieldVoleSender sender;
SmallFieldVoleReceiver recver;
recver.init(fieldBits, numVoles, 0);
sender.init(fieldBits, numVoles, 0);
std::vector<block> u(sender.uPadded()), v(sender.vPadded()),
w(recver.wPadded());
cout << "u.size(): " << u.size() << endl;
cout << "v.size(): " << v.size() << endl;
cout << "w.size(): " << w.size() << endl;
sender.setBaseOts(baseSend);
recver.setBaseOts(baseRecv, baseChoice);
cp::sync_wait(cp::when_all_ready(sender.expand(sockets[0], prng1, 1),
recver.expand(sockets[1], prng0, 1)));
sender.generate(0, mAesFixedKey, u, v);
recver.generate(0, mAesFixedKey, w);
u.resize(numVoles);
BitVector delta = recver.mDelta;
cout << "delta.size(): " << delta.size() << endl;
std::cout << "Delta:\n";
for (size_t i = 0; i < delta.sizeBlocks(); ++i)
std::cout << delta.blocks()[i] << ", ";
std::cout << "\nSeeds:\n";
size_t fieldSize = recver.fieldSize();
cout << "fieldSize: " << fieldSize << endl;
for (size_t i = 0; i < numVoles; ++i) {
size_t deltaI = 0;
for (size_t j = 0; j < fieldBits; ++j)
deltaI += (size_t)delta[i * fieldBits + j] << j;
// for (size_t j = 0; j < fieldSize; ++j)
// std::cout << j << ": " << sender.mSeeds[i * fieldSize + j] << '\n';
// for (size_t j = 1; j < fieldSize; ++j)
// std::cout << j << ": " << recver.mSeeds[i * (fieldSize - 1) + j - 1]
// << '\n';
// for (size_t j = 0; j < fieldSize; ++j) {
// if (j == deltaI)
// // Punctured point.
// continue;
// block senderSeed = sender.mSeeds[i * fieldSize + j];
// block recvSeed = recver.mSeeds[i * (fieldSize - 1) + (j ^ deltaI) - 1];
// }
}
cout << "recver.wSize(): " << recver.wSize() << endl;
for (size_t i = 0; i < 4; ++i) {
// std::cout << "u[" << i << "]: " << u[i] << '\n';
cout << "u[i/fieldBits]: " << u[i / fieldBits] << endl;
std::cout << "v[" << i << "]: " << v[i] << '\n';
std::cout << "w[" << i << "]: " << w[i] << '\n';
std::cout << "delta[" << i << "]: " << delta[i] << '\n';
// cout << "should be v: " << (w[i] ^ (block::allSame((bool)delta[i]) & u[i
// / fieldBits])) << endl;
cout << "left[" << i << "]: " << (w[i] ^ v[i]) << endl;
cout << "right[" << i
<< "]: " << (block::allSame((bool)delta[i]) & u[i / fieldBits])
<< endl;
}
}
#include "coproto/Socket/AsioSocket.h"
#include "cryptoTools/Common/CLP.h"
#include "libOTe/Base/BaseOT.h"
#include "libOTe/TwoChooseOne/Iknp/IknpOtExtReceiver.h"
#include "libOTe/TwoChooseOne/Iknp/IknpOtExtSender.h"
#include "libOTe/TwoChooseOne/Kos/KosOtExtReceiver.h"
#include "libOTe/TwoChooseOne/Kos/KosOtExtSender.h"
#include "libOTe/Vole/Silent/SilentVoleReceiver.h"
#include "libOTe/Vole/Silent/SilentVoleSender.h"
using namespace osuCrypto;
using namespace std;
int numOTs = 2 << 19;
int main(int argc, char** argv) {
auto chl = cp::LocalAsyncSocket::makePair();
PRNG prng(sysRandomSeed());
SilentVoleSender sender;
sender.configure(numOTs);
SilentVoleReceiver receiver;
receiver.configure(numOTs);
// sender.mMultType = MultType::QuasiCyclic;
// receiver.mMultType = MultType::QuasiCyclic;
// Cannot have this
// cp::sync_wait(
// macoro::when_all_ready(sender.genSilentBaseOts(prng, chl[1]),
// receiver.genSilentBaseOts(prng, chl[0])));
cout << sender.mMultType << endl;
vector<block> z0(numOTs);
vector<block> z1(numOTs);
block delta = prng.get();
vector<block> c(numOTs);
cp::sync_wait(macoro::when_all_ready(
sender.silentSend(delta, z1, prng, chl[0]),
receiver.silentReceive(c, z0, prng, chl[1])));
cout << "delta " << delta << endl;
cout << "send { " << z1[0] << ", " << z1[1] << "}" << endl;
cout << "recv { " << z0[0] << ", " << z0[1] << "}" << endl;
cout << "choice " << c[0] << ", " << c[1] << endl;
cout << "left " << (z0[0] ^ z1[0]) << endl;
cout << "right " << c[0].gf128Mul(delta) << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment