modInverse & powMod

Salih Dincer salihdb at hotmail.com
Fri Jun 7 13:43:29 UTC 2024


I know there is modular exponentiation 
[std.math.exponential.powmod](https://dlang.org/library/std/math/exponential/powmod.html) 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.

```d
//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 at 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?

* 
[std.mathspecial.logmdigammaInverse](https://dlang.org/phobos/std_mathspecial.html)
* 
[std.numeric.inverseFft](https://dlang.org/library/std/numeric.html)


More information about the Digitalmars-d-learn mailing list