From 58309eb349b6db8a025851c6a24de35ea0a5fb73 Mon Sep 17 00:00:00 2001 From: xbw886 <1042740841@qq.com> Date: Wed, 4 Dec 2024 00:45:56 +0800 Subject: [PATCH] fix problems --- .../examples/{primitives => }/sse/BUILD.bazel | 19 ++- .../sse => sse/data}/income_census_test.csv | 0 .../sse/test.csv => sse/data/test_data.csv} | 0 yacl/examples/{primitives => }/sse/sse.cc | 145 +++++++++--------- yacl/examples/{primitives => }/sse/sse.h | 74 ++++----- .../examples/{primitives => }/sse/sse_test.cc | 32 ++-- .../{primitives/sse/TSet.cc => sse/tset.cc} | 89 ++++++----- .../{primitives/sse/TSet.h => sse/tset.h} | 30 ++-- .../sse/TSet_test.cc => sse/tset_test.cc} | 34 ++-- 9 files changed, 206 insertions(+), 217 deletions(-) rename yacl/examples/{primitives => }/sse/BUILD.bazel (82%) rename yacl/examples/{primitives/sse => sse/data}/income_census_test.csv (100%) rename yacl/examples/{primitives/sse/test.csv => sse/data/test_data.csv} (100%) rename yacl/examples/{primitives => }/sse/sse.cc (80%) mode change 100755 => 100644 rename yacl/examples/{primitives => }/sse/sse.h (59%) mode change 100755 => 100644 rename yacl/examples/{primitives => }/sse/sse_test.cc (87%) rename yacl/examples/{primitives/sse/TSet.cc => sse/tset.cc} (76%) rename yacl/examples/{primitives/sse/TSet.h => sse/tset.h} (73%) rename yacl/examples/{primitives/sse/TSet_test.cc => sse/tset_test.cc} (81%) diff --git a/yacl/examples/primitives/sse/BUILD.bazel b/yacl/examples/sse/BUILD.bazel similarity index 82% rename from yacl/examples/primitives/sse/BUILD.bazel rename to yacl/examples/sse/BUILD.bazel index ece1e6f3..f38c792a 100755 --- a/yacl/examples/primitives/sse/BUILD.bazel +++ b/yacl/examples/sse/BUILD.bazel @@ -1,4 +1,4 @@ -# Copyright 2024 Ant Group Co., Ltd. +# Copyright 2024 Li Zhihang. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -17,9 +17,9 @@ load("//bazel:yacl.bzl", "yacl_cc_library", "yacl_cc_test") package(default_visibility = ["//visibility:public"]) yacl_cc_library( - name = "TSet", - srcs = ["TSet.cc"], - hdrs = ["TSet.h"], + name = "tset", + srcs = ["tset.cc"], + hdrs = ["tset.h"], deps = [ "//yacl/crypto/hash:ssl_hash", "//yacl/crypto/hmac:hmac_sha256", @@ -28,10 +28,10 @@ yacl_cc_library( ) yacl_cc_test( - name = "TSet_test", - srcs = ["TSet_test.cc"], + name = "tset_test", + srcs = ["tset_test.cc"], deps = [ - ":TSet", + ":tset", ], ) @@ -42,7 +42,7 @@ yacl_cc_library( deps = [ "//yacl/crypto/ecc/openssl", "//yacl/crypto/rand", - "//yacl/examples/primitives/sse:TSet", + "//yacl/examples/sse:tset", "//yacl/io/rw:csv_reader", "//yacl/io/stream:file_io", "//yacl/math/mpint", @@ -52,6 +52,9 @@ yacl_cc_library( yacl_cc_test( name = "sse_test", srcs = ["sse_test.cc"], + data = [ + "data/test_data.csv", + ], deps = [ ":sse", ], diff --git a/yacl/examples/primitives/sse/income_census_test.csv b/yacl/examples/sse/data/income_census_test.csv similarity index 100% rename from yacl/examples/primitives/sse/income_census_test.csv rename to yacl/examples/sse/data/income_census_test.csv diff --git a/yacl/examples/primitives/sse/test.csv b/yacl/examples/sse/data/test_data.csv similarity index 100% rename from yacl/examples/primitives/sse/test.csv rename to yacl/examples/sse/data/test_data.csv diff --git a/yacl/examples/primitives/sse/sse.cc b/yacl/examples/sse/sse.cc old mode 100755 new mode 100644 similarity index 80% rename from yacl/examples/primitives/sse/sse.cc rename to yacl/examples/sse/sse.cc index f96ca848..6694eb77 --- a/yacl/examples/primitives/sse/sse.cc +++ b/yacl/examples/sse/sse.cc @@ -1,4 +1,4 @@ -// Copyright 2024 Ant Group Co., Ltd. +// Copyright 2024 Li Zhihang. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,27 +12,24 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "yacl/examples/primitives/sse/sse.h" +#include "yacl/examples/sse/sse.h" -namespace yacl::examples::primitives::sse { +namespace examples::sse { -SSE::SSE(int bucket_size, int slot_size, int lambda, int n_lambda, - const std::string& filename) +Sse::Sse(int bucket_size, int slot_size, int lambda, int n_lambda) : tset_(bucket_size, slot_size, lambda, n_lambda) { - initialize(filename); + Initialize(); } -std::string SSE::getKt() { return K_map_["Kt"]; } +std::string Sse::GetKt() { return k_map_["Kt"]; } // EDBSetup -std::pair< - std::vector>, - std::string> -SSE::EDBSetup() { - processAndUpdateTAndXSet(); +std::pair>, std::string> +Sse::EDBSetup() { + ProcessAndUpdateTAndXSet(); auto [TSet, Kt] = tset_.TSetSetup(T_, keywords_); TSet_ = TSet; - K_map_["Kt"] = Kt; + k_map_["Kt"] = Kt; return {TSet, Kt}; } @@ -40,36 +37,36 @@ SSE::EDBSetup() { std::tuple, std::vector>, std::vector> -SSE::SaveEDB(const std::string& k_map_file, const std::string& tset_file, +Sse::SaveEDB(const std::string& k_map_file, const std::string& tset_file, const std::string& xset_file) { - SaveKeys(K_map_, k_map_file); + SaveKeys(k_map_, k_map_file); SaveTSet(TSet_, tset_file); SaveXSet(XSet_, xset_file, ec_group_); - return {K_map_, TSet_, XSet_}; + return {k_map_, TSet_, XSet_}; } // LoadEDB std::tuple, std::vector>, std::vector> -SSE::LoadEDB(const std::string& k_map_file, const std::string& tset_file, +Sse::LoadEDB(const std::string& k_map_file, const std::string& tset_file, const std::string& xset_file) { - K_map_ = LoadKeys(k_map_file); + k_map_ = LoadKeys(k_map_file); TSet_ = LoadTSet(tset_file); XSet_ = LoadXSet(xset_file, ec_group_); - return {K_map_, TSet_, XSet_}; + return {k_map_, TSet_, XSet_}; } // SearchProtocol -std::vector SSE::SearchProtocol( +std::vector Sse::SearchProtocol( const std::vector& keywords_Search) { if (keywords_Search.empty()) { return {}; } std::string w1 = keywords_Search[0]; // Client computes stag - auto vector_stag = tset_.TSetGetTag(K_map_["Kt"], w1); - std::string stag = vectorToString(vector_stag); + auto vector_stag = tset_.TSetGetTag(k_map_["Kt"], w1); + std::string stag = VectorToString(vector_stag); // Server computes t ← TSetRetrieve(TSet, stag) std::vector, std::string>> t = @@ -79,22 +76,22 @@ std::vector SSE::SearchProtocol( // Client computes xtoken std::vector> xtoken; xtoken.resize(size + 1); - yacl::crypto::HmacSha256 hmac_F_SSE_Search_Kz(K_map_["Kz"]); - yacl::crypto::HmacSha256 hmac_F_SSE_Search_Kx(K_map_["Kx"]); + yacl::crypto::HmacSha256 hmac_F_SSE_Search_Kz(k_map_["Kz"]); + yacl::crypto::HmacSha256 hmac_F_SSE_Search_Kx(k_map_["Kx"]); for (size_t c = 1; c <= size; c++) { xtoken[c].resize(keywords_Search.size() + 1); for (size_t i = 2; i <= keywords_Search.size(); i++) { hmac_F_SSE_Search_Kz.Reset(); hmac_F_SSE_Search_Kz.Update(w1 + std::to_string(c)); auto mac_z1 = hmac_F_SSE_Search_Kz.CumulativeMac(); - std::string string_z1 = vectorToString(mac_z1); + std::string string_z1 = VectorToString(mac_z1); yacl::math::MPInt z1(string_z1); z1 = z1.Mod(ec_group_->GetOrder()); hmac_F_SSE_Search_Kx.Reset(); hmac_F_SSE_Search_Kx.Update(keywords_Search[i - 1]); auto mac_for_xtag_search = hmac_F_SSE_Search_Kx.CumulativeMac(); - std::string string_for_xtag_search = vectorToString(mac_for_xtag_search); + std::string string_for_xtag_search = VectorToString(mac_for_xtag_search); yacl::math::MPInt for_xtag_search(string_for_xtag_search); for_xtag_search = for_xtag_search.Mod(ec_group_->GetOrder()); @@ -114,7 +111,7 @@ std::vector SSE::SearchProtocol( bool allInXSet = true; for (size_t i = 2; i <= keywords_Search.size(); i++) { auto xtag = ec_group_->Mul(xtoken[c][i], y); - if (!isInXSet(ec_group_, xtag, XSet_)) { + if (!IsInXSet(ec_group_, xtag, XSet_)) { allInXSet = false; break; } @@ -132,13 +129,13 @@ std::vector SSE::SearchProtocol( // Client computes Ke ← Dec(Ks, w1) std::vector results; - yacl::crypto::HmacSha256 hmac_F_SSE_Search_Ks(K_map_["Ks"]); + yacl::crypto::HmacSha256 hmac_F_SSE_Search_Ks(k_map_["Ks"]); hmac_F_SSE_Search_Ks.Reset(); hmac_F_SSE_Search_Ks.Update(w1); auto Ke_mac = hmac_F_SSE_Search_Ks.CumulativeMac(); - uint128_t Ke = convert_to_uint128(Ke_mac); + uint128_t Ke = ConvertToUint128(Ke_mac); for (const auto& e : E) { - std::vector ind = aes_ctr_decrypt(e, Ke, 0); + std::vector ind = AesCtrDecrypt(e, Ke, 0); std::string ind_string(ind.begin(), ind.end()); std::cout << "Found match: " << ind_string << std::endl; results.push_back(ind_string); @@ -147,11 +144,11 @@ std::vector SSE::SearchProtocol( return results; } -SSE::~SSE() { ec_group_.reset(); } +Sse::~Sse() { ec_group_.reset(); } -// ? private functions +// ! private functions -bool SSE::isInXSet(const std::unique_ptr& ec_group, +bool Sse::IsInXSet(const std::unique_ptr& ec_group, const yacl::crypto::EcPoint& xtag, const std::vector& XSet) { for (const auto& xtag_in_XSet : XSet) { @@ -164,44 +161,46 @@ bool SSE::isInXSet(const std::unique_ptr& ec_group, } // 初始化密钥,curve等参数 -void SSE::initialize(const std::string& filename) { - auto [keywords, keyValuePairs, reverseIndex] = processAndSaveCSV(filename); +void Sse::Initialize() { + std::string filename = fmt::format("{}/yacl/examples/sse/data/test_data.csv", + std::filesystem::current_path().string()); + auto [keywords, keyValuePairs, reverseIndex] = ProcessAndSaveCSV(filename); keywords_ = keywords; keyValuePairs_ = keyValuePairs; reverseIndex_ = reverseIndex; - K_map_["Ks"] = "This is Ks"; - K_map_["Kx"] = "This is Kx"; - K_map_["Ki"] = "This is Ki"; - K_map_["Kz"] = "This is Kz"; + k_map_["Ks"] = "This is Ks"; + k_map_["Kx"] = "This is Kx"; + k_map_["Ki"] = "This is Ki"; + k_map_["Kz"] = "This is Kz"; const auto& curve = yacl::crypto::GetCurveMetaByName("secp224r1"); ec_group_ = yacl::crypto::openssl::OpensslGroup::Create(curve); } // 主功能函数:计算并更新 T 和 XSet -void SSE::processAndUpdateTAndXSet() { - 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"]); - yacl::crypto::HmacSha256 hmac_F_SSE_Kz(K_map_["Kz"]); +void Sse::ProcessAndUpdateTAndXSet() { + 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"]); + yacl::crypto::HmacSha256 hmac_F_SSE_Kz(k_map_["Kz"]); for (const auto& keyword : keywords_) { auto mac_Ke = hmac_F_SSE_Ks.Reset().Update(keyword).CumulativeMac(); - uint128_t Ke = convert_to_uint128(mac_Ke); + uint128_t Ke = ConvertToUint128(mac_Ke); auto mac_for_xtag = hmac_F_SSE_Kx.Reset().Update(keyword).CumulativeMac(); - std::string string_for_xtag = vectorToString(mac_for_xtag); + std::string string_for_xtag = VectorToString(mac_for_xtag); yacl::math::MPInt for_xtag(string_for_xtag); for_xtag = for_xtag.Mod(ec_group_->GetOrder()); - std::vector inds = fetchKeysByValue(reverseIndex_, keyword); + std::vector inds = FetchKeysByValue(reverseIndex_, keyword); std::vector, std::string>> t; size_t c = 1; for (const auto& ind : inds) { // xind auto mac_xind = hmac_F_SSE_Ki.Reset().Update(ind).CumulativeMac(); - std::string string_xind = vectorToString(mac_xind); + std::string string_xind = VectorToString(mac_xind); yacl::math::MPInt xind(string_xind); xind = xind.Mod(ec_group_->GetOrder()); @@ -209,7 +208,7 @@ void SSE::processAndUpdateTAndXSet() { auto mac_z = hmac_F_SSE_Kz.Reset() .Update(keyword + std::to_string(c)) .CumulativeMac(); - std::string string_z = vectorToString(mac_z); + std::string string_z = VectorToString(mac_z); yacl::math::MPInt z(string_z); z = z.Mod(ec_group_->GetOrder()); @@ -220,7 +219,7 @@ void SSE::processAndUpdateTAndXSet() { // append (e, y) to t. std::vector ind_vector(ind.begin(), ind.end()); - std::vector e = aes_ctr_encrypt(ind_vector, Ke, 0); + std::vector e = AesCtrEncrypt(ind_vector, Ke, 0); t.push_back(std::make_pair(e, y.ToString())); // add xtag to XSet. @@ -238,7 +237,7 @@ void SSE::processAndUpdateTAndXSet() { std::tuple, std::vector>, std::unordered_map>> -SSE::processAndSaveCSV(const std::string& file_path) { +Sse::ProcessAndSaveCSV(const std::string& file_path) { // 存储键值对 std::vector> keyValuePairs; @@ -306,14 +305,14 @@ SSE::processAndSaveCSV(const std::string& file_path) { } // 将 HMAC 结果转换为 uint128_t -uint128_t SSE::convert_to_uint128(const std::vector& mac) { +uint128_t Sse::ConvertToUint128(const std::vector& mac) { uint128_t result = 0; std::memcpy(&result, mac.data(), std::min(mac.size(), sizeof(result))); return result; } // std::vector 转换为std::string -std::string SSE::vectorToString(const std::vector& vec) { +std::string Sse::VectorToString(const std::vector& vec) { std::string result; for (auto& byte : vec) { result += std::to_string(static_cast(byte)); @@ -322,7 +321,7 @@ std::string SSE::vectorToString(const std::vector& vec) { } // 通过值获取对应的多个键 -std::vector SSE::fetchKeysByValue( +std::vector Sse::FetchKeysByValue( const std::unordered_map>& reverseIndex, const std::string& value) { @@ -339,9 +338,9 @@ std::vector SSE::fetchKeysByValue( } // AES-CTR 加密函数 -std::vector SSE::aes_ctr_encrypt(const std::vector& plaintext, - const uint128_t& key, - const uint128_t& iv) { +std::vector Sse::AesCtrEncrypt(const std::vector& plaintext, + const uint128_t& key, + const uint128_t& iv) { yacl::crypto::SymmetricCrypto crypto( yacl::crypto::SymmetricCrypto::CryptoType::AES128_CTR, key, iv); std::vector ciphertext(plaintext.size()); @@ -350,9 +349,9 @@ std::vector SSE::aes_ctr_encrypt(const std::vector& plaintext, } // AES-CTR 解密函数 -std::vector SSE::aes_ctr_decrypt( - const std::vector& ciphertext, const uint128_t& key, - const uint128_t& iv) { +std::vector Sse::AesCtrDecrypt(const std::vector& ciphertext, + const uint128_t& key, + const uint128_t& iv) { yacl::crypto::SymmetricCrypto crypto( yacl::crypto::SymmetricCrypto::CryptoType::AES128_CTR, key, iv); std::vector plaintext(ciphertext.size()); @@ -360,7 +359,7 @@ std::vector SSE::aes_ctr_decrypt( return plaintext; } -void SSE::SaveKeys(const std::map& K_map, +void Sse::SaveKeys(const std::map& K_map, const std::string& file_path) { std::ofstream K_file(file_path, std::ios::binary); if (K_file.is_open()) { @@ -383,10 +382,8 @@ void SSE::SaveKeys(const std::map& K_map, } } -void SSE::SaveTSet( - const std::vector< - std::vector>& TSet, - const std::string& file_path) { +void Sse::SaveTSet(const std::vector>& TSet, + const std::string& file_path) { std::ofstream tset_file(file_path, std::ios::binary); if (tset_file.is_open()) { @@ -413,7 +410,7 @@ void SSE::SaveTSet( } } -void SSE::SaveXSet(const std::vector& XSet, +void Sse::SaveXSet(const std::vector& XSet, const std::string& file_path, const std::unique_ptr& ec_group) { std::ofstream xset_file(file_path, std::ios::binary); @@ -447,7 +444,7 @@ void SSE::SaveXSet(const std::vector& XSet, } } -std::map SSE::LoadKeys(const std::string& file_path) { +std::map Sse::LoadKeys(const std::string& file_path) { std::ifstream K_file_read(file_path, std::ios::binary); std::map K_map_read; @@ -476,18 +473,18 @@ std::map SSE::LoadKeys(const std::string& file_path) { return K_map_read; } -std::vector> -SSE::LoadTSet(const std::string& file_path) { +std::vector> Sse::LoadTSet( + const std::string& file_path) { std::ifstream tset_file(file_path, std::ios::binary); - std::vector> TSet; + std::vector> TSet; while (tset_file.good()) { size_t bucket_size; tset_file.read(reinterpret_cast(&bucket_size), sizeof(size_t)); if (!tset_file.good()) break; // 检查是否到达文件末尾 - std::vector bucket; + std::vector bucket; for (size_t i = 0; i < bucket_size; i++) { - yacl::examples::primitives::sse::TSet::Record entry; + TSet::Record entry; size_t entry_size; tset_file.read(reinterpret_cast(&entry_size), sizeof(size_t)); entry.value.resize(entry_size); @@ -499,7 +496,7 @@ SSE::LoadTSet(const std::string& file_path) { return TSet; } -std::vector SSE::LoadXSet( +std::vector Sse::LoadXSet( const std::string& file_path, const std::unique_ptr& ec_group) { std::ifstream xset_file(file_path, std::ios::binary); @@ -530,4 +527,4 @@ std::vector SSE::LoadXSet( return XSet; } -} // namespace yacl::examples::primitives::sse +} // namespace examples::sse diff --git a/yacl/examples/primitives/sse/sse.h b/yacl/examples/sse/sse.h old mode 100755 new mode 100644 similarity index 59% rename from yacl/examples/primitives/sse/sse.h rename to yacl/examples/sse/sse.h index c05af5f7..7baf9920 --- a/yacl/examples/primitives/sse/sse.h +++ b/yacl/examples/sse/sse.h @@ -1,4 +1,4 @@ -// Copyright 2024 Ant Group Co., Ltd. +// Copyright 2024 Li Zhihang. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -15,6 +15,7 @@ #pragma once #include +#include #include #include #include @@ -27,37 +28,30 @@ #include "yacl/crypto/block_cipher/symmetric_crypto.h" #include "yacl/crypto/ecc/openssl/openssl_group.h" #include "yacl/crypto/rand/rand.h" -#include "yacl/examples/primitives/sse/TSet.h" +#include "yacl/examples/sse/tset.h" #include "yacl/io/rw/csv_reader.h" #include "yacl/io/stream/file_io.h" #include "yacl/math/mpint/mp_int.h" -namespace yacl::examples::primitives::sse { +namespace examples::sse { -class SSE { +class Sse { public: - SSE(int bucket_size = 8, int slot_size = 8, int lambda = 128, - int n_lambda = 256, - const std::string& filename = - "/home/xbw/yacl/yacl/examples/primitives/sse/test.csv"); + Sse(int bucket_size = 8, int slot_size = 8, int lambda = 128, + int n_lambda = 256); - std::pair< - std::vector>, - std::string> - EDBSetup(); + std::pair>, std::string> EDBSetup(); - std::string getKt(); - std::tuple< - std::map, - std::vector>, - std::vector> + std::string GetKt(); + std::tuple, + std::vector>, + std::vector> SaveEDB(const std::string& k_map_file = "/tmp/sse_test_data/K_map.bin", const std::string& tset_file = "/tmp/sse_test_data/TSet.bin", const std::string& xset_file = "/tmp/sse_test_data/XSet.bin"); - std::tuple< - std::map, - std::vector>, - std::vector> + std::tuple, + std::vector>, + std::vector> LoadEDB(const std::string& k_map_file = "/tmp/sse_test_data/K_map.bin", const std::string& tset_file = "/tmp/sse_test_data/TSet.bin", const std::string& xset_file = "/tmp/sse_test_data/XSet.bin"); @@ -65,45 +59,41 @@ class SSE { std::vector SearchProtocol( const std::vector& keywords); - ~SSE(); + ~Sse(); private: - bool isInXSet(const std::unique_ptr& ec_group, + bool IsInXSet(const std::unique_ptr& ec_group, const yacl::crypto::EcPoint& xtag, const std::vector& XSet); - void initialize(const std::string& filename); - void processAndUpdateTAndXSet(); + void Initialize(); + void ProcessAndUpdateTAndXSet(); std::tuple, std::vector>, std::unordered_map>> - processAndSaveCSV(const std::string& file_path); - uint128_t convert_to_uint128(const std::vector& mac); - std::string vectorToString(const std::vector& vec); - std::vector fetchKeysByValue( + ProcessAndSaveCSV(const std::string& file_path); + uint128_t ConvertToUint128(const std::vector& mac); + std::string VectorToString(const std::vector& vec); + std::vector FetchKeysByValue( const std::unordered_map>& reverseIndex, const std::string& value); - std::vector aes_ctr_encrypt(const std::vector& plaintext, - const uint128_t& key, - const uint128_t& iv); - std::vector aes_ctr_decrypt(const std::vector& ciphertext, - const uint128_t& key, - const uint128_t& iv); + std::vector AesCtrEncrypt(const std::vector& plaintext, + const uint128_t& key, const uint128_t& iv); + std::vector AesCtrDecrypt(const std::vector& ciphertext, + const uint128_t& key, const uint128_t& iv); void SaveKeys(const std::map& K_map, const std::string& file_path); void SaveTSet( - const std::vector< - std::vector>& TSet, + const std::vector>& TSet, const std::string& file_path); void SaveXSet(const std::vector& XSet, const std::string& file_path, const std::unique_ptr& ec_group); std::map LoadKeys(const std::string& file_path); - std::vector> - LoadTSet(const std::string& file_path); + std::vector> LoadTSet(const std::string& file_path); std::vector LoadXSet( const std::string& file_path, const std::unique_ptr& ec_group); @@ -112,14 +102,14 @@ class SSE { std::vector keywords_; std::vector> keyValuePairs_; std::unordered_map> reverseIndex_; - std::map K_map_; + std::map k_map_; std::unique_ptr ec_group_; std::unordered_map, std::string>>> T_; std::vector XSet_; - std::vector> TSet_; + std::vector> TSet_; TSet tset_; }; -} // namespace yacl::examples::primitives::sse +} // namespace examples::sse diff --git a/yacl/examples/primitives/sse/sse_test.cc b/yacl/examples/sse/sse_test.cc similarity index 87% rename from yacl/examples/primitives/sse/sse_test.cc rename to yacl/examples/sse/sse_test.cc index 71c47b02..1264d6f1 100755 --- a/yacl/examples/primitives/sse/sse_test.cc +++ b/yacl/examples/sse/sse_test.cc @@ -1,4 +1,4 @@ -// Copyright 2024 Ant Group Co., Ltd. +// Copyright 2024 Li Zhihang. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,19 +12,19 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "yacl/examples/primitives/sse/sse.h" +#include "yacl/examples/sse/sse.h" #include #include -namespace yacl::examples::primitives::sse { +namespace examples::sse { -class SSETest : public ::testing::Test { +class SseTest : public ::testing::Test { protected: void SetUp() override { // 初始化SSE系统,使用默认参数 - sse_ = std::make_unique(8, // bucket_size + sse_ = std::make_unique(8, // bucket_size 8, // slot_size 128, // lambda (安全参数) 256); // n_lambda @@ -35,10 +35,10 @@ class SSETest : public ::testing::Test { EXPECT_FALSE(kt.empty()); } - std::unique_ptr sse_; + std::unique_ptr sse_; }; -TEST_F(SSETest, BasicSearch) { +TEST_F(SseTest, BasicSearch) { std::vector keyword = {"race=Black"}; auto results = sse_->SearchProtocol(keyword); std::unordered_set expected_results = {"ID_130162", "ID_130165"}; @@ -49,21 +49,21 @@ TEST_F(SSETest, BasicSearch) { } // 测试空关键词搜索 -TEST_F(SSETest, EmptyKeywordSearch) { +TEST_F(SseTest, EmptyKeywordSearch) { std::vector keyword_empty = {}; auto results = sse_->SearchProtocol(keyword_empty); EXPECT_TRUE(results.empty()); } // 测试不存在的关键词搜索 -TEST_F(SSETest, NonExistentKeywordSearch) { +TEST_F(SseTest, NonExistentKeywordSearch) { std::vector non_existent = {"education=NonExistent"}; auto results = sse_->SearchProtocol(non_existent); EXPECT_TRUE(results.empty()); } // 测试两个关键词 -TEST_F(SSETest, TwoKeywordsSearch) { +TEST_F(SseTest, TwoKeywordsSearch) { std::vector two_keywords = {"race=Black", "gender=Male"}; auto results = sse_->SearchProtocol(two_keywords); std::unordered_set expected_results = {"ID_130162", "ID_130165"}; @@ -74,7 +74,7 @@ TEST_F(SSETest, TwoKeywordsSearch) { } // 测试三个关键词 -TEST_F(SSETest, ThreeKeywordsSearch) { +TEST_F(SseTest, ThreeKeywordsSearch) { std::vector three_keywords = {"race=Black", "gender=Male", "relationship=Husband"}; auto results = sse_->SearchProtocol(three_keywords); @@ -86,7 +86,7 @@ TEST_F(SSETest, ThreeKeywordsSearch) { } // 测试两个关键词,结果为空 -TEST_F(SSETest, TwoKeywordsNotExistSearch) { +TEST_F(SseTest, TwoKeywordsNotExistSearch) { std::vector two_keywords_not_exist = {"race=Black", "education=NonExistent"}; auto results = sse_->SearchProtocol(two_keywords_not_exist); @@ -94,7 +94,7 @@ TEST_F(SSETest, TwoKeywordsNotExistSearch) { } // 测试多次搜索的一致性 -TEST_F(SSETest, SearchConsistency) { +TEST_F(SseTest, SearchConsistency) { std::vector keyword = {"workclass=Private"}; // 第一次搜索 @@ -110,7 +110,7 @@ TEST_F(SSETest, SearchConsistency) { } // 测试EDB的保存和加载 -TEST_F(SSETest, SaveAndLoadEDB) { +TEST_F(SseTest, SaveAndLoadEDB) { // 首先执行一次搜索并保存结果 std::vector keyword = {"education=Bachelors"}; auto results_before = sse_->SearchProtocol(keyword); @@ -123,7 +123,7 @@ TEST_F(SSETest, SaveAndLoadEDB) { test_dir + "K_map.bin", test_dir + "TSet.bin", test_dir + "XSet.bin"); // 创建新的SSE实例并加载EDB - auto new_sse = std::make_unique(); + auto new_sse = std::make_unique(); auto [loaded_k_map, loaded_tset, loaded_xset] = new_sse->LoadEDB( test_dir + "K_map.bin", test_dir + "TSet.bin", test_dir + "XSet.bin"); @@ -137,4 +137,4 @@ TEST_F(SSETest, SaveAndLoadEDB) { } } -} // namespace yacl::examples::primitives::sse +} // namespace examples::sse diff --git a/yacl/examples/primitives/sse/TSet.cc b/yacl/examples/sse/tset.cc similarity index 76% rename from yacl/examples/primitives/sse/TSet.cc rename to yacl/examples/sse/tset.cc index 10afcf65..9eca661b 100755 --- a/yacl/examples/primitives/sse/TSet.cc +++ b/yacl/examples/sse/tset.cc @@ -1,4 +1,4 @@ -// Copyright 2024 Ant Group Co., Ltd. +// Copyright 2024 Li Zhihang. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,21 +12,21 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "yacl/examples/primitives/sse/TSet.h" +#include "yacl/examples/sse/tset.h" -namespace yacl::examples::primitives::sse { +namespace examples::sse { -TSet::TSet(int B, int S, int lambda, int n_lambda) - : B_(B), S_(S), lambda_(lambda), n_lambda_(n_lambda) { - initialize(); +TSet::TSet(int b, int s, int lambda, int n_lambda) + : b_(b), s_(s), lambda_(lambda), n_lambda_(n_lambda) { + Initialize(); } -bool TSet::areVectorsEqual(const std::vector& vec1, +bool TSet::AreVectorsEqual(const std::vector& vec1, const std::vector& vec2) const { return std::equal(vec1.begin(), vec1.end(), vec2.begin(), vec2.end()); } -std::vector TSet::pack( +std::vector TSet::Pack( const std::pair, std::string>& data) const { const auto& first = data.first; // 前部分(vector) const auto& second = data.second; // 后部分(string) @@ -48,7 +48,7 @@ std::vector TSet::pack( return result; } -std::pair, std::string> TSet::unpack( +std::pair, std::string> TSet::UnPack( const std::vector& packed_data) const { // 1. 提取前部分(固定 9 个字节) std::vector first(packed_data.begin(), packed_data.begin() + 9); @@ -64,7 +64,7 @@ std::pair, std::string> TSet::unpack( return {first, second}; } -std::string TSet::vectorToString(const std::vector& vec) const { +std::string TSet::VectorToString(const std::vector& vec) const { std::string result; for (auto& byte : vec) { result += std::to_string(static_cast(byte)); @@ -72,23 +72,23 @@ std::string TSet::vectorToString(const std::vector& vec) const { return result; } -void TSet::initialize() { +void TSet::Initialize() { // 初始化 TSet 数组 - TSet_.resize(B_, std::vector(S_)); + tset_.resize(b_, std::vector(s_)); // 初始化 Free 数组 - Free_.resize(B_); - for (int i = 0; i < B_; ++i) { - for (int j = 1; j <= S_; ++j) { - Free_[i].insert(j); + free_.resize(b_); + for (int i = 0; i < b_; ++i) { + for (int j = 1; j <= s_; ++j) { + free_[i].insert(j); } } // 初始化 TSet 中的每个 record - for (int i = 0; i < B_; ++i) { - for (int j = 0; j < S_; ++j) { - TSet_[i][j].label.resize(lambda_ / 8, 0); // 初始化为长度为 λ 的位字符串 - TSet_[i][j].value.resize(n_lambda_ / 8 + 1, + for (int i = 0; i < b_; ++i) { + for (int j = 0; j < s_; ++j) { + tset_[i][j].label.resize(lambda_ / 8, 0); // 初始化为长度为 λ 的位字符串 + tset_[i][j].value.resize(n_lambda_ / 8 + 1, 0); // 初始化为长度为 n(λ) + 1 的位字符串 } } @@ -103,16 +103,16 @@ std::pair>, std::string> TSet::TSetSetup( std::vector> Free; restart: - initialize(); + Initialize(); std::vector rand_bytes_Kt = yacl::crypto::RandBytes(32); - std::string Kt = vectorToString(rand_bytes_Kt); + std::string Kt = VectorToString(rand_bytes_Kt); yacl::crypto::HmacSha256 hmac_F_line_Tset(Kt); for (const auto& keyword : keywords) { hmac_F_line_Tset.Reset(); hmac_F_line_Tset.Update(keyword); auto mac = hmac_F_line_Tset.CumulativeMac(); - std::string stag = vectorToString(mac); + std::string stag = VectorToString(mac); const auto& t = T.at(keyword); // 使用 at 方法访问元素 yacl::crypto::HmacSha256 hmac_F_Tset(stag); size_t i = 1; @@ -120,37 +120,37 @@ std::pair>, std::string> TSet::TSetSetup( hmac_F_Tset.Reset(); hmac_F_Tset.Update(std::to_string(i)); auto mac = hmac_F_Tset.CumulativeMac(); - std::string mac_str = vectorToString(mac); + std::string mac_str = VectorToString(mac); yacl::crypto::Sm3Hash sm3; sm3.Reset(); std::vector hash = sm3.Update(mac_str).CumulativeHash(); size_t hash_value = 0; for (size_t i = 0; i < hash.size(); ++i) { - hash_value = (hash_value * 256 + hash[i]) % B_; + hash_value = (hash_value * 256 + hash[i]) % b_; } - size_t b = (hash_value % B_); + size_t b = (hash_value % b_); // 只取前 128 位(16 字节) std::vector L(hash.begin(), hash.begin() + lambda_ / 8); yacl::crypto::Sha256Hash sha256; sha256.Reset(); std::vector K = sha256.Update(mac_str).CumulativeHash(); - if (Free_[b].empty()) { + if (free_[b].empty()) { goto restart; } // 从 Free[b] 中随机选择一个元素 j,并删除 - auto it = Free_[b].begin(); + auto it = free_[b].begin(); std::advance( - it, yacl::crypto::RandU32() % Free_[b].size()); // 随机移动到某个位置 + it, yacl::crypto::RandU32() % free_[b].size()); // 随机移动到某个位置 int j = *it; - Free_[b].erase(j); + free_[b].erase(j); - j = (j - 1) % S_; - TSet_[b][j].label = L; + j = (j - 1) % s_; + tset_[b][j].label = L; // 计算 (β|si) ⊕ K - auto packed_si = pack(si); + auto packed_si = Pack(si); size_t beta = (i < t.size()) ? 1 : 0; std::vector beta_si; beta_si.push_back(static_cast(beta)); @@ -159,17 +159,16 @@ std::pair>, std::string> TSet::TSetSetup( for (size_t k = 0; k < beta_si.size(); ++k) { value_xor_k[k] = beta_si[k] ^ K[k % K.size()]; } - TSet_[b][j].value = value_xor_k; + tset_[b][j].value = value_xor_k; i++; } } - return {TSet_, Kt}; + return {tset_, Kt}; } std::vector TSet::TSetGetTag(const std::string& Kt, const std::string& w) const { - // std::string Kt = vectorToString(rand_bytes_Kt); yacl::crypto::HmacSha256 hmac_F_line_Tset(Kt); hmac_F_line_Tset.Reset(); hmac_F_line_Tset.Update(w); @@ -178,7 +177,7 @@ std::vector TSet::TSetGetTag(const std::string& Kt, } std::vector, std::string>> TSet::TSetRetrieve( - const std::vector>& TSet, + const std::vector>& tset, const std::string& stag) const { yacl::crypto::HmacSha256 hmac_F_Tset(stag); @@ -190,25 +189,25 @@ std::vector, std::string>> TSet::TSetRetrieve( hmac_F_Tset.Reset(); hmac_F_Tset.Update(std::to_string(i)); auto mac = hmac_F_Tset.CumulativeMac(); - std::string mac_str = vectorToString(mac); + std::string mac_str = VectorToString(mac); yacl::crypto::Sm3Hash sm3; sm3.Reset(); std::vector hash = sm3.Update(mac_str).CumulativeHash(); size_t hash_value = 0; for (size_t i = 0; i < hash.size(); ++i) { - hash_value = (hash_value * 256 + hash[i]) % B_; + hash_value = (hash_value * 256 + hash[i]) % b_; } - size_t b = (hash_value % B_); + size_t b = (hash_value % b_); // 只取前 128 位(16 字节) std::vector L(hash.begin(), hash.begin() + lambda_ / 8); yacl::crypto::Sha256Hash sha256; sha256.Reset(); std::vector K = sha256.Update(mac_str).CumulativeHash(); - auto& B = TSet[b]; + auto& B = tset[b]; int j = 0; - for (; j < S_; ++j) { - if (areVectorsEqual(B[j].label, L)) { + for (; j < s_; ++j) { + if (AreVectorsEqual(B[j].label, L)) { std::vector v(B[j].value.size()); for (size_t k = 0; k < v.size(); ++k) { v[k] = B[j].value[k] ^ K[k % K.size()]; @@ -216,7 +215,7 @@ std::vector, std::string>> TSet::TSetRetrieve( // Let β be the first bit of v, and s the remaining n(λ) bits of v beta = v[0]; std::vector s(v.begin() + 1, v.end()); - auto unpacked_s = unpack(s); + auto unpacked_s = UnPack(s); t.push_back(unpacked_s); } } @@ -229,4 +228,4 @@ std::vector, std::string>> TSet::TSetRetrieve( return t; } -} // namespace yacl::examples::primitives::sse +} // namespace examples::sse diff --git a/yacl/examples/primitives/sse/TSet.h b/yacl/examples/sse/tset.h similarity index 73% rename from yacl/examples/primitives/sse/TSet.h rename to yacl/examples/sse/tset.h index 94d1481c..f61ffbf7 100755 --- a/yacl/examples/primitives/sse/TSet.h +++ b/yacl/examples/sse/tset.h @@ -1,4 +1,4 @@ -// Copyright 2024 Ant Group Co., Ltd. +// Copyright 2024 Li Zhihang. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -24,7 +24,7 @@ #include "yacl/crypto/hmac/hmac_sha256.h" #include "yacl/crypto/rand/rand.h" -namespace yacl::examples::primitives::sse { +namespace examples::sse { class TSet { public: @@ -33,18 +33,18 @@ class TSet { std::vector value; // 存储长度为 n(λ) + 1 的位字符串 }; - TSet(int B, int S, int lambda, int n_lambda); + TSet(int b, int s, int lambda, int n_lambda); - bool areVectorsEqual(const std::vector& vec1, + bool AreVectorsEqual(const std::vector& vec1, const std::vector& vec2) const; - std::vector pack( + std::vector Pack( const std::pair, std::string>& data) const; - std::pair, std::string> unpack( + std::pair, std::string> UnPack( const std::vector& packed_data) const; - std::string vectorToString(const std::vector& vec) const; + std::string VectorToString(const std::vector& vec) const; std::pair>, std::string> TSetSetup( const std::unordered_map< @@ -60,18 +60,18 @@ class TSet { const std::string& stag) const; // 公共接口函数,用于访问私有成员变量 - const std::vector>& getTSet() const { return TSet_; } - const std::vector>& getFree() const { return Free_; } + const std::vector>& GetTSet() const { return tset_; } + const std::vector>& GetFree() const { return free_; } private: - void initialize(); + void Initialize(); - int B_; - int S_; + int b_; + int s_; int lambda_; int n_lambda_; - std::vector> TSet_; - std::vector> Free_; + std::vector> tset_; + std::vector> free_; }; -} // namespace yacl::examples::primitives::sse +} // namespace examples::sse diff --git a/yacl/examples/primitives/sse/TSet_test.cc b/yacl/examples/sse/tset_test.cc similarity index 81% rename from yacl/examples/primitives/sse/TSet_test.cc rename to yacl/examples/sse/tset_test.cc index be2610b3..77346532 100755 --- a/yacl/examples/primitives/sse/TSet_test.cc +++ b/yacl/examples/sse/tset_test.cc @@ -1,4 +1,4 @@ -// Copyright 2024 Ant Group Co., Ltd. +// Copyright 2024 Li Zhihang. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,20 +12,20 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "yacl/examples/primitives/sse/TSet.h" +#include "yacl/examples/sse/tset.h" #include #include -namespace yacl::examples::primitives::sse { +namespace examples::sse { class TSetTest : public ::testing::Test { protected: void SetUp() override { // 初始化TSet系统,使用默认参数 - tset_ = std::make_unique(2, // bucket_size (B) - 3, // slot_size (S) + tset_ = std::make_unique(2, // bucket_size (b) + 3, // slot_size (s) 128, // lambda 256); // n_lambda } @@ -38,19 +38,19 @@ TEST_F(TSetTest, AreVectorsEqual) { std::vector vec2 = {1, 2, 3}; std::vector vec3 = {4, 5, 6}; - EXPECT_TRUE(tset_->areVectorsEqual(vec1, vec2)); - EXPECT_FALSE(tset_->areVectorsEqual(vec1, vec3)); + EXPECT_TRUE(tset_->AreVectorsEqual(vec1, vec2)); + EXPECT_FALSE(tset_->AreVectorsEqual(vec1, vec3)); } // 测试 initialize 函数 TEST_F(TSetTest, Initialize) { // 检查 TSet 和 Free 的大小 - EXPECT_EQ(tset_->getTSet().size(), 2); - EXPECT_EQ(tset_->getTSet()[0].size(), 3); - EXPECT_EQ(tset_->getTSet()[0][0].label.size(), 128 / 8); - EXPECT_EQ(tset_->getTSet()[0][0].value.size(), 256 / 8 + 1); - EXPECT_EQ(tset_->getFree().size(), 2); - EXPECT_EQ(tset_->getFree()[0].size(), 3); + EXPECT_EQ(tset_->GetTSet().size(), 2); + EXPECT_EQ(tset_->GetTSet()[0].size(), 3); + EXPECT_EQ(tset_->GetTSet()[0][0].label.size(), 128 / 8); + EXPECT_EQ(tset_->GetTSet()[0][0].value.size(), 256 / 8 + 1); + EXPECT_EQ(tset_->GetFree().size(), 2); + EXPECT_EQ(tset_->GetFree()[0].size(), 3); } // 测试 pack/unpack 函数 @@ -60,14 +60,14 @@ TEST_F(TSetTest, PackAndUnpack) { std::pair, std::string> data = {vec, str}; // 测试 pack - std::vector packed = tset_->pack(data); + std::vector packed = tset_->Pack(data); EXPECT_EQ(packed.size(), vec.size() + str.size() + 4); EXPECT_TRUE(std::equal(vec.begin(), vec.end(), packed.begin())); EXPECT_TRUE( std::equal(str.begin(), str.end(), packed.begin() + vec.size() + 4)); // 测试 unpack - auto result = tset_->unpack(packed); + auto result = tset_->UnPack(packed); EXPECT_EQ(result.first, vec); EXPECT_EQ(result.second, str); } @@ -117,7 +117,7 @@ TEST_F(TSetTest, CompleteRetrievalFlow) { // Get tag for keyword12 std::string w = "keyword12"; auto vector_stag = tset_->TSetGetTag(Kt, w); - std::string stag = tset_->vectorToString(vector_stag); + std::string stag = tset_->VectorToString(vector_stag); // Retrieve auto retrieved = tset_->TSetRetrieve(TSet, stag); @@ -132,4 +132,4 @@ TEST_F(TSetTest, CompleteRetrievalFlow) { EXPECT_EQ(retrieved[1].second, "value2"); } -} // namespace yacl::examples::primitives::sse +} // namespace yacl::examples::sse