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

Add secret share generator adapter. #47

Merged
merged 5 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 0 additions & 29 deletions src/main/cc/any_sketch/crypto/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,6 @@ cc_library(
],
)

cc_library(
name = "secret_share_generator",
srcs = [":secret_share_generator.cc"],
hdrs = [":secret_share_generator.h"],
strip_include_prefix = _INCLUDE_PREFIX,
deps = [
"//src/main/cc/math:open_ssl_uniform_random_generator",
"//src/main/proto/wfa/any_sketch:secret_share_cc_proto",
"@wfa_common_cpp//src/main/cc/common_cpp/macros",
],
)

cc_library(
name = "sketch_encrypter_adapter",
srcs = [":sketch_encrypter_adapter.cc"],
Expand All @@ -55,20 +43,3 @@ cc_binary(
"@com_google_absl//absl/strings",
],
)

cc_library(
name = "shuffle",
srcs = [
"shuffle.cc",
],
hdrs = [
"shuffle.h",
],
strip_include_prefix = _INCLUDE_PREFIX,
deps = [
"//src/main/cc/math:open_ssl_uniform_random_generator",
"//src/main/proto/wfa/any_sketch:secret_share_cc_proto",
"@com_google_absl//absl/status",
"@wfa_common_cpp//src/main/cc/common_cpp/macros",
],
)
20 changes: 20 additions & 0 deletions src/main/cc/crypto/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package(default_visibility = ["//visibility:public"])

_INCLUDE_PREFIX = "/src/main/cc/"

cc_library(
name = "shuffle",
srcs = [
"shuffle.cc",
],
hdrs = [
"shuffle.h",
],
strip_include_prefix = _INCLUDE_PREFIX,
deps = [
"//src/main/cc/math:open_ssl_uniform_random_generator",
"//src/main/proto/wfa/frequency_count:secret_share_cc_proto",
"@com_google_absl//absl/status",
"@wfa_common_cpp//src/main/cc/common_cpp/macros",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "any_sketch/crypto/shuffle.h"
#include "crypto/shuffle.h"

#include <memory>

#include "absl/status/status.h"
#include "common_cpp/macros/macros.h"
#include "math/open_ssl_uniform_random_generator.h"

namespace wfa::measurement::common::crypto {
namespace wfa::crypto {

absl::Status SecureShuffleWithSeed(std::vector<uint32_t>& data,
const any_sketch::PrngSeed& seed) {
const frequency_count::PrngSeed& seed) {
// Does nothing if the input is empty or has size 1.
if (data.size() <= 1) {
return absl::OkStatus();
Expand Down Expand Up @@ -61,4 +61,4 @@ absl::Status SecureShuffleWithSeed(std::vector<uint32_t>& data,
return absl::OkStatus();
}

} // namespace wfa::measurement::common::crypto
} // namespace wfa::crypto
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef SRC_MAIN_CC_ANY_SKETCH_CRYPTO_SHUFFLE_H_
#define SRC_MAIN_CC_ANY_SKETCH_CRYPTO_SHUFFLE_H_
#ifndef SRC_MAIN_CC_CRYPTO_SHUFFLE_H_
#define SRC_MAIN_CC_CRYPTO_SHUFFLE_H_

#include <string>
#include <utility>
#include <vector>

#include "absl/status/status.h"
#include "wfa/any_sketch/secret_share.pb.h"
#include "wfa/frequency_count/secret_share.pb.h"

namespace wfa::measurement::common::crypto {
namespace wfa::crypto {

// Shuffles the vector data using Fisher-Yates approach. Let n be the size of
// data, the Fisher-Yates shuffle is as below.
// For i = 0 to (n-2):
// Draws a random value j in the range [i; n-1]
// Swaps data[i] and data[j]
absl::Status SecureShuffleWithSeed(std::vector<uint32_t>& data,
const any_sketch::PrngSeed& seed);
const frequency_count::PrngSeed& seed);

} // namespace wfa::measurement::common::crypto
} // namespace wfa::crypto

#endif // SRC_MAIN_CC_ANY_SKETCH_CRYPTO_SHUFFLE_H_
#endif // SRC_MAIN_CC_CRYPTO_SHUFFLE_H_
27 changes: 27 additions & 0 deletions src/main/cc/frequency_count/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package(default_visibility = ["//visibility:public"])

_INCLUDE_PREFIX = "/src/main/cc/"

cc_library(
name = "generate_secret_shares",
srcs = [":generate_secret_shares.cc"],
hdrs = [":generate_secret_shares.h"],
strip_include_prefix = _INCLUDE_PREFIX,
deps = [
"//src/main/cc/math:open_ssl_uniform_random_generator",
"//src/main/proto/wfa/frequency_count:secret_share_cc_proto",
"@wfa_common_cpp//src/main/cc/common_cpp/macros",
],
)

cc_library(
name = "secret_share_generator_adapter",
srcs = [":secret_share_generator_adapter.cc"],
hdrs = [":secret_share_generator_adapter.h"],
strip_include_prefix = _INCLUDE_PREFIX,
deps = [
":generate_secret_shares",
"//src/main/proto/wfa/frequency_count:secret_share_cc_proto",
"//src/main/proto/wfa/frequency_count:secret_share_methods_cc_proto",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "any_sketch/crypto/secret_share_generator.h"
#include "frequency_count/generate_secret_shares.h"

#include <memory>
#include <string>
Expand All @@ -22,7 +22,7 @@
#include "common_cpp/macros/macros.h"
#include "math/open_ssl_uniform_random_generator.h"

namespace wfa::any_sketch::crypto {
namespace wfa::frequency_count {

using wfa::math::kBytesPerAes256Iv;
using wfa::math::kBytesPerAes256Key;
Expand Down Expand Up @@ -101,4 +101,4 @@ absl::StatusOr<SecretShare> GenerateSecretShares(
return secret_share;
}

} // namespace wfa::any_sketch::crypto
} // namespace wfa::frequency_count
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef SRC_MAIN_CC_ANY_SKETCH_CRYPTO_SECRET_SHARE_GENERATOR_H_
#define SRC_MAIN_CC_ANY_SKETCH_CRYPTO_SECRET_SHARE_GENERATOR_H_
#ifndef SRC_MAIN_CC_FREQUENCY_COUNT_GENERATE_SECRET_SHARES_H_
#define SRC_MAIN_CC_FREQUENCY_COUNT_GENERATE_SECRET_SHARES_H_

#include <vector>

#include "absl/status/statusor.h"
#include "wfa/any_sketch/secret_share.pb.h"
#include "wfa/frequency_count/secret_share.pb.h"

using wfa::any_sketch::SecretShare;
using wfa::any_sketch::SecretShareParameter;
using wfa::frequency_count::SecretShare;
using wfa::frequency_count::SecretShareParameter;

namespace wfa::any_sketch::crypto {
namespace wfa::frequency_count {

absl::StatusOr<SecretShare> GenerateSecretShares(
const SecretShareParameter& secret_share_parameter,
const absl::Span<const uint32_t> input);

} // namespace wfa::any_sketch::crypto
} // namespace wfa::frequency_count

#endif // SRC_MAIN_CC_ANY_SKETCH_CRYPTO_SECRET_SHARE_GENERATOR_H_
#endif // SRC_MAIN_CC_FREQUENCY_COUNT_GENERATE_SECRET_SHARES_H_
47 changes: 47 additions & 0 deletions src/main/cc/frequency_count/secret_share_generator_adapter.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2024 The Cross-Media Measurement Authors
//
// 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 "frequency_count/secret_share_generator_adapter.h"

#include <string>
#include <vector>

#include "absl/memory/memory.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "common_cpp/macros/macros.h"
#include "frequency_count/generate_secret_shares.h"
#include "wfa/frequency_count/secret_share.pb.h"
#include "wfa/frequency_count/secret_share_methods.pb.h"

namespace wfa::frequency_count {

absl::StatusOr<std::string> SecretShareFrequencyVector(
const std::string& serialized_request) {
SecretShareGeneratorRequest request_proto;
if (!request_proto.ParseFromString(serialized_request)) {
return absl::InvalidArgumentError(
"failed to parse the SecretShareGeneratorRequest proto.");
}
std::vector<uint32_t> frequency_vector = std::vector<uint32_t>(
request_proto.data().begin(), request_proto.data().end());
SecretShareParameter secret_share_param;
secret_share_param.set_modulus(request_proto.ring_modulus());

ASSIGN_OR_RETURN(auto secret_share,
GenerateSecretShares(secret_share_param, frequency_vector));
return secret_share.SerializeAsString();
}

} // namespace wfa::frequency_count
35 changes: 35 additions & 0 deletions src/main/cc/frequency_count/secret_share_generator_adapter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2024 The Cross-Media Measurement Authors
//
// 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.

#ifndef SRC_MAIN_CC_FREQUENCY_COUNT_SECRET_SHARE_GENERATOR_ADAPTER_H_
#define SRC_MAIN_CC_FREQUENCY_COUNT_SECRET_SHARE_GENERATOR_ADAPTER_H_

#include <string>

#include "absl/status/statusor.h"

// Wrapper methods used to generate the swig/JNI Java classes.
// The only functionality of these methods are converting between proto messages
// and their corresponding serialized strings, and then calling the function
// GenerateSecretShares to secret share a frequency vector.
// Note: this method shouldn't be used in any c++ binary, use
// GenerateSecretShares directly.
namespace wfa::frequency_count {

absl::StatusOr<std::string> SecretShareFrequencyVector(
const std::string& serialized_request);

} // namespace wfa::frequency_count

#endif // SRC_MAIN_CC_FREQUENCY_COUNT_SECRET_SHARE_GENERATOR_ADAPTER_H_
2 changes: 1 addition & 1 deletion src/main/cc/math/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ cc_library(
strip_include_prefix = _INCLUDE_PREFIX,
deps = [
":uniform_pseudorandom_generator",
"//src/main/proto/wfa/any_sketch:secret_share_cc_proto",
"//src/main/proto/wfa/frequency_count:secret_share_cc_proto",
"@boringssl//:ssl",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
Expand Down
4 changes: 2 additions & 2 deletions src/main/cc/math/open_ssl_uniform_random_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
#include "absl/status/statusor.h"
#include "absl/strings/substitute.h"
#include "math/uniform_pseudorandom_generator.h"
#include "wfa/any_sketch/secret_share.pb.h"
#include "wfa/frequency_count/secret_share.pb.h"

namespace wfa::math {

using any_sketch::PrngSeed;
using frequency_count::PrngSeed;

// Key length for EVP_aes_256_ctr.
// See https://www.openssl.org/docs/man1.1.1/man3/EVP_aes_256_ctr.html
Expand Down
22 changes: 0 additions & 22 deletions src/main/proto/wfa/any_sketch/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,3 @@ cc_proto_library(
name = "differential_privacy_cc_proto",
deps = [":differential_privacy_proto"],
)

proto_library(
name = "secret_share_proto",
srcs = ["secret_share.proto"],
strip_import_prefix = IMPORT_PREFIX,
)

cc_proto_library(
name = "secret_share_cc_proto",
deps = [":secret_share_proto"],
)

proto_library(
name = "share_shuffle_sketch_proto",
srcs = ["share_shuffle_sketch.proto"],
strip_import_prefix = IMPORT_PREFIX,
)

cc_proto_library(
name = "share_shuffle_sketch_cc_proto",
deps = [":share_shuffle_sketch_proto"],
)
42 changes: 42 additions & 0 deletions src/main/proto/wfa/frequency_count/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
load("@rules_cc//cc:defs.bzl", "cc_proto_library")
load("@rules_proto//proto:defs.bzl", "proto_library")

package(default_visibility = ["//visibility:public"])

IMPORT_PREFIX = "/src/main/proto"

proto_library(
name = "secret_share_proto",
srcs = ["secret_share.proto"],
strip_import_prefix = IMPORT_PREFIX,
)

cc_proto_library(
name = "secret_share_cc_proto",
deps = [":secret_share_proto"],
)

proto_library(
name = "frequency_vector_proto",
srcs = ["frequency_vector.proto"],
strip_import_prefix = IMPORT_PREFIX,
)

cc_proto_library(
name = "frequency_vector_cc_proto",
deps = [":frequency_vector_proto"],
)

proto_library(
name = "secret_share_methods_proto",
srcs = ["secret_share_methods.proto"],
strip_import_prefix = IMPORT_PREFIX,
deps = [
":secret_share_proto",
],
)

cc_proto_library(
name = "secret_share_methods_cc_proto",
deps = [":secret_share_methods_proto"],
)
Loading
Loading