项目作者: TNO-MPC

项目描述 :
TNO MPC Lab - Protocols - Secure Comparison
高级语言: Python
项目地址: git://github.com/TNO-MPC/protocols.secure_comparison.git
创建时间: 2021-03-31T14:42:31Z
项目社区:https://github.com/TNO-MPC/protocols.secure_comparison

开源协议:Apache License 2.0

下载


TNO PET Lab - secure Multi-Party Computation (MPC) - Protocols - Secure Comparison

Implementation of a secure comparison protocol based on the DGK encryption
scheme. The implementation follows the description of the paper
Improving the DGK comparison
protocol
, a paper by Thijs Veugen
improving upon the secure comparison protocol by Damgård, Geisler, and
Krøigaard
.

Note that a correction was published in Correction to “Improving the DGK
comparison protocol”
, which is
incorporated in the implementation.

PET Lab

The TNO PET Lab consists of generic software components, procedures, and functionalities developed and maintained on a regular basis to facilitate and aid in the development of PET solutions. The lab is a cross-project initiative allowing us to integrate and reuse previously developed PET functionalities to boost the development of new protocols and solutions.

The package tno.mpc.protocols.secure_comparison is part of the TNO Python Toolbox.

Limitations in (end-)use: the content of this software package may solely be used for applications that comply with international export control laws.
This implementation of cryptographic software has not been audited. Use at your own risk.

Documentation

Documentation of the tno.mpc.protocols.secure_comparison package can be found
here.

Install

Easily install the tno.mpc.protocols.secure_comparison package using pip:

  1. $ python -m pip install tno.mpc.protocols.secure_comparison

Note: If you are cloning the repository and wish to edit the source code, be
sure to install the package in editable mode:

  1. $ python -m pip install -e 'tno.mpc.protocols.secure_comparison'

If you wish to run the tests you can use:

  1. $ python -m pip install 'tno.mpc.protocols.secure_comparison[tests]'

Note: A significant performance improvement can be achieved by installing the GMPY2 library.

  1. $ python -m pip install 'tno.mpc.protocols.secure_comparison[gmpy]'

Usage

Usage example:

  1. import asyncio
  2. from tno.mpc.communication import Pool
  3. from tno.mpc.encryption_schemes.dgk import DGK
  4. from tno.mpc.encryption_schemes.paillier import Paillier
  5. from tno.mpc.encryption_schemes.utils import next_prime
  6. from tno.mpc.protocols.secure_comparison import Initiator, KeyHolder
  7. async def run_protocol() -> None:
  8. taskA = asyncio.create_task(alice.perform_secure_comparison(x_enc, y_enc))
  9. taskB = asyncio.create_task(bob.perform_secure_comparison())
  10. x_leq_y_enc, _ = await asyncio.gather(*[taskA, taskB])
  11. x_leq_y = scheme_paillier.decrypt(x_leq_y_enc)
  12. assert x_leq_y == 1
  13. if __name__ == "__main__":
  14. # Set maximum bit length
  15. l = 16
  16. # Setup the Paillier scheme
  17. scheme_paillier = Paillier.from_security_parameter(key_length=2048)
  18. # Setup the DGK scheme. This may take up to a minute.
  19. u = next_prime((1 << (l + 2)))
  20. scheme_dgk = DGK.from_security_parameter(
  21. v_bits=160, n_bits=2048, u=u, full_decryption=False
  22. )
  23. # Setup communication pools
  24. pool_alice = Pool()
  25. pool_alice.add_http_server(8040)
  26. pool_alice.add_http_client("keyholder", "localhost", 8041)
  27. pool_bob = Pool()
  28. pool_bob.add_http_server(8041)
  29. pool_bob.add_http_client("initiator", "localhost", 8040)
  30. # Encrypt two numbers (x,y) for the protocol and set the maximum bit_length (l)
  31. x = 23
  32. y = 42
  33. x_enc = scheme_paillier.unsafe_encrypt(x)
  34. y_enc = scheme_paillier.unsafe_encrypt(y)
  35. alice = Initiator(l, communicator=pool_alice, other_party="keyholder")
  36. bob = KeyHolder(
  37. l,
  38. communicator=pool_bob,
  39. other_party="initiator",
  40. scheme_paillier=scheme_paillier,
  41. scheme_dgk=scheme_dgk,
  42. )
  43. # Run entire protocol interactively:
  44. loop = asyncio.get_event_loop()
  45. loop.run_until_complete(run_protocol())
  46. # Or execute the protocol steps without interaction
  47. z_enc, r = alice.step_1(x_enc, y_enc, l, scheme_paillier)
  48. z, beta = bob.step_2(z_enc, l, scheme_paillier)
  49. alpha = alice.step_3(r, l)
  50. d_enc = bob.step_4a(z, scheme_dgk, scheme_paillier, l)
  51. beta_is_enc = bob.step_4b(beta, l, scheme_dgk)
  52. d_enc = alice.step_4c(d_enc, r, scheme_dgk, scheme_paillier)
  53. alpha_is_xor_beta_is_enc = alice.step_4d(alpha, beta_is_enc)
  54. w_is_enc, alpha_tilde = alice.step_4e(
  55. r, alpha, alpha_is_xor_beta_is_enc, d_enc, scheme_paillier
  56. )
  57. w_is_enc = alice.step_4f(w_is_enc)
  58. s, delta_a = alice.step_4g()
  59. c_is_enc = alice.step_4h(
  60. s, alpha, alpha_tilde, d_enc, beta_is_enc, w_is_enc, delta_a, scheme_dgk
  61. )
  62. c_is_enc = alice.step_4i(c_is_enc, scheme_dgk)
  63. delta_b = bob.step_4j(c_is_enc, scheme_dgk)
  64. zeta_1_enc, zeta_2_enc, delta_b_enc = bob.step_5(z, l, delta_b, scheme_paillier)
  65. beta_lt_alpha_enc = alice.step_6(delta_a, delta_b_enc)
  66. x_leq_y_enc = alice.step_7(
  67. zeta_1_enc, zeta_2_enc, r, l, beta_lt_alpha_enc, scheme_paillier
  68. )
  69. x_leq_y = scheme_paillier.decrypt(x_leq_y_enc)
  70. assert x_leq_y == 1
  71. # Shut down encryption schemes (optional but recommended)
  72. alice.scheme_paillier.shut_down()
  73. alice.scheme_dgk.shut_down()
  74. bob.scheme_paillier.shut_down()
  75. bob.scheme_dgk.shut_down()

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).