Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by Cargo
/target
/verification/creusot/target

# IDE
.idea
Expand Down
1 change: 1 addition & 0 deletions .proven/coverage-exclusions.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ src/ijson.rs 221-224 afced9dbdd974f8cf35d6d3aedbcbd87996442c0cac6277c28ebd267228
src/ijson.rs 234 28d86778615f6af47bb1bc4f40face756749768e5111f114cfa234a5060c25af unreachable implicit else after as_f64(): a serde_json::Number is always i64, u64 or f64
src/jcs.rs 125-126 78794b4982c6b480eda0ee4efa87c54898f9b52aba2a31b5a38520c26e9f39ef unreachable u64 at or below 2^53-1 is always also an i64, so the first branch takes it
src/jcs.rs 145 eff5313cfd9eb5a1150c6c7f8f74f8ecbdffd415cd5ed94092b960f0ce737aa3 unreachable es_number_to_string is only called with a Number's f64, which is always finite
src/jcs_core.rs 49 8cf9087aa9d19bdaa3e01a4606ae1ec5607e672b8b054df2dac968c5750392ff unreachable bytes F5..FF never occur in the UTF-8 of a Rust str, which is the only input; the branch keeps the key a permutation of all byte values (unit test order_key_is_a_permutation executes it)
src/jsonld.rs 89-92 402488d41ddef455de57876a02b952263b009c7276aad704a0651f11073670e3 unreachable JCS output is built from UTF-8 strings and ASCII escapes, so from_utf8 cannot fail
src/strict.rs 45-47 10cec3347de43875134bcc80b462ce19cbe071095b3c736c864bd4d4c98ebf0e unreachable Visitor::expecting is only called for a type mismatch; serde_json's deserialize_any always picks the matching visit_* method
src/strict.rs 187 1e7485fbef031241f6cabf64563ef6f5973ecf51e927241b2147bff185decac8 unreachable a comma at nesting depth 0 is impossible in text that has already parsed as JSON
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ all-features = true
[[bench]]
name = "jcs"
harness = false

[lints.rust]
# `cfg(creusot)` is set only by the Creusot harness in verification/creusot.
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(creusot)'] }
28 changes: 7 additions & 21 deletions src/jcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@
// changing the canonical output for their manifest format.

use serde_json::Value;
use std::cmp::Ordering;

use crate::error::CanonicalizationError;
use crate::ijson::{self, SAFE_INT_MAX, SAFE_INT_MIN, SAFE_UINT_MAX};
use crate::jcs_core;

/// Canonicalize a JSON value to JCS format (RFC 8785)
///
Expand Down Expand Up @@ -150,16 +150,9 @@ pub(crate) fn es_number_to_string(f: f64) -> Result<String, CanonicalizationErro
Ok(ryu_js::Buffer::new().format_finite(f).to_owned())
}

/// Write a string in JCS format
///
/// Uses serde_json's RFC 8259 compliant string escaping.
/// Write a string in JCS format (RFC 8785 §3.2.2.2).
fn write_string_jcs(s: &str, out: &mut Vec<u8>) {
// serde_json::to_string produces RFC 8259 compliant string escaping
// which matches JCS requirements for the code points we care about
let escaped = serde_json::to_string(s).expect("String is always serializable");
// The escaped string will be in quotes, but we want just the content
// Actually, serde_json::to_string on a string value returns the quoted string
out.extend_from_slice(escaped.as_bytes());
jcs_core::write_string_literal(s.as_bytes(), out);
}

/// Write an array in JCS format
Expand Down Expand Up @@ -190,9 +183,7 @@ fn write_object_jcs(
out.push(b',');
}

// Write the key (with quotes and escaping via serde_json)
let key_str = serde_json::to_string(key).expect("Key is always serializable");
out.extend_from_slice(key_str.as_bytes());
write_string_jcs(key, out);
out.push(b':');

// Write the value
Expand All @@ -208,15 +199,10 @@ fn write_object_jcs(
/// UTF-16 code unit order of their keys."
///
/// This is NOT the same as simple string comparison for non-ASCII keys.
/// For ASCII-only keys (which is the common case), this behaves the same
/// as lexicographic comparison.
/// The comparison never ties on distinct keys, and the keys of one object
/// are distinct, so the result does not depend on the sort being stable.
fn sort_keys_utf16(keys: &mut [&String]) {
keys.sort_by(|a, b| utf16_code_unit_cmp(a, b));
}

/// Compare two strings by UTF-16 code unit order
fn utf16_code_unit_cmp(a: &str, b: &str) -> Ordering {
a.encode_utf16().cmp(b.encode_utf16())
keys.sort_by(|a, b| jcs_core::cmp_utf16(a.as_bytes(), b.as_bytes()));
}

#[cfg(test)]
Expand Down
Loading
Loading