Files
ANSCORE/tests/ANSModelPackage-UnitTest/ANSModelPackage-UnitTest.cpp
T

482 lines
20 KiB
C++
Raw Normal View History

// ANSModelPackage-UnitTest — the model package format, and its agreement with
// the other implementation.
//
// Usage: ANSModelPackage-UnitTest [data-dir]
//
// The interop fixture in data/ was written by ANSMINDCORE's packer. Opening it
// here is what makes "the two products read the same format" a fact rather
// than an intention: a change to either implementation that alters the wire
// shows up as this test failing.
#include <algorithm>
#include <chrono>
#include <cstdio>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <string>
#include <vector>
#include "ANSModelOpen.h"
#include "ANSOnnxShape.h"
#include "ANSModelPackage.h"
extern "C" {
#include "monocypher-ed25519.h"
#include "monocypher.h"
}
namespace fs = std::filesystem;
using namespace ANSCENTER;
using modelpkg::Json;
static int gChecks = 0, gFailures = 0;
static void CHECK(bool ok, const char* what) {
++gChecks;
if (!ok) {
++gFailures;
std::printf("FAIL %s\n", what);
}
}
// The fixture's keys, by the rules its generator recorded (see data/README).
struct Identity {
modelpkg::Key basePriv{}, basePub{};
std::vector<uint8_t> rootSk = std::vector<uint8_t>(64);
std::vector<uint8_t> rootPk = std::vector<uint8_t>(32);
std::vector<uint8_t> signerSk = std::vector<uint8_t>(64);
std::vector<uint8_t> signerPk = std::vector<uint8_t>(32);
};
static Identity fixtureIdentity() {
Identity id;
std::vector<uint8_t> seedRoot(32), seedSigner(32);
for (size_t i = 0; i < 32; ++i) {
id.basePriv[i] = uint8_t(i * 3 + 9);
seedRoot[i] = uint8_t(i * 7 + 1);
seedSigner[i] = uint8_t(i * 11 + 5);
}
crypto_x25519_public_key(id.basePub.data(), id.basePriv.data());
crypto_ed25519_key_pair(id.rootSk.data(), id.rootPk.data(), seedRoot.data());
crypto_ed25519_key_pair(id.signerSk.data(), id.signerPk.data(), seedSigner.data());
return id;
}
static modelpkg::Keyring fixtureKeyring(const Identity& id) {
modelpkg::Keyring ring;
ring.addX25519("ansk-v1-base", id.basePriv);
modelpkg::Key rootPub{};
std::memcpy(rootPub.data(), id.rootPk.data(), rootPub.size());
ring.addRoot("ansroot-v1", rootPub);
ring.nowSec = 1788000000; // a fixed "now", so the fixture does not age
return ring;
}
// ── the primitives, against their own published numbers ───────────────────
static void testVectors() {
auto hex = [](const std::string& h) {
std::vector<uint8_t> v;
for (size_t i = 0; i + 1 < h.size(); i += 2)
v.push_back(uint8_t(std::stoi(h.substr(i, 2), nullptr, 16)));
return v;
};
const std::string text =
"Ladies and Gentlemen of the class of '99: If I could offer you only one "
"tip for the future, sunscreen would be it.";
const auto key = hex("808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f");
const auto aad = hex("50515253c0c1c2c3c4c5c6c7");
const auto nonce = hex("070000004041424344454647");
const std::vector<uint8_t> plain(text.begin(), text.end());
std::vector<uint8_t> ct(plain.size());
uint8_t tag[16];
aead::aeadSeal(key.data(), nonce.data(), aad.data(), aad.size(), plain.data(),
plain.size(), ct.data(), tag);
CHECK(std::memcmp(tag, hex("1ae10b594f09e26a7e902ecbd0600691").data(), 16) == 0,
"RFC 8439 §2.8.2: the AEAD tag is the published one");
const auto aSec = hex("77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a");
const auto bSec = hex("5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb");
std::vector<uint8_t> aPub(32), bPub(32), s1(32), s2(32);
crypto_x25519_public_key(aPub.data(), aSec.data());
crypto_x25519_public_key(bPub.data(), bSec.data());
crypto_x25519(s1.data(), aSec.data(), bPub.data());
crypto_x25519(s2.data(), bSec.data(), aPub.data());
CHECK(s1 == hex("4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742") &&
s1 == s2,
"RFC 7748: X25519 reaches the published shared secret");
auto seed = hex("4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb");
const auto msg = hex("72");
std::vector<uint8_t> sk(64), pk(32), sig(64);
crypto_ed25519_key_pair(sk.data(), pk.data(), seed.data());
crypto_ed25519_sign(sig.data(), sk.data(), msg.data(), msg.size());
CHECK(sig == hex("92a009a9f0d4cab8720e820b5f642540a2b27b5416503f8fb3762223ebdb69da"
"085ac1e43e15996e458f3613d0f11d8c387b2eaeb4302aeeb00d291612bb0c00"),
"RFC 8032 test 2: the signature is the published one, so this is "
"SHA-512 Ed25519 rather than Monocypher's BLAKE2b EdDSA");
}
// ── the package ANSMINDCORE wrote ─────────────────────────────────────────
static void testInterop(const fs::path& dataDir) {
const std::string path = (dataDir / "interop.ansmodel").string();
if (!fs::exists(path)) {
std::printf("SKIP interop: %s is not here\n", path.c_str());
return;
}
const Identity id = fixtureIdentity();
const modelpkg::Keyring ring = fixtureKeyring(id);
modelpkg::Package pkg;
std::string err;
CHECK(modelpkg::open(path, ring, pkg, err),
("a package written by ANSMINDCORE opens here: " + err).c_str());
if (!err.empty()) return;
CHECK(pkg.signerId == "anssig-2026-09", "signed by the release key it names");
CHECK(!pkg.certExpired, "with a certificate still in date");
CHECK(pkg.manifest.value("project", std::string()) == "Interop",
"the manifest decrypts to what was packed");
CHECK(pkg.manifest.value("family", std::string()) == "yolo", "including the family");
CHECK(pkg.manifest.value("classes", Json::array()).size() == 2, "and its classes");
CHECK(pkg.entryPayloadId() == "p0", "and names the model to run");
std::vector<uint8_t> got;
CHECK(modelpkg::readPayload(pkg, "p0", got, err), ("read: " + err).c_str());
std::vector<uint8_t> want(12000);
for (size_t i = 0; i < want.size(); ++i) want[i] = uint8_t((i * 31 + 21) & 0xff);
CHECK(got == want, "and the model comes back byte for byte, across chunks");
}
// ── this implementation on its own ────────────────────────────────────────
static std::vector<uint8_t> fakeModel(size_t n, uint8_t seed) {
std::vector<uint8_t> v(n);
for (size_t i = 0; i < n; ++i) v[i] = uint8_t((i * 31 + seed) & 0xff);
return v;
}
static void testRoundTripAndRefusals(const fs::path& dir) {
const Identity id = fixtureIdentity();
const modelpkg::Keyring ring = fixtureKeyring(id);
modelpkg::PackRequest req;
req.keyId = "ansk-v1-base";
req.useX25519 = true;
req.basePublic = id.basePub;
req.chunk = 2048;
req.nonceSeed = 9;
req.signerId = "anssig-2026-09";
req.signerSecret = id.signerSk;
req.signerPublic = id.signerPk;
req.rootKeyId = "ansroot-v1";
req.notAfter = "2030-01-01T00:00:00Z";
{
Json cert{{"keyId", req.signerId},
{"pub", bits::base64Encode(
std::string(id.signerPk.begin(), id.signerPk.end()))},
{"notAfter", req.notAfter}};
const std::string cb = modelpkg::detail::certBytes(cert);
req.rootSig.assign(64, 0);
crypto_ed25519_sign(req.rootSig.data(), id.rootSk.data(),
reinterpret_cast<const uint8_t*>(cb.data()), cb.size());
}
req.manifest = Json{{"task", "segmentation"},
{"family", "rfdetr"},
{"runtime", "onnx"},
{"classes", Json::array({Json{{"id", 1}, {"name", "Road"}}})},
{"models", Json::array({Json{{"id", "m0"},
{"payload", "p0"},
{"entry", true}}})}};
const auto model = fakeModel(7000, 3);
req.models.push_back({"p0", model});
const std::string path = (dir / "core.ansmodel").string();
std::string err;
CHECK(modelpkg::pack(path, req, err), ("pack: " + err).c_str());
modelpkg::Package pkg;
CHECK(modelpkg::open(path, ring, pkg, err), ("open: " + err).c_str());
std::vector<uint8_t> got;
CHECK(modelpkg::readPayload(pkg, "p0", got, err) && got == model,
"what this implementation writes, it reads back");
auto bytes = [&](const std::string& p) {
std::ifstream f(p, std::ios::binary);
return std::string((std::istreambuf_iterator<char>(f)),
std::istreambuf_iterator<char>());
};
auto write = [&](const std::string& p, const std::string& b) {
std::ofstream f(p, std::ios::binary | std::ios::trunc);
f.write(b.data(), std::streamsize(b.size()));
};
const std::string good = bytes(path);
{ // a flipped bit in the model body
std::string bad = good;
bad[bad.size() - 30] = char(bad[bad.size() - 30] ^ 0x01);
const std::string p = (dir / "flipped.ansmodel").string();
write(p, bad);
modelpkg::Package t;
std::vector<uint8_t> out;
CHECK(modelpkg::open(p, ring, t, err) &&
!modelpkg::readPayload(t, "p0", out, err) && out.empty(),
"a flipped bit fails its chunk and yields no partial model");
}
{ // an unknown root
modelpkg::Keyring stranger;
stranger.addX25519("ansk-v1-base", id.basePriv);
stranger.addRoot("ansroot-other", modelpkg::Key{});
modelpkg::Package t;
CHECK(!modelpkg::open(path, stranger, t, err) &&
err.find("unknown root") != std::string::npos,
"an unknown root is refused");
}
{ // a revoked signer
modelpkg::Keyring revoking = ring;
revoking.revokedSigners.push_back("anssig-2026-09");
modelpkg::Package t;
CHECK(!modelpkg::open(path, revoking, t, err) &&
err.find("revoked") != std::string::npos,
"a revoked signing key is refused");
}
{ // not a package
const std::string p = (dir / "rubbish.bin").string();
write(p, "nothing to see here");
modelpkg::Package t;
CHECK(!modelpkg::open(p, ring, t, err) &&
err.find("not a model package") != std::string::npos,
"something else entirely is refused by its magic");
}
}
// ── the glue the engines load through ─────────────────────────────────────
//
// ANSODBase asks these three questions and nothing else: is this path a
// package, can this build open one, and what came out of it. The engines
// themselves need CUDA to compile, so this is where that contract is checked.
static void testOpenForAnEngine(const fs::path& dataDir) {
const std::string path = (dataDir / "interop.ansmodel").string();
if (!fs::exists(path)) {
std::printf("SKIP engine glue: %s is not here\n", path.c_str());
return;
}
CHECK(modelpkg::IsPackagePath(path), "a .ansmodel path is recognised as one");
CHECK(!modelpkg::IsPackagePath("/models/ANS_Test_v1.0.zip"),
"and a zip is not");
CHECK(!modelpkg::IsPackagePath(".ansmodel"),
"nor is a bare extension with no name in front of it");
// A build with no keys installed refuses every package with a sentence
// that says why, rather than failing somewhere deeper.
{
modelpkg::SetProcessModelKeys(modelpkg::Keyring{});
CHECK(!modelpkg::HaveProcessModelKeys(),
"a build with no keys knows it cannot open packages");
modelpkg::OpenedModel m;
std::string err;
CHECK(!modelpkg::OpenModelPackage(path, m, err) &&
err.find("no model-package key") != std::string::npos,
"and says so instead of failing at the decryption");
}
const Identity id = fixtureIdentity();
modelpkg::SetProcessModelKeys(fixtureKeyring(id));
CHECK(modelpkg::HaveProcessModelKeys(), "with keys set, it can");
modelpkg::OpenedModel m;
std::string err;
CHECK(modelpkg::OpenModelPackage(path, m, err), ("open: " + err).c_str());
CHECK(m.model.size() == 12000, "the model comes out whole");
CHECK(m.task == "detection" && m.family == "yolo",
"with the task and family the engine branches on");
CHECK(m.classes.size() == 2 && m.classes[0] == "Cavity",
"and the classes in order, so there is no classes.names to read");
CHECK(!m.name.empty(), "and a name for the log line");
// What the engine does the moment its session has the bytes.
m.wipe();
CHECK(m.model.empty(), "wiping leaves the plaintext nowhere");
// Describing one is the same answer without the decryption.
modelpkg::OpenedModel d;
CHECK(modelpkg::DescribeModelPackage(path, d, err) && d.model.empty() &&
d.classes.size() == 2 && d.task == "detection",
"describing a package answers without decrypting the model");
}
// ── what a model says about its own input ─────────────────────────────────
//
// A package written before manifests carried an input size still has to
// answer, so the model's own graph is read. These are hand-written graphs
// with node and initializer ahead of the input, as a real export has them —
// which also proves the reader seeks past the weights rather than reading
// them.
static std::string pbVarint(uint64_t v) {
std::string out;
do {
uint8_t b = uint8_t(v & 0x7f);
v >>= 7;
if (v) b |= 0x80;
out.push_back(char(b));
} while (v);
return out;
}
static std::string pbField(uint32_t num, const std::string& payload) {
return pbVarint(uint64_t(num) << 3 | 2) + pbVarint(payload.size()) + payload;
}
static std::string pbDim(int64_t v) { // Dimension.dim_value
return pbField(1, pbVarint(uint64_t(1) << 3 | 0) + pbVarint(uint64_t(v)));
}
static std::string pbDynamicDim(const std::string& name) {
return pbField(1, pbField(2, name)); // dim_param: the axis is open
}
static std::string fakeOnnx(const std::string& dims,
const std::string& outDims = std::string()) {
auto value = [](const std::string& name, const std::string& d) {
return pbField(1, name) + pbField(2, pbField(1, pbField(2, d)));
};
std::string graph = pbField(1, std::string(4096, 'n')) +
pbField(5, std::string(65536, 'w')) +
pbField(11, value("images", dims));
if (!outDims.empty()) graph += pbField(12, value("output0", outDims));
return pbField(7, graph);
}
static void testShapeFromTheGraph(const fs::path& dir) {
onnxshape::InputShape shape;
std::string why;
const std::string fixed = fakeOnnx(pbDim(1) + pbDim(3) + pbDim(224) + pbDim(224));
CHECK(onnxshape::fromMemory(fixed.data(), fixed.size(), shape, why) &&
shape.width == 224 && shape.height == 224 && shape.channels == 3,
"a graph in memory gives the size it fixes, not a default");
const fs::path onFile = dir / "graph.onnx";
{
std::ofstream f(onFile, std::ios::binary | std::ios::trunc);
f.write(fixed.data(), std::streamsize(fixed.size()));
}
onnxshape::InputShape viaFile;
CHECK(onnxshape::fromFile(onFile.string(), viaFile, why) &&
viaFile.width == shape.width && viaFile.height == shape.height,
"a graph on disk reads the same as the same graph in memory");
const std::string dynamic = fakeOnnx(pbDynamicDim("batch") + pbDim(3) +
pbDynamicDim("height") + pbDynamicDim("width"));
CHECK(!onnxshape::fromMemory(dynamic.data(), dynamic.size(), shape, why) &&
why.find("dynamic") != std::string::npos,
"a model that takes any size says so rather than inventing one");
const std::string rubbish(600, '\x7f');
CHECK(!onnxshape::fromMemory(rubbish.data(), rubbish.size(), shape, why),
"and something that is not a model is refused, not guessed at");
// End to end: a package whose manifest says nothing about size, opened the
// way ANSODBase opens one.
const Identity id = fixtureIdentity();
modelpkg::SetProcessModelKeys(fixtureKeyring(id));
modelpkg::PackRequest req;
req.keyId = "ansk-v1-base";
req.useX25519 = true;
req.basePublic = id.basePub;
req.nonceSeed = 21;
req.signerId = "anssig-2026-09";
req.signerSecret = id.signerSk;
req.signerPublic = id.signerPk;
req.rootKeyId = "ansroot-v1";
req.notAfter = "2030-01-01T00:00:00Z";
{
Json cert{{"keyId", req.signerId},
{"pub", bits::base64Encode(
std::string(id.signerPk.begin(), id.signerPk.end()))},
{"notAfter", req.notAfter}};
const std::string cb = modelpkg::detail::certBytes(cert);
req.rootSig.assign(64, 0);
crypto_ed25519_sign(req.rootSig.data(), id.rootSk.data(),
reinterpret_cast<const uint8_t*>(cb.data()), cb.size());
}
req.manifest = Json{{"task", "classification"},
{"family", "yolo"},
{"classes", Json::array({Json{{"id", 1}, {"name", "Good"}}})},
{"models", Json::array({Json{{"id", "m0"},
{"payload", "p0"},
{"entry", true}}})}};
CHECK(!req.manifest.contains("input"), "this manifest says nothing about size");
req.models.push_back({"p0", std::vector<uint8_t>(fixed.begin(), fixed.end())});
const std::string path = (dir / "noinput.ansmodel").string();
std::string err;
CHECK(modelpkg::pack(path, req, err), ("pack: " + err).c_str());
modelpkg::OpenedModel opened;
CHECK(modelpkg::OpenModelPackage(path, opened, err), ("open: " + err).c_str());
CHECK(opened.inputWidth == 224 && opened.inputHeight == 224,
"so the engine is told 224 from the graph, not left to assume 640");
// AN END-TO-END HEAD IN A PACKAGE THAT SAYS "yolo". Boxes used to take the
// family from the backend marker, and the marker names the runtime, not the
// head — so a yolo26 export with end2end on arrived declared as plain yolo.
// Decoding [1,N,6] as a v8 grid reads coordinates as confidences: boxes
// everywhere, and no error to notice. The graph settles it.
{
const std::string graph =
fakeOnnx(pbDim(1) + pbDim(3) + pbDim(640) + pbDim(640),
pbDim(1) + pbDim(300) + pbDim(6));
modelpkg::PackRequest r = req;
r.manifest = Json{{"task", "detection"},
{"family", "yolo"},
{"classes", Json::array({Json{{"id", 1}, {"name", "Tooth"}}})},
{"models", Json::array({Json{{"id", "m0"},
{"payload", "p0"},
{"entry", true}}})}};
r.models.clear();
r.models.push_back({"p0", std::vector<uint8_t>(graph.begin(), graph.end())});
const std::string p = (dir / "e2e.ansmodel").string();
CHECK(modelpkg::pack(p, r, err), ("pack e2e: " + err).c_str());
modelpkg::OpenedModel m;
CHECK(modelpkg::OpenModelPackage(p, m, err) && m.family == "yolo10",
"an end-to-end head is read as yolo10 whatever the package says");
CHECK(m.warning.find("end-to-end") != std::string::npos,
"and the reason is said out loud rather than silently swapped");
CHECK(m.legacyMarker.empty() || m.legacyMarker == "tensorrt.txt",
"while the legacy marker is left as it was");
}
// A two-class v8 export is [1, 6, 8400]: a 6 in the shape, not a head that
// has already decoded itself.
{
const std::string graph =
fakeOnnx(pbDim(1) + pbDim(3) + pbDim(640) + pbDim(640),
pbDim(1) + pbDim(6) + pbDim(8400));
modelpkg::PackRequest r = req;
r.manifest = Json{{"task", "detection"},
{"family", "yolo"},
{"classes", Json::array({Json{{"id", 1}, {"name", "Tooth"}}})},
{"models", Json::array({Json{{"id", "m0"},
{"payload", "p0"},
{"entry", true}}})}};
r.models.clear();
r.models.push_back({"p0", std::vector<uint8_t>(graph.begin(), graph.end())});
const std::string p = (dir / "twoclass.ansmodel").string();
CHECK(modelpkg::pack(p, r, err), ("pack v8: " + err).c_str());
modelpkg::OpenedModel m;
CHECK(modelpkg::OpenModelPackage(p, m, err) && m.family == "yolo",
"a 6 on the wrong axis with thousands of anchors stays plain yolo");
}
}
int main(int argc, char** argv) {
const fs::path dataDir = argc > 1 ? fs::path(argv[1]) : fs::path("data");
const auto stamp = std::chrono::steady_clock::now().time_since_epoch().count();
const fs::path tmp =
fs::temp_directory_path() / ("anscore-modelpkg-" + std::to_string(stamp));
std::error_code ec;
fs::create_directories(tmp, ec);
testVectors();
testInterop(dataDir);
testRoundTripAndRefusals(tmp);
testOpenForAnEngine(dataDir);
testShapeFromTheGraph(tmp);
fs::remove_all(tmp, ec);
std::printf("%d checks, %d failures\n", gChecks, gFailures);
return gFailures == 0 ? 0 : 1;
}