Heist Lab

Performance analysis · field edition

Every heist is a running-time problem.

Two jobs. Both come down to the lecture's question: how long will this program take? Measure it, model it, predict it, then find the algorithm that makes the question go away.

1 The door

Banco Infinito's vault opens with a 5-digit code. There is no lockout after wrong guesses, which is their first mistake. You know nothing about the code, so the only plan is the one from the slides: process all possibilities.

Locked.

Attempts0
Search space100,000
Clock0 s
def crack(check, d):
    for n in range(10**d):      # every code
        guess = str(n).zfill(d)
        if check(guess):
            return guess

2 Now scale it

The loop body runs once per code, so the cost model is one line: T(d) = c · 10d, where c is the time per guess. In the lecture, doubling N multiplied the time by a constant 2b. Here, adding one digit multiplies it by 10. That is the signature of exponential growth, and it is the whole reason codes work.

DigitsCodesBrute force, worst caseLeaky lock (step 3)

Times use the rig you picked above. The average case is half the worst case: same order of growth.

3 The inside job: the lock leaks

A contractor wrote the comparison like this. It is correct. It is also a disaster.

def check(guess, secret):
    for g, s in zip(guess, secret):
        if g != s:
            return False        # gives up early
    return True

A guess with the right first digit survives one more comparison before it is rejected, so the door answers a hair later. Measure the response time for each of the ten candidates, keep the slowest, move to the next position. The digits stop being a product and become a sum.

Comparisons the lock performs before it says no, per candidate digit.

Guesses used0
Upper bound 10·d50
Brute force bound100,000

Real attackers see nanoseconds buried in noise, so they repeat each guess many times and average, which is the same reason you repeat timing experiments. The fix is a comparison whose running time does not depend on the data (hmac.compare_digest in Python), plus a lockout.

4 Debrief

The bank upgrades from 5 digits to 10. Worst-case brute-force work is multiplied by…

Against the leaky lock, the same 10-digit code needs at most…

Keep Heist Lab handy