Merged
Conversation
perf(mfkey): comprehensive performance optimization of Crypto1 key recovery
The recovery pipeline transitions from a monolithic scalar approach to a
modular, bitsliced, batch-oriented architecture tailored for the
FlipperZero's ARM Cortex-M4.
Refactored monolithic mfkey.c into specialized compilation units:
mfkey_batch_prelude (parallel R0-R3 tree), mfkey_state_expansion
(R4-R12 LFSR search), mfkey_bs_verify (SWAR verification kernels),
mfkey_recovery (sort + dispatch), mfkey_dedup (hash + scan).
Filter optimizations (crypto1.h):
- filter_pair/filter_pair_xor: compute filter(v) and filter(v|1) with
shared lookup work, since bit 0 only affects one LUT index
- ADJ_FILTER macro: pre-fold keystream bit into filter constant,
eliminating a per-iteration XOR
- update_contribution_reg: register-based parity update avoiding
array access overhead
Batch prelude (rounds 0-3):
- 2KB precomputed Super-LUT bitmasks (R0-R3) enable 32-lane parallel
survival checks via single indexed load per round
- Unified tree returns per-child leaf masks, eliminating the separate
prefilter + reconstruction pipeline
State expansion (rounds 4-12):
- fork_delta deferred past round 4 early-exit (~95% of calls exit
before needing it)
- Rounds 5-6 manually unrolled with in-place buffer updates
- ADJ_FILTER XOR shortcut applied throughout
Bitsliced SWAR verification:
- 32-lane parallel candidate verification for all three attack types
(mfkey32, static_nested, static_encrypted), replacing scalar
candidate-at-a-time paths
- VFP register parking: use M4F FPU registers as scratchpad via vmov,
reducing register pressure during bitsliced filter computation
- Fused rollback/crypt + keystream comparison with byte-boundary
early exit
- 32x32 butterfly transpose for scalar-to-SWAR conversion
Recovery and deduplication:
- 8-bit radix sort replacing quicksort for O(n) candidate ordering
- Fibonacci hash (golden ratio multiply-shift) bitmask filter for
identity deduplication
- 8x unrolled (Duff's device) loop in duplicate scan
- Level-0 sort bypass and empty cross-product short-circuit in
MSB-walk
Attack engine:
- R4 lane mask precomputation (batch-invariant, avoids per-lane
Flash table reads)
- OPT_BARRIER prevents compiler from hoisting BIT(xks, round)
extractions and spilling to stack
hedger
approved these changes
Feb 16, 2026
This was referenced Feb 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What's new
Verification
Here are my test cases, if they are helpful for verification:
Checklist (For Reviewer)