diff --git a/fbpcf/mpc_std_lib/unified_data_process/data_processor/DataProcessor.h b/fbpcf/mpc_std_lib/unified_data_process/data_processor/DataProcessor.h index 9be6fe92..d25f00df 100644 --- a/fbpcf/mpc_std_lib/unified_data_process/data_processor/DataProcessor.h +++ b/fbpcf/mpc_std_lib/unified_data_process/data_processor/DataProcessor.h @@ -12,6 +12,8 @@ #include "fbpcf/engine/util/util.h" #include "fbpcf/mpc_std_lib/aes_circuit/IAesCircuitCtr.h" #include "fbpcf/mpc_std_lib/unified_data_process/data_processor/IDataProcessor.h" +#include "fbpcf/primitive/mac/S2v.h" +#include "fbpcf/primitive/mac/S2vFactory.h" namespace fbpcf::mpc_std_lib::unified_data_process::data_processor { @@ -59,7 +61,10 @@ class DataProcessor final : public IDataProcessor { protected: // locally encrypt the plaintext, output expanded keys and ciphertext - std::tuple, std::vector>> + std::tuple< + std::array<__m128i, 11>, + std::vector>, + std::vector> localEncryption(const std::vector>& plaintextData); // privately share the input byte stream from party inputPartyID into vector diff --git a/fbpcf/mpc_std_lib/unified_data_process/data_processor/DataProcessor_impl.h b/fbpcf/mpc_std_lib/unified_data_process/data_processor/DataProcessor_impl.h index 06d9683d..0c00d379 100644 --- a/fbpcf/mpc_std_lib/unified_data_process/data_processor/DataProcessor_impl.h +++ b/fbpcf/mpc_std_lib/unified_data_process/data_processor/DataProcessor_impl.h @@ -7,6 +7,8 @@ #pragma once +#include +#include #include "fbpcf/engine/util/aes.h" #include "fbpcf/mpc_std_lib/aes_circuit/AesCircuitCtr.h" #include "fbpcf/mpc_std_lib/unified_data_process/data_processor/DataProcessor.h" @@ -25,11 +27,13 @@ DataProcessor::processMyData( auto keyAndCiphertext = localEncryption(plaintextData); auto& expandedKeyM128i = std::get<0>(keyAndCiphertext); auto& ciphertextByte = std::get<1>(keyAndCiphertext); + auto& s2vByte = std::get<2>(keyAndCiphertext); - // 2a. send encryted data to peer + // 2a. send encryted data and IV to peer for (auto& item : ciphertextByte) { agent_->send(item); } + agent_->send(s2vByte); // 1b. (peer)receive encryted data from peer // 2b. (peer)pick desired ciphertext blocks @@ -84,6 +88,9 @@ DataProcessor::processPeersData( for (size_t i = 0; i < dataSize; i++) { ciphertextByte[i] = agent_->receive(dataWidth); } + std::vector s2vVec(16); + s2vVec = agent_->receive(16); + __m128i s2vM128 = engine::util::buildM128i(s2vVec); // 2b. pick desired ciphertext blocks std::vector> intersection( @@ -109,6 +116,8 @@ DataProcessor::processPeersData( for (uint64_t j = 0; j < cipherBlocks; ++j) { filteredCountersM128i[i][j] = _mm_set_epi64x(0, indexes[i] * cipherBlocks + j); + filteredCountersM128i[i][j] = + _mm_add_epi64(s2vM128, filteredCountersM128i[i][j]); } } auto filteredCounters = @@ -136,7 +145,10 @@ DataProcessor::processPeersData( } template -std::tuple, std::vector>> +std::tuple< + std::array<__m128i, 11>, + std::vector>, + std::vector> DataProcessor::localEncryption( const std::vector>& plaintextData) { size_t rowCounts = plaintextData.size(); @@ -147,10 +159,27 @@ DataProcessor::localEncryption( fbpcf::engine::util::Aes localAes(keyM128i); auto expandedKeyM128i = localAes.expandEncryptionKey(keyM128i); // generate counters for each block + const primitive::mac::S2vFactory s2vFactory; + std::vector keyByte(16); + _mm_storeu_si128((__m128i*)keyByte.data(), keyM128i); + const auto s2v = s2vFactory.create(keyByte); + std::vector plaintextCombined; + plaintextCombined.reserve(rowSize * rowCounts); + std::for_each( + plaintextData.begin(), + plaintextData.end(), + [&plaintextCombined](const auto& v) { + std::copy(v.begin(), v.end(), std::back_inserter(plaintextCombined)); + }); + __m128i s2vRes = s2v->getMacM128i(plaintextCombined); std::vector<__m128i> counterM128i(rowCounts * rowBlocks); for (uint64_t i = 0; i < counterM128i.size(); ++i) { counterM128i[i] = _mm_set_epi64x(0, i); + counterM128i[i] = _mm_add_epi64(s2vRes, counterM128i[i]); } + std::vector s2vVec(16); + _mm_storeu_si128((__m128i*)s2vVec.data(), s2vRes); + // encrypt counters localAes.encryptInPlace(counterM128i); @@ -170,7 +199,7 @@ DataProcessor::localEncryption( plaintextData[i][j] ^ maskByte[i * rowBlocks * 16 + j]; } } - return {expandedKeyM128i, ciphertextByte}; + return {expandedKeyM128i, ciphertextByte, s2vVec}; } template