{ "cells": [ { "cell_type": "raw", "id": "1771ba9c-b779-494b-aff0-bd9f0909cc33", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ ".. _slicing:\n", "\n", "Slicing\n", "=======\n", "\n", "Recall from the :ref:`tutorial` that in Cicada, *secrets* are always arrays that could have zero, one, or many dimensions. Consequently *secret shares* are also multidimensional arrays, supporting similar array manipulation functions. Chief among these is *slicing*, which returns a secret share referencing a subset of the original array.\n", "\n", "For example, you could compute the sum of a range of values from a 1D array:" ] }, { "cell_type": "code", "execution_count": 1, "id": "6996e15d-2bae-4310-9218-078babad1075", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:root:Player 0 sum: 45.0\n", "INFO:root:Player 1 sum: 45.0\n", "INFO:root:Player 2 sum: 45.0\n", "INFO:root:Player 0 subset sum: 9.0\n", "INFO:root:Player 1 subset sum: 9.0\n", "INFO:root:Player 2 subset sum: 9.0\n" ] }, { "data": { "text/plain": [ "[None, None, None]" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import logging\n", "\n", "import numpy\n", "\n", "from cicada.active import ActiveProtocolSuite\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 = ActiveProtocolSuite(communicator, threshold=2)\n", "\n", " secret = numpy.arange(10) if communicator.rank == 0 else None\n", " share = protocol.share(src=0, secret=secret, shape=(10,))\n", "\n", " # Sum every value in the array.\n", " log.info(f\"Player {communicator.rank} sum: {protocol.reveal(protocol.sum(share))}\")\n", "\n", " # Sum the 2nd, 3rd, and 4th values in the array.\n", " log.info(f\"Player {communicator.rank} subset sum: {protocol.reveal(protocol.sum(share[2:5]))}\")\n", "\n", "SocketCommunicator.run(world_size=3, fn=main, show_traceback=True)" ] }, { "cell_type": "raw", "id": "c93a2620-72bd-438b-ba5f-ff2dd53cb4a9", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "In fact Cicada supports all of the indexing operations supported by :doc:`numpy:user/basics.indexing`. For example, you could sum just the odd-numbered elements in an array:" ] }, { "cell_type": "code", "execution_count": 2, "id": "2a5f46d6-a84a-4171-9e4d-0ca1e1d76562", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:root:Player 0 sum: 45.0\n", "INFO:root:Player 1 sum: 45.0\n", "INFO:root:Player 2 sum: 45.0\n", "INFO:root:Player 0 subset sum: 25.0\n", "INFO:root:Player 1 subset sum: 25.0\n", "INFO:root:Player 2 subset sum: 25.0\n" ] }, { "data": { "text/plain": [ "[None, None, None]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def main(communicator):\n", " log = Logger(logging.getLogger(), communicator)\n", " protocol = ActiveProtocolSuite(communicator, threshold=2)\n", "\n", " secret = numpy.arange(10) if communicator.rank == 0 else None\n", " share = protocol.share(src=0, secret=secret, shape=(10,))\n", "\n", " # Sum every value in the array.\n", " log.info(f\"Player {communicator.rank} sum: {protocol.reveal(protocol.sum(share))}\")\n", "\n", " # Sum the odd numbered elements in the array.\n", " log.info(f\"Player {communicator.rank} subset sum: {protocol.reveal(protocol.sum(share[1::2]))}\")\n", "\n", "SocketCommunicator.run(world_size=3, fn=main, show_traceback=True)" ] } ], "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.12.2" } }, "nbformat": 4, "nbformat_minor": 5 }