Interactive Programs
We define an interactive Cicada program as one that prompts a human for secret input to be used in a computation. Because handling secret interactive input can be tricky, Cicada includes the cicada.interactive.secret_input()
function, which prompts one player for input from a console, which could then be secret-shared with the group. You can use it for simple demos and as a starting-point for more sophisticated interaction. Here are some examples:
Tip
These programs can’t be run in a notebook, they need to be run in separate windows using the cicada command … see Running Cicada Programs for some helpful examples of how to run interactive programs.
Here’s a minimal program that prompts player 0 for a secret. Note that the startup timeout for the communicator has been increased to five minutes (300 seconds) to allow enough time to manually start all of the player processes:
import logging
from cicada.additive import AdditiveProtocolSuite
from cicada.communicator import SocketCommunicator
from cicada.interactive import secret_input
from cicada.logging import Logger
logging.basicConfig(level=logging.INFO)
with SocketCommunicator.connect(startup_timeout=300) as communicator:
log = Logger(logging.getLogger(), communicator)
protocol = AdditiveProtocolSuite(communicator)
secret = secret_input(communicator=communicator, src=0)
log.info(f"Player {communicator.rank} secret: {secret}")
secret_share = protocol.share(src=0, secret=secret, shape=())
log.info(f"Player {communicator.rank} share: {secret_share}")
In this example, every player provides a secret, and the group computes the sum:
import logging
import numpy
from cicada.additive import AdditiveProtocolSuite
from cicada.communicator import SocketCommunicator
from cicada.interactive import secret_input
from cicada.logging import Logger
logging.basicConfig(level=logging.INFO, format="{message}", style="{")
with SocketCommunicator.connect(startup_timeout=300) as communicator:
log = Logger(logging.getLogger(), communicator)
protocol = AdditiveProtocolSuite(communicator)
total = protocol.share(src=0, secret=numpy.zeros(shape=()), shape=())
for i in range(communicator.world_size):
secret = secret_input(communicator=communicator, src=i)
share = protocol.share(src=i, secret=secret, shape=())
total = protocol.add(total, share)
total = protocol.reveal(total)
log.info(f"Player {communicator.rank} total: {total}")
Finally, the following is an interactive version of the millionaire’s dilemma … every player provides a secret, and the group determines which person entered the largest value:
import logging
import numpy
from cicada.additive import AdditiveProtocolSuite
from cicada.communicator import SocketCommunicator
from cicada.encoding import Boolean
from cicada.interactive import secret_input
from cicada.logging import Logger
logging.basicConfig(level=logging.INFO, format="{message}", style="{")
with SocketCommunicator.connect(startup_timeout=300) as communicator:
log = Logger(logging.getLogger(), communicator)
protocol = AdditiveProtocolSuite(communicator)
winner = None
winning_share = protocol.share(src=0, secret=numpy.zeros(shape=()), shape=())
for rank in communicator.ranks:
fortune = secret_input(communicator=communicator, src=rank, prompt=f"Player {communicator.rank} fortune: ")
fortune_share = protocol.share(src=rank, secret=fortune, shape=())
less_share = protocol.less(fortune_share, winning_share)
less = protocol.reveal(less_share, encoding=Boolean())
if not less:
winner = rank
winning_share = fortune_share
log.info(f"Winner: player {winner}")