Search

I know there is modular exponentiation std.math.exponential.powmod in the standard library.While it works great in Pyrhon (even with very large numbers), it doesn't work with signed numbers in D. That's why I turned to the alternative below. Don't let it be misunderstood, I don't want to wear out the D language, I use whatever I have, I love D and I don't ask why it works so strangely.

``````//import std.math.exponential : fpowmod = powmod; /*
T fpowmod(T)(T base, T exponent, T modulus)
{
auto r = T(1);
for (T x = base, y = exponent; y;
x = x * x % modulus, y /= 2)
if (y % 2) r = r * x % modulus;

return r;
}//*/
``````

Thanks...

SDB@79

I have only one question: Is there a modInverse() function in the standard library for use in RSA? I did research on this subject within the relevant modules. I guess not these?

On Friday, 7 June 2024 at 13:43:29 UTC, Salih Dincer wrote:

>

SDB@79

I have only one question: Is there a modInverse() function in the standard library for use in RSA?

Apparently not, it fell to lot :)

``````auto modInverse(T)(T m, T n) pure
{
T q, ilk = n;
T y, tmp, x = 1;

while (m > 1)
{
q = m / n;

tmp = n;
n = m % n;
m = tmp;

tmp = y;
y = x - q * y;
x = tmp;
}
return x < 0 ? x + ilk : x;
}
``````

And in the BigInt module there was divMod() next to powmod():

``````auto modInverse(BigInt a, BigInt m)
{
BigInt q, m0 = m;
BigInt tmp, y, x = 1;

while (a > 1)
{
// m is remainder now
tmp = m;
divMod(a, m, q, m);

// process same as Euclid's algorithm
a = tmp;
tmp = y;

// Update y and x
y = x - q * y;
x = tmp;
}

// Make x positive
if (x < 0) x += m0;

return x;
}
``````

Is PR required? Why not modInverse too!

SDB@79