{ "cells": [ { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ ".. _logical-not:\n", "\n", "Logical Not\n", "===========\n", "\n", "In this example, we will generate a small secret shared vector of random bits, then apply Cicada's logical negation operator to flip them elementwise.\n", "\n", "Note that the secret inputs to :meth:`~cicada.additive.AdditiveProtocolSuite.logical_not` must be the field values :math:`0` or :math:`1`, producing similar outputs. We use the :any:`Bits` encoding to reveal the output 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 bits: [1 1 1 0 0 1 0 0] negated bits: [0 0 0 1 1 0 1 1]\n", "INFO:root:Player 1 bits: [1 1 1 0 0 1 0 0] negated bits: [0 0 0 1 1 0 1 1]\n", "INFO:root:Player 2 bits: [1 1 1 0 0 1 0 0] negated bits: [0 0 0 1 1 0 1 1]\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", " generator = numpy.random.default_rng(seed=1234)\n", " \n", " bits_share, _ = protocol.random_bitwise_secret(generator=generator, bits=8)\n", " negated_bits_share = protocol.logical_not(bits_share)\n", " \n", " bits = protocol.reveal(bits_share, encoding=Bits())\n", " negated_bits = protocol.reveal(negated_bits_share, encoding=Bits())\n", " \n", " log.info(f\"Player {communicator.rank} bits: {bits} negated bits: {negated_bits}\")\n", "\n", "SocketCommunicator.run(world_size=3, fn=main);" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ ".. seealso:: :ref:`logical-xor`" ] } ], "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 }