| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
|
| 3 |
//! Differential privacy (DP) primitives. |
| 4 |
//! |
| 5 |
//! There are three main traits defined in this module: |
| 6 |
//! |
| 7 |
//! - `DifferentialPrivacyBudget`: Implementors should be types of DP-budgets, |
| 8 |
//! i.e., methods to measure the amount of privacy provided by DP-mechanisms. |
| 9 |
//! Examples: zCDP, ApproximateDP (Epsilon-Delta), PureDP |
| 10 |
//! |
| 11 |
//! - `DifferentialPrivacyDistribution`: Distribution from which noise is sampled. |
| 12 |
//! Examples: DiscreteGaussian, DiscreteLaplace |
| 13 |
//! |
| 14 |
//! - `DifferentialPrivacyStrategy`: This is a combination of choices for budget and distribution. |
| 15 |
//! Examples: zCDP-DiscreteGaussian, EpsilonDelta-DiscreteGaussian |
| 16 |
//! |
| 17 |
use num_bigint::{BigInt, BigUint, TryFromBigIntError}; |
| 18 |
use num_rational::{BigRational, Ratio}; |
| 19 |
use serde::{Deserialize, Serialize}; |
| 20 |
|
| 21 |
/// Errors propagated by methods in this module. |
| 22 |
#[derive(Debug, thiserror::Error)] |
| 23 |
#[non_exhaustive] |
| 24 |
pub enum DpError { |
| 25 |
/// Tried to use an invalid float as privacy parameter. |
| 26 |
#[error( |
| 27 |
"DP error: input value was not a valid privacy parameter. \ |
| 28 |
It should to be a non-negative, finite float." |
| 29 |
)] |
| 30 |
InvalidFloat, |
| 31 |
|
| 32 |
/// Tried to construct a rational number with zero denominator. |
| 33 |
#[error("DP error: input denominator was zero.")] |
| 34 |
ZeroDenominator, |
| 35 |
|
| 36 |
/// Tried to convert BigInt into something incompatible. |
| 37 |
#[error("DP error: {0}")] |
| 38 |
BigIntConversion(#[from] TryFromBigIntError<BigInt>), |
| 39 |
} |
| 40 |
|
| 41 |
/// Positive arbitrary precision rational number to represent DP and noise distribution parameters in |
| 42 |
/// protocol messages and manipulate them without rounding errors. |
| 43 |
#[derive(Clone, Debug)] |
| 44 |
pub struct Rational(Ratio<BigUint>); |
| 45 |
|
| 46 |
impl Rational { |
| 47 |
/// Construct a [`Rational`] number from numerator `n` and denominator `d`. Errors if denominator is zero. |
| 48 |
pub fn from_unsigned<T>(n: T, d: T) -> Result<Self, DpError> |
| 49 |
where |
| 50 |
T: Into<u128>, |
| 51 |
{ |
| 52 |
// we don't want to expose BigUint in the public api, hence the Into<u128> bound |
| 53 |
let d = d.into(); |
| 54 |
if d == 0 { |
| 55 |
Err(DpError::ZeroDenominator) |
| 56 |
} else { |
| 57 |
Ok(Rational(Ratio::<BigUint>::new(n.into().into(), d.into()))) |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
impl TryFrom<f32> for Rational { |
| 63 |
type Error = DpError; |
| 64 |
/// Constructs a `Rational` from a given `f32` value. |
| 65 |
/// |
| 66 |
/// The special float values (NaN, positive and negative infinity) result in |
| 67 |
/// an error. All other values are represented exactly, without rounding errors. |
| 68 |
fn try_from(value: f32) -> Result<Self, DpError> { |
| 69 |
match BigRational::from_float(value) { |
| 70 |
Some(y) => Ok(Rational(Ratio::<BigUint>::new( |
| 71 |
y.numer().clone().try_into()?, |
| 72 |
y.denom().clone().try_into()?, |
| 73 |
))), |
| 74 |
None => Err(DpError::InvalidFloat)?, |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/// Marker trait for differential privacy budgets (regardless of the specific accounting method). |
| 80 |
pub trait DifferentialPrivacyBudget {} |
| 81 |
|
| 82 |
/// Marker trait for differential privacy scalar noise distributions. |
| 83 |
pub trait DifferentialPrivacyDistribution {} |
| 84 |
|
| 85 |
/// Zero-concentrated differential privacy (ZCDP) budget as defined in [[BS16]]. |
| 86 |
/// |
| 87 |
/// [BS16]: https://arxiv.org/pdf/1605.02065.pdf |
| 88 |
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Ord, PartialOrd)] |
| 89 |
pub struct ZCdpBudget { |
| 90 |
epsilon: Ratio<BigUint>, |
| 91 |
} |
| 92 |
|
| 93 |
impl ZCdpBudget { |
| 94 |
/// Create a budget for parameter `epsilon`, using the notation from [[CKS20]] where `rho = (epsilon**2)/2` |
| 95 |
/// for a `rho`-ZCDP budget. |
| 96 |
/// |
| 97 |
/// [CKS20]: https://arxiv.org/pdf/2004.00010.pdf |
| 98 |
pub fn new(epsilon: Rational) -> Self { |
| 99 |
Self { epsilon: epsilon.0 } |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
impl DifferentialPrivacyBudget for ZCdpBudget {} |
| 104 |
|
| 105 |
/// Strategy to make aggregate results differentially private, e.g. by adding noise from a specific |
| 106 |
/// type of distribution instantiated with a given DP budget. |
| 107 |
pub trait DifferentialPrivacyStrategy { |
| 108 |
/// The type of the DP budget, i.e. the variant of differential privacy that can be obtained |
| 109 |
/// by using this strategy. |
| 110 |
type Budget: DifferentialPrivacyBudget; |
| 111 |
|
| 112 |
/// The distribution type this strategy will use to generate the noise. |
| 113 |
type Distribution: DifferentialPrivacyDistribution; |
| 114 |
|
| 115 |
/// The type the sensitivity used for privacy analysis has. |
| 116 |
type Sensitivity; |
| 117 |
|
| 118 |
/// Create a strategy from a differential privacy budget. The distribution created with |
| 119 |
/// `create_distribution` should provide the amount of privacy specified here. |
| 120 |
fn from_budget(b: Self::Budget) -> Self; |
| 121 |
|
| 122 |
/// Create a new distribution parametrized s.t. adding samples to the result of a function |
| 123 |
/// with sensitivity `s` will yield differential privacy of the DP variant given in the |
| 124 |
/// `Budget` type. Can error upon invalid parameters. |
| 125 |
fn create_distribution(&self, s: Self::Sensitivity) -> Result<Self::Distribution, DpError>; |
| 126 |
} |
| 127 |
|
| 128 |
pub mod distributions; |