{"id":13704510,"url":"https://github.com/IBM/discrete-gaussian-differential-privacy","last_synced_at":"2025-05-05T09:33:53.102Z","repository":{"id":66064717,"uuid":"272830041","full_name":"IBM/discrete-gaussian-differential-privacy","owner":"IBM","description":"Code for Canonne-Kamath-Steinke paper https://arxiv.org/abs/2004.00010","archived":true,"fork":false,"pushed_at":"2020-06-16T23:18:13.000Z","size":20,"stargazers_count":59,"open_issues_count":1,"forks_count":18,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-12-20T23:33:24.339Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/IBM.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-06-16T23:06:41.000Z","updated_at":"2024-08-20T19:02:22.000Z","dependencies_parsed_at":"2023-02-20T18:31:18.909Z","dependency_job_id":null,"html_url":"https://github.com/IBM/discrete-gaussian-differential-privacy","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IBM%2Fdiscrete-gaussian-differential-privacy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IBM%2Fdiscrete-gaussian-differential-privacy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IBM%2Fdiscrete-gaussian-differential-privacy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IBM%2Fdiscrete-gaussian-differential-privacy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/IBM","download_url":"https://codeload.github.com/IBM/discrete-gaussian-differential-privacy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252471724,"owners_count":21753239,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T21:01:11.163Z","updated_at":"2025-05-05T09:33:53.093Z","avatar_url":"https://github.com/IBM.png","language":"Python","funding_links":[],"categories":["Tools"],"sub_categories":["Privacy"],"readme":"# The Discrete Gaussian for Differential Privacy\n\nThis repository consists of code for the following paper.\n\n\u003e[Clément Canonne, Gautam Kamath, Thomas Steinke. _The Discrete Gaussian for Differential Privacy_. 2020.](https://arxiv.org/abs/2004.00010)\n\nIn particular, the plots in the paper are generated by the script `plots.py`. The code is implemented in Python 3.\n\nWe hope that the sampling code and other utilities will be useful to others.\n\n## Sampling\n\nThe main file is `discretegauss.py`. This includes the exact sampling methods.\n\nThe function `sample_dgauss(sigma2)` will generate one sample from the discrete Gaussian. Note that the argument to the function is the square of the scale, rather than the scale parameter. \n\nTo generate a sample from the discrete Laplace distribution use `discretegauss.sample_dlaplace(scale)` instead.\n\nBoth functions use [`random.SystemRandom()` ](https://docs.python.org/3/library/random.html#random.SystemRandom) to obtain high-quality randomn bits (on unix this should use `/dev/urandom`). A different random number generator may be specified via the optional argument `rng`. The parameters sigma2 and scale are cast to a rational number ([`fractions.Fraction`](https://docs.python.org/3/library/fractions.html)) to allow exact arithmetic.\n\n_Testing:_ Simply executing `python3 discretegauss.py` will run a basic test. A more thorough and sophisticated test is provided by the script `testing-kolmogorov-discretegaussian.py`, but be warned that this will take several hours to run.\n\n## Differential Privacy\n\nThe file `cdp2adp.py` contains utilities for analyzing the differential privacy properties of the discrete Gaussian and, more generally, algorithms satisfying concentrated differential privacy. Unlike the sampling methods, these functions use floating point arithmetic and are, therefore, not exact.\n\nThe function `dg_delta(sigma2,eps)` computes the smallest delta such that adding discrete Gaussian noise with parameter sigma2 to a sensitivity-1 function (e.g., a counting query) provides (eps,delta)-differential privacy; `cg_delta(sigma2,eps)` performs the equivalent calculation for continuous Gaussian noise.\n\nMore generally, `cdp_delta(rho,eps)` computes a delta such that any algorithm satisfying rho-concentrated differential privacy satisfies (eps,delta)-differential privacy. (Recall that adding discrete or continuous Gaussian noise with parameter sigma2 to a sensitivity-1 function provides rho-concentrated differential privacy for rho=1/(2*sigma2).) For comparison, the standard bound `cdp_delta_standard(rho,eps)` is also provided.\nAlternatively, given rho and delta, `cdp_eps(rho,delta)` computes a eps value such that rho-concentrated differential privacy implies (eps,delta)-differential privacy. Similarly, `cdp_rho(eps,delta)` computes a rho such that rho-concentrated differential privacy implies (eps,delta)-differential privacy.\n\n## Example\n\nThe following example code shows how to release several counts in a (1,10^-6)-differentially private manner.\n\n```python\nimport math\nfrom fractions import Fraction\n\nimport discretegauss\nimport cdp2adp\n\n#load some data, one line = one person\ndata = [x for x in open(\"mydata.txt\")]\n#define some queries, count the number of rows that contain search strings\nsearch_strings = [\"Asparagus\",\"Broccoli\",\"Carrot\"]\n\n#set overall DP parameters\neps=1\ndelta=1e-6\n#convert to concentrated DP\nrho=cdp2adp.cdp_rho(eps,delta)\nprint(str(rho)+\"-CDP implies (\"+str(eps)+\",\"+str(delta)+\")-DP\")\n#number of queries\nk=len(search_strings)\n#divide privacy budget up amongst queries\n#Each query needs to be (rho/k)-concentrated DP\n#cast to Fraction so subsequent arithmetic is exact\nrho_per_q = Fraction(rho)/k \n#compute noise variance parameter per query\nsigma2=1/(2*rho_per_q)\n#actual variance, at most sigma2\nvar = discretegauss.variance(sigma2)\nprint(\"standard deviation for each count = \"+str(math.sqrt(var)))\n\n#evaluate queries and privatize\nfor q in search_strings:\n    #count number of people whose data string contains term q\n    true_ans = sum(1 if q in x else 0 for x in data)\n    assert isinstance(true_ans,int) #important that it is rounded to an integer\n    #sample noise and add\n    noise = discretegauss.sample_dgauss(sigma2)\n    privatized_ans = true_ans + noise\n    #output\n    print(str(privatized_ans)+\"\\t\"+q)\n```\n\n## Disclaimer\n\nThis is research code.\n\n## Licence\n\n[Apache](https://www.apache.org/licenses/LICENSE-2.0)\n\n© Copyright IBM Corp. 2020\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FIBM%2Fdiscrete-gaussian-differential-privacy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FIBM%2Fdiscrete-gaussian-differential-privacy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FIBM%2Fdiscrete-gaussian-differential-privacy/lists"}