12,699,492 members (32,643 online)
Tip/Trick
alternative version

7.8K views
2 bookmarked
Posted

# Solving the Chinese Remainder Theorem Using Totients

, 2 Sep 2014 CPOL
 Rate this:
This code gives a solution to the Chinese Remainder Theorem using totients instead of the Extended Euclidean algorithm.

## Introduction

Using Euclid's Extended Modular Inversion formula to solve for the CRT can be overkill in the case that the totients of the number field are already known.  I present a very efficient formula that's deceptively simple.  Not to mention... FAST!

## Background

Here is an example of the problem:

We have a basket of eggs.  When they are grouped by 3, 2 are left over.  When they are grouped by 7, 6 are left over.  When they are grouped by 13, 10 are left over.  What's the smallest number of eggs which will satisfy this scenario?

Our equation looks like this (where % is the modulo operator):

1>  X % 3 = 2  (X divided by 3 has a remainder of 2)

2>  X % 7 = 6 (X divided by 7 has a remainder of 6)

3>  X % 13 = 10 (X divided by 13 has a remainder of 10)

Notice that I used prime numbers as our divisors in the calculations.  This makes our formula simple because the totient of a prime is (p - 1).

After performing the CRT on equations 1 & 2, we arrive at:

X % 21 = 20 (and the totient of 21 = 12).

After performing the CRT with the last result and equation 3, we arrive at:

X % 273 = 62 (and the totient of 273 = 144).

62 is the solution because:

1> 62 % 3 = 2

2> 62 % 7 = 6

3> 62 % 13 = 10

You can now chain the output with more equations and extend it to represent very large integers.

## Using the code

Calling into the method Solve will return the CRT solution of the given inputs.  The only restrictions are that the input values (V1, V2) must be coprime and the totient values must be correct.

```public static void Solve(BigInteger V1,
BigInteger TotientV1,
BigInteger Residue1,
BigInteger V2,
BigInteger TotientV2,
BigInteger Residue2,
out BigInteger V3,
out BigInteger TotientV3,
out BigInteger Residue3)
{
if (BigInteger.GreatestCommonDivisor(V1, V2) != 1)
{
throw new Exception("The two parameters specified must be coprime and greater than 1.");
}

//we will trust that the user supplied the correct totient values from here
V3 = V1 * V2; //we will reduce everything to this new modulus
TotientV3 = TotientV1 * TotientV2; //the totient of our new modulus

//our simple CRT equation:
//Residue3 = Residue1 * V2 * [V2^(TotientV1 - 1) % V3] + Residue2 * V1 * [V1^(TotientV2 - 1) % V3]

Residue3 = Residue1 * V2 * BigInteger.ModPow(V2, TotientV1 - 1, V3) + Residue2 * V1 * BigInteger.ModPow(V1, TotientV2 - 1, V3);
Residue3 = BigInteger.Remainder(Residue3, V3);
}
```

## Points of Interest

You can use this code to handle modular operations of very large numbers in a very efficient manner (such as in RSA and PGP modular arithmetic).

## Share

 United States
No Biography provided

## You may also be interested in...

 Pro Pro

 First Prev Next
 My vote of 5 sbarnes9-Dec-15 16:57 sbarnes 9-Dec-15 16:57
 My vote of 5 Volynsky Alex7-Sep-14 11:52 Volynsky Alex 7-Sep-14 11:52
 Re: My vote of 5 Cryptonite25-Sep-14 9:37 Cryptonite 25-Sep-14 9:37
 Re: My vote of 5 Volynsky Alex25-Sep-14 10:38 Volynsky Alex 25-Sep-14 10:38
 Last Visit: 31-Dec-99 19:00     Last Update: 22-Jan-17 14:39 Refresh 1