mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-28 01:11:23 +01:00
* Extract the material graph runtime from TextureCraft Preserve its evaluator, compiler, existing verification, and format reference from TextureCraft #777 at 22a37c29. Original TextureCraft importer author: xilefenko (TextureCraft #521). * Adapt graph helpers to the shared workspace lints Preserve node evaluation and version tracking while splitting long dispatch branches. Update the EXR doctest and rule table references for the shared crate. * Read the format reference with LF endings in its test * Test that the format reference example reads the same with CRLF line endings * Make the kind versions follow module attributes, arm order, raw strings, includes and the epoch * Fix graph cache keys, publish, import and subgraph extraction; harden the graph tests * Keep the cubic sampler from panicking on NaN texels; say what RGBE drops; check NaN halves by their bits * Lower vector inputs of f_to_bool and f_vec to valid WGSL; tighten the FX-map error and pattern coverage tests; document clamping and the splatter data cutoff * Say which composite glyph placements the font reader lacks; fail the y-up text test on a blank render * Leave publish as it was; unit-test the WGSL vector lowering; document EPOCH and keep_outputs results * Satisfy clippy in the version scanner and the WGSL lowering; compare cache hits by relation
246 lines
9.1 KiB
Rust
246 lines
9.1 KiB
Rust
//! [`Fingerprint`]: SHA-256 (FIPS 180-4) over typed values. A collision would be a stale result that nothing could
|
|
//! detect, which rules out the 64-bit hashes the in-memory caches get away with. Every value is written with its length
|
|
//! or a fixed width, so that two different sequences of values never give the same bytes.
|
|
|
|
/// The round constants: the first 32 bits of the fractional parts of the cube roots of the first 64 primes.
|
|
const K: [u32; 64] = [
|
|
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
|
|
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
|
|
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
|
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
];
|
|
|
|
const START: [u32; 8] = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19];
|
|
|
|
/// A finished fingerprint.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
|
pub struct Digest(pub [u8; 32]);
|
|
|
|
impl Digest {
|
|
pub fn to_hex(self) -> String {
|
|
let mut out = String::with_capacity(64);
|
|
for b in self.0 {
|
|
out.push(char::from_digit(u32::from(b >> 4), 16).unwrap_or('0'));
|
|
out.push(char::from_digit(u32::from(b & 15), 16).unwrap_or('0'));
|
|
}
|
|
out
|
|
}
|
|
|
|
/// The digest `hex` spells, as [`Digest::to_hex`] writes it.
|
|
pub fn from_hex(hex: &str) -> Option<Digest> {
|
|
let bytes = hex.as_bytes();
|
|
if bytes.len() != 64 {
|
|
return None;
|
|
}
|
|
let mut out = [0u8; 32];
|
|
for (o, [high, low]) in out.iter_mut().zip(bytes.as_chunks::<2>().0) {
|
|
let digit = |b: u8| char::from(b).to_digit(16);
|
|
*o = (digit(*high)? * 16 + digit(*low)?) as u8;
|
|
}
|
|
Some(Digest(out))
|
|
}
|
|
}
|
|
|
|
/// A running SHA-256 over typed values.
|
|
#[derive(Clone)]
|
|
pub struct Fingerprint {
|
|
state: [u32; 8],
|
|
block: [u8; 64],
|
|
filled: usize,
|
|
length: u64,
|
|
}
|
|
|
|
impl Default for Fingerprint {
|
|
fn default() -> Self {
|
|
Fingerprint { state: START, block: [0; 64], filled: 0, length: 0 }
|
|
}
|
|
}
|
|
|
|
impl Fingerprint {
|
|
/// A fingerprint of one purpose: results of different kinds never meet, even over equal values.
|
|
pub fn of(purpose: &str) -> Fingerprint {
|
|
let mut f = Fingerprint::default();
|
|
f.text(purpose);
|
|
f
|
|
}
|
|
|
|
/// Raw bytes, with no length: for a sequence whose length is written apart.
|
|
pub fn raw(&mut self, mut data: &[u8]) {
|
|
self.length = self.length.wrapping_add(data.len() as u64);
|
|
if self.filled > 0 {
|
|
let room = 64 - self.filled;
|
|
let (now, rest) = data.split_at(room.min(data.len()));
|
|
if let Some(slot) = self.block.get_mut(self.filled..self.filled + now.len()) {
|
|
slot.copy_from_slice(now);
|
|
}
|
|
self.filled += now.len();
|
|
data = rest;
|
|
if self.filled < 64 {
|
|
return;
|
|
}
|
|
let block = self.block;
|
|
self.compress(&block);
|
|
self.filled = 0;
|
|
}
|
|
let (blocks, rest) = data.as_chunks::<64>();
|
|
for block in blocks {
|
|
self.compress(block);
|
|
}
|
|
if let Some(slot) = self.block.get_mut(..rest.len()) {
|
|
slot.copy_from_slice(rest);
|
|
}
|
|
self.filled = rest.len();
|
|
}
|
|
|
|
pub fn u64(&mut self, v: u64) {
|
|
self.raw(&v.to_le_bytes());
|
|
}
|
|
|
|
/// Bytes with their length.
|
|
pub fn bytes(&mut self, data: &[u8]) {
|
|
self.u64(data.len() as u64);
|
|
self.raw(data);
|
|
}
|
|
|
|
pub fn text(&mut self, text: &str) {
|
|
self.bytes(text.as_bytes());
|
|
}
|
|
|
|
pub fn digest(&mut self, d: &Digest) {
|
|
self.raw(&d.0);
|
|
}
|
|
|
|
/// Floats by their bits: a different bit pattern is a different input.
|
|
pub fn floats(&mut self, values: &[f32]) {
|
|
self.u64(values.len() as u64);
|
|
let mut buffer = [0u8; 4096];
|
|
for chunk in values.chunks(buffer.len() / 4) {
|
|
for (o, v) in buffer.as_chunks_mut::<4>().0.iter_mut().zip(chunk) {
|
|
o.copy_from_slice(&v.to_bits().to_le_bytes());
|
|
}
|
|
if let Some(used) = buffer.get(..chunk.len() * 4) {
|
|
self.raw(used);
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn finish(mut self) -> Digest {
|
|
let bits = self.length.wrapping_mul(8);
|
|
self.raw(&[0x80]);
|
|
while self.filled != 56 {
|
|
self.raw(&[0]);
|
|
}
|
|
self.raw(&bits.to_be_bytes());
|
|
let mut out = [0u8; 32];
|
|
for (o, s) in out.as_chunks_mut::<4>().0.iter_mut().zip(self.state) {
|
|
o.copy_from_slice(&s.to_be_bytes());
|
|
}
|
|
Digest(out)
|
|
}
|
|
|
|
fn compress(&mut self, block: &[u8; 64]) {
|
|
let mut w = [0u32; 64];
|
|
for (o, c) in w.iter_mut().zip(block.as_chunks::<4>().0) {
|
|
*o = u32::from_be_bytes(*c);
|
|
}
|
|
for i in 16..64 {
|
|
let at = |k: usize| w.get(i - k).copied().unwrap_or(0);
|
|
let s0 = at(15).rotate_right(7) ^ at(15).rotate_right(18) ^ (at(15) >> 3);
|
|
let s1 = at(2).rotate_right(17) ^ at(2).rotate_right(19) ^ (at(2) >> 10);
|
|
let value = at(16).wrapping_add(s0).wrapping_add(at(7)).wrapping_add(s1);
|
|
if let Some(slot) = w.get_mut(i) {
|
|
*slot = value;
|
|
}
|
|
}
|
|
let [mut a, mut b, mut c, mut d, mut e, mut f, mut g, mut h] = self.state;
|
|
for (word, constant) in w.iter().zip(K) {
|
|
let s1 = e.rotate_right(6) ^ e.rotate_right(11) ^ e.rotate_right(25);
|
|
let ch = (e & f) ^ (!e & g);
|
|
let t1 = h.wrapping_add(s1).wrapping_add(ch).wrapping_add(constant).wrapping_add(*word);
|
|
let s0 = a.rotate_right(2) ^ a.rotate_right(13) ^ a.rotate_right(22);
|
|
let maj = (a & b) ^ (a & c) ^ (b & c);
|
|
let t2 = s0.wrapping_add(maj);
|
|
(h, g, f, e, d, c, b, a) = (g, f, e, d.wrapping_add(t1), c, b, a, t1.wrapping_add(t2));
|
|
}
|
|
for (s, v) in self.state.iter_mut().zip([a, b, c, d, e, f, g, h]) {
|
|
*s = s.wrapping_add(v);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// What `pub const EPOCH` is set to in the text of `versions.rs`; empty when it isn't found.
|
|
pub fn epoch_in(text: &str) -> String {
|
|
let line = text.lines().find(|l| l.trim_start().starts_with("pub const EPOCH"));
|
|
line.and_then(|l| l.split('=').nth(1)).map(|v| v.trim().trim_end_matches(';').trim().to_string()).unwrap_or_default()
|
|
}
|
|
|
|
/// `version` with `epoch` mixed in: bumping the epoch changes every version that goes through here.
|
|
pub fn with_epoch(epoch: &str, version: &Digest) -> Digest {
|
|
let mut f = Fingerprint::of("epoch");
|
|
f.text(epoch);
|
|
f.digest(version);
|
|
f.finish()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn the_epoch_is_read_and_changes_every_version() {
|
|
assert_eq!(epoch_in("/// docs\npub const EPOCH: u32 = 12;\nmod generated {}"), "12");
|
|
assert_eq!(epoch_in("nothing"), "");
|
|
let version = Fingerprint::of("kind").finish();
|
|
assert_ne!(with_epoch("1", &version), with_epoch("2", &version));
|
|
assert_ne!(with_epoch("1", &version), version);
|
|
assert_eq!(with_epoch("1", &version), with_epoch("1", &version));
|
|
}
|
|
|
|
fn sha(data: &[u8]) -> String {
|
|
let mut f = Fingerprint::default();
|
|
f.raw(data);
|
|
f.finish().to_hex()
|
|
}
|
|
|
|
#[test]
|
|
fn known_answers() {
|
|
assert_eq!(sha(b""), "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
|
|
assert_eq!(sha(b"abc"), "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
|
|
assert_eq!(
|
|
sha(b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"),
|
|
"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"
|
|
);
|
|
assert_eq!(sha(&[b'a'; 1_000_000]), "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0");
|
|
}
|
|
|
|
#[test]
|
|
fn feeding_in_pieces_gives_the_same() {
|
|
let data: Vec<u8> = (0..1000u32).map(|v| v as u8).collect();
|
|
let mut pieces = Fingerprint::default();
|
|
for chunk in data.chunks(7) {
|
|
pieces.raw(chunk);
|
|
}
|
|
assert_eq!(pieces.finish().to_hex(), sha(&data));
|
|
}
|
|
|
|
#[test]
|
|
fn values_do_not_run_into_each_other() {
|
|
let of = |parts: &[&str]| {
|
|
let mut f = Fingerprint::of("test");
|
|
parts.iter().for_each(|p| f.text(p));
|
|
f.finish()
|
|
};
|
|
assert_ne!(of(&["ab", "c"]), of(&["a", "bc"]));
|
|
assert_ne!(Fingerprint::of("one").finish(), Fingerprint::of("two").finish());
|
|
}
|
|
|
|
#[test]
|
|
fn hex_round_trip() {
|
|
let d = Fingerprint::of("x").finish();
|
|
assert_eq!(Digest::from_hex(&d.to_hex()), Some(d));
|
|
assert_eq!(Digest::from_hex("zz"), None);
|
|
}
|
|
}
|