< Return to Video

Fermat and Euler (18 min)

  • 0:00 - 0:04
    In the previous segment we talked about
    modular inversion and we said the Euclid's
  • 0:04 - 0:08
    algorithm gives us an efficient way to
    find the inverse of an element modulo N.
  • 0:08 - 0:12
    In this segment we're going to forward
    through time and we're going to move to
  • 0:12 - 0:16
    the seventeenth and eighteenth century
    where we're going to talk about
  • 0:16 - 0:20
    Fermat and Euler contributions.
    Before that let's do a quick review of
  • 0:20 - 0:24
    what we discussed in the previous segment.
    So as usual I'm going to let N denote the
  • 0:24 - 0:28
    positive integer and let's just say that N
    happens to be a n-bit integer, in other
  • 0:28 - 0:32
    words it's between two to the n and two to
    the n+1, as usual we're going to let P
  • 0:32 - 0:37
    denote a prime. Now we said that
    ZN is a set of integers from zero
  • 0:37 - 0:41
    to N-1 and we said that we can add and
    multiply elements in the set modulo N. We
  • 0:41 - 0:46
    also said that ZN star is basically the
    set of invertible elements in ZN. And we
  • 0:46 - 0:51
    proved a simple lemma to say that, X is
    invertible if and only if X is relatively
  • 0:51 - 0:56
    prime to N. And not only did we
    completely understand which elements are
  • 0:56 - 1:01
    invertible and which aren't, we also
    showed a very efficient algorithm based on
  • 1:01 - 1:06
    Euclid's extended algorithm, to find the
    inverse of an element X in ZN. And we said
  • 1:06 - 1:10
    that the running time of this algorithm,
    is basically order n squared, where
  • 1:10 - 1:16
    again, n is the number of bits of the
    number capital N. So as I said, now
  • 1:16 - 1:21
    we're going to move from Greek times all
    the way to the seventeenth century and
  • 1:21 - 1:26
    talk about Fermat. So Fermat did a number
    of important theorems. The one that I want
  • 1:26 - 1:31
    to show you here today is the following.
    So suppose I give you a prime P. Then in
  • 1:31 - 1:36
    fact for any element X in ZP star, it so
    happens that if I look at X and raise it
  • 1:36 - 1:41
    to the power of P - 1, I'm a gonna get
    one, in ZP. So let's look at a quick
  • 1:41 - 1:46
    example. Suppose I set the number P to be
    five. And I look at, three to the power of
  • 1:46 - 1:51
    P-1. In other words, three to the power of
    four, Three to the power of four is 81,
  • 1:51 - 1:55
    which in fact, is one modulo five. This
    example satisfies Fermat's theorem.
  • 1:55 - 2:00
    Interestingly, Fermat actually didn't prove this theorem himself. The proof
  • 2:00 - 2:04
    actually waited until Euler, who
    proved that almost 100 years later. And in
  • 2:04 - 2:09
    fact, he proved a much more general
    version of this theorem. So let's look at
  • 2:09 - 2:14
    a simple application of Fermat's theorem.
    Suppose I look at an element X in Z P
  • 2:14 - 2:19
    star. And I want to remind you here that P
    [inaudible] must be a prime. Well, then what do we
  • 2:19 - 2:25
    know? We know that X to the P minus one is
    equal to one. Well, we can write X to the
  • 2:25 - 2:30
    P minus one as X times X to the power of P
    minus two. Well so now we know that X
  • 2:30 - 2:34
    times X to the power of P minus two
    happens to be equal to one. And what that
  • 2:34 - 2:39
    says, is that really the inverse of X
    modulo P, is simply X to the P minus two.
  • 2:39 - 2:44
    So this gives us another algorithm for
    finding the inverse of X modulo a prime.
  • 2:44 - 2:49
    Simply raise X to the power of p minus
    two, and that will give us the inverse of
  • 2:49 - 2:54
    x. It turns out, actually this is a fine
    way to compute inverses modulo a prime.
  • 2:54 - 2:58
    But it has two deficiencies compared to
    Euclid's algorithm. First of all, it only
  • 2:58 - 3:03
    works modulo primes, Whereas, Euclid's
    algorithm worked modulo composites as
  • 3:03 - 3:07
    well. And second of all, it turns out this
    algorithm is actually less efficient. When
  • 3:07 - 3:11
    we talk about how to do modular
    exponentiations--we're gonna do that in
  • 3:11 - 3:15
    the last segment in this module--you'll
    see that the running time to compute this
  • 3:15 - 3:20
    exponentiation is actually cubic in the
    size of P. So this will take roughly log
  • 3:20 - 3:24
    cube of P, whereas if you remember,
    Euclid's algorithm was able to compute the
  • 3:24 - 3:30
    inverse in quadratic time in the
    representation of P. So not only is this
  • 3:30 - 3:37
    algorithm less general it only works for
    primes, it's also less efficient. So score
  • 3:37 - 3:41
    one for Euclid. But nevertheless, this
    fact about primes is extremely important,
  • 3:41 - 3:48
    and we're gonna be making extensive use of
    it in the next couple of weeks. So let me
  • 3:48 - 3:52
    show you a quick and simple application
    for Fermat's theorem suppose we wanted
  • 3:52 - 3:57
    to generate a large random prime, say our
    prime needed to be 1,000 bits or so. So
  • 3:57 - 4:02
    the prime we're looking for is on the
    order of two to the 1024 [inaudible]. So here's
  • 4:02 - 4:07
    a very simple probabilistic algorithm.
    What we would do is we would choose a
  • 4:07 - 4:12
    random integer in the interval that was
    specified. And then we would test whether
  • 4:12 - 4:17
    this integer satisfies Fermat's theorem.
    In other words, we would test for example
  • 4:17 - 4:22
    using the base two; we would test whether
    two to the power of p minus one equals one
  • 4:22 - 4:27
    in Z p. If the answer is no, then if this
    equality doesn't hold, then we know for
  • 4:27 - 4:33
    sure that the number p that we chose is
    not a prime. And if that happens, all we
  • 4:33 - 4:37
    do is we go back to step one and we try
    another prime. And we do this again and
  • 4:37 - 4:42
    again and again, until finally we find an
    integer that satisfies this condition. And
  • 4:42 - 4:46
    once we find an integer that satisfies
    this condition, we simply output it and
  • 4:46 - 4:52
    stop. Now it turns out, this is actually a
    fairly difficult statement to prove. But
  • 4:52 - 4:56
    it turns out if a random number passes
    this test, then it's extremely likely to
  • 4:56 - 5:01
    be a prime. In particular the probability
    that P is not a prime is very small. It's
  • 5:01 - 5:06
    like less than two to the -60 for the
    range of 1024 bit numbers. As the
  • 5:06 - 5:11
    number gets bigger and bigger the
    probability that it passes this test here,
  • 5:11 - 5:16
    but is not a prime drops to zero very
    quickly. So this is actually quite an
  • 5:16 - 5:20
    interesting algorithm. You realize we're
    not guaranteed that the output is in fact
  • 5:20 - 5:25
    a prime. All we know is that it's very,
    very likely to be a prime. In other words
  • 5:25 - 5:30
    the only way that it's not a prime is that
    we got extremely unlucky. In fact so
  • 5:30 - 5:34
    unlucky that a negligible probability
    event just happened. Another way to say
  • 5:34 - 5:40
    this is that if you look at the set of all
    1024 integers, then, well, there's the set
  • 5:40 - 5:45
    of primes. Let me write prime here. And
    then there is a small number of
  • 5:45 - 5:51
    composites. That actually will falsify the
    test. Let's call them F for false primes.
  • 5:51 - 5:56
    Let's call them FP, for false primes.
    There's a very small number of composites
  • 5:56 - 6:01
    that are not prime and yet will pass this
    test. But as we choose random integers,
  • 6:01 - 6:05
    you know, we choose one here, one here,
    one here, and so on, as we choose random
  • 6:05 - 6:10
    integers, the probability that we fall
    into the set of false primes is so small
  • 6:10 - 6:15
    That it's essentially a negligible event
    that we can ignore. In other words, one
  • 6:15 - 6:21
    can prove that the set of false primes is
    extremely small, and a random choice is
  • 6:21 - 6:25
    unlikely to pick such a false prime. Now I
    should mention, in fact, this is a very
  • 6:25 - 6:29
    simple algorithm for generating primes.
    It's actually, by far, not the best
  • 6:29 - 6:33
    algorithm. We have much better algorithms
    now. And, in fact, once you have a
  • 6:33 - 6:36
    candidate prime, we now have very
    efficient algorithms that will actually
  • 6:36 - 6:40
    prove beyond a doubt that this candidate
    prime really is a prime. So we don't even
  • 6:40 - 6:45
    have to rely on probabilistic statements.
    But nevertheless, this Fermat test is so
  • 6:45 - 6:49
    simple, that I just wanted to show you
    that it's an easy way to generate primes.
  • 6:49 - 6:53
    Although in reality, this is not how
    primes are generated. As the last point,
  • 6:53 - 6:57
    I'll say that you might be wondering how
    many times this iteration has to repeat
  • 6:57 - 7:02
    until we actually find the prime. And
    that's actually a classic result; it's
  • 7:02 - 7:06
    called the prime number theorem, which
    says that, after a few hundred iterations,
  • 7:06 - 7:10
    in fact, we'll find the prime with
    high probability. So in expectation, one would
  • 7:10 - 7:15
    expect a few hundred iterations and no
    more. Now that we understand
  • 7:15 - 7:19
    Fermat's theorem, the next thing I want
    to talk about is what's called the
  • 7:19 - 7:24
    structure of ZP star. So here, we are
    going to move 100 years into the future...
  • 7:24 - 7:29
    Into the eighteenth century and look at
    the work of Euler. And one of the first
  • 7:29 - 7:33
    things Euler showed is in modern language
    is that ZP star is what's called a
  • 7:33 - 7:38
    cyclic group. What does it mean that ZP
    star is a cyclic group? What it means is
  • 7:38 - 7:43
    that there exists some element G in ZP
    star, such that if we take G and raise to
  • 7:43 - 7:48
    a bunch of powers G, G squared, G cubed, G
    to the fourth. And so on and so forth up
  • 7:48 - 7:53
    until we reach G to the P minus two.
    Notice there's no point of going beyond G
  • 7:53 - 7:57
    to the p minus two, because G to the p
    minus one by Fermat's theorem is equal to
  • 7:57 - 8:02
    one, so then we will cycle back to one. If
    we go back to G to the p minus one, then G
  • 8:02 - 8:07
    to the p will be equal to G, G to the p
    plus one will be equal to G squared, and
  • 8:07 - 8:12
    so on and so forth. So we'll actually get
    a cycle if we keep raising to higher and
  • 8:12 - 8:17
    higher powers of G. So we might as well
    stop at the power of G to the p minus two.
  • 8:17 - 8:21
    And what Euler showed is that in fact
    there is an element G such that if you
  • 8:21 - 8:26
    look at all of its powers all of its
    powers expand the entire group ZP Star.
  • 8:26 - 8:31
    The powers of G give us all the elements
    in ZP star. Elements of this, of this type
  • 8:31 - 8:36
    is called a generator. So G would be said
    to be a generator of ZP star. So let's
  • 8:36 - 8:41
    look at a quick example. So let's look,
    for example, at P equals seven. And let's
  • 8:41 - 8:46
    look at all the powers of three. So three
    squared three cubed, three to the fourth,
  • 8:46 - 8:50
    three to the fifth, Three to the six,
    already we know, is equal to one modular
  • 8:50 - 8:55
    seven by Fermat's Theorem. So there's no
    point in looking at three to the six. We
  • 8:55 - 9:00
    know we would just get one. So here, I
    calculated all these powers for you, and I
  • 9:00 - 9:04
    wrote them out. And you see that in fact,
    we get all the numbers [inaudible] in Z,
  • 9:04 - 9:09
    in Z7 star. In other words, we get
    one, two, three, four, five, and six. So
  • 9:09 - 9:15
    we would say that three is a generator of
    Z7 star. Now I should point out that not
  • 9:15 - 9:20
    every element is a generator. For example,
    if we look at all the powers of two, well,
  • 9:20 - 9:25
    that's not gonna generate the entire
    group. In particular, if we look at two to
  • 9:25 - 9:30
    the zero, we get one. Two to the one, we
    get two. Two squared is four, and two
  • 9:30 - 9:34
    cubed is eight, which is one modular
    seven. So we cycled back and then two to
  • 9:34 - 9:40
    the fourth would be two, two to the fifth
    would be four. And so on and so forth. So
  • 9:40 - 9:45
    if we look at the powers of two, we just
    get one, two, and four. We don't get the
  • 9:45 - 9:50
    whole group and therefore we say that two
    is not a generator of Z7 star. Now again,
  • 9:50 - 9:56
    something that we'll use very often is
    given an element G of ZP*, if we look at a
  • 9:56 - 10:02
    set of all powers of G, the resulting set
    is gonna be called the group generated by
  • 10:02 - 10:07
    G, okay? So these are all the powers of G.
    They give us what's called a
  • 10:07 - 10:13
    multiplicative group. Again, the technical
    term doesn't matter. The point is we're
  • 10:13 - 10:18
    gonna call all these powers of G, the
    group generated by G. In fact there's this
  • 10:18 - 10:24
    notation which I don't use very often,
    angle G angle, to denote this group that's
  • 10:24 - 10:30
    generated by G. And then we call the order
    of G in Z p star, we simply let that be
  • 10:30 - 10:36
    the size of the group that's generated by
    G. So in other words, the order of G in Z
  • 10:36 - 10:41
    p star is the size of this group. But
    another way to think about that is
  • 10:41 - 10:46
    basically it's the smallest integer A such
    that G to the A is equal to one in Z P.
  • 10:47 - 10:53
    Okay, it's basically the smallest power
    that causes the power of G to be equal to
  • 10:53 - 10:59
    one. And it's very easy to see that this
    equality here basically if we look at all
  • 10:59 - 11:04
    the powers of G and we look at one, G, G
    squared, G cubed and so on and so forth up
  • 11:04 - 11:10
    until we get to G to the order of G minus
    one. And then if we look at the order of G
  • 11:10 - 11:15
    to the order of G. This thing is simply
    going to be equal to one, by definition.
  • 11:16 - 11:22
    Okay, so there's no point in looking at
    any higher powers. We might as well just
  • 11:22 - 11:28
    stop raising to powers here. And as a
    result the size of the set, in fact, is
  • 11:28 - 11:33
    the order of G. And you can see that the
    order is the smallest power such that
  • 11:33 - 11:39
    raising G to that power gives us one in Z
    p. So I hope this is clear although it
  • 11:39 - 11:43
    might take a little bit of thinking to
    absorb all this notation. But in the
  • 11:43 - 11:48
    meantime let's look at a few examples. So,
    again, let's fix our, our prime to be
  • 11:48 - 11:53
    seven. And let's look at the order of the
    number of elements. So what is the order
  • 11:53 - 11:58
    of three modulus of seven? Well, we're
    asking what is the size of the group that
  • 11:58 - 12:03
    three generates modulus of seven? Well, we
    said that three is a generator of Z7 star.
  • 12:03 - 12:08
    So it generates all of Z7 star. There are
    six elements in Z7 star. And therefore we
  • 12:08 - 12:13
    say that the order of three is equal to
    six. Similarly, I can ask, well, what is
  • 12:13 - 12:17
    the order of two modulo seven? And in
    fact, we already saw the answer. So let's,
  • 12:17 - 12:22
    I'll ask you, what is the order of two
    modulo seven and see if you can f igure
  • 12:22 - 12:29
    out what this answer is. So the answer is
    three and again, the reason is if we look
  • 12:29 - 12:34
    at the set of powers of two modulo seven,
    we have one, two, two squared, and then
  • 12:34 - 12:39
    two cubed is already equal to one. So this
    is the entire set of powers of two modulo
  • 12:39 - 12:44
    seven. There are only three of them and,
    therefore, the order of two modulo seven
  • 12:44 - 12:49
    is exactly three. Now let me ask you a
    trick question. What's the order of one
  • 12:49 - 12:54
    modulo seven? Well, the answer is one.
    Because if you look at the group that's
  • 12:54 - 12:59
    generated by one, well, there's only one
    number in that group, namely the number
  • 12:59 - 13:03
    one. If I raise one to a whole bunch of
    powers, I'm always gonna get one, And
  • 13:03 - 13:07
    therefore the order of 1 modulo 7
    In fact the order of 1 modulo any prime
  • 13:07 - 13:13
    is always gonna be 1. Now there's an
    important theorem of Lagrange, that
  • 13:13 - 13:17
    actually this is a very, very special case
    of it, what I am stating here. But
  • 13:17 - 13:22
    Langrage's theorem basically implies that
    if you look at the order of G modulo p,
  • 13:22 - 13:27
    the order is always going to divide P-1. So in our two example you see,
  • 13:27 - 13:32
    six divides seven minus one, six divides
    six, and similarly, three divides seven
  • 13:32 - 13:37
    minus one. Namely again three divides six.
    But this is very general; your order is
  • 13:37 - 13:41
    always going be a factor of P minus one.
    In fact, I'll tell you, maybe it's a
  • 13:41 - 13:45
    puzzle for you to think about. It's
    actually very easy to deduce Fermat's
  • 13:45 - 13:49
    theorem directly from this fact, from this
    Lagrange's theorem in fact. And so
  • 13:49 - 13:53
    Fermat's theorem really in some sense
    follows directly from Lagrange's theorem.
  • 13:55 - 13:59
    Lagrange, by the way, did his work in the
    nineteenth century, so you can already see
  • 13:59 - 14:04
    how we're making progress through time. We
    started in Greek times, and already we
  • 14:04 - 14:09
    ended up in the nineteenth century. And I
    can tell you that more advanced crypto
  • 14:09 - 14:14
    actually uses twentieth century math very
    extensively. Now that we understand the
  • 14:14 - 14:18
    structure of ZP star, let's generalize
    that to composites, and look at the
  • 14:18 - 14:23
    structure of ZN star. So what I wanna show
    you here is what's called Euler's Theorem
  • 14:23 - 14:28
    which is a, a direct generalization of
    Fermat's Theorem. So, Euler defined the
  • 14:28 - 14:33
    following function. So given an integer N,
    he defined what's called the phi
  • 14:33 - 14:37
    function, phi of M, to be
    basically the size of the set ZN star.
  • 14:37 - 14:43
    This is sometimes called, Euler's phi
    function. The size of the set Z N star. So
  • 14:43 - 14:49
    for example, we already looked at Z twelve
    star. We said that Z twelve star contains
  • 14:49 - 14:54
    these four elements, one, five, seven, and
    eleven. And therefore we say that phi of
  • 14:54 - 14:59
    twelve is simply the number four. So let
    me ask you as a puzzle, what is phi of P?
  • 14:59 - 15:06
    It will basically be the size of Z P star.
    And so, in fact, we said that in the Z P
  • 15:06 - 15:12
    star contains all of Z P except for the
    number zero. And therefore, phi of P for
  • 15:12 - 15:19
    any prime P is gonna be P minus one. Now,
    there is a special case, which I'm gonna
  • 15:19 - 15:23
    state here and we're gonna use later for
    the RSA system. If N happens to be a
  • 15:23 - 15:28
    product of two primes, then phi of N is
    simply N minus P minus Q plus one. And let
  • 15:28 - 15:33
    me just show you why that's true. So
    basically N is the size of Z N. And now we
  • 15:33 - 15:38
    need to remove all the elements that are
    not relatively prime to m. Well how can an
  • 15:38 - 15:43
    element not be relatively prime to m? It's
    gotta be divisible by p or it's gotta be
  • 15:43 - 15:47
    divisible by q. Well how many elements
    between zero and m minus one are there,
  • 15:47 - 15:52
    there that are divisible by p? Well there
    are exactly q of them. How many elements
  • 15:52 - 15:56
    are there that are divisible by q. Well
    there are exactly p of them. So we
  • 15:56 - 16:01
    subtract p to get rid of those divisible
    by q. We subtract q to get rid of those
  • 16:01 - 16:06
    divisible by p. And you notice we
    subtracted zero twice, because zero is
  • 16:06 - 16:12
    divisible both by P and Q. And therefore,
    we add one just to make sure we subtract
  • 16:12 - 16:18
    zero only once. And so it's not difficult
    to see that phi(N) is N-P-Q+1. And another way
  • 16:18 - 16:25
    of writing that is simply (P-1) times (Q-1). Okay,
    so this is a fact that we will use later
  • 16:25 - 16:30
    on, when we come back and talk about the
    RSA system. So far, this is just defining
  • 16:30 - 16:36
    this phi function of Euler. But now Euler
    put this phi function to really good use.
  • 16:36 - 16:41
    And what he proved is this amazing fact
    here that basically says that if you give
  • 16:41 - 16:46
    me any element X in Z N star. In fact, and
    it so happens that X to the power of phi(N)
  • 16:46 - 16:51
    is equal to one in Z N. So you can see
    that this is a generalization of Fermat's
  • 16:51 - 16:55
    theorem; in particular, Fermat's theorem
    only applied to primes. For primes we know
  • 16:55 - 17:00
    that phi(p) is equal to p minus one, and
    in other words if N were prime we would
  • 17:00 - 17:04
    simply write p minus one here, and then we
    would get exactly Fermat's theorem. The
  • 17:04 - 17:09
    beauty of Euler's theorem is that it
    applies to composites, and not just
  • 17:09 - 17:16
    primes. So let's look at some examples. So
    let's look at five to the power of phi(12).
  • 17:16 - 17:22
    So five is an element of Z12 star.
    If we raise it to the power of five of
  • 17:22 - 17:27
    twelve, we should be getting one. Well, we
    know that phi(12) is 4, so we're
  • 17:27 - 17:32
    raising 5 to the power of 4. Five to
    the power of four is 625 and it's a simple
  • 17:32 - 17:36
    calculation to show that that's equal to
    1 modulo 12. So this is proof
  • 17:36 - 17:40
    by example but of course it's not a proof
    at all. It's just an example. But in fact
  • 17:40 - 17:45
    it's not difficult to prove Euler's
    theorem and in fact I'll tell you that
  • 17:45 - 17:49
    Euler's theorem is also a very special
    case of Lagrange's general theorem.
  • 17:50 - 17:54
    Okay so we say that this is a
    generalization of Fermat's theorem and
  • 17:54 - 17:58
    in fact as we'll see this Euler's
    theorem is the basis of the RSA crypto
  • 17:58 - 18:04
    system. So I stop here and we continue
    with modular quadratic equations in the
  • 18:04 - 18:05
    next segment.
Title:
Fermat and Euler (18 min)
Video Language:
English

English subtitles

Revisions