Skip to content

Commit

Permalink
Cleanup and documentation changes
Browse files Browse the repository at this point in the history
C++:
* cleanup code, refine comments
* update absl dependency

Java:
* Load java rules from @rules_java

Change-Id: I3141a3389af98ba221c13c230b36e9afb2420187
GitOrigin-RevId: cb0c561fbd518ac22686e23e7833ddf2c75e9468
  • Loading branch information
Differential Privacy Team authored and dibakch committed Oct 23, 2024
1 parent 6f7deb3 commit 6ec24b2
Show file tree
Hide file tree
Showing 36 changed files with 180 additions and 140 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ the contents of a dataset and/or its order. Our integer implementations are not
subject to the vulnerabilities described in the paper (though note that Java
does not have an integer implementation).

Please refer to our [attack model](common_docs/attack_model.md) to learn more
about how to use our libraries in a safe way.

## Support

We will continue to publish updates and improvements to the library. We are
Expand Down
5 changes: 3 additions & 2 deletions cc/accounting/accountant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "accounting/accountant.h"

#include <cmath>
#include <optional>

#include "absl/status/status.h"
#include "absl/status/statusor.h"
Expand All @@ -28,14 +29,14 @@ namespace accounting {
absl::StatusOr<double> GetSmallestParameter(EpsilonDelta epsilon_delta,
int num_queries, double sensitivity,
NoiseFunction noise_function,
absl::optional<double> upper_bound,
std::optional<double> upper_bound,
double tolerance) {
BinarySearchParameters search_parameters = {
.lower_bound = 0,
.upper_bound = upper_bound.has_value() ? upper_bound.value()
: 2 * num_queries * sensitivity /
epsilon_delta.epsilon,
.initial_guess = absl::nullopt,
.initial_guess = std::nullopt,
.tolerance = tolerance};

auto compute_delta = [noise_function, sensitivity, num_queries,
Expand Down
2 changes: 1 addition & 1 deletion cc/accounting/accountant.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ using NoiseFunction = std::function<std::unique_ptr<AdditiveNoisePrivacyLoss>(
absl::StatusOr<double> GetSmallestParameter(EpsilonDelta epsilon_delta,
int num_queries, double sensitivity,
NoiseFunction noise_function,
absl::optional<double> upper_bound,
std::optional<double> upper_bound,
double tolerance = 1e-4);

// Uses the optimal advanced composition theorem, Theorem 3.3 from the paper
Expand Down
4 changes: 3 additions & 1 deletion cc/accounting/accountant_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "accounting/accountant.h"

#include <optional>

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/status/status.h"
Expand Down Expand Up @@ -74,7 +76,7 @@ TEST_P(AccountantTest, Basic) {

auto result = GetSmallestParameter(param.epsilon_delta, param.num_queries,
param.sensitivity, param.noise_function,
absl::optional<double>());
std::optional<double>());
EXPECT_OK(result.status());
EXPECT_NEAR(result.value(), param.expected_parameter, kMaxError);
}
Expand Down
2 changes: 1 addition & 1 deletion cc/accounting/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct BinarySearchParameters {
// An initial guess to start the search with. Must be positive. When this
// guess is close to the true value, it can help make the binary search
// faster.
absl::optional<double> initial_guess;
std::optional<double> initial_guess;
// An acceptable error on the returned value.
double tolerance = 1e-7;
// Whether the search is over integers.
Expand Down
39 changes: 22 additions & 17 deletions cc/accounting/common/common_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

#include "accounting/common/common.h"

#include <cmath>
#include <optional>

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/status/status.h"
Expand Down Expand Up @@ -56,7 +59,7 @@ struct InverseMonotoneFunctionParam {
double value;
double lower_x;
double upper_x;
absl::optional<double> initial_guess;
std::optional<double> initial_guess;
bool increasing;
double expected_x;
bool discrete = false;
Expand All @@ -73,7 +76,7 @@ INSTANTIATE_TEST_SUITE_P(
.value = -4.5,
.lower_x = 0,
.upper_x = 10,
.initial_guess = absl::nullopt,
.initial_guess = std::nullopt,
.increasing = false,
.expected_x = 4.5},
// with initial guess
Expand All @@ -94,27 +97,29 @@ INSTANTIATE_TEST_SUITE_P(
.increasing = false,
.expected_x = 5},
// increasing, without initial guess
InverseMonotoneFunctionParam{.func = [](double x) { return pow(x, 2); },
.value = 25,
.lower_x = 0,
.upper_x = 10,
.initial_guess = absl::nullopt,
.increasing = true,
.expected_x = 5},
InverseMonotoneFunctionParam{
.func = [](double x) { return std::pow(x, 2); },
.value = 25,
.lower_x = 0,
.upper_x = 10,
.initial_guess = std::nullopt,
.increasing = true,
.expected_x = 5},
// increasing, with initial guess
InverseMonotoneFunctionParam{.func = [](double x) { return pow(x, 2); },
.value = 25,
.lower_x = 0,
.upper_x = 10,
.initial_guess = 2,
.increasing = true,
.expected_x = 5},
InverseMonotoneFunctionParam{
.func = [](double x) { return std::pow(x, 2); },
.value = 25,
.lower_x = 0,
.upper_x = 10,
.initial_guess = 2,
.increasing = true,
.expected_x = 5},
// discrete
InverseMonotoneFunctionParam{.func = [](double x) { return -x; },
.value = -4.5,
.lower_x = 0,
.upper_x = 10,
.initial_guess = absl::nullopt,
.initial_guess = std::nullopt,
.increasing = false,
.expected_x = 5,
.discrete = true}));
Expand Down
3 changes: 2 additions & 1 deletion cc/accounting/convolution.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <cmath>
#include <complex>
#include <optional>
#include <ostream>
#include <vector>

Expand Down Expand Up @@ -121,7 +122,7 @@ ProbabilityMassFunction Convolve(const ProbabilityMassFunction& x,

ConvolutionTruncationBounds ComputeConvolutionTruncationBounds(
const UnpackedProbabilityMassFunction& x, int num_times,
double tail_mass_truncation, absl::optional<std::vector<double>> orders) {
double tail_mass_truncation, std::optional<std::vector<double>> orders) {
const int size = x.items.size();
int64_t upper_bound = static_cast<int64_t>(size - 1) * num_times;
int64_t lower_bound = 0;
Expand Down
2 changes: 1 addition & 1 deletion cc/accounting/convolution.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ struct ConvolutionTruncationBounds {
ConvolutionTruncationBounds ComputeConvolutionTruncationBounds(
const UnpackedProbabilityMassFunction& x, int num_times,
double tail_mass_truncation = 0,
absl::optional<std::vector<double>> orders = {});
std::optional<std::vector<double>> orders = {});

// Returns convolution of probability mass function with itself num_times.
// Additional parameter:
Expand Down
3 changes: 2 additions & 1 deletion cc/accounting/privacy_loss_distribution.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include <algorithm>
#include <cmath>
#include <optional>
#include <vector>

#include "absl/status/status.h"
Expand Down Expand Up @@ -230,7 +231,7 @@ PrivacyLossDistribution::CreateForGaussianMechanism(
absl::StatusOr<std::unique_ptr<PrivacyLossDistribution>>
PrivacyLossDistribution::CreateForDiscreteGaussianMechanism(
double sigma, int sensitivity, EstimateType estimate_type,
double discretization_interval, absl::optional<int> truncation_bound) {
double discretization_interval, std::optional<int> truncation_bound) {
ASSIGN_OR_RETURN(std::unique_ptr<DiscreteGaussianPrivacyLoss> privacy_loss,
DiscreteGaussianPrivacyLoss::Create(sigma, sensitivity,
truncation_bound));
Expand Down
2 changes: 1 addition & 1 deletion cc/accounting/privacy_loss_distribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class PrivacyLossDistribution {
double sigma, int sensitivity = 1,
EstimateType estimate_type = EstimateType::kPessimistic,
double discretization_interval = 1e-4,
absl::optional<int> truncation_bound = absl::nullopt);
std::optional<int> truncation_bound = std::nullopt);

// Creates {@link PrivacyLossDistribution} from epsilon and delta parameters.
//
Expand Down
4 changes: 3 additions & 1 deletion cc/accounting/privacy_loss_distribution_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "accounting/privacy_loss_distribution.h"

#include <optional>

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/status/statusor.h"
Expand Down Expand Up @@ -860,7 +862,7 @@ struct DiscreteGaussianPrivacyLossDistributionParam {
double discretization_interval;
ProbabilityMassFunction expected_pmf;
double expected_infinity_mass;
absl::optional<int> truncation_bound = absl::nullopt;
std::optional<int> truncation_bound = std::nullopt;
};

class DiscreteGaussianPrivacyLossDistribution
Expand Down
3 changes: 2 additions & 1 deletion cc/accounting/privacy_loss_mechanism.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "accounting/privacy_loss_mechanism.h"

#include <cmath>
#include <optional>

#include "base/status_macros.h"

Expand Down Expand Up @@ -244,7 +245,7 @@ PrivacyLossTail DiscreteLaplacePrivacyLoss::PrivacyLossDistributionTail()
}
absl::StatusOr<std::unique_ptr<DiscreteGaussianPrivacyLoss>>
DiscreteGaussianPrivacyLoss::Create(double sigma, int sensitivity,
absl::optional<int> truncation_bound) {
std::optional<int> truncation_bound) {
if (sigma <= 0) {
return absl::InvalidArgumentError("sigma should be positive.");
}
Expand Down
2 changes: 1 addition & 1 deletion cc/accounting/privacy_loss_mechanism.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ class DiscreteGaussianPrivacyLoss : public AdditiveNoisePrivacyLoss {
// outside of this range is at most 1e-30.
static absl::StatusOr<std::unique_ptr<DiscreteGaussianPrivacyLoss>> Create(
double sigma, int sensitivity,
absl::optional<int> truncation_bound = absl::nullopt);
std::optional<int> truncation_bound = std::nullopt);

NoiseType Discrete() const override { return NoiseType::kDiscrete; }

Expand Down
4 changes: 2 additions & 2 deletions cc/algorithms/approx-bounds.h
Original file line number Diff line number Diff line change
Expand Up @@ -811,13 +811,13 @@ class ApproxBounds<T>::Builder {
}
}

absl::optional<double> epsilon_;
std::optional<double> epsilon_;
int max_partitions_contributed_ = 1;
int max_contributions_per_partition_ = 1;
std::unique_ptr<NumericalMechanismBuilder> mechanism_builder_ =
absl::make_unique<LaplaceMechanism::Builder>();

absl::optional<double> threshold_;
std::optional<double> threshold_;
double scale_ = DefaultScaleForT();
double base_ = 2.0;
double success_probability_ = 1 - std::pow(10, -9);
Expand Down
12 changes: 6 additions & 6 deletions cc/algorithms/bounded-algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,24 +86,24 @@ class BoundedAlgorithmBuilder : public AlgorithmBuilder<T, Algorithm, Builder> {
// automatically this will be the full epsilon - epsilon spent on that
// ApproxBounds. If called before BoundsSetup this will always return the full
// epsilon.
absl::optional<double> GetRemainingEpsilon() {
std::optional<double> GetRemainingEpsilon() {
if (remaining_epsilon_.has_value()) {
return remaining_epsilon_;
}
return AlgorithmBuilder::GetEpsilon();
}

absl::optional<T> GetLower() const { return lower_; }
absl::optional<T> GetUpper() const { return upper_; }
std::optional<T> GetLower() const { return lower_; }
std::optional<T> GetUpper() const { return upper_; }

private:
// Bounds are optional and do not need to be set. If they are not set,
// automatic bounds will be determined.
absl::optional<T> lower_;
absl::optional<T> upper_;
std::optional<T> lower_;
std::optional<T> upper_;

// Epsilon left over after creating an ApproxBounds.
absl::optional<double> remaining_epsilon_;
std::optional<double> remaining_epsilon_;

// Common initialization and checks for building bounded algorithms.
absl::StatusOr<std::unique_ptr<Algorithm>> BuildAlgorithm() final {
Expand Down
6 changes: 3 additions & 3 deletions cc/algorithms/bounded-mean.h
Original file line number Diff line number Diff line change
Expand Up @@ -533,10 +533,10 @@ class BoundedMean<T>::Builder {
}

private:
absl::optional<double> epsilon_;
std::optional<double> epsilon_;
double delta_ = 0;
absl::optional<T> upper_;
absl::optional<T> lower_;
std::optional<T> upper_;
std::optional<T> lower_;
int max_partitions_contributed_ = 1;
int max_contributions_per_partition_ = 1;
std::unique_ptr<NumericalMechanismBuilder> mechanism_builder_ =
Expand Down
2 changes: 1 addition & 1 deletion cc/algorithms/count.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class Count<T>::Builder {
}

private:
absl::optional<double> epsilon_;
std::optional<double> epsilon_;
double delta_ = 0;
int max_partitions_contributed_ = 1;
int max_contributions_per_partition_ = 1;
Expand Down
2 changes: 1 addition & 1 deletion cc/algorithms/numerical-mechanisms.cc
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ GaussianMechanism::Builder::Build() {
return result;
} // Else construct from DP parameters.

absl::optional<double> epsilon = GetEpsilon();
std::optional<double> epsilon = GetEpsilon();
RETURN_IF_ERROR(ValidateIsFiniteAndPositive(epsilon, "Epsilon"));
RETURN_IF_ERROR(DeltaIsSetAndValid());
ASSIGN_OR_RETURN(double l2, CalculateL2Sensitivity());
Expand Down
26 changes: 12 additions & 14 deletions cc/algorithms/numerical-mechanisms.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,16 @@ class NumericalMechanismBuilder {
return absl::OkStatus();
}

absl::optional<double> GetEpsilon() const { return epsilon_; }
absl::optional<double> GetDelta() const { return delta_; }
absl::optional<double> GetL0Sensitivity() const { return l0_sensitivity_; }
absl::optional<double> GetLInfSensitivity() const {
return linf_sensitivity_;
}
std::optional<double> GetEpsilon() const { return epsilon_; }
std::optional<double> GetDelta() const { return delta_; }
std::optional<double> GetL0Sensitivity() const { return l0_sensitivity_; }
std::optional<double> GetLInfSensitivity() const { return linf_sensitivity_; }

private:
absl::optional<double> epsilon_;
absl::optional<double> delta_;
absl::optional<double> l0_sensitivity_;
absl::optional<double> linf_sensitivity_;
std::optional<double> epsilon_;
std::optional<double> delta_;
std::optional<double> l0_sensitivity_;
std::optional<double> linf_sensitivity_;
};

// Provides differential privacy by adding Laplace noise. This class also
Expand Down Expand Up @@ -205,10 +203,10 @@ class LaplaceMechanism : public NumericalMechanism {
}

protected:
absl::optional<double> GetL1Sensitivity() const { return l1_sensitivity_; }
std::optional<double> GetL1Sensitivity() const { return l1_sensitivity_; }

private:
absl::optional<double> l1_sensitivity_;
std::optional<double> l1_sensitivity_;
};

ABSL_DEPRECATED(
Expand Down Expand Up @@ -333,8 +331,8 @@ class GaussianMechanism : public NumericalMechanism {
}

protected:
absl::optional<double> l2_sensitivity_;
absl::optional<double> stddev_;
std::optional<double> l2_sensitivity_;
std::optional<double> stddev_;

private:
// Returns the l2 sensitivity when it has been set or returns an upper bound
Expand Down
Loading

0 comments on commit 6ec24b2

Please sign in to comment.