Skip to content

Python Bindings

Claudio Lucisano edited this page Sep 22, 2026 · 1 revision

Python Bindings

bindings/python wraps the crypto box API in a Cython extension called pyopenabe.

Platforms: Linux and macOS only. setup.py exits on any other platform, so the bindings do not build on Windows/MSYS2 yet. CI builds and runs them on Linux.

Build

Build the C++ library first (see Getting Started), then:

. ./env                                   # setup.py refuses to run without ZROOT
python3 -m venv .venv && . .venv/bin/activate
pip install -r bindings/python/requirements.txt setuptools
cd bindings/python
python3 setup.py build_ext --inplace
python3 test.py

This is what CI does. The older make -C bindings path relies on virtualenv and setup.py bdist, and is not tested.

API

import pyopenabe

openabe = pyopenabe.PyOpenABE()
Factory Returns Mirrors
openabe.CreateABEContext("CP-ABE" or "KP-ABE") PyABEContext OpenABECryptoContext
openabe.CreatePKEContext() PyPKEContext OpenPKEContext (NIST P-256)
openabe.CreatePKSIGContext() PyPKSIGContext OpenPKSIGContext (NIST P-256)

openabe.getSchemeTypes() lists the scheme IDs the bindings know about.

The methods keep the C++ names but return values instead of filling output parameters.

ABE

cpabe = openabe.CreateABEContext("CP-ABE")
cpabe.generateParams()
cpabe.keygen("|two|three|", "alice")

pt = b"hello world!"                                  # plaintexts are bytes
ct = cpabe.encrypt("((one or two) and three)", pt)
assert cpabe.decrypt("alice", ct) == pt

# moving parameters and keys around
mpk = cpabe.exportPublicParams()
msk = cpabe.exportSecretParams()
uk  = cpabe.exportUserKey("alice")

other = openabe.CreateABEContext("CP-ABE")
other.importPublicParams(mpk)
other.importSecretParams(msk)
other.importUserKey("alice", uk)

PKE and signatures

pke = openabe.CreatePKEContext()
pke.keygen("user1")
ct = pke.encrypt("user1", b"hello")
assert pke.decrypt("user1", ct) == b"hello"

sig_ctx = openabe.CreatePKSIGContext()
sig_ctx.keygen("user2")
sig = sig_ctx.sign("user2", b"hello")
assert sig_ctx.verify("user2", b"hello", sig)

Errors

Failures raise pyopenabe.PyOpenABEError. For example, decrypt raises Failed to decrypt! when the key does not satisfy the policy, so wrap calls in try/except rather than checking return values.

  • Strings are accepted as str (encoded as UTF-8) or bytes. Anything else raises PyOpenABEError.
  • The PKE decrypt failure message currently reads Failed to encrypt. It is a known copy-paste slip in the bindings.

Clone this wiki locally