liveBook · Manning

Square and multiply

As explained in Hacking Cryptography

The square-and-multiply method is an efficient exponentiation algorithm that repeatedly squares and selectively multiplies according to the exponent bits.

Square-and-multiply computes modular exponentiation efficiently by scanning the exponent from its most significant bit to its least significant bit. Starting with 1, it squares the answer for every bit and multiplies by x when the bit is 1. section 10.4.2

The optimization has a security cost: the conditional multiplication creates an observable operation pattern. Timing or power traces can therefore reveal which exponent bits are 1, including sensitive RSA exponents. section 10.4.2

Table 10.1 Square-and-multiply algorithm for exponent \(42 = (101010)_2\)
Previous answer Bit processed Previous answer squared Multiplied by \(x\) New answer
\(1\) \(1\) \(1\) \(x\) \(x^1 = x^{(1)_2}\)
\(x\) \(0\) \(x^2\) - \(x^2 = x^{(10)_2}\)
\(x^2\) \(1\) \(x^4\) \(x^5\) \(x^5 = x^{(101)_2}\)
\(x^5\) \(0\) \(x^{10}\) - \(x^{10} = x^{(1010)_2}\)
\(x^{10}\) \(1\) \(x^{20}\) \(x^{21}\) \(x^{21} = x^{(10101)_2}\)
\(x^{21}\) \(0\) \(x^{42}\) - \(x^{42} = x^{(101010)_2}\)

Trace exponent 42 = (101010)₂ from the initial answer through each square, optional multiplication, and resulting power. section 10.4.2

How Hacking Cryptography covers it

How deep it goes
Defines the bit-scanning procedure, explains its binary arithmetic, and connects conditional multiplication to exponent-revealing traces.
Examples
Uses exponent 42, its binary form 101010, a step table, and an RSA power trace.
What it assumes
Assumes familiarity with exponents and binary notation while deriving the algorithm’s bit-by-bit behavior.
What you can do after
Evaluate exponentiation implementations for operation-pattern leakage from secret exponent bits.
How it connects
Links modular exponentiation with binary representation, timing attacks, side-channel analysis, and RSA.

Also explained in

Compare how 2 books explain square and multiply →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Hacking Cryptography. The section links point to the parts of the book it draws on.