Skip to content

Conversation

@melvincarvalho
Copy link
Contributor

Summary

  • Adds explicit build rules to compile LYRA2RE-related files with -O1 optimization
  • Fixes GCC 13+ optimization bug that causes BMW256 hash corruption
  • Resolves blockchain sync failures at Fork update to qt 4.8.6 #1 (block 450,947) and beyond

Problem

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:

  • All LYRA2REv2 block hashes to be computed incorrectly
  • Proof-of-work verification failures for LYRA2REv2 blocks
  • Node unable to sync past Fork update to qt 4.8.6 #1 (block 450,947)

Solution

Add explicit Makefile rules to compile the following files with -O1:

  • bmw.c - BMW256 hash implementation (directly affected)
  • Lyra2RE.c - LYRA2REv2 main implementation
  • Lyra2.c - LYRA2 core functions
  • Sponge.c - Sponge construction used by LYRA2

Testing

Fixes #141

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
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 -O1 optimization instead of the default -O2
  • Targets four specific files affected by the compiler bug: bmw.c, Lyra2RE.c, Lyra2.c, and Sponge.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
Comment on lines 11 to 20
$(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 $@ $<
Copy link

Copilot AI Jan 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The custom compilation rules are missing the $(CFLAGS) variable that should be included in the compilation command. The standard automake rule for C files includes both $(AM_CFLAGS) and $(CFLAGS). Without $(CFLAGS), user-specified or configure-time CFLAGS will not be applied to these files.

The correct approach is to include $(CFLAGS) in the compilation command but place $(LYRA2RE_CFLAGS) after it to ensure the -O1 flag overrides any optimization level specified in CFLAGS. The compilation command should be:

$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) $(LYRA2RE_CFLAGS) -c -o $@ $<

This ensures that:

  1. All standard flags are applied
  2. The -O1 optimization override comes last and takes precedence
  3. Other non-optimization CFLAGS are still applied to these files
Suggested change
$(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 $@ $<

Copilot uses AI. Check for mistakes.
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.
Copy link

Copilot AI Jan 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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."

Suggested change
# 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.

Copilot uses AI. Check for mistakes.
- 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"
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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.

GCC 13 optimization bug causes LYRA2REv2 hash failures - fix identified

2 participants