Skip to content

fix: correct sign in Unfe odd-round subtraction - #21

Merged
Mzack9999 merged 1 commit into
projectdiscovery:mainfrom
SergoRoberto:fix-bug-with-unsuffle
Aug 3, 2026
Merged

Mzack9999 merged 1 commit into
projectdiscovery:mainfrom
SergoRoberto:fix-bug-with-unsuffle

Conversation

@SergoRoberto

Copy link
Copy Markdown

Problem

UnShuffle does not invert Shuffle. Since that round-trip is the whole point
of the package, the reverse direction is effectively unusable today.

range checked round-trip errors
1,000 1,000 988
100,000 20,000 19,997
651,399,200 20,001 20,001
6,300,000,000 20,000 20,000

(seed 0; same picture on seeds 1 and 42)

Cause

blackrock.go:91, in the odd-round branch of Unfe:

tmp -= -R   // adds R

The double negation adds R instead of subtracting it, so the subsequent
a - (tmp % a) wrap starts from the wrong value. The even-round branch a few
lines below already does it correctly (tmp = (tmp - R)), which makes the typo
easy to confirm by symmetry.

Shuffle is unaffected — it remains a valid permutation both before and after
this change. Only the inverse was broken.

Fix

One character:

-				tmp -= -R
+				tmp -= R

Tests

The existing suite only covered New(), so nothing exercised the cipher itself
and the bug passed CI green. Added:

  • TestShuffleUnShuffleRoundTrip — UnShuffle(Shuffle(i)) == i across 6 ranges
    x 4 seeds, from range=1 up to 6,300,000,000
  • TestShuffleIsPermutation — full domain, no duplicates, no values outside
    [0, Range)
  • TestUnfeInvertsFe — Unfe ∘ Fe == id for 1, 2 and 3 rounds; each parity
    takes a different branch of the reverse construction, so testing only the
    default round count hides an error in one of them
  • TestUnfeSubtractsRoundOutput — targeted regression on the fixed line; it
    fails if no input reaches the F(j, L, seed) > R branch, so the coverage
    cannot rot silently

Verified the tests actually catch the bug: reverting the one-character fix makes
three of the four fail. With the fix, go test ./... and go vet are clean, and
an exhaustive sweep over every range from 1 to 3000 x 4 seeds (18,006,000 checks)
reports zero failures.

Note

gofmt -l flags both files, but that is pre-existing on main — the repo stores
them with CRLF line endings. With \r stripped both are clean. I kept CRLF in the
added lines so the source diff stays at one line instead of rewriting the file;

Closes #20

@ehsandeep
ehsandeep requested a review from Mzack9999 August 2, 2026 19:41
@Mzack9999
Mzack9999 merged commit ced9807 into projectdiscovery:main Aug 3, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UnShuffle does not invert Shuffle (sign error in Unfe, blackrock.go:91)

2 participants