F1.2: std.hash zero-heap [64]u8 hex API + chunked file + pinned vectors
Make the SHA-256 digest path allocation-free (foundation heap-discipline):
- final() and sha256_hex() now return the 64-char lowercase hex digest as
a [64]u8 by value on the stack; the cstring(64) heap allocation is gone.
- sha256_file() streams the file in fixed 64KB stack chunks via open_file/
File.read/File.close (defer-closed on every path) instead of slurping it
with read_file; peak memory is O(chunk), not O(filesize).
Tests (compare via a zero-copy string view over the [64]u8):
- 0710 updated to the by-value API (output unchanged).
- 0711 known-answer vectors: "", "abc", NIST-56/112, padding boundaries
{0,55,56,57,63,64,65,119,120}, and 1000 / 1,000,000 'a' repeats, each
pinned to its published digest (cross-checked with shasum -a 256).
- 0712 streaming equivalence (one-shot == byte-at-a-time == split-mid-block
== split-on-boundary) plus sha256_file(temp) == in-memory digest.
src/ untouched. zig build && zig build test && tests/run_examples.sh green.
This commit is contained in:
76
examples/0712-modules-sha256-streaming.sx
Normal file
76
examples/0712-modules-sha256-streaming.sx
Normal file
@@ -0,0 +1,76 @@
|
||||
// SHA-256 streaming-equivalence + file hashing for `modules/std/hash.sx`.
|
||||
//
|
||||
// The chunk boundary must not affect the result: feeding the same bytes
|
||||
// one-shot, one byte at a time, split mid-block, and split exactly on a
|
||||
// 64-byte block boundary all yield the same digest, anchored to a pinned
|
||||
// value. Then `sha256_file` of a written temp file must equal the
|
||||
// in-memory digest of the same bytes — the streaming file path agrees
|
||||
// with the buffered path.
|
||||
//
|
||||
// All comparisons go through `string` views over the zero-heap `[64]u8`
|
||||
// digests; the byte/split updates view directly into the input buffer
|
||||
// (no `substr`, no copies).
|
||||
|
||||
#import "modules/std.sx";
|
||||
#import "modules/std/hash.sx";
|
||||
#import "modules/fs.sx";
|
||||
|
||||
// 112-byte NIST multi-block vector — long enough that a 64-byte split is
|
||||
// a genuine block boundary and a 30-byte split lands mid-block.
|
||||
MSG :: "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
|
||||
PIN :: "cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1";
|
||||
|
||||
check :: (label: string, got: [64]u8, want: string) {
|
||||
view := string.{ ptr = @got[0], len = 64 };
|
||||
if view == want {
|
||||
print("{}: ok\n", label);
|
||||
} else {
|
||||
print("{}: FAIL got {} want {}\n", label, view, want);
|
||||
}
|
||||
}
|
||||
|
||||
report :: (label: string, ok: bool) {
|
||||
if ok { print("{}: ok\n", label); } else { print("{}: FAIL\n", label); }
|
||||
}
|
||||
|
||||
// Absorb `data` one byte at a time (views of length 1 into the buffer).
|
||||
stream_by_byte :: (data: string) -> [64]u8 {
|
||||
h := init();
|
||||
i := 0;
|
||||
while i < data.len {
|
||||
h.update(string.{ ptr = @data[i], len = 1 });
|
||||
i += 1;
|
||||
}
|
||||
h.final()
|
||||
}
|
||||
|
||||
// Absorb `data` as two updates split at `at` (views into the buffer).
|
||||
stream_split :: (data: string, at: s64) -> [64]u8 {
|
||||
h := init();
|
||||
h.update(string.{ ptr = @data[0], len = at });
|
||||
h.update(string.{ ptr = @data[at], len = data.len - at });
|
||||
h.final()
|
||||
}
|
||||
|
||||
main :: () {
|
||||
check("oneshot-pinned", sha256_hex(MSG), PIN);
|
||||
check("byte-at-a-time", stream_by_byte(MSG), PIN);
|
||||
check("split-mid-block", stream_split(MSG, 30), PIN); // 30: mid first block
|
||||
check("split-on-boundary", stream_split(MSG, 64), PIN); // 64: exact block edge
|
||||
|
||||
// sha256_file (streaming) must equal the in-memory digest.
|
||||
path := "/tmp/sx_0712_stream.bin";
|
||||
if !write_file(path, MSG) { print("file-write: FAIL\n"); return; }
|
||||
|
||||
maybe := sha256_file(path);
|
||||
if maybe == null { print("file-eq-memory: FAIL (open)\n"); return; }
|
||||
file_digest := maybe!;
|
||||
mem_digest := sha256_hex(MSG);
|
||||
|
||||
fv := string.{ ptr = @file_digest[0], len = 64 };
|
||||
mv := string.{ ptr = @mem_digest[0], len = 64 };
|
||||
report("file-eq-memory", fv == mv);
|
||||
check("file-pinned", file_digest, PIN);
|
||||
|
||||
delete_file(path);
|
||||
}
|
||||
Reference in New Issue
Block a user