Skip to content

feat(crypto): make libsodium a build-selectable optional backend (#102)#103

Merged
igorls merged 2 commits into
mainfrom
feat/optional-libsodium
Jun 7, 2026
Merged

feat(crypto): make libsodium a build-selectable optional backend (#102)#103
igorls merged 2 commits into
mainfrom
feat/optional-libsodium

Conversation

@igorls
Copy link
Copy Markdown
Owner

@igorls igorls commented Jun 7, 2026

Summary

Makes -Dno-sodium / -Dcrypto-backend actually reach the source, so MeshGuard can build with std.crypto and no libsodium — addresses #102.

Previously the build option only controlled linkage: tunnel.zig and main.zig chose the AEAD backend from builtin.os.tag alone, so -Dno-sodium=true on Linux skipped linking libsodium yet still compiled the sodium path → unresolved symbols at link time. The flag was effectively a no-op (or broke the build) on Linux.

Changes

  • Add a build_options module exposing use_libsodium, resolved in build.zig from a new -Dcrypto-backend=auto|std|sodium (with -Dno-sodium kept as a std alias), and thread it into every module (ffi, exe, lib, interop, test).
  • tunnel.zig and main.zig now read build_options.use_libsodium instead of builtin.os.tag, so source and linker always agree. Linkage collapses to a single if (use_libsodium).
  • auto preserves prior behaviour: libsodium on Linux desktop (non-Android), std.crypto everywhere else.

Verification

  • Builds across backends on macOS: default / -Dno-sodium=true / -Dcrypto-backend=std.
  • zig build -Dtarget=x86_64-linux-gnu -Dno-sodium=true → builds cleanly with std.crypto, no libsodium (the acceptance criterion that previously failed at link time).
  • Sodium path still compiles and runs: zig build test -Dcrypto-backend=sodium (linking system libsodium) → 40/40 tests pass, including the tunnel encrypt/decrypt roundtrip through libsodium.

Acceptance criteria (#102)

  • Build/test with std.crypto on Linux without libsodium installed
  • Still build with libsodium acceleration when requested/available
  • Downstream packages can force the no-sodium path without patching MeshGuard source (wormdb's build.zig passes use_libsodium through to the embedded module)
  • Benchmarks compare libsodium vs std.crypto (run downstream; libsodium ~2× MTU / ~3× bulk on x86_64 AVX2, ~1.2× ARM, slightly slower at gossip sizes)
  • CI: a no-sodium Linux build/test job (follow-up)
  • Docs: a dedicated section describing libsodium as optional acceleration (this PR adds the build-option help text + comments)

libsodium was meant to be an optional accelerator, but the build option never
reached the source: tunnel.zig and main.zig chose the AEAD backend from
builtin.os.tag alone, so -Dno-sodium skipped linkage yet still compiled the
sodium path, leaving unresolved symbols on Linux.

Add a build_options module carrying use_libsodium (resolved in build.zig from
-Dcrypto-backend=auto|std|sodium, with -Dno-sodium kept as a std alias) and
thread it into every module. tunnel.zig and main.zig now read
build_options.use_libsodium, so source and linker always agree; linkage
collapses to a single `if (use_libsodium)`.

auto preserves the prior default (libsodium on Linux desktop, std.crypto
elsewhere). -Dno-sodium / -Dcrypto-backend=std builds a pure std.crypto binary
on Linux x86_64 with no libsodium dependency (verified end-to-end).
Copilot AI review requested due to automatic review settings June 7, 2026 23:23
Copy link
Copy Markdown

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 makes libsodium a truly optional crypto backend by threading the build-selected backend choice into Zig source via a generated build_options module, ensuring compile-time backend selection matches linker configuration (fixing the Linux -Dno-sodium link-failure described in #102).

Changes:

  • Adds -Dcrypto-backend=auto|std|sodium (with -Dno-sodium kept as an alias) and derives a single use_libsodium boolean in build.zig.
  • Passes use_libsodium into source as @import("build_options").use_libsodium, updating backend selection in tunnel.zig and libsodium init gating in main.zig.
  • Collapses libsodium linkage to if (use_libsodium) across build targets/modules so source and linkage agree.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/wireguard/tunnel.zig Switches AEAD backend selection to build_options.use_libsodium so codegen aligns with build config.
src/main.zig Gates libsodium initialization on build_options.use_libsodium rather than OS-only detection.
build.zig Introduces crypto-backend selection, generates build_options, imports it into modules, and links libsodium only when selected.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread build.zig
Comment on lines +32 to +36
const use_libsodium = if (no_sodium) false else switch (crypto_backend) {
.std => false,
.sodium => true,
.auto => auto_libsodium,
};
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Good catch — fixed in c843ff4. The build now fails fast with a clear message when -Dno-sodium=true is combined with -Dcrypto-backend=sodium (only that pairing; -Dno-sodium with std/auto is still accepted as a no-op/alias).

@igorls igorls merged commit 39c0f9b into main Jun 7, 2026
4 checks passed
@igorls igorls deleted the feat/optional-libsodium branch June 7, 2026 23:32
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.

2 participants