4.7 KiB
TLA+ Specification for Registration Key Algorithm
This directory contains the formal TLA+ specification for the registration key generation algorithm.
Files
- RegistrationKey.tla - Main TLA+ specification module
- RegistrationKey.cfg - TLC model checker configuration
- README.md - This file
Overview
The specification models the registration key generation as a state machine with the following phases:
- Init - Initial state with input parameters
- Concatenate - Combine input strings in specific order
- Reverse - Reverse the byte array
- Transform - Apply mathematical transformation to each byte
- SpecialCharHandle - Handle special characters
- HashInput - Prepare input for MD5 hashing
- MD5Round1-3 - Multiple rounds of MD5 updates
- MD5Final - Final MD5 digest
- Checksum - Generate CRC checksum
- Combine - Combine checksum and hash
- Encode - Base-32 encode the result
- Complete - Final state with generated key
State Variables
Input Variables
firstName- User's first namelastName- User's last nameproductName- Product nameemail- User's email addresssecret- Secret seed value (optional)additionalField1-3- Additional fields (optional)
Algorithm State
phase- Current phase of executionbyteArray- Working byte arrayposition- Current position in array processinghashDigest- MD5 hash result (16 bytes)checksum- CRC checksum (4 bytes)registrationKey- Final 20-character key
Invariants
Type Invariant
Ensures all variables maintain their correct types throughout execution.
Phase Order
Enforces correct sequencing of algorithm phases:
- Complete phase must have 20-character key
- MD5 phases only occur when secret is provided
Deterministic
Same inputs always produce the same registration key.
No Empty Key
Complete phase must produce a non-empty key.
Properties
Correctness
The specification always maintains type invariants.
Termination
The algorithm eventually reaches the Complete phase.
Uniqueness
Different inputs produce different keys (with high probability due to cryptographic hash).
Running the Model Checker
To check the specification with TLC:
tlc RegistrationKey.tla -config RegistrationKey.cfg
State Space Constraints
To make model checking feasible, the configuration limits:
- firstName: ≤ 10 characters
- lastName: ≤ 10 characters
- productName: ≤ 15 characters
- email: ≤ 20 characters
Key Insights from Formal Specification
-
Deterministic: The algorithm is completely deterministic - same inputs always produce the same output.
-
Phase-Based: The algorithm progresses through well-defined phases in a specific order.
-
Byte-Level Operations: Most operations work at the byte level with mathematical transformations.
-
Multiple Hash Rounds: MD5 is applied multiple times (update-update-digest-update-digest) for additional complexity.
-
Character Set Variations: Two different base-32 character sets are used depending on whether additional fields are present.
-
Fixed Output Length: Always produces exactly 20 characters regardless of input length.
Abstract vs Concrete
This TLA+ specification is more abstract than the Java implementation:
-
MD5 Hash: Modeled abstractly as a non-deterministic choice from valid 16-byte sequences. In practice, it's the actual MD5 algorithm.
-
Checksum: Modeled abstractly as a 4-byte sequence. In practice, it's a CRC calculation on the hash.
-
Byte Values: Constrained to valid ranges but not modeling exact arithmetic overflow behavior.
This abstraction level is appropriate for verifying the algorithm's structure, phase ordering, and key properties without getting lost in cryptographic implementation details.
Verification Goals
The TLA+ specification can verify:
- ✓ Algorithm always terminates
- ✓ All phases execute in correct order
- ✓ Output is always correct length (20 chars)
- ✓ Type safety throughout execution
- ✓ Deterministic behavior
- ✗ Cryptographic security (out of scope for TLA+)
- ✗ Key collision probability (requires statistical analysis)
Extensions
Possible extensions to this specification:
- Multi-Algorithm Support: Model all 4 variants (4, 5, 7, 8 parameters)
- Validation: Add registration key validation logic
- Concurrent Usage: Model multiple users generating keys simultaneously
- Key Database: Model storage and lookup of generated keys
- Expiration: Add time-based key expiration logic