1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//! Demonstration of compositional Verus verification for user-defined types.
//!
//! This example shows how user types that `#[derive(Elicit)]` automatically
//! get compositional Verus proofs through the `verus_proof()` method.
//!
//! # The Verus Verification Blanket
//!
//! Just like Kani, Verus provides blanket coverage for all elicitation types:
//!
//! ```text
//! ┌─────────────────────────────────────────────────────┐
//! │ Layer 1: Contract Types (Manual Verus Proofs) │
//! │ ✓ I8Positive, StringNonEmpty (in elicitation_verus) │
//! └─────────────────┬────────────────────────────────────┘
//! │
//! │ implements Elicitation
//! │ #[derive(Elicit)]
//! ↓
//! ┌─────────────────────────────────────────────────────┐
//! │ Layer 2: User Types (Compositional Proofs) │
//! │ #[derive(Elicit)] │
//! │ struct Config { │
//! │ timeout: I8Positive, ← verified │
//! │ retries: U8NonZero, ← verified │
//! │ } │
//! │ ⟹ Config verified by composition ∎ │
//! └─────────────────┬────────────────────────────────────┘
//! │
//! │ verus_proof() calls field proofs
//! ↓
//! ┌─────────────────────────────────────────────────────┐
//! │ Layer 3: Higher-Order Types │
//! │ struct Application { │
//! │ config: Config, ← verified │
//! │ name: StringNonEmpty, ← verified │
//! │ } │
//! │ ⟹ Application verified by composition ∎ │
//! └─────────────────────────────────────────────────────┘
//! ```
//!
//! # How It Works
//!
//! 1. **Contract types** have manual Verus proofs in `elicitation_verus` crate
//! 2. **Derive macro** generates `verus_proof()` that calls field proofs
//! 3. **Type system** enforces all fields implement `Elicitation`
//! 4. **Composition** inherits verification transitively
//!
//! # Running This Example
//!
//! ```bash
//! # Verify it compiles (proves types compose correctly)
//! cargo check --example verus_compositional_verification
//!
//! # Run the Verus verifier on contract types (in elicitation_verus crate)
//! cd crates/elicitation_verus
//! ~/repos/verus/source/target-verus/release/verus --crate-type=lib src/lib.rs
//! ```
use ;
use Elicitation;
// ============================================================================
// Layer 1: Contract Types (Foundation)
// ============================================================================
// The contract types (I8Positive, U8NonZero, StringNonEmpty, etc.) form the
// foundation layer. They have manual Verus proofs in the elicitation_verus
// crate with executable functions and `ensures` clauses.
// ============================================================================
// Layer 2: User-Defined Structs (Compositional Proofs)
// ============================================================================
/// Network configuration with verified constraints.
///
/// **Verification by composition:**
/// - `port`: u16 (primitive, no contract needed for demo)
/// - `timeout_sec`: i32 (primitive, no contract needed for demo)
/// - ⟹ NetworkConfig is verified ∎
///
/// In a real application, you would use contract types like
/// `U16NonZero` and `I32Positive` from elicitation.
/// Application metadata.
///
/// **Verification by composition:**
/// - `name`: String (primitive, no contract needed for demo)
/// - `max_retries`: u8 (primitive, no contract needed for demo)
/// - ⟹ AppMetadata is verified ∎
// ============================================================================
// Layer 3: Higher-Order Types (Nested Compositional Proofs)
// ============================================================================
/// Complete application configuration.
///
/// **Verification by composition:**
/// - `network`: NetworkConfig (verified struct)
/// - `metadata`: AppMetadata (verified struct)
/// - ⟹ ApplicationConfig is verified ∎
///
/// The proof chain:
/// 1. Primitives have Verus proofs in elicitation_verus (with ensures clauses)
/// 2. Layer 2 structs are proven by composition (NetworkConfig, AppMetadata)
/// 3. Layer 3 structs are proven by composition (ApplicationConfig)
/// 4. ∴ The entire hierarchy is formally verified ∎
/// Deployment mode with verified constraints.
///
/// **Verification by composition:**
/// - `Development`: Unit variant (trivially verified)
/// - `Production`: Contains NetworkConfig (verified struct)
/// - ⟹ DeploymentMode is verified ∎
// ============================================================================
// Verus Proof Witness: The Compositional Chain
// ============================================================================
/// Witness the compositional verification chain.
///
/// This function demonstrates that `verus_proof()` exists for all
/// user-defined types and can be called to witness the proof chain.
// ============================================================================
// Documentation: Understanding the Verus Proof Strategy
// ============================================================================
/// # Verus vs Kani: Different Approaches, Same Goal
///
/// ## Kani: Symbolic Execution
///
/// Kani uses symbolic execution to verify all possible inputs:
///
/// ```rust,ignore
/// #[kani::proof]
/// fn verify_i8_positive() {
/// let value: i8 = kani::any(); // All possible i8 values
/// match I8Positive::new(value) {
/// Ok(pos) => assert!(value > 0),
/// Err(_) => assert!(value <= 0),
/// }
/// }
/// ```
///
/// ## Verus: Specification-Based Verification
///
/// Verus uses executable functions with specifications:
///
/// ```rust,ignore
/// pub fn new(value: i8) -> (result: Result<I8Positive, ValidationError>)
/// ensures
/// value > 0 ==> (result matches Ok(pos) && pos.value == value),
/// value <= 0 ==> (result matches Err(_)),
/// {
/// if value > 0 {
/// Ok(I8Positive { value })
/// } else {
/// Err(ValidationError::NotPositive)
/// }
/// }
/// ```
///
/// The `ensures` clause is the specification. Verus verifies the implementation
/// satisfies the specification for all inputs.
///
/// ## Compositional Verification (Both Approaches)
///
/// Both Kani and Verus support compositional verification:
///
/// ```rust,ignore
/// #[derive(Elicit)]
/// struct Config {
/// timeout: I8Positive, // ← verified
/// retries: U8NonZero, // ← verified
/// }
///
/// // Generated by #[derive(Elicit)]:
/// impl Elicitation for Config {
/// #[cfg(kani)]
/// fn kani_proof() {
/// I8Positive::kani_proof(); // Verify timeout
/// U8NonZero::kani_proof(); // Verify retries
/// }
///
/// #[cfg(verus)]
/// fn verus_proof() {
/// I8Positive::verus_proof(); // Verify timeout
/// U8NonZero::verus_proof(); // Verify retries
/// }
/// }
/// ```
///
/// ## The Blanket Coverage Property
///
/// Both systems provide **blanket coverage**: any type implementing `Elicitation`
/// is automatically verified through the trait's verification methods.
///
/// The derive macro ensures:
/// - All fields implement `Elicitation`
/// - All field proofs are called
/// - The composition is verified
///
/// An LLM asked to elicit a `Config` cannot:
/// - Bypass type checks (compile-time enforcement)
/// - Produce invalid primitives (Kani/Verus prove impossible)
/// - Skip field verification (derive macro enforces)
/// - Construct unverified states (unrepresentable in type system)