aver-cert 0.1.3

Independent artifact certificate engine and verifier for Aver WebAssembly
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
//! Content-addressed Lake build-output cache for artifact-specific cert DATA.
//!
//! Entries contain only `.lake` plus an integrity manifest. Source inputs are
//! always staged afresh by the verifier; `lake build` still runs on every verify,
//! and the checker witness is never cached.
//!
//! This cache is explicitly opt-in because its directory is trusted local
//! state. The integrity manifest detects accidental corruption, but a writer
//! able to replace both `.olean` outputs and Lake traces can also replace that
//! manifest. Strict verification therefore runs with no cache configured.

use std::path::{Path, PathBuf};

use sha2::{Digest, Sha256};

const CACHE_ENV: &str = "AVER_CERT_DATA_CACHE";
// Do not reuse outputs produced before Lean subprocesses had a cleared,
// exact-toolchain environment.
const CACHE_LAYOUT_VERSION: &str = "v2-hermetic-env";

pub(crate) struct KeyMaterial<'a> {
    pub schema_version: u64,
    pub pinned_sha256: &'a [(&'a str, &'a str)],
    pub toolchain_version: &'a str,
}

/// A prepared cache lookup. Cache I/O is deliberately best-effort: failure to
/// read, validate, or publish an entry leaves verification on the fresh build.
pub(crate) struct ArtifactBuildCache {
    entry: Option<PathBuf>,
    hit: bool,
}

impl ArtifactBuildCache {
    pub(crate) fn prepare(build_dir: &Path, material: &KeyMaterial<'_>) -> Self {
        let Some(store) = cache_store() else {
            return Self {
                entry: None,
                hit: false,
            };
        };
        let Ok(key) = artifact_cache_key(build_dir, material) else {
            return Self {
                entry: None,
                hit: false,
            };
        };
        let entry = store.join(CACHE_LAYOUT_VERSION).join(key);
        let hit = try_reuse(&entry, build_dir).is_ok();
        Self {
            entry: Some(entry),
            hit,
        }
    }

    pub(crate) fn was_hit(&self) -> bool {
        self.hit
    }

    /// Remove an entry that made `lake build` fail so the caller can retry from
    /// freshly staged sources (optionally seeded only by the pristine prelude).
    pub(crate) fn invalidate(&mut self, build_dir: &Path) {
        if let Some(entry) = &self.entry {
            let _ = std::fs::remove_dir_all(entry);
        }
        let _ = std::fs::remove_dir_all(build_dir.join(".lake"));
        self.hit = false;
    }

    pub(crate) fn publish(&self, build_dir: &Path) {
        if self.hit {
            return;
        }
        let Some(entry) = &self.entry else {
            return;
        };
        let _ = try_publish(entry, build_dir);
    }
}

/// Any explicit value except `0|off|false` opts into a trusted cache directory.
/// An absent variable is the strict, cache-free default.
fn cache_store() -> Option<PathBuf> {
    let value = std::env::var_os(CACHE_ENV)?;
    let text = value.to_string_lossy();
    if text.is_empty()
        || text == "0"
        || text.eq_ignore_ascii_case("off")
        || text.eq_ignore_ascii_case("false")
    {
        None
    } else {
        Some(PathBuf::from(value))
    }
}

/// Hash all inputs that can affect artifact DATA build products. The explicit
/// manifest wall is included even though the exact freshly staged source tree
/// is also hashed: schema version, artifact sha, every audited-file sha, and
/// toolchain version are visible, independently framed invalidators. The source
/// tree then covers ArtifactBytes plus every certificate/model DATA file, so a
/// DATA edit that leaves the JSON pins untouched still cannot reuse an entry.
fn artifact_cache_key(build_dir: &Path, material: &KeyMaterial<'_>) -> Result<String, ()> {
    let mut hasher = Sha256::new();
    hash_part(&mut hasher, b"layout", CACHE_LAYOUT_VERSION.as_bytes());
    hash_part(
        &mut hasher,
        b"schema_version",
        material.schema_version.to_string().as_bytes(),
    );

    let mut pins = material.pinned_sha256.to_vec();
    pins.sort_by_key(|(name, _)| *name);
    for (name, value) in pins {
        hash_part(&mut hasher, name.as_bytes(), value.as_bytes());
    }
    hash_part(
        &mut hasher,
        b"toolchain_version",
        material.toolchain_version.as_bytes(),
    );

    for (name, bytes) in staged_source_files(build_dir)? {
        hash_part(&mut hasher, name.as_bytes(), &bytes);
    }
    Ok(format!("{:x}", hasher.finalize()))
}

fn hash_part(hasher: &mut Sha256, name: &[u8], bytes: &[u8]) {
    hasher.update((name.len() as u64).to_be_bytes());
    hasher.update(name);
    hasher.update((bytes.len() as u64).to_be_bytes());
    hasher.update(bytes);
}

/// Every freshly staged source file, keyed by its `/`-normalized relative
/// name. The walk is recursive because nested model modules stage at nested
/// paths (`Apps/Notepad/Store.lean`); a key over top-level files only would
/// let two certificates differing solely in nested models share an entry and
/// reuse each other's stale `.olean` outputs. Mirrors `lake_tree_hashes`:
/// newline-carrying names and non-file, non-directory entries poison the key
/// (no cache, fresh build). Dot-directories (`.lake`) are never staged
/// sources and are skipped.
fn staged_source_files(build_dir: &Path) -> Result<Vec<(String, Vec<u8>)>, ()> {
    // The verifier caps staged nesting at 16 directory levels, so a deeper
    // tree here is out of contract; erroring poisons the key (no cache,
    // fresh build) instead of recursing without bound over a directory
    // chain the checker did not author.
    const MAX_STAGED_DEPTH: usize = 16;

    fn visit(
        root: &Path,
        dir: &Path,
        depth: usize,
        files: &mut Vec<(String, Vec<u8>)>,
    ) -> Result<(), ()> {
        if depth > MAX_STAGED_DEPTH {
            return Err(());
        }
        for entry in std::fs::read_dir(dir).map_err(|_| ())? {
            let entry = entry.map_err(|_| ())?;
            let file_type = entry.file_type().map_err(|_| ())?;
            if file_type.is_dir() {
                let name = entry.file_name();
                if name.to_str().ok_or(())?.starts_with('.') {
                    continue;
                }
                visit(root, &entry.path(), depth + 1, files)?;
            } else if file_type.is_file() {
                let path = entry.path();
                let relative = path.strip_prefix(root).map_err(|_| ())?;
                let relative = relative
                    .to_str()
                    .ok_or(())?
                    .replace(std::path::MAIN_SEPARATOR, "/");
                if relative.contains(['\n', '\r']) {
                    return Err(());
                }
                if relative == "CheckerWitness.lean" {
                    continue;
                }
                files.push((relative, std::fs::read(path).map_err(|_| ())?));
            } else {
                return Err(());
            }
        }
        Ok(())
    }

    let mut files = Vec::new();
    visit(build_dir, build_dir, 0, &mut files)?;
    files.sort_by(|a, b| a.0.cmp(&b.0));
    Ok(files)
}

fn try_reuse(entry: &Path, build_dir: &Path) -> Result<(), ()> {
    let cached_lake = entry.join(".lake");
    if !cached_lake.is_dir() {
        return Err(());
    }
    if verify_integrity(entry, &cached_lake).is_err() {
        let _ = std::fs::remove_dir_all(entry);
        return Err(());
    }

    let destination = build_dir.join(".lake");
    let _ = std::fs::remove_dir_all(&destination);
    if copy_tree(&cached_lake, &destination).is_err()
        || verify_integrity(entry, &destination).is_err()
    {
        let _ = std::fs::remove_dir_all(destination);
        let _ = std::fs::remove_dir_all(entry);
        return Err(());
    }
    Ok(())
}

fn try_publish(entry: &Path, build_dir: &Path) -> Result<(), ()> {
    let lake = build_dir.join(".lake");
    if !lake.is_dir() {
        return Err(());
    }
    let parent = entry.parent().ok_or(())?;
    std::fs::create_dir_all(parent).map_err(|_| ())?;
    let temp = parent.join(format!("tmp-{}-{}", std::process::id(), unique_nanos()));
    std::fs::create_dir(&temp).map_err(|_| ())?;
    let result = (|| {
        copy_tree(&lake, &temp.join(".lake"))?;
        write_integrity(&temp)?;
        match std::fs::rename(&temp, entry) {
            Ok(()) => Ok(()),
            Err(_) if entry.join(".lake").is_dir() => Ok(()),
            Err(_) => Err(()),
        }
    })();
    let _ = std::fs::remove_dir_all(temp);
    result
}

fn copy_tree(source: &Path, destination: &Path) -> Result<(), ()> {
    std::fs::create_dir(destination).map_err(|_| ())?;
    for entry in std::fs::read_dir(source).map_err(|_| ())? {
        let entry = entry.map_err(|_| ())?;
        let destination_entry = destination.join(entry.file_name());
        let file_type = entry.file_type().map_err(|_| ())?;
        if file_type.is_dir() {
            copy_tree(&entry.path(), &destination_entry)?;
        } else if file_type.is_file() {
            std::fs::copy(entry.path(), destination_entry).map_err(|_| ())?;
        } else {
            return Err(());
        }
    }
    Ok(())
}

fn write_integrity(entry: &Path) -> Result<(), ()> {
    let mut manifest = String::new();
    for (path, hash) in lake_tree_hashes(&entry.join(".lake"))? {
        manifest.push_str(&hash);
        manifest.push_str("  ");
        manifest.push_str(&path);
        manifest.push('\n');
    }
    std::fs::write(entry.join("manifest.sha256"), manifest).map_err(|_| ())
}

fn verify_integrity(entry: &Path, lake: &Path) -> Result<(), ()> {
    let manifest = std::fs::read_to_string(entry.join("manifest.sha256")).map_err(|_| ())?;
    let mut expected = Vec::new();
    for line in manifest.lines() {
        let (hash, path) = line.split_once("  ").ok_or(())?;
        if hash.len() != 64 || !hash.bytes().all(|byte| byte.is_ascii_hexdigit()) {
            return Err(());
        }
        let relative = Path::new(path);
        if relative.is_absolute()
            || relative
                .components()
                .any(|component| !matches!(component, std::path::Component::Normal(_)))
        {
            return Err(());
        }
        expected.push((path.to_string(), hash.to_ascii_lowercase()));
    }
    expected.sort();
    if expected != lake_tree_hashes(lake)? {
        return Err(());
    }
    Ok(())
}

fn lake_tree_hashes(root: &Path) -> Result<Vec<(String, String)>, ()> {
    fn visit(root: &Path, dir: &Path, hashes: &mut Vec<(String, String)>) -> Result<(), ()> {
        for entry in std::fs::read_dir(dir).map_err(|_| ())? {
            let entry = entry.map_err(|_| ())?;
            let file_type = entry.file_type().map_err(|_| ())?;
            if file_type.is_dir() {
                visit(root, &entry.path(), hashes)?;
            } else if file_type.is_file() {
                let path = entry.path();
                let relative = path.strip_prefix(root).map_err(|_| ())?;
                let relative = relative
                    .to_str()
                    .ok_or(())?
                    .replace(std::path::MAIN_SEPARATOR, "/");
                if relative.contains(['\n', '\r']) {
                    return Err(());
                }
                let bytes = std::fs::read(path).map_err(|_| ())?;
                hashes.push((relative, format!("{:x}", Sha256::digest(bytes))));
            } else {
                return Err(());
            }
        }
        Ok(())
    }

    let mut hashes = Vec::new();
    visit(root, root, &mut hashes)?;
    hashes.sort();
    Ok(hashes)
}

fn unique_nanos() -> u128 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|duration| duration.as_nanos())
        .unwrap_or(0)
}

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

    #[test]
    fn cache_key_invalidates_every_explicit_input_and_staged_source() {
        let dir = std::env::temp_dir().join(format!(
            "aver-cert-data-cache-key-{}-{}",
            std::process::id(),
            unique_nanos()
        ));
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(dir.join("Artifact.lean"), "def artifact := 1\n").unwrap();
        let pins = [("wasm_sha256", "aa"), ("schema_sha256", "bb")];
        let material = KeyMaterial {
            schema_version: 50,
            pinned_sha256: &pins,
            toolchain_version: "leanprover/lean4:v4.32.2",
        };
        let baseline = artifact_cache_key(&dir, &material).unwrap();

        let changed_schema = KeyMaterial {
            schema_version: 51,
            ..material
        };
        assert_ne!(baseline, artifact_cache_key(&dir, &changed_schema).unwrap());

        for index in 0..pins.len() {
            let mut changed = pins;
            changed[index].1 = "changed";
            assert_ne!(
                baseline,
                artifact_cache_key(
                    &dir,
                    &KeyMaterial {
                        pinned_sha256: &changed,
                        ..material
                    }
                )
                .unwrap()
            );
        }

        let changed_toolchain = KeyMaterial {
            toolchain_version: "leanprover/lean4:v4.33.0",
            ..material
        };
        assert_ne!(
            baseline,
            artifact_cache_key(&dir, &changed_toolchain).unwrap()
        );

        std::fs::write(dir.join("Artifact.lean"), "def artifact := 2\n").unwrap();
        let flat_edit = artifact_cache_key(&dir, &material).unwrap();
        assert_ne!(baseline, flat_edit);

        // A staged NESTED model file is part of the key: two certificates
        // differing only under a subdirectory must not share an entry.
        std::fs::create_dir_all(dir.join("Apps").join("Notepad")).unwrap();
        std::fs::write(
            dir.join("Apps").join("Notepad").join("Store.lean"),
            "def store := 1\n",
        )
        .unwrap();
        let nested_added = artifact_cache_key(&dir, &material).unwrap();
        assert_ne!(flat_edit, nested_added);
        std::fs::write(
            dir.join("Apps").join("Notepad").join("Store.lean"),
            "def store := 2\n",
        )
        .unwrap();
        assert_ne!(nested_added, artifact_cache_key(&dir, &material).unwrap());

        // A directory chain deeper than the staging contract poisons the
        // key: no cache is used rather than recursing without bound.
        let mut deep = dir.join("Deep");
        for _ in 0..20 {
            deep = deep.join("D");
        }
        std::fs::create_dir_all(&deep).unwrap();
        std::fs::write(deep.join("X.lean"), "def x := 0\n").unwrap();
        assert!(artifact_cache_key(&dir, &material).is_err());
        std::fs::remove_dir_all(dir.join("Deep")).unwrap();

        // Build products under a dot-directory never enter the key.
        std::fs::create_dir_all(dir.join(".lake")).unwrap();
        std::fs::write(dir.join(".lake").join("junk.olean"), b"junk").unwrap();
        assert_eq!(
            artifact_cache_key(&dir, &material).unwrap(),
            artifact_cache_key(&dir, &material).unwrap()
        );
        std::fs::write(
            dir.join("Apps").join("Notepad").join("Store.lean"),
            "def store := 2\n",
        )
        .unwrap();
        let with_dot_dir = artifact_cache_key(&dir, &material).unwrap();
        std::fs::remove_dir_all(dir.join(".lake")).unwrap();
        assert_eq!(with_dot_dir, artifact_cache_key(&dir, &material).unwrap());

        let _ = std::fs::remove_dir_all(dir);
    }
}