Demonstration: Designing a Combination Lock

A combination lock is the classic first state machine, and for good reason. It sees one key at a time, so it has to remember something, and what it remembers is exactly what a state is.
The specification is one sentence: the door opens when someone types the code. Getting the forward path right takes a minute. Everything else in this demonstration is about the keys that are not the code, which is where real designs go wrong.
How to use it
Section titled “How to use it”The demonstration has five steps, and the code you choose in step 1 carries through all of them.
1 States asks how many states a three-digit lock needs. 2 Wrong keys has a table: for each state and each wrong key, you choose where the lock goes. Try it on the keypad, then press Test every sequence. The lock is checked against every possible run of keys, and if it fails, you are shown the shortest sequence that catches it, which you can replay.
3 Encoding builds your lock with two flip-flops and with four, and counts the gates. 4 Moore or Mealy puts both kinds of lock on a timing diagram. 5 Brute force attacks the lock you built.
Design a Combination Lock
Four states and a keypad. Then find out what happens when someone types it wrong, or tries every code.
Walkthrough
Section titled “Walkthrough”Step 1: Count what the lock must remember
Section titled “Step 1: Count what the lock must remember”With the code 3-1-4, choose each number of states in turn. Four is the answer for this design: nothing typed yet, one digit right, two digits right, and open. The lock never needs to know which wrong keys were pressed, only how much of the code has just been typed.
Step 2: Send every wrong key back to the start, then test it
Section titled “Step 2: Send every wrong key back to the start, then test it”Open 2 Wrong keys. Every cell starts at S0, which is what most people would choose. Type 3, 1, 4 on the keypad and the door opens. Now press Test every sequence.
It fails on 3 3 1 4. After the first 3 the lock is in S1. The second 3 is wrong, so it goes back to S0 and the 3 is thrown away. But that 3 was the start of the code. Change the cell for S1 on key 3 to S1 and test again.
Keep going until the test passes, then try 1-1-2, 1-2-1 and 7-7-7. Each one fails in its own way, except 7-7-7, where sending every wrong key to S0 is already correct.
The rule underneath every fix is the same: after a wrong key, ask how much of the code the last few keys could still be the start of.
Step 3: Compare the encodings
Section titled “Step 3: Compare the encodings”Open 3 Encoding. Binary stores four states in two flip-flops. One-hot uses four, one per state. Read the one-hot equations: each term is one arrow on the state diagram, written down directly. Then compare the gate counts, which are computed from the lock you built.
Step 4: Watch the door open
Section titled “Step 4: Watch the door open”Open 4 Moore or Mealy. The Moore lock opens on the clock edge after the last key and stays open. The Mealy lock has no OPEN state, so it needs one state fewer. Its door opens the instant the key goes down, but closes on the next edge.
Move the slider towards 95%. The Moore door does not change shape. The Mealy door shrinks to a sliver, because its width depends on when a person happened to press the key.
Step 5: Attack it
Section titled “Step 5: Attack it”Open 5 Brute force and press Attack. Trying codes one at a time takes up to 3,000 keypresses. A de Bruijn sequence contains every three-digit code as overlapping windows, and it opens your lock in at most 1,002. The overlap handling that made the lock forgiving in step 2 is what lets every single key test a new code.
Then look at the table. Only a lock that checks fixed blocks of keys knows when an attempt has failed, so only that lock can have a lockout.
Check your understanding
Section titled “Check your understanding”Why does a lock for a three-digit code need four states?
The code is 3-1-4. The lock sends every wrong key back to S0. Someone types 3 3 1 4. What happens?
A Mealy version of the lock opens the door from S2 when the last key is pressed. Why can its door pulse be very short?
Why is a lock that handles overlapping keys correctly easier to brute-force?
Match each design choice to its consequence
Wrap-up
Section titled “Wrap-up”The forward path through a state machine is the easy part. Nearly all of the design is in the arrows nobody draws first.
- A state records what the machine must remember to decide what to do next, and nothing more.
- Every state needs an arrow for every input. For a sequence detector, a wrong input goes to the longest part of the sequence that the latest inputs could still be the start of.
- Test against every sequence, not just the intended one. The shortest failing sequence is usually only one key longer than the code.
- Binary encoding saves flip-flops. One-hot encoding saves design effort and drives outputs directly.
- Moore outputs are clean and clock-aligned. Mealy outputs are faster but take their timing from the inputs.
- Security is a property of the whole machine. A lockout needs a lock that knows when an attempt has ended.
© 2026 Derek Molloy, Dublin City University. All rights reserved.