{ "cells": [ { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ ".. _less-than:\n", "\n", "Less Than Comparison\n", "====================\n", "\n", "Here we demonstrate comparing two secret-shared arrays elementwise to see which values from the first are less than those from the second. The resulting secret shared array will be the same shape as the input, containing :math:`1` where the first input is less than the second, and :math:`0` where it is not. Note that we use the :any:`Boolean` encoding when we reveal the output because the default :any:`FixedPoint` encoding would produce unexpected results.\n", "\n", "In this example, we compare :math:`2 < 3.5`, with the revealed result indicating that the comparison is, indeed true:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:root:Player 1 secret: 2\n", "INFO:root:Player 2 secret: 3.5\n", "INFO:root:Player 0 result: True\n", "INFO:root:Player 1 result: True\n", "INFO:root:Player 2 result: True\n" ] } ], "source": [ "import logging\n", "\n", "import numpy\n", "\n", "from cicada.additive import AdditiveProtocolSuite\n", "from cicada.communicator import SocketCommunicator\n", "from cicada.encoding import Boolean\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", " a = numpy.array(2) if communicator.rank == 1 else None\n", " b = numpy.array(3.5) if communicator.rank == 2 else None\n", "\n", " log.info(f\"Player {communicator.rank} secret: {a}\", src=1)\n", " log.info(f\"Player {communicator.rank} secret: {b}\", src=2)\n", "\n", " a_share = protocol.share(src=1, secret=a, shape=())\n", " b_share = protocol.share(src=2, secret=b, shape=())\n", " less_than_share = protocol.less(a_share, b_share)\n", " less_than = protocol.reveal(less_than_share, encoding=Boolean())\n", " \n", " log.info(f\"Player {communicator.rank} result: {less_than}\")\n", "\n", "SocketCommunicator.run(world_size=3, fn=main);\n" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ ".. seealso:: :ref:`equality`, :ref:`less-than-zero`" ] } ], "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 }