#ifndef COMPONENTS_ATTRIBUTION_REPORTING_PRIVACY_MATH_H_
#define COMPONENTS_ATTRIBUTION_REPORTING_PRIVACY_MATH_H_
#include <stdint.h>
#include <compare>
#include <optional>
#include <utility>
#include <vector>
#include "base/component_export.h"
#include "base/numerics/checked_math.h"
#include "base/types/expected.h"
#include "components/attribution_reporting/source_type.mojom-forward.h"
namespace attribution_reporting {
class AttributionScopesData;
class EventReportWindows;
class MaxEventLevelReports;
class TriggerDataSet;
struct FakeEventLevelReport {
uint32_t trigger_data;
int window_index;
friend std::strong_ordering operator<=>(const FakeEventLevelReport&,
const FakeEventLevelReport&) =
default;
};
using RandomizedResponse = std::optional<std::vector<FakeEventLevelReport>>;
COMPONENT_EXPORT(ATTRIBUTION_REPORTING)
bool IsValid(const RandomizedResponse&,
const TriggerDataSet&,
const EventReportWindows&,
MaxEventLevelReports);
enum class RandomizedResponseError {
kExceedsChannelCapacityLimit,
kExceedsScopesChannelCapacityLimit,
kExceedsTriggerStateCardinalityLimit,
kExceedsMaxEventStatesLimit,
};
class COMPONENT_EXPORT(ATTRIBUTION_REPORTING) RandomizedResponseData {
public:
RandomizedResponseData(double rate, RandomizedResponse);
~RandomizedResponseData();
RandomizedResponseData(const RandomizedResponseData&);
RandomizedResponseData& operator=(const RandomizedResponseData&);
RandomizedResponseData(RandomizedResponseData&&);
RandomizedResponseData& operator=(RandomizedResponseData&&);
double rate() const { return rate_; }
const RandomizedResponse& response() const { return response_; }
RandomizedResponse& response() { return response_; }
friend bool operator==(const RandomizedResponseData&,
const RandomizedResponseData&) = default;
private:
double rate_;
RandomizedResponse response_;
};
COMPONENT_EXPORT(ATTRIBUTION_REPORTING) bool GenerateWithRate(double r);
COMPONENT_EXPORT(ATTRIBUTION_REPORTING)
double GetRandomizedResponseRate(uint32_t num_states, double epsilon);
COMPONENT_EXPORT(ATTRIBUTION_REPORTING)
base::expected<uint32_t, RandomizedResponseError> GetNumStates(
const TriggerDataSet&,
const EventReportWindows&,
MaxEventLevelReports);
struct COMPONENT_EXPORT(ATTRIBUTION_REPORTING) PrivacyMathConfig {
double max_channel_capacity_navigation = 11.5;
double max_channel_capacity_scopes_navigation = 11.55;
double max_channel_capacity_event = 6.5;
double max_channel_capacity_scopes_event = 6.5;
double GetMaxChannelCapacity(mojom::SourceType) const;
double GetMaxChannelCapacityScopes(mojom::SourceType) const;
friend bool operator==(const PrivacyMathConfig&,
const PrivacyMathConfig&) = default;
};
COMPONENT_EXPORT(ATTRIBUTION_REPORTING)
base::expected<RandomizedResponseData, RandomizedResponseError>
DoRandomizedResponse(const TriggerDataSet&,
const EventReportWindows&,
MaxEventLevelReports,
double epsilon,
mojom::SourceType,
const std::optional<AttributionScopesData>&,
const PrivacyMathConfig&);
COMPONENT_EXPORT(ATTRIBUTION_REPORTING) uint32_t MaxTriggerStateCardinality();
namespace internal {
COMPONENT_EXPORT(ATTRIBUTION_REPORTING)
base::CheckedNumeric<uint32_t> BinomialCoefficient(
base::StrictNumeric<uint32_t> n,
base::StrictNumeric<uint32_t> k);
COMPONENT_EXPORT(ATTRIBUTION_REPORTING)
std::vector<uint32_t> GetKCombinationAtIndex(
base::StrictNumeric<uint32_t> combination_index,
base::StrictNumeric<uint32_t> k);
COMPONENT_EXPORT(ATTRIBUTION_REPORTING)
base::CheckedNumeric<uint32_t> GetNumberOfStarsAndBarsSequences(
base::StrictNumeric<uint32_t> num_stars,
base::StrictNumeric<uint32_t> num_bars);
COMPONENT_EXPORT(ATTRIBUTION_REPORTING)
base::expected<std::vector<uint32_t>, std::monostate> GetStarIndices(
base::StrictNumeric<uint32_t> num_stars,
base::StrictNumeric<uint32_t> num_bars,
base::StrictNumeric<uint32_t> sequence_index);
COMPONENT_EXPORT(ATTRIBUTION_REPORTING)
std::vector<uint32_t> GetBarsPrecedingEachStar(
std::vector<uint32_t> star_indices);
COMPONENT_EXPORT(ATTRIBUTION_REPORTING) double BinaryEntropy(double p);
COMPONENT_EXPORT(ATTRIBUTION_REPORTING)
double ComputeChannelCapacity(base::StrictNumeric<uint32_t> num_states_strict,
double randomized_response_rate);
COMPONENT_EXPORT(ATTRIBUTION_REPORTING)
double ComputeChannelCapacityScopes(
base::StrictNumeric<uint32_t> num_states_strict,
base::StrictNumeric<uint32_t> max_event_states,
base::StrictNumeric<uint32_t> attribution_scope_limit);
COMPONENT_EXPORT(ATTRIBUTION_REPORTING)
base::expected<std::vector<FakeEventLevelReport>, RandomizedResponseError>
GetFakeReportsForSequenceIndex(
const TriggerDataSet&,
const EventReportWindows&,
MaxEventLevelReports,
base::StrictNumeric<uint32_t> random_stars_and_bars_sequence_index);
}
class COMPONENT_EXPORT(ATTRIBUTION_REPORTING)
ScopedMaxTriggerStateCardinalityForTesting {
public:
explicit ScopedMaxTriggerStateCardinalityForTesting(uint32_t);
~ScopedMaxTriggerStateCardinalityForTesting();
ScopedMaxTriggerStateCardinalityForTesting(
const ScopedMaxTriggerStateCardinalityForTesting&) = delete;
ScopedMaxTriggerStateCardinalityForTesting& operator=(
const ScopedMaxTriggerStateCardinalityForTesting&) = delete;
ScopedMaxTriggerStateCardinalityForTesting(
ScopedMaxTriggerStateCardinalityForTesting&&) = delete;
ScopedMaxTriggerStateCardinalityForTesting& operator=(
ScopedMaxTriggerStateCardinalityForTesting&&) = delete;
private:
uint32_t previous_;
};
}
#endif