SPB Git

Repository Public Apache-2.0

mod.rs 3.6 KB 110 lines rust

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SPB Quantum Crypto7// File             : crates/spb-pqc-core/tests/common/mod.rs8// Module           : Tests9// Repository       : https://git.spboucher.ai/spb-quantum-crypto10// Project Website  : https://pqce.spboucher.ai11// Created          : 2026-12-0312// Last Modified    : 2026-12-0313//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : Apache-2.033//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041//! Shared helpers for the ACVP known-answer tests.42//!43//! Fixtures are the unmodified `internalProjection.json` files of44//! `usnistgov/ACVP-Server` (revision pinned in `tests/kat/acvp/ACVP_SERVER_REV`),45//! stored gzip-compressed; their SHA-256 are in `tests/kat/acvp/SHA256SUMS`.46#![allow(dead_code)]4748use serde_json::Value;49use std::path::PathBuf;50use std::process::Command;5152pub fn fixture_dir() -> PathBuf {53    PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/kat/acvp")54}5556/// Loads `tests/kat/acvp/<name>/internalProjection.json[.gz]`.57pub fn load(name: &str) -> Value {58    let dir = fixture_dir().join(name);59    let raw = dir.join("internalProjection.json");60    let gz = dir.join("internalProjection.json.gz");61    let bytes = if raw.exists() {62        std::fs::read(&raw).expect("read fixture")63    } else if gz.exists() {64        let out = Command::new("gzip")65            .arg("-dc")66            .arg(&gz)67            .output()68            .expect("run gzip");69        assert!(out.status.success(), "gzip -dc failed for {}", gz.display());70        out.stdout71    } else {72        panic!(73            "missing official fixture {} — run ./scripts/fetch-kat.sh (it downloads the pinned \74             NIST ACVP-Server revision and verifies SHA-256)",75            gz.display()76        );77    };78    serde_json::from_slice(&bytes).expect("parse fixture")79}8081pub fn unhex(s: &str) -> Vec<u8> {82    assert!(s.len() % 2 == 0, "odd hex length");83    (0..s.len())84        .step_by(2)85        .map(|i| u8::from_str_radix(&s[i..i + 2], 16).expect("hex digit"))86        .collect()87}8889pub fn hex(b: &[u8]) -> String {90    b.iter().map(|x| format!("{x:02X}")).collect()91}9293pub fn s<'a>(v: &'a Value, key: &str) -> &'a str {94    v[key]95        .as_str()96        .unwrap_or_else(|| panic!("missing string field {key}"))97}9899pub fn bytes(v: &Value, key: &str) -> Vec<u8> {100    unhex(s(v, key))101}102103pub fn groups(v: &Value) -> &Vec<Value> {104    v["testGroups"].as_array().expect("testGroups")105}106107pub fn tests(g: &Value) -> &Vec<Value> {108    g["tests"].as_array().expect("tests")109}110
Files main
Menu

Theme