- Zig 83%
- Nix 17%
zig-build-nettle #
Zig 0.17 build package for GNU Nettle 3.10.2. It builds libnettle and libhogweed directly from the official release tarball as static libraries and exposes both the library artifacts and raw translate-C bindings. Autotools, m4 and a system C compiler are not required.
gmp comes from zig-build-gmp.
Use as a dependency #
Add this package to your project's build.zig.zon, then pass the target and
optimization mode when requesting the dependency:
const nettle = b.dependency("nettle", .{
.target = target,
.optimize = optimize,
});
Import the translated nettle and hogweed API as a Zig module:
exe.root_module.addImport("nettle", nettle.module("nettle"));
The module covers every installed header, including gmp.h, and links
libhogweed, libnettle, gmp and libc transitively. It can be used with
const nettle = @import("nettle");. The short names that the headers define
as macros work as in C (nettle.sha256_init for nettle_sha256_init).
Functions without such a macro keep their full name, for example
nettle.nettle_get_secp_256r1.
Alternatively, link the C library artifacts directly:
exe.root_module.linkLibrary(nettle.artifact("nettle"));
exe.root_module.linkLibrary(nettle.artifact("hogweed"));
nettle propagates the installed nettle/*.h include path. As with an
upstream installation this holds all headers, including the public key ones
such as nettle/rsa.h that include <gmp.h>. hogweed links nettle and
gmp and also propagates gmp.h, so consumers of the public key functions need
no gmp dependency of their own.
Supported targets #
x86_64 and aarch64, each with linux-gnu, linux-musl or macos. The
build stops with an error for any other target. Windows is not supported.
The build corresponds to configuring upstream with --disable-assembler
--disable-fat --disable-shared:
- Only portable C is compiled. There is no assembly and no runtime CPU feature detection, so this is noticeably slower than a distribution build.
- The full default algorithm set is enabled, including all ECC curves (secp192r1 to secp521r1, Curve25519, Curve448 and the GOST curves), Ed25519, Ed448, RSA, DSA, GOST DSA and the GOST hashes and cipher.
- hogweed uses gmp, not the bundled mini-gmp.
- The tools (
nettle-hash,pkcs1-convand so on), the documentation, the benchmarks,nettle.pcandhogweed.pcare not built.
nettle calls functions through pointers of generic type, for example hash
functions that take their context as void *. The library sources are
therefore compiled with -fno-sanitize=function, because Zig's C sanitizer in
safe modes would otherwise trap on these calls.
Build and test #
Zig 0.17.0 is required.
zig build
zig build test
The default build installs libnettle.a, libhogweed.a, libgmp.a, the
nettle headers and gmp.h under zig-out/. The test step runs a Zig smoke
test through the nettle module: a SHA-256 known answer, an AES-128-GCM known
answer and round trip, the RFC 8032 Ed25519 test vector, RSA key generation
with signing and verification, and a check that the non-Suite-B curves
secp192r1 and secp224r1 are present.
The Nix flake provides the pinned Zig toolchain and two checks:
nix develop
nix flake check
checks.defaultruns the build and the test step. Inside the Nix build sandbox Zig skips natively linked test executables, so on Linux it also runs the test step for the static<arch>-linux-musltarget.checks.symbolsbuilds the same tarball with upstream's own configure and Makefiles (--disable-assembler --disable-fat) against nixpkgs' gmp and requires that itslibnettle.aandlibhogweed.aexport exactly the same symbols as the Zig build. It also requires the samenettle/version.h, apart from the line Zig's ConfigHeader adds at the top.
How the build works #
Upstream's configure and Makefiles generate a few files at build time:
config.his produced fromconfig.h.inwith the values configure finds on each supported target. No routine has a native implementation.nettle/version.his produced fromversion.h.infor a build against gmp.- Upstream's
eccdatais built for the build machine with the bundled mini-gmp and produces theecc-*.hcurve tables, with the window parameters fromMakefile.inand 64-bit limbs, which holds for every supported target. - translate-C reads a generated umbrella header that includes every installed header.
nettle 3.10.2 no longer has a nettle-stdint.h, and the DES tables
rotors.h and keymap.h ship pregenerated in the tarball.
Upstream source #
- Version:
3.10.2 - Archive: https://mirrors.kernel.org/gnu/nettle/nettle-3.10.2.tar.gz (the GNU mirror of https://ftp.gnu.org/gnu/nettle/nettle-3.10.2.tar.gz)
- SHA-256:
fe9ff51cb1f2abb5e65a6b8c10a92da0ab5ab6eaf26e7fc2b675c45f1fb519b5
The archive is pinned by its Zig package hash in build.zig.zon.
License #
Like GNU Nettle itself, this package is licensed under the GNU Lesser General
Public License, version 3 or later, or the GNU General Public License, version
2 or later, or both in parallel. See LICENSE.