SSSS-Swift is a Swift implementation of Shamir's Secret Sharing Scheme (SSSS) over the Galois field GF(2⁸). This library is a Swift port of the Go implementation used in Hashicorp Vault. It allows you to split an arbitrary secret (byte array) into a configurable number of shares, such that any threshold number of shares is sufficient to reconstruct the original secret.
- Reliable – uses a cryptographically secure random number generator (via
Array.shuffled(), which relies on the system's RNG). - Flexible – supports up to 255 shares and a threshold from 2 to 255.
- Simple – minimalistic API for splitting and combining.
- Secure – each byte of the secret is encrypted using an independent polynomial, preventing information leakage when fewer than the threshold shares are available.
- Cross‑platform – works on iOS, macOS, tvOS, watchOS, and Linux (Swift 5.0+).
- Swift 5.0 or newer
- Platforms: iOS 10.0+, macOS 10.12+, tvOS 10.0+, watchOS 3.0+, Linux
Add the dependency to your Package.swift:
dependencies: [
.package(url: "https://github.com/hIMEI29A/SSSS-Swift.git", from: "0.1.0")
]Then import the module:
import SSSS_Swiftlet secret: [UInt8] = "Hello, World!".utf8.map { UInt8($0) }
let ssss = SSSS()
do {
// Split into 5 shares, requiring 3 to reconstruct
let shares = try ssss.split(secret: secret, parts: 5, threshold: 3)
// shares[0]...shares[4] – each share contains the secret bytes plus a final byte (the x‑coordinate)
// Store or distribute these shares to different parties.
} catch {
print("Split error: \(error)")
}// Suppose we have 3 shares (any of the 5)
let recoveredParts = [shares[0], shares[2], shares[4]] // threshold is 3
do {
let recoveredSecret = try ssss.combine(parts: recoveredParts)
let recoveredString = String(bytes: recoveredSecret, encoding: .utf8)
print("Recovered secret: \(recoveredString ?? "invalid UTF-8")")
} catch {
print("Combine error: \(error)")
}All possible errors are declared in the SsssError enumeration:
partsLesser – number of shares is less than the threshold.
partsExceed – number of shares exceeds 255.
thresholdIsWrong – threshold is less than 2.
thresholdExceed – threshold exceeds 255.
emptySecret – the secret is empty.
initError – failed to generate a polynomial.
partsTooShort – shares are shorter than 2 bytes.
invalidPartsLength – shares have different lengths.
duplicatePart – a share with a duplicate x‑coordinate was detected.
Every error includes a human‑readable description.
public func split(secret: [Byte], parts: Int, threshold: Int) throws -> [[Byte]]- Parameters:
- secret – the byte array to be split.
- parts – total number of shares to create (2…255).
- threshold – minimum number of shares required for reconstruction (2…255, must be ≤ parts).
- Returns: an array of parts shares, each of length secret.count + 1. The last byte of each share is the unique x‑coordinate (1…255).
- Throws: SsssError if constraints are violated or polynomial generation fails.
public func combine(parts: [[Byte]]) throws -> [Byte]- Parameters:
- parts – an array of shares (each share is a byte array of equal length).
- Returns: the reconstructed secret as a byte array.
- Throws: SsssError if fewer than two shares are provided, lengths differ, duplicate x‑coordinates are found, or other errors occur.
- Note: the library assumes all shares originate from a single split call and have correct x‑coordinates.
The GF(2⁸) field is implemented using arithmetic with the irreducible polynomial 0x11B (as in AES).
For each byte of the secret, a separate polynomial of degree threshold‑1 is built with random coefficients.
The constant term of the polynomial equals the corresponding secret byte.
X‑coordinates are randomly generated without replacement from the range [1, 255].
Reconstruction uses Lagrange interpolation at x = 0 to obtain the constant term.
SSSS-Swift is released under the MIT License. See the LICENSE file for details. Please note that the documentation and certain parts of the logic were generated with assistance from DeepSeek.
Pull requests and issue reports are welcome. Please ensure that your code is covered by tests and conforms to the SwiftLint style.
- Алексей Матвеев – development, testing, integration.
- DeepSeek – AI assistant that contributed to code generation, documentation, and unit tests.