-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
repo-sync-2024-06-25T13:11:42+0800 (#345)
- Loading branch information
Jamie-Cui
authored
Jun 25, 2024
1 parent
be528ae
commit a91c4f2
Showing
36 changed files
with
848 additions
and
471 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Copyright 2024 zhangwfjh | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
load("//bazel:yacl.bzl", "AES_COPT_FLAGS", "yacl_cc_library", "yacl_cc_test") | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
yacl_cc_library( | ||
name = "ecdh_psi", | ||
srcs = [ | ||
"ecdh_psi.cc", | ||
], | ||
hdrs = [ | ||
"ecdh_psi.h", | ||
], | ||
deps = [ | ||
"//yacl/crypto/ecc", | ||
"//yacl/link", | ||
], | ||
) | ||
|
||
yacl_cc_test( | ||
name = "ecdh_psi_test", | ||
srcs = ["ecdh_psi_test.cc"], | ||
deps = [":ecdh_psi"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2023 Ant Group Co., Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "examples/psi/ecdh_psi.h" | ||
|
||
#include <memory> | ||
#include <vector> | ||
|
||
#include "yacl/crypto/ecc/ec_point.h" | ||
#include "yacl/crypto/ecc/ecc_spi.h" | ||
#include "yacl/link/link.h" | ||
#include "yacl/secparam.h" | ||
|
||
namespace examples::psi { | ||
|
||
void EcdhPsi::MaskStrings(absl::Span<std::string> in, | ||
absl::Span<yc::EcPoint> out) { | ||
YACL_ENFORCE(in.size() == out.size()); | ||
for (size_t i = 0; i < in.size(); ++i) { | ||
out[i] = ec_->HashToCurve(yc::HashToCurveStrategy::Autonomous, in[i]); | ||
ec_->MulInplace(&out[i], sk_); | ||
} | ||
} | ||
|
||
void EcdhPsi::MaskEcPoints(absl::Span<yc::EcPoint> in, | ||
absl::Span<std::string> out) { | ||
YACL_ENFORCE(in.size() == out.size()); | ||
for (size_t i = 0; i < in.size(); ++i) { | ||
out[i] = ec_->SerializePoint(ec_->Mul(in[i], sk_)); | ||
} | ||
} | ||
|
||
} // namespace examples::psi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2023 Ant Group Co., Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#pragma once | ||
|
||
#include <memory> | ||
#include <vector> | ||
|
||
#include "yacl/crypto/ecc/ec_point.h" | ||
#include "yacl/crypto/ecc/ecc_spi.h" | ||
#include "yacl/link/link.h" | ||
#include "yacl/secparam.h" | ||
|
||
namespace examples::psi { | ||
|
||
namespace yc = yacl::crypto; | ||
|
||
// An example of PSI protocol | ||
// | ||
// NOTE: this PSI protocol is designed solely for demonstation and is not ready, | ||
// or designed for production use, please do not use this in production. | ||
// | ||
// NOTE: we recommend user to use https://github.com/secretflow/psi | ||
// | ||
class EcdhPsi { | ||
public: | ||
EcdhPsi() { | ||
// Use FourQ curve | ||
ec_ = yc::EcGroupFactory::Instance().Create(/* curve name */ "FourQ"); | ||
|
||
// Generate random key | ||
yc::MPInt::RandomLtN(ec_->GetOrder(), &sk_); | ||
} | ||
|
||
// Mask input strings with secret key, and outputs the EcPoint results | ||
void MaskStrings(absl::Span<std::string> in, absl::Span<yc::EcPoint> out); | ||
|
||
// Mask input EcPoints with secret key, and outputs the serialized | ||
// EcPoint strings | ||
void MaskEcPoints(absl::Span<yc::EcPoint> in, absl::Span<std::string> out); | ||
|
||
private: | ||
yc::MPInt sk_; // secret key | ||
std::shared_ptr<yc::EcGroup> ec_; // ec group | ||
}; | ||
|
||
} // namespace examples::psi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright 2023 Ant Group Co., Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "examples/psi/ecdh_psi.h" | ||
|
||
#include <algorithm> | ||
|
||
#include "gtest/gtest.h" | ||
|
||
#include "yacl/crypto/hash/hash_utils.h" | ||
|
||
namespace examples::psi { | ||
|
||
namespace { | ||
std::vector<std::string> CreateRangeItems(size_t begin, size_t size) { | ||
std::vector<std::string> ret; | ||
for (size_t i = 0; i < size; i++) { | ||
ret.push_back(std::to_string(begin + i)); | ||
} | ||
return ret; | ||
} | ||
inline std::vector<size_t> GetIntersectionIdx( | ||
const std::vector<std::string> &x, const std::vector<std::string> &y) { | ||
std::set<std::string> set(x.begin(), x.end()); | ||
std::vector<size_t> ret; | ||
for (size_t i = 0; i < y.size(); ++i) { | ||
if (set.count(y[i]) != 0) { | ||
ret.push_back(i); | ||
} | ||
} | ||
return ret; | ||
} | ||
} // namespace | ||
|
||
TEST(PsiTest, Works) { | ||
size_t n = 4; | ||
auto x = CreateRangeItems(0, n); | ||
auto y = CreateRangeItems(3, n); | ||
|
||
EcdhPsi alice; | ||
EcdhPsi bob; | ||
|
||
// ------------------- | ||
// Step 1 | ||
// ------------------- | ||
std::vector<yc::EcPoint> x_points(n); | ||
// x_points = H(x) ^ {alice_sk} | ||
alice.MaskStrings(absl::MakeSpan(x), absl::MakeSpan(x_points)); | ||
|
||
std::vector<yc::EcPoint> y_points(n); | ||
// y_points = H(y) ^ {bob_sk} | ||
bob.MaskStrings(absl::MakeSpan(y), absl::MakeSpan(y_points)); | ||
|
||
// ------------------- | ||
// Step 2 | ||
// ------------------- | ||
// | ||
// Alice send x_points to bob, and bob send y_points to alice | ||
// | ||
// ... code here (omitted) ... | ||
// | ||
// You may mannually send the EcPoints through yacl::link::Context, which | ||
// handles an RPC channel, see: yacl/link/context.h. You may also use any | ||
// method that you like to let Alice talk to Bob. Remember the communication | ||
// channel needs to be a secure P2P channel. | ||
// | ||
// Since most of communication methods only accept strings or bytes, you may | ||
// serialize EcPoints by calling ec_->SerializePoint(/* ec points here */). | ||
// see: yacl/ecc/ecc_spi.h for more details. | ||
|
||
// ------------------- | ||
// Step 3 | ||
// ------------------- | ||
std::vector<std::string> y_str(n); | ||
// y_str = y_points ^ {alice_sk} | ||
alice.MaskEcPoints(absl::MakeSpan(y_points), absl::MakeSpan(y_str)); | ||
|
||
std::vector<std::string> x_str(n); | ||
// x_str = x_points ^ {bob_sk} | ||
bob.MaskEcPoints(absl::MakeSpan(x_points), absl::MakeSpan(x_str)); | ||
|
||
/* check results */ | ||
auto compare = GetIntersectionIdx(x, y); // result | ||
auto z = GetIntersectionIdx(x_str, y_str); | ||
|
||
EXPECT_EQ(compare.size(), z.size()); | ||
|
||
for (size_t i = 0; i < z.size(); ++i) { | ||
EXPECT_EQ(compare[i], z[i]); | ||
} | ||
} | ||
|
||
} // namespace examples::psi |
Oops, something went wrong.