elicitation_kani 0.11.1

Kani model-checking proofs for elicitation contract types
Documentation
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! Kani proofs for the serde deserialization boundary.
//!
//! # What these proofs verify
//!
//! The serde bridge (Phase A) adds `Deserialize` impls that call the validated
//! constructor `Type::new(v)` under the hood. These harnesses prove the
//! **consistency invariant**:
//!
//! > `serde_json::from_value(v)` succeeds **iff** `Type::new(v)` succeeds.
//!
//! In other words: deserialization never creates a value that the constructor
//! would reject, and deserialization never rejects a value that the constructor
//! would accept.
//!
//! This is critical because it means:
//! 1. Agents cannot bypass validation by crafting JSON payloads.
//! 2. The serde bridge does not silently drop or corrupt valid inputs.
//!
//! # Proof strategy
//!
//! For integer types we can use `kani::any::<T>()` for full symbolic coverage.
//! For floats we cover boundary patterns concretely (Kani's float support is
//! limited for relational comparisons).
//! For URLs we test concrete representative strings.

// ============================================================================
// Integer serde boundary proofs
// ============================================================================

use elicitation::{
    I8NonNegative, I8NonZero, I8Positive, I8Range, I16NonNegative, I16NonZero, I16Positive,
    U8NonZero, U8Positive, U16NonZero, U16Positive,
};

/// Prove: I8Positive serde and constructor agree on every symbolic i8.
#[kani::proof]
fn serde_i8_positive_consistency() {
    let v: i8 = kani::any();
    let new_result = I8Positive::new(v);
    let json = serde_json::Value::Number(v.into());
    let serde_result: Result<I8Positive, _> = serde_json::from_value(json);
    // They must agree: both succeed or both fail
    assert_eq!(new_result.is_ok(), serde_result.is_ok());
}

/// Prove: if I8Positive deserialization succeeds, the invariant (> 0) holds.
#[kani::proof]
fn serde_i8_positive_invariant() {
    let v: i8 = kani::any();
    let json = serde_json::Value::Number(v.into());
    if let Ok(pos) = serde_json::from_value::<I8Positive>(json) {
        assert!(
            pos.get() > 0,
            "I8Positive invariant must hold after deserialization"
        );
    }
}

/// Prove: I8NonNegative serde and constructor agree on every symbolic i8.
#[kani::proof]
fn serde_i8_non_negative_consistency() {
    let v: i8 = kani::any();
    let new_result = I8NonNegative::new(v);
    let json = serde_json::Value::Number(v.into());
    let serde_result: Result<I8NonNegative, _> = serde_json::from_value(json);
    assert_eq!(new_result.is_ok(), serde_result.is_ok());
}

/// Prove: if I8NonNegative deserialization succeeds, invariant (>= 0) holds.
#[kani::proof]
fn serde_i8_non_negative_invariant() {
    let v: i8 = kani::any();
    let json = serde_json::Value::Number(v.into());
    if let Ok(nn) = serde_json::from_value::<I8NonNegative>(json) {
        assert!(
            nn.get() >= 0,
            "I8NonNegative invariant must hold after deserialization"
        );
    }
}

/// Prove: I8NonZero serde and constructor agree on every symbolic i8.
#[kani::proof]
fn serde_i8_non_zero_consistency() {
    let v: i8 = kani::any();
    let new_result = I8NonZero::new(v);
    let json = serde_json::Value::Number(v.into());
    let serde_result: Result<I8NonZero, _> = serde_json::from_value(json);
    assert_eq!(new_result.is_ok(), serde_result.is_ok());
}

/// Prove: if I8NonZero deserialization succeeds, invariant (!= 0) holds.
#[kani::proof]
fn serde_i8_non_zero_invariant() {
    let v: i8 = kani::any();
    let json = serde_json::Value::Number(v.into());
    if let Ok(nz) = serde_json::from_value::<I8NonZero>(json) {
        assert!(
            nz.get() != 0,
            "I8NonZero invariant must hold after deserialization"
        );
    }
}

/// Prove: I16Positive serde and constructor agree on every symbolic i16.
#[kani::proof]
fn serde_i16_positive_consistency() {
    let v: i16 = kani::any();
    let new_result = I16Positive::new(v);
    let json = serde_json::Value::Number(v.into());
    let serde_result: Result<I16Positive, _> = serde_json::from_value(json);
    assert_eq!(new_result.is_ok(), serde_result.is_ok());
}

/// Prove: I16NonNegative serde and constructor agree.
#[kani::proof]
fn serde_i16_non_negative_consistency() {
    let v: i16 = kani::any();
    let new_result = I16NonNegative::new(v);
    let json = serde_json::Value::Number(v.into());
    let serde_result: Result<I16NonNegative, _> = serde_json::from_value(json);
    assert_eq!(new_result.is_ok(), serde_result.is_ok());
}

/// Prove: I16NonZero serde and constructor agree.
#[kani::proof]
fn serde_i16_non_zero_consistency() {
    let v: i16 = kani::any();
    let new_result = I16NonZero::new(v);
    let json = serde_json::Value::Number(v.into());
    let serde_result: Result<I16NonZero, _> = serde_json::from_value(json);
    assert_eq!(new_result.is_ok(), serde_result.is_ok());
}

/// Prove: U8Positive serde and constructor agree on every symbolic u8.
#[kani::proof]
fn serde_u8_positive_consistency() {
    let v: u8 = kani::any();
    let new_result = U8Positive::new(v);
    let json = serde_json::Value::Number(v.into());
    let serde_result: Result<U8Positive, _> = serde_json::from_value(json);
    assert_eq!(new_result.is_ok(), serde_result.is_ok());
}

/// Prove: U8NonZero serde and constructor agree on every symbolic u8.
#[kani::proof]
fn serde_u8_non_zero_consistency() {
    let v: u8 = kani::any();
    let new_result = U8NonZero::new(v);
    let json = serde_json::Value::Number(v.into());
    let serde_result: Result<U8NonZero, _> = serde_json::from_value(json);
    assert_eq!(new_result.is_ok(), serde_result.is_ok());
}

/// Prove: U16Positive serde and constructor agree on every symbolic u16.
#[kani::proof]
fn serde_u16_positive_consistency() {
    let v: u16 = kani::any();
    let new_result = U16Positive::new(v);
    let json = serde_json::Value::Number(v.into());
    let serde_result: Result<U16Positive, _> = serde_json::from_value(json);
    assert_eq!(new_result.is_ok(), serde_result.is_ok());
}

/// Prove: U16NonZero serde and constructor agree on every symbolic u16.
#[kani::proof]
fn serde_u16_non_zero_consistency() {
    let v: u16 = kani::any();
    let new_result = U16NonZero::new(v);
    let json = serde_json::Value::Number(v.into());
    let serde_result: Result<U16NonZero, _> = serde_json::from_value(json);
    assert_eq!(new_result.is_ok(), serde_result.is_ok());
}

// ============================================================================
// Integer range serde boundary proofs
// ============================================================================

/// Prove: I8Range<0,100> serde and constructor agree on every symbolic i8.
#[kani::proof]
fn serde_i8_range_consistency() {
    let v: i8 = kani::any();
    let new_result = I8Range::<0, 100>::new(v);
    let json = serde_json::Value::Number(v.into());
    let serde_result: Result<I8Range<0, 100>, _> = serde_json::from_value(json);
    assert_eq!(new_result.is_ok(), serde_result.is_ok());
}

/// Prove: if I8Range<0,100> deserialization succeeds, value is in [0,100].
#[kani::proof]
fn serde_i8_range_invariant() {
    let v: i8 = kani::any();
    let json = serde_json::Value::Number(v.into());
    if let Ok(r) = serde_json::from_value::<I8Range<0, 100>>(json) {
        assert!(
            r.get() >= 0 && r.get() <= 100,
            "I8Range<0,100> invariant must hold after deserialization"
        );
    }
}

// ============================================================================
// Float serde boundary proofs (concrete — Kani float comparison limitations)
// ============================================================================

use elicitation::{F64Finite, F64NonNegative, F64Positive};

/// Prove: F64Positive serde invariant with concrete boundary values.
///
/// Uses concrete inputs instead of kani::any::<f64>() to avoid
/// Kani's symbolic float limitations with relational comparisons.
#[kani::proof]
fn serde_f64_positive_boundary_values() {
    // Positive: should succeed
    for v in [1.0_f64, 0.001, f64::MAX, f64::MIN_POSITIVE] {
        let json = serde_json::json!(v);
        assert!(
            serde_json::from_value::<F64Positive>(json).is_ok(),
            "F64Positive should accept positive value {v}"
        );
    }
    // Non-positive: should fail
    for v in [0.0_f64, -1.0, -f64::MIN_POSITIVE] {
        let json = serde_json::json!(v);
        assert!(
            serde_json::from_value::<F64Positive>(json).is_err(),
            "F64Positive should reject non-positive value {v}"
        );
    }
}

/// Prove: F64NonNegative serde invariant with concrete boundary values.
#[kani::proof]
fn serde_f64_non_negative_boundary_values() {
    // Non-negative: should succeed
    for v in [0.0_f64, 1.0, f64::MAX] {
        let json = serde_json::json!(v);
        assert!(
            serde_json::from_value::<F64NonNegative>(json).is_ok(),
            "F64NonNegative should accept {v}"
        );
    }
    // Negative: should fail
    for v in [-0.001_f64, -1.0, f64::NEG_INFINITY] {
        let json = serde_json::json!(v);
        // NEG_INFINITY is not a valid JSON number, so it becomes null
        // only test finite negatives as strict failures
        if v.is_finite() {
            assert!(
                serde_json::from_value::<F64NonNegative>(json).is_err(),
                "F64NonNegative should reject {v}"
            );
        }
    }
}

/// Prove: F64Finite rejects NaN and infinity representations.
///
/// JSON has no NaN/Infinity literals, but the constructor is the proof site.
#[kani::proof]
fn serde_f64_finite_accepts_finite() {
    for v in [0.0_f64, 1.0, -1.0, f64::MAX, f64::MIN] {
        let json = serde_json::json!(v);
        assert!(
            serde_json::from_value::<F64Finite>(json).is_ok(),
            "F64Finite should accept finite value {v}"
        );
    }
}

// ============================================================================
// String serde boundary proofs
// ============================================================================

use elicitation::StringNonEmpty;

/// Prove: StringNonEmpty rejects empty string, accepts non-empty.
#[kani::proof]
fn serde_string_non_empty_boundary() {
    // Should fail
    let json = serde_json::json!("");
    assert!(
        serde_json::from_value::<StringNonEmpty<4096>>(json).is_err(),
        "StringNonEmpty should reject empty string"
    );

    // Should succeed
    let json = serde_json::json!("hello");
    assert!(
        serde_json::from_value::<StringNonEmpty<4096>>(json).is_ok(),
        "StringNonEmpty should accept non-empty string"
    );

    // Single char — boundary
    let json = serde_json::json!("x");
    assert!(
        serde_json::from_value::<StringNonEmpty<4096>>(json).is_ok(),
        "StringNonEmpty should accept single-char string"
    );
}

// ============================================================================
// URL serde boundary proofs (concrete strings — URL parsing is opaque to Kani)
// ============================================================================

#[cfg(feature = "url")]
use elicitation::{UrlHttp, UrlHttps, UrlValid, UrlWithHost};

/// Prove: UrlHttps accepts HTTPS, rejects HTTP and invalid.
#[cfg(feature = "url")]
#[kani::proof]
fn serde_url_https_boundary() {
    // Valid HTTPS — should succeed
    let json = serde_json::json!("https://example.com");
    assert!(
        serde_json::from_value::<UrlHttps>(json).is_ok(),
        "UrlHttps should accept https://example.com"
    );

    // HTTP — should fail (not HTTPS)
    let json = serde_json::json!("http://example.com");
    assert!(
        serde_json::from_value::<UrlHttps>(json).is_err(),
        "UrlHttps should reject http://example.com"
    );

    // Invalid URL — should fail
    let json = serde_json::json!("not-a-url");
    assert!(
        serde_json::from_value::<UrlHttps>(json).is_err(),
        "UrlHttps should reject not-a-url"
    );

    // FTP — should fail
    let json = serde_json::json!("ftp://files.example.com");
    assert!(
        serde_json::from_value::<UrlHttps>(json).is_err(),
        "UrlHttps should reject ftp://files.example.com"
    );
}

/// Prove: UrlHttps serde is consistent with UrlHttps::new.
#[cfg(feature = "url")]
#[kani::proof]
fn serde_url_https_constructor_consistency() {
    // For each concrete test string, serde and constructor must agree
    let cases: &[&str] = &[
        "https://example.com",
        "http://example.com",
        "https://",
        "not-a-url",
        "ftp://files.example.com",
        "https://user:pass@host.example.com/path?q=1#frag",
    ];
    for s in cases {
        let new_result = UrlHttps::new(s);
        let json = serde_json::json!(s);
        let serde_result: Result<UrlHttps, _> = serde_json::from_value(json);
        assert_eq!(
            new_result.is_ok(),
            serde_result.is_ok(),
            "serde and constructor must agree for '{s}'"
        );
    }
}

/// Prove: UrlHttp accepts HTTP, rejects HTTPS.
#[cfg(feature = "url")]
#[kani::proof]
fn serde_url_http_boundary() {
    let json = serde_json::json!("http://example.com");
    assert!(serde_json::from_value::<UrlHttp>(json).is_ok());

    let json = serde_json::json!("https://example.com");
    assert!(serde_json::from_value::<UrlHttp>(json).is_err());
}

/// Prove: UrlValid accepts any valid URL, rejects invalid.
#[cfg(feature = "url")]
#[kani::proof]
fn serde_url_valid_boundary() {
    // Valid URLs of various schemes
    for s in [
        "https://example.com",
        "http://example.com",
        "ftp://files.example.com",
    ] {
        let json = serde_json::json!(s);
        assert!(
            serde_json::from_value::<UrlValid>(json).is_ok(),
            "UrlValid should accept '{s}'"
        );
    }

    // Invalid
    let json = serde_json::json!("not a url");
    assert!(
        serde_json::from_value::<UrlValid>(json).is_err(),
        "UrlValid should reject 'not a url'"
    );
}

/// Prove: UrlWithHost requires a host component.
#[cfg(feature = "url")]
#[kani::proof]
fn serde_url_with_host_boundary() {
    let json = serde_json::json!("https://example.com");
    assert!(serde_json::from_value::<UrlWithHost>(json).is_ok());
}

// ============================================================================
// Round-trip proofs: serialize then deserialize preserves value
// ============================================================================

/// Prove: I8Positive round-trip preserves the value.
#[kani::proof]
fn serde_i8_positive_round_trip() {
    let v: i8 = kani::any();
    if let Ok(pos) = I8Positive::new(v) {
        let json = serde_json::to_value(&pos).unwrap();
        let pos2: I8Positive = serde_json::from_value(json).unwrap();
        assert_eq!(pos.get(), pos2.get(), "Round-trip must preserve value");
    }
}

/// Prove: I8Range<0,100> round-trip preserves the value.
#[kani::proof]
fn serde_i8_range_round_trip() {
    let v: i8 = kani::any();
    if let Ok(r) = I8Range::<0, 100>::new(v) {
        let json = serde_json::to_value(&r).unwrap();
        let r2: I8Range<0, 100> = serde_json::from_value(json).unwrap();
        assert_eq!(r.get(), r2.get(), "Round-trip must preserve value");
    }
}