|
| 1 | +#include "cryptoTools/Common/CLP.h" |
| 2 | +#include "coproto/Socket/BufferingSocket.h" |
| 3 | +#include <fstream> |
| 4 | +#include "libOTe/TwoChooseOne/Silent/SilentOtExtReceiver.h" |
| 5 | +#include "libOTe/TwoChooseOne/Silent/SilentOtExtSender.h" |
| 6 | + |
| 7 | +// This example demonstates how one can get and manually send the protocol messages |
| 8 | +// that are generated. This communicate method is one possible way of doing this. |
| 9 | +// It takes a protocol that has been started and coproto buffering socket as input. |
| 10 | +// It alternates between "sending" and "receiving" protocol messages. Instead of |
| 11 | +// sending the messages on a socket, this program writes them to a file and the other |
| 12 | +// party reads that file to get the message. In a real program the communication could |
| 13 | +// handled in any way the user decides. |
| 14 | +auto communicate( |
| 15 | + macoro::eager_task<>& protocol, |
| 16 | + bool sender, |
| 17 | + coproto::BufferingSocket& sock, |
| 18 | + bool verbose) |
| 19 | +{ |
| 20 | + |
| 21 | + int s = 0, r = 0; |
| 22 | + std::string me = sender ? "sender" : "recver"; |
| 23 | + std::string them = !sender ? "sender" : "recver"; |
| 24 | + |
| 25 | + // write any outgoing data to a file me_i.bin where i in the message index. |
| 26 | + auto write = [&]() |
| 27 | + { |
| 28 | + // the the outbound messages that the protocol has generated. |
| 29 | + // This will consist of all the outbound messages that can be |
| 30 | + // generated without receiving the next inbound message. |
| 31 | + auto b = sock.getOutbound(); |
| 32 | + |
| 33 | + // If we do have outbound messages, then lets write them to a file. |
| 34 | + if (b && b->size()) |
| 35 | + { |
| 36 | + std::ofstream message; |
| 37 | + auto temp = me + ".tmp"; |
| 38 | + auto file = me + "_" + std::to_string(s) + ".bin"; |
| 39 | + message.open(temp, std::ios::binary | std::ios::trunc); |
| 40 | + message.write((char*)b->data(), b->size()); |
| 41 | + message.close(); |
| 42 | + |
| 43 | + if (verbose) |
| 44 | + { |
| 45 | + // optional for debug purposes. |
| 46 | + oc::RandomOracle hash(16); |
| 47 | + hash.Update(b->data(), b->size()); |
| 48 | + oc::block h; hash.Final(h); |
| 49 | + |
| 50 | + std::cout << me << " write " << std::to_string(s) << " " << h << "\n"; |
| 51 | + } |
| 52 | + |
| 53 | + if (rename(temp.c_str(), file.c_str()) != 0) |
| 54 | + std::cout << me << " file renamed failed\n"; |
| 55 | + else if (verbose) |
| 56 | + std::cout << me << " file renamed successfully\n"; |
| 57 | + |
| 58 | + ++s; |
| 59 | + } |
| 60 | + |
| 61 | + }; |
| 62 | + |
| 63 | + // write incoming data from a file them_i.bin where i in the message index. |
| 64 | + auto read = [&]() { |
| 65 | + |
| 66 | + std::ifstream message; |
| 67 | + auto file = them + "_" + std::to_string(r) + ".bin"; |
| 68 | + while (message.is_open() == false) |
| 69 | + { |
| 70 | + message.open(file, std::ios::binary); |
| 71 | + if ((message.is_open() == false)) |
| 72 | + std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 73 | + } |
| 74 | + |
| 75 | + auto fsize = message.tellg(); |
| 76 | + message.seekg(0, std::ios::end); |
| 77 | + fsize = message.tellg() - fsize; |
| 78 | + message.seekg(0, std::ios::beg); |
| 79 | + std::vector<oc::u8> buff(fsize); |
| 80 | + message.read((char*)buff.data(), fsize); |
| 81 | + message.close(); |
| 82 | + std::remove(file.c_str()); |
| 83 | + |
| 84 | + if (verbose) |
| 85 | + { |
| 86 | + oc::RandomOracle hash(16); |
| 87 | + hash.Update(buff.data(), buff.size()); |
| 88 | + oc::block h; hash.Final(h); |
| 89 | + |
| 90 | + std::cout << me << " read " << std::to_string(r) << " " << h << "\n"; |
| 91 | + } |
| 92 | + ++r; |
| 93 | + |
| 94 | + // This gives this socket the message which forwards it to the protocol and |
| 95 | + // run the protocol forward, possibly generating more outbound protocol |
| 96 | + // messages. |
| 97 | + sock.processInbound(buff); |
| 98 | + }; |
| 99 | + |
| 100 | + // The sender we generate the first message. |
| 101 | + if (!sender) |
| 102 | + write(); |
| 103 | + |
| 104 | + // While the protocol is not done we alternate between reading and writing messages. |
| 105 | + while (protocol.is_ready() == false) |
| 106 | + { |
| 107 | + read(); |
| 108 | + write(); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +void messagePassingExampleRun(oc::CLP& cmd) |
| 113 | +{ |
| 114 | +#ifdef ENABLE_SILENTOT |
| 115 | + auto isReceiver = cmd.get<int>("r"); |
| 116 | + |
| 117 | + // The number of OTs. |
| 118 | + auto n = cmd.getOr("n", 100); |
| 119 | + |
| 120 | + auto verbose = cmd.isSet("v"); |
| 121 | + |
| 122 | + // A buffering socket. This socket type internally buffers the |
| 123 | + // protocol messages. It is then up to the user to manually send |
| 124 | + // and receive messages via the getOutbond(...) and processInbount(...) |
| 125 | + // methods. |
| 126 | + coproto::BufferingSocket sock; |
| 127 | + |
| 128 | + // randomness source |
| 129 | + PRNG prng(oc::sysRandomSeed()); |
| 130 | + |
| 131 | + // Sets are always represented as 16 byte values. To support longer elements one can hash them. |
| 132 | + if (!isReceiver) |
| 133 | + { |
| 134 | + oc::SilentOtExtSender sender; |
| 135 | + |
| 136 | + std::vector<std::array<block, 2>> senderOutput(n); |
| 137 | + |
| 138 | + |
| 139 | + if (verbose) |
| 140 | + std::cout << "sender start\n"; |
| 141 | + |
| 142 | + // Eagerly start the protocol. This will run the protocol up to the point |
| 143 | + // that it need to receive a message from the other party. |
| 144 | + auto protocol = |
| 145 | + sender.silentSend(senderOutput, prng, sock) |
| 146 | + | macoro::make_eager(); |
| 147 | + |
| 148 | + // Perform the communication and complete the protocol. |
| 149 | + communicate(protocol, true, sock, verbose); |
| 150 | + |
| 151 | + std::cout << "sender done\n"; |
| 152 | + |
| 153 | + for (u64 i = 0; i < std::min<u64>(10, n); ++i) |
| 154 | + std::cout << "sender.msg[" << i << "] = { " << senderOutput[i][0] << ", " << senderOutput[i][1] << "}" << std::endl; |
| 155 | + if (n > 10) |
| 156 | + std::cout << "..." << std::endl; |
| 157 | + } |
| 158 | + else |
| 159 | + { |
| 160 | + std::vector<block> receiverOutputMsg(n); |
| 161 | + oc::BitVector receiverOutputBits(n); |
| 162 | + |
| 163 | + oc::SilentOtExtReceiver receiver; |
| 164 | + |
| 165 | + if (verbose) |
| 166 | + std::cout << "recver start\n"; |
| 167 | + |
| 168 | + // Eagerly start the protocol. This will run the protocol up to the point |
| 169 | + // that it need to receive a message from the other party. |
| 170 | + auto protocol = |
| 171 | + receiver.silentReceive(receiverOutputBits, receiverOutputMsg, prng, sock) |
| 172 | + | macoro::make_eager(); |
| 173 | + |
| 174 | + // Perform the communication and complete the protocol. |
| 175 | + communicate(protocol, false, sock, verbose); |
| 176 | + |
| 177 | + std::cout << "recver done\n"; |
| 178 | + |
| 179 | + for (u64 i = 0; i < std::min<u64>(10, n); ++i) |
| 180 | + std::cout << "receiver.msg[" << i << "] = " << receiverOutputMsg[i] << " = sender.msg["<<i <<"][" << receiverOutputBits[i] << "]" << std::endl; |
| 181 | + if (n > 10) |
| 182 | + std::cout << "..." << std::endl; |
| 183 | + } |
| 184 | +#else |
| 185 | + std::cout << "ENABLE_SILENTOT is not defined. Rebuilt with -DENABLE_SILENTOT=true" << std::endl; |
| 186 | +#endif |
| 187 | +} |
| 188 | + |
| 189 | + |
| 190 | +void messagePassingExample(oc::CLP& cmd) |
| 191 | +{ |
| 192 | + // If the user specified -r, then run that party. |
| 193 | + // Otherwise run both parties. |
| 194 | + if (cmd.hasValue("r")) |
| 195 | + { |
| 196 | + messagePassingExampleRun(cmd); |
| 197 | + } |
| 198 | + else |
| 199 | + { |
| 200 | + auto s = cmd; |
| 201 | + s.setDefault("r", 0); |
| 202 | + cmd.setDefault("r", 1); |
| 203 | + auto a = std::async([&]() {messagePassingExampleRun(s); }); |
| 204 | + messagePassingExampleRun(cmd); |
| 205 | + a.get(); |
| 206 | + } |
| 207 | +} |
| 208 | + |
0 commit comments