{ "cells": [ { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ ".. _logical-xor:\n", "\n", "Logical Exclusive Or\n", "====================\n", "\n", "In this example, we will compute the elementwise logical XOR of secret shared bits.\n", "\n", "Note that the secret inputs to :meth:`~cicada.additive.AdditiveProtocolSuite.logical_xor` must be the field values :math:`0` or :math:`1`, producing similar outputs. We use the :any:`Bits` encoding for our inputs and outputs because the default :any:`FixedPoint` encoding would produce unexpected results." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:root:Player 0 x: [0 1 0 1]\n", "INFO:root:Player 1 y: [0 0 1 1]\n", "INFO:root:Player 0 x xor y: [0 1 1 0]\n", "INFO:root:Player 1 x xor y: [0 1 1 0]\n", "INFO:root:Player 2 x xor y: [0 1 1 0]\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 Bits\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", " x = numpy.array([0, 1, 0, 1]) if communicator.rank == 0 else None\n", " y = numpy.array([0, 0, 1, 1]) if communicator.rank == 1 else None\n", " \n", " x_share = protocol.share(src=0, secret=x, shape=(4,), encoding=Bits())\n", " y_share = protocol.share(src=1, secret=y, shape=(4,), encoding=Bits())\n", " xor_share = protocol.logical_xor(x_share, y_share)\n", " xor = protocol.reveal(xor_share, encoding=Bits())\n", " \n", " log.info(f\"Player {communicator.rank} x: {x}\", src=0)\n", " log.info(f\"Player {communicator.rank} y: {y}\", src=1)\n", " log.info(f\"Player {communicator.rank} x xor y: {xor}\")\n", "\n", "SocketCommunicator.run(world_size=3, fn=main);" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ ".. seealso:: :ref:`logical-not`" ] } ], "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 }