Skip to main content

sail_api/
contracts.rs

1//! Version-one neutral source job inputs and evidence, without backend execution.
2use serde::{Deserialize, Serialize};
3
4/// Neutral contract job protocol version.
5pub const VERSION: u32 = 1;
6/// Maximum complete source file size.
7pub const MAX_SOURCE_BYTES: usize = 64 * 1024;
8/// Fixed whole-process safety deadline for a neutral worker.
9pub const WALL_TIME_SECS: u64 = 300;
10/// Maximum retained exact worker stdout.
11pub const MAX_STDOUT_BYTES: usize = 24 * 1024 * 1024;
12/// Maximum retained exact worker stderr.
13pub const MAX_STDERR_BYTES: usize = 8 * 1024 * 1024;
14
15/// Complete caller input; execution controls belong exclusively to Sail.
16#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
17#[serde(deny_unknown_fields)]
18pub struct Submission {
19    /// Explicit neutral source profile; omitted legacy submissions select profile one.
20    #[serde(default = "legacy_profile", skip_serializing_if = "is_legacy_profile")]
21    pub profile: u32,
22    /// Exact UTF-8 original source snapshot.
23    pub source: String,
24    /// Required SHA-256 of the exact source bytes.
25    pub source_sha256: String,
26    /// Rust compiler crate identity, not a filesystem path.
27    pub crate_name: String,
28}
29const fn legacy_profile() -> u32 {
30    1
31}
32#[allow(
33    clippy::trivially_copy_pass_by_ref,
34    reason = "Serde skip_serializing_if callbacks borrow the field."
35)]
36fn is_legacy_profile(profile: &u32) -> bool {
37    *profile == 1
38}
39
40impl Submission {
41    /// Validate bounded source, exact content identity and a conservative crate name.
42    /// # Errors
43    /// Returns a source admission diagnostic without starting any process.
44    pub fn validate(&self) -> Result<(), String> {
45        let expected = sail_key::QueryKey(sail_key::sha256(self.source.as_bytes())).to_string();
46        if !matches!(self.profile, 1 | 2)
47            || self.source.is_empty()
48            || self.source.len() > MAX_SOURCE_BYTES
49            || self.source_sha256 != expected
50            || self.crate_name.is_empty()
51            || self.crate_name.len() > 64
52            || !self
53                .crate_name
54                .bytes()
55                .enumerate()
56                .all(|(i, b)| b == b'_' || b.is_ascii_alphabetic() || (i > 0 && b.is_ascii_digit()))
57        {
58            return Err("SAIL3142: invalid neutral source, source digest or crate identity".into());
59        }
60        Ok(())
61    }
62}
63
64/// An original source range. Columns are zero-based, lines are one-based.
65#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
66pub struct SourceRange {
67    /// First source line.
68    pub line: usize,
69    /// First source column.
70    pub column: usize,
71    /// Exclusive final source line.
72    pub end_line: usize,
73    /// Exclusive final source column.
74    pub end_column: usize,
75}
76
77/// A requirement-to-guarantee binding, before actual backend obligation mapping.
78#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
79pub struct ContractBinding {
80    /// Stable explicit owner requirement identity.
81    pub requirement_id: String,
82    /// Original fully qualified function identity.
83    pub function: String,
84    /// Exact generated Verus symbol; evidence must actually name this symbol.
85    pub lowered_function: String,
86    /// Hash of the exact parsed guarantee syntax in its canonical token rendering.
87    pub guarantee_sha256: String,
88    /// Original complete function range.
89    pub source: SourceRange,
90}
91
92/// Content-bound, deterministic original-to-shadow mapping.
93#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
94pub struct LoweringManifest {
95    /// Lowering semantics version.
96    pub version: u32,
97    /// Neutral contract syntax version.
98    pub contract_version: u32,
99    /// Validated compiler crate name.
100    pub crate_name: String,
101    /// Exact original UTF-8 source digest.
102    pub original_sha256: String,
103    /// Exact emitted Verus source digest.
104    pub lowered_sha256: String,
105    /// Explicit supported arithmetic interpretation.
106    pub arithmetic: String,
107    /// Sorted one-guarantee function bindings; no invented solver obligation IDs.
108    pub bindings: Vec<ContractBinding>,
109}
110
111/// Whether the submitted contracts were proved under their stated preconditions.
112/// This does not establish requirement entailment, feasibility or a mutation kill.
113#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
114#[serde(rename_all = "kebab-case")]
115pub enum ProofState {
116    /// Complete attributed proof evidence for every bound function.
117    Proved,
118    /// Missing, failed, unknown, unstable or incomplete evidence; not a refutation.
119    NotProved,
120}
121
122/// Underlying check counts, including advisory obligations and dependency targets.
123#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
124pub struct CheckCounts {
125    /// Recorded unsatisfiable answers.
126    pub unsat: usize,
127    /// Recorded satisfiable answers; no source-level refutation is inferred.
128    pub sat: usize,
129    /// Recorded unknown answers, even when a legacy verdict says failed.
130    pub unknown: usize,
131    /// Checks with no recorded answer.
132    pub missing_answer: usize,
133    /// Checks with no recorded establishment source.
134    pub missing_source: usize,
135}
136
137/// Exact-function proof projection; the complete original backend report is retained.
138#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
139pub struct ContractProof {
140    /// Conservative aggregate result, with no refuted state.
141    pub state: ProofState,
142    /// Every underlying recorded check, not just bound-function checks.
143    pub checks: CheckCounts,
144    /// Sorted distinct verbatim unknown reasons; null reasons remain in the backend.
145    pub unknown_reasons: Vec<String>,
146    /// Sorted component observations of complete nonadvisory function proofs.
147    /// These are not standalone establishment or tiers when the aggregate is `NotProved`.
148    pub proved_functions: Vec<String>,
149}
150
151/// Exact source-policy and resolution evidence for neutral translation.
152#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
153pub struct TranslationTrust {
154    /// Independently qualified complete vstd and builtin macro source policy.
155    pub source_policy_sha256: String,
156    /// Actual Cargo metadata bytes used to admit all selected dependency features.
157    pub metadata_sha256: String,
158    /// Exact dependency lockfile used by the managed verification.
159    pub cargo_lock_sha256: String,
160}
161
162/// Snapshot contract evidence accompanying the original managed project result.
163#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
164pub struct Evidence {
165    /// Exact contract service format version.
166    pub version: u32,
167    /// Original source, guarantee and generated symbol relationships.
168    pub manifest: LoweringManifest,
169    /// Conservative snapshot proof; never live-source requirement establishment.
170    pub proof: ContractProof,
171    /// Actual qualified compiler-loaded source and resolution identity.
172    pub translation_trust: TranslationTrust,
173}
174
175/// Complete output of Sail's own supervised contract worker.
176#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
177pub struct WorkerResult {
178    /// Equals VERSION.
179    pub version: u32,
180    /// Original complete managed report, including failures and unknowns.
181    pub backend: crate::project::ProjectResult,
182    /// Exact qualified snapshot evidence.
183    pub contracts: Evidence,
184}