Skip to main content

An interface to libprio

Project description

python-libprio

This module contains a Python wrapper around libprio.

Quickstart

Install the required dependencies via the system package manager and install the package from pip. The shared libraries for msgpack and NSS must be available for the module to initialize properly.

On an image derived from Ubuntu:

docker run -it python:3 bash
apt update && apt install libmsgpackc2 libnss3

On Centos:

docker run -it centos:8 bash
dnf update && dnf install epel-release && dnf install python36 msgpack nss

On macOS:

brew install msgpack nss

To test the package:

pip3 install prio
python3 -c "from prio.libprio import *; Prio_init(); print(PrioPRGSeed_randomize())"

Building from source

Ensure all of the libprio build tools are available, such as scons and clang

python3 -m venv venv
source venv/bin/activate
make install

# run tests
pip install pytest
python -m pytest

To run a coverage report:

pip install pytest-cov
python -m pytest --cov-report=html --cov-report=term --cov=prio tests

Distributing

Binary eggs and wheels are built for Python 3.6, 3.7, and 3.8 for macOS 10.15 and Linux. From the project root, run the following command to distribute the wheels for macOS.

export TWINE_REPOSITORY=testpypi  # optional: for testing
export TWINE_USERNAME=__token__
export TWINE_PASSWORD=<API_KEY>
./scripts/python-dist.sh

Run the docker container for distributing the linux wheels.

docker-compose build libprio-dist
export TWINE_USERNAME=__token__
export TWINE_PASSWORD=<API_KEY>
docker-compose run -e TWINE_USERNAME -e TWINE_PASSWORD libprio-dist

Build Process

There are issues running an installation of the extension module inside of the current directory (i.e. libprio/python). The directory is the first item on the PYTHONPATH. The extension module cannot be referenced until it has been completely packaged, due to the implicit import statement made by the swig wrapper:

from . import _libprio

The directory sturcture of module after running python setup.py build shows the intermediate shared object stored in a directory underneath build.

# make

% tree prio build
prio
├── __init__.py
└── libprio.py
build
├── lib.macosx-10.15-x86_64-3.8
│   └── prio
│       ├── __init__.py
│       ├── _libprio.cpython-38-darwin.so
│       └── libprio.py
└── temp.macosx-10.15-x86_64-3.8
    └── libprio_wrap.o

The package references are valid once installed into the Python site packages.

# python3 -m venv venv
# source venv/bin/activate
# make install

% tree venv/lib/python3.8/site-packages/prio
venv/lib/python3.8/site-packages/prio
├── __init__.py
├── __pycache__
│   ├── __init__.cpython-38.pyc
│   └── libprio.cpython-38.pyc
├── _libprio.cpython-38-darwin.so
└── libprio.py

1 directory, 5 files

See this StackOverflow post for reference.

Multiprocessing on Linux

There are certain pathological behaviors that exibit themselves when run on a specific operating system. Due to the way that NSS is initialized, naive multiprocessing using the %init block and atexit in the SWIG interface file will fail on the multiprocessing test in Linux, but not on macOS. Management of the Prio context must be done by the library user.

=================================== FAILURES ===================================
____________________ test_multiprocessing_encoding_succeeds ____________________
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
  File "/usr/lib64/python3.6/multiprocessing/pool.py", line 119, in worker
    result = (True, func(*args, **kwds))
  File "/usr/lib64/python3.6/multiprocessing/pool.py", line 47, in starmapstar
    return list(itertools.starmap(args[0], args[1]))
  File "/app/python/tests/test_libprio_multiprocessing.py", line 14, in _encode
    for_server_a, for_server_b = PrioClient_encode(cfg, data_items)
  File "/usr/local/lib64/python3.6/site-packages/prio/libprio.py", line 380, in PrioClient_encode
    return _libprio.PrioClient_encode(cfg, data_in)
RuntimeError: PrioClient_encode was not successful.
"""

The above exception was the direct cause of the following exception:

    def test_multiprocessing_encoding_succeeds():
        _, pkA = Keypair_new()
        _, pkB = Keypair_new()
        internal_hex = PublicKey_export_hex(pkA)
        external_hex = PublicKey_export_hex(pkB)

        pool_size = 2
        num_elements = 10

        p = Pool(pool_size)
>       res = p.starmap(_encode, [(internal_hex, external_hex)] * num_elements)

python/tests/test_libprio_multiprocessing.py:28:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/usr/lib64/python3.6/multiprocessing/pool.py:274: in starmap
    return self._map_async(func, iterable, starmapstar, chunksize).get()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <multiprocessing.pool.MapResult object at 0x7f7a86db0048>, timeout = None

    def get(self, timeout=None):
        self.wait(timeout)
        if not self.ready():
            raise TimeoutError
        if self._success:
            return self._value
        else:
>           raise self._value
E           RuntimeError: PrioClient_encode was not successful.

/usr/lib64/python3.6/multiprocessing/pool.py:644: RuntimeError
=========================== short test summary info ============================
FAILED python/tests/test_libprio_multiprocessing.py::test_multiprocessing_encoding_succeeds
========================= 1 failed, 20 passed in 0.49s =========================

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

prio-1.1-cp39-cp39-manylinux1_x86_64.whl (139.7 kB view details)

Uploaded CPython 3.9

prio-1.1-cp38-cp38-manylinux1_x86_64.whl (142.6 kB view details)

Uploaded CPython 3.8

prio-1.1-cp38-cp38-macosx_10_15_x86_64.whl (69.7 kB view details)

Uploaded CPython 3.8 macOS 10.15+ x86-64

prio-1.1-cp37-cp37m-manylinux1_x86_64.whl (138.1 kB view details)

Uploaded CPython 3.7m

prio-1.1-cp37-cp37m-macosx_10_15_x86_64.whl (69.5 kB view details)

Uploaded CPython 3.7m macOS 10.15+ x86-64

prio-1.1-cp36-cp36m-manylinux1_x86_64.whl (137.1 kB view details)

Uploaded CPython 3.6m

prio-1.1-cp36-cp36m-macosx_10_15_x86_64.whl (69.5 kB view details)

Uploaded CPython 3.6m macOS 10.15+ x86-64

File details

Details for the file prio-1.1-cp39-cp39-manylinux1_x86_64.whl.

File metadata

  • Download URL: prio-1.1-cp39-cp39-manylinux1_x86_64.whl
  • Upload date:
  • Size: 139.7 kB
  • Tags: CPython 3.9
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.4

File hashes

Hashes for prio-1.1-cp39-cp39-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 fae7ced7e65b7f33a37064ed31a18493f670ed9d96c6c88f7d201681b172edc4
MD5 937a2a0cfd2d7f9e402c147cdf4f6dd1
BLAKE2b-256 44cee31561116a7962d1df6729c29b26cbb3b44c295ec061e50baf3657e08978

See more details on using hashes here.

File details

Details for the file prio-1.1-cp38-cp38-manylinux1_x86_64.whl.

File metadata

  • Download URL: prio-1.1-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 142.6 kB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.4

File hashes

Hashes for prio-1.1-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 62c6163b290055338a487da0ad458bc3362b1eaffcacc42dc049c4493712eb38
MD5 1b24b79a64984e6ac7a642dcdb7d7db8
BLAKE2b-256 f869efd8db3ec8943ece7898d074692963b0a1bbb762e8ceebd65ba1f2461899

See more details on using hashes here.

File details

Details for the file prio-1.1-cp38-cp38-macosx_10_15_x86_64.whl.

File metadata

  • Download URL: prio-1.1-cp38-cp38-macosx_10_15_x86_64.whl
  • Upload date:
  • Size: 69.7 kB
  • Tags: CPython 3.8, macOS 10.15+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.3

File hashes

Hashes for prio-1.1-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 ff7f5a919796207562f788a9fd1459245f17a10ce540b2ee80b9ec044f5c5caf
MD5 0fc160b05f64ee7ab34099d539d3288c
BLAKE2b-256 c23b7fc44c00ea56a071680fdfda15987164d1551098fdfb00be1d949dcaf006

See more details on using hashes here.

File details

Details for the file prio-1.1-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

  • Download URL: prio-1.1-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 138.1 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.4

File hashes

Hashes for prio-1.1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ddd99c5d1ef146045917e9837c29a852833d435e41b5f9a0ed775cccaa8502fc
MD5 d56f21daa7c046e2d91bccd8a081f762
BLAKE2b-256 6d8b69b786893dd846fac41ab9ffbbe3e1a9df1ae9d56195e8ac6af07593dbf1

See more details on using hashes here.

File details

Details for the file prio-1.1-cp37-cp37m-macosx_10_15_x86_64.whl.

File metadata

  • Download URL: prio-1.1-cp37-cp37m-macosx_10_15_x86_64.whl
  • Upload date:
  • Size: 69.5 kB
  • Tags: CPython 3.7m, macOS 10.15+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.3

File hashes

Hashes for prio-1.1-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 6c46a06da0beee4ee84b8371d0edaaa40061d666ccba4fcaf1339479660e39d2
MD5 5761a058523e6d7846dcea157f99f0be
BLAKE2b-256 e2a451d57cf576c99e19f3050dc7d057199079bf5e48725fc8c9b141b2c9a137

See more details on using hashes here.

File details

Details for the file prio-1.1-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

  • Download URL: prio-1.1-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 137.1 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.4

File hashes

Hashes for prio-1.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 7d76e244cd821108665a9ed1c74dc0124c51761c88c612f0736e4b3edf3a7f50
MD5 f718a5333d82fb2637028aedbb7a3c45
BLAKE2b-256 06e4c770f4201c77533f17f0552354d8a06ee1c2b7a5671236690e358a806282

See more details on using hashes here.

File details

Details for the file prio-1.1-cp36-cp36m-macosx_10_15_x86_64.whl.

File metadata

  • Download URL: prio-1.1-cp36-cp36m-macosx_10_15_x86_64.whl
  • Upload date:
  • Size: 69.5 kB
  • Tags: CPython 3.6m, macOS 10.15+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.3

File hashes

Hashes for prio-1.1-cp36-cp36m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 e9336660f7eae368cff7b59bd7192456a9ce58daa0f3c19cb272dbb734b5ee1c
MD5 fd7437ba8b37858e6a22f4f1f6070504
BLAKE2b-256 caec298b94d1c89b6386058ce04ca8c9064f31101376c8389f76de7402198a05

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page