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

Commit bc2b134

Browse files
ajinkyaghongefacebook-github-bot
authored andcommitted
Called the OramEncoder library.
Differential Revision: D44634384 fbshipit-source-id: cf53d36c32b36734d0d6c3947da58a6c87dcd9cc
1 parent 0fe2b39 commit bc2b134

File tree

6 files changed

+122
-26
lines changed

6 files changed

+122
-26
lines changed

fbpcs/pc_translator/PCTranslator.cpp

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@
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 <stdexcept>
19+
#include "fbpcs/emp_games/common/Csv.h"
1420
#include "folly/String.h"
1521

1622
namespace pc_translator {
@@ -20,8 +26,12 @@ std::string PCTranslator::encode(const std::string& inputDataset) {
2026
PCTranslator::retrieveInstructionSetNamesForRun(pcsFeatures_);
2127
auto pcInstructionSets =
2228
PCTranslator::retrieveInstructionSets(validInstructionSetNames);
23-
PCTranslator::transformDataset(inputDataset, pcInstructionSets);
24-
return "";
29+
if (pcInstructionSets.empty()) {
30+
// No instruction set found. return the input dataset path.
31+
return inputDataset;
32+
}
33+
return PCTranslator::transformDataset(
34+
inputDataset, pcInstructionSets.front());
2535
}
2636

2737
std::string PCTranslator::decode(
@@ -34,7 +44,13 @@ PCTranslator::retrieveInstructionSets(
3444
std::vector<std::string>& instructionSetNames) {
3545
std::vector<std::shared_ptr<PCInstructionSet>> pcInstructionSets;
3646
for (auto instructionSetName : instructionSetNames) {
37-
auto file_path = instructionSetBasePath + instructionSetName + ".json";
47+
instructionSetName.erase(
48+
remove(instructionSetName.begin(), instructionSetName.end(), '\''),
49+
instructionSetName.end());
50+
instructionSetName.erase(
51+
remove(instructionSetName.begin(), instructionSetName.end(), ' '),
52+
instructionSetName.end());
53+
auto file_path = instructionSetBasePath_ + instructionSetName + ".json";
3854
auto contents = fbpcf::io::FileIOWrappers::readFile(file_path);
3955
pcInstructionSets.push_back(PCTranslator::parseInstructionSet(contents));
4056
}
@@ -55,16 +71,52 @@ std::vector<std::string> PCTranslator::retrieveInstructionSetNamesForRun(
5571
enabledFeatureFlags.begin(),
5672
enabledFeatureFlags.end(),
5773
std::back_inserter(validPCInstructionSets),
58-
[](const std::string& feature) { return feature.find("pc_instr") == 0; });
74+
[](const std::string& feature) {
75+
return feature.find("pc_instr") != std::string::npos;
76+
});
5977

6078
return validPCInstructionSets;
6179
}
6280

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");
81+
std::string PCTranslator::transformDataset(
82+
const std::string& inputData,
83+
std::shared_ptr<pc_translator::PCInstructionSet> pcInstructionSet) {
84+
// Parse the input CSV
85+
auto lineNo = 0;
86+
std::vector<std::vector<uint32_t>> inputColums;
87+
private_measurement::csv::readCsv(
88+
inputData,
89+
[&](const std::vector<std::string>& header,
90+
const std::vector<std::string>& parts) {
91+
std::vector<uint32_t> inputColumnPerRow;
92+
for (std::vector<std::string>::size_type i = 0; i < header.size();
93+
++i) {
94+
auto& column = header[i];
95+
auto value = std::atoi(parts[i].c_str());
96+
auto iter = std::find(
97+
pcInstructionSet->getGroupByIds().begin(),
98+
pcInstructionSet->getGroupByIds().end(),
99+
column);
100+
if (iter != pcInstructionSet->getGroupByIds().end()) {
101+
inputColumnPerRow.push_back(value);
102+
}
103+
}
104+
105+
inputColums.push_back(inputColumnPerRow);
106+
lineNo++;
107+
});
108+
109+
auto filters = std::make_unique<
110+
std::vector<std::unique_ptr<fbpcf::mpc_std_lib::oram::IFilter>>>(0);
111+
std::unique_ptr<fbpcf::mpc_std_lib::oram::IOramEncoder> encoder =
112+
std::make_unique<fbpcf::mpc_std_lib::oram::OramEncoder>(
113+
std::move(filters));
114+
115+
auto encodedIndexes = encoder->generateORAMIndexes(inputColums);
116+
117+
// TODO : Append the enodedIndexes at the end of publisher output and return
118+
// output path.
119+
return "";
68120
}
69121

70122
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
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)