Download Modular Arithmetic

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

List of important publications in mathematics wikipedia , lookup

Wiles's proof of Fermat's Last Theorem wikipedia , lookup

Large numbers wikipedia , lookup

Line (geometry) wikipedia , lookup

Location arithmetic wikipedia , lookup

Mathematics of radio engineering wikipedia , lookup

Factorization wikipedia , lookup

Factorization of polynomials over finite fields wikipedia , lookup

Fundamental theorem of algebra wikipedia , lookup

Addition wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Algebra wikipedia , lookup

Arithmetic wikipedia , lookup

Elementary mathematics wikipedia , lookup

Transcript
The Algebra of Encryption
18 July 2011
The Algebra of Encryption
1 Modular Arithmetic
Much of modern cryptography is based on modular arithmetic. We can think of modular
arithmetic as a cycle. When we reach the base number or modulus, we cycle back to zero. We
use modular arithmetic every day. For example, when we look at the clock we are using base 12.
If the current time is 9 am and an appointment is at 1 pm, we know that the appointment is four
hours away. Modular arithmetic tells us that 1 βˆ’ 9 ≑ 4 π‘šπ‘œπ‘‘ 12. (Modular arithmetic, 2011)
1.1 Division Algorithm
The division algorithm roughly states β€œthat any integer a can be β€œdivided” by b in such a way
that the remainder is smaller than b.” (Burton, 2007, p. 17) Mathematically, this can be
expressed as follows
π‘Ž = π‘šπ‘ + π‘Ÿ
Equation 1-1
Where m is a multiplier and r is a residue.
Example 1-1
13 = 1 βˆ— 12 + 1, the multiplier is 1 and the residue is 1. Using modular arithmetic, we write
13 ≑ 1 π‘šπ‘œπ‘‘ 12.
Example 1-2
9 = 0 βˆ— 12 + 9, in this case the multiplier is zero and the residue is equal to 9 or a. Using
modular arithmetic, we write 9 ≑ 9 π‘šπ‘œπ‘‘ 12.
1.2 Addition
Addition follows the same rules that we are used to from normal arithmetic with the additional
step that the result is reduced by the modulus.
To add two numbers π‘Ž1 and π‘Ž2 we first express the numbers in the form of Equation 1-1.
π‘Ž1 = π‘š1 βˆ— 𝑏 + π‘Ÿ1
π‘Ž2 = π‘š2 βˆ— 𝑏 + π‘Ÿ2
Next we add and collect terms.
π‘Ž1 + π‘Ž2 = (π‘š1 + π‘š2 ) βˆ— 𝑏 + (π‘Ÿ1 + π‘Ÿ2 )
Note that this remains in the form of Equation 1-1 but does not preclude the possibility that the
sum π‘Ÿ1 + π‘Ÿ2 could be greater than the modulus, thus causing a change to the multiplier.
Example 1-3
13 + 9 ≑ 10 π‘šπ‘œπ‘‘ 12
13 + 9 = (1 βˆ— 12 + 1) + (0 βˆ— 12 + 9) = (1 + 0) βˆ— 12 + (1 + 9) = 1 βˆ— 12 + 10
In this case, the multiplier is not affected.
Page 1 of 17
The Algebra of Encryption
18 July 2011
Example 1-4
9 + 9 ≑ 6 π‘šπ‘œπ‘‘ 12
9 + 9 = (0 βˆ— 12 + 9) + (0 βˆ— 12 + 9) = (0 + 0) βˆ— 12 + (9 + 9) = 0 βˆ— 12 + 18
In this case, the resulting residue is greater than the modulus and should be reduced. Add and
subtract 12, and collect terms.
18 = 0 βˆ— 12 + 18 + 1 βˆ— 12 βˆ’ 12 = (0 + 1) βˆ— 12 + (18 βˆ’ 12) = 1 βˆ— 12 + 6
1.3 Subtraction
Modular subtraction is similar to modular addition. To subtract two numbers π‘Ž1 and π‘Ž2 we first
express the numbers in the form of Error! Reference source not found..
π‘Ž1 = π‘š1 βˆ— 𝑏 + π‘Ÿ1
π‘Ž2 = π‘š2 βˆ— 𝑏 + π‘Ÿ2
Next we subtract and collect terms.
π‘Ž1 βˆ’ π‘Ž2 = (π‘š1 βˆ’ π‘š2 ) βˆ— 𝑏 + (π‘Ÿ1 βˆ’ π‘Ÿ2 )
Again, the result remains in the form of Equation 1-1 but does not preclude the possibility that
the difference π‘Ÿ1 βˆ’ π‘Ÿ2 could be less than the zero. Since we generally deal with positive
numbers, this causes a change to the multiplier.
Example 1-5
9 βˆ’ 13 ≑ 8 π‘šπ‘œπ‘‘ 12
9 βˆ’ 13 = (0 βˆ— 12 + 9) βˆ’ (1 βˆ— 12 + 1) = (0 βˆ’ 1) βˆ— 12 + (9 βˆ’ 1) = (βˆ’1) βˆ— 12 + 8
In this case, the multiplier is not affected.
Example 1-6
13 βˆ’ 9 ≑ 4 π‘šπ‘œπ‘‘ 12
13 βˆ’ 9 = (1 βˆ— 12 + 1) βˆ’ (0 βˆ— 12 + 9) = (1 βˆ’ 0) βˆ— 12 + (1 βˆ’ 9) = 1 βˆ— 12 + (βˆ’8)
In this case, the resulting residue is less than zero and should be adjusted. Add and subtract 12,
and collect terms.
(βˆ’8) = 1 βˆ— 12 + (βˆ’8) + (βˆ’1) βˆ— 12 + 12 = (1 βˆ’ 1) βˆ— 12 + (βˆ’8 + 12) = 0 βˆ— 12 + 4
1.4 Multiplication
Multiplication is merely repeated addition. As with addition and subtraction, the resulting
residue is generally adjusted to be less than the modulus and non-negative.
Example 1-7
4 βˆ— 13 ≑ 4 π‘šπ‘œπ‘‘ 12
4 βˆ— 13 = (1 βˆ— 12 + 1) + (1 βˆ— 12 + 1) + (1 βˆ— 12 + 1) + (1 βˆ— 12 + 1)
= (1 + 1 + 1 + 1) βˆ— 12 + (1 + 1 + 1 + 1) = (4) βˆ— 12 + 4
In this case, the multiplier is not affected.
Example 1-8
13 βˆ— 4 ≑ 4 π‘šπ‘œπ‘‘ 12
Page 2 of 17
The Algebra of Encryption
18 July 2011
13 βˆ— 4 = 13 βˆ— (0 βˆ— 12 + 4) = (13 βˆ— 0) βˆ— 12 + (13 βˆ— 4) = 0 βˆ— 12 + 52
In this case, the resulting residue should be adjusted.
52 = 4 βˆ— 12 + 4
1.5 Division
Division is a little tricky. Normally we think of division as dividing one number into equal parts
countable to another. Thus the name. However, when we stick to Integers, we don’t always get
equal Integer parts.
We need to think of modular division a little differently. Generally, when we divide c by d we
use the following notation.
𝑐
=𝑒
𝑑
Equation 1-2
We need to write this in a different way.
𝑐 = 𝑑 βˆ— 𝑒 or 𝑑 βˆ— 𝑒 = 𝑐
Equation 1-3
If trying to divide c by d, we need to ask by what number, e, can we multiply d such that the
result is congruent to c modulo the modulus?
Example 1-9
10 ÷ 2 ≑ 5 π‘šπ‘œπ‘‘ 13
2 βˆ— 5 = 10 = 0 βˆ— 13 + 10
This is easy because we know we can multiply 2 by 5 and the result is 10.
Example 1-10
10 ÷ 4 ≑ 9 π‘šπ‘œπ‘‘ 13
In this case, 4 βˆ— 9 = 36 = 2 βˆ— 13 + 10. That is to say, we found a number, 9, that we can
multiply by 4 such that the result is congruent to 10 modulo 13. Exactly how we find the number
9 directly, without trial and error, is discussed in section 1.6 and 0.
1.6 Division by Multiplicative Inverse
Another way of accomplishing modular division is by using the multiplicative inverse of the
divisor. Re-writing Equation 1-2, we get the following equation for division.
𝑐 βˆ— π‘‘βˆ’1 = 𝑒
Equation 1-4
But this begs the question, what is the multiplicative inverse in modular arithmetic?
Remembering back to regular arithmetic, we again think of multiplicative inverse in a different
way. We need to ask by what number, π‘‘βˆ’1 , can we multiply d such that the result is congruent
to 1 modulo the modulus? This is expressed mathematically as follows.
1 ≑ 𝑑 βˆ— 𝑑 βˆ’1 π‘šπ‘œπ‘‘ π‘šπ‘œπ‘‘π‘’π‘™π‘’π‘ 
Equation 1-5
Page 3 of 17
The Algebra of Encryption
18 July 2011
Expressed algebraically for modular arithmetic, we have the following.
1 = π‘š βˆ— 𝑏 + 𝑑 βˆ— 𝑑 βˆ’1
Equation 1-6
Where again, m is the multiplier, b is the modulus, d is the number of interest, and 𝑑 βˆ’1 is the
modular multiplicative inverse (MMI).
Example 1-11
(4)βˆ’1 ≑ 10 π‘šπ‘œπ‘‘ 13
1 = (βˆ’3) βˆ— 13 + 4 βˆ— 10.
Again, exactly how we find the number 10 directly, without trial and error, is discussed in
section 0.
Example 1-12
Going back to Example 1-10, we get the following.
10 ÷ 4 = 10 βˆ— 4βˆ’1 ≑ 10 βˆ— 10 π‘šπ‘œπ‘‘ 13 = 100 = 7 βˆ— 13 + 9 ≑ 9 π‘šπ‘œπ‘‘ 13, which agrees with the
findings in Example 1-10.
2 Useful Functions
2.1 Euclidean Algorithm
The Euclidean algorithm is an important part of the algebra of encryption. It is generally thought
of as providing the Greatest Common Divisor (GCD) of two numbers. When the two numbers
are properly chosen, the Euclidean algorithm can also be used to directly determine the Modular
Multiplicative Inverse (MMI).
2.1.1 Greatest Common Divisor
β€œThe Euclidean algorithm (also called Euclid’s algorithm) is an efficient method for computing
the greatest common divisor.” β€œThe GCD of two numbers is the largest number that divides both
of them without leaving a remainder.” (Euclidean algorithm, 2011)
Figure 2-1 reproduces an example discussed in (Euclidean algorithm, 2011). We would like to
find the GCD of two numbers represented by lines AB and CD.
1. Start by comparing the smaller number to the larger number.
2. Find the quotient of the two numbers. In this case, the quotient refers to the greatest
integer less than or equal to 𝐴𝐡 ÷ 𝐢𝐷. In other words, how many times can we multiply
CD and still remain smaller than or equal to AB. In the figure, this quotient is 1.
3. Multiply the second number by the quotient and subtract from the first. In the figure, the
result, or residue, is AE.
4. Now compare the residue with the previous smaller number. In the figure, this is
comparing CD and AE. Repeat the above steps until the larger number is an integral
multiple of the smaller number. That is to say, there is no resulting residue. The GCD is
the last residue, in this case, CF.
Page 4 of 17
The Algebra of Encryption
18 July 2011
B
E
D
F
E
A
F
C
F
C
F
E
A
C
A
C
Figure 2-1: Euclidean Algorithm
Now we can add up the line lengths and verify that CF is the GCD of AB and CD.
𝐴𝐸 = 3 βˆ— 𝐢𝐹
𝐢𝐷 = 2 βˆ— 𝐴𝐸 + 𝐢𝐹 = 2 βˆ— 3 βˆ— 𝐢𝐹 + 𝐢𝐹 = 7 βˆ— 𝐢𝐹
𝐴𝐡 = 𝐢𝐷 + 𝐴𝐸 = 7 βˆ— 𝐢𝐹 + 3 βˆ— 𝐢𝐹 = 10 βˆ— 𝐢𝐹
Referring to Figure 2-2, it is easy to see that CF does indeed evenly divide both AB and CD.
B
E
A
10 * FC
D
F
C
7 * FC
E
F
C
F
C
F
A
E
C
F
A
C
F
C
F
C
F
C
F
C
F
C
F
C
Figure 2-2: Comparing the Results
2.1.2 Modular Multiplicative Inverse
By computing the Euclidian algorithm on a number of interest and the modulus, we can directly
compute the MMI of the number of interest. In these calculations, it is efficient to accumulate
the quotients at each step as coefficients of the two numbers. This is often referred to as the
Extended Euclidean algorithm.
If we put numbers to the lines in Figure 2-1, for example 50 and 35, we have the following
example.
Example 2-1
1. Start by writing the two numbers as below.
50 = 50 ( 1) + 35 ( 0)
35 = 50 ( 0) + 35 ( 1)
Page 5 of 17
The Algebra of Encryption
18 July 2011
2. Find the quotient of the two numbers.
q = int(50 / 35) = 1
3. Multiply the second equation through by the quotient and subtract from the first.
50 = 50 ( 1) + 35 ( 0)
35 = 50 ( 0) + 35 ( 1), q = 1
15 = 50 ( 1) + 35 ( -1)
4. Repeat steps 2 and 3 comparing the residues until the result is 0
50 = 50 ( 1) + 35 ( 0)
35 = 50 ( 0) + 35 ( 1), q = 1
15 = 50 ( 1) + 35 ( -1), q = 2
5 = 50 ( -2) + 35 ( 3), q = 3
0 = 50 ( 7) + 35 (-10)
The GCD is the last residue, in this case, 5. If we work this example again using the numbers 10
and 7, we will find that the GCD is 1. If two numbers have a GCD of 1, they are said to be
relatively prime or coprime. (Coprime, 2011)
Now, how does this find the MMI? If we return to Example 1-11, we wish to find the MMI of 4
modulo 13. If we compute the Extended Euclidian algorithm of 13 and 4 we have the following.
13 = 13 (
4 = 13 (
1 = 13 (
1) + 4 ( 0)
0) + 4 ( 1), q = 3
1) + 4 ( -3)
When searching for the MMI, we can stop when the residue is 1. Now compare our result with
Equation 1-6. (-3) is the MMI of 4 modulo 13. We prefer to deal with non-negative numbers, so
the result needs to be adjusted. We do this by adding and subtracting 13 * 4 and collecting
terms.
1 = 13 (1
) + 4 (-3
) + 13 (-4) + 4 (13)
1 = 13 (1 - 4) + 4 (-3 + 13)
1 = 13 (
-3) + 4 (
10)
Thus 10 is the MMI of 4 mod 13. This result agrees with our findings in Example 1-11.
Mentioned above is that we can stop searching for the MMI when the residue is 1. This is
because the next line would result in a residue of zero. When we arrive at a residue of zero but
the previous line does not have a residue of 1, the number of interest and the modulus are not
coprime and no MMI exists for that number of interest for that modulus. (Burton, 2007, pp. 7677)
2.2 Modular Exponentiation
Many encryption schemes employ modular exponentiation. When dealing with very large
numbers, such as in cryptography, efficient computing is essential. (Garrett, 2004, p. 123)
describes an algorithm entitled Fast Modula Exponentiation.
Page 6 of 17
The Algebra of Encryption
ο‚·
ο‚·
ο‚·
ο‚·
18 July 2011
Initiate X = base, E = exponent, Y = 1: x e ≑ y mod m.
If E is odd: replace Y by (X * Y) mod m and replace E = E - 1.
E is now even: replace X by (X * X) mod m and replace E = E / 2.
When E = 0: x e ≑ y mod m.
X keeps the current binary power of the base number. Y is the accumulator. Note how E is
parsed for its binary bit values.
1. Starting from the least significant bit, check if the bit is 1 (if E is odd).
2. If E is odd, accumulate the corresponding power of the base: Y = Y * X. The decrement
of E simply makes the division by 2 in the next step easier.
3. E is now even, update X to the next binary power of the base: X = X * X. Divide E by 2,
that is to say, right shift so the next binary bit is in the least significant bit position.
4. Loop to step 2 until E = 0.
Example 2-2
First let’s calculate 311 without a modulus.
E = 11 = 8 + 2 + 1.
Y = 311 = 38 * 32 * 31 = 6561 * 9 * 3 = 177147
Notes
X
E
1
Initialization
11
3 =3
E is odd
11 βˆ’ 1 = 10
2
E is even
10 ÷ 2 = 5
3 =9
E is odd
5βˆ’1=4
E is even
34 = 81
4÷2=2
8
E is even
2÷2=1
3 = 6561
E is odd
1βˆ’1=0
Y
1
3βˆ—1=3
9 βˆ— 3 = 27
6561 βˆ— 9 βˆ— 3 = 177147
Example 2-3
Now let’s calculate 311 π‘šπ‘œπ‘‘ 527
Y = 311 = 38 * 32 * 31 = 237 * 9 * 3 mod 527 = 75 mod 527
Notes
X
E
Initialization
11
31 = 3
E is odd
11 βˆ’ 1 = 10
2
E is even
10 ÷ 2 = 5
3 π‘šπ‘œπ‘‘ 527 = 9
E is odd
5βˆ’1=4
4
E is even
3 π‘šπ‘œπ‘‘ 527 = 81
4÷2=2
8
E is even
2÷2=1
3 π‘šπ‘œπ‘‘ 527 = 237
E is odd
1βˆ’1=0
Y
1
3 βˆ— 1 π‘šπ‘œπ‘‘ 527 = 3
9 βˆ— 3 = 27 π‘šπ‘œπ‘‘ 527
237 βˆ— 9 βˆ— 3 π‘šπ‘œπ‘‘ 527
= 75
You can verify that 177,147 = 75 π‘šπ‘œπ‘‘ 527.
Notice that everything happens the same with a modulus as without a modulus. The only
difference is in the size of the numbers - the calculation results are reduced modulo 527.
Page 7 of 17
The Algebra of Encryption
18 July 2011
Also notice the significant role of multiplication in this algorithm. We multiply X*X for each
exponent bit position. We multiply X*Y when the exponent’s least significant bit is 1. In
Example 2-2 we dealt with larger numbers. In Example 2-3 the numbers were limited by the
modulus and remained smaller.
Consider multiplying two 4 digit binary numbers and two 2 digit binary numbers.
1111
x
1111
----------------1111
1111
1111
+
1111
----------------11100001
11
x
11
----------------11
+
11
----------------1001
The 4 digit multiplication involved the addition of 16 bits. The 2 digit multiplication involved
the addition of only 4 bits. Since the size of numbers cryptography deals with are usually larger
than a computer’s arithmetic logic unit can deal with, special Big Integer computational
packages such as Multiple Precision Integers and Rationals (MPIR) (MPIR home page) or Gnu
Multiple Precision (GMP) (The GNU Multiple Precision Arithmetic Library) are usually
employed. The smaller the modulus, the smaller the numbers we deal with, and the less time
involved in multiplying.
2.3 Chinese Remainder Theorem (CRT)
Chinese Remainder Theorem is frequently used in cryptography to reduce the calculation time
inherent in calculating with large numbers. This time reduction is accomplished by doing a few
extra calculations, but with much smaller numbers. (Chinese remainder theorem, 2011) Also, a
significant part of the calculations may be done once, in advance, and then used for subsequent
calculations.
2.3.1 Pre-calculations
This discussion and example follows (Stallings, 2011, pp. p 254-257):
Choose the Factors of M
Choose π‘š1 and π‘š2 . The numbers must be coprime.
π‘š1 = 37
π‘š2 = 49
𝑀 = π‘š1 βˆ— π‘š2 = 37 βˆ— 49 = 1813
Calculate π‘΄π’Š
Calculate 𝑀𝑖 = 𝑀 ÷ π‘šπ‘– for each π‘šπ‘– .
To continue the example:
Page 8 of 17
The Algebra of Encryption
18 July 2011
𝑀1 = 𝑀 ÷ π‘š1 = 1813 ÷ 37 = 49
𝑀2 = 𝑀 ÷ π‘š2 = 1813 ÷ 49 = 37
Note that this is NOT a simple swapping of π‘š1 and π‘š2 . The example only gives that appearance
because we are using two factors. If M had three or more factors, this would be more clear.
Calculate π‘΄π’Šβˆ’πŸ π’Žπ’π’… π’Žπ’Š
Use the Extended Euclidean Algorithm to calculate π‘€π‘–βˆ’1 π‘šπ‘œπ‘‘ π‘šπ‘– for each π‘šπ‘– .
To continue the example:
𝑀1βˆ’1 π‘šπ‘œπ‘‘ π‘š1 = 49βˆ’1 π‘šπ‘œπ‘‘ 37 ≑ 34
𝑀2βˆ’1 π‘šπ‘œπ‘‘ π‘š2 = 37βˆ’1 π‘šπ‘œπ‘‘ 49 ≑ 4
Calculate π‘¨π’Š
For convenience, define 𝐴𝑖 = 𝑀𝑖 βˆ— π‘€π‘–βˆ’1 π‘šπ‘œπ‘‘ 𝑀 for each π‘šπ‘– .
In the example:
𝐴1 = 𝑀1 βˆ— 𝑀1βˆ’1 π‘šπ‘œπ‘‘ 𝑀 = 49 βˆ— 34 π‘šπ‘œπ‘‘ 1813 ≑ 1666
𝐴2 = 𝑀2 βˆ— 𝑀2βˆ’1 π‘šπ‘œπ‘‘ 𝑀 = 37 βˆ— 4 π‘šπ‘œπ‘‘ 1813 ≑ 148
2.3.2 Addition
To add two numbers, complete the addition for each π‘šπ‘– , then combine the results.
Add for each π’Žπ’Š
Compute π‘₯ + 𝑦 = 𝑧𝑖 π‘šπ‘œπ‘‘ π‘šπ‘– for each π‘šπ‘– .
For example (Stallings, 2011, pp. p 254-257), to add 973 and 678 mod 1813:
973 π‘šπ‘œπ‘‘ 37 = 11
+
678 π‘šπ‘œπ‘‘ 37 = 12
--------------------------------𝑧1 = 23 π‘šπ‘œπ‘‘ 37
973 π‘šπ‘œπ‘‘ 49 = 42
+
678 π‘šπ‘œπ‘‘ 49 = 41
--------------------------------𝑧2 = 34 π‘šπ‘œπ‘‘ 49
Combine the results
Find the result of (π‘₯ + 𝑦) π‘šπ‘œπ‘‘ 𝑀 = (𝑧1 βˆ— 𝐴1 + 𝑧2 βˆ— 𝐴2 ) π‘šπ‘œπ‘‘ 𝑀
To complete the example:
(973 + 678) π‘šπ‘œπ‘‘ 1813 = (23 βˆ— 1666 + 34 βˆ— 148) π‘šπ‘œπ‘‘ 1813 ≑ 1651
2.3.3 Multiplication
To multiply two numbers, complete the multiplication for each π‘šπ‘– , then combine the results.
Multiply for each π’Žπ’Š
Compute π‘₯ βˆ— 𝑦 = 𝑧𝑖 π‘šπ‘œπ‘‘ π‘šπ‘– for each π‘šπ‘– .
For example (Stallings, 2011, pp. p 254-257), to multiply 1651 and 73 mod 1813:
Page 9 of 17
The Algebra of Encryption
18 July 2011
1651 π‘šπ‘œπ‘‘ 37 = 23
x
73 π‘šπ‘œπ‘‘ 37 = 36
--------------------------------𝑧1 = 14 π‘šπ‘œπ‘‘ 37
1651 π‘šπ‘œπ‘‘ 49 = 34
x
73 π‘šπ‘œπ‘‘ 49 = 24
--------------------------------𝑧2 = 32 π‘šπ‘œπ‘‘ 49
Combine the results
Find the result of (π‘₯ βˆ— 𝑦) π‘šπ‘œπ‘‘ 𝑀 = (𝑧1 βˆ— 𝐴1 + 𝑧2 βˆ— 𝐴2 ) π‘šπ‘œπ‘‘ 𝑀
To complete the example:
(1651 βˆ— 73) π‘šπ‘œπ‘‘ 1813 = (14 βˆ— 1666 + 32 βˆ— 148) π‘šπ‘œπ‘‘ 1813 ≑ 865
2.4 Euler’s Totient Function
Euler’s totient function, 𝛷(𝑛), identifies the number of integers, less than n, that are relatively
prime to n. A good treatment of Euler’s Totient function can be found in (Burton, 2007, pp. 131135).
(Stallings, 2011, pp. 249-250) presents Euler’s totient function as 𝛷(𝑝) = 𝑝 βˆ’ 1 for prime p.
This is correct, but not good for a general definition of the function. (Burton, 2007, pp. 131-135)
gives a more versatile definition as 𝛷(π‘π‘˜ ) = π‘π‘˜ βˆ’ π‘π‘˜βˆ’1 . It is easy to see that this version
degenerates to the previous version in the case of π‘˜ = 1.
To determine the totient of a composite number, we must know the prime factors of the number.
Given 𝑛 = 𝑝𝑖 βˆ— π‘ž 𝑗 , 𝛷(𝑛) = 𝛷(𝑝𝑖 ) βˆ— 𝛷(π‘ž 𝑗 ) = (𝑝𝑖 βˆ’ π‘π‘–βˆ’1 ) βˆ— (π‘ž 𝑗 βˆ’ π‘ž π‘—βˆ’1 ) (Burton, 2007, pp.
131-135). Again, if 𝑖 and 𝑗 = 1, then the equation degenerates to 𝛷(𝑛) = 𝛷(𝑝) βˆ— 𝛷(π‘ž) =
(𝑝 βˆ’ 1) βˆ— (π‘ž βˆ’ 1) as described by (Stallings, 2011, pp. 249-250).
Example 2-4
To calculate 𝛷(21), the first form is adequate. First we must know the prime factors of 21,
namely 3 and 7.
𝛷(21) = (3 βˆ’ 1) βˆ— (7 βˆ’ 1) = 2 βˆ— 6 = 12
Therefore, the number of integers less than 21 relatively prime to 21 should count to 12.
1, 2, 4, 5, 8, 10, 11, 13, 16, 17, 19, 20 are the 12 numbers less than 21 that are relatively prime to
21.
Example 2-5
To calculate 𝛷(20), the second form of the totient function is required. The prime factors of 20
are 22 and 5.
𝛷(20) = (22 βˆ’ 21 ) βˆ— (51 βˆ’ 50 ) = (4 βˆ’ 2) βˆ— (5 βˆ’ 1) = 2 βˆ— 4 = 8
The 8 integers less than 20 relatively prime to 20 are 1, 3, 7, 9, 11, 13, 17, 19.
3 Public Key Cryptography - RSA
Euler’s theorem states that if 𝑛 β‰₯ 1 and gcd(π‘Ž, 𝑛) = 1, then π‘Žπ›·(𝑛) ≑ 1 π‘šπ‘œπ‘‘ 𝑛. (Burton, 2007, p.
137) This is the heart of the RSA encryption and decryption method. (RSA, 2011).
If we choose two prime numbers p and q we can form 𝑛 = 𝑝 βˆ— π‘ž and determine 𝛷(𝑛). If we then
choose an encryption exponent e so that gcd(𝑒, 𝛷(𝑛)) = 1, we can find a decryption exponent d
Page 10 of 17
The Algebra of Encryption
18 July 2011
such that 𝑑 βˆ— 𝑒 ≑ 1 π‘šπ‘œπ‘‘ 𝛷(𝑛). The exponent d is merely the MMI of 𝑒 π‘šπ‘œπ‘‘ 𝛷(𝑛), and we
know from section 2.1.2 how to find d. Note that 𝑑 βˆ— 𝑒 = π‘š βˆ— 𝛷(𝑛) + 1 for some multiplier m.
Now take a message expressed as a number M. Calculate the encrypted message by using the
encryption exponent.
𝐢 = 𝑀𝑒 π‘šπ‘œπ‘‘ 𝑛
To decrypt, use the decryption exponent. Calculate 𝐢 𝑑 π‘šπ‘œπ‘‘ 𝑛 = 𝑀.
𝐢 𝑑 = (𝑀𝑒 )𝑑 = π‘€π‘šβˆ—π›·(𝑛)+1 = π‘€π‘šβˆ—π›·(𝑛) βˆ— 𝑀 = (𝑀𝛷(𝑛) )π‘š βˆ— 𝑀
Taking note of Euler’s theorem, if the decryption calculations are made modulo n, we have
𝐢 𝑑 = (1)π‘š βˆ— 𝑀 π‘šπ‘œπ‘‘ 𝑛 = 𝑀
Thus we have successfully decrypted the message, assuming gcd(𝑀, 𝑛) = 1 to satisfy the
requirements of Euler’s theorem. β€œIn the unlikely event that M and n are not relatively prime, a
similar argument establishes that [𝐢 𝑑 ] ≑ 𝑀 π‘šπ‘œπ‘‘ 𝑝 and [𝐢 𝑑 ] ≑ 𝑀 π‘šπ‘œπ‘‘ π‘ž, which then yields the
desired congruence [𝐢 𝑑 ] ≑ 𝑀 π‘šπ‘œπ‘‘ 𝑛. We omit the details.” (Burton, 2007, p. 204)
RSA involves calculating modular exponentials. The example uses normal encryption and CRT
decryption. Note that CRT only benefits decryption because the factors of the modulus must be
known and they are part of the private key.
Let:
𝑝 = 17
π‘ž = 31
𝑒 = 11
message: 𝑀 = 3
calculate 𝑛 = 𝑝 βˆ— π‘ž = 17 βˆ— 31 = 527
calculate 𝛷(𝑛) = (𝑝 βˆ’ 1) βˆ— (π‘ž βˆ’ 1) = 16 βˆ— 30 = 480
calculate 𝑑 = 𝑒 βˆ’1 π‘šπ‘œπ‘‘ 𝛷(𝑛) ≑ 131
3.1
CRT Pre-calculations
𝑃 = 𝑛 ÷ 𝑝 = 31
π‘ƒβˆ’1 π‘šπ‘œπ‘‘ 𝑝 ≑ 11
𝐴𝑝 = 𝑃 βˆ— π‘ƒβˆ’1 π‘šπ‘œπ‘‘ 𝑛 = 31 βˆ— 11 π‘šπ‘œπ‘‘ 527 ≑ 341
𝑄 = 𝑛 ÷ π‘ž = 17
𝑄 βˆ’1 π‘šπ‘œπ‘‘ π‘ž ≑ 11
π΄π‘ž = 𝑄 βˆ— 𝑄 βˆ’1 π‘šπ‘œπ‘‘ 𝑛 = 17 βˆ— 11 π‘šπ‘œπ‘‘ 527 ≑ 187
𝑑𝑝 = 𝑑 π‘šπ‘œπ‘‘ 𝛷(𝑝) = 131 π‘šπ‘œπ‘‘ 16 ≑ 3
π‘‘π‘ž = 𝑑 π‘šπ‘œπ‘‘ 𝛷(π‘ž) = 131 π‘šπ‘œπ‘‘ 30 ≑ 11
3.2
Encrypt
𝐢 = 𝑀𝑒 π‘šπ‘œπ‘‘ 𝑛 = 311 π‘šπ‘œπ‘‘ 527 ≑ 75 (See Example 2-3.)
Page 11 of 17
The Algebra of Encryption
18 July 2011
3.3 Decrypt
Note the use of 𝑑𝑝 and π‘‘π‘ž in place of the normal decrypt exponent. These exponents are d
modulus 𝛷(𝑝) and 𝛷(π‘ž), NOT d modulus p and q.
𝑀𝑝 = 𝐢 𝑑𝑝 π‘šπ‘œπ‘‘ 𝑝 = 753 π‘šπ‘œπ‘‘ 17
= 73 π‘šπ‘œπ‘‘ 17 ≑ 3
3.4
π‘€π‘ž = 𝐢 π‘‘π‘ž π‘šπ‘œπ‘‘ π‘ž = 7511 π‘šπ‘œπ‘‘ 31
= 1311 π‘šπ‘œπ‘‘ 31 ≑ 3
Combine the results
𝑀 = (𝑀𝑝 βˆ— 𝐴𝑝 + π‘€π‘ž βˆ— π΄π‘ž ) π‘šπ‘œπ‘‘ 𝑛 = (3 βˆ— 341 + 3 βˆ— 187) π‘šπ‘œπ‘‘ 527 ≑ 3
3.5 Conclusion
Decryption is accomplished using smaller exponents and smaller moduli. Multiplication is a
large part of calculating modular exponentials. When computing many exponentials using the
same private key, time savings becomes significant.
4 How to Share a Secret
(Shamir, November, 1979) describes how to share secret information such that the holders of a
share cannot individually obtain the secret. Only if a certain minimum number of share holders
collaborate can the secret information be identified.
A simple description of this method is to use a curve described by a polynomial function.
𝑓(π‘₯) = π‘Žπ‘‘ π‘₯ 𝑑 + π‘Žπ‘‘βˆ’1 π‘₯ π‘‘βˆ’1 β‹― π‘Ž1 π‘₯ + π‘Ž0
Equation 4-1
Typically, π‘Ž0 is the secret information, for example a secret key, and the other t coefficients in
the polynomial may be chosen at random. If two or more elements comprise the secret key, such
as 𝑝, π‘ž, π‘Žπ‘›π‘‘ 𝑑 , necessary for CRT decryption of RSA, then more coefficients may be set and the
remainder chosen at random.
Basic algebra tells us that when we have 𝑑 + 1 unknowns - the 𝑑 + 1 coefficients, we need to
know 𝑑 + 1 points on the curve to identify all of the coefficients. We really only want the one
coefficient, π‘Ž0 , but even that cannot be determined without the required minimum number of
points from the curve.
Points on the curve - x and f(x) pairs, are the secret shares given to the trusted authorities. Many
more than 𝑑 + 1 secret shares may be given out. But, only if a minimum of 𝑑 + 1 trusted
authorities agree to provide their secret share and collaborate, can the secret information be
identified. The secret information - coefficient(s) of the polynomial, may be identified by any
linear algebra method for solving a system of simultaneous equations.
The x values may be as simple as indexes, incremented between one share holder to the next.
Only the value of f(x) must be held secret.
Page 12 of 17
The Algebra of Encryption
18 July 2011
5 Paillier Cryptography
The cryptographic systems proposed by (Paillier, 1999) involve polynomial expansion of
exponentiation. Paillier proposed three systems, one will be discussed here. It is similar to RSA
cryptography.
5.1 Carmichael Function
In order to understand the algebra of Paillier cryptography, we need to understand the
Carmichael function. The Carmichael function, πœ†(𝑛), is similar to Euler’s totient function 𝛷(𝑛).
(Paillier, 1999, p. 2)
πœ†(𝑛) = π‘™π‘π‘š(𝑝 βˆ’ 1, π‘ž βˆ’ 1)
Equation 5-1
where 𝑛 = π‘π‘ž, 𝑝 and π‘ž are distinct primes, and lcm stands for Least Common Multiple.
The Carmichael function also has some useful properties for Paillier cryptography. For any
integer 𝑀 that is coprime with 𝑛:
πœ†
{ π‘€π‘›πœ† ≑ 1 π‘šπ‘œπ‘‘ 𝑛2
𝑀 ≑ 1 π‘šπ‘œπ‘‘ 𝑛
Equation 5-2
Which implies there exists some integer π‘Ž and 𝑏 such that
πœ†
{ π‘€πœ†π‘› = π‘Žπ‘›2+ 1
𝑀 = 𝑏𝑛 + 1
Equation 5-3
5.2 Preliminaries
Choose two safe prime numbers p and q, form 𝑛 = 𝑝 βˆ— π‘ž, and determine πœ† = π‘™π‘π‘š(𝑝 βˆ’ 1, π‘ž βˆ’ 1).
Safe prime numbers are of the form 2𝑝 + 1, where p is also a prime. (Safe prime, 2010)
Define the function
𝐿(𝑒) =
π‘’βˆ’1
𝑛
Equation 5-4
Choose a generator value g such that
gcd(𝐿(𝑔 πœ† π‘šπ‘œπ‘‘ 𝑛2 ), 𝑛) = 1
Equation 5-5
The public key is (g, n). The private key is Ξ».
5.3 Encrypt
The plaintext message is π‘š < 𝑛. Choose a random number π‘Ÿ < 𝑛. Calculate the encrypted
message
𝑐 = π‘”π‘š π‘Ÿ 𝑛 π‘šπ‘œπ‘‘ 𝑛2
Equation 5-6
Page 13 of 17
The Algebra of Encryption
18 July 2011
5.4 Decrypt
Plaintext
𝐿(𝑐 πœ† π‘šπ‘œπ‘‘ 𝑛2 )
π‘š=
π‘šπ‘œπ‘‘ 𝑛
𝐿(𝑔 πœ† π‘šπ‘œπ‘‘ 𝑛2 )
Equation 5-7
5.5 Explanation
The Generator g
Starting from the Carmichael function Equation 5-3.
𝑔 πœ† = 1 + π‘Žπ‘›
𝑔 πœ†π‘₯ = (1 + π‘Žπ‘›)π‘₯
Equation 5-8
If we use binomial expansion of the polynomial, we get (Binomial theorem, 2011)
π‘₯
(1 + π‘Žπ‘›)π‘₯ = 1π‘₯ + π‘₯ βˆ— (1π‘₯βˆ’1 ) βˆ— (π‘Žπ‘›)1 + ( ) βˆ— (1π‘₯βˆ’2 ) βˆ— 𝑛2 + β‹―
2
where
π‘₯
π‘₯!
( )=
𝑖
𝑖! (π‘₯ βˆ’ 𝑖)!
When we reduce modulo 𝑛2 , all of the polynomial terms of 𝑛2 and higher drop out. We are left
with
𝑔 πœ†π‘₯ = (1 + π‘Žπ‘›)π‘₯ = (1 + π‘₯π‘Žπ‘›) π‘šπ‘œπ‘‘ 𝑛2
Equation 5-9
The Decrypt Numerator
π‘πœ† βˆ’ 1
(π‘”π‘š π‘Ÿ 𝑛 )πœ† βˆ’ 1
π‘”π‘šπœ† π‘Ÿ π‘›πœ† βˆ’ 1
𝐿(𝑐 πœ† π‘šπ‘œπ‘‘ 𝑛2 ) =
π‘šπ‘œπ‘‘ 𝑛2 =
π‘šπ‘œπ‘‘ 𝑛2 =
π‘šπ‘œπ‘‘ 𝑛2
𝑛
𝑛
𝑛
Applying Equation 5-2.
π‘”π‘šπœ† (1) βˆ’ 1
𝐿(𝑐 π‘šπ‘œπ‘‘ 𝑛 ) ≑
π‘šπ‘œπ‘‘ 𝑛2
𝑛
πœ†
2
Equation 5-10
Applying Equation 5-9 to π‘”π‘šπœ†
(1 + π‘šπ‘Žπ‘›) βˆ’ 1
π‘šπ‘Žπ‘›
𝐿(𝑐 πœ† π‘šπ‘œπ‘‘ 𝑛2 ) ≑
π‘šπ‘œπ‘‘ 𝑛2 =
π‘šπ‘œπ‘‘ 𝑛2 = π‘šπ‘Ž π‘šπ‘œπ‘‘ 𝑛2
𝑛
𝑛
The Decrypt Denominator
(1 + π‘Žπ‘›) βˆ’ 1
π‘”πœ† βˆ’ 1
𝐿(𝑔 π‘šπ‘œπ‘‘ 𝑛 ) =
π‘šπ‘œπ‘‘ 𝑛2 =
π‘šπ‘œπ‘‘ 𝑛2 = π‘Ž π‘šπ‘œπ‘‘ 𝑛2
𝑛
𝑛
Equation 5-11
πœ†
2
Page 14 of 17
The Algebra of Encryption
18 July 2011
The Decrypt
Combining Equation 5-10 and Equation 5-11 into Equation 5-7, we get
𝐿(𝑐 πœ† π‘šπ‘œπ‘‘ 𝑛2 )
π‘šπ‘Ž π‘šπ‘œπ‘‘ 𝑛2
π‘š=
π‘šπ‘œπ‘‘ 𝑛 =
π‘šπ‘œπ‘‘ 𝑛
π‘Ž π‘šπ‘œπ‘‘ 𝑛2
𝐿(𝑔 πœ† π‘šπ‘œπ‘‘ 𝑛2 )
5.6 Blinding
Cryptographic blinding allows for a message to be multiplied by a specially treated random
number, while still allowing the message to be decrypted without knowledge of the random
number. (Blinding (cryptography), 2011)
Due to the properties of Paillier cryptography noted in Equation 5-2, any succession of blinding
factors may be applied to the ciphertext without affecting the successful decryption.
To see this, apply a succession of π‘˜ blinding factors to the same message.
𝑐 = π‘”π‘š βˆ— π‘Ÿ1𝑛 π‘Ÿ2𝑛 β‹― π‘Ÿπ‘˜π‘› π‘šπ‘œπ‘‘ 𝑛2
= π‘”π‘š βˆ— π‘Ÿ 𝑛 π‘šπ‘œπ‘‘ 𝑛2
where π‘Ÿ = βˆπ‘˜ π‘Ÿπ‘– . π‘Ÿ is still a random number and does not affect the successful decryption of the
message.
5.7 Additive Homomorphic Properties
Paillier cryptography includes an interesting homomorphic property in that the multiplication of
two ciphertexts is equivalent to the addition of the respective plaintexts. (Paillier, 1999, p. 13)
To see this, start with two messages π‘š1 and π‘š2 and encrypt.
𝑐1 = π‘”π‘š1 π‘Ÿ1𝑛 π‘šπ‘œπ‘‘ 𝑛2
𝑐2 = π‘”π‘š2 π‘Ÿ2𝑛 π‘šπ‘œπ‘‘ 𝑛2
Now multiply the two ciphertexts.
𝑐1 βˆ— 𝑐2 = π‘”π‘š1 π‘Ÿ1𝑛 βˆ— π‘”π‘š2 π‘Ÿ2𝑛 π‘šπ‘œπ‘‘ 𝑛2
= π‘”π‘š1 π‘”π‘š2 βˆ— π‘Ÿ1𝑛 π‘Ÿ2𝑛 π‘šπ‘œπ‘‘ 𝑛2
= π‘”π‘š1 +π‘š2 βˆ— (π‘Ÿ1 π‘Ÿ2 )𝑛 π‘šπ‘œπ‘‘ 𝑛2
= π‘”π‘š3 βˆ— π‘Ÿ3𝑛 π‘šπ‘œπ‘‘ 𝑛2
where m3 = m1 + m2 and r3 = r1 βˆ— r2 . r3 is a random number, and the product c1 βˆ— c2 will
correctly decrypt as m1 + m2 .
5.8 Pre-calculations
Some elements of Paillier cryptography can be pre-calculated.
Decryption
All of the elements of the decrypt denominator are known to the decryptor and may be precalculated.
βˆ’1
π‘”πœ† βˆ’ 1
πœ‡=[
] π‘šπ‘œπ‘‘ 𝑛2
𝑛
Equation 5-12
Page 15 of 17
The Algebra of Encryption
18 July 2011
This is of course an MMI modulo n2 .
Decryption then becomes
π‘š = [𝐿(𝑐 πœ† ) βˆ— πœ‡ π‘šπ‘œπ‘‘ 𝑛2 ] π‘šπ‘œπ‘‘ 𝑛
Equation 5-13
Determine Ξ»
Because p and q are safe primes, we have the following determination for Ξ».
𝑝 = 2𝑝′ + 1
π‘ž = 2π‘ž β€² + 1
where 𝑝′ and π‘žβ€² are also primes.
This leads to the following pre-calculation for Ξ».
πœ† = π‘™π‘π‘š(𝑝 βˆ’ 1, π‘ž βˆ’ 1) =
=
(𝑝 βˆ’ 1)(π‘ž βˆ’ 1)
gcd(𝑝 βˆ’ 1, π‘ž βˆ’ 1)
(2𝑝′ )(2π‘ž β€² )
4π‘β€²π‘žβ€²
=
β€²
β€²
gcd(2𝑝 , 2π‘ž )
2
= 2π‘β€²π‘žβ€²
Equation 5-14
5.9 Observations
Choosing g with a common factor
The choosing of g, Equation 5-5, allows a vulnerability. If 𝑔 shares a factor with 𝑛, the division
of the numerator, 𝐿(𝑔 πœ† βˆ’ 1), by the denominator, 𝑛, does not always yield an integer. If this
goes un-noticed, and the result is truncated, the equality of Equation 5-5 may hold and the value
of 𝑔 will be accepted.
The public key is (𝑔, 𝑛). If 𝑔 shares a factor with 𝑛, the astute adversary would be able to
employ the Euclid algorithm to factor n and thereby find the secret key πœ†.
Choosing g without Blinding
If one should choose 𝑔 = 1 + π‘Žπ‘›, for some integer π‘Ž, blinding is in fact necessary. Consider
encryption Equation 5-6 without the blinding factor π‘Ÿ 𝑛 .
𝑐 = π‘”π‘š = (1 + π‘Žπ‘›)π‘š
Following the same process as the derivation of Equation 5-9, we arrive at
𝑐 = (1 + π‘Žπ‘›)π‘š = (1 + π‘šπ‘Žπ‘›) π‘šπ‘œπ‘‘ 𝑛2
Because (g, n) is the public key, the value of a is known publicly. Thus m can be easily
calculated from c. Thus Blinding is necessary if 𝑔 is chosen such that 𝑔 = 1 + π‘Žπ‘›.
6 Works Cited
Binomial theorem. (2011, June 21). Retrieved July 10, 2011, from Wikipedia:
http://en.wikipedia.org/wiki/Binomial_expansion
Page 16 of 17
The Algebra of Encryption
18 July 2011
Blinding (cryptography). (2011, June 3). Retrieved July 10, 2011, from Wikipedia:
http://en.wikipedia.org/wiki/Blinding_(cryptography)
Burton, D. M. (2007). Elementary Number Theory, Sixth Edition. New York, New York 10020:
McGraw-Hill Higher Education.
Chinese remainder theorem. (2011, July 2). Retrieved July 7, 2011, from Wikipedia:
http://en.wikipedia.org/wiki/Chinese_remainder_theorem
Coprime. (2011, June 25). Retrieved July 7, 2011, from Wikipedia:
http://en.wikipedia.org/wiki/Coprime
Euclidean algorithm. (2011, June 30). Retrieved July 7, 2011, from Wikipedia:
http://en.wikipedia.org/wiki/Euclid_algorithm
Garrett, P. (2004). The Mathematics of Coding Theory. Upper Saddle River, New Jersey:
Pearson Prentice Hall.
Modular arithmetic. (2011, July 5). Retrieved July 7, 2011, from Wikipedia:
http://en.wikipedia.org/wiki/Modular_arithmetic
MPIR home page. (n.d.). Retrieved July 9, 2011, from MPIR: http://www.mpir.org/
Paillier, P. (1999). Public-Key Cryptosystems Based on Composite Degree Residuosity Clases.
Advances in Cryptology - Eurocrypt '99 , pp. 223-238.
RSA. (2011, July 8). Retrieved July 8, 2011, from Wikipedia: http://en.wikipedia.org/wiki/RSA
Safe prime. (2010, August 24). Retrieved July 9, 2011, from Wikipedia:
http://en.wikipedia.org/wiki/Safe_prime
Shamir, A. (November, 1979). How to Share a Secret. Communications of the ACM , 612-613.
Stallings, W. (2011). Cryptography and Network Security, Principles and Practice, Fifth
Edition. Prentice Hall.
The GNU Multiple Precision Arithmetic Library. (n.d.). Retrieved July 9, 2011, from GNU:
http://gmplib.org/
Page 17 of 17