Skip to main content

Simple implementation of Shamir's Secret Sharing scheme

Project description

🔐 Shamir Secret Sharing — Python Implementation

PyPI Python License: MIT

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 number p, total shares n, threshold k)
  • 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

  1. The secret (bytes) is converted into a large integer.
  2. A random polynomial of degree k-1 is generated in a finite field defined by a large prime.
  3. Each share corresponds to a point (x, y) on that polynomial.
  4. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

shamir_lbodlev-0.1.2.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

shamir_lbodlev-0.1.2-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file shamir_lbodlev-0.1.2.tar.gz.

File metadata

  • Download URL: shamir_lbodlev-0.1.2.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

Hashes for shamir_lbodlev-0.1.2.tar.gz
Algorithm Hash digest
SHA256 9ac89d12553de64f2e034b383fc0b03ec266c46b4b5484f6adb6875eed80ea4d
MD5 bbd14108a9a03cb7822f78476adbf47b
BLAKE2b-256 bdde84fa796919e75f6f16ea96c94bdd1b7f1d4eb26942a754371dd49bdc0b0b

See more details on using hashes here.

File details

Details for the file shamir_lbodlev-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: shamir_lbodlev-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for shamir_lbodlev-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ea8307c1a6b13ba7644232a0c8628194c6c37bbca6cbdf42b9c23e7b27635ac5
MD5 06dabb7181a6ff8449ef5e2888a2bbcb
BLAKE2b-256 7ffc5549fbec734fa1489a5a8da5cf0fbd1bc485b6db8e01b70f1946c432ab00

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page