Class: SecretSharing::Shamir::Container
- Inherits:
-
Object
- Object
- SecretSharing::Shamir::Container
- Includes:
- SecretSharing::Shamir
- Defined in:
- lib/secretsharing/shamir/container.rb
Overview
The SecretSharing::Shamir::Container class can be used to share random secrets between n people, so that k < n people can recover the secret, but k-1 people learn nothing (in an information-theoretical sense) about the secret.
For a theoretical background, see:
http://www.cs.tau.ac.il/~bchor/Shamir.html
http://en.wikipedia.org/wiki/Secret_sharing#Shamir.27s_scheme
Constant Summary collapse
- MIN_SHARES =
2
- MAX_SHARES =
512
Instance Attribute Summary collapse
-
#k ⇒ Object
readonly
Returns the value of attribute k.
-
#n ⇒ Object
readonly
Returns the value of attribute n.
-
#secret ⇒ Object
Returns the value of attribute secret.
-
#shares ⇒ Object
readonly
Returns the value of attribute shares.
Instance Method Summary collapse
-
#<<(share) ⇒ Object
Add a secret share to the object.
-
#initialize(n, k = n) ⇒ Container
constructor
To create a new SecretSharing::Shamir::Container object, you can pass either just n, or n and k where:.
- #secret? ⇒ Boolean
Methods included from SecretSharing::Shamir
#evaluate_polynomial_at, #get_random_number, #lagrange, #usafe_decode64, #usafe_encode64
Constructor Details
#initialize(n, k = n) ⇒ Container
To create a new SecretSharing::Shamir::Container object, you can pass either just n, or n and k where:
n = The total number of shares that will be created.
k = The threshold number of the total shares needed to
recreate the original secret. (Default = n)
For example:
# 3(k) out of 5(n) shares needed to recover secret
s = SecretSharing::Shamir::Container.new(5, 3)
# 3(k) out of 3(n) shares needed to recover secret
s = SecretSharing::Shamir::Container.new(3)
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/secretsharing/shamir/container.rb', line 50 def initialize(n, k = n) @n = n.to_i @k = k.to_i fail ArgumentError, 'n must be an Integer' unless @n.is_a?(Integer) fail ArgumentError, 'k must be an Integer' unless @k.is_a?(Integer) fail ArgumentError, 'k must be <= n' unless @k <= @n fail ArgumentError, 'k must be >= #{MIN_SHARES}' unless @k >= MIN_SHARES fail ArgumentError, 'n must be <= #{MAX_SHARES}' unless @n <= MAX_SHARES @secret = nil @shares = [] end |
Instance Attribute Details
#k ⇒ Object (readonly)
Returns the value of attribute k.
30 31 32 |
# File 'lib/secretsharing/shamir/container.rb', line 30 def k @k end |
#n ⇒ Object (readonly)
Returns the value of attribute n.
30 31 32 |
# File 'lib/secretsharing/shamir/container.rb', line 30 def n @n end |
#secret ⇒ Object
Returns the value of attribute secret.
30 31 32 |
# File 'lib/secretsharing/shamir/container.rb', line 30 def secret @secret end |
#shares ⇒ Object (readonly)
Returns the value of attribute shares.
30 31 32 |
# File 'lib/secretsharing/shamir/container.rb', line 30 def shares @shares end |
Instance Method Details
#<<(share) ⇒ Object
Add a secret share to the object. Accepts a SecretSharing::Shamir::Share instance. Returns secret as a SecretSharing::Shamir::Secret if enough valid shares have been added to recover the secret, and false otherwise. The secret can also be recovered later with SecretSharing::Shamir::Container#secret if enough valid shares were previously provided.
83 84 85 86 87 88 89 90 |
# File 'lib/secretsharing/shamir/container.rb', line 83 def <<(share) # You can't add more shares than were originally generated with value of @n fail ArgumentError, 'You have added more shares than allowed by the value of @n' if @shares.size >= @n share = SecretSharing::Shamir::Share.new(:share => share) unless share.is_a?(SecretSharing::Shamir::Share) @shares << share unless @shares.include?(share) @secret = Share.recover_secret(@shares) end |
#secret? ⇒ Boolean
65 66 67 |
# File 'lib/secretsharing/shamir/container.rb', line 65 def secret? @secret.is_a?(SecretSharing::Shamir::Secret) end |