Giter Site home page Giter Site logo

brick/math method for gmp_invert about math HOT 8 CLOSED

brick avatar brick commented on August 28, 2024
brick/math method for gmp_invert

from math.

Comments (8)

BenMorel avatar BenMorel commented on August 28, 2024 1

Hi, thank you for the feature request and for providing working code! 👍

I'll take some time in the coming days to implement it in the library and add some tests.

Question: is there any value in providing gcdExtended() as a public method? Or is it just an implementation detail, with no real value on its own? I can see that Java's BigInteger has a modInverse() method, but no gcdExtended().

Finally, out of curiosity, I'd be interested to know what's your use case for modInverse()!

from math.

BenMorel avatar BenMorel commented on August 28, 2024 1

With no feedback received, I've moved forward with throwing a NegativeNumberException when the modulus is negative.

Now available as modInverse() and released as 0.8.16! ✨

from math.

Spomky avatar Spomky commented on August 28, 2024

Hi,

I am happy to read that!
The method gcdExtended can be private. At the moment, I don't see any reason for using it directly.
In my context, this function is used for the computation of the Chinese Remainder Theorem (CRT) and especially for RSA encryption algorithm (the CRT reduces the computation time).

from math.

BenMorel avatar BenMorel commented on August 28, 2024

Hi, I just started implementing modInverse(), however it looks like the algorithm above fails for negative numbers:

gmp_invert('1234567890', '-19'); // 11
modInverse('1234567890', '-19'); // -8

gmp_invert('-1234567890', '19'); // 8
modInverse('-1234567890', '19'); // exception

gmp_invert('-1234567890', '-19'); // 8
modInverse('-1234567890', '-19'); // exception

Any idea how to fix it?

from math.

BenMorel avatar BenMorel commented on August 28, 2024

Any idea how to fix it?

No worries, I got the right result by borrowing this solution from the JDK.

from math.

BenMorel avatar BenMorel commented on August 28, 2024

Now the question is, what should we do when $m is negative? Java & Dart throw an exception, while gmp_invert() returns the value of modInverse($x, abs($m)). For now I've allowed negative modulo this way, but I'm not sure this is the right way the API should respond.

from math.

BenMorel avatar BenMorel commented on August 28, 2024

Otherwise looks good to me now. Could you please test the master branch (with and without GMP please!) and report any issue?

from math.

Spomky avatar Spomky commented on August 28, 2024

Thank you! Sorry for the lack of feedback, but it works great.
Regards

from math.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.