Modular arithmetic and wraparound

8 min read

Modular arithmetic and wraparound

Modular arithmetic means arithmetic inside a fixed range. If the modulus is q=7q = 7, then every integer reduced modulo 77 lands in one of these values:

0,1,2,3,4,5,60, 1, 2, 3, 4, 5, 6

This should be familiar if you have done any programming: a mod q gives a remainder between 0 and q - 1.

In lattice cryptography it is not the modulo operation itself that really matters, it is how we reason about values after they have wrapped around. In normal programming, it is often enough to know the result is inside the range. In lattice cryptography, we often need to ask another question:

How far is this modular value from zero?

For example, modulo 77, the value 66 is close to 00, because one more step wraps it back to 00.

Reducing values modulo qq

Reducing a value modulo qq means keeping its remainder after division by qq. For q=7q = 7:

8mod7=18 \bmod 7 = 1

You can read this as: "when 88 is reduced modulo 77, the result is 11". That is because:

8=17+18 = 1 \cdot 7 + 1

The remainder is 11. A few more examples:

15mod7=115 \bmod 7 = 1

because:

15=27+115 = 2 \cdot 7 + 1

And:

1mod7=6-1 \bmod 7 = 6

because 1-1 is one step below 00, and modulo 77 that lands on 66. So modular arithmetic keeps values inside a fixed range. Values do not keep growing forever, they wrap back into the range 00 to q1q - 1.

Same value, different integers

Because values wrap around, different integers can land in the same modulo value. For q=7q = 7:

1mod7=18mod7=115mod7=16mod7=11 \bmod 7 = 1\\ 8 \bmod 7 = 1\\ 15 \bmod 7 = 1\\ -6 \bmod 7 = 1

These are different integers, but they all land in the same place modulo 77. We can write this as:

18156(mod7)1 \equiv 8 \equiv 15 \equiv -6 \pmod 7

This is read as: "11, 88, 1515, and 6-6 are congruent modulo 77". This does not mean the integers are equal, it means they become the same value after reducing modulo 77.

A representative is the integer we choose to write down for a modular value. For example, all of these integers represent the same value modulo 77:

...,13,6,1,8,15,22,......, -13, -6, 1, 8, 15, 22, ...

The usual representative is the one in the range:

0,1,...,q10, 1, ..., q - 1

This is called the standard representative. For the modular value above, the standard representative is 11.

Signed distance from zero

The standard representative is the usual output of modulo reduction. For q=7q = 7, the standard representatives are:

0,1,2,3,4,5,60, 1, 2, 3, 4, 5, 6

But when we ask whether a modular value is small, we usually do not mean:

Is the standard representative numerically small?

We mean:

How far is this value from 00 after accounting for wraparound?

For example, the standard representative 66 is one step below 00 modulo 77:

61(mod7)6 \equiv -1 \pmod 7

The standard representative 55 is two steps below 00:

52(mod7)5 \equiv -2 \pmod 7

The standard representative 44 is three steps below 00:

43(mod7)4 \equiv -3 \pmod 7

Writing modular values this way is called using centered representatives. For q=7q = 7, the standard representatives are:

0,1,2,3,4,5,60, 1, 2, 3, 4, 5, 6

The centered representatives are:

0,1,2,3,3,2,10, 1, 2, 3, -3, -2, -1

Or, written in order around zero:

3,2,1,0,1,2,3-3, -2, -1, 0, 1, 2, 3

These are the same modular values. The centered version just writes them by signed distance from 00.

Small values modulo qq

A modular value is small if its centered representative has small absolute value.

For q=7q = 7:

Standard representativeCentered representativeDistance from 00
000000
111111
661-111
222222
552-222
333333
443-333

From the table, the values closest to 00 modulo 77 are:

0,1,60, 1, 6

The values 11 and 66 are both distance 11 from 00. The difference is the sign:

11(mod7)61(mod7)1 \equiv 1 \pmod 7\\ 6 \equiv -1 \pmod 7

The values 33 and 44 are the farthest from 00 modulo 77:

33(mod7)43(mod7)3 \equiv 3 \pmod 7\\ 4 \equiv -3 \pmod 7

So the centered representative gives two pieces of information:

  • the sign, meaning which side of 00 the value sits on,
  • the size, meaning its distance from 00.

This is the meaning of "small modulo qq" used throughout lattice cryptography. It does not mean "small as a standard representative", it means "small after converting to the centered representative".

What about when qq is even?

When qq is odd, the centered range is symmetric. For q=7q = 7, we used:

3,2,1,0,1,2,3-3, -2, -1, 0, 1, 2, 3

When qq is even, one value sits exactly opposite 00. For q=8q = 8, the standard representatives are:

0,1,2,3,4,5,6,70, 1, 2, 3, 4, 5, 6, 7

A common centered range is:

4,3,2,1,0,1,2,3-4, -3, -2, -1, 0, 1, 2, 3

Here:

44(mod8)4 \equiv -4 \pmod 8

The value 44 is exactly halfway around the modulo range. It is equally far from 00 in both directions. Some conventions choose 44. Some choose 4-4. The convention we choose is important when writing precise code or proofs. For now, the main point to remember is that values around q/2q/2 are the farthest from 00.

So, in summary:

  • values near 00 are close to 00,
  • after reduction to the standard range, values near qq represent small negative values,
  • values near q/2q/2 are far from 00.

Decoding by distance from zero

Here is a small example of why distance from 00 matters. Suppose we have a rule for turning a modular value back into a bit:

  • values near 00 decode as 00,
  • values near q/2q/2 decode as 11.

If a value meant to be 00 shifts to 11, it is still close to 00 and decodes correctly. Working modulo 1717, if it shifts to 1616 it also decodes correctly, because:

161(mod17)16 \equiv -1 \pmod {17}

But if it moves near 88 or 99, it is no longer close to 00. It is near q/2q/2, and the rule above would decode it as the wrong bit. The arithmetic modulo qq is still correct; the value has crossed a decoding boundary.

Vectors modulo qq

A vector modulo qq has entries that are each interpreted modulo qq. For example:

x=(16,2,9)(mod17)x = (16, 2, 9) \pmod {17}

Read this as: "xx is a vector whose entries are considered modulo 1717". Each entry is called a coefficient. The coefficients are:

16,2,916, 2, 9

To reason about size, we center each coefficient. Modulo 1717:

161(mod17)22(mod17)98(mod17)16 \equiv -1 \pmod {17}\\ 2 \equiv 2 \pmod {17}\\ 9 \equiv -8 \pmod {17}

So the centered version is:

(1,2,8)(-1, 2, -8)

Now we can use the same norm ideas from chapter 2. If we care about the largest coefficient, we use the infinity norm. First take the absolute value of each centered coefficient:

1=1,2=2,8=8|-1| = 1,\quad |2| = 2,\quad |-8| = 8

The largest of these is 88, so:

x=8\|x\|_\infty = 8

This tells us that xx has one coefficient that is far from 00 modulo 1717 and that coefficient is 99 in standard form. So although 1616 may look large in the standard range, it is actually small after centering. The coefficient 99 is the problem if we are trying to keep every coefficient of the vector close to 00.

What to remember

  • Modular arithmetic keeps values inside a fixed range: 00 to q1q - 1.
  • Different integers can land in the same modular value.
  • The standard representative is the usual value in the range 0,1,...,q10, 1, ..., q - 1.
  • When reasoning about size, we usually care about signed distance from 00.
  • Centered representatives are the formal way to write that signed-distance view.
  • For q=7q = 7, the standard representative 66 has centered representative 1-1.
  • A modular value is small if its centered representative has small absolute value.
  • After reduction to the standard range, values near qq represent small negative values.
  • Values near q/2q/2 are far from 00.