9

Apologies in advance for what I anticipate will be a very dumb question.

To give some background:

I am a software architect that’s been programming since the age of 8 and professionally for the last 15 years. Never had any formal university-level training; everything I know is self taught. As such, there are unorthodox gaps in my knowledge; for example, I have in-depth knowledge of subjects like set theory or statistics (given that I primarily work on business software), but when it comes to something like calculus the extent of my knowledge is that it exists and that it’s used in advanced graphics.

However, now my career may take a turn to where I need an in-depth understanding of crypthography (on a mathematical / theoretical level). So, here I am in my 30’s going back to the roots and learning abstract, theoretical math & comp sci. I’ve hired a tutor for the purpose, who is an undergrad student in a comp sci program that’s known for being very theory-focused.

The question:

Recently, he started teaching me modular notation. Being a software engineer, I am obviously deeply familiar with the modulus operator, and in the world of programming we use the % operator for the purpose. I.e. 3 % 5 = 3.

However, I was told that in mathematics, the notation is to arbitrarily add mod X to the end of an equation/problem block. So, everything is written as usual and there is a note at the end of the equation/problem specifying “under what modulus” everything in the problem is.

This makes no sense to me whatsoever, and when I asked my tutor he said that he asked the same question in his class and the professor replied that “it’s just the way it’s done”. I understand that conventions can be unique, but this to me feels like a very radical departure from the way math is usually written down, and because of that — I feel like there has to be an underlying reason for it that I am not seeing… I am hoping that someone much more knowledgeable than me can help clarify my few questions and help it all make sense.

  1. Math is typically written left to right, with parenthesis and/or other symbols defining blocks/scopes. For example, the body of a square root can include massive formulas, but the scope of the square root is still visually defined. Same with parenthesis blocks and global operations done upon the entire block.

Numbers and operations also follow in sequence, which have a value that they act on and an argument. For example, to get a sum of 4 numbers, we would write (2+5+7+8) (three individual operations), not (2,5,7,8 +) (apply this operator on all numbers in the set).

But with the mod operator, it seems like it’s an arbitrarily-placed footnote at the end of a block, which on top of everything contains extremely vital information. There is no purpose to reading whatever formula is inside the block without first knowing “under what modulus” it is, so how does this work out in academia with page-long formulas?

  1. How exactly does scoping work?

From my understanding, all of the below is syntactically legal:

3x = 15y mod 5

(2x + 8y) - 12z mod 5

(2x + 8y)(12z - 5x) mod 5

What happens if my problem is using a different modulus/base for different parts of the problem?

Would this be legal? (3x + 4y mod 5) - (8z - 2a mod 8)?

What if I have nested clauses? I.e. (3x + (2a - 2b mod 7)^2 + 4y mod 5) - 17z mod 3?

Thanks so much in advance to whoever can help me make sense of this system!

Ruslan
  • 193
  • 5
    Writing $a\equiv b \pmod c$ simply means that $c$ divides $a-b$. That's all. The programming $a% b$ takes the remainder on division by $b$. In particular if $a% b=r$, it is true that $a\equiv r\pmod b$ but the converse is not true. Thus $3\equiv 13\pmod 5$, but $3%5\neq 13$. Very similar and closely related concepts but not precisely the same. – lulu Apr 10 '22 at 21:01
  • 2
    It may be worth noting that the "remainder" notion does not extend particularly well to, say, rings of integers in number fields. That is to say, there may well not be such a good definition of remainder as exists in the ordinary integers. The notion of congruence with respect to an ideal, however, extends perfectly well. – lulu Apr 10 '22 at 21:08
  • It is historically ingrained notation. – copper.hat Apr 10 '22 at 21:22
  • 1
    @lulu The programming $a%b$ takes the remainder on division by $b.$ Not quite: $(-7)\bmod4$ equals $1$ but $(-7)\div4$ has remainder $-3.$ – ryang Apr 10 '22 at 21:22
  • 1
    @ryang Not necessarily. Various conventions are in use that extend the $!\bmod!$ operation from $\Bbb N$ to $\Bbb Z,,$ e.g. see this aswer and its links. – Bill Dubuque Apr 10 '22 at 21:26
  • 1
    @ryang Perhaps you are following a different convention than I am. I would say that the remainder on division by $d$ was always between $0$ and $d-1$, inclusive. – lulu Apr 10 '22 at 21:27
  • 1
    For pertinent context, please state in the question if you have knowledge of any of these: (1) equivalence relations and quotient sets, (2) congrueces, (3) rings (or fields) and quotient rings. – Bill Dubuque Apr 10 '22 at 21:30
  • @lulu I don't always agree with Wolfram's choices, but just for reference, it agrees with my convention that reminder and the modulo operator aren't synonymous: (-7) mod 4 is 1, while remainder of (-7)÷4 is (-3). To be clear, your convention returns the remainder as $1,$ right? I think Bill on the other hand is suggesting that the mod operation could also return $-3.$ – ryang Apr 10 '22 at 21:39
  • 1
    @lulu: But in many programming languages, the result of the operator a % d is not necessarily in the range [0, d-1], e.g. if a is negative. – Nate Eldredge Apr 10 '22 at 21:40
  • 1
    I used to be confused by this too. The 'mod $r$' doesn't apply to the right hand side; it applies to the $=$ sign. A clearer notation would be $\equiv_r$. – Oscar Cunningham Apr 11 '22 at 05:40

3 Answers3

9

In programming terminology, the symbol $\bmod$ is "overloaded" in math to mean two different things: the modulus operator, and the "congruent mod $r$" relation.

The operator, written $a \bmod r$, is the equivalent of your % operator. You can think of it as a function taking two integers and returning an integer: %(int a, int r) -> int.

The relation, written $a \equiv b \pmod r$, is effectively a predicate: it is a statement with a true/false truth value, like a function returning bool. So you can think of it as cong(int a, int b, int r) -> bool. The connection between them is that cong(a, b, r) := ((a - b) % r == 0). Or ignoring what % might do for negative numbers, cong(a, b, r) := (a % r == b % r).

Now, it may be that an extended passage of a math paper will be "working mod $r$". Formally, this means that all operations are not intended as operations on the integers $\mathbb{Z}$, but on the ring $\mathbb{Z}_r$ of integers mod $r$. Without getting into abstract algebra, you can think of it as roughly "the notation $a = b$ is now redefined as cong(a, b, r)". Two numbers that are congruent mod $r$ are now considered to be the same number; they compare as equal.

It's true that the "scope" may not be specified explicitly, and the author may expect you to understand from context which ring we are working in. Usually, once you know enough abstract algebra to be able to read the paper at all, this does not lead to any confusion. A paper is after all written for a human mathematician to read, not for a compiler to parse.

However, if we say something like "Let $a,b,c \in \mathbb{Z}_r$", this means that a,b,c are "declared", not as integers, but as objects of a class for which the == operator is overloaded as cong(a, b, r). And so a following expression like $a + b = c$ means cong(a+b, c, r) == true, where you can think of the + operator also having been overloaded to return an object of the $\mathbb{Z}_r$ class.

Nate Eldredge
  • 97,710
  • 2
    +1 Very nicely done, explaining the mathematical meaning with references to programming constructs. I hope the OP appreciates this. – Ethan Bolker Apr 10 '22 at 21:35
  • Wow... This is amazing. Thanks so much for taking the time to write this up; it clears up so many questions -- including ones that I didn't even know I had yet! – Ruslan Apr 11 '22 at 15:31
4

The reason for the notation becomes clear with later understanding of how modular arithmetic is used in mathematics - and this is not by mixing and matching calculations like "(3x + 4y mod 5) - (8z - 2a mod 8)", which possibly occur in programming (I'm not sure where) but do not occur (regularly) in mathematics.

With the idea of all calculation being taken modulo a particular number, we can form particular mathematical objects that generalise the concepts of, say, addition and multiplication.

Concretely, a group is a set of objects together with an operation that takes two objects and produces a new one. For instance, the integers $\mathbb{Z}$ (the set) together with $+$ (the operation). It is subject to certain constraints: there needs to be "a $0$" (some number where applying the operation - here, "adding $0$"); each number needs an "inverse" (and with its inverse the operation produces a $0$: here, $a+(-a)=0$); and the $+$ has to satisfy $a+(b+c)=(a+b)+c$, as our normal $+$ does.

With our modular arithmetic notation, $6\%5=1$ is no longer an isolated calculation: instead, we can make a group where the numbers $1,6,11,16,\dots$ are fundamentally the same number (because $\%5$ they are all $1$) - this number is called $\overline{1}$. It turns out that mod 5 arithmetic only has five different numbers, $\overline{0},\overline{1},\overline{2},\overline{3},\overline{4}$, and we can use addition and times tables just as we normally do. For instance, $1\equiv 11\pmod 5$ and $3\equiv 18\pmod 5$, and $1+3=4$ and $11+18=29$, but $4\equiv 29\pmod 5$, so it does not matter whether we choose $1$ or $11$ as our representation of $\overline{1}$ in our calculations: we get $\overline{4}$ as our result. This also applies to multiplication, where $11\times 18\equiv 11\times 3\equiv 1\times 18\equiv 1\times 3\pmod 5$. Choose any representation of $\overline{1}$ and $\overline{3}$ that you like: $\overline{1}\times \overline{3}=\overline{3}$.

We do not use infix notation like $\%$, but append a $\pmod 5$ at the end of our calculations, because unlike when using the modulus in programming, here we want to say: $4$ is the same number as $29$. It is not a result of a calculation: for our purposes, they are literally the same number: $\overline{4}$.

The reason for forming groups with their own special addition and multiplication tables is so we can come to extremely generalised conclusions - every group has properties like "cardinality" and "(non-)commutativity" and each value in the set has properties like "order". This is true whether the group represents modular arithmetic or the symmetries of a triangle. With these properties, we make theorems like Lagrange's theorem from which it is trivial to get number theory results like Fermat's little theorem, that $a^p\equiv a\pmod p$, but that are more widely applicable to all sorts of areas of mathematics.

We also form rings, fields and all sorts of other structures with more strict or more loose rules than those of groups.

Cryptography and its underlying number theory is in a sense no more than applied group theory and field theory and all these other studies of abstract structures collectively described by mathematicians as "algebra" (not the same meaning as when laypeople use the word).

A.M.
  • 3,944
2

I think in the example

3x ≡ 15y (mod 5)

you gave, you can treat the (mod 5) part as an "environment". Arguably, this is the same as the with ...: statement giving contexts in some programming languages.

This "informal" notation may be related to the fact that all the integers modulo 5 form a certain new number type or a "ring" (as said in the comments).

As with other informal notation, scoping is often implied (which may cause confusion/complexity).

I am not so sure about the infix notation in:

(2x + 8y) - 12z mod 5

(2x + 8y)(12z - 5x) mod 5

You can probably define and use a prefix function notation mod(.,.) when necessary.

tinlyx
  • 1,534