Skip to main content

RandomizedResponse

Struct RandomizedResponse 

Source
pub struct RandomizedResponse { /* private fields */ }
👎Deprecated since 0.4.0:

not differentially private (predictable xorshift64 state, time-derived seed, floating-point inverse transform); use alice_crypto::dp instead. Removed in 0.5.0

Expand description

Randomized Response for binary data

Classic technique: to report a sensitive bit b:

  • With probability p, report b truthfully
  • With probability 1-p, report a random bit

This provides ε-differential privacy where ε = ln((p + 0.5(1-p)) / (0.5(1-p)))

Implementations§

Source§

impl RandomizedResponse

Source

pub fn new(epsilon: f64) -> Self

Create from privacy parameter epsilon

Higher epsilon = more accuracy, less privacy

std only (system entropy); no_std は Self::with_probability

§Panics

epsilon が有限でない、または負の場合 検査するのは入力の epsilon で、 p_true はそこからの導出値 負の ε は p_true < 0.5 を作り、 Self::estimate_proportion の推定値の符号が反転する (実測: epsilon = -5.0 で p_true = 6.69e-3) ⚠️ 2026-10-07 までは Self::with_probability だけが定義域を検査していたので、new 経由だと 同じ不変条件を破れる状態だった

epsilon = 0.0 は許す (p_true = 0.5 = 常に無作為に答える、 with_probability(0.5) と同じ意味のある端)

上端は切らない: p = 1/(1 + e^(−ε)) (logistic の数値安定形) で計算するので ε → ∞ の極限 1.0 をそのまま返す ⚠️ 旧式の e^ε/(1 + e^ε) は e^ε が ε ≳ 709.79 で inf になり inf/inf = NaN を返していた (実測) NaN は代数的な書き方の副産物で定義域の問題ではないので、ε の上限を assert で切るのでなく式を直した (ε = 0 / 0.5 / 1 / 50 / 709 では旧式と bit 一致、ε = 3 で 1 ulp 異なる)

Source

pub const fn with_probability(p_true: f64, seed: u64) -> Self

Create with explicit probability and seed

§Panics

p_true が [0.5, 1.0] の外 (NaN / 無限大を含む、どちらも比較が 偽になるので同じ assert で弾かれる) の場合

定義域の扱いは同 crate の ExponentialMovingAverage::new に合わせた (範囲外は黙って丸めず panic する) 2026-10-07 までは clamp(0.5, 1.0) だったので、0.0 を渡すと黙って 0.5 に、2.0 は 1.0 になり、NaN は clamp が NaN を返すので丸められずに残り、 p_true() が NaN を報告する機構ができていた 丸めは呼び出し側が 頼んだのと違う ε の機構を作るので、推定値の意味が変わる

0.5 は「常に無作為に答える」という意味のある下端なので許す (new(0.0) がちょうど 0.5 を作る)

Source

pub fn privatize(&mut self, value: bool) -> bool

Privatize a boolean value

Source

pub fn privatize_bit(&mut self, bit: u8) -> u8

Privatize a bit (0 or 1)

Source

pub const fn p_true(&self) -> f64

Get the probability of truthful response

Source

pub fn estimate_proportion(p_true: f64, n: u64, k: u64) -> f64

Estimate true proportion from noisy counts

Given N total responses with K positive responses, estimate the true proportion of positive values.

Trait Implementations§

Source§

impl Clone for RandomizedResponse

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RandomizedResponse

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.