David Wong

cryptologie.net

cryptography, security, and random thoughts

Hey! I'm David, cofounder of zkSecurity, research advisor at Archetype, and author of the Real-World Cryptography book. I was previously a cryptography architect of Mina at O(1) Labs, the security lead for Libra/Diem at Facebook, and a security engineer at the Cryptography Services of NCC Group. Welcome to my blog about cryptography, security, and other related topics.

← 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=aim1modb 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=0n1bi·(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
currently reading:
Short Note On Montgomery Reduction: Why Operating Modulo b?
05-24 blog
📖 my book
Real-World Cryptography is available from Manning Publications.
A practical guide to applied cryptography for developers and security professionals.
🎙️ my podcast
Two And A Half Coins on Spotify.
Discussing cryptocurrencies, databases, banking, and distributed systems.
📺 my youtube
Cryptography videos on YouTube.
Video explanations of cryptographic concepts and security topics.