{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Four Demos" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# use pip (or, conda) to make sure numpy is installed:\n", "!pip -q install numpy " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from mpyc.runtime import mpc\n", "import secretsanta, id3gini, lpsolver, np_cnnmnist" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Default runs" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Setting input to default = 8\n", "2022-09-17 10:37:18,694 Start MPyC runtime v0.8.8\n", "Using secure integers: SecInt32\n", "2 [1, 0]\n", "3 [2, 0, 1]\n", "4 [3, 2, 1, 0]\n", "5 [1, 4, 0, 2, 3]\n", "6 [3, 4, 5, 0, 2, 1]\n", "7 [2, 3, 6, 0, 5, 1, 4]\n", "8 [2, 0, 6, 5, 7, 4, 1, 3]\n", "Using secure fixed-point numbers: SecFxp32:16\n", "2 [1.0, 0.0]\n", "3 [2.0, 0.0, 1.0]\n", "4 [3.0, 2.0, 0.0, 1.0]\n", "5 [3.0, 4.0, 1.0, 2.0, 0.0]\n", "6 [5.0, 0.0, 4.0, 1.0, 2.0, 3.0]\n", "7 [6.0, 4.0, 1.0, 2.0, 3.0, 0.0, 5.0]\n", "8 [7.0, 0.0, 4.0, 1.0, 6.0, 3.0, 2.0, 5.0]\n", "Using secure prime fields: SecFld4(GF(11))\n", "2 [1, 0]\n", "3 [1, 2, 0]\n", "4 [2, 3, 1, 0]\n", "5 [1, 3, 4, 0, 2]\n", "6 [4, 2, 5, 0, 3, 1]\n", "7 [4, 5, 1, 0, 6, 2, 3]\n", "8 [5, 4, 3, 1, 2, 0, 7, 6]\n", "Using secure binary fields: SecFld3(GF(2^3))\n", "2 [1, 0]\n", "3 [x, 0, 1]\n", "4 [x, 0, x+1, 1]\n", "5 [x^2, 0, 1, x, x+1]\n", "6 [x^2, x, 1, x^2+1, x+1, 0]\n", "7 [x^2+1, 0, x+1, x^2+x, 1, x^2, x]\n", "8 [x^2+x+1, x^2+1, 1, x^2+x, x, 0, x+1, x^2]\n", "Using secure quinary fields: SecFld5(GF(5^2))\n", "2 [1, 0]\n", "3 [2, 0, 1]\n", "4 [3, 2, 1, 0]\n", "5 [2, 4, 0, 1, 3]\n", "6 [1, 4, 0, 2, x, 3]\n", "7 [x, 4, x+1, 2, 0, 1, 3]\n", "8 [x, 0, x+2, 4, 3, 2, 1, x+1]\n", "Using secure extension fields (medium prime): SecFld25(GF(11^7))\n", "2 [1, 0]\n", "3 [1, 2, 0]\n", "4 [3, 2, 1, 0]\n", "5 [3, 4, 1, 2, 0]\n", "6 [3, 0, 1, 5, 2, 4]\n", "7 [2, 5, 0, 4, 3, 6, 1]\n", "8 [4, 6, 1, 5, 0, 3, 7, 2]\n", "Using secure extension fields (larger prime): SecFld31(GF(1031^3))\n", "2 [1, 0]\n", "3 [2, 0, 1]\n", "4 [3, 2, 1, 0]\n", "5 [2, 3, 4, 0, 1]\n", "6 [3, 5, 0, 2, 1, 4]\n", "7 [1, 3, 6, 0, 5, 2, 4]\n", "8 [5, 7, 0, 4, 6, 1, 3, 2]\n", "2022-09-17 10:37:19,273 Stop MPyC runtime -- elapsed time: 0:00:00.575169\n" ] } ], "source": [ "mpc.run(secretsanta.main())" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Using secure integers: SecInt32\n", "dataset: tennis with 14 samples and 4 attributes\n", "2022-09-17 10:37:19,293 Start MPyC runtime v0.8.8\n", "2022-09-17 10:37:19,300 Attribute node 0\n", "2022-09-17 10:37:19,303 Leaf node label 1\n", "2022-09-17 10:37:19,309 Attribute node 3\n", "2022-09-17 10:37:19,312 Leaf node label 0\n", "2022-09-17 10:37:19,315 Leaf node label 1\n", "2022-09-17 10:37:19,320 Attribute node 2\n", "2022-09-17 10:37:19,323 Leaf node label 0\n", "2022-09-17 10:37:19,325 Leaf node label 1\n", "2022-09-17 10:37:19,326 Stop MPyC runtime -- elapsed time: 0:00:00.032990\n", "Decision tree of depth 2 and size 8: \n", "if Outlook == Overcast: Yes\n", "if Outlook == Rain: \n", "| if Wind == Strong: No\n", "| if Wind == Weak: Yes\n", "if Outlook == Sunny: \n", "| if Humidity == High: No\n", "| if Humidity == Normal: Yes\n" ] } ], "source": [ "mpc.run(id3gini.main())" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Using secure 8-bit integers: SecInt8\n", "dataset: uvlp with 2 constraints and 3 variables (scale factor 1)\n", "2022-09-17 10:37:19,340 Start MPyC runtime v0.8.8\n", "2022-09-17 10:37:19,344 Iteration 1/2: 0.0 pivot=1.0\n", "2022-09-17 10:37:19,349 Iteration 2/2: 10.0 pivot=3.0\n", "max = 37 / 3 / 1 = 12.333333333333334 in 2 iterations\n", "2022-09-17 10:37:19,352 Solution x\n", "2022-09-17 10:37:19,355 Dual solution y\n", "verification c.x == y.b, A.x <= b, x >= 0, y.A >= c, y >= 0: True\n", "solution = [1.3333333333333333, 0.3333333333333333, 0.0]\n", "2022-09-17 10:37:19,360 Stop MPyC runtime -- elapsed time: 0:00:00.019084\n" ] } ], "source": [ "mpc.run(lpsolver.main())" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2022-09-17 10:37:19,370 Start MPyC runtime v0.8.8\n", "2022-09-17 10:37:19,371 --------------- INPUT -------------\n", "Type = SecInt37, range = (1488, 1489)\n", "Labels: [4]\n", "[[0000000000000000000000000000]\n", " [0000000000000000000000000000]\n", " [0000000000000000000000000000]\n", " [0000000000000000000000000000]\n", " [0000000000000000000000000000]\n", " [0000000000000000000000000000]\n", " [0000000110000000000000111000]\n", " [0000001111000000000011111000]\n", " [0000011111000000000111111000]\n", " [0000011111000000000111111000]\n", " [0000011111000000000111110000]\n", " [0000011110000000001111100000]\n", " [0000011110000000001111100000]\n", " [0000011110000000011111000000]\n", " [0000011110000000111111000000]\n", " [0000011110000001111110000000]\n", " [0000011111000111111110000000]\n", " [0000011111111111111100000000]\n", " [0000001111111111111100000000]\n", " [0000000111111100111110000000]\n", " [0000000000000000011110000000]\n", " [0000000000000000011111000000]\n", " [0000000000000000011110000000]\n", " [0000000000000000001110000000]\n", " [0000000000000000000000000000]\n", " [0000000000000000000000000000]\n", " [0000000000000000000000000000]\n", " [0000000000000000000000000000]]\n", "2022-09-17 10:37:19,414 --------------- LAYER 1 -------------\n", "2022-09-17 10:37:19,414 - - - - - - - - conv2d - - - - - - -\n", "2022-09-17 10:37:19,461 - - - - - - - - maxpool - - - - - - -\n", "2022-09-17 10:37:20,767 - - - - - - - - ReLU - - - - - - -\n", "2022-09-17 10:37:21,197 Barrier-after-layer-1 0 0\n", "2022-09-17 10:37:21,197 --------------- LAYER 2 -------------\n", "2022-09-17 10:37:21,223 - - - - - - - - conv2d - - - - - - -\n", "2022-09-17 10:37:22,222 - - - - - - - - maxpool - - - - - - -\n", "2022-09-17 10:37:23,136 - - - - - - - - ReLU - - - - - - -\n", "2022-09-17 10:37:23,429 Barrier-after-layer-2 0 0\n", "2022-09-17 10:37:23,429 --------------- LAYER 3 -------------\n", "2022-09-17 10:37:24,266 - - - - - - - - fc 3136 x 1024 - - -\n", "2022-09-17 10:37:24,422 - - - - - - - - ReLU - - - - - - -\n", "2022-09-17 10:37:24,562 Barrier-after-layer-3 0 0\n", "2022-09-17 10:37:24,562 --------------- LAYER 4 -------------\n", "2022-09-17 10:37:24,607 - - - - - - - - fc 1024 x 10 - - - -\n", "2022-09-17 10:37:24,608 --------------- OUTPUT -------------\n", "Image #1488 with label 4: 4 predicted. \n", "[-3754640229 -3026277190 -6936057232 -10456779375 20035698270 -8239621192\n", " -3735898106 -6070673158 -6724021956 -370619516]\n", "2022-09-17 10:37:24,616 Stop MPyC runtime -- elapsed time: 0:00:05.244793\n" ] } ], "source": [ "mpc.run(np_cnnmnist.main())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Runs with specific inputs" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Using secure integers: SecInt32\n", "2 [1, 0]\n", "3 [1, 2, 0]\n", "4 [1, 3, 0, 2]\n", "5 [1, 2, 3, 4, 0]\n", "6 [2, 0, 4, 5, 1, 3]\n", "Using secure fixed-point numbers: SecFxp32:16\n", "2 [1.0, 0.0]\n", "3 [2.0, 0.0, 1.0]\n", "4 [2.0, 0.0, 3.0, 1.0]\n", "5 [1.0, 4.0, 3.0, 2.0, 0.0]\n", "6 [3.0, 4.0, 0.0, 5.0, 2.0, 1.0]\n", "Using secure prime fields: SecFld3(GF(7))\n", "2 [1, 0]\n", "3 [2, 0, 1]\n", "4 [1, 0, 3, 2]\n", "5 [1, 0, 3, 4, 2]\n", "6 [1, 2, 5, 0, 3, 4]\n", "Using secure binary fields: SecFld3(GF(2^3))\n", "2 [1, 0]\n", "3 [x, 0, 1]\n", "4 [x, x+1, 1, 0]\n", "5 [1, x^2, 0, x, x+1]\n", "6 [x+1, x^2, x^2+1, 1, x, 0]\n", "Using secure quinary fields: SecFld5(GF(5^2))\n", "2 [1, 0]\n", "3 [1, 2, 0]\n", "4 [3, 2, 0, 1]\n", "5 [2, 4, 0, 1, 3]\n", "6 [2, 4, 3, x, 0, 1]\n", "Using secure extension fields (medium prime): SecFld25(GF(11^7))\n", "2 [1, 0]\n", "3 [2, 0, 1]\n", "4 [1, 0, 3, 2]\n", "5 [4, 2, 3, 0, 1]\n", "6 [1, 2, 3, 0, 5, 4]\n", "Using secure extension fields (larger prime): SecFld31(GF(1031^3))\n", "2 [1, 0]\n", "3 [1, 2, 0]\n", "4 [1, 0, 3, 2]\n", "5 [2, 0, 3, 4, 1]\n", "6 [4, 5, 3, 2, 0, 1]\n", "\n", "Using secure integers: SecInt95\n", "dataset: car with 1728 samples and 6 attributes\n", "Decision tree of depth 3 and size 25: \n", "if safety == high: \n", "| if persons == 2: unacc\n", "| if persons == 4: \n", "| | if buying == high: acc\n", "| | if buying == low: acc\n", "| | if buying == med: acc\n", "| | if buying == vhigh: acc\n", "| if persons == more: \n", "| | if buying == high: acc\n", "| | if buying == low: vgood\n", "| | if buying == med: acc\n", "| | if buying == vhigh: unacc\n", "if safety == low: unacc\n", "if safety == med: \n", "| if persons == 2: unacc\n", "| if persons == 4: \n", "| | if buying == high: unacc\n", "| | if buying == low: acc\n", "| | if buying == med: acc\n", "| | if buying == vhigh: unacc\n", "| if persons == more: \n", "| | if lug_boot == big: acc\n", "| | if lug_boot == med: acc\n", "| | if lug_boot == small: unacc\n", "\n", "Using secure 6-bit integers: SecInt6\n", "dataset: wiki with 2 constraints and 3 variables (scale factor 1)\n", "max = 60 / 3 / 1 = 20.0 in 1 iterations\n", "verification c.x == y.b, A.x <= b, x >= 0, y.A >= c, y >= 0: True\n", "solution = [0.0, 0.0, 5.0]\n", "\n", "Type = SecFxp10:4, range = (4269, 4270)\n", "Labels: [4]\n", "[[0000000000000000000000000000]\n", " [0000000000000000000000000000]\n", " [0000001110000000000000000000]\n", " [0000001100000000000000000000]\n", " [0000001100000000000000000000]\n", " [0000001110000000000000000000]\n", " [0000001110000000000000000000]\n", " [0000001110000000000110000000]\n", " [0000001110000000000110000000]\n", " [0000001110000000000110000000]\n", " [0000001110000000000110000000]\n", " [0000001110000000000110000000]\n", " [0000001111000000000111000000]\n", " [0000001111000000000111000000]\n", " [0000000111000000000111000000]\n", " [0000000111000000011111000000]\n", " [0000000111101111111111100000]\n", " [0000000111111111111111100000]\n", " [0000000011111111111111100000]\n", " [0000000001111111000011110000]\n", " [0000000000000000000011110000]\n", " [0000000000000000000001110000]\n", " [0000000000000000000000000000]\n", " [0000000000000000000000000000]\n", " [0000000000000000000000000000]\n", " [0000000000000000000000000000]\n", " [0000000000000000000000000000]\n", " [0000000000000000000000000000]]\n", "Image #4269 with label 4: 4 predicted. \n", "[-5.25 -4.8125 -3.6875 -9.0625 17.375 -5.6875 0. -5.5 -8.\n", " -1.3125]\n" ] } ], "source": [ "import sys\n", "mpc.logging(False)\n", "\n", "sys.argv[1:] = ['6']\n", "mpc.run(secretsanta.main())\n", "\n", "print()\n", "\n", "sys.argv[1:] = ['-i', '2']\n", "mpc.run(id3gini.main())\n", "\n", "print()\n", "\n", "sys.argv[1:] = ['-i', '1']\n", "mpc.run(lpsolver.main())\n", "\n", "print()\n", "\n", "sys.argv[1:] = ['1.5'] # forces use of secure fixed-point arithmetic\n", "mpc.run(np_cnnmnist.main()) # faster than secure integers\n", "\n", "sys.argv[1:] = []" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "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", "version": "3.8.10" } }, "nbformat": 4, "nbformat_minor": 2 }