{ "cells": [ { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ ".. _random-numbers:\n", "\n", "Random Number Generation\n", "========================\n", "\n", "Here we will demonstrate generating uniform random shared secrets of any shape using :any:`AdditiveProtocolSuite`. We use an instance of :class:`numpy.random.Generator` to seed the process. After each secret is generated, we reveal it to the players and log it.\n", "\n", "We will initialize the protocol suite with a custom order (251) and a :any:`FixedPoint` encoding with 4 fractional bits so that the random secrets are small and easy to read in the output. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:root:****************************************\n", "INFO:root:Player 0 random values: 3.5\n", "INFO:root:Player 1 random values: 3.5\n", "INFO:root:Player 2 random values: 3.5\n", "INFO:root:****************************************\n", "INFO:root:Player 0 random values: [-1.5]\n", "INFO:root:Player 1 random values: [-1.5]\n", "INFO:root:Player 2 random values: [-1.5]\n", "INFO:root:****************************************\n", "INFO:root:Player 0 random values: [-7.1875 0.875 ]\n", "INFO:root:Player 1 random values: [-7.1875 0.875 ]\n", "INFO:root:Player 2 random values: [-7.1875 0.875 ]\n", "INFO:root:****************************************\n", "INFO:root:Player 0 random values: [-0.75 -7.1875 0.0625]\n", "INFO:root:Player 1 random values: [-0.75 -7.1875 0.0625]\n", "INFO:root:Player 2 random values: [-0.75 -7.1875 0.0625]\n" ] } ], "source": [ "import logging\n", "import sys\n", "\n", "import numpy\n", "\n", "from cicada.additive import AdditiveProtocolSuite\n", "from cicada.communicator import SocketCommunicator\n", "from cicada.encoding import FixedPoint\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, order=251, encoding=FixedPoint(precision=4))\n", " generator = numpy.random.default_rng(seed=1234)\n", " \n", " for shape in [(), (1,), (2,), (3,)]:\n", " log.info(\"*\" * 40, src=0)\n", " \n", " random_share = protocol.field_uniform(generator=generator, shape=shape)\n", " random = protocol.reveal(random_share)\n", " log.info(f\"Player {communicator.rank} random values: {random}\")\n", "\n", "SocketCommunicator.run(world_size=3, fn=main);" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ ".. seealso:: :ref:`random-bits`" ] } ], "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 }