Python-based MultiParty Computation

Python-based Multi party computation

How multiparty computation can be built quickly using the PyMPC library?

This document describes how python based Multi Party Computing (MPC) can be built  quickly using the Python MPC library called PyMPC. A sample unanimous voting system is built using this library to demonstrate its key features.

What is MPC

In a secure multi-party computation, several parties jointly compute an agreed function without leaking any information on their inputs. A good example is an election where the final tally is computed *without* revealing any information on the individual who votes.

In an m party computation, Secure MPC ( Multiparty Computation) is tolerant to t corruptions when t is between

m ≥ 1   and 0 ≤ t ≤ (m)/2

PyMPC is one such secure multi-party computation library for Python. It was born in 2018 at the Theory and Practice of Multiparty Computation (TPMPC) 2018 workshop in Aarhus, Denmark.

The core idea was to build a set of Python primitives types and extensions that can be used to convert any computation to the standards of secure MPC.

PyMPC is a small and efficient Python library that provides secure types and overrides common Python operators to work on these types.

A brief overview of PyMPC

Details of PyMPC can be found in this excellent readme: https://mpyc.readthedocs.io/en/latest/

Like most MPC frameworks, PyMPC performs computation on secret shared-values using algorithms such as Shamir’s threshold secret sharing scheme that can withstand passive adversaries of less than ½ of parties involved in computation.

PyMPC has extensions to Python primitives and functional programming operators to store and work on these secure types. In addition to binary, integer and float secure types, it provides support for list and finite group operations. Finite group operations are built-in for a range of groups, particularly for use in threshold cryptography (e.g., Schnorr groups and elliptic curves).

Operator overloading, support for comparison and bitwise operations, and python functional programming operators (e.g. all(), any(), sum(), min(), max(), sorted() , map/reduce ) make implementation of most cryptography related algorithms in Pythonic way. PyMPC also provides secure input and output mechanisms.

PyMPC uses Async IO (async and await-based co-routines)  so that processing can be delegated to independently running co-routines on localhost or a list of hosts provided for distributed computing. These co-routines wait for all parties to join, start computation on secure types, and return the result that has been securely computed.

Example program for unanimous voting

Following simple Python code can do unanimous vote computation for m parties

import sys
from mpyc.runtime import mpc

m = len(mpc.parties)

if m%2 == 0:
    print('Odd number of parties required.')
    sys.exit()

t = m//2

voters = list(range(t+1))  # parties P[0],...,P[t]

if mpc.pid in voters:
    vote = int(sys.argv[1]) if sys.argv[1:] else 1  # default "yes"
else:
    vote = None  # no inputthat

secbit = mpc.SecFld(2)  # secure bits over GF(2)

mpc.run(mpc.start())
votes = mpc.input(secbit(vote), senders=voters)
result = mpc.run(mpc.output(mpc.all(votes), receivers=voters))
mpc.run(mpc.shutdown())

if result is None:  # no output
    print('Thanks for serving as oblivious matchmaker;)')
elif result:
    print(f'Match: unanimous agreement between {t+1} part{"ies" if t else "y"}!')
else:
    print(f'No match: someone disagrees among {t+1} part{"ies" if t else "y"}?')

This program can take yes or no (binaries 1 and 0) votes from any odd number of parties P[0],…,P[t]

By calculating the product of their votes (binaries 0 and 1) and only tell parties if product is 1 (all yes votes ) or 0  (not all votes were ‘Yes’), this program prevents any party that tries to guess any other party’s vote . For extra oblivion and privacy, t parties P[t+1],…,P[2t]  are added to computation. These parties do not provide input and get no result. 

Assuming all computation is done on local host and each party owns a process window, the execution will look like as following running with 6 process windows

Secure bit secbit = mpc.SecFld(2)  is a finite shared security based field with length 2^P, the core of the program is just following two lines.

votes = mpc.input(secbit(vote), senders=voters)
result = mpc.run(mpc.output(mpc.all(votes), receivers=voters))

Which involve encoding votes into secure field and calculating result from each party’s view/input of securebit  via mpc.all(secure bit)

PyMPC Use Cases

Aside from obvious use cases of secure voting and elections, secure multi party computing can be used to build BFT (Byzantine fault tolerance) capable computation engines that are tolerant to corruption from less than ½ of the parties involved.

Implementation of common algorithms like ID3 based decision trees, linear and logistic regression, convolution neural networks, statistical methods like Monte Carlo simulation and Kaplan Meier survival analysis can be implemented and performance verified.

For cryptography,  block ciphers like AES or one-way hashing methods can use PyMPC’s secure lists for quick and verifiable implementation. Here is an example voting where each party will enter a (random) bit v representing a (random) yes/no vote. Each party encrypts its vote v using additively homomorphic ElGamal, hence puts c = (g^u, h^u g^v) as ciphertext for a random nonce u (generated privately by each party). The parties then broadcast their ciphertexts, and multiply everything together to obtain a ciphertext representing the sum of their

votes. Finally, the sum is decrypted, and the total number of “yes” votes is obtain

PyMPC Use Case  in Blockchain

Below is a  use case of PyMPC in blockchain

Tendermint core is a replicated state machine-type blockchain that is tolerant to the failure of the entire network due to malicious attacks. It uses PyMPC to add BFT for  its secure and distributed computation. This link  shows how PyMPC is used to implement all protocols and transactions of Tendermint core.

Sample secure voting implementation

This section describes the actual implementation of secure five (5) party unanimous voting on AWS that was completed for one of the High Plains Computing’s client.

We used JWT tokens for each party to encrypt their vote, transmit over SSL via REST service and then provide inputs to secure MPC hosts running in private networks.

Python-based Multiparty Computation

Key highlights of the above figure:

  1. Five (5) parties encode their vote  1=yes , 0 =No into JWT token and send to web server
  2. Web Server acts as SSL enabled proxy for flask application
  3. Flask decodes using the voter’s public key and invokes unanimous.py for each vote.

PyMPC based unanimous.py computes results and notify all voters if a unanimous vote has been reached or not.

Committed to delivering the best

Thousands of AWS and CNCF-certified Kubernetes solution partners have unique expertise and focus areas. Our focus is on best practices in security, automation, and excellence in Cloud operations.

Please reach out to us if you have any questions.

Social Share :

AWS Inspector vs Guardduty

Both Amazon Inspector and Amazon GuardDuty are services that enhance your cloud security posture. Both…

AWS Cost Optimization Best Practices

Introduction Cloud costs are a daily concern for companies running applications on Amazon Web Services…

Python-based MultiParty Computation

How multiparty computation can be built quickly using the PyMPC library? This document describes how…

Ready to make your business more efficient?