5 attr_reader :n, :k, :secret, :secret_bitlength, :shares
7 DEFAULT_SECRET_BITLENGTH = 128
11 raise ArgumentError, 'k must be smaller or equal than n'
14 raise ArgumentError, 'k must be greater or equal to two'
26 def create_random_secret(bitlength = DEFAULT_SECRET_BITLENGTH)
27 raise RuntimeError, 'secret already set' if secret_set?
28 @secret = get_random_number(bitlength)
29 @secret_bitlength = bitlength
33 def get_random_number(bitlength, highest_bit_one = true)
34 byte_length = (bitlength / 8.0).ceil
35 rand_hex = OpenSSL::Random.random_bytes(byte_length).each_byte.to_a.map { |a| "%02x" % a }.join('')
36 rand = OpenSSL::BN.new(rand_hex, 16)
38 rand.mask_bits!(bitlength)
39 rescue OpenSSL::BNError
40 # never mind if there was an error, this just means
41 # rand was already smaller than 2^bitlength - 1
43 if highest_bit_one then
44 rand.set_bit!(bitlength)