TNO Protocols¶
Implementation of the secure comparison protocol.
This building block is included in the TNO MPC Python Toolbox.
Note:¶
A significant performance improvement for some algorithms can be achieved by installing the GMPY2 library. The protocol requires both a Paillier and DGK cryptosystem.
Install¶
Install the tno.mpc.protocols 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.protocols.secure_comparison --extra-index-url https://__token__:<personal_access_token>@ci.tno.nl/gitlab/api/v4/groups/3209/-/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.protocols.secure_comparison --extra-index-url https://<GITLAB_DEPLOY_TOKEN>:<GITLAB_DEPLOY_PASSWORD>@ci.tno.nl/gitlab/api/v4/groups/3209/-/packages/pypi/simple
Dockerfile¶
FROM python:3.8
ARG GITLAB_DEPLOY_TOKEN
ARG GITLAB_DEPLOY_PASSWORD
RUN python -m pip install tno.mpc.protocols.secure_comparison --extra-index-url https://$GITLAB_DEPLOY_TOKEN:$GITLAB_DEPLOY_PASSWORD@ci.tno.nl/gitlab/api/v4/groups/3209/-/packages/pypi/simple
Usage¶
Usage example:
import asyncio
from tno.mpc.communication import Pool
from tno.mpc.encryption_schemes.dgk import DGK
from tno.mpc.encryption_schemes.paillier import Paillier
from tno.mpc.encryption_schemes.utils import next_prime
from tno.mpc.protocols.secure_comparison import Initiator, KeyHolder
async def run_protocol():
taskA = asyncio.create_task(alice.perform_secure_comparison(x_enc, y_enc))
taskB = asyncio.create_task(bob.perform_secure_comparison())
x_leq_y_enc, _ = await asyncio.gather(*[taskA, taskB])
x_leq_y = scheme_paillier.decrypt(x_leq_y_enc)
assert x_leq_y == 1
if __name__ == "__main__":
# Set maximum bit length
l = 16
# Setup the Paillier scheme
scheme_paillier = Paillier.from_security_parameter(key_length=2048)
# Setup the DGK scheme
u = next_prime((1 << (l + 2)))
scheme_dgk = DGK.from_security_parameter(
v_bits=160, n_bits=2048, u=u, full_decryption=False
)
# Setup communication pools
pool_alice = Pool()
pool_alice.add_http_server(8040)
pool_alice.add_http_client("keyholder", "localhost", 8041)
pool_bob = Pool()
pool_bob.add_http_server(8041)
pool_bob.add_http_client("initiator", "localhost", 8040)
# Encrypt two numbers (x,y) for the protocol and set the maximum bit_length (l)
x = 23
y = 42
x_enc = scheme_paillier.encrypt(x)
y_enc = scheme_paillier.encrypt(y)
alice = Initiator(l, communicator=pool_alice, other_party="keyholder")
bob = KeyHolder(
l,
communicator=pool_bob,
other_party="initiator",
scheme_paillier=scheme_paillier,
scheme_dgk=scheme_dgk,
)
# Run entire protocol interactively:
loop = asyncio.get_event_loop()
loop.run_until_complete(run_protocol())
# Or execute the protocol steps without interaction
z_enc, r = alice.step_1(x_enc, y_enc, l, scheme_paillier)
z, beta = bob.step_2(z_enc, l, scheme_paillier)
alpha = alice.step_3(r, l)
d_enc = bob.step_4a(z, scheme_dgk, scheme_paillier, l)
beta_is_enc = bob.step_4b(beta, l, scheme_dgk)
d_enc = alice.step_4c(d_enc, r, scheme_dgk, scheme_paillier)
alpha_is_xor_beta_is_enc = alice.step_4d(alpha, beta_is_enc)
w_is_enc, alpha_tilde = alice.step_4e(
r, alpha, alpha_is_xor_beta_is_enc, d_enc, scheme_paillier
)
w_is_enc = alice.step_4f(w_is_enc)
s, delta_a = alice.step_4g()
c_is_enc = alice.step_4h(
s, alpha, alpha_tilde, d_enc, beta_is_enc, w_is_enc, delta_a, scheme_dgk
)
c_is_enc = alice.step_4i(c_is_enc, scheme_dgk)
delta_b = bob.step_4j(c_is_enc, scheme_dgk)
zeta_1_enc, zeta_2_enc, delta_b_enc = bob.step_5(z, l, delta_b, scheme_paillier)
beta_lt_alpha_enc = alice.step_6(delta_a, delta_b_enc)
x_leq_y_enc = alice.step_7(
zeta_1_enc, zeta_2_enc, r, l, beta_lt_alpha_enc, scheme_paillier
)
x_leq_y = scheme_paillier.decrypt(x_leq_y_enc)
assert x_leq_y == 1
The communicator object is required only when the protocol is ran through perform_secure_comparison
. In that case, one may choose to pass any communicator object that adheres to the tno.mpc.protocols.secure_comparison.Communicator
protocol. An example can be found in the unit tests.
! SAFETY NOTICE ! ENSURE CIPHERTEXTS ARE RANDOMIZED¶
Since version 2.0.0 of tno.mpc.encryption_schemes.paillier
and tno.mpc.encryption_schemes.dgk
, it is possible to (potentially) make protocols more efficient by delaying randomization of ciphertexts. This library always operates in this ‘expert’ mode and therefore several protocol steps yield non-randomized ciphertext outputs. As a consequence, if the user chooses to perform the secure comparison steps manually, she needs to make sure that the resulting ciphertexts are randomized before they are communicated. If the tno.mpc.communication
library is used (or more specifically, the Paillier and DGK serialize methods), then this will be done automatically for you (but warnings might be raised).