<!-- Copyright (c) 2026 defconxt. All rights reserved. -->
<!-- Licensed under AGPL-3.0 — see LICENSE file for details. -->
---
name: implementing-differential-privacy
description: >-
  Implement differential privacy mechanisms including Laplace and Gaussian noise,
  epsilon-delta budgeting, and privacy-preserving queries to enable statistical
  analysis while providing mathematical privacy guarantees.
domain: cybersecurity
subdomain: privacy-engineering
tags:
  - differential-privacy
  - epsilon
  - laplace
  - gaussian
  - privacy-budget
  - noise
  - statistical-privacy
version: "1.0"
author: defconxt
license: AGPL-3.0
metadata:
  mitre-attack: []
  mitre-attack: []
---

# Implementing Differential Privacy

## Overview

Differential privacy provides mathematical guarantees that the output of a statistical
query does not reveal whether any individual's data was included in the dataset. By
adding calibrated noise (Laplace, Gaussian) controlled by a privacy budget (ε, δ),
organizations can release aggregate statistics, train ML models, and share datasets
while bounding individual privacy loss. This skill covers implementing DP mechanisms,
managing privacy budgets, and applying DP to common analytical queries.

Mode: `[MODE: PRIVACY]` — Privacy-preserving data analysis.

## Prerequisites

- Understanding of ε-differential privacy and (ε,δ)-differential privacy definitions
- Knowledge of sensitivity analysis for queries (global vs. local sensitivity)
- Python 3.10+ with numpy for noise generation
- Optional: Google DP library, OpenDP, or PipelineDP

## Key Concepts

### Differential Privacy Definition

```text
A randomized mechanism M satisfies ε-differential privacy if for all
datasets D₁, D₂ differing in one record, and all outputs S:

    Pr[M(D₁) ∈ S] ≤ e^ε × Pr[M(D₂) ∈ S]

Lower ε = stronger privacy (more noise)
Higher ε = weaker privacy (less noise, more utility)

Common ε values:
  ε = 0.1  — very strong privacy (Census Bureau)
  ε = 1.0  — strong privacy (typical for sensitive data)
  ε = 5.0  — moderate privacy (less sensitive analytics)
  ε = 10.0 — weak privacy (minimal protection)
```

### Noise Mechanisms

| Mechanism | Distribution | Use Case | Privacy |
|-----------|-------------|----------|---------|
| Laplace | Laplace(0, Δf/ε) | Counting, sums | ε-DP |
| Gaussian | N(0, (Δf/ε)²·2ln(1.25/δ)) | Floating-point | (ε,δ)-DP |
| Exponential | exp(εu(x)/2Δu) | Categorical selection | ε-DP |
| Randomized Response | Coin flip probability | Surveys, local DP | ε-DP |

### Privacy Budget Management

```text
Composition Theorem:
  - Sequential: ε_total = ε₁ + ε₂ + ... + εₙ
  - Parallel: ε_total = max(ε₁, ε₂, ..., εₙ) (disjoint subsets)
  - Advanced: Rényi DP or moments accountant for tighter bounds
```

### CLI Commands

```bash
# Add Laplace noise to a count query
node scripts/agent.js --action laplace \
  --true-value 1523 --sensitivity 1 --epsilon 1.0

# Add Gaussian noise
node scripts/agent.js --action gaussian \
  --true-value 45.7 --sensitivity 1.0 --epsilon 1.0 --delta 1e-5

# Manage privacy budget
node scripts/agent.js --action budget \
  --total-epsilon 5.0 --queries '[{"name":"count","epsilon":1.0},{"name":"mean","epsilon":2.0}]'

# Apply DP to a histogram
node scripts/agent.js --action histogram \
  --values "10,25,30,15,20" --epsilon 1.0
```

## Workflow

### Step 1: Determine Query Sensitivity

```bash
node scripts/agent.js --action sensitivity \
  --query-type "count" --bounds "0,100"
```

### Step 2: Choose Epsilon and Mechanism

```bash
node scripts/agent.js --action laplace \
  --true-value 1523 --sensitivity 1 --epsilon 1.0
```

### Step 3: Track Privacy Budget

```bash
node scripts/agent.js --action budget \
  --total-epsilon 5.0 \
  --queries '[{"name":"count_users","epsilon":1.0},{"name":"avg_age","epsilon":2.0}]'
```

### Step 4: Validate Utility vs. Privacy Trade-off

Assess whether noised results remain useful for the intended analytical purpose.

## Verification

- [ ] ε value appropriate for data sensitivity
- [ ] Query sensitivity correctly calculated
- [ ] Noise mechanism matches query type (Laplace for counts, Gaussian for floats)
- [ ] Privacy budget tracked across all queries (composition theorem)
- [ ] Budget not exceeded — no more queries after exhaustion
- [ ] Post-processing does not introduce new privacy leakage
- [ ] Utility assessment confirms results are actionable

## References

- [Dwork & Roth — The Algorithmic Foundations of Differential Privacy](https://www.cis.upenn.edu/~aaroth/Papers/privacybook.pdf)
- [Google Differential Privacy Library](https://github.com/google/differential-privacy)
- [OpenDP Project](https://opendp.org/)
- [US Census Bureau DP Deployment](https://www.census.gov/programs-surveys/decennial-census/decade/2020/planning-management/process/disclosure-avoidance.html)
- [Apple Differential Privacy Overview](https://www.apple.com/privacy/docs/Differential_Privacy_Overview.pdf)
