dcbor 0.25.2

Deterministic CBOR ("dCBOR") for Rust.
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
//! CBOR bignum (tags 2 and 3) support for `num-bigint` types.
//!
//! This module provides conversion between [`CBOR`] and the `num-bigint`
//! crate's [`BigInt`] and [`BigUint`] types, implementing RFC 8949 ยง3.4.3
//! (Bignums) with dCBOR/CDE canonical encoding rules.
//!
//! # Encoding
//!
//! - [`BigUint`] always encodes as tag 2 (positive bignum) with a byte string
//!   content.
//! - [`BigInt`] encodes as tag 2 for non-negative values or tag 3 (negative
//!   bignum) for negative values.
//! - No numeric reduction is performed: values are always encoded as bignums,
//!   even if they would fit in normal CBOR integers.
//!
//! # Decoding
//!
//! - Accepts CBOR integers (major types 0 and 1) and converts them to bignums.
//! - Accepts tag 2 (positive bignum) and tag 3 (negative bignum) with byte
//!   string content.
//! - Enforces shortest-form canonical representation for bignum magnitudes.
//! - Rejects floating-point values.
//!
//! # Examples
//!
//! ```
//! use dcbor::prelude::*;
//! use num_bigint::{BigInt, BigUint};
//!
//! // Encode a BigUint
//! let big = BigUint::from(256u32);
//! let cbor = CBOR::from(big.clone());
//! assert_eq!(cbor.diagnostic(), "2(h'0100')");
//!
//! // Decode back to BigUint
//! let decoded: BigUint = cbor.try_into().unwrap();
//! assert_eq!(decoded, big);
//!
//! // Encode a negative BigInt
//! let neg = BigInt::from(-1);
//! let cbor = CBOR::from(neg.clone());
//! assert_eq!(cbor.diagnostic(), "3(h'00')");
//!
//! // Decode back to BigInt
//! let decoded: BigInt = cbor.try_into().unwrap();
//! assert_eq!(decoded, neg);
//! ```

import_stdlib!();

pub use num_bigint::{BigInt, BigUint, Sign};

use crate::{
    CBOR, CBORCase, Error, Result, TAG_NEGATIVE_BIGNUM, TAG_POSITIVE_BIGNUM,
    Tag,
};

/// Validates that a bignum magnitude byte string is in shortest canonical form.
///
/// Rules:
/// - For positive bignums (tag 2): empty byte string represents zero; non-empty
///   must not have leading zero bytes.
/// - For negative bignums (tag 3): byte string must not be empty (magnitude
///   zero is encoded as `0x00`); must not have leading zero bytes except when
///   the magnitude is zero (single `0x00`).
fn validate_bignum_magnitude(bytes: &[u8], is_negative: bool) -> Result<()> {
    if is_negative {
        // Tag 3: byte string must not be empty
        if bytes.is_empty() {
            return Err(Error::NonCanonicalNumeric);
        }
        // No leading zeros unless the entire magnitude is zero (single 0x00
        // byte)
        if bytes.len() > 1 && bytes[0] == 0 {
            return Err(Error::NonCanonicalNumeric);
        }
    } else {
        // Tag 2: empty byte string is valid (represents zero)
        // Non-empty must not have leading zeros
        if !bytes.is_empty() && bytes[0] == 0 {
            return Err(Error::NonCanonicalNumeric);
        }
    }
    Ok(())
}

/// Strips leading zero bytes from a byte slice, returning the minimal
/// representation.
fn strip_leading_zeros(bytes: &[u8]) -> &[u8] {
    let start = bytes.iter().position(|&b| b != 0).unwrap_or(bytes.len());
    &bytes[start..]
}

impl From<BigUint> for CBOR {
    /// Converts a [`BigUint`] to CBOR as a tag 2 (positive bignum).
    ///
    /// The value is always encoded as a bignum regardless of size.
    /// Zero is encoded as tag 2 with an empty byte string.
    fn from(value: BigUint) -> Self {
        let bytes = value.to_bytes_be();
        let stripped = strip_leading_zeros(&bytes);
        let byte_string = CBOR::to_byte_string(stripped);
        CBOR::to_tagged_value(Tag::with_value(TAG_POSITIVE_BIGNUM), byte_string)
    }
}

impl From<&BigUint> for CBOR {
    fn from(value: &BigUint) -> Self { value.clone().into() }
}

impl From<BigInt> for CBOR {
    /// Converts a [`BigInt`] to CBOR as a tag 2 or tag 3 bignum.
    ///
    /// - Non-negative values use tag 2 (positive bignum).
    /// - Negative values use tag 3 (negative bignum), where the encoded
    ///   magnitude is `|value| - 1` per RFC 8949.
    fn from(value: BigInt) -> Self {
        let (sign, magnitude) = value.into_parts();
        match sign {
            Sign::NoSign | Sign::Plus => {
                // Zero or positive: use tag 2
                CBOR::from(magnitude)
            }
            Sign::Minus => {
                // Negative: use tag 3 with magnitude = |value| - 1
                // For value = -1, magnitude = 1, so n = 0 -> encode as 0x00
                // For value = -2, magnitude = 2, so n = 1 -> encode as 0x01
                let n = magnitude - 1u32;
                let bytes = n.to_bytes_be();
                let stripped = strip_leading_zeros(&bytes);
                // For n = 0 (value = -1), to_bytes_be() returns empty, but we
                // need 0x00
                let content = if stripped.is_empty() {
                    CBOR::to_byte_string([0u8])
                } else {
                    CBOR::to_byte_string(stripped)
                };
                CBOR::to_tagged_value(
                    Tag::with_value(TAG_NEGATIVE_BIGNUM),
                    content,
                )
            }
        }
    }
}

impl From<&BigInt> for CBOR {
    fn from(value: &BigInt) -> Self { value.clone().into() }
}

/// Decodes a [`BigUint`] from an untagged CBOR byte string.
///
/// This function is intended for use in tag summarizers where the tag has
/// already been stripped. It expects a CBOR byte string representing the
/// big-endian magnitude of a positive bignum (tag 2 content).
///
/// Enforces canonical encoding: no leading zero bytes (except empty for zero).
pub fn biguint_from_untagged_cbor(cbor: CBOR) -> Result<BigUint> {
    let bytes = cbor.try_into_byte_string()?;
    validate_bignum_magnitude(&bytes, false)?;
    Ok(BigUint::from_bytes_be(&bytes))
}

/// Decodes a [`BigInt`] from an untagged CBOR byte string for a negative
/// bignum.
///
/// This function is intended for use in tag summarizers where the tag has
/// already been stripped. It expects a CBOR byte string representing `n` where
/// the actual value is `-1 - n` (tag 3 content per RFC 8949).
///
/// Enforces canonical encoding: no leading zero bytes (except single `0x00`
/// for -1).
pub fn bigint_from_negative_untagged_cbor(cbor: CBOR) -> Result<BigInt> {
    let bytes = cbor.try_into_byte_string()?;
    validate_bignum_magnitude(&bytes, true)?;
    let n = BigUint::from_bytes_be(&bytes);
    let magnitude = n + 1u32;
    Ok(BigInt::from_biguint(Sign::Minus, magnitude))
}

impl TryFrom<CBOR> for BigUint {
    type Error = Error;

    /// Converts CBOR to a [`BigUint`].
    ///
    /// Accepts:
    /// - Major type 0 (unsigned integer)
    /// - Tag 2 (positive bignum) with canonical byte string
    ///
    /// Rejects:
    /// - Major type 1 (negative integer)
    /// - Tag 3 (negative bignum)
    /// - Floating-point values
    /// - Non-canonical bignum encodings
    fn try_from(cbor: CBOR) -> Result<Self> {
        match cbor.into_case() {
            CBORCase::Unsigned(n) => Ok(BigUint::from(n)),
            CBORCase::Negative(_) => Err(Error::OutOfRange),
            CBORCase::Tagged(tag, inner) => {
                let tag_value = tag.value();
                if tag_value == TAG_POSITIVE_BIGNUM {
                    // Tag 2: positive bignum
                    let bytes = inner.try_into_byte_string()?;
                    validate_bignum_magnitude(&bytes, false)?;
                    Ok(BigUint::from_bytes_be(&bytes))
                } else if tag_value == TAG_NEGATIVE_BIGNUM {
                    // Tag 3: negative bignum - not allowed for BigUint
                    Err(Error::OutOfRange)
                } else {
                    Err(Error::WrongType)
                }
            }
            CBORCase::Simple(_) => {
                // This covers floats (Simple::Float) - reject them
                Err(Error::WrongType)
            }
            _ => Err(Error::WrongType),
        }
    }
}

impl TryFrom<CBOR> for BigInt {
    type Error = Error;

    /// Converts CBOR to a [`BigInt`].
    ///
    /// Accepts:
    /// - Major type 0 (unsigned integer)
    /// - Major type 1 (negative integer)
    /// - Tag 2 (positive bignum) with canonical byte string
    /// - Tag 3 (negative bignum) with canonical byte string
    ///
    /// Rejects:
    /// - Floating-point values
    /// - Non-canonical bignum encodings
    fn try_from(cbor: CBOR) -> Result<Self> {
        match cbor.into_case() {
            CBORCase::Unsigned(n) => Ok(BigInt::from(n)),
            CBORCase::Negative(n) => {
                // CBOR negative: value = -1 - n
                // n is the raw u64 from the encoding
                let magnitude = BigUint::from(n) + 1u32;
                Ok(BigInt::from_biguint(Sign::Minus, magnitude))
            }
            CBORCase::Tagged(tag, inner) => {
                let tag_value = tag.value();
                if tag_value == TAG_POSITIVE_BIGNUM {
                    // Tag 2: positive bignum
                    let bytes = inner.try_into_byte_string()?;
                    validate_bignum_magnitude(&bytes, false)?;
                    let magnitude = BigUint::from_bytes_be(&bytes);
                    if magnitude == BigUint::ZERO {
                        Ok(BigInt::from(0))
                    } else {
                        Ok(BigInt::from_biguint(Sign::Plus, magnitude))
                    }
                } else if tag_value == TAG_NEGATIVE_BIGNUM {
                    // Tag 3: negative bignum, value = -1 - n
                    let bytes = inner.try_into_byte_string()?;
                    validate_bignum_magnitude(&bytes, true)?;
                    let n = BigUint::from_bytes_be(&bytes);
                    let magnitude = n + 1u32;
                    Ok(BigInt::from_biguint(Sign::Minus, magnitude))
                } else {
                    Err(Error::WrongType)
                }
            }
            CBORCase::Simple(_) => {
                // This covers floats (Simple::Float) - reject them
                Err(Error::WrongType)
            }
            _ => Err(Error::WrongType),
        }
    }
}

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

    #[test]
    fn test_biguint_zero() {
        let big = BigUint::from(0u32);
        let cbor = CBOR::from(big.clone());
        assert_eq!(cbor.diagnostic(), "2(h'')");
        let decoded: BigUint = cbor.try_into().unwrap();
        assert_eq!(decoded, big);
    }

    #[test]
    fn test_biguint_one() {
        let big = BigUint::from(1u32);
        let cbor = CBOR::from(big.clone());
        assert_eq!(cbor.diagnostic(), "2(h'01')");
        let decoded: BigUint = cbor.try_into().unwrap();
        assert_eq!(decoded, big);
    }

    #[test]
    fn test_biguint_255() {
        let big = BigUint::from(255u32);
        let cbor = CBOR::from(big.clone());
        assert_eq!(cbor.diagnostic(), "2(h'ff')");
        let decoded: BigUint = cbor.try_into().unwrap();
        assert_eq!(decoded, big);
    }

    #[test]
    fn test_biguint_256() {
        let big = BigUint::from(256u32);
        let cbor = CBOR::from(big.clone());
        assert_eq!(cbor.diagnostic(), "2(h'0100')");
        let decoded: BigUint = cbor.try_into().unwrap();
        assert_eq!(decoded, big);
    }

    #[test]
    fn test_bigint_zero() {
        let big = BigInt::from(0);
        let cbor = CBOR::from(big.clone());
        assert_eq!(cbor.diagnostic(), "2(h'')");
        let decoded: BigInt = cbor.try_into().unwrap();
        assert_eq!(decoded, big);
    }

    #[test]
    fn test_bigint_positive() {
        let big = BigInt::from(256);
        let cbor = CBOR::from(big.clone());
        assert_eq!(cbor.diagnostic(), "2(h'0100')");
        let decoded: BigInt = cbor.try_into().unwrap();
        assert_eq!(decoded, big);
    }

    #[test]
    fn test_bigint_negative_one() {
        let big = BigInt::from(-1);
        let cbor = CBOR::from(big.clone());
        // -1 -> magnitude = 1, n = 0 -> 0x00
        assert_eq!(cbor.diagnostic(), "3(h'00')");
        let decoded: BigInt = cbor.try_into().unwrap();
        assert_eq!(decoded, big);
    }

    #[test]
    fn test_bigint_negative_two() {
        let big = BigInt::from(-2);
        let cbor = CBOR::from(big.clone());
        // -2 -> magnitude = 2, n = 1 -> 0x01
        assert_eq!(cbor.diagnostic(), "3(h'01')");
        let decoded: BigInt = cbor.try_into().unwrap();
        assert_eq!(decoded, big);
    }

    #[test]
    fn test_bigint_negative_256() {
        let big = BigInt::from(-256);
        let cbor = CBOR::from(big.clone());
        // -256 -> magnitude = 256, n = 255 -> 0xff
        assert_eq!(cbor.diagnostic(), "3(h'ff')");
        let decoded: BigInt = cbor.try_into().unwrap();
        assert_eq!(decoded, big);
    }

    #[test]
    fn test_bigint_negative_257() {
        let big = BigInt::from(-257);
        let cbor = CBOR::from(big.clone());
        // -257 -> magnitude = 257, n = 256 -> 0x0100
        assert_eq!(cbor.diagnostic(), "3(h'0100')");
        let decoded: BigInt = cbor.try_into().unwrap();
        assert_eq!(decoded, big);
    }

    #[test]
    fn test_decode_plain_unsigned_to_biguint() {
        let cbor = CBOR::from(12345u64);
        let big: BigUint = cbor.try_into().unwrap();
        assert_eq!(big, BigUint::from(12345u64));
    }

    #[test]
    fn test_decode_plain_unsigned_to_bigint() {
        let cbor = CBOR::from(12345u64);
        let big: BigInt = cbor.try_into().unwrap();
        assert_eq!(big, BigInt::from(12345));
    }

    #[test]
    fn test_decode_plain_negative_to_bigint() {
        let cbor = CBOR::from(-12345i64);
        let big: BigInt = cbor.try_into().unwrap();
        assert_eq!(big, BigInt::from(-12345));
    }

    #[test]
    fn test_decode_plain_negative_to_biguint_fails() {
        let cbor = CBOR::from(-1i64);
        let result: Result<BigUint> = cbor.try_into();
        assert!(matches!(result, Err(Error::OutOfRange)));
    }

    #[test]
    fn test_decode_tag3_to_biguint_fails() {
        let big = BigInt::from(-1);
        let cbor = CBOR::from(big);
        let result: Result<BigUint> = cbor.try_into();
        assert!(matches!(result, Err(Error::OutOfRange)));
    }
}