Skip to main content

hyphae_engine/proof/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! Canonical snapshot-witness result proofs and offline verification.
4
5mod codec;
6mod model;
7mod verify;
8
9pub use model::{
10    MAX_RESULT_PROOF_BYTES, ProofAnchor, ProofError, ProvenOperation, ProvenResult,
11    RESULT_PROOF_FORMAT_VERSION, ResultProof, ResultProofArtifact, VerificationLimits,
12    VerificationReport,
13};
14pub use verify::{read_result_proof, verify_result_proof, write_result_proof};
15
16use hyphae_query::{Query, QueryResult, Record};
17use hyphae_storage::SnapshotInfo;
18
19use self::codec::{decode_proof, encode_proof, finalize_proof};
20
21impl ResultProof {
22    /// Creates a canonical proof for one exact KV lookup.
23    ///
24    /// # Errors
25    ///
26    /// Returns an error for a noncanonical key/result pair or proof bounds.
27    pub fn for_get(
28        snapshot: &SnapshotInfo,
29        key: Vec<u8>,
30        result: Option<Record>,
31    ) -> Result<Self, ProofError> {
32        finalize_proof(
33            ProofAnchor::from_snapshot(snapshot),
34            ProvenOperation::Get { key },
35            ProvenResult::Get(result),
36        )
37    }
38
39    /// Creates a canonical proof for one complete structured query result.
40    ///
41    /// # Errors
42    ///
43    /// Returns an error for unsupported lengths, document bounds, or total
44    /// proof size.
45    pub fn for_query(
46        snapshot: &SnapshotInfo,
47        query: Query,
48        result: QueryResult,
49    ) -> Result<Self, ProofError> {
50        finalize_proof(
51            ProofAnchor::from_snapshot(snapshot),
52            ProvenOperation::Query(query),
53            ProvenResult::Query(result),
54        )
55    }
56
57    /// Encodes the complete proof into canonical portable bytes.
58    ///
59    /// # Errors
60    ///
61    /// Returns an error if the proof exceeds a hard bound or contains a
62    /// noncanonical model value.
63    pub fn to_bytes(&self) -> Result<Vec<u8>, ProofError> {
64        encode_proof(self)
65    }
66
67    /// Verifies and decodes canonical proof bytes.
68    ///
69    /// # Errors
70    ///
71    /// Returns a framing, version, canonicality, bound, checksum, or digest
72    /// error.
73    pub fn from_bytes(encoded: &[u8]) -> Result<Self, ProofError> {
74        decode_proof(encoded)
75    }
76}