Class: SecretSharing::Shamir::Share
- Inherits:
-
Object
- Object
- SecretSharing::Shamir::Share
- Defined in:
- lib/secretsharing/shamir.rb
Overview
A SecretSharing::Shamir::Share object represents a share in the Shamir secret sharing scheme. The share consists of a point (x,y) on a polynomial over Z/Zp, where p is a prime.
Constant Summary collapse
- FORMAT_VERSION =
'0'
Instance Attribute Summary collapse
-
#prime ⇒ Object
readonly
Returns the value of attribute prime.
-
#prime_bitlength ⇒ Object
readonly
Returns the value of attribute prime_bitlength.
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Class Method Summary collapse
-
.from_string(string) ⇒ Object
Create a new share from a string format representation.
Instance Method Summary collapse
-
#==(share) ⇒ Object
Shares are equal if their string representation is the same.
-
#initialize(x, y, prime, prime_bitlength) ⇒ Share
constructor
Create a new share with the given point, prime and prime bitlength.
-
#to_s ⇒ Object
A string representation of the share, that can for example be distributed in printed form.
Constructor Details
#initialize(x, y, prime, prime_bitlength) ⇒ Share
Create a new share with the given point, prime and prime bitlength.
240 241 242 243 244 245 |
# File 'lib/secretsharing/shamir.rb', line 240 def initialize(x, y, prime, prime_bitlength) @x = x @y = y @prime = prime @prime_bitlength = prime_bitlength end |
Instance Attribute Details
#prime ⇒ Object (readonly)
Returns the value of attribute prime.
235 236 237 |
# File 'lib/secretsharing/shamir.rb', line 235 def prime @prime end |
#prime_bitlength ⇒ Object (readonly)
Returns the value of attribute prime_bitlength.
235 236 237 |
# File 'lib/secretsharing/shamir.rb', line 235 def prime_bitlength @prime_bitlength end |
#x ⇒ Object (readonly)
Returns the value of attribute x.
235 236 237 |
# File 'lib/secretsharing/shamir.rb', line 235 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
235 236 237 |
# File 'lib/secretsharing/shamir.rb', line 235 def y @y end |
Class Method Details
.from_string(string) ⇒ Object
Create a new share from a string format representation. For a discussion of the format, see the to_s() method.
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/secretsharing/shamir.rb', line 249 def self.from_string(string) version = string[0,1] if version != '0' then raise "invalid share format version #{version}." end x = string[1,2].hex prime_bitlength = 4 * string[-2,2].hex + 1 p_x_str = string[3, string.length - 9] checksum = string[-6, 4] computed_checksum = Digest::SHA1.hexdigest(p_x_str)[0,4].upcase if checksum != computed_checksum then raise "invalid checksum. expected #{checksum}, " + \ "got #{computed_checksum}" end prime = SecretSharing::Shamir. \ smallest_prime_of_bitlength(prime_bitlength) self.new(x, OpenSSL::BN.new(p_x_str, 16), prime, prime_bitlength) end |
Instance Method Details
#==(share) ⇒ Object
Shares are equal if their string representation is the same.
294 295 296 |
# File 'lib/secretsharing/shamir.rb', line 294 def ==(share) share.to_s == self.to_s end |
#to_s ⇒ Object
A string representation of the share, that can for example be distributed in printed form. The string is an uppercase hexadecimal string of the following format: ABBC*DDDDEEEE, where
-
A (the first nibble) is the version number of the format, currently fixed to 0.
-
B (the next byte, two hex characters) is the x coordinate of the point on the polynomial.
-
C (the next variable length of bytes) is the y coordinate of the point on the polynomial.
-
D (the next two bytes, four hex characters) is the two highest bytes of the SHA1 hash on the string representing the y coordinate, it is used as a checksum to guard against typos
-
E (the next two bytes, four hex characters) is the bitlength of the prime number in nibbles.
283 284 285 286 287 288 289 290 291 |
# File 'lib/secretsharing/shamir.rb', line 283 def to_s # bitlength in nibbles to save space prime_nibbles = (@prime_bitlength - 1) / 4 p_x = ("%x" % @y).upcase FORMAT_VERSION + ("%02x" % @x).upcase \ + p_x \ + Digest::SHA1.hexdigest(p_x)[0,4].upcase \ + ("%02x" % prime_nibbles).upcase end |