← back to all posts

Short Note On Montgomery Reduction: Why Operating Modulo b?

blog

Here’s a short note on the Montgomery reduction algorithm, which we explained in this audit report of p256. If you don’t know, this is an algorithm that is used to perform modular reductions in an efficient way. I invite you to read the explanations in the report, up until the section on word-by-word Montgomery reduction.

In this note, I wanted to offer a different explanation as to why most of the computation happens modulo b instead of modulo R.

As a recap, we want to use A (called x¯ in the report’s explanations) in a base-b decomposition so that in our implementation we can use limbs of size b:

A=a0+a1·b+a2·b2+⋯

Knowing that we can write the nominator part of N/R as explained here as:

N=(a0+a1·b+a2·b2+⋯)+k·m

Grouping by bi terms we get the following terms:

N=(a0+k0·m)+b·(a1+k1·m)+b2·(a2+k2·m)+⋯

but then why do algorithms compute ki=aim−1modb at this point?

The trick is to notice that if a value is divisible by a power of 2, let’s say 2l, then it means that the first l least-significant bits are 0.

As such, the value A+k·m we are computing in the integers will have the first n chunks sum to zero if it wants to be divisible by R=bn (where b is a power of 2):

∑i=0n−1bi·(ai+ki·m)=0

This means that:

  • either ai+ki·m=0 (each term will separately be 0)
  • or ai+ki·m=b·carry (cancellation will occur with carries except for the last chunk that needs to be 0)

In both case, we can write:

ai+ki·m=0modb
← back to all posts blog • 2025-05-24

recent posts

elsewhere

book Real-World Cryptography Applied cryptography, from Manning. podcast Two And A Half Coins Crypto, banking & distributed systems. youtube Cryptography videos Concepts & security, explained.