libertaria-stack/build.zig

380 lines
14 KiB
Zig

const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// ========================================================================
// L0: Transport Layer
// ========================================================================
const l0_mod = b.createModule(.{
.root_source_file = b.path("l0-transport/lwf.zig"),
.target = target,
.optimize = optimize,
});
const utcp_mod = b.createModule(.{
.root_source_file = b.path("l0-transport/utcp/socket.zig"),
.target = target,
.optimize = optimize,
});
utcp_mod.addImport("lwf", l0_mod);
const opq_mod = b.createModule(.{
.root_source_file = b.path("l0-transport/opq.zig"),
.target = target,
.optimize = optimize,
});
opq_mod.addImport("lwf", l0_mod);
const l0_service_mod = b.createModule(.{
.root_source_file = b.path("l0-transport/service.zig"),
.target = target,
.optimize = optimize,
});
l0_service_mod.addImport("lwf", l0_mod);
l0_service_mod.addImport("utcp", utcp_mod);
l0_service_mod.addImport("opq", opq_mod);
// ========================================================================
// Crypto: SHA3/SHAKE & FIPS 202
// ========================================================================
const crypto_shake_mod = b.createModule(.{
.root_source_file = b.path("src/crypto/shake.zig"),
.target = target,
.optimize = optimize,
});
const crypto_fips202_mod = b.createModule(.{
.root_source_file = b.path("src/crypto/fips202_bridge.zig"),
.target = target,
.optimize = optimize,
});
const crypto_exports_mod = b.createModule(.{
.root_source_file = b.path("src/crypto/exports.zig"),
.target = target,
.optimize = optimize,
});
crypto_exports_mod.addImport("fips202_bridge", crypto_fips202_mod);
// ========================================================================
// L1: Identity & Crypto Layer
// ========================================================================
const l1_mod = b.createModule(.{
.root_source_file = b.path("l1-identity/crypto.zig"),
.target = target,
.optimize = optimize,
});
// Add crypto modules as imports to L1
l1_mod.addImport("shake", crypto_shake_mod);
l1_mod.addImport("fips202_bridge", crypto_fips202_mod);
// ========================================================================
// L1 PQXDH Module (Phase 3) - Core Dependency
// ========================================================================
const l1_pqxdh_mod = b.createModule(.{
.root_source_file = b.path("l1-identity/pqxdh.zig"),
.target = target,
.optimize = optimize,
});
l1_pqxdh_mod.addIncludePath(b.path("vendor/liboqs/install/include"));
l1_pqxdh_mod.addLibraryPath(b.path("vendor/liboqs/install/lib"));
l1_pqxdh_mod.linkSystemLibrary("oqs", .{ .needed = true });
// Ensure l1_mod uses PQXDH
l1_mod.addImport("pqxdh", l1_pqxdh_mod);
// ========================================================================
// L1 Modules: SoulKey, Entropy, Prekey (Phase 2B + 2C)
// ========================================================================
const l1_soulkey_mod = b.createModule(.{
.root_source_file = b.path("l1-identity/soulkey.zig"),
.target = target,
.optimize = optimize,
});
// SoulKey needs PQXDH for deterministic generation
l1_soulkey_mod.addImport("pqxdh", l1_pqxdh_mod);
const l1_entropy_mod = b.createModule(.{
.root_source_file = b.path("l1-identity/entropy.zig"),
.target = target,
.optimize = optimize,
});
// UTCP needs entropy for fast validation
utcp_mod.addImport("entropy", l1_entropy_mod);
const l1_prekey_mod = b.createModule(.{
.root_source_file = b.path("l1-identity/prekey.zig"),
.target = target,
.optimize = optimize,
});
l1_prekey_mod.addImport("pqxdh", l1_pqxdh_mod);
const l1_did_mod = b.createModule(.{
.root_source_file = b.path("l1-identity/did.zig"),
.target = target,
.optimize = optimize,
});
l1_did_mod.addImport("pqxdh", l1_pqxdh_mod);
// ========================================================================
// L1 QVL (Quasar Vector Lattice) - Advanced Graph Engine
// ========================================================================
const l1_qvl_mod = b.createModule(.{
.root_source_file = b.path("l1-identity/qvl.zig"),
.target = target,
.optimize = optimize,
});
// ========================================================================
// Tests (with C FFI support for Argon2 + liboqs)
// ========================================================================
// Crypto tests (SHA3/SHAKE)
const crypto_tests = b.addTest(.{
.root_module = crypto_shake_mod,
});
const run_crypto_tests = b.addRunArtifact(crypto_tests);
// Crypto FFI bridge tests
const crypto_ffi_tests = b.addTest(.{
.root_module = crypto_fips202_mod,
});
const run_crypto_ffi_tests = b.addRunArtifact(crypto_ffi_tests);
// L0 tests
const l0_tests = b.addTest(.{
.root_module = l0_mod,
});
const run_l0_tests = b.addRunArtifact(l0_tests);
// UTCP tests
const utcp_tests = b.addTest(.{
.root_module = utcp_mod,
});
const run_utcp_tests = b.addRunArtifact(utcp_tests);
// OPQ tests
const opq_tests = b.addTest(.{
.root_module = opq_mod,
});
const run_opq_tests = b.addRunArtifact(opq_tests);
// L0 Service tests
const l0_service_tests = b.addTest(.{
.root_module = l0_service_mod,
});
const run_l0_service_tests = b.addRunArtifact(l0_service_tests);
// L1 SoulKey tests (Phase 2B)
const l1_soulkey_tests = b.addTest(.{
.root_module = l1_soulkey_mod,
});
// Tests linking liboqs effectively happen via the module now, but we also link LibC
l1_soulkey_tests.linkLibC();
const run_l1_soulkey_tests = b.addRunArtifact(l1_soulkey_tests);
// L1 Entropy tests (Phase 2B)
const l1_entropy_tests = b.addTest(.{
.root_module = l1_entropy_mod,
});
l1_entropy_tests.addCSourceFiles(.{
.files = &.{
"vendor/argon2/src/argon2.c",
"vendor/argon2/src/core.c",
"vendor/argon2/src/blake2/blake2b.c",
"vendor/argon2/src/thread.c",
"vendor/argon2/src/encoding.c",
"vendor/argon2/src/opt.c",
},
.flags = &.{
"-std=c99",
"-O3",
"-fPIC",
"-DHAVE_PTHREAD",
},
});
l1_entropy_tests.addIncludePath(b.path("vendor/argon2/include"));
l1_entropy_tests.linkLibC();
const run_l1_entropy_tests = b.addRunArtifact(l1_entropy_tests);
// L1 Prekey tests (Phase 2C)
const l1_prekey_tests = b.addTest(.{
.root_module = l1_prekey_mod,
});
l1_prekey_tests.linkLibC();
const run_l1_prekey_tests = b.addRunArtifact(l1_prekey_tests);
// L1 DID tests (Phase 2D)
const l1_did_tests = b.addTest(.{
.root_module = l1_did_mod,
});
l1_did_tests.linkLibC();
const run_l1_did_tests = b.addRunArtifact(l1_did_tests);
// Import PQXDH into main L1 module
// Tests (root is test_pqxdh.zig)
const l1_pqxdh_tests_mod = b.createModule(.{
.root_source_file = b.path("l1-identity/test_pqxdh.zig"),
.target = target,
.optimize = optimize,
});
// Tests import the library module 'pqxdh' (relative import works too, but module is cleaner if we use @import("pqxdh"))
// But test_pqxdh.zig uses @import("pqxdh.zig") which is relative file import.
// If we use relative import, the test module must be able to resolve pqxdh.zig.
// Since they are in same dir, relative import works.
// BUT the artifact compiled from test_pqxdh.zig needs to link liboqs because it effectively includes pqxdh.zig code.
const l1_pqxdh_tests = b.addTest(.{
.root_module = l1_pqxdh_tests_mod,
});
l1_pqxdh_tests.linkLibC();
l1_pqxdh_tests.addIncludePath(b.path("vendor/liboqs/install/include"));
l1_pqxdh_tests.addLibraryPath(b.path("vendor/liboqs/install/lib"));
l1_pqxdh_tests.linkSystemLibrary("oqs");
const run_l1_pqxdh_tests = b.addRunArtifact(l1_pqxdh_tests);
// Link time module to l1_vector_mod
// ========================================================================
// Time Module (L0)
// ========================================================================
const time_mod = b.createModule(.{
.root_source_file = b.path("l0-transport/time.zig"),
.target = target,
.optimize = optimize,
});
// L1 Vector tests (Phase 3C)
const l1_vector_mod = b.createModule(.{
.root_source_file = b.path("l1-identity/vector.zig"),
.target = target,
.optimize = optimize,
});
l1_vector_mod.addImport("time", time_mod);
l1_vector_mod.addImport("pqxdh", l1_pqxdh_mod);
// QVL also needs time (via proof_of_path.zig dependency)
l1_qvl_mod.addImport("time", time_mod);
// QVL FFI (C ABI exports for L2 integration)
const l1_qvl_ffi_mod = b.createModule(.{
.root_source_file = b.path("l1-identity/qvl_ffi.zig"),
.target = target,
.optimize = optimize,
});
l1_qvl_ffi_mod.addImport("qvl", l1_qvl_mod);
l1_qvl_ffi_mod.addImport("time", time_mod);
const l1_vector_tests = b.addTest(.{
.root_module = l1_vector_mod,
});
// Add Argon2 support for vector tests (via entropy.zig)
l1_vector_tests.addCSourceFiles(.{
.files = &.{
"vendor/argon2/src/argon2.c",
"vendor/argon2/src/core.c",
"vendor/argon2/src/blake2/blake2b.c",
"vendor/argon2/src/thread.c",
"vendor/argon2/src/encoding.c",
"vendor/argon2/src/opt.c",
},
.flags = &.{
"-std=c99",
"-O3",
"-fPIC",
"-DHAVE_PTHREAD",
},
});
l1_vector_tests.addIncludePath(b.path("vendor/argon2/include"));
l1_vector_tests.linkLibC();
const run_l1_vector_tests = b.addRunArtifact(l1_vector_tests);
// NOTE: Phase 3 PQXDH uses ML-KEM-768 via liboqs (integrated).
// Test step (runs Phase 2B + 2C + 2D + 3C SDK tests)
const test_step = b.step("test", "Run SDK tests");
test_step.dependOn(&run_crypto_tests.step);
test_step.dependOn(&run_crypto_ffi_tests.step);
test_step.dependOn(&run_l0_tests.step);
test_step.dependOn(&run_l1_soulkey_tests.step);
test_step.dependOn(&run_l1_entropy_tests.step);
test_step.dependOn(&run_l1_prekey_tests.step);
test_step.dependOn(&run_l1_did_tests.step);
test_step.dependOn(&run_l1_vector_tests.step);
test_step.dependOn(&run_l1_pqxdh_tests.step);
test_step.dependOn(&run_utcp_tests.step);
test_step.dependOn(&run_opq_tests.step);
test_step.dependOn(&run_l0_service_tests.step);
// L1 QVL tests
const l1_qvl_tests = b.addTest(.{
.root_module = l1_qvl_mod,
});
const run_l1_qvl_tests = b.addRunArtifact(l1_qvl_tests);
test_step.dependOn(&run_l1_qvl_tests.step);
// L1 QVL FFI tests (C ABI validation)
const l1_qvl_ffi_tests = b.addTest(.{
.root_module = l1_qvl_ffi_mod,
});
l1_qvl_ffi_tests.linkLibC(); // Required for C allocator
const run_l1_qvl_ffi_tests = b.addRunArtifact(l1_qvl_ffi_tests);
test_step.dependOn(&run_l1_qvl_ffi_tests.step);
// NOTE: C test harness (test_qvl_ffi.c) can be compiled manually:
// zig cc -I. l1-identity/test_qvl_ffi.c zig-out/lib/libqvl_ffi.a -o test_qvl_ffi
// ========================================================================
// Examples
// ========================================================================
// Example: LWF frame usage
const lwf_example_mod = b.createModule(.{
.root_source_file = b.path("examples/lwf_example.zig"),
.target = target,
.optimize = optimize,
});
lwf_example_mod.addImport("../l0-transport/lwf.zig", l0_mod);
const lwf_example = b.addExecutable(.{
.name = "lwf_example",
.root_module = lwf_example_mod,
});
b.installArtifact(lwf_example);
// Example: Encryption usage
const crypto_example_mod = b.createModule(.{
.root_source_file = b.path("examples/crypto_example.zig"),
.target = target,
.optimize = optimize,
});
crypto_example_mod.addImport("../l1-identity/crypto.zig", l1_mod);
const crypto_example = b.addExecutable(.{
.name = "crypto_example",
.root_module = crypto_example_mod,
});
b.installArtifact(crypto_example);
// Examples step
const examples_step = b.step("examples", "Build example programs");
examples_step.dependOn(&b.addInstallArtifact(lwf_example, .{}).step);
examples_step.dependOn(&b.addInstallArtifact(crypto_example, .{}).step);
// ========================================================================
// Convenience Commands
// ========================================================================
// Run LWF example
const run_lwf_example = b.addRunArtifact(lwf_example);
const run_lwf_step = b.step("run-lwf", "Run LWF frame example");
run_lwf_step.dependOn(&run_lwf_example.step);
// Run crypto example
const run_crypto_example = b.addRunArtifact(crypto_example);
const run_crypto_step = b.step("run-crypto", "Run encryption example");
run_crypto_step.dependOn(&run_crypto_example.step);
}