mirror of
https://github.com/storytold/lightcraft.git
synced 2026-10-26 11:09:23 -06:00
New L1 crate `lightcraft-faces`, the base of a one-drop model install: - `ModelManifest` (role, licence with a commercial yes / no / unknown flag and a plain-language notice, provenance, input preprocessing, output, thresholds) with strict validation: sizes capped, numbers finite and sane, https-only addresses, bounded text. A manifest from any source is untrusted. - `known`: models recognised by SHA-256 (YuNet, SFace, AuraFace v1), with what is and is not known about their licences and training data. - `onnx`: a streaming, hostile-input-safe protobuf probe of a model's graph inputs and outputs. It reads a 248 MB model in about 4 ms and never loads the weights; truncation at every length and random corruption never panic. - `suggest`: known hash -> its manifest; an unknown model shaped like a face embedder -> a draft manifest with the assumptions spelled out; anything else -> a plain reason it is not supported yet. - `hash`: streamed SHA-256. Checked against the real model files with an opt-in test (LC_FACE_MODELS). It found an old exporter listing every weight as an input, now handled. Running a model (tract) is not part of this crate yet. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
47 lines
1.2 KiB
Rust
47 lines
1.2 KiB
Rust
//! SHA-256 of a model file, streamed (a model can be hundreds of megabytes).
|
|
|
|
use std::io::{self, Read};
|
|
use std::path::Path;
|
|
|
|
use sha2::{Digest, Sha256};
|
|
|
|
/// Lowercase hex SHA-256 of everything `r` yields.
|
|
pub fn sha256_reader<R: Read>(mut r: R) -> io::Result<String> {
|
|
let mut h = Sha256::new();
|
|
let mut buf = vec![0u8; 1 << 20];
|
|
loop {
|
|
let n = r.read(&mut buf)?;
|
|
if n == 0 {
|
|
break;
|
|
}
|
|
h.update(buf.get(..n).unwrap_or(&[]));
|
|
}
|
|
Ok(hex(&h.finalize()))
|
|
}
|
|
|
|
/// Lowercase hex SHA-256 of a file.
|
|
pub fn sha256_file(path: &Path) -> io::Result<String> {
|
|
sha256_reader(io::BufReader::new(std::fs::File::open(path)?))
|
|
}
|
|
|
|
/// Lowercase hex SHA-256 of `bytes`.
|
|
pub fn sha256_hex(bytes: &[u8]) -> String {
|
|
hex(&Sha256::digest(bytes))
|
|
}
|
|
|
|
fn hex(bytes: &[u8]) -> String {
|
|
bytes.iter().map(|b| format!("{b:02x}")).collect()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn known_digests() {
|
|
assert_eq!(sha256_hex(b""), "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
|
|
assert_eq!(sha256_hex(b"abc"), "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
|
|
assert_eq!(sha256_reader(&b"abc"[..]).unwrap(), sha256_hex(b"abc"));
|
|
}
|
|
}
|