{ "cells": [ { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ ".. _absolute:\n", "\n", "Absolute Value\n", "==============\n", "\n", "In this example, player 1 secret-shares a vector, and all players compute the absolute values element-wise while preserving privacy:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:root:Player 1 secret: [ 1. -2. -3.5 6.7]\n", "INFO:root:Player 0 abs(secret): [1. 2. 3.5 6.69999695]\n", "INFO:root:Player 1 abs(secret): [1. 2. 3.5 6.69999695]\n", "INFO:root:Player 2 abs(secret): [1. 2. 3.5 6.69999695]\n" ] } ], "source": [ "import logging\n", "\n", "import numpy\n", "\n", "from cicada.additive import AdditiveProtocolSuite\n", "from cicada.communicator import SocketCommunicator\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", " secret = numpy.array([1, -2, -3.5, 6.7]) if communicator.rank == 1 else None\n", " log.info(f\"Player {communicator.rank} secret: {secret}\", src=1) \n", " \n", " secret_share = protocol.share(src=1, secret=secret, shape=(4,))\n", " absolute_share = protocol.absolute(secret_share)\n", " absolute = protocol.reveal(absolute_share)\n", " \n", " log.info(f\"Player {communicator.rank} abs(secret): {absolute}\")\n", "\n", "SocketCommunicator.run(world_size=3, fn=main);" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "Note that :math:`6.69999695` is an approximation to the original value, due to the limited precision of the fixed point encoding.\n", "\n", ".. seealso:: :ref:`floor`" ] } ], "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 }