Question

Edit - clarified

I'm trying to implement modular exponentiation in Java using lagrange and the chinese remainder theorem.

For example, if N is 55, having been given the prime factors 5 and 11, phi is 40, so I know there are 40 numbers co-prime to N below 55. My instructor says the way to do it is "using Lagrange’s theorem, a few multiplications modulo 5 and 11 and CRT to combine both results"

My problem is how to I calculate these numbers? I need them to put them into a Chinese remainder theorem to finish the calculations, but I can't think of a clever way to cycle through N using phi(n) as a result. N will be an extremely large number, at least 1024 bits. I could possibly be on the wrong track here, do I even need all of these primes?

I do suspect that the answer will be related to the extended euclid function, which I already have coded so if I need to use results from it that's fine.

I don't understand the code in How many numbers below N are coprimes to N? so it isn't much help to me, and the math papers I look at I find very difficult to follow, the sum and product type notation baffles me a bit. Also, some answers use square roots and logs which aren't really an option with a BigInteger (correct me if I'm wrong)

Can anyone give me an answer in plain English??

It's ok to show me code, this is more of a learning exercise because the answer I'm going to submit uses Montgomery. (Yeah I know, bizarre that I could work out Montgomery from a math formula but this Lagrange plus CRT has me totally baffled)

I've gotten as far as this, working through an example I've found. The prime factors are seven and five, so phi of 35 is 24 (I have a working Euler totient function).

Was it helpful?

Solution

See this answer for a worked-out example. It shows exactly how to perform modular exponentation modulo a composite by doing the operation modulo the prime factors, and combining the results.

OTHER TIPS

To find all the numbers coprime with N just iterate euclid GCD() algorithm over [1,N]. If GCD(a,N) == 1 then a,N are coprime

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top