Method: Object#chinese_remainder
- Defined in:
- lib/crypto_toolchain/extensions/object_extensions.rb
#chinese_remainder(residues, mods) ⇒ Object
Transcribed from cryptopals.com/sets/5/challenges/40
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 |