Building block: MPyC exponentiation¶
A generic extension to MPyC that allows you to securely compute \(`a^{[x]}`\), where \(`a`\) is a non-negative, plain-text base (floating point or integer) and \(`x`\) is a secure number (SecFxp or SecInt).
This building block is included in the TNO MPC Python Toolbox.
Install¶
Install the tno.mpc.mpyc.exponentiation package using one of the following options.
Personal access token
Deploy tokens
Cloning this repo (developer mode)
Personal access token¶
Generate a personal access token with
read_api
scope. Instruction are found here.Install
python -m pip install tno.mpc.mpyc.exponentiation --extra-index-url https://__token__:<personal_access_token>@ci.tno.nl/gitlab/api/v4/projects/5977/packages/pypi/simple
Deploy tokens¶
Generate a deploy token with
read_package_registry
scope. Instruction are found here.Install
python -m pip install tno.mpc.mpyc.exponentiation --extra-index-url https://<GITLAB_DEPLOY_TOKEN>:<GITLAB_DEPLOY_PASSWORD>@ci.tno.nl/gitlab/api/v4/projects/5977/packages/pypi/simple
Dockerfile¶
FROM python:3.8
ARG GITLAB_DEPLOY_TOKEN
ARG GITLAB_DEPLOY_PASSWORD
RUN python -m pip install tno.mpc.mpyc.exponentiation --extra-index-url https://$GITLAB_DEPLOY_TOKEN:$GITLAB_DEPLOY_PASSWORD@ci.tno.nl/gitlab/api/v4/projects/5977/packages/pypi/simple
Usage¶
Minimal example¶
scripts/example_usage.py
from mpyc.runtime import mpc from tno.mpc.mpyc.exponentiation import secure_pow import math async def main(base=math.e, x=[-1.5, 2.3, 4.5]): async with mpc: stype = mpc.SecFxp() sec_x = [stype(xx, integral=False) for xx in x] if isinstance(x, list) else stype(x) result = secure_pow(base, sec_x) revealed_result = await mpc.output(result) plain_result = [base ** xx for xx in x] if isinstance(x, list) else base ** x print(f"Result of secure exponentiation is {revealed_result}") print(f"In the plain we would have gotten {plain_result}") diff = ( [abs(sec - plain) for sec, plain in zip(revealed_result, plain_result)] if isinstance(x, list) else abs(revealed_result - plain_result) ) print(f"The absolute difference is {diff}") if __name__ == "__main__": mpc.run(main())