From 151b87bea6f602f237f3765f00b3d7438f9c7483 Mon Sep 17 00:00:00 2001 From: Melvin Carvalho Date: Fri, 9 Jan 2026 15:51:10 +0100 Subject: [PATCH 1/2] Fix GCC 13+ optimization bug in BMW256 for LYRA2REv2 hashing 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 --- src/Makefile.am | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Makefile.am b/src/Makefile.am index 9d3ddb2d30..a17fe5bc58 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -2,6 +2,23 @@ include Makefile.include 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. +# See: https://github.com/project-bitmark/bitmark/issues/141 +LYRA2RE_CFLAGS = -O1 + +bmw.o: bmw.c + $(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 $@ $< + noinst_LIBRARIES = \ libbitmark_server.a \ libbitmark_common.a \ From 880294e537bdfa12cc6081bbfc50f7422953f80d Mon Sep 17 00:00:00 2001 From: Melvin Carvalho Date: Fri, 9 Jan 2026 16:38:07 +0100 Subject: [PATCH 2/2] Address Copilot review feedback - 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" --- src/Makefile.am | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index a17fe5bc58..a6a4821780 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -3,21 +3,22 @@ include Makefile.include 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. +# 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. # See: https://github.com/project-bitmark/bitmark/issues/141 LYRA2RE_CFLAGS = -O1 bmw.o: bmw.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) $(LYRA2RE_CFLAGS) $(AM_CFLAGS) -c -o $@ $< + $(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) $(LYRA2RE_CFLAGS) $(AM_CFLAGS) -c -o $@ $< + $(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) $(LYRA2RE_CFLAGS) $(AM_CFLAGS) -c -o $@ $< + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) $(LYRA2RE_CFLAGS) -c -o $@ $< noinst_LIBRARIES = \ libbitmark_server.a \