From 43254ca8be5315b447944d77bae2b1c30966e7e7 Mon Sep 17 00:00:00 2001 From: flynn162 <54922775+flynn162@users.noreply.github.com> Date: Tue, 1 Jul 2025 21:08:08 -0700 Subject: [PATCH 1/7] makefile: filter out the .so file from compiler input, add rules for object merging --- src/makefile | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/makefile b/src/makefile index a06f1a9..f41422b 100644 --- a/src/makefile +++ b/src/makefile @@ -62,7 +62,7 @@ OBJECTS=$(patsubst %.c,%.o,$(SOURCES)) # Uncomment to use $ORIGIN as the runtime search path #SOFLAGS += -Wl,-rpath,'$$'ORIGIN -all: liboprf.$(SOEXT) liboprf.$(STATICEXT) noise_xk/liboprf-noiseXK.$(SOEXT) liboprf.pc +all: liboprf.$(SOEXT) liboprf.$(STATICEXT) noise_xk/liboprf-noiseXK.$(SOEXT) liboprf.pc liboprf_merged_localized.o debug: DEFINES=-DTRACE debug: all @@ -83,10 +83,16 @@ asan: all AR ?= ar liboprf.$(SOEXT): $(SOURCES) noise_xk/liboprf-noiseXK.$(SOEXT) - $(CC) $(CFLAGS) -fPIC -shared $(SOFLAGS) -o $@ $^ $(LDFLAGS) + $(CC) $(CFLAGS) -fPIC -shared $(SOFLAGS) -o $@ $(filter %.c,$^) $(LDFLAGS) liboprf-corrupt-dkg.$(SOEXT): $(SOURCES) noise_xk/liboprf-noiseXK.$(SOEXT) - $(CC) $(CFLAGS) -DUNITTEST -DUNITTEST_CORRUPT -fPIC -shared $(SOFLAGS) -o $@ $^ $(LDFLAGS) + $(CC) $(CFLAGS) -DUNITTEST -DUNITTEST_CORRUPT -fPIC -shared $(SOFLAGS) -o $@ $(filter %.c,$^) $(LDFLAGS) + +liboprf_merged.o: $(OBJECTS) + ld -r -o $@ $^ + +liboprf_merged_localized.o: liboprf_merged.o + objcopy --localize-hidden $^ $@ liboprf.$(STATICEXT): $(OBJECTS) $(AR) rcs $@ $^ From 25dc7b3b0865e53ca4ee91bab95c8aa0861768bc Mon Sep 17 00:00:00 2001 From: flynn162 <54922775+flynn162@users.noreply.github.com> Date: Thu, 3 Jul 2025 20:37:57 -0700 Subject: [PATCH 2/7] makefile: Package the merged localized object into liboprf_release.a --- .gitignore | 2 +- src/makefile | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 20bb6ba..1243bae 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -liboprf.a +liboprf*.a *.o .arch bench diff --git a/src/makefile b/src/makefile index f41422b..5180e90 100644 --- a/src/makefile +++ b/src/makefile @@ -13,6 +13,9 @@ SOEXT?=so STATICEXT?=a SOVER=0 +# To statically link the noiseXK library, use these LDFLAGS (and comment out the other LDFLAGS) +#LDFLAGS?=-lsodium -l:liboprf-noiseXK.a -Wl,--exclude-libs,ALL -Lnoise_xk + UNAME := $(shell uname -s) ARCH := $(shell uname -m) ifeq ($(UNAME),Darwin) @@ -56,13 +59,14 @@ endif CFLAGS+=$(INCLUDES) -SOURCES=oprf.c toprf.c dkg.c dkg-vss.c utils.c tp-dkg.c mpmult.c stp-dkg.c toprf-update.c $(EXTRA_SOURCES) +LIBOPRF_SOURCES=oprf.c toprf.c dkg.c dkg-vss.c utils.c tp-dkg.c mpmult.c stp-dkg.c toprf-update.c +SOURCES=$(LIBOPRF_SOURCES) $(EXTRA_SOURCES) OBJECTS=$(patsubst %.c,%.o,$(SOURCES)) # Uncomment to use $ORIGIN as the runtime search path #SOFLAGS += -Wl,-rpath,'$$'ORIGIN -all: liboprf.$(SOEXT) liboprf.$(STATICEXT) noise_xk/liboprf-noiseXK.$(SOEXT) liboprf.pc liboprf_merged_localized.o +all: liboprf.$(SOEXT) liboprf_release.$(STATICEXT) noise_xk/liboprf-noiseXK.$(SOEXT) liboprf.pc debug: DEFINES=-DTRACE debug: all @@ -94,6 +98,9 @@ liboprf_merged.o: $(OBJECTS) liboprf_merged_localized.o: liboprf_merged.o objcopy --localize-hidden $^ $@ +liboprf_release.$(STATICEXT): liboprf_merged_localized.o + $(AR) rcs $@ $^ + liboprf.$(STATICEXT): $(OBJECTS) $(AR) rcs $@ $^ @@ -145,7 +152,7 @@ $(DESTDIR)$(PREFIX)/lib/liboprf.$(SOEXT): liboprf.$(SOEXT) cp $< $@.$(SOVER) ln -sf $@.$(SOVER) $@ -$(DESTDIR)$(PREFIX)/lib/liboprf.$(STATICEXT): liboprf.$(STATICEXT) +$(DESTDIR)$(PREFIX)/lib/liboprf.$(STATICEXT): liboprf_release.$(STATICEXT) mkdir -p $(DESTDIR)$(PREFIX)/lib cp $< $@ From eec368ab20cec1a2e5ba4468947596a153218a60 Mon Sep 17 00:00:00 2001 From: flynn162 <54922775+flynn162@users.noreply.github.com> Date: Thu, 3 Jul 2025 20:38:31 -0700 Subject: [PATCH 3/7] makefile: Build objects with -ffunction-sections -fdata-sections --- src/makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/makefile b/src/makefile index 5180e90..3ff64f5 100644 --- a/src/makefile +++ b/src/makefile @@ -1,6 +1,6 @@ PREFIX?=/usr/local INCLUDES=-Inoise_xk/include -Inoise_xk/include/karmel -Inoise_xk/include/karmel/minimal -CFLAGS?=-march=native -Wall -O2 -g \ +CFLAGS?=-march=native -Wall -O2 -g -ffunction-sections -fdata-sections \ -Werror=attributes -Werror=format-security -Werror=format-truncation -Werror=implicit-function-declaration \ -Wformat=2 -Wconversion -Wimplicit-fallthrough \ -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3 \ From 3efc71058aaf18eb8a155235e0072a7d11ae2d8d Mon Sep 17 00:00:00 2001 From: flynn162 <54922775+flynn162@users.noreply.github.com> Date: Thu, 3 Jul 2025 21:48:00 -0700 Subject: [PATCH 4/7] makefile: Clean up 'aux_/*.o' --- src/makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/src/makefile b/src/makefile index 3ff64f5..a4620a3 100644 --- a/src/makefile +++ b/src/makefile @@ -116,6 +116,7 @@ liboprf.pc: clean: rm -f *.o liboprf.$(SOEXT) liboprf.$(STATICEXT) liboprf-corrupt-dkg.$(SOEXT) + rm -f aux_/*.o make -C tests clean make -C noise_xk clean From ce5671dba2b2b0d7f2833978b1f21dcdcd746883 Mon Sep 17 00:00:00 2001 From: flynn162 <54922775+flynn162@users.noreply.github.com> Date: Thu, 3 Jul 2025 21:50:01 -0700 Subject: [PATCH 5/7] makefile: suppress implicit rules, add terminal colors - Build all objects explicitly. The rules are generated by putting `eval` inside a `foreach` loop. Guides for writing dynamic rules are in the comments. - `$(info )` : The space is strictly required. --- src/makefile | 74 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 4 deletions(-) diff --git a/src/makefile b/src/makefile index a4620a3..15dc9c5 100644 --- a/src/makefile +++ b/src/makefile @@ -66,6 +66,10 @@ OBJECTS=$(patsubst %.c,%.o,$(SOURCES)) # Uncomment to use $ORIGIN as the runtime search path #SOFLAGS += -Wl,-rpath,'$$'ORIGIN +# Terminal colors: purely for UX, comment out if causing problems +decor = $(shell [ -t 0 ] && printf "\033[38;5;2m====" || printf "====") +endDecor = $(shell [ -t 0 ] && printf "====\033[0m" || printf "====") + all: liboprf.$(SOEXT) liboprf_release.$(STATICEXT) noise_xk/liboprf-noiseXK.$(SOEXT) liboprf.pc debug: DEFINES=-DTRACE @@ -86,28 +90,35 @@ asan: all AR ?= ar -liboprf.$(SOEXT): $(SOURCES) noise_xk/liboprf-noiseXK.$(SOEXT) - $(CC) $(CFLAGS) -fPIC -shared $(SOFLAGS) -o $@ $(filter %.c,$^) $(LDFLAGS) +liboprf.$(SOEXT): liboprf_merged.o noise_xk/liboprf-noiseXK.$(SOEXT) + $(info $(decor) Build dynamic release library: $@ $(endDecor)) + $(CC) $(CFLAGS) -fPIC -shared $(SOFLAGS) -o $@ liboprf_merged.o $(LDFLAGS) -liboprf-corrupt-dkg.$(SOEXT): $(SOURCES) noise_xk/liboprf-noiseXK.$(SOEXT) - $(CC) $(CFLAGS) -DUNITTEST -DUNITTEST_CORRUPT -fPIC -shared $(SOFLAGS) -o $@ $(filter %.c,$^) $(LDFLAGS) +liboprf-corrupt-dkg.$(SOEXT): liboprf_merged.o noise_xk/liboprf-noiseXK.$(SOEXT) + $(info $(decor) Build unit test library: $@ $(endDecor)) + $(CC) $(CFLAGS) -DUNITTEST -DUNITTEST_CORRUPT -fPIC -shared $(SOFLAGS) -o $@ liboprf_merged.o $(LDFLAGS) liboprf_merged.o: $(OBJECTS) + $(info $(decor) Merge object files $(endDecor)) ld -r -o $@ $^ liboprf_merged_localized.o: liboprf_merged.o + $(info $(decor) Localize symbols $(endDecor)) objcopy --localize-hidden $^ $@ liboprf_release.$(STATICEXT): liboprf_merged_localized.o + $(info $(decor) Build static release library: $@ $(endDecor)) $(AR) rcs $@ $^ liboprf.$(STATICEXT): $(OBJECTS) $(AR) rcs $@ $^ noise_xk/liboprf-noiseXK.$(SOEXT): + $(info $(decor) Compile vendorized noiseXK library: $@ $(endDecor)) make -C noise_xk all noise_xk/liboprf-noiseXK.$(STATICEXT): + $(info $(decor) Compile vendorized noiseXK library: $@ $(endDecor)) make -C noise_xk all liboprf.pc: @@ -194,3 +205,58 @@ test: liboprf-corrupt-dkg.$(SOEXT) liboprf.$(STATICEXT) noise_xk/liboprf-noiseXK make -C noise_xk test PHONY: clean + +# Dynamic rules area + +# NOTE: +# 1. The $(__NL__)'s are required - don't remove them! +# 2. Every variable reference must be double-escaped in the defines ($VAR -> $$VAR) +# 3. There is one exception to the rule: do not escape $1, $2, etc. and $__NL__ +# 4. Change `eval` to `info` to debug. Also, GMake has a `--dry-run` option + +# "New line" variable +# Don't remove these empty comments! +define __NL__ +# +# +endef + +# Compile a first-party C file into an object file +# Input params: +# $(1) = C file path +define compileObject +$(__NL__) + +$(1:.c=.o) : $(1) + $$(info ) + $$(info $$(decor) Compile C source: $$^ $$(endDecor)) + $$(CC) $$(CFLAGS) $$(INCLUDES) -o $$@ -c $$^ + +$(__NL__) +endef + +# Compile a vendor C file into an object file +# Input params: +# $(1) = C file path +define compileVendorObject +$(__NL__) + +$(1:.c=.o) : $(1) + $$(info ) + $$(info $$(decor) Compile vendor C source: $$^ $$(endDecor)) + $$(CC) $$(CFLAGS) -fvisibility=hidden $$(INCLUDES) -o $$@ -c $$^ + +$(__NL__) +endef + +# Compile liboprf C files +$(foreach var,$(LIBOPRF_SOURCES), \ + $(eval $(call compileObject,$(var) )) \ +) + +# Compile vendor C files +$(foreach var,$(EXTRA_SOURCES), \ + $(eval $(call compileVendorObject,$(var) )) \ +) + +# END dynamic rules section From 90705a23c6894c193e4ec7a9eadcd91684f6bf61 Mon Sep 17 00:00:00 2001 From: flynn162 <54922775+flynn162@users.noreply.github.com> Date: Thu, 3 Jul 2025 22:14:37 -0700 Subject: [PATCH 6/7] makefile: Compile libsodium objects with static linking DEFINES - Pass in `-DSODIUM_STATIC=1 -DSODIUM_EXPORT=""` to hide the libsodium symbols that we vendorized into liboprf - In the previous commit, `-fvisibility=hidden` was over-ruled by SODIUM_EXPORT - Ref: --- src/makefile | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/makefile b/src/makefile index 15dc9c5..bc1f022 100644 --- a/src/makefile +++ b/src/makefile @@ -235,16 +235,17 @@ $(1:.c=.o) : $(1) $(__NL__) endef -# Compile a vendor C file into an object file +# Compile a vendorized libsodium C file into an object file +# Static linking DEFINES can be found at # Input params: # $(1) = C file path -define compileVendorObject +define compileSodiumObject $(__NL__) $(1:.c=.o) : $(1) $$(info ) - $$(info $$(decor) Compile vendor C source: $$^ $$(endDecor)) - $$(CC) $$(CFLAGS) -fvisibility=hidden $$(INCLUDES) -o $$@ -c $$^ + $$(info $$(decor) Compile vendorized libsodium source: $$^ $$(endDecor)) + $$(CC) $$(CFLAGS) -fvisibility=hidden -DSODIUM_STATIC=1 -DSODIUM_EXPORT="" $$(INCLUDES) -o $$@ -c $$^ $(__NL__) endef @@ -256,7 +257,7 @@ $(foreach var,$(LIBOPRF_SOURCES), \ # Compile vendor C files $(foreach var,$(EXTRA_SOURCES), \ - $(eval $(call compileVendorObject,$(var) )) \ + $(eval $(call compileSodiumObject,$(var) )) \ ) # END dynamic rules section From 68e268f7856a511af49778c8c97df87fd1df73fe Mon Sep 17 00:00:00 2001 From: flynn162 <54922775+flynn162@users.noreply.github.com> Date: Fri, 4 Jul 2025 00:15:25 -0700 Subject: [PATCH 7/7] makefile: fix uninstall-oprf rule --- src/makefile | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/makefile b/src/makefile index bc1f022..5ed9e41 100644 --- a/src/makefile +++ b/src/makefile @@ -149,12 +149,16 @@ install-noiseXK: uninstall: uninstall-oprf uninstall-noiseXK -uninstall-oprf: $(DESTDIR)$(PREFIX)/lib/liboprf.$(SOEXT) $(DESTDIR)$(PREFIX)/lib/liboprf.$(STATICEXT) \ - $(DESTDIR)$(PREFIX)/include/oprf/oprf.h $(DESTDIR)$(PREFIX)/include/oprf/toprf.h \ - $(PREFIX)/include/oprf/dkg.h $(DESTDIR)$(PREFIX)/include/oprf/toprf-update.h \ - $(DESTDIR)$(PREFIX)/include/oprf/utils.h - rm $^ - rmdir $(PREFIX)/include/oprf/ +uninstall-oprf: + @ \ + if [ ! '$(strip $(DESTDIR)$(PREFIX))' ]; then echo 'liboprf: DESTDIR-PREFIX is empty!' && exit 1; fi; \ + if [ ! -d '$(strip $(DESTDIR)$(PREFIX))' ]; then echo 'liboprf: DESTDIR-PREFIX is not a folder' && exit 1; fi; + + rm -vf '$(DESTDIR)$(PREFIX)/lib/liboprf.$(SOEXT)' '$(DESTDIR)$(PREFIX)/lib/liboprf.$(STATICEXT)' + rm -vf '$(DESTDIR)$(PREFIX)/include/oprf/oprf.h' '$(DESTDIR)$(PREFIX)/include/oprf/toprf.h' + rm -vf '$(DESTDIR)$(PREFIX)/include/oprf/dkg.h' '$(DESTDIR)$(PREFIX)/include/oprf/toprf-update.h' + rm -vf '$(DESTDIR)$(PREFIX)/include/oprf/utils.h' + rmdir -v '$(DESTDIR)$(PREFIX)/include/oprf/' uninstall-noiseXK: make -C noise_xk uninstall