2023-12-19 18:19:01 +00:00
|
|
|
{
|
|
|
|
"cells": [
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"# Break AES using DPA with correlations\n",
|
|
|
|
"\n",
|
|
|
|
"You need:\n",
|
|
|
|
"* `plaintext.txt`: all PT blocks, (one block per line, in hex, bytes separated by spaces)\n",
|
|
|
|
"* `ciphertext.txt`: all CT blocks, (one block per line, in hex, bytes separated by spaces)\n",
|
|
|
|
"* `traceLength.txt`: how many samples per trace (one decimal number)\n",
|
|
|
|
"* `traces.bin`: raw measured traces, one byte per sample (uint8), all traces together continuously\n"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2024-01-12 11:29:39 +00:00
|
|
|
"execution_count": 1,
|
2023-12-19 18:19:01 +00:00
|
|
|
"metadata": {
|
|
|
|
"id": "GEwwR12Gupsi"
|
|
|
|
},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"import matplotlib.pyplot as plt\n",
|
|
|
|
"import numpy as np"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2024-01-12 11:29:39 +00:00
|
|
|
"execution_count": 2,
|
2023-12-19 18:19:01 +00:00
|
|
|
"metadata": {
|
|
|
|
"id": "8fW8nPQ5uyEO"
|
|
|
|
},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"# AES SBOX\n",
|
|
|
|
"sbox = np.array([\n",
|
|
|
|
" 0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76,\n",
|
|
|
|
" 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0,\n",
|
|
|
|
" 0xB7, 0xFD, 0x93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 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A,\n",
|
|
|
|
" 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E,\n",
|
|
|
|
" 0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF,\n",
|
|
|
|
" 0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16\n",
|
|
|
|
" ], dtype='uint8')\n",
|
|
|
|
"\n",
|
|
|
|
"# Hamming weight lookup table\n",
|
|
|
|
"hw_table = []\n",
|
|
|
|
"for i in range(256):\n",
|
|
|
|
" s = '{0:08b}'.format(i)\n",
|
|
|
|
" hw_table.append(s.count('1'))\n",
|
|
|
|
"hw_table = np.array(hw_table, 'uint8')\n",
|
|
|
|
"\n",
|
|
|
|
"# Correlation of two matrices\n",
|
|
|
|
"def correlate(x, y):\n",
|
|
|
|
" \"\"\"\n",
|
|
|
|
" Correlate all columns from matrix x of shape (a,b)\n",
|
|
|
|
" with all columns from matrix y of shape (a,c),\n",
|
|
|
|
" creating correlation matrix C of shape (b,c).\n",
|
|
|
|
" \n",
|
|
|
|
" Originally matlab script by Jiri Bucek in NI-HWB.\n",
|
|
|
|
" \"\"\"\n",
|
|
|
|
" x = x - np.average(x, 0) # remove vertical averages\n",
|
|
|
|
" y = y - np.average(y, 0) # remove vertical averages\n",
|
|
|
|
" C = x.T @ y # (n-1) Cov(x,y)\n",
|
|
|
|
" C = C / (np.sum(x**2, 0)**(1/2))[:,np.newaxis] # divide by (n-1) Var(x)\n",
|
|
|
|
" C = C / (np.sum(y**2, 0)**(1/2)) # divide by (n-1) Var(y)\n",
|
|
|
|
" return C\n",
|
|
|
|
"\n",
|
|
|
|
"# Load PT of CT from file\n",
|
|
|
|
"def load_text(file_name):\n",
|
|
|
|
" \"\"\"\n",
|
|
|
|
" Load any text PT/CT from file containing hex strings with bytes \n",
|
|
|
|
" separated by spaces, one block per line\n",
|
|
|
|
" Output is a matrix of bytes (np.array)\n",
|
|
|
|
" \"\"\"\n",
|
|
|
|
" txt_str = open(file_name).readlines()\n",
|
|
|
|
" del txt_str[-1] #discard last empty line\n",
|
|
|
|
" #split each line into bytes and convert from hex\n",
|
|
|
|
" txt_bytes_list = list(\n",
|
|
|
|
" map(lambda line: \n",
|
|
|
|
" list(\n",
|
|
|
|
" map(lambda s: int(s, 16),\n",
|
|
|
|
" line.rstrip().split(\" \"))\n",
|
|
|
|
" ),\n",
|
|
|
|
" txt_str)\n",
|
|
|
|
" )\n",
|
|
|
|
" return np.array(txt_bytes_list, 'uint8')"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2024-01-12 11:29:39 +00:00
|
|
|
"execution_count": 3,
|
2023-12-19 18:19:01 +00:00
|
|
|
"metadata": {
|
|
|
|
"id": "--PH16eNuz_H"
|
|
|
|
},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"# read plaintext inputs\n",
|
|
|
|
"inputs = load_text(\"plaintext.txt\")\n",
|
|
|
|
"\n",
|
|
|
|
"# read length of one complete trace (number of samples per trace)\n",
|
|
|
|
"with open(\"traceLength.txt\", \"r\") as fin:\n",
|
|
|
|
" trace_length = int(fin.readline())\n",
|
|
|
|
"\n",
|
|
|
|
"# trim each trace - select interesting part\n",
|
|
|
|
"start = 0\n",
|
|
|
|
"len = trace_length # CHANGE to the length of the first round; \n",
|
|
|
|
"\n",
|
|
|
|
"# read traces from binary file\n",
|
|
|
|
"traces = np.fromfile(\"traces.bin\", dtype='uint8') # read as linear array\n",
|
|
|
|
"traces = np.reshape(traces, (traces.size // trace_length, trace_length)) # reshape into matrix\n",
|
|
|
|
"traces = traces[:, start:len] # select only the interesting part of each trace"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2024-01-12 11:29:39 +00:00
|
|
|
"execution_count": 4,
|
2023-12-19 18:19:01 +00:00
|
|
|
"metadata": {
|
|
|
|
"id": "ZVJ_Tk55u1wu"
|
|
|
|
},
|
2024-01-12 11:29:39 +00:00
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "stdout",
|
|
|
|
"output_type": "stream",
|
|
|
|
"text": [
|
|
|
|
"(100, 16)\n",
|
|
|
|
"200000\n",
|
|
|
|
"(100, 200000)\n"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
2023-12-19 18:19:01 +00:00
|
|
|
"source": [
|
|
|
|
"print(inputs.shape) # dimensions of inputs\n",
|
|
|
|
"print(trace_length)\n",
|
|
|
|
"print(traces.shape) # dimensions of matrix of traces"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2024-01-12 11:29:39 +00:00
|
|
|
"execution_count": 6,
|
|
|
|
"metadata": {},
|
2023-12-19 18:19:01 +00:00
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
2024-01-12 11:29:39 +00:00
|
|
|
"%matplotlib widget"
|
2023-12-19 18:19:01 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2024-01-12 11:29:39 +00:00
|
|
|
"execution_count": 7,
|
2023-12-19 18:19:01 +00:00
|
|
|
"metadata": {
|
|
|
|
"id": "wDAUVmNOu3BP"
|
|
|
|
},
|
2024-01-12 11:29:39 +00:00
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"application/vnd.jupyter.widget-view+json": {
|
|
|
|
"model_id": "8a25fae7cccb4c078bb6a094f047f388",
|
|
|
|
"version_major": 2,
|
|
|
|
"version_minor": 0
|
|
|
|
},
|
|
|
|
"image/png": "iVB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qGoQkTJuibb77R4sWL1alTJ5v1AwYMULNmzbRo0SLrsrS0NO3bt0+JiYmSpMTERG3ZskWHD5+rqVqwYIFCQ0N18cUXN86BuEDVHqne0HPsUH7NM+fafLPxgP6TsselZdjnxrH4/vrDTmUc9p5akX+v2uP0tofs1Gowq4x3ced7tyG+XH/A6aGbPN2erHq7vYwjtp/Tyhq8hmpobeiunKIGP2ZTs+/YSSXP26EcD7TLRU0+2QZw/Pjx+vTTT/Xtt98qJCTE2mYvLCxMzZs3V1hYmO6//35NmjRJERERCg0N1aOPPqrExERddtllkqRrr71WF198se6880698sorys7O1jPPPKPx48crKCjIk4dnKhM/3yRJuvqiaF0Q3tzDpalbYXGZdU5mb1BuSLuPFKlz21YNun/lzAmAPSVl5Zoye4tG9oxR65a1j4wwf5tn25ON/WftNXOfrtmntxebqzbO29zyQYqyC05rTWau5oy/wtPFMT2frAF87733lJ+fr6FDhyo2NtZ6+/zzz63bvPHGG7r++us1ZswYDRkyRDExMZo9e7Z1vb+/v+bOnSt/f38lJibqjjvu0F133aWXXnrJE4dUL6syjvlEO7d5W7JqXIaZ/nOm3TGzCr1sbktJ+mLdfv3m9WX6fnOWS/drGIbeXrRLS9Jc007S2fZcDZF5tKInbWV7OjPJKWhYj1Bnbdh3vEYbsfrYcqBiPLsTLhjPri6nnCjjweP1q/13hapj+h2w8/jlZ9tUrtx11
|
|
|
|
"text/html": [
|
|
|
|
"\n",
|
|
|
|
" <div style=\"display: inline-block;\">\n",
|
|
|
|
" <div class=\"jupyter-widgets widget-label\" style=\"text-align: center;\">\n",
|
|
|
|
" Figure\n",
|
|
|
|
" </div>\n",
|
|
|
|
" <img src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoAAAAHgCAYAAAA10dzkAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8g+/7EAAAACXBIWXMAAA9hAAAPYQGoP6dpAABRjElEQ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G+XH/A6aGbPN2erHq7vYwjtp/Tyhq8hmpobeiunKIGP2ZTs+/YSSXP26EcD7TLRU0+2QZw/Pjx+vTTT/Xtt98qJCTE2mYvLCxMzZs3V1hYmO6//35NmjRJERERCg0N1aOPPqrExERddtllkqRrr71WF198se6880698sorys7O1jPPPKPx48crKCjIk4dnKhM/3yRJuvqiaF0Q3tzDpalbYXGZdU5mb1BuSLuPFKlz21YNun/lzAmAPSVl5Zoye4tG9oxR65a1j4wwf5tn25ON/WftNXOfrtmntxebqzbO29zyQYqyC05rTWau5oy/wtPFMT2frAF87733lJ+fr6FDhyo2NtZ6+/zzz63bvPHGG7r++us1ZswYDRkyRDExMZo9e7Z1vb+/v+bOnSt/f38lJibqjjvu0F133aWXXnrJE4dUL6syjvlEO7d5W7JqXIaZ/nOm3TGzCr1sbktJ+mLdfv3m9WX6fnOWS/drGIbeXrRLS9Jc007S2fZcDZF5tKInbWV7OjPJKWhYj1Bnbdh3vEYbsfrYcqBiPLsTLhjP
|
|
|
|
" </div>\n",
|
|
|
|
" "
|
|
|
|
],
|
|
|
|
"text/plain": [
|
|
|
|
"Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"metadata": {},
|
|
|
|
"output_type": "display_data"
|
|
|
|
}
|
|
|
|
],
|
2023-12-19 18:19:01 +00:00
|
|
|
"source": [
|
|
|
|
"# Plot one trace\n",
|
|
|
|
"fig = plt.figure()\n",
|
|
|
|
"plt.plot(traces[0])\n",
|
|
|
|
"plt.show()"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {
|
|
|
|
"id": "w6boaqAQvF1G"
|
|
|
|
},
|
|
|
|
"source": [
|
|
|
|
"## **Attack the first key byte**\n",
|
|
|
|
"![Intermediate value](dpa-aes-v.png)\n"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2024-01-12 11:29:39 +00:00
|
|
|
"execution_count": 8,
|
2023-12-19 18:19:01 +00:00
|
|
|
"metadata": {
|
|
|
|
"id": "WaKiOUmbvbQR"
|
|
|
|
},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"# Generate key hypotheses (all possible byte values)\n",
|
|
|
|
"keys = np.arange(start=0, stop=256, step=1, dtype='uint8')\n",
|
|
|
|
"# Select the first byte of each input block\n",
|
|
|
|
"inp = inputs[:, 0]\n",
|
|
|
|
"# XOR each data byte with each key\n",
|
|
|
|
"xmat = inp[:, np.newaxis] ^ keys"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2024-01-12 11:29:39 +00:00
|
|
|
"execution_count": 9,
|
2023-12-19 18:19:01 +00:00
|
|
|
"metadata": {},
|
2024-01-12 11:29:39 +00:00
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "stdout",
|
|
|
|
"output_type": "stream",
|
|
|
|
"text": [
|
|
|
|
"[[ 37 235 140 ... 71 237 252]\n",
|
|
|
|
" [134 25 178 ... 142 50 68]\n",
|
|
|
|
" [215 215 233 ... 61 22 57]\n",
|
|
|
|
" ...\n",
|
|
|
|
" [ 18 188 253 ... 68 197 189]\n",
|
|
|
|
" [ 87 194 19 ... 160 8 136]\n",
|
|
|
|
" [148 238 68 ... 23 94 218]]\n",
|
|
|
|
"(100, 16)\n",
|
|
|
|
"(100,)\n",
|
|
|
|
"(100, 1)\n",
|
|
|
|
"(256,)\n",
|
|
|
|
"(100, 256)\n",
|
|
|
|
"[[ 37 36 39 ... 216 219 218]\n",
|
|
|
|
" [134 135 132 ... 123 120 121]\n",
|
|
|
|
" [215 214 213 ... 42 41 40]\n",
|
|
|
|
" ...\n",
|
|
|
|
" [ 18 19 16 ... 239 236 237]\n",
|
|
|
|
" [ 87 86 85 ... 170 169 168]\n",
|
|
|
|
" [148 149 150 ... 105 106 107]]\n"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
2023-12-19 18:19:01 +00:00
|
|
|
"source": [
|
|
|
|
"# Examine the inputs matrix. Does it contain the data from plaintext.txt?\n",
|
|
|
|
"print(inputs)\n",
|
|
|
|
"# What is the shape of all the operands from the previous cell?\n",
|
|
|
|
"print(inputs.shape)\n",
|
|
|
|
"print(inp.shape)\n",
|
|
|
|
"print(inp[:, np.newaxis].shape)\n",
|
|
|
|
"print(keys.shape)\n",
|
|
|
|
"print(xmat.shape)\n",
|
|
|
|
"# Do you understand the values after the XOR operation? What AES operation do they represent?\n",
|
|
|
|
"print(xmat)"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {
|
|
|
|
"id": "VrBZd18VwBOH"
|
|
|
|
},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"# Substitute with SBOX all XORed values -- matrix of intermediate values\n",
|
|
|
|
"smat = sbox[?]"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {
|
|
|
|
"id": "4GfR9BU-wT4G"
|
|
|
|
},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"# Compute Hamming Weights -- the matrix of hypothetical power consumption\n",
|
|
|
|
"hmat = ?[?]"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {
|
|
|
|
"id": "J8TTPk-WwjQH"
|
|
|
|
},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"# Compute the correlation matrix -- correlate the hypotheses with measured traces\n",
|
|
|
|
"print(hmat.shape)\n",
|
|
|
|
"print(traces.shape)\n",
|
|
|
|
"corr = correlate(?, ?)\n",
|
|
|
|
"# What is the shape and contents of the correlation matrix?"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {
|
|
|
|
"id": "iOqbuNAKxCvP"
|
|
|
|
},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"# Find the absolute maximum correlation\n",
|
|
|
|
"acorr = abs(?)\n",
|
|
|
|
"max_acorr = ?.max()\n",
|
|
|
|
"(k, j) = np.where(acorr == ?) # find idices of maximum\n",
|
|
|
|
"print(\"key: %d time: %d\" % (k[0], j[0]))\n",
|
|
|
|
"print(\"key: %1c, %02x\" % (k[0], k[0]))"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"# Plot the correlation traces for the right key byte guess and one wrong key byte guess\n",
|
|
|
|
"# Do you see the correlation peaks?\n",
|
|
|
|
"fig = plt.figure()\n",
|
|
|
|
"plt.plot(?)\n",
|
|
|
|
"plt.plot(?)\n",
|
|
|
|
"plt.show()"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {
|
|
|
|
"id": "Z62RVYJYzncZ"
|
|
|
|
},
|
|
|
|
"source": [
|
|
|
|
"## **Break all key bytes!**"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {
|
|
|
|
"id": "T7HhwO-ezpoQ"
|
|
|
|
},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"keys = np.array(range(0, 256))\n",
|
|
|
|
"kk = np.zeros(16, dtype='uint8')\n",
|
|
|
|
"for i in range(0, 16):\n",
|
|
|
|
" inp = inputs[:, ?]\n",
|
|
|
|
" ????\n",
|
|
|
|
" kk[i] = k\n",
|
|
|
|
" print(\"%1c, %02x @ %d\" % (k[0], k[0], j[0]))"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"## **Verify the key on a PT, CT pair!**"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"key_bytes = bytes(kk)\n",
|
|
|
|
"outputs = ?"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"# !pip install pycryptodome\n",
|
|
|
|
"from Crypto.Cipher import AES\n",
|
|
|
|
"cipher = AES.new(key_bytes, AES.MODE_ECB)\n",
|
|
|
|
"??"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"metadata": {
|
|
|
|
"colab": {
|
|
|
|
"collapsed_sections": [],
|
|
|
|
"name": "dpa_student.ipynb",
|
|
|
|
"provenance": []
|
|
|
|
},
|
|
|
|
"kernelspec": {
|
|
|
|
"display_name": "Python 3",
|
|
|
|
"name": "python3"
|
|
|
|
},
|
|
|
|
"language_info": {
|
|
|
|
"codemirror_mode": {
|
|
|
|
"name": "ipython",
|
|
|
|
"version": 3
|
|
|
|
},
|
|
|
|
"file_extension": ".py",
|
|
|
|
"mimetype": "text/x-python",
|
|
|
|
"name": "python",
|
|
|
|
"nbconvert_exporter": "python",
|
|
|
|
"pygments_lexer": "ipython3",
|
2024-01-12 11:29:39 +00:00
|
|
|
"version": "3.11.6"
|
2023-12-19 18:19:01 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"nbformat": 4,
|
|
|
|
"nbformat_minor": 0
|
|
|
|
}
|