Skip to content

der: add Set_of_ref struct#2247

Open
kraemv wants to merge 1 commit intoRustCrypto:masterfrom
kraemv:feature/set_of_ref
Open

der: add Set_of_ref struct#2247
kraemv wants to merge 1 commit intoRustCrypto:masterfrom
kraemv:feature/set_of_ref

Conversation

@kraemv
Copy link
Copy Markdown

@kraemv kraemv commented Feb 27, 2026

Hello everyone,
in this PR I add a referenced version to the SetOf struct. This can be useful to implement referenced versions of structs containing SetOfs.

In order to allow both decoding from DER bytes and creating references from existing SetOfs, the SetOfRef contains an internal reference that can be both. The inner reference to raw bytes is not pretty, but I do not see another way to implement this with no_std requirements. Most functions like get are then implemented over iterators, that hide the inner reference type. I check sorting on all constructors.

In order for CI to go through, we need a MSRV of 1.87 for the heapless crate.

I am happy to receive feedback on this PR!

@kraemv kraemv force-pushed the feature/set_of_ref branch 2 times, most recently from b38a2cc to fc1d408 Compare February 27, 2026 09:45
@kraemv kraemv force-pushed the feature/set_of_ref branch from fc1d408 to 7aa875a Compare February 27, 2026 10:04
@kraemv kraemv force-pushed the feature/set_of_ref branch from 7aa875a to 43bf173 Compare February 27, 2026 10:21
Copy link
Copy Markdown
Contributor

@franziskuskiefer franziskuskiefer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few quick notes.


new_set
.iter()
.is_sorted_by(|a, b| matches!(a.der_cmp(b), Ok(Ordering::Less)))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem right. is_sorted_by expects the comparator to return true when a <= b, but the closure returns true only for Ordering::Less (strict).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is intentional, as this will prevent building a set with duplicate elements. Do you think I should elaborate on this in a comment?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think elaborating on this in a comment and arguing why it's correct with respect to the spec would be good. Otherwise this is confusing.

Copy link
Copy Markdown
Author

@kraemv kraemv Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, in X.680, 27.3 NOTE 3 it states that:

"The set-of type is not a mathematical set of values, thus, as an example, for SET OF INTEGER the values { 1 } and { 1 1 } are distinct."

Hence duplicate values are explicitly allowed. But since the other SET OF implementation does not allow duplicate values, I will also not allow them in a SET OF reference.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, good point. I'm noticing that we do have logic to check for duplicates (along with an error variant), but re-reading X.680 I'm not actually seeing a requirement that precludes duplicates for set-of. It says for DER the order needs to be ascending but not necessarily strictly ascending.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and the example in X.680 explicitly states an example with duplicate elements.
Only for SETs no duplicates are allowed, as it says in X.690, 26.4.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What matters for our purposes is whether it’s actually a valid DER production. There are examples of all sorts of things in X.690 which are invalid DER

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, checked with an expert and duplicates are allowed.

@kraemv good find! I opened an issue: #2271

Copy link
Copy Markdown
Author

@kraemv kraemv Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed the handling of duplicates in the new PR #2272. Once this gets merged, I can rebase and also change the behavior for the SetOfRef struct.

@kraemv kraemv force-pushed the feature/set_of_ref branch from 43bf173 to 119445e Compare March 31, 2026 11:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants