Random Number Generation

Here we will demonstrate generating uniform random shared secrets of any shape using AdditiveProtocolSuite. We use an instance of numpy.random.Generator to seed the process. After each secret is generated, we reveal it to the players and log it.

We will initialize the protocol suite with a custom order (251) and a FixedPoint encoding with 4 fractional bits so that the random secrets are small and easy to read in the output.

[1]:
import logging
import sys

import numpy

from cicada.additive import AdditiveProtocolSuite
from cicada.communicator import SocketCommunicator
from cicada.encoding import FixedPoint
from cicada.logging import Logger

logging.basicConfig(level=logging.INFO)

def main(communicator):
    log = Logger(logging.getLogger(), communicator)
    protocol = AdditiveProtocolSuite(communicator, order=251, encoding=FixedPoint(precision=4))
    generator = numpy.random.default_rng(seed=1234)

    for shape in [(), (1,), (2,), (3,)]:
        log.info("*" * 40, src=0)

        random_share = protocol.field_uniform(generator=generator, shape=shape)
        random = protocol.reveal(random_share)
        log.info(f"Player {communicator.rank} random values: {random}")

SocketCommunicator.run(world_size=3, fn=main);
INFO:root:****************************************
INFO:root:Player 0 random values: 3.5
INFO:root:Player 1 random values: 3.5
INFO:root:Player 2 random values: 3.5
INFO:root:****************************************
INFO:root:Player 0 random values: [-1.5]
INFO:root:Player 1 random values: [-1.5]
INFO:root:Player 2 random values: [-1.5]
INFO:root:****************************************
INFO:root:Player 0 random values: [-7.1875  0.875 ]
INFO:root:Player 1 random values: [-7.1875  0.875 ]
INFO:root:Player 2 random values: [-7.1875  0.875 ]
INFO:root:****************************************
INFO:root:Player 0 random values: [-0.75   -7.1875  0.0625]
INFO:root:Player 1 random values: [-0.75   -7.1875  0.0625]
INFO:root:Player 2 random values: [-0.75   -7.1875  0.0625]