1

(Update 1, 11/11/19: corrected a calculation error)

This is my first time asking a question here.
I was recently watching a video on crashcourse regarding cryptography, and an equation from Diffie–Hellman key exchange came up. The equation from video is :

$$ \left ( B^{x}\bmod C \right )^{y} = \left ( B^{y}\bmod C \right )^{x} = B^{xy}\bmod C $$

But that doesn't sound to be true. Infact, I tried replacing x,y with 2,3 and B,C with 3,7 , which makes the above statement as:
$$\left ( 3^{2}\bmod 7 \right )^{3} | \left ( 3^{3}\bmod 7 \right )^{2} | B^{2*3}\bmod 7$$ $$=\left ( 3^{2}\bmod 7 \right )^{3} | \left ( 3^{3}\bmod 7 \right )^{2} | 3^{2*3}\bmod 7$$ $$=\left ( 9\bmod 7 \right )^{3} | \left ( 27\bmod 7 \right )^{2} | 729 \bmod 7 $$ $$=( 2 )^{3} | ( 6 )^{2} | 1$$
But $$ 8 \neq 36\neq 1$$

So how is that equation right? I thought that its probably because of the higher precedence of exponential over modular division will be the case and equation would be something like this when expanded:
$$ \left ( B^{xy}\bmod C^{y} \right ) = \left ( B^{yx}\bmod C^{x} \right ) = (B^{xy}\bmod C^{xy}) $$

But that also fails for last case, ie:
$$ \left ( 3^{6}\bmod 7^{2} \right ) = \left ( 3^{6}\bmod 7^{3} \right ) but \neq (3^{6}\bmod 7^{6}) $$

So what's the problem here? is the video claiming a wrong equation or my maths is wrong?

  • Use \bmod for the binary operator; \mod gives wrong spacing. – Arturo Magidin Nov 11 '19 at 03:19
  • 1
    There’s a modular operation missing. You should have $((B^x\bmod C)^y)\bmod C = ((B^y\bmod C)^x)\bmod C = B^{xy}\bmod C$. Alternatively, these statements should be modular statements, $(B^x)^y \equiv (B^y)^x\equiv B^{xy}\pmod{C}$. – Arturo Magidin Nov 11 '19 at 03:20
  • You're interpreting the short-hand notation incorrectly. $B \pmod{C}$ is a value in $ \mathbb{Z}_C$, which you are then raising to the $y$ power, and it is still in $\mathbb{Z}_C$. – Calvin Lin Nov 11 '19 at 03:23
  • @CalvinLin: Well, there’s two distinct notations: the binary mod operator, and the congruence relation; if the written equation was as you write, $B\pmod{C}$, then it means the modular congruence relation, and you are correct; if it was $B\bmod C$, then that represents the remainder of $B$ when divided by $C$, which is a different operation. The video uses the binary operator. That said, it is not uncommon for Computer Scientists to confuse and conflate the two, leading to confusion such as the one here. – Arturo Magidin Nov 11 '19 at 03:26

1 Answers1

1

This is not a precedence problem; it is a problem of sloppy notation, and of a common conflation of two different notions, especially by computer scientists.

See this previous answer.

Basically, there are two notions:

  1. The equivalence relation “congruent modulo $C$”, denoted $a\equiv b\pmod{C}$. This is a relation between integers $a$ and $b$, and it holds if and only if $C|a-b$, which turns out to be equivalent to “$a$ and $b$ have the same remainder when divided by $C$”. Like any equivalence relation, it induces a partition on the integers into equivalence classes, and $a\pmod{C}$ denotes the equivalence class of $a$ modulo $C$, which contains all integers that are congruent to $a$ modulo $C$.

  2. The binary relation “modulo”, often denoted by % in computer languages. Here, “$a\bmod C$“ (or $a\% C$) means “the remainder when dividing $a$ by $C$”. It is usually the unique integer $r$, $0\leq r\lt |C|$ such that $a=qC+r$; sometimes, it is chosen to lie between $-\frac{|C|}{2}$ and $\frac{|C|}{2}$ instead. But in any case, this is a binary operation, which is how you interpreted it.

Computer scientists are often sloppy, conflating the two. The statement would be correct for the binary modulo operator if you reduce after exponentiation: $$(B^x\bmod C)^y\bmod C = (B^y\bmod C)^x\bmod C = B^{xy}\bmod C.$$

Alternatively, it can be a statement about congruences: $$(B^x)^y\equiv (B^y)^x \equiv B^{xy}\pmod{C}.$$

So you are correct that taking it literally it is incorrect as given.

Note that your final computation is incorrect, though. Taking $C=7$, $B=3$, $x=2$, and $y=3$, we have: $$\begin{align*} (B^x\bmod C)^y &= (3^2\bmod 7)^3 = (9\bmod 7)^3\\ &= 2^3 = 8.\\ (B^y\bmod C)^x &= (3^3\bmod 7)^2 = (27\bmod 7)^2\\ &= 6^2 = 36.\\ B^{xy}\bmod C &= 3^{6}\bmod 7 = 729\bmod 7\\ &= 1. \end{align*}$$ (because $729\bmod 7 = 1$, not $43$).

So you are right that $8$ is not equal to $36$ and is not equal to $1$. But you may note that $8\equiv 36\equiv 1\pmod{7}$, and if you take one more modulo operation, $8\bmod 7 = 36\bmod 7 = 1$ giving you the desired equality.

Arturo Magidin
  • 398,050
  • Thankyou for clarifying things up. I am able to get this: that for first 2 parts of the equation , I have to add another modulo ( even though its lil confusing coz a simple % in programming would always equal to a 1 time modulo or remainder, like 7/2=3(quotient) and 7%2=1(remainder) , and here , the % somewhat means "modulo of modulo" ) and not for the third part, because its congruent modulo and your first 2 points explain this.

    But if you can, can you explain those 2 points in more simpler terms, probably with an example differencing b/w modulo and congruent modulo?

    – ansh sachdeva Nov 11 '19 at 12:21
  • also, will it be correct to say that my last assumptions of precedence of ^ is higher than % is wrong ? – ansh sachdeva Nov 11 '19 at 12:28
  • 1
    @anshsachdeva: Honestly, I think you missed both points. You would still need multiple % in programming, because you are doing $(3^2%7)^3$; that does not guarantee you have a number between $0$ and $6$. You are doing $(3^2%7)^3 % 7$. You could just do $(3^2)^3 %7$ to get the final answer... but that's kind of the whole point of the equality you are trying to establish. I don't see how "simpler terms" can be done, honestly. What exactly are you not understanding? Do you not know what a binary relation is? Do you not know what a binary operation is? – Arturo Magidin Nov 11 '19 at 15:13
  • @anshsachdeva: It's not that your assumption on precedence is incorrect. It's that it is irrelevant to the issue at hand. But normally you would do exponentiation before reduction, yes. – Arturo Magidin Nov 11 '19 at 15:14
  • Oh i get it now. They basically want to say that DES equation would actually be shorthanded as this : $$ (3^{x})^{y} \bmod 7 = (3^{y})^{x} \bmod 7= 3^{xy} \bmod 7$$ . When we transmit $$ (3^{x}) \bmod 7 $$ to from A to B, and B raises its power by y, then to bring it back to the range of 0-6, we make another modulo – ansh sachdeva Nov 11 '19 at 20:01
  • 1
    @anshsachdeva They are observing that all of them are equal, even if you reduce after each arithmetical operation, yes. You don't need to first calculate $(3^x)^y$ (which could be really big and take a lot of time!) and then reduce; you can calculate $3^x$ and reduce; then take that result $(3^x%7)$ and raise to the $y$th power, $(3^x%7)^y$, and then reduce, $(3^x%7)^y%7$; you will get the same thing as if you do $3^{xy}%7$, and the same as if you do $(3^y%7)^x%7$. The fact that $(3^x)^y$ is the same as $3^{xy}$ is the same as $(3^y)^x$ is just basic properties of the operations. – Arturo Magidin Nov 11 '19 at 20:05
  • Thank you Arturo, for giving me such a nice explanation. (also for readers, DH key exchange, not DES*) – ansh sachdeva Nov 11 '19 at 20:07