// K2316-K2320: file_tools.hash_tool — Dosya SHA256 aracı // Invariant: Dosya byteı ve hata ayrılır // Positive: Boş ve çok extent dosya // Counterexample: Kısa I/O başarı sayılır // Acceptance: Bağımsız dosya hash oracle ile eşit pub const FILE_TOOLS_HASH_TOOL_SCHEMA: &str = "turkos.os.file_tools.hash_tool.v1"; pub const FILE_TOOLS_HASH_TOOL_INVARIANT: &str = "Dosya byteı ve hata ayrılır"; pub const FILE_TOOLS_HASH_TOOL_POSITIVE: &str = "Boş ve çok extent dosya"; pub const FILE_TOOLS_HASH_TOOL_COUNTEREXAMPLE: &str = "Kısa I/O başarı sayılır"; pub const FILE_TOOLS_HASH_TOOL_ACCEPTANCE: &str = "Bağımsız dosya hash oracle ile eşit"; pub const EXPECTED_HASHED_FILES_COUNT: u32 = 2; pub const EXPECTED_EXTENT_SPANS_VERIFIED: u32 = 4; pub const EXPECTED_ORACLE_MATCHES: u32 = 2; pub const EXPECTED_SHORT_IO_FALSE_SUCCESSES: u32 = 0; #[repr(u8)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum FileToolsHashToolError { ShortIoCountedAsSuccess = 1, FileBytesConflatedWithError = 2, IndependentHashOracleMismatch = 3, HashedFilesCountMismatch = 4, ExtentSpansVerifiedMismatch = 5, OracleMatchesMismatch = 6, ZeroHashToolEpoch = 7, NonMonotonicHashToolEpoch = 8, ZeroOracleDigest = 9, UnboundFileToolsHashToolDigest = 10, AlreadyCommitted = 11, } #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct FileToolsHashToolRequest { pub short_io_counted_as_success: bool, pub file_bytes_conflated_with_error: bool, pub independent_oracle_matched: bool, pub hashed_files_count: u32, pub extent_spans_verified: u32, pub oracle_matches: u32, pub hash_tool_epoch: u64, pub previous_hash_tool_epoch: u64, pub oracle_digest: u64, pub hash_tool_binding_digest: u64, } #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct FileToolsHashToolReceipt { pub hashed_files_count: u32, pub extent_spans_verified: u32, pub oracle_matches: u32, pub short_io_false_successes: u32, pub independent_oracle_matched: bool, pub hash_tool_epoch: u64, pub oracle_digest: u64, pub receipt_digest: u64, } pub const fn compute_file_tools_hash_tool_binding_digest( hashed_files_count: u32, extent_spans_verified: u32, oracle_matches: u32, short_io_false_successes: u32, hash_tool_epoch: u64, oracle_digest: u64, ) -> u64 { let mut h: u64 = 0xcbf2_9ce4_8422_2325; h = (h ^ 0x2320_4654_4841_5348).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ (hashed_files_count as u64)).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ (extent_spans_verified as u64)).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ (oracle_matches as u64)).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ (short_io_false_successes as u64)).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ hash_tool_epoch).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ oracle_digest).wrapping_mul(0x0000_0100_0000_01b3); h } pub struct FileToolsHashToolCoordinator { committed: bool, committed_total: u64, rejected_total: u64, short_io_error_refusals: u64, extent_oracle_refusals: u64, committed_receipt: Option, } impl FileToolsHashToolCoordinator { pub const fn new() -> Self { Self { committed: false, committed_total: 0, rejected_total: 0, short_io_error_refusals: 0, extent_oracle_refusals: 0, committed_receipt: None, } } pub fn is_committed(&self) -> bool { self.committed } pub fn committed_total(&self) -> u64 { self.committed_total } pub fn rejected_total(&self) -> u64 { self.rejected_total } pub fn short_io_error_refusals(&self) -> u64 { self.short_io_error_refusals } pub fn extent_oracle_refusals(&self) -> u64 { self.extent_oracle_refusals } pub fn committed_receipt(&self) -> Option { self.committed_receipt } pub fn commit_hash_verification( &mut self, req: &FileToolsHashToolRequest, ) -> Result { if self.committed { self.rejected_total += 1; return Err(FileToolsHashToolError::AlreadyCommitted); } if req.short_io_counted_as_success { self.rejected_total += 1; self.short_io_error_refusals += 1; return Err(FileToolsHashToolError::ShortIoCountedAsSuccess); } if req.file_bytes_conflated_with_error { self.rejected_total += 1; self.short_io_error_refusals += 1; return Err(FileToolsHashToolError::FileBytesConflatedWithError); } if !req.independent_oracle_matched { self.rejected_total += 1; self.short_io_error_refusals += 1; return Err(FileToolsHashToolError::IndependentHashOracleMismatch); } if req.hashed_files_count != EXPECTED_HASHED_FILES_COUNT { self.rejected_total += 1; self.extent_oracle_refusals += 1; return Err(FileToolsHashToolError::HashedFilesCountMismatch); } if req.extent_spans_verified != EXPECTED_EXTENT_SPANS_VERIFIED { self.rejected_total += 1; self.extent_oracle_refusals += 1; return Err(FileToolsHashToolError::ExtentSpansVerifiedMismatch); } if req.oracle_matches != EXPECTED_ORACLE_MATCHES { self.rejected_total += 1; self.extent_oracle_refusals += 1; return Err(FileToolsHashToolError::OracleMatchesMismatch); } if req.hash_tool_epoch == 0 { self.rejected_total += 1; return Err(FileToolsHashToolError::ZeroHashToolEpoch); } if req.hash_tool_epoch <= req.previous_hash_tool_epoch { self.rejected_total += 1; return Err(FileToolsHashToolError::NonMonotonicHashToolEpoch); } if req.oracle_digest == 0 { self.rejected_total += 1; return Err(FileToolsHashToolError::ZeroOracleDigest); } let expected_binding = compute_file_tools_hash_tool_binding_digest( req.hashed_files_count, req.extent_spans_verified, req.oracle_matches, EXPECTED_SHORT_IO_FALSE_SUCCESSES, req.hash_tool_epoch, req.oracle_digest, ); if req.hash_tool_binding_digest != expected_binding { self.rejected_total += 1; self.extent_oracle_refusals += 1; return Err(FileToolsHashToolError::UnboundFileToolsHashToolDigest); } self.committed = true; self.committed_total += 1; let receipt = FileToolsHashToolReceipt { hashed_files_count: req.hashed_files_count, extent_spans_verified: req.extent_spans_verified, oracle_matches: req.oracle_matches, short_io_false_successes: EXPECTED_SHORT_IO_FALSE_SUCCESSES, independent_oracle_matched: true, hash_tool_epoch: req.hash_tool_epoch, oracle_digest: req.oracle_digest, receipt_digest: expected_binding ^ 0x2320_ACE0_4841_5348, }; self.committed_receipt = Some(receipt); Ok(receipt) } }