Path: blob/main/foundations/06-elliptic-curves/sage/06e-scalar-multiplication.ipynb
483 views
Notebook 06e: Scalar Multiplication
Module 06. Elliptic Curves
Motivating Question. To use elliptic curves for cryptography, we need to compute (adding to itself times) for very large , say . Adding one step at a time would take operations, which is impossibly slow. Is there a shortcut? Yes: the double-and-add algorithm computes in only steps, just as square-and-multiply computes efficiently in .
Prerequisites. You should be comfortable with:
Point addition and doubling on elliptic curves (Notebook 06b)
Group structure and point orders (Notebook 06d)
Square-and-multiply for modular exponentiation (Module 04/05)
Learning objectives. By the end of this notebook you will be able to:
Implement naive repeated addition and understand why it is too slow.
Implement the double-and-add algorithm and prove it is .
Compare the performance of naive vs double-and-add.
Understand the analogy between double-and-add and square-and-multiply.
Appreciate that scalar multiplication is the one-way function underlying EC crypto.
1. Naive Scalar Multiplication
The simplest approach: add to itself times.
This requires point additions. For , this is operations, far beyond feasibility.
Checkpoint 1. Why is naive scalar multiplication while we need ? The key insight is the same as for exponentiation: we can double the point instead of adding it one at a time. Doubling to get , then doubling again to get , and so on, reaches in only steps.
2. The Double-and-Add Algorithm
Idea: Write in binary: where . Then:
We precompute by repeated doubling, and add together only the terms where .
| Analogy | Multiplicative group | Additive (EC) group |
|---|---|---|
| Operation | Multiplication | Addition |
| Repeated operation | (exponentiation) | (scalar multiplication) |
| Efficient method | Square-and-multiply | Double-and-add |
| Complexity | squarings + multiplications | doublings + additions |
Misconception alert. "Double-and-add always does exactly additions." Not quite, it does doublings, but the number of additions equals the Hamming weight of (the number of 1-bits in the binary representation). For a random 256-bit , this is about 128 additions + 256 doublings total operations.
3. Performance Comparison
Let us compare naive vs double-and-add on increasingly large scalars.
Checkpoint 2. For a 256-bit scalar, double-and-add needs doublings and additions = point operations. Naive would need additions. The speedup factor is . This is the difference between "impossible" and "milliseconds."
4. The One-Way Function
Scalar multiplication is the one-way function of elliptic curve cryptography:
| Direction | Operation | Complexity |
|---|---|---|
| Forward (easy) | Given , compute | via double-and-add |
| Backward (hard) | Given , find such that | Best known: (Pollard's rho) |
This asymmetry is the Elliptic Curve Discrete Logarithm Problem (ECDLP).
Bridge from Module 05. In Module 05, the one-way function was (fast via square-and-multiply, hard to invert). Here the one-way function is (fast via double-and-add, hard to invert). The mathematical structure is different, but the cryptographic paradigm is identical.
5. Windowed and Montgomery Methods (Overview)
Double-and-add is the basic algorithm, but real implementations use optimizations:
| Method | Idea | Speed-up |
|---|---|---|
| Double-and-add (basic) | Process 1 bit at a time | Baseline |
| -ary method | Process bits at a time, precompute | Fewer additions |
| NAF (Non-Adjacent Form) | Use digits to reduce additions | ~33% fewer additions |
| Montgomery ladder | Always do same operations regardless of bit value | Side-channel resistant |
The Montgomery ladder is particularly important for security: it prevents attackers from learning secret bits by measuring timing or power consumption.
Checkpoint 3. Why is constant-time execution important? If double-and-add takes different amounts of time for bits 0 vs 1, an attacker measuring execution time can deduce the secret scalar bit by bit. This is a timing side-channel attack. The Montgomery ladder defeats this by performing the same operations regardless of the bit value.
Crypto foreshadowing. In the next notebook, we will use scalar multiplication as the core operation in ECDH (key exchange) and ECDSA (digital signatures). The security of both protocols rests on the ECDLP: computing is easy, but recovering from and is believed infeasible for properly chosen curves.
6. Exercises
Exercise 1 (Worked): Tracing Double-and-Add
Problem. On over , compute where using double-and-add. Show each step.
Solution. , so we process bits right-to-left:
| Step | Bit | Action | ||
|---|---|---|---|---|
| 0 | 1 | |||
| 1 | 1 | |||
| 2 | 0 | (skip) | ||
| 3 | 1 |
Result: . Let us verify:
Exercise 2 (Guided): Operation Count
Problem. For a 256-bit scalar :
How many doublings does double-and-add perform?
On average, how many additions? (Hint: each bit is 1 with probability 1/2.)
Compare the total operation count with naive scalar multiplication.
Fill in the TODO cells below.
Exercise 3 (Independent): ECDLP Brute Force
Problem.
On over , pick a generator and compute using SageMath.
Write a brute-force function
ecdlp_brute(G, Q)that finds by computing and comparing with .Time your brute-force ECDLP solver for curves over with having 10, 15, and 20 bits. Plot the time vs (or vs ).
Based on the trend, estimate how long brute force would take for a 256-bit curve.
Summary
| Concept | Key Fact |
|---|---|
| Naive scalar multiplication | by repeated addition: , infeasible for large |
| Double-and-add | Process bits of : doublings + additions |
| Operation count | doublings + additions on average |
| Analogy | Double-and-add is to EC what square-and-multiply is to |
| Montgomery ladder | Constant-time variant; resists timing side-channel attacks |
| One-way function | Computing is fast; recovering from is the ECDLP |
Scalar multiplication is the workhorse operation of elliptic curve cryptography. Every key generation, key exchange, and signature computation reduces to one or a few scalar multiplications. In the final notebook, we put it all together with ECDH and ECDSA.
Next: 06f: ECDH and ECDSA