Constant-time comparison
A constant-time comparison is an equality-checking technique whose execution pattern does not reveal information through the position or content of mismatches.
Hacking Cryptography
A constant-time comparison compares values without execution time depending on the position or content of mismatches, aside from permitted length handling.
- How it is used
- Protects secret comparisons from timing leakage during cryptographic implementation.
- How deep it goes
- Explains short-circuit leakage and a Go implementation, including accumulator behavior, but does not develop timing-measurement methodology.
- Examples
- Uses decimal-guessing intuition, glibc memcmp, and Go's crypto/subtle ConstantTimeCompare.
- What you can do after
- Apply Go's crypto/subtle.ConstantTimeCompare or an equivalent full-scan comparison for equal-length secret values.
Read how Hacking Cryptography explains constant-time comparison →
Real-World Cryptography
A constant-time comparison compares two values in an execution pattern whose duration does not depend on the location of their first difference.
- How it is used
- Protects authentication-tag verification from timing attacks caused by early comparison exits.
- How deep it goes
- Explains branch avoidance and XOR-OR accumulation, while leaving the exact trick as an exercise.
- Examples
- Uses MAC tag verification and Go byte-array comparison with XOR and OR accumulation.
- What you can do after
- Apply it via cryptographic library verification functions when comparing received and computed authentication tags.
Read how Real-World Cryptography explains constant-time comparison →
What to know first
- logical XORLogical XOR is a binary operation that produces true when exactly one operand is true and false when both operands have the same truth value.
Related concepts
- timing attackaddresses · A timing attack is a side-channel attack that infers information, often secret data, by measuring differences in the time taken to process inputs or perform computations.
- crypto/subtleimplemented by · crypto/subtle is the Go standard-library package providing cryptographic operations designed to reduce timing side channels.
- Goimplemented by · Go is a statically typed, compiled, general-purpose programming language with garbage collection and built-in support for concurrency.
- logical ORuses · Logical OR is a Boolean disjunction that is true when at least one of its component propositions is true.
- memcmpcontrasts with · memcmp is a C standard-library function that compares a specified number of bytes in two memory regions and indicates whether they are equal or which differs first.
- comparisonkind of · A comparison is a language operation that evaluates a relationship, such as equality or ordering, between values and produces a result representing that relationship.
Questions about constant-time comparison
What is constant-time comparison?
A constant-time comparison is an equality-checking technique whose execution pattern does not reveal information through the position or content of mismatches.
Which Manning books explain constant-time comparison?
Hacking Cryptography and Real-World Cryptography.
What should I know before learning constant-time comparison?
Manning books that teach constant-time comparison usually introduce logical XOR first.
What is the difference between constant-time comparison and memcmp?
A constant-time comparison is an equality-checking technique whose execution pattern does not reveal information through the position or content of mismatches. memcmp is a C standard-library function that compares a specified number of bytes in two memory regions and indicates whether they are equal or which differs first.
What is constant-time comparison a kind of?
Constant-time comparison is a kind of comparison. A comparison is a language operation that evaluates a relationship, such as equality or ordering, between values and produces a result representing that relationship.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.

