API Reference
Complete API documentation for the Shamir package.
Shamir Facade
Section titled “Shamir Facade”Static facade providing convenient access to ShamirManager functionality.
split()
Section titled “split()”Split a secret into shares (static API).
public static function split( string $secret, int $threshold, int $shares,): ShareCollectionParameters:
$secret- The secret to split (any binary string)$threshold- Minimum shares needed to reconstruct (k)$shares- Total number of shares to generate (n)
Returns: ShareCollection - Collection of generated shares
Throws:
InvalidArgumentException- If threshold > shares or threshold < 2
Example:
$shares = Shamir::split('my-secret', threshold: 3, shares: 5);Begin fluent split operation.
public static function for(string $secret): SplitConductorParameters:
$secret- The secret to split
Returns: SplitConductor - Fluent conductor for configuration
Example:
$shares = Shamir::for('my-secret') ->threshold(3) ->shares(5) ->split();combine()
Section titled “combine()”Reconstruct a secret from shares (static API).
public static function combine(iterable $shares): stringParameters:
$shares- Iterable of Share objects or share strings
Returns: string - The reconstructed secret
Throws:
InsufficientSharesException- If fewer shares than thresholdInvalidShareException- If share format is invalid or checksum failsIncompatibleSharesException- If shares are from different splits
Example:
$secret = Shamir::combine([$share1, $share2, $share3]);from()
Section titled “from()”Begin fluent combine operation.
public static function from(iterable $shares): CombineConductorParameters:
$shares- Iterable of Share objects or share strings
Returns: CombineConductor - Fluent conductor for combination
Example:
$secret = Shamir::from([$share1, $share2, $share3])->combine();withConfig()
Section titled “withConfig()”Create manager with different configuration.
public static function withConfig(Config $config): ShamirManagerParameters:
$config- Configuration instance
Returns: ShamirManager - New manager with config
Example:
$config = new Config(prime: Config::PRIME_512, encoding: 'hex');$manager = Shamir::withConfig($config);$shares = $manager->split('secret', 3, 5);areCompatible()
Section titled “areCompatible()”Check if shares are from the same split operation.
public static function areCompatible(Share ...$shares): boolParameters:
...$shares- Variable number of Share objects
Returns: bool - True if shares have the same threshold
Example:
if (Shamir::areCompatible($share1, $share2, $share3)) { // Safe to combine}ShamirManager
Section titled “ShamirManager”Central manager for orchestrating secret sharing operations.
Constructor
Section titled “Constructor”public function __construct(private Config $config = new Config())split()
Section titled “split()”Split a secret into shares.
public function split( string $secret, int $threshold, int $shares,): ShareCollectioncombine()
Section titled “combine()”Reconstruct a secret from shares.
public function combine(iterable $shares): stringBegin fluent split operation.
public function for(string $secret): SplitConductorfrom()
Section titled “from()”Begin fluent combine operation.
public function from(iterable $shares): CombineConductorwithConfig()
Section titled “withConfig()”Create new manager with different config.
public function withConfig(Config $config): selfareCompatible()
Section titled “areCompatible()”Verify shares are compatible.
public function areCompatible(Share ...$shares): boolgetConfig()
Section titled “getConfig()”Get current configuration.
public function getConfig(): ConfigConductors
Section titled “Conductors”SplitConductor
Section titled “SplitConductor”Fluent API for configuring split operations.
threshold()
Section titled “threshold()”Set minimum shares needed.
public function threshold(int $threshold): selfshares()
Section titled “shares()”Set total shares to generate.
public function shares(int $shares): selfsplit()
Section titled “split()”Execute the split operation.
public function split(): ShareCollectionExample:
$shares = $manager->for($secret) ->threshold(3) ->shares(5) ->split();CombineConductor
Section titled “CombineConductor”Fluent API for combine operations.
combine()
Section titled “combine()”Execute the combine operation.
public function combine(): stringExample:
$secret = $manager->from($shares)->combine();Share Class
Section titled “Share Class”Pure value object representing a single share in the secret sharing scheme.
Constructor
Section titled “Constructor”public function __construct( private int $index, private string $value, private int $threshold, private string $checksum,)Methods
Section titled “Methods”getIndex()
Section titled “getIndex()”public function getIndex(): intGet the share’s index (1-based).
getValue()
Section titled “getValue()”public function getValue(): stringGet the encoded share value.
getThreshold()
Section titled “getThreshold()”public function getThreshold(): intGet the threshold required to reconstruct the secret.
getChecksum()
Section titled “getChecksum()”public function getChecksum(): stringGet the share’s checksum for validation.
toString()
Section titled “toString()”public function toString(): stringSerialize to portable string format: "index:threshold:checksum:value"
Delegates to ShareSerializer internally.
Example:
$shareString = $share->toString();// "1:3:abc123...:encoded_value"fromString()
Section titled “fromString()”public static function fromString(string $encoded): selfParse a share from string format.
Delegates to ShareSerializer internally.
Throws: InvalidShareException - If format is invalid
Example:
$share = Share::fromString('1:3:checksum:value');fromArray()
Section titled “fromArray()”public static function fromArray(array $data): selfCreate share from array (JSON deserialization).
Delegates to ShareSerializer internally.
Throws: InvalidShareException - If required fields are missing
Example:
$data = json_decode($json, true);$share = Share::fromArray($data);jsonSerialize()
Section titled “jsonSerialize()”public function jsonSerialize(): arraySerialize to array for JSON encoding.
Delegates to ShareSerializer internally.
Example:
$json = json_encode($share);ShareSerializer
Section titled “ShareSerializer”Service for serializing and deserializing Share objects.
toString()
Section titled “toString()”Serialize a share to string format.
public function toString(Share $share): stringfromString()
Section titled “fromString()”Deserialize a share from string format.
public function fromString(string $encoded): ShareThrows: InvalidShareException
toArray()
Section titled “toArray()”Serialize a share to array format.
public function toArray(Share $share): arrayfromArray()
Section titled “fromArray()”Deserialize a share from array format.
public function fromArray(array $data): ShareThrows: InvalidShareException
Example:
$serializer = new ShareSerializer();
// String serialization$string = $serializer->toString($share);$share = $serializer->fromString($string);
// Array serialization$array = $serializer->toArray($share);$share = $serializer->fromArray($array);ShareCollection Class
Section titled “ShareCollection Class”Collection of shares with utility methods.
Constructor
Section titled “Constructor”public function __construct(private array $shares)Methods
Section titled “Methods”public function get(int $index): ShareGet share by index (1-based).
Throws: InvalidShareException - If share not found
take()
Section titled “take()”public function take(int $count): selfTake first N shares.
random()
Section titled “random()”public function random(int $count): selfGet N random shares.
Throws: InvalidArgumentException - If count > available shares
count()
Section titled “count()”public function count(): intGet total number of shares.
toArray()
Section titled “toArray()”public function toArray(): arrayConvert to plain array of Share objects.
forDistribution()
Section titled “forDistribution()”public function forDistribution(): arrayGet shares in randomized order, indexed by share number.
Returns: array<int, Share> - Shares indexed by their index
Example:
foreach ($shares->forDistribution() as $index => $share) { sendToKeyHolder($index, $share);}Config Class
Section titled “Config Class”Configuration options for Shamir operations.
Constructor
Section titled “Constructor”public function __construct( public string $prime = Config::PRIME_256, public string $encoding = 'base64',)Constants
Section titled “Constants”Prime Field Sizes
Section titled “Prime Field Sizes”// 128-bit prime (secrets up to ~16 bytes per chunk)Config::PRIME_128
// 256-bit prime (secrets up to ~32 bytes per chunk) - DEFAULTConfig::PRIME_256
// 512-bit prime (larger secrets)Config::PRIME_512Properties
Section titled “Properties”$prime
Section titled “$prime”The prime number defining the Galois field size.
$encoding
Section titled “$encoding”Encoding format for share values: 'base64' (default) or 'hex'.
Example
Section titled “Example”$config = new Config( prime: Config::PRIME_512, encoding: 'hex',);
$shares = Shamir::split($secret, 3, 5, $config);Exceptions
Section titled “Exceptions”ShamirException
Section titled “ShamirException”Base exception for all Shamir-related errors.
InsufficientSharesException
Section titled “InsufficientSharesException”Thrown when fewer shares than threshold are provided.
Static Constructor:
InsufficientSharesException::notEnoughShares(int $provided, int $required)InvalidShareException
Section titled “InvalidShareException”Thrown when share format is invalid or checksum fails.
Static Constructors:
InvalidShareException::invalidFormat(string $encoded)InvalidShareException::missingRequiredFields()InvalidShareException::shareNotFound(int $index)InvalidShareException::checksumMismatch()IncompatibleSharesException
Section titled “IncompatibleSharesException”Thrown when shares are from different split operations.
Static Constructors:
IncompatibleSharesException::differentThresholds()IncompatibleSharesException::differentChecksums()SecretTooLargeException
Section titled “SecretTooLargeException”Thrown when secret exceeds field size (rarely occurs due to automatic chunking).
Static Constructor:
SecretTooLargeException::exceedsFieldSize(int $secretSize, int $maxSize)Type Reference
Section titled “Type Reference”Iterables
Section titled “Iterables”The combine() method accepts any iterable of shares:
// Array of Share objectsShamir::combine([$share1, $share2, $share3]);
// ShareCollectionShamir::combine($shares->take(3));
// Array of stringsShamir::combine([ '1:3:checksum:value1', '2:3:checksum:value2', '3:3:checksum:value3',]);
// MixedShamir::combine([ $shareObject, '2:3:checksum:value', Share::fromString('3:3:checksum:value'),]);