Module: SecretSharing

Extended by:
SecretSharing
Included in:
SecretSharing
Defined in:
lib/secret_sharing.rb,
lib/secret_sharing/point.rb,
lib/secret_sharing/prime.rb,
lib/secret_sharing/share.rb,
lib/secret_sharing/version.rb,
lib/secret_sharing/charset.rb,
lib/secret_sharing/polynomial.rb

Overview

A ruby implementation of Shamir's Secret Sharing. This module is the only connection to be consumed by a user. Do not use any other class/module of this library unless you really know what you are doing.

Defined Under Namespace

Modules: Prime Classes: Charset, Point, Polynomial, Share

Constant Summary collapse

VERSION =

gem version

'0.0.5'

Instance Method Summary collapse

Instance Method Details

#reconstruct(raw_shares) ⇒ String

Recover a secret with an array of string shares generated with #split.

Example

SecretSharing.reconstruct ["1-1e489c32507823aa", "2-1c9134c644739461"]
# => "secret"

Parameters:

  • raw_shares (Array)

    Shares (array of strings) to recover the secret

Returns:

  • (String)

    Recovered secret in a string representation.


48
49
50
51
52
53
# File 'lib/secret_sharing.rb', line 48

def reconstruct(raw_shares)
  shares = raw_shares.map { |raw_share| Share.from_string raw_share }
  points = shares.map(&:point)
  secret_int = Polynomial.modular_lagrange_interpolation(points)
  Charset::ASCIICharset.i_to_s(secret_int)
end

#split(secret_string, share_threshold, num_shares) ⇒ Array

Split a secret using Shamir's Secret Sharing algorithm.

Example

SecretSharing.split('secret', 2, 3)
# => ["1-1e489c32507823aa", "2-1c9134c644739461", "3-1ad9cd5a386f0518"]

Parameters:

  • secret_string (String)

    Secret to split.

  • share_threshold (Integer)

    Number of shares to recover the secret.

  • num_shares (Integer)

    Total number of shares to generate.

Returns:

  • (Array)

    Array of shares that can be used to recover the secret.


26
27
28
29
30
31
32
33
34
35
# File 'lib/secret_sharing.rb', line 26

def split(secret_string, share_threshold, num_shares)
  secret_int = Charset::ASCIICharset.s_to_i(secret_string)
  points = Polynomial.points_from_secret(secret_int,
                                         share_threshold,
                                         num_shares)

  points.map do |point|
    Share.new(point).to_s(Math.log10(points.length) + 1)
  end
end