{ "cells": [ { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ ".. _random-bits:\n", "\n", "Random Bit Generation\n", "=====================\n", "\n", "Here we briefly demonstrate generating collections of secret shared uniform random bits, a primitive often useful in the construction of more complex protocols.\n", "\n", "We provide an instance of :class:`numpy.random.Generator` to seed the process along with the required number of bits. The result is a secret shared vector containing the requested bits in big-endian order, and the secret shared integer value of the bits. Since neither result was encoded from a real value, we use the :any:`Bits` encoding to reveal the bits, and :any:`Identity` to reveal the secret.\n", "\n", "In this example we call the function several times using four bits and print the outputs for verification." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:root:****************************************\n", "INFO:root:Player 0 bits: [1 1 1 0] secret: 14\n", "INFO:root:Player 1 bits: [1 1 1 0] secret: 14\n", "INFO:root:Player 2 bits: [1 1 1 0] secret: 14\n", "INFO:root:****************************************\n", "INFO:root:Player 0 bits: [0 1 0 0] secret: 4\n", "INFO:root:Player 1 bits: [0 1 0 0] secret: 4\n", "INFO:root:Player 2 bits: [0 1 0 0] secret: 4\n", "INFO:root:****************************************\n", "INFO:root:Player 0 bits: [0 0 1 0] secret: 2\n", "INFO:root:Player 1 bits: [0 0 1 0] secret: 2\n", "INFO:root:Player 2 bits: [0 0 1 0] secret: 2\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, Identity\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", " generator = numpy.random.default_rng(seed=1234)\n", " \n", " for index in range(3):\n", " log.info(\"*\" * 40, src=0)\n", " \n", " bits_share, secret_share = protocol.random_bitwise_secret(generator=generator, bits=4)\n", " bits = protocol.reveal(bits_share, encoding=Bits())\n", " secret = protocol.reveal(secret_share, encoding=Identity())\n", " \n", " log.info(f\"Player {communicator.rank} bits: {bits} secret: {secret}\")\n", "\n", "SocketCommunicator.run(world_size=3, fn=main);" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ ".. seealso:: :ref:`random-numbers`" ] } ], "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 }