Skip to main content

fips203 Python module

This Python module provides an implementation of FIPS 203, the Module-Lattice-based Key Encapsulation Mechanism Standard.

The underlying mechanism is intended to offer "post-quantum" key establishment.

Example

The following example shows using the standard ML-KEM algorithm to produce identical 32-byte shared secrets:

from fips203 import ML_KEM_512

(encapsulation_key, decapsulation_key) = ML_KEM_512.keygen()
(ciphertext, shared_secret_1) = encapsulation_key.encaps()
shared_secret_2 = decapsulation_key.decaps(ciphertext)
assert(shared_secret_1 == shared_secret_2)

Key generation can also be done deterministically, by passing a SEED_SIZE-byte seed (the concatenation of d and z) to keygen:

from fips203 import ML_KEM_512, Seed

seed1 = Seed()  # Generate a random seed
(ek1, dk1) = ML_KEM_512.keygen(seed1)

seed2 = Seed(b'\x00'*ML_KEM_512.SEED_SIZE)  # This seed is clearly not a secret!
(ek2, dk2) = ML_KEM_512.keygen(seed2)

Encapsulation keys, decapsulation keys, seeds, and ciphertexts can all be serialized by accessing them as bytes, and deserialized by initializing them with the appropriate size bytes object.

A serialization example:

from fips203 import ML_KEM_768, Seed

seed = Seed()
(ek,dk) = ML_KEM_768.keygen(seed)
with open('encapskey.bin', 'wb') as f:
    f.write(bytes(ek))
with open('decapskey.bin', 'wb') as f:
    f.write(bytes(dk))
with open('seed.bin', 'wb') as f:
    f.write(bytes(seed))

A deserialization example, followed by use:

import fips203

with open('encapskey.bin', 'rb') as f:
    ekdata = f.read()

ek = fips203.EncapsulationKey(ekdata)
(ct, ss) = ek.encaps()

The expected sizes (in bytes) of the different objects in each parameter set can be accessed with EK_SIZE, DK_SIZE, CT_SIZE, SEED_SIZE, and SS_SIZE:

from fips203 import ML_KEM_768

print(f"ML-KEM-768 Ciphertext size (in bytes) is {ML_KEM_768.CT_SIZE}")

Implementation Notes

This is a wrapper around libfips203, built from the Rust fips203-ffi crate.

If that library is not installed in the expected path for libraries on your system, importing this module will fail. For in-tree tests, set FIPS203_PYTHON_TESTING_LIBRARY to the built libfips203.so (libfips203.dylib on macOS). On macOS, Python searches DYLD_LIBRARY_PATH, then ~/lib, /usr/local/lib, /lib and /usr/lib, so a library installed elsewhere, such as MacPorts' /opt/local/lib or Homebrew's /opt/homebrew/lib, also needs FIPS203_PYTHON_TESTING_LIBRARY.

The NIST tests under test/ read vectors from ../../tests/nist_vectors in a source checkout, or from the directory named by FIPS203_NIST_VECTORS. The published fips203 crate includes the keyGen vectors, under tests/nist_vectors/.

This module should have reasonable type annotations and docstrings for the public interface. If you discover a problem with type annotations, or see a way that this kind of documentation could be improved, please report it!

See Also

Bug Reporting

Please report issues at https://github.com/integritychain/fips203/issues

Metadata

Release files for fips203 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fips203 0.5.0
File Size Uploaded Provenance Actions
fips203-0.5.0.tar.gz 5.8 kB Without provenance Details for fips203-0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fips203 0.5.0
File Interpreter ABI Platform Actions
fips203-0.5.0-py3-none-any.whl Python 3 none any Details for fips203-0.5.0-py3-none-any.whl

Total release size: 12.6 kB

Release files / fips203-0.5.0.tar.gz

Download URL fips203-0.5.0.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
0d0c97325eecf81e3928c249280d1b8a18774909169b275868e1a74f0f7c9383
BLAKE2b-256 checksum
How to use checksums
5c75f399c5a2d8a63d3526fc17e6070ddec48dbd34a166fe68cb7f40973e0a53
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.8

Release files / fips203-0.5.0-py3-none-any.whl

Download URL fips203-0.5.0-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b0abe2d565c4830b630d1c011890e35193edc8eadbd62749fac1830b74f9f8b7
BLAKE2b-256 checksum
How to use checksums
1ec2b0679ac5ba4afa682cc61918f08e8587e04082fa0dd7c6188c24f4de0583
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.8

Security for fips203 0.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page