libverify-core 0.7.2

Platform-agnostic SDLC verification engine — evidence model, controls, assessment
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//! Release integrity verification predicates.
//!
//! Pure functions amenable to formal verification with Creusot.
//! The critical decision logic is proven correct in the `gh-verify-verif` crate.

use crate::verdict::Severity;

/// Truncate a SHA to 7 characters for display.
pub fn short_sha(sha: &str) -> &str {
    if sha.len() >= 7 { &sha[..7] } else { sha }
}

// --- Creusot-verifiable core predicates ---
//
// These pure functions operate on primitive types only, making them
// directly verifiable by Creusot's SMT backend. Controls in the
// `controls/` module delegate to these predicates, ensuring the
// critical logic is proven correct.

/// Core predicate for the four-eyes principle.
/// An approver is independent iff they are neither a commit author nor the change request author.
///
/// Verified by Creusot in `gh-verify-verif` crate.
pub fn is_approver_independent(is_commit_author: bool, is_pr_author: bool) -> bool {
    !is_commit_author && !is_pr_author
}

/// Core predicate for signature verification result severity.
/// Verified by Creusot in `gh-verify-verif` crate.
pub fn signature_severity(unsigned_count: usize) -> Severity {
    if unsigned_count == 0 {
        Severity::Pass
    } else {
        Severity::Error
    }
}

/// Core predicate for build provenance verification severity.
/// Zero unverified attestations → Pass, any unverified → Error.
pub fn build_provenance_severity(unverified_count: usize) -> Severity {
    if unverified_count == 0 {
        Severity::Pass
    } else {
        Severity::Error
    }
}

/// Core predicate for required status checks severity.
/// Pass iff zero check runs have a failing conclusion.
///
/// Verified by Creusot in `gh-verify-verif` crate.
pub fn required_status_checks_severity(fail_count: usize) -> Severity {
    if fail_count == 0 {
        Severity::Pass
    } else {
        Severity::Error
    }
}

/// Core predicate for branch history integrity severity (Source L2).
/// Zero merge commits (linear history) -> Pass, any merge commits -> Error.
///
/// Verified by Creusot in `gh-verify-verif` crate.
pub fn branch_history_severity(unprotected_count: usize) -> Severity {
    if unprotected_count == 0 {
        Severity::Pass
    } else {
        Severity::Error
    }
}

/// Core predicate for technical enforcement severity (Source L3).
/// Zero violations (CI passed + independent review) -> Pass, any violations -> Error.
///
/// Verified by Creusot in `gh-verify-verif` crate.
pub fn branch_protection_enforcement_severity(non_enforced_count: usize) -> Severity {
    if non_enforced_count == 0 {
        Severity::Pass
    } else {
        Severity::Error
    }
}

/// Core predicate for two-party review severity (Source L4).
/// At least 2 independent approvers -> Pass, fewer -> Error.
///
/// Verified by Creusot in `gh-verify-verif` crate.
pub fn two_party_review_severity(independent_count: usize) -> Severity {
    if independent_count >= 2 {
        Severity::Pass
    } else {
        Severity::Error
    }
}

/// Core predicate for hosted build platform severity (Build L2).
/// Zero non-hosted builds -> Pass, any non-hosted -> Error.
///
/// Verified by Creusot in `gh-verify-verif` crate.
pub fn hosted_build_severity(non_hosted_count: usize) -> Severity {
    if non_hosted_count == 0 {
        Severity::Pass
    } else {
        Severity::Error
    }
}

/// Core predicate for provenance authenticity severity (Build L2).
/// Zero unauthenticated attestations -> Pass, any unauthenticated -> Error.
///
/// Verified by Creusot in `gh-verify-verif` crate.
pub fn provenance_authenticity_severity(unauthenticated_count: usize) -> Severity {
    if unauthenticated_count == 0 {
        Severity::Pass
    } else {
        Severity::Error
    }
}

/// Core predicate for build isolation severity (Build L3).
/// Zero non-isolated builds -> Pass, any non-isolated -> Error.
///
/// Verified by Creusot in `gh-verify-verif` crate.
pub fn build_isolation_severity(non_isolated_count: usize) -> Severity {
    if non_isolated_count == 0 {
        Severity::Pass
    } else {
        Severity::Error
    }
}

/// Core predicate for dependency signature verification severity.
/// Zero unverified dependencies -> Pass, any unverified -> Error.
///
/// Verified by Creusot in `libverify-verif` crate.
pub fn dependency_signature_severity(unsigned_count: usize) -> Severity {
    if unsigned_count == 0 {
        Severity::Pass
    } else {
        Severity::Error
    }
}

// --- Compliance control predicates ---

/// Core predicate for stale review severity (CC7.2).
/// Zero stale approvals -> Pass, any stale -> Error.
///
/// Verified by Creusot in `gh-verify-verif` crate.
pub fn stale_review_severity(stale_count: usize) -> Severity {
    if stale_count == 0 {
        Severity::Pass
    } else {
        Severity::Error
    }
}

/// Core predicate for description quality severity (CC8.1).
/// Body length >= minimum -> Pass, otherwise -> Error.
///
/// Verified by Creusot in `gh-verify-verif` crate.
pub fn description_quality_severity(body_length: usize, min_length: usize) -> Severity {
    if body_length >= min_length {
        Severity::Pass
    } else {
        Severity::Error
    }
}

/// Core predicate for merge commit policy severity (CC8.1).
/// Zero merge commits -> Pass, any merge commits -> Error.
///
/// Verified by Creusot in `gh-verify-verif` crate.
pub fn merge_commit_policy_severity(merge_count: usize) -> Severity {
    if merge_count == 0 {
        Severity::Pass
    } else {
        Severity::Error
    }
}

/// Core predicate for conventional title severity (CC8.1).
/// Title is conventional -> Pass, otherwise -> Error.
///
/// Verified by Creusot in `gh-verify-verif` crate.
pub fn conventional_title_severity(is_conventional: bool) -> Severity {
    if is_conventional {
        Severity::Pass
    } else {
        Severity::Error
    }
}

/// Core predicate for security file change severity (CC7.2).
/// Zero sensitive files changed -> Pass, any sensitive -> Error.
///
/// Verified by Creusot in `gh-verify-verif` crate.
pub fn security_file_change_severity(sensitive_count: usize) -> Severity {
    if sensitive_count == 0 {
        Severity::Pass
    } else {
        Severity::Error
    }
}

/// Core predicate for release traceability severity (CC7.1).
/// At least one linked change request -> Pass, none -> Error.
///
/// Verified by Creusot in `gh-verify-verif` crate.
pub fn release_traceability_severity(linked_cr_count: usize) -> Severity {
    if linked_cr_count > 0 {
        Severity::Pass
    } else {
        Severity::Error
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // --- verif↔core equivalence tests (exhaustive for boolean predicates) ---

    #[test]
    fn is_approver_independent_exhaustive_equivalence() {
        // Exhaustive truth table: both inputs are bool, so 4 combinations
        for &ca in &[false, true] {
            for &pa in &[false, true] {
                let result = is_approver_independent(ca, pa);
                let spec = !ca && !pa;
                assert_eq!(
                    result, spec,
                    "is_approver_independent({ca}, {pa}): got {result}, spec {spec}"
                );
            }
        }
    }

    #[test]
    fn signature_severity_equivalence() {
        assert_eq!(signature_severity(0), Severity::Pass);
        for count in 1..=100 {
            assert_eq!(
                signature_severity(count),
                Severity::Error,
                "signature_severity({count}) should be Error"
            );
        }
    }

    #[test]
    fn build_provenance_severity_equivalence() {
        assert_eq!(build_provenance_severity(0), Severity::Pass);
        for count in 1..=100 {
            assert_eq!(
                build_provenance_severity(count),
                Severity::Error,
                "build_provenance_severity({count}) should be Error"
            );
        }
    }

    /// Exhaustive equivalence for classify_scope against Creusot spec.
    #[test]
    fn classify_scope_exhaustive_equivalence() {
        use crate::scope::classify_scope;
        for files in 0..=20usize {
            for comps in 0..=20usize {
                let result = classify_scope(files, comps);
                let spec = if files <= 1 {
                    Severity::Pass
                } else if comps <= 1 {
                    Severity::Pass
                } else if comps == 2 {
                    Severity::Warning
                } else {
                    Severity::Error
                };
                assert_eq!(
                    result, spec,
                    "classify_scope({files}, {comps}): got {result:?}, spec {spec:?}"
                );
            }
        }
    }

    #[test]
    fn required_status_checks_severity_equivalence() {
        assert_eq!(required_status_checks_severity(0), Severity::Pass);
        for count in 1..=10 {
            assert_eq!(
                required_status_checks_severity(count),
                Severity::Error,
                "required_status_checks_severity({count}) should be Error"
            );
        }
    }

    #[test]
    fn branch_history_severity_equivalence() {
        assert_eq!(branch_history_severity(0), Severity::Pass);
        for count in 1..=10 {
            assert_eq!(
                branch_history_severity(count),
                Severity::Error,
                "branch_history_severity({count}) should be Error"
            );
        }
    }

    #[test]
    fn branch_protection_enforcement_severity_equivalence() {
        assert_eq!(branch_protection_enforcement_severity(0), Severity::Pass);
        for count in 1..=10 {
            assert_eq!(
                branch_protection_enforcement_severity(count),
                Severity::Error,
                "branch_protection_enforcement_severity({count}) should be Error"
            );
        }
    }

    #[test]
    fn two_party_review_severity_equivalence() {
        assert_eq!(two_party_review_severity(0), Severity::Error);
        assert_eq!(two_party_review_severity(1), Severity::Error);
        assert_eq!(two_party_review_severity(2), Severity::Pass);
        for count in 3..=10 {
            assert_eq!(
                two_party_review_severity(count),
                Severity::Pass,
                "two_party_review_severity({count}) should be Pass"
            );
        }
    }

    #[test]
    fn hosted_build_severity_equivalence() {
        assert_eq!(hosted_build_severity(0), Severity::Pass);
        for count in 1..=10 {
            assert_eq!(
                hosted_build_severity(count),
                Severity::Error,
                "hosted_build_severity({count}) should be Error"
            );
        }
    }

    #[test]
    fn provenance_authenticity_severity_equivalence() {
        assert_eq!(provenance_authenticity_severity(0), Severity::Pass);
        for count in 1..=10 {
            assert_eq!(
                provenance_authenticity_severity(count),
                Severity::Error,
                "provenance_authenticity_severity({count}) should be Error"
            );
        }
    }

    #[test]
    fn build_isolation_severity_equivalence() {
        assert_eq!(build_isolation_severity(0), Severity::Pass);
        for count in 1..=10 {
            assert_eq!(
                build_isolation_severity(count),
                Severity::Error,
                "build_isolation_severity({count}) should be Error"
            );
        }
    }

    // --- Compliance predicate equivalence tests ---

    #[test]
    fn stale_review_severity_equivalence() {
        assert_eq!(stale_review_severity(0), Severity::Pass);
        for count in 1..=10 {
            assert_eq!(
                stale_review_severity(count),
                Severity::Error,
                "stale_review_severity({count}) should be Error"
            );
        }
    }

    #[test]
    fn description_quality_severity_equivalence() {
        let min = 10;
        for len in 0..min {
            assert_eq!(
                description_quality_severity(len, min),
                Severity::Error,
                "description_quality_severity({len}, {min}) should be Error"
            );
        }
        for len in min..=50 {
            assert_eq!(
                description_quality_severity(len, min),
                Severity::Pass,
                "description_quality_severity({len}, {min}) should be Pass"
            );
        }
    }

    #[test]
    fn merge_commit_policy_severity_equivalence() {
        assert_eq!(merge_commit_policy_severity(0), Severity::Pass);
        for count in 1..=10 {
            assert_eq!(
                merge_commit_policy_severity(count),
                Severity::Error,
                "merge_commit_policy_severity({count}) should be Error"
            );
        }
    }

    #[test]
    fn conventional_title_severity_equivalence() {
        assert_eq!(conventional_title_severity(true), Severity::Pass);
        assert_eq!(conventional_title_severity(false), Severity::Error);
    }

    #[test]
    fn security_file_change_severity_equivalence() {
        assert_eq!(security_file_change_severity(0), Severity::Pass);
        for count in 1..=10 {
            assert_eq!(
                security_file_change_severity(count),
                Severity::Error,
                "security_file_change_severity({count}) should be Error"
            );
        }
    }

    #[test]
    fn release_traceability_severity_equivalence() {
        assert_eq!(release_traceability_severity(0), Severity::Error);
        for count in 1..=10 {
            assert_eq!(
                release_traceability_severity(count),
                Severity::Pass,
                "release_traceability_severity({count}) should be Pass"
            );
        }
    }

    #[test]
    fn dependency_signature_severity_equivalence() {
        assert_eq!(dependency_signature_severity(0), Severity::Pass);
        for count in 1..=100 {
            assert_eq!(
                dependency_signature_severity(count),
                Severity::Error,
                "dependency_signature_severity({count}) should be Error"
            );
        }
    }
}