Files
crypto/hqc/randombytes_shim.c
Antje WorringandHanzo Dev 0fe370c77c hqc: decouple cgo build from native HQC lib (bump accel v1.2.4); fix HQC PQClean CI
Root cause of the never-passing 'HQC PQClean cgo e2e (NIST KAT
roundtrip)' job, in two layers:

1. The lux-build runner had no C compiler (gcc/cc/clang absent), so cgo
   aborted before any code ran. Fixed in prior commits by installing
   build-essential + selecting an available CC.

2. With a compiler present, the cgo build then failed on
   accel@v1.1.9/ops/code/code_cpu.go's '#include "lux/gpu/hqc.h"' and
   '-lluxgpu_hqc' — the native HQC library (built from luxfi/mlx) that
   is NOT present in CI (nor in most consumers). accel v1.1.9 gated that
   native path on '//go:build cgo', so every cgo build hard-required the
   native lib/header.

Fix: bump github.com/luxfi/accel v1.1.9 -> v1.2.4, which gates the
native path behind '-tags=lux_hqc_native' and otherwise returns
code.ErrNativeHQCUnavailable (documented: 'callers should fall back to
their own CPU KEM path'). crypto/hqc now honours that contract:

- gpu.go: batchEncapsulateGPU/batchDecapsulateGPU treat
  ErrNativeHQCUnavailable as 'declined' (return false,nil) so callers
  fall back to the per-item PQClean path. Secret buffers are zeroised on
  the decline path.
- gpu_cgo.go: GF2Polymul falls back to the pure-Go Karatsuba path on
  ErrNativeHQCUnavailable. Removed the duplicated vecNSize64 /
  redMaskForMode / GF2Add (now shared).
- gpu_polymul_purego.go (new, tag-neutral): the canonical pure-Go,
  PQClean-byte-identical GF(2)^N polymul (Karatsuba + reduce + base_mul),
  extracted from gpu_nocgo.go so BOTH builds share one implementation.
- gpu_nocgo.go: trimmed to the thin !cgo public wrappers over the shared
  helpers.
- randombytes_shim.c: split. Default (hqc_pqclean, no native) forwards
  randombytes -> hqcGoRandombytes only; the native seed-buffer dispatch
  (which references lux_hqc_randombytes / lux_hqc_seed_has_bytes from the
  native lib) moves to randombytes_shim_native.c, gated on lux_hqc_native.
  Previously the default build referenced those native-only symbols
  unconditionally, which would not link without the native lib.

NOT a crypto/KAT regression: the NIST KAT roundtrip (TestKAT_HQC128/192/
256), TestRoundTrip_AllModes, TestDeterminism, and the GF2 polymul
known-answer vectors all pass byte-for-byte. The GPU batch tests skip
gracefully when the native kernel is absent (their existing t.Skip path).

Verified: CGO_ENABLED=1 go test -tags=hqc_pqclean ./hqc/ passes from a
clean cache; cgo (with/without hqc_pqclean) and nocgo builds all green;
broader suite unchanged (only pre-existing pkg-config failures in cgo/
and hash/poseidon2 remain, unrelated).

Co-authored-by: Hanzo Dev <dev@hanzo.ai>
2026-06-07 00:19:52 -07:00

34 lines
1.4 KiB
C

//go:build hqc_pqclean && !lux_hqc_native
/*
* randombytes_shim.c — PQClean randombytes() bridge (non-native build).
*
* PQClean's HQC reference calls `randombytes(out, n)` for every byte
* of entropy it needs. In the default (non-native) build there is no
* luxgpu_hqc static library linked in, so the only entropy source is
* the Go-side io.Reader, reached via the cgo-exported hqcGoRandombytes
* callback (backend_pqclean.go's installRNG + lock).
*
* The native batch path (C++ host kernel installing a per-slot TLS
* seed buffer, served by lux_hqc_randombytes) is compiled only under
* the `lux_hqc_native` build tag — see randombytes_shim_native.c.
* Referencing those symbols here would leave them undefined at link
* time whenever the native library is absent (which is the common
* case: CI, pure-Go deployments). So this default TU forwards
* unconditionally to Go.
*
* Determinism contract: every call consumes exactly `n` bytes from
* the Go reader, in call order. Two KeyGen/Encapsulate invocations
* seeded from byte-identical streams produce byte-identical outputs.
* This is load-bearing for the HQC precompile.
*/
#include <stdint.h>
#include <stddef.h>
/* Forward declaration of the cgo-exported Go callback. */
extern int hqcGoRandombytes(uint8_t *buf, size_t n);
int randombytes(uint8_t *output, size_t n) {
return hqcGoRandombytes(output, n);
}