From 14036630339d583c29579611b729fd131772c314 Mon Sep 17 00:00:00 2001 From: Arthur Meyre Date: Fri, 29 Mar 2024 17:01:03 +0100 Subject: [PATCH] chore(tfhe): add forward compatibility with 0.6 --- .github/workflows/aws_tfhe_fast_tests.yml | 10 +- .github/workflows/aws_tfhe_tests.yml | 10 +- Makefile | 24 +- apps/trivium/README.md | 2 +- scripts/c_api_tests.sh | 13 +- tfhe/Cargo.toml | 60 +- tfhe/build.rs | 14 +- tfhe/c_api_tests/CMakeLists.txt | 5 + tfhe/c_api_tests/test_forward_compatibility.c | 115 +++ tfhe/cbindgen.toml | 1 + tfhe/docs/core_crypto/tutorial.md | 8 +- tfhe/docs/getting_started/installation.md | 4 +- tfhe/docs/getting_started/quick_start.md | 2 +- tfhe/docs/how_to/migrate_data.md | 67 +- tfhe/docs/how_to/run_on_gpu.md | 4 +- tfhe/docs/how_to/serialization.md | 2 +- tfhe/docs/tutorials/ascii_fhe_string.md | 2 +- tfhe/docs/tutorials/parity_bit.md | 2 +- tfhe/src/c_api/high_level_api/integers.rs | 161 ++++ tfhe/src/c_api/high_level_api/keys.rs | 12 + tfhe/src/c_api/high_level_api/utils.rs | 65 ++ .../boolean/ciphertext/mod.rs | 64 ++ .../boolean/client_key/mod.rs | 33 + .../boolean/engine/bootstrapping/mod.rs | 69 ++ .../boolean/engine/mod.rs | 1 + .../boolean/key_switching_key/mod.rs | 38 + tfhe/src/forward_compatibility/boolean/mod.rs | 6 + .../boolean/parameters/mod.rs | 109 +++ .../boolean/public_key/mod.rs | 65 ++ .../commons/ciphertext_modulus/mod.rs | 41 + .../core_crypto/commons/dispersion/mod.rs | 27 + .../core_crypto/commons/math/mod.rs | 1 + .../core_crypto/commons/math/random/mod.rs | 45 + .../core_crypto/commons/mod.rs | 4 + .../core_crypto/commons/parameters/mod.rs | 790 ++++++++++++++++++ .../core_crypto/entities/cleartext/mod.rs | 34 + .../entities/ggsw_ciphertext/mod.rs | 61 ++ .../entities/ggsw_ciphertext_list/mod.rs | 67 ++ .../entities/glwe_ciphertext/mod.rs | 50 ++ .../entities/glwe_ciphertext_list/mod.rs | 58 ++ .../entities/glwe_secret_key/mod.rs | 43 + .../entities/lwe_bootstrap_key/mod.rs | 66 ++ .../entities/lwe_ciphertext/mod.rs | 42 + .../entities/lwe_ciphertext_list/mod.rs | 50 ++ .../entities/lwe_compact_public_key/mod.rs | 42 + .../entities/lwe_keyswitch_key/mod.rs | 57 ++ .../lwe_multi_bit_bootstrap_key/mod.rs | 157 ++++ .../entities/lwe_packing_keyswitch_key/mod.rs | 67 ++ .../mod.rs | 69 ++ .../mod.rs | 65 ++ .../entities/lwe_public_key/mod.rs | 50 ++ .../entities/lwe_secret_key/mod.rs | 38 + .../core_crypto/entities/mod.rs | 33 + .../core_crypto/entities/plaintext/mod.rs | 31 + .../entities/plaintext_list/mod.rs | 35 + .../core_crypto/entities/polynomial/mod.rs | 36 + .../entities/polynomial_list/mod.rs | 40 + .../entities/seeded_ggsw_ciphertext/mod.rs | 68 ++ .../seeded_ggsw_ciphertext_list/mod.rs | 72 ++ .../entities/seeded_glwe_ciphertext/mod.rs | 60 ++ .../seeded_glwe_ciphertext_list/mod.rs | 63 ++ .../entities/seeded_lwe_bootstrap_key/mod.rs | 64 ++ .../entities/seeded_lwe_ciphertext/mod.rs | 55 ++ .../seeded_lwe_ciphertext_list/mod.rs | 60 ++ .../seeded_lwe_compact_public_key/mod.rs | 56 ++ .../entities/seeded_lwe_keyswitch_key/mod.rs | 62 ++ .../seeded_lwe_multi_bit_bootstrap_key/mod.rs | 67 ++ .../seeded_lwe_packing_keyswitch_key/mod.rs | 71 ++ .../entities/seeded_lwe_public_key/mod.rs | 54 ++ .../fft_impl/fft128/crypto/bootstrap/mod.rs | 78 ++ .../fft_impl/fft128/crypto/ggsw/mod.rs | 73 ++ .../core_crypto/fft_impl/fft128/crypto/mod.rs | 2 + .../core_crypto/fft_impl/fft128/mod.rs | 1 + .../fft_impl/fft64/crypto/bootstrap/mod.rs | 83 ++ .../fft_impl/fft64/crypto/ggsw/mod.rs | 78 ++ .../core_crypto/fft_impl/fft64/crypto/mod.rs | 2 + .../fft_impl/fft64/math/fft/mod.rs | 48 ++ .../core_crypto/fft_impl/fft64/math/mod.rs | 1 + .../core_crypto/fft_impl/fft64/mod.rs | 2 + .../core_crypto/fft_impl/mod.rs | 2 + .../forward_compatibility/core_crypto/mod.rs | 3 + .../high_level_api/mod.rs | 306 +++++++ .../integer/ciphertext/mod.rs | 154 ++++ .../integer/client_key/mod.rs | 77 ++ .../integer/key_switching_key/mod.rs | 40 + tfhe/src/forward_compatibility/integer/mod.rs | 6 + .../integer/public_key/compact/mod.rs | 58 ++ .../integer/public_key/compressed/mod.rs | 32 + .../integer/public_key/mod.rs | 3 + .../integer/public_key/standard/mod.rs | 31 + .../integer/server_key/mod.rs | 54 ++ .../integer/wopbs/mod.rs | 34 + tfhe/src/forward_compatibility/mod.rs | 46 + .../shortint/ciphertext/mod.rs | 173 ++++ .../shortint/client_key/mod.rs | 36 + .../shortint/key_switching_key/mod.rs | 50 ++ .../src/forward_compatibility/shortint/mod.rs | 7 + .../shortint/parameters/key_switching/mod.rs | 43 + .../shortint/parameters/mod.rs | 189 +++++ .../shortint/parameters/multi_bit/mod.rs | 108 +++ .../parameters/parameters_wopbs/mod.rs | 73 ++ .../shortint/public_key/compact/mod.rs | 87 ++ .../shortint/public_key/compressed/mod.rs | 40 + .../shortint/public_key/mod.rs | 3 + .../shortint/public_key/standard/mod.rs | 40 + .../shortint/server_key/compressed/mod.rs | 92 ++ .../shortint/server_key/mod.rs | 116 +++ .../shortint/wopbs/mod.rs | 46 + tfhe/src/lib.rs | 3 + 109 files changed, 6090 insertions(+), 49 deletions(-) create mode 100644 tfhe/c_api_tests/test_forward_compatibility.c create mode 100644 tfhe/src/forward_compatibility/boolean/ciphertext/mod.rs create mode 100644 tfhe/src/forward_compatibility/boolean/client_key/mod.rs create mode 100644 tfhe/src/forward_compatibility/boolean/engine/bootstrapping/mod.rs create mode 100644 tfhe/src/forward_compatibility/boolean/engine/mod.rs create mode 100644 tfhe/src/forward_compatibility/boolean/key_switching_key/mod.rs create mode 100644 tfhe/src/forward_compatibility/boolean/mod.rs create mode 100644 tfhe/src/forward_compatibility/boolean/parameters/mod.rs create mode 100644 tfhe/src/forward_compatibility/boolean/public_key/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/commons/ciphertext_modulus/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/commons/dispersion/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/commons/math/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/commons/math/random/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/commons/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/commons/parameters/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/cleartext/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/ggsw_ciphertext/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/ggsw_ciphertext_list/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/glwe_ciphertext/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/glwe_ciphertext_list/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/glwe_secret_key/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/lwe_bootstrap_key/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/lwe_ciphertext/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/lwe_ciphertext_list/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/lwe_compact_public_key/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/lwe_keyswitch_key/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/lwe_multi_bit_bootstrap_key/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/lwe_packing_keyswitch_key/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/lwe_private_functional_packing_keyswitch_key/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/lwe_private_functional_packing_keyswitch_key_list/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/lwe_public_key/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/lwe_secret_key/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/plaintext/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/plaintext_list/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/polynomial/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/polynomial_list/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/seeded_ggsw_ciphertext/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/seeded_ggsw_ciphertext_list/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/seeded_glwe_ciphertext/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/seeded_glwe_ciphertext_list/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_bootstrap_key/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_ciphertext/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_ciphertext_list/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_compact_public_key/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_keyswitch_key/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_multi_bit_bootstrap_key/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_packing_keyswitch_key/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_public_key/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/fft_impl/fft128/crypto/bootstrap/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/fft_impl/fft128/crypto/ggsw/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/fft_impl/fft128/crypto/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/fft_impl/fft128/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/fft_impl/fft64/crypto/bootstrap/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/fft_impl/fft64/crypto/ggsw/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/fft_impl/fft64/crypto/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/fft_impl/fft64/math/fft/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/fft_impl/fft64/math/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/fft_impl/fft64/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/fft_impl/mod.rs create mode 100644 tfhe/src/forward_compatibility/core_crypto/mod.rs create mode 100644 tfhe/src/forward_compatibility/high_level_api/mod.rs create mode 100644 tfhe/src/forward_compatibility/integer/ciphertext/mod.rs create mode 100644 tfhe/src/forward_compatibility/integer/client_key/mod.rs create mode 100644 tfhe/src/forward_compatibility/integer/key_switching_key/mod.rs create mode 100644 tfhe/src/forward_compatibility/integer/mod.rs create mode 100644 tfhe/src/forward_compatibility/integer/public_key/compact/mod.rs create mode 100644 tfhe/src/forward_compatibility/integer/public_key/compressed/mod.rs create mode 100644 tfhe/src/forward_compatibility/integer/public_key/mod.rs create mode 100644 tfhe/src/forward_compatibility/integer/public_key/standard/mod.rs create mode 100644 tfhe/src/forward_compatibility/integer/server_key/mod.rs create mode 100644 tfhe/src/forward_compatibility/integer/wopbs/mod.rs create mode 100644 tfhe/src/forward_compatibility/mod.rs create mode 100644 tfhe/src/forward_compatibility/shortint/ciphertext/mod.rs create mode 100644 tfhe/src/forward_compatibility/shortint/client_key/mod.rs create mode 100644 tfhe/src/forward_compatibility/shortint/key_switching_key/mod.rs create mode 100644 tfhe/src/forward_compatibility/shortint/mod.rs create mode 100644 tfhe/src/forward_compatibility/shortint/parameters/key_switching/mod.rs create mode 100644 tfhe/src/forward_compatibility/shortint/parameters/mod.rs create mode 100644 tfhe/src/forward_compatibility/shortint/parameters/multi_bit/mod.rs create mode 100644 tfhe/src/forward_compatibility/shortint/parameters/parameters_wopbs/mod.rs create mode 100644 tfhe/src/forward_compatibility/shortint/public_key/compact/mod.rs create mode 100644 tfhe/src/forward_compatibility/shortint/public_key/compressed/mod.rs create mode 100644 tfhe/src/forward_compatibility/shortint/public_key/mod.rs create mode 100644 tfhe/src/forward_compatibility/shortint/public_key/standard/mod.rs create mode 100644 tfhe/src/forward_compatibility/shortint/server_key/compressed/mod.rs create mode 100644 tfhe/src/forward_compatibility/shortint/server_key/mod.rs create mode 100644 tfhe/src/forward_compatibility/shortint/wopbs/mod.rs diff --git a/.github/workflows/aws_tfhe_fast_tests.yml b/.github/workflows/aws_tfhe_fast_tests.yml index 5079526690..ee2f15c34e 100644 --- a/.github/workflows/aws_tfhe_fast_tests.yml +++ b/.github/workflows/aws_tfhe_fast_tests.yml @@ -26,13 +26,13 @@ on: description: "Action runner name" type: string request_id: - description: 'Slab request ID' + description: "Slab request ID" type: string fork_repo: - description: 'Name of forked repo as user/repo' + description: "Name of forked repo as user/repo" type: string fork_git_sha: - description: 'Git SHA to checkout from fork' + description: "Git SHA to checkout from fork" type: string jobs: @@ -115,6 +115,10 @@ jobs: run: | make test_safe_deserialization + - name: Run forward compatibility tests + run: | + make test_forward_compatibility + - name: Slack Notification if: ${{ always() }} continue-on-error: true diff --git a/.github/workflows/aws_tfhe_tests.yml b/.github/workflows/aws_tfhe_tests.yml index 40fac82946..fd52df84ac 100644 --- a/.github/workflows/aws_tfhe_tests.yml +++ b/.github/workflows/aws_tfhe_tests.yml @@ -25,13 +25,13 @@ on: description: "Action runner name" type: string request_id: - description: 'Slab request ID' + description: "Slab request ID" type: string fork_repo: - description: 'Name of forked repo as user/repo' + description: "Name of forked repo as user/repo" type: string fork_git_sha: - description: 'Git SHA to checkout from fork' + description: "Git SHA to checkout from fork" type: string jobs: @@ -82,6 +82,10 @@ jobs: run: | make test_c_api + - name: Run C API tests with forward_compatibility + run: | + FORWARD_COMPAT=ON make test_c_api + - name: Run user docs tests run: | make test_user_doc diff --git a/Makefile b/Makefile index 8c06fd597a..44b44b279e 100644 --- a/Makefile +++ b/Makefile @@ -233,11 +233,17 @@ clippy_trivium: install_rs_check_toolchain -p tfhe-trivium -- --no-deps -D warnings .PHONY: clippy_all_targets # Run clippy lints on all targets (benches, examples, etc.) -clippy_all_targets: +clippy_all_targets: install_rs_check_toolchain RUSTFLAGS="$(RUSTFLAGS)" cargo "$(CARGO_RS_CHECK_TOOLCHAIN)" clippy --all-targets \ --features=$(TARGET_ARCH_FEATURE),boolean,shortint,integer,internal-keycache \ -p $(TFHE_SPEC) -- --no-deps -D warnings +.PHONY: clippy_all_targets_forward_compatibility # Run clippy lints on all targets (benches, examples, etc.) +clippy_all_targets_forward_compatibility: install_rs_check_toolchain + RUSTFLAGS="$(RUSTFLAGS)" cargo "$(CARGO_RS_CHECK_TOOLCHAIN)" clippy --all-targets \ + --features=$(TARGET_ARCH_FEATURE),boolean,shortint,integer,internal-keycache,forward_compatibility \ + -p $(TFHE_SPEC) -- --no-deps -D warnings + .PHONY: clippy_concrete_csprng # Run clippy lints on concrete-csprng clippy_concrete_csprng: RUSTFLAGS="$(RUSTFLAGS)" cargo "$(CARGO_RS_CHECK_TOOLCHAIN)" clippy --all-targets \ @@ -246,7 +252,8 @@ clippy_concrete_csprng: .PHONY: clippy_all # Run all clippy targets clippy_all: clippy clippy_boolean clippy_shortint clippy_integer clippy_all_targets clippy_c_api \ -clippy_js_wasm_api clippy_tasks clippy_core clippy_concrete_csprng clippy_trivium +clippy_js_wasm_api clippy_tasks clippy_core clippy_concrete_csprng clippy_trivium \ +clippy_all_targets_forward_compatibility .PHONY: clippy_fast # Run main clippy targets clippy_fast: clippy clippy_all_targets clippy_c_api clippy_js_wasm_api clippy_tasks clippy_core \ @@ -314,7 +321,6 @@ build_c_api_gpu: install_rs_check_toolchain build_c_api_experimental_deterministic_fft: install_rs_check_toolchain RUSTFLAGS="$(RUSTFLAGS)" cargo $(CARGO_RS_CHECK_TOOLCHAIN) build --profile $(CARGO_PROFILE) \ --features=$(TARGET_ARCH_FEATURE),boolean-c-api,shortint-c-api,high-level-c-api,experimental-force_fft_algo_dif4,$(FORWARD_COMPAT_FEATURE) \ - -p $(TFHE_SPEC) @"$(MAKE)" symlink_c_libs_without_fingerprint .PHONY: build_web_js_api # Build the js API targeting the web browser @@ -408,7 +414,7 @@ test_c_api_rs: install_rs_check_toolchain .PHONY: test_c_api_c # Run the C tests for the C API test_c_api_c: build_c_api - ./scripts/c_api_tests.sh + ./scripts/c_api_tests.sh --forward-compat "$(FORWARD_COMPAT)" .PHONY: test_c_api # Run all the tests for the C API test_c_api: test_c_api_rs test_c_api_c @@ -508,6 +514,12 @@ test_high_level_api: install_rs_build_toolchain --features=$(TARGET_ARCH_FEATURE),boolean,shortint,integer,internal-keycache -p $(TFHE_SPEC) \ -- high_level_api:: +.PHONY: test_forward_compatibility # Run forward compatibility tests +test_forward_compatibility: install_rs_build_toolchain + RUSTFLAGS="$(RUSTFLAGS)" cargo $(CARGO_RS_BUILD_TOOLCHAIN) test --tests --profile $(CARGO_PROFILE) \ + --features=$(TARGET_ARCH_FEATURE),boolean,shortint,integer,forward_compatibility,internal-keycache -p $(TFHE_SPEC) \ + -- forward_compatibility:: + .PHONY: test_user_doc # Run tests from the .md documentation test_user_doc: install_rs_build_toolchain RUSTFLAGS="$(RUSTFLAGS)" cargo $(CARGO_RS_BUILD_TOOLCHAIN) test --profile $(CARGO_PROFILE) --doc \ @@ -585,7 +597,9 @@ check_compile_tests: @if [[ "$(OS)" == "Linux" || "$(OS)" == "Darwin" ]]; then \ "$(MAKE)" build_c_api && \ - ./scripts/c_api_tests.sh --build-only; \ + ./scripts/c_api_tests.sh --build-only --forward-compat "$(FORWARD_COMPAT)" && \ + FORWARD_COMPAT=ON "$(MAKE)" build_c_api && \ + ./scripts/c_api_tests.sh --build-only --forward-compat "$(FORWARD_COMPAT)"; \ fi .PHONY: build_nodejs_test_docker # Build a docker image with tools to run nodejs tests for wasm API diff --git a/apps/trivium/README.md b/apps/trivium/README.md index c4b0d388c5..d74ba34dd7 100644 --- a/apps/trivium/README.md +++ b/apps/trivium/README.md @@ -146,7 +146,7 @@ use tfhe::prelude::*; use tfhe_trivium::TriviumStreamShortint; fn test_shortint() { - let config = ConfigBuilder::all_disabled().enable_default_integers().build(); + let config = ConfigBuilder::default().build(); let (hl_client_key, hl_server_key) = generate_keys(config); let (client_key, server_key): (ClientKey, ServerKey) = gen_keys(PARAM_MESSAGE_1_CARRY_1_KS_PBS); let ksk = CastingKey::new((&client_key, &server_key), (&hl_client_key, &hl_server_key)); diff --git a/scripts/c_api_tests.sh b/scripts/c_api_tests.sh index 526254978c..fe84f11f5a 100755 --- a/scripts/c_api_tests.sh +++ b/scripts/c_api_tests.sh @@ -8,11 +8,14 @@ function usage() { echo "--help Print this message" echo "--build-only Pass to only build the tests without running them" echo "--gpu Enable GPU support" + echo "--forward-compat Indicate if we have forward compatibility enabled" echo } BUILD_ONLY=0 WITH_FEATURE_GPU="OFF" +WITH_FORWARD_COMPAT="OFF" + while [ -n "$1" ] do case "$1" in @@ -28,6 +31,12 @@ do "--gpu" ) WITH_FEATURE_GPU="ON" ;; + + "--forward-compat" ) + shift + WITH_FORWARD_COMPAT="$1" + ;; + *) echo "Unknown param : $1" exit 1 @@ -44,7 +53,9 @@ mkdir -p "${TFHE_BUILD_DIR}" cd "${TFHE_BUILD_DIR}" -cmake .. -DCMAKE_BUILD_TYPE=RELEASE -DCARGO_PROFILE="${CARGO_PROFILE}" -DWITH_FEATURE_GPU="${WITH_FEATURE_GPU}" +cmake .. -DCMAKE_BUILD_TYPE=RELEASE -DCARGO_PROFILE="${CARGO_PROFILE}" \ + -DWITH_FEATURE_GPU="${WITH_FEATURE_GPU}" \ + -DWITH_FORWARD_COMPATIBILITY="${WITH_FORWARD_COMPAT}" make -j diff --git a/tfhe/Cargo.toml b/tfhe/Cargo.toml index a3c2691898..84c7e90ef9 100644 --- a/tfhe/Cargo.toml +++ b/tfhe/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tfhe" -version = "0.5.3" +version = "0.5.4" edition = "2021" readme = "../README.md" keywords = ["fully", "homomorphic", "encryption", "fhe", "cryptography"] @@ -46,7 +46,7 @@ hex = "0.4.3" # End regex-engine deps [build-dependencies] -cbindgen = { version = "0.26.0", optional = true } +cbindgen = { package = "tfhe-c-api-bindgen", version = "0.26.1", optional = true } [dependencies] concrete-csprng = { version = "0.4.0", path = "../concrete-csprng", features = [ @@ -69,6 +69,7 @@ paste = "1.0.7" fs2 = { version = "0.4.3", optional = true } # While we wait for repeat_n in rust standard library itertools = "0.11.0" +next_tfhe = { package = "tfhe", version = "0.6.1", optional = true } # wasm deps wasm-bindgen = { version = "0.2.86", features = [ @@ -82,18 +83,30 @@ getrandom = { version = "0.2.8", optional = true } bytemuck = "1.14.3" [features] -boolean = [] -shortint = [] -integer = ["shortint"] -internal-keycache = ["dep:lazy_static", "dep:fs2"] +forward_compatibility = ["dep:tfhe-c-api-dynamic-buffer", "dep:next_tfhe"] + +boolean = ["next_tfhe?/boolean"] +shortint = ["next_tfhe?/shortint"] +integer = ["shortint", "next_tfhe?/integer"] +internal-keycache = [ + "dep:lazy_static", + "dep:fs2", + "next_tfhe?/internal-keycache", +] gpu = ["tfhe-cuda-backend"] # Experimental section experimental = [] -experimental-force_fft_algo_dif4 = [] +experimental-force_fft_algo_dif4 = [ + "next_tfhe?/experimental-force_fft_algo_dif4", +] # End experimental section -__c_api = ["dep:cbindgen", "dep:tfhe-c-api-dynamic-buffer"] +__c_api = [ + "dep:cbindgen", + "dep:tfhe-c-api-dynamic-buffer", + "next_tfhe?/__force_skip_cbindgen", +] # For the semver trick to skip the build.rs __force_skip_cbindgen = [] boolean-c-api = ["boolean", "__c_api"] @@ -114,32 +127,45 @@ integer-client-js-wasm-api = ["integer", "__wasm_api"] high-level-client-js-wasm-api = ["boolean", "shortint", "integer", "__wasm_api"] parallel-wasm-api = ["dep:wasm-bindgen-rayon"] -nightly-avx512 = ["concrete-fft/nightly", "pulp/nightly"] +nightly-avx512 = [ + "concrete-fft/nightly", + "pulp/nightly", + "next_tfhe?/nightly-avx512", +] # Enable the x86_64 specific accelerated implementation of the random generator for the default # backend -generator_x86_64_aesni = ["concrete-csprng/generator_x86_64_aesni"] +generator_x86_64_aesni = [ + "concrete-csprng/generator_x86_64_aesni", + "next_tfhe?/generator_x86_64_aesni", +] # Enable the aarch64 specific accelerated implementation of the random generator for the default # backend -generator_aarch64_aes = ["concrete-csprng/generator_aarch64_aes"] +generator_aarch64_aes = [ + "concrete-csprng/generator_aarch64_aes", + "next_tfhe?/generator_aarch64_aes", +] # Private features __profiling = [] __coverage = [] -seeder_unix = ["concrete-csprng/seeder_unix"] -seeder_x86_64_rdseed = ["concrete-csprng/seeder_x86_64_rdseed"] +seeder_unix = ["concrete-csprng/seeder_unix", "next_tfhe?/seeder_unix"] +seeder_x86_64_rdseed = [ + "concrete-csprng/seeder_x86_64_rdseed", + "next_tfhe?/seeder_x86_64_rdseed", +] # These target_arch features enable a set of public features for tfhe if users want a known # good/working configuration for tfhe. # For a target_arch that does not yet have such a feature, one can still enable features manually or # create a feature for said target_arch to make its use simpler. -x86_64 = ["generator_x86_64_aesni", "seeder_x86_64_rdseed"] -x86_64-unix = ["x86_64", "seeder_unix"] +x86_64 = ["generator_x86_64_aesni", "seeder_x86_64_rdseed", "next_tfhe?/x86_64"] +x86_64-unix = ["x86_64", "seeder_unix", "next_tfhe?/x86_64-unix"] -aarch64 = ["generator_aarch64_aes"] -aarch64-unix = ["aarch64", "seeder_unix"] +aarch64 = ["generator_aarch64_aes", "next_tfhe?/aarch64"] +aarch64-unix = ["aarch64", "seeder_unix", "next_tfhe?/aarch64-unix"] [package.metadata.docs.rs] # TODO: manage builds for docs.rs based on their documentation https://docs.rs/about diff --git a/tfhe/build.rs b/tfhe/build.rs index 54deb47e58..678477232f 100644 --- a/tfhe/build.rs +++ b/tfhe/build.rs @@ -57,6 +57,8 @@ fn gen_c_api() { "integer", #[cfg(feature = "gpu")] "gpu", + #[cfg(feature = "forward_compatibility")] + "forward_compatibility", ]; let parse_expand_vec = if parse_expand_features_vec.is_empty() { @@ -65,14 +67,16 @@ fn gen_c_api() { vec![package_name.as_str()] }; - cbindgen::Builder::new() + let builder = cbindgen::Builder::new() .with_crate(crate_dir.as_path()) .with_config(cbindgen::Config::from_file(crate_dir.join("cbindgen.toml")).unwrap()) .with_parse_expand(&parse_expand_vec) - .with_parse_expand_features(&parse_expand_features_vec) - .generate() - .unwrap() - .write_to_file(output_file); + .with_parse_expand_features(&parse_expand_features_vec); + + #[cfg(feature = "forward_compatibility")] + let builder = builder.with_include("tfhe-c-api-dynamic-buffer.h"); + + builder.generate().unwrap().write_to_file(output_file); } fn main() { diff --git a/tfhe/c_api_tests/CMakeLists.txt b/tfhe/c_api_tests/CMakeLists.txt index 4b3df9d433..f7ca90701e 100644 --- a/tfhe/c_api_tests/CMakeLists.txt +++ b/tfhe/c_api_tests/CMakeLists.txt @@ -8,6 +8,7 @@ endif() set(TFHE_C_API_RELEASE "${CMAKE_CURRENT_SOURCE_DIR}/../../target/${CARGO_PROFILE}") option(WITH_FEATURE_GPU "Enable if tfhe-rs C API was compiled with the 'gpu' feature activated" OFF) +option(WITH_FORWARD_COMPATIBILITY "Enable if tfhe-rs C API was compiled with the 'forward_compatibility' feature activated" OFF) include_directories(${TFHE_C_API_RELEASE}) # This one is to fetch the dynamic buffer header @@ -29,6 +30,10 @@ if (WITH_FEATURE_GPU) find_package(OpenMP REQUIRED) endif() +if("${WITH_FORWARD_COMPATIBILITY}" STREQUAL "ON") + add_definitions(-DWITH_FORWARD_COMPATIBILITY) +endif() + file(GLOB TEST_CASES test_*.c) foreach (testsourcefile ${TEST_CASES}) get_filename_component(testname ${testsourcefile} NAME_WLE) diff --git a/tfhe/c_api_tests/test_forward_compatibility.c b/tfhe/c_api_tests/test_forward_compatibility.c new file mode 100644 index 0000000000..ec5ab284d8 --- /dev/null +++ b/tfhe/c_api_tests/test_forward_compatibility.c @@ -0,0 +1,115 @@ +#include + +#include +#include +#include + +#ifdef WITH_FORWARD_COMPATIBILITY +int uint8_format_update(const ClientKey *client_key, const ServerKey *server_key) { + int ok; + FheUint8 *lhs = NULL; + FheUint8 *deserialized_lhs = NULL; + FheUint8 *result = NULL; + DynamicBuffer value_buffer = {.pointer = NULL, .length = 0, .destructor = NULL}; + DynamicBuffer conformant_value_buffer = {.pointer = NULL, .length = 0, .destructor = NULL}; + DynamicBuffer cks_buffer = {.pointer = NULL, .length = 0, .destructor = NULL}; + DynamicBufferView deser_view = {.pointer = NULL, .length = 0}; + ClientKey *deserialized_client_key = NULL; + DynamicBuffer out_buffer = {.pointer = NULL, .length = 0, .destructor = NULL}; + + const uint64_t max_serialization_size = UINT64_C(1) << UINT64_C(20); + + uint8_t lhs_clear = 123; + + ok = client_key_serialize(client_key, &cks_buffer); + assert(ok == 0); + + deser_view.pointer = cks_buffer.pointer; + deser_view.length = cks_buffer.length; + + ok = client_key_update_serialization_from_0_5_to_0_6(deser_view, &out_buffer); + assert(ok == 0); + + destroy_dynamic_buffer(&out_buffer); + + deser_view.pointer = cks_buffer.pointer; + deser_view.length = cks_buffer.length; + ok = client_key_deserialize(deser_view, &deserialized_client_key); + assert(ok == 0); + + ok = fhe_uint8_try_encrypt_with_client_key_u8(lhs_clear, deserialized_client_key, &lhs); + assert(ok == 0); + + ok = fhe_uint8_serialize(lhs, &value_buffer); + assert(ok == 0); + + deser_view.pointer = value_buffer.pointer; + deser_view.length = value_buffer.length; + + ok = fhe_uint8_update_serialization_from_0_5_to_0_6(deser_view, &out_buffer); + assert(ok == 0); + + destroy_dynamic_buffer(&out_buffer); + + ok = fhe_uint8_safe_serialize(lhs, &conformant_value_buffer, max_serialization_size); + assert(ok == 0); + + deser_view.pointer = conformant_value_buffer.pointer; + deser_view.length = conformant_value_buffer.length; + + ok = fhe_uint8_safe_update_serialization_conformant_from_0_5_to_0_6( + deser_view, max_serialization_size, server_key, &out_buffer); + assert(ok == 0); + + destroy_dynamic_buffer(&out_buffer); + + deser_view.pointer = value_buffer.pointer; + deser_view.length = value_buffer.length; + ok = fhe_uint8_deserialize(deser_view, &deserialized_lhs); + assert(ok == 0); + + uint8_t clear; + ok = fhe_uint8_decrypt(deserialized_lhs, deserialized_client_key, &clear); + assert(ok == 0); + + assert(clear == lhs_clear); + + destroy_dynamic_buffer(&value_buffer); + destroy_dynamic_buffer(&conformant_value_buffer); + + fhe_uint8_destroy(lhs); + fhe_uint8_destroy(deserialized_lhs); + fhe_uint8_destroy(result); + return ok; +} +#endif + +int main(void) { + int ok = 0; + +#ifdef WITH_FORWARD_COMPATIBILITY + { + ConfigBuilder *builder; + Config *config; + + ok = config_builder_default(&builder); + assert(ok == 0); + ok = config_builder_build(builder, &config); + assert(ok == 0); + + ClientKey *client_key = NULL; + ServerKey *server_key = NULL; + PublicKey *public_key = NULL; + + ok = generate_keys(config, &client_key, &server_key); + assert(ok == 0); + ok = uint8_format_update(client_key, server_key); + + client_key_destroy(client_key); + public_key_destroy(public_key); + server_key_destroy(server_key); + } +#endif + + return ok; +} diff --git a/tfhe/cbindgen.toml b/tfhe/cbindgen.toml index d7316f4a58..5a742fd56a 100644 --- a/tfhe/cbindgen.toml +++ b/tfhe/cbindgen.toml @@ -46,6 +46,7 @@ usize_is_size_t = true [defines] # "target_os = freebsd" = "DEFINE_FREEBSD" "feature = gpu" = "WITH_FEATURE_GPU" +"feature = forward_compatibility" = "WITH_FORWARD_COMPATIBILITY" [export] diff --git a/tfhe/docs/core_crypto/tutorial.md b/tfhe/docs/core_crypto/tutorial.md index 2403ad709a..2ed3990350 100644 --- a/tfhe/docs/core_crypto/tutorial.md +++ b/tfhe/docs/core_crypto/tutorial.md @@ -9,7 +9,7 @@ Welcome to this tutorial about `TFHE-rs` `core_crypto` module. To use `TFHE-rs`, it first has to be added as a dependency in the `Cargo.toml`: ```toml -tfhe = { version = "0.5.3", features = [ "x86_64-unix" ] } +tfhe = { version = "0.5.4", features = [ "x86_64-unix" ] } ``` This enables the `x86_64-unix` feature to have efficient implementations of various algorithms for `x86_64` CPUs on a Unix-like system. The 'unix' suffix indicates that the `UnixSeeder`, which uses `/dev/random` to generate random numbers, is activated as a fallback if no hardware number generator is available (like `rdseed` on `x86_64` or if the [`Randomization Services`](https://developer.apple.com/documentation/security/1399291-secrandomcopybytes?language=objc) on Apple platforms are not available). To avoid having the `UnixSeeder` as a potential fallback or to run on non-Unix systems (e.g., Windows), the `x86_64` feature is sufficient. @@ -19,19 +19,19 @@ For Apple Silicon, the `aarch64-unix` or `aarch64` feature should be enabled. `a In short: For `x86_64`-based machines running Unix-like OSes: ```toml -tfhe = { version = "0.5.3", features = ["x86_64-unix"] } +tfhe = { version = "0.5.4", features = ["x86_64-unix"] } ``` For Apple Silicon or aarch64-based machines running Unix-like OSes: ```toml -tfhe = { version = "0.5.3", features = ["aarch64-unix"] } +tfhe = { version = "0.5.4", features = ["aarch64-unix"] } ``` For `x86_64`-based machines with the [`rdseed instruction`](https://en.wikipedia.org/wiki/RDRAND) running Windows: ```toml -tfhe = { version = "0.5.3", features = ["x86_64"] } +tfhe = { version = "0.5.4", features = ["x86_64"] } ``` ### Commented code to double a 2-bit message in a leveled fashion and using a PBS with the `core_crypto` module. diff --git a/tfhe/docs/getting_started/installation.md b/tfhe/docs/getting_started/installation.md index 437cd516ac..a81a23aa08 100644 --- a/tfhe/docs/getting_started/installation.md +++ b/tfhe/docs/getting_started/installation.md @@ -8,12 +8,12 @@ To use `TFHE-rs` in your project, you first need to add it as a dependency in yo If you are using an `x86` machine: ```toml -tfhe = { version = "0.5.3", features = [ "boolean", "shortint", "integer", "x86_64-unix" ] } +tfhe = { version = "0.5.4", features = [ "boolean", "shortint", "integer", "x86_64-unix" ] } ``` If you are using an `ARM` machine: ```toml -tfhe = { version = "0.5.3", features = [ "boolean", "shortint", "integer", "aarch64-unix" ] } +tfhe = { version = "0.5.4", features = [ "boolean", "shortint", "integer", "aarch64-unix" ] } ``` {% hint style="info" %} diff --git a/tfhe/docs/getting_started/quick_start.md b/tfhe/docs/getting_started/quick_start.md index 77ce5d2bed..114a40b0cd 100644 --- a/tfhe/docs/getting_started/quick_start.md +++ b/tfhe/docs/getting_started/quick_start.md @@ -44,7 +44,7 @@ fn main() { The default configuration for x86 Unix machines: ```toml -tfhe = { version = "0.5.3", features = ["integer", "x86_64-unix"]} +tfhe = { version = "0.5.4", features = ["integer", "x86_64-unix"]} ``` Configuration options for different platforms can be found [here](../getting_started/installation.md). Other rust and homomorphic types features can be found [here](../how_to/rust_configuration.md). diff --git a/tfhe/docs/how_to/migrate_data.md b/tfhe/docs/how_to/migrate_data.md index c09ca440f2..3baa674e5c 100644 --- a/tfhe/docs/how_to/migrate_data.md +++ b/tfhe/docs/how_to/migrate_data.md @@ -1,3 +1,66 @@ -# Migrating Data to TFHE-rs 0.5.3 (This Release) +# Managing Data Through Various TFHE-rs Versions -Forward compatibility code to migrate data from TFHE-rs 0.4 to TFHE-rs 0.5 has been added in a minor release of TFHE-rs 0.4, the documentation about the process can be found [here](https://docs.zama.ai/tfhe-rs/v/0.4-1/how-to/migrate_data). +In what follows, the process to manage data when upgrading the TFHE-rs version (starting from the 0.5.4 release) is given. This page details the methods to make data, which have initially been generated with an older version of TFHE-rs, usable with a newer version. + +## Forward Compatibility Strategy + +The current strategy that has been adopted for TFHE-rs is the following: + +- TFHE-rs has a global `SERIALIZATION_VERSION` constant; +- When breaking serialization changes are introduced, this global version is bumped; +- Safe serialization primitives check this constant upon deserialization, if the data is incompatible, these primitives return an error. + +To be able to use older serialized data with newer versions, the following is done on new major TFHE-rs releases: + +- A minor update is done to the previously released branch to add the new release as an optional dependency; +- Conversion code is added to the previous branch to be able to load old data and convert it to the new data format. + +In practice, if we take the 0.6 release as a concrete example, here is what will happen: + +- 0.6.0 is released with breaking changes to the serialization; +- 0.5.4 has tfhe@0.6.0 as optional dependency gated by the `forward_compatibility` feature; +- Conversion code is added to 0.5.4, if possible without any user input, but some data migration will likely require some information to be provided by the developer writing the migration code; +- 0.5.4 is released. + +{% hint style="info" %} +Note that if you do not need forward compatibility 0.5.4 will be equivalent to 0.5.3 from a usability perspective and you can safely update. +Note also that the 0.6.0 has no knowledge of previous releases. +{% endhint %} + +## What it means from a developer perspective + +A set of generic tooling is given to allow migrating data by using several workflows. The data migration is considered to be an application/protocol layer concern to avoid imposing design choices. + +Examples to migrate data: + +An `Application` uses TFHE-rs 0.5.3 and needs/wants to upgrade to 0.6.0 to benefit from various improvements. + +Example timeline of the data migration or `Bulk Data Migration`: +- A new transition version of the `Application` is compiled with the 0.5.4 release of TFHE-rs; +- The transition version of the `Application` adds code to read previously stored data, convert it to the proper format for 0.6.0 and save it back to disk; +- The service enters a maintenance period (if relevant); +- Migration of data from 0.5.4 to 0.6.0 is done with the transition version of the `Application`, note that depending on the volume of data this transition can take a significant amount of time; +- The updated version of the `Application` is compiled with the 0.6.0 release of TFHE-rs and put in production; +- Service is resumed with the updated `Application` (if relevant). + +The above case is describing a simple use case, where only a single version of data has to be managed. Moreover, the above strategy is not relevant in the case where the data is so large that migrating it in one go is not doable, or if the service cannot suffer any interruption. + +In order to manage more complicated cases, another method called `Migrate On Read` can be used. + +Here is an example timeline where data is migrated only as needed with the `Migrate On Read` approach: +- A new version of the `Application` is compiled, it has tfhe@0.5.4 as dependency (the dependency will have to be renamed to avoid conflicts, a possible name is to use the major version like `tfhe_0_5`) and tfhe@0.6.0 which will not be renamed and can be accessed as `tfhe` +- Code to manage reading the data is added to the `Application`: +- The code determines whether the data was saved with the 0.5 `Application` or the 0.6 `Application`, if the data is already up to date with the 0.6 format it can be loaded right away, if it's in the 0.5 format the `Application` can check if an updated version of the data is already available in the 0.6 format and loads that if it's available, otherwise it converts the data to 0.6, saves the converted data to avoid having to convert it every time it is accessed and continue processing with the 0.6 data + +The above is more complicated to manage as data will be present on disk with several versions, however it allows to run the service continuously or near-continuously once the new `Application` is deployed (it will require careful routing or error handling as nodes with outdated `Application` won't be able to process the 0.6 data). + +Also, if required, several version of TFHE-rs can be "chained" to upgrade very old data to newer formats. +The above pattern can be extended to have `tfhe_0_5` (tfhe@0.5.4 renamed), `tfhe_0_6` (tfhe@0.6.0 renamed) and `tfhe` being tfhe@0.7.0, this will require special handling from the developers so that their protocol can handle data from 0.5.4, 0.6.0 and 0.7.0 using all the conversion tooling from the relevant version. + +E.g., if some computation requires data from version 0.5.4 a conversion function could be called `upgrade_data_from_0_5_to_0_7` and do: + +- read data from 0.5.4 +- convert to 0.6.0 format using `tfhe_0_6` +- convert to 0.7.0 format using `tfhe_0_7` +- save to disk in 0.7.0 format +- process 0.7.0 data with `tfhe` which is tfhe@0.7.0 diff --git a/tfhe/docs/how_to/run_on_gpu.md b/tfhe/docs/how_to/run_on_gpu.md index f8bc72bb8f..15addad810 100644 --- a/tfhe/docs/how_to/run_on_gpu.md +++ b/tfhe/docs/how_to/run_on_gpu.md @@ -13,12 +13,12 @@ To use the `TFHE-rs GPU backend` in your project, you first need to add it as a If you are using an `x86` machine: ```toml -tfhe = { version = "0.5.3", features = [ "boolean", "shortint", "integer", "x86_64-unix", "gpu" ] } +tfhe = { version = "0.5.4", features = [ "boolean", "shortint", "integer", "x86_64-unix", "gpu" ] } ``` If you are using an `ARM` machine: ```toml -tfhe = { version = "0.5.3", features = [ "boolean", "shortint", "integer", "aarch64-unix", "gpu" ] } +tfhe = { version = "0.5.4", features = [ "boolean", "shortint", "integer", "aarch64-unix", "gpu" ] } ``` diff --git a/tfhe/docs/how_to/serialization.md b/tfhe/docs/how_to/serialization.md index c27c3fd1ed..1e17563824 100644 --- a/tfhe/docs/how_to/serialization.md +++ b/tfhe/docs/how_to/serialization.md @@ -11,7 +11,7 @@ To serialize our data, a [data format](https://serde.rs/#data-formats) should be [dependencies] # ... -tfhe = { version = "0.5.3", features = ["integer","x86_64-unix"]} +tfhe = { version = "0.5.4", features = ["integer","x86_64-unix"]} bincode = "1.3.3" ``` diff --git a/tfhe/docs/tutorials/ascii_fhe_string.md b/tfhe/docs/tutorials/ascii_fhe_string.md index ed4b7ab640..459863b75b 100644 --- a/tfhe/docs/tutorials/ascii_fhe_string.md +++ b/tfhe/docs/tutorials/ascii_fhe_string.md @@ -24,7 +24,7 @@ To use the `FheUint8` type, the `integer` feature must be activated: [dependencies] # Default configuration for x86 Unix machines: -tfhe = { version = "0.5.3", features = ["integer", "x86_64-unix"]} +tfhe = { version = "0.5.4", features = ["integer", "x86_64-unix"]} ``` Other configurations can be found [here](../getting_started/installation.md). diff --git a/tfhe/docs/tutorials/parity_bit.md b/tfhe/docs/tutorials/parity_bit.md index e627ab7055..20d0d9baad 100644 --- a/tfhe/docs/tutorials/parity_bit.md +++ b/tfhe/docs/tutorials/parity_bit.md @@ -19,7 +19,7 @@ This function returns a Boolean that will be either `true` or `false` so that th # Cargo.toml # Default configuration for x86 Unix machines: -tfhe = { version = "0.5.3", features = ["integer", "x86_64-unix"]} +tfhe = { version = "0.5.4", features = ["integer", "x86_64-unix"]} ``` Other configurations can be found [here](../getting_started/installation.md). diff --git a/tfhe/src/c_api/high_level_api/integers.rs b/tfhe/src/c_api/high_level_api/integers.rs index 734a2e794e..9fb8696e96 100644 --- a/tfhe/src/c_api/high_level_api/integers.rs +++ b/tfhe/src/c_api/high_level_api/integers.rs @@ -522,6 +522,167 @@ impl_try_encrypt_list_with_compact_public_key_on_type!(CompactFheInt16List{crate impl_try_encrypt_list_with_compact_public_key_on_type!(CompactFheInt32List{crate::high_level_api::CompactFheInt32List}, i32); impl_try_encrypt_list_with_compact_public_key_on_type!(CompactFheInt64List{crate::high_level_api::CompactFheInt64List}, i64); +#[cfg(feature = "forward_compatibility")] +pub mod forward_compatibility { + use super::*; + + // FheInt don't have the 10, 12, 14 variants so we define the impl here + impl_update_serialization_format_on_type!(FheUint8); + impl_update_serialization_format_on_type!(FheUint10); + impl_update_serialization_format_on_type!(FheUint12); + impl_update_serialization_format_on_type!(FheUint14); + impl_update_serialization_format_on_type!(FheUint16); + impl_update_serialization_format_on_type!(FheUint32); + impl_update_serialization_format_on_type!(FheUint64); + impl_update_serialization_format_on_type!(FheUint128); + impl_update_serialization_format_on_type!(FheUint256); + impl_update_serialization_format_on_type!(CompressedFheUint8); + impl_update_serialization_format_on_type!(CompressedFheUint10); + impl_update_serialization_format_on_type!(CompressedFheUint12); + impl_update_serialization_format_on_type!(CompressedFheUint14); + impl_update_serialization_format_on_type!(CompressedFheUint16); + impl_update_serialization_format_on_type!(CompressedFheUint32); + impl_update_serialization_format_on_type!(CompressedFheUint64); + impl_update_serialization_format_on_type!(CompressedFheUint128); + impl_update_serialization_format_on_type!(CompressedFheUint256); + + impl_safe_update_serialization_format_conformant_on_type!( + FheUint8, + crate::high_level_api::safe_deserialize_conformant + ); + impl_safe_update_serialization_format_conformant_on_type!( + FheUint10, + crate::high_level_api::safe_deserialize_conformant + ); + impl_safe_update_serialization_format_conformant_on_type!( + FheUint12, + crate::high_level_api::safe_deserialize_conformant + ); + impl_safe_update_serialization_format_conformant_on_type!( + FheUint14, + crate::high_level_api::safe_deserialize_conformant + ); + impl_safe_update_serialization_format_conformant_on_type!( + FheUint16, + crate::high_level_api::safe_deserialize_conformant + ); + impl_safe_update_serialization_format_conformant_on_type!( + FheUint32, + crate::high_level_api::safe_deserialize_conformant + ); + impl_safe_update_serialization_format_conformant_on_type!( + FheUint64, + crate::high_level_api::safe_deserialize_conformant + ); + impl_safe_update_serialization_format_conformant_on_type!( + FheUint128, + crate::high_level_api::safe_deserialize_conformant + ); + impl_safe_update_serialization_format_conformant_on_type!( + FheUint256, + crate::high_level_api::safe_deserialize_conformant + ); + impl_safe_update_serialization_format_conformant_on_type!( + CompressedFheUint8, + crate::high_level_api::safe_deserialize_conformant + ); + impl_safe_update_serialization_format_conformant_on_type!( + CompressedFheUint10, + crate::high_level_api::safe_deserialize_conformant + ); + impl_safe_update_serialization_format_conformant_on_type!( + CompressedFheUint12, + crate::high_level_api::safe_deserialize_conformant + ); + impl_safe_update_serialization_format_conformant_on_type!( + CompressedFheUint14, + crate::high_level_api::safe_deserialize_conformant + ); + impl_safe_update_serialization_format_conformant_on_type!( + CompressedFheUint16, + crate::high_level_api::safe_deserialize_conformant + ); + impl_safe_update_serialization_format_conformant_on_type!( + CompressedFheUint32, + crate::high_level_api::safe_deserialize_conformant + ); + impl_safe_update_serialization_format_conformant_on_type!( + CompressedFheUint64, + crate::high_level_api::safe_deserialize_conformant + ); + impl_safe_update_serialization_format_conformant_on_type!( + CompressedFheUint128, + crate::high_level_api::safe_deserialize_conformant + ); + impl_safe_update_serialization_format_conformant_on_type!( + CompressedFheUint256, + crate::high_level_api::safe_deserialize_conformant + ); + + // FheInt don't have the 10, 12, 14 variants so we define the impl here + impl_update_serialization_format_on_type!(FheInt8); + impl_update_serialization_format_on_type!(FheInt16); + impl_update_serialization_format_on_type!(FheInt32); + impl_update_serialization_format_on_type!(FheInt64); + impl_update_serialization_format_on_type!(FheInt128); + impl_update_serialization_format_on_type!(FheInt256); + impl_update_serialization_format_on_type!(CompressedFheInt8); + impl_update_serialization_format_on_type!(CompressedFheInt16); + impl_update_serialization_format_on_type!(CompressedFheInt32); + impl_update_serialization_format_on_type!(CompressedFheInt64); + impl_update_serialization_format_on_type!(CompressedFheInt128); + impl_update_serialization_format_on_type!(CompressedFheInt256); + + impl_safe_update_serialization_format_conformant_on_type!( + FheInt8, + crate::high_level_api::safe_deserialize_conformant + ); + impl_safe_update_serialization_format_conformant_on_type!( + FheInt16, + crate::high_level_api::safe_deserialize_conformant + ); + impl_safe_update_serialization_format_conformant_on_type!( + FheInt32, + crate::high_level_api::safe_deserialize_conformant + ); + impl_safe_update_serialization_format_conformant_on_type!( + FheInt64, + crate::high_level_api::safe_deserialize_conformant + ); + impl_safe_update_serialization_format_conformant_on_type!( + FheInt128, + crate::high_level_api::safe_deserialize_conformant + ); + impl_safe_update_serialization_format_conformant_on_type!( + FheInt256, + crate::high_level_api::safe_deserialize_conformant + ); + impl_safe_update_serialization_format_conformant_on_type!( + CompressedFheInt8, + crate::high_level_api::safe_deserialize_conformant + ); + impl_safe_update_serialization_format_conformant_on_type!( + CompressedFheInt16, + crate::high_level_api::safe_deserialize_conformant + ); + impl_safe_update_serialization_format_conformant_on_type!( + CompressedFheInt32, + crate::high_level_api::safe_deserialize_conformant + ); + impl_safe_update_serialization_format_conformant_on_type!( + CompressedFheInt64, + crate::high_level_api::safe_deserialize_conformant + ); + impl_safe_update_serialization_format_conformant_on_type!( + CompressedFheInt128, + crate::high_level_api::safe_deserialize_conformant + ); + impl_safe_update_serialization_format_conformant_on_type!( + CompressedFheInt256, + crate::high_level_api::safe_deserialize_conformant + ); +} + #[no_mangle] pub unsafe extern "C" fn compact_fhe_uint128_list_try_encrypt_with_compact_public_key_u128( input: *const U128, diff --git a/tfhe/src/c_api/high_level_api/keys.rs b/tfhe/src/c_api/high_level_api/keys.rs index da04699a3c..017f8f3187 100644 --- a/tfhe/src/c_api/high_level_api/keys.rs +++ b/tfhe/src/c_api/high_level_api/keys.rs @@ -33,6 +33,18 @@ impl_serialize_deserialize_on_type!(CompressedCompactPublicKey); impl_serialize_deserialize_on_type!(ServerKey); impl_serialize_deserialize_on_type!(CompressedServerKey); +#[cfg(feature = "forward_compatibility")] +mod forward_compatibility { + use super::*; + + impl_update_serialization_format_on_type!(ClientKey); + impl_update_serialization_format_on_type!(PublicKey); + impl_update_serialization_format_on_type!(CompactPublicKey); + impl_update_serialization_format_on_type!(CompressedCompactPublicKey); + impl_update_serialization_format_on_type!(ServerKey); + impl_update_serialization_format_on_type!(CompressedServerKey); +} + #[no_mangle] pub unsafe extern "C" fn generate_keys( config: *mut super::config::Config, diff --git a/tfhe/src/c_api/high_level_api/utils.rs b/tfhe/src/c_api/high_level_api/utils.rs index 3fd101e4ca..8e3eaf6869 100644 --- a/tfhe/src/c_api/high_level_api/utils.rs +++ b/tfhe/src/c_api/high_level_api/utils.rs @@ -642,3 +642,68 @@ macro_rules! define_casting_operation( )* } ); + +#[cfg(feature = "forward_compatibility")] +macro_rules! impl_update_serialization_format_on_type { + ($wrapper_type:ty) => { + ::paste::paste! { + #[no_mangle] + pub unsafe extern "C" fn [<$wrapper_type:snake _update_serialization_from_0_5_to_0_6>]( + buffer_view: tfhe_c_api_dynamic_buffer::DynamicBufferView, + result: *mut tfhe_c_api_dynamic_buffer::DynamicBuffer, + ) -> ::std::os::raw::c_int { + use crate::forward_compatibility::ConvertInto; + crate::c_api::utils::catch_panic(|| { + let object: $wrapper_type = $wrapper_type( + bincode::deserialize(buffer_view.as_slice()).unwrap() + ); + + let next_object: next_tfhe::$wrapper_type = (object.0).convert_into(); + + let buffer = bincode::serialize(&next_object).unwrap(); + + *result = buffer.into(); + }) + } + } + }; +} + +#[cfg(feature = "forward_compatibility")] +macro_rules! impl_safe_update_serialization_format_conformant_on_type { + ($wrapper_type:ty, $function_name:path) => { + ::paste::paste! { + #[no_mangle] + pub unsafe extern "C" fn [<$wrapper_type:snake _safe_update_serialization_conformant_from_0_5_to_0_6>]( + buffer_view: tfhe_c_api_dynamic_buffer::DynamicBufferView, + serialized_size_limit: u64, + server_key: *const crate::c_api::high_level_api::keys::ServerKey, + result: *mut tfhe_c_api_dynamic_buffer::DynamicBuffer, + ) -> ::std::os::raw::c_int { + crate::c_api::utils::catch_panic(|| { + use crate::forward_compatibility::ConvertInto; + crate::c_api::utils::check_ptr_is_non_null_and_aligned(result).unwrap(); + + let sk = crate::c_api::utils::get_ref_checked(server_key).unwrap(); + + let buffer_view: &[u8] = buffer_view.as_slice(); + + let object: $wrapper_type = $wrapper_type( + $function_name( + buffer_view, + serialized_size_limit, + &sk.0, + ) + .unwrap(), + ); + + let next_object: next_tfhe::$wrapper_type = (object.0).convert_into(); + + let buffer = bincode::serialize(&next_object).unwrap(); + + *result = buffer.into(); + }) + } + } + }; +} diff --git a/tfhe/src/forward_compatibility/boolean/ciphertext/mod.rs b/tfhe/src/forward_compatibility/boolean/ciphertext/mod.rs new file mode 100644 index 0000000000..0e914ee461 --- /dev/null +++ b/tfhe/src/forward_compatibility/boolean/ciphertext/mod.rs @@ -0,0 +1,64 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::boolean::ciphertext::CompressedCiphertext; +use next_tfhe::boolean::ciphertext::CompressedCiphertext as NextCompressedCiphertext; + +impl crate::forward_compatibility::ConvertFrom for NextCompressedCiphertext { + #[inline] + fn convert_from(value: CompressedCiphertext) -> Self { + let CompressedCiphertext { ciphertext } = value; + + Self::from_raw_parts(ciphertext.convert_into()) + } +} + +use crate::boolean::ciphertext::Ciphertext; +use next_tfhe::boolean::ciphertext::Ciphertext as NextCiphertext; + +impl crate::forward_compatibility::ConvertFrom for NextCiphertext { + #[inline] + fn convert_from(value: Ciphertext) -> Self { + match value { + Ciphertext::Encrypted(encrypted) => Self::Encrypted(encrypted.convert_into()), + Ciphertext::Trivial(trivial) => Self::Trivial(trivial), + } + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_ciphertext() { + use next_tfhe::boolean::ciphertext::Ciphertext as NextCiphertext; + + use crate::boolean::gen_keys; + + let (cks, sks) = gen_keys(); + + { + let tfhe_struct = cks.encrypt(true); + let _next_tfhe_struct: NextCiphertext = tfhe_struct.convert_into(); + } + + { + let tfhe_struct = sks.trivial_encrypt(true); + let _next_tfhe_struct: NextCiphertext = tfhe_struct.convert_into(); + } + } + + #[test] + fn test_conversion_compressed_ciphertext() { + use next_tfhe::boolean::ciphertext::CompressedCiphertext as NextCompressedCiphertext; + + use crate::boolean::gen_keys; + + let (cks, _sks) = gen_keys(); + + { + let tfhe_struct = cks.encrypt_compressed(true); + let _next_tfhe_struct: NextCompressedCiphertext = tfhe_struct.convert_into(); + } + } +} diff --git a/tfhe/src/forward_compatibility/boolean/client_key/mod.rs b/tfhe/src/forward_compatibility/boolean/client_key/mod.rs new file mode 100644 index 0000000000..bff5c3c8cb --- /dev/null +++ b/tfhe/src/forward_compatibility/boolean/client_key/mod.rs @@ -0,0 +1,33 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::boolean::client_key::ClientKey; +use next_tfhe::boolean::client_key::ClientKey as NextClientKey; + +impl crate::forward_compatibility::ConvertFrom for NextClientKey { + #[inline] + fn convert_from(value: ClientKey) -> Self { + let (lwe_secret_key, glwe_secret_key, parameters) = value.into_raw_parts(); + + Self::new_from_raw_parts( + lwe_secret_key.convert_into(), + glwe_secret_key.convert_into(), + parameters.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_client_key() { + use crate::boolean::client_key::ClientKey; + use next_tfhe::boolean::client_key::ClientKey as NextClientKey; + + use crate::boolean::parameters::DEFAULT_PARAMETERS_KS_PBS; + + let tfhe_struct = ClientKey::new(&DEFAULT_PARAMETERS_KS_PBS); + let _next_tfhe_struct: NextClientKey = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/boolean/engine/bootstrapping/mod.rs b/tfhe/src/forward_compatibility/boolean/engine/bootstrapping/mod.rs new file mode 100644 index 0000000000..56b47df77a --- /dev/null +++ b/tfhe/src/forward_compatibility/boolean/engine/bootstrapping/mod.rs @@ -0,0 +1,69 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::boolean::engine::bootstrapping::ServerKey; +use next_tfhe::boolean::engine::bootstrapping::ServerKey as NextServerKey; + +impl crate::forward_compatibility::ConvertFrom for NextServerKey { + #[inline] + fn convert_from(value: ServerKey) -> Self { + let ServerKey { + bootstrapping_key, + key_switching_key, + pbs_order, + } = value; + + Self::from_raw_parts( + bootstrapping_key.convert_into(), + key_switching_key.convert_into(), + pbs_order.convert_into(), + ) + } +} + +use crate::boolean::engine::bootstrapping::CompressedServerKey; +use next_tfhe::boolean::engine::bootstrapping::CompressedServerKey as NextCompressedServerKey; + +impl crate::forward_compatibility::ConvertFrom for NextCompressedServerKey { + #[inline] + fn convert_from(value: CompressedServerKey) -> Self { + let CompressedServerKey { + bootstrapping_key, + key_switching_key, + pbs_order, + } = value; + + Self::from_raw_parts( + bootstrapping_key.convert_into(), + key_switching_key.convert_into(), + pbs_order.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_server_key() { + use next_tfhe::boolean::engine::bootstrapping::ServerKey as NextServerKey; + + use crate::boolean::gen_keys; + + let (_cks, tfhe_struct) = gen_keys(); + let _next_tfhe_struct: NextServerKey = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_compressed_server_key() { + use crate::boolean::engine::bootstrapping::CompressedServerKey; + use next_tfhe::boolean::engine::bootstrapping::CompressedServerKey as NextCompressedServerKey; + + use crate::boolean::client_key::ClientKey; + use crate::boolean::parameters::DEFAULT_PARAMETERS_KS_PBS; + + let cks = ClientKey::new(&DEFAULT_PARAMETERS_KS_PBS); + let tfhe_struct = CompressedServerKey::new(&cks); + let _next_tfhe_struct: NextCompressedServerKey = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/boolean/engine/mod.rs b/tfhe/src/forward_compatibility/boolean/engine/mod.rs new file mode 100644 index 0000000000..ae37a906c1 --- /dev/null +++ b/tfhe/src/forward_compatibility/boolean/engine/mod.rs @@ -0,0 +1 @@ +pub mod bootstrapping; diff --git a/tfhe/src/forward_compatibility/boolean/key_switching_key/mod.rs b/tfhe/src/forward_compatibility/boolean/key_switching_key/mod.rs new file mode 100644 index 0000000000..3cee69707d --- /dev/null +++ b/tfhe/src/forward_compatibility/boolean/key_switching_key/mod.rs @@ -0,0 +1,38 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::boolean::key_switching_key::KeySwitchingKey; +use next_tfhe::boolean::key_switching_key::KeySwitchingKey as NextKeySwitchingKey; + +impl crate::forward_compatibility::ConvertFrom for NextKeySwitchingKey { + #[inline] + fn convert_from(value: KeySwitchingKey) -> Self { + let KeySwitchingKey { key_switching_key } = value; + + Self::from_raw_parts(key_switching_key.convert_into()) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_key_switching_key() { + use crate::boolean::key_switching_key::KeySwitchingKey; + use next_tfhe::boolean::key_switching_key::KeySwitchingKey as NextKeySwitchingKey; + + use crate::boolean::gen_keys; + use crate::boolean::parameters::BooleanKeySwitchingParameters; + + let (cks1, _sks1) = gen_keys(); + let (cks2, _sks2) = gen_keys(); + + let ksk_params = BooleanKeySwitchingParameters::new( + cks2.parameters.ks_base_log, + cks2.parameters.ks_level, + ); + + let tfhe_struct = KeySwitchingKey::new(&cks1, &cks2, ksk_params); + let _next_tfhe_struct: NextKeySwitchingKey = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/boolean/mod.rs b/tfhe/src/forward_compatibility/boolean/mod.rs new file mode 100644 index 0000000000..64e5b4d5ac --- /dev/null +++ b/tfhe/src/forward_compatibility/boolean/mod.rs @@ -0,0 +1,6 @@ +pub mod ciphertext; +pub mod client_key; +pub mod engine; +pub mod key_switching_key; +pub mod parameters; +pub mod public_key; diff --git a/tfhe/src/forward_compatibility/boolean/parameters/mod.rs b/tfhe/src/forward_compatibility/boolean/parameters/mod.rs new file mode 100644 index 0000000000..f4e3db9339 --- /dev/null +++ b/tfhe/src/forward_compatibility/boolean/parameters/mod.rs @@ -0,0 +1,109 @@ +use crate::boolean::parameters::BooleanParameters; +use crate::forward_compatibility::ConvertInto; +use next_tfhe::boolean::parameters::BooleanParameters as NextBooleanParameters; +use next_tfhe::core_crypto::commons::parameters::DynamicDistribution; + +impl crate::forward_compatibility::ConvertFrom for NextBooleanParameters { + #[inline] + fn convert_from(value: BooleanParameters) -> Self { + let BooleanParameters { + lwe_dimension, + glwe_dimension, + polynomial_size, + lwe_modular_std_dev, + glwe_modular_std_dev, + pbs_base_log, + pbs_level, + ks_base_log, + ks_level, + encryption_key_choice, + } = value; + Self { + lwe_dimension: lwe_dimension.convert_into(), + glwe_dimension: glwe_dimension.convert_into(), + polynomial_size: polynomial_size.convert_into(), + lwe_noise_distribution: DynamicDistribution::new_gaussian_from_std_dev( + lwe_modular_std_dev.convert_into(), + ), + glwe_noise_distribution: DynamicDistribution::new_gaussian_from_std_dev( + glwe_modular_std_dev.convert_into(), + ), + pbs_base_log: pbs_base_log.convert_into(), + pbs_level: pbs_level.convert_into(), + ks_base_log: ks_base_log.convert_into(), + ks_level: ks_level.convert_into(), + encryption_key_choice: encryption_key_choice.convert_into(), + } + } +} + +use crate::boolean::parameters::BooleanKeySwitchingParameters; +use next_tfhe::boolean::parameters::BooleanKeySwitchingParameters as NextBooleanKeySwitchingParameters; + +impl crate::forward_compatibility::ConvertFrom + for NextBooleanKeySwitchingParameters +{ + #[inline] + fn convert_from(value: BooleanKeySwitchingParameters) -> Self { + let BooleanKeySwitchingParameters { + ks_base_log, + ks_level, + } = value; + Self { + ks_base_log: ks_base_log.convert_into(), + ks_level: ks_level.convert_into(), + } + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_boolean_parameters() { + use crate::boolean::parameters::BooleanParameters; + use crate::core_crypto::commons::dispersion::StandardDev; + use crate::core_crypto::commons::parameters::*; + use next_tfhe::boolean::parameters::BooleanParameters as NextBooleanParameters; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + for encryption_key_choice in [EncryptionKeyChoice::Big, EncryptionKeyChoice::Small] { + let tfhe_struct = BooleanParameters { + lwe_dimension: LweDimension(rng.gen()), + glwe_dimension: GlweDimension(rng.gen()), + polynomial_size: PolynomialSize(rng.gen()), + lwe_modular_std_dev: StandardDev(rng.gen()), + glwe_modular_std_dev: StandardDev(rng.gen()), + pbs_base_log: DecompositionBaseLog(rng.gen()), + pbs_level: DecompositionLevelCount(rng.gen()), + ks_base_log: DecompositionBaseLog(rng.gen()), + ks_level: DecompositionLevelCount(rng.gen()), + encryption_key_choice, + }; + + let _next_tfhe_struct: NextBooleanParameters = tfhe_struct.convert_into(); + } + } + + #[test] + fn test_conversion_boolean_key_switching_parameters() { + use crate::boolean::parameters::BooleanKeySwitchingParameters; + use crate::core_crypto::commons::parameters::*; + use next_tfhe::boolean::parameters::BooleanKeySwitchingParameters as NextBooleanKeySwitchingParameters; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = BooleanKeySwitchingParameters { + ks_base_log: DecompositionBaseLog(rng.gen()), + ks_level: DecompositionLevelCount(rng.gen()), + }; + + let _next_tfhe_struct: NextBooleanKeySwitchingParameters = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/boolean/public_key/mod.rs b/tfhe/src/forward_compatibility/boolean/public_key/mod.rs new file mode 100644 index 0000000000..5e52a80227 --- /dev/null +++ b/tfhe/src/forward_compatibility/boolean/public_key/mod.rs @@ -0,0 +1,65 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::boolean::public_key::CompressedPublicKey; +use next_tfhe::boolean::public_key::CompressedPublicKey as NextCompressedPublicKey; + +impl crate::forward_compatibility::ConvertFrom for NextCompressedPublicKey { + #[inline] + fn convert_from(value: CompressedPublicKey) -> Self { + let CompressedPublicKey { + compressed_lwe_public_key, + parameters, + } = value; + + Self::from_raw_parts( + compressed_lwe_public_key.convert_into(), + parameters.convert_into(), + ) + } +} + +use crate::boolean::public_key::PublicKey; +use next_tfhe::boolean::public_key::PublicKey as NextPublicKey; + +impl crate::forward_compatibility::ConvertFrom for NextPublicKey { + #[inline] + fn convert_from(value: PublicKey) -> Self { + let PublicKey { + lwe_public_key, + parameters, + } = value; + + Self::from_raw_parts(lwe_public_key.convert_into(), parameters.convert_into()) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_compressed_public_key() { + use crate::boolean::public_key::CompressedPublicKey; + use next_tfhe::boolean::public_key::CompressedPublicKey as NextCompressedPublicKey; + + use crate::boolean::client_key::ClientKey; + use crate::boolean::parameters::DEFAULT_PARAMETERS_KS_PBS; + + let cks = ClientKey::new(&DEFAULT_PARAMETERS_KS_PBS); + let tfhe_struct = CompressedPublicKey::new(&cks); + let _next_tfhe_struct: NextCompressedPublicKey = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_public_key() { + use crate::boolean::public_key::PublicKey; + use next_tfhe::boolean::public_key::PublicKey as NextPublicKey; + + use crate::boolean::client_key::ClientKey; + use crate::boolean::parameters::DEFAULT_PARAMETERS_KS_PBS; + + let cks = ClientKey::new(&DEFAULT_PARAMETERS_KS_PBS); + let tfhe_struct = PublicKey::new(&cks); + let _next_tfhe_struct: NextPublicKey = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/commons/ciphertext_modulus/mod.rs b/tfhe/src/forward_compatibility/core_crypto/commons/ciphertext_modulus/mod.rs new file mode 100644 index 0000000000..30554c3e51 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/commons/ciphertext_modulus/mod.rs @@ -0,0 +1,41 @@ +use crate::core_crypto::commons::ciphertext_modulus::CiphertextModulus; +use crate::core_crypto::commons::numeric::UnsignedInteger; + +use next_tfhe::core_crypto::commons::ciphertext_modulus::CiphertextModulus as NextCiphertextModulus; +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; + +impl crate::forward_compatibility::ConvertFrom> + for NextCiphertextModulus +where + Scalar: UnsignedInteger + NextUnsignedInteger, +{ + #[inline] + fn convert_from(value: CiphertextModulus) -> Self { + if value.is_native_modulus() { + Self::new_native() + } else { + Self::new(value.get_custom_modulus()) + } + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_ciphertext_modulus() { + use crate::core_crypto::commons::ciphertext_modulus::CiphertextModulus; + use next_tfhe::core_crypto::commons::ciphertext_modulus::CiphertextModulus as NextCiphertextModulus; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = CiphertextModulus::new(0); + let _next_tfhe_struct: NextCiphertextModulus = tfhe_struct.convert_into(); + + let tfhe_struct = CiphertextModulus::new(rng.gen_range(0..=(1 << 64))); + let _next_tfhe_struct: NextCiphertextModulus = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/commons/dispersion/mod.rs b/tfhe/src/forward_compatibility/core_crypto/commons/dispersion/mod.rs new file mode 100644 index 0000000000..60f7b15d75 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/commons/dispersion/mod.rs @@ -0,0 +1,27 @@ +use crate::core_crypto::commons::dispersion::StandardDev; +use next_tfhe::core_crypto::commons::dispersion::StandardDev as NextStandardDev; + +impl crate::forward_compatibility::ConvertFrom for NextStandardDev { + #[inline] + fn convert_from(value: StandardDev) -> Self { + let StandardDev(field_0) = value; + Self(field_0) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + #[test] + fn test_conversion_standard_dev() { + use crate::core_crypto::commons::dispersion::StandardDev; + use next_tfhe::core_crypto::commons::dispersion::StandardDev as NextStandardDev; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = StandardDev(rng.gen()); + let _next_tfhe_struct: NextStandardDev = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/commons/math/mod.rs b/tfhe/src/forward_compatibility/core_crypto/commons/math/mod.rs new file mode 100644 index 0000000000..7bcbfe0e20 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/commons/math/mod.rs @@ -0,0 +1 @@ +pub mod random; diff --git a/tfhe/src/forward_compatibility/core_crypto/commons/math/random/mod.rs b/tfhe/src/forward_compatibility/core_crypto/commons/math/random/mod.rs new file mode 100644 index 0000000000..b958a2b464 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/commons/math/random/mod.rs @@ -0,0 +1,45 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::core_crypto::commons::math::random::Seed; +use next_tfhe::core_crypto::commons::math::random::Seed as NextSeed; + +impl crate::forward_compatibility::ConvertFrom for NextSeed { + #[inline] + fn convert_from(value: Seed) -> Self { + let Seed(seed) = value; + Self(seed) + } +} + +use crate::core_crypto::commons::math::random::CompressionSeed; +use next_tfhe::core_crypto::commons::math::random::CompressionSeed as NextCompressionSeed; + +impl crate::forward_compatibility::ConvertFrom for NextCompressionSeed { + #[inline] + fn convert_from(value: CompressionSeed) -> Self { + let CompressionSeed { seed } = value; + Self { + seed: seed.convert_into(), + } + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_compression_seed() { + use crate::core_crypto::commons::math::random::{CompressionSeed, Seed}; + use next_tfhe::core_crypto::commons::math::random::CompressionSeed as NextCompressionSeed; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = CompressionSeed { + seed: Seed(rng.gen()), + }; + let _next_tfhe_struct: NextCompressionSeed = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/commons/mod.rs b/tfhe/src/forward_compatibility/core_crypto/commons/mod.rs new file mode 100644 index 0000000000..d61dc2f223 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/commons/mod.rs @@ -0,0 +1,4 @@ +pub mod ciphertext_modulus; +pub mod dispersion; +pub mod math; +pub mod parameters; diff --git a/tfhe/src/forward_compatibility/core_crypto/commons/parameters/mod.rs b/tfhe/src/forward_compatibility/core_crypto/commons/parameters/mod.rs new file mode 100644 index 0000000000..4f008345b2 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/commons/parameters/mod.rs @@ -0,0 +1,790 @@ +use crate::core_crypto::commons::parameters::PlaintextCount; +use next_tfhe::core_crypto::commons::parameters::PlaintextCount as NextPlaintextCount; + +impl crate::forward_compatibility::ConvertFrom for NextPlaintextCount { + #[inline] + fn convert_from(value: PlaintextCount) -> Self { + let PlaintextCount(field_0) = value; + Self(field_0) + } +} + +use crate::core_crypto::commons::parameters::CleartextCount; +use next_tfhe::core_crypto::commons::parameters::CleartextCount as NextCleartextCount; + +impl crate::forward_compatibility::ConvertFrom for NextCleartextCount { + #[inline] + fn convert_from(value: CleartextCount) -> Self { + let CleartextCount(field_0) = value; + Self(field_0) + } +} + +use crate::core_crypto::commons::parameters::CiphertextCount; +use next_tfhe::core_crypto::commons::parameters::CiphertextCount as NextCiphertextCount; + +impl crate::forward_compatibility::ConvertFrom for NextCiphertextCount { + #[inline] + fn convert_from(value: CiphertextCount) -> Self { + let CiphertextCount(field_0) = value; + Self(field_0) + } +} + +use crate::core_crypto::commons::parameters::LweCiphertextCount; +use next_tfhe::core_crypto::commons::parameters::LweCiphertextCount as NextLweCiphertextCount; + +impl crate::forward_compatibility::ConvertFrom for NextLweCiphertextCount { + #[inline] + fn convert_from(value: LweCiphertextCount) -> Self { + let LweCiphertextCount(field_0) = value; + Self(field_0) + } +} + +use crate::core_crypto::commons::parameters::GlweCiphertextCount; +use next_tfhe::core_crypto::commons::parameters::GlweCiphertextCount as NextGlweCiphertextCount; + +impl crate::forward_compatibility::ConvertFrom for NextGlweCiphertextCount { + #[inline] + fn convert_from(value: GlweCiphertextCount) -> Self { + let GlweCiphertextCount(field_0) = value; + Self(field_0) + } +} + +use crate::core_crypto::commons::parameters::GswCiphertextCount; +use next_tfhe::core_crypto::commons::parameters::GswCiphertextCount as NextGswCiphertextCount; + +impl crate::forward_compatibility::ConvertFrom for NextGswCiphertextCount { + #[inline] + fn convert_from(value: GswCiphertextCount) -> Self { + let GswCiphertextCount(field_0) = value; + Self(field_0) + } +} + +use crate::core_crypto::commons::parameters::GgswCiphertextCount; +use next_tfhe::core_crypto::commons::parameters::GgswCiphertextCount as NextGgswCiphertextCount; + +impl crate::forward_compatibility::ConvertFrom for NextGgswCiphertextCount { + #[inline] + fn convert_from(value: GgswCiphertextCount) -> Self { + let GgswCiphertextCount(field_0) = value; + Self(field_0) + } +} + +use crate::core_crypto::commons::parameters::LweSize; +use next_tfhe::core_crypto::commons::parameters::LweSize as NextLweSize; + +impl crate::forward_compatibility::ConvertFrom for NextLweSize { + #[inline] + fn convert_from(value: LweSize) -> Self { + let LweSize(field_0) = value; + Self(field_0) + } +} + +use crate::core_crypto::commons::parameters::LweDimension; +use next_tfhe::core_crypto::commons::parameters::LweDimension as NextLweDimension; + +impl crate::forward_compatibility::ConvertFrom for NextLweDimension { + #[inline] + fn convert_from(value: LweDimension) -> Self { + let LweDimension(field_0) = value; + Self(field_0) + } +} + +use crate::core_crypto::commons::parameters::LwePublicKeyZeroEncryptionCount; +use next_tfhe::core_crypto::commons::parameters::LwePublicKeyZeroEncryptionCount as NextLwePublicKeyZeroEncryptionCount; + +impl crate::forward_compatibility::ConvertFrom + for NextLwePublicKeyZeroEncryptionCount +{ + #[inline] + fn convert_from(value: LwePublicKeyZeroEncryptionCount) -> Self { + let LwePublicKeyZeroEncryptionCount(field_0) = value; + Self(field_0) + } +} + +use crate::core_crypto::commons::parameters::LweMaskCount; +use next_tfhe::core_crypto::commons::parameters::LweMaskCount as NextLweMaskCount; + +impl crate::forward_compatibility::ConvertFrom for NextLweMaskCount { + #[inline] + fn convert_from(value: LweMaskCount) -> Self { + let LweMaskCount(field_0) = value; + Self(field_0) + } +} + +use crate::core_crypto::commons::parameters::LweBodyCount; +use next_tfhe::core_crypto::commons::parameters::LweBodyCount as NextLweBodyCount; + +impl crate::forward_compatibility::ConvertFrom for NextLweBodyCount { + #[inline] + fn convert_from(value: LweBodyCount) -> Self { + let LweBodyCount(field_0) = value; + Self(field_0) + } +} + +use crate::core_crypto::commons::parameters::GlweSize; +use next_tfhe::core_crypto::commons::parameters::GlweSize as NextGlweSize; + +impl crate::forward_compatibility::ConvertFrom for NextGlweSize { + #[inline] + fn convert_from(value: GlweSize) -> Self { + let GlweSize(field_0) = value; + Self(field_0) + } +} + +use crate::core_crypto::commons::parameters::GlweDimension; +use next_tfhe::core_crypto::commons::parameters::GlweDimension as NextGlweDimension; + +impl crate::forward_compatibility::ConvertFrom for NextGlweDimension { + #[inline] + fn convert_from(value: GlweDimension) -> Self { + let GlweDimension(field_0) = value; + Self(field_0) + } +} + +use crate::core_crypto::commons::parameters::PolynomialSize; +use next_tfhe::core_crypto::commons::parameters::PolynomialSize as NextPolynomialSize; + +impl crate::forward_compatibility::ConvertFrom for NextPolynomialSize { + #[inline] + fn convert_from(value: PolynomialSize) -> Self { + let PolynomialSize(field_0) = value; + Self(field_0) + } +} + +use crate::core_crypto::commons::parameters::FourierPolynomialSize; +use next_tfhe::core_crypto::commons::parameters::FourierPolynomialSize as NextFourierPolynomialSize; + +impl crate::forward_compatibility::ConvertFrom + for NextFourierPolynomialSize +{ + #[inline] + fn convert_from(value: FourierPolynomialSize) -> Self { + let FourierPolynomialSize(field_0) = value; + Self(field_0) + } +} + +use crate::core_crypto::commons::parameters::PolynomialSizeLog; +use next_tfhe::core_crypto::commons::parameters::PolynomialSizeLog as NextPolynomialSizeLog; + +impl crate::forward_compatibility::ConvertFrom for NextPolynomialSizeLog { + #[inline] + fn convert_from(value: PolynomialSizeLog) -> Self { + let PolynomialSizeLog(field_0) = value; + Self(field_0) + } +} + +use crate::core_crypto::commons::parameters::PolynomialCount; +use next_tfhe::core_crypto::commons::parameters::PolynomialCount as NextPolynomialCount; + +impl crate::forward_compatibility::ConvertFrom for NextPolynomialCount { + #[inline] + fn convert_from(value: PolynomialCount) -> Self { + let PolynomialCount(field_0) = value; + Self(field_0) + } +} + +use crate::core_crypto::commons::parameters::MonomialDegree; +use next_tfhe::core_crypto::commons::parameters::MonomialDegree as NextMonomialDegree; + +impl crate::forward_compatibility::ConvertFrom for NextMonomialDegree { + #[inline] + fn convert_from(value: MonomialDegree) -> Self { + let MonomialDegree(field_0) = value; + Self(field_0) + } +} + +use crate::core_crypto::commons::parameters::DecompositionBaseLog; +use next_tfhe::core_crypto::commons::parameters::DecompositionBaseLog as NextDecompositionBaseLog; + +impl crate::forward_compatibility::ConvertFrom for NextDecompositionBaseLog { + #[inline] + fn convert_from(value: DecompositionBaseLog) -> Self { + let DecompositionBaseLog(field_0) = value; + Self(field_0) + } +} + +use crate::core_crypto::commons::parameters::DecompositionLevelCount; +use next_tfhe::core_crypto::commons::parameters::DecompositionLevelCount as NextDecompositionLevelCount; + +impl crate::forward_compatibility::ConvertFrom + for NextDecompositionLevelCount +{ + #[inline] + fn convert_from(value: DecompositionLevelCount) -> Self { + let DecompositionLevelCount(field_0) = value; + Self(field_0) + } +} + +use crate::core_crypto::commons::parameters::LutCountLog; +use next_tfhe::core_crypto::commons::parameters::LutCountLog as NextLutCountLog; + +impl crate::forward_compatibility::ConvertFrom for NextLutCountLog { + #[inline] + fn convert_from(value: LutCountLog) -> Self { + let LutCountLog(field_0) = value; + Self(field_0) + } +} + +use crate::core_crypto::commons::parameters::ModulusSwitchOffset; +use next_tfhe::core_crypto::commons::parameters::ModulusSwitchOffset as NextModulusSwitchOffset; + +impl crate::forward_compatibility::ConvertFrom for NextModulusSwitchOffset { + #[inline] + fn convert_from(value: ModulusSwitchOffset) -> Self { + let ModulusSwitchOffset(field_0) = value; + Self(field_0) + } +} + +use crate::core_crypto::commons::parameters::DeltaLog; +use next_tfhe::core_crypto::commons::parameters::DeltaLog as NextDeltaLog; + +impl crate::forward_compatibility::ConvertFrom for NextDeltaLog { + #[inline] + fn convert_from(value: DeltaLog) -> Self { + let DeltaLog(field_0) = value; + Self(field_0) + } +} + +use crate::core_crypto::commons::parameters::ExtractedBitsCount; +use next_tfhe::core_crypto::commons::parameters::ExtractedBitsCount as NextExtractedBitsCount; + +impl crate::forward_compatibility::ConvertFrom for NextExtractedBitsCount { + #[inline] + fn convert_from(value: ExtractedBitsCount) -> Self { + let ExtractedBitsCount(field_0) = value; + Self(field_0) + } +} + +use crate::core_crypto::commons::parameters::FunctionalPackingKeyswitchKeyCount; +use next_tfhe::core_crypto::commons::parameters::FunctionalPackingKeyswitchKeyCount as NextFunctionalPackingKeyswitchKeyCount; + +impl crate::forward_compatibility::ConvertFrom + for NextFunctionalPackingKeyswitchKeyCount +{ + #[inline] + fn convert_from(value: FunctionalPackingKeyswitchKeyCount) -> Self { + let FunctionalPackingKeyswitchKeyCount(field_0) = value; + Self(field_0) + } +} + +use crate::core_crypto::commons::parameters::CiphertextModulusLog; +use next_tfhe::core_crypto::commons::parameters::CiphertextModulusLog as NextCiphertextModulusLog; + +impl crate::forward_compatibility::ConvertFrom for NextCiphertextModulusLog { + #[inline] + fn convert_from(value: CiphertextModulusLog) -> Self { + let CiphertextModulusLog(field_0) = value; + Self(field_0) + } +} + +use crate::core_crypto::commons::parameters::ThreadCount; +use next_tfhe::core_crypto::commons::parameters::ThreadCount as NextThreadCount; + +impl crate::forward_compatibility::ConvertFrom for NextThreadCount { + #[inline] + fn convert_from(value: ThreadCount) -> Self { + let ThreadCount(field_0) = value; + Self(field_0) + } +} + +use crate::core_crypto::commons::parameters::LweBskGroupingFactor; +use next_tfhe::core_crypto::commons::parameters::LweBskGroupingFactor as NextLweBskGroupingFactor; + +impl crate::forward_compatibility::ConvertFrom for NextLweBskGroupingFactor { + #[inline] + fn convert_from(value: LweBskGroupingFactor) -> Self { + let LweBskGroupingFactor(field_0) = value; + Self(field_0) + } +} + +use crate::core_crypto::commons::parameters::GgswPerLweMultiBitBskElement; +use next_tfhe::core_crypto::commons::parameters::GgswPerLweMultiBitBskElement as NextGgswPerLweMultiBitBskElement; + +impl crate::forward_compatibility::ConvertFrom + for NextGgswPerLweMultiBitBskElement +{ + #[inline] + fn convert_from(value: GgswPerLweMultiBitBskElement) -> Self { + let GgswPerLweMultiBitBskElement(field_0) = value; + Self(field_0) + } +} + +use crate::core_crypto::commons::parameters::EncryptionKeyChoice; +use next_tfhe::core_crypto::commons::parameters::EncryptionKeyChoice as NextEncryptionKeyChoice; + +impl crate::forward_compatibility::ConvertFrom for NextEncryptionKeyChoice { + #[inline] + fn convert_from(value: EncryptionKeyChoice) -> Self { + match value { + EncryptionKeyChoice::Big => Self::Big, + EncryptionKeyChoice::Small => Self::Small, + } + } +} + +use crate::core_crypto::commons::parameters::PBSOrder; +use next_tfhe::core_crypto::commons::parameters::PBSOrder as NextPBSOrder; + +impl crate::forward_compatibility::ConvertFrom for NextPBSOrder { + #[inline] + fn convert_from(value: PBSOrder) -> Self { + match value { + PBSOrder::KeyswitchBootstrap => Self::KeyswitchBootstrap, + PBSOrder::BootstrapKeyswitch => Self::BootstrapKeyswitch, + } + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_plaintext_count() { + use crate::core_crypto::commons::parameters::PlaintextCount; + use next_tfhe::core_crypto::commons::parameters::PlaintextCount as NextPlaintextCount; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = PlaintextCount(rng.gen()); + let _next_tfhe_struct: NextPlaintextCount = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_cleartext_count() { + use crate::core_crypto::commons::parameters::CleartextCount; + use next_tfhe::core_crypto::commons::parameters::CleartextCount as NextCleartextCount; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = CleartextCount(rng.gen()); + let _next_tfhe_struct: NextCleartextCount = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_ciphertext_count() { + use crate::core_crypto::commons::parameters::CiphertextCount; + use next_tfhe::core_crypto::commons::parameters::CiphertextCount as NextCiphertextCount; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = CiphertextCount(rng.gen()); + let _next_tfhe_struct: NextCiphertextCount = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_lwe_ciphertext_count() { + use crate::core_crypto::commons::parameters::LweCiphertextCount; + use next_tfhe::core_crypto::commons::parameters::LweCiphertextCount as NextLweCiphertextCount; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = LweCiphertextCount(rng.gen()); + let _next_tfhe_struct: NextLweCiphertextCount = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_glwe_ciphertext_count() { + use crate::core_crypto::commons::parameters::GlweCiphertextCount; + use next_tfhe::core_crypto::commons::parameters::GlweCiphertextCount as NextGlweCiphertextCount; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = GlweCiphertextCount(rng.gen()); + let _next_tfhe_struct: NextGlweCiphertextCount = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_gsw_ciphertext_count() { + use crate::core_crypto::commons::parameters::GswCiphertextCount; + use next_tfhe::core_crypto::commons::parameters::GswCiphertextCount as NextGswCiphertextCount; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = GswCiphertextCount(rng.gen()); + let _next_tfhe_struct: NextGswCiphertextCount = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_ggsw_ciphertext_count() { + use crate::core_crypto::commons::parameters::GgswCiphertextCount; + use next_tfhe::core_crypto::commons::parameters::GgswCiphertextCount as NextGgswCiphertextCount; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = GgswCiphertextCount(rng.gen()); + let _next_tfhe_struct: NextGgswCiphertextCount = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_lwe_size() { + use crate::core_crypto::commons::parameters::LweSize; + use next_tfhe::core_crypto::commons::parameters::LweSize as NextLweSize; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = LweSize(rng.gen()); + let _next_tfhe_struct: NextLweSize = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_lwe_dimension() { + use crate::core_crypto::commons::parameters::LweDimension; + use next_tfhe::core_crypto::commons::parameters::LweDimension as NextLweDimension; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = LweDimension(rng.gen()); + let _next_tfhe_struct: NextLweDimension = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_lwe_public_key_zero_encryption_count() { + use crate::core_crypto::commons::parameters::LwePublicKeyZeroEncryptionCount; + use next_tfhe::core_crypto::commons::parameters::LwePublicKeyZeroEncryptionCount as NextLwePublicKeyZeroEncryptionCount; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = LwePublicKeyZeroEncryptionCount(rng.gen()); + let _next_tfhe_struct: NextLwePublicKeyZeroEncryptionCount = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_lwe_mask_count() { + use crate::core_crypto::commons::parameters::LweMaskCount; + use next_tfhe::core_crypto::commons::parameters::LweMaskCount as NextLweMaskCount; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = LweMaskCount(rng.gen()); + let _next_tfhe_struct: NextLweMaskCount = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_lwe_body_count() { + use crate::core_crypto::commons::parameters::LweBodyCount; + use next_tfhe::core_crypto::commons::parameters::LweBodyCount as NextLweBodyCount; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = LweBodyCount(rng.gen()); + let _next_tfhe_struct: NextLweBodyCount = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_glwe_size() { + use crate::core_crypto::commons::parameters::GlweSize; + use next_tfhe::core_crypto::commons::parameters::GlweSize as NextGlweSize; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = GlweSize(rng.gen()); + let _next_tfhe_struct: NextGlweSize = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_glwe_dimension() { + use crate::core_crypto::commons::parameters::GlweDimension; + use next_tfhe::core_crypto::commons::parameters::GlweDimension as NextGlweDimension; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = GlweDimension(rng.gen()); + let _next_tfhe_struct: NextGlweDimension = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_polynomial_size() { + use crate::core_crypto::commons::parameters::PolynomialSize; + use next_tfhe::core_crypto::commons::parameters::PolynomialSize as NextPolynomialSize; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = PolynomialSize(rng.gen()); + let _next_tfhe_struct: NextPolynomialSize = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_fourier_polynomial_size() { + use crate::core_crypto::commons::parameters::FourierPolynomialSize; + use next_tfhe::core_crypto::commons::parameters::FourierPolynomialSize as NextFourierPolynomialSize; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = FourierPolynomialSize(rng.gen()); + let _next_tfhe_struct: NextFourierPolynomialSize = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_polynomial_size_log() { + use crate::core_crypto::commons::parameters::PolynomialSizeLog; + use next_tfhe::core_crypto::commons::parameters::PolynomialSizeLog as NextPolynomialSizeLog; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = PolynomialSizeLog(rng.gen()); + let _next_tfhe_struct: NextPolynomialSizeLog = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_polynomial_count() { + use crate::core_crypto::commons::parameters::PolynomialCount; + use next_tfhe::core_crypto::commons::parameters::PolynomialCount as NextPolynomialCount; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = PolynomialCount(rng.gen()); + let _next_tfhe_struct: NextPolynomialCount = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_monomial_degree() { + use crate::core_crypto::commons::parameters::MonomialDegree; + use next_tfhe::core_crypto::commons::parameters::MonomialDegree as NextMonomialDegree; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = MonomialDegree(rng.gen()); + let _next_tfhe_struct: NextMonomialDegree = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_decomposition_base_log() { + use crate::core_crypto::commons::parameters::DecompositionBaseLog; + use next_tfhe::core_crypto::commons::parameters::DecompositionBaseLog as NextDecompositionBaseLog; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = DecompositionBaseLog(rng.gen()); + let _next_tfhe_struct: NextDecompositionBaseLog = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_decomposition_level_count() { + use crate::core_crypto::commons::parameters::DecompositionLevelCount; + use next_tfhe::core_crypto::commons::parameters::DecompositionLevelCount as NextDecompositionLevelCount; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = DecompositionLevelCount(rng.gen()); + let _next_tfhe_struct: NextDecompositionLevelCount = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_lut_count_log() { + use crate::core_crypto::commons::parameters::LutCountLog; + use next_tfhe::core_crypto::commons::parameters::LutCountLog as NextLutCountLog; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = LutCountLog(rng.gen()); + let _next_tfhe_struct: NextLutCountLog = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_modulus_switch_offset() { + use crate::core_crypto::commons::parameters::ModulusSwitchOffset; + use next_tfhe::core_crypto::commons::parameters::ModulusSwitchOffset as NextModulusSwitchOffset; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = ModulusSwitchOffset(rng.gen()); + let _next_tfhe_struct: NextModulusSwitchOffset = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_delta_log() { + use crate::core_crypto::commons::parameters::DeltaLog; + use next_tfhe::core_crypto::commons::parameters::DeltaLog as NextDeltaLog; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = DeltaLog(rng.gen()); + let _next_tfhe_struct: NextDeltaLog = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_extracted_bits_count() { + use crate::core_crypto::commons::parameters::ExtractedBitsCount; + use next_tfhe::core_crypto::commons::parameters::ExtractedBitsCount as NextExtractedBitsCount; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = ExtractedBitsCount(rng.gen()); + let _next_tfhe_struct: NextExtractedBitsCount = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_functional_packing_keyswitch_key_count() { + use crate::core_crypto::commons::parameters::FunctionalPackingKeyswitchKeyCount; + use next_tfhe::core_crypto::commons::parameters::FunctionalPackingKeyswitchKeyCount as NextFunctionalPackingKeyswitchKeyCount; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = FunctionalPackingKeyswitchKeyCount(rng.gen()); + let _next_tfhe_struct: NextFunctionalPackingKeyswitchKeyCount = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_ciphertext_modulus_log() { + use crate::core_crypto::commons::parameters::CiphertextModulusLog; + use next_tfhe::core_crypto::commons::parameters::CiphertextModulusLog as NextCiphertextModulusLog; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = CiphertextModulusLog(rng.gen()); + let _next_tfhe_struct: NextCiphertextModulusLog = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_thread_count() { + use crate::core_crypto::commons::parameters::ThreadCount; + use next_tfhe::core_crypto::commons::parameters::ThreadCount as NextThreadCount; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = ThreadCount(rng.gen()); + let _next_tfhe_struct: NextThreadCount = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_lwe_bsk_grouping_factor() { + use crate::core_crypto::commons::parameters::LweBskGroupingFactor; + use next_tfhe::core_crypto::commons::parameters::LweBskGroupingFactor as NextLweBskGroupingFactor; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = LweBskGroupingFactor(rng.gen()); + let _next_tfhe_struct: NextLweBskGroupingFactor = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_ggsw_per_lwe_multi_bit_bsk_element() { + use crate::core_crypto::commons::parameters::GgswPerLweMultiBitBskElement; + use next_tfhe::core_crypto::commons::parameters::GgswPerLweMultiBitBskElement as NextGgswPerLweMultiBitBskElement; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = GgswPerLweMultiBitBskElement(rng.gen()); + let _next_tfhe_struct: NextGgswPerLweMultiBitBskElement = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_encryption_key_choice() { + use crate::core_crypto::commons::parameters::EncryptionKeyChoice; + use next_tfhe::core_crypto::commons::parameters::EncryptionKeyChoice as NextEncryptionKeyChoice; + + let enum_val_big = EncryptionKeyChoice::Big; + let enum_val_small = EncryptionKeyChoice::Small; + + let next_enum_val_big: NextEncryptionKeyChoice = enum_val_big.convert_into(); + let next_enum_val_small: NextEncryptionKeyChoice = enum_val_small.convert_into(); + + assert_eq!(next_enum_val_big, NextEncryptionKeyChoice::Big); + assert_eq!(next_enum_val_small, NextEncryptionKeyChoice::Small); + } + + #[test] + fn test_conversion_pbs_order() { + use crate::core_crypto::commons::parameters::PBSOrder; + use next_tfhe::core_crypto::commons::parameters::PBSOrder as NextPBSOrder; + + let enum_val_pbs_ks = PBSOrder::BootstrapKeyswitch; + let enum_val_ks_pbs = PBSOrder::KeyswitchBootstrap; + + let next_enum_val_pbs_ks: NextPBSOrder = enum_val_pbs_ks.convert_into(); + let next_enum_val_ls_pbs: NextPBSOrder = enum_val_ks_pbs.convert_into(); + + assert_eq!(next_enum_val_pbs_ks, NextPBSOrder::BootstrapKeyswitch); + assert_eq!(next_enum_val_ls_pbs, NextPBSOrder::KeyswitchBootstrap); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/cleartext/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/cleartext/mod.rs new file mode 100644 index 0000000000..96bbabbb61 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/cleartext/mod.rs @@ -0,0 +1,34 @@ +use crate::core_crypto::entities::cleartext::Cleartext; +use next_tfhe::core_crypto::entities::cleartext::Cleartext as NextCleartext; + +use crate::core_crypto::commons::numeric::Numeric; + +use next_tfhe::core_crypto::commons::numeric::Numeric as NextNumeric; + +impl crate::forward_compatibility::ConvertFrom> for NextCleartext +where + Scalar: Numeric + NextNumeric, +{ + #[inline] + fn convert_from(value: Cleartext) -> Self { + let Cleartext(field_0) = value; + Self(field_0) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_cleartext() { + use crate::core_crypto::entities::cleartext::Cleartext; + use next_tfhe::core_crypto::entities::cleartext::Cleartext as NextCleartext; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + let tfhe_struct = Cleartext(rng.gen::()); + let _next_tfhe_struct: NextCleartext<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/ggsw_ciphertext/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/ggsw_ciphertext/mod.rs new file mode 100644 index 0000000000..c9b00112ae --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/ggsw_ciphertext/mod.rs @@ -0,0 +1,61 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::core_crypto::entities::ggsw_ciphertext::GgswCiphertext; +use next_tfhe::core_crypto::entities::ggsw_ciphertext::GgswCiphertext as NextGgswCiphertext; + +use crate::core_crypto::commons::numeric::UnsignedInteger; +use crate::core_crypto::commons::traits::Container; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; + +impl crate::forward_compatibility::ConvertFrom> + for NextGgswCiphertext +where + Scalar: UnsignedInteger + NextUnsignedInteger, + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: GgswCiphertext) -> Self { + let glwe_size = value.glwe_size(); + let polynomial_size = value.polynomial_size(); + let decomp_base_log = value.decomposition_base_log(); + let ciphertext_modulus = value.ciphertext_modulus(); + let container = value.into_container(); + + Self::from_container( + container, + glwe_size.convert_into(), + polynomial_size.convert_into(), + decomp_base_log.convert_into(), + ciphertext_modulus.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_ggsw_ciphertext() { + use crate::core_crypto::commons::parameters::*; + use crate::core_crypto::entities::ggsw_ciphertext::GgswCiphertext; + use next_tfhe::core_crypto::entities::ggsw_ciphertext::GgswCiphertext as NextGgswCiphertext; + + let glwe_size = GlweSize(2); + let polynomial_size = PolynomialSize(2048); + let decomp_base_log = DecompositionBaseLog(23); + let container = vec![0u64; glwe_size.0 * glwe_size.0 * polynomial_size.0]; + let ciphertext_modulus = CiphertextModulus::new_native(); + let tfhe_struct = GgswCiphertext::from_container( + container, + glwe_size, + polynomial_size, + decomp_base_log, + ciphertext_modulus, + ); + + let _next_tfhe_struct: NextGgswCiphertext<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/ggsw_ciphertext_list/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/ggsw_ciphertext_list/mod.rs new file mode 100644 index 0000000000..0e4a68e6d5 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/ggsw_ciphertext_list/mod.rs @@ -0,0 +1,67 @@ +use crate::core_crypto::entities::ggsw_ciphertext_list::GgswCiphertextList; +use crate::forward_compatibility::ConvertInto; +use next_tfhe::core_crypto::entities::ggsw_ciphertext_list::GgswCiphertextList as NextGgswCiphertextList; + +use crate::core_crypto::commons::numeric::UnsignedInteger; +use crate::core_crypto::commons::traits::Container; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; + +impl crate::forward_compatibility::ConvertFrom> + for NextGgswCiphertextList +where + Scalar: UnsignedInteger + NextUnsignedInteger, + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: GgswCiphertextList) -> Self { + let glwe_size = value.glwe_size(); + let polynomial_size = value.polynomial_size(); + let decomp_base_log = value.decomposition_base_log(); + let decomp_level_count = value.decomposition_level_count(); + let ciphertext_modulus = value.ciphertext_modulus(); + let container = value.into_container(); + + Self::from_container( + container, + glwe_size.convert_into(), + polynomial_size.convert_into(), + decomp_base_log.convert_into(), + decomp_level_count.convert_into(), + ciphertext_modulus.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_ggsw_ciphertext_list() { + use crate::core_crypto::entities::ggsw_ciphertext_list::GgswCiphertextList; + use next_tfhe::core_crypto::entities::ggsw_ciphertext_list::GgswCiphertextList as NextGgswCiphertextList; + + use crate::core_crypto::commons::parameters::*; + + let glwe_size = GlweSize(2); + let polynomial_size = PolynomialSize(2048); + let decomp_base_log = DecompositionBaseLog(23); + let decomp_level_count = DecompositionLevelCount(1); + let ciphertext_count = GgswCiphertextCount(100); + let ciphertext_modulus = CiphertextModulus::new_native(); + + let tfhe_struct = GgswCiphertextList::new( + 0u64, + glwe_size, + polynomial_size, + decomp_base_log, + decomp_level_count, + ciphertext_count, + ciphertext_modulus, + ); + + let _next_tfhe_struct: NextGgswCiphertextList<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/glwe_ciphertext/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/glwe_ciphertext/mod.rs new file mode 100644 index 0000000000..e4bacd94aa --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/glwe_ciphertext/mod.rs @@ -0,0 +1,50 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::core_crypto::commons::numeric::UnsignedInteger; +use crate::core_crypto::commons::traits::Container; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; + +use crate::core_crypto::entities::glwe_ciphertext::GlweCiphertext; +use next_tfhe::core_crypto::entities::glwe_ciphertext::GlweCiphertext as NextGlweCiphertext; + +impl crate::forward_compatibility::ConvertFrom> + for NextGlweCiphertext +where + Scalar: UnsignedInteger + NextUnsignedInteger, + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: GlweCiphertext) -> Self { + let polynomial_size = value.polynomial_size(); + let ciphertext_modulus = value.ciphertext_modulus(); + let container = value.into_container(); + + Self::from_container( + container, + polynomial_size.convert_into(), + ciphertext_modulus.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_glwe_ciphertext() { + use crate::core_crypto::entities::glwe_ciphertext::GlweCiphertext; + use next_tfhe::core_crypto::entities::glwe_ciphertext::GlweCiphertext as NextGlweCiphertext; + + use crate::core_crypto::commons::parameters::*; + + let polynomial_size = PolynomialSize(2048); + let glwe_size = GlweSize(2); + let ciphertext_modulus = CiphertextModulus::new_native(); + + let tfhe_struct = GlweCiphertext::new(0u64, glwe_size, polynomial_size, ciphertext_modulus); + let _next_tfhe_struct: NextGlweCiphertext<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/glwe_ciphertext_list/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/glwe_ciphertext_list/mod.rs new file mode 100644 index 0000000000..7458e7de8b --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/glwe_ciphertext_list/mod.rs @@ -0,0 +1,58 @@ +use crate::core_crypto::entities::glwe_ciphertext_list::GlweCiphertextList; +use crate::forward_compatibility::ConvertInto; +use next_tfhe::core_crypto::entities::glwe_ciphertext_list::GlweCiphertextList as NextGlweCiphertextList; + +use crate::core_crypto::commons::numeric::UnsignedInteger; +use crate::core_crypto::commons::traits::Container; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; + +impl crate::forward_compatibility::ConvertFrom> + for NextGlweCiphertextList +where + Scalar: UnsignedInteger + NextUnsignedInteger, + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: GlweCiphertextList) -> Self { + let glwe_size = value.glwe_size(); + let polynomial_size = value.polynomial_size(); + let ciphertext_modulus = value.ciphertext_modulus(); + let container = value.into_container(); + + Self::from_container( + container, + glwe_size.convert_into(), + polynomial_size.convert_into(), + ciphertext_modulus.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_glwe_ciphertext_list() { + use crate::core_crypto::entities::glwe_ciphertext_list::GlweCiphertextList; + use next_tfhe::core_crypto::entities::glwe_ciphertext_list::GlweCiphertextList as NextGlweCiphertextList; + + use crate::core_crypto::commons::parameters::*; + + let polynomial_size = PolynomialSize(2048); + let glwe_size = GlweSize(2); + let glwe_ciphertext_count = GlweCiphertextCount(10); + let ciphertext_modulus = CiphertextModulus::new_native(); + + let tfhe_struct = GlweCiphertextList::new( + 0u64, + glwe_size, + polynomial_size, + glwe_ciphertext_count, + ciphertext_modulus, + ); + let _next_tfhe_struct: NextGlweCiphertextList<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/glwe_secret_key/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/glwe_secret_key/mod.rs new file mode 100644 index 0000000000..220a205a65 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/glwe_secret_key/mod.rs @@ -0,0 +1,43 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::core_crypto::entities::glwe_secret_key::GlweSecretKey; +use next_tfhe::core_crypto::entities::glwe_secret_key::GlweSecretKey as NextGlweSecretKey; + +use crate::core_crypto::commons::numeric::UnsignedInteger; +use crate::core_crypto::commons::traits::Container; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; + +impl crate::forward_compatibility::ConvertFrom> for NextGlweSecretKey +where + Scalar: UnsignedInteger + NextUnsignedInteger, + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: GlweSecretKey) -> Self { + let polynomial_size = value.polynomial_size(); + let container = value.into_container(); + + Self::from_container(container, polynomial_size.convert_into()) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_glwe_secret_key() { + use crate::core_crypto::entities::glwe_secret_key::GlweSecretKey; + use next_tfhe::core_crypto::entities::glwe_secret_key::GlweSecretKey as NextGlweSecretKey; + + use crate::core_crypto::commons::parameters::*; + + let polynomial_size = PolynomialSize(2048); + let glwe_dimension = GlweDimension(1); + + let tfhe_struct = GlweSecretKey::new_empty_key(0u64, glwe_dimension, polynomial_size); + let _next_tfhe_struct: NextGlweSecretKey<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/lwe_bootstrap_key/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/lwe_bootstrap_key/mod.rs new file mode 100644 index 0000000000..fbbdf39c07 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/lwe_bootstrap_key/mod.rs @@ -0,0 +1,66 @@ +use crate::core_crypto::entities::lwe_bootstrap_key::LweBootstrapKey; +use crate::forward_compatibility::ConvertInto; +use next_tfhe::core_crypto::entities::lwe_bootstrap_key::LweBootstrapKey as NextLweBootstrapKey; + +use crate::core_crypto::commons::numeric::UnsignedInteger; +use crate::core_crypto::commons::traits::Container; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; + +impl crate::forward_compatibility::ConvertFrom> + for NextLweBootstrapKey +where + Scalar: UnsignedInteger + NextUnsignedInteger, + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: LweBootstrapKey) -> Self { + let glwe_size = value.glwe_size(); + let polynomial_size = value.polynomial_size(); + let decomp_base_log = value.decomposition_base_log(); + let decomp_level_count = value.decomposition_level_count(); + let ciphertext_modulus = value.ciphertext_modulus(); + let container = value.into_container(); + + Self::from_container( + container, + glwe_size.convert_into(), + polynomial_size.convert_into(), + decomp_base_log.convert_into(), + decomp_level_count.convert_into(), + ciphertext_modulus.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_lwe_bootstrap_key() { + use crate::core_crypto::entities::lwe_bootstrap_key::LweBootstrapKey; + use next_tfhe::core_crypto::entities::lwe_bootstrap_key::LweBootstrapKey as NextLweBootstrapKey; + + use crate::core_crypto::commons::parameters::*; + + let input_lwe_dimension = LweDimension(100); + let polynomial_size = PolynomialSize(2048); + let glwe_size = GlweSize(2); + let decomp_base_log = DecompositionBaseLog(23); + let decomp_level_count = DecompositionLevelCount(1); + let ciphertext_modulus = CiphertextModulus::new_native(); + + let tfhe_struct = LweBootstrapKey::new( + 0u64, + glwe_size, + polynomial_size, + decomp_base_log, + decomp_level_count, + input_lwe_dimension, + ciphertext_modulus, + ); + let _next_tfhe_struct: NextLweBootstrapKey<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/lwe_ciphertext/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/lwe_ciphertext/mod.rs new file mode 100644 index 0000000000..f572bf24bf --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/lwe_ciphertext/mod.rs @@ -0,0 +1,42 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::core_crypto::entities::lwe_ciphertext::LweCiphertext; +use next_tfhe::core_crypto::entities::lwe_ciphertext::LweCiphertext as NextLweCiphertext; + +use crate::core_crypto::commons::numeric::UnsignedInteger; +use crate::core_crypto::commons::traits::Container; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; + +impl crate::forward_compatibility::ConvertFrom> for NextLweCiphertext +where + Scalar: UnsignedInteger + NextUnsignedInteger, + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: LweCiphertext) -> Self { + let ciphertext_modulus = value.ciphertext_modulus(); + let data = value.into_container(); + Self::from_container(data, ciphertext_modulus.convert_into()) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_lwe_ciphertext() { + use crate::core_crypto::entities::lwe_ciphertext::LweCiphertext; + use next_tfhe::core_crypto::entities::lwe_ciphertext::LweCiphertext as NextLweCiphertext; + + use crate::core_crypto::commons::parameters::*; + + let lwe_size = LweSize(101); + let ciphertext_modulus = CiphertextModulus::new_native(); + + let tfhe_struct = LweCiphertext::new(0u64, lwe_size, ciphertext_modulus); + let _next_tfhe_struct: NextLweCiphertext<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/lwe_ciphertext_list/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/lwe_ciphertext_list/mod.rs new file mode 100644 index 0000000000..06c5660c82 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/lwe_ciphertext_list/mod.rs @@ -0,0 +1,50 @@ +use crate::core_crypto::entities::lwe_ciphertext_list::LweCiphertextList; +use crate::forward_compatibility::ConvertInto; +use next_tfhe::core_crypto::entities::lwe_ciphertext_list::LweCiphertextList as NextLweCiphertextList; + +use crate::core_crypto::commons::numeric::UnsignedInteger; +use crate::core_crypto::commons::traits::Container; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; + +impl crate::forward_compatibility::ConvertFrom> + for NextLweCiphertextList +where + Scalar: UnsignedInteger + NextUnsignedInteger, + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: LweCiphertextList) -> Self { + let lwe_size = value.lwe_size(); + let ciphertext_modulus = value.ciphertext_modulus(); + let container = value.into_container(); + + Self::from_container( + container, + lwe_size.convert_into(), + ciphertext_modulus.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_lwe_ciphertext_list() { + use crate::core_crypto::entities::lwe_ciphertext_list::LweCiphertextList; + use next_tfhe::core_crypto::entities::lwe_ciphertext_list::LweCiphertextList as NextLweCiphertextList; + + use crate::core_crypto::commons::parameters::*; + + let lwe_size = LweSize(101); + let lwe_ciphertext_count = LweCiphertextCount(10); + let ciphertext_modulus = CiphertextModulus::new_native(); + + let tfhe_struct = + LweCiphertextList::new(0u64, lwe_size, lwe_ciphertext_count, ciphertext_modulus); + let _next_tfhe_struct: NextLweCiphertextList<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/lwe_compact_public_key/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/lwe_compact_public_key/mod.rs new file mode 100644 index 0000000000..bbfd5919b0 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/lwe_compact_public_key/mod.rs @@ -0,0 +1,42 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::core_crypto::entities::lwe_compact_public_key::LweCompactPublicKey; +use next_tfhe::core_crypto::entities::lwe_compact_public_key::LweCompactPublicKey as NextLweCompactPublicKey; + +use crate::core_crypto::commons::numeric::UnsignedInteger; +use crate::core_crypto::commons::traits::Container; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; + +impl crate::forward_compatibility::ConvertFrom> + for NextLweCompactPublicKey +where + Scalar: UnsignedInteger + NextUnsignedInteger, + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: LweCompactPublicKey) -> Self { + let ciphertext_modulus = value.ciphertext_modulus(); + let container = value.into_container(); + + Self::from_container(container, ciphertext_modulus.convert_into()) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_lwe_compact_public_key() { + use crate::core_crypto::entities::lwe_compact_public_key::LweCompactPublicKey; + use next_tfhe::core_crypto::entities::lwe_compact_public_key::LweCompactPublicKey as NextLweCompactPublicKey; + + use crate::core_crypto::commons::parameters::*; + + let tfhe_struct = + LweCompactPublicKey::new(0u64, LweDimension(1024), CiphertextModulus::new_native()); + let _next_tfhe_struct: NextLweCompactPublicKey<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/lwe_keyswitch_key/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/lwe_keyswitch_key/mod.rs new file mode 100644 index 0000000000..a01c41a9a4 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/lwe_keyswitch_key/mod.rs @@ -0,0 +1,57 @@ +use crate::core_crypto::entities::lwe_keyswitch_key::LweKeyswitchKey; +use crate::forward_compatibility::ConvertInto; +use next_tfhe::core_crypto::entities::lwe_keyswitch_key::LweKeyswitchKey as NextLweKeyswitchKey; + +use crate::core_crypto::commons::numeric::UnsignedInteger; +use crate::core_crypto::commons::traits::Container; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; + +impl crate::forward_compatibility::ConvertFrom> + for NextLweKeyswitchKey +where + Scalar: UnsignedInteger + NextUnsignedInteger, + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: LweKeyswitchKey) -> Self { + let decomp_base_log = value.decomposition_base_log(); + let decomp_level_count = value.decomposition_level_count(); + let output_lwe_size = value.output_lwe_size(); + let ciphertext_modulus = value.ciphertext_modulus(); + let container = value.into_container(); + + Self::from_container( + container, + decomp_base_log.convert_into(), + decomp_level_count.convert_into(), + output_lwe_size.convert_into(), + ciphertext_modulus.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_lwe_keyswitch_key() { + use crate::core_crypto::entities::lwe_keyswitch_key::LweKeyswitchKey; + use next_tfhe::core_crypto::entities::lwe_keyswitch_key::LweKeyswitchKey as NextLweKeyswitchKey; + + use crate::core_crypto::commons::parameters::*; + + let tfhe_struct = LweKeyswitchKey::new( + 0u64, + DecompositionBaseLog(5), + DecompositionLevelCount(3), + LweDimension(200), + LweDimension(100), + CiphertextModulus::new_native(), + ); + + let _next_tfhe_struct: NextLweKeyswitchKey<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/lwe_multi_bit_bootstrap_key/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/lwe_multi_bit_bootstrap_key/mod.rs new file mode 100644 index 0000000000..f11d990c39 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/lwe_multi_bit_bootstrap_key/mod.rs @@ -0,0 +1,157 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::core_crypto::entities::lwe_multi_bit_bootstrap_key::LweMultiBitBootstrapKey; +use crate::core_crypto::fft_impl::fft64::math::fft::FourierPolynomialList; +use next_tfhe::core_crypto::entities::lwe_multi_bit_bootstrap_key::LweMultiBitBootstrapKey as NextLweMultiBitBootstrapKey; +use next_tfhe::core_crypto::fft_impl::fft64::math::fft::FourierPolynomialList as NextFourierPolynomialList; + +use crate::core_crypto::commons::numeric::UnsignedInteger; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; + +impl crate::forward_compatibility::ConvertFrom> + for NextLweMultiBitBootstrapKey +where + Scalar: UnsignedInteger + NextUnsignedInteger, + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: LweMultiBitBootstrapKey) -> Self { + let glwe_size = value.glwe_size(); + let polynomial_size = value.polynomial_size(); + let decomp_base_log = value.decomposition_base_log(); + let decomp_level_count = value.decomposition_level_count(); + let grouping_factor = value.grouping_factor(); + let ciphertext_modulus = value.ciphertext_modulus(); + let container = value.into_container(); + + Self::from_container( + container, + glwe_size.convert_into(), + polynomial_size.convert_into(), + decomp_base_log.convert_into(), + decomp_level_count.convert_into(), + grouping_factor.convert_into(), + ciphertext_modulus.convert_into(), + ) + } +} + +use crate::core_crypto::entities::lwe_multi_bit_bootstrap_key::FourierLweMultiBitBootstrapKey; +use next_tfhe::core_crypto::entities::lwe_multi_bit_bootstrap_key::FourierLweMultiBitBootstrapKey as NextFourierLweMultiBitBootstrapKey; + +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; +use next_tfhe::core_crypto::fft_impl::fft64::{c64 as NextC64, ABox as NextABox}; + +use crate::core_crypto::commons::traits::Container; +use crate::core_crypto::fft_impl::fft64::c64; + +impl crate::forward_compatibility::ConvertFrom> + for NextFourierLweMultiBitBootstrapKey> +where + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: FourierLweMultiBitBootstrapKey) -> Self { + let input_lwe_dimension = value.input_lwe_dimension(); + let glwe_size = value.glwe_size(); + let polynomial_size = value.polynomial_size(); + let decomposition_base_log = value.decomposition_base_log(); + let decomposition_level_count = value.decomposition_level_count(); + let grouping_factor = value.grouping_factor(); + let data = value.data(); + + let poly_list = FourierPolynomialList { + data, + polynomial_size, + }; + + let next_poly_list: NextFourierPolynomialList<_> = poly_list.convert_into(); + let data = next_poly_list.data; + + Self::from_container( + data, + input_lwe_dimension.convert_into(), + glwe_size.convert_into(), + polynomial_size.convert_into(), + decomposition_base_log.convert_into(), + decomposition_level_count.convert_into(), + grouping_factor.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_lwe_multi_bit_bootstrap_key() { + use crate::core_crypto::entities::lwe_multi_bit_bootstrap_key::LweMultiBitBootstrapKey; + use next_tfhe::core_crypto::entities::lwe_multi_bit_bootstrap_key::LweMultiBitBootstrapKey as NextLweMultiBitBootstrapKey; + + use crate::core_crypto::commons::parameters::*; + + let input_lwe_dimension = LweDimension(100); + let polynomial_size = PolynomialSize(2048); + let glwe_size = GlweSize(2); + let decomp_base_log = DecompositionBaseLog(23); + let decomp_level_count = DecompositionLevelCount(1); + let grouping_factor = LweBskGroupingFactor(2); + let ciphertext_modulus = CiphertextModulus::new_native(); + + let tfhe_struct = LweMultiBitBootstrapKey::new( + 0u64, + glwe_size, + polynomial_size, + decomp_base_log, + decomp_level_count, + input_lwe_dimension, + grouping_factor, + ciphertext_modulus, + ); + let _next_tfhe_struct: NextLweMultiBitBootstrapKey<_> = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_fourier_lwe_multi_bit_bootstrap_key() { + use crate::core_crypto::entities::lwe_multi_bit_bootstrap_key::{ + FourierLweMultiBitBootstrapKey, LweMultiBitBootstrapKey, + }; + use next_tfhe::core_crypto::entities::lwe_multi_bit_bootstrap_key::FourierLweMultiBitBootstrapKey as NextFourierLweMultiBitBootstrapKey; + + use crate::core_crypto::algorithms::par_convert_standard_lwe_multi_bit_bootstrap_key_to_fourier; + use crate::core_crypto::commons::parameters::*; + + let input_lwe_dimension = LweDimension(100); + let polynomial_size = PolynomialSize(2048); + let glwe_size = GlweSize(2); + let decomp_base_log = DecompositionBaseLog(23); + let decomp_level_count = DecompositionLevelCount(1); + let grouping_factor = LweBskGroupingFactor(2); + let ciphertext_modulus = CiphertextModulus::new_native(); + + let bsk = LweMultiBitBootstrapKey::new( + 0u64, + glwe_size, + polynomial_size, + decomp_base_log, + decomp_level_count, + input_lwe_dimension, + grouping_factor, + ciphertext_modulus, + ); + let mut tfhe_struct = FourierLweMultiBitBootstrapKey::new( + bsk.input_lwe_dimension(), + bsk.glwe_size(), + bsk.polynomial_size(), + bsk.decomposition_base_log(), + bsk.decomposition_level_count(), + bsk.grouping_factor(), + ); + + par_convert_standard_lwe_multi_bit_bootstrap_key_to_fourier(&bsk, &mut tfhe_struct); + + let _next_tfhe_struct: NextFourierLweMultiBitBootstrapKey<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/lwe_packing_keyswitch_key/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/lwe_packing_keyswitch_key/mod.rs new file mode 100644 index 0000000000..312db00632 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/lwe_packing_keyswitch_key/mod.rs @@ -0,0 +1,67 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::core_crypto::entities::lwe_packing_keyswitch_key::LwePackingKeyswitchKey; +use next_tfhe::core_crypto::entities::lwe_packing_keyswitch_key::LwePackingKeyswitchKey as NextLwePackingKeyswitchKey; + +use crate::core_crypto::commons::numeric::UnsignedInteger; +use crate::core_crypto::commons::traits::Container; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; + +impl crate::forward_compatibility::ConvertFrom> + for NextLwePackingKeyswitchKey +where + Scalar: UnsignedInteger + NextUnsignedInteger, + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: LwePackingKeyswitchKey) -> Self { + let decomp_base_log = value.decomposition_base_log(); + let decomp_level_count = value.decomposition_level_count(); + let output_glwe_size = value.output_glwe_size(); + let output_polynomial_size = value.output_polynomial_size(); + let ciphertext_modulus = value.ciphertext_modulus(); + let container = value.into_container(); + + Self::from_container( + container, + decomp_base_log.convert_into(), + decomp_level_count.convert_into(), + output_glwe_size.convert_into(), + output_polynomial_size.convert_into(), + ciphertext_modulus.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_lwe_packing_keyswitch_key() { + use crate::core_crypto::entities::lwe_packing_keyswitch_key::LwePackingKeyswitchKey; + use next_tfhe::core_crypto::entities::lwe_packing_keyswitch_key::LwePackingKeyswitchKey as NextLwePackingKeyswitchKey; + + use crate::core_crypto::commons::parameters::*; + + let decomp_base_log = DecompositionBaseLog(23); + let decomp_level_count = DecompositionLevelCount(1); + let input_key_lwe_dimension = LweDimension(100); + let output_key_glwe_dimension = GlweDimension(2); + let output_key_polynomial_size = PolynomialSize(2048); + let ciphertext_modulus = CiphertextModulus::new_native(); + + let tfhe_struct = LwePackingKeyswitchKey::new( + 0u64, + decomp_base_log, + decomp_level_count, + input_key_lwe_dimension, + output_key_glwe_dimension, + output_key_polynomial_size, + ciphertext_modulus, + ); + let _next_tfhe_struct: NextLwePackingKeyswitchKey<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/lwe_private_functional_packing_keyswitch_key/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/lwe_private_functional_packing_keyswitch_key/mod.rs new file mode 100644 index 0000000000..e7b54d6877 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/lwe_private_functional_packing_keyswitch_key/mod.rs @@ -0,0 +1,69 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::core_crypto::entities::lwe_private_functional_packing_keyswitch_key::LwePrivateFunctionalPackingKeyswitchKey; +use next_tfhe::core_crypto::entities::lwe_private_functional_packing_keyswitch_key::LwePrivateFunctionalPackingKeyswitchKey as NextLwePrivateFunctionalPackingKeyswitchKey; + +use crate::core_crypto::commons::numeric::UnsignedInteger; +use crate::core_crypto::commons::traits::Container; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; + +impl + crate::forward_compatibility::ConvertFrom> + for NextLwePrivateFunctionalPackingKeyswitchKey +where + Scalar: UnsignedInteger + NextUnsignedInteger, + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: LwePrivateFunctionalPackingKeyswitchKey) -> Self { + let decomp_base_log = value.decomposition_base_log(); + let decomp_level_count = value.decomposition_level_count(); + let output_glwe_size = value.output_glwe_size(); + let output_polynomial_size = value.output_polynomial_size(); + let ciphertext_modulus = value.ciphertext_modulus(); + let container = value.into_container(); + + Self::from_container( + container, + decomp_base_log.convert_into(), + decomp_level_count.convert_into(), + output_glwe_size.convert_into(), + output_polynomial_size.convert_into(), + ciphertext_modulus.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_lwe_private_functional_packing_keyswitch_key() { + use crate::core_crypto::entities::lwe_private_functional_packing_keyswitch_key::LwePrivateFunctionalPackingKeyswitchKey; + use next_tfhe::core_crypto::entities::lwe_private_functional_packing_keyswitch_key::LwePrivateFunctionalPackingKeyswitchKey as NextLwePrivateFunctionalPackingKeyswitchKey; + + use crate::core_crypto::commons::parameters::*; + + let decomp_base_log = DecompositionBaseLog(23); + let decomp_level_count = DecompositionLevelCount(1); + let input_key_lwe_dimension = LweDimension(100); + let output_key_glwe_size = GlweSize(2); + let output_key_polynomial_size = PolynomialSize(2048); + let ciphertext_modulus = CiphertextModulus::new_native(); + + let tfhe_struct = LwePrivateFunctionalPackingKeyswitchKey::new( + 0u64, + decomp_base_log, + decomp_level_count, + input_key_lwe_dimension, + output_key_glwe_size, + output_key_polynomial_size, + ciphertext_modulus, + ); + let _next_tfhe_struct: NextLwePrivateFunctionalPackingKeyswitchKey<_> = + tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/lwe_private_functional_packing_keyswitch_key_list/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/lwe_private_functional_packing_keyswitch_key_list/mod.rs new file mode 100644 index 0000000000..ac25c3c3c6 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/lwe_private_functional_packing_keyswitch_key_list/mod.rs @@ -0,0 +1,65 @@ +use crate::core_crypto::entities::lwe_private_functional_packing_keyswitch_key_list::LwePrivateFunctionalPackingKeyswitchKeyList; +use crate::forward_compatibility::ConvertInto; +use next_tfhe::core_crypto::entities::lwe_private_functional_packing_keyswitch_key_list::LwePrivateFunctionalPackingKeyswitchKeyList as NextLwePrivateFunctionalPackingKeyswitchKeyList; + +use crate::core_crypto::commons::numeric::UnsignedInteger; +use crate::core_crypto::commons::traits::Container; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; + +impl + crate::forward_compatibility::ConvertFrom> + for NextLwePrivateFunctionalPackingKeyswitchKeyList +where + Scalar: UnsignedInteger + NextUnsignedInteger, + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: LwePrivateFunctionalPackingKeyswitchKeyList) -> Self { + let decomp_base_log = value.decomposition_base_log(); + let decomp_level_count = value.decomposition_level_count(); + let input_lwe_size = value.input_lwe_size(); + let output_glwe_size = value.output_glwe_size(); + let output_polynomial_size = value.output_polynomial_size(); + let ciphertext_modulus = value.ciphertext_modulus(); + let container = value.into_container(); + + Self::from_container( + container, + decomp_base_log.convert_into(), + decomp_level_count.convert_into(), + input_lwe_size.convert_into(), + output_glwe_size.convert_into(), + output_polynomial_size.convert_into(), + ciphertext_modulus.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_lwe_private_functional_packing_keyswitch_key_list() { + use crate::core_crypto::entities::lwe_private_functional_packing_keyswitch_key_list::LwePrivateFunctionalPackingKeyswitchKeyList; + use next_tfhe::core_crypto::entities::lwe_private_functional_packing_keyswitch_key_list::LwePrivateFunctionalPackingKeyswitchKeyList as NextLwePrivateFunctionalPackingKeyswitchKeyList; + + use crate::core_crypto::commons::parameters::*; + + let tfhe_struct = LwePrivateFunctionalPackingKeyswitchKeyList::new( + 0u64, + DecompositionBaseLog(12), + DecompositionLevelCount(2), + LweDimension(100), + GlweSize(2), + PolynomialSize(1024), + FunctionalPackingKeyswitchKeyCount(2), + CiphertextModulus::new_native(), + ); + + let _next_tfhe_struct: NextLwePrivateFunctionalPackingKeyswitchKeyList<_> = + tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/lwe_public_key/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/lwe_public_key/mod.rs new file mode 100644 index 0000000000..e61ce3657f --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/lwe_public_key/mod.rs @@ -0,0 +1,50 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::core_crypto::entities::lwe_public_key::LwePublicKey; +use next_tfhe::core_crypto::entities::lwe_public_key::LwePublicKey as NextLwePublicKey; + +use crate::core_crypto::commons::numeric::UnsignedInteger; +use crate::core_crypto::commons::traits::Container; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; + +impl crate::forward_compatibility::ConvertFrom> for NextLwePublicKey +where + Scalar: UnsignedInteger + NextUnsignedInteger, + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: LwePublicKey) -> Self { + let lwe_size = value.lwe_size(); + let ciphertext_modulus = value.ciphertext_modulus(); + let container = value.into_container(); + + Self::from_container( + container, + lwe_size.convert_into(), + ciphertext_modulus.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_lwe_public_key() { + use crate::core_crypto::entities::lwe_public_key::LwePublicKey; + use next_tfhe::core_crypto::entities::lwe_public_key::LwePublicKey as NextLwePublicKey; + + use crate::core_crypto::commons::parameters::*; + + let tfhe_struct = LwePublicKey::new( + 0u64, + LweSize(101), + LwePublicKeyZeroEncryptionCount(10), + CiphertextModulus::new_native(), + ); + let _next_tfhe_struct: NextLwePublicKey<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/lwe_secret_key/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/lwe_secret_key/mod.rs new file mode 100644 index 0000000000..f4a4552c91 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/lwe_secret_key/mod.rs @@ -0,0 +1,38 @@ +use crate::core_crypto::entities::lwe_secret_key::LweSecretKey; + +use next_tfhe::core_crypto::entities::lwe_secret_key::LweSecretKey as NextLweSecretKey; + +use crate::core_crypto::commons::numeric::UnsignedInteger; +use crate::core_crypto::commons::traits::Container; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; + +impl crate::forward_compatibility::ConvertFrom> for NextLweSecretKey +where + Scalar: UnsignedInteger + NextUnsignedInteger, + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: LweSecretKey) -> Self { + let container = value.into_container(); + + Self::from_container(container) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_lwe_secret_key() { + use crate::core_crypto::entities::lwe_secret_key::LweSecretKey; + use next_tfhe::core_crypto::entities::lwe_secret_key::LweSecretKey as NextLweSecretKey; + + use crate::core_crypto::commons::parameters::*; + + let tfhe_struct = LweSecretKey::new_empty_key(0u64, LweDimension(100)); + let _next_tfhe_struct: NextLweSecretKey<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/mod.rs new file mode 100644 index 0000000000..905c4be7e6 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/mod.rs @@ -0,0 +1,33 @@ +pub mod cleartext; +pub mod ggsw_ciphertext; +pub mod ggsw_ciphertext_list; +pub mod glwe_ciphertext; +pub mod glwe_ciphertext_list; +pub mod glwe_secret_key; +pub mod lwe_bootstrap_key; +pub mod lwe_ciphertext; +pub mod lwe_ciphertext_list; +pub mod lwe_compact_public_key; +pub mod lwe_keyswitch_key; +pub mod lwe_multi_bit_bootstrap_key; +pub mod lwe_packing_keyswitch_key; +pub mod lwe_private_functional_packing_keyswitch_key; +pub mod lwe_private_functional_packing_keyswitch_key_list; +pub mod lwe_public_key; +pub mod lwe_secret_key; +pub mod plaintext; +pub mod plaintext_list; +pub mod polynomial; +pub mod polynomial_list; +pub mod seeded_ggsw_ciphertext; +pub mod seeded_ggsw_ciphertext_list; +pub mod seeded_glwe_ciphertext; +pub mod seeded_glwe_ciphertext_list; +pub mod seeded_lwe_bootstrap_key; +pub mod seeded_lwe_ciphertext; +pub mod seeded_lwe_ciphertext_list; +pub mod seeded_lwe_compact_public_key; +pub mod seeded_lwe_keyswitch_key; +pub mod seeded_lwe_multi_bit_bootstrap_key; +pub mod seeded_lwe_packing_keyswitch_key; +pub mod seeded_lwe_public_key; diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/plaintext/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/plaintext/mod.rs new file mode 100644 index 0000000000..c08fef135d --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/plaintext/mod.rs @@ -0,0 +1,31 @@ +use crate::core_crypto::entities::plaintext::Plaintext; +use next_tfhe::core_crypto::entities::plaintext::Plaintext as NextPlaintext; + +use crate::core_crypto::commons::numeric::Numeric; + +use next_tfhe::core_crypto::commons::numeric::Numeric as NextNumeric; + +impl crate::forward_compatibility::ConvertFrom> for NextPlaintext +where + Scalar: Numeric + NextNumeric, +{ + #[inline] + fn convert_from(value: Plaintext) -> Self { + let Plaintext(field_0) = value; + Self(field_0) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_plaintext() { + use crate::core_crypto::entities::plaintext::Plaintext; + use next_tfhe::core_crypto::entities::plaintext::Plaintext as NextPlaintext; + + let tfhe_struct = Plaintext(42u64); + let _next_tfhe_struct: NextPlaintext<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/plaintext_list/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/plaintext_list/mod.rs new file mode 100644 index 0000000000..009fb2651c --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/plaintext_list/mod.rs @@ -0,0 +1,35 @@ +use crate::core_crypto::entities::plaintext_list::PlaintextList; +use next_tfhe::core_crypto::entities::plaintext_list::PlaintextList as NextPlaintextList; + +use crate::core_crypto::commons::numeric::UnsignedInteger; +use crate::core_crypto::commons::traits::Container; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; + +impl crate::forward_compatibility::ConvertFrom> for NextPlaintextList +where + Scalar: UnsignedInteger + NextUnsignedInteger, + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: PlaintextList) -> Self { + Self::from_container(value.into_container()) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_plaintext_list() { + use crate::core_crypto::entities::plaintext_list::PlaintextList; + use next_tfhe::core_crypto::entities::plaintext_list::PlaintextList as NextPlaintextList; + + use crate::core_crypto::commons::parameters::*; + + let tfhe_struct = PlaintextList::new(0u64, PlaintextCount(10)); + let _next_tfhe_struct: NextPlaintextList<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/polynomial/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/polynomial/mod.rs new file mode 100644 index 0000000000..6d80b89ab7 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/polynomial/mod.rs @@ -0,0 +1,36 @@ +use crate::core_crypto::entities::polynomial::Polynomial; + +use next_tfhe::core_crypto::entities::polynomial::Polynomial as NextPolynomial; + +use crate::core_crypto::commons::numeric::UnsignedInteger; +use crate::core_crypto::commons::traits::Container; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; + +impl crate::forward_compatibility::ConvertFrom> for NextPolynomial +where + Scalar: UnsignedInteger + NextUnsignedInteger, + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: Polynomial) -> Self { + Self::from_container(value.into_container()) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_polynomial() { + use crate::core_crypto::entities::polynomial::Polynomial; + use next_tfhe::core_crypto::entities::polynomial::Polynomial as NextPolynomial; + + use crate::core_crypto::commons::parameters::*; + + let tfhe_struct = Polynomial::new(0u64, PolynomialSize(2048)); + let _next_tfhe_struct: NextPolynomial<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/polynomial_list/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/polynomial_list/mod.rs new file mode 100644 index 0000000000..63d5255095 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/polynomial_list/mod.rs @@ -0,0 +1,40 @@ +use crate::core_crypto::entities::polynomial_list::PolynomialList; +use crate::forward_compatibility::ConvertInto; +use next_tfhe::core_crypto::entities::polynomial_list::PolynomialList as NextPolynomialList; + +use crate::core_crypto::commons::numeric::UnsignedInteger; +use crate::core_crypto::commons::traits::Container; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; + +impl crate::forward_compatibility::ConvertFrom> + for NextPolynomialList +where + Scalar: UnsignedInteger + NextUnsignedInteger, + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: PolynomialList) -> Self { + let polynomial_size = value.polynomial_size(); + let container = value.into_container(); + + Self::from_container(container, polynomial_size.convert_into()) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_polynomial_list() { + use crate::core_crypto::entities::polynomial_list::PolynomialList; + use next_tfhe::core_crypto::entities::polynomial_list::PolynomialList as NextPolynomialList; + + use crate::core_crypto::commons::parameters::*; + + let tfhe_struct = PolynomialList::new(0u64, PolynomialSize(2048), PolynomialCount(10)); + let _next_tfhe_struct: NextPolynomialList<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/seeded_ggsw_ciphertext/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/seeded_ggsw_ciphertext/mod.rs new file mode 100644 index 0000000000..626621dc60 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/seeded_ggsw_ciphertext/mod.rs @@ -0,0 +1,68 @@ +use crate::core_crypto::entities::seeded_ggsw_ciphertext::SeededGgswCiphertext; +use crate::forward_compatibility::ConvertInto; +use next_tfhe::core_crypto::entities::seeded_ggsw_ciphertext::SeededGgswCiphertext as NextSeededGgswCiphertext; + +use crate::core_crypto::commons::numeric::UnsignedInteger; +use crate::core_crypto::commons::traits::Container; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; + +impl crate::forward_compatibility::ConvertFrom> + for NextSeededGgswCiphertext +where + Scalar: UnsignedInteger + NextUnsignedInteger, + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: SeededGgswCiphertext) -> Self { + let glwe_size = value.glwe_size(); + let polynomial_size = value.polynomial_size(); + let decomp_base_log = value.decomposition_base_log(); + let compression_seed = value.compression_seed(); + let ciphertext_modulus = value.ciphertext_modulus(); + let container = value.into_container(); + + Self::from_container( + container, + glwe_size.convert_into(), + polynomial_size.convert_into(), + decomp_base_log.convert_into(), + compression_seed.convert_into(), + ciphertext_modulus.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_seeded_ggsw_ciphertext() { + use crate::core_crypto::entities::seeded_ggsw_ciphertext::SeededGgswCiphertext; + use next_tfhe::core_crypto::entities::seeded_ggsw_ciphertext::SeededGgswCiphertext as NextSeededGgswCiphertext; + + use crate::core_crypto::commons::math::random::Seed; + use crate::core_crypto::commons::parameters::*; + + let glwe_size = GlweSize(2); + let polynomial_size = PolynomialSize(2048); + let decomp_base_log = DecompositionBaseLog(23); + let decomp_level_count = DecompositionLevelCount(1); + let compression_seed = Seed(42).into(); + let ciphertext_modulus = CiphertextModulus::new_native(); + + let tfhe_struct = SeededGgswCiphertext::new( + 0u64, + glwe_size, + polynomial_size, + decomp_base_log, + decomp_level_count, + compression_seed, + ciphertext_modulus, + ); + + let _next_tfhe_struct: NextSeededGgswCiphertext<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/seeded_ggsw_ciphertext_list/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/seeded_ggsw_ciphertext_list/mod.rs new file mode 100644 index 0000000000..b7c8e85541 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/seeded_ggsw_ciphertext_list/mod.rs @@ -0,0 +1,72 @@ +use crate::core_crypto::entities::seeded_ggsw_ciphertext_list::SeededGgswCiphertextList; +use crate::forward_compatibility::ConvertInto; +use next_tfhe::core_crypto::entities::seeded_ggsw_ciphertext_list::SeededGgswCiphertextList as NextSeededGgswCiphertextList; + +use crate::core_crypto::commons::numeric::UnsignedInteger; +use crate::core_crypto::commons::traits::Container; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; + +impl crate::forward_compatibility::ConvertFrom> + for NextSeededGgswCiphertextList +where + Scalar: UnsignedInteger + NextUnsignedInteger, + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: SeededGgswCiphertextList) -> Self { + let glwe_size = value.glwe_size(); + let polynomial_size = value.polynomial_size(); + let decomp_base_log = value.decomposition_base_log(); + let decomp_level_count = value.decomposition_level_count(); + let compression_seed = value.compression_seed(); + let ciphertext_modulus = value.ciphertext_modulus(); + let container = value.into_container(); + + Self::from_container( + container, + glwe_size.convert_into(), + polynomial_size.convert_into(), + decomp_base_log.convert_into(), + decomp_level_count.convert_into(), + compression_seed.convert_into(), + ciphertext_modulus.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_seeded_ggsw_ciphertext_list() { + use crate::core_crypto::entities::seeded_ggsw_ciphertext_list::SeededGgswCiphertextList; + use next_tfhe::core_crypto::entities::seeded_ggsw_ciphertext_list::SeededGgswCiphertextList as NextSeededGgswCiphertextList; + + use crate::core_crypto::commons::math::random::Seed; + use crate::core_crypto::commons::parameters::*; + + let glwe_size = GlweSize(2); + let polynomial_size = PolynomialSize(2048); + let decomp_base_log = DecompositionBaseLog(23); + let decomp_level_count = DecompositionLevelCount(1); + let ciphertext_count = GgswCiphertextCount(10); + let compression_seed = Seed(42).into(); + let ciphertext_modulus = CiphertextModulus::new_native(); + + let tfhe_struct = SeededGgswCiphertextList::new( + 0u64, + glwe_size, + polynomial_size, + decomp_base_log, + decomp_level_count, + ciphertext_count, + compression_seed, + ciphertext_modulus, + ); + + let _next_tfhe_struct: NextSeededGgswCiphertextList<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/seeded_glwe_ciphertext/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/seeded_glwe_ciphertext/mod.rs new file mode 100644 index 0000000000..6289fefa62 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/seeded_glwe_ciphertext/mod.rs @@ -0,0 +1,60 @@ +use crate::core_crypto::entities::seeded_glwe_ciphertext::SeededGlweCiphertext; +use crate::forward_compatibility::ConvertInto; +use next_tfhe::core_crypto::entities::seeded_glwe_ciphertext::SeededGlweCiphertext as NextSeededGlweCiphertext; + +use crate::core_crypto::commons::numeric::UnsignedInteger; +use crate::core_crypto::commons::traits::Container; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; + +impl crate::forward_compatibility::ConvertFrom> + for NextSeededGlweCiphertext +where + Scalar: UnsignedInteger + NextUnsignedInteger, + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: SeededGlweCiphertext) -> Self { + let glwe_size = value.glwe_size(); + let compression_seed = value.compression_seed(); + let ciphertext_modulus = value.ciphertext_modulus(); + let container = value.into_container(); + + Self::from_container( + container, + glwe_size.convert_into(), + compression_seed.convert_into(), + ciphertext_modulus.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_seeded_glwe_ciphertext() { + use crate::core_crypto::entities::seeded_glwe_ciphertext::SeededGlweCiphertext; + use next_tfhe::core_crypto::entities::seeded_glwe_ciphertext::SeededGlweCiphertext as NextSeededGlweCiphertext; + + use crate::core_crypto::commons::math::random::Seed; + use crate::core_crypto::commons::parameters::*; + + let glwe_size = GlweSize(2); + let polynomial_size = PolynomialSize(2048); + let compression_seed = Seed(42).into(); + let ciphertext_modulus = CiphertextModulus::new_native(); + + let tfhe_struct = SeededGlweCiphertext::new( + 0u64, + glwe_size, + polynomial_size, + compression_seed, + ciphertext_modulus, + ); + + let _next_tfhe_struct: NextSeededGlweCiphertext<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/seeded_glwe_ciphertext_list/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/seeded_glwe_ciphertext_list/mod.rs new file mode 100644 index 0000000000..4ac8f91dee --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/seeded_glwe_ciphertext_list/mod.rs @@ -0,0 +1,63 @@ +use crate::core_crypto::entities::seeded_glwe_ciphertext_list::SeededGlweCiphertextList; +use crate::forward_compatibility::ConvertInto; +use next_tfhe::core_crypto::entities::seeded_glwe_ciphertext_list::SeededGlweCiphertextList as NextSeededGlweCiphertextList; + +use crate::core_crypto::commons::numeric::UnsignedInteger; +use crate::core_crypto::commons::traits::Container; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; + +impl crate::forward_compatibility::ConvertFrom> + for NextSeededGlweCiphertextList +where + Scalar: UnsignedInteger + NextUnsignedInteger, + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: SeededGlweCiphertextList) -> Self { + let glwe_size = value.glwe_size(); + let polynomial_size = value.polynomial_size(); + let compression_seed = value.compression_seed(); + let ciphertext_modulus = value.ciphertext_modulus(); + let container = value.into_container(); + + Self::from_container( + container, + glwe_size.convert_into(), + polynomial_size.convert_into(), + compression_seed.convert_into(), + ciphertext_modulus.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + #[test] + fn test_conversion_seeded_glwe_ciphertext_list() { + use crate::core_crypto::entities::seeded_glwe_ciphertext_list::SeededGlweCiphertextList; + use next_tfhe::core_crypto::entities::seeded_glwe_ciphertext_list::SeededGlweCiphertextList as NextSeededGlweCiphertextList; + + use crate::core_crypto::commons::math::random::Seed; + use crate::core_crypto::commons::parameters::*; + + let glwe_size = GlweSize(2); + let polynomial_size = PolynomialSize(2048); + let ciphertext_count = GlweCiphertextCount(10); + let compression_seed = Seed(42).into(); + let ciphertext_modulus = CiphertextModulus::new_native(); + + let tfhe_struct = SeededGlweCiphertextList::new( + 0u64, + glwe_size, + polynomial_size, + ciphertext_count, + compression_seed, + ciphertext_modulus, + ); + + let _next_tfhe_struct: NextSeededGlweCiphertextList<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_bootstrap_key/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_bootstrap_key/mod.rs new file mode 100644 index 0000000000..a7607e80c6 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_bootstrap_key/mod.rs @@ -0,0 +1,64 @@ +use crate::core_crypto::entities::seeded_lwe_bootstrap_key::SeededLweBootstrapKey; +use crate::forward_compatibility::ConvertInto; +use next_tfhe::core_crypto::entities::seeded_lwe_bootstrap_key::SeededLweBootstrapKey as NextSeededLweBootstrapKey; + +use crate::core_crypto::commons::numeric::UnsignedInteger; +use crate::core_crypto::commons::traits::Container; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; + +impl crate::forward_compatibility::ConvertFrom> + for NextSeededLweBootstrapKey +where + Scalar: UnsignedInteger + NextUnsignedInteger, + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: SeededLweBootstrapKey) -> Self { + let glwe_size = value.glwe_size(); + let polynomial_size = value.polynomial_size(); + let decomp_base_log = value.decomposition_base_log(); + let decomp_level_count = value.decomposition_level_count(); + let compression_seed = value.compression_seed(); + let ciphertext_modulus = value.ciphertext_modulus(); + let container = value.into_container(); + + Self::from_container( + container, + glwe_size.convert_into(), + polynomial_size.convert_into(), + decomp_base_log.convert_into(), + decomp_level_count.convert_into(), + compression_seed.convert_into(), + ciphertext_modulus.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_seeded_lwe_bootstrap_key() { + use crate::core_crypto::entities::seeded_lwe_bootstrap_key::SeededLweBootstrapKey; + use next_tfhe::core_crypto::entities::seeded_lwe_bootstrap_key::SeededLweBootstrapKey as NextSeededLweBootstrapKey; + + use crate::core_crypto::commons::math::random::Seed; + use crate::core_crypto::commons::parameters::*; + + let tfhe_struct = SeededLweBootstrapKey::new( + 0u64, + GlweSize(2), + PolynomialSize(2048), + DecompositionBaseLog(23), + DecompositionLevelCount(1), + LweDimension(100), + Seed(42).into(), + CiphertextModulus::new_native(), + ); + + let _next_tfhe_struct: NextSeededLweBootstrapKey<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_ciphertext/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_ciphertext/mod.rs new file mode 100644 index 0000000000..e7fb974116 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_ciphertext/mod.rs @@ -0,0 +1,55 @@ +use crate::core_crypto::entities::seeded_lwe_ciphertext::SeededLweCiphertext; +use crate::forward_compatibility::ConvertInto; +use next_tfhe::core_crypto::entities::seeded_lwe_ciphertext::SeededLweCiphertext as NextSeededLweCiphertext; + +use crate::core_crypto::commons::numeric::UnsignedInteger; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; + +impl crate::forward_compatibility::ConvertFrom> + for NextSeededLweCiphertext +where + Scalar: UnsignedInteger + NextUnsignedInteger, +{ + #[inline] + fn convert_from(value: SeededLweCiphertext) -> Self { + let lwe_size = value.lwe_size(); + let compression_seed = value.compression_seed(); + let ciphertext_modulus = value.ciphertext_modulus(); + let scalar = value.into_scalar(); + + Self::from_scalar( + scalar, + lwe_size.convert_into(), + compression_seed.convert_into(), + ciphertext_modulus.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_seeded_lwe_ciphertext() { + use crate::core_crypto::commons::math::random::CompressionSeed; + use crate::core_crypto::commons::parameters::*; + use crate::core_crypto::entities::seeded_lwe_ciphertext::SeededLweCiphertext; + use crate::core_crypto::seeders::new_seeder; + use next_tfhe::core_crypto::entities::seeded_lwe_ciphertext::SeededLweCiphertext as NextSeededLweCiphertext; + + let mut seeder = new_seeder(); + + let tfhe_struct = SeededLweCiphertext::new( + 0u64, + LweSize(101), + CompressionSeed { + seed: seeder.seed(), + }, + CiphertextModulus::new_native(), + ); + + let _next_tfhe_struct: NextSeededLweCiphertext<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_ciphertext_list/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_ciphertext_list/mod.rs new file mode 100644 index 0000000000..571eeee9f1 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_ciphertext_list/mod.rs @@ -0,0 +1,60 @@ +use crate::core_crypto::entities::seeded_lwe_ciphertext_list::SeededLweCiphertextList; +use crate::forward_compatibility::ConvertInto; +use next_tfhe::core_crypto::entities::seeded_lwe_ciphertext_list::SeededLweCiphertextList as NextSeededLweCiphertextList; + +use crate::core_crypto::commons::numeric::UnsignedInteger; +use crate::core_crypto::commons::traits::Container; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; + +impl crate::forward_compatibility::ConvertFrom> + for NextSeededLweCiphertextList +where + Scalar: UnsignedInteger + NextUnsignedInteger, + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: SeededLweCiphertextList) -> Self { + let lwe_size = value.lwe_size(); + let compression_seed = value.compression_seed(); + let ciphertext_modulus = value.ciphertext_modulus(); + let container = value.into_container(); + + Self::from_container( + container, + lwe_size.convert_into(), + compression_seed.convert_into(), + ciphertext_modulus.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_seeded_lwe_ciphertext_list() { + use crate::core_crypto::entities::seeded_lwe_ciphertext_list::SeededLweCiphertextList; + use next_tfhe::core_crypto::entities::seeded_lwe_ciphertext_list::SeededLweCiphertextList as NextSeededLweCiphertextList; + + use crate::core_crypto::commons::math::random::Seed; + use crate::core_crypto::commons::parameters::*; + + let lwe_size = LweSize(101); + let ciphertext_count = LweCiphertextCount(10); + let compression_seed = Seed(42).into(); + let ciphertext_modulus = CiphertextModulus::new_native(); + + let tfhe_struct = SeededLweCiphertextList::new( + 0u64, + lwe_size, + ciphertext_count, + compression_seed, + ciphertext_modulus, + ); + + let _next_tfhe_struct: NextSeededLweCiphertextList<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_compact_public_key/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_compact_public_key/mod.rs new file mode 100644 index 0000000000..ecfe3a3d0e --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_compact_public_key/mod.rs @@ -0,0 +1,56 @@ +use crate::core_crypto::entities::seeded_lwe_compact_public_key::SeededLweCompactPublicKey; +use crate::forward_compatibility::ConvertInto; +use next_tfhe::core_crypto::entities::seeded_lwe_compact_public_key::SeededLweCompactPublicKey as NextSeededLweCompactPublicKey; + +use crate::core_crypto::commons::numeric::UnsignedInteger; +use crate::core_crypto::commons::traits::Container; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; + +impl crate::forward_compatibility::ConvertFrom> + for NextSeededLweCompactPublicKey +where + Scalar: UnsignedInteger + NextUnsignedInteger, + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: SeededLweCompactPublicKey) -> Self { + let compression_seed = value.compression_seed(); + let ciphertext_modulus = value.ciphertext_modulus(); + let container = value.into_container(); + + Self::from_container( + container, + compression_seed.convert_into(), + ciphertext_modulus.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_seeded_lwe_compact_public_key() { + use crate::core_crypto::entities::seeded_lwe_compact_public_key::SeededLweCompactPublicKey; + use next_tfhe::core_crypto::entities::seeded_lwe_compact_public_key::SeededLweCompactPublicKey as NextSeededLweCompactPublicKey; + + use crate::core_crypto::commons::math::random::Seed; + use crate::core_crypto::commons::parameters::*; + + let lwe_dimension = LweDimension(1024); + let compression_seed = Seed(42).into(); + let ciphertext_modulus = CiphertextModulus::new_native(); + + let tfhe_struct = SeededLweCompactPublicKey::new( + 0u64, + lwe_dimension, + compression_seed, + ciphertext_modulus, + ); + + let _next_tfhe_struct: NextSeededLweCompactPublicKey<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_keyswitch_key/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_keyswitch_key/mod.rs new file mode 100644 index 0000000000..ba10138c9f --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_keyswitch_key/mod.rs @@ -0,0 +1,62 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::core_crypto::entities::seeded_lwe_keyswitch_key::SeededLweKeyswitchKey; +use next_tfhe::core_crypto::entities::seeded_lwe_keyswitch_key::SeededLweKeyswitchKey as NextSeededLweKeyswitchKey; + +use crate::core_crypto::commons::numeric::UnsignedInteger; +use crate::core_crypto::commons::traits::Container; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; + +impl crate::forward_compatibility::ConvertFrom> + for NextSeededLweKeyswitchKey +where + Scalar: UnsignedInteger + NextUnsignedInteger, + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: SeededLweKeyswitchKey) -> Self { + let decomp_base_log = value.decomposition_base_log(); + let decomp_level_count = value.decomposition_level_count(); + let output_lwe_size = value.output_lwe_size(); + let compression_seed = value.compression_seed(); + let ciphertext_modulus = value.ciphertext_modulus(); + let container = value.into_container(); + + Self::from_container( + container, + decomp_base_log.convert_into(), + decomp_level_count.convert_into(), + output_lwe_size.convert_into(), + compression_seed.convert_into(), + ciphertext_modulus.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_seeded_lwe_keyswitch_key() { + use crate::core_crypto::entities::seeded_lwe_keyswitch_key::SeededLweKeyswitchKey; + use next_tfhe::core_crypto::entities::seeded_lwe_keyswitch_key::SeededLweKeyswitchKey as NextSeededLweKeyswitchKey; + + use crate::core_crypto::commons::math::random::Seed; + use crate::core_crypto::commons::parameters::*; + + let tfhe_struct = SeededLweKeyswitchKey::new( + 0u64, + DecompositionBaseLog(5), + DecompositionLevelCount(3), + LweDimension(200), + LweDimension(100), + Seed(42).into(), + CiphertextModulus::new_native(), + ); + + let _next_tfhe_struct: NextSeededLweKeyswitchKey<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_multi_bit_bootstrap_key/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_multi_bit_bootstrap_key/mod.rs new file mode 100644 index 0000000000..dfa9f01b48 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_multi_bit_bootstrap_key/mod.rs @@ -0,0 +1,67 @@ +use crate::core_crypto::entities::seeded_lwe_multi_bit_bootstrap_key::SeededLweMultiBitBootstrapKey; +use crate::forward_compatibility::ConvertInto; +use next_tfhe::core_crypto::entities::seeded_lwe_multi_bit_bootstrap_key::SeededLweMultiBitBootstrapKey as NextSeededLweMultiBitBootstrapKey; + +use crate::core_crypto::commons::numeric::UnsignedInteger; +use crate::core_crypto::commons::traits::Container; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; + +impl crate::forward_compatibility::ConvertFrom> + for NextSeededLweMultiBitBootstrapKey +where + Scalar: UnsignedInteger + NextUnsignedInteger, + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: SeededLweMultiBitBootstrapKey) -> Self { + let glwe_size = value.glwe_size(); + let polynomial_size = value.polynomial_size(); + let decomp_base_log = value.decomposition_base_log(); + let decomp_level_count = value.decomposition_level_count(); + let compression_seed = value.compression_seed(); + let grouping_factor = value.grouping_factor(); + let ciphertext_modulus = value.ciphertext_modulus(); + let container = value.into_container(); + + Self::from_container( + container, + glwe_size.convert_into(), + polynomial_size.convert_into(), + decomp_base_log.convert_into(), + decomp_level_count.convert_into(), + compression_seed.convert_into(), + grouping_factor.convert_into(), + ciphertext_modulus.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_seeded_lwe_multi_bit_bootstrap_key() { + use crate::core_crypto::entities::seeded_lwe_multi_bit_bootstrap_key::SeededLweMultiBitBootstrapKey; + use next_tfhe::core_crypto::entities::seeded_lwe_multi_bit_bootstrap_key::SeededLweMultiBitBootstrapKey as NextSeededLweMultiBitBootstrapKey; + + use crate::core_crypto::commons::math::random::Seed; + use crate::core_crypto::commons::parameters::*; + + let tfhe_struct = SeededLweMultiBitBootstrapKey::new( + 0u64, + GlweSize(2), + PolynomialSize(2048), + DecompositionBaseLog(23), + DecompositionLevelCount(1), + LweDimension(100), + LweBskGroupingFactor(2), + Seed(42).into(), + CiphertextModulus::new_native(), + ); + + let _next_tfhe_struct: NextSeededLweMultiBitBootstrapKey<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_packing_keyswitch_key/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_packing_keyswitch_key/mod.rs new file mode 100644 index 0000000000..c3f8145581 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_packing_keyswitch_key/mod.rs @@ -0,0 +1,71 @@ +use crate::core_crypto::entities::seeded_lwe_packing_keyswitch_key::SeededLwePackingKeyswitchKey; +use crate::forward_compatibility::ConvertInto; +use next_tfhe::core_crypto::entities::seeded_lwe_packing_keyswitch_key::SeededLwePackingKeyswitchKey as NextSeededLwePackingKeyswitchKey; + +use crate::core_crypto::commons::numeric::UnsignedInteger; +use crate::core_crypto::commons::traits::Container; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; + +impl crate::forward_compatibility::ConvertFrom> + for NextSeededLwePackingKeyswitchKey +where + Scalar: UnsignedInteger + NextUnsignedInteger, + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: SeededLwePackingKeyswitchKey) -> Self { + let decomp_base_log = value.decomposition_base_log(); + let decomp_level_count = value.decomposition_level_count(); + let output_glwe_size = value.output_glwe_size(); + let output_polynomial_size = value.output_polynomial_size(); + let compression_seed = value.compression_seed(); + let ciphertext_modulus = value.ciphertext_modulus(); + let container = value.into_container(); + + Self::from_container( + container, + decomp_base_log.convert_into(), + decomp_level_count.convert_into(), + output_glwe_size.convert_into(), + output_polynomial_size.convert_into(), + compression_seed.convert_into(), + ciphertext_modulus.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_seeded_lwe_packing_keyswitch_key() { + use crate::core_crypto::entities::seeded_lwe_packing_keyswitch_key::SeededLwePackingKeyswitchKey; + use next_tfhe::core_crypto::entities::seeded_lwe_packing_keyswitch_key::SeededLwePackingKeyswitchKey as NextSeededLwePackingKeyswitchKey; + + use crate::core_crypto::commons::math::random::Seed; + use crate::core_crypto::commons::parameters::*; + + let decomp_base_log = DecompositionBaseLog(23); + let decomp_level_count = DecompositionLevelCount(1); + let input_key_lwe_dimension = LweDimension(100); + let output_key_glwe_dimension = GlweDimension(2); + let output_key_polynomial_size = PolynomialSize(2048); + let compression_seed = Seed(42).into(); + let ciphertext_modulus = CiphertextModulus::new_native(); + + let tfhe_struct = SeededLwePackingKeyswitchKey::new( + 0u64, + decomp_base_log, + decomp_level_count, + input_key_lwe_dimension, + output_key_glwe_dimension, + output_key_polynomial_size, + compression_seed, + ciphertext_modulus, + ); + let _next_tfhe_struct: NextSeededLwePackingKeyswitchKey<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_public_key/mod.rs b/tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_public_key/mod.rs new file mode 100644 index 0000000000..ecb57e4cbd --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/entities/seeded_lwe_public_key/mod.rs @@ -0,0 +1,54 @@ +use crate::core_crypto::entities::seeded_lwe_public_key::SeededLwePublicKey; +use crate::forward_compatibility::ConvertInto; +use next_tfhe::core_crypto::entities::seeded_lwe_public_key::SeededLwePublicKey as NextSeededLwePublicKey; + +use crate::core_crypto::commons::numeric::UnsignedInteger; +use crate::core_crypto::commons::traits::Container; + +use next_tfhe::core_crypto::commons::numeric::UnsignedInteger as NextUnsignedInteger; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; + +impl crate::forward_compatibility::ConvertFrom> + for NextSeededLwePublicKey +where + Scalar: UnsignedInteger + NextUnsignedInteger, + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: SeededLwePublicKey) -> Self { + let lwe_size = value.lwe_size(); + let compression_seed = value.compression_seed(); + let ciphertext_modulus = value.ciphertext_modulus(); + let container = value.into_container(); + + Self::from_container( + container, + lwe_size.convert_into(), + compression_seed.convert_into(), + ciphertext_modulus.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_seeded_lwe_public_key() { + use crate::core_crypto::entities::seeded_lwe_public_key::SeededLwePublicKey; + use next_tfhe::core_crypto::entities::seeded_lwe_public_key::SeededLwePublicKey as NextSeededLwePublicKey; + + use crate::core_crypto::commons::math::random::Seed; + use crate::core_crypto::commons::parameters::*; + + let tfhe_struct = SeededLwePublicKey::new( + 0u64, + LweSize(101), + LwePublicKeyZeroEncryptionCount(10), + Seed(42).into(), + CiphertextModulus::new_native(), + ); + let _next_tfhe_struct: NextSeededLwePublicKey<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/fft_impl/fft128/crypto/bootstrap/mod.rs b/tfhe/src/forward_compatibility/core_crypto/fft_impl/fft128/crypto/bootstrap/mod.rs new file mode 100644 index 0000000000..b9641beb7e --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/fft_impl/fft128/crypto/bootstrap/mod.rs @@ -0,0 +1,78 @@ +use crate::core_crypto::commons::traits::Container; +use crate::core_crypto::fft_impl::fft128::crypto::bootstrap::Fourier128LweBootstrapKey; +use crate::forward_compatibility::ConvertInto; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; +use next_tfhe::core_crypto::fft_impl::fft128::crypto::bootstrap::Fourier128LweBootstrapKey as NextFourier128LweBootstrapKey; + +impl crate::forward_compatibility::ConvertFrom> + for NextFourier128LweBootstrapKey +where + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: Fourier128LweBootstrapKey) -> Self { + let polynomial_size = value.polynomial_size(); + let input_lwe_dimension = value.input_lwe_dimension(); + let glwe_size = value.glwe_size(); + let decomposition_base_log = value.decomposition_base_log(); + let decomposition_level_count = value.decomposition_level_count(); + + let (data_re0, data_re1, data_im0, data_im1) = value.data(); + + Self::from_container( + data_re0, + data_re1, + data_im0, + data_im1, + polynomial_size.convert_into(), + input_lwe_dimension.convert_into(), + glwe_size.convert_into(), + decomposition_base_log.convert_into(), + decomposition_level_count.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_fourier128_lwe_bootstrap_key() { + use crate::core_crypto::fft_impl::fft128::crypto::bootstrap::Fourier128LweBootstrapKey; + use next_tfhe::core_crypto::fft_impl::fft128::crypto::bootstrap::Fourier128LweBootstrapKey as NextFourier128LweBootstrapKey; + + use crate::core_crypto::commons::parameters::*; + + let input_lwe_dimension = LweDimension(100); + let polynomial_size = PolynomialSize(2048); + let glwe_size = GlweSize(2); + let decomposition_level_count = DecompositionLevelCount(1); + let decomposition_base_log = DecompositionBaseLog(23); + + let container_len = input_lwe_dimension.0 + * polynomial_size.to_fourier_polynomial_size().0 + * glwe_size.0 + * glwe_size.0 + * decomposition_level_count.0; + + let data_re0 = vec![0.0f64; container_len]; + let data_re1 = vec![1.0f64; container_len]; + let data_im0 = vec![2.0f64; container_len]; + let data_im1 = vec![3.0f64; container_len]; + + let tfhe_struct = Fourier128LweBootstrapKey::from_container( + data_re0, + data_re1, + data_im0, + data_im1, + polynomial_size, + input_lwe_dimension, + glwe_size, + decomposition_base_log, + decomposition_level_count, + ); + + let _next_tfhe_struct: NextFourier128LweBootstrapKey<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/fft_impl/fft128/crypto/ggsw/mod.rs b/tfhe/src/forward_compatibility/core_crypto/fft_impl/fft128/crypto/ggsw/mod.rs new file mode 100644 index 0000000000..1c3ecd7c9c --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/fft_impl/fft128/crypto/ggsw/mod.rs @@ -0,0 +1,73 @@ +use crate::core_crypto::commons::traits::Container; +use crate::core_crypto::fft_impl::fft128::crypto::ggsw::Fourier128GgswCiphertext; +use crate::forward_compatibility::ConvertInto; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; +use next_tfhe::core_crypto::fft_impl::fft128::crypto::ggsw::Fourier128GgswCiphertext as NextFourier128GgswCiphertext; + +impl crate::forward_compatibility::ConvertFrom> + for NextFourier128GgswCiphertext +where + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: Fourier128GgswCiphertext) -> Self { + let polynomial_size = value.polynomial_size(); + let glwe_size = value.glwe_size(); + let decomposition_base_log = value.decomposition_base_log(); + let decomposition_level_count = value.decomposition_level_count(); + + let (data_re0, data_re1, data_im0, data_im1) = value.data(); + + Self::from_container( + data_re0, + data_re1, + data_im0, + data_im1, + polynomial_size.convert_into(), + glwe_size.convert_into(), + decomposition_base_log.convert_into(), + decomposition_level_count.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_fourier128_ggsw_ciphertext() { + use crate::core_crypto::fft_impl::fft128::crypto::ggsw::Fourier128GgswCiphertext; + use next_tfhe::core_crypto::fft_impl::fft128::crypto::ggsw::Fourier128GgswCiphertext as NextFourier128GgswCiphertext; + + use crate::core_crypto::commons::parameters::*; + + let polynomial_size = PolynomialSize(2048); + let glwe_size = GlweSize(2); + let decomposition_level_count = DecompositionLevelCount(1); + let decomposition_base_log = DecompositionBaseLog(23); + + let container_len = polynomial_size.to_fourier_polynomial_size().0 + * glwe_size.0 + * glwe_size.0 + * decomposition_level_count.0; + + let data_re0 = vec![0.0f64; container_len]; + let data_re1 = vec![1.0f64; container_len]; + let data_im0 = vec![2.0f64; container_len]; + let data_im1 = vec![3.0f64; container_len]; + + let tfhe_struct = Fourier128GgswCiphertext::from_container( + data_re0, + data_re1, + data_im0, + data_im1, + polynomial_size, + glwe_size, + decomposition_base_log, + decomposition_level_count, + ); + + let _next_tfhe_struct: NextFourier128GgswCiphertext<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/fft_impl/fft128/crypto/mod.rs b/tfhe/src/forward_compatibility/core_crypto/fft_impl/fft128/crypto/mod.rs new file mode 100644 index 0000000000..1a9cfdb294 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/fft_impl/fft128/crypto/mod.rs @@ -0,0 +1,2 @@ +pub mod bootstrap; +pub mod ggsw; diff --git a/tfhe/src/forward_compatibility/core_crypto/fft_impl/fft128/mod.rs b/tfhe/src/forward_compatibility/core_crypto/fft_impl/fft128/mod.rs new file mode 100644 index 0000000000..274f0edcd3 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/fft_impl/fft128/mod.rs @@ -0,0 +1 @@ +pub mod crypto; diff --git a/tfhe/src/forward_compatibility/core_crypto/fft_impl/fft64/crypto/bootstrap/mod.rs b/tfhe/src/forward_compatibility/core_crypto/fft_impl/fft64/crypto/bootstrap/mod.rs new file mode 100644 index 0000000000..69d9f6dee4 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/fft_impl/fft64/crypto/bootstrap/mod.rs @@ -0,0 +1,83 @@ +use crate::core_crypto::commons::traits::Container; +use crate::core_crypto::fft_impl::fft64::c64; +use crate::core_crypto::fft_impl::fft64::crypto::bootstrap::FourierLweBootstrapKey; +use crate::core_crypto::fft_impl::fft64::math::fft::FourierPolynomialList; +use crate::forward_compatibility::ConvertInto; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; +use next_tfhe::core_crypto::fft_impl::fft64::crypto::bootstrap::FourierLweBootstrapKey as NextFourierLweBootstrapKey; +use next_tfhe::core_crypto::fft_impl::fft64::math::fft::FourierPolynomialList as NextFourierPolynomialList; +use next_tfhe::core_crypto::fft_impl::fft64::{c64 as NextC64, ABox as NextABox}; + +impl crate::forward_compatibility::ConvertFrom> + for NextFourierLweBootstrapKey> +where + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: FourierLweBootstrapKey) -> Self { + let input_lwe_dimension = value.input_lwe_dimension(); + let glwe_size = value.glwe_size(); + let polynomial_size = value.polynomial_size(); + let decomposition_base_log = value.decomposition_base_log(); + let decomposition_level_count = value.decomposition_level_count(); + let data = value.data(); + + let poly_list = FourierPolynomialList { + data, + polynomial_size, + }; + + let next_poly_list: NextFourierPolynomialList<_> = poly_list.convert_into(); + let data = next_poly_list.data; + + Self::from_container( + data, + input_lwe_dimension.convert_into(), + glwe_size.convert_into(), + polynomial_size.convert_into(), + decomposition_base_log.convert_into(), + decomposition_level_count.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_fourier_lwe_bootstrap_key() { + use crate::core_crypto::fft_impl::fft64::crypto::bootstrap::FourierLweBootstrapKey; + use next_tfhe::core_crypto::fft_impl::fft64::crypto::bootstrap::FourierLweBootstrapKey as NextFourierLweBootstrapKey; + + use crate::core_crypto::commons::parameters::*; + use crate::core_crypto::fft_impl::fft64::c64; + use aligned_vec::avec; + + let input_lwe_dimension = LweDimension(100); + let polynomial_size = PolynomialSize(2048); + let glwe_size = GlweSize(2); + let decomposition_level_count = DecompositionLevelCount(1); + let decomposition_base_log = DecompositionBaseLog(23); + let container = avec![ + c64::new(0.0, 0.0); + input_lwe_dimension.0 + * polynomial_size.to_fourier_polynomial_size().0 + * glwe_size.0 + * glwe_size.0 + * decomposition_level_count.0 + ] + .into_boxed_slice(); + + let tfhe_struct = FourierLweBootstrapKey::from_container( + container, + input_lwe_dimension, + glwe_size, + polynomial_size, + decomposition_base_log, + decomposition_level_count, + ); + + let _next_tfhe_struct: NextFourierLweBootstrapKey<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/fft_impl/fft64/crypto/ggsw/mod.rs b/tfhe/src/forward_compatibility/core_crypto/fft_impl/fft64/crypto/ggsw/mod.rs new file mode 100644 index 0000000000..f159bce023 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/fft_impl/fft64/crypto/ggsw/mod.rs @@ -0,0 +1,78 @@ +use crate::core_crypto::commons::traits::Container; +use crate::core_crypto::fft_impl::fft64::c64; +use crate::core_crypto::fft_impl::fft64::crypto::ggsw::FourierGgswCiphertext; +use crate::core_crypto::fft_impl::fft64::math::fft::FourierPolynomialList; +use crate::forward_compatibility::ConvertInto; +use next_tfhe::core_crypto::commons::traits::Container as NextContainer; +use next_tfhe::core_crypto::fft_impl::fft64::crypto::ggsw::FourierGgswCiphertext as NextFourierGgswCiphertext; +use next_tfhe::core_crypto::fft_impl::fft64::math::fft::FourierPolynomialList as NextFourierPolynomialList; +use next_tfhe::core_crypto::fft_impl::fft64::{c64 as NextC64, ABox as NextABox}; + +impl crate::forward_compatibility::ConvertFrom> + for NextFourierGgswCiphertext> +where + C: Container + NextContainer, +{ + #[inline] + fn convert_from(value: FourierGgswCiphertext) -> Self { + let glwe_size = value.glwe_size(); + let polynomial_size = value.polynomial_size(); + let decomposition_base_log = value.decomposition_base_log(); + let decomposition_level_count = value.decomposition_level_count(); + let data = value.data(); + + let poly_list = FourierPolynomialList { + data, + polynomial_size, + }; + + let next_poly_list: NextFourierPolynomialList<_> = poly_list.convert_into(); + let data = next_poly_list.data; + + Self::from_container( + data, + glwe_size.convert_into(), + polynomial_size.convert_into(), + decomposition_base_log.convert_into(), + decomposition_level_count.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_fourier_ggsw_ciphertext() { + use crate::core_crypto::fft_impl::fft64::crypto::ggsw::FourierGgswCiphertext; + use next_tfhe::core_crypto::fft_impl::fft64::crypto::ggsw::FourierGgswCiphertext as NextFourierGgswCiphertext; + + use crate::core_crypto::commons::parameters::*; + use crate::core_crypto::fft_impl::fft64::c64; + use aligned_vec::avec; + + let polynomial_size = PolynomialSize(2048); + let glwe_size = GlweSize(2); + let decomposition_level_count = DecompositionLevelCount(1); + let decomposition_base_log = DecompositionBaseLog(23); + let container = avec![ + c64::new(0.0, 0.0); + polynomial_size.to_fourier_polynomial_size().0 + * glwe_size.0 + * glwe_size.0 + * decomposition_level_count.0 + ] + .into_boxed_slice(); + + let tfhe_struct = FourierGgswCiphertext::from_container( + container, + glwe_size, + polynomial_size, + decomposition_base_log, + decomposition_level_count, + ); + + let _next_tfhe_struct: NextFourierGgswCiphertext<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/fft_impl/fft64/crypto/mod.rs b/tfhe/src/forward_compatibility/core_crypto/fft_impl/fft64/crypto/mod.rs new file mode 100644 index 0000000000..1a9cfdb294 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/fft_impl/fft64/crypto/mod.rs @@ -0,0 +1,2 @@ +pub mod bootstrap; +pub mod ggsw; diff --git a/tfhe/src/forward_compatibility/core_crypto/fft_impl/fft64/math/fft/mod.rs b/tfhe/src/forward_compatibility/core_crypto/fft_impl/fft64/math/fft/mod.rs new file mode 100644 index 0000000000..3bda75ec17 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/fft_impl/fft64/math/fft/mod.rs @@ -0,0 +1,48 @@ +use crate::core_crypto::commons::traits::Container; +use crate::core_crypto::fft_impl::fft64::c64; +use crate::core_crypto::fft_impl::fft64::math::fft::FourierPolynomialList; +use next_tfhe::core_crypto::fft_impl::fft64::math::fft::FourierPolynomialList as NextFourierPolynomialList; +use next_tfhe::core_crypto::fft_impl::fft64::{c64 as NextC64, ABox as NextABox}; + +impl crate::forward_compatibility::ConvertFrom> + for NextFourierPolynomialList> +where + C: Container, +{ + #[inline] + fn convert_from(value: FourierPolynomialList) -> Self { + // As fft plans are unique per TFHE-rs instance, we need to convert to a common format then + // convert back, for now we go through serialization, otherwise coefficients will be + // completely in the wrong order + let serialized = bincode::serialize(&value).unwrap(); + drop(value); + + bincode::deserialize(&serialized).unwrap() + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_fourier_polynomial_list() { + use crate::core_crypto::commons::parameters::PolynomialSize; + use crate::core_crypto::fft_impl::fft64::c64; + use crate::core_crypto::fft_impl::fft64::math::fft::FourierPolynomialList; + use next_tfhe::core_crypto::fft_impl::fft64::math::fft::FourierPolynomialList as NextFourierPolynomialList; + use next_tfhe::core_crypto::fft_impl::fft64::{c64 as NextC64, ABox as NextABox}; + + let polynomial_size = PolynomialSize(2048); + + let tfhe_struct = FourierPolynomialList { + data: vec![c64::new(0.0, 0.0); polynomial_size.0], + polynomial_size, + }; + + let next_tfhe_struct: NextFourierPolynomialList> = + tfhe_struct.convert_into(); + + assert_eq!(next_tfhe_struct.polynomial_size.0, polynomial_size.0); + } +} diff --git a/tfhe/src/forward_compatibility/core_crypto/fft_impl/fft64/math/mod.rs b/tfhe/src/forward_compatibility/core_crypto/fft_impl/fft64/math/mod.rs new file mode 100644 index 0000000000..14b7cbebdb --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/fft_impl/fft64/math/mod.rs @@ -0,0 +1 @@ +pub mod fft; diff --git a/tfhe/src/forward_compatibility/core_crypto/fft_impl/fft64/mod.rs b/tfhe/src/forward_compatibility/core_crypto/fft_impl/fft64/mod.rs new file mode 100644 index 0000000000..b91478c02c --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/fft_impl/fft64/mod.rs @@ -0,0 +1,2 @@ +pub mod crypto; +pub mod math; diff --git a/tfhe/src/forward_compatibility/core_crypto/fft_impl/mod.rs b/tfhe/src/forward_compatibility/core_crypto/fft_impl/mod.rs new file mode 100644 index 0000000000..dc42a63419 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/fft_impl/mod.rs @@ -0,0 +1,2 @@ +pub mod fft128; +pub mod fft64; diff --git a/tfhe/src/forward_compatibility/core_crypto/mod.rs b/tfhe/src/forward_compatibility/core_crypto/mod.rs new file mode 100644 index 0000000000..b48e2fb4e2 --- /dev/null +++ b/tfhe/src/forward_compatibility/core_crypto/mod.rs @@ -0,0 +1,3 @@ +pub mod commons; +pub mod entities; +pub mod fft_impl; diff --git a/tfhe/src/forward_compatibility/high_level_api/mod.rs b/tfhe/src/forward_compatibility/high_level_api/mod.rs new file mode 100644 index 0000000000..53a491980c --- /dev/null +++ b/tfhe/src/forward_compatibility/high_level_api/mod.rs @@ -0,0 +1,306 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::high_level_api::ClientKey; +use next_tfhe::ClientKey as NextClientKey; + +impl crate::forward_compatibility::ConvertFrom for NextClientKey { + #[inline] + fn convert_from(value: ClientKey) -> Self { + let (key, wopbs_block_parameters) = value.into_raw_parts(); + + Self::from_raw_parts( + key.convert_into(), + wopbs_block_parameters.map(ConvertInto::convert_into), + ) + } +} + +use crate::high_level_api::CompactPublicKey; +use next_tfhe::CompactPublicKey as NextCompactPublicKey; + +impl crate::forward_compatibility::ConvertFrom for NextCompactPublicKey { + #[inline] + fn convert_from(value: CompactPublicKey) -> Self { + let integer_key = value.into_raw_parts(); + + Self::from_raw_parts(integer_key.convert_into()) + } +} + +use crate::high_level_api::CompressedCompactPublicKey; +use next_tfhe::CompressedCompactPublicKey as NextCompressedCompactPublicKey; + +impl crate::forward_compatibility::ConvertFrom + for NextCompressedCompactPublicKey +{ + #[inline] + fn convert_from(value: CompressedCompactPublicKey) -> Self { + let key = value.into_raw_parts(); + + Self::from_raw_parts(key.convert_into()) + } +} + +use crate::high_level_api::CompressedPublicKey; +use next_tfhe::CompressedPublicKey as NextCompressedPublicKey; + +impl crate::forward_compatibility::ConvertFrom for NextCompressedPublicKey { + #[inline] + fn convert_from(value: CompressedPublicKey) -> Self { + let base_integer_key = value.into_raw_parts(); + + Self::from_raw_parts(base_integer_key.convert_into()) + } +} + +use crate::high_level_api::CompressedServerKey; +use next_tfhe::CompressedServerKey as NextCompressedServerKey; + +impl crate::forward_compatibility::ConvertFrom for NextCompressedServerKey { + #[inline] + fn convert_from(value: CompressedServerKey) -> Self { + let integer_key = value.into_raw_parts(); + + Self::from_raw_parts(integer_key.convert_into()) + } +} + +use crate::high_level_api::PublicKey; +use next_tfhe::PublicKey as NextPublicKey; + +impl crate::forward_compatibility::ConvertFrom for NextPublicKey { + #[inline] + fn convert_from(value: PublicKey) -> Self { + let base_integer_key = value.into_raw_parts(); + + Self::from_raw_parts(base_integer_key.convert_into()) + } +} + +use crate::high_level_api::ServerKey; +use next_tfhe::ServerKey as NextServerKey; + +impl crate::forward_compatibility::ConvertFrom for NextServerKey { + #[inline] + fn convert_from(value: ServerKey) -> Self { + let (key, wopbs_key) = value.into_raw_parts(); + + Self::from_raw_parts(key.convert_into(), wopbs_key.map(ConvertInto::convert_into)) + } +} + +macro_rules! impl_convert_from_uint_hl_type { + ($($num_bits: literal),* $(,)?) => { + $( + ::paste::paste! { + impl_convert_from_uint_hl_type!( + [] { + [] + } + ); + impl_convert_from_uint_hl_type!( + compressed => + [] { + [] + } + ); + } + )* + }; + ($old_ty: ident { $old_id: ident } ) => { + impl crate::forward_compatibility::ConvertFrom + for next_tfhe::$old_ty + { + fn convert_from(value: crate::high_level_api::$old_ty) -> Self { + let (inner, _id) = value.into_raw_parts(); + + Self::from_raw_parts(inner.convert_into(), next_tfhe::$old_id) + } + } + }; + (compressed => $old_ty: ident { $old_id: ident } ) => { + impl crate::forward_compatibility::ConvertFrom + for next_tfhe::$old_ty + { + fn convert_from(value: crate::high_level_api::$old_ty) -> Self { + let (inner, _id) = value.into_raw_parts(); + + Self::from_integer_compressed_radix_ciphertext( + inner.convert_into(), + next_tfhe::$old_id + ) + } + } + }; +} + +impl_convert_from_uint_hl_type!(8, 10, 12, 14, 16, 32, 64, 128, 256); + +macro_rules! impl_convert_from_int_hl_type { + ($($num_bits: literal),* $(,)?) => { + $( + ::paste::paste! { + impl_convert_from_int_hl_type!( + [] { + [] + } + ); + impl_convert_from_int_hl_type!( + compressed => + [] { + [] + } + ); + } + )* + }; + ($old_ty: ident { $old_id: ident } ) => { + impl crate::forward_compatibility::ConvertFrom + for next_tfhe::$old_ty + { + fn convert_from(value: crate::high_level_api::$old_ty) -> Self { + let (inner, _id) = value.into_raw_parts(); + + Self::from_raw_parts(inner.convert_into(), next_tfhe::$old_id) + } + } + }; + (compressed => $old_ty: ident { $old_id: ident } ) => { + impl crate::forward_compatibility::ConvertFrom + for next_tfhe::$old_ty + { + fn convert_from(value: crate::high_level_api::$old_ty) -> Self { + let (inner, _id) = value.into_raw_parts(); + + Self::from_integer_compressed_signed_radix_ciphertext( + inner.convert_into(), + next_tfhe::$old_id + ) + } + } + }; +} + +impl_convert_from_int_hl_type!(8, 16, 32, 64, 128, 256); + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_client_key() { + use crate::{ClientKey, ConfigBuilder}; + use next_tfhe::ClientKey as NextClientKey; + + let config = ConfigBuilder::default(); + let tfhe_struct = ClientKey::generate(config); + + let _next_tfhe_struct: NextClientKey = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_compact_public_key() { + use crate::{ClientKey, CompactPublicKey, ConfigBuilder}; + use next_tfhe::CompactPublicKey as NextCompactPublicKey; + + let config = ConfigBuilder::default(); + let cks = ClientKey::generate(config); + + let tfhe_struct = CompactPublicKey::new(&cks); + let _next_tfhe_struct: NextCompactPublicKey = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_compressed_compact_public_key() { + use crate::{ClientKey, CompressedCompactPublicKey, ConfigBuilder}; + use next_tfhe::CompressedCompactPublicKey as NextCompressedCompactPublicKey; + + let config = ConfigBuilder::default(); + let cks = ClientKey::generate(config); + + let tfhe_struct = CompressedCompactPublicKey::new(&cks); + let _next_tfhe_struct: NextCompressedCompactPublicKey = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_compressed_public_key() { + use crate::{ClientKey, CompressedPublicKey, ConfigBuilder}; + use next_tfhe::CompressedPublicKey as NextCompressedPublicKey; + + let config = ConfigBuilder::default(); + let cks = ClientKey::generate(config); + + let tfhe_struct = CompressedPublicKey::new(&cks); + let _next_tfhe_struct: NextCompressedPublicKey = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_compressed_server_key() { + use crate::{ClientKey, CompressedServerKey, ConfigBuilder}; + use next_tfhe::CompressedServerKey as NextCompressedServerKey; + + let config = ConfigBuilder::default(); + let cks = ClientKey::generate(config); + + let tfhe_struct = CompressedServerKey::new(&cks); + let _next_tfhe_struct: NextCompressedServerKey = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_public_key() { + use crate::{ClientKey, ConfigBuilder, PublicKey}; + use next_tfhe::PublicKey as NextPublicKey; + + let config = ConfigBuilder::default(); + let cks = ClientKey::generate(config); + + let tfhe_struct = PublicKey::new(&cks); + let _next_tfhe_struct: NextPublicKey = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_server_key() { + use crate::{ClientKey, ConfigBuilder, ServerKey}; + use next_tfhe::ServerKey as NextServerKey; + + let config = ConfigBuilder::default(); + let cks = ClientKey::generate(config); + + let tfhe_struct = ServerKey::new(&cks); + let _next_tfhe_struct: NextServerKey = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_ciphertexts() { + use crate::prelude::*; + use crate::*; + use next_tfhe::{ + CompressedFheInt64 as NextCompressedFheInt64, + CompressedFheUint64 as NextCompressedFheUint64, FheInt64 as NextFheInt64, + FheUint64 as NextFheUint64, + }; + + let config = ConfigBuilder::default(); + let cks = ClientKey::generate(config); + + { + let tfhe_struct = FheUint64::encrypt(42u64, &cks); + let _next_tfhe_struct: NextFheUint64 = tfhe_struct.convert_into(); + } + + { + let tfhe_struct = FheInt64::encrypt(-42i64, &cks); + let _next_tfhe_struct: NextFheInt64 = tfhe_struct.convert_into(); + } + + { + let tfhe_struct = CompressedFheUint64::encrypt(42u64, &cks); + let _next_tfhe_struct: NextCompressedFheUint64 = tfhe_struct.convert_into(); + } + + { + let tfhe_struct = CompressedFheInt64::encrypt(-42i64, &cks); + let _next_tfhe_struct: NextCompressedFheInt64 = tfhe_struct.convert_into(); + } + } +} diff --git a/tfhe/src/forward_compatibility/integer/ciphertext/mod.rs b/tfhe/src/forward_compatibility/integer/ciphertext/mod.rs new file mode 100644 index 0000000000..0d9c27b95d --- /dev/null +++ b/tfhe/src/forward_compatibility/integer/ciphertext/mod.rs @@ -0,0 +1,154 @@ +use crate::forward_compatibility::ConvertInto; +use crate::integer::ciphertext::IntegerCiphertext; +use crate::shortint::{Ciphertext, CompressedCiphertext}; +use next_tfhe::shortint::{ + Ciphertext as NextCiphertext, CompressedCiphertext as NextCompressedCiphertext, +}; + +use crate::integer::ciphertext::BaseRadixCiphertext; +use next_tfhe::integer::ciphertext::BaseRadixCiphertext as NextBaseRadixCiphertext; + +impl crate::forward_compatibility::ConvertFrom> + for NextBaseRadixCiphertext +{ + #[inline] + fn convert_from(value: BaseRadixCiphertext) -> Self { + let blocks: Vec = value + .blocks() + .iter() + .map(|block| block.clone().convert_into()) + .collect(); + blocks.into() + } +} + +impl crate::forward_compatibility::ConvertFrom> + for NextBaseRadixCiphertext +{ + #[inline] + fn convert_from(value: BaseRadixCiphertext) -> Self { + let blocks: Vec = value + .blocks + .into_iter() + .map(ConvertInto::convert_into) + .collect(); + blocks.into() + } +} + +use crate::integer::ciphertext::BaseSignedRadixCiphertext; +use next_tfhe::integer::ciphertext::BaseSignedRadixCiphertext as NextBaseSignedRadixCiphertext; + +impl crate::forward_compatibility::ConvertFrom> + for NextBaseSignedRadixCiphertext +{ + #[inline] + fn convert_from(value: BaseSignedRadixCiphertext) -> Self { + let blocks: Vec = value + .blocks() + .iter() + .map(|block| block.clone().convert_into()) + .collect(); + blocks.into() + } +} + +impl crate::forward_compatibility::ConvertFrom> + for NextBaseSignedRadixCiphertext +{ + #[inline] + fn convert_from(value: BaseSignedRadixCiphertext) -> Self { + let blocks: Vec = value + .blocks + .into_iter() + .map(ConvertInto::convert_into) + .collect(); + blocks.into() + } +} + +use crate::integer::ciphertext::BaseCrtCiphertext; +use next_tfhe::integer::ciphertext::BaseCrtCiphertext as NextBaseCrtCiphertext; + +impl crate::forward_compatibility::ConvertFrom> + for NextBaseCrtCiphertext +{ + #[inline] + fn convert_from(value: BaseCrtCiphertext) -> Self { + let moduli = value.moduli(); + let blocks = value.blocks().to_vec(); + + (blocks, moduli).into() + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_base_radix_ciphertext() { + use next_tfhe::integer::ciphertext::BaseRadixCiphertext as NextBaseRadixCiphertext; + + use crate::integer::gen_keys_radix; + use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS; + + let (cks, _sks) = gen_keys_radix(PARAM_MESSAGE_2_CARRY_2_KS_PBS, 4); + let tfhe_struct = cks.encrypt(42u64); + let _next_tfhe_struct: NextBaseRadixCiphertext<_> = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_base_compressed_radix_ciphertext() { + use next_tfhe::integer::ciphertext::BaseRadixCiphertext as NextBaseRadixCiphertext; + + use crate::integer::{gen_keys_radix, CompressedPublicKey}; + use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS; + + let (cks, _sks) = gen_keys_radix(PARAM_MESSAGE_2_CARRY_2_KS_PBS, 4); + let cpk = CompressedPublicKey::new(&cks); + let tfhe_struct = cpk.encrypt_radix(42u64, 4); + let _next_tfhe_struct: NextBaseRadixCiphertext<_> = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_base_signed_radix_ciphertext() { + use next_tfhe::integer::ciphertext::BaseSignedRadixCiphertext as NextBaseSignedRadixCiphertext; + + use crate::integer::gen_keys_radix; + use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS; + + let (cks, _sks) = gen_keys_radix(PARAM_MESSAGE_2_CARRY_2_KS_PBS, 4); + let tfhe_struct = cks.encrypt_signed(-42i64); + let _next_tfhe_struct: NextBaseSignedRadixCiphertext<_> = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_base_signed_compressed_radix_ciphertext() { + use next_tfhe::integer::ciphertext::BaseSignedRadixCiphertext as NextBaseSignedRadixCiphertext; + + use crate::integer::{gen_keys_radix, CompressedPublicKey}; + use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS; + + let (cks, _sks) = gen_keys_radix(PARAM_MESSAGE_2_CARRY_2_KS_PBS, 4); + let cpk = CompressedPublicKey::new(&cks); + let tfhe_struct = cpk.encrypt_signed_radix(-42i64, 4); + let _next_tfhe_struct: NextBaseSignedRadixCiphertext<_> = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_base_crt_ciphertext() { + use next_tfhe::integer::ciphertext::BaseCrtCiphertext as NextBaseCrtCiphertext; + + use crate::integer::gen_keys_crt; + use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS; + + let (cks, _sks) = gen_keys_crt( + PARAM_MESSAGE_2_CARRY_2_KS_PBS, + vec![3u64, 11, 13, 19, 23, 29, 31, 32], + ); + + let tfhe_struct = cks.encrypt(32); + let _next_tfhe_struct: NextBaseCrtCiphertext<_> = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/integer/client_key/mod.rs b/tfhe/src/forward_compatibility/integer/client_key/mod.rs new file mode 100644 index 0000000000..dd9076ed67 --- /dev/null +++ b/tfhe/src/forward_compatibility/integer/client_key/mod.rs @@ -0,0 +1,77 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::integer::client_key::CrtClientKey; +use next_tfhe::integer::client_key::CrtClientKey as NextCrtClientKey; + +impl crate::forward_compatibility::ConvertFrom for NextCrtClientKey { + #[inline] + fn convert_from(value: CrtClientKey) -> Self { + let key = value.as_ref().to_owned(); + let moduli = value.moduli().to_vec(); + + Self::from((key.convert_into(), moduli)) + } +} + +use crate::integer::client_key::RadixClientKey; +use next_tfhe::integer::client_key::RadixClientKey as NextRadixClientKey; + +impl crate::forward_compatibility::ConvertFrom for NextRadixClientKey { + #[inline] + fn convert_from(value: RadixClientKey) -> Self { + let key = value.as_ref().to_owned(); + let num_blocks = value.num_blocks(); + + Self::from((key.convert_into(), num_blocks)) + } +} + +use crate::integer::client_key::ClientKey; +use next_tfhe::integer::client_key::ClientKey as NextClientKey; + +impl crate::forward_compatibility::ConvertFrom for NextClientKey { + #[inline] + fn convert_from(value: ClientKey) -> Self { + let ClientKey { key } = value; + + Self::from_raw_parts(key.convert_into()) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_crt_client_key() { + use crate::integer::client_key::CrtClientKey; + use next_tfhe::integer::client_key::CrtClientKey as NextCrtClientKey; + + use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS; + + let tfhe_struct = CrtClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS, vec![2u64, 3u64]); + let _next_tfhe_struct: NextCrtClientKey = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_radix_client_key() { + use crate::integer::client_key::RadixClientKey; + use next_tfhe::integer::client_key::RadixClientKey as NextRadixClientKey; + + use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS; + + let tfhe_struct = RadixClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS, 32); + let _next_tfhe_struct: NextRadixClientKey = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_client_key() { + use crate::integer::client_key::ClientKey; + use next_tfhe::integer::client_key::ClientKey as NextClientKey; + + use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS; + + let tfhe_struct = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS); + let _next_tfhe_struct: NextClientKey = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/integer/key_switching_key/mod.rs b/tfhe/src/forward_compatibility/integer/key_switching_key/mod.rs new file mode 100644 index 0000000000..67872eec53 --- /dev/null +++ b/tfhe/src/forward_compatibility/integer/key_switching_key/mod.rs @@ -0,0 +1,40 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::integer::key_switching_key::KeySwitchingKey; +use next_tfhe::integer::key_switching_key::KeySwitchingKey as NextKeySwitchingKey; + +impl crate::forward_compatibility::ConvertFrom for NextKeySwitchingKey { + #[inline] + fn convert_from(value: KeySwitchingKey) -> Self { + let key = value.into_raw_parts(); + + Self::from_raw_parts(key.convert_into()) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_key_switching_key() { + use crate::integer::key_switching_key::KeySwitchingKey; + use next_tfhe::integer::key_switching_key::KeySwitchingKey as NextKeySwitchingKey; + + use crate::integer::{gen_keys, IntegerKeyKind}; + use crate::shortint::parameters::{ + ShortintKeySwitchingParameters, PARAM_MESSAGE_2_CARRY_2_KS_PBS, + }; + + let (cks1, sks1) = gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS, IntegerKeyKind::Radix); + let (cks2, sks2) = gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS, IntegerKeyKind::Radix); + + let ksk_params = ShortintKeySwitchingParameters::new( + cks2.parameters().ks_base_log(), + cks2.parameters().ks_level(), + ); + let tfhe_struct = KeySwitchingKey::new((&cks1, &sks1), (&cks2, &sks2), ksk_params); + + let _next_tfhe_struct: NextKeySwitchingKey = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/integer/mod.rs b/tfhe/src/forward_compatibility/integer/mod.rs new file mode 100644 index 0000000000..3d7f5dabdf --- /dev/null +++ b/tfhe/src/forward_compatibility/integer/mod.rs @@ -0,0 +1,6 @@ +pub mod ciphertext; +pub mod client_key; +pub mod key_switching_key; +pub mod public_key; +pub mod server_key; +pub mod wopbs; diff --git a/tfhe/src/forward_compatibility/integer/public_key/compact/mod.rs b/tfhe/src/forward_compatibility/integer/public_key/compact/mod.rs new file mode 100644 index 0000000000..1d9c97a674 --- /dev/null +++ b/tfhe/src/forward_compatibility/integer/public_key/compact/mod.rs @@ -0,0 +1,58 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::integer::public_key::compact::CompactPublicKey; +use next_tfhe::integer::public_key::compact::CompactPublicKey as NextCompactPublicKey; + +impl crate::forward_compatibility::ConvertFrom for NextCompactPublicKey { + #[inline] + fn convert_from(value: CompactPublicKey) -> Self { + let CompactPublicKey { key } = value; + + Self::from_raw_parts(key.convert_into()) + } +} + +use crate::integer::public_key::compact::CompressedCompactPublicKey; +use next_tfhe::integer::public_key::compact::CompressedCompactPublicKey as NextCompressedCompactPublicKey; + +impl crate::forward_compatibility::ConvertFrom + for NextCompressedCompactPublicKey +{ + #[inline] + fn convert_from(value: CompressedCompactPublicKey) -> Self { + let CompressedCompactPublicKey { key } = value; + + Self::from_raw_parts(key.convert_into()) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_compact_public_key() { + use crate::integer::public_key::compact::CompactPublicKey; + use next_tfhe::integer::public_key::compact::CompactPublicKey as NextCompactPublicKey; + + use crate::integer::{gen_keys, IntegerKeyKind}; + use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS; + + let (cks, _sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS, IntegerKeyKind::Radix); + let tfhe_struct = CompactPublicKey::new(&cks); + let _next_tfhe_struct: NextCompactPublicKey = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_compressed_compact_public_key() { + use crate::integer::public_key::compact::CompressedCompactPublicKey; + use next_tfhe::integer::public_key::compact::CompressedCompactPublicKey as NextCompressedCompactPublicKey; + + use crate::integer::{gen_keys, IntegerKeyKind}; + use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS; + + let (cks, _sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS, IntegerKeyKind::Radix); + let tfhe_struct = CompressedCompactPublicKey::new(&cks); + let _next_tfhe_struct: NextCompressedCompactPublicKey = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/integer/public_key/compressed/mod.rs b/tfhe/src/forward_compatibility/integer/public_key/compressed/mod.rs new file mode 100644 index 0000000000..2874feff10 --- /dev/null +++ b/tfhe/src/forward_compatibility/integer/public_key/compressed/mod.rs @@ -0,0 +1,32 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::integer::public_key::compressed::CompressedPublicKey; +use next_tfhe::integer::public_key::compressed::CompressedPublicKey as NextCompressedPublicKey; + +impl crate::forward_compatibility::ConvertFrom for NextCompressedPublicKey { + #[inline] + fn convert_from(value: CompressedPublicKey) -> Self { + let CompressedPublicKey { key } = value; + + Self::from_raw_parts(key.convert_into()) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_compressed_public_key() { + use crate::integer::public_key::compressed::CompressedPublicKey; + use next_tfhe::integer::public_key::compressed::CompressedPublicKey as NextCompressedPublicKey; + + use crate::integer::{gen_keys, IntegerKeyKind}; + use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS; + + let (cks, _sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS, IntegerKeyKind::Radix); + + let tfhe_struct = CompressedPublicKey::new(&cks); + let _next_tfhe_struct: NextCompressedPublicKey = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/integer/public_key/mod.rs b/tfhe/src/forward_compatibility/integer/public_key/mod.rs new file mode 100644 index 0000000000..1618ab7e94 --- /dev/null +++ b/tfhe/src/forward_compatibility/integer/public_key/mod.rs @@ -0,0 +1,3 @@ +pub mod compact; +pub mod compressed; +pub mod standard; diff --git a/tfhe/src/forward_compatibility/integer/public_key/standard/mod.rs b/tfhe/src/forward_compatibility/integer/public_key/standard/mod.rs new file mode 100644 index 0000000000..ffd25c8331 --- /dev/null +++ b/tfhe/src/forward_compatibility/integer/public_key/standard/mod.rs @@ -0,0 +1,31 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::integer::public_key::standard::PublicKey; +use next_tfhe::integer::public_key::standard::PublicKey as NextPublicKey; + +impl crate::forward_compatibility::ConvertFrom for NextPublicKey { + #[inline] + fn convert_from(value: PublicKey) -> Self { + let key = value.into_raw_parts(); + + Self::from_raw_parts(key.convert_into()) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_public_key() { + use crate::integer::public_key::standard::PublicKey; + use next_tfhe::integer::public_key::standard::PublicKey as NextPublicKey; + + use crate::integer::{gen_keys, IntegerKeyKind}; + use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS; + + let (cks, _sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS, IntegerKeyKind::Radix); + let tfhe_struct = PublicKey::new(&cks); + let _next_tfhe_struct: NextPublicKey = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/integer/server_key/mod.rs b/tfhe/src/forward_compatibility/integer/server_key/mod.rs new file mode 100644 index 0000000000..1f0abdf3be --- /dev/null +++ b/tfhe/src/forward_compatibility/integer/server_key/mod.rs @@ -0,0 +1,54 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::integer::server_key::ServerKey; +use next_tfhe::integer::server_key::ServerKey as NextServerKey; + +impl crate::forward_compatibility::ConvertFrom for NextServerKey { + #[inline] + fn convert_from(value: ServerKey) -> Self { + let ServerKey { key } = value; + + Self::from_raw_parts(key.convert_into()) + } +} + +use crate::integer::server_key::CompressedServerKey; +use next_tfhe::integer::server_key::CompressedServerKey as NextCompressedServerKey; + +impl crate::forward_compatibility::ConvertFrom for NextCompressedServerKey { + #[inline] + fn convert_from(value: CompressedServerKey) -> Self { + let CompressedServerKey { key } = value; + + Self::from_raw_parts(key.convert_into()) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_server_key() { + use next_tfhe::integer::server_key::ServerKey as NextServerKey; + + use crate::integer::gen_keys_radix; + use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS; + + let (_cks, tfhe_struct) = gen_keys_radix(PARAM_MESSAGE_2_CARRY_2_KS_PBS, 4); + let _next_tfhe_struct: NextServerKey = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_compressed_server_key() { + use crate::integer::server_key::CompressedServerKey; + use next_tfhe::integer::server_key::CompressedServerKey as NextCompressedServerKey; + + use crate::integer::ClientKey; + use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS; + + let cks = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS); + let tfhe_struct = CompressedServerKey::new_radix_compressed_server_key(&cks); + let _next_tfhe_struct: NextCompressedServerKey = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/integer/wopbs/mod.rs b/tfhe/src/forward_compatibility/integer/wopbs/mod.rs new file mode 100644 index 0000000000..86f2c05c88 --- /dev/null +++ b/tfhe/src/forward_compatibility/integer/wopbs/mod.rs @@ -0,0 +1,34 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::integer::wopbs::WopbsKey; +use next_tfhe::integer::wopbs::WopbsKey as NextWopbsKey; + +impl crate::forward_compatibility::ConvertFrom for NextWopbsKey { + #[inline] + fn convert_from(value: WopbsKey) -> Self { + let wopbskey = value.into_raw_parts(); + + Self::from_raw_parts(wopbskey.convert_into()) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_wopbs_key() { + use crate::integer::wopbs::WopbsKey; + use next_tfhe::integer::wopbs::WopbsKey as NextWopbsKey; + + use crate::integer::{gen_keys, IntegerKeyKind}; + use crate::shortint::parameters::parameters_wopbs_message_carry::WOPBS_PARAM_MESSAGE_2_CARRY_2_KS_PBS; + use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS; + + let (cks, sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS, IntegerKeyKind::Radix); + + let tfhe_struct = + WopbsKey::new_wopbs_key(&cks, &sks, &WOPBS_PARAM_MESSAGE_2_CARRY_2_KS_PBS); + let _next_tfhe_struct: NextWopbsKey = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/mod.rs b/tfhe/src/forward_compatibility/mod.rs new file mode 100644 index 0000000000..ccd0148799 --- /dev/null +++ b/tfhe/src/forward_compatibility/mod.rs @@ -0,0 +1,46 @@ +#[cfg(feature = "boolean")] +pub mod boolean; +pub mod core_crypto; +#[cfg(feature = "integer")] +pub mod high_level_api; +#[cfg(feature = "integer")] +pub mod integer; +#[cfg(feature = "shortint")] +pub mod shortint; + +pub trait ConvertFrom: Sized { + fn convert_from(value: T) -> Self; +} + +pub trait ConvertInto: Sized { + fn convert_into(self) -> T; +} + +impl ConvertInto for T +where + U: ConvertFrom, +{ + #[inline] + fn convert_into(self) -> U { + U::convert_from(self) + } +} + +macro_rules! impl_for_native_type { + ($($Type: ty),* $(,)?) => { + $( + impl ConvertFrom<$Type> for $Type { + #[inline(always)] + fn convert_from(value: $Type) -> Self { + value + } + } + )* + }; +} + +// The blanket implementation for ConvertFrom for T was causing issues, so the trait is +// implemented "manually" for basic types to make our lives easier +impl_for_native_type!( + bool, u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64 +); diff --git a/tfhe/src/forward_compatibility/shortint/ciphertext/mod.rs b/tfhe/src/forward_compatibility/shortint/ciphertext/mod.rs new file mode 100644 index 0000000000..e6c042cc87 --- /dev/null +++ b/tfhe/src/forward_compatibility/shortint/ciphertext/mod.rs @@ -0,0 +1,173 @@ +use crate::forward_compatibility::ConvertInto; +use crate::shortint::ciphertext::Degree; +use next_tfhe::shortint::ciphertext::Degree as NextDegree; + +impl crate::forward_compatibility::ConvertFrom for NextDegree { + #[inline] + fn convert_from(value: Degree) -> Self { + let field_0 = value.get(); + Self::new(field_0) + } +} + +use crate::shortint::ciphertext::MaxNoiseLevel; +use next_tfhe::shortint::ciphertext::MaxNoiseLevel as NextMaxNoiseLevel; + +impl crate::forward_compatibility::ConvertFrom for NextMaxNoiseLevel { + #[inline] + fn convert_from(value: MaxNoiseLevel) -> Self { + let field_0 = value.get(); + Self::new(field_0) + } +} + +use crate::shortint::ciphertext::NoiseLevel; +use next_tfhe::shortint::ciphertext::NoiseLevel as NextNoiseLevel; + +impl crate::forward_compatibility::ConvertFrom for NextNoiseLevel { + #[inline] + fn convert_from(value: NoiseLevel) -> Self { + let field_0 = value.get(); + // Apparently we cannot set any NoiseLevel we want + Self::NOMINAL * field_0 + } +} + +use crate::shortint::ciphertext::Ciphertext; +use next_tfhe::shortint::ciphertext::Ciphertext as NextCiphertext; + +impl crate::forward_compatibility::ConvertFrom for NextCiphertext { + #[inline] + fn convert_from(value: Ciphertext) -> Self { + let Ciphertext { + ct, + degree, + noise_level, + message_modulus, + carry_modulus, + pbs_order, + } = value; + + Self::new( + ct.convert_into(), + degree.convert_into(), + noise_level.convert_into(), + message_modulus.convert_into(), + carry_modulus.convert_into(), + pbs_order.convert_into(), + ) + } +} + +use crate::shortint::ciphertext::CompressedCiphertext; +use next_tfhe::shortint::ciphertext::CompressedCiphertext as NextCompressedCiphertext; + +impl crate::forward_compatibility::ConvertFrom for NextCompressedCiphertext { + #[inline] + fn convert_from(value: CompressedCiphertext) -> Self { + let CompressedCiphertext { + ct, + degree, + noise_level, + message_modulus, + carry_modulus, + pbs_order, + } = value; + + // A CompressedCiphertext is just out of encryption so has a nominal noise level. + Self::from_raw_parts( + ct.convert_into(), + degree.convert_into(), + message_modulus.convert_into(), + carry_modulus.convert_into(), + pbs_order.convert_into(), + noise_level.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_degree() { + use crate::shortint::ciphertext::Degree; + use next_tfhe::shortint::ciphertext::Degree as NextDegree; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = Degree::new(rng.gen()); + let next_tfhe_struct: NextDegree = tfhe_struct.convert_into(); + + assert_eq!(tfhe_struct.get(), next_tfhe_struct.get()); + } + + #[test] + fn test_conversion_max_noise_level() { + use crate::shortint::ciphertext::MaxNoiseLevel; + use next_tfhe::shortint::ciphertext::MaxNoiseLevel as NextMaxNoiseLevel; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = MaxNoiseLevel::new(rng.gen()); + let next_tfhe_struct: NextMaxNoiseLevel = tfhe_struct.convert_into(); + + assert_eq!(tfhe_struct.get(), next_tfhe_struct.get()); + } + + #[test] + fn test_conversion_noise_level() { + use crate::shortint::ciphertext::NoiseLevel; + use next_tfhe::shortint::ciphertext::NoiseLevel as NextNoiseLevel; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = NoiseLevel::NOMINAL * rng.gen::(); + let next_tfhe_struct: NextNoiseLevel = tfhe_struct.convert_into(); + + assert_eq!(tfhe_struct.get(), next_tfhe_struct.get()); + } + + #[test] + fn test_conversion_ciphertext() { + use next_tfhe::shortint::ciphertext::Ciphertext as NextCiphertext; + + use crate::shortint::gen_keys; + use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS; + + let (cks, _sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS); + + let tfhe_struct = cks.encrypt(2); + let next_tfhe_struct: NextCiphertext = tfhe_struct.clone().convert_into(); + + assert_eq!( + next_tfhe_struct.noise_level().get(), + tfhe_struct.noise_level().get() + ); + } + + #[test] + fn test_conversion_compressed_ciphertext() { + use next_tfhe::shortint::ciphertext::CompressedCiphertext as NextCompressedCiphertext; + + use crate::shortint::gen_keys; + use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS; + + let (cks, _sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS); + + let tfhe_struct = cks.encrypt_compressed(2); + let next_tfhe_struct: NextCompressedCiphertext = tfhe_struct.convert_into(); + + assert_eq!( + next_tfhe_struct.noise_level, + next_tfhe::shortint::ciphertext::NoiseLevel::NOMINAL + ); + } +} diff --git a/tfhe/src/forward_compatibility/shortint/client_key/mod.rs b/tfhe/src/forward_compatibility/shortint/client_key/mod.rs new file mode 100644 index 0000000000..dbbccc2ac5 --- /dev/null +++ b/tfhe/src/forward_compatibility/shortint/client_key/mod.rs @@ -0,0 +1,36 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::shortint::client_key::ClientKey; +use next_tfhe::shortint::client_key::ClientKey as NextClientKey; + +impl crate::forward_compatibility::ConvertFrom for NextClientKey { + #[inline] + fn convert_from(value: ClientKey) -> Self { + let ClientKey { + glwe_secret_key, + lwe_secret_key, + parameters, + } = value; + + Self::from_raw_parts( + glwe_secret_key.convert_into(), + lwe_secret_key.convert_into(), + parameters.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_client_key() { + use crate::shortint::client_key::ClientKey; + use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS; + use next_tfhe::shortint::client_key::ClientKey as NextClientKey; + + let tfhe_struct = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS); + let _next_tfhe_struct: NextClientKey = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/shortint/key_switching_key/mod.rs b/tfhe/src/forward_compatibility/shortint/key_switching_key/mod.rs new file mode 100644 index 0000000000..bbd1f48647 --- /dev/null +++ b/tfhe/src/forward_compatibility/shortint/key_switching_key/mod.rs @@ -0,0 +1,50 @@ +use crate::forward_compatibility::ConvertInto; +use crate::shortint::key_switching_key::KeySwitchingKey; +use next_tfhe::shortint::key_switching_key::KeySwitchingKey as NextKeySwitchingKey; + +impl crate::forward_compatibility::ConvertFrom for NextKeySwitchingKey { + #[inline] + fn convert_from(value: KeySwitchingKey) -> Self { + let KeySwitchingKey { + key_switching_key, + dest_server_key, + src_server_key, + cast_rshift, + } = value; + + Self::from_raw_parts( + key_switching_key.convert_into(), + dest_server_key.convert_into(), + src_server_key.convert_into(), + cast_rshift.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_key_switching_key() { + use crate::shortint::key_switching_key::KeySwitchingKey; + use next_tfhe::shortint::key_switching_key::KeySwitchingKey as NextKeySwitchingKey; + + use crate::shortint::gen_keys; + use crate::shortint::parameters::key_switching::PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS; + use crate::shortint::parameters::{ + PARAM_MESSAGE_1_CARRY_1_KS_PBS, PARAM_MESSAGE_2_CARRY_2_KS_PBS, + }; + + let (cks1, sks1) = gen_keys(PARAM_MESSAGE_1_CARRY_1_KS_PBS); + let (cks2, sks2) = gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS); + + let tfhe_struct = KeySwitchingKey::new( + (&cks1, &sks1), + (&cks2, &sks2), + PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS, + ); + + let _next_tfhe_struct: NextKeySwitchingKey = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/shortint/mod.rs b/tfhe/src/forward_compatibility/shortint/mod.rs new file mode 100644 index 0000000000..b8b539a4ff --- /dev/null +++ b/tfhe/src/forward_compatibility/shortint/mod.rs @@ -0,0 +1,7 @@ +pub mod ciphertext; +pub mod client_key; +pub mod key_switching_key; +pub mod parameters; +pub mod public_key; +pub mod server_key; +pub mod wopbs; diff --git a/tfhe/src/forward_compatibility/shortint/parameters/key_switching/mod.rs b/tfhe/src/forward_compatibility/shortint/parameters/key_switching/mod.rs new file mode 100644 index 0000000000..96866d4c2e --- /dev/null +++ b/tfhe/src/forward_compatibility/shortint/parameters/key_switching/mod.rs @@ -0,0 +1,43 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::shortint::parameters::key_switching::ShortintKeySwitchingParameters; +use next_tfhe::shortint::parameters::key_switching::ShortintKeySwitchingParameters as NextShortintKeySwitchingParameters; + +impl crate::forward_compatibility::ConvertFrom + for NextShortintKeySwitchingParameters +{ + #[inline] + fn convert_from(value: ShortintKeySwitchingParameters) -> Self { + let ShortintKeySwitchingParameters { + ks_base_log, + ks_level, + } = value; + Self { + ks_base_log: ks_base_log.convert_into(), + ks_level: ks_level.convert_into(), + } + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_shortint_key_switching_parameters() { + use crate::core_crypto::commons::parameters::*; + use crate::shortint::parameters::key_switching::ShortintKeySwitchingParameters; + use next_tfhe::shortint::parameters::key_switching::ShortintKeySwitchingParameters as NextShortintKeySwitchingParameters; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = ShortintKeySwitchingParameters { + ks_base_log: DecompositionBaseLog(rng.gen()), + ks_level: DecompositionLevelCount(rng.gen()), + }; + + let _next_tfhe_struct: NextShortintKeySwitchingParameters = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/shortint/parameters/mod.rs b/tfhe/src/forward_compatibility/shortint/parameters/mod.rs new file mode 100644 index 0000000000..91c0ef1832 --- /dev/null +++ b/tfhe/src/forward_compatibility/shortint/parameters/mod.rs @@ -0,0 +1,189 @@ +pub mod key_switching; +pub mod multi_bit; +pub mod parameters_wopbs; + +use crate::forward_compatibility::ConvertInto; + +use crate::shortint::parameters::MessageModulus; +use next_tfhe::core_crypto::commons::parameters::DynamicDistribution; +use next_tfhe::shortint::ciphertext::MaxNoiseLevel as NextMaxNoiseLevel; +use next_tfhe::shortint::parameters::MessageModulus as NextMessageModulus; + +impl crate::forward_compatibility::ConvertFrom for NextMessageModulus { + #[inline] + fn convert_from(value: MessageModulus) -> Self { + let MessageModulus(field_0) = value; + Self(field_0) + } +} + +use crate::shortint::parameters::CarryModulus; +use next_tfhe::shortint::parameters::CarryModulus as NextCarryModulus; + +impl crate::forward_compatibility::ConvertFrom for NextCarryModulus { + #[inline] + fn convert_from(value: CarryModulus) -> Self { + let CarryModulus(field_0) = value; + Self(field_0) + } +} + +use crate::shortint::parameters::ClassicPBSParameters; +use next_tfhe::shortint::parameters::ClassicPBSParameters as NextClassicPBSParameters; + +impl crate::forward_compatibility::ConvertFrom for NextClassicPBSParameters { + #[inline] + fn convert_from(value: ClassicPBSParameters) -> Self { + let ClassicPBSParameters { + lwe_dimension, + glwe_dimension, + polynomial_size, + lwe_modular_std_dev, + glwe_modular_std_dev, + pbs_base_log, + pbs_level, + ks_base_log, + ks_level, + message_modulus, + carry_modulus, + ciphertext_modulus, + encryption_key_choice, + } = value; + Self { + lwe_dimension: lwe_dimension.convert_into(), + glwe_dimension: glwe_dimension.convert_into(), + polynomial_size: polynomial_size.convert_into(), + lwe_noise_distribution: DynamicDistribution::new_gaussian_from_std_dev( + lwe_modular_std_dev.convert_into(), + ), + glwe_noise_distribution: DynamicDistribution::new_gaussian_from_std_dev( + glwe_modular_std_dev.convert_into(), + ), + pbs_base_log: pbs_base_log.convert_into(), + pbs_level: pbs_level.convert_into(), + ks_base_log: ks_base_log.convert_into(), + ks_level: ks_level.convert_into(), + message_modulus: message_modulus.convert_into(), + carry_modulus: carry_modulus.convert_into(), + max_noise_level: NextMaxNoiseLevel::from_msg_carry_modulus( + message_modulus.convert_into(), + carry_modulus.convert_into(), + ), + // Unknown value is log2_p_fail = 1.0 + log2_p_fail: 1.0, + ciphertext_modulus: ciphertext_modulus.convert_into(), + encryption_key_choice: encryption_key_choice.convert_into(), + } + } +} + +use crate::shortint::parameters::PBSParameters; +use next_tfhe::shortint::parameters::PBSParameters as NextPBSParameters; + +impl crate::forward_compatibility::ConvertFrom for NextPBSParameters { + #[inline] + fn convert_from(value: PBSParameters) -> Self { + match value { + PBSParameters::PBS(params) => Self::PBS(params.convert_into()), + PBSParameters::MultiBitPBS(params) => Self::MultiBitPBS(params.convert_into()), + } + } +} + +use crate::shortint::parameters::ShortintParameterSet; +use next_tfhe::shortint::parameters::ShortintParameterSet as NextShortintParameterSet; + +impl crate::forward_compatibility::ConvertFrom for NextShortintParameterSet { + #[inline] + fn convert_from(value: ShortintParameterSet) -> Self { + match (value.pbs_parameters(), value.wopbs_parameters()) { + (None, None) => unreachable!(), + (None, Some(params)) => Self::new_wopbs_param_set(params.convert_into()), + (Some(params), None) => Self::new_pbs_param_set(params.convert_into()), + (Some(pbs_params), Some(wopbs_params)) => { + let pbs_params: NextPBSParameters = pbs_params.convert_into(); + Self::try_new_pbs_and_wopbs_param_set((pbs_params, wopbs_params.convert_into())) + .unwrap() + } + } + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_message_modulus() { + use crate::shortint::parameters::MessageModulus; + use next_tfhe::shortint::parameters::MessageModulus as NextMessageModulus; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = MessageModulus(rng.gen()); + + let next_tfhe_struct: NextMessageModulus = tfhe_struct.convert_into(); + + assert_eq!(tfhe_struct.0, next_tfhe_struct.0); + } + + #[test] + fn test_conversion_carry_modulus() { + use crate::shortint::parameters::CarryModulus; + use next_tfhe::shortint::parameters::CarryModulus as NextCarryModulus; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = CarryModulus(rng.gen()); + + let next_tfhe_struct: NextCarryModulus = tfhe_struct.convert_into(); + + assert_eq!(tfhe_struct.0, next_tfhe_struct.0); + } + + #[test] + fn test_conversion_classic_pbs_parameters() { + use next_tfhe::shortint::parameters::ClassicPBSParameters as NextClassicPBSParameters; + + use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS; + + let tfhe_struct = PARAM_MESSAGE_2_CARRY_2_KS_PBS; + let _next_tfhe_struct: NextClassicPBSParameters = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_shortint_parameter_set() { + use crate::shortint::parameters::ShortintParameterSet; + use next_tfhe::shortint::parameters::ShortintParameterSet as NextShortintParameterSet; + + use crate::shortint::parameters::parameters_wopbs_message_carry::WOPBS_PARAM_MESSAGE_2_CARRY_2_KS_PBS; + use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS; + + { + let tfhe_struct = + ShortintParameterSet::new_pbs_param_set(PARAM_MESSAGE_2_CARRY_2_KS_PBS.into()); + let _next_tfhe_struct: NextShortintParameterSet = tfhe_struct.convert_into(); + } + + { + let tfhe_struct = + ShortintParameterSet::new_wopbs_param_set(WOPBS_PARAM_MESSAGE_2_CARRY_2_KS_PBS); + let _next_tfhe_struct: NextShortintParameterSet = tfhe_struct.convert_into(); + } + + { + let tfhe_struct = ShortintParameterSet::try_new_pbs_and_wopbs_param_set(( + PARAM_MESSAGE_2_CARRY_2_KS_PBS, + WOPBS_PARAM_MESSAGE_2_CARRY_2_KS_PBS, + )); + + assert!(tfhe_struct.is_ok()); + let tfhe_struct = tfhe_struct.unwrap(); + let _next_tfhe_struct: NextShortintParameterSet = tfhe_struct.convert_into(); + } + } +} diff --git a/tfhe/src/forward_compatibility/shortint/parameters/multi_bit/mod.rs b/tfhe/src/forward_compatibility/shortint/parameters/multi_bit/mod.rs new file mode 100644 index 0000000000..0b1373c005 --- /dev/null +++ b/tfhe/src/forward_compatibility/shortint/parameters/multi_bit/mod.rs @@ -0,0 +1,108 @@ +use crate::forward_compatibility::ConvertInto; +use crate::shortint::parameters::multi_bit::MultiBitPBSParameters; +use next_tfhe::core_crypto::commons::parameters::DynamicDistribution; +use next_tfhe::shortint::ciphertext::MaxNoiseLevel as NextMaxNoiseLevel; +use next_tfhe::shortint::parameters::multi_bit::MultiBitPBSParameters as NextMultiBitPBSParameters; + +impl crate::forward_compatibility::ConvertFrom + for NextMultiBitPBSParameters +{ + #[inline] + fn convert_from(value: MultiBitPBSParameters) -> Self { + let MultiBitPBSParameters { + lwe_dimension, + glwe_dimension, + polynomial_size, + lwe_modular_std_dev, + glwe_modular_std_dev, + pbs_base_log, + pbs_level, + ks_base_log, + ks_level, + message_modulus, + carry_modulus, + ciphertext_modulus, + encryption_key_choice, + grouping_factor, + deterministic_execution, + } = value; + Self { + lwe_dimension: lwe_dimension.convert_into(), + glwe_dimension: glwe_dimension.convert_into(), + polynomial_size: polynomial_size.convert_into(), + lwe_noise_distribution: DynamicDistribution::new_gaussian_from_std_dev( + lwe_modular_std_dev.convert_into(), + ), + glwe_noise_distribution: DynamicDistribution::new_gaussian_from_std_dev( + glwe_modular_std_dev.convert_into(), + ), + pbs_base_log: pbs_base_log.convert_into(), + pbs_level: pbs_level.convert_into(), + ks_base_log: ks_base_log.convert_into(), + ks_level: ks_level.convert_into(), + message_modulus: message_modulus.convert_into(), + carry_modulus: carry_modulus.convert_into(), + max_noise_level: NextMaxNoiseLevel::from_msg_carry_modulus( + message_modulus.convert_into(), + carry_modulus.convert_into(), + ), + // Unknown value is log2_p_fail = 1.0 + log2_p_fail: 1.0, + ciphertext_modulus: ciphertext_modulus.convert_into(), + encryption_key_choice: encryption_key_choice.convert_into(), + grouping_factor: grouping_factor.convert_into(), + deterministic_execution, + } + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_multi_bit_pbs_parameters() { + use crate::core_crypto::commons::dispersion::StandardDev; + use crate::core_crypto::commons::parameters::*; + use crate::shortint::parameters::multi_bit::MultiBitPBSParameters; + use crate::shortint::{CarryModulus, MessageModulus}; + use next_tfhe::shortint::parameters::multi_bit::MultiBitPBSParameters as NextMultiBitPBSParameters; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + for encryption_key_choice in [EncryptionKeyChoice::Big, EncryptionKeyChoice::Small] { + for deterministic_execution in [true, false] { + let tfhe_struct = MultiBitPBSParameters { + lwe_dimension: LweDimension(rng.gen()), + glwe_dimension: GlweDimension(rng.gen()), + polynomial_size: PolynomialSize(rng.gen()), + lwe_modular_std_dev: StandardDev(rng.gen()), + glwe_modular_std_dev: StandardDev(rng.gen()), + pbs_base_log: DecompositionBaseLog(rng.gen()), + pbs_level: DecompositionLevelCount(rng.gen()), + ks_base_log: DecompositionBaseLog(rng.gen()), + ks_level: DecompositionLevelCount(rng.gen()), + message_modulus: MessageModulus(rng.gen()), + carry_modulus: CarryModulus(rng.gen()), + ciphertext_modulus: CiphertextModulus::new_native(), + encryption_key_choice, + grouping_factor: LweBskGroupingFactor(rng.gen()), + deterministic_execution, + }; + + let next_tfhe_struct: NextMultiBitPBSParameters = tfhe_struct.convert_into(); + + assert_eq!( + next_tfhe_struct.encryption_key_choice, + encryption_key_choice.convert_into() + ); + assert_eq!( + next_tfhe_struct.deterministic_execution, + deterministic_execution + ); + } + } + } +} diff --git a/tfhe/src/forward_compatibility/shortint/parameters/parameters_wopbs/mod.rs b/tfhe/src/forward_compatibility/shortint/parameters/parameters_wopbs/mod.rs new file mode 100644 index 0000000000..3dc85be541 --- /dev/null +++ b/tfhe/src/forward_compatibility/shortint/parameters/parameters_wopbs/mod.rs @@ -0,0 +1,73 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::shortint::parameters::parameters_wopbs::WopbsParameters; +use next_tfhe::core_crypto::commons::parameters::DynamicDistribution; +use next_tfhe::shortint::parameters::parameters_wopbs::WopbsParameters as NextWopbsParameters; + +impl crate::forward_compatibility::ConvertFrom for NextWopbsParameters { + #[inline] + fn convert_from(value: WopbsParameters) -> Self { + let WopbsParameters { + lwe_dimension, + glwe_dimension, + polynomial_size, + lwe_modular_std_dev, + glwe_modular_std_dev, + pbs_base_log, + pbs_level, + ks_level, + ks_base_log, + pfks_level, + pfks_base_log, + pfks_modular_std_dev, + cbs_level, + cbs_base_log, + message_modulus, + carry_modulus, + ciphertext_modulus, + encryption_key_choice, + } = value; + Self { + lwe_dimension: lwe_dimension.convert_into(), + glwe_dimension: glwe_dimension.convert_into(), + polynomial_size: polynomial_size.convert_into(), + lwe_noise_distribution: DynamicDistribution::new_gaussian_from_std_dev( + lwe_modular_std_dev.convert_into(), + ), + glwe_noise_distribution: DynamicDistribution::new_gaussian_from_std_dev( + glwe_modular_std_dev.convert_into(), + ), + pbs_base_log: pbs_base_log.convert_into(), + pbs_level: pbs_level.convert_into(), + ks_level: ks_level.convert_into(), + ks_base_log: ks_base_log.convert_into(), + pfks_level: pfks_level.convert_into(), + pfks_base_log: pfks_base_log.convert_into(), + pfks_noise_distribution: DynamicDistribution::new_gaussian_from_std_dev( + pfks_modular_std_dev.convert_into(), + ), + cbs_level: cbs_level.convert_into(), + cbs_base_log: cbs_base_log.convert_into(), + message_modulus: message_modulus.convert_into(), + carry_modulus: carry_modulus.convert_into(), + ciphertext_modulus: ciphertext_modulus.convert_into(), + encryption_key_choice: encryption_key_choice.convert_into(), + } + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_wopbs_parameters() { + use next_tfhe::shortint::parameters::parameters_wopbs::WopbsParameters as NextWopbsParameters; + + use crate::shortint::parameters::parameters_wopbs_message_carry::WOPBS_PARAM_MESSAGE_2_CARRY_2_KS_PBS; + + let tfhe_struct = WOPBS_PARAM_MESSAGE_2_CARRY_2_KS_PBS; + + let _next_tfhe_struct: NextWopbsParameters = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/shortint/public_key/compact/mod.rs b/tfhe/src/forward_compatibility/shortint/public_key/compact/mod.rs new file mode 100644 index 0000000000..8c1ed4a40c --- /dev/null +++ b/tfhe/src/forward_compatibility/shortint/public_key/compact/mod.rs @@ -0,0 +1,87 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::shortint::public_key::compact::CompactPublicKey; +use next_tfhe::shortint::public_key::compact::CompactPublicKey as NextCompactPublicKey; + +impl crate::forward_compatibility::ConvertFrom for NextCompactPublicKey { + #[inline] + fn convert_from(value: CompactPublicKey) -> Self { + let CompactPublicKey { + key, + parameters, + pbs_order, + } = value; + + Self::from_raw_parts( + key.convert_into(), + parameters.convert_into(), + pbs_order.convert_into(), + ) + } +} + +use crate::shortint::public_key::compact::CompressedCompactPublicKey; +use next_tfhe::shortint::public_key::compact::CompressedCompactPublicKey as NextCompressedCompactPublicKey; + +impl crate::forward_compatibility::ConvertFrom + for NextCompressedCompactPublicKey +{ + #[inline] + fn convert_from(value: CompressedCompactPublicKey) -> Self { + let CompressedCompactPublicKey { + key, + parameters, + pbs_order, + } = value; + + Self::from_raw_parts( + key.convert_into(), + parameters.convert_into(), + pbs_order.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_compact_public_key() { + use crate::shortint::public_key::compact::CompactPublicKey; + use next_tfhe::shortint::public_key::compact::CompactPublicKey as NextCompactPublicKey; + + use crate::shortint::gen_keys; + use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS; + + let (cks, _sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS); + + let tfhe_struct = CompactPublicKey::new(&cks); + let _next_tfhe_struct: NextCompactPublicKey = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_compressed_compact_public_key() { + use crate::shortint::public_key::compact::CompressedCompactPublicKey; + use next_tfhe::shortint::public_key::compact::{ + CompactPublicKey as NextCompactPublicKey, + CompressedCompactPublicKey as NextCompressedCompactPublicKey, + }; + + use crate::shortint::gen_keys; + use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS; + + let (cks, _sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS); + + let tfhe_struct = CompressedCompactPublicKey::new(&cks); + let next_tfhe_struct: NextCompressedCompactPublicKey = tfhe_struct.clone().convert_into(); + + let tfhe_decompressed = tfhe_struct.decompress(); + let next_tfhe_decompressed_converted: NextCompactPublicKey = + tfhe_decompressed.convert_into(); + + let next_tfhe_decompressed = next_tfhe_struct.decompress(); + + assert_eq!(next_tfhe_decompressed_converted, next_tfhe_decompressed); + } +} diff --git a/tfhe/src/forward_compatibility/shortint/public_key/compressed/mod.rs b/tfhe/src/forward_compatibility/shortint/public_key/compressed/mod.rs new file mode 100644 index 0000000000..3d093aae85 --- /dev/null +++ b/tfhe/src/forward_compatibility/shortint/public_key/compressed/mod.rs @@ -0,0 +1,40 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::shortint::public_key::compressed::CompressedPublicKey; +use next_tfhe::shortint::public_key::compressed::CompressedPublicKey as NextCompressedPublicKey; + +impl crate::forward_compatibility::ConvertFrom for NextCompressedPublicKey { + #[inline] + fn convert_from(value: CompressedPublicKey) -> Self { + let CompressedPublicKey { + lwe_public_key, + parameters, + pbs_order, + } = value; + + Self::from_raw_parts( + lwe_public_key.convert_into(), + parameters.convert_into(), + pbs_order.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_compressed_public_key() { + use crate::shortint::public_key::compressed::CompressedPublicKey; + use next_tfhe::shortint::public_key::compressed::CompressedPublicKey as NextCompressedPublicKey; + + use crate::shortint::gen_keys; + use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS; + + let (cks, _sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS); + let tfhe_struct = CompressedPublicKey::new(&cks); + + let _next_tfhe_struct: NextCompressedPublicKey = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/shortint/public_key/mod.rs b/tfhe/src/forward_compatibility/shortint/public_key/mod.rs new file mode 100644 index 0000000000..1618ab7e94 --- /dev/null +++ b/tfhe/src/forward_compatibility/shortint/public_key/mod.rs @@ -0,0 +1,3 @@ +pub mod compact; +pub mod compressed; +pub mod standard; diff --git a/tfhe/src/forward_compatibility/shortint/public_key/standard/mod.rs b/tfhe/src/forward_compatibility/shortint/public_key/standard/mod.rs new file mode 100644 index 0000000000..2fc84b52e6 --- /dev/null +++ b/tfhe/src/forward_compatibility/shortint/public_key/standard/mod.rs @@ -0,0 +1,40 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::shortint::public_key::standard::PublicKey; +use next_tfhe::shortint::public_key::standard::PublicKey as NextPublicKey; + +impl crate::forward_compatibility::ConvertFrom for NextPublicKey { + #[inline] + fn convert_from(value: PublicKey) -> Self { + let PublicKey { + lwe_public_key, + parameters, + pbs_order, + } = value; + + Self::from_raw_parts( + lwe_public_key.convert_into(), + parameters.convert_into(), + pbs_order.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_public_key() { + use crate::shortint::public_key::standard::PublicKey; + use next_tfhe::shortint::public_key::standard::PublicKey as NextPublicKey; + + use crate::shortint::gen_keys; + use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS; + + let (cks, _sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS); + let tfhe_struct = PublicKey::new(&cks); + + let _next_tfhe_struct: NextPublicKey = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/shortint/server_key/compressed/mod.rs b/tfhe/src/forward_compatibility/shortint/server_key/compressed/mod.rs new file mode 100644 index 0000000000..2020bf64c5 --- /dev/null +++ b/tfhe/src/forward_compatibility/shortint/server_key/compressed/mod.rs @@ -0,0 +1,92 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::shortint::server_key::compressed::ShortintCompressedBootstrappingKey; +use next_tfhe::shortint::server_key::compressed::ShortintCompressedBootstrappingKey as NextShortintCompressedBootstrappingKey; + +impl crate::forward_compatibility::ConvertFrom + for NextShortintCompressedBootstrappingKey +{ + #[inline] + fn convert_from(value: ShortintCompressedBootstrappingKey) -> Self { + match value { + ShortintCompressedBootstrappingKey::Classic(seeded_bsk) => { + Self::Classic(seeded_bsk.convert_into()) + } + ShortintCompressedBootstrappingKey::MultiBit { + seeded_bsk, + deterministic_execution, + } => Self::MultiBit { + seeded_bsk: seeded_bsk.convert_into(), + deterministic_execution: deterministic_execution.convert_into(), + }, + } + } +} + +use crate::shortint::server_key::compressed::CompressedServerKey; +use next_tfhe::shortint::ciphertext::MaxNoiseLevel as NextMaxNoiseLevel; +use next_tfhe::shortint::server_key::compressed::CompressedServerKey as NextCompressedServerKey; + +impl crate::forward_compatibility::ConvertFrom for NextCompressedServerKey { + #[inline] + fn convert_from(value: CompressedServerKey) -> Self { + let CompressedServerKey { + key_switching_key, + bootstrapping_key, + message_modulus, + carry_modulus, + max_degree, + ciphertext_modulus, + pbs_order, + } = value; + + Self::from_raw_parts( + key_switching_key.convert_into(), + bootstrapping_key.convert_into(), + message_modulus.convert_into(), + carry_modulus.convert_into(), + max_degree.convert_into(), + NextMaxNoiseLevel::from_msg_carry_modulus( + message_modulus.convert_into(), + carry_modulus.convert_into(), + ), + ciphertext_modulus.convert_into(), + pbs_order.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_shortint_compressed_bootstrapping_key() { + use next_tfhe::shortint::server_key::ShortintCompressedBootstrappingKey as NextCompressedShortintBootstrappingKey; + + use crate::shortint::gen_keys; + use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS; + use crate::shortint::server_key::compressed::CompressedServerKey; + + let (cks, _sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS); + + let compressed_sks = CompressedServerKey::new(&cks); + + let tfhe_struct = compressed_sks.bootstrapping_key; + + let _next_tfhe_struct: NextCompressedShortintBootstrappingKey = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_compressed_server_key() { + use crate::shortint::server_key::compressed::CompressedServerKey; + use next_tfhe::shortint::server_key::compressed::CompressedServerKey as NextCompressedServerKey; + + use crate::shortint::gen_keys; + use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS; + + let (cks, _sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS); + let tfhe_struct = CompressedServerKey::new(&cks); + let _next_tfhe_struct: NextCompressedServerKey = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/shortint/server_key/mod.rs b/tfhe/src/forward_compatibility/shortint/server_key/mod.rs new file mode 100644 index 0000000000..e8597d4252 --- /dev/null +++ b/tfhe/src/forward_compatibility/shortint/server_key/mod.rs @@ -0,0 +1,116 @@ +use crate::forward_compatibility::ConvertInto; + +pub mod compressed; + +use crate::shortint::server_key::ShortintBootstrappingKey; +use next_tfhe::shortint::server_key::ShortintBootstrappingKey as NextShortintBootstrappingKey; + +impl crate::forward_compatibility::ConvertFrom + for NextShortintBootstrappingKey +{ + #[inline] + fn convert_from(value: ShortintBootstrappingKey) -> Self { + let mut converted = match value { + ShortintBootstrappingKey::Classic(fourier_bsk) => { + Self::Classic(fourier_bsk.convert_into()) + } + ShortintBootstrappingKey::MultiBit { + fourier_bsk, + thread_count, + deterministic_execution, + } => Self::MultiBit { + fourier_bsk: fourier_bsk.convert_into(), + thread_count: thread_count.convert_into(), + deterministic_execution: deterministic_execution.convert_into(), + }, + }; + converted.recompute_thread_count(); + converted + } +} + +use crate::shortint::ciphertext::MaxDegree; +use next_tfhe::shortint::ciphertext::MaxDegree as NextMaxDegree; + +impl crate::forward_compatibility::ConvertFrom for NextMaxDegree { + #[inline] + fn convert_from(value: MaxDegree) -> Self { + let field_0 = value.get(); + Self::new(field_0) + } +} + +use crate::shortint::server_key::ServerKey; +use next_tfhe::shortint::server_key::ServerKey as NextServerKey; + +impl crate::forward_compatibility::ConvertFrom for NextServerKey { + #[inline] + fn convert_from(value: ServerKey) -> Self { + let ServerKey { + key_switching_key, + bootstrapping_key, + message_modulus, + carry_modulus, + max_degree, + max_noise_level, + ciphertext_modulus, + pbs_order, + } = value; + + Self::from_raw_parts( + key_switching_key.convert_into(), + bootstrapping_key.convert_into(), + message_modulus.convert_into(), + carry_modulus.convert_into(), + max_degree.convert_into(), + max_noise_level.convert_into(), + ciphertext_modulus.convert_into(), + pbs_order.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_shortint_bootstrapping_key() { + use next_tfhe::shortint::server_key::ShortintBootstrappingKey as NextShortintBootstrappingKey; + + use crate::shortint::gen_keys; + use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS; + + let (_cks, tfhe_struct) = gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS); + let tfhe_struct = tfhe_struct.bootstrapping_key; + + let _next_tfhe_struct: NextShortintBootstrappingKey = tfhe_struct.convert_into(); + } + + #[test] + fn test_conversion_max_degree() { + use crate::shortint::ciphertext::MaxDegree; + use next_tfhe::shortint::ciphertext::MaxDegree as NextMaxDegree; + + use rand::Rng; + + let mut rng = rand::thread_rng(); + + let tfhe_struct = MaxDegree::new(rng.gen()); + let next_tfhe_struct: NextMaxDegree = tfhe_struct.convert_into(); + + assert_eq!(tfhe_struct.get(), next_tfhe_struct.get()); + } + + #[test] + fn test_conversion_server_key() { + use next_tfhe::shortint::server_key::ServerKey as NextServerKey; + + use crate::shortint::gen_keys; + use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS; + + let (_cks, tfhe_struct) = gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS); + + let _next_tfhe_struct: NextServerKey = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/forward_compatibility/shortint/wopbs/mod.rs b/tfhe/src/forward_compatibility/shortint/wopbs/mod.rs new file mode 100644 index 0000000000..5d59a7ebbe --- /dev/null +++ b/tfhe/src/forward_compatibility/shortint/wopbs/mod.rs @@ -0,0 +1,46 @@ +use crate::forward_compatibility::ConvertInto; + +use crate::shortint::wopbs::WopbsKey; +use next_tfhe::shortint::wopbs::WopbsKey as NextWopbsKey; + +impl crate::forward_compatibility::ConvertFrom for NextWopbsKey { + #[inline] + fn convert_from(value: WopbsKey) -> Self { + let WopbsKey { + wopbs_server_key, + pbs_server_key, + cbs_pfpksk, + ksk_pbs_to_wopbs, + param, + } = value; + + Self::from_raw_parts( + wopbs_server_key.convert_into(), + pbs_server_key.convert_into(), + cbs_pfpksk.convert_into(), + ksk_pbs_to_wopbs.convert_into(), + param.convert_into(), + ) + } +} + +#[cfg(test)] +mod test { + use crate::forward_compatibility::ConvertInto; + + #[test] + fn test_conversion_wopbs_key() { + use crate::shortint::wopbs::WopbsKey; + use next_tfhe::shortint::wopbs::WopbsKey as NextWopbsKey; + + use crate::shortint::gen_keys; + use crate::shortint::parameters::parameters_wopbs_message_carry::WOPBS_PARAM_MESSAGE_2_CARRY_2_KS_PBS; + use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS; + + let (cks, sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS); + + let tfhe_struct = + WopbsKey::new_wopbs_key(&cks, &sks, &WOPBS_PARAM_MESSAGE_2_CARRY_2_KS_PBS); + let _next_tfhe_struct: NextWopbsKey = tfhe_struct.convert_into(); + } +} diff --git a/tfhe/src/lib.rs b/tfhe/src/lib.rs index 1a68e5eef1..28a26a072a 100644 --- a/tfhe/src/lib.rs +++ b/tfhe/src/lib.rs @@ -126,3 +126,6 @@ pub mod safe_deserialization; pub mod conformance; pub mod named; + +#[cfg(feature = "forward_compatibility")] +pub mod forward_compatibility;