Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: rfc8949 canonicalization #144

Open
1 task done
hoxxep opened this issue Nov 26, 2024 · 0 comments
Open
1 task done

[Feature]: rfc8949 canonicalization #144

hoxxep opened this issue Nov 26, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@hoxxep
Copy link
Contributor

hoxxep commented Nov 26, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Description

The current ciborium implementation follows RFC 7049 / RFC 8949 4.2.3 backwards compatible canonical CBOR output (length-first map key ordering).

I would quite like to output canonical CBOR in the more "modern" RFC 8949 4.2.1 spec (lexicographic byte ordering).

The change is as simple as modifying the cmp_value function, but toggling this functionality is tricky. It's really easy to implement as a crate feature, but then different libraries might cause the features to conflict (eg. two libs for different protocols using different scheme features in the same application project).

It would be nice to pass the canonicalisation scheme as an enum to the Serializer, into_reader, to_vec methods; but then how this makes it into the CanonicalValue struct would require a lot of work. An alternate suggestion of how canonicalisation/key ordering could be done is suggested below (albeit messily gated behind #[cfg(std)] flags for now).

I'm happy to continue with this work if it's something you're interested in.

Acceptance Criteria

No response

Suggestions for a technical implementation

The starts to a bodged alternative implementation, but would allow for RFC 7049, 8049, or no sorting for faster serialization. Only sorts keys once: #143

Lazy cmp_value implementation:

/// Compares two values using canonical comparison as defined in RFC 8949 4.2.1.
pub fn cmp_value_rfc8949(v1: &Value, v2: &Value) -> Ordering {
    let mut bytes1 = Vec::new();
    let _ = crate::ser::into_writer(v1, &mut bytes1);
    let mut bytes2 = Vec::new();
    let _ = crate::ser::into_writer(v2, &mut bytes2);
    bytes1.cmp(&bytes2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant