1

Prelude

We want compute the modular exponentiation A(BC) mod p = ?, where A, B, C, and p are known and p is a prim number. For example: 243mod 23 = 6

If we compute it in a straightforward way, first BC = e, and then Ae = n, and finally n mod p; we will run into the problem of creating (potentially) very large intermediary results for e and n. For example: e = 43 = 64, n = 264 ≈ 1.845x1019, and finally n mod 23 = 6

However, doing it in the straightforward way we did not take advantage of the fact that p is a prim number and that we are doing modular exponentiation. And also doing so, we will run into problems computing the result with a computer program in terms of time (CPU) and space (memory).

(Yes, we could do fast modular exponentiation using the identity (a ⋅ b) mod m = [(a mod m) ⋅ (b mod m)] mod m by first reducing A(BC) mod p to Ae mode p. For exmaple A2 mod p = (A ⋅ A) mod p = [(A mod p) ⋅ (A mod p)] mod p – but that is not where we want to go with this).

The smart way – use Fermat's little theorem

As documented in Find power of power under mod of a prime on GeeksforGeeks and the origin of this question, our exponent BC in A(BC) mod p can be expressed differently using Fermat's little theorem.

Fermat's little theorem states: a(p - 1) ≡ 1 (mod p) if p is a prime

Leading to following transformations:

  1. It is possible to rewrite the exponent BC as x ⋅ (p - 1) + y

  2. Using that alternate expression our ABC becomes Ax ⋅ (p - 1) + y = Ax ⋅ (p - 1) ⋅ Ay

  3. Using Fermat's little theorem Ax ⋅ (p - 1) = 1; computing A(BC) mod p becomes computing Ay

  4. Using BC = x ⋅ (p - 1) + y then y can be written as BC mod (p - 1)

From the above we get A(BC) mod p = (Ay) mod p

And with all of that we can compute A(BC) mod p in two steps while keeping the intermediary results small.

  1. y = (BC) mod (p - 1)
  2. result = (Ay) mod p

For example: 243mod 23

  1. y = 43 mod (23 - 1) = 64 mod 22 = 20
  2. result = 220 mod 23 = 1048576 mod 23 = 6

Question

My question is about above transformations, and I could not find an (easy to understand) explanation anywhere. Also intensly looking at Fermat's little theorem did not help. Possibly these transformations should be obvious, but it is simply not clear to me.

In particular, I do not understand why the exponent BC can be expressed as x ⋅ (p - 1) + y. – What is the reasoning behind this?

And also, why should it be "obvious" when using Fermat's little theorem:

a(p - 1) ≡ 1 (mod p) that Ax ⋅ (p - 1) = 1?

It would be great if someone could explain those transformations in an easy understandable way.

Ivo Mori
  • 2,177
  • 5
  • 24
  • 35
  • This seems like a maths problem more than a programming one per se. – Damien_The_Unbeliever Jul 17 '20 at 08:38
  • @Damien_The_Unbeliever Yes, I agree – it's a math understanding problem. To me it isn't obvious whether this question is or isn't [on-topic](https://stackoverflow.com/help/on-topic). Still, this question is about understanding a software algorithm which is relevant when trying to do this kind of computation and solving (very likely) number overflows. – Ivo Mori Jul 17 '20 at 11:53
  • Should this question not get any answers here – you may also want to consider asking your question either on the [Computer Science](https://cs.stackexchange.com/help/on-topic) or (may be) the [Mathematics](https://math.stackexchange.com) Stack Exchange sites. In any case, be sure to check their respective help pages (what's on-topic, how to ask) before posting there. – Ivo Mori Jul 17 '20 at 12:03
  • Don't forget to accept an answer (tick the check-mark next to it) when it answers your question. In this way your question isn't showing up as unanswered in the searches anymore. – Ivo Mori Jul 19 '20 at 01:14

1 Answers1

2

Let me address the two main questions you have:

In particular, I do not understand why the exponent BC can be expressed as x ⋅ (p - 1) + y. – What is the reasoning behind this?

Any integer k can be expressed as k = xm + y for some modulus m. Think of it as dividing k by m, and getting the quotient x and the remainder y. So let k = BC and m = p - 1 and tada.

If it helps you understand, an analogy is that you can turn any amount of minutes into "hours + minutes", when m = 60, then x = hours, y = remaining minutes.

And also, why should it be "obvious" when using Fermat's little theorem:

a(p - 1) ≡ 1 (mod p) that Ax ⋅ (p - 1) = 1?

Say we have a(p - 1) ≡ 1 (mod p). What happens if we multiply both sides by a(p - 1)? We get:

    a(p - 1) a(p - 1) ≡ a(p - 1) (mod p)
    a(p - 1) + (p - 1) ≡ 1 (mod p)             (zxzy = zx+y and the right hand side is equivalent to 1 as we've seen before)
    a2(p - 1) ≡ 1 (mod p)

We can repeatedly multiply both sides by a(p - 1) to get a3(p - 1), a4(p - 1), etc, so we say that for any integer x we have ax(p - 1) ≡ 1 (mod p).

orlp
  • 112,504
  • 36
  • 218
  • 315
  • I think that's an understandable explanation for steps (1) and (3). Although it wasn't asked for specifically in the question; maybe you want to extend your answer for the last step, (4): Using B^C = x ⋅ (p - 1) + y then y can be written as B^C mod (p - 1)? – Ivo Mori Jul 19 '20 at 00:59
  • @IvoMori I think I also explain that by explaining that *y* is the *remainder*. – orlp Jul 19 '20 at 01:03
  • Agree (just needed to think about it for a second myself – didn't had my coffee yet), if any integer n can be expressed as x ⋅ (p - 1) + y as you explained for (1) – then the remainder y of that n is n mod (p-1). If n is B^C then y = B^C mod (p-1). – Ivo Mori Jul 19 '20 at 01:10