1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//! Definitions of security and leakage measures.
//! 
//! In the documentation, we write R* to indicate the Bayes risk,
//! and G to indicate the error of random guessing (i.e., 1 - max priors).
//!
//! # References
//! [1] M. S. Alvim et al. "Additive and multiplicative notions of leakage,
//!     and their capacities." CSF, 2014.
//! [2] G. Cherubin "Bayes, not Naïve: Security Bounds on Website
//!     Fingerprinting Defenses." PoPETS, 2017
extern crate float_cmp;

use self::float_cmp::approx_eq;


/// Prints several security measures that can be derived from a Bayes risk
/// estimate and Random guessing error.
pub fn print_all_measures(bayes_risk_estimate: f64, random_guessing: f64) {
    println!("Multiplicative Leakage: {}",
             multiplicative_leakage(bayes_risk_estimate, random_guessing));
    println!("Additive Leakage: {}",
             additive_leakage(bayes_risk_estimate, random_guessing));
    println!("Bayes security measure: {}",
             bayes_security_measure(bayes_risk_estimate, random_guessing));
    println!("Min-entropy Leakage: {}",
             min_entropy_leakage(bayes_risk_estimate, random_guessing));
}

/// Computes the Multiplicative Leakage, as defined in [1].
pub fn multiplicative_leakage(bayes_risk: f64, random_guessing: f64) -> f64 {
    assert!(!approx_eq!(f64, random_guessing, 0.),
            "Random guessing error cannot be 0");

    (1.-bayes_risk) / (1.-random_guessing)
}

/// Computes the Additive Leakage, as defined in [1].
pub fn additive_leakage(bayes_risk: f64, random_guessing: f64) -> f64 {
    assert!(!approx_eq!(f64, random_guessing, 0.),
            "Random guessing error cannot be 0");

    random_guessing - bayes_risk
}

/// Computes the Bayes security measure, as defined in [2].
pub fn bayes_security_measure(bayes_risk: f64, random_guessing: f64) -> f64 {
    assert!(!approx_eq!(f64, random_guessing, 0.),
            "Random guessing error cannot be 0");

    bayes_risk / random_guessing
}

/// Computes the Min-entropy leakage.
pub fn min_entropy_leakage(bayes_risk: f64, random_guessing: f64) -> f64 {
    assert!(!approx_eq!(f64, random_guessing, 0.),
            "Random guessing error cannot be 0");

    - (1.-random_guessing).log(2.) + (1.-bayes_risk).log(2.)
}