nordlow / gmp-d

D-language high-level wrapper for GNU MP (GMP) library
14 stars 6 forks source link

need of mpz_invert (modular multiplicative inverse) #6

Closed basiliscos closed 6 years ago

basiliscos commented 6 years ago

Hi,

There is need of mpz_invert [1] function export in the module.

However, I'm not sure what D interface should it have, if a modular multiplicative inverse might not exist. Nullable?

[1] https://gmplib.org/manual/Number-Theoretic-Functions.html#Number-Theoretic-Functions

nordlow commented 6 years ago

If we make the invert function a free function we can apply more optimizations and still use it with UFCS as

auto z = x.invert(y);

Nullable is more robust, assert is simpler for the user. What do you say? I haven't used Nullable yet in gmp-d. I'm not sure.

basiliscos commented 6 years ago

I think that assert is more appropriate, as the inverse number cannot be found only if LCM(base, N) != 1, i.e. violation of contract / assumptions.

Let me try to make a PR (I'm D-newbie, ha-ha).