{ "cells": [ { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ ".. _equality:\n", "\n", "Equality Comparison\n", "===================\n", "\n", "This notebook demonstrates comparing secret shared arrays elementwise for equality. The resulting secret shared array will be the same shape as the inputs, containing :math:`1` where the inputs are equal, and :math:`0` where they are 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", "The following example compares :math:`[1, -2, 3, -4.5]` to :math:`[1, 2, 3, -4.5]` and returns :math:`[1, 0, 1, 1]`:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:root:Player 0 secret: [ 1. -2. 3. -4.5]\n", "INFO:root:Player 1 secret: [ 1. 2. 3. -4.5]\n", "INFO:root:Player 0 result: [ True False True True]\n", "INFO:root:Player 1 result: [ True False True True]\n", "INFO:root:Player 2 result: [ True False True 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", "\n", "def main(communicator):\n", " log = Logger(logging.getLogger(), communicator)\n", " protocol = AdditiveProtocolSuite(communicator)\n", "\n", " a = numpy.array([1, -2, 3, -4.5]) if communicator.rank == 0 else None\n", " b = numpy.array([1, 2, 3, -4.5]) if communicator.rank == 1 else None\n", " \n", " log.info(f\"Player {communicator.rank} secret: {a}\", src=0)\n", " log.info(f\"Player {communicator.rank} secret: {b}\", src=1)\n", "\n", " a_share = protocol.share(src=0, secret=a, shape=(4,))\n", " b_share = protocol.share(src=1, secret=b, shape=(4,))\n", " equal_share = protocol.equal(a_share, b_share)\n", " equal = protocol.reveal(equal_share, encoding=Boolean())\n", " \n", " log.info(f\"Player {communicator.rank} result: {equal}\")\n", "\n", "SocketCommunicator.run(world_size=3, fn=main);" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ ".. seealso:: :ref:`less-than`, :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 }