Simple implementation of Shamir's Secret Sharing scheme
Project description
Shamir's Secret Sharing — Python Implementation
A lightweight and robust Python implementation of Shamir’s Secret Sharing Scheme (SSS). This cryptographic technique splits a secret into multiple shares, requiring only a threshold number of shares to reconstruct the original secret. Ideal for secure key management and distributed systems.
Features
- Pure Python with minimal dependencies (
pycryptodome) - Generate any number of secure shares with a configurable threshold
- Reconstruct secrets using only the threshold number of shares
- Support for both file-based and variable-based workflows
- Human-readable share export/import (JSON + Base64)
- Simple and intuitive API
- MIT License for flexible use
Installation
Install the package via PyPI:
pip install shamir-lbodlev
Usage
Importing the Package
from shamir import Shamir
Example 1: File-Based Workflow
Splitting a Secret
Create shares for a secret and export them to files.
from shamir import Shamir
# Initialize with secret, total shares (n), and threshold (k)
shamir = Shamir(secret=b"My secret message", n=5, k=3)
# Export public parameters and shares
shamir.export_public("public.json")
shamir.export_shares("share{}.dat") # Creates share1.dat, share2.dat, ...
This generates:
public.json: Contains public data (primep, total sharesn, thresholdk)share1.dat,share2.dat, ...: Base64-encoded share files
Reconstructing the Secret
Recover the secret using at least k shares.
from shamir import Shamir
# Initialize recovery instance
recoverer = Shamir()
# Load public parameters and at least 3 shares
recoverer.load_public("public.json")
recoverer.load_shares("share{}.dat", indexes=[1, 3, 5])
# Recover the secret
secret = recoverer.recover()
print(secret.decode()) # Output: My secret message
Example 2: Variable-Based Workflow
Splitting a Secret
Create shares and store them in variables.
from shamir import Shamir
# Initialize with secret, total shares (n), and threshold (k)
shamir = Shamir(secret=b"My secret message", n=5, k=3)
# Get public parameters and shares
public_data = shamir.get_public()
shares = shamir.get_shares()
Reconstructing the Secret
Recover the secret using variables.
from shamir import Shamir
# Initialize recovery instance
recoverer = Shamir()
# Set public parameters and at least 3 shares
recoverer.set_public(public_data)
recoverer.set_shares(shares[:3]) # Use first 3 shares
# Recover the secret
secret = recoverer.recover()
print(secret.decode()) # Output: My secret message
API Reference
Shamir(secret: bytes | None = None, n: int | None = None, k: int | None = None)
Initializes a Shamir instance for splitting or recovering a secret.
| Parameter | Type | Description |
|---|---|---|
secret |
bytes, optional |
The secret to split (as bytes). |
n |
int, optional |
Total number of shares to generate. |
k |
int, optional |
Minimum number of shares needed to reconstruct the secret. |
Raises: ValueError if k > n.
recover() -> bytes
Reconstructs and returns the secret as bytes. Requires public parameters and at least k shares to be loaded.
Example:
secret = recoverer.recover()
print(secret.decode())
export_public(filename: str) -> None
Exports public parameters (p, n, k) to a JSON file.
Example:
shamir.export_public("public.json")
load_public(filename: str) -> None
Loads public parameters from a JSON file.
Example:
recoverer.load_public("public.json")
get_public() -> dict
Returns public parameters (p, k) as a dictionary.
Example:
public_data = shamir.get_public()
# Returns: {'p': <prime>, 'k': <threshold>}
set_public(public_data: dict) -> None
Sets public parameters from a dictionary. Requires keys p (prime) and k (threshold).
Raises: ValueError if p or k is missing.
Example:
recoverer.set_public({'p': 123456789, 'k': 3})
export_shares(template: str) -> None
Exports shares to files using a filename template (e.g., share{}.dat).
Example:
shamir.export_shares("share{}.dat") # Creates share1.dat, share2.dat, ...
load_shares(template: str, indexes: list[int]) -> None
Loads shares from files based on a template and a list of indexes. If more than k shares are provided, only the first k are used.
Example:
recoverer.load_shares("share{}.dat", indexes=[1, 3, 5])
get_shares() -> list
Returns the list of generated shares.
Example:
shares = shamir.get_shares()
set_shares(shares: list) -> None
Sets shares from a list of Base64-encoded strings.
Example:
recoverer.set_shares(shares[:3]) # Use first 3 shares
Security Notes
- Shares are generated using a cryptographically secure random number generator (
secrets.token_bytes). - The finite field is defined by a large prime (
p) to ensure security. - Ensure shares are stored securely, as any
kshares can reconstruct the secret. - The implementation uses
pycryptodomefor secure prime generation and number conversions.
How It Works
- Splitting: The secret is converted to an integer and used as the constant term of a random polynomial of degree
k-1in a finite field defined by a large primep. Shares are points(x, y)on this polynomial. - Reconstruction: Using Lagrange interpolation modulo
p, the secret is recovered from at leastkshares. - Storage: Shares can be exported to files or handled as variables, with public parameters stored separately.
👤 Author
[Your Name and Details Here]
License
This project is licensed under the MIT License. See the LICENSE file for details.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file shamir_lbodlev-0.1.6.tar.gz.
File metadata
- Download URL: shamir_lbodlev-0.1.6.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
56e88e20d4b6e14b7f5bb6b394f9f1740fb6c881c7a96232d2cce5b9d23909b9
|
|
| MD5 |
960960f15ba985427ed0163c0769f5cf
|
|
| BLAKE2b-256 |
66b458ad8587401d3d8d1f57d79fc24d2c1491f79ad9b9cca299386f78d040b7
|
File details
Details for the file shamir_lbodlev-0.1.6-py3-none-any.whl.
File metadata
- Download URL: shamir_lbodlev-0.1.6-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e4f68d82c1265a8b64b311dbd97a52d0050d540af40e0bfc7c70735ea1648ca5
|
|
| MD5 |
596e9afe76d4d927d81e883fe9864a83
|
|
| BLAKE2b-256 |
d77f3c98cd2000c87f7504195f9a88d86539da1faf20ac468fb49d6887da5760
|