diff options
| author | Ján Jančár | 2023-12-21 20:07:48 +0100 |
|---|---|---|
| committer | GitHub | 2023-12-21 20:07:48 +0100 |
| commit | 1e5af4ecf41ad828d55cd8cfb3e76abecfcf2779 (patch) | |
| tree | ab27dbf93c7988c82849b4baa5312eb1e785fee8 | |
| parent | 10b1456c8e2ad43ffc54b6f14e0f044f8c721f4e (diff) | |
| parent | c56d846d43aa4c5682a87a180a8ce46e2fd3d075 (diff) | |
| download | pyecsca-notebook-1e5af4ecf41ad828d55cd8cfb3e76abecfcf2779.tar.gz pyecsca-notebook-1e5af4ecf41ad828d55cd8cfb3e76abecfcf2779.tar.zst pyecsca-notebook-1e5af4ecf41ad828d55cd8cfb3e76abecfcf2779.zip | |
Merge pull request #5 from andrr3j/DPA_CPA_notebooks
DPA and CPA notebooks
| -rw-r--r-- | CPA.ipynb | 325 | ||||
| -rw-r--r-- | DPA.ipynb | 431 |
2 files changed, 684 insertions, 72 deletions
@@ -4,14 +4,21 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Correlation power analysis on pyecsca emulated traces" + "# Correlation Power Analysis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "### Setup" + "This notebook demonstrates the functionality of the `CPA` class, which implements Correlation Power Analysis side-channel attack. The attack is performed and evaluated on leakage traces simulated by the `LeakageTarget` class." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Initialisation" ] }, { @@ -27,9 +34,41 @@ "from pyecsca.ec.curve import EllipticCurve\n", "from pyecsca.ec.params import DomainParameters\n", "from pyecsca.sca.attack.leakage_model import HammingWeight\n", - "from pyecsca.sca.target.emulator import EmulatorTarget\n", + "from pyecsca.sca.target.leakage import LeakageTarget\n", "from random import randint\n", - "from pyecsca.sca.attack.CPA import CPA" + "from pyecsca.sca.attack.CPA import CPA\n", + "import holoviews as hv\n", + "import multiprocessing as mp\n", + "import matplotlib.pyplot as plt\n", + "from pyecsca.sca.trace import Trace\n", + "import numpy as np\n", + "import warnings\n", + "from scipy.stats import ConstantInputWarning" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "warnings.filterwarnings(\"ignore\", category=ConstantInputWarning)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "hv.extension(\"bokeh\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We first define the elliptic curve parameters we are going to be using for the demonstration." ] }, { @@ -60,14 +99,27 @@ ] }, { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We create and initialize an instance of the `LeakageTarget` class with the above elliptic curve parameters." + ] + }, + { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "scalar_bit_length = 8\n", - "secret_scalar = randint(1 << (scalar_bit_length - 1), (1 << scalar_bit_length) - 1)\n", - "print(secret_scalar)" + "target = LeakageTarget(model, coords, mult, HammingWeight())\n", + "target.set_params(params)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Using the attack, we will try to recover an 8-bit scalar used in the scalar multiplication operation." ] }, { @@ -76,15 +128,15 @@ "metadata": {}, "outputs": [], "source": [ - "mult.init(params, generator)\n", - "real_pub_key = mult.multiply(secret_scalar)" + "scalar_bit_length = 8\n", + "secret_scalar = 229" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "### Emulation of traces" + "We will perform scalar multiplication on 10 000 random points on the curve and simulate 10 000 corresponding leakage traces, which we will then use to evaluate the attack. " ] }, { @@ -102,9 +154,32 @@ "metadata": {}, "outputs": [], "source": [ - "emulator = EmulatorTarget(model, coords, mult)\n", - "emulator.set_params(params)\n", - "emulator.set_leakage_model(HammingWeight())" + "generated_points, generated_traces = target.simulate_scalar_mult_traces(num_of_traces, secret_scalar)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Performing the attack" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We create and initialize an instance of the `CPA` class containing the attack's implementation." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "mult.init(params, params.generator)\n", + "real_pub_key = mult.multiply(secret_scalar)\n", + "leakage_model = HammingWeight()" ] }, { @@ -113,14 +188,14 @@ "metadata": {}, "outputs": [], "source": [ - "points, traces = emulator.emulate_scalar_mult_traces(num_of_traces, secret_scalar)" + "cpa = CPA(generated_points, generated_traces, leakage_model, mult, params)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "### CPA" + "We perform the attack and test that the scalar we recovered is correct." ] }, { @@ -129,14 +204,16 @@ "metadata": {}, "outputs": [], "source": [ - "cpa = CPA(points, traces, HammingWeight(), mult, params)" + "recovered_scalar = cpa.perform(scalar_bit_length, real_pub_key)\n", + "print(recovered_scalar)\n", + "print(recovered_scalar == secret_scalar)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Recover the 8-bit scalar bit by bit" + "We can visualize the correlations calculated during the attack for correct and incorrect guess of the second bit of the scalar and correct guesses for fourth and sixth bit of the scalar to show where in the computation were the correlations stongest." ] }, { @@ -145,7 +222,8 @@ "metadata": {}, "outputs": [], "source": [ - "recovered_scalar = 128 #1000 0000" + "# Correct difference of means when guessing 2. bit (guessed scalar 192 = 1100 0000)\n", + "cpa.plot_correlations(cpa.correlations['guess_one'][0])" ] }, { @@ -154,8 +232,8 @@ "metadata": {}, "outputs": [], "source": [ - "recovered_scalar = cpa.recover_bit(recovered_scalar, 1, scalar_bit_length, real_pub_key)\n", - "print(f\"Recovered scalar after recovering 2. bit of the secret scalar: {recovered_scalar} = {recovered_scalar:b}\")" + "# Incorrect difference of means when guessing 2. bit (guessed scalar 128 = 1000 0000)\n", + "cpa.plot_correlations(cpa.correlations['guess_zero'][0])" ] }, { @@ -164,18 +242,39 @@ "metadata": {}, "outputs": [], "source": [ - "# We can do this for each bit of the scalar\n", - "for i in range(2, scalar_bit_length):\n", - " recovered_scalar = cpa.recover_bit(recovered_scalar, i, scalar_bit_length, real_pub_key)\n", - " print(f\"Recovered scalar after recovering {i + 1}. bit of the secret scalar: {recovered_scalar} = {recovered_scalar:b}\")\n", - "print(recovered_scalar == secret_scalar)" + "# Correct difference of means when guessing 4. bit (guessed scalar 224 = 1110 0000)\n", + "cpa.plot_correlations(cpa.correlations['guess_zero'][2])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Correct difference of means when guessing 6. bit (guessed scalar 229 = 1110 0100)\n", + "cpa.plot_correlations(cpa.correlations['guess_one'][-2])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Evaluating the effectiveness of the attack" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Recover the whole 8-bit scalar" + "We will evaluate how the success rate of the attack increases with an increasing number of traces and subsequently evaluate how the success rate for a fixed number of traces changes with increasing noise. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "To have a convenient way of generating random subsets, we define the following function:" ] }, { @@ -184,9 +283,175 @@ "metadata": {}, "outputs": [], "source": [ - "recovered_scalar = cpa.perform(scalar_bit_length, real_pub_key)\n", - "print(recovered_scalar)\n", - "print(recovered_scalar == secret_scalar)" + "def select_random_sample(size, points, traces):\n", + " sample_points = []\n", + " sample_traces = []\n", + " selected_numbers = []\n", + " for _ in range(size):\n", + " num = randint(0, num_of_traces - 1)\n", + " if num not in selected_numbers:\n", + " selected_numbers.append(num)\n", + " sample_points.append(points[num])\n", + " sample_traces.append(traces[num])\n", + " return sample_points, sample_traces" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Success rate with changing number of traces" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We take subsets of different sizes and plot the success rate of the DPA corresponding to them. For each size, we perform the DPA 100 times on 100 generated subsets." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def CPA_traces(points, traces, sample_size, leakage_model, scalar_length, pub_key, multiplier, domain_params):\n", + " sample_points, sample_traces = select_random_sample(sample_size, points, traces)\n", + " sample_cpa = CPA(sample_points, sample_traces, leakage_model, multiplier, domain_params)\n", + " recovered = sample_cpa.perform(scalar_length, pub_key)\n", + " return recovered == secret_scalar" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "number_of_subsets = 17\n", + "successes_rates_traces = []\n", + "number_of_samples = [x * 2 for x in range(1, number_of_subsets)]\n", + "for num in number_of_samples:\n", + " with mp.Pool() as pool:\n", + " result_objects = [pool.apply_async(CPA_traces, args=(generated_points, generated_traces, num, leakage_model, scalar_bit_length, \n", + " real_pub_key, mult, params)) for _ in range(100)]\n", + " results = [result.get() for result in result_objects]\n", + " successes_rates_traces.append(sum(results) / 100)\n", + " print(f\"done for {num}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(successes_rates_traces)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We plot the results." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "plt.plot(number_of_samples, successes_rates_traces, color='black')\n", + "plt.ylim([0, 1])\n", + "plt.xlabel('number of traces')\n", + "plt.ylabel('CPA success rate')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Success rate with fixed number of traces and changing noise" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As we now know the the minimum amount of traces needed for 100% success rate, we generate 100 subsets with that amount of traces and perform CPA for each subset. We do this multiple times with different amount of noise." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def normalize_trace(trace: Trace):\n", + " min_val = np.min(trace.samples)\n", + " max_val = np.max(trace.samples)\n", + " scaled_samples = (trace.samples - min_val) / (max_val - min_val) \n", + " trace.samples = scaled_samples \n", + "\n", + "def CPA_noise(points, traces, sample_size, standard_deviation, leakage_model, scalar_length, pub_key, multiplier, domain_params):\n", + " sample_points, sample_traces = select_random_sample(sample_size, points, traces)\n", + " for trace in sample_traces:\n", + " normalize_trace(trace)\n", + " noise = np.random.normal(0, standard_deviation, sample_traces[0].samples.shape)\n", + " for trace in sample_traces:\n", + " trace.samples = trace.samples + noise\n", + " sample_cpa = CPA(sample_points, sample_traces, leakage_model, multiplier, domain_params)\n", + " recovered = sample_cpa.perform(scalar_length, pub_key)\n", + " return recovered == secret_scalar" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "number_of_subsets = 17\n", + "successes_rates_noise = []\n", + "sample_size = 50\n", + "noise_sds = [0.2 * x for x in range(number_of_subsets)] \n", + "for sd in noise_sds:\n", + " with mp.Pool() as pool:\n", + " result_objects = [pool.apply_async(CPA_noise, args=(generated_points, generated_traces, sample_size, sd, leakage_model, scalar_bit_length, \n", + " real_pub_key, mult, params)) for _ in range(100)]\n", + " results = [result.get() for result in result_objects]\n", + " successes_rates_noise.append(sum(results) / 100)\n", + " print(f\"done for {sd}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We plot the results." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(successes_rates_noise)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "plt.plot(noise_sds[0:-1], successes_rates_noise[0:-1], color='black')\n", + "plt.ylim([0, 1])\n", + "plt.xlabel('Gaussian noise (standard deviation)')\n", + "plt.ylabel('CPA success rate')" ] } ], @@ -206,7 +471,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.16" + "version": "3.9.18" }, "orig_nbformat": 4 }, @@ -4,14 +4,21 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Differential power analysis on pyecsca emulated traces" + "# Differential Power Analysis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "### Setup" + "This notebook demonstrates the functionality of the `DPA` class, which implements Differential Power Analysis side-channel attack. The attack is first performed and evaluated on leakage traces simulated by the `LeakageTarget` class and subsequently on leakage traces simulated by the `EmulatorTarget` class." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Initialisation" ] }, { @@ -27,10 +34,21 @@ "from pyecsca.ec.curve import EllipticCurve\n", "from pyecsca.ec.params import DomainParameters\n", "from pyecsca.sca.attack.DPA import DPA\n", + "from pyecsca.sca.attack.CPA import CPA\n", "from pyecsca.sca.attack.leakage_model import HammingWeight\n", - "from pyecsca.sca.target.emulator import EmulatorTarget\n", + "from pyecsca.sca.target.leakage import LeakageTarget\n", + "from pyecsca.sca.trace import Trace\n", + "from pyecsca.sca.trace.edit import stretch\n", + "from pyecsca.sca.trace.process import rolling_mean\n", + "from pyecsca.codegen.client import EmulatorTarget\n", + "from rainbow import TraceConfig\n", + "from rainbow.leakage_models import HammingWeight\n", "from random import randint\n", - "import holoviews as hv" + "import holoviews as hv\n", + "import multiprocessing as mp\n", + "import numpy as np\n", + "import matplotlib.pyplot as plt\n", + "import pickle" ] }, { @@ -43,6 +61,13 @@ ] }, { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We first define the elliptic curve parameters we are going to be using for the demonstration." + ] + }, + { "cell_type": "code", "execution_count": null, "metadata": {}, @@ -62,29 +87,35 @@ "curve = EllipticCurve(model, coords, p, neutral, {\"a\": a, \"b\": b})\n", "params = DomainParameters(curve, generator, n, h)\n", "\n", - "add = coords.formulas[\"add-2015-rcb\"]\n", - "dbl = coords.formulas[\"dbl-2015-rcb\"]\n", + "add = coords.formulas[\"add-1998-cmo\"]\n", + "dbl = coords.formulas[\"dbl-1998-cmo\"]\n", "scl = coords.formulas[\"z\"]\n", "\n", "mult = LTRMultiplier(add, dbl, None)" ] }, { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We create and initialize an instance of the `LeakageTarget` class with the above elliptic curve parameters." + ] + }, + { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "scalar_bit_length = 8\n", - "secret_scalar = randint(128, 255)\n", - "print(secret_scalar)" + "target = LeakageTarget(model, coords, mult, HammingWeight())\n", + "target.set_params(params)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "### Emulation of traces" + "Using the attack, we will try to recover an 8-bit scalar used in the scalar multiplication operation." ] }, { @@ -93,7 +124,15 @@ "metadata": {}, "outputs": [], "source": [ - "num_of_traces = 5000" + "scalar_bit_length = 8\n", + "secret_scalar = 229" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We will perform scalar multiplication on 10 000 random points on the curve and simulate 10 000 corresponding leakage traces, which we will then use to evaluate the attack. " ] }, { @@ -102,9 +141,7 @@ "metadata": {}, "outputs": [], "source": [ - "emulator = EmulatorTarget(model, coords, mult)\n", - "emulator.set_params(params)\n", - "emulator.set_leakage_model(HammingWeight())" + "num_of_traces = 2000" ] }, { @@ -113,14 +150,21 @@ "metadata": {}, "outputs": [], "source": [ - "points, traces = emulator.emulate_scalar_mult_traces(num_of_traces, secret_scalar)" + "generated_points, generated_traces = target.simulate_scalar_mult_traces(num_of_traces, secret_scalar)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Performing the attack" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "### DPA" + "We create and initialize an instance of the `DPA` class containing the attack's implementation." ] }, { @@ -130,15 +174,23 @@ "outputs": [], "source": [ "mult.init(params, params.generator)\n", - "real_pub_key = mult.multiply(secret_scalar)\n", - "dpa = DPA(points, traces, mult, params)" + "real_pub_key = mult.multiply(secret_scalar)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dpa = DPA(generated_points, generated_traces, mult, params)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Recover the 8-bit scalar bit by bit" + "We perform the attack and test that the scalar we recovered is correct." ] }, { @@ -147,7 +199,16 @@ "metadata": {}, "outputs": [], "source": [ - "recovered_scalar = 128 #1000 0000" + "recovered_scalar = dpa.perform(scalar_bit_length, real_pub_key)\n", + "print(recovered_scalar)\n", + "print(recovered_scalar == secret_scalar)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can visualize the differences of means calculated during the attack. Below are shown DOMs for correct and incorrect guess of the second bit of the scalar, followed by DOMs of correct guesses for fourth and sixth bit of the scalar to show the location of peeks." ] }, { @@ -156,8 +217,8 @@ "metadata": {}, "outputs": [], "source": [ - "recovered_scalar = dpa.recover_bit(recovered_scalar, 1, scalar_bit_length, real_pub_key)\n", - "print(f\"Recovered scalar after recovering 2. bit of the secret scalar: {recovered_scalar} = {recovered_scalar:b}\")" + "# Correct difference of means when guessing 2. bit (guessed scalar 192 = 1100 0000)\n", + "dpa.plot_difference_of_means(dpa.doms['guess_one'][0])" ] }, { @@ -166,8 +227,8 @@ "metadata": {}, "outputs": [], "source": [ - "# Difference of means when guessing 2. bit (guessed scalar 192 = 1100 0000)\n", - "dpa.plot_difference_of_means(dpa.doms['guess_one'][0])" + "# Incorrect difference of means when guessing 2. bit (guessed scalar 128 = 1000 0000)\n", + "dpa.plot_difference_of_means(dpa.doms['guess_zero'][0])" ] }, { @@ -176,28 +237,39 @@ "metadata": {}, "outputs": [], "source": [ - "# Difference of means when guessing 2. bit (guessed scalar 128 = 1000 0000)\n", - "dpa.plot_difference_of_means(dpa.doms['guess_zero'][0])" + "# Correct difference of means when guessing 4. bit (guessed scalar 224 = 1110 0000)\n", + "dpa.plot_difference_of_means(dpa.doms['guess_zero'][2])" ] }, { "cell_type": "code", - "execution_count": 24, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "# We can do this for each bit of the scalar\n", - "for i in range(2, scalar_bit_length):\n", - " recovered_scalar = dpa.recover_bit(recovered_scalar, i, scalar_bit_length, real_pub_key)\n", - " print(f\"Recovered scalar after recovering {i + 1}. bit of the secret scalar: {recovered_scalar} = {recovered_scalar:b}\")\n", - "print(recovered_scalar == secret_scalar)" + "# Correct difference of means when guessing 6. bit (guessed scalar 229 = 1110 0100)\n", + "dpa.plot_difference_of_means(dpa.doms['guess_one'][-2])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Evaluating the effectiveness of the attack" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We will evaluate how the success rate of the attack increases with an increasing number of traces and subsequently evaluate how the success rate for a fixed number of traces changes with increasing noise. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Recover the whole 8-bit scalar" + "To have a convenient way of generating random subsets, we define the following function:" ] }, { @@ -206,16 +278,291 @@ "metadata": {}, "outputs": [], "source": [ - "recovered_scalar = dpa.perform(scalar_bit_length, real_pub_key)\n", - "print(recovered_scalar)\n", - "print(recovered_scalar == secret_scalar)" + "def select_random_sample(size, points, traces):\n", + " sample_points = []\n", + " sample_traces = []\n", + " selected_numbers = []\n", + " for _ in range(size):\n", + " num = randint(0, num_of_traces - 1)\n", + " if num not in selected_numbers:\n", + " selected_numbers.append(num)\n", + " sample_points.append(points[num])\n", + " sample_traces.append(traces[num])\n", + " return sample_points, sample_traces" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### We can look at the difference of means after last recovered bit for both zero guess and one guess" + "### Success rate with changing number of traces" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We take subsets of different sizes and plot the success rate of the DPA corresponding to them. For each size, we perform the DPA 100 times on 100 generated subsets." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def DPA_traces(points, traces, sample_size, scalar_length, pub_key, multiplier, domain_params):\n", + " sample_points, sample_traces = select_random_sample(sample_size, points, traces)\n", + " sample_dpa = DPA(sample_points, sample_traces, multiplier, domain_params)\n", + " recovered = sample_dpa.perform(scalar_length, pub_key)\n", + " return recovered == secret_scalar\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "number_of_subsets = 17\n", + "successes_rates_traces = []\n", + "number_of_samples = [x * 100 for x in range(1, number_of_subsets)]\n", + "for num in number_of_samples:\n", + " with mp.Pool() as pool:\n", + " result_objects = [pool.apply_async(DPA_traces, args=(generated_points, generated_traces, num, scalar_bit_length, \n", + " real_pub_key, mult, params)) for _ in range(100)]\n", + " results = [result.get() for result in result_objects]\n", + " successes_rates_traces.append(sum(results) / 100)\n", + " print(f\"done for {num}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We plot the results." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "plt.plot(number_of_samples, successes_rates_traces, color='black')\n", + "plt.ylim([0, 1])\n", + "plt.xlabel('number of traces')\n", + "plt.ylabel('DPA success rate')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Success rate with fixed number of traces and changing noise" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As we now know the the minimum amount of traces needed for 100% success rate, we generate 100 subsets with that amount of traces and perform DPA for each subset. We do this multiple times with different amount of noise." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def normalize_trace(trace: Trace):\n", + " min_val = np.min(trace.samples)\n", + " max_val = np.max(trace.samples)\n", + " scaled_samples = (trace.samples - min_val) / (max_val - min_val) \n", + " trace.samples = scaled_samples \n", + "\n", + "def DPA_noise(points, traces, sample_size, standard_deviation, scalar_length, pub_key, multiplier, domain_params):\n", + " sample_points, sample_traces = select_random_sample(sample_size, points, traces)\n", + " for trace in sample_traces:\n", + " normalize_trace(trace)\n", + " noise = np.random.normal(0, standard_deviation, sample_traces[0].samples.shape)\n", + " for trace in sample_traces:\n", + " trace.samples = trace.samples + noise\n", + " sample_dpa = DPA(sample_points, sample_traces, multiplier, domain_params)\n", + " recovered = sample_dpa.perform(scalar_length, pub_key)\n", + " return recovered == secret_scalar\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "number_of_subsets = 17\n", + "successes_rates_noise = []\n", + "sample_size = 1600\n", + "noise_sds = [0.05 * x for x in range(number_of_subsets)] \n", + "for sd in noise_sds:\n", + " with mp.Pool() as pool:\n", + " result_objects = [pool.apply_async(DPA_noise, args=(generated_points, generated_traces, sample_size, sd, scalar_bit_length, \n", + " real_pub_key, mult, params)) for _ in range(100)]\n", + " results = [result.get() for result in result_objects]\n", + " successes_rates_noise.append(sum(results) / 100)\n", + " print(f\"done for {sd}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We plot the results." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "plt.plot(noise_sds, successes_rates_noise, color='black')\n", + "plt.ylim([0, 1])\n", + "plt.xlabel('Gaussian noise (standard deviation)')\n", + "plt.ylabel('DPA success rate')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## DPA on leakage traces simulated by `EmulatorTarget`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We gather leakage traces simulated by the `EmulatorTarget`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "with open('/data/xbatora/traces_8bit.pickle', 'rb') as f:\n", + " traces_emulator = pickle.load(f)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "points_emulator = []\n", + "max_length = 0\n", + "for trace in traces_emulator:\n", + " if(len(trace) > max_length):\n", + " max_length = len(trace)\n", + " points_emulator.append(Point(coords, **trace.meta['coords']))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The traces are not equally long. We `stretch` them to the same length. This should also reduce misalignment." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "stretched_traces = []\n", + "for trace in traces_emulator:\n", + " stretched_traces.append(stretch(trace, max_length))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We recover the second bit of the scalar. We know it should equal one, so we plot its corresponding DOM that should show a peak." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "recovered_scalar = 128 #1000 0000" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dpa_emulator = DPA(points_emulator, stretched_traces, mult, params)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "recovered_scalar = 128 #1000 0000\n", + "res = dpa_emulator.recover_bit(recovered_scalar, 1, scalar_bit_length, real_pub_key)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The peak is not visible" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dpa_emulator.plot_difference_of_means(dpa_emulator.doms['guess_one'][0])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The peak is not visible due to a significant misalignment. We apply rolling mean to the traces, which should reduce the misalignment. We perform the DPA again." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "rolling_mean_traces = []\n", + "for trace in stretched_traces:\n", + " rolling_mean_traces.append(rolling_mean(trace, 3000))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dpa_emulator = DPA(points_emulator, rolling_mean_traces, mult, params)" ] }, { @@ -224,8 +571,9 @@ "metadata": {}, "outputs": [], "source": [ - "# Zero guess\n", - "dpa.plot_difference_of_means(dpa.doms['guess_zero'][-1])" + "recovered_scalar = 128 #1000 0000\n", + "res = dpa_emulator.recover_bit(recovered_scalar, 1, scalar_bit_length, real_pub_key)\n", + "print(res)" ] }, { @@ -234,8 +582,7 @@ "metadata": {}, "outputs": [], "source": [ - "# One guess\n", - "dpa.plot_difference_of_means(dpa.doms['guess_one'][-1])" + "dpa_emulator.plot_difference_of_means(dpa_emulator.doms['guess_one'][0])" ] } ], @@ -255,7 +602,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.16" + "version": "3.9.18" }, "orig_nbformat": 4 }, |
