Simple implementation of Shamir's Secret Sharing scheme
Project description
🔐 Shamir Secret Sharing — Python Implementation
A clean and minimal Python implementation of Shamir’s Secret Sharing Scheme (SSS) —
a cryptographic method to divide a secret into multiple parts (shares),
so that only a threshold number of them are required to reconstruct the original secret.
🚀 Features
- Pure Python, minimal dependencies (
pycryptodome) - Generate any number of secure shares
- Reconstruct the secret using only the threshold shares
- Human-readable export/import formats (JSON + Base64)
- Easy-to-use API for both sharing and recovery
📦 Installation
Install the package from PyPI:
pip install shamir-lbodlev
Importing the Package
from Shamir import Shamir
Quick Start Example
1. Split a Secret into Shares
from Shamir import Shamir
# Create shares for a secret message
shamir = Shamir(secret=b"My top secret message", n=5, k=3)
# total of 5 shares with 3 threshold(minimal amount of shares to reconstruct the message)
# Export parameters and shares
shamir.export_public("public.json")
shamir.export_shares("share{}.dat") # Creates share1.dat, share2.dat, ...
This produces:
public.json→ contains public data (prime numberp, total sharesn, thresholdk)share1.dat,share2.dat, ... → Base64-encoded shares
2. Reconstruct the Secret
from Shamir import Shamir
# Initialize recovery instance
recoverer = Shamir()
recoverer.load_public("public.json")
# Load any 3 of the generated shares
recoverer.load_shares("share{}.dat", indexes=[1, 3, 5])
# Recover the secret
secret = recoverer.recover()
print(secret.decode())
API Reference
class Shamir(secret: bytes | None = None, n: int | None = None, k: int | None = None)
Initializes a new instance of the Shamir scheme.
| Parameter | Type | Description |
|---|---|---|
secret |
bytes, optional |
The secret to share (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 shared secret (in bytes). Must be called after loading public parameters and shares.
export_public(filename: str) -> None
Exports public data (p, n, k) in JSON format.
Required for secret reconstruction.
shamir.export_public("public.json")
export_shares(template: str) -> None
Exports all generated shares using a filename template. The share index is automatically inserted into the template.
shamir.export_shares("share{}.dat")
# Produces: share1.dat, share2.dat, share3.dat, ...
load_public(filename: str) -> None
Loads the public parameters from a previously exported JSON file.
shamir.load_public("public.json")
load_shares(template: str, indexes: list[int]) -> None
Loads share files according to a template and list of indexes.
If more than k shares are given, only the first k are used.
shamir.load_shares("share{}.dat", indexes=[1, 2, 5])
Internal Methods
The following methods are used internally and should not be called directly:
__generate_coefs()__generate_shares()__generate_random_point()
How It Works
- The secret (bytes) is converted into a large integer.
- A random polynomial of degree k-1 is generated in a finite field defined by a large prime.
- Each share corresponds to a point
(x, y)on that polynomial. - Reconstruction uses Lagrange interpolation modulo the same prime to recover the constant term — the original secret.
Author
Laurentiu Bodlev 📍 Cahul, Republic of Moldova 💡 Passionate about cryptography, networking, and distributed systems. 🌐 GitHub: https://github.com/lbodlev888
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.4.tar.gz.
File metadata
- Download URL: shamir_lbodlev-0.1.4.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
885385d084866dcc3b6caeda0fdeaf356654d691417deda6b3967054a1741426
|
|
| MD5 |
67552ad6cd9d2c040677712e29a13125
|
|
| BLAKE2b-256 |
cf41b80f06db2bedfb65704844294421520b1027fea7046154261497738231f0
|
File details
Details for the file shamir_lbodlev-0.1.4-py3-none-any.whl.
File metadata
- Download URL: shamir_lbodlev-0.1.4-py3-none-any.whl
- Upload date:
- Size: 5.8 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 |
bfc588b7080aa7c43142ecd0a5d82e315adf0e5b0f67a58867faa48d5e2c9a73
|
|
| MD5 |
14b574e7277324142b4922ef61cd5b65
|
|
| BLAKE2b-256 |
46cdd8625ceef14a367e558a3475c82750c7af4c804b2202a2f79a42c6e3cf94
|