zenkey-model 0.20.0

zk2 keys and contracts without a Zenoh session: the key grammar, the contract authoring format, canonical form, fingerprints and bundles
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
//! A sample's bytes as its data item (§2.6, text 0.2): strict JSON, and
//! CBOR with its bignums, each into a [`Datum`] whose integers are exact.
//!
//! | §2.6 | Here |
//! |---|---|
//! | a JSON sample is strict JSON (F-143): UTF-8, RFC 8259's grammar, nothing after the value but whitespace, no duplicate member at any depth, no lone surrogate escape, every number finite | [`json`]: anything else is `None`, the sample `undecodable` |
//! | an integer's text is read exactly | `Datum::Int` within i128; beyond it, within the double's finite range, `Datum::BigInt` (its nearest double), which no range of §2.6 admits |
//! | a float's text is the nearest double (round to nearest, ties to even) | `str::parse::<f64>`, correctly rounded; an infinite one is not strict JSON |
//! | a CBOR integer: major types 0 and 1, or a bignum, tag 2 or 3 around a byte string, of any length, definite or not (F-144) | [`cbor`]: `Datum::Int`, or `Datum::BigInt` beyond i128 |
//! | every other CBOR tag, and a tag 2 or 3 around anything but a byte string, is read as its content | [`cbor`] |
//! | a CBOR map is read as core §5.2 reads the envelope's: text keys, no duplicate; `undefined` is `null` | [`cbor`]: anything else is `None` |
//! | a CBOR float keeps NaN and the infinities | `Datum::Float`, which reads `not_finite` |
//!
//! | a data item nests at most 256 arrays and maps deep (0.3): an array or a map at the top is at depth 1, one inside another one deeper; a tag or a scalar adds none | both readers count the same: an array or a map at depth [`MAX_DEPTH`] + 1 is `None`, the sample `undecodable`. A chain of tags is read without recursion |
//!
//! No Point is anywhere near the depth.

use std::collections::BTreeMap;

use super::Datum;

/// How deep an array or an object (a map) may nest (§2.6, 0.3): the top
/// one at depth 1, one inside another one deeper.
pub const MAX_DEPTH: usize = 256;

/// One JSON text as a strict JSON data item, or `None` when the bytes are
/// not one.
#[must_use]
pub fn json(bytes: &[u8]) -> Option<Datum> {
    let text = std::str::from_utf8(bytes).ok()?;
    let mut p = Json {
        s: text.as_bytes(),
        text,
        at: 0,
    };
    p.ws();
    let v = p.value(0)?;
    p.ws();
    (p.at == p.s.len()).then_some(v)
}

struct Json<'a> {
    s: &'a [u8],
    text: &'a str,
    at: usize,
}

impl Json<'_> {
    fn ws(&mut self) {
        while let Some(b' ' | b'\t' | b'\n' | b'\r') = self.s.get(self.at) {
            self.at += 1;
        }
    }

    fn peek(&self) -> Option<u8> {
        self.s.get(self.at).copied()
    }

    fn eat(&mut self, b: u8) -> Option<()> {
        (self.peek()? == b).then(|| self.at += 1)
    }

    fn literal(&mut self, word: &str, v: Datum) -> Option<Datum> {
        self.s[self.at..]
            .starts_with(word.as_bytes())
            .then(|| self.at += word.len())?;
        Some(v)
    }

    fn value(&mut self, depth: usize) -> Option<Datum> {
        match self.peek()? {
            b'{' => self.object(depth + 1),
            b'[' => self.array(depth + 1),
            b'"' => self.string().map(Datum::Text),
            b't' => self.literal("true", Datum::Bool(true)),
            b'f' => self.literal("false", Datum::Bool(false)),
            b'n' => self.literal("null", Datum::Null),
            b'-' | b'0'..=b'9' => self.number(),
            _ => None,
        }
    }

    fn object(&mut self, depth: usize) -> Option<Datum> {
        if depth > MAX_DEPTH {
            return None;
        }
        self.eat(b'{')?;
        let mut m = BTreeMap::new();
        self.ws();
        if self.eat(b'}').is_some() {
            return Some(Datum::Map(m));
        }
        loop {
            self.ws();
            let k = self.string()?;
            self.ws();
            self.eat(b':')?;
            self.ws();
            let v = self.value(depth)?;
            if m.insert(k, v).is_some() {
                return None;
            }
            self.ws();
            if self.eat(b',').is_none() {
                self.eat(b'}')?;
                return Some(Datum::Map(m));
            }
        }
    }

    fn array(&mut self, depth: usize) -> Option<Datum> {
        if depth > MAX_DEPTH {
            return None;
        }
        self.eat(b'[')?;
        let mut a = Vec::new();
        self.ws();
        if self.eat(b']').is_some() {
            return Some(Datum::Array(a));
        }
        loop {
            self.ws();
            a.push(self.value(depth)?);
            self.ws();
            if self.eat(b',').is_none() {
                self.eat(b']')?;
                return Some(Datum::Array(a));
            }
        }
    }

    fn hex4(&mut self) -> Option<u32> {
        let h = self.text.get(self.at..self.at + 4)?;
        if !h.bytes().all(|b| b.is_ascii_hexdigit()) {
            return None;
        }
        self.at += 4;
        u32::from_str_radix(h, 16).ok()
    }

    /// A string, its escapes read; `None` for a control character, a bad
    /// escape, or a lone surrogate.
    fn string(&mut self) -> Option<String> {
        self.eat(b'"')?;
        let mut out = String::new();
        loop {
            let c = self.text[self.at..].chars().next()?;
            self.at += c.len_utf8();
            match c {
                '"' => return Some(out),
                '\u{0}'..='\u{1f}' => return None,
                '\\' => {
                    let e = self.peek()?;
                    self.at += 1;
                    out.push(match e {
                        b'"' => '"',
                        b'\\' => '\\',
                        b'/' => '/',
                        b'b' => '\u{8}',
                        b'f' => '\u{c}',
                        b'n' => '\n',
                        b'r' => '\r',
                        b't' => '\t',
                        b'u' => {
                            let hi = self.hex4()?;
                            let cp = match hi {
                                0xd800..=0xdbff => {
                                    self.eat(b'\\')?;
                                    self.eat(b'u')?;
                                    let lo = self.hex4()?;
                                    if !(0xdc00..=0xdfff).contains(&lo) {
                                        return None;
                                    }
                                    0x10000 + ((hi - 0xd800) << 10) + (lo - 0xdc00)
                                }
                                0xdc00..=0xdfff => return None,
                                cp => cp,
                            };
                            char::from_u32(cp)?
                        }
                        _ => return None,
                    });
                }
                c => out.push(c),
            }
        }
    }

    /// A number, by RFC 8259's grammar: an integer's text exactly, any other
    /// as its nearest double; `None` when it is not finite.
    fn number(&mut self) -> Option<Datum> {
        let start = self.at;
        self.eat(b'-');
        match self.peek()? {
            b'0' => self.at += 1,
            b'1'..=b'9' => self.digits(),
            _ => return None,
        }
        let mut integer = true;
        if self.eat(b'.').is_some() {
            integer = false;
            if !self.peek()?.is_ascii_digit() {
                return None;
            }
            self.digits();
        }
        if let Some(b'e' | b'E') = self.peek() {
            integer = false;
            self.at += 1;
            if let Some(b'+' | b'-') = self.peek() {
                self.at += 1;
            }
            if !self.peek()?.is_ascii_digit() {
                return None;
            }
            self.digits();
        }
        let t = &self.text[start..self.at];
        if integer && let Ok(i) = t.parse::<i128>() {
            return Some(Datum::Int(i));
        }
        let d = t.parse::<f64>().ok().filter(|d| d.is_finite())?;
        Some(if integer {
            Datum::BigInt(d)
        } else {
            Datum::Float(d)
        })
    }

    fn digits(&mut self) {
        while self.peek().is_some_and(|b| b.is_ascii_digit()) {
            self.at += 1;
        }
    }
}

/// The nearest double to the integer whose big-endian magnitude is `m`,
/// rounding to nearest, ties to even; infinite past the double's range.
fn magnitude_f64(m: &[u8]) -> f64 {
    let m = &m[m.iter().position(|b| *b != 0).unwrap_or(m.len())..];
    let bits = m.len() * 8 - m.first().map_or(8, |b| b.leading_zeros() as usize);
    if bits <= 128 {
        let mut v: u128 = 0;
        for b in m {
            v = (v << 8) | u128::from(*b);
        }
        // `as` rounds to nearest, ties to even.
        return v as f64;
    }
    if bits > 1100 {
        return f64::INFINITY;
    }
    // The top 128 bits, the rest folded into their lowest as a sticky bit:
    // one rounding, then an exact scaling by a power of two.
    let shift = bits - 128;
    let bit = |i: usize| {
        // bit i, counted from the least significant
        let byte = m[m.len() - 1 - i / 8];
        (byte >> (i % 8)) & 1 == 1
    };
    let mut top: u128 = 0;
    for i in (shift..bits).rev() {
        top = (top << 1) | u128::from(bit(i));
    }
    if (0..shift).any(bit) {
        top |= 1;
    }
    let scale = i32::try_from(shift).unwrap_or(i32::MAX);
    (top as f64) * 2f64.powi(scale)
}

/// A bignum (RFC 8949 §3.4.3): tag 2 for `n`, tag 3 for `−1 − n`, `n` the
/// byte string's big-endian magnitude.
pub(crate) fn bignum_of(negative: bool, m: &[u8]) -> Datum {
    let first = m.iter().position(|b| *b != 0).unwrap_or(m.len());
    let m = &m[first..];
    if m.len() <= 16 {
        let mut n: u128 = 0;
        for b in m {
            n = (n << 8) | u128::from(*b);
        }
        let v = if negative {
            i128::try_from(n).ok().map(|n| -1 - n)
        } else {
            i128::try_from(n).ok()
        };
        if let Some(v) = v {
            return Datum::Int(v);
        }
    }
    if negative {
        // −1 − n is −(n + 1).
        let mut up = m.to_vec();
        let mut carry = true;
        for b in up.iter_mut().rev() {
            let (v, c) = b.overflowing_add(u8::from(carry));
            *b = v;
            carry = c;
            if !carry {
                break;
            }
        }
        if carry {
            up.insert(0, 1);
        }
        Datum::BigInt(-magnitude_f64(&up))
    } else {
        Datum::BigInt(magnitude_f64(m))
    }
}

/// One CBOR data item and nothing after it, as a datum, or `None` when the
/// bytes are not one this profile reads (the module doc's table).
#[must_use]
pub fn cbor(bytes: &[u8]) -> Option<Datum> {
    let mut d = ciborium_ll::Decoder::from(bytes);
    let v = cbor_item(&mut d, 0)?;
    (d.offset() == bytes.len()).then_some(v)
}

fn segments_bytes(d: &mut ciborium_ll::Decoder<&[u8]>, len: Option<usize>) -> Option<Vec<u8>> {
    let mut out = Vec::new();
    let mut buf = [0u8; 4096];
    let mut segs = d.bytes(len);
    while let Some(mut seg) = segs.pull().ok()? {
        while let Some(chunk) = seg.pull(&mut buf).ok()? {
            out.extend_from_slice(chunk);
        }
    }
    Some(out)
}

fn segments_text(d: &mut ciborium_ll::Decoder<&[u8]>, len: Option<usize>) -> Option<String> {
    let mut out = String::new();
    let mut buf = [0u8; 4096];
    let mut segs = d.text(len);
    while let Some(mut seg) = segs.pull().ok()? {
        while let Some(chunk) = seg.pull(&mut buf).ok()? {
            out.push_str(chunk);
        }
    }
    Some(out)
}

/// One item, `depth` the arrays and maps around it (§2.6, 0.3): an array or
/// a map is one deeper, a tag none.
fn cbor_item(d: &mut ciborium_ll::Decoder<&[u8]>, depth: usize) -> Option<Datum> {
    use ciborium_ll::{Header, simple};
    Some(match d.pull().ok()? {
        Header::Positive(n) => Datum::Int(i128::from(n)),
        Header::Negative(n) => Datum::Int(-1 - i128::from(n)),
        Header::Float(f) => Datum::Float(f),
        Header::Simple(simple::FALSE) => Datum::Bool(false),
        Header::Simple(simple::TRUE) => Datum::Bool(true),
        Header::Simple(simple::NULL | simple::UNDEFINED) => Datum::Null,
        Header::Simple(_) | Header::Break => return None,
        Header::Bytes(len) => Datum::Bytes(segments_bytes(d, len)?),
        Header::Text(len) => Datum::Text(segments_text(d, len)?),
        Header::Tag(first) => {
            // A tag is read as its content, at its depth; a chain of tags,
            // one after another, without recursion. The innermost decides
            // a bignum: a tag 2 or 3 right around a byte string.
            let mut tag = first;
            loop {
                match d.pull().ok()? {
                    Header::Tag(t) => tag = t,
                    Header::Bytes(len) if tag == 2 || tag == 3 => {
                        break bignum_of(tag == 3, &segments_bytes(d, len)?);
                    }
                    h => {
                        d.push(h);
                        break cbor_item(d, depth)?;
                    }
                }
            }
        }
        Header::Array(len) => {
            let depth = depth + 1;
            if depth > MAX_DEPTH {
                return None;
            }
            let mut a = Vec::new();
            match len {
                Some(n) => {
                    for _ in 0..n {
                        a.push(cbor_item(d, depth)?);
                    }
                }
                None => loop {
                    match d.pull().ok()? {
                        Header::Break => break,
                        h => {
                            d.push(h);
                            a.push(cbor_item(d, depth)?);
                        }
                    }
                },
            }
            Datum::Array(a)
        }
        Header::Map(len) => {
            let depth = depth + 1;
            if depth > MAX_DEPTH {
                return None;
            }
            let mut m = BTreeMap::new();
            let mut entry = |d: &mut ciborium_ll::Decoder<&[u8]>| -> Option<()> {
                let Datum::Text(k) = cbor_item(d, depth)? else {
                    return None;
                };
                let v = cbor_item(d, depth)?;
                m.insert(k, v).is_none().then_some(())
            };
            match len {
                Some(n) => {
                    for _ in 0..n {
                        entry(d)?;
                    }
                }
                None => loop {
                    match d.pull().ok()? {
                        Header::Break => break,
                        h => {
                            d.push(h);
                            entry(d)?;
                        }
                    }
                },
            }
            Datum::Map(m)
        }
    })
}

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

    fn hex(h: &str) -> Vec<u8> {
        (0..h.len())
            .step_by(2)
            .map(|i| u8::from_str_radix(&h[i..i + 2], 16).unwrap())
            .collect()
    }

    #[test]
    fn json_is_strict() {
        assert_eq!(
            json(br#" {"a": [1, -0, 2.5, true, null, "\u00e9\ud83d\ude00"]} "#),
            {
                let a = Datum::Array(vec![
                    Datum::Int(1),
                    Datum::Int(0),
                    Datum::Float(2.5),
                    Datum::Bool(true),
                    Datum::Null,
                    Datum::Text("é😀".to_owned()),
                ]);
                Some(Datum::Map([("a".to_owned(), a)].into()))
            }
        );
        for bad in [
            &br#"{"a": 1, "a": 2}"#[..],
            br#"{"x": [{"a": 1, "a": 1}]}"#,
            b"1e400",
            br#""\ud800""#,
            br#""\udc00x""#,
            b"1 x",
            b"",
            b"01",
            b"1.",
            b"+1",
            b"[1,]",
            b"\"a\tb\"",
            b"\xff",
            b"NaN",
        ] {
            assert_eq!(json(bad), None, "{}", String::from_utf8_lossy(bad));
        }
        let big = format!("1{}", "0".repeat(400));
        assert_eq!(json(big.as_bytes()), None, "10^400 is past the double");
        assert_eq!(
            json(b"-9223372036854775809"),
            Some(Datum::Int(-9_223_372_036_854_775_809)),
            "exactly"
        );
        let e = format!("1{}", "0".repeat(40));
        assert_eq!(json(e.as_bytes()), Some(Datum::BigInt(1e40)));
        let deep = format!("{}{}", "[".repeat(MAX_DEPTH + 1), "]".repeat(MAX_DEPTH + 1));
        assert_eq!(json(deep.as_bytes()), None);
    }

    /// Depth (0.3): an array or a map at the top is at depth 1, one inside
    /// another one deeper, a tag or a scalar none — in JSON as in CBOR. A
    /// chain of tags is read without recursion.
    #[test]
    fn both_readers_count_depth_alike() {
        for n in [MAX_DEPTH, MAX_DEPTH + 1] {
            let j = format!("{}{}", "[".repeat(n), "]".repeat(n));
            let mut c = vec![0x81_u8; n - 1];
            c.push(0x80);
            let decodes = n <= MAX_DEPTH;
            assert_eq!(json(j.as_bytes()).is_some(), decodes, "JSON, {n} deep");
            assert_eq!(cbor(&c).is_some(), decodes, "CBOR, {n} deep");
            let m = format!("{}1{}", r#"{"a":"#.repeat(n), "}".repeat(n));
            assert_eq!(json(m.as_bytes()).is_some(), decodes, "JSON maps, {n} deep");
        }
        let mut scalar = vec![0x81_u8; MAX_DEPTH];
        scalar.push(0x01);
        assert!(cbor(&scalar).is_some(), "a scalar adds no depth");
        let mut tagged = vec![0xd9, 0xd9, 0xf7];
        tagged.extend(vec![0x81_u8; MAX_DEPTH - 1]);
        tagged.push(0x80);
        assert!(cbor(&tagged).is_some(), "a tag adds no depth");
        let mut chain: Vec<u8> = std::iter::repeat_n([0xd8_u8, 0x20], 100_000)
            .flatten()
            .collect();
        chain.push(0x05);
        assert_eq!(cbor(&chain), Some(Datum::Int(5)), "no recursion");
    }

    #[test]
    fn cbor_reads_bignums_as_integers() {
        // 2^64: tag 2 around 01 00…00 (9 bytes).
        assert_eq!(
            cbor(&hex("c249010000000000000000")),
            Some(Datum::Int(1 << 64))
        );
        assert_eq!(cbor(&hex("c24105")), Some(Datum::Int(5)), "a small one");
        assert_eq!(cbor(&hex("c3420100")), Some(Datum::Int(-257)), "tag 3");
        // Indefinite: 01, then 00.
        assert_eq!(cbor(&hex("c25f41014100ff")), Some(Datum::Int(256)));
        // 17 bytes: 2^128, beyond i128.
        let long = format!("c251{}{}", "01", "00".repeat(16));
        assert_eq!(cbor(&hex(&long)), Some(Datum::BigInt(2f64.powi(128))));
        // 2^127 in 16 bytes is beyond i128 too.
        let edge = format!("c250{}{}", "80", "00".repeat(15));
        assert_eq!(cbor(&hex(&edge)), Some(Datum::BigInt(2f64.powi(127))));
        // tag 1 around an integer: the integer.
        assert_eq!(cbor(&hex("c11a5a5a5a5a")), Some(Datum::Int(0x5a5a_5a5a)));
        // A duplicate key, a key that is not text, trailing bytes.
        assert_eq!(cbor(&hex("a2616101616102")), None);
        assert_eq!(cbor(&hex("a10101")), None);
        assert_eq!(cbor(&hex("0101")), None);
        assert_eq!(
            cbor(&hex("f97e00")).map(|d| matches!(d, Datum::Float(f) if f.is_nan())),
            Some(true)
        );
        assert_eq!(cbor(&hex("f7")), Some(Datum::Null), "undefined is null");
    }

    #[test]
    fn a_long_magnitude_rounds_to_its_nearest_double() {
        // 2^200 + 1: the sticky bit, below every one the double keeps.
        let mut m = vec![0u8; 26];
        m[0] = 1;
        m[25] = 1;
        assert_eq!(magnitude_f64(&m), 2f64.powi(200));
        assert_eq!(magnitude_f64(&[0xff; 200]), f64::INFINITY);
        assert_eq!(magnitude_f64(&[0, 0, 7]), 7.0);
    }
}