Mod p vs. Mod q in Elliptic Curve Cryptography
Elliptic Curve Cryptography (ECC) powers almost everything from HTTPS to cryptocurrencies. There is a distinction between mod p and mod q operations and I always forget, so I’ve written this post.
1. Core Mathematical Framework
ECC’s security rests upon two distinct mathematical structures:
- The elliptic curve group E(F_p), defined over a finite field (mod p)
 - A cyclic subgroup of prime order q, where cryptographic operations take place
 
Consider mod p as defining our mathematical workspace (the field where points exist), while mod q governs our cryptographic operations (how we generate and use keys).
2. The Field of Definition (mod p)
Mathematical Foundation
- p is a large prime defining our finite field F_p
 - E(F_p) is given by the Weierstrass equation:
where:
- a, b ∈ F_p
 - 4a^3 + 27b^2 ≢ 0 (mod p) (ensuring non-singularity)
 - Points P = (x, y) have coordinates in F_p
 - O denotes the point at infinity (group identity)
 
 
Primality Requirement
- Prime fields F_p are essential because they:
- Provide optimal arithmetic performance
 - Ensure F_p* is cyclic (crucial for point multiplication)
 - Minimize potential cryptanalytic attacks
 - Avoid the additional structure present in extension fields
 
 
3. The Cryptographic Subgroup (mod q)
Group Order and Structure
- By Hasse’s theorem: |#E(F_p) - (p + 1)| ≤ 2√p
 - The curve order factors as: #E(F_p) = h × q where:
- q is prime (the cryptographic subgroup order)
 - h is the cofactor (ideally 1 or small)
 
 
Security Requirements
- q must be:
- Large (≥ 256 bits for modern security)
 - Prime (preventing Pohlig-Hellman attacks)
 - Not equal to p (avoiding anomalous curves)
 - Not a special form number (resisting specialized ECDLP attacks)
 
 
4. Operational Domain Mapping
| Operation | mod p | mod q | 
|---|---|---|
| Point Addition | Yes (coordinate arithmetic) | No | 
| Scalar Multiplication | Yes (resulting point) | Yes (scalar choice) | 
| Private Keys | No | Yes | 
| ECDSA Signatures | No | Yes (r,s components) | 
5. Implementation Security
Critical Vulnerabilities
- 
Invalid Point Validation
- Always verify:
 - Confirm:
 - Check:
 
 - 
Timing Attack Prevention
- Use constant-time algorithms
 - Prefer Montgomery ladder over double-and-add
 - Implement blinding techniques
 
 - 
Modular Arithmetic Pitfalls
# INCORRECT - potential bias private_key = random_bytes(32) # [0, 2^256-1] # CORRECT - uniform distribution private_key = random_int_modq() # [1, q-1] 
6. Real-World Parameters
Bitcoin (secp256k1)
p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
q = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141Curve25519
p = 2^255 - 19
q = 2^252 + 277423177773723535358519377908836484937. Historical Context
The distinction between mod p and mod q emerged from:
- Lenstra’s ECM work (1987)
 - Koblitz and Miller’s concurrent ECC proposals (1985)
 - Early implementation vulnerabilities in SSL/TLS
 
Further Reading
Further Reading
- NIST FIPS 186-5 for ECC standards.
 - SafeCurves for analyzing curve security.
 
Last updated on