Module: SchnorrSig::Utils

Included in:
Fast, Pure
Defined in:
lib/schnorr_sig/utils.rb

Instance Method Summary collapse

Instance Method Details

#big2bin(bignum) ⇒ Object

convert a giant integer to a binary string



28
# File 'lib/schnorr_sig/utils.rb', line 28

def big2bin(bignum) = hex2bin(bignum.to_s(16).rjust(64, '0'))

#bin2big(str) ⇒ Object

likely returns a Bignum, larger than a 64-bit hardware integer



25
# File 'lib/schnorr_sig/utils.rb', line 25

def bin2big(str) = bin2hex(str).to_i(16)

#bin2hex(str) ⇒ Object

convert a binary string to a lowercase hex string



31
# File 'lib/schnorr_sig/utils.rb', line 31

def bin2hex(str) = str.unpack1('H*')

#binary!(str, length) ⇒ Object

raise SpecError or return str

Raises:



15
16
17
18
19
20
21
22
# File 'lib/schnorr_sig/utils.rb', line 15

def binary!(str, length)
  check!(str, String)
  if str.encoding != Encoding::BINARY
    raise(SpecError, "Encoding: #{str.encoding}")
  end
  raise(SpecError, "Length: #{str.length}") if str.length != length
  str
end

#check!(val, cls) ⇒ Object

raise SpecError or return val



10
11
12
# File 'lib/schnorr_sig/utils.rb', line 10

def check!(val, cls)
  val.is_a?(cls) ? val : raise(SpecError, "#{cls}: #{val.inspect}")
end

#hex2bin(hex) ⇒ Object

convert a hex string to a binary string



34
# File 'lib/schnorr_sig/utils.rb', line 34

def hex2bin(hex) = [hex].pack('H*')