From fd900aa3ba6e53ae32bfef967b0af4b36548d43e Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Fri, 2 Oct 2026 16:45:46 -0500 Subject: [PATCH] decomp: reconstruct MD5 endian-loaded rotation operands --- libs/RevoEX/src/net/md5.c | 12 ++- .../allhands-md5-rotation.attempts.md | 86 +++++++++++++++++++ 2 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 tools/decomp-assist/allhands-md5-rotation.attempts.md diff --git a/libs/RevoEX/src/net/md5.c b/libs/RevoEX/src/net/md5.c index c451c87a..c53b94c6 100644 --- a/libs/RevoEX/src/net/md5.c +++ b/libs/RevoEX/src/net/md5.c @@ -68,8 +68,14 @@ void NETMD5GetDigest(NETMD5Context* context, void* digest) { memset(context, 0, sizeof(*context)); } -#define ROTATE(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) -#define STEP(a, b, c, d, f, word, n) ((a) += (f), (a) = (b) + ROTATE(__lwbrx(0, (int)(word)) + (*constant + (a)), n)) +static inline u32 rotateSum(u32 state, const u32* word, u32 constant, int shift) { + // Each rotation half reads its own byte-reversed message word. + u32 right = __lwbrx((void*)word, 0); + u32 left = __lwbrx((void*)word, 0); + return ((state + (left + constant)) << shift) | ((right + (constant + state)) >> (32 - shift)); +} + +#define STEP(a, b, c, d, f, word, n) ((a) = (b) + rotateSum((a) + (f), word, *constant, n)) static void ProcessBlock(NETMD5Context* context) { static u32 constants[64] = {0xD76AA478, 0xE8C7B756, 0x242070DB, 0xC1BDCEEE, 0xF57C0FAF, 0x4787C62A, 0xA8304613, 0xFD469501, 0x698098D8, 0x8B44F7AF, 0xFFFF5BB1, 0x895CD7BE, 0x6B901122, 0xFD987193, 0xA679438E, 0x49B40821, 0xF61E2562, 0xC040B340, 0x265E5A51, 0xE9B6C7AA, 0xD62F105D, 0x02441453, 0xD8A1E681, 0xE7D3FBC8, 0x21E1CDE6, 0xC33707D6, 0xF4D50D87, 0x455A14ED, 0xA9E3E905, 0xFCEFA3F8, 0x676F02D9, 0x8D2A4C8A, 0xFFFA3942, 0x8771F681, 0x6D9D6122, 0xFDE5380C, 0xA4BEEA44, 0x4BDECFA9, 0xF6BB4B60, 0xBEBFBC70, 0x289B7EC6, 0xEAA127FA, 0xD4EF3085, 0x04881D05, 0xD9D4D039, 0xE6DB99E5, 0x1FA27CF8, 0xC4AC5665, 0xF4292244, 0x432AFF97, 0xAB9423A7, 0xFC93A039, 0x655B59C3, 0x8F0CCC92, 0xFFEFF47D, 0x85845DD1, 0x6FA87E4F, 0xFE2CE6E0, 0xA3014314, 0x4E0811A1, 0xF7537E82, 0xBD3AF235, 0x2AD7D2BB, 0xEB86D391}; @@ -83,13 +89,13 @@ static void ProcessBlock(NETMD5Context* context) { block = context->buffer32; word = block; constant = constants; - index = indices; for (round = 0; round < 4; ++round) { STEP(a,b,c,d,(b & c) | (~b & d),word,7); ++word; ++constant; STEP(d,a,b,c,(a & b) | (~a & c),word,12); ++word; ++constant; STEP(c,d,a,b,(d & a) | (~d & b),word,17); ++word; ++constant; STEP(b,c,d,a,(c & d) | (~c & a),word,22); ++word; ++constant; } + index = indices; for (round = 0; round < 4; ++round) { STEP(a,b,c,d,(b & d) | (c & ~d),block + *index,5); ++index; ++constant; STEP(d,a,b,c,(a & c) | (b & ~c),block + *index,9); ++index; ++constant; diff --git a/tools/decomp-assist/allhands-md5-rotation.attempts.md b/tools/decomp-assist/allhands-md5-rotation.attempts.md new file mode 100644 index 00000000..f3ddf1e3 --- /dev/null +++ b/tools/decomp-assist/allhands-md5-rotation.attempts.md @@ -0,0 +1,86 @@ +# MD5 rotation-expression reconstruction, 2026-10-02 + +Baseline: `449529d9`, 43U only. Owned source: `libs/RevoEX/src/net/md5.c`. +The current user priority permits validated structural/fuzzy improvements before +exact matching and linking. This is a partial matching candidate, not an exact +match or a linking change. AES experiments were saved outside the checkout and +AES was restored to the baseline before this candidate was validated. + +## Source evidence + +- Ground truth: `build/43U/obj/libs/RevoEX/src/net/md5.o`, `ProcessBlock`. +- The target is 0x4C8 bytes / 306 instructions, with a 0x20-byte stack frame. +- Each of the sixteen unrolled MD5 steps contains two byte-reversed word loads. + The first pair is at function-relative offsets 0x3C and 0x40. The right-shift + half consumes the first value, and the left-shift half consumes the second. +- The target retains four additions across the two rotation halves: the + right-shift half adds the message word to (round constant + state), and the + left-shift half adds the state to (message word + round constant). The state + includes the round Boolean function. The first rotation uses right shift 25 + and left shift 7; the remaining shifts follow the existing MD5 round schedule. +- The index-table address is first materialized after the first sixteen steps, + rather than before the sequential-word round. +- The inline helper expresses these real unsigned 32-bit operands and restores + the target-sized body. It introduces no assembly, volatile qualifier, + uninitialized value, fake storage, padding, or compiler-option change. +- All four round algorithms, all 64 constants, all 48 indices, and all public + API bodies are otherwise unchanged. The old ROTATE macro became unused and + was removed. + +## Attempts + +1. Direct pointer-form byte-reversed loads and alternate ordinary arithmetic + groupings: 274-294 instructions, at best near the starting score. Rejected. +2. A typed inline endian-load wrapper: improved instruction similarity but + retained 276 instructions and insufficient target operation boundaries. +3. Rotation-expression helpers without independent load results: 288-290 + instructions, approximately 41-44% ProcessBlock fuzzy. Rejected. +4. Explicitly named byte-reversed operands for the two rotation halves: + 306 instructions / 81.82026% fuzzy. Retained the target's first-read/right- + shift ordering. Reversing those loads reached 82.0% but added two instructions + and contradicted the target's operand ordering, so it was rejected. +5. Sequential partial-sum statements and equivalent regroupings did not + improve the retained structural form. No register-only declaration sweep. + +## Validation + +Command setup: +`python3 configure.py --version 43U --wrapper ../toolchain/wibo-build/wibo` +`WIBO_SJIS_MISSING_IMPORTS=1 ../.venv/bin/ninja` + +- `pool_diff.py`: identical empty MD5 string pool, 0/0 strings. +- `.data`: all 448 bytes identical. The source `.sdata` is the one-byte + end marker (0x80); the target has the same byte followed by seven alignment + zeros. This existing section-size difference is unchanged, and objdiff + reports `.sdata` and unit data at 100%. No padding was added. +- `NETMD5Init`: 16/16 instructions, ctxdiff diffs 0, objdiff 100%. +- `NETMD5Update`: 60/60 instructions, ctxdiff diffs 0, objdiff 100%. +- `NETMD5GetDigest`: 74/74 instructions, ctxdiff diffs 0, objdiff 100%. +- `ProcessBlock`: 294 -> 306 instructions against target 306; + 43.290848 -> 81.82026% fuzzy. Final ctxdiff has 255 positional differences, + primarily allocation, scheduling and remaining arithmetic grouping. Not exact. +- MD5 unit: 61.945175 -> 87.80044% fuzzy; exact 3/4 -> 3/4; + exact code 600/1824 unchanged; data 456/456 (100%) unchanged; unlinked unchanged. +- Full 1027-unit report: no unit regressed in exact functions, exact code, + exact data or fuzzy score versus the baseline report. Overall fuzzy moved + 99.588615 -> 99.60437%; exact code and linking did not change. +- Full 43U build passed. `build/43U/ok` passed. DOL SHA1: + `26116613f624061ba99c8d1a299aaa6efa85670d`. + +Host algorithm validation: `/tmp/crypto-remainder/validate_md5.py` compiles the +production source unchanged with host shims for PPC byte-reversed intrinsics. +It verifies ProcessBlock through NETMD5Update with externally supplied RFC1321 +padding, comparing state words with Python hashlib. The host test deliberately +does not exercise GetDigest's platform-specific big-endian length stores. + +Result: 3,088 successful digest/chunking comparisons, covering the seven RFC1321 +example strings, one million `a` bytes, 1,500 seeded messages, lengths around +55/56/63/64/65 and larger block boundaries, and fixed/random chunk sizes. +Run: `python3 /tmp/crypto-remainder/validate_md5.py`. + +Local evidence (not committed or uploaded): `/tmp/crypto-remainder/md5.target`, +`md5-final.ctx`, `md5-final.json`, `baseline.json`, `full-build.log`, and the +host test plus its header shim. Only source and this summary are committed. + +GATE: partial decompilation candidate validated; no regressions; exact matching +and linking remain open. No claim of all-100% completion. -- 2.49.1