Sie sind auf Seite 1von 18

Modular Arithmetic

• Several important cryptosystems make use


of modular arithmetic. This is when the
answer to a calculation is always in the
range 0 – m where m is the modulus.
• To calculate the value of n mod m, you take
away as many multiples of m as possible
until you are left with an answer between 0
and m.
If n is a negative number then you add as
many multiples of m as necessary to get an
answer in the range 0 – m.

Examples
17 mod 5 = 2 7 mod 11 = 7
20 mod 3 = 2 11 mod 11 = 0
-3 mod 11 = 8 -1 mod 11 = 10
25 mod 5 = 0 -11 mod 11 = 0
• Two numbers r and s are said to be
“congruent mod m” if
r mod m = s mod m
• In this case we write r  s mod m
• The difference between r and s will be a
multiple of m
So r-s = km for some value of k

• E.g. 4 9  1419  -1 -6 mod 5


A good thing about modular arithmetic is
that the numbers you are working with will
be kept relatively small. At each stage of an
algorithm, the mod function should be
applied.
Thus to multiply 39 * 15 mod 11 we first
take mods to get
39 mod 11 = 6 and 15 mod 11= 4
The multiplication required is now
6*4 mod 11 = 24 mod 11 = 2
• The computational complexity of
calculating a mod is O(b2)
• Therefore the computational complexity of
performing a multiplication mod m is O(b2)
• And the complexity of calculating xn mod m
is O(b3) where b is the size of n.

• Thus using modular arithmetic does not in


general increase the complexity of
algorithms.
Algorithm for modular exponentiation
To Compute xn mod m
Initialise y=1, u=x mod m
Repeat
if n mod 2=1 then y=(y*u) mod m
n=n div 2
u=(u*u) mod m
Until n=0
Output y
Modular Division
What is 5 ÷ 3 mod 11?
We need to multiply 5 by the inverse of 3 mod 11
When you multiply a number by its inverse, the answer is
1.
Thus the inverse of 2 is ½ since 2* ½ = 1
The inverse of 3 mod 11 is 4 since 3*4=1 mod 11
Thus 5 ÷ 3 mod 11 = 5*4 mod 11 = 9 mod 11
• It is relatively easy to find the inverse of
x mod m using Euclids algorithm which has
computational complexity O(b3) where b is
the size of m.
• Note however that x does not have an
inverse mod m unless x and m are co-prime
(have no factors in common).
Prime Numbers - Advanced Concepts
Prime Numbers
A Prime Number can be divided evenly only by 1 or
itself. And it must be a whole number greater than 1.
Example: 2, 3, 5, 7, 11, etc.
Twin Primes
A pair of prime numbers that differ by 2 (successive
odd numbers that are both Prime numbers).
Examples: (3,5), (5,7), (11,13), ...
It is not known whether the set of twin prime
numbers ends or not.
Co-primes or Relatively prime numbers

A pair of numbers not having any common


factors other than 1 or -1. (Or alternatively their
Greatest Common Division is 1 or -1)

Example: 15 and 28 are co-prime, because the


factors of 15 (1,3,5,15),
and
the factors of 28 (1,2,4,7,14,28) are not in
common (except for 1).
The GCD of two numbers is the largest number
that divides both of them without leaving a
remainder

The Euclidean algorithm is based on the


principle that the greatest common divisor of two
numbers does not change if the smaller number is
subtracted from the larger number.

For example, 21 is the GCD of 252 and 105


(252 = 21 × 12; 105 = 21 × 5);
since 252 − 105 = 147,
the GCD of 147 and 105 is also 21.
The GCD of two numbers a and b can be defined as the
product of the prime factors shared by the two numbers.

For example, since 462 can be factored into 2 × 3 × 7 × 11


and 1071 can be factored into 3 × 3 × 7 × 17, the greatest
common divisor of 462 and 1071 equals 21 = 3 × 7,

the product of their shared prime factors. If two numbers have


no prime factors in common, their greatest common divisor is
1—they are coprime.

A key advantage of the Euclidean algorithm is that it can find


the GCD efficiently without having to compute the prime
factors. Factorization of large integers is believed to be such a
difficult problem that many modern cryptography systems are
based upon it
For illustration, the Euclidean algorithm can be used to find the
greatest common divisor of

a = 1071 and b = 462.

To begin, multiples of 462 are subtracted from 1071


until the remainder is less than 462.
Two such multiples can be subtracted (q0 = 2), leaving a
remainder of 147

1071 = 2 × 462 + 147.

Then multiples of 147 are subtracted from 462 until the


remainder is less than 147.
Three multiples can be subtracted (q1 = 3), leaving a remainder of
21

462 = 3 × 147 + 21.


Then multiples of 21 are subtracted from 147 until the
remainder is less than 21.

Seven multiples can be subtracted (q2 = 7), leaving no


remainder

147 = 7 × 21 + 0.

Since the last remainder is zero, the algorithm ends with 21 as


the greatest common divisor of 1071 and 462.

This agrees with the GCD(1071, 462) found by prime


factorization
An old woman goes to market and a horse steps on her
basket and crashes the eggs. The rider offers to pay for the
damages and asks her how many eggs she had brought. She
does not remember the exact number, but when she had
taken them out two at a time, there was one egg left. The
same happened when she picked them out three, four, five,
and six at a time, but when she took them seven at a time
they came out even. What is the smallest number of eggs she
could have had?
Problems of this kind are all examples of what
universally became known as the Chinese Remainder
Theorem. In mathematical parlance the problems can be
stated as finding n, given its remainders of division by
several numbers m1, m2, ..., mk:

n = n1 (mod m1)
n = n2 (mod m2)
...
n = nk (mod mk)
Using the extended Euclidean algorithm for 3 and 4×5 = 20, we
find (-13) × 3 + 2 × 20 = 1, i.e. e1 = 40. Using the Euclidean
algorithm for 4 and 3×5 = 15, we get (-11) × 4 + 3 × 15 = 1.
Hence, e2 = 45.

Finally, using the Euclidean algorithm for 5 and 3×4 = 12, we get
5 × 5 + (-2) × 12 = 1, meaning e3 = -24. A solution x is therefore 2
× 40 + 3 × 45 + 2 × (-24) = 167.

All other solutions are congruent to 167 modulo 60, which means
that they are all congruent to 47 modulo 60.

Das könnte Ihnen auch gefallen