264 lines
10 KiB
C++
264 lines
10 KiB
C++
// 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 "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");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
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);
|
||
|
|
|
||
|
|
fs::remove_all(tmp, ec);
|
||
|
|
std::printf("%d checks, %d failures\n", gChecks, gFailures);
|
||
|
|
return gFailures == 0 ? 0 : 1;
|
||
|
|
}
|