From f9ba771a49d5a334b01bc8d56e368b4a23e97264 Mon Sep 17 00:00:00 2001 From: Peter Rindal Date: Mon, 22 Jan 2024 15:10:22 -0800 Subject: [PATCH] reworked examples and ENABLED fixes --- CMakePresets.json | 15 +- cmake/buildOptions.cmake | 19 +- cmake/buildOptions.cmake.in | 1 + frontend/ExampleBase.cpp | 154 ++++++++++ frontend/ExampleBase.h | 59 +--- frontend/ExampleNChooseOne.cpp | 245 ++++++++++++++++ frontend/ExampleNChooseOne.h | 226 +-------------- frontend/ExampleSilent.cpp | 189 +++++++++++++ frontend/ExampleSilent.h | 183 +----------- frontend/ExampleTwoChooseOne.cpp | 356 ++++++++++++++++++++++++ frontend/ExampleTwoChooseOne.h | 313 +-------------------- frontend/ExampleVole.cpp | 111 ++++++++ frontend/ExampleVole.h | 136 +-------- frontend/benchmark.h | 77 +---- frontend/main.cpp | 138 +-------- frontend/util.cpp | 3 + frontend/util.h | 27 +- libOTe/Tools/Pprf/RegularPprf.h | 7 +- libOTe/Vole/Silent/SilentVoleReceiver.h | 10 +- libOTe/Vole/Silent/SilentVoleSender.h | 4 + libOTe/config.h.in | 2 + libOTe_Tests/Pprf_Tests.cpp | 22 ++ libOTe_Tests/Vole_Tests.cpp | 31 ++- 23 files changed, 1188 insertions(+), 1140 deletions(-) create mode 100644 frontend/ExampleBase.cpp create mode 100644 frontend/ExampleNChooseOne.cpp create mode 100644 frontend/ExampleSilent.cpp create mode 100644 frontend/ExampleTwoChooseOne.cpp create mode 100644 frontend/ExampleVole.cpp diff --git a/CMakePresets.json b/CMakePresets.json index 20225f0a..5b7eb95d 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -13,6 +13,7 @@ "ENABLE_ALL_OT": true, "ENABLE_SSE": true, "ENABLE_AVX": true, + "ENABLE_BOOST": true, "ENABLE_BITPOLYMUL": false, "ENABLE_CIRCUITS": true, "LIBOTE_STD_VER": "17", @@ -43,17 +44,13 @@ "cacheVariables": { "CMAKE_BUILD_TYPE": "Debug", "ENABLE_INSECURE_SILVER": false, - "ENABLE_LDPC": false, + "ENABLE_PPRF": true, + "ENABLE_SILENT_VOLE": true, "LIBOTE_STD_VER": "17", - "ENABLE_ALL_OT": true, - "ENABLE_KKRT": "ON", - "ENABLE_IKNP": "ON", - "ENABLE_MR": "ON", - "ENABLE_SIMPLESTOT": "ON", "ENABLE_RELIC": false, "ENABLE_SODIUM": true, - "ENABLE_BOOST": false, - "ENABLE_BITPOLYMUL": true, + "ENABLE_BOOST": true, + "ENABLE_BITPOLYMUL": false, "FETCH_AUTO": "ON", "ENABLE_CIRCUITS": true, "VERBOSE_FETCH": true, @@ -88,7 +85,7 @@ "ENABLE_ALL_OT": true, "ENABLE_RELIC": true, "ENABLE_SODIUM": false, - "ENABLE_BOOST": false, + "ENABLE_BOOST": true, "ENABLE_OPENSSL": false, "FETCH_AUTO": true, "ENABLE_CIRCUITS": true, diff --git a/cmake/buildOptions.cmake b/cmake/buildOptions.cmake index 4d05ed90..cefdd3c4 100644 --- a/cmake/buildOptions.cmake +++ b/cmake/buildOptions.cmake @@ -94,9 +94,8 @@ option(ENABLE_DELTA_IKNP "Build the IKNP Delta-OT-Ext protocol." OFF) option(ENABLE_OOS "Build the OOS 1-oo-N OT-Ext protocol." OFF) option(ENABLE_KKRT "Build the KKRT 1-oo-N OT-Ext protocol." OFF) +option(ENABLE_PPRF "Build the PPRF protocol." OFF) option(ENABLE_SILENT_VOLE "Build the Silent Vole protocol." OFF) -#option(COPROTO_ENABLE_BOOST "Build with coproto boost support." OFF) -#option(COPROTO_ENABLE_OPENSSL "Build with coproto boost open ssl support." OFF) option(ENABLE_INSECURE_SILVER "Build with silver codes." OFF) option(ENABLE_LDPC "Build with ldpc functions." OFF) @@ -105,21 +104,14 @@ if(ENABLE_INSECURE_SILVER) endif() option(NO_KOS_WARNING "Build with no kos security warning." OFF) -#option(FETCH_BITPOLYMUL "download and build bitpolymul" OFF)) EVAL(FETCH_BITPOLYMUL_IMPL (DEFINED FETCH_BITPOLYMUL AND FETCH_BITPOLYMUL) OR ((NOT DEFINED FETCH_BITPOLYMUL) AND (FETCH_AUTO AND ENABLE_BITPOLYMUL))) - -#option(FETCH_BITPOLYMUL "download and build bitpolymul" OFF)) -EVAL(FETCH_BITPOLYMUL_IMPL - (DEFINED FETCH_BITPOLYMUL AND FETCH_BITPOLYMUL) OR - ((NOT DEFINED FETCH_BITPOLYMUL) AND (FETCH_AUTO AND ENABLE_BITPOLYMUL))) - - - - +if(ENABLE_SILENT_VOLE OR ENABLE_SILENTOT OR ENABLE_SOFTSPOKEN_OT) + set(ENABLE_PPRF true) +endif() option(VERBOSE_FETCH "Print build info for fetched libraries" ON) @@ -159,7 +151,8 @@ message(STATUS "Option: ENABLE_KKRT = ${ENABLE_KKRT}\n\n") message(STATUS "other \n=======================================================") -message(STATUS "Option: NO_KOS_WARNING = ${NO_KOS_WARNING}\n\n") +message(STATUS "Option: NO_KOS_WARNING = ${NO_KOS_WARNING}") +message(STATUS "Option: ENABLE_PPRF = ${ENABLE_PPRF}\n\n") ############################################# # Config Checks # diff --git a/cmake/buildOptions.cmake.in b/cmake/buildOptions.cmake.in index 1c8cf600..f3329773 100644 --- a/cmake/buildOptions.cmake.in +++ b/cmake/buildOptions.cmake.in @@ -70,6 +70,7 @@ set(ENABLE_KKRT @ENABLE_KKRT@) set(ENABLE_SILENT_VOLE @ENABLE_SILENT_VOLE@) set(NO_SILVER_WARNING @NO_SILVER_WARNING@) +set(ENABLE_PPRF @ENABLE_PPRF@) set(libOTe_boost_FOUND ${ENABLE_BOOST}) set(libOTe_relic_FOUND ${ENABLE_RELIC}) diff --git a/frontend/ExampleBase.cpp b/frontend/ExampleBase.cpp new file mode 100644 index 00000000..13b2726d --- /dev/null +++ b/frontend/ExampleBase.cpp @@ -0,0 +1,154 @@ + +#include "libOTe/Base/SimplestOT.h" +#include "libOTe/Base/McRosRoyTwist.h" +#include "libOTe/Base/McRosRoy.h" +#include "libOTe/Tools/Popf/EKEPopf.h" +#include "libOTe/Tools/Popf/MRPopf.h" +#include "libOTe/Tools/Popf/FeistelPopf.h" +#include "libOTe/Tools/Popf/FeistelMulPopf.h" +#include "libOTe/Tools/Popf/FeistelRistPopf.h" +#include "libOTe/Tools/Popf/FeistelMulRistPopf.h" +#include "libOTe/Base/MasnyRindal.h" +#include "libOTe/Base/MasnyRindalKyber.h" + +#include "cryptoTools/Common/BitVector.h" +#include "cryptoTools/Common/CLP.h" +#include "util.h" +#include "coproto/Socket/AsioSocket.h" +#include "cryptoTools/Common/Timer.h" + +namespace osuCrypto +{ + + template + void baseOT_example_from_ot(Role role, int totalOTs, int numThreads, std::string ip, std::string tag, const CLP&, BaseOT ot) + { +#ifdef COPROTO_ENABLE_BOOST + PRNG prng(sysRandomSeed()); + + if (totalOTs == 0) + totalOTs = 128; + + if (numThreads > 1) + std::cout << "multi threading for the base OT example is not implemented.\n" << std::flush; + + Timer t; + Timer::timeUnit s; + if (role == Role::Receiver) + { + auto sock = cp::asioConnect(ip, false); + BaseOT recv = ot; + + AlignedVector msg(totalOTs); + BitVector choice(totalOTs); + choice.randomize(prng); + + + s = t.setTimePoint("base OT start"); + + coproto::sync_wait(recv.receive(choice, msg, prng, sock)); + + // make sure all messages are sent. + cp::sync_wait(sock.flush()); + } + else + { + auto sock = cp::asioConnect(ip, true); + + BaseOT send = ot; + + AlignedVector> msg(totalOTs); + + s = t.setTimePoint("base OT start"); + + coproto::sync_wait(send.send(msg, prng, sock)); + + + // make sure all messages are sent. + cp::sync_wait(sock.flush()); + } + + + + auto e = t.setTimePoint("base OT end"); + auto milli = std::chrono::duration_cast(e - s).count(); + + std::cout << tag << (role == Role::Receiver ? " (receiver)" : " (sender)") + << " n=" << totalOTs << " " << milli << " ms" << std::endl; +#endif + } + + template + void baseOT_example(Role role, int totalOTs, int numThreads, std::string ip, std::string tag, const CLP& clp) + { + return baseOT_example_from_ot(role, totalOTs, numThreads, ip, tag, clp, BaseOT()); + } + + bool baseOT_examples(const CLP& cmd) + { + bool flagSet = false; + +#ifdef ENABLE_SIMPLESTOT + flagSet |= runIf(baseOT_example, cmd, simple); +#endif + +#ifdef ENABLE_SIMPLESTOT_ASM + flagSet |= runIf(baseOT_example, cmd, simpleasm); +#endif + +#ifdef ENABLE_MRR_TWIST +#ifdef ENABLE_SSE + flagSet |= runIf([&](Role role, int totalOTs, int numThreads, std::string ip, std::string tag, const CLP& clp) { + DomainSepEKEPopf factory; + const char* domain = "EKE POPF OT example"; + factory.Update(domain, std::strlen(domain)); + baseOT_example_from_ot(role, totalOTs, numThreads, ip, tag, clp, McRosRoyTwist(factory)); + }, cmd, moellerpopf, { "eke" }); +#endif + + flagSet |= runIf([&](Role role, int totalOTs, int numThreads, std::string ip, std::string tag, const CLP& clp) { + DomainSepMRPopf factory; + const char* domain = "MR POPF OT example"; + factory.Update(domain, std::strlen(domain)); + baseOT_example_from_ot(role, totalOTs, numThreads, ip, tag, clp, McRosRoyTwistMR(factory)); + }, cmd, moellerpopf, { "mrPopf" }); + + flagSet |= runIf([&](Role role, int totalOTs, int numThreads, std::string ip, std::string tag, const CLP& clp) { + DomainSepFeistelPopf factory; + const char* domain = "Feistel POPF OT example"; + factory.Update(domain, std::strlen(domain)); + baseOT_example_from_ot(role, totalOTs, numThreads, ip, tag, clp, McRosRoyTwistFeistel(factory)); + }, cmd, moellerpopf, { "feistel" }); + + flagSet |= runIf([&](Role role, int totalOTs, int numThreads, std::string ip, std::string tag, const CLP& clp) { + DomainSepFeistelMulPopf factory; + const char* domain = "Feistel With Multiplication POPF OT example"; + factory.Update(domain, std::strlen(domain)); + baseOT_example_from_ot(role, totalOTs, numThreads, ip, tag, clp, McRosRoyTwistMul(factory)); + }, cmd, moellerpopf, { "feistelMul" }); +#endif + +#ifdef ENABLE_MRR + flagSet |= runIf([&](Role role, int totalOTs, int numThreads, std::string ip, std::string tag, const CLP& clp) { + DomainSepFeistelRistPopf factory; + const char* domain = "Feistel POPF OT example (Risretto)"; + factory.Update(domain, std::strlen(domain)); + baseOT_example_from_ot(role, totalOTs, numThreads, ip, tag, clp, McRosRoy(factory)); + }, cmd, ristrettopopf, { "feistel" }); + + flagSet |= runIf([&](Role role, int totalOTs, int numThreads, std::string ip, std::string tag, const CLP& clp) { + DomainSepFeistelMulRistPopf factory; + const char* domain = "Feistel With Multiplication POPF OT example (Risretto)"; + factory.Update(domain, std::strlen(domain)); + baseOT_example_from_ot(role, totalOTs, numThreads, ip, tag, clp, McRosRoyMul(factory)); + }, cmd, ristrettopopf, { "feistelMul" }); +#endif + +#ifdef ENABLE_MR + flagSet |= runIf(baseOT_example, cmd, mr); +#endif + + return flagSet; + } + +} diff --git a/frontend/ExampleBase.h b/frontend/ExampleBase.h index a4684405..762d3b78 100644 --- a/frontend/ExampleBase.h +++ b/frontend/ExampleBase.h @@ -14,65 +14,8 @@ #include "cryptoTools/Common/BitVector.h" #include "cryptoTools/Common/CLP.h" -#include "util.h" -#include "coproto/Socket/AsioSocket.h" namespace osuCrypto { - - template - void baseOT_example_from_ot(Role role, int totalOTs, int numThreads, std::string ip, std::string tag, CLP&, BaseOT ot) - { -#ifdef COPROTO_ENABLE_BOOST - PRNG prng(sysRandomSeed()); - - if (totalOTs == 0) - totalOTs = 128; - - if (numThreads > 1) - std::cout << "multi threading for the base OT example is not implemented.\n" << std::flush; - - Timer t; - Timer::timeUnit s; - if (role == Role::Receiver) - { - auto sock = cp::asioConnect(ip, false); - BaseOT recv = ot; - - AlignedVector msg(totalOTs); - BitVector choice(totalOTs); - choice.randomize(prng); - - - s = t.setTimePoint("base OT start"); - - coproto::sync_wait(recv.receive(choice, msg, prng, sock)); - - } - else - { - auto sock = cp::asioConnect(ip, true); - - BaseOT send = ot; - - AlignedVector> msg(totalOTs); - - s = t.setTimePoint("base OT start"); - - coproto::sync_wait(send.send(msg, prng, sock)); - } - - auto e = t.setTimePoint("base OT end"); - auto milli = std::chrono::duration_cast(e - s).count(); - - std::cout << tag << (role == Role::Receiver ? " (receiver)" : " (sender)") - << " n=" << totalOTs << " " << milli << " ms" << std::endl; -#endif - } - - template - void baseOT_example(Role role, int totalOTs, int numThreads, std::string ip, std::string tag, CLP& clp) - { - return baseOT_example_from_ot(role, totalOTs, numThreads, ip, tag, clp, BaseOT()); - } + bool baseOT_examples(const CLP& clp); } diff --git a/frontend/ExampleNChooseOne.cpp b/frontend/ExampleNChooseOne.cpp new file mode 100644 index 00000000..86bd1d5f --- /dev/null +++ b/frontend/ExampleNChooseOne.cpp @@ -0,0 +1,245 @@ + +#include "cryptoTools/Common/Matrix.h" +#include "libOTe/NChooseOne/Oos/OosNcoOtReceiver.h" +#include "libOTe/NChooseOne/Oos/OosNcoOtSender.h" +#include "libOTe/NChooseOne/Kkrt/KkrtNcoOtReceiver.h" +#include "libOTe/NChooseOne/Kkrt/KkrtNcoOtSender.h" +#include "cryptoTools/Common/Matrix.h" +#include "libOTe/Tools/Coproto.h" +#include "util.h" +#include "coproto/Socket/AsioSocket.h" + +namespace osuCrypto +{ + + + + auto chls = cp::LocalAsyncSocket::makePair(); + + template + void NChooseOne_example(Role role, int totalOTs, int numThreads, std::string ip, std::string tag, const CLP&) + { +#ifdef COPROTO_ENABLE_BOOST + const u64 step = 1024; + + if (totalOTs == 0) + totalOTs = 1 << 20; + + bool randomOT = true; + u64 numOTs = (u64)totalOTs; + auto numChosenMsgs = 256; + + // get up the networking + auto chl = cp::asioConnect(ip, role == Role::Sender); + //auto chl = role == Role::Sender ? chls[0] : chls[1]; + + PRNG prng(ZeroBlock);// sysRandomSeed()); + + NcoOtSender sender; + NcoOtReceiver recver; + + // all Nco Ot extenders must have configure called first. This determines + // a variety of parameters such as how many base OTs are required. + bool maliciousSecure = false; + u64 statSecParam = 40; + u64 inputBitCount = 76; // the kkrt protocol default to 128 but oos can only do 76. + + // create a lambda function that performs the computation of a single receiver thread. + auto recvRoutine = [&]() -> task<> + { + MC_BEGIN(task<>, &, + i = u64{}, min = u64{}, + recvMsgs = std::vector{}, + choices = std::vector{} + ); + + recver.configure(maliciousSecure, statSecParam, inputBitCount); + //MC_AWAIT(sync(chl, Role::Receiver)); + + if (randomOT) + { + // once configure(...) and setBaseOts(...) are called, + // we can compute many batches of OTs. First we need to tell + // the instance how many OTs we want in this batch. This is done here. + MC_AWAIT(recver.init(numOTs, prng, chl)); + + // now we can iterate over the OTs and actually retrieve the desired + // messages. However, for efficiency we will do this in steps where + // we do some computation followed by sending off data. This is more + // efficient since data will be sent in the background :). + for (i = 0; i < numOTs; ) + { + // figure out how many OTs we want to do in this step. + min = std::min(numOTs - i, step); + + //// iterate over this step. + for (u64 j = 0; j < min; ++j, ++i) + { + // For the OT index by i, we need to pick which + // one of the N OT messages that we want. For this + // example we simply pick a random one. Note only the + // first log2(N) bits of choice is considered. + block choice = prng.get(); + + // this will hold the (random) OT message of our choice + block otMessage; + + // retrieve the desired message. + recver.encode(i, &choice, &otMessage); + + // do something cool with otMessage + //otMessage; + } + + // Note that all OTs in this region must be encode. If there are some + // that you don't actually care about, then you can skip them by calling + // + // recver.zeroEncode(i); + // + + // Now that we have gotten out the OT mMessages for this step, + // we are ready to send over network some information that + // allows the sender to also compute the OT mMessages. Since we just + // encoded "min" OT mMessages, we will tell the class to send the + // next min "correction" values. + MC_AWAIT(recver.sendCorrection(chl, min)); + } + + // once all numOTs have been encoded and had their correction values sent + // we must call check. This allows to sender to make sure we did not cheat. + // For semi-honest protocols, this can and will be skipped. + MC_AWAIT(recver.check(chl, prng.get())); + } + else + { + recvMsgs.resize(numOTs); + choices.resize(numOTs); + + // define which messages the receiver should learn. + for (i = 0; i < numOTs; ++i) + choices[i] = prng.get(); + + // the messages that were learned are written to recvMsgs. + MC_AWAIT(recver.receiveChosen(numChosenMsgs, recvMsgs, choices, prng, chl)); + } + + MC_AWAIT(chl.flush()); + MC_END(); + }; + + // create a lambda function that performs the computation of a single sender thread. + auto sendRoutine = [&]() + { + MC_BEGIN(task<>, &, + sendMessages = Matrix{}, + i = u64{}, min = u64{} + ); + + sender.configure(maliciousSecure, statSecParam, inputBitCount); + //MC_AWAIT(sync(chl, Role::Sender)); + + if (randomOT) + { + // Same explanation as above. + MC_AWAIT(sender.init(numOTs, prng, chl)); + + // Same explanation as above. + for (i = 0; i < numOTs; ) + { + // Same explanation as above. + min = std::min(numOTs - i, step); + + // unlike for the receiver, before we call encode to get + // some desired OT message, we must call recvCorrection(...). + // This receivers some information that the receiver had sent + // and allows the sender to compute any OT message that they desired. + // Note that the step size must match what the receiver used. + // If this is unknown you can use recvCorrection(chl) -> u64 + // which will tell you how many were sent. + MC_AWAIT(sender.recvCorrection(chl, min)); + + // we now encode any OT message with index less that i + min. + for (u64 j = 0; j < min; ++j, ++i) + { + // in particular, the sender can retrieve many OT messages + // at a single index, in this case we chose to retrieve 3 + // but that is arbitrary. + auto choice0 = prng.get(); + auto choice1 = prng.get(); + auto choice2 = prng.get(); + + // these we hold the actual OT messages. + block + otMessage0, + otMessage1, + otMessage2; + + // now retrieve the messages + sender.encode(i, &choice0, &otMessage0); + sender.encode(i, &choice1, &otMessage1); + sender.encode(i, &choice2, &otMessage2); + } + } + + // This call is required to make sure the receiver did not cheat. + // All corrections must be received before this is called. + MC_AWAIT(sender.check(chl, ZeroBlock)); + } + else + { + // populate this with the messages that you want to send. + sendMessages.resize(numOTs, numChosenMsgs); + prng.get(sendMessages.data(), sendMessages.size()); + + // perform the OTs with the given messages. + MC_AWAIT(sender.sendChosen(sendMessages, prng, chl)); + + } + + MC_AWAIT(chl.flush()); + MC_END(); + }; + + + Timer time; + auto s = time.setTimePoint("start"); + + + task<> proto; + if (role == Role::Sender) + proto = sendRoutine(); + else + proto = recvRoutine(); + try + { + cp::sync_wait(proto); + } + catch (std::exception& e) + { + std::cout << e.what() << std::endl; + } + + auto e = time.setTimePoint("finish"); + auto milli = std::chrono::duration_cast(e - s).count(); + + if (role == Role::Sender) + std::cout << tag << " n=" << totalOTs << " " << milli << " ms " << std::endl; +#endif + } + + + bool NChooseOne_Examples(const CLP& cmd) + { + bool flagSet = false; +#ifdef ENABLE_KKRT + flagSet |= runIf(NChooseOne_example, cmd, kkrt); +#endif + +#ifdef ENABLE_OOS + flagSet |= runIf(NChooseOne_example, cmd, oos); +#endif + + return flagSet; + } + +} diff --git a/frontend/ExampleNChooseOne.h b/frontend/ExampleNChooseOne.h index 7d886cc4..571657ed 100644 --- a/frontend/ExampleNChooseOne.h +++ b/frontend/ExampleNChooseOne.h @@ -1,230 +1,8 @@ #pragma once - - -#include "cryptoTools/Common/Matrix.h" -#include "libOTe/NChooseOne/Oos/OosNcoOtReceiver.h" -#include "libOTe/NChooseOne/Oos/OosNcoOtSender.h" -#include "libOTe/NChooseOne/Kkrt/KkrtNcoOtReceiver.h" -#include "libOTe/NChooseOne/Kkrt/KkrtNcoOtSender.h" -#include "cryptoTools/Common/Matrix.h" -#include "libOTe/Tools/Coproto.h" +#include "cryptoTools/Common/CLP.h" namespace osuCrypto { - - - auto chls = cp::LocalAsyncSocket::makePair(); - - template - void NChooseOne_example(Role role, int totalOTs, int numThreads, std::string ip, std::string tag, CLP&) - { -#ifdef COPROTO_ENABLE_BOOST - const u64 step = 1024; - - if (totalOTs == 0) - totalOTs = 1 << 20; - - bool randomOT = true; - u64 numOTs = (u64)totalOTs; - auto numChosenMsgs = 256; - - // get up the networking - auto chl = cp::asioConnect(ip, role == Role::Sender); - //auto chl = role == Role::Sender ? chls[0] : chls[1]; - - PRNG prng(ZeroBlock);// sysRandomSeed()); - - NcoOtSender sender; - NcoOtReceiver recver; - - // all Nco Ot extenders must have configure called first. This determines - // a variety of parameters such as how many base OTs are required. - bool maliciousSecure = false; - u64 statSecParam = 40; - u64 inputBitCount = 76; // the kkrt protocol default to 128 but oos can only do 76. - - // create a lambda function that performs the computation of a single receiver thread. - auto recvRoutine = [&]() -> task<> - { - MC_BEGIN(task<>,&, - i = u64{}, min = u64{}, - recvMsgs = std::vector{}, - choices = std::vector{} - ); - - recver.configure(maliciousSecure, statSecParam, inputBitCount); - //MC_AWAIT(sync(chl, Role::Receiver)); - - if (randomOT) - { - // once configure(...) and setBaseOts(...) are called, - // we can compute many batches of OTs. First we need to tell - // the instance how many OTs we want in this batch. This is done here. - MC_AWAIT(recver.init(numOTs, prng, chl)); - - // now we can iterate over the OTs and actually retrieve the desired - // messages. However, for efficiency we will do this in steps where - // we do some computation followed by sending off data. This is more - // efficient since data will be sent in the background :). - for (i = 0; i < numOTs; ) - { - // figure out how many OTs we want to do in this step. - min = std::min(numOTs - i, step); - - //// iterate over this step. - for (u64 j = 0; j < min; ++j, ++i) - { - // For the OT index by i, we need to pick which - // one of the N OT messages that we want. For this - // example we simply pick a random one. Note only the - // first log2(N) bits of choice is considered. - block choice = prng.get(); - - // this will hold the (random) OT message of our choice - block otMessage; - - // retrieve the desired message. - recver.encode(i, &choice, &otMessage); - - // do something cool with otMessage - //otMessage; - } - - // Note that all OTs in this region must be encode. If there are some - // that you don't actually care about, then you can skip them by calling - // - // recver.zeroEncode(i); - // - - // Now that we have gotten out the OT mMessages for this step, - // we are ready to send over network some information that - // allows the sender to also compute the OT mMessages. Since we just - // encoded "min" OT mMessages, we will tell the class to send the - // next min "correction" values. - MC_AWAIT(recver.sendCorrection(chl, min)); - } - - // once all numOTs have been encoded and had their correction values sent - // we must call check. This allows to sender to make sure we did not cheat. - // For semi-honest protocols, this can and will be skipped. - MC_AWAIT(recver.check(chl, prng.get())); - } - else - { - recvMsgs.resize(numOTs); - choices.resize(numOTs); - - // define which messages the receiver should learn. - for (i = 0; i < numOTs; ++i) - choices[i] = prng.get(); - - // the messages that were learned are written to recvMsgs. - MC_AWAIT(recver.receiveChosen(numChosenMsgs, recvMsgs, choices, prng, chl)); - } - - MC_AWAIT(chl.flush()); - MC_END(); - }; - - // create a lambda function that performs the computation of a single sender thread. - auto sendRoutine = [&]() - { - MC_BEGIN(task<>,&, - sendMessages = Matrix{}, - i = u64{}, min = u64{} - ); - - sender.configure(maliciousSecure, statSecParam, inputBitCount); - //MC_AWAIT(sync(chl, Role::Sender)); - - if (randomOT) - { - // Same explanation as above. - MC_AWAIT(sender.init(numOTs, prng, chl)); - - // Same explanation as above. - for (i = 0; i < numOTs; ) - { - // Same explanation as above. - min = std::min(numOTs - i, step); - - // unlike for the receiver, before we call encode to get - // some desired OT message, we must call recvCorrection(...). - // This receivers some information that the receiver had sent - // and allows the sender to compute any OT message that they desired. - // Note that the step size must match what the receiver used. - // If this is unknown you can use recvCorrection(chl) -> u64 - // which will tell you how many were sent. - MC_AWAIT(sender.recvCorrection(chl, min)); - - // we now encode any OT message with index less that i + min. - for (u64 j = 0; j < min; ++j, ++i) - { - // in particular, the sender can retrieve many OT messages - // at a single index, in this case we chose to retrieve 3 - // but that is arbitrary. - auto choice0 = prng.get(); - auto choice1 = prng.get(); - auto choice2 = prng.get(); - - // these we hold the actual OT messages. - block - otMessage0, - otMessage1, - otMessage2; - - // now retrieve the messages - sender.encode(i, &choice0, &otMessage0); - sender.encode(i, &choice1, &otMessage1); - sender.encode(i, &choice2, &otMessage2); - } - } - - // This call is required to make sure the receiver did not cheat. - // All corrections must be received before this is called. - MC_AWAIT(sender.check(chl, ZeroBlock)); - } - else - { - // populate this with the messages that you want to send. - sendMessages.resize(numOTs, numChosenMsgs); - prng.get(sendMessages.data(), sendMessages.size()); - - // perform the OTs with the given messages. - MC_AWAIT(sender.sendChosen(sendMessages, prng, chl)); - - } - - MC_AWAIT(chl.flush()); - MC_END(); - }; - - - Timer time; - auto s = time.setTimePoint("start"); - - - task<> proto; - if (role == Role::Sender) - proto = sendRoutine(); - else - proto = recvRoutine(); - try - { - cp::sync_wait(proto); - } - catch (std::exception& e) - { - std::cout << e.what() << std::endl; - } - - auto e = time.setTimePoint("finish"); - auto milli = std::chrono::duration_cast(e - s).count(); - - if (role == Role::Sender) - std::cout << tag << " n=" << totalOTs << " " << milli << " ms " << std::endl; -#endif - } - + bool NChooseOne_Examples(const CLP& cmd); } diff --git a/frontend/ExampleSilent.cpp b/frontend/ExampleSilent.cpp new file mode 100644 index 00000000..c2b17d02 --- /dev/null +++ b/frontend/ExampleSilent.cpp @@ -0,0 +1,189 @@ + +#include "libOTe/TwoChooseOne/Silent/SilentOtExtReceiver.h" +#include "libOTe/TwoChooseOne/Silent/SilentOtExtSender.h" +#include "util.h" +#include + +#include "cryptoTools/Network/IOService.h" +#include "coproto/Socket/AsioSocket.h" + +namespace osuCrypto +{ + + void Silent_example(Role role, u64 numOTs, u64 numThreads, std::string ip, std::string tag, const CLP& cmd) + { +#if defined(ENABLE_SILENTOT) && defined(COPROTO_ENABLE_BOOST) + + if (numOTs == 0) + numOTs = 1 << 20; + + // get up the networking + auto chl = cp::asioConnect(ip, role == Role::Sender); + + + PRNG prng(sysRandomSeed()); + + bool fakeBase = cmd.isSet("fakeBase"); + u64 trials = cmd.getOr("trials", 1); + auto malicious = cmd.isSet("mal") ? SilentSecType::Malicious : SilentSecType::SemiHonest; + + auto multType = (MultType)cmd.getOr("multType", (int)DefaultMultType); + + std::vector types; + if (cmd.isSet("base")) + types.push_back(SilentBaseType::Base); + else + types.push_back(SilentBaseType::BaseExtend); + + macoro::thread_pool threadPool; + auto work = threadPool.make_work(); + if (numThreads > 1) + threadPool.create_threads(numThreads); + + for (auto type : types) + { + for (u64 tt = 0; tt < trials; ++tt) + { + Timer timer; + auto start = timer.setTimePoint("start"); + if (role == Role::Sender) + { + SilentOtExtSender sender; + + // optionally request the LPN encoding matrix. + sender.mMultType = multType; + + // optionally configure the sender. default is semi honest security. + sender.configure(numOTs, 2, numThreads, malicious); + + if (fakeBase) + { + auto nn = sender.baseOtCount(); + BitVector bits(nn); + bits.randomize(prng); + std::vector> baseSendMsgs(bits.size()); + std::vector baseRecvMsgs(bits.size()); + + auto commonPrng = PRNG(ZeroBlock); + commonPrng.get(baseSendMsgs.data(), baseSendMsgs.size()); + for (u64 i = 0; i < bits.size(); ++i) + baseRecvMsgs[i] = baseSendMsgs[i][bits[i]]; + + sender.setBaseOts(baseRecvMsgs, bits); + } + else + { + // optional. You can request that the base ot are generated either + // using just base OTs (few rounds, more computation) or 128 base OTs and then extend those. + // The default is the latter, base + extension. + cp::sync_wait(sender.genSilentBaseOts(prng, chl, type == SilentBaseType::BaseExtend)); + } + + std::vector> messages(numOTs); + + // create the protocol object. + auto protocol = sender.silentSend(messages, prng, chl); + + // run the protocol + if (numThreads <= 1) + cp::sync_wait(protocol); + else + // launch the protocol on the thread pool. + cp::sync_wait(std::move(protocol) | macoro::start_on(threadPool)); + + // messages has been populated with random OT messages. + // See the header for other options. + } + else + { + + SilentOtExtReceiver recver; + + // optionally request the LPN encoding matrix. + recver.mMultType = multType; + + // configure the sender. optional for semi honest security... + recver.configure(numOTs, 2, numThreads, malicious); + + if (fakeBase) + { + auto nn = recver.baseOtCount(); + BitVector bits(nn); + bits.randomize(prng); + std::vector> baseSendMsgs(bits.size()); + std::vector baseRecvMsgs(bits.size()); + + auto commonPrng = PRNG(ZeroBlock); + commonPrng.get(baseSendMsgs.data(), baseSendMsgs.size()); + for (u64 i = 0; i < bits.size(); ++i) + baseRecvMsgs[i] = baseSendMsgs[i][bits[i]]; + + recver.setBaseOts(baseSendMsgs); + } + else + { + // optional. You can request that the base ot are generated either + // using just base OTs (few rounds, more computation) or 128 base OTs and then extend those. + // The default is the latter, base + extension. + cp::sync_wait(recver.genSilentBaseOts(prng, chl, type == SilentBaseType::BaseExtend)); + } + + std::vector messages(numOTs); + BitVector choices(numOTs); + + // create the protocol object. + auto protocol = recver.silentReceive(choices, messages, prng, chl); + + // run the protocol + if (numThreads <= 1) + cp::sync_wait(protocol); + else + // launch the protocol on the thread pool. + cp::sync_wait(std::move(protocol) | macoro::start_on(threadPool)); + + // choices, messages has been populated with random OT messages. + // messages[i] = sender.message[i][choices[i]] + // See the header for other options. + } + auto end = timer.setTimePoint("end"); + auto milli = std::chrono::duration_cast(end - start).count(); + + u64 com = chl.bytesReceived() + chl.bytesSent(); + + if (role == Role::Sender) + { + std::string typeStr = type == SilentBaseType::Base ? "b " : "be "; + lout << tag << + " n:" << Color::Green << std::setw(6) << std::setfill(' ') << numOTs << Color::Default << + " type: " << Color::Green << typeStr << Color::Default << + " || " << Color::Green << + std::setw(6) << std::setfill(' ') << milli << " ms " << + std::setw(6) << std::setfill(' ') << com << " bytes" << std::endl << Color::Default; + + if (cmd.getOr("v", 0) > 1) + lout << gTimer << std::endl; + } + + if (cmd.isSet("v")) + { + if (role == Role::Sender) + lout << " **** sender ****\n" << timer << std::endl; + + if (role == Role::Receiver) + lout << " **** receiver ****\n" << timer << std::endl; + } + } + + } + + cp::sync_wait(chl.flush()); + +#endif + } + bool Silent_Examples(const CLP& cmd) + { + return runIf(Silent_example, cmd, Silent); + } + + +} diff --git a/frontend/ExampleSilent.h b/frontend/ExampleSilent.h index e792ba73..abc4122f 100644 --- a/frontend/ExampleSilent.h +++ b/frontend/ExampleSilent.h @@ -1,186 +1,7 @@ #pragma once - -#include "libOTe/TwoChooseOne/Silent/SilentOtExtReceiver.h" -#include "libOTe/TwoChooseOne/Silent/SilentOtExtSender.h" -#include "util.h" -#include - -#include "cryptoTools/Network/IOService.h" -#include "coproto/Socket/AsioSocket.h" +#include "cryptoTools/Common/CLP.h" namespace osuCrypto { - - void Silent_example(Role role, u64 numOTs, u64 numThreads, std::string ip, std::string tag, CLP& cmd) - { -#if defined(ENABLE_SILENTOT) && defined(COPROTO_ENABLE_BOOST) - - if (numOTs == 0) - numOTs = 1 << 20; - - // get up the networking - auto chl = cp::asioConnect(ip, role == Role::Sender); - - - PRNG prng(sysRandomSeed()); - - bool fakeBase = cmd.isSet("fakeBase"); - u64 trials = cmd.getOr("trials", 1); - auto malicious = cmd.isSet("mal") ? SilentSecType::Malicious : SilentSecType::SemiHonest; - - auto multType = (MultType)cmd.getOr("multType", (int)DefaultMultType); - - std::vector types; - if (cmd.isSet("base")) - types.push_back(SilentBaseType::Base); - else - types.push_back(SilentBaseType::BaseExtend); - - macoro::thread_pool threadPool; - auto work = threadPool.make_work(); - if (numThreads > 1) - threadPool.create_threads(numThreads); - - for (auto type : types) - { - for (u64 tt = 0; tt < trials; ++tt) - { - Timer timer; - auto start = timer.setTimePoint("start"); - if (role == Role::Sender) - { - SilentOtExtSender sender; - - // optionally request the LPN encoding matrix. - sender.mMultType = multType; - - // optionally configure the sender. default is semi honest security. - sender.configure(numOTs, 2, numThreads, malicious); - - if (fakeBase) - { - auto nn = sender.baseOtCount(); - BitVector bits(nn); - bits.randomize(prng); - std::vector> baseSendMsgs(bits.size()); - std::vector baseRecvMsgs(bits.size()); - - auto commonPrng = PRNG(ZeroBlock); - commonPrng.get(baseSendMsgs.data(), baseSendMsgs.size()); - for (u64 i = 0; i < bits.size(); ++i) - baseRecvMsgs[i] = baseSendMsgs[i][bits[i]]; - - sender.setBaseOts(baseRecvMsgs, bits); - } - else - { - // optional. You can request that the base ot are generated either - // using just base OTs (few rounds, more computation) or 128 base OTs and then extend those. - // The default is the latter, base + extension. - cp::sync_wait(sender.genSilentBaseOts(prng, chl, type == SilentBaseType::BaseExtend)); - } - - std::vector> messages(numOTs); - - // create the protocol object. - auto protocol = sender.silentSend(messages, prng, chl); - - // run the protocol - if (numThreads <= 1) - cp::sync_wait(protocol); - else - // launch the protocol on the thread pool. - cp::sync_wait(std::move(protocol) | macoro::start_on(threadPool)); - - // messages has been populated with random OT messages. - // See the header for other options. - } - else - { - - SilentOtExtReceiver recver; - - // optionally request the LPN encoding matrix. - recver.mMultType = multType; - - // configure the sender. optional for semi honest security... - recver.configure(numOTs, 2, numThreads, malicious); - - if (fakeBase) - { - auto nn = recver.baseOtCount(); - BitVector bits(nn); - bits.randomize(prng); - std::vector> baseSendMsgs(bits.size()); - std::vector baseRecvMsgs(bits.size()); - - auto commonPrng = PRNG(ZeroBlock); - commonPrng.get(baseSendMsgs.data(), baseSendMsgs.size()); - for (u64 i = 0; i < bits.size(); ++i) - baseRecvMsgs[i] = baseSendMsgs[i][bits[i]]; - - recver.setBaseOts(baseSendMsgs); - } - else - { - // optional. You can request that the base ot are generated either - // using just base OTs (few rounds, more computation) or 128 base OTs and then extend those. - // The default is the latter, base + extension. - cp::sync_wait(recver.genSilentBaseOts(prng, chl, type == SilentBaseType::BaseExtend)); - } - - std::vector messages(numOTs); - BitVector choices(numOTs); - - // create the protocol object. - auto protocol = recver.silentReceive(choices, messages, prng, chl); - - // run the protocol - if (numThreads <= 1) - cp::sync_wait(protocol); - else - // launch the protocol on the thread pool. - cp::sync_wait(std::move(protocol) | macoro::start_on(threadPool)); - - // choices, messages has been populated with random OT messages. - // messages[i] = sender.message[i][choices[i]] - // See the header for other options. - } - auto end = timer.setTimePoint("end"); - auto milli = std::chrono::duration_cast(end - start).count(); - - u64 com = chl.bytesReceived() + chl.bytesSent(); - - if (role == Role::Sender) - { - std::string typeStr = type == SilentBaseType::Base ? "b " : "be "; - lout << tag << - " n:" << Color::Green << std::setw(6) << std::setfill(' ') << numOTs << Color::Default << - " type: " << Color::Green << typeStr << Color::Default << - " || " << Color::Green << - std::setw(6) << std::setfill(' ') << milli << " ms " << - std::setw(6) << std::setfill(' ') << com << " bytes" << std::endl << Color::Default; - - if (cmd.getOr("v", 0) > 1) - lout << gTimer << std::endl; - } - - if (cmd.isSet("v")) - { - if (role == Role::Sender) - lout << " **** sender ****\n" << timer << std::endl; - - if (role == Role::Receiver) - lout << " **** receiver ****\n" << timer << std::endl; - } - } - - } - - cp::sync_wait(chl.flush()); - -#endif - } - - + bool Silent_Examples(const CLP& cmd); } diff --git a/frontend/ExampleTwoChooseOne.cpp b/frontend/ExampleTwoChooseOne.cpp new file mode 100644 index 00000000..4dc5527b --- /dev/null +++ b/frontend/ExampleTwoChooseOne.cpp @@ -0,0 +1,356 @@ + +#include "libOTe/Base/BaseOT.h" +#include "libOTe/TwoChooseOne/Kos/KosOtExtReceiver.h" +#include "libOTe/TwoChooseOne/Kos/KosOtExtSender.h" +#include "libOTe/TwoChooseOne/KosDot/KosDotExtReceiver.h" +#include "libOTe/TwoChooseOne/KosDot/KosDotExtSender.h" +#include "libOTe/TwoChooseOne/Iknp/IknpOtExtReceiver.h" +#include "libOTe/TwoChooseOne/Iknp/IknpOtExtSender.h" + + +#include "libOTe/TwoChooseOne/Silent/SilentOtExtReceiver.h" +#include "libOTe/TwoChooseOne/Silent/SilentOtExtSender.h" +#include "util.h" +#include "coproto/Socket/AsioSocket.h" + +namespace osuCrypto +{ +#ifdef ENABLE_IKNP + void noHash(IknpOtExtSender& s, IknpOtExtReceiver& r) + { + s.mHash = false; + r.mHash = false; + } +#endif + + template + void noHash(Sender&, Receiver&) + { + throw std::runtime_error("This protocol does not support noHash"); + } + +#ifdef ENABLE_SOFTSPOKEN_OT + // soft spoken takes an extra parameter as input what determines + // the computation/communication trade-off. + template + using is_SoftSpoken = typename std::conditional< + //std::is_same::value || + //std::is_same::value || + //std::is_same::value || + //std::is_same::value || + //std::is_same::value || + //std::is_same::value || + //std::is_same::value || + //std::is_same::value + false + , + std::true_type, std::false_type>::type; +#else + template + using is_SoftSpoken = std::false_type; +#endif + + template + typename std::enable_if::value, T>::type + construct(CLP& cmd) + { + return T(cmd.getOr("f", 2)); + } + + template + typename std::enable_if::value, T>::type + construct(CLP& cmd) + { + return T{}; + } + + template + void TwoChooseOne_example(Role role, int totalOTs, int numThreads, std::string ip, std::string tag, const CLP& cmd) + { + +#ifdef COPROTO_ENABLE_BOOST + if (totalOTs == 0) + totalOTs = 1 << 20; + + bool randomOT = true; + + // get up the networking + auto chl = cp::asioConnect(ip, role == Role::Sender); + + PRNG prng(sysRandomSeed()); + + + OtExtSender sender; + OtExtRecver receiver; + + +#ifdef LIBOTE_HAS_BASE_OT + // Now compute the base OTs, we need to set them on the first pair of extenders. + // In real code you would only have a sender or reciever, not both. But we do + // here just showing the example. + if (role == Role::Receiver) + { + DefaultBaseOT base; + std::array, 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); + } + else + { + + DefaultBaseOT base; + BitVector bv(128); + std::array baseMsg; + bv.randomize(prng); + + // 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); + } + +#else + if (!cmd.isSet("fakeBase")) + std::cout << "warning, base ots are not enabled. Fake base OTs will be used. " << std::endl; + PRNG commonPRNG(oc::ZeroBlock); + std::array, 128> sendMsgs; + commonPRNG.get(sendMsgs.data(), sendMsgs.size()); + if (role == Role::Receiver) + { + receiver.setBaseOts(sendMsgs); + } + else + { + BitVector bv(128); + bv.randomize(commonPRNG); + std::array recvMsgs; + for (u64 i = 0; i < 128; ++i) + recvMsgs[i] = sendMsgs[i][bv[i]]; + sender.setBaseOts(recvMsgs, bv); + } +#endif + + if (cmd.isSet("noHash")) + noHash(sender, receiver); + + Timer timer, sendTimer, recvTimer; + sendTimer.setTimePoint("start"); + recvTimer.setTimePoint("start"); + auto s = timer.setTimePoint("start"); + + if (numThreads == 1) + { + if (role == Role::Receiver) + { + // 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. + AlignedUnVector rMsgs(totalOTs); + + try { + + if (randomOT) + { + // perform totalOTs random OTs, the results will be written to msgs. + cp::sync_wait(receiver.receive(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)); + } + } + catch (std::exception& e) + { + std::cout << e.what() << std::endl; + chl.close(); + } + } + else + { + // construct a vector to stored the random send messages. + AlignedUnVector> sMsgs(totalOTs); + + + // if delta OT is used, then the user can call the following + // to set the desired XOR difference between the zero messages + // and the one messages. + // + // senders[i].setDelta(some 128 bit delta); + // + try + { + if (randomOT) + { + // perform the OTs and write the random OTs to msgs. + cp::sync_wait(sender.send(sMsgs, prng, chl)); + } + else + { + // 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)); + } + } + catch (std::exception& e) + { + std::cout << e.what() << std::endl; + chl.close(); + } + } + + // make sure all messages have been sent. + cp::sync_wait(chl.flush()); + } + else + { + + // for multi threading, we only show example for random OTs. + // We first need to construct the inputs + // that each thread will use. Note that the actual protocol + // is not thread safe so everything needs to be independent. + std::vector> tasks(numThreads); + std::vector threadPrngs(numThreads); + std::vector threadChls(numThreads); + + macoro::thread_pool::work work; + macoro::thread_pool threadPool(numThreads, work); + + if (role == Role::Receiver) + { + std::vector receivers(numThreads); + std::vector threadChoices(numThreads); + std::vector> threadMsgs(numThreads); + + for (u64 threadIndex = 0; threadIndex < (u64)numThreads; ++threadIndex) + { + u64 beginIndex = oc::roundUpTo(totalOTs * threadIndex / numThreads, 128); + u64 endIndex = oc::roundUpTo((totalOTs + 1) * threadIndex / numThreads, 128); + + threadChoices[threadIndex].resize(endIndex - beginIndex); + threadChoices[threadIndex].randomize(prng); + + threadMsgs[threadIndex].resize(endIndex - beginIndex); + + // create a copy of the receiver so that each can run + // independently. A single receiver is not thread safe. + receivers[threadIndex] = receiver.splitBase(); + + // create a PRNG for this thread. + threadPrngs[threadIndex].SetSeed(prng.get()); + + // create a socket for this thread. This is done by calling fork(). + threadChls[threadIndex] = chl.fork(); + + // start the receive protocol on the thread pool + tasks[threadIndex] = + receivers[threadIndex].receive( + threadChoices[threadIndex], + threadMsgs[threadIndex], + threadPrngs[threadIndex], + threadChls[threadIndex]) + | macoro::start_on(threadPool); + } + + // block this thread until the receive operations + // have completed. + for (u64 threadIndex = 0; threadIndex < (u64)numThreads; ++threadIndex) + cp::sync_wait(tasks[threadIndex]); + } + else + { + std::vector senders(numThreads); + std::vector>> threadMsgs(numThreads); + + for (u64 threadIndex = 0; threadIndex < (u64)numThreads; ++threadIndex) + { + u64 beginIndex = oc::roundUpTo(totalOTs * threadIndex / numThreads, 128); + u64 endIndex = oc::roundUpTo((totalOTs + 1) * threadIndex / numThreads, 128); + + threadMsgs[threadIndex].resize(endIndex - beginIndex); + + // create a copy of the receiver so that each can run + // independently. A single receiver is not thread safe. + senders[threadIndex] = sender.splitBase(); + + // create a PRNG for this thread. + threadPrngs[threadIndex].SetSeed(prng.get()); + + // create a socket for this thread. This is done by calling fork(). + threadChls[threadIndex] = chl.fork(); + + // start the send protocol on the thread pool + tasks[threadIndex] = + senders[threadIndex].send( + threadMsgs[threadIndex], + threadPrngs[threadIndex], + threadChls[threadIndex]) + | macoro::start_on(threadPool); + } + + // block this thread until the receive operations + // have completed. + for (u64 threadIndex = 0; threadIndex < (u64)numThreads; ++threadIndex) + cp::sync_wait(tasks[threadIndex]); + } + + work.reset(); + } + + + + auto e = timer.setTimePoint("finish"); + auto milli = std::chrono::duration_cast(e - s).count(); + + auto com = 0;// (chls[0].getTotalDataRecv() + chls[0].getTotalDataSent())* numThreads; + + if (role == Role::Sender) + lout << tag << " n=" << Color::Green << totalOTs << " " << milli << " ms " << com << " bytes" << std::endl << Color::Default; + + + if (cmd.isSet("v") && role == Role::Sender) + { + if (role == Role::Sender) + lout << " **** sender ****\n" << sendTimer << std::endl; + + if (role == Role::Receiver) + lout << " **** receiver ****\n" << recvTimer << std::endl; + } + +#else + throw std::runtime_error("This example requires coproto to enable boost support. Please build libOTe with `-DCOPROTO_ENABLE_BOOST=ON`. " LOCATION); +#endif + } + + + + + bool TwoChooseOne_Examples(const CLP& cmd) + { + bool flagSet = false; +#ifdef ENABLE_IKNP + flagSet |= runIf(TwoChooseOne_example, cmd, iknp); +#endif + +#ifdef ENABLE_KOS + flagSet |= runIf(TwoChooseOne_example, cmd, kos); +#endif + +#ifdef ENABLE_DELTA_KOS + flagSet |= runIf(TwoChooseOne_example, cmd, dkos); +#endif + +#ifdef ENABLE_SOFTSPOKEN_OT + flagSet |= runIf(TwoChooseOne_example, SoftSpokenShOtReceiver<>>, cmd, sshonest); + flagSet |= runIf(TwoChooseOne_example, cmd, smalicious); +#endif + return flagSet; + } + } diff --git a/frontend/ExampleTwoChooseOne.h b/frontend/ExampleTwoChooseOne.h index d532c8f3..c90ad94d 100644 --- a/frontend/ExampleTwoChooseOne.h +++ b/frontend/ExampleTwoChooseOne.h @@ -1,317 +1,8 @@ #pragma once - -#include "libOTe/Base/BaseOT.h" -#include "libOTe/TwoChooseOne/Kos/KosOtExtReceiver.h" -#include "libOTe/TwoChooseOne/Kos/KosOtExtSender.h" -#include "libOTe/TwoChooseOne/KosDot/KosDotExtReceiver.h" -#include "libOTe/TwoChooseOne/KosDot/KosDotExtSender.h" -#include "libOTe/TwoChooseOne/Iknp/IknpOtExtReceiver.h" -#include "libOTe/TwoChooseOne/Iknp/IknpOtExtSender.h" - - -#include "libOTe/TwoChooseOne/Silent/SilentOtExtReceiver.h" -#include "libOTe/TwoChooseOne/Silent/SilentOtExtSender.h" - -//#include "libOTe/TwoChooseOne/SoftSpokenOT/SoftSpokenMalLeakyDotExt.h" -//#include "libOTe/TwoChooseOne/SoftSpokenOT/SoftSpokenMalOtExt.h" -//#include "libOTe/TwoChooseOne/SoftSpokenOT/SoftSpokenShOtExt.h" -//#include "libOTe/TwoChooseOne/SoftSpokenOT/SoftSpokenShDotExt.h" +#include "cryptoTools/Common/CLP.h" namespace osuCrypto { -#ifdef ENABLE_IKNP - void noHash(IknpOtExtSender& s, IknpOtExtReceiver& r) - { - s.mHash = false; - r.mHash = false; - } -#endif - - template - void noHash(Sender&, Receiver&) - { - throw std::runtime_error("This protocol does not support noHash"); - } - -#ifdef ENABLE_SOFTSPOKEN_OT - // soft spoken takes an extra parameter as input what determines - // the computation/communication trade-off. - template - using is_SoftSpoken = typename std::conditional< - //std::is_same::value || - //std::is_same::value || - //std::is_same::value || - //std::is_same::value || - //std::is_same::value || - //std::is_same::value || - //std::is_same::value || - //std::is_same::value - false - , - std::true_type, std::false_type>::type; -#else - template - using is_SoftSpoken = std::false_type; -#endif - - template - typename std::enable_if::value,T>::type - construct(CLP& cmd) - { - return T( cmd.getOr("f", 2) ); - } - - template - typename std::enable_if::value, T>::type - construct(CLP& cmd) - { - return T{}; - } - - template - void TwoChooseOne_example(Role role, int totalOTs, int numThreads, std::string ip, std::string tag, CLP& cmd) - { - -#ifdef COPROTO_ENABLE_BOOST - if (totalOTs == 0) - totalOTs = 1 << 20; - - bool randomOT = true; - - // get up the networking - auto chl = cp::asioConnect(ip, role == Role::Sender); - - PRNG prng(sysRandomSeed()); - - - OtExtSender sender; - OtExtRecver receiver; - - -#ifdef LIBOTE_HAS_BASE_OT - // Now compute the base OTs, we need to set them on the first pair of extenders. - // In real code you would only have a sender or reciever, not both. But we do - // here just showing the example. - if (role == Role::Receiver) - { - DefaultBaseOT base; - std::array, 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); - } - else - { - - DefaultBaseOT base; - BitVector bv(128); - std::array baseMsg; - bv.randomize(prng); - - // 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); - } - -#else - if (!cmd.isSet("fakeBase")) - std::cout << "warning, base ots are not enabled. Fake base OTs will be used. " << std::endl; - PRNG commonPRNG(oc::ZeroBlock); - std::array, 128> sendMsgs; - commonPRNG.get(sendMsgs.data(), sendMsgs.size()); - if (role == Role::Receiver) - { - receiver.setBaseOts(sendMsgs); - } - else - { - BitVector bv(128); - bv.randomize(commonPRNG); - std::array recvMsgs; - for (u64 i = 0; i < 128; ++i) - recvMsgs[i] = sendMsgs[i][bv[i]]; - sender.setBaseOts(recvMsgs, bv); - } -#endif - - if (cmd.isSet("noHash")) - noHash(sender, receiver); - - Timer timer, sendTimer, recvTimer; - sendTimer.setTimePoint("start"); - recvTimer.setTimePoint("start"); - auto s = timer.setTimePoint("start"); - - if (numThreads == 1) - { - if (role == Role::Receiver) - { - // 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 rMsgs(totalOTs); - - if (randomOT) - { - // perform totalOTs random OTs, the results will be written to msgs. - cp::sync_wait(receiver.receive(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)); - } - } - else - { - // construct a vector to stored the random send messages. - std::vector> sMsgs(totalOTs); - - - // if delta OT is used, then the user can call the following - // to set the desired XOR difference between the zero messages - // and the one messages. - // - // senders[i].setDelta(some 128 bit delta); - // - - if (randomOT) - { - // perform the OTs and write the random OTs to msgs. - cp::sync_wait(sender.send(sMsgs, prng, chl)); - } - else - { - // 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)); - } - } - - } - else - { - - // for multi threading, we only show example for random OTs. - // We first need to construct the inputs - // that each thread will use. Note that the actual protocol - // is not thread safe so everything needs to be independent. - std::vector> tasks(numThreads); - std::vector threadPrngs(numThreads); - std::vector threadChls(numThreads); - - macoro::thread_pool::work work; - macoro::thread_pool threadPool(numThreads, work); - - if (role == Role::Receiver) - { - std::vector receivers(numThreads); - std::vector threadChoices(numThreads); - std::vector> threadMsgs(numThreads); - - for (u64 threadIndex = 0; threadIndex < (u64)numThreads; ++threadIndex) - { - u64 beginIndex = oc::roundUpTo(totalOTs * threadIndex / numThreads, 128); - u64 endIndex = oc::roundUpTo((totalOTs + 1) * threadIndex / numThreads, 128); - - threadChoices[threadIndex].resize(endIndex - beginIndex); - threadChoices[threadIndex].randomize(prng); - - threadMsgs[threadIndex].resize(endIndex - beginIndex); - - // create a copy of the receiver so that each can run - // independently. A single receiver is not thread safe. - receivers[threadIndex] = receiver.splitBase(); - - // create a PRNG for this thread. - threadPrngs[threadIndex].SetSeed(prng.get()); - - // create a socket for this thread. This is done by calling fork(). - threadChls[threadIndex] = chl.fork(); - - // start the receive protocol on the thread pool - tasks[threadIndex] = - receivers[threadIndex].receive( - threadChoices[threadIndex], - threadMsgs[threadIndex], - threadPrngs[threadIndex], - threadChls[threadIndex]) - | macoro::start_on(threadPool); - } - - // block this thread until the receive operations - // have completed. - for (u64 threadIndex = 0; threadIndex < (u64)numThreads; ++threadIndex) - cp::sync_wait(tasks[threadIndex]); - } - else - { - std::vector senders(numThreads); - std::vector>> threadMsgs(numThreads); - - for (u64 threadIndex = 0; threadIndex < (u64)numThreads; ++threadIndex) - { - u64 beginIndex = oc::roundUpTo(totalOTs * threadIndex / numThreads, 128); - u64 endIndex = oc::roundUpTo((totalOTs + 1) * threadIndex / numThreads, 128); - - threadMsgs[threadIndex].resize(endIndex - beginIndex); - - // create a copy of the receiver so that each can run - // independently. A single receiver is not thread safe. - senders[threadIndex] = sender.splitBase(); - - // create a PRNG for this thread. - threadPrngs[threadIndex].SetSeed(prng.get()); - - // create a socket for this thread. This is done by calling fork(). - threadChls[threadIndex] = chl.fork(); - - // start the send protocol on the thread pool - tasks[threadIndex] = - senders[threadIndex].send( - threadMsgs[threadIndex], - threadPrngs[threadIndex], - threadChls[threadIndex]) - | macoro::start_on(threadPool); - } - - // block this thread until the receive operations - // have completed. - for (u64 threadIndex = 0; threadIndex < (u64)numThreads; ++threadIndex) - cp::sync_wait(tasks[threadIndex]); - } - - work.reset(); - } - - auto e = timer.setTimePoint("finish"); - auto milli = std::chrono::duration_cast(e - s).count(); - - auto com = 0;// (chls[0].getTotalDataRecv() + chls[0].getTotalDataSent())* numThreads; - - if (role == Role::Sender) - lout << tag << " n=" << Color::Green << totalOTs << " " << milli << " ms " << com << " bytes" << std::endl << Color::Default; - - - if (cmd.isSet("v") && role == Role::Sender) - { - if (role == Role::Sender) - lout << " **** sender ****\n" << sendTimer << std::endl; - - if (role == Role::Receiver) - lout << " **** receiver ****\n" << recvTimer << std::endl; - } - -#else - throw std::runtime_error("This example requires coproto to enable boost support. Please build libOTe with `-DCOPROTO_ENABLE_BOOST=ON`. " LOCATION); -#endif - } + bool TwoChooseOne_Examples(const CLP& cmd); } diff --git a/frontend/ExampleVole.cpp b/frontend/ExampleVole.cpp new file mode 100644 index 00000000..2ef71894 --- /dev/null +++ b/frontend/ExampleVole.cpp @@ -0,0 +1,111 @@ + +#include "libOTe/Vole/Silent/SilentVoleReceiver.h" +#include "libOTe/Vole/Silent/SilentVoleSender.h" +#include "util.h" +#include "coproto/Socket/AsioSocket.h" + +namespace osuCrypto +{ + + + template + void Vole_example(Role role, int numVole, int numThreads, std::string ip, std::string tag, const CLP& cmd) + { +#if defined(ENABLE_SILENT_VOLE) && defined(COPROTO_ENABLE_BOOST) + + if (numVole == 0) + numVole = 1 << 20; + + // get up the networking + auto chl = cp::asioConnect(ip, role == Role::Sender); + + // get a random number generator seeded from the system + PRNG prng(sysRandomSeed()); + + auto mulType = (MultType)cmd.getOr("multType", (int)DefaultMultType); + + u64 milli; + Timer timer; + + gTimer.setTimePoint("begin"); + if (role == Role::Receiver) + { + // construct a vector to stored the received messages. + // A = B + C * delta + AlignedUnVector C(numVole); + AlignedUnVector A(numVole); + gTimer.setTimePoint("recver.msg.alloc"); + + SilentVoleReceiver receiver; + receiver.mMultType = mulType; + receiver.configure(numVole); + gTimer.setTimePoint("recver.config"); + + // block until both parties are ready (optional). + cp::sync_wait(sync(chl, role)); + auto b = timer.setTimePoint("start"); + receiver.setTimePoint("start"); + gTimer.setTimePoint("recver.genBase"); + + // perform numVole random OTs, the results will be written to msgs. + cp::sync_wait(receiver.silentReceive(C, A, prng, chl)); + + // record the time. + receiver.setTimePoint("finish"); + auto e = timer.setTimePoint("finish"); + milli = std::chrono::duration_cast(e - b).count(); + } + else + { + gTimer.setTimePoint("sender.thrd.begin"); + + // A = B + C * delta + AlignedUnVector B(numVole); + block delta = prng.get(); + + gTimer.setTimePoint("sender.msg.alloc"); + + SilentVoleSender sender; + sender.mMultType = mulType; + sender.configure(numVole); + gTimer.setTimePoint("sender.config"); + timer.setTimePoint("start"); + + // block until both parties are ready (optional). + cp::sync_wait(sync(chl, role)); + auto b = sender.setTimePoint("start"); + gTimer.setTimePoint("sender.genBase"); + + // perform the OTs and write the random OTs to msgs. + cp::sync_wait(sender.silentSend(delta, B, prng, chl)); + + sender.setTimePoint("finish"); + auto e = timer.setTimePoint("finish"); + milli = std::chrono::duration_cast(e - b).count(); + } + if (role == Role::Sender) + { + + lout << tag << + " n:" << Color::Green << std::setw(6) << std::setfill(' ') << numVole << Color::Default << + " || " << Color::Green << + std::setw(6) << std::setfill(' ') << milli << " ms " << + //std::setw(6) << std::setfill(' ') << com << " bytes" << + std::endl << Color::Default; + + if (cmd.getOr("v", 0) > 1) + lout << gTimer << std::endl; + + } + + // make sure all messages are sent. + cp::sync_wait(chl.flush()); +#endif + } + bool Vole_Examples(const CLP& cmd) + { + return + runIf(Vole_example, cmd, vole); + } + +} diff --git a/frontend/ExampleVole.h b/frontend/ExampleVole.h index c8891879..da6e58a5 100644 --- a/frontend/ExampleVole.h +++ b/frontend/ExampleVole.h @@ -1,142 +1,10 @@ #pragma once - -#include "libOTe/Vole/Silent/SilentVoleReceiver.h" -#include "libOTe/Vole/Silent/SilentVoleSender.h" +#include "cryptoTools/Common/CLP.h" namespace osuCrypto { - - //template - void Vole_example(Role role, int numOTs, int numThreads, std::string ip, std::string tag, CLP& cmd) - { -#if defined(ENABLE_SILENT_VOLE) && defined(COPROTO_ENABLE_BOOST) - - if (numOTs == 0) - numOTs = 1 << 20; - using OtExtSender = SilentVoleSender; - using OtExtRecver = SilentVoleReceiver; - - // get up the networking - auto chl = cp::asioConnect(ip, role == Role::Sender); - - // get a random number generator seeded from the system - PRNG prng(sysRandomSeed()); - - auto mulType = (MultType)cmd.getOr("multType", (int)DefaultMultType); - bool fakeBase = cmd.isSet("fakeBase"); - - u64 milli; - Timer timer; - - gTimer.setTimePoint("begin"); - if (role == Role::Receiver) - { - // construct a vector to stored the received messages. - std::unique_ptr backing0(new block[numOTs]); - std::unique_ptr backing1(new block[numOTs]); - span choice(backing0.get(), numOTs); - span msgs(backing1.get(), numOTs); - gTimer.setTimePoint("recver.msg.alloc"); - - OtExtRecver receiver; - receiver.mMultType = mulType; - receiver.configure(numOTs); - gTimer.setTimePoint("recver.config"); - - // generate base OTs - if (fakeBase) - { - auto nn = receiver.baseOtCount(); - std::vector> baseSendMsgs(nn); - PRNG pp(oc::ZeroBlock); - pp.get(baseSendMsgs.data(), baseSendMsgs.size()); - receiver.setBaseOts(baseSendMsgs); - } - else - { - cp::sync_wait(receiver.genSilentBaseOts(prng, chl)); - } - - // block until both parties are ready (optional). - cp::sync_wait(sync(chl, role)); - auto b = timer.setTimePoint("start"); - receiver.setTimePoint("start"); - gTimer.setTimePoint("recver.genBase"); - - // perform numOTs random OTs, the results will be written to msgs. - cp::sync_wait(receiver.silentReceive(choice, msgs, prng, chl)); - - // record the time. - receiver.setTimePoint("finish"); - auto e = timer.setTimePoint("finish"); - milli = std::chrono::duration_cast(e - b).count(); - } - else - { - gTimer.setTimePoint("sender.thrd.begin"); - - - std::unique_ptr backing(new block[numOTs]); - span msgs(backing.get(), numOTs); - - gTimer.setTimePoint("sender.msg.alloc"); - - OtExtSender sender; - sender.mMultType = mulType; - sender.configure(numOTs); - gTimer.setTimePoint("sender.config"); - timer.setTimePoint("start"); - - // generate base OTs - if (fakeBase) - { - auto nn = sender.baseOtCount(); - BitVector bits(nn); bits.randomize(prng); - std::vector> baseSendMsgs(nn); - std::vector baseRecvMsgs(nn); - PRNG pp(oc::ZeroBlock); - pp.get(baseSendMsgs.data(), baseSendMsgs.size()); - for (u64 i = 0; i < nn; ++i) - baseRecvMsgs[i] = baseSendMsgs[i][bits[i]]; - sender.setBaseOts(baseRecvMsgs, bits); - } - else - { - cp::sync_wait(sender.genSilentBaseOts(prng, chl)); - } - - // block until both parties are ready (optional). - cp::sync_wait(sync(chl, role)); - auto b = sender.setTimePoint("start"); - gTimer.setTimePoint("sender.genBase"); - - // perform the OTs and write the random OTs to msgs. - block delta = prng.get(); - cp::sync_wait(sender.silentSend(delta, msgs, prng, chl)); - - sender.setTimePoint("finish"); - auto e = timer.setTimePoint("finish"); - milli = std::chrono::duration_cast(e - b).count(); - } - if (role == Role::Sender) - { - - lout << tag << - " n:" << Color::Green << std::setw(6) << std::setfill(' ') << numOTs << Color::Default << - " || " << Color::Green << - std::setw(6) << std::setfill(' ') << milli << " ms " << - //std::setw(6) << std::setfill(' ') << com << " bytes" << - std::endl << Color::Default; - - if (cmd.getOr("v", 0) > 1) - lout << gTimer << std::endl; - - } - - cp::sync_wait(chl.flush()); -#endif - } + bool Vole_Examples(const CLP& cmd); } diff --git a/frontend/benchmark.h b/frontend/benchmark.h index 63984a5e..25043f5e 100644 --- a/frontend/benchmark.h +++ b/frontend/benchmark.h @@ -12,6 +12,7 @@ #include "libOTe/TwoChooseOne/Silent/SilentOtExtSender.h" #include "libOTe/Vole/Silent/SilentVoleSender.h" #include "libOTe/Vole/Silent/SilentVoleReceiver.h" +#include "libOTe/Tools/CoeffCtx.h" namespace osuCrypto { @@ -171,74 +172,12 @@ namespace osuCrypto std::cout << verbose << std::endl; } - inline void encodeBench(CLP& cmd) - { -#ifdef ENABLE_INSECURE_SILVER - u64 trials = cmd.getOr("t", 10); - - // the message length of the code. - // The noise vector will have size n=2*m. - // the user can use - // -m X - // to state that exactly X rows should be used or - // -mm X - // to state that 2^X rows should be used. - u64 m = cmd.getOr("m", 1ull << cmd.getOr("mm", 10)); - - // the weight of the code, must be 5 or 11. - u64 w = cmd.getOr("w", 5); - - // verbose flag. - bool v = cmd.isSet("v"); - - - SilverCode code; - if (w == 11) - code = SilverCode::Weight11; - else if (w == 5) - code = SilverCode::Weight5; - else - { - std::cout << "invalid weight" << std::endl; - throw RTE_LOC; - } - - PRNG prng(ZeroBlock); - SilverEncoder encoder; - encoder.init(m, code); - - - std::vector x(encoder.cols()); - Timer timer, verbose; - - if (v) - encoder.setTimer(verbose); - - timer.setTimePoint("_____________________"); - for (u64 i = 0; i < trials; ++i) - { - encoder.dualEncode(x); - timer.setTimePoint("encode"); - } - - std::cout << timer << std::endl; - - if (v) - std::cout << verbose << std::endl; -#else - std::cout << "disabled, ENABLE_INSECURE_SILVER not defined " << std::endl; -#endif - } - - - inline void transpose(const CLP& cmd) { #ifdef ENABLE_AVX u64 trials = cmd.getOr("trials", 1ull << 18); { - AlignedArray data; Timer timer; @@ -376,17 +315,14 @@ namespace osuCrypto { std::cout << e.what() << std::endl; } +#else + std::cout << "ENABLE_SILENTOT = false" << std::endl; #endif } - - - - inline void VoleBench2(const CLP& cmd) { -#ifdef ENABLE_SILENTOT - +#ifdef ENABLE_SILENT_VOLE try { @@ -412,6 +348,8 @@ namespace osuCrypto baseSend[i] = prng.get(); baseRecv[i] = baseSend[i][baseChoice[i]]; } + +#ifdef ENABLE_SOFTSPOKEN_OT sender.mOtExtRecver.emplace(); sender.mOtExtSender.emplace(); recver.mOtExtRecver.emplace(); @@ -420,6 +358,7 @@ namespace osuCrypto recver.mOtExtRecver->setBaseOts(baseSend); sender.mOtExtSender->setBaseOts(baseRecv, baseChoice); recver.mOtExtSender->setBaseOts(baseRecv, baseChoice); +#endif // ENABLE_SOFTSPOKEN_OT PRNG prng0(ZeroBlock), prng1(ZeroBlock); block delta = prng0.get(); @@ -478,6 +417,8 @@ namespace osuCrypto { std::cout << e.what() << std::endl; } +#else + std::cout << "ENABLE_Silent_VOLE = false" << std::endl; #endif } } \ No newline at end of file diff --git a/frontend/main.cpp b/frontend/main.cpp index b3c64c07..f98584f1 100644 --- a/frontend/main.cpp +++ b/frontend/main.cpp @@ -11,9 +11,6 @@ using namespace osuCrypto; #include #include -#include -#include -#include #include #include @@ -32,30 +29,12 @@ using namespace osuCrypto; #include "cryptoTools/Crypto/RandomOracle.h" #include "libOTe/Tools/EACode/EAChecker.h" -static const std::vector -unitTestTag{ "u", "unitTest" }, -kos{ "k", "kos" }, -dkos{ "d", "dkos" }, -ssdelta{ "ssd", "ssdelta" }, -sshonest{ "ss", "sshonest" }, -smleakydelta{ "smld", "smleakydelta" }, -smalicious{ "sm", "smalicious" }, -kkrt{ "kk", "kkrt" }, -iknp{ "i", "iknp" }, -diknp{ "diknp" }, -oos{ "o", "oos" }, -moellerpopf{ "p", "moellerpopf" }, -ristrettopopf{ "r", "ristrettopopf" }, -mr{ "mr" }, -mrb{ "mrb" }, -Silent{ "s", "Silent" }, -vole{ "vole" }, -akn{ "a", "akn" }, -np{ "np" }, -simple{ "simplest" }, -simpleasm{ "simplest-asm" }; +#include "libOTe/TwoChooseOne/Iknp/IknpOtExtSender.h" +#include "libOTe/TwoChooseOne/Iknp/IknpOtExtReceiver.h" #ifdef ENABLE_IKNP +using namespace oc; + void minimal() { // Setup networking. See cryptoTools\frontend_cryptoTools\Tutorials\Network.cpp @@ -113,9 +92,7 @@ int main(int argc, char** argv) // various benchmarks if (cmd.isSet("bench")) { - if (cmd.isSet("silver")) - encodeBench(cmd); - else if (cmd.isSet("QC")) + if (cmd.isSet("QC")) QCCodeBench(cmd); else if (cmd.isSet("silent")) SilentOtBench(cmd); @@ -128,14 +105,6 @@ int main(int argc, char** argv) return 0; } - - // minimum distance checker for EA codes. - if (cmd.isSet("ea")) - { - EAChecker(cmd); - return 0; - } - // unit tests. if (cmd.isSet(unitTestTag)) { @@ -158,96 +127,11 @@ int main(int argc, char** argv) // run various examples. - - -#ifdef ENABLE_SIMPLESTOT - flagSet |= runIf(baseOT_example, cmd, simple); -#endif - -#ifdef ENABLE_SIMPLESTOT_ASM - flagSet |= runIf(baseOT_example, cmd, simpleasm); -#endif - -#ifdef ENABLE_MRR_TWIST -#ifdef ENABLE_SSE - flagSet |= runIf([&](Role role, int totalOTs, int numThreads, std::string ip, std::string tag, CLP& clp) { - DomainSepEKEPopf factory; - const char* domain = "EKE POPF OT example"; - factory.Update(domain, std::strlen(domain)); - baseOT_example_from_ot(role, totalOTs, numThreads, ip, tag, clp, McRosRoyTwist(factory)); - }, cmd, moellerpopf, { "eke" }); -#endif - - flagSet |= runIf([&](Role role, int totalOTs, int numThreads, std::string ip, std::string tag, CLP& clp) { - DomainSepMRPopf factory; - const char* domain = "MR POPF OT example"; - factory.Update(domain, std::strlen(domain)); - baseOT_example_from_ot(role, totalOTs, numThreads, ip, tag, clp, McRosRoyTwistMR(factory)); - }, cmd, moellerpopf, { "mrPopf" }); - - flagSet |= runIf([&](Role role, int totalOTs, int numThreads, std::string ip, std::string tag, CLP& clp) { - DomainSepFeistelPopf factory; - const char* domain = "Feistel POPF OT example"; - factory.Update(domain, std::strlen(domain)); - baseOT_example_from_ot(role, totalOTs, numThreads, ip, tag, clp, McRosRoyTwistFeistel(factory)); - }, cmd, moellerpopf, { "feistel" }); - - flagSet |= runIf([&](Role role, int totalOTs, int numThreads, std::string ip, std::string tag, CLP& clp) { - DomainSepFeistelMulPopf factory; - const char* domain = "Feistel With Multiplication POPF OT example"; - factory.Update(domain, std::strlen(domain)); - baseOT_example_from_ot(role, totalOTs, numThreads, ip, tag, clp, McRosRoyTwistMul(factory)); -}, cmd, moellerpopf, { "feistelMul" }); -#endif - -#ifdef ENABLE_MRR - flagSet |= runIf([&](Role role, int totalOTs, int numThreads, std::string ip, std::string tag, CLP& clp) { - DomainSepFeistelRistPopf factory; - const char* domain = "Feistel POPF OT example (Risretto)"; - factory.Update(domain, std::strlen(domain)); - baseOT_example_from_ot(role, totalOTs, numThreads, ip, tag, clp, McRosRoy(factory)); - }, cmd, ristrettopopf, { "feistel" }); - - flagSet |= runIf([&](Role role, int totalOTs, int numThreads, std::string ip, std::string tag, CLP& clp) { - DomainSepFeistelMulRistPopf factory; - const char* domain = "Feistel With Multiplication POPF OT example (Risretto)"; - factory.Update(domain, std::strlen(domain)); - baseOT_example_from_ot(role, totalOTs, numThreads, ip, tag, clp, McRosRoyMul(factory)); - }, cmd, ristrettopopf, { "feistelMul" }); -#endif - -#ifdef ENABLE_MR - flagSet |= runIf(baseOT_example, cmd, mr); -#endif - -#ifdef ENABLE_IKNP - flagSet |= runIf(TwoChooseOne_example, cmd, iknp); -#endif - -#ifdef ENABLE_KOS - flagSet |= runIf(TwoChooseOne_example, cmd, kos); -#endif - -#ifdef ENABLE_DELTA_KOS - flagSet |= runIf(TwoChooseOne_example, cmd, dkos); -#endif - -#ifdef ENABLE_SOFTSPOKEN_OT - flagSet |= runIf(TwoChooseOne_example, SoftSpokenShOtReceiver<>>, cmd, sshonest); - flagSet |= runIf(TwoChooseOne_example, cmd, smalicious); -#endif - -#ifdef ENABLE_KKRT - flagSet |= runIf(NChooseOne_example, cmd, kkrt); -#endif - -#ifdef ENABLE_OOS - flagSet |= runIf(NChooseOne_example, cmd, oos); -#endif - - flagSet |= runIf(Silent_example, cmd, Silent); - flagSet |= runIf(Vole_example, cmd, vole); - + flagSet |= baseOT_examples(cmd); + flagSet |= TwoChooseOne_Examples(cmd); + flagSet |= NChooseOne_Examples(cmd); + flagSet |= Silent_Examples(cmd); + flagSet |= Vole_Examples(cmd); if (cmd.isSet("messagePassing")) { @@ -256,8 +140,6 @@ int main(int argc, char** argv) } - - if (flagSet == false) { diff --git a/frontend/util.cpp b/frontend/util.cpp index ac7a95ce..fa6bc91b 100644 --- a/frontend/util.cpp +++ b/frontend/util.cpp @@ -7,6 +7,7 @@ #include #include +#include "util.h" namespace osuCrypto { @@ -67,6 +68,7 @@ namespace osuCrypto else { u8 dummy[1]; + dummy[0] = 0; chl.asyncSend(dummy, 1); chl.recv(dummy, 1); chl.asyncSend(dummy, 1); @@ -79,6 +81,7 @@ namespace osuCrypto { u8 dummy[1]; + dummy[0] = 0; chl.asyncSend(dummy, 1); diff --git a/frontend/util.h b/frontend/util.h index 599a922c..27fb08fe 100644 --- a/frontend/util.h +++ b/frontend/util.h @@ -10,11 +10,34 @@ #include #include #include "libOTe/Tools/Coproto.h" - +#include namespace osuCrypto { + + static const std::vector + unitTestTag{ "u", "unitTest" }, + kos{ "k", "kos" }, + dkos{ "d", "dkos" }, + ssdelta{ "ssd", "ssdelta" }, + sshonest{ "ss", "sshonest" }, + smleakydelta{ "smld", "smleakydelta" }, + smalicious{ "sm", "smalicious" }, + kkrt{ "kk", "kkrt" }, + iknp{ "i", "iknp" }, + diknp{ "diknp" }, + oos{ "o", "oos" }, + moellerpopf{ "p", "moellerpopf" }, + ristrettopopf{ "r", "ristrettopopf" }, + mr{ "mr" }, + mrb{ "mrb" }, + Silent{ "s", "Silent" }, + vole{ "vole" }, + akn{ "a", "akn" }, + np{ "np" }, + simple{ "simplest" }, + simpleasm{ "simplest-asm" }; enum class Role { Sender, @@ -36,7 +59,7 @@ namespace osuCrypto //using ProtocolFunc = std::function; template - inline bool runIf(ProtocolFunc protocol, CLP & cmd, std::vector tag, + inline bool runIf(ProtocolFunc protocol, const CLP & cmd, std::vector tag, std::vector tag2 = std::vector()) { auto n = cmd.isSet("nn") diff --git a/libOTe/Tools/Pprf/RegularPprf.h b/libOTe/Tools/Pprf/RegularPprf.h index ebede7f9..439150af 100644 --- a/libOTe/Tools/Pprf/RegularPprf.h +++ b/libOTe/Tools/Pprf/RegularPprf.h @@ -1,4 +1,7 @@ #pragma once +#include "libOTe/config.h" + +#ifdef ENABLE_PPRF #include "cryptoTools/Common/Defines.h" #include "cryptoTools/Common/BitVector.h" #include "cryptoTools/Common/Matrix.h" @@ -1031,4 +1034,6 @@ namespace osuCrypto } } }; -} \ No newline at end of file +} + +#endif \ No newline at end of file diff --git a/libOTe/Vole/Silent/SilentVoleReceiver.h b/libOTe/Vole/Silent/SilentVoleReceiver.h index a7fcd311..1818f1b2 100644 --- a/libOTe/Vole/Silent/SilentVoleReceiver.h +++ b/libOTe/Vole/Silent/SilentVoleReceiver.h @@ -25,6 +25,8 @@ #include #include #include "libOTe/Tools/QuasiCyclicCode.h" +#include "libOTe/TwoChooseOne/Silent/SilentOtExtUtil.h" + namespace osuCrypto { @@ -145,6 +147,8 @@ namespace osuCrypto // generate the needed OTs. task<> genSilentBaseOts(PRNG& prng, Socket& chl) { +#ifdef LIBOTE_HAS_BASE_OT + using BaseOT = DefaultBaseOT; @@ -246,7 +250,11 @@ namespace osuCrypto setSilentBaseOts(msg, baseAs); setTimePoint("SilentVoleReceiver.genSilent.done"); - MC_END(); + MC_END(); +#else + throw std::runtime_error("LIBOTE_HAS_BASE_OT = false, must enable relic, sodium or simplest ot asm." LOCATION); +#endif + }; // configure the silent OT extension. This sets diff --git a/libOTe/Vole/Silent/SilentVoleSender.h b/libOTe/Vole/Silent/SilentVoleSender.h index 682e2b5b..a60625a7 100644 --- a/libOTe/Vole/Silent/SilentVoleSender.h +++ b/libOTe/Vole/Silent/SilentVoleSender.h @@ -123,6 +123,7 @@ namespace osuCrypto // generate the needed OTs. task<> genSilentBaseOts(PRNG& prng, Socket& chl, F delta) { +#ifdef LIBOTE_HAS_BASE_OT using BaseOT = DefaultBaseOT; MC_BEGIN(task<>, this, delta, &prng, &chl, @@ -195,6 +196,9 @@ namespace osuCrypto setSilentBaseOts(msg, b); setTimePoint("SilentVoleSender.genSilent.done"); MC_END(); +#else + throw std::runtime_error("LIBOTE_HAS_BASE_OT = false, must enable relic, sodium or simplest ot asm." LOCATION); +#endif } // configure the silent OT extension. This sets diff --git a/libOTe/config.h.in b/libOTe/config.h.in index f9c5bb36..40b1bec8 100644 --- a/libOTe/config.h.in +++ b/libOTe/config.h.in @@ -52,6 +52,8 @@ // build the library with silent vole enabled #cmakedefine ENABLE_SILENT_VOLE @ENABLE_SILENT_VOLE@ +#cmakedefine ENABLE_PPRF @ENABLE_PPRF@ + // build the library with silver codes. #cmakedefine ENABLE_INSECURE_SILVER @ENABLE_INSECURE_SILVER@ diff --git a/libOTe_Tests/Pprf_Tests.cpp b/libOTe_Tests/Pprf_Tests.cpp index fd3622c9..00c49971 100644 --- a/libOTe_Tests/Pprf_Tests.cpp +++ b/libOTe_Tests/Pprf_Tests.cpp @@ -1,6 +1,9 @@ #include "Pprf_Tests.h" #include "libOTe/Tools/Pprf/RegularPprf.h" +#include "cryptoTools/Common/TestCollection.h" + +#ifdef ENABLE_PPRF #include "cryptoTools/Common/Log.h" #include "Common.h" #include @@ -447,3 +450,22 @@ void Tools_Pprf_callback_test(const oc::CLP& cmd) throw UnitTestSkipped("ENABLE_SILENTOT not defined."); #endif } +#else + + +namespace { + void throwDisabled() + { + throw oc::UnitTestSkipped( + "ENABLE_PPRF not defined. " + ); + } +} + +void Tools_Pprf_expandOne_test(const oc::CLP& cmd) { throwDisabled(); } +void Tools_Pprf_inter_test(const oc::CLP& cmd) { throwDisabled(); } +void Tools_Pprf_ByLeafIndex_test(const oc::CLP& cmd) { throwDisabled(); } +void Tools_Pprf_ByTreeIndex_test(const oc::CLP& cmd) { throwDisabled(); } +void Tools_Pprf_callback_test(const oc::CLP& cmd) { throwDisabled(); } + +#endif diff --git a/libOTe_Tests/Vole_Tests.cpp b/libOTe_Tests/Vole_Tests.cpp index 26dafae6..52ced20e 100644 --- a/libOTe_Tests/Vole_Tests.cpp +++ b/libOTe_Tests/Vole_Tests.cpp @@ -18,7 +18,7 @@ using namespace oc; #include "libOTe/Tools/CoeffCtx.h" using namespace tests_libOTe; - +#ifdef ENABLE_SILENT_VOLE template void Vole_Noisy_test_impl(u64 n) { @@ -356,15 +356,26 @@ void Vole_Silent_Rounds_test(const oc::CLP& cmd) timer.setTimePoint("done"); } } -// -//namespace { -// void throwDisabled() -// { -// throw UnitTestSkipped( -// "ENABLE_SILENT_VOLE not defined. " -// ); -// } -//} +#else + + +namespace { + void throwDisabled() + { + throw UnitTestSkipped( + "ENABLE_SILENT_VOLE not defined. " + ); + } +} +void Vole_Noisy_test(const oc::CLP& cmd) { throwDisabled(); } +void Vole_Silent_QuasiCyclic_test(const oc::CLP& cmd) { throwDisabled(); } +void Vole_Silent_paramSweep_test(const oc::CLP& cmd) { throwDisabled(); } +void Vole_Silent_baseOT_test(const oc::CLP& cmd) { throwDisabled(); } +void Vole_Silent_mal_test(const oc::CLP& cmd) { throwDisabled(); } +void Vole_Silent_Rounds_test(const oc::CLP& cmd) { throwDisabled(); } + + +#endif // // //void Vole_Silent_QuasiCyclic_test(const oc::CLP& cmd) { throwDisabled(); }