{ "cells": [ { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ ".. _power:\n", "\n", "Power\n", "=====\n", "\n", "This section demonstrates raising secret-shared values to a power. As always, this is computed element-wise on arrays of any shape.\n", "\n", "Note that for this function, we are raising secret-shared values to a **public**, **unencoded**, **non-negative integer** power that is known to all players. The results are secret shared, maintaining the privacy of the inputs.\n", "\n", "In this case, we raise a vector of values :math:`[-1, 2, 3.4, -2.3]` to the power 3,\n", "returning :math:`[-1, 8, 39.304, -12.167]`" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:root:Player 0 power: [ -1. 8. 39.30377197 -12.16680908]\n", "INFO:root:Player 1 power: [ -1. 8. 39.30377197 -12.16680908]\n", "INFO:root:Player 2 power: [ -1. 8. 39.30377197 -12.16680908]\n" ] } ], "source": [ "import logging\n", "\n", "import numpy\n", "\n", "from cicada.additive import AdditiveProtocolSuite\n", "from cicada.communicator import SocketCommunicator\n", "from cicada.logger import Logger\n", "\n", "logging.basicConfig(level=logging.INFO)\n", "\n", "def main(communicator):\n", " log = Logger(logging.getLogger(), communicator)\n", " protocol = AdditiveProtocolSuite(communicator)\n", "\n", " base = numpy.array([-1, 2, 3.4, -2.3]) if communicator.rank == 0 else None\n", " exponent = numpy.array(3)\n", " \n", " base_share = protocol.share(src=0, secret=base, shape=(4,))\n", " power_share = protocol.power(base_share, exponent)\n", " power = protocol.reveal(power_share)\n", " log.info(f\"Player {communicator.rank} power: {power}\")\n", " \n", "SocketCommunicator.run(world_size=3, fn=main);" ] }, { "cell_type": "raw", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "Note that the results 39.30381775 and -12.16680908 are slightly off due to the limited precision of our fixed point encoding.\n", "\n", "The normal numpy rules for :doc:`numpy:user/basics.broadcasting` apply, so you could raise all of the base values to the same exponent as above, or pass a different exponent for every base value, or other broadcastable configurations:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:root:Player 0 power: [ 1. 2. 4. 8. 16.]\n", "INFO:root:Player 1 power: [ 1. 2. 4. 8. 16.]\n", "INFO:root:Player 2 power: [ 1. 2. 4. 8. 16.]\n" ] } ], "source": [ "def main(communicator):\n", " log = Logger(logging.getLogger(), communicator)\n", " protocol = AdditiveProtocolSuite(communicator)\n", "\n", " base = numpy.array([2, 2, 2, 2, 2]) if communicator.rank == 0 else None\n", " exponent = numpy.array([0, 1, 2, 3, 4])\n", " \n", " base_share = protocol.share(src=0, secret=base, shape=(5,))\n", " power_share = protocol.power(base_share, exponent)\n", " power = protocol.reveal(power_share)\n", " log.info(f\"Player {communicator.rank} power: {power}\")\n", " \n", "SocketCommunicator.run(world_size=3, fn=main);" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:root:Player 0 power:\n", "[[3. 9.]\n", " [3. 9.]]\n", "INFO:root:Player 1 power:\n", "[[3. 9.]\n", " [3. 9.]]\n", "INFO:root:Player 2 power:\n", "[[3. 9.]\n", " [3. 9.]]\n" ] } ], "source": [ "def main(communicator):\n", " log = Logger(logging.getLogger(), communicator)\n", " protocol = AdditiveProtocolSuite(communicator)\n", "\n", " base = numpy.array([[3, 3],[3, 3]]) if communicator.rank == 0 else None\n", " exponent = numpy.array([1, 2])\n", " \n", " base_share = protocol.share(src=0, secret=base, shape=(2, 2))\n", " power_share = protocol.power(base_share, exponent)\n", " power = protocol.reveal(power_share)\n", " log.info(f\"Player {communicator.rank} power:\\n{power}\")\n", " \n", "SocketCommunicator.run(world_size=3, fn=main);" ] } ], "metadata": { "celltoolbar": "Raw Cell Format", "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.12.2" } }, "nbformat": 4, "nbformat_minor": 4 }