Skip to content

Commit

Permalink
C++ change return types of NumericalMechanism::AddNoise
Browse files Browse the repository at this point in the history
We are changing the return type of NumericalMechanism::AddNoise from
T to double or int64_t.  This reduces confusion when T is an unsigned
type.  This is *not* a breaking change, since there is an implicit
conversion and when using T, which was in the library, and is now in
control of the user.

Additionally, this commit contains minor clean-ups and an absl
dependency update.

PiperOrigin-RevId: 634249123
Change-Id: I8423326fb6056e944fbc4d9ceae2c8918edcfedc
GitOrigin-RevId: b9193e7d7621855b41d1b430ec01d4534b73f407
  • Loading branch information
Differential Privacy Team authored and monsieurmuffin committed Jun 5, 2024
1 parent 0b109e9 commit 6bc750c
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 12 deletions.
2 changes: 2 additions & 0 deletions cc/algorithms/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ cc_library(
deps = [
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_cc_differential_privacy//base:status_macros",
"@com_google_differential_privacy//proto:summary_cc_proto",
],
Expand All @@ -36,6 +37,7 @@ cc_test(
":count-tree",
"//base/testing:proto_matchers",
"//base/testing:status_matchers",
"@com_google_absl//absl/status",
"@com_google_googletest//:gtest_main",
],
)
Expand Down
5 changes: 4 additions & 1 deletion cc/algorithms/internal/count-tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

#include "algorithms/internal/count-tree.h"

#include <unordered_map>
#include <cmath>
#include <cstdint>
#include <utility>

#include "absl/status/status.h"
#include "absl/strings/str_cat.h"
#include "proto/summary.pb.h"
#include "base/status_macros.h"

Expand Down
1 change: 1 addition & 0 deletions cc/algorithms/internal/count-tree_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "base/testing/status_matchers.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/status/status.h"

namespace differential_privacy {
namespace internal {
Expand Down
4 changes: 2 additions & 2 deletions cc/algorithms/numerical-mechanisms.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ class NumericalMechanism {
virtual ~NumericalMechanism() = default;

template <typename T, std::enable_if_t<std::is_integral<T>::value>* = nullptr>
T AddNoise(T result) {
int64_t AddNoise(T result) {
return AddInt64Noise(result);
}

template <typename T,
std::enable_if_t<std::is_floating_point<T>::value>* = nullptr>
T AddNoise(T result) {
double AddNoise(T result) {
return AddDoubleNoise(result);
}

Expand Down
23 changes: 23 additions & 0 deletions cc/algorithms/numerical-mechanisms_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
#include "algorithms/numerical-mechanisms.h"

#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <memory>
#include <vector>

#include "base/testing/status_matchers.h"
#include "gmock/gmock.h"
Expand All @@ -29,11 +32,13 @@ namespace differential_privacy {
namespace {

using ::testing::_;
using ::testing::Contains;
using ::testing::DoubleNear;
using ::testing::Eq;
using ::testing::Ge;
using ::testing::HasSubstr;
using ::testing::IsNull;
using ::testing::Lt;
using ::testing::MatchesRegex;
using ::testing::Not;
using ::testing::Return;
Expand Down Expand Up @@ -1284,5 +1289,23 @@ TEST(NumericalMechanismTest, MinVarianceMechanismBuilderFailsWithoutEpsilon) {
EXPECT_THAT(fails.status().message(), HasSubstr("Epsilon must be set"));
}

TEST(NumericalMechanismTest, AddNoiseReturnsNegativeValuesForUnsignedInt) {
// Flakiness of this test is approximately 1 / 2**50 ~= 8e-16
absl::StatusOr<std::unique_ptr<NumericalMechanism>> mechanism =
LaplaceMechanism::Builder()
.SetEpsilon(0.001) // low epsilon for more variance
.SetL0Sensitivity(20)
.SetLInfSensitivity(100)
.Build();
ASSERT_OK(mechanism);

std::vector<int64_t> results;
for (int i = 0; i < 50; ++i) {
results.push_back(mechanism.value()->AddNoise<uint8_t>(0));
}

EXPECT_THAT(results, Contains(Lt(0)));
}

} // namespace
} // namespace differential_privacy
6 changes: 3 additions & 3 deletions cc/cc_differential_privacy_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def cc_differential_privacy_deps():
# Abseil
http_archive(
name = "com_google_absl",
url = "https://github.com/abseil/abseil-cpp/archive/refs/tags/20230802.1.tar.gz",
sha256 = "987ce98f02eefbaf930d6e38ab16aa05737234d7afbab2d5c4ea7adbe50c28ed",
strip_prefix = "abseil-cpp-20230802.1",
url = "https://github.com/abseil/abseil-cpp/archive/refs/tags/20240116.2.tar.gz",
sha256 = "733726b8c3a6d39a4120d7e45ea8b41a434cdacde401cba500f14236c49b39dc",
strip_prefix = "abseil-cpp-20240116.2",
)

# Common bazel rules. Also required for Abseil.
Expand Down
5 changes: 3 additions & 2 deletions examples/zetasql/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "com_google_zetasql",
strip_prefix = "zetasql-2023.11.1",
url = "https://github.com/google/zetasql/archive/refs/tags/2023.11.1.tar.gz",
sha256 = "58510f44dc815648039fd4e45fc40da3d9f0a746cf483e6bbc5171e984eb7e79",
strip_prefix = "zetasql-2024.03.1",
url = "https://github.com/google/zetasql/archive/refs/tags/2024.03.1.tar.gz",
)

# Transitive dependencies from the ZetaSQL WORKSPACE file:
Expand Down
8 changes: 4 additions & 4 deletions python/dp_auditorium/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ guarantees as described in the introductory paper
The root directory is structured as follows. It contains four main folders and a
runner module described below:

* [Testers](testers/): Contains modules introducing `PropertyTesters` that
* [Testers](dp_auditorium/testers/): Contains modules introducing `PropertyTesters` that
check if there is evidence to reject the hypothesis that a privacy guarantee
holds on a fixed pair of datasets.
* [Generators](generators/): Contains modules introducing `DatasetGenerators`
* [Generators](dp_auditorium/generators/): Contains modules introducing `DatasetGenerators`
that output pairs of neighboring datasets, for example under the add/remove
neighboring relation.
* [Mechanisms](mechanims/): Contains examples of private and non-private
* [Mechanisms](dp_auditorium/mechanisms/): Contains examples of private and non-private
mechanisms used to exemplify testers.
* `privacy_test_runner.py`: Module with runner that specializes to a tester
and generator and instantiates the `PropertyTester` on several datasets'
trials generated by the `DatasetGenerator`.
* [Examples](examples/): Folder containing examples on how to combine the
* [Examples](dp_auditorium/examples/): Folder containing examples on how to combine the
above tools.

Details on the signature of the above objects, can be found in the
Expand Down

0 comments on commit 6bc750c

Please sign in to comment.