Skip to content
This repository was archived by the owner on Jun 20, 2025. It is now read-only.

Commit e944eec

Browse files
Ajinkya Ghongefacebook-github-bot
authored andcommitted
Integrate OramEncoder library with pc_translator. (#2269)
Summary: Pull Request resolved: #2269 # Context As per PC Translator design, we need a runtime library will be called during PC run. This library will be called at the beginning of PC run to encode specified fields in publisher side input into a encoded breakdown (aggregation) Ids based on active PC instruction sets for the run. The library will filter the active PC Instruction sets for the run based on parsing the pcs_features i.e. gatekeepers for the particular run. # Product decisions In this stack we would focus solely on functionality required for private lift runs. We would focus on the MVP implementation of the library and its integration with fbpcf ORAM encoder library in this stack. # Stack 1. Create runtime pc_translator library. 2. Add logic to retrieve and parse PC instruction set, filtered based on the active gatekeepers for the run. 3. Integrate pc_translator library with fbpcf ORAM encoder. 4. Add logic to generate transformed publisher output with encoded breakdown ID and write the output. # In this diff Integrate pc_translator library with fbpcf ORAM encoder. Differential Revision: D44634384 Privacy Context Container: L416713 fbshipit-source-id: c328cf3ce4759e02c0a3e0fd4aa7f36699cfb7f3
1 parent b9b21b8 commit e944eec

File tree

6 files changed

+117
-26
lines changed

6 files changed

+117
-26
lines changed

fbpcs/pc_translator/PCTranslator.cpp

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010

1111
#include <fbpcf/common/FunctionalUtil.h>
1212
#include <fbpcf/io/api/FileIOWrappers.h>
13+
#include <fbpcf/mpc_std_lib/oram/encoder/IFilter.h>
14+
#include <fbpcf/mpc_std_lib/oram/encoder/IOramEncoder.h>
15+
#include <fbpcf/mpc_std_lib/oram/encoder/OramEncoder.h>
16+
#include <algorithm>
1317
#include <set>
18+
#include "fbpcs/emp_games/common/Csv.h"
1419
#include "folly/String.h"
1520

1621
namespace pc_translator {
@@ -20,8 +25,8 @@ std::string PCTranslator::encode(const std::string& inputDataset) {
2025
PCTranslator::retrieveInstructionSetNamesForRun(pcsFeatures_);
2126
auto pcInstructionSets =
2227
PCTranslator::retrieveInstructionSets(validInstructionSetNames);
23-
PCTranslator::transformDataset(inputDataset, pcInstructionSets);
24-
return "";
28+
return PCTranslator::transformDataset(
29+
inputDataset, pcInstructionSets.front());
2530
}
2631

2732
std::string PCTranslator::decode(
@@ -34,7 +39,13 @@ PCTranslator::retrieveInstructionSets(
3439
std::vector<std::string>& instructionSetNames) {
3540
std::vector<std::shared_ptr<PCInstructionSet>> pcInstructionSets;
3641
for (auto instructionSetName : instructionSetNames) {
37-
auto file_path = instructionSetBasePath + instructionSetName + ".json";
42+
instructionSetName.erase(
43+
remove(instructionSetName.begin(), instructionSetName.end(), '\''),
44+
instructionSetName.end());
45+
instructionSetName.erase(
46+
remove(instructionSetName.begin(), instructionSetName.end(), ' '),
47+
instructionSetName.end());
48+
auto file_path = instructionSetBasePath_ + instructionSetName + ".json";
3849
auto contents = fbpcf::io::FileIOWrappers::readFile(file_path);
3950
pcInstructionSets.push_back(PCTranslator::parseInstructionSet(contents));
4051
}
@@ -55,16 +66,52 @@ std::vector<std::string> PCTranslator::retrieveInstructionSetNamesForRun(
5566
enabledFeatureFlags.begin(),
5667
enabledFeatureFlags.end(),
5768
std::back_inserter(validPCInstructionSets),
58-
[](const std::string& feature) { return feature.find("pc_instr") == 0; });
69+
[](const std::string& feature) {
70+
return feature.find("pc_instr") != std::string::npos;
71+
});
5972

6073
return validPCInstructionSets;
6174
}
6275

63-
void PCTranslator::transformDataset(
64-
const std::string& /* inputData */,
65-
const std::vector<std::shared_ptr<pc_translator::PCInstructionSet>>&
66-
/* pcInstructionSets */) {
67-
throw std::runtime_error("Unimplemented");
76+
std::string PCTranslator::transformDataset(
77+
const std::string& inputData,
78+
std::shared_ptr<pc_translator::PCInstructionSet> pcInstructionSet) {
79+
// Parse the input CSV
80+
auto lineNo = 0;
81+
std::vector<std::vector<uint32_t>> inputColums;
82+
private_measurement::csv::readCsv(
83+
inputData,
84+
[&](const std::vector<std::string>& header,
85+
const std::vector<std::string>& parts) {
86+
std::vector<uint32_t> inputColumnPerRow;
87+
for (std::vector<std::string>::size_type i = 0; i < header.size();
88+
++i) {
89+
auto column = header[i];
90+
auto value = std::atoi(parts[i].c_str());
91+
auto iter = std::find(
92+
pcInstructionSet->getGroupByIds().begin(),
93+
pcInstructionSet->getGroupByIds().end(),
94+
column);
95+
if (iter != pcInstructionSet->getGroupByIds().end()) {
96+
inputColumnPerRow.push_back(value);
97+
}
98+
}
99+
100+
inputColums.push_back(inputColumnPerRow);
101+
lineNo++;
102+
});
103+
104+
auto filters = std::make_unique<
105+
std::vector<std::unique_ptr<fbpcf::mpc_std_lib::oram::IFilter>>>(0);
106+
std::unique_ptr<fbpcf::mpc_std_lib::oram::IOramEncoder> encoder =
107+
std::make_unique<fbpcf::mpc_std_lib::oram::OramEncoder>(
108+
std::move(filters));
109+
110+
auto encodedIndexes = encoder->generateORAMIndexes(inputColums);
111+
112+
// TODO : Append the enodedIndexes at the end of publisher output and return
113+
// output path.
114+
return "";
68115
}
69116

70117
std::shared_ptr<PCInstructionSet> PCTranslator::parseInstructionSet(

fbpcs/pc_translator/PCTranslator.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ class PCTranslator {
2626
explicit PCTranslator(const std::string& pcsFeatures)
2727
: pcsFeatures_(pcsFeatures) {}
2828

29-
/*
30-
* Method to encode the configurable fields in input dataset as per the active
31-
* pc instruction sets for the run. This method will output the path of
32-
* transformed input dataset, which can be used in further PC run.
33-
*/
29+
explicit PCTranslator(
30+
const std::string& pcsFeatures,
31+
const std::string& instructionSetBasePath)
32+
: pcsFeatures_(pcsFeatures),
33+
instructionSetBasePath_(instructionSetBasePath) {}
34+
3435
std::string encode(const std::string& inputDataset);
3536

3637
/*
@@ -43,18 +44,17 @@ class PCTranslator {
4344

4445
private:
4546
std::string pcsFeatures_;
46-
const std::string instructionSetBasePath =
47+
std::string instructionSetBasePath_ =
4748
"https://pc-translator.s3.us-west-2.amazonaws.com/";
4849
std::vector<std::shared_ptr<PCInstructionSet>> retrieveInstructionSets(
4950
std::vector<std::string>& instructionSetNames);
5051
std::vector<std::string> retrieveInstructionSetNamesForRun(
5152
const std::string& pcsFeatures);
5253
std::shared_ptr<PCInstructionSet> parseInstructionSet(
5354
const std::string& instructionSet);
54-
void transformDataset(
55-
const std::string& input_data,
56-
const std::vector<std::shared_ptr<pc_translator::PCInstructionSet>>&
57-
pcInstructionSets);
55+
std::string transformDataset(
56+
const std::string& inputData,
57+
std::shared_ptr<pc_translator::PCInstructionSet> pcInstructionSet);
5858
};
5959

6060
} // namespace pc_translator
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#include <gtest/gtest.h>
9+
#include "../../emp_games/common/TestUtil.h"
10+
#include "fbpcs/pc_translator/PCTranslator.h"
11+
12+
namespace pc_translator {
13+
class TestPCTranslator : public ::testing::Test {
14+
public:
15+
protected:
16+
std::string pcs_features_;
17+
std::string test_instruction_set_base_path_;
18+
std::string test_publisher_input_path_;
19+
20+
void SetUp() override {
21+
pcs_features_ =
22+
"'num_mpc_container_mutation', 'private_lift_unified_data_process', 'pc_instr_test_instruction_set'";
23+
std::string baseDir =
24+
private_measurement::test_util::getBaseDirFromPath(__FILE__);
25+
test_instruction_set_base_path_ = baseDir + "input_processing/";
26+
test_publisher_input_path_ = baseDir + "publisher_unittest.csv";
27+
}
28+
};
29+
30+
TEST_F(TestPCTranslator, TestEncode) {
31+
auto pcTranslator = std::make_shared<PCTranslator>(
32+
pcs_features_, test_instruction_set_base_path_);
33+
auto outputPath = pcTranslator->encode(test_publisher_input_path_);
34+
EXPECT_EQ(outputPath, "");
35+
}
36+
} // namespace pc_translator

fbpcs/pc_translator/tests/input_processing/TestPCInstructionSet.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <gtest/gtest.h>
1414
#include "../../../emp_games/common/TestUtil.h"
1515
#include "fbpcs/pc_translator/input_processing/PCInstructionSet.h"
16-
#include "folly/Random.h"
1716

1817
namespace pc_translator {
1918
class TestPCInstructionSet : public ::testing::Test {
@@ -24,7 +23,7 @@ class TestPCInstructionSet : public ::testing::Test {
2423
void SetUp() override {
2524
std::string baseDir =
2625
private_measurement::test_util::getBaseDirFromPath(__FILE__);
27-
testInstructionSetPath_ = baseDir + "test_instruction_set.json";
26+
testInstructionSetPath_ = baseDir + "pc_instr_test_instruction_set.json";
2827
}
2928
};
3029

@@ -35,7 +34,7 @@ TEST_F(TestPCInstructionSet, TestStandardWorkflowTest) {
3534
auto groupByIds = pcInstructionSet->getGroupByIds();
3635
auto filterConstraints = pcInstructionSet->getFilterConstraints();
3736
EXPECT_EQ(groupByIds.size(), 2);
38-
EXPECT_EQ(filterConstraints.size(), 4);
37+
EXPECT_EQ(filterConstraints.size(), 3);
3938
EXPECT_EQ(filterConstraints[0].getName(), "gender");
4039
EXPECT_EQ(filterConstraints[0].getType(), "EQ");
4140
EXPECT_EQ(filterConstraints[0].getValue(), 0);

fbpcs/pc_translator/tests/input_processing/test_instruction_set.json renamed to fbpcs/pc_translator/tests/input_processing/pc_instr_test_instruction_set.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@
3030
{
3131
"constraint_type": "EQ",
3232
"value": "0"
33-
},
34-
{
35-
"constraint_type": "EQ",
36-
"value": "1"
3733
}
3834
]
3935
},
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
id_,opportunity,test_flag,opportunity_timestamp, age, gender
2+
cfcd208495d565ef66e7dff9f98764da,1,0,1600000430, 25, 0
3+
c4ca4238a0b923820dcc509a6f75849b,1,1,1600000401, 26, 1
4+
c81e728d9d4c2f636f067f89cc14862c,0,0,0, 44, 0
5+
eccbc87e4b5ce2fe28308fd9f2a7baf3,0,0,0, 23, 0
6+
a87ff679a2f3e71d9181a67b7542122c,0,0,0, 25, 0
7+
e4da3b7fbbce2345d7772b0674a318d5,1,1,1600000461, 24, 1
8+
1679091c5a880faf6fb5e6087eb1b2dc,1,0,1600000052, 25, 1
9+
8f14e45fceea167a5a36dedd4bea2543,1,0,1600000831, 26, 0
10+
c9f0f895fb98ab9159f51fd0297e236d,1,0,1600000530, 50, 0
11+
45c48cce2e2d7fbdea1afc51c7c6ad26,1,0,1600000972, 25, 1
12+
d3d9446802a44259755d38e6d163e820,0,0,0, 25, 0
13+
6512bd43d9caa6e02c990b0a82652dca,0,0,0, 25, 0

0 commit comments

Comments
 (0)