In the gmpy2 extension module for Python there's a multiple-precision integer type called mpz. It contains a powmod(x, y, m)
function and I've been missing that one in Ruby. I was recently made aware that Ruby actually has a powmod
. It's hidden in the OpenSSL module.
require 'openssl'
result = a_big_int.to_bn.mod_exp(exponent, modulo)
Another function, that's also in gmpy2, I've been missing is divm(...)
.
divm(a, b, m) returns x such that b * x == a modulo m. Raises a ZeroDivisionError exception if no such value x exists.
Do you know if the OpenSSL module has yet another surprise up its sleeve or of any gem that sports such a function? It would be very helpful if it's a fast one.