Class: Object

Inherits:
BasicObject
Defined in:
lib/crypto_toolchain/extensions/object_extensions.rb

Instance Method Summary collapse

Instance Method Details

#chinese_remainder(residues, mods) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/crypto_toolchain/extensions/object_extensions.rb', line 4

def chinese_remainder(residues, mods)
  mod_product = ->(without) { mods.inject(:*) / without }
  sum = 0
  residues.zip(mods) do |(residue, mod)|
    mp = mod_product.call(mod)
    sum += residue * mp * mp.invmod(mod)
  end
  sum % mods.inject(:*)
end

#numberize(n) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/crypto_toolchain/extensions/object_extensions.rb', line 14

def numberize(n)
  if n.respond_to?(:to_number)
    n.to_number
  elsif n.is_a?(Numeric)
    n
  else
    raise ArgumentError, "#{n} cannot be numberized"
  end
end