Skip to content

Commit

Permalink
Apply S2v in DataProcessor (#472)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #472

In this diff, we apply S2V in DataProcessor for generating synthetic IV.

Changes are made in following places:
1. In `localEncryption`, we generate S2v based on plaintext. Then, we use the S2v as counters. The counter accumulation happened in low 64-bit of S2v.

2. In `processMyData`, send the `s2v` to peer along with ciphertext.

3. In `processPeersData`, receive `s2v` and use it as initial counter accordingly.

Reviewed By: robotal, RuiyuZhu

Differential Revision: D42542990

fbshipit-source-id: a93528712a7b006e299869c46594017c59f01c7d
  • Loading branch information
Chen Yuan authored and facebook-github-bot committed Jan 19, 2023
1 parent 2bf34a1 commit 20d6beb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -59,7 +61,10 @@ class DataProcessor final : public IDataProcessor<schedulerId> {

protected:
// locally encrypt the plaintext, output expanded keys and ciphertext
std::tuple<std::array<__m128i, 11>, std::vector<std::vector<uint8_t>>>
std::tuple<
std::array<__m128i, 11>,
std::vector<std::vector<uint8_t>>,
std::vector<uint8_t>>
localEncryption(const std::vector<std::vector<unsigned char>>& plaintextData);

// privately share the input byte stream from party inputPartyID into vector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#pragma once

#include <emmintrin.h>
#include <fbpcf/engine/util/util.h>
#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"
Expand All @@ -25,11 +27,13 @@ DataProcessor<schedulerId>::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
Expand Down Expand Up @@ -84,6 +88,9 @@ DataProcessor<schedulerId>::processPeersData(
for (size_t i = 0; i < dataSize; i++) {
ciphertextByte[i] = agent_->receive(dataWidth);
}
std::vector<unsigned char> s2vVec(16);
s2vVec = agent_->receive(16);
__m128i s2vM128 = engine::util::buildM128i(s2vVec);

// 2b. pick desired ciphertext blocks
std::vector<std::vector<unsigned char>> intersection(
Expand All @@ -109,6 +116,8 @@ DataProcessor<schedulerId>::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 =
Expand Down Expand Up @@ -136,7 +145,10 @@ DataProcessor<schedulerId>::processPeersData(
}

template <int schedulerId>
std::tuple<std::array<__m128i, 11>, std::vector<std::vector<uint8_t>>>
std::tuple<
std::array<__m128i, 11>,
std::vector<std::vector<uint8_t>>,
std::vector<uint8_t>>
DataProcessor<schedulerId>::localEncryption(
const std::vector<std::vector<unsigned char>>& plaintextData) {
size_t rowCounts = plaintextData.size();
Expand All @@ -147,10 +159,27 @@ DataProcessor<schedulerId>::localEncryption(
fbpcf::engine::util::Aes localAes(keyM128i);
auto expandedKeyM128i = localAes.expandEncryptionKey(keyM128i);
// generate counters for each block
const primitive::mac::S2vFactory s2vFactory;
std::vector<unsigned char> keyByte(16);
_mm_storeu_si128((__m128i*)keyByte.data(), keyM128i);
const auto s2v = s2vFactory.create(keyByte);
std::vector<unsigned char> 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<unsigned char> s2vVec(16);
_mm_storeu_si128((__m128i*)s2vVec.data(), s2vRes);

// encrypt counters
localAes.encryptInPlace(counterM128i);

Expand All @@ -170,7 +199,7 @@ DataProcessor<schedulerId>::localEncryption(
plaintextData[i][j] ^ maskByte[i * rowBlocks * 16 + j];
}
}
return {expandedKeyM128i, ciphertextByte};
return {expandedKeyM128i, ciphertextByte, s2vVec};
}

template <int schedulerId>
Expand Down

0 comments on commit 20d6beb

Please sign in to comment.