diff --git a/fbpcf/unified_data_process/adapter/Adapter.h b/fbpcf/unified_data_process/adapter/Adapter.h deleted file mode 100644 index a0b6a950..00000000 --- a/fbpcf/unified_data_process/adapter/Adapter.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#pragma once - -#include "fbpcf/frontend/BitString.h" -#include "fbpcf/mpc_std_lib/shuffler/IShuffler.h" -#include "fbpcf/unified_data_process/adapter/IAdapter.h" - -namespace fbpcf::udp::adapter { - -template -class Adapter final : public IAdapter { - using SecBit = frontend::Bit; - using PubBit = frontend::Bit; - using SecString = frontend::BitString; - - public: - Adapter( - bool amIParty0, - int32_t party0Id, - int32_t party1Id, - std::unique_ptr> shuffler) - : amIParty0_(amIParty0), - party0Id_(party0Id), - party1Id_(party1Id), - shuffler_(std::move(shuffler)) {} - - std::vector adapt( - const std::vector& unionMap) const override; - - private: - bool amIParty0_; - int32_t party0Id_; - int32_t party1Id_; - std::unique_ptr> shuffler_; -}; - -} // namespace fbpcf::udp::adapter - -#include "fbpcf/unified_data_process/adapter/Adapter_impl.h" diff --git a/fbpcf/unified_data_process/adapter/AdapterFactory.h b/fbpcf/unified_data_process/adapter/AdapterFactory.h deleted file mode 100644 index 5910b081..00000000 --- a/fbpcf/unified_data_process/adapter/AdapterFactory.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#pragma once - -#include "fbpcf/unified_data_process/adapter/Adapter.h" -#include "fbpcf/unified_data_process/adapter/IAdapterFactory.h" - -namespace fbpcf::udp::adapter { - -template -class AdapterFactory final : public IAdapterFactory { - using SecString = frontend::BitString; - - public: - AdapterFactory( - bool amIParty0, - int32_t party0Id, - int32_t party1Id, - std::unique_ptr> - shufflerFactory) - : amIParty0_(amIParty0), - party0Id_(party0Id), - party1Id_(party1Id), - shufflerFactory_(std::move(shufflerFactory)) {} - std::unique_ptr create() override { - return std::make_unique>( - amIParty0_, party0Id_, party1Id_, shufflerFactory_->create()); - } - - private: - bool amIParty0_; - int32_t party0Id_; - int32_t party1Id_; - std::unique_ptr> - shufflerFactory_; -}; - -} // namespace fbpcf::udp::adapter diff --git a/fbpcf/unified_data_process/adapter/Adapter_impl.h b/fbpcf/unified_data_process/adapter/Adapter_impl.h deleted file mode 100644 index 17492d7f..00000000 --- a/fbpcf/unified_data_process/adapter/Adapter_impl.h +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#pragma once - -#include -#include "fbpcf/unified_data_process/adapter/Adapter.h" - -namespace fbpcf::udp::adapter { - -template -std::vector Adapter::adapt( - const std::vector& unionMap) const { - auto unionSize = unionMap.size(); - - if (unionSize == 0) { - throw std::runtime_error("Union size can not be 0."); - } - size_t indexWidth = std::ceil(std::log2(unionSize)); - - SecString ids(2 * indexWidth + 1); - - std::vector hasValue(unionSize); - std::vector myMap(unionSize); - - for (size_t i = 0; i < unionSize; i++) { - hasValue[i] = unionMap.at(i) >= 0; - myMap[i] = unionMap.at(i) >= 0 ? unionMap.at(i) : 0; - } - - // This bit indicates whether both parties have value. If only one party has - // value, then this bit will be 0; if both parties have value, this bit will - // be 1. It is impossible that neither party has value. - ids[0] = !SecBit(typename SecBit::ExtractedBit(hasValue)); - for (size_t i = 0; i < indexWidth; i++) { - std::vector myShare(unionSize); - for (size_t j = 0; j < unionSize; j++) { - myShare[j] = (myMap[j] >> i) & 1; - } - ids[i + 1] = SecBit(myShare, party0Id_); - ids[1 + indexWidth + i] = SecBit(myShare, party1Id_); - } - - auto shuffledIds = shuffler_->shuffle(std::move(ids)); - auto match0 = shuffledIds[0].openToParty(party0Id_); - auto match1 = shuffledIds[0].openToParty(party1Id_); - auto matchResult = - amIParty0_ ? std::move(match0).getValue() : std::move(match1).getValue(); - int64_t intersectionSize = 0; - for (auto item : matchResult) { - intersectionSize += item; - } - std::vector> share0Plaintext( - indexWidth, std::vector(intersectionSize)); - std::vector> share1Plaintext( - indexWidth, std::vector(intersectionSize)); - for (size_t i = 0; i < indexWidth; i++) { - auto firstShare = shuffledIds[1 + i].extractBit().getValue(); - auto secondShare = shuffledIds[indexWidth + 1 + i].extractBit().getValue(); - int index = 0; - for (size_t j = 0; j < unionSize; j++) { - if (matchResult.at(j)) { - share0Plaintext[i][index] = firstShare.at(j); - share1Plaintext[i][index] = secondShare.at(j); - index++; - } - } - } - SecString share0( - typename SecString::ExtractedString(std::move(share0Plaintext))); - SecString share1( - typename SecString::ExtractedString(std::move(share1Plaintext))); - auto myShare0 = share0.openToParty(party1Id_); - auto myShare1 = share1.openToParty(party0Id_); - auto myShare = amIParty0_ ? std::move(myShare1).getValue() - : std::move(myShare0).getValue(); - - std::vector rst(intersectionSize, 0); - for (size_t i = 0; i < indexWidth; i++) { - for (size_t j = 0; j < intersectionSize; j++) { - rst[j] += (myShare.at(i).at(j)) << i; - } - } - return rst; -} - -} // namespace fbpcf::udp::adapter diff --git a/fbpcf/unified_data_process/adapter/IAdapter.h b/fbpcf/unified_data_process/adapter/IAdapter.h deleted file mode 100644 index 668f2cbb..00000000 --- a/fbpcf/unified_data_process/adapter/IAdapter.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#pragma once - -#include -#include - -namespace fbpcf::udp::adapter { - -/* - * An adapter converts a union-like mapping result into an intersection-like - * mapping result. - */ -class IAdapter { - public: - virtual ~IAdapter() = default; - - /** - * convert a union-like mapping result to an intersection-like mapping - * result. - * @param unionMap the union-like mapping result, no mapping is represented - * as -1. The i-th index in the input represents the index of this party's - * element that corresponds to the i-th element in the union. - * @return the corresponding intersection-like mapping result. The i-th index - * in the output represents the index of peer's element that corresponds to - * the i-th element in the intersection. - */ - virtual std::vector adapt( - const std::vector& unionMap) const = 0; -}; - -} // namespace fbpcf::udp::adapter diff --git a/fbpcf/unified_data_process/adapter/IAdapterFactory.h b/fbpcf/unified_data_process/adapter/IAdapterFactory.h deleted file mode 100644 index a02e582b..00000000 --- a/fbpcf/unified_data_process/adapter/IAdapterFactory.h +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#pragma once - -#include "fbpcf/unified_data_process/adapter/IAdapter.h" - -namespace fbpcf::udp::adapter { - -class IAdapterFactory { - public: - virtual ~IAdapterFactory() = default; - virtual std::unique_ptr create() = 0; -}; - -} // namespace fbpcf::udp::adapter diff --git a/fbpcf/unified_data_process/adapter/test/AdapterTest.cpp b/fbpcf/unified_data_process/adapter/test/AdapterTest.cpp deleted file mode 100644 index c6f2da38..00000000 --- a/fbpcf/unified_data_process/adapter/test/AdapterTest.cpp +++ /dev/null @@ -1,132 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#include -#include -#include -#include -#include -#include -#include - -#include "fbpcf/engine/communication/test/AgentFactoryCreationHelper.h" -#include "fbpcf/mpc_std_lib/shuffler/NonShufflerFactory.h" -#include "fbpcf/scheduler/SchedulerHelper.h" -#include "fbpcf/test/TestHelper.h" -#include "fbpcf/unified_data_process/adapter/AdapterFactory.h" - -namespace fbpcf::udp::adapter { - -std::vector generateShuffledIndex(size_t size) { - std::vector rst(size); - for (size_t i = 0; i < size; i++) { - rst[i] = i; - } - std::random_shuffle(rst.begin(), rst.end()); - return rst; -} - -std::tuple< - std::vector, - std::vector, - std::unordered_map> -generateAdapterTestData() { - std::random_device rd; - std::mt19937_64 e(rd()); - std::uniform_int_distribution randomUnionSize(3, 0xFF); - std::uniform_int_distribution randomElement(0, 2); - - auto unionSize = randomUnionSize(e); - size_t p0InputSize = 0; - size_t p1InputSize = 0; - std::vector u(unionSize); - for (auto& item : u) { - item = randomElement(e); - if (item != 0) { - p0InputSize++; - } - if (item != 1) { - p1InputSize++; - } - } - - std::vector p0Data = generateShuffledIndex(p0InputSize); - std::vector p1Data = generateShuffledIndex(p1InputSize); - std::vector p0Input(unionSize); - std::vector p1Input(unionSize); - size_t p0Index = 0; - size_t p1Index = 0; - std::unordered_map expectedOutput; - for (size_t i = 0; i < unionSize; i++) { - if (u.at(i) == 2) { - expectedOutput[p0Data.at(p0Index)] = p1Data.at(p1Index); - } - - if (u.at(i) != 0) { - p0Input[i] = p0Data.at(p0Index); - p0Index++; - } else { - p0Input[i] = -1; - } - if (u.at(i) != 1) { - p1Input[i] = p1Data.at(p1Index); - p1Index++; - } else { - p1Input[i] = -1; - } - } - return {p0Input, p1Input, expectedOutput}; -} - -void checkOutput( - const std::vector& p0Output, - const std::vector& p1Output, - const std::unordered_map& expectedOutput) { - ASSERT_EQ(p0Output.size(), expectedOutput.size()); - ASSERT_EQ(p1Output.size(), expectedOutput.size()); - for (size_t i = 0; i < p0Output.size(); i++) { - ASSERT_TRUE(expectedOutput.find(p1Output.at(i)) != expectedOutput.end()); - EXPECT_EQ(expectedOutput.at(p1Output.at(i)), p0Output.at(i)); - } -} - -void adapterTest( - std::unique_ptr adapter0, - std::unique_ptr adapter1) { - auto [p0Input, p1Input, expectedOutput] = generateAdapterTestData(); - auto task = [](std::unique_ptr adapter, - const std::vector& input) { - return adapter->adapt(input); - }; - auto future0 = std::async(task, std::move(adapter0), p0Input); - auto future1 = std::async(task, std::move(adapter1), p1Input); - auto rst0 = future0.get(); - auto rst1 = future1.get(); - checkOutput(rst0, rst1, expectedOutput); -} - -TEST(AdapterTest, testAdapterWithNonShuffler) { - auto agentFactories = engine::communication::getInMemoryAgentFactory(2); - setupRealBackend<0, 1>(*agentFactories[0], *agentFactories[1]); - AdapterFactory<0> factory0( - true, - 0, - 1, - std::make_unique>>()); - - AdapterFactory<1> factory1( - false, - 0, - 1, - std::make_unique>>()); - - adapterTest(factory0.create(), factory1.create()); -} - -} // namespace fbpcf::udp::adapter diff --git a/fbpcf/unified_data_process/data_processor/DummyDataProcessor.h b/fbpcf/unified_data_process/data_processor/DummyDataProcessor.h deleted file mode 100644 index bb2c8d40..00000000 --- a/fbpcf/unified_data_process/data_processor/DummyDataProcessor.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#pragma once - -#include "fbpcf/engine/communication/IPartyCommunicationAgent.h" -#include "fbpcf/unified_data_process/data_processor/IDataProcessor.h" - -namespace fbpcf::udp::data_processor::insecure { - -/** - * This is an insecure implementation. This object is only meant to be used as a - * placeholder for testing. - */ -template -class DummyDataProcessor final : public IDataProcessor { - public: - explicit DummyDataProcessor( - int32_t myId, - int32_t partnerId, - std::unique_ptr - agent) - : myId_(myId), partnerId_(partnerId), agent_(std::move(agent)) {} - - /** - * @inherit doc - */ - typename IDataProcessor::SecString processMyData( - const std::vector>& plaintextData, - size_t outputSize) override; - - /** - * @inherit doc - */ - typename IDataProcessor::SecString processPeersData( - size_t dataSize, - const std::vector& indexes, - size_t dataWidth) override; - - private: - int32_t myId_; - int32_t partnerId_; - std::unique_ptr - agent_; -}; - -} // namespace fbpcf::udp::data_processor::insecure - -#include "fbpcf/unified_data_process/data_processor/DummyDataProcessor_impl.h" diff --git a/fbpcf/unified_data_process/data_processor/DummyDataProcessorFactory.h b/fbpcf/unified_data_process/data_processor/DummyDataProcessorFactory.h deleted file mode 100644 index 6da3d510..00000000 --- a/fbpcf/unified_data_process/data_processor/DummyDataProcessorFactory.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#pragma once - -#include -#include "fbpcf/engine/communication/IPartyCommunicationAgentFactory.h" -#include "fbpcf/unified_data_process/data_processor/DummyDataProcessor.h" -#include "fbpcf/unified_data_process/data_processor/IDataProcessorFactory.h" - -namespace fbpcf::udp::data_processor::insecure { - -template -class DummyDataProcessorFactory final - : public IDataProcessorFactory { - public: - DummyDataProcessorFactory( - int32_t myId, - int32_t partnerId, - fbpcf::engine::communication::IPartyCommunicationAgentFactory& - agentFactory) - : myId_(myId), partnerId_(partnerId), agentFactory_(agentFactory) {} - - std::unique_ptr> create() { - return std::make_unique>( - myId_, partnerId_, agentFactory_.create(partnerId_)); - } - - private: - int32_t myId_; - int32_t partnerId_; - fbpcf::engine::communication::IPartyCommunicationAgentFactory& agentFactory_; -}; - -} // namespace fbpcf::udp::data_processor::insecure diff --git a/fbpcf/unified_data_process/data_processor/DummyDataProcessor_impl.h b/fbpcf/unified_data_process/data_processor/DummyDataProcessor_impl.h deleted file mode 100644 index bf694e23..00000000 --- a/fbpcf/unified_data_process/data_processor/DummyDataProcessor_impl.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#pragma once - -namespace fbpcf::udp::data_processor::insecure { - -template -typename IDataProcessor::SecString -DummyDataProcessor::processMyData( - const std::vector>& plaintextData, - size_t outputSize) { - if (plaintextData.size() == 0) { - throw std::runtime_error("payload can't be empty!"); - } - if (outputSize == 0) { - throw std::runtime_error("output can't be empty!"); - } - for (auto& item : plaintextData) { - agent_->send(item); - } - std::vector> dummyShare( - plaintextData.at(0).size() * 8, std::vector(outputSize)); - return - typename IDataProcessor::SecString(dummyShare, partnerId_); -} - -template -typename IDataProcessor::SecString -DummyDataProcessor::processPeersData( - size_t dataSize, - const std::vector& indexes, - size_t dataWidth) { - std::vector> plaintext; - for (size_t i = 0; i < dataSize; i++) { - plaintext.push_back(agent_->receive(dataWidth)); - } - std::vector> myShare( - dataWidth * 8, std::vector(indexes.size())); - for (size_t i = 0; i < dataWidth; i++) { - for (uint8_t j = 0; j < 8; j++) { - for (size_t k = 0; k < indexes.size(); k++) { - myShare[i * 8 + j][k] = (plaintext.at(indexes.at(k)).at(i) >> j) & 1; - } - } - } - return typename IDataProcessor::SecString(myShare, myId_); -} - -} // namespace fbpcf::udp::data_processor::insecure diff --git a/fbpcf/unified_data_process/data_processor/IDataProcessor.h b/fbpcf/unified_data_process/data_processor/IDataProcessor.h deleted file mode 100644 index 14ab3cd9..00000000 --- a/fbpcf/unified_data_process/data_processor/IDataProcessor.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#pragma once - -#include -#include -#include "fbpcf/frontend/BitString.h" - -namespace fbpcf::udp::data_processor { - -/** - * A data processor can generate the secret shares of the data of the matched - * rows based on the indexes of those rows provided by one party and the actual - * data provided by the other. - */ -template -class IDataProcessor { - public: - using SecString = frontend::BitString; - using PubString = frontend::BitString; - - virtual ~IDataProcessor() = default; - - /** - * Process this party's data and generate the secret shares of the data of the - * matched rows. The other party will provide the indexes of those rows. - * @param plaintextData my data - * @param outputSize how many rows are expected to appear in the output - * @return the secret-shared values of the data of the matched rows - */ - virtual SecString processMyData( - const std::vector>& plaintextData, - size_t outputSize) = 0; - - /** - * Process the other party's data and generate the secret shares of the data - * of the matched rows. The other party will provide the data while this party - * will specify the indexes of the matched rows. - * @param dataSize how many rows are expected from the other party - * @param indexes the indexes of the matched rows, the order matters - * @param dataWidth how many bytes are there in the data. - * @return the secret-shared values of the data of the matched rows - */ - virtual SecString processPeersData( - size_t dataSize, - const std::vector& indexes, - size_t dataWidth) = 0; -}; - -} // namespace fbpcf::udp::data_processor diff --git a/fbpcf/unified_data_process/data_processor/IDataProcessorFactory.h b/fbpcf/unified_data_process/data_processor/IDataProcessorFactory.h deleted file mode 100644 index 70542435..00000000 --- a/fbpcf/unified_data_process/data_processor/IDataProcessorFactory.h +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#pragma once - -#include -#include "fbpcf/unified_data_process/data_processor/IDataProcessor.h" - -namespace fbpcf::udp::data_processor { - -template -class IDataProcessorFactory { - public: - virtual ~IDataProcessorFactory() = default; - virtual std::unique_ptr> create() = 0; -}; - -} // namespace fbpcf::udp::data_processor diff --git a/fbpcf/unified_data_process/data_processor/test/DataProcessorTest.cpp b/fbpcf/unified_data_process/data_processor/test/DataProcessorTest.cpp deleted file mode 100644 index 538513ac..00000000 --- a/fbpcf/unified_data_process/data_processor/test/DataProcessorTest.cpp +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#include -#include -#include -#include -#include -#include -#include - -#include "fbpcf/engine/communication/test/AgentFactoryCreationHelper.h" -#include "fbpcf/scheduler/SchedulerHelper.h" -#include "fbpcf/test/TestHelper.h" -#include "fbpcf/unified_data_process/data_processor/DummyDataProcessorFactory.h" - -namespace fbpcf::udp::data_processor { - -std::tuple< - std::vector>, - std::vector, - std::vector>> -generateDataProcessorTestData() { - std::random_device rd; - std::mt19937_64 e(rd()); - std::uniform_int_distribution randomSize(10, 0xFF); - std::uniform_int_distribution randomData(0, 0xFF); - auto outputSize = randomSize(e); - auto inputSize = outputSize + randomSize(e); - - size_t dataWidth = 20; - std::vector> inputData( - inputSize, std::vector(dataWidth)); - - for (auto& item : inputData) { - for (auto& data : item) { - data = randomData(e); - } - } - std::vector index(inputSize); - for (size_t i = 0; i < inputSize; i++) { - index[i] = i; - } - std::random_shuffle(index.begin(), index.end()); - index.erase(index.begin() + outputSize, index.end()); - - std::vector> expectedOutput(outputSize); - for (size_t i = 0; i < outputSize; i++) { - expectedOutput[i] = inputData.at(index.at(i)); - } - return {inputData, index, expectedOutput}; -} - -void testDataProcessor( - std::unique_ptr> processor0, - std::unique_ptr> processor1) { - auto [data, index, expectedOutput] = generateDataProcessorTestData(); - auto outputSize = index.size(); - auto dataSize = data.size(); - auto dataWidth = data.at(0).size(); - auto task0 = [](std::unique_ptr> processor, - const std::vector>& plaintextData, - size_t outputSize, - size_t dataWidth) { - auto secretSharedOutput = - processor->processMyData(plaintextData, outputSize); - auto plaintextOutputBitString = - secretSharedOutput.openToParty(0).getValue(); - std::vector> rst( - outputSize, std::vector(dataWidth)); - for (size_t i = 0; i < dataWidth; i++) { - for (uint8_t j = 0; j < 8; j++) { - for (size_t k = 0; k < outputSize; k++) { - rst[k][i] += (plaintextOutputBitString.at(i * 8 + j).at(k) << j); - } - } - } - return rst; - }; - auto task1 = [](std::unique_ptr> processor, - size_t dataSize, - const std::vector& indexes, - size_t dataWidth) { - auto secretSharedOutput = - processor->processPeersData(dataSize, indexes, dataWidth); - secretSharedOutput.openToParty(0); - }; - - auto future0 = - std::async(task0, std::move(processor0), data, outputSize, dataWidth); - auto future1 = - std::async(task1, std::move(processor1), dataSize, index, dataWidth); - future1.get(); - auto rst = future0.get(); - for (size_t i = 0; i < outputSize; i++) { - fbpcf::testVectorEq(rst.at(i), expectedOutput.at(i)); - } -} - -TEST(DummyDataProcessor, testDummyDataProcessor) { - auto agentFactories = engine::communication::getInMemoryAgentFactory(2); - setupRealBackend<0, 1>(*agentFactories[0], *agentFactories[1]); - - insecure::DummyDataProcessorFactory<0> factory0(0, 1, *agentFactories[0]); - insecure::DummyDataProcessorFactory<1> factory1(1, 0, *agentFactories[1]); - testDataProcessor(factory0.create(), factory1.create()); -} - -} // namespace fbpcf::udp::data_processor