Files
boxmaker/docs/plans/M2a/files/crates/proto/tests/hash.rs
T
kyleandClaude Fable 5.1 76ccc251cd Add M2a plan: thirteen tasks, tests, fake server and recordings
The tasks build the inference path: emsha-backed SHA-256, inferproxy,
config, a hand-written HTTP and SSE client, request building, delta
assembly, the chat state machine, the thinking cap, the slot gate with
retry, the startup self-test and on-device verification.

Everything the tasks copy in was checked against a private reference
implementation: the gate passes after each task in order, the timing
tests pass repeatedly under CPU load, and the reference passes the
self-test and all four device checks on straylight. Expected results
for the recorded streams were derived by a separate script.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-17 13:34:11 -07:00

101 lines
2.7 KiB
Rust

//! SHA-256 vectors. Do not edit: these define the required behaviour.
//!
//! The lengths 55, 56, 63, 64 and 65 sit on either side of the padding boundaries of SHA-256,
//! which is where implementations go wrong. Expected values come from `sha256sum`.
use proto::{Sha256, sha256};
const VECTORS: &[(usize, &str)] = &[
(
0,
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
),
(
1,
"ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb",
),
(
55,
"9f4390f8d30c2dd92ec9f095b65e2b9ae9b0a925a5258e241c9f1e910f734318",
),
(
56,
"b35439a4ac6f0948b6d6f9e3c6af0f5f590ce20f1bde7090ef7970686ec6738a",
),
(
63,
"7d3e74a05d7db15bce4ad9ec0658ea98e3f06eeecf16b4c6fff2da457ddc2f34",
),
(
64,
"ffe054fe7ae0cb6dc65c3af9b61d5209f439851db43d0ba5997337df154668eb",
),
(
65,
"635361c48bb9eab14198e76ea8ab7f1a41685d6ad62aa9146d301d4f17eb0ae0",
),
(
119,
"31eba51c313a5c08226adf18d4a359cfdfd8d2e816b13f4af952f7ea6584dcfb",
),
(
127,
"c57e9278af78fa3cab38667bef4ce29d783787a2f731d4e12200270f0c32320a",
),
(
128,
"6836cf13bac400e9105071cd6af47084dfacad4e5e302c94bfed24e013afb73e",
),
(
1000,
"41edece42d63e8d9bf515a9ba6932e1c20cbc9f5a5d134645adb5db1b9737ea3",
),
];
/// `n` bytes of the letter `a`.
fn letters(n: usize) -> Vec<u8> {
vec![b'a'; n]
}
#[test]
fn abc() {
let want = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad";
assert_eq!(sha256(b"abc").unwrap().to_hex(), want);
}
#[test]
fn lengths_around_the_padding_boundaries() {
for (n, want) in VECTORS {
assert_eq!(sha256(&letters(*n)).unwrap().to_hex(), *want, "{n} bytes");
}
}
#[test]
fn input_in_two_pieces_gives_the_same_hash() {
for (n, want) in VECTORS {
let data = letters(*n);
for split in [0, 1, 55, 56, 63, 64, 65, *n / 2, *n] {
let split = split.min(*n);
let mut h = Sha256::new();
h.update(&data[..split]).unwrap();
h.update(&data[split..]).unwrap();
assert_eq!(
h.finish().unwrap().to_hex(),
*want,
"{n} bytes split at {split}"
);
}
}
}
#[test]
fn one_million_letters() {
let want = "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0";
assert_eq!(sha256(&letters(1_000_000)).unwrap().to_hex(), want);
let mut h = Sha256::default();
for _ in 0..1000 {
h.update(&letters(1000)).unwrap();
}
assert_eq!(h.finish().unwrap().to_hex(), want);
}