rite-model 0.7.0

Domain model and intermediate representation for the Rite key ceremony DSL
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
//! The published JSON Schemas for the transcript and the bundle index.
//!
//! The schemas are generated from the types that read and write the files,
//! and committed under `docs/schema/`. A test fails when the committed file
//! differs from the generated one; run it with `RITE_UPDATE_SCHEMA=1` to
//! write the new schema instead.
//!
//! The schemas are public, so their descriptions are written for readers of
//! the files, apart from the Rust doc comments: each type, variant and field
//! carries its own `schemars(description = ...)`. The line types below exist
//! only for the schema, so their doc comments are that text.
//!
//! A schema checks the shape of a line. It does not check what makes a
//! transcript evidence: that each leaf and chain value recomputes, that the
//! fact is written in canonical form, that each level is declared in the
//! header. `rite verify` checks those.

use std::path::PathBuf;
use std::sync::OnceLock;

use schemars::generate::SchemaSettings;
use schemars::transform::{Transform, transform_subschemas};
use schemars::{JsonSchema, Schema};
use serde::Deserialize;
use serde_json::Value;

use crate::Sha256Digest;
use crate::bundle::{BUNDLE_FORMAT, BUNDLE_SCHEMA, BundleIndex};
use crate::transcript::{
    FACT_TYPES, FACT_VOCABULARY, Level, StepFact, TRANSCRIPT_FORMAT, TRANSCRIPT_SCHEMA,
    TranscriptHeader,
};

#[derive(Deserialize, JsonSchema)]
#[serde(untagged)]
#[allow(dead_code)]
#[schemars(
    description = "One line of a transcript.jsonl file. The first line is the header; every other \
    line records one fact, either complete or withheld. Each line is a JSON object on its own \
    line, and the lines are chained: each line's chain value commits to the line and to every line \
    before it."
)]
enum TranscriptLine {
    Header(HeaderLine),
    Complete(CompleteLine),
    Withheld(WithheldLine),
}

#[derive(Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
#[allow(dead_code)]
#[schemars(
    description = "The first line of a transcript: what the file is, and the start of the chain."
)]
struct HeaderLine {
    #[serde(rename = "$schema")]
    #[schemars(
        description = "The URL of the JSON Schema of the release that wrote the transcript, for \
        editors and other tools. The chain does not commit to it, and a reader ignores it.",
        extend("format" = "uri")
    )]
    schema: Option<String>,
    header: TranscriptHeader,
    #[schemars(
        description = "The first chain value: SHA-256 over the byte 0x02 followed by the header in \
        RFC 8785 canonical JSON."
    )]
    chain: Sha256Digest,
}

#[derive(Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
#[allow(dead_code)]
#[schemars(
    description = "A fact line with its content: the fact, the salt that hides it, and the values \
    that commit to both."
)]
struct CompleteLine {
    at: At,
    level: Level,
    #[schemars(
        description = "The commitment to the fact: SHA-256 over the byte 0x00, the 16 salt bytes, \
        and the fact in RFC 8785 canonical JSON."
    )]
    leaf: Sha256Digest,
    #[schemars(
        description = "The chain value after this line: SHA-256 over the byte 0x01, the previous \
        chain value, the length of `at` as an 8-byte big-endian integer, `at` in UTF-8, the level \
        as an 8-byte big-endian integer, and the leaf. The chain value of the last line is the \
        transcript's fingerprint."
    )]
    chain: Sha256Digest,
    #[schemars(regex(pattern = r"^[0-9a-f]{32}$"))]
    #[schemars(
        description = "16 random bytes, written as 32 lowercase hex digits. The leaf hashes the \
        decoded bytes. The salt keeps a withheld fact from being guessed from its leaf."
    )]
    salt: String,
    fact: StepFact,
}

#[derive(Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
#[allow(dead_code)]
#[schemars(
    description = "A fact line whose fact is withheld from a disclosure. It keeps the time, the \
    level, the leaf and the chain value, so it folds into the chain exactly as the complete line \
    does, and the transcript keeps its fingerprint."
)]
struct WithheldLine {
    at: At,
    level: Level,
    #[schemars(description = "The commitment to the withheld fact.")]
    leaf: Sha256Digest,
    #[schemars(description = "The chain value after this line.")]
    chain: Sha256Digest,
}

#[derive(Deserialize, JsonSchema)]
#[schemars(extend(
    "format" = "date-time",
    "pattern" = r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}Z$"
))]
#[allow(dead_code)]
#[schemars(
    description = "When the fact was recorded: an RFC 3339 time in UTC with six fractional digits. \
    The chain commits to this exact string."
)]
struct At(String);

/// Bounds every integer to `±(2^53 - 1)`, the range the canonical form
/// writes, or to its Rust type's range where that is narrower. Covers an
/// optional integer, whose type is `["integer", "null"]`.
#[derive(Clone)]
struct SafeIntegers;

const MAX_SAFE_INTEGER: i64 = (1 << 53) - 1;

impl Transform for SafeIntegers {
    fn transform(&mut self, schema: &mut Schema) {
        let integer = match schema.get("type") {
            Some(Value::String(kind)) => kind == "integer",
            Some(Value::Array(kinds)) => kinds.iter().any(|kind| kind == "integer"),
            _ => false,
        };
        if integer {
            // `uint32` and friends describe a Rust type, not the wire; only
            // the bound they imply is kept.
            let (low, high) = match schema.remove("format").as_ref().and_then(Value::as_str) {
                Some("uint8") => (0, i64::from(u8::MAX)),
                Some("uint16") => (0, i64::from(u16::MAX)),
                Some("uint32") => (0, i64::from(u32::MAX)),
                Some("int8") => (i64::from(i8::MIN), i64::from(i8::MAX)),
                Some("int16") => (i64::from(i16::MIN), i64::from(i16::MAX)),
                Some("int32") => (i64::from(i32::MIN), i64::from(i32::MAX)),
                _ => (-MAX_SAFE_INTEGER, MAX_SAFE_INTEGER),
            };
            let minimum = schema
                .get("minimum")
                .and_then(Value::as_i64)
                .map_or(low, |min| min.max(low));
            schema.insert("minimum".to_string(), minimum.into());
            schema.insert("maximum".to_string(), high.into());
        }
        transform_subschemas(self, schema);
    }
}

fn schema_of<T: JsonSchema>() -> Schema {
    SchemaSettings::draft2020_12()
        .with_transform(SafeIntegers)
        .into_generator()
        .into_root_schema_for::<T>()
}

/// The schema describes one format version: pin the version fields to it.
fn pin(schema: &mut Schema, pointer: &str, value: u32) {
    schema
        .pointer_mut(pointer)
        .and_then(Value::as_object_mut)
        .unwrap_or_else(|| panic!("no {pointer} in the schema"))
        .insert("const".to_string(), value.into());
}

/// Name the schema: its `$id`, where the release publishes it, and a title.
/// Both go first, after `$schema`, where a reader looks for them.
fn identify(schema: Schema, id: &str, title: &str) -> Schema {
    let Value::Object(generated) = schema.to_value() else {
        panic!("a root schema is an object");
    };
    let mut named = serde_json::Map::new();
    for (key, value) in generated {
        if key == "title" {
            continue;
        }
        let first = key == "$schema";
        named.insert(key, value);
        if first {
            named.insert("$id".to_string(), id.into());
            named.insert("title".to_string(), title.into());
        }
    }
    Schema::try_from(Value::Object(named)).expect("still a schema")
}

fn transcript_schema() -> Schema {
    let mut schema = identify(
        schema_of::<TranscriptLine>(),
        TRANSCRIPT_SCHEMA,
        &format!("Rite transcript line (format {TRANSCRIPT_FORMAT}, vocabulary {FACT_VOCABULARY})"),
    );
    let header = "/$defs/TranscriptHeader/properties";
    pin(
        &mut schema,
        &format!("{header}/rite_transcript"),
        TRANSCRIPT_FORMAT,
    );
    pin(
        &mut schema,
        &format!("{header}/vocabulary"),
        FACT_VOCABULARY,
    );
    schema
}

fn bundle_schema() -> Schema {
    let mut schema = identify(
        schema_of::<BundleIndex>(),
        BUNDLE_SCHEMA,
        &format!("Rite bundle index (format {BUNDLE_FORMAT})"),
    );
    pin(&mut schema, "/properties/rite_bundle", BUNDLE_FORMAT);
    schema
}

fn schema_path(file: &str) -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("../../docs/schema")
        .join(file)
}

/// Compare the committed schema with the generated one, or write it when
/// `RITE_UPDATE_SCHEMA` is set.
fn check_committed(file: &str, schema: &Schema) {
    let mut generated = serde_json::to_string_pretty(schema).expect("serialize schema");
    generated.push('\n');
    let path = schema_path(file);
    if std::env::var_os("RITE_UPDATE_SCHEMA").is_some() {
        std::fs::create_dir_all(path.parent().expect("a parent")).expect("schema dir");
        std::fs::write(&path, generated).expect("write schema");
        return;
    }
    let committed = std::fs::read_to_string(&path).unwrap_or_default();
    assert!(
        committed == generated,
        "docs/schema/{file} is not the generated schema; run \
         `RITE_UPDATE_SCHEMA=1 cargo test -p rite-model schema` and commit the result"
    );
}

fn validator(schema: &Schema) -> jsonschema::Validator {
    jsonschema::draft202012::new(schema.as_value()).expect("a valid schema")
}

fn transcript_validator() -> &'static jsonschema::Validator {
    static VALIDATOR: OnceLock<jsonschema::Validator> = OnceLock::new();
    VALIDATOR.get_or_init(|| validator(&transcript_schema()))
}

/// Check one serialized fact against the published transcript schema, as
/// the fact of a complete line.
pub(crate) fn assert_fact_matches_schema(fact: &Value) {
    let line = serde_json::json!({
        "at": "2026-09-22T10:00:00.000000Z",
        "level": 10,
        "salt": "00000000000000000000000000000000",
        "fact": fact,
        "leaf": Sha256Digest::of(b"leaf").as_str(),
        "chain": Sha256Digest::of(b"chain").as_str(),
    });
    let errors: Vec<String> = transcript_validator()
        .iter_errors(&line)
        .map(|e| e.to_string())
        .collect();
    assert!(
        errors.is_empty(),
        "fact does not match the schema: {errors:?}\n{fact}"
    );
}

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

    /// A field without its own description falls back to its doc comment,
    /// which shows as Rust link syntax or as the comment's line breaks.
    #[test]
    fn descriptions_hold_no_rust_syntax() {
        for schema in [transcript_schema(), bundle_schema()] {
            let text = serde_json::to_string(&schema).expect("serialize");
            for doc_comment in ["](", "::", "\\n"] {
                assert!(!text.contains(doc_comment), "{doc_comment} in {text}");
            }
        }
    }

    #[test]
    fn committed_schemas_are_the_generated_ones() {
        check_committed("transcript.schema.json", &transcript_schema());
        check_committed("bundle.schema.json", &bundle_schema());
    }

    /// The fact types the schema lists are the ones a reader knows.
    #[test]
    fn the_schema_lists_every_fact_type() {
        let schema = transcript_schema();
        let variants = schema
            .pointer("/$defs/StepFact/oneOf")
            .and_then(Value::as_array)
            .expect("StepFact variants");
        let mut tags: Vec<&str> = variants
            .iter()
            .filter_map(|v| v.pointer("/properties/type/const").and_then(Value::as_str))
            .collect();
        let mut known = FACT_TYPES.to_vec();
        tags.sort_unstable();
        known.sort_unstable();
        assert_eq!(tags, known);
    }

    fn demo_file(path: &str) -> String {
        let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("../../docs/demo")
            .join(path);
        std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display()))
    }

    /// The demo bundle's complete transcript, and its public disclosure,
    /// which holds withheld lines.
    #[test]
    fn every_line_of_the_demo_transcripts_matches() {
        for file in [
            "demo-bundle/transcript.jsonl",
            "demo-disclosure/transcript.jsonl",
        ] {
            for (i, line) in demo_file(file).lines().enumerate() {
                let value: Value = serde_json::from_str(line).expect("json");
                assert!(
                    transcript_validator().is_valid(&value),
                    "{file} line {} does not match the schema",
                    i + 1
                );
            }
        }
    }

    #[test]
    fn the_demo_bundle_indexes_match() {
        let validator = validator(&bundle_schema());
        for file in ["demo-bundle/bundle.json", "demo-disclosure/bundle.json"] {
            let value: Value = serde_json::from_str(&demo_file(file)).expect("json");
            assert!(validator.is_valid(&value), "{file}");
        }
    }

    #[test]
    fn a_withheld_line_matches_and_a_mixed_one_does_not() {
        let withheld = serde_json::json!({
            "at": "2026-09-22T10:00:00.000000Z",
            "level": 30,
            "leaf": Sha256Digest::of(b"leaf").as_str(),
            "chain": Sha256Digest::of(b"chain").as_str(),
        });
        assert!(transcript_validator().is_valid(&withheld));

        let mut salt_only = withheld;
        salt_only
            .as_object_mut()
            .expect("an object")
            .insert("salt".to_string(), "00".repeat(16).into());
        assert!(!transcript_validator().is_valid(&salt_only));
    }

    #[test]
    fn integers_beyond_the_canonical_range_do_not_match() {
        let line = serde_json::json!({
            "at": "2026-09-22T10:00:00.000000Z",
            "level": 1_u64 << 53,
            "leaf": Sha256Digest::of(b"leaf").as_str(),
            "chain": Sha256Digest::of(b"chain").as_str(),
        });
        assert!(!transcript_validator().is_valid(&line));
    }

    #[test]
    fn bundle_indexes_match() {
        let validator = validator(&bundle_schema());
        for index in [
            BundleIndex::complete(Sha256Digest::of(b"t"), vec![BundleFile::transcript()]),
            BundleIndex::disclosure(Sha256Digest::of(b"t"), Level::PUBLIC, vec![]),
        ] {
            let value = serde_json::to_value(&index).expect("serialize");
            assert!(validator.is_valid(&value), "{value}");
        }
    }
}