r/programming • • Jan 16 '18

Cryptography: Diffie-Hellman key exchange explained intuitively using colors

https://youtu.be/YEBfamv-_do?t=2m18s
2.5k Upvotes

170 comments sorted by

View all comments

Show parent comment

1

u/evincarofautumn Jan 17 '18

An intuition about modular arithmetic that may be more familiar to programmers is unsigned overflow—you’re essentially “chopping off” the most significant digits in base c. If you’re using an unsigned machine integer then the base might be, say, 232 for a 32-bit value, but we can choose any base—say, ten thousand. We can illustrate that by representing each base–ten-thousand digit as a four-digit number in base ten:

(4 5678 + 5 6789) mod 1 0000
=
10 2467 mod 1 0000
=
2467
=
((4 5678 mod 1 0000) + (5 6789 mod 1 0000)) mod 1 0000
=
(5678 + 6789) mod 1 0000
=
1 2467 mod 1 0000
=
2467

Whether you truncate before you add or after, you get the same result, because the digits you discard before would have been guaranteed to overflow anyway and get discarded after.