Python library for working with circuit definitions represented using the Bristol Fashion.
Project description
Bristol Fashion Circuit Library (BFCL) for working with circuit definitions represented using the Bristol Fashion.
Purpose
This library includes data structures and associated methods for working with logical circuits typically used in secure multi-party computation (MPC) applications. The data structures follow in their organization the Bristol Fashion format, extrapolating and generalizing where necessary in order to support a wider variety of features and operations.
Package Installation and Usage
The package is available on PyPI:
python -m pip install bfcl
The library can be imported in the usual way:
import bfcl from bfcl import *
This library makes it possible to parse a circuit definition that conforms to the Bristol Fashion syntax:
>>> ss = ['7 36', '2 4 4', '1 1'] >>> ss += ['2 1 0 1 15 AND', '2 1 2 3 16 AND'] >>> ss += ['2 1 15 16 8 AND', '2 1 4 5 22 AND'] >>> ss += ['2 1 6 7 23 AND', '2 1 22 23 9 AND'] >>> ss += ['2 1 8 9 35 AND'] >>> c = circuit('\n'.join(ss))
A string representation that conforms to the Bristol Fashion syntax can be emitted:
>>> for line in c.emit().split('\n'): ... print(line) ... 7 36 2 4 4 1 1 2 1 0 1 15 AND 2 1 2 3 16 AND 2 1 15 16 8 AND 2 1 4 5 22 AND 2 1 6 7 23 AND 2 1 22 23 9 AND 2 1 8 9 35 AND
It is possible to evaluate a circuit on a sequence of input bit vectors. The circuit defined in the example above takes two 4-bit input vectors and returns the logical conjunction of all the bits. In the example below, it is evaluated on a few pairs of input bit vectors. The result is organized into a list of output bit vectors according to the original circuit definition (in the example below, the result consists of only a single output bit vector that contains a single bit):
>>> c.evaluate([[1, 0, 1, 1], [1, 1, 1, 0]]) [[0]] >>> c.evaluate([[1, 1, 1, 1], [1, 1, 1, 1]]) [[1]]
As an alternative to using a string representation to define a circuit, it is also possible to construct a circuit using the circuit library. In the example below, the constructor for the circuit class found in the bfcl library is applied to an object built using the classes and methods exported by the circuit library (note the use of a synonym to avoid a conflict with the circuit class defined in the bfcl library):
>>> import circuit as circuit_ >>> c = circuit_.circuit() >>> g0 = c.gate(circuit_.op.id_, is_input=True) >>> g1 = c.gate(circuit_.op.id_, is_input=True) >>> g2 = c.gate(circuit_.op.and_, [g0, g1]) >>> g3 = c.gate(circuit_.op.id_, [g2], is_output=True) >>> circuit(c).emit().split('\n') ['2 4', '1 2', '1 1', '2 1 0 1 2 AND', '1 1 2 3 LID']
Documentation
The documentation can be generated automatically from the source files using Sphinx:
cd docs python -m pip install -r requirements.txt sphinx-apidoc -f -E --templatedir=_templates -o _source .. ../setup.py && make html
Testing and Conventions
All unit tests are executed and their coverage is measured when using pytest (see setup.cfg for configuration details):
python -m pip install pytest pytest-cov python -m pytest
Alternatively, all unit tests are included in the module itself and can be executed using doctest:
python bfcl/bfcl.py -v
Style conventions are enforced using Pylint:
python -m pip install pylint python -m pylint bfcl
Contributions
In order to contribute to the source code, open an issue or submit a pull request on the GitHub page for this library.
Versioning
Beginning with version 0.2.0, the version number format for this library and the changes to the library associated with version number increments conform with Semantic Versioning 2.0.0.
Publishing
This library can be published as a package on PyPI by a package maintainer. Install the wheel package, remove any old build/distribution files, and package the source into a distribution archive:
python -m pip install wheel rm -rf dist *.egg-info python setup.py sdist bdist_wheel
Next, install the twine package and upload the package distribution archive to PyPI:
python -m pip install twine python -m twine upload dist/*
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file bfcl-1.0.1.tar.gz
.
File metadata
- Download URL: bfcl-1.0.1.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.64.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a8fb58bdc8eeb323b459156bf3d7d070d8339c13a129a43ebaab7317ec58e3d3 |
|
MD5 | fcaed3b3883279e92d9e5cb91307ba07 |
|
BLAKE2b-256 | ded8a0947cb2894f02ecfc35866f07ca120149c3dcec592c94eef7859f59c047 |
File details
Details for the file bfcl-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: bfcl-1.0.1-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.64.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5c1e49d92913d00d000ba38ea36597236de601a4caf6abe089bb4b20e916f3ba |
|
MD5 | 64715f72a8b03062f979feeec41c6850 |
|
BLAKE2b-256 | dbadc28f283ba9ebd79ea58d17f3a64432eac284fff224e7ae264ba0e56e67ba |