diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..d60f070 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,7 @@ +# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates +version: 2 +updates: + - package-ecosystem: "github-actions" + directory: "/" # Location of package manifests + schedule: + interval: "monthly" diff --git a/.github/workflows/CompatHelper.yml b/.github/workflows/CompatHelper.yml new file mode 100644 index 0000000..98e1e3b --- /dev/null +++ b/.github/workflows/CompatHelper.yml @@ -0,0 +1,45 @@ +name: CompatHelper +on: + schedule: + - cron: 0 0 * * * + workflow_dispatch: +permissions: + contents: write + pull-requests: write +jobs: + CompatHelper: + runs-on: ubuntu-latest + steps: + - name: Check if Julia is already available in the PATH + id: julia_in_path + run: which julia + continue-on-error: true + - name: Install Julia, but only if it is not already available in the PATH + uses: julia-actions/setup-julia@v1 + with: + version: '1' + arch: ${{ runner.arch }} + if: steps.julia_in_path.outcome != 'success' + - name: "Add the General registry via Git" + run: | + import Pkg + ENV["JULIA_PKG_SERVER"] = "" + Pkg.Registry.add("General") + shell: julia --color=yes {0} + - name: "Install CompatHelper" + run: | + import Pkg + name = "CompatHelper" + uuid = "aa819f21-2bde-4658-8897-bab36330d9b7" + version = "3" + Pkg.add(; name, uuid, version) + shell: julia --color=yes {0} + - name: "Run CompatHelper" + run: | + import CompatHelper + CompatHelper.main(; subdirs=["", "test", "docs"]) + shell: julia --color=yes {0} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + COMPATHELPER_PRIV: ${{ secrets.DOCUMENTER_KEY }} + # COMPATHELPER_PRIV: ${{ secrets.COMPATHELPER_PRIV }} diff --git a/.github/workflows/Documenter.yml b/.github/workflows/Documenter.yml new file mode 100644 index 0000000..3cfbcdb --- /dev/null +++ b/.github/workflows/Documenter.yml @@ -0,0 +1,41 @@ +name: Documentation + +on: + push: + branches: + - 'main' + tags: '*' + paths-ignore: + - '.github/workflows/ci.yml' + - '.github/workflows/CompatHelper.yml' + - '.github/workflows/TagBot.yml' + pull_request: + paths-ignore: + - '.github/workflows/ci.yml' + - '.github/workflows/CompatHelper.yml' + - '.github/workflows/TagBot.yml' + workflow_dispatch: + +concurrency: + # Skip intermediate builds: always. + # Cancel intermediate builds: only if it is a pull request build. + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: ${{ startsWith(github.ref, 'refs/pull/') }} + +jobs: + build-docs: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: julia-actions/setup-julia@v1 + with: + version: '1' + show-versioninfo: true + - uses: julia-actions/julia-buildpkg@v1 + - name: Install dependencies + run: julia --project=docs/ -e 'using Pkg; Pkg.develop(PackageSpec(path=pwd())); Pkg.instantiate()' + - name: Build and deploy + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + DOCUMENTER_KEY: ${{ secrets.DOCUMENTER_KEY }} # For authentication with SSH deploy key + run: julia --project=docs --color=yes docs/make.jl diff --git a/.github/workflows/SpellCheck.yml b/.github/workflows/SpellCheck.yml new file mode 100644 index 0000000..fa3a3d2 --- /dev/null +++ b/.github/workflows/SpellCheck.yml @@ -0,0 +1,13 @@ +name: Spell Check + +on: [pull_request, workflow_dispatch] + +jobs: + typos-check: + name: Spell Check with Typos + runs-on: ubuntu-latest + steps: + - name: Checkout Actions Repository + uses: actions/checkout@v4 + - name: Check spelling + uses: crate-ci/typos@v1.17.0 diff --git a/.github/workflows/TagBot.yml b/.github/workflows/TagBot.yml new file mode 100644 index 0000000..90dc100 --- /dev/null +++ b/.github/workflows/TagBot.yml @@ -0,0 +1,33 @@ +name: TagBot +on: + issue_comment: + types: + - created + workflow_dispatch: + inputs: + lookback: + default: 3 +permissions: + actions: read + checks: read + contents: write + deployments: read + issues: read + discussions: read + packages: read + pages: read + pull-requests: read + repository-projects: read + security-events: read + statuses: read +jobs: + TagBot: + if: github.event_name == 'workflow_dispatch' || github.actor == 'JuliaTagBot' + runs-on: ubuntu-latest + steps: + - uses: JuliaRegistries/TagBot@v1 + with: + token: ${{ secrets.GITHUB_TOKEN }} + # Edit the following line to reflect the actual name of the GitHub Secret containing your private key + ssh: ${{ secrets.DOCUMENTER_KEY }} + # ssh: ${{ secrets.NAME_OF_MY_SSH_PRIVATE_KEY_SECRET }} diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..92dd33d --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,78 @@ +name: CI + +on: + push: + branches: + - main + tags: ['*'] + paths-ignore: + - 'CITATION.bib' + - 'LICENSE.md' + - 'README.md' + - '.zenodo.json' + - '.github/workflows/CompatHelper.yml' + - '.github/workflows/SpellCheck.yml' + - '.github/workflows/TagBot.yml' + - 'docs/**' + pull_request: + paths-ignore: + - 'CITATION.bib' + - 'LICENSE.md' + - 'README.md' + - '.zenodo.json' + - '.github/workflows/CompatHelper.yml' + - '.github/workflows/SpellCheck.yml' + - '.github/workflows/TagBot.yml' + - 'docs/**' + workflow_dispatch: + +concurrency: + # Skip intermediate builds: always. + # Cancel intermediate builds: only if it is a pull request build. + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: ${{ startsWith(github.ref, 'refs/pull/') }} + +jobs: + test: + name: ${{ matrix.os }} - Julia ${{ matrix.version }} - ${{ github.event_name }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + version: + - '1.8' + - '1.9' + - '1.10' + os: + - ubuntu-latest + - macOS-latest + - windows-latest + exclude: + # Include once OpenFHE.jl issues are fixed + - os: windows-latest + version: '1.8' + steps: + - uses: actions/checkout@v4 + - uses: julia-actions/setup-julia@v1 + with: + version: ${{ matrix.version }} + - run: julia -e 'using InteractiveUtils; versioninfo(verbose=true)' + - uses: julia-actions/cache@v1 + - uses: julia-actions/julia-buildpkg@v1 + - uses: julia-actions/julia-runtest@v1 + - uses: julia-actions/julia-processcoverage@v1 + with: + directories: src,examples + - uses: codecov/codecov-action@v3 + with: + files: lcov.info + token: ${{ secrets.CODECOV_TOKEN }} + - uses: coverallsapp/github-action@master + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + path-to-lcov: ./lcov.info + # Enable tmate debugging of manually-triggered workflows if the input option was provided + - name: Setup tmate session for debugging + if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.debug_enabled && always() }} + uses: mxschmitt/action-tmate@v3 + timeout-minutes: 15 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..359e63b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +Manifest.toml +run/ diff --git a/.zenodo.json b/.zenodo.json new file mode 100644 index 0000000..10f0cfb --- /dev/null +++ b/.zenodo.json @@ -0,0 +1,14 @@ +{ + "title": "SecureArithmetic.jl", + "description": "Secure arithmetic operations in Julia using fully homomorphic encryption", + "license": "MIT", + "upload_type": "software", + "creators": [ + { + "affiliation": "Applied and Computational Mathematics, RWTH Aachen University, Germany", + "name": "Schlottke-Lakemper, Michael", + "orcid": "0000-0002-3195-2536" + } + ], + "access_right": "open" +} diff --git a/CITATION.bib b/CITATION.bib new file mode 100644 index 0000000..80b18d2 --- /dev/null +++ b/CITATION.bib @@ -0,0 +1,7 @@ +@misc{schlottkelakemper2024securearithmetic, + title={{S}ecure{A}rithmetic.jl: {S}ecure arithmetic operations in {J}ulia using fully homomorphic encryption}, + author={Schlottke-Lakemper, Michael}, + year={2024}, + howpublished={\url{https://github.com/sloede/SecureArithmetic.jl}} +} + diff --git a/Project.toml b/Project.toml index 6c5df7b..afacf78 100644 --- a/Project.toml +++ b/Project.toml @@ -2,3 +2,9 @@ name = "SecureArithmetic" uuid = "38cee09b-6d7e-4d21-866e-807a7f642fe9" authors = ["Michael Schlottke-Lakemper "] version = "0.1.0" + +[deps] +OpenFHE = "77ce9b8e-ecf5-45d1-bd8a-d31f384f2f95" + +[compat] +julia = "1.8" diff --git a/README.md b/README.md index 827f868..d73be2e 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,94 @@ [![License: MIT](https://img.shields.io/badge/License-MIT-success.svg)](https://opensource.org/license/mit/) SecureArithmetic.jl is a Julia package for performing cryptographically secure arithmetic -operations using fully homomorphic encryption. +operations using fully homomorphic encryption. It currently provides a backend for +OpenFHE-secured computations using [OpenFHE.jl](https://github.com/sloede/OpenFHE.jl), and +an unencrypted backend for fast verification of a computation pipeline. + + +## Getting started + +### Prerequisites +If you have not yet installed Julia, please [follow the instructions for your +operating system](https://julialang.org/downloads/platform/). +[SecureArithmetic.jl](https://github.com/sloede/SecureArithmetic.jl) works with Julia v1.8 +and later on Linux and macOS platforms, and with Julia v1.9 or later on Windows platforms. + +### Installation +Since SecureArithmetic.jl is not yet a registered Julia package, you can install it by +executing the following commands in the Julia REPL: +```julia +julia> import Pkg; Pkg.add("https://github.com/sloede/SecureArithmetic.jl") +``` +If you plan on running the examples in the +[`examples`](https://github.com/sloede/SecureArithmetic.jl/tree/main/examples) directory, +you also need to install OpenFHE.jl: +```julia +julia> import Pkg; Pkg.add("OpenFHE") +``` + +### Usage +The easiest way to get started is to run one of the examples from the +[`examples`](https://github.com/sloede/SecureArithmetic.jl/tree/main/examples) directory by +`include`ing them in Julia, e.g., +```julia +julia> using SecureArithmetic + +julia> include(joinpath(pkgdir(SecureArithmetic), "examples", "simple_real_numbers.jl")) +================================================================================ +Creating OpenFHE context... +CKKS scheme is using ring dimension 16384 + +================================================================================ +Creating unencrypted context... +================================================================================ +simple_real_numbers with an OpenFHE context +Input x1: (0.25, 0.5, 0.75, 1, 2, 3, 4, 5, ... ); Estimated precision: 50 bits + +Input x2: (5, 4, 3, 2, 1, 0.75, 0.5, 0.25, ... ); Estimated precision: 50 bits + + +Results of homomorphic computations: +x1 = (0.25, 0.5, 0.75, 1, 2, 3, 4, 5, ... ); Estimated precision: 43 bits + +x1 + x2 = (5.25, 4.5, 3.75, 3, 3, 3.75, 4.5, 5.25, ... ); Estimated precision: 43 bits + +x1 - x2 = (-4.75, -3.5, -2.25, -1, 1, 2.25, 3.5, 4.75, ... ); Estimated precision: 43 bits + +4 * x1 = (1, 2, 3, 4, 8, 12, 16, 20, ... ); Estimated precision: 41 bits + +x1 * x2 = (1.25, 2, 2.25, 2, 2, 2.25, 2, 1.25, ... ); Estimated precision: 41 bits + +x1 shifted circularly by -1 = (0.5, 0.75, 1, 2, 3, 4, 5, 0.25, ... ); Estimated precision: 43 bits + +x1 shifted circularly by 2 = (4, 5, 0.25, 0.5, 0.75, 1, 2, 3, ... ); Estimated precision: 43 bits + +================================================================================ +simple_real_numbers with an Unencrypted context +Input x1: [0.25, 0.5, 0.75, 1.0, 2.0, 3.0, 4.0, 5.0] +Input x2: [5.0, 4.0, 3.0, 2.0, 1.0, 0.75, 0.5, 0.25] + +Results of homomorphic computations: +x1 = [0.25, 0.5, 0.75, 1.0, 2.0, 3.0, 4.0, 5.0] +x1 + x2 = [5.25, 4.5, 3.75, 3.0, 3.0, 3.75, 4.5, 5.25] +x1 - x2 = [-4.75, -3.5, -2.25, -1.0, 1.0, 2.25, 3.5, 4.75] +4 * x1 = [1.0, 2.0, 3.0, 4.0, 8.0, 12.0, 16.0, 20.0] +x1 * x2 = [1.25, 2.0, 2.25, 2.0, 2.0, 2.25, 2.0, 1.25] +x1 shifted circularly by -1 = [0.5, 0.75, 1.0, 2.0, 3.0, 4.0, 5.0, 0.25] +x1 shifted circularly by 2 = [4.0, 5.0, 0.25, 0.5, 0.75, 1.0, 2.0, 3.0] +``` + + +## Referencing +If you use SecureArithmetic.jl in your own research, please cite this repository as follows: +```bibtex +@misc{schlottkelakemper2024securearithmetic, + title={{S}ecure{A}rithmetic.jl: {S}ecure arithmetic operations in {J}ulia using fully homomorphic encryption}, + author={Schlottke-Lakemper, Michael}, + year={2024}, + howpublished={\url{https://github.com/sloede/SecureArithmetic.jl}} +} +``` ## Authors diff --git a/docs/.gitignore b/docs/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/docs/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/docs/Project.toml b/docs/Project.toml new file mode 100644 index 0000000..1814eb3 --- /dev/null +++ b/docs/Project.toml @@ -0,0 +1,5 @@ +[deps] +Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" + +[compat] +Documenter = "1" diff --git a/docs/make.jl b/docs/make.jl new file mode 100644 index 0000000..c18b766 --- /dev/null +++ b/docs/make.jl @@ -0,0 +1,73 @@ +using Documenter + +# Get SecureArithmetic.jl root directory +securearithmetic_root_dir = dirname(@__DIR__) + +# Fix for https://github.com/trixi-framework/Trixi.jl/issues/668 +if (get(ENV, "CI", nothing) != "true") && (get(ENV, "SECUREARITHMETIC_DOC_DEFAULT_ENVIRONMENT", nothing) != "true") + push!(LOAD_PATH, securearithmetic_root_dir) +end + +using SecureArithmetic + +# Define module-wide setups such that the respective modules are available in doctests +DocMeta.setdocmeta!(SecureArithmetic, :DocTestSetup, :(using SecureArithmetic); recursive=true) + +# Copy some files from the top level directory to the docs and modify them +# as necessary +open(joinpath(@__DIR__, "src", "index.md"), "w") do io + # Point to source file + println(io, """ + ```@meta + EditURL = "https://github.com/sloede/SecureArithmetic.jl/blob/main/README.md" + ``` + """) + # Write the modified contents + for line in eachline(joinpath(securearithmetic_root_dir, "README.md")) + line = replace(line, "[LICENSE.md](LICENSE.md)" => "[License](@ref)") + println(io, line) + end +end + +open(joinpath(@__DIR__, "src", "license.md"), "w") do io + # Point to source file + println(io, """ + ```@meta + EditURL = "https://github.com/sloede/SecureArithmetic/blob/main/LICENSE.md" + ``` + """) + # Write the modified contents + println(io, "# License") + println(io, "") + for line in eachline(joinpath(securearithmetic_root_dir, "LICENSE.md")) + println(io, "> ", line) + end +end + +# Make documentation +makedocs( + # Specify modules for which docstrings should be shown + modules = [SecureArithmetic], + # Set sitename to Trixi.jl + sitename="SecureArithmetic.jl", + # Provide additional formatting options + format = Documenter.HTML( + # Disable pretty URLs during manual testing + prettyurls = get(ENV, "CI", nothing) == "true", + # Set canonical URL to GitHub pages URL + canonical = "https://securearithmetic-jl.lakemper.eu/stable" + ), + # Explicitly specify documentation structure + pages = [ + "Home" => "index.md", + "API reference" => "reference.md", + "License" => "license.md" + ], +) + + +deploydocs(; + repo = "github.com/sloede/SecureArithmetic.jl", + devbranch = "main", + push_preview = true +) diff --git a/docs/src/.gitignore b/docs/src/.gitignore new file mode 100644 index 0000000..312f831 --- /dev/null +++ b/docs/src/.gitignore @@ -0,0 +1,2 @@ +index.md +license.md diff --git a/docs/src/reference.md b/docs/src/reference.md new file mode 100644 index 0000000..0f49988 --- /dev/null +++ b/docs/src/reference.md @@ -0,0 +1,9 @@ +# API reference + +```@meta +CurrentModule = SecureArithmetic +``` + +```@autodocs +Modules = [SecureArithmetic] +``` diff --git a/examples/simple_real_numbers.jl b/examples/simple_real_numbers.jl new file mode 100644 index 0000000..ba37a1b --- /dev/null +++ b/examples/simple_real_numbers.jl @@ -0,0 +1,101 @@ +using SecureArithmetic +using OpenFHE + +function simple_real_numbers(context) + public_key, private_key = generate_keys(context) + + init_multiplication(context, private_key) + init_rotation(context, private_key, [1, -2]) + + + x1 = [0.25, 0.5, 0.75, 1.0, 2.0, 3.0, 4.0, 5.0] + x2 = [5.0, 4.0, 3.0, 2.0, 1.0, 0.75, 0.5, 0.25] + + pv1 = PlainVector(context, x1) + pv2 = PlainVector(context, x2) + + println("Input x1: ", pv1) + println("Input x2: ", pv2) + + sv1 = encrypt(context, public_key, pv1) + sv2 = encrypt(context, public_key, pv2) + + sv_add = sv1 + sv2 + + sv_sub = sv1 - sv2 + + sv_scalar = sv1 * 4.0 + + sv_mult = sv1 * sv2 + + sv_shift1 = circshift(sv1, -1) + sv_shift2 = circshift(sv1, 2) + + + println() + println("Results of homomorphic computations: ") + + result_sv1 = decrypt(context, private_key, sv1) + println("x1 = ", result_sv1) + + result_sv_add = decrypt(context, private_key, sv_add) + println("x1 + x2 = ", result_sv_add) + + result_sv_sub = decrypt(context, private_key, sv_sub) + println("x1 - x2 = ", result_sv_sub) + + result_sv_scalar = decrypt(context, private_key, sv_scalar) + println("4 * x1 = ", result_sv_scalar) + + result_sv_mult = decrypt(context, private_key, sv_mult) + println("x1 * x2 = ", result_sv_mult) + + result_sv_shift1 = decrypt(context, private_key, sv_shift1) + println("x1 shifted circularly by -1 = ", result_sv_shift1) + + result_sv_shift2 = decrypt(context, private_key, sv_shift2) + println("x1 shifted circularly by 2 = ", result_sv_shift2) +end + + +################################################################################ +println("="^80) +println("Creating OpenFHE context...") + +multiplicative_depth = 1 +scaling_modulus = 50 +batch_size = 8 + +parameters = CCParams{CryptoContextCKKSRNS}() +SetMultiplicativeDepth(parameters, multiplicative_depth) +SetScalingModSize(parameters, scaling_modulus) +SetBatchSize(parameters, batch_size) + +cc = GenCryptoContext(parameters) +Enable(cc, PKE) +Enable(cc, KEYSWITCH) +Enable(cc, LEVELEDSHE) +println("CKKS scheme is using ring dimension ", GetRingDimension(cc)) +println() + +context_openfhe = SecureContext(OpenFHEBackend(cc)) + + +################################################################################ +println("="^80) +println("Creating unencrypted context...") + +context_unencrypted = SecureContext(Unencrypted()) + + +################################################################################ +println("="^80) +println("simple_real_numbers with an OpenFHE context") +simple_real_numbers(context_openfhe) + + +################################################################################ +println("="^80) +println("simple_real_numbers with an Unencrypted context") +simple_real_numbers(context_unencrypted) + diff --git a/src/SecureArithmetic.jl b/src/SecureArithmetic.jl index 54ff8fc..dc8ca8c 100644 --- a/src/SecureArithmetic.jl +++ b/src/SecureArithmetic.jl @@ -1,5 +1,19 @@ module SecureArithmetic -greet() = print("Hello World!") +using OpenFHE: OpenFHE + +# Basic types +export SecureContext, SecureVector, PlainVector + +# Backends +export Unencrypted, OpenFHEBackend + +# Crypto operations +export generate_keys, init_multiplication, init_rotation, encrypt, decrypt + +include("types.jl") +include("openfhe.jl") +include("unencrypted.jl") +include("arithmetic.jl") end # module SecureArithmetic diff --git a/src/arithmetic.jl b/src/arithmetic.jl new file mode 100644 index 0000000..0662f4c --- /dev/null +++ b/src/arithmetic.jl @@ -0,0 +1,15 @@ +Base.:+(sv1::SecureVector, sv2::SecureVector) = add(sv1, sv2) +Base.:+(sv::SecureVector, pv::PlainVector) = add(sv, pv) +Base.:+(pv::PlainVector, sv::SecureVector) = add(sv, pv) + +Base.:-(sv1::SecureVector, sv2::SecureVector) = subtract(sv1, sv2) +Base.:-(sv::SecureVector, pv::PlainVector) = subtract(sv, pv) +Base.:-(pv::PlainVector, sv::SecureVector) = subtract(pv, sv) + +Base.:*(sv1::SecureVector, sv2::SecureVector) = multiply(sv1, sv2) +Base.:*(sv::SecureVector, pv::PlainVector) = multiply(sv, pv) +Base.:*(pv::PlainVector, sv::SecureVector) = multiply(sv, pv) +Base.:*(sv::SecureVector, scalar::Real) = multiply(sv, scalar) +Base.:*(scalar::Real, sv::SecureVector) = multiply(sv, scalar) + +Base.circshift(sv::SecureVector, shift::Integer) = rotate(sv, shift) diff --git a/src/openfhe.jl b/src/openfhe.jl new file mode 100644 index 0000000..ed09a09 --- /dev/null +++ b/src/openfhe.jl @@ -0,0 +1,124 @@ +struct OpenFHEBackend{CryptoContextT} <: AbstractCryptoBackend + crypto_context::CryptoContextT +end + +function get_crypto_context(context::SecureContext{<:OpenFHEBackend}) + context.backend.crypto_context +end +function get_crypto_context(secure_vector::SecureVector{<:OpenFHEBackend}) + get_crypto_context(secure_vector.context) +end +function get_crypto_context(plain_vector::PlainVector{<:OpenFHEBackend}) + get_crypto_context(plain_vector.context) +end + +function generate_keys(context::SecureContext{<:OpenFHEBackend}) + cc = get_crypto_context(context) + keys = OpenFHE.KeyGen(cc) + public_key = PublicKey(context, OpenFHE.public_key(keys)) + private_key = PrivateKey(context, OpenFHE.private_key(keys)) + + public_key, private_key +end + +function init_multiplication(context::SecureContext{<:OpenFHEBackend}, private_key) + cc = get_crypto_context(context) + OpenFHE.EvalMultKeyGen(cc, private_key.private_key) + + nothing +end + +function init_rotation(context::SecureContext{<:OpenFHEBackend}, private_key, shifts) + cc = get_crypto_context(context) + OpenFHE.EvalRotateKeyGen(cc, private_key.private_key, shifts) + + nothing +end + +function PlainVector(context::SecureContext{<:OpenFHEBackend}, data::Vector{<:Real}) + cc = get_crypto_context(context) + plaintext = OpenFHE.MakeCKKSPackedPlaintext(cc, data) + plain_vector = PlainVector(plaintext, context) + + plain_vector +end + +function encrypt(context::SecureContext{<:OpenFHEBackend}, public_key, data::Vector{<:Real}) + plain_vector = PlainVector(context, data) + secure_vector = encrypt(context, public_key, plain_vector) + + secure_vector +end + +function encrypt(context::SecureContext{<:OpenFHEBackend}, public_key, + plain_vector::PlainVector) + cc = get_crypto_context(context) + ciphertext = OpenFHE.Encrypt(cc, public_key.public_key, plain_vector.plaintext) + secure_vector = SecureVector(ciphertext, context) + + secure_vector +end + +function decrypt!(plain_vector, context::SecureContext{<:OpenFHEBackend}, private_key, + secure_vector) + cc = get_crypto_context(context) + OpenFHE.Decrypt(cc, private_key.private_key, secure_vector.ciphertext, + plain_vector.plaintext) + + plain_vector +end + +function decrypt(context::SecureContext{<:OpenFHEBackend}, private_key, secure_vector) + plain_vector = PlainVector(OpenFHE.Plaintext(), context) + + decrypt!(plain_vector, context, private_key, secure_vector) +end + +function add(sv1::SecureVector{<:OpenFHEBackend}, sv2::SecureVector{<:OpenFHEBackend}) + cc = get_crypto_context(sv1) + ciphertext = OpenFHE.EvalAdd(cc, sv1.ciphertext, sv2.ciphertext) + secure_vector = SecureVector(ciphertext, sv1.context) + + secure_vector +end + +function subtract(sv1::SecureVector{<:OpenFHEBackend}, sv2::SecureVector{<:OpenFHEBackend}) + cc = get_crypto_context(sv1) + ciphertext = OpenFHE.EvalSub(cc, sv1.ciphertext, sv2.ciphertext) + secure_vector = SecureVector(ciphertext, sv1.context) + + secure_vector +end + +function multiply(sv1::SecureVector{<:OpenFHEBackend}, sv2::SecureVector{<:OpenFHEBackend}) + cc = get_crypto_context(sv1) + ciphertext = OpenFHE.EvalMult(cc, sv1.ciphertext, sv2.ciphertext) + secure_vector = SecureVector(ciphertext, sv1.context) + + secure_vector +end + +function multiply(sv::SecureVector{<:OpenFHEBackend}, pv::PlainVector{<:OpenFHEBackend}) + cc = get_crypto_context(sv) + ciphertext = OpenFHE.EvalMult(cc, sv.ciphertext, pv.plaintext) + secure_vector = SecureVector(ciphertext, sv.context) + + secure_vector +end + +function multiply(sv::SecureVector{<:OpenFHEBackend}, scalar::Real) + cc = get_crypto_context(sv) + ciphertext = OpenFHE.EvalMult(cc, sv.ciphertext, scalar) + secure_vector = SecureVector(ciphertext, sv.context) + + secure_vector +end + +function rotate(sv::SecureVector{<:OpenFHEBackend}, shift) + cc = get_crypto_context(sv) + # We use `-shift` to match Julia's usual `circshift` direction + ciphertext = OpenFHE.EvalRotate(cc, sv.ciphertext, -shift) + secure_vector = SecureVector(ciphertext, sv.context) + + secure_vector +end diff --git a/src/types.jl b/src/types.jl new file mode 100644 index 0000000..01e536a --- /dev/null +++ b/src/types.jl @@ -0,0 +1,43 @@ +abstract type AbstractCryptoBackend end + +struct SecureContext{CryptoBackendT <: AbstractCryptoBackend} + backend::CryptoBackendT +end + +struct SecureVector{CryptoBackendT <: AbstractCryptoBackend, CiphertextT} + ciphertext::CiphertextT + context::SecureContext{CryptoBackendT} + + function SecureVector(ciphertext, context::SecureContext{CryptoBackendT}) where CryptoBackendT + new{CryptoBackendT, typeof(ciphertext)}(ciphertext, context) + end +end + +struct PlainVector{CryptoBackendT <: AbstractCryptoBackend, PlaintextT} + plaintext::PlaintextT + context::SecureContext{CryptoBackendT} + + function PlainVector(plaintext, context::SecureContext{CryptoBackendT}) where CryptoBackendT + new{CryptoBackendT, typeof(plaintext)}(plaintext, context) + end +end + +Base.print(io::IO, plain_vector::PlainVector) = print(io, plain_vector.plaintext) + +struct PrivateKey{CryptoBackendT <: AbstractCryptoBackend, KeyT} + private_key::KeyT + context::SecureContext{CryptoBackendT} + + function PrivateKey(context::SecureContext{CryptoBackendT}, key) where CryptoBackendT + new{CryptoBackendT, typeof(key)}(key, context) + end +end + +struct PublicKey{CryptoBackendT <: AbstractCryptoBackend, KeyT} + public_key::KeyT + context::SecureContext{CryptoBackendT} + + function PublicKey(context::SecureContext{CryptoBackendT}, key) where CryptoBackendT + new{CryptoBackendT, typeof(key)}(key, context) + end +end diff --git a/src/unencrypted.jl b/src/unencrypted.jl new file mode 100644 index 0000000..1ff1f1d --- /dev/null +++ b/src/unencrypted.jl @@ -0,0 +1,58 @@ +struct Unencrypted <: AbstractCryptoBackend + # No data fields required +end + +function generate_keys(context::SecureContext{<:Unencrypted}) + PublicKey(context, nothing), PrivateKey(context, nothing) +end + +init_multiplication(context::SecureContext{<:Unencrypted}, private_key) = nothing +init_rotation(context::SecureContext{<:Unencrypted}, private_key, shifts) = nothing + +function PlainVector(context::SecureContext{<:Unencrypted}, data::Vector{<:Real}) + plain_vector = PlainVector(data, context) +end + +function encrypt(context::SecureContext{<:Unencrypted}, public_key, data::Vector{<:Real}) + SecureVector(data, context) +end + +function encrypt(context::SecureContext{<:Unencrypted}, public_key, + plain_vector::PlainVector) + SecureVector(plain_vector.plaintext, context) +end + +function decrypt!(plain_vector, context::SecureContext{<:Unencrypted}, private_key, + secure_vector) + plain_vector.plaintext .= secure_vector.ciphertext +end + +function decrypt(context::SecureContext{<:Unencrypted}, private_key, secure_vector) + plain_vector = PlainVector(similar(secure_vector.ciphertext), context) + + decrypt!(plain_vector, context, private_key, secure_vector) +end + +function add(sv1::SecureVector{<:Unencrypted}, sv2::SecureVector{<:Unencrypted}) + SecureVector(sv1.ciphertext .+ sv2.ciphertext, sv1.context) +end + +function subtract(sv1::SecureVector{<:Unencrypted}, sv2::SecureVector{<:Unencrypted}) + SecureVector(sv1.ciphertext .- sv2.ciphertext, sv1.context) +end + +function multiply(sv1::SecureVector{<:Unencrypted}, sv2::SecureVector{<:Unencrypted}) + SecureVector(sv1.ciphertext .* sv2.ciphertext, sv1.context) +end + +function multiply(sv::SecureVector{<:Unencrypted}, pv::PlainVector{<:Unencrypted}) + SecureVector(sv.ciphertext .* pv.plaintext, sv.context) +end + +function multiply(sv::SecureVector{<:Unencrypted}, scalar::Real) + SecureVector(sv.ciphertext .* scalar, sv.context) +end + +function rotate(sv::SecureVector{<:Unencrypted}, shift) + SecureVector(circshift(sv.ciphertext, shift), sv.context) +end diff --git a/test/Project.toml b/test/Project.toml new file mode 100644 index 0000000..fa49760 --- /dev/null +++ b/test/Project.toml @@ -0,0 +1,7 @@ +[deps] +OpenFHE = "77ce9b8e-ecf5-45d1-bd8a-d31f384f2f95" +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" + +[compat] +OpenFHE = "0.1" +Test = "1" diff --git a/test/runtests.jl b/test/runtests.jl new file mode 100644 index 0000000..c8217d2 --- /dev/null +++ b/test/runtests.jl @@ -0,0 +1,6 @@ +using Test + +@time @testset verbose=true showtiming=true "SecureArithmetic.jl tests" begin + include("test_examples.jl") +end + diff --git a/test/test_examples.jl b/test/test_examples.jl new file mode 100644 index 0000000..6120ed9 --- /dev/null +++ b/test/test_examples.jl @@ -0,0 +1,15 @@ +module TestExamples + +using Test +using SecureArithmetic + +@testset verbose=true showtiming=true "test_examples.jl" begin + +@testset verbose=true showtiming=true "examples/simple_real_numbers.jl" begin + @test_nowarn include("../examples/simple_real_numbers.jl") +end + +end # @testset "test_examples.jl" + +end # module +