{"id":19390355,"url":"https://github.com/nucypher/heaan.jl","last_synced_at":"2026-03-03T13:32:39.374Z","repository":{"id":101544158,"uuid":"180084714","full_name":"nucypher/HEAAN.jl","owner":"nucypher","description":"Implementation of HEAAN in Julia","archived":false,"fork":false,"pushed_at":"2019-12-20T08:15:31.000Z","size":385,"stargazers_count":2,"open_issues_count":15,"forks_count":2,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-01-07T09:43:26.869Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://nucypher.github.io/HEAAN.jl/","language":"Julia","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nucypher.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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":"2019-04-08T06:31:59.000Z","updated_at":"2022-11-11T05:06:04.000Z","dependencies_parsed_at":"2024-05-11T06:15:33.965Z","dependency_job_id":null,"html_url":"https://github.com/nucypher/HEAAN.jl","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nucypher%2FHEAAN.jl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nucypher%2FHEAAN.jl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nucypher%2FHEAAN.jl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nucypher%2FHEAAN.jl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nucypher","download_url":"https://codeload.github.com/nucypher/HEAAN.jl/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240557489,"owners_count":19820358,"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-11-10T10:20:30.077Z","updated_at":"2026-03-03T13:32:34.341Z","avatar_url":"https://github.com/nucypher.png","language":"Julia","funding_links":[],"categories":[],"sub_categories":[],"readme":"# A Julia implementation of the HEAAN scheme\n\nMaster branch: [![CircleCI](https://circleci.com/gh/nucypher/HEAAN.jl.svg?style=svg)](https://circleci.com/gh/nucypher/HEAAN.jl) [![codecov](https://codecov.io/gh/nucypher/HEAAN.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/nucypher/HEAAN.jl)\n\n\n## Sources\n\nThis is an implementation of:\n\nJ. H. Cheon, A. Kim, M. Kim, and Y. Song, *\"Homomorphic Encryption for Arithmetic of Approximate Numbers,\"* [Lecture Notes in Computer Science, Vol. 10624 LNCS, pp. 409–437 (2017)](https://doi.org/10.1007/978-3-319-70694-8_15),\n\nand partially\n\nJ. H. Cheon, K. Han, A. Kim, M. Kim, and Y. Song, *\"Improved Bootstrapping for Approximate Homomorphic Encryption,\"* [eprint 2018/1043](https://eprint.iacr.org/2018/1043).\n\nFor the further development of this scheme one can refer to\n\nJ. H. Cheon, K. Han, A. Kim, M. Kim, and Y. Song, *\"A Full RNS Variant of Approximate Homomorphic Encryption,\"* [eprint 2018/931](https://eprint.iacr.org/2018/931.pdf).\n\nThe implementation is based on the [reference C++ code](https://github.com/snucrypto/HEAAN).\n\n\n## A simple example\n\nThe following example illustrates basics of working with the library.\nFor more examples see `examples/basic.jl` and `test/api.test.jl` in the repository.\n\nFirst, we import the required libraries and initialize constants:\n\n```julia\nusing Random\nusing HEAAN\n\n# Polynomial length and full modulus determine the security of the scheme\nparams = Params(log_polynomial_length=8, log_lo_modulus=300)\n\n# Vector size\n# The maximum vector size is polynomial_length / 2\nn = 2^6\n\n# Initial precision for ciphertexts (that is, the absolute precision is 1/2^30)\nlog_precision = 30\n\n# Initial precision cap for ciphertexts\n# Gets consumed during e.g. multiplication\nlog_cap = 200\n\nrng = MersenneTwister(123)\n```\n\nThen we need to create keys. A secret key is required to decrypt ciphertexts and initialize public keys.\nThere are several different public keys in HEAAN; in this example we will only use two: the one required for encryption, and the one required for multiplication.\nAddition of ciphertexts can be performed without a key.\n\n```julia\nsecret_key = SecretKey(rng, params)\n\n# Public key used for encryption\nenc_key = EncryptionKey(rng, secret_key)\n\n# Public key used for multiplication\nmul_key = MultiplicationKey(rng, secret_key)\n```\n\nWe create two complex-valued vectors of length `n` and initialize the reference array.\n\n```julia\nv1 = rand(rng, n) + im * rand(rng, n)\nv2 = rand(rng, n) + im * rand(rng, n)\n\n# Reference calculation\nref = x .* y .+ x\n```\n\nUse `enc_key` to create initial ciphertexts.\n`log_precision` specifies the absolute precision for encrypted values; `log_cap` is essentially the \"resource\" that gets spent during arithmetic operations on ciphertexts.\nNaturally, `log_cap` should be greater than `log_precision`.\n\n```julia\n# Encrypt the initial vectors\nc1 = encrypt(rng, enc_key, v1, log_precision, log_cap)\nc2 = encrypt(rng, enc_key, v2, log_precision, log_cap)\n\nprintln(\"Before: precision=$(c1.log_precision), cap=$(c1.log_cap)\")\n\n# output\nBefore: precision=30, cap=200\n```\n\nWe start from performing the (elementwise) multiplication.\n\n```julia\nt1 = mul(mul_key, c1, c2)\n\nprintln(\"After multiplication: precision=$(t1.log_precision), cap=$(t1.log_cap)\")\n\n# output\nAfter multiplication: precision=60, cap=200\n```\n\nNote that the precision of the result is the sum of the precisions of ciphertexts, while the cap remained the same.\nThis means that you cannot just multiply indefinitely --- after several iterations you will lose the encrypted information.\nThis problem is solved by `bootstrap()` which increases the difference between `precision` and `log_cap`, essentially adding the \"computation resource\".\n\nAnother consequence of this is that now `t1` and `c1` which we want to add together have different `log_cap`.\nIn order to `add()` them, both their `log_cap` and `log_precision` must be equal.\nThere are two functions that help with that: `rescale_by()` and `mod_down_by()`.\nThe former decreases both `log_cap` and `log_precision` by the same amount; the latter just decreases `log_cap`.\nAs it happens, in our case we need both of them.\n\n```julia\nt2 = rescale_by(t1, 30)\n\nprintln(\"After rescale: precision=$(t2.log_precision), cap=$(t2.log_cap)\")\n\nt3 = mod_down_by(c1, 30)\n\nprintln(\"After mod_down: precision=$(t3.log_precision), cap=$(t3.log_cap)\")\n\n# output\nAfter rescale: precision=30, cap=170\nAfter mod_down: precision=30, cap=170\n```\n\nNow that the parameters are equalized, we can call `add()` and decrypt the result.\n\n```julia\ncres = add(t2, t3)\n\nres = decrypt(secret_key, cres)\nref = reference(v1, v2)\n\nfor i in 1:n\n    println(\"$i-th element: diff=$(abs(res[i] - ref[i]))\")\nend\n\n# output\n1-th element: diff=1.8071040828704262e-8\n2-th element: diff=4.8677934407457675e-8\n3-th element: diff=3.612811639903806e-8\n4-th element: diff=2.9650269405023588e-8\n...\n```\n\nAs you can see, the calculation is accurate to about 25 bits.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnucypher%2Fheaan.jl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnucypher%2Fheaan.jl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnucypher%2Fheaan.jl/lists"}