Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-sovereign-core-mv/blob/main/crates/aien-kv-store/src/validate.rs # aien-sovereign-core-mv AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-sovereign-core-mv / crates / aien-kv-store / src / validate.rs` 110 lines ยท 4619 bytes Raw Blame History 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 //! Validation before any allocation or copy (ADR 0003 section 1.4). 17 18 use crate::error::KvError; 19 use crate::record::{ 20 segment_count, ChunkHeader, RecordHeader, FLAG_PARTIAL_TAIL, HEADER_REGION_LEN, PAYLOAD_DOMAIN, 21 PAYLOAD_OFFSET, SEAL_SLOT_OFFSET, SEGMENT_TABLE_OFFSET, 22 }; 23 use sha2::{Digest, Sha256}; 24 25 /// Check `buf` (the first 4096 bytes of a record, the header region) and the 26 /// total `file_size` against what the caller expects. `segment_bytes` is the 27 /// payload segment size the caller derived from its own geometry 28 /// (`NamespaceFields::segment_bytes`; the file does not store it, ADR 2.1 item 29 /// 3). Order: length (4096), magic, version, kind, header_len, header 30 /// checksum, reserved flags (all in `RecordHeader::decode`), then namespace, 31 /// key, parent, position, token count, geometry, partial flag, payload_len, 32 /// segment count (`TooManySegments`), region padding (`BadPadding`), segment 33 /// root (`SegmentRootMismatch`), file size (`4096 + payload_len`). Performs no 34 /// heap allocation and copies no payload; the first failure is returned. 35 /// 36 /// `TooManySegments` is checked before the padding and the root because both 37 /// need the table length; the error enum lists the three in a different order 38 /// (ADR 12 H7), which only matters for source order. 39 /// 40 /// `expect` is the header the caller derived for the chunk it asked for 41 /// (`NamespaceFields::chunk_header`); its `written_unix_ns` is ignored. 42 pub fn validate_record( 43 buf: &[u8], 44 file_size: u64, 45 expect: &ChunkHeader, 46 segment_bytes: u64, 47 ) -> Result { 48 let rec = RecordHeader::decode(buf)?; 49 let h = &rec.chunk; 50 if h.namespace_id != expect.namespace_id { 51 return Err(KvError::NamespaceMismatch); 52 } 53 if h.chunk_key != expect.chunk_key { 54 return Err(KvError::KeyMismatch); 55 } 56 if h.parent_key != expect.parent_key { 57 return Err(KvError::ParentMismatch); 58 } 59 if h.first_position != expect.first_position 60 || h.chunk_tokens == 0 61 || !h.first_position.is_multiple_of(h.chunk_tokens) 62 { 63 return Err(KvError::PositionMismatch); 64 } 65 if h.token_count == 0 || h.token_count > h.chunk_tokens { 66 return Err(KvError::TokenCountOutOfRange); 67 } 68 if h.token_count != expect.token_count { 69 return Err(KvError::TokenCountMismatch); 70 } 71 if h.model_sha256 != expect.model_sha256 72 || h.chunk_tokens != expect.chunk_tokens 73 || h.block_size != expect.block_size 74 || h.num_layers != expect.num_layers 75 || h.num_kv_heads != expect.num_kv_heads 76 || h.head_dim != expect.head_dim 77 || h.dtype != expect.dtype 78 || h.backend_format != expect.backend_format 79 { 80 return Err(KvError::GeometryMismatch); 81 } 82 let partial = h.token_count < h.chunk_tokens; 83 if (h.flags & FLAG_PARTIAL_TAIL != 0) != partial || h.flags != expect.flags { 84 return Err(KvError::BadFlags); 85 } 86 if h.payload_len != expect.payload_len { 87 return Err(KvError::PayloadLenMismatch); 88 } 89 90 // Header region: the segment digest table, zero padding up to the seal 91 // slot, and the root. The seal slot (last 32 bytes) is not padding: the 92 // seal module checks and authenticates it (ADR 0003 section 20). 93 let n_seg = segment_count(h.payload_len, segment_bytes)? as usize; 94 let table_end = SEGMENT_TABLE_OFFSET + 32 * n_seg; 95 let region = &buf[..HEADER_REGION_LEN]; 96 if region[table_end..SEAL_SLOT_OFFSET].iter().any(|b| *b != 0) { 97 return Err(KvError::BadPadding); 98 } 99 let mut root = Sha256::new(); 100 root.update(PAYLOAD_DOMAIN); 101 root.update(®ion[SEGMENT_TABLE_OFFSET..table_end]); 102 if root.finalize().as_slice() != rec.payload_sha256 { 103 return Err(KvError::SegmentRootMismatch); 104 } 105 106 match (PAYLOAD_OFFSET as u64).checked_add(h.payload_len) { 107 Some(total) if total == file_size => Ok(rec), 108 _ => Err(KvError::FileSizeMismatch), 109 } 110 }