Class: Integer

Inherits:
Object
  • Object
show all
Defined in:
lib/schnorr.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



107
108
109
110
# File 'lib/schnorr.rb', line 107

def method_missing(method, *args)
  return mod_pow(args[0], args[1]) if method == :pow && args.length < 3
  super
end

Instance Method Details

#mod_pow(x, y) ⇒ Object

alternative implementation of Integer#pow for ruby 2.4 and earlier.



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/schnorr.rb', line 113

def mod_pow(x, y)
  return self**x unless y

  b = self
  result = 1
  while x > 0
    result = (result * b) % y if (x & 1) == 1
    x >>= 1
    b = (b * b) % y
  end
  result
end

#to_hexObject



102
103
104
105
# File 'lib/schnorr.rb', line 102

def to_hex
  hex = to_s(16)
  hex.rjust((hex.length / 2.0).ceil * 2, '0')
end