Files
zeekay cc84032371 hqc: wire real PQClean cgo backend, NIST KATs pass byte-for-byte
Replaces the stub backend (ErrBackendNotWired) with a working byte-
for-byte NIST-compatible HQC implementation. Build tag hqc_pqclean
activates the cgo path; default builds remain stub for portability.

Vendored:
  - PQClean HQC-128/192/256 clean reference C (2023-04-30 NIST
    submission), public domain
  - PQClean shared fips202 (SHA-3 / SHAKE)
  - Per-mode/per-source shim TUs (36 total) to keep PQClean's
    static helpers isolated and avoid duplicate-symbol collisions
  - randombytes_shim.c wiring PQClean's randombytes() to a Go-side
    io.Reader callback, mutex-protected for concurrent dispatch

Tests:
  - TestKAT_HQC128/192/256 — all three NIST KAT vectors verified
    byte-for-byte against META.yml nistkat-sha256
  - TestRoundTrip_AllModes — keygen → encap → decap roundtrip,
    shared secrets byte-equal in all 3 modes
  - TestDeterminism_AllModes — same seed produces identical ct/ss
    (load-bearing for the precompile's on-chain determinism)
  - -race clean

Correction: ciphertext sizes updated from 4481/9026/14469 (older
HQC round-3 listing) to PQClean's 4433/8978/14421 (2023-04-30 NIST
submission); the prior numbers would have failed round-trip with
a 48-byte mismatch.
2026-05-15 17:43:48 -07:00

57 lines
1.7 KiB
Go

// Copyright (C) 2026, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package hqc
import (
"errors"
"testing"
)
// TestParams_FixedSizes pins the NIST IR 8528 §4.1 wire-format sizes
// for HQC-128/192/256. These constants are part of the public API and
// must not drift across backend wirings.
func TestParams_FixedSizes(t *testing.T) {
cases := []struct {
mode Mode
pk, sk int
ct, ss int
secBits int
}{
// Sizes match the vendored PQClean reference
// (hqc-submission_2023-04-30): see
// crypto/hqc/pqclean/hqc-N/clean/api.h.
{HQC128, 2249, 2305, 4433, 64, 128},
{HQC192, 4522, 4586, 8978, 64, 192},
{HQC256, 7245, 7317, 14421, 64, 256},
}
for _, c := range cases {
p := MustParamsFor(c.mode)
if p.PublicKeySize != c.pk {
t.Errorf("HQC%d PublicKeySize=%d, want %d", c.secBits, p.PublicKeySize, c.pk)
}
if p.PrivateKeySize != c.sk {
t.Errorf("HQC%d PrivateKeySize=%d, want %d", c.secBits, p.PrivateKeySize, c.sk)
}
if p.CiphertextSize != c.ct {
t.Errorf("HQC%d CiphertextSize=%d, want %d", c.secBits, p.CiphertextSize, c.ct)
}
if p.SharedSecretSize != c.ss {
t.Errorf("HQC%d SharedSecretSize=%d, want %d", c.secBits, p.SharedSecretSize, c.ss)
}
}
}
// TestDecapsulate_ModeMismatch — a ciphertext from one parameter set
// fed to a private key for another must return ErrModeMismatch, not
// silently produce a bogus shared secret.
func TestDecapsulate_ModeMismatch(t *testing.T) {
_, err := Decapsulate(
&PrivateKey{Mode: HQC128, Bytes: make([]byte, 2305)},
&Ciphertext{Mode: HQC256, Bytes: make([]byte, 14421)},
)
if !errors.Is(err, ErrModeMismatch) {
t.Errorf("mode mismatch: want ErrModeMismatch, got %v", err)
}
}