graphforge-core 0.5.1

GraphForge core types and traits
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
460
461
//! Shared `graphforge-canonical/1` envelope and byte primitives.
//!
//! Domain owners define their registered schemas and logical-value encoders.
//! This module owns the common bounded byte writer, closed fingerprint domains,
//! SHA-256 envelope, and UUIDv8 projection.

use sha2::{Digest, Sha256};

/// Frozen canonical contract version.
pub const CANONICAL_CONTRACT_VERSION: u32 = 1;
/// Maximum canonical payload size in version 1.
pub const MAX_CANONICAL_PAYLOAD_BYTES: u64 = 2_147_483_648;
/// Maximum UTF-8 value size in version 1.
pub const MAX_CANONICAL_TEXT_BYTES: u64 = 16_777_216;
/// Maximum binary value size in version 1.
pub const MAX_CANONICAL_BINARY_BYTES: u64 = 67_108_864;

/// Closed fingerprint domains in `graphforge-canonical/1`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum CanonicalDomain {
    /// Authoritative schema bytes.
    Schema,
    /// Assertion record/table.
    Assertion,
    /// Evidence-link record/table.
    EvidenceLink,
    /// Confidence-assessment record/table.
    ConfidenceAssessment,
    /// Immutable reasoning record/table.
    Reasoning,
    /// Append-only assertion-status event/table.
    AssertionStatus,
    /// Append-only assertion-validity event/table.
    AssertionValidity,
    /// Immutable assertion-supersession relation/table.
    AssertionSupersession,
    /// Immutable hypothesis-group record/table.
    HypothesisGroup,
    /// Append-only hypothesis-membership event/table.
    HypothesisMembership,
    /// Append-only hypothesis-selection event/table.
    HypothesisSelection,
    /// Ordered composite graph-mutation content.
    CompositeGraphMutationContent,
    /// Composite graph and knowledge request content.
    CompositeRequest,
    /// Provenance event/table.
    ProvenanceEvent,
    /// Lineage record/table.
    Lineage,
    /// Neutral M18 invocation descriptor.
    InvocationDescriptor,
    /// Resolved graph projection.
    GraphProjection,
    /// Resolved-belief policy bytes.
    BeliefProjectionPolicy,
    /// Append-only interpretation attachment.
    BeliefProjectionAttachment,
    /// Public Arrow result table.
    ArrowResult,
}

impl CanonicalDomain {
    /// Exact lowercase domain tag.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Schema => "graphforge/schema",
            Self::Assertion => "graphforge/assertion",
            Self::EvidenceLink => "graphforge/evidence-link",
            Self::ConfidenceAssessment => "graphforge/confidence-assessment",
            Self::Reasoning => "graphforge/reasoning",
            Self::AssertionStatus => "graphforge/assertion-status",
            Self::AssertionValidity => "graphforge/assertion-validity",
            Self::AssertionSupersession => "graphforge/assertion-supersession",
            Self::HypothesisGroup => "graphforge/hypothesis-group",
            Self::HypothesisMembership => "graphforge/hypothesis-membership",
            Self::HypothesisSelection => "graphforge/hypothesis-selection",
            Self::CompositeGraphMutationContent => "graphforge/composite-graph-mutation-content",
            Self::CompositeRequest => "graphforge/composite-request",
            Self::ProvenanceEvent => "graphforge/provenance-event",
            Self::Lineage => "graphforge/lineage",
            Self::InvocationDescriptor => "graphforge/invocation-descriptor",
            Self::GraphProjection => "graphforge/graph-projection",
            Self::BeliefProjectionPolicy => "graphforge/belief-projection-policy",
            Self::BeliefProjectionAttachment => "graphforge/belief-projection-attachment",
            Self::ArrowResult => "graphforge/arrow-result",
        }
    }
}

/// Structured canonicalization failures.
#[derive(thiserror::Error, Clone, Debug, PartialEq, Eq)]
pub enum CanonicalError {
    /// A bounded byte or item limit was exceeded.
    #[error("canonical limit exceeded for {item}: observed {observed}, limit {limit}")]
    Limit {
        /// Safe item category.
        item: &'static str,
        /// Observed byte/item count.
        observed: u64,
        /// Frozen maximum.
        limit: u64,
    },
    /// A caller requested an unsupported canonical contract version.
    #[error("unsupported canonical contract version {version}")]
    UnsupportedVersion {
        /// Unsupported version.
        version: u32,
    },
    /// Canonical bytes are truncated or structurally invalid.
    #[error("invalid canonical payload: {0}")]
    Malformed(&'static str),
}

impl CanonicalError {
    /// Stable public error code.
    #[must_use]
    pub const fn code(&self) -> &'static str {
        match self {
            Self::Limit { .. } => "GF_CANONICAL_LIMIT",
            Self::UnsupportedVersion { .. } => "GF_UNSUPPORTED_CONTRACT_VERSION",
            Self::Malformed(_) => "GF_CANONICAL_INVALID",
        }
    }
}

/// Bounded writer for the fixed-width canonical grammar.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct CanonicalWriter {
    bytes: Vec<u8>,
}

/// Strict reader for the fixed-width canonical grammar.
#[derive(Clone, Debug)]
pub struct CanonicalReader<'a> {
    bytes: &'a [u8],
    offset: usize,
}

impl<'a> CanonicalReader<'a> {
    /// Construct a reader after enforcing the top-level payload bound.
    pub fn new(bytes: &'a [u8]) -> Result<Self, CanonicalError> {
        let observed = u64::try_from(bytes.len()).unwrap_or(u64::MAX);
        if observed > MAX_CANONICAL_PAYLOAD_BYTES {
            return Err(CanonicalError::Limit {
                item: "payload",
                observed,
                limit: MAX_CANONICAL_PAYLOAD_BYTES,
            });
        }
        Ok(Self { bytes, offset: 0 })
    }

    /// Read an exact number of bytes.
    pub fn raw(&mut self, length: usize) -> Result<&'a [u8], CanonicalError> {
        let end = self
            .offset
            .checked_add(length)
            .ok_or(CanonicalError::Malformed("byte range overflow"))?;
        let value = self
            .bytes
            .get(self.offset..end)
            .ok_or(CanonicalError::Malformed("truncated payload"))?;
        self.offset = end;
        Ok(value)
    }

    /// Read one unsigned byte.
    pub fn u8(&mut self) -> Result<u8, CanonicalError> {
        Ok(self.raw(1)?[0])
    }

    /// Read one big-endian unsigned 32-bit integer.
    pub fn u32(&mut self) -> Result<u32, CanonicalError> {
        Ok(u32::from_be_bytes(
            self.raw(4)?.try_into().expect("reader returned four bytes"),
        ))
    }

    /// Read one big-endian unsigned 64-bit integer.
    pub fn u64(&mut self) -> Result<u64, CanonicalError> {
        Ok(u64::from_be_bytes(
            self.raw(8)?
                .try_into()
                .expect("reader returned eight bytes"),
        ))
    }

    /// Read one bounded length-prefixed UTF-8 value.
    pub fn text(&mut self) -> Result<&'a str, CanonicalError> {
        let length = self.u64()?;
        if length > MAX_CANONICAL_TEXT_BYTES {
            return Err(CanonicalError::Limit {
                item: "text",
                observed: length,
                limit: MAX_CANONICAL_TEXT_BYTES,
            });
        }
        let length = usize::try_from(length)
            .map_err(|_| CanonicalError::Malformed("text length exceeds usize"))?;
        std::str::from_utf8(self.raw(length)?)
            .map_err(|_| CanonicalError::Malformed("text is not valid UTF-8"))
    }

    /// Reject unconsumed trailing bytes.
    pub fn finish(self) -> Result<(), CanonicalError> {
        if self.offset == self.bytes.len() {
            Ok(())
        } else {
            Err(CanonicalError::Malformed("trailing bytes"))
        }
    }
}

impl CanonicalWriter {
    /// Construct an empty writer.
    #[must_use]
    pub const fn new() -> Self {
        Self { bytes: Vec::new() }
    }

    /// Append exact bytes while enforcing the payload bound.
    pub fn raw(&mut self, bytes: &[u8]) -> Result<(), CanonicalError> {
        let next = self
            .bytes
            .len()
            .checked_add(bytes.len())
            .and_then(|value| u64::try_from(value).ok())
            .ok_or(CanonicalError::Limit {
                item: "payload",
                observed: u64::MAX,
                limit: MAX_CANONICAL_PAYLOAD_BYTES,
            })?;
        if next > MAX_CANONICAL_PAYLOAD_BYTES {
            return Err(CanonicalError::Limit {
                item: "payload",
                observed: next,
                limit: MAX_CANONICAL_PAYLOAD_BYTES,
            });
        }
        self.bytes.extend_from_slice(bytes);
        Ok(())
    }

    /// Append one unsigned byte.
    pub fn u8(&mut self, value: u8) -> Result<(), CanonicalError> {
        self.raw(&[value])
    }

    /// Append an unsigned big-endian 16-bit integer.
    pub fn u16(&mut self, value: u16) -> Result<(), CanonicalError> {
        self.raw(&value.to_be_bytes())
    }

    /// Append an unsigned big-endian 32-bit integer.
    pub fn u32(&mut self, value: u32) -> Result<(), CanonicalError> {
        self.raw(&value.to_be_bytes())
    }

    /// Append an unsigned big-endian 64-bit integer.
    pub fn u64(&mut self, value: u64) -> Result<(), CanonicalError> {
        self.raw(&value.to_be_bytes())
    }

    /// Append a signed big-endian 64-bit integer.
    pub fn i64(&mut self, value: i64) -> Result<(), CanonicalError> {
        self.raw(&value.to_be_bytes())
    }

    /// Append a bounded binary item (`u64` length plus bytes).
    pub fn binary(&mut self, value: &[u8]) -> Result<(), CanonicalError> {
        let length = u64::try_from(value.len()).map_err(|_| CanonicalError::Limit {
            item: "binary",
            observed: u64::MAX,
            limit: MAX_CANONICAL_BINARY_BYTES,
        })?;
        if length > MAX_CANONICAL_BINARY_BYTES {
            return Err(CanonicalError::Limit {
                item: "binary",
                observed: length,
                limit: MAX_CANONICAL_BINARY_BYTES,
            });
        }
        self.u64(length)?;
        self.raw(value)
    }

    /// Append a bounded UTF-8 item (`u64` length plus exact UTF-8 bytes).
    pub fn text(&mut self, value: &str) -> Result<(), CanonicalError> {
        let length = u64::try_from(value.len()).map_err(|_| CanonicalError::Limit {
            item: "text",
            observed: u64::MAX,
            limit: MAX_CANONICAL_TEXT_BYTES,
        })?;
        if length > MAX_CANONICAL_TEXT_BYTES {
            return Err(CanonicalError::Limit {
                item: "text",
                observed: length,
                limit: MAX_CANONICAL_TEXT_BYTES,
            });
        }
        self.u64(length)?;
        self.raw(value.as_bytes())
    }

    /// Consume the writer.
    #[must_use]
    pub fn finish(self) -> Vec<u8> {
        self.bytes
    }
}

/// Build the frozen fingerprint preimage for a canonical payload.
pub fn fingerprint_preimage(
    domain: CanonicalDomain,
    contract_version: u32,
    payload: &[u8],
) -> Result<Vec<u8>, CanonicalError> {
    if contract_version != CANONICAL_CONTRACT_VERSION {
        return Err(CanonicalError::UnsupportedVersion {
            version: contract_version,
        });
    }
    let payload_length = u64::try_from(payload.len()).map_err(|_| CanonicalError::Limit {
        item: "payload",
        observed: u64::MAX,
        limit: MAX_CANONICAL_PAYLOAD_BYTES,
    })?;
    if payload_length > MAX_CANONICAL_PAYLOAD_BYTES {
        return Err(CanonicalError::Limit {
            item: "payload",
            observed: payload_length,
            limit: MAX_CANONICAL_PAYLOAD_BYTES,
        });
    }
    let domain_bytes = domain.as_str().as_bytes();
    let domain_length = u16::try_from(domain_bytes.len()).expect("closed domains fit UInt16");
    let mut writer = CanonicalWriter::new();
    writer.raw(b"GFFP")?;
    writer.u8(1)?;
    writer.u16(domain_length)?;
    writer.raw(domain_bytes)?;
    writer.u32(contract_version)?;
    writer.u64(payload_length)?;
    writer.raw(payload)?;
    Ok(writer.finish())
}

/// Compute the domain-separated SHA-256 fingerprint.
pub fn fingerprint(
    domain: CanonicalDomain,
    contract_version: u32,
    payload: &[u8],
) -> Result<[u8; 32], CanonicalError> {
    Ok(Sha256::digest(fingerprint_preimage(domain, contract_version, payload)?).into())
}

/// Project a full canonical fingerprint into an RFC 9562 UUIDv8.
#[must_use]
pub fn uuid_v8(fingerprint: [u8; 32]) -> uuid::Uuid {
    let mut bytes = [0_u8; 16];
    bytes.copy_from_slice(&fingerprint[..16]);
    bytes[6] = (bytes[6] & 0x0f) | 0x80;
    bytes[8] = (bytes[8] & 0x3f) | 0x80;
    uuid::Uuid::from_bytes(bytes)
}

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

    fn decode_hex(value: &str) -> Vec<u8> {
        value
            .as_bytes()
            .chunks_exact(2)
            .map(|pair| {
                let text = std::str::from_utf8(pair).unwrap();
                u8::from_str_radix(text, 16).unwrap()
            })
            .collect()
    }

    #[test]
    fn envelope_hash_and_uuid_match_the_frozen_vector() {
        let payload = decode_hex(
            "474654310000000000000027474653310000000100000000000000096e6f64655f757569640032000000100000000000000000000000000000000101018f47d2a1b27c3d8e4f001122334455",
        );
        let preimage = fingerprint_preimage(
            CanonicalDomain::ArrowResult,
            CANONICAL_CONTRACT_VERSION,
            &payload,
        )
        .unwrap();
        assert_eq!(
            preimage,
            decode_hex(
                "474646500100176772617068666f7267652f6172726f772d726573756c7400000001000000000000004c474654310000000000000027474653310000000100000000000000096e6f64655f757569640032000000100000000000000000000000000000000101018f47d2a1b27c3d8e4f001122334455",
            )
        );
        let digest = fingerprint(
            CanonicalDomain::ArrowResult,
            CANONICAL_CONTRACT_VERSION,
            &payload,
        )
        .unwrap();
        assert_eq!(
            digest,
            decode_hex("3cc432a310818554c11f6eb272fead2c549bcd108ab059453a641ab379c7bbb9")
                .as_slice()
        );
        assert_eq!(
            uuid_v8(digest).to_string(),
            "3cc432a3-1081-8554-811f-6eb272fead2c"
        );
    }

    #[test]
    fn writer_is_big_endian_exact_and_bounded() {
        let mut writer = CanonicalWriter::new();
        writer.u16(0x0102).unwrap();
        writer.u32(0x0304_0506).unwrap();
        writer.u64(7).unwrap();
        writer.text("é").unwrap();
        assert_eq!(
            writer.finish(),
            [
                1, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 2, 0xc3, 0xa9,
            ]
        );
    }

    #[test]
    fn unknown_versions_fail_before_hashing() {
        let error = fingerprint(CanonicalDomain::Schema, 2, b"").unwrap_err();
        assert_eq!(error.code(), "GF_UNSUPPORTED_CONTRACT_VERSION");
    }

    #[test]
    fn reader_round_trips_and_rejects_truncation_trailing_and_utf8() {
        let mut writer = CanonicalWriter::new();
        writer.u8(7).unwrap();
        writer.u32(9).unwrap();
        writer.u64(11).unwrap();
        writer.text("é").unwrap();
        let bytes = writer.finish();
        let mut reader = CanonicalReader::new(&bytes).unwrap();
        assert_eq!(reader.u8().unwrap(), 7);
        assert_eq!(reader.u32().unwrap(), 9);
        assert_eq!(reader.u64().unwrap(), 11);
        assert_eq!(reader.text().unwrap(), "é");
        reader.finish().unwrap();

        assert!(CanonicalReader::new(&[0, 0]).unwrap().u32().is_err());
        let mut trailing = CanonicalReader::new(&[1, 2]).unwrap();
        assert_eq!(trailing.u8().unwrap(), 1);
        assert!(trailing.finish().is_err());
        let mut invalid = CanonicalReader::new(&[0, 0, 0, 0, 0, 0, 0, 1, 0xff]).unwrap();
        assert!(invalid.text().is_err());
    }
}