Algorithm Search⌘K

Definitionresearch

g-leakage

Also called gain function leakage.

Generalizes min-entropy leakage with a gain function that scores each guess, to model attackers who profit from partial or several guesses.

g-leakage measures how much a channel helps an attacker whose payoff is not just "right or wrong in one guess". A gain function g(w,x)g(w, x) says how much the attacker earns by guessing ww when the secret is xx, so it can model an attacker who wins by guessing a value close to the secret, a property of the secret, or the secret within kk tries. The leakage is the base-2 log of the ratio of the best expected gain after and before seeing the output. Alvim, Chatzikokolakis, Palamidessi and Smith defined it in 2012. Min-entropy leakage is the special case where only an exact guess earns anything.

Definition

Secret, prior and channel. As for min-entropy leakage: a secret XX in a finite set X\mathcal X with prior π\pi, and a channel matrix CC with C[x,y]=P(Y=y∣X=x)C[x, y] = P(Y = y \mid X = x) from X\mathcal X to a finite set Y\mathcal Y of outputs.

Gain function. A finite nonempty set W\mathcal W of allowed guesses and a function g:W×X→[0,1]g : \mathcal W \times \mathcal X \to [0, 1]. The guesses need not be secrets. Alvim et al. rule out gain functions for which some secret earns 0 under every guess, so that Vg(π)>0V_g(\pi) > 0 for every prior.

Adversary. The attacker knows π\pi, CC and gg, sees one output yy, and picks one guess w∈Ww \in \mathcal W as any function of yy. The payoff is g(w,X)g(w, X), and the attacker maximizes its expectation. Nothing outside YY is observed.

g-vulnerability and g-leakage.

Vg(π)=max⁡w∑xπ(x) g(w,x),Vg(π,C)=∑ymax⁡w∑xπ(x) C[x,y] g(w,x).V_g(\pi) = \max_{w} \sum_{x} \pi(x)\, g(w, x), \qquad V_g(\pi, C) = \sum_{y} \max_{w} \sum_{x} \pi(x)\, C[x, y]\, g(w, x).
Hg(π)=−log⁡2Vg(π),Hg(π,C)=−log⁡2Vg(π,C),Lg(π,C)=Hg(π)−Hg(π,C)=log⁡2Vg(π,C)Vg(π).H_g(\pi) = -\log_2 V_g(\pi), \qquad H_g(\pi, C) = -\log_2 V_g(\pi, C), \qquad \mathcal L_g(\pi, C) = H_g(\pi) - H_g(\pi, C) = \log_2 \frac{V_g(\pi, C)}{V_g(\pi)}.

The g-capacity is MLg(C)=sup⁡πLg(π,C)\mathcal{ML}_g(C) = \sup_\pi \mathcal L_g(\pi, C). As on the other QIF pages, the logarithms are base 2 and the unit is the bit.

Standard gain functions.

  • Identity gain. W=X\mathcal W = \mathcal X and gid(w,x)=1g_{\mathrm{id}}(w, x) = 1 if w=xw = x, else 0. Then Vgid=VV_{g_{\mathrm{id}}} = V and the g-leakage is the min-entropy leakage (their Proposition 3.1).
  • Metric gain. For a metric dd on X\mathcal X normalized to [0,1][0, 1], gd(w,x)=1−d(w,x)g_d(w, x) = 1 - d(w, x). A guess close to the secret earns almost as much as the exact secret.
  • Binary gain. Each guess is a set W⊆XW \subseteq \mathcal X and earns 1 when x∈Wx \in W. With the blocks of a partition as guesses, the attacker wants to learn which block holds the secret. With all sets of at most kk secrets, Vg(π)V_g(\pi) is the chance to find the secret within kk tries.

Computation. With nn secrets, kk outputs and mm guesses, the g-leakage costs O(nkm)O(nkm) arithmetic operations:

  1. G-LEAKAGE(π, C, g)
  2. 1V0 ← max over w of Σ_x π[x] · g(w, x)
  3. 2V1 ← 0
  4. 3for each output y do
  5. 4V1 ← V1 + max over w of Σ_x π[x] · C[x, y] · g(w, x)
  6. 5return log2(V1 / V0)

The Python implementation uses fractions.Fraction and takes the logarithm only at the end. Its tests check the examples below, compare Vg(π,C)V_g(\pi, C) with an enumeration of all maps from outputs to guesses on 600 random cases, and check the Miracle bound on a grid of priors. The kk-tries gain is built from the sets of exactly kk secrets; for k≤nk \le n this gives the same vulnerability as sets of at most kk, because a larger set never earns less.

Basic facts. Each is a theorem from Alvim et al. (2012).

  • Best strategy. Vg(π,C)V_g(\pi, C) is the largest expected gain over all strategies s:Y→Ws : \mathcal Y \to \mathcal W, by the same argument as for min-entropy leakage. A random strategy does no better, since the expected gain is linear in the strategy.
  • Never negative (Theorem 4.1). Vg(π,C)≥Vg(π)V_g(\pi, C) \ge V_g(\pi): the attacker can ignore yy and play the best prior guess. So Lg(π,C)≥0\mathcal L_g(\pi, C) \ge 0.
  • Zero leakage (Theorem 4.2). Lg(π,C)=0\mathcal L_g(\pi, C) = 0 if and only if one guess w∗w^\ast maximizes ∑xπ(x)C[x,y]g(w,x)\sum_x \pi(x) C[x, y] g(w, x) for every output yy at once.
  • Miracle theorem (Theorem 5.1). For every channel CC and every gain function gg, MLg(C)≤ML(C)=log⁡2∑ymax⁡xC[x,y]\mathcal{ML}_g(C) \le \mathcal{ML}(C) = \log_2 \sum_y \max_x C[x, y]. The proof replaces C[x,y]C[x, y] by max⁡xC[x,y]\max_x C[x, y] inside the sum and pulls it out. So the min-capacity bounds the leakage for every attacker of this form and every prior. As a corollary, allowing kk tries cannot raise the capacity above the one-try capacity (Corollary 5.2).
  • Composition (Theorem 6.2). If C1=C2C3C_1 = C_2 C_3 for some channel C3C_3, so that C1C_1 is C2C_2 followed by further processing, then Lg(π,C1)≤Lg(π,C2)\mathcal L_g(\pi, C_1) \le \mathcal L_g(\pi, C_2) for every prior and every gain function.

Examples and non-examples

Example: a near pair of secrets. This is Example 4 of Alvim et al. Take three secrets, the uniform prior and the channel

y1y_1 y2y_2
x1x_1 1/2 1/2
x2x_2 1 0
x3x_3 0 1

with the metric gain where x2x_2 and x3x_3 are at distance 0.020.02 and every other pair of distinct secrets is at distance 1: so g(x2,x3)=g(x3,x2)=0.98g(x_2, x_3) = g(x_3, x_2) = 0.98, g(w,w)=1g(w, w) = 1, and the other gains are 0. The best prior guess is x2x_2 or x3x_3, with Vg(π)=(0+1+0.98)/3=0.66V_g(\pi) = (0 + 1 + 0.98)/3 = 0.66. After seeing the output, Vg(π,C)=2/3V_g(\pi, C) = 2/3, so Lg=log⁡2(2/3/0.66)=0.0145\mathcal L_g = \log_2 (2/3 / 0.66) = 0.0145 bits. The min-entropy leakage of the same channel is 1 bit, about 70 times more. The output separates x2x_2 from x3x_3, which matters to a one-guess attacker but hardly to one who is paid for being close.

Example: g-leakage above min-entropy leakage. This is Example 5 of Alvim et al. Take the prior π=(0.6,0.2,0.2)\pi = (0.6, 0.2, 0.2) and the channel with rows (0.6,0.4)(0.6, 0.4), (0,1)(0, 1) and (0,1)(0, 1). The best single guess is x1x_1 for both outputs, so the min-entropy leakage is 0. With the gain of the previous example, Vg(π)=0.6V_g(\pi) = 0.6 and Vg(π,C)=0.36+0.396=0.756V_g(\pi, C) = 0.36 + 0.396 = 0.756, so Lg=log⁡21.26=0.3334\mathcal L_g = \log_2 1.26 = 0.3334 bits. With the partition gain for the blocks {x1}\{x_1\} and {x2,x3}\{x_2, x_3\}, Vg(π,C)=0.36+0.4=0.76V_g(\pi, C) = 0.36 + 0.4 = 0.76 and Lg=log⁡2(19/15)=0.3410\mathcal L_g = \log_2 (19/15) = 0.3410 bits. So neither leakage bounds the other for a fixed prior; only the capacities are ordered.

Example: two tries at the last two bits. Let hh be uniform on {0,…,7}\{0, \ldots, 7\} and let the program output h mod 4h \bmod 4. With the 2-tries gain, Vg(π)=2/8=1/4V_g(\pi) = 2/8 = 1/4. Each output leaves two secrets and the attacker tries both, so Vg(π,C)=1V_g(\pi, C) = 1 and Lg=2\mathcal L_g = 2 bits, the same as the one-try min-entropy leakage. The attacker's success rises from 1/41/4 to certainty.

Non-example: the happy gain. One guess that earns 1 for every secret gives Vg(π)=Vg(π,C)=1V_g(\pi) = V_g(\pi, C) = 1, so every channel has g-leakage 0. The attacker already has all it wants, so the measure says nothing about the channel.

Non-example: a gain that is 0 on the support. If g(w,x)=0g(w, x) = 0 for every ww and every xx with π(x)>0\pi(x) > 0, then Vg(π)=0V_g(\pi) = 0 and the ratio is undefined. Alvim et al. exclude such gain functions; the implementation states Vg(π)>0V_g(\pi) > 0 as a precondition.

Non-example: a change of scale. A gain with values in [0,a][0, a] instead of [0,1][0, 1] is not a new measure: it multiplies both vulnerabilities by aa, so the g-leakage does not change. Alvim et al. note this in a footnote to Definition 3.2.

  • Min-entropy leakage. The identity gain. Every result on this page applies to it.
  • Min-capacity bound. The theorem ML(C)=log⁡2∑ymax⁡xC[x,y]\mathcal{ML}(C) = \log_2 \sum_y \max_x C[x, y], which the Miracle theorem uses as the bound for every gain function.
  • Shannon leakage and channel capacity. The Shannon leakage is the mutual information H(X)−H(X∣Y)H(X) - H(X \mid Y), built from Shannon entropy. Theorem 5.3 of Alvim et al. shows that the Shannon capacity is at most the min-capacity; its proof uses Jensen's inequality.
  • Additive g-leakage. Vg(π,C)−Vg(π)V_g(\pi, C) - V_g(\pi). Alvim et al. (2014) study its capacities next to those of the multiplicative form on this page.
  • Loss functions. A gain function is a bounded payoff in the sense of decision theory, and the attacker picks the guess of the largest expected payoff. A loss function is the same idea with the sign turned around.
  • Quantitative information flow. The field that compares these measures.

Code

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

g_leakage.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/g-leakage/. The authors are listed in prov/XKQ40D.jsonl.

import itertools
import math
from collections.abc import Sequence
from fractions import Fraction

Prior = Sequence[Fraction]
Channel = Sequence[Sequence[Fraction]]
Gain = Sequence[Sequence[Fraction]]  # gain[w][x] for guess w and secret x


def g_vulnerability(pi: Prior, gain: Gain) -> Fraction:
    """V_g(pi) = max_w sum_x pi[x] gain[w][x]. Cost: O(|W| |X|)."""
    return max(sum(pi[x] * row[x] for x in range(len(pi))) for row in gain)


def posterior_g_vulnerability(pi: Prior, c: Channel, gain: Gain) -> Fraction:
    """V_g(pi, C) = sum_y max_w sum_x pi[x] C[x][y] gain[w][x]. Cost: O(|Y| |W| |X|)."""
    total = Fraction(0)
    for y in range(len(c[0])):
        total += max(sum(pi[x] * c[x][y] * row[x] for x in range(len(pi))) for row in gain)
    return total


def g_leakage(pi: Prior, c: Channel, gain: Gain) -> float:
    """g-leakage L_g(pi, C) = log2(V_g(pi, C) / V_g(pi)) in bits.

    Precondition: pi is a distribution on the secrets 0..|X|-1, each row of c
    sums to 1, every gain value is in [0, 1], and V_g(pi) > 0. Cost: O(|Y| |W| |X|)
    exact arithmetic operations for |W| guesses.
    """
    vg0 = max(sum(pi[x] * row[x] for x in range(len(pi))) for row in gain)  # @line L1
    vg1 = Fraction(0)  # @line L2
    for y in range(len(c[0])):  # @line L3
        vg1 += max(sum(pi[x] * c[x][y] * row[x] for x in range(len(pi))) for row in gain)  # @line L4
    return math.log2(vg1 / vg0)  # @line L5


def identity_gain(n: int) -> list[list[Fraction]]:
    """The gain of one exact guess: 1 when w = x, else 0. It gives min-entropy leakage."""
    return [[Fraction(int(w == x)) for x in range(n)] for w in range(n)]


def k_tries_gain(n: int, k: int) -> list[list[Fraction]]:
    """The gain of k guesses: one guess per set of k secrets, worth 1 when the secret is in the set."""
    return [[Fraction(int(x in s)) for x in range(n)] for s in itertools.combinations(range(n), k)]


def partition_gain(blocks: Sequence[Sequence[int]], n: int) -> list[list[Fraction]]:
    """The gain of guessing the block of a partition that holds the secret."""
    return [[Fraction(int(x in b)) for x in range(n)] for b in blocks]
test_g_leakage.py 147 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/g-leakage/. The authors are listed in prov/XKQ40D.jsonl.

import itertools
import math
import random
from fractions import Fraction as F

from g_leakage import (
    g_leakage,
    g_vulnerability,
    identity_gain,
    k_tries_gain,
    partition_gain,
    posterior_g_vulnerability,
)


def uniform(n):
    return [F(1, n)] * n


def min_entropy_ratio(pi, c):
    """Min-entropy leakage ratio written out directly, without a gain function."""
    post = sum(max(pi[x] * c[x][y] for x in range(len(pi))) for y in range(len(c[0])))
    return post / max(pi)


def min_capacity_ratio(c):
    return sum(max(row[y] for row in c) for y in range(len(c[0])))


def best_strategy(pi, c, gain):
    """Oracle: try every map from outputs to guesses and keep the best expected gain."""
    ny, nw = len(c[0]), len(gain)
    best = F(-1)
    for choice in itertools.product(range(nw), repeat=ny):
        val = sum(pi[x] * c[x][y] * gain[choice[y]][x] for y in range(ny) for x in range(len(pi)))
        best = max(best, val)
    return best


def random_channel(rng, nx, ny):
    c = []
    for _ in range(nx):
        w = [rng.randint(0, 4) for _ in range(ny)]
        if sum(w) == 0:
            w[rng.randrange(ny)] = 1
        c.append([F(v, sum(w)) for v in w])
    return c


def random_gain(rng, nw, nx):
    g = [[F(rng.randint(0, 4), 4) for _ in range(nx)] for _ in range(nw)]
    g[0][rng.randrange(nx)] = F(1)  # some guess has a positive gain
    return g


def grid_priors(nx, d):
    for parts in itertools.product(range(d + 1), repeat=nx - 1):
        if sum(parts) <= d:
            yield [F(p, d) for p in parts] + [F(d - sum(parts), d)]


def test_alvim_example_4() -> None:
    # Example 4 of Alvim, Chatzikokolakis, Palamidessi and Smith (CSF 2012).
    c = [[F(1, 2), F(1, 2)], [F(1), F(0)], [F(0), F(1)]]
    g = [[F(1), F(0), F(0)], [F(0), F(1), F(98, 100)], [F(0), F(98, 100), F(1)]]
    pi = uniform(3)
    assert g_vulnerability(pi, g) == F(33, 50)
    assert posterior_g_vulnerability(pi, c, g) == F(2, 3)
    assert abs(g_leakage(pi, c, g) - 0.014500) < 1e-6
    assert g_leakage(pi, c, identity_gain(3)) == 1.0


def test_partition_gain_sees_what_one_guess_misses() -> None:
    c = [[F(3, 5), F(2, 5)], [F(0), F(1)], [F(0), F(1)]]
    pi = [F(3, 5), F(1, 5), F(1, 5)]
    assert g_leakage(pi, c, identity_gain(3)) == 0.0
    g = partition_gain([[0], [1, 2]], 3)
    assert g_vulnerability(pi, g) == F(3, 5)
    assert posterior_g_vulnerability(pi, c, g) == F(19, 25)
    assert abs(g_leakage(pi, c, g) - math.log2(F(19, 15))) < 1e-12
    # Example 5 of Alvim et al. (2012): the same channel and prior with the gain of Example 4.
    gd = [[F(1), F(0), F(0)], [F(0), F(1), F(98, 100)], [F(0), F(98, 100), F(1)]]
    assert g_vulnerability(pi, gd) == F(3, 5)
    assert posterior_g_vulnerability(pi, c, gd) == F(756, 1000)
    assert abs(g_leakage(pi, c, gd) - 0.333424) < 1e-6


def test_k_tries_on_mod_four() -> None:
    c = [[F(int(h % 4 == y)) for y in range(4)] for h in range(8)]
    g = k_tries_gain(8, 2)
    assert len(g) == 28
    assert g_vulnerability(uniform(8), g) == F(1, 4)
    assert posterior_g_vulnerability(uniform(8), c, g) == 1
    assert g_leakage(uniform(8), c, g) == 2.0


def test_edges() -> None:
    # One output: nothing leaks for any gain.
    c = [[F(1)], [F(1)], [F(1)]]
    g = [[F(1, 2), F(1), F(0)], [F(1), F(0), F(1, 4)]]
    assert g_leakage([F(1, 2), F(1, 4), F(1, 4)], c, g) == 0.0
    # A single guess: V_g(pi, C) = V_g(pi), so nothing leaks.
    ident = [[F(1), F(0)], [F(0), F(1)]]
    assert g_leakage(uniform(2), ident, [[F(1), F(1, 3)]]) == 0.0


def test_against_strategies_and_theorems() -> None:
    rng = random.Random(2012)
    for _ in range(600):
        nx, ny, nw = rng.randint(1, 3), rng.randint(1, 3), rng.randint(1, 3)
        c = random_channel(rng, nx, ny)
        w = [rng.randint(1, 5) for _ in range(nx)]
        pi = [F(v, sum(w)) for v in w]
        g = random_gain(rng, nw, nx)
        v0 = g_vulnerability(pi, g)
        v1 = posterior_g_vulnerability(pi, c, g)
        assert v1 == best_strategy(pi, c, g)
        # Theorem 4.1: g-leakage is never negative.
        assert v1 >= v0
        # Theorem 4.2: zero leakage iff one guess is best for every output.
        one_guess = any(
            all(
                sum(pi[x] * c[x][y] * g[k][x] for x in range(nx))
                == max(sum(pi[x] * c[x][y] * row[x] for x in range(nx)) for row in g)
                for y in range(ny)
            )
            for k in range(nw)
        )
        assert (v1 == v0) == one_guess
        # The identity gain gives min-entropy leakage.
        assert posterior_g_vulnerability(pi, c, identity_gain(nx)) / g_vulnerability(pi, identity_gain(nx)) == min_entropy_ratio(pi, c)


def test_miracle_bound_on_prior_grid() -> None:
    rng = random.Random(77)
    for _ in range(100):
        nx, ny, nw = rng.randint(2, 3), rng.randint(1, 3), rng.randint(1, 3)
        c = random_channel(rng, nx, ny)
        g = random_gain(rng, nw, nx)
        cap = min_capacity_ratio(c)
        for p in grid_priors(nx, 6):
            if g_vulnerability(p, g) > 0:
                # Theorem 5.1: the g-leakage never exceeds the min-capacity.
                assert posterior_g_vulnerability(p, c, g) <= cap * g_vulnerability(p, g)

References

  1. Mario S. Alvim, Konstantinos Chatzikokolakis, Catuscia Palamidessi and Geoffrey Smith, Measuring information leakage using generalized gain functions, Proceedings of CSF 2012, pp. 265 to 279; Definitions 3.1 to 3.6 and Proposition 3.1 (gain functions, g-vulnerability, g-leakage, identity, metric and binary gains), Examples 4 and 5, Theorems 4.1 and 4.2, Theorem 5.1 (the Miracle theorem), Corollary 5.2, Theorem 5.3 and Theorem 6.2
  2. Mario S. Alvim, Konstantinos Chatzikokolakis, Annabelle McIver, Carroll Morgan, Catuscia Palamidessi and Geoffrey Smith, Additive and multiplicative notions of leakage, and their capacities, Proceedings of CSF 2014, additive and multiplicative g-leakage and their capacities over priors and gain functions
  3. Mario S. Alvim, Konstantinos Chatzikokolakis, Annabelle McIver, Carroll Morgan, Catuscia Palamidessi and Geoffrey Smith, The Science of Quantitative Information Flow, Springer, 2020, Chapter 3 (gain functions and g-vulnerability), Chapter 5 (posterior g-vulnerability and g-leakage) and Chapter 7 (capacity)

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/XKQ40D/text.yaml