diff --git a/src/main/cc/any_sketch/crypto/BUILD.bazel b/src/main/cc/any_sketch/crypto/BUILD.bazel index 90813e4..99476b8 100644 --- a/src/main/cc/any_sketch/crypto/BUILD.bazel +++ b/src/main/cc/any_sketch/crypto/BUILD.bazel @@ -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"], @@ -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", - ], -) diff --git a/src/main/cc/crypto/BUILD.bazel b/src/main/cc/crypto/BUILD.bazel new file mode 100644 index 0000000..3a9648a --- /dev/null +++ b/src/main/cc/crypto/BUILD.bazel @@ -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", + ], +) diff --git a/src/main/cc/any_sketch/crypto/shuffle.cc b/src/main/cc/crypto/shuffle.cc similarity index 92% rename from src/main/cc/any_sketch/crypto/shuffle.cc rename to src/main/cc/crypto/shuffle.cc index 986081c..852f113 100644 --- a/src/main/cc/any_sketch/crypto/shuffle.cc +++ b/src/main/cc/crypto/shuffle.cc @@ -12,7 +12,7 @@ // 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 @@ -20,10 +20,10 @@ #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& 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(); @@ -61,4 +61,4 @@ absl::Status SecureShuffleWithSeed(std::vector& data, return absl::OkStatus(); } -} // namespace wfa::measurement::common::crypto +} // namespace wfa::crypto diff --git a/src/main/cc/any_sketch/crypto/shuffle.h b/src/main/cc/crypto/shuffle.h similarity index 73% rename from src/main/cc/any_sketch/crypto/shuffle.h rename to src/main/cc/crypto/shuffle.h index d447831..f7c40ea 100644 --- a/src/main/cc/any_sketch/crypto/shuffle.h +++ b/src/main/cc/crypto/shuffle.h @@ -12,17 +12,17 @@ // 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 #include #include #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. @@ -30,8 +30,8 @@ namespace wfa::measurement::common::crypto { // Draws a random value j in the range [i; n-1] // Swaps data[i] and data[j] absl::Status SecureShuffleWithSeed(std::vector& 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_ diff --git a/src/main/cc/frequency_count/BUILD.bazel b/src/main/cc/frequency_count/BUILD.bazel new file mode 100644 index 0000000..5a2347c --- /dev/null +++ b/src/main/cc/frequency_count/BUILD.bazel @@ -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", + ], +) diff --git a/src/main/cc/any_sketch/crypto/secret_share_generator.cc b/src/main/cc/frequency_count/generate_secret_shares.cc similarity index 96% rename from src/main/cc/any_sketch/crypto/secret_share_generator.cc rename to src/main/cc/frequency_count/generate_secret_shares.cc index 2a39082..a2c2c4f 100644 --- a/src/main/cc/any_sketch/crypto/secret_share_generator.cc +++ b/src/main/cc/frequency_count/generate_secret_shares.cc @@ -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 #include @@ -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; @@ -101,4 +101,4 @@ absl::StatusOr GenerateSecretShares( return secret_share; } -} // namespace wfa::any_sketch::crypto +} // namespace wfa::frequency_count diff --git a/src/main/cc/any_sketch/crypto/secret_share_generator.h b/src/main/cc/frequency_count/generate_secret_shares.h similarity index 67% rename from src/main/cc/any_sketch/crypto/secret_share_generator.h rename to src/main/cc/frequency_count/generate_secret_shares.h index d0a4377..3448603 100644 --- a/src/main/cc/any_sketch/crypto/secret_share_generator.h +++ b/src/main/cc/frequency_count/generate_secret_shares.h @@ -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 #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 GenerateSecretShares( const SecretShareParameter& secret_share_parameter, const absl::Span 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_ diff --git a/src/main/cc/frequency_count/secret_share_generator_adapter.cc b/src/main/cc/frequency_count/secret_share_generator_adapter.cc new file mode 100644 index 0000000..146fe7d --- /dev/null +++ b/src/main/cc/frequency_count/secret_share_generator_adapter.cc @@ -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 +#include + +#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 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 frequency_vector = std::vector( + 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 diff --git a/src/main/cc/frequency_count/secret_share_generator_adapter.h b/src/main/cc/frequency_count/secret_share_generator_adapter.h new file mode 100644 index 0000000..2838689 --- /dev/null +++ b/src/main/cc/frequency_count/secret_share_generator_adapter.h @@ -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 + +#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 SecretShareFrequencyVector( + const std::string& serialized_request); + +} // namespace wfa::frequency_count + +#endif // SRC_MAIN_CC_FREQUENCY_COUNT_SECRET_SHARE_GENERATOR_ADAPTER_H_ diff --git a/src/main/cc/math/BUILD.bazel b/src/main/cc/math/BUILD.bazel index 2baf6e3..81d7eb5 100644 --- a/src/main/cc/math/BUILD.bazel +++ b/src/main/cc/math/BUILD.bazel @@ -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", diff --git a/src/main/cc/math/open_ssl_uniform_random_generator.h b/src/main/cc/math/open_ssl_uniform_random_generator.h index 84a914d..8d453f7 100644 --- a/src/main/cc/math/open_ssl_uniform_random_generator.h +++ b/src/main/cc/math/open_ssl_uniform_random_generator.h @@ -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 diff --git a/src/main/proto/wfa/any_sketch/BUILD.bazel b/src/main/proto/wfa/any_sketch/BUILD.bazel index 4591a27..25aa93f 100644 --- a/src/main/proto/wfa/any_sketch/BUILD.bazel +++ b/src/main/proto/wfa/any_sketch/BUILD.bazel @@ -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"], -) diff --git a/src/main/proto/wfa/frequency_count/BUILD.bazel b/src/main/proto/wfa/frequency_count/BUILD.bazel new file mode 100644 index 0000000..acb74c9 --- /dev/null +++ b/src/main/proto/wfa/frequency_count/BUILD.bazel @@ -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"], +) diff --git a/src/main/proto/wfa/any_sketch/share_shuffle_sketch.proto b/src/main/proto/wfa/frequency_count/frequency_vector.proto similarity index 81% rename from src/main/proto/wfa/any_sketch/share_shuffle_sketch.proto rename to src/main/proto/wfa/frequency_count/frequency_vector.proto index 503647e..07c6c23 100644 --- a/src/main/proto/wfa/any_sketch/share_shuffle_sketch.proto +++ b/src/main/proto/wfa/frequency_count/frequency_vector.proto @@ -18,14 +18,13 @@ syntax = "proto3"; -package wfa.any_sketch; +package wfa.frequency_count; -option java_package = "org.wfanet.anysketch"; +option java_package = "org.wfanet.frequencycount"; option java_multiple_files = true; -option java_outer_classname = "ShareShuffleSketchProto"; +option java_outer_classname = "FrequencyVectorProto"; -// The data share of HonestMajorityShareShuffle protocol. -message ShareShuffleSketch { +message FrequencyVector { // A vector of count where each count corresponds to the frequency of a single // VID. repeated uint32 data = 1; diff --git a/src/main/proto/wfa/any_sketch/secret_share.proto b/src/main/proto/wfa/frequency_count/secret_share.proto similarity index 93% rename from src/main/proto/wfa/any_sketch/secret_share.proto rename to src/main/proto/wfa/frequency_count/secret_share.proto index 609792a..38d6799 100644 --- a/src/main/proto/wfa/any_sketch/secret_share.proto +++ b/src/main/proto/wfa/frequency_count/secret_share.proto @@ -18,9 +18,9 @@ syntax = "proto3"; -package wfa.any_sketch; +package wfa.frequency_count; -option java_package = "org.wfanet.anysketch"; +option java_package = "org.wfanet.frequencycount"; option java_multiple_files = true; option java_outer_classname = "SecretShareProto"; diff --git a/src/main/proto/wfa/frequency_count/secret_share_methods.proto b/src/main/proto/wfa/frequency_count/secret_share_methods.proto new file mode 100644 index 0000000..1f27433 --- /dev/null +++ b/src/main/proto/wfa/frequency_count/secret_share_methods.proto @@ -0,0 +1,29 @@ +// 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. + +syntax = "proto3"; + +package wfa.frequency_count; + +import "wfa/frequency_count/secret_share.proto"; + +option java_package = "org.wfanet.frequencycount"; +option java_multiple_files = true; + +// The request to secret share a frequency vector. +message SecretShareGeneratorRequest { + uint32 ring_modulus = 1; + // The input frequency vector + repeated uint32 data = 2; +} diff --git a/src/test/cc/any_sketch/crypto/BUILD.bazel b/src/test/cc/any_sketch/crypto/BUILD.bazel index 15838bf..5544abc 100644 --- a/src/test/cc/any_sketch/crypto/BUILD.bazel +++ b/src/test/cc/any_sketch/crypto/BUILD.bazel @@ -28,33 +28,3 @@ cc_test( "@wfa_common_cpp//src/main/cc/common_cpp/testing:status", ], ) - -cc_test( - name = "secret_share_generator_test", - size = "small", - srcs = [ - ":secret_share_generator_test.cc", - ], - deps = [ - "//src/main/cc/any_sketch/crypto:secret_share_generator", - "//src/main/cc/math:open_ssl_uniform_random_generator", - "//src/main/proto/wfa/any_sketch:secret_share_cc_proto", - "@com_google_googletest//:gtest", - "@com_google_googletest//:gtest_main", - "@wfa_common_cpp//src/main/cc/common_cpp/testing:status", - ], -) - -cc_test( - name = "shuffle_test", - size = "small", - srcs = [ - "shuffle_test.cc", - ], - deps = [ - "//src/main/cc/any_sketch/crypto:shuffle", - "@com_google_googletest//:gtest", - "@com_google_googletest//:gtest_main", - "@wfa_common_cpp//src/main/cc/common_cpp/testing:status", - ], -) diff --git a/src/test/cc/crypto/BUILD.bazel b/src/test/cc/crypto/BUILD.bazel new file mode 100644 index 0000000..3d2cd9c --- /dev/null +++ b/src/test/cc/crypto/BUILD.bazel @@ -0,0 +1,13 @@ +cc_test( + name = "shuffle_test", + size = "small", + srcs = [ + "shuffle_test.cc", + ], + deps = [ + "//src/main/cc/crypto:shuffle", + "@com_google_googletest//:gtest", + "@com_google_googletest//:gtest_main", + "@wfa_common_cpp//src/main/cc/common_cpp/testing:status", + ], +) diff --git a/src/test/cc/any_sketch/crypto/shuffle_test.cc b/src/test/cc/crypto/shuffle_test.cc similarity index 95% rename from src/test/cc/any_sketch/crypto/shuffle_test.cc rename to src/test/cc/crypto/shuffle_test.cc index d2a4d1e..8790f9c 100644 --- a/src/test/cc/any_sketch/crypto/shuffle_test.cc +++ b/src/test/cc/crypto/shuffle_test.cc @@ -12,7 +12,7 @@ // 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 "absl/status/status.h" #include "absl/status/statusor.h" @@ -21,12 +21,12 @@ #include "gtest/gtest.h" #include "math/open_ssl_uniform_random_generator.h" -namespace wfa::any_sketch::crypto { +namespace wfa::crypto { namespace { -using any_sketch::PrngSeed; -using measurement::common::crypto::SecureShuffleWithSeed; +using frequency_count::PrngSeed; using ::wfa::StatusIs; +using ::wfa::crypto::SecureShuffleWithSeed; using ::wfa::math::kBytesPerAes256Iv; using ::wfa::math::kBytesPerAes256Key; @@ -117,4 +117,4 @@ TEST(SecureShuffleWithSeed, ShufflingWithSameSeedSucceeds) { } } // namespace -} // namespace wfa::any_sketch::crypto +} // namespace wfa::crypto diff --git a/src/test/cc/frequency_count/BUILD.bazel b/src/test/cc/frequency_count/BUILD.bazel new file mode 100644 index 0000000..63fe9ab --- /dev/null +++ b/src/test/cc/frequency_count/BUILD.bazel @@ -0,0 +1,30 @@ +cc_test( + name = "generate_secret_shares_test", + size = "small", + srcs = [ + ":generate_secret_shares_test.cc", + ], + deps = [ + "//src/main/cc/frequency_count:generate_secret_shares", + "//src/main/cc/math:open_ssl_uniform_random_generator", + "//src/main/proto/wfa/frequency_count:secret_share_cc_proto", + "@com_google_googletest//:gtest", + "@com_google_googletest//:gtest_main", + "@wfa_common_cpp//src/main/cc/common_cpp/testing:status", + ], +) + +cc_test( + name = "secret_share_generator_adapter_test", + size = "small", + srcs = [":secret_share_generator_adapter_test.cc"], + deps = [ + "//src/main/cc/frequency_count:secret_share_generator_adapter", + "//src/main/cc/math:open_ssl_uniform_random_generator", + "//src/main/proto/wfa/frequency_count:secret_share_cc_proto", + "//src/main/proto/wfa/frequency_count:secret_share_methods_cc_proto", + "@com_google_googletest//:gtest", + "@com_google_googletest//:gtest_main", + "@wfa_common_cpp//src/main/cc/common_cpp/testing:status", + ], +) diff --git a/src/test/cc/any_sketch/crypto/secret_share_generator_test.cc b/src/test/cc/frequency_count/generate_secret_shares_test.cc similarity index 97% rename from src/test/cc/any_sketch/crypto/secret_share_generator_test.cc rename to src/test/cc/frequency_count/generate_secret_shares_test.cc index d7983f1..a879a32 100644 --- a/src/test/cc/any_sketch/crypto/secret_share_generator_test.cc +++ b/src/test/cc/frequency_count/generate_secret_shares_test.cc @@ -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 "absl/status/status.h" #include "absl/status/statusor.h" @@ -21,7 +21,7 @@ #include "gtest/gtest.h" #include "math/open_ssl_uniform_random_generator.h" -namespace wfa::any_sketch::crypto { +namespace wfa::frequency_count { namespace { using math::OpenSslUniformPseudorandomGenerator; @@ -122,4 +122,4 @@ TEST(AdditiveSecretSharing, InputOutOfBoundFails) { } } // namespace -} // namespace wfa::any_sketch::crypto +} // namespace wfa::frequency_count diff --git a/src/test/cc/frequency_count/secret_share_generator_adapter_test.cc b/src/test/cc/frequency_count/secret_share_generator_adapter_test.cc new file mode 100644 index 0000000..5fb85f5 --- /dev/null +++ b/src/test/cc/frequency_count/secret_share_generator_adapter_test.cc @@ -0,0 +1,94 @@ +// 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 "absl/status/status.h" +#include "absl/status/statusor.h" +#include "common_cpp/testing/status_macros.h" +#include "common_cpp/testing/status_matchers.h" +#include "frequency_count/generate_secret_shares.h" +#include "gtest/gtest.h" +#include "math/open_ssl_uniform_random_generator.h" +#include "wfa/frequency_count/secret_share_methods.pb.h" + +namespace wfa::frequency_count { +namespace { + +using math::OpenSslUniformPseudorandomGenerator; +using math::UniformPseudorandomGenerator; + +constexpr int kRingModulus = 128; + +TEST(SecretShareAdapterTest, EmptyFrequencyVectorFails) { + // Build the request + SecretShareGeneratorRequest request; + request.set_ring_modulus(kRingModulus); + std::vector frequency_vector(0); + + EXPECT_THAT(SecretShareFrequencyVector(request.SerializeAsString()).status(), + StatusIs(absl::StatusCode::kInvalidArgument, "Input")); +} + +TEST(SecretShareAdapterTest, InvalidRingModulusFails) { + // Build the request + SecretShareGeneratorRequest request; + request.set_ring_modulus(1); + std::vector frequency_vector = {1, 2, 3, 4, 5}; + request.mutable_data()->Add(frequency_vector.begin(), frequency_vector.end()); + + EXPECT_THAT(SecretShareFrequencyVector(request.SerializeAsString()).status(), + StatusIs(absl::StatusCode::kInvalidArgument, "modulus")); +} + +TEST(SecretShareAdapterTest, SecretShareGenerationSucceeds) { + // Build the request + SecretShareGeneratorRequest request; + request.set_ring_modulus(kRingModulus); + std::vector frequency_vector = {1, 2, 3, 4, 5}; + request.mutable_data()->Add(frequency_vector.begin(), frequency_vector.end()); + + ASSERT_OK_AND_ASSIGN(std::string response, + SecretShareFrequencyVector(request.SerializeAsString())); + + SecretShare secret_share; + secret_share.ParseFromString(response); + + std::string key = secret_share.share_seed().key(); + std::string iv = secret_share.share_seed().iv(); + + std::vector key_vec, iv_vec; + key_vec.insert(key_vec.end(), key.begin(), key.end()); + iv_vec.insert(iv_vec.end(), iv.begin(), iv.end()); + + ASSERT_OK_AND_ASSIGN( + std::unique_ptr prng, + OpenSslUniformPseudorandomGenerator::Create(key_vec, iv_vec)); + + ASSERT_OK_AND_ASSIGN( + std::vector share_vector_from_seed, + prng->GenerateUniformRandomRange(frequency_vector.size(), kRingModulus)); + + ASSERT_EQ(secret_share.share_vector().size(), frequency_vector.size()); + ASSERT_EQ(share_vector_from_seed.size(), frequency_vector.size()); + + for (int i = 0; i < frequency_vector.size(); i++) { + ASSERT_EQ(frequency_vector[i], + (share_vector_from_seed[i] + secret_share.share_vector(i)) % + kRingModulus); + } +} + +} // namespace +} // namespace wfa::frequency_count