{ "cells": [ { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ ".. _division:\n", "\n", "Division\n", "========\n", "\n", "In this notebook we demonstrate the division functionality available within cicada, by computing :math:`37 / 7 = 5.2857142857`. As usual, the result will be an approximation limited by the precision of the fixed point representation of our operands.\n", "\n", "Note that the following is a true `private` division where both the `dividend` and the `divisor` are secret-shared values." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:root:Player 0 dividend: 37\n", "INFO:root:Player 1 divisor: 7\n", "INFO:root:Player 0 quotient: 5.2854766845703125\n", "INFO:root:Player 1 quotient: 5.2854766845703125\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", " # Player 0 will provide the secret dividend.\n", " a = numpy.array(37) if communicator.rank == 0 else None\n", " # Player 1 will provide the secret divisor.\n", " b = numpy.array(7) if communicator.rank == 1 else None\n", " \n", " log.info(f\"Player 0 dividend: {a}\", src=0)\n", " log.info(f\"Player 1 divisor: {b}\", src=1)\n", "\n", " # Compute the quotient\n", " a_share = protocol.share(src=0, secret=a, shape=())\n", " b_share = protocol.share(src=1, secret=b, shape=())\n", " quotient_share = protocol.divide(a_share, b_share)\n", " quotient = protocol.reveal(quotient_share)\n", " log.info(f\"Player {communicator.rank} quotient: {quotient}\")\n", "\n", "SocketCommunicator.run(world_size=2, fn=main);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you can see, the result revealed to both players is a close approximation to the expected value." ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ ".. seealso:: :ref:`multiplication`" ] } ], "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 }