From d99548b3c943c1c31b95fe8d0b8653c704114a94 Mon Sep 17 00:00:00 2001 From: Kelly McGuire <25494243+klmcguir@users.noreply.github.com> Date: Thu, 5 Jan 2023 02:53:38 -0500 Subject: [PATCH] documentation: Fix typos in tutorial notebooks (#422) * typo fixes * fix typos in tutorial 1 * typo fix tutorial 1 * typo fixes tutorial 3 * typo fix tutorial 4 Co-authored-by: Bogdan Cebere --- tutorials/Tutorial 0 - Getting Started.ipynb | 261 +++---- ...ogistic Regression on Encrypted Data.ipynb | 390 ++++++----- tutorials/Tutorial 3 - Benchmarks.ipynb | 654 +++++++++--------- ...l 4 - Encrypted Convolution on MNIST.ipynb | 152 ++-- 4 files changed, 736 insertions(+), 721 deletions(-) diff --git a/tutorials/Tutorial 0 - Getting Started.ipynb b/tutorials/Tutorial 0 - Getting Started.ipynb index 34871f22..b725ba17 100644 --- a/tutorials/Tutorial 0 - Getting Started.ipynb +++ b/tutorials/Tutorial 0 - Getting Started.ipynb @@ -2,6 +2,7 @@ "cells": [ { "cell_type": "markdown", + "metadata": {}, "source": [ "# Tutorial 0: Getting Started\n", "\n", @@ -14,15 +15,16 @@ "\n", "Authors:\n", "- Ayoub Benaissa - Twitter: [@y0uben11](https://twitter.com/y0uben11)" - ], - "metadata": {} + ] }, { + "attachments": {}, "cell_type": "markdown", + "metadata": {}, "source": [ "## Homomorphic Encryption\n", "\n", - "__Definition__ : Homomorphic encription (HE) is an encryption technique that allows computations to be made on ciphertexts and generates results that when decrypted, correspond to the results of the same computations made on plaintexts.\n", + "__Definition__ : Homomorphic encryption (HE) is an encryption technique that allows computations to be made on ciphertexts and generates results that when decrypted, correspond to the results of the same computations made on plaintexts.\n", "\n", "\"he-black-box\"\n", "\n", @@ -43,73 +45,60 @@ "\n", "```\n", "\n", - "Many details are hidden in this Python script, things like key generation doesn't appear, and that `+` operation over encrypted numbers isn't the usual `+` over integers, but a special evaluation algorithm that can evaluate addition over encrypted numbers. TenSEAL supports addition, substraction and multiplication of encrypted vectors of either integers (using BFV) or real numbers (using CKKS).\n", + "Many details are hidden in this Python script, things like key generation doesn't appear, and that `+` operation over encrypted numbers isn't the usual `+` over integers, but a special evaluation algorithm that can evaluate addition over encrypted numbers. TenSEAL supports addition, subtraction and multiplication of encrypted vectors of either integers (using BFV) or real numbers (using CKKS).\n", "\n", "Next we will look at the most important object of the library, the TenSEALContext." - ], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "## TenSEALContext\n", "\n", "The TenSEALContext is a special object that holds different encryption keys and parameters for you, so that you only need to use a single object to make your encrypted computation instead of managing all the keys and the HE details. Basically, you will want to create a single TenSEALContext before doing your encrypted computation. Let's see how to create one !" - ], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 1, - "source": [ - "import tenseal as ts\n", - "\n", - "context = ts.context(ts.SCHEME_TYPE.BFV, poly_modulus_degree=4096, plain_modulus=1032193)\n", - "context" - ], + "metadata": {}, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "<_tenseal_cpp.TenSEALContext at 0x7fcb980c71f0>" ] }, + "execution_count": 1, "metadata": {}, - "execution_count": 1 + "output_type": "execute_result" } ], - "metadata": {} + "source": [ + "import tenseal as ts\n", + "\n", + "context = ts.context(ts.SCHEME_TYPE.BFV, poly_modulus_degree=4096, plain_modulus=1032193)\n", + "context" + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "That's it ! We need to specify the HE scheme (BFV here) that we want to use, as well as its parameters. Don't worry about the parameters now, you will learn more about them in upcoming tutorials.\n", "\n", "An important thing to note is that the TenSEALContext is now holding the secret key and you can decrypt without the need to provide it, however, you can choose to manage it as a separate object and you will need to pass it to functions that require the secret key. Let's see how this translates into Python!" - ], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 2, - "source": [ - "public_context = ts.context(ts.SCHEME_TYPE.BFV, poly_modulus_degree=4096, plain_modulus=1032193)\n", - "print(\"Is the context private?\", (\"Yes\" if public_context.is_private() else \"No\"))\n", - "print(\"Is the context public?\", (\"Yes\" if public_context.is_public() else \"No\"))\n", - "\n", - "sk = public_context.secret_key()\n", - "\n", - "# the context will drop the secret-key at this point\n", - "public_context.make_context_public()\n", - "print(\"Secret-key dropped\")\n", - "print(\"Is the context private?\", (\"Yes\" if public_context.is_private() else \"No\"))\n", - "print(\"Is the context public?\", (\"Yes\" if public_context.is_public() else \"No\"))" - ], + "metadata": {}, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "Is the context private? Yes\n", "Is the context public? No\n", @@ -119,184 +108,208 @@ ] } ], - "metadata": {} + "source": [ + "public_context = ts.context(ts.SCHEME_TYPE.BFV, poly_modulus_degree=4096, plain_modulus=1032193)\n", + "print(\"Is the context private?\", (\"Yes\" if public_context.is_private() else \"No\"))\n", + "print(\"Is the context public?\", (\"Yes\" if public_context.is_public() else \"No\"))\n", + "\n", + "sk = public_context.secret_key()\n", + "\n", + "# the context will drop the secret-key at this point\n", + "public_context.make_context_public()\n", + "print(\"Secret-key dropped\")\n", + "print(\"Is the context private?\", (\"Yes\" if public_context.is_private() else \"No\"))\n", + "print(\"Is the context public?\", (\"Yes\" if public_context.is_public() else \"No\"))" + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "You can now try to fetch the secret key from the `public_context` and see that it raises an error. We will now continue using our first created TenSEALContext `context` which is still holding the secret key." - ], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "## Encryption and Evaluation" - ], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "The next step after creating our TenSEALContext is to start doing some encrypted computation. First, we create an encrypted vector of integers." - ], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 3, - "source": [ - "plain_vector = [60, 66, 73, 81, 90]\n", - "encrypted_vector = ts.bfv_vector(context, plain_vector)\n", - "print(\"We just encrypted our plaintext vector of size:\", encrypted_vector.size())\n", - "encrypted_vector" - ], + "metadata": {}, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "We just encrypted our plaintext vector of size: 5\n" ] }, { - "output_type": "execute_result", "data": { "text/plain": [ "<_tenseal_cpp.BFVVector at 0x7fcb980bc330>" ] }, + "execution_count": 3, "metadata": {}, - "execution_count": 3 + "output_type": "execute_result" } ], - "metadata": {} + "source": [ + "plain_vector = [60, 66, 73, 81, 90]\n", + "encrypted_vector = ts.bfv_vector(context, plain_vector)\n", + "print(\"We just encrypted our plaintext vector of size:\", encrypted_vector.size())\n", + "encrypted_vector" + ] }, { + "attachments": {}, "cell_type": "markdown", + "metadata": {}, "source": [ - "Here we encrypted a vector of integers into a BFVVector, a vector type that uses the BFV scheme. Now we can do both addition, substraction and multiplication in an element-wise fashion with other encrypted or plain vectors." - ], - "metadata": {} + "Here we encrypted a vector of integers into a BFVVector, a vector type that uses the BFV scheme. Now we can do both addition, subtraction and multiplication in an element-wise fashion with other encrypted or plain vectors." + ] }, { "cell_type": "code", "execution_count": 4, - "source": [ - "add_result = encrypted_vector + [1, 2, 3, 4, 5]\n", - "print(add_result.decrypt())" - ], + "metadata": {}, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "[61, 68, 76, 85, 95]\n" ] } ], - "metadata": {} + "source": [ + "add_result = encrypted_vector + [1, 2, 3, 4, 5]\n", + "print(add_result.decrypt())" + ] }, { "cell_type": "code", "execution_count": 5, - "source": [ - "sub_result = encrypted_vector - [1, 2, 3, 4, 5]\n", - "print(sub_result.decrypt())" - ], + "metadata": {}, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "[59, 64, 70, 77, 85]\n" ] } ], - "metadata": {} + "source": [ + "sub_result = encrypted_vector - [1, 2, 3, 4, 5]\n", + "print(sub_result.decrypt())" + ] }, { "cell_type": "code", "execution_count": 6, - "source": [ - "mul_result = encrypted_vector * [1, 2, 3, 4, 5]\n", - "print(mul_result.decrypt())" - ], + "metadata": {}, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "[60, 132, 219, 324, 450]\n" ] } ], - "metadata": {} + "source": [ + "mul_result = encrypted_vector * [1, 2, 3, 4, 5]\n", + "print(mul_result.decrypt())" + ] }, { "cell_type": "code", "execution_count": 7, - "source": [ - "encrypted_add = add_result + sub_result\n", - "print(encrypted_add.decrypt())" - ], + "metadata": {}, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "[120, 132, 146, 162, 180]\n" ] } ], - "metadata": {} + "source": [ + "encrypted_add = add_result + sub_result\n", + "print(encrypted_add.decrypt())" + ] }, { "cell_type": "code", "execution_count": 8, - "source": [ - "encrypted_sub = encrypted_add - encrypted_vector\n", - "print(encrypted_sub.decrypt())" - ], + "metadata": {}, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "[60, 66, 73, 81, 90]\n" ] } ], - "metadata": {} + "source": [ + "encrypted_sub = encrypted_add - encrypted_vector\n", + "print(encrypted_sub.decrypt())" + ] }, { "cell_type": "code", "execution_count": 9, - "source": [ - "encrypted_mul = encrypted_add * encrypted_sub\n", - "print(encrypted_mul.decrypt())" - ], + "metadata": {}, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "[7200, 8712, 10658, 13122, 16200]\n" ] } ], - "metadata": {} + "source": [ + "encrypted_mul = encrypted_add * encrypted_sub\n", + "print(encrypted_mul.decrypt())" + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "We just made both ciphertext to plaintext (c2p) and ciphertext to ciphertext (c2c) evaluations (add, sub and mul). An important thing to note is that you should never encrypt your plaintext values to evaluate them with ciphertexts if they don't need to be kept private. That's because c2p evaluations are more efficient than c2c. Look at the below script to see how much faster a c2p multiplication is compared to a c2c one." - ], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "c2c multiply time: 18.739938735961914 ms\n", + "c2p multiply time: 1.5423297882080078 ms\n" + ] + } + ], "source": [ "from time import time\n", "\n", @@ -309,40 +322,26 @@ "_ = encrypted_add * [1, 2, 3, 4, 5]\n", "t_end = time()\n", "print(\"c2p multiply time: {} ms\".format((t_end - t_start) * 1000))" - ], - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "c2c multiply time: 18.739938735961914 ms\n", - "c2p multiply time: 1.5423297882080078 ms\n" - ] - } - ], - "metadata": {} + ] }, { + "attachments": {}, "cell_type": "markdown", + "metadata": {}, "source": [ "## More about TenSEALContext\n", "\n", - "TenSEALContext is holding more attributes than what we have seen so far, so it's worth mentioning some other interesting ones. The coolest attributes (at least to me) are the ones for setting automatic relinearization, rescaling (for CKKS only) and modulus switching. These features are enabled by defaut as you can see below:" - ], - "metadata": {} + "TenSEALContext is holding more attributes than what we have seen so far, so it's worth mentioning some other interesting ones. The coolest attributes (at least to me) are the ones for setting automatic relinearization, rescaling (for CKKS only) and modulus switching. These features are enabled by default as you can see below:" + ] }, { "cell_type": "code", "execution_count": 11, - "source": [ - "print(\"Automatic relinearization is:\", (\"on\" if context.auto_relin else \"off\"))\n", - "print(\"Automatic rescaling is:\", (\"on\" if context.auto_rescale else \"off\"))\n", - "print(\"Automatic modulus switching is:\", (\"on\" if context.auto_mod_switch else \"off\"))" - ], + "metadata": {}, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "Automatic relinearization is: on\n", "Automatic rescaling is: on\n", @@ -350,20 +349,35 @@ ] } ], - "metadata": {} + "source": [ + "print(\"Automatic relinearization is:\", (\"on\" if context.auto_relin else \"off\"))\n", + "print(\"Automatic rescaling is:\", (\"on\" if context.auto_rescale else \"off\"))\n", + "print(\"Automatic modulus switching is:\", (\"on\" if context.auto_mod_switch else \"off\"))" + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "Experienced users can choose to disable one or more of these features and manage for themselves when and how to do these operations.\n", "\n", "TenSEALContext can also hold a `global_scale` (only used when using CKKS), which is used as a default scale value when the user doesn't provide one. As most often users will define a single value to be used as scale during the entire HE computation, defining it globally can be more straight forward compared to passing it to every function call." - ], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The global_scale isn't defined yet\n", + "global_scale: 1048576.0\n" + ] + } + ], "source": [ "# this should throw an error as the global_scale isn't defined yet\n", "try:\n", @@ -374,21 +388,11 @@ "# you can define it to 2 ** 20 for instance\n", "context.global_scale = 2 ** 20\n", "print(\"global_scale:\", context.global_scale)" - ], - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "The global_scale isn't defined yet\n", - "global_scale: 1048576.0\n" - ] - } - ], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "# Congratulations!!! - Time to Join the Community!\n", "\n", @@ -409,8 +413,7 @@ "If you don't have time to contribute to our codebase, but would still like to lend support, you can also become a Backer on our Open Collective. All donations go towards our web hosting and other community expenses such as hackathons and meetups!\n", "\n", "[OpenMined's Open Collective Page](https://opencollective.com/openmined)\n" - ], - "metadata": {} + ] } ], "metadata": { diff --git a/tutorials/Tutorial 1 - Training and Evaluation of Logistic Regression on Encrypted Data.ipynb b/tutorials/Tutorial 1 - Training and Evaluation of Logistic Regression on Encrypted Data.ipynb index 2546dd7e..2743a20a 100644 --- a/tutorials/Tutorial 1 - Training and Evaluation of Logistic Regression on Encrypted Data.ipynb +++ b/tutorials/Tutorial 1 - Training and Evaluation of Logistic Regression on Encrypted Data.ipynb @@ -2,6 +2,7 @@ "cells": [ { "cell_type": "markdown", + "metadata": {}, "source": [ "# Tutorial 1: Training and Evaluation of Logistic Regression on Encrypted Data\n", "\n", @@ -13,21 +14,22 @@ "\n", "Authors:\n", "- Ayoub Benaissa - Twitter: [@y0uben11](https://twitter.com/y0uben11)" - ], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "## Setup\n", "\n", "All modules are imported here. Make sure everything is installed by running the cell below:" - ], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 1, + "metadata": {}, + "outputs": [], "source": [ "import torch\n", "import tenseal as ts\n", @@ -38,22 +40,35 @@ "# those are optional and are not necessary for training\n", "import numpy as np\n", "import matplotlib.pyplot as plt" - ], - "outputs": [], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "We now prepare the training and test data. The dataset was downloaded from Kaggle [here](https://www.kaggle.com/dileep070/heart-disease-prediction-using-logistic-regression). This dataset includes patients' information along with a 10-year risk of future coronary heart disease (CHD) as a label. The goal is to build a model that can predict this 10-year CHD risk based on patients' information. You can read more about the dataset in the link provided. \n", "\n", "Alternatively, we also provide the `random_data()` function below that generates random, linearly separable points. You can use it instead of the dataset from Kaggle, for those who just want to see how things work. The rest of the tutorial should work in the same way." - ], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "############# Data summary #############\n", + "x_train has shape: torch.Size([780, 9])\n", + "y_train has shape: torch.Size([780, 1])\n", + "x_test has shape: torch.Size([334, 9])\n", + "y_test has shape: torch.Size([334, 1])\n", + "#######################################\n" + ] + } + ], "source": [ "torch.random.manual_seed(73)\n", "random.seed(73)\n", @@ -105,35 +120,22 @@ "print(f\"x_test has shape: {x_test.shape}\")\n", "print(f\"y_test has shape: {y_test.shape}\")\n", "print(\"#######################################\")" - ], - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "############# Data summary #############\n", - "x_train has shape: torch.Size([780, 9])\n", - "y_train has shape: torch.Size([780, 1])\n", - "x_test has shape: torch.Size([334, 9])\n", - "y_test has shape: torch.Size([334, 1])\n", - "#######################################\n" - ] - } - ], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "## Training a Logistic Regression Model\n", "\n", "We will start by training a logistic regression model (without any encryption), which can be viewed as a single layer neural network with a single node. We will be using this model as a means of comparison against encrypted training and evaluation." - ], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 3, + "metadata": {}, + "outputs": [], "source": [ "class LR(torch.nn.Module):\n", "\n", @@ -144,13 +146,13 @@ " def forward(self, x):\n", " out = torch.sigmoid(self.lr(x))\n", " return out" - ], - "outputs": [], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 4, + "metadata": {}, + "outputs": [], "source": [ "n_features = x_train.shape[1]\n", "model = LR(n_features)\n", @@ -158,13 +160,25 @@ "optim = torch.optim.SGD(model.parameters(), lr=1)\n", "# use Binary Cross Entropy Loss\n", "criterion = torch.nn.BCELoss()" - ], - "outputs": [], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Loss at epoch 1: 0.8504332900047302\n", + "Loss at epoch 2: 0.6863385438919067\n", + "Loss at epoch 3: 0.635811448097229\n", + "Loss at epoch 4: 0.6193529367446899\n", + "Loss at epoch 5: 0.6124349236488342\n" + ] + } + ], "source": [ "# define the number of epochs for both plain and encrypted training\n", "EPOCHS = 5\n", @@ -180,25 +194,21 @@ " return model\n", "\n", "model = train(model, optim, criterion, x_train, y_train)" - ], + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ - "Loss at epoch 1: 0.8504332900047302\n", - "Loss at epoch 2: 0.6863385438919067\n", - "Loss at epoch 3: 0.635811448097229\n", - "Loss at epoch 4: 0.6193529367446899\n", - "Loss at epoch 5: 0.6124349236488342\n" + "Accuracy on plain test_set: 0.703592836856842\n" ] } ], - "metadata": {} - }, - { - "cell_type": "code", - "execution_count": 6, "source": [ "def accuracy(model, x, y):\n", " out = model(x)\n", @@ -207,37 +217,29 @@ "\n", "plain_accuracy = accuracy(model, x_test, y_test)\n", "print(f\"Accuracy on plain test_set: {plain_accuracy}\")" - ], - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "Accuracy on plain test_set: 0.703592836856842\n" - ] - } - ], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "It is worth to remember that a high accuracy isn't our goal. We just want to see that training on encrypted data doesn't affect the final result, so we will be comparing accuracies over encrypted data against the `plain_accuracy` we got here." - ], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "## Encrypted Evaluation\n", "\n", "In this part, we will just focus on evaluating the logistic regression model with plain parameters (optionally encrypted parameters) on the encrypted test set. We first create a PyTorch-like LR model that can evaluate encrypted data:" - ], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 7, + "metadata": {}, + "outputs": [], "source": [ "class EncryptedLR:\n", " \n", @@ -272,20 +274,20 @@ " \n", "\n", "eelr = EncryptedLR(model)" - ], - "outputs": [], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "We now create a TenSEALContext for specifying the scheme and the parameters we are going to use. Here we choose small and secure parameters that allow us to make a single multiplication. That's enough for evaluating a logistic regression model, however, we will see that we need larger parameters when doing training on encrypted data." - ], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 8, + "metadata": {}, + "outputs": [], "source": [ "# parameters\n", "poly_mod_degree = 4096\n", @@ -296,57 +298,67 @@ "ctx_eval.global_scale = 2 ** 20\n", "# this key is needed for doing dot-product operations\n", "ctx_eval.generate_galois_keys()" - ], - "outputs": [], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "We will encrypt the whole test set before the evaluation:" - ], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 9, - "source": [ - "t_start = time()\n", - "enc_x_test = [ts.ckks_vector(ctx_eval, x.tolist()) for x in x_test]\n", - "t_end = time()\n", - "print(f\"Encryption of the test-set took {int(t_end - t_start)} seconds\")" - ], + "metadata": {}, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "Encryption of the test-set took 1 seconds\n" ] } ], - "metadata": {} + "source": [ + "t_start = time()\n", + "enc_x_test = [ts.ckks_vector(ctx_eval, x.tolist()) for x in x_test]\n", + "t_end = time()\n", + "print(f\"Encryption of the test-set took {int(t_end - t_start)} seconds\")" + ] }, { "cell_type": "code", "execution_count": 10, + "metadata": {}, + "outputs": [], "source": [ "# (optional) encrypt the model's parameters\n", "# eelr.encrypt(ctx_eval)" - ], - "outputs": [], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "As you may have already noticed when we built the EncryptedLR class, we don't compute the sigmoid function on the encrypted output of the linear layer, simply because it's not needed, and computing sigmoid over encrypted data will increase the computation time and require larger encryption parameters. However, we will use sigmoid for the encrypted training part. We now proceed with the evaluation of the encrypted test set and compare the accuracy to the one on the plain test set." - ], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Evaluated test_set of 334 entries in 1 seconds\n", + "Accuracy: 225/334 = 0.6736526946107785\n", + "Difference between plain and encrypted accuracies: 0.029940128326416016\n" + ] + } + ], "source": [ "def encrypted_evaluation(model, enc_x_test, y_test):\n", " t_start = time()\n", @@ -355,7 +367,7 @@ " for enc_x, y in zip(enc_x_test, y_test):\n", " # encrypted evaluation\n", " enc_out = model(enc_x)\n", - " # plain comparaison\n", + " # plain comparison\n", " out = enc_out.decrypt()\n", " out = torch.tensor(out)\n", " out = torch.sigmoid(out)\n", @@ -373,29 +385,19 @@ "print(f\"Difference between plain and encrypted accuracies: {diff_accuracy}\")\n", "if diff_accuracy < 0:\n", " print(\"Oh! We got a better accuracy on the encrypted test-set! The noise was on our side...\")" - ], - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "Evaluated test_set of 334 entries in 1 seconds\n", - "Accuracy: 225/334 = 0.6736526946107785\n", - "Difference between plain and encrypted accuracies: 0.029940128326416016\n" - ] - } - ], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "We saw that evaluating on the encrypted test set doesn't affect the accuracy that much. I've even seen examples where the encrypted evaluation performs better." - ], - "metadata": {} + ] }, { + "attachments": {}, "cell_type": "markdown", + "metadata": {}, "source": [ "## Training an Encrypted Logistic Regression Model on Encrypted Data\n", "\n", @@ -425,13 +427,14 @@ "\n", "#### Homomorphic Encryption Parameters\n", "\n", - "From the input data to the parameter update, a ciphertext will need a multiplicative depth of 6, 1 for the dot product operation, 2 for the sigmoid approximation, and 3 for the backprobagation phase (one is actually hidden in the `self._delta_w += enc_x * out_minus_y` operation in the `backward()` function, which is multiplying a 1-sized vector with an n-sized one, which requires masking the first slot and replicating it n times in the first vector). With a scale of around 20 bits, we need 6 coefficients modulus with the same bit-size as the scale, plus the last coeffcient, which needs more bits, we are already out of the 4096 polynomial modulus degree (which requires < 109 total bit count of the coefficients modulus, if we consider 128-bit security), so we will use 8192. This will allow us to batch up to 4096 values in a single ciphertext, but we are far away from this limitation, so we shouldn't even think about it.\n" - ], - "metadata": {} + "From the input data to the parameter update, a ciphertext will need a multiplicative depth of 6, 1 for the dot product operation, 2 for the sigmoid approximation, and 3 for the backpropagation phase (one is actually hidden in the `self._delta_w += enc_x * out_minus_y` operation in the `backward()` function, which is multiplying a 1-sized vector with an n-sized one, which requires masking the first slot and replicating it n times in the first vector). With a scale of around 20 bits, we need 6 coefficients modulus with the same bit-size as the scale, plus the last coefficient, which needs more bits, we are already out of the 4096 polynomial modulus degree (which requires < 109 total bit count of the coefficients modulus, if we consider 128-bit security), so we will use 8192. This will allow us to batch up to 4096 values in a single ciphertext, but we are far away from this limitation, so we shouldn't even think about it.\n" + ] }, { "cell_type": "code", "execution_count": 12, + "metadata": {}, + "outputs": [], "source": [ "class EncryptedLR:\n", " \n", @@ -494,13 +497,13 @@ " \n", " def __call__(self, *args, **kwargs):\n", " return self.forward(*args, **kwargs)\n" - ], - "outputs": [], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 13, + "metadata": {}, + "outputs": [], "source": [ "# parameters\n", "poly_mod_degree = 8192\n", @@ -509,45 +512,84 @@ "ctx_training = ts.context(ts.SCHEME_TYPE.CKKS, poly_mod_degree, -1, coeff_mod_bit_sizes)\n", "ctx_training.global_scale = 2 ** 21\n", "ctx_training.generate_galois_keys()" - ], - "outputs": [], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 14, - "source": [ - "t_start = time()\n", - "enc_x_train = [ts.ckks_vector(ctx_training, x.tolist()) for x in x_train]\n", - "enc_y_train = [ts.ckks_vector(ctx_training, y.tolist()) for y in y_train]\n", - "t_end = time()\n", - "print(f\"Encryption of the training_set took {int(t_end - t_start)} seconds\")" - ], + "metadata": {}, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "Encryption of the training_set took 26 seconds\n" ] } ], - "metadata": {} + "source": [ + "t_start = time()\n", + "enc_x_train = [ts.ckks_vector(ctx_training, x.tolist()) for x in x_train]\n", + "enc_y_train = [ts.ckks_vector(ctx_training, y.tolist()) for y in y_train]\n", + "t_end = time()\n", + "print(f\"Encryption of the training_set took {int(t_end - t_start)} seconds\")" + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "Below we study the distribution of `x.dot(weight) + bias` in both plain and encrypted domains. Making sure that it falls into the range $[-5,5]$, which is where our sigmoid approximation is good at, and we don't want to feed it data that is out of this range so that we don't get erroneous output, which can make our training behave unpredictably. But the weights will change during the training process, and we should try to keep them as small as possible while still learning. A technique often used with logistic regression, and we do exactly this (but serving another purpose which is *generalization*), is known as *regularization*, and you might already have spotted the additional term `self.weight * 0.05` in the `update_parameters()` function, which is the result of doing regularization.\n", "\n", "To recap, since our sigmoid approximation is only good in the range $[-5,5]$, we want to have all its inputs in that range. In order to do this, we need to keep our logistic regression parameters as small as possible, so we apply regularization.\n", "\n", "**Note:** Keeping the parameters small certainly reduces the magnitude of the output, but we can also get out of range if the data wasn't standardized. You may have spotted that we standardized the data with a mean of 0 and std of 1, this was both for better performance, as well as to keep the inputs to the sigmoid in the desired range." - ], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Distribution on pl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U9tiZ2ReAb5fZVbEbe4c4Zh8Efgf4LQ8GEMu8RuTHrERsb35uZnXkw/wr7v710v3FAe/uh83s82bW4e4VX4QqxNelmjeMvwv4hbtfLN1RzWNGuCyK5LglccjlEHCPmdWb2S7yv2X/cY52HwwefxCYq8e/XO8Annf37nI7zazJzFoKj8mfFHymXNuolIxZvneO9wtz8+9K1LYP+BjwbncfnaPNShyzWN78PDgf8z+A59z9v87RZlPQDjPbS/7n+HIl6wreK8zXpZo3jJ/zL+VqHbNAmCyK5uex0md9l/pBPoi6gQngIvBY0b5PkD8jfAK4q2j7FwmuiAHWA/8AvBj8216hOr8E3F+y7TrgcPD4evJnrJ8EjpMfdqj0sfsb4GngqeCbYrTYGwwAAACgSURBVHNpXcHzu8lfRfGrlagreM+T5McKjwUfB6t1zMr9/4H7C19P8n8GPxTsf5qiq60qeHzeSv5P7aeKjtHdJXU9EByXJ8mfWH7LCn3tyn5dqn3MgvdtJB/Q64q2rfgxI/8L5TwwGeTXh+fKokr8PGrqv4hISiRxyEVERMpQoIuIpIQCXUQkJRToIiIpoUAXEUkJBbqISEoo0EVEUuL/A3Ar/hj8WM8fAAAAAElFTkSuQmCC", + "text/plain": [ + "
" + ] + }, + "metadata": { + "needs_background": "light" + }, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Distribution on encrypted data:\n" + ] + }, + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD4CAYAAAD8Zh1EAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+j8jraAAAgAElEQVR4nO3deXCc933f8fd3F8DiJkhcFG9KgiRTtmRLMG3HluP4qCUlY/qIGykZ243jqmqrup5OJlbGrSdTt5NR3LSZNHIYxdUkTTNVPPXFOrTko3GUjC2blKyLlChR1EGIJE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qrbbodrn7q+Ap4Ancz8UlxXXlbt/K9lRFC/Uoq7cax4n21f4eO5rf1DnrNT/H7gz//0k+2fwvbntT1Ew2qqK5+ddZP/UfrLgHN1aVNddufPyBNkPln+hRt+7kt+XoM9Z7nWbyQb0hoLHan7OyL6hnAbmc/n1W8tlUTV+HzX1X0QkJqLY5SIiIiUo0EVEYkKBLiISEwp0EZGYUKCLiMSEAl1EJCYU6CIiMfH/Af+E0IWtdQ3qAAAAAElFTkSuQmCC", + "text/plain": [ + "
" + ] + }, + "metadata": { + "needs_background": "light" + }, + "output_type": "display_data" + } + ], "source": [ "normal_dist = lambda x, mean, var: np.exp(- np.square(x - mean) / (2 * var)) / np.sqrt(2 * np.pi * var)\n", "\n", @@ -581,66 +623,44 @@ "eelr = EncryptedLR(lr)\n", "eelr.encrypt(ctx_training)\n", "encrypted_out_distribution(eelr, enc_x_train)" - ], - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "Distribution on plain data:\n" - ] - }, - { - "output_type": "display_data", - "data": { - "text/plain": [ - "
" - ], - "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD4CAYAAAD8Zh1EAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+j8jraAAAgAElEQVR4nO3daXAc553f8e8fAwxAHCQAAjzEWzJlmbKtC0t7vdbaqbVjSrtr2nt4JW/WTmyXSkmUxC82ZbpccZxV5YVjbzaVtbxc2VHZ2XJZScUX10uv5GzF97GkbOqgJEo0RYngCRAXcQ6Of15MDzgaDoAG0IPpbvw+VSjOdD+Y+bMB/PDg6X6eNndHRESSr6baBYiISDQU6CIiKaFAFxFJCQW6iEhKKNBFRFKitlpv3NHR4Tt37qzW24uIJNITTzzR6+6d5fZVLdB37tzJ0aNHq/X2IiKJZGYvz7VPQy4iIimhQB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I/8L5TwwGeTXh+fKokr8PGrqv4hISiRxyEVERMpQoIuIpIQCXUQkJRToIiIpoUAXEUkJBbqISEoo0EVEUuL/A3Ar/hj8WM8fAAAAAElFTkSuQmCC" - }, - "metadata": { - "needs_background": "light" - } - }, - { - "output_type": "stream", - "name": "stdout", - "text": [ - "Distribution on encrypted data:\n" - ] - }, - { - "output_type": "display_data", - "data": { - "text/plain": [ - "
" - ], - "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD4CAYAAAD8Zh1EAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+j8jraAAAgAElEQVR4nO3deXCc933f8fd3F8DiJkhcFG9KgiRTtmRLMG3HluP4qCUlY/qIGykZ243jqmqrup5OJlbGrSdTt5NR3LSZNHIYxdUkTTNVPPXFOrTko3GUjC2blKyLlChR1EGIJE6SuBdY4Ns/dhdaLRfEAtzd58DnNYPh7j7PPvvlA+CzP/z293t+5u6IiEj0JYIuQEREKkOBLiISEwp0EZGYUKCLiMSEAl1EJCbqgnrhrq4u37VrV1AvLyISSY8++uiIu3eX2hZYoO/atYvDhw8H9fIiIpFkZi8vt01dLiIiMaFAFxGJCQW6iEhMKNBFRGJCgS4iEhMKdBGRmFCgi4jEhAJd1p0Hnz7N1x8dQJeOlrgJbGKRSBC+9fNX+dzfPA7AyGSaf/GLVwRckUjllNVCN7ObzeyYmR03s7uX2ec9Zva4mR0xs7+vbJkil25x0fmjHzzHtVvaefdV3dz7d8eZnV8IuiyRilkx0M0sCdwL3ALsAW43sz1F+3QAXwE+5O7XAh+vQq0il+TnJ8/x0ug0n7lpN3fcdDnjsxl+dGw46LJEKqacFvpe4Li7n3D3OeABYF/RPr8OfMPdXwFw96HKlily6X7wzCB1CeO91/Ty9ss3sbG5nu8dPRN0WSIVU06gbwVOFtwfyD1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LPrch5LOecmdkXgAzw18scpuLnrLjMEo+tafHzajCzVuDrwOfcfbxo82NkuxQmc5+PfAvoq0VdrPx9CfKcNQAfAn63xOYgz1k5KnLeAg10d3//Gp5W7oLUg2Z2mbufzv3JN1SNGs2sDvgocONFjnEq9++QmX2T7J9XlxRO5Z47M/tz4DslNlVtYe8yztmngF8B3ue5DsQSx6j4OSsS2sXPzayebJj/tbt/o3h7YcC7+0Ez+4qZdbl71S9CVcb3JcgF428BHnP3weINQZ4zysuiipy3KHa5HABuM7OUme0m+y77s2X2+1Tu9qeA5Vr8l+r9wLPuPlBqo5m1mFlb/jbZDwWfLrVvpRT1WX5kmdcrZ/HvatR2M/B54EPuPr3MPrU4Z6Fc/Dz3ecz/AJ5x9/+6zD6bc/thZnvJ/h6PVrOu3GuV830JcsH4Zf9SDuqc5ZSTRZX5faz2p75r/SIbRANAGhgEHirY9gWynwgfA24pePyr5EbEAJ3AD4Hnc/9uqlKdf4TQndsAAADBSURBVAHcWfTYFuBg7vblZD+xfgI4Qrbbodrn7q+Ap4Ancz8UlxXXlbt/K9lRFC/Uoq7cax4n21f4eO5rf1DnrNT/H7gz//0k+2fwvbntT1Ew2qqK5+ddZP/UfrLgHN1aVNddufPyBNkPln+hRt+7kt+XoM9Z7nWbyQb0hoLHan7OyL6hnAbmc/n1W8tlUTV+HzX1X0QkJqLY5SIiIiUo0EVEYkKBLiISEwp0EZGYUKCLiMSEAl1EJCYU6CIiMfH/Af+E0IWtdQ3qAAAAAElFTkSuQmCC" - }, - "metadata": { - "needs_background": "light" - } - } - ], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "Most of the data falls into $[-5,5]$, the sigmoid approximation should be good enough!" - ], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "We finally reached the last part, which is about training an encrypted logistic regression model on encrypted data! You can see that we decrypt the weights and re-encrypt them again after every epoch, this is necessary since after updating the weights at the end of the epoch, we can no longer use them to perform enough multiplications, so we need to get them back to the initial ciphertext level. In a real scenario, this would translate to sending the weights back to the secret-key holder for decryption and re-encryption. In that case, it will result in just a few Kilobytes of communication per epoch." - ], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Accuracy at epoch #0 is 0.523952066898346\n", + "Accuracy at epoch #1 is 0.6796407103538513\n", + "Accuracy at epoch #2 is 0.6796407103538513\n", + "Accuracy at epoch #3 is 0.7005987763404846\n", + "Accuracy at epoch #4 is 0.6916167736053467\n", + "Accuracy at epoch #5 is 0.703592836856842\n", + "\n", + "Average time per epoch: 160 seconds\n", + "Final accuracy is 0.703592836856842\n", + "Difference between plain and encrypted accuracies: 0.0\n" + ] + } + ], "source": [ "eelr = EncryptedLR(LR(n_features))\n", "accuracy = eelr.plain_accuracy(x_test, y_test)\n", @@ -651,7 +671,7 @@ " eelr.encrypt(ctx_training)\n", " \n", " # if you want to keep an eye on the distribution to make sure\n", - " # the function approxiamation is still working fine\n", + " # the function approximation is still working fine\n", " # WARNING: this operation is time consuming\n", " # encrypted_out_distribution(eelr, enc_x_train)\n", " \n", @@ -675,36 +695,18 @@ "print(f\"Difference between plain and encrypted accuracies: {diff_accuracy}\")\n", "if diff_accuracy < 0:\n", " print(\"Oh! We got a better accuracy when training on encrypted data! The noise was on our side...\")" - ], - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "Accuracy at epoch #0 is 0.523952066898346\n", - "Accuracy at epoch #1 is 0.6796407103538513\n", - "Accuracy at epoch #2 is 0.6796407103538513\n", - "Accuracy at epoch #3 is 0.7005987763404846\n", - "Accuracy at epoch #4 is 0.6916167736053467\n", - "Accuracy at epoch #5 is 0.703592836856842\n", - "\n", - "Average time per epoch: 160 seconds\n", - "Final accuracy is 0.703592836856842\n", - "Difference between plain and encrypted accuracies: 0.0\n" - ] - } - ], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "Even after running this cell many times myself, I always feel the joy when I see it working on encrypted data, so I hope you are feeling this joy as well!" - ], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "# Congratulations!!! - Time to Join the Community!\n", "\n", @@ -732,8 +734,7 @@ "If you don't have time to contribute to our codebase, but would still like to lend support, you can also become a Backer on our Open Collective. All donations go toward our web hosting and other community expenses such as hackathons and meetups!\n", "\n", "[OpenMined's Open Collective Page](https://opencollective.com/openmined)\n" - ], - "metadata": {} + ] } ], "metadata": { @@ -752,9 +753,14 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.0" + "version": "3.9.6 (default, Sep 26 2022, 11:37:49) \n[Clang 14.0.0 (clang-1400.0.29.202)]" + }, + "vscode": { + "interpreter": { + "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" + } } }, "nbformat": 4, "nbformat_minor": 4 -} \ No newline at end of file +} diff --git a/tutorials/Tutorial 3 - Benchmarks.ipynb b/tutorials/Tutorial 3 - Benchmarks.ipynb index aebf82b3..c3bec394 100644 --- a/tutorials/Tutorial 3 - Benchmarks.ipynb +++ b/tutorials/Tutorial 3 - Benchmarks.ipynb @@ -2,6 +2,7 @@ "cells": [ { "cell_type": "markdown", + "metadata": {}, "source": [ "# Tutorial 3 - Benchmarks\n", "\n", @@ -13,11 +14,11 @@ "\n", "Authors:\n", "- Bogdan Cebere - Twitter: [@bcebere](https://twitter.com/bcebere)" - ], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "## Introduction\n", "\n", @@ -25,11 +26,11 @@ "\n", "TenSEAL is a library for doing homomorphic encryption operations on tensors. It's built on top of [Microsoft SEAL](https://github.com/Microsoft/SEAL), a C++ library implementing the BFV and CKKS homomorphic encryption schemes.\n", "\n" - ], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "## Homomorphic Encryption Parameters Review\n", "\n", @@ -63,19 +64,28 @@ " - The security level (bigger is worse).\n", " \n", "In TenSEAL, as in Microsoft SEAL, each of the prime numbers in the coefficient modulus must be at most 60 bits and must be congruent to 1 modulo 2*poly_modulus_degree." - ], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "## Setup " - ], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: tabulate in /home/bcebere/anaconda3/envs/tenseal/lib/python3.8/site-packages (0.8.7)\r\n" + ] + } + ], "source": [ "!pip install tabulate\n", "\n", @@ -110,137 +120,30 @@ "\n", "def decrypt(enc):\n", " return enc.decrypt()" - ], - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "Requirement already satisfied: tabulate in /home/bcebere/anaconda3/envs/tenseal/lib/python3.8/site-packages (0.8.7)\r\n" - ] - } - ], - "metadata": {} + ] }, { + "attachments": {}, "cell_type": "markdown", + "metadata": {}, "source": [ "## Context serialization\n", "\n", "\n", - "The TenSEAL context is required for defining the perfomance and security of your application.\n", + "The TenSEAL context is required for defining the performance and security of your application.\n", "\n", "In a client-server setup, it is required only on the initial handshake, as the ciphertexts can be linked with an existing context on deserialization.\n", "\n", "Here we are benchmarking the size of the context, depending on the input parameters." - ], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 2, - "source": [ - "ctx_size_benchmarks = [[\"Encryption Type\", \"Scheme Type\", \"Polynomial modulus\", \"Coefficient modulus sizes\", \"Saved keys\", \"Context serialized size\", ]]\n", - "\n", - "for enc_type in [ts.ENCRYPTION_TYPE.SYMMETRIC, ts.ENCRYPTION_TYPE.ASYMMETRIC]:\n", - " for (poly_mod, coeff_mod_bit_sizes) in [\n", - " (8192, [40, 21, 21, 21, 21, 21, 21, 40]),\n", - " (8192, [40, 20, 40]),\n", - " (8192, [20, 20, 20]),\n", - " (8192, [17, 17]),\n", - " (4096, [40, 20, 40]),\n", - " (4096, [30, 20, 30]),\n", - " (4096, [20, 20, 20]),\n", - " (4096, [19, 19, 19]),\n", - " (4096, [18, 18, 18]),\n", - " (4096, [18, 18]),\n", - " (4096, [17, 17]),\n", - " (2048, [20, 20]),\n", - " (2048, [18, 18]),\n", - " (2048, [16, 16]),\n", - " ]:\n", - " context = ts.context(\n", - " scheme=ts.SCHEME_TYPE.CKKS,\n", - " poly_modulus_degree=poly_mod,\n", - " coeff_mod_bit_sizes=coeff_mod_bit_sizes,\n", - " encryption_type=enc_type,\n", - " )\n", - " context.generate_galois_keys()\n", - " context.generate_relin_keys()\n", - " \n", - " ser = context.serialize(save_public_key=True, save_secret_key=True, save_galois_keys=True, save_relin_keys=True)\n", - " ctx_size_benchmarks.append([enc_type_str[enc_type], scheme_str[ts.SCHEME_TYPE.CKKS], poly_mod, coeff_mod_bit_sizes, \"all\", convert_size(len(ser))])\n", - " \n", - " if enc_type is ts.ENCRYPTION_TYPE.ASYMMETRIC:\n", - " ser = context.serialize(save_public_key=True, save_secret_key=False, save_galois_keys=False, save_relin_keys=False)\n", - " ctx_size_benchmarks.append([enc_type_str[enc_type], scheme_str[ts.SCHEME_TYPE.CKKS], poly_mod, coeff_mod_bit_sizes, \"Public key\", convert_size(len(ser))])\n", - " \n", - " ser = context.serialize(save_public_key=False, save_secret_key=True, save_galois_keys=False, save_relin_keys=False)\n", - " ctx_size_benchmarks.append([enc_type_str[enc_type], scheme_str[ts.SCHEME_TYPE.CKKS], poly_mod, coeff_mod_bit_sizes, \"Secret key\", convert_size(len(ser))])\n", - " \n", - " ser = context.serialize(save_public_key=False, save_secret_key=False, save_galois_keys=True, save_relin_keys=False)\n", - " ctx_size_benchmarks.append([enc_type_str[enc_type], scheme_str[ts.SCHEME_TYPE.CKKS], poly_mod, coeff_mod_bit_sizes, \"Galois keys\", convert_size(len(ser))])\n", - " \n", - " ser = context.serialize(save_public_key=False, save_secret_key=False, save_galois_keys=False, save_relin_keys=True)\n", - " ctx_size_benchmarks.append([enc_type_str[enc_type], scheme_str[ts.SCHEME_TYPE.CKKS], poly_mod, coeff_mod_bit_sizes, \"Relin keys\", convert_size(len(ser))])\n", - " \n", - " ser = context.serialize(save_public_key=False, save_secret_key=False, save_galois_keys=False, save_relin_keys=False)\n", - " ctx_size_benchmarks.append([enc_type_str[enc_type], scheme_str[ts.SCHEME_TYPE.CKKS], poly_mod, coeff_mod_bit_sizes, \"none\", convert_size(len(ser))])\n", - " \n", - " for (poly_mod, coeff_mod_bit_sizes) in [\n", - " (8192, [40, 21, 21, 21, 21, 21, 21, 40]),\n", - " (8192, [40, 21, 21, 21, 21, 21, 40]),\n", - " (8192, [40, 21, 21, 21, 21, 40]),\n", - " (8192, [40, 21, 21, 21, 40]),\n", - " (8192, [40, 21, 21, 40]),\n", - " (8192, [40, 20, 40]),\n", - " (4096, [40, 20, 40]),\n", - " (4096, [30, 20, 30]),\n", - " (4096, [20, 20, 20]),\n", - " (4096, [19, 19, 19]),\n", - " (4096, [18, 18, 18]),\n", - " (2048, [20, 20]),\n", - " ]:\n", - " context = ts.context(\n", - " scheme=ts.SCHEME_TYPE.BFV,\n", - " poly_modulus_degree=poly_mod,\n", - " plain_modulus=786433,\n", - " coeff_mod_bit_sizes=coeff_mod_bit_sizes,\n", - " encryption_type=enc_type,\n", - " )\n", - " \n", - " context.generate_galois_keys()\n", - " context.generate_relin_keys()\n", - " \n", - " ser = context.serialize(save_public_key=True, save_secret_key=True, save_galois_keys=True, save_relin_keys=True)\n", - " ctx_size_benchmarks.append([enc_type_str[enc_type], scheme_str[ts.SCHEME_TYPE.BFV], poly_mod, coeff_mod_bit_sizes, \"all\", convert_size(len(ser))])\n", - " \n", - " if enc_type is ts.ENCRYPTION_TYPE.ASYMMETRIC:\n", - " ser = context.serialize(save_public_key=True, save_secret_key=False, save_galois_keys=False, save_relin_keys=False)\n", - " ctx_size_benchmarks.append([enc_type_str[enc_type], scheme_str[ts.SCHEME_TYPE.BFV], poly_mod, coeff_mod_bit_sizes, \"Public key\", convert_size(len(ser))])\n", - " \n", - " ser = context.serialize(save_public_key=False, save_secret_key=True, save_galois_keys=False, save_relin_keys=False)\n", - " ctx_size_benchmarks.append([enc_type_str[enc_type], scheme_str[ts.SCHEME_TYPE.BFV], poly_mod, coeff_mod_bit_sizes, \"Secret key\", convert_size(len(ser))])\n", - " \n", - " ser = context.serialize(save_public_key=False, save_secret_key=False, save_galois_keys=True, save_relin_keys=False)\n", - " ctx_size_benchmarks.append([enc_type_str[enc_type], scheme_str[ts.SCHEME_TYPE.BFV], poly_mod, coeff_mod_bit_sizes, \"Galois keys\", convert_size(len(ser))])\n", - " \n", - " ser = context.serialize(save_public_key=False, save_secret_key=False, save_galois_keys=False, save_relin_keys=True)\n", - " ctx_size_benchmarks.append([enc_type_str[enc_type], scheme_str[ts.SCHEME_TYPE.BFV], poly_mod, coeff_mod_bit_sizes, \"Relin keys\", convert_size(len(ser))])\n", - " \n", - " ser = context.serialize(save_public_key=False, save_secret_key=False, save_galois_keys=False, save_relin_keys=False)\n", - " ctx_size_benchmarks.append([enc_type_str[enc_type], scheme_str[ts.SCHEME_TYPE.BFV], poly_mod, coeff_mod_bit_sizes, \"none\", convert_size(len(ser))])\n", - " \n", - "\n", - "display(HTML(tabulate.tabulate(ctx_size_benchmarks, tablefmt='html')))" - ], + "metadata": {}, "outputs": [ { - "output_type": "display_data", "data": { - "text/plain": [ - "" - ], "text/html": [ "\n", "\n", @@ -533,15 +436,114 @@ "\n", "\n", "
asymmetric bfv 2048 [20, 20] none 83.0 B
" + ], + "text/plain": [ + "" ] }, - "metadata": {} + "metadata": {}, + "output_type": "display_data" } ], - "metadata": {} + "source": [ + "ctx_size_benchmarks = [[\"Encryption Type\", \"Scheme Type\", \"Polynomial modulus\", \"Coefficient modulus sizes\", \"Saved keys\", \"Context serialized size\", ]]\n", + "\n", + "for enc_type in [ts.ENCRYPTION_TYPE.SYMMETRIC, ts.ENCRYPTION_TYPE.ASYMMETRIC]:\n", + " for (poly_mod, coeff_mod_bit_sizes) in [\n", + " (8192, [40, 21, 21, 21, 21, 21, 21, 40]),\n", + " (8192, [40, 20, 40]),\n", + " (8192, [20, 20, 20]),\n", + " (8192, [17, 17]),\n", + " (4096, [40, 20, 40]),\n", + " (4096, [30, 20, 30]),\n", + " (4096, [20, 20, 20]),\n", + " (4096, [19, 19, 19]),\n", + " (4096, [18, 18, 18]),\n", + " (4096, [18, 18]),\n", + " (4096, [17, 17]),\n", + " (2048, [20, 20]),\n", + " (2048, [18, 18]),\n", + " (2048, [16, 16]),\n", + " ]:\n", + " context = ts.context(\n", + " scheme=ts.SCHEME_TYPE.CKKS,\n", + " poly_modulus_degree=poly_mod,\n", + " coeff_mod_bit_sizes=coeff_mod_bit_sizes,\n", + " encryption_type=enc_type,\n", + " )\n", + " context.generate_galois_keys()\n", + " context.generate_relin_keys()\n", + " \n", + " ser = context.serialize(save_public_key=True, save_secret_key=True, save_galois_keys=True, save_relin_keys=True)\n", + " ctx_size_benchmarks.append([enc_type_str[enc_type], scheme_str[ts.SCHEME_TYPE.CKKS], poly_mod, coeff_mod_bit_sizes, \"all\", convert_size(len(ser))])\n", + " \n", + " if enc_type is ts.ENCRYPTION_TYPE.ASYMMETRIC:\n", + " ser = context.serialize(save_public_key=True, save_secret_key=False, save_galois_keys=False, save_relin_keys=False)\n", + " ctx_size_benchmarks.append([enc_type_str[enc_type], scheme_str[ts.SCHEME_TYPE.CKKS], poly_mod, coeff_mod_bit_sizes, \"Public key\", convert_size(len(ser))])\n", + " \n", + " ser = context.serialize(save_public_key=False, save_secret_key=True, save_galois_keys=False, save_relin_keys=False)\n", + " ctx_size_benchmarks.append([enc_type_str[enc_type], scheme_str[ts.SCHEME_TYPE.CKKS], poly_mod, coeff_mod_bit_sizes, \"Secret key\", convert_size(len(ser))])\n", + " \n", + " ser = context.serialize(save_public_key=False, save_secret_key=False, save_galois_keys=True, save_relin_keys=False)\n", + " ctx_size_benchmarks.append([enc_type_str[enc_type], scheme_str[ts.SCHEME_TYPE.CKKS], poly_mod, coeff_mod_bit_sizes, \"Galois keys\", convert_size(len(ser))])\n", + " \n", + " ser = context.serialize(save_public_key=False, save_secret_key=False, save_galois_keys=False, save_relin_keys=True)\n", + " ctx_size_benchmarks.append([enc_type_str[enc_type], scheme_str[ts.SCHEME_TYPE.CKKS], poly_mod, coeff_mod_bit_sizes, \"Relin keys\", convert_size(len(ser))])\n", + " \n", + " ser = context.serialize(save_public_key=False, save_secret_key=False, save_galois_keys=False, save_relin_keys=False)\n", + " ctx_size_benchmarks.append([enc_type_str[enc_type], scheme_str[ts.SCHEME_TYPE.CKKS], poly_mod, coeff_mod_bit_sizes, \"none\", convert_size(len(ser))])\n", + " \n", + " for (poly_mod, coeff_mod_bit_sizes) in [\n", + " (8192, [40, 21, 21, 21, 21, 21, 21, 40]),\n", + " (8192, [40, 21, 21, 21, 21, 21, 40]),\n", + " (8192, [40, 21, 21, 21, 21, 40]),\n", + " (8192, [40, 21, 21, 21, 40]),\n", + " (8192, [40, 21, 21, 40]),\n", + " (8192, [40, 20, 40]),\n", + " (4096, [40, 20, 40]),\n", + " (4096, [30, 20, 30]),\n", + " (4096, [20, 20, 20]),\n", + " (4096, [19, 19, 19]),\n", + " (4096, [18, 18, 18]),\n", + " (2048, [20, 20]),\n", + " ]:\n", + " context = ts.context(\n", + " scheme=ts.SCHEME_TYPE.BFV,\n", + " poly_modulus_degree=poly_mod,\n", + " plain_modulus=786433,\n", + " coeff_mod_bit_sizes=coeff_mod_bit_sizes,\n", + " encryption_type=enc_type,\n", + " )\n", + " \n", + " context.generate_galois_keys()\n", + " context.generate_relin_keys()\n", + " \n", + " ser = context.serialize(save_public_key=True, save_secret_key=True, save_galois_keys=True, save_relin_keys=True)\n", + " ctx_size_benchmarks.append([enc_type_str[enc_type], scheme_str[ts.SCHEME_TYPE.BFV], poly_mod, coeff_mod_bit_sizes, \"all\", convert_size(len(ser))])\n", + " \n", + " if enc_type is ts.ENCRYPTION_TYPE.ASYMMETRIC:\n", + " ser = context.serialize(save_public_key=True, save_secret_key=False, save_galois_keys=False, save_relin_keys=False)\n", + " ctx_size_benchmarks.append([enc_type_str[enc_type], scheme_str[ts.SCHEME_TYPE.BFV], poly_mod, coeff_mod_bit_sizes, \"Public key\", convert_size(len(ser))])\n", + " \n", + " ser = context.serialize(save_public_key=False, save_secret_key=True, save_galois_keys=False, save_relin_keys=False)\n", + " ctx_size_benchmarks.append([enc_type_str[enc_type], scheme_str[ts.SCHEME_TYPE.BFV], poly_mod, coeff_mod_bit_sizes, \"Secret key\", convert_size(len(ser))])\n", + " \n", + " ser = context.serialize(save_public_key=False, save_secret_key=False, save_galois_keys=True, save_relin_keys=False)\n", + " ctx_size_benchmarks.append([enc_type_str[enc_type], scheme_str[ts.SCHEME_TYPE.BFV], poly_mod, coeff_mod_bit_sizes, \"Galois keys\", convert_size(len(ser))])\n", + " \n", + " ser = context.serialize(save_public_key=False, save_secret_key=False, save_galois_keys=False, save_relin_keys=True)\n", + " ctx_size_benchmarks.append([enc_type_str[enc_type], scheme_str[ts.SCHEME_TYPE.BFV], poly_mod, coeff_mod_bit_sizes, \"Relin keys\", convert_size(len(ser))])\n", + " \n", + " ser = context.serialize(save_public_key=False, save_secret_key=False, save_galois_keys=False, save_relin_keys=False)\n", + " ctx_size_benchmarks.append([enc_type_str[enc_type], scheme_str[ts.SCHEME_TYPE.BFV], poly_mod, coeff_mod_bit_sizes, \"none\", convert_size(len(ser))])\n", + " \n", + "\n", + "display(HTML(tabulate.tabulate(ctx_size_benchmarks, tablefmt='html')))" + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "## Context serialization interpretation\n", "\n", @@ -552,11 +554,11 @@ " - Galois keys increase the context size only for public contexts (without the secret key). Send them only when you need to perform ciphertext rotations on the other end.\n", " - Relinearization keys increase the context size only for public contexts. Send them only when you need to perform multiplications on ciphertexts on the other end.\n", " - When we send the secret key, the Relinearization/Galois key can be regenerated on the other end without sending them." - ], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "## Ciphertext serialization\n", "\n", @@ -565,97 +567,22 @@ "The first observation here is that the symmetric/asymmetric encryption switch doesn't actually impact the size of the ciphertext, only of the context.\n", "\n", "We will review the benchmarks only for the asymmetric scenario." - ], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 3, - "source": [ - "data = [random.uniform(-10, 10) for _ in range(10 ** 3)]\n", - "network_data = pickle.dumps(data)\n", - "print(\"Plain data size in bytes {}\".format(convert_size(len(network_data))))\n", - "\n", - "enc_type = ts.ENCRYPTION_TYPE.ASYMMETRIC\n", - "ct_size_benchmarks = [[\"Encryption Type\", \"Scheme Type\", \"Polynomial modulus\", \"Coefficient modulus sizes\", \"Precision\", \"Ciphertext serialized size\", \"Encryption increase ratio\"]]\n", - "\n", - "\n", - "for (poly_mod, coeff_mod_bit_sizes, prec) in [\n", - " (8192, [60, 40, 60], 40),\n", - " (8192, [40, 21, 21, 21, 21, 21, 21, 40], 40),\n", - " (8192, [40, 21, 21, 21, 21, 21, 21, 40], 21),\n", - " (8192, [40, 20, 40], 40),\n", - " (8192, [20, 20, 20], 38),\n", - " (8192, [60, 60], 38),\n", - " (8192, [40, 40], 38),\n", - " (8192, [17, 17], 15),\n", - " (4096, [40, 20, 40], 40),\n", - " (4096, [30, 20, 30], 40),\n", - " (4096, [20, 20, 20], 38),\n", - " (4096, [19, 19, 19], 35),\n", - " (4096, [18, 18, 18], 33),\n", - " (4096, [30, 30], 25),\n", - " (4096, [25, 25], 20),\n", - " (4096, [18, 18], 16),\n", - " (4096, [17, 17], 15),\n", - " (2048, [20, 20], 18),\n", - " (2048, [18, 18], 16),\n", - " (2048, [16, 16], 14),\n", - "]:\n", - " context = ts.context(\n", - " scheme=ts.SCHEME_TYPE.CKKS,\n", - " poly_modulus_degree=poly_mod,\n", - " coeff_mod_bit_sizes=coeff_mod_bit_sizes,\n", - " encryption_type=enc_type,\n", - " )\n", - " scale = 2 ** prec\n", - " ckks_vec = ts.ckks_vector(context, data, scale)\n", - " \n", - " enc_network_data = ckks_vec.serialize()\n", - " ct_size_benchmarks.append([enc_type_str[enc_type], scheme_str[ts.SCHEME_TYPE.CKKS], poly_mod, coeff_mod_bit_sizes, \"2**{}\".format(prec),convert_size(len(enc_network_data)), round(len(enc_network_data) / len(network_data), 2)])\n", - " \n", - "for (poly_mod, coeff_mod_bit_sizes) in [\n", - " (8192, [40, 21, 21, 21, 21, 21, 21, 40]),\n", - " (8192, [40, 21, 21, 21, 21, 21, 40]),\n", - " (8192, [40, 21, 21, 21, 21, 40]),\n", - " (8192, [40, 21, 21, 21, 40]),\n", - " (8192, [40, 21, 21, 40]),\n", - " (8192, [40, 20, 40]),\n", - " (4096, [40, 20, 40]),\n", - " (4096, [30, 20, 30]),\n", - " (4096, [20, 20, 20]),\n", - " (4096, [19, 19, 19]),\n", - " (4096, [18, 18, 18]),\n", - " (2048, [20, 20]),\n", - "]:\n", - " context = ts.context(\n", - " scheme=ts.SCHEME_TYPE.BFV,\n", - " poly_modulus_degree=poly_mod,\n", - " plain_modulus=786433,\n", - " coeff_mod_bit_sizes=coeff_mod_bit_sizes,\n", - " encryption_type=enc_type,\n", - " )\n", - " vec = ts.bfv_vector(context, data)\n", - " enc_network_data = vec.serialize()\n", - " ct_size_benchmarks.append([enc_type_str[enc_type], scheme_str[ts.SCHEME_TYPE.BFV], poly_mod, coeff_mod_bit_sizes, \"-\",convert_size(len(enc_network_data)), round(len(enc_network_data) / len(network_data), 2)])\n", - "\n", - " \n", - "display(HTML(tabulate.tabulate(ct_size_benchmarks, tablefmt='html')))" - ], + "metadata": {}, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "Plain data size in bytes 8.8 KB\n" ] }, { - "output_type": "display_data", "data": { - "text/plain": [ - "" - ], "text/html": [ "\n", "\n", @@ -694,15 +621,90 @@ "\n", "\n", "
asymmetric bfv 2048 [20, 20] - 13.03 KB 1.48
" + ], + "text/plain": [ + "" ] }, - "metadata": {} + "metadata": {}, + "output_type": "display_data" } ], - "metadata": {} + "source": [ + "data = [random.uniform(-10, 10) for _ in range(10 ** 3)]\n", + "network_data = pickle.dumps(data)\n", + "print(\"Plain data size in bytes {}\".format(convert_size(len(network_data))))\n", + "\n", + "enc_type = ts.ENCRYPTION_TYPE.ASYMMETRIC\n", + "ct_size_benchmarks = [[\"Encryption Type\", \"Scheme Type\", \"Polynomial modulus\", \"Coefficient modulus sizes\", \"Precision\", \"Ciphertext serialized size\", \"Encryption increase ratio\"]]\n", + "\n", + "\n", + "for (poly_mod, coeff_mod_bit_sizes, prec) in [\n", + " (8192, [60, 40, 60], 40),\n", + " (8192, [40, 21, 21, 21, 21, 21, 21, 40], 40),\n", + " (8192, [40, 21, 21, 21, 21, 21, 21, 40], 21),\n", + " (8192, [40, 20, 40], 40),\n", + " (8192, [20, 20, 20], 38),\n", + " (8192, [60, 60], 38),\n", + " (8192, [40, 40], 38),\n", + " (8192, [17, 17], 15),\n", + " (4096, [40, 20, 40], 40),\n", + " (4096, [30, 20, 30], 40),\n", + " (4096, [20, 20, 20], 38),\n", + " (4096, [19, 19, 19], 35),\n", + " (4096, [18, 18, 18], 33),\n", + " (4096, [30, 30], 25),\n", + " (4096, [25, 25], 20),\n", + " (4096, [18, 18], 16),\n", + " (4096, [17, 17], 15),\n", + " (2048, [20, 20], 18),\n", + " (2048, [18, 18], 16),\n", + " (2048, [16, 16], 14),\n", + "]:\n", + " context = ts.context(\n", + " scheme=ts.SCHEME_TYPE.CKKS,\n", + " poly_modulus_degree=poly_mod,\n", + " coeff_mod_bit_sizes=coeff_mod_bit_sizes,\n", + " encryption_type=enc_type,\n", + " )\n", + " scale = 2 ** prec\n", + " ckks_vec = ts.ckks_vector(context, data, scale)\n", + " \n", + " enc_network_data = ckks_vec.serialize()\n", + " ct_size_benchmarks.append([enc_type_str[enc_type], scheme_str[ts.SCHEME_TYPE.CKKS], poly_mod, coeff_mod_bit_sizes, \"2**{}\".format(prec),convert_size(len(enc_network_data)), round(len(enc_network_data) / len(network_data), 2)])\n", + " \n", + "for (poly_mod, coeff_mod_bit_sizes) in [\n", + " (8192, [40, 21, 21, 21, 21, 21, 21, 40]),\n", + " (8192, [40, 21, 21, 21, 21, 21, 40]),\n", + " (8192, [40, 21, 21, 21, 21, 40]),\n", + " (8192, [40, 21, 21, 21, 40]),\n", + " (8192, [40, 21, 21, 40]),\n", + " (8192, [40, 20, 40]),\n", + " (4096, [40, 20, 40]),\n", + " (4096, [30, 20, 30]),\n", + " (4096, [20, 20, 20]),\n", + " (4096, [19, 19, 19]),\n", + " (4096, [18, 18, 18]),\n", + " (2048, [20, 20]),\n", + "]:\n", + " context = ts.context(\n", + " scheme=ts.SCHEME_TYPE.BFV,\n", + " poly_modulus_degree=poly_mod,\n", + " plain_modulus=786433,\n", + " coeff_mod_bit_sizes=coeff_mod_bit_sizes,\n", + " encryption_type=enc_type,\n", + " )\n", + " vec = ts.bfv_vector(context, data)\n", + " enc_network_data = vec.serialize()\n", + " ct_size_benchmarks.append([enc_type_str[enc_type], scheme_str[ts.SCHEME_TYPE.BFV], poly_mod, coeff_mod_bit_sizes, \"-\",convert_size(len(enc_network_data)), round(len(enc_network_data) / len(network_data), 2)])\n", + "\n", + " \n", + "display(HTML(tabulate.tabulate(ct_size_benchmarks, tablefmt='html')))" + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "### Ciphertext serialization interpretation\n", "\n", @@ -711,129 +713,22 @@ " - The length of coefficient modulus sizes impacts the ciphertext size.\n", " - The values of the coefficient modulus sizes impact the ciphertext size, as well as the precision.\n", " - For a fixed set of polynomial modulus and coefficient modulus sizes, changing the precision doesn't affect the ciphertext size." - ], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "### Understanding the ciphertext precision impact\n" - ], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 4, - "source": [ - "data = [ random.random()]\n", - "\n", - "enc_type = ts.ENCRYPTION_TYPE.ASYMMETRIC\n", - "ct_size_benchmarks = [[\"Value range\", \"Polynomial modulus\", \"Coefficient modulus sizes\", \"Precision\", \"Operation\", \"Status\"]]\n", - "\n", - "\n", - "for data_pow in [-1, 0, 1, 5, 11, 21, 41, 51]:\n", - " data = [ random.uniform(2 ** data_pow, 2 ** (data_pow + 1))]\n", - " for (poly_mod, coeff_mod_bit_sizes, prec) in [\n", - " (8192, [60, 40, 60], 40),\n", - " (8192, [40, 21, 21, 21, 21, 21, 21, 40], 40),\n", - " (8192, [40, 21, 21, 21, 21, 21, 21, 40], 21),\n", - " (8192, [40, 20, 40], 40),\n", - " (8192, [20, 20, 20], 38),\n", - " (8192, [60, 60], 38),\n", - " (8192, [40, 40], 38),\n", - " (8192, [17, 17], 15),\n", - " (4096, [40, 20, 40], 40),\n", - " (4096, [30, 20, 30], 40),\n", - " (4096, [20, 20, 20], 38),\n", - " (4096, [19, 19, 19], 35),\n", - " (4096, [18, 18, 18], 33),\n", - " (4096, [30, 30], 25),\n", - " (4096, [25, 25], 20),\n", - " (4096, [18, 18], 16),\n", - " (4096, [17, 17], 15),\n", - " (2048, [20, 20], 18),\n", - " (2048, [18, 18], 16),\n", - " (2048, [16, 16], 14),\n", - " ]:\n", - " val_str = \"[2^{} - 2^{}]\".format(data_pow, data_pow + 1)\n", - " context = ts.context(\n", - " scheme=ts.SCHEME_TYPE.CKKS,\n", - " poly_modulus_degree=poly_mod,\n", - " coeff_mod_bit_sizes=coeff_mod_bit_sizes,\n", - " encryption_type=enc_type,\n", - " )\n", - " scale = 2 ** prec\n", - " try:\n", - " ckks_vec = ts.ckks_vector(context, data, scale)\n", - " except BaseException as e:\n", - " ct_size_benchmarks.append([val_str, poly_mod, coeff_mod_bit_sizes, \"2**{}\".format(prec), \"encrypt\", \"encryption failed\"])\n", - " continue\n", - " \n", - " decrypted = decrypt(ckks_vec)\n", - " for dec_prec in reversed(range(prec)):\n", - " if pytest.approx(decrypted, abs=2 ** -dec_prec) == data:\n", - " ct_size_benchmarks.append([val_str, poly_mod, coeff_mod_bit_sizes, \"2**{}\".format(prec), \"encrypt\", \"decryption prec 2 ** {}\".format(-dec_prec)])\n", - " break\n", - " ckks_sum = ckks_vec + ckks_vec\n", - " decrypted = decrypt(ckks_sum)\n", - " for dec_prec in reversed(range(prec)):\n", - " if pytest.approx(decrypted, abs=2 ** -dec_prec) == [data[0] + data[0]]:\n", - " ct_size_benchmarks.append([val_str, poly_mod, coeff_mod_bit_sizes, \"2**{}\".format(prec), \"sum\", \"decryption prec 2 ** {}\".format(-dec_prec)])\n", - " break\n", - " \n", - "\n", - "# We add more depth for the multiplication scenario\n", - "for data_pow in [-1, 0, 1, 5, 11, 21, 41, 51]:\n", - " data = [ random.uniform(2 ** data_pow, 2 ** (data_pow + 1))]\n", - " for (poly_mod, coeff_mod_bit_sizes, prec) in [\n", - " (8192, [60, 40, 40, 60], 40),\n", - " (8192, [40, 21, 21, 40], 40),\n", - " (8192, [40, 21, 21, 40], 21),\n", - " (8192, [40, 20, 20, 40], 40),\n", - " (8192, [20, 20, 20], 38),\n", - " (4096, [40, 20, 40], 40),\n", - " (4096, [30, 20, 30], 40),\n", - " (4096, [20, 20, 20], 38),\n", - " (4096, [19, 19, 19], 35),\n", - " (4096, [18, 18, 18], 33),\n", - " (4096, [30, 30, 30], 25),\n", - " (4096, [25, 25, 25], 20),\n", - " (4096, [18, 18, 18], 16),\n", - " (2048, [18, 18, 18], 16),\n", - " ]:\n", - " val_str = \"[2^{} - 2^{}]\".format(data_pow, data_pow + 1)\n", - " context = ts.context(\n", - " scheme=ts.SCHEME_TYPE.CKKS,\n", - " poly_modulus_degree=poly_mod,\n", - " coeff_mod_bit_sizes=coeff_mod_bit_sizes,\n", - " encryption_type=enc_type,\n", - " )\n", - " scale = 2 ** prec\n", - " try:\n", - " ckks_vec = ts.ckks_vector(context, data, scale)\n", - " except BaseException as e:\n", - " continue\n", - " \n", - " try:\n", - " ckks_mul = ckks_vec * ckks_vec\n", - " except:\n", - " ct_size_benchmarks.append([val_str, poly_mod, coeff_mod_bit_sizes, \"2**{}\".format(prec), \"mul\", \"failed\"])\n", - " continue\n", - " decrypted = decrypt(ckks_mul)\n", - " for dec_prec in reversed(range(prec)):\n", - " if pytest.approx(decrypted, abs=2 ** -dec_prec) == [data[0] * data[0]]:\n", - " ct_size_benchmarks.append([val_str, poly_mod, coeff_mod_bit_sizes, \"2**{}\".format(prec), \"mul\", \"decryption prec 2 ** {}\".format(-dec_prec)])\n", - " break\n", - " \n", - "display(HTML(tabulate.tabulate(ct_size_benchmarks, tablefmt='html')))" - ], + "metadata": {}, "outputs": [ { - "output_type": "display_data", "data": { - "text/plain": [ - "" - ], "text/html": [ "\n", "\n", @@ -1085,15 +980,122 @@ "\n", "\n", "
[2^21 - 2^22]8192 [40, 20, 20, 40] 2**40 mul failed
" + ], + "text/plain": [ + "" ] }, - "metadata": {} + "metadata": {}, + "output_type": "display_data" } ], - "metadata": {} + "source": [ + "data = [ random.random()]\n", + "\n", + "enc_type = ts.ENCRYPTION_TYPE.ASYMMETRIC\n", + "ct_size_benchmarks = [[\"Value range\", \"Polynomial modulus\", \"Coefficient modulus sizes\", \"Precision\", \"Operation\", \"Status\"]]\n", + "\n", + "\n", + "for data_pow in [-1, 0, 1, 5, 11, 21, 41, 51]:\n", + " data = [ random.uniform(2 ** data_pow, 2 ** (data_pow + 1))]\n", + " for (poly_mod, coeff_mod_bit_sizes, prec) in [\n", + " (8192, [60, 40, 60], 40),\n", + " (8192, [40, 21, 21, 21, 21, 21, 21, 40], 40),\n", + " (8192, [40, 21, 21, 21, 21, 21, 21, 40], 21),\n", + " (8192, [40, 20, 40], 40),\n", + " (8192, [20, 20, 20], 38),\n", + " (8192, [60, 60], 38),\n", + " (8192, [40, 40], 38),\n", + " (8192, [17, 17], 15),\n", + " (4096, [40, 20, 40], 40),\n", + " (4096, [30, 20, 30], 40),\n", + " (4096, [20, 20, 20], 38),\n", + " (4096, [19, 19, 19], 35),\n", + " (4096, [18, 18, 18], 33),\n", + " (4096, [30, 30], 25),\n", + " (4096, [25, 25], 20),\n", + " (4096, [18, 18], 16),\n", + " (4096, [17, 17], 15),\n", + " (2048, [20, 20], 18),\n", + " (2048, [18, 18], 16),\n", + " (2048, [16, 16], 14),\n", + " ]:\n", + " val_str = \"[2^{} - 2^{}]\".format(data_pow, data_pow + 1)\n", + " context = ts.context(\n", + " scheme=ts.SCHEME_TYPE.CKKS,\n", + " poly_modulus_degree=poly_mod,\n", + " coeff_mod_bit_sizes=coeff_mod_bit_sizes,\n", + " encryption_type=enc_type,\n", + " )\n", + " scale = 2 ** prec\n", + " try:\n", + " ckks_vec = ts.ckks_vector(context, data, scale)\n", + " except BaseException as e:\n", + " ct_size_benchmarks.append([val_str, poly_mod, coeff_mod_bit_sizes, \"2**{}\".format(prec), \"encrypt\", \"encryption failed\"])\n", + " continue\n", + " \n", + " decrypted = decrypt(ckks_vec)\n", + " for dec_prec in reversed(range(prec)):\n", + " if pytest.approx(decrypted, abs=2 ** -dec_prec) == data:\n", + " ct_size_benchmarks.append([val_str, poly_mod, coeff_mod_bit_sizes, \"2**{}\".format(prec), \"encrypt\", \"decryption prec 2 ** {}\".format(-dec_prec)])\n", + " break\n", + " ckks_sum = ckks_vec + ckks_vec\n", + " decrypted = decrypt(ckks_sum)\n", + " for dec_prec in reversed(range(prec)):\n", + " if pytest.approx(decrypted, abs=2 ** -dec_prec) == [data[0] + data[0]]:\n", + " ct_size_benchmarks.append([val_str, poly_mod, coeff_mod_bit_sizes, \"2**{}\".format(prec), \"sum\", \"decryption prec 2 ** {}\".format(-dec_prec)])\n", + " break\n", + " \n", + "\n", + "# We add more depth for the multiplication scenario\n", + "for data_pow in [-1, 0, 1, 5, 11, 21, 41, 51]:\n", + " data = [ random.uniform(2 ** data_pow, 2 ** (data_pow + 1))]\n", + " for (poly_mod, coeff_mod_bit_sizes, prec) in [\n", + " (8192, [60, 40, 40, 60], 40),\n", + " (8192, [40, 21, 21, 40], 40),\n", + " (8192, [40, 21, 21, 40], 21),\n", + " (8192, [40, 20, 20, 40], 40),\n", + " (8192, [20, 20, 20], 38),\n", + " (4096, [40, 20, 40], 40),\n", + " (4096, [30, 20, 30], 40),\n", + " (4096, [20, 20, 20], 38),\n", + " (4096, [19, 19, 19], 35),\n", + " (4096, [18, 18, 18], 33),\n", + " (4096, [30, 30, 30], 25),\n", + " (4096, [25, 25, 25], 20),\n", + " (4096, [18, 18, 18], 16),\n", + " (2048, [18, 18, 18], 16),\n", + " ]:\n", + " val_str = \"[2^{} - 2^{}]\".format(data_pow, data_pow + 1)\n", + " context = ts.context(\n", + " scheme=ts.SCHEME_TYPE.CKKS,\n", + " poly_modulus_degree=poly_mod,\n", + " coeff_mod_bit_sizes=coeff_mod_bit_sizes,\n", + " encryption_type=enc_type,\n", + " )\n", + " scale = 2 ** prec\n", + " try:\n", + " ckks_vec = ts.ckks_vector(context, data, scale)\n", + " except BaseException as e:\n", + " continue\n", + " \n", + " try:\n", + " ckks_mul = ckks_vec * ckks_vec\n", + " except:\n", + " ct_size_benchmarks.append([val_str, poly_mod, coeff_mod_bit_sizes, \"2**{}\".format(prec), \"mul\", \"failed\"])\n", + " continue\n", + " decrypted = decrypt(ckks_mul)\n", + " for dec_prec in reversed(range(prec)):\n", + " if pytest.approx(decrypted, abs=2 ** -dec_prec) == [data[0] * data[0]]:\n", + " ct_size_benchmarks.append([val_str, poly_mod, coeff_mod_bit_sizes, \"2**{}\".format(prec), \"mul\", \"decryption prec 2 ** {}\".format(-dec_prec)])\n", + " break\n", + " \n", + "display(HTML(tabulate.tabulate(ct_size_benchmarks, tablefmt='html')))" + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "# Congratulations!!! - Time to Join the Community!\n", "\n", @@ -1114,19 +1116,18 @@ "If you don't have time to contribute to our codebase, but would still like to lend support, you can also become a Backer on our Open Collective. All donations go toward our web hosting and other community expenses such as hackathons and meetups!\n", "\n", "[OpenMined's Open Collective Page](https://opencollective.com/openmined)" - ], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "## References\n", "\n", "1. Yongsoo Song, Introduction to CKKS, [Private AI Bootcamp](microsoft.com/en-us/research/event/private-ai-bootcamp/#!videos).\n", "2. [Microsoft SEAL](https://github.com/microsoft/SEAL).\n", "3. Daniel Huynh, [CKKS Explained Series](https://blog.openmined.org/ckks-explained-part-1-simple-encoding-and-decoding/)." - ], - "metadata": {} + ] } ], "metadata": { @@ -1145,9 +1146,14 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.0" + "version": "3.9.6 (default, Sep 26 2022, 11:37:49) \n[Clang 14.0.0 (clang-1400.0.29.202)]" + }, + "vscode": { + "interpreter": { + "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" + } } }, "nbformat": 4, "nbformat_minor": 4 -} \ No newline at end of file +} diff --git a/tutorials/Tutorial 4 - Encrypted Convolution on MNIST.ipynb b/tutorials/Tutorial 4 - Encrypted Convolution on MNIST.ipynb index f689127f..e55d2539 100644 --- a/tutorials/Tutorial 4 - Encrypted Convolution on MNIST.ipynb +++ b/tutorials/Tutorial 4 - Encrypted Convolution on MNIST.ipynb @@ -2,6 +2,7 @@ "cells": [ { "cell_type": "markdown", + "metadata": {}, "source": [ "# Tutorial 4: Encrypted Convolution on MNIST\n", "\n", @@ -14,11 +15,11 @@ "Authors:\n", "- Ayoub Benaissa - Twitter: [@y0uben11](https://twitter.com/y0uben11)\n", "- Bilal Retiat - Twitter: [@philomath213](https://twitter.com/philomath213)" - ], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "## Machine Learning Model\n", "\n", @@ -77,21 +78,39 @@ "\n", "\n", "Building on these operations, we now know that this evaluation requires exactly 6 multiplications to be performed, 2 for the convolution, 1 for the first square activation, 1 for the first linear layer, 1 for the second square activation, and finally 1 for the last linear layer." - ], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "## Training\n", "\n", "Now that we know how we can implement such a model via HE, we will start using a library called [TenSEAL](https://github.com/OpenMined/TenSEAL) that implements all these operations we have been describing. But first, we need to train a plain PyTorch model to classify the MNIST dataset." - ], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Epoch: 1 \tTraining Loss: 0.392145\n", + "Epoch: 2 \tTraining Loss: 0.131439\n", + "Epoch: 3 \tTraining Loss: 0.090824\n", + "Epoch: 4 \tTraining Loss: 0.070182\n", + "Epoch: 5 \tTraining Loss: 0.059312\n", + "Epoch: 6 \tTraining Loss: 0.049881\n", + "Epoch: 7 \tTraining Loss: 0.045489\n", + "Epoch: 8 \tTraining Loss: 0.038426\n", + "Epoch: 9 \tTraining Loss: 0.035883\n", + "Epoch: 10 \tTraining Loss: 0.031704\n" + ] + } + ], "source": [ "import torch\n", "from torchvision import datasets\n", @@ -155,37 +174,41 @@ "criterion = torch.nn.CrossEntropyLoss()\n", "optimizer = torch.optim.Adam(model.parameters(), lr=0.001)\n", "model = train(model, train_loader, criterion, optimizer, 10)" - ], - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "Epoch: 1 \tTraining Loss: 0.392145\n", - "Epoch: 2 \tTraining Loss: 0.131439\n", - "Epoch: 3 \tTraining Loss: 0.090824\n", - "Epoch: 4 \tTraining Loss: 0.070182\n", - "Epoch: 5 \tTraining Loss: 0.059312\n", - "Epoch: 6 \tTraining Loss: 0.049881\n", - "Epoch: 7 \tTraining Loss: 0.045489\n", - "Epoch: 8 \tTraining Loss: 0.038426\n", - "Epoch: 9 \tTraining Loss: 0.035883\n", - "Epoch: 10 \tTraining Loss: 0.031704\n" - ] - } - ], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "Then test its accuracy on the test set:" - ], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Test Loss: 0.099073\n", + "\n", + "Test Accuracy of 0: 99% (971/980)\n", + "Test Accuracy of 1: 99% (1130/1135)\n", + "Test Accuracy of 2: 97% (1005/1032)\n", + "Test Accuracy of 3: 98% (995/1010)\n", + "Test Accuracy of 4: 97% (960/982)\n", + "Test Accuracy of 5: 97% (869/892)\n", + "Test Accuracy of 6: 97% (938/958)\n", + "Test Accuracy of 7: 96% (994/1028)\n", + "Test Accuracy of 8: 96% (937/974)\n", + "Test Accuracy of 9: 96% (978/1009)\n", + "\n", + "Test Accuracy (Overall): 97% (9777/10000)\n" + ] + } + ], "source": [ "def test(model, test_loader, criterion):\n", " # initialize lists to monitor test loss and accuracy\n", @@ -226,49 +249,28 @@ " )\n", " \n", "test(model, test_loader, criterion)" - ], - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "Test Loss: 0.099073\n", - "\n", - "Test Accuracy of 0: 99% (971/980)\n", - "Test Accuracy of 1: 99% (1130/1135)\n", - "Test Accuracy of 2: 97% (1005/1032)\n", - "Test Accuracy of 3: 98% (995/1010)\n", - "Test Accuracy of 4: 97% (960/982)\n", - "Test Accuracy of 5: 97% (869/892)\n", - "Test Accuracy of 6: 97% (938/958)\n", - "Test Accuracy of 7: 96% (994/1028)\n", - "Test Accuracy of 8: 96% (937/974)\n", - "Test Accuracy of 9: 96% (978/1009)\n", - "\n", - "Test Accuracy (Overall): 97% (9777/10000)\n" - ] - } - ], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "## Encrypted Evaluation\n", "\n", "Now start the encrypted evaluation that will use the pre-trained model:" - ], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 3, + "metadata": {}, + "outputs": [], "source": [ "\"\"\"\n", "It's a PyTorch-like model using operations implemented in TenSEAL.\n", " - .mm() method is doing the vector-matrix multiplication explained above.\n", " - you can use + operator to add a plain vector as a bias.\n", - " - .conv2d_im2col() method is doing a single convlution operation.\n", + " - .conv2d_im2col() method is doing a single convolution operation.\n", " - .square_() just square the encrypted vector inplace.\n", "\"\"\"\n", "\n", @@ -365,12 +367,11 @@ "# required for encoding\n", "kernel_shape = model.conv1.kernel_size\n", "stride = model.conv1.stride[0]" - ], - "outputs": [], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "Choosing the parameters isn't easy, so we list some intuition here for why we have chosen these parameters exactly:\n", "\n", @@ -380,12 +381,13 @@ "4. The scale is what controls the precision of the fractional part, since it's the value that plaintexts are multiplied with before being encoded into a polynomial of integer coefficients.\n", "\n", "Starting with a scale of more than 20 bits, we need to choose the number of bits of all the middle primes equal to that, so we are already over 120 bits. With this lower bound of coefficient modulus and a security level of 128-bits, we will need a polynomial modulus degree of at least 8192. The upper bound for choosing a higher degree is at 218. Trying different values for the precision and adjusting the coefficient modulus, while studying the loss and accuracy, we end up with 26-bits of scale and primes. We also have 5 bits (31 - 26) for the integer part in the last coefficient modulus, which should be enough for our use case, since output values aren't that big." - ], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 4, + "metadata": {}, + "outputs": [], "source": [ "## Encryption Parameters\n", "\n", @@ -404,28 +406,23 @@ "\n", "# galois keys are required to do ciphertext rotations\n", "context.generate_galois_keys()" - ], - "outputs": [], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "This will now run encrypted evaluation over the whole test-set. It's gonna take time, but with this, you can feel proud of having done encrypted inference on a test-set of 10000 elements, congratulations!" - ], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 5, - "source": [ - "enc_model = EncConvNet(model)\n", - "enc_test(context, enc_model, test_loader, criterion, kernel_shape, stride)" - ], + "metadata": {}, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "Test Loss: 0.136371\n", "\n", @@ -444,19 +441,23 @@ ] } ], - "metadata": {} + "source": [ + "enc_model = EncConvNet(model)\n", + "enc_test(context, enc_model, test_loader, criterion, kernel_shape, stride)" + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "## Cost of the Encrypted Inference\n", "\n", "To conclude, I wanted to give you some numbers about memory and computation costs for this specific use case. Running this on a personal computer with a *Intel(R) Core(TM) i7-3612QM CPU @ 2.10GHz* CPU requires 2 seconds per encrypted inference. In a real-world use case, this would also require sending the encrypted input from the client to the server, and the encrypted result from the server to the client, so the size of these objects really matters. The encrypted input takes about 476KB, while the encrypted result is only about 70KB." - ], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "# Congratulations!!! - Time to Join the Community!\n", "\n", @@ -484,8 +485,7 @@ "If you don't have time to contribute to our codebase, but would still like to lend support, you can also become a Backer on our Open Collective. All donations go toward our web hosting and other community expenses such as hackathons and meetups!\n", "\n", "[OpenMined's Open Collective Page](https://opencollective.com/openmined)\n" - ], - "metadata": {} + ] } ], "metadata": { @@ -509,4 +509,4 @@ }, "nbformat": 4, "nbformat_minor": 4 -} \ No newline at end of file +}