Fix GCC 13+ optimization bug in BMW256 for LYRA2REv2 hashing#142
Fix GCC 13+ optimization bug in BMW256 for LYRA2REv2 hashing#142melvincarvalho wants to merge 2 commits intomasterfrom
Conversation
GCC 13 and later have optimization bugs that cause undefined behavior in the BMW256 hash function when compiled with -O2 or higher. This results in incorrect LYRA2REv2 hashes and causes block verification failures at Fork #1 (block 450,947) and beyond. The fix adds explicit build rules for LYRA2RE-related source files (bmw.c, Lyra2RE.c, Lyra2.c, Sponge.c) to compile them with -O1 optimization level instead of the default. Fixes #141
There was a problem hiding this comment.
Pull request overview
This PR addresses a critical GCC 13+ compiler optimization bug that causes incorrect BMW256 hash calculations in the LYRA2REv2 algorithm, preventing blockchain synchronization past block 450,947.
Key changes:
- Adds custom Makefile rules to compile LYRA2RE-related files with
-O1optimization instead of the default-O2 - Targets four specific files affected by the compiler bug:
bmw.c,Lyra2RE.c,Lyra2.c, andSponge.c - Includes documentation explaining the workaround and referencing issue #141
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/Makefile.am
Outdated
| $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(LYRA2RE_CFLAGS) $(AM_CFLAGS) -c -o $@ $< | ||
|
|
||
| Lyra2RE.o: Lyra2RE.c | ||
| $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(LYRA2RE_CFLAGS) $(AM_CFLAGS) -c -o $@ $< | ||
|
|
||
| Lyra2.o: Lyra2.c | ||
| $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(LYRA2RE_CFLAGS) $(AM_CFLAGS) -c -o $@ $< | ||
|
|
||
| Sponge.o: Sponge.c | ||
| $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(LYRA2RE_CFLAGS) $(AM_CFLAGS) -c -o $@ $< |
There was a problem hiding this comment.
The custom compilation rules are missing the
The correct approach is to include
This ensures that:
- All standard flags are applied
- The -O1 optimization override comes last and takes precedence
- Other non-optimization CFLAGS are still applied to these files
| $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(LYRA2RE_CFLAGS) $(AM_CFLAGS) -c -o $@ $< | |
| Lyra2RE.o: Lyra2RE.c | |
| $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(LYRA2RE_CFLAGS) $(AM_CFLAGS) -c -o $@ $< | |
| Lyra2.o: Lyra2.c | |
| $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(LYRA2RE_CFLAGS) $(AM_CFLAGS) -c -o $@ $< | |
| Sponge.o: Sponge.c | |
| $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(LYRA2RE_CFLAGS) $(AM_CFLAGS) -c -o $@ $< | |
| $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) $(LYRA2RE_CFLAGS) -c -o $@ $< | |
| Lyra2RE.o: Lyra2RE.c | |
| $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) $(LYRA2RE_CFLAGS) -c -o $@ $< | |
| Lyra2.o: Lyra2.c | |
| $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) $(LYRA2RE_CFLAGS) -c -o $@ $< | |
| Sponge.o: Sponge.c | |
| $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) $(LYRA2RE_CFLAGS) -c -o $@ $< |
src/Makefile.am
Outdated
| AM_CPPFLAGS += -I$(builddir) | ||
|
|
||
| # GCC 13+ has optimization bugs that affect the BMW256 hash function used in LYRA2RE. | ||
| # These files must be compiled with reduced optimization (-O1) to avoid undefined behavior. |
There was a problem hiding this comment.
The comment states "to avoid undefined behavior" which is technically imprecise. This is a GCC optimizer bug that produces incorrect code, not undefined behavior in the C standard sense (which would indicate a problem with the source code). Consider rephrasing to be more accurate, such as:
"These files must be compiled with reduced optimization (-O1) to work around a GCC 13+ optimizer bug that produces incorrect code at higher optimization levels."
| # These files must be compiled with reduced optimization (-O1) to avoid undefined behavior. | |
| # These files must be compiled with reduced optimization (-O1) to work around a GCC 13+ optimizer bug that produces incorrect code at higher optimization levels. |
- Add $(CFLAGS) to compilation rules so user-specified flags are applied - Place $(LYRA2RE_CFLAGS) last to ensure -O1 overrides any optimization flags - Improve comment wording: "optimizer bug" is more accurate than "undefined behavior"
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Summary
-O1optimizationProblem
GCC 13 and later have optimization bugs affecting the BMW256 hash function used in the LYRA2REv2 hash chain. When compiled with
-O2(the default), the BMW256 function produces incorrect results, causing:Solution
Add explicit Makefile rules to compile the following files with
-O1:bmw.c- BMW256 hash implementation (directly affected)Lyra2RE.c- LYRA2REv2 main implementationLyra2.c- LYRA2 core functionsSponge.c- Sponge construction used by LYRA2Testing
Fixes #141