Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【OSCP】 + 在 YACL 上实现对称可搜索加密算法 #423

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
20 changes: 10 additions & 10 deletions yacl/examples/sse/sse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@
namespace examples::sse {

Sse::Sse(int bucket_size, int slot_size, int lambda, int n_lambda)
: tset_(bucket_size, slot_size, lambda, n_lambda) {
: iv_(yacl::crypto::RandU32()),
xbw886 marked this conversation as resolved.
Show resolved Hide resolved
tset_(bucket_size, slot_size, lambda, n_lambda) {
Initialize();
}

std::string Sse::GetKt() { return k_map_["Kt"]; }

// EDBSetup
std::pair<std::vector<std::vector<TSet::Record>>, std::string> Sse::EDBSetup(
const uint128_t& iv) {
ProcessAndUpdateTAndXSet(iv);
std::pair<std::vector<std::vector<TSet::Record>>, std::string> Sse::EDBSetup() {
ProcessAndUpdateTAndXSet(iv_);
auto [TSet, Kt] = tset_.TSetSetup(T_, keywords_);
TSet_ = TSet;
k_map_["Kt"] = Kt;
Expand Down Expand Up @@ -59,7 +57,7 @@ Sse::LoadEDB(const std::string& k_map_file, const std::string& tset_file,

// SearchProtocol
std::vector<std::string> Sse::SearchProtocol(
const std::vector<std::string>& keywords_Search, const uint128_t& iv) {
const std::vector<std::string>& keywords_Search) {
if (keywords_Search.empty()) {
return {};
}
Expand Down Expand Up @@ -135,7 +133,7 @@ std::vector<std::string> Sse::SearchProtocol(
auto Ke_mac = hmac_F_SSE_Search_Ks.CumulativeMac();
uint128_t Ke = ConvertToUint128(Ke_mac);
for (const auto& e : E) {
std::vector<uint8_t> ind = AesCtrDecrypt(e, Ke, iv);
std::vector<uint8_t> ind = AesCtrDecrypt(e, Ke, iv_);
std::string ind_string(ind.begin(), ind.end());
std::cout << "Found match: " << ind_string << std::endl;
results.push_back(ind_string);
Expand Down Expand Up @@ -169,6 +167,8 @@ void Sse::Initialize() {
keyValuePairs_ = keyValuePairs;
reverseIndex_ = reverseIndex;

iv_ = yacl::crypto::RandU32();
xbw886 marked this conversation as resolved.
Show resolved Hide resolved

auto rand_bytes_Ks = yacl::crypto::RandU32();
auto rand_bytes_Kx = yacl::crypto::RandU32();
auto rand_bytes_Ki = yacl::crypto::RandU32();
Expand All @@ -183,7 +183,7 @@ void Sse::Initialize() {
}

// 主功能函数:计算并更新 T 和 XSet
void Sse::ProcessAndUpdateTAndXSet(const uint128_t& iv) {
void Sse::ProcessAndUpdateTAndXSet(const uint128_t& iv_) {
xbw886 marked this conversation as resolved.
Show resolved Hide resolved
yacl::crypto::HmacSha256 hmac_F_SSE_Ks(k_map_["Ks"]);
yacl::crypto::HmacSha256 hmac_F_SSE_Kx(k_map_["Kx"]);
yacl::crypto::HmacSha256 hmac_F_SSE_Ki(k_map_["Ki"]);
Expand Down Expand Up @@ -223,7 +223,7 @@ void Sse::ProcessAndUpdateTAndXSet(const uint128_t& iv) {

// append (e, y) to t.
std::vector<uint8_t> ind_vector(ind.begin(), ind.end());
std::vector<uint8_t> e = AesCtrEncrypt(ind_vector, Ke, iv);
std::vector<uint8_t> e = AesCtrEncrypt(ind_vector, Ke, iv_);
xbw886 marked this conversation as resolved.
Show resolved Hide resolved
t.push_back(std::make_pair(e, y.ToString()));

// add xtag to XSet.
Expand Down
7 changes: 3 additions & 4 deletions yacl/examples/sse/sse.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ class Sse {
Sse(int bucket_size = 8, int slot_size = 8, int lambda = 128,
int n_lambda = 256);

std::pair<std::vector<std::vector<TSet::Record>>, std::string> EDBSetup(
const uint128_t& iv);
std::pair<std::vector<std::vector<TSet::Record>>, std::string> EDBSetup();

std::string GetKt();
std::tuple<std::map<std::string, std::string>,
std::vector<std::vector<TSet::Record>>,
std::vector<yacl::crypto::EcPoint>>
Expand All @@ -58,7 +56,7 @@ class Sse {
const std::string& xset_file = "/tmp/sse_test_data/XSet.bin");

std::vector<std::string> SearchProtocol(
xbw886 marked this conversation as resolved.
Show resolved Hide resolved
const std::vector<std::string>& keywords, const uint128_t& iv);
const std::vector<std::string>& keywords);

~Sse();

Expand Down Expand Up @@ -99,6 +97,7 @@ class Sse {
const std::unique_ptr<yacl::crypto::EcGroup>& ec_group);

// 其他私有成员
uint128_t iv_;
std::vector<std::string> keywords_;
std::vector<std::pair<std::string, std::string>> keyValuePairs_;
std::unordered_map<std::string, std::vector<std::string>> reverseIndex_;
Expand Down
23 changes: 11 additions & 12 deletions yacl/examples/sse/sse_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "yacl/crypto/rand/rand.h"

namespace examples::sse {
auto iv = yacl::crypto::RandU32();

class SseTest : public ::testing::Test {
protected:
Expand All @@ -33,7 +32,7 @@ class SseTest : public ::testing::Test {
256); // n_lambda

// 设置EDB
auto [tset, kt] = sse_->EDBSetup(iv);
auto [tset, kt] = sse_->EDBSetup();
EXPECT_FALSE(tset.empty());
EXPECT_FALSE(kt.empty());
}
Expand All @@ -43,7 +42,7 @@ class SseTest : public ::testing::Test {

TEST_F(SseTest, BasicSearch) {
std::vector<std::string> keyword = {"race=Black"};
auto results = sse_->SearchProtocol(keyword, iv);
auto results = sse_->SearchProtocol(keyword);
std::unordered_set<std::string> expected_results = {"ID_130162", "ID_130165"};
EXPECT_EQ(results.size(), expected_results.size());
std::unordered_set<std::string> actual_results(results.begin(),
Expand All @@ -54,21 +53,21 @@ TEST_F(SseTest, BasicSearch) {
// 测试空关键词搜索
TEST_F(SseTest, EmptyKeywordSearch) {
std::vector<std::string> keyword_empty = {};
auto results = sse_->SearchProtocol(keyword_empty, iv);
auto results = sse_->SearchProtocol(keyword_empty);
EXPECT_TRUE(results.empty());
}

// 测试不存在的关键词搜索
TEST_F(SseTest, NonExistentKeywordSearch) {
std::vector<std::string> non_existent = {"education=NonExistent"};
auto results = sse_->SearchProtocol(non_existent, iv);
auto results = sse_->SearchProtocol(non_existent);
EXPECT_TRUE(results.empty());
}

// 测试两个关键词
TEST_F(SseTest, TwoKeywordsSearch) {
std::vector<std::string> two_keywords = {"race=Black", "gender=Male"};
auto results = sse_->SearchProtocol(two_keywords, iv);
auto results = sse_->SearchProtocol(two_keywords);
std::unordered_set<std::string> expected_results = {"ID_130162", "ID_130165"};
EXPECT_EQ(results.size(), expected_results.size());
std::unordered_set<std::string> actual_results(results.begin(),
Expand All @@ -80,7 +79,7 @@ TEST_F(SseTest, TwoKeywordsSearch) {
TEST_F(SseTest, ThreeKeywordsSearch) {
std::vector<std::string> three_keywords = {"race=Black", "gender=Male",
"relationship=Husband"};
auto results = sse_->SearchProtocol(three_keywords, iv);
auto results = sse_->SearchProtocol(three_keywords);
std::unordered_set<std::string> expected_results = {"ID_130165"};
EXPECT_EQ(results.size(), expected_results.size());
std::unordered_set<std::string> actual_results(results.begin(),
Expand All @@ -92,7 +91,7 @@ TEST_F(SseTest, ThreeKeywordsSearch) {
TEST_F(SseTest, TwoKeywordsNotExistSearch) {
std::vector<std::string> two_keywords_not_exist = {"race=Black",
"education=NonExistent"};
auto results = sse_->SearchProtocol(two_keywords_not_exist, iv);
auto results = sse_->SearchProtocol(two_keywords_not_exist);
EXPECT_TRUE(results.empty());
}

Expand All @@ -101,9 +100,9 @@ TEST_F(SseTest, SearchConsistency) {
std::vector<std::string> keyword = {"workclass=Private"};

// 第一次搜索
auto results1 = sse_->SearchProtocol(keyword, iv);
auto results1 = sse_->SearchProtocol(keyword);
// 第二次搜索
auto results2 = sse_->SearchProtocol(keyword, iv);
auto results2 = sse_->SearchProtocol(keyword);

// 验证两次搜索结果一致
EXPECT_EQ(results1.size(), results2.size());
Expand All @@ -116,7 +115,7 @@ TEST_F(SseTest, SearchConsistency) {
TEST_F(SseTest, SaveAndLoadEDB) {
// 首先执行一次搜索并保存结果
std::vector<std::string> keyword = {"education=Bachelors"};
auto results_before = sse_->SearchProtocol(keyword, iv);
auto results_before = sse_->SearchProtocol(keyword);

// 保存EDB到文件
std::string test_dir = "/tmp/sse_test_data/";
Expand All @@ -133,7 +132,7 @@ TEST_F(SseTest, SaveAndLoadEDB) {
test_dir + "K_map.bin", test_dir + "TSet.bin", test_dir + "XSet.bin");

// 使用加载后的实例执行相同的搜索
auto results_after = new_sse->SearchProtocol(keyword, iv);
auto results_after = new_sse->SearchProtocol(keyword);

// 验证搜索结果一致性
EXPECT_EQ(results_before.size(), results_after.size());
Expand Down