Skip to content

Commit

Permalink
Merge 54e0d0a into 29eaf57
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver E. Anderson committed Nov 8, 2022
2 parents 29eaf57 + 54e0d0a commit b2ffb07
Show file tree
Hide file tree
Showing 13 changed files with 1,201 additions and 290 deletions.
52 changes: 50 additions & 2 deletions identity_core/src/common/ordered_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,16 @@ impl<T> OrderedSet<T> {
self.change(update, |item, update| item.key() == update.key())
}

/// Removes all matching items from the set.
/// Removes and returns the matching item from the set, if it exists.
#[inline]
pub fn remove<U>(&mut self, item: &U) -> bool
where
T: KeyComparable,
U: KeyComparable<Key = T::Key>,
{
// self.iter().enumerate().find(|(_, entry)| entry.key() == item.key()).map(|(idx,_)| idx).map(|idx|
// self.0.remove(idx))

if self.contains(item) {
self.0.retain(|this| this.borrow().key() != item.key());
true
Expand Down Expand Up @@ -312,6 +315,9 @@ where
#[cfg(test)]
mod tests {
use super::*;
use proptest::prelude::Rng;
use proptest::strategy::Strategy;
use proptest::*;

#[test]
fn test_ordered_set_works() {
Expand Down Expand Up @@ -383,7 +389,7 @@ mod tests {
assert!(!set.contains(&cs4));
}

#[derive(Clone, Copy, PartialEq, Eq)]
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
struct ComparableStruct {
key: u8,
value: i32,
Expand Down Expand Up @@ -456,4 +462,46 @@ mod tests {
assert_eq!(set.head().unwrap().key, cs2.key);
assert_eq!(set.head().unwrap().value, cs2.value);
}

// ===========================================================================================================================
// Test key uniqueness invariant with randomly generated input
// ===========================================================================================================================

fn arbitrary_set_comparable_struct() -> impl Strategy<Value = OrderedSet<ComparableStruct>> {
proptest::arbitrary::any::<Vec<(u8, i32)>>().prop_map(|values| {
values
.into_iter()
.map(|(key, value)| ComparableStruct { key, value })
.collect()
})
}

fn arbitrary_set_u128() -> impl Strategy<Value = OrderedSet<u128>> {
proptest::arbitrary::any::<Vec<u128>>().prop_map(|vector| vector.into_iter().collect())
}

// construct a set together with a pair of values. Given one of these values there is a 50% chance that it is
// contained in the set.
fn set_with_values<F, T, U>(f: F) -> impl Strategy<Value = (OrderedSet<T>, T, T)>
where
T: KeyComparable + Default + Debug + Clone,
U: Strategy<Value = (OrderedSet<T>, OrderedSet<T>)>,
F: Fn() -> U,
{
f().prop_perturb(|(s0, s1), mut rng| {
let sets = [&s0, &s1];
let mut pick_value = || {
let set_idx = usize::from(rng.gen_bool(0.5));
let set_range = if set_idx == 0 { 0..s0.len() } else { 0..s1.len() };
if set_range.is_empty() {
T::default()
} else {
let entry_idx = rng.gen_range(set_range);
(sets[set_idx])[entry_idx].clone()
}
};
let (v0, v1) = (pick_value(), pick_value());
(s0, v0, v1)
})
}
}
10 changes: 5 additions & 5 deletions identity_core/src/crypto/proof/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,11 @@ impl Serialize for Proof {
} else {
3 // type + method + value
};
count_fields += if self.created.is_some() { 1 } else { 0 };
count_fields += if self.expires.is_some() { 1 } else { 0 };
count_fields += if self.challenge.is_some() { 1 } else { 0 };
count_fields += if self.domain.is_some() { 1 } else { 0 };
count_fields += if self.purpose.is_some() { 1 } else { 0 };
count_fields += usize::from(self.created.is_some());
count_fields += usize::from(self.expires.is_some());
count_fields += usize::from(self.challenge.is_some());
count_fields += usize::from(self.domain.is_some());
count_fields += usize::from(self.purpose.is_some());
let mut state: S::SerializeMap = serializer.serialize_map(Some(count_fields))?;

state.serialize_entry("type", &self.type_)?;
Expand Down
2 changes: 1 addition & 1 deletion identity_credential/src/validator/credential_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ mod tests {

// Add a RevocationBitmap service to the issuer.
let bitmap: RevocationBitmap = RevocationBitmap::new();
assert!(issuer_doc.service_mut().append(
assert!(issuer_doc.service_mut_unchecked().append(
Service::builder(Object::new())
.id(service_url.clone())
.type_(RevocationBitmap::TYPE)
Expand Down
5 changes: 5 additions & 0 deletions identity_did/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ strum = { version = "0.24.0", default-features = false, features = ["std", "deri
thiserror = { version = "1.0", default-features = false }

[dev-dependencies]
criterion = { version = "0.4.0", default-features = false, features = ["cargo_bench_support"] }
proptest = { version = "1.0" }
serde_json = { version = "1.0", default-features = false }

Expand All @@ -35,3 +36,7 @@ rustdoc-args = ["--cfg", "docsrs"]
[features]
default = ["revocation-bitmap"]
revocation-bitmap = ["dataurl", "flate2", "roaring"]

[[bench]]
name = "deserialize_document"
harness = false
239 changes: 239 additions & 0 deletions identity_did/benches/deserialize_document.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
// Copyright 2020-2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use criterion::Throughput;
use identity_core::convert::FromJson;
// This is a benchmark measuring the time it takes to deserialize a DID document from JSON.
use criterion::criterion_group;
use criterion::criterion_main;
use criterion::BenchmarkId;
use criterion::Criterion;
use identity_did::document::CoreDocument;

const JSON_DOC_SHORT: &str = r#"
{
"id": "did:iota:0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"verificationMethod": [
{
"id": "did:iota:0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa#issuerKey",
"controller": "did:iota:0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"type": "Ed25519VerificationKey2018",
"publicKeyMultibase": "zFVen3X669xLzsi6N2V91DoiyzHzg1uAgqiT8jZ9nS96Z"
}
]
}"#;

const JSON_DOC_DID_KEY: &str = r#"{
"@context": [
"https://www.w3.org/ns/did/v1",
"https://w3id.org/security/suites/ed25519-2020/v1",
"https://w3id.org/security/suites/x25519-2020/v1"
],
"id": "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK",
"verificationMethod": [{
"id": "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK#z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK",
"type": "Ed25519VerificationKey2018",
"controller": "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK",
"publicKeyMultibase": "z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK"
}],
"authentication": [
"did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK#z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK"
],
"assertionMethod": [
"did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK#z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK"
],
"capabilityDelegation": [
"did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK#z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK"
],
"capabilityInvocation": [
"did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK#z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK"
],
"keyAgreement": [{
"id": "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK#z6LSj72tK8brWgZja8NLRwPigth2T9QRiG1uH9oKZuKjdh9p",
"type": "X25519KeyAgreementKey2019",
"controller": "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK",
"publicKeyMultibase": "z6LSj72tK8brWgZja8NLRwPigth2T9QRiG1uH9oKZuKjdh9p"
}]
}"#;

// This is not a realistic document in any way.
const JSON_DOCUMENT_LARGE: &str = r#"{
"@context": [
"https://www.w3.org/ns/did/v1",
"https://w3id.org/security/suites/ed25519-2020/v1"
],
"id": "did:example:123",
"verificationMethod": [
{
"id": "did:example:123#key1",
"controller": "did:example:1234",
"type": "Ed25519VerificationKey2018",
"publicKeyBase58": "3M5RCDjPTWPkKSN3sxUmmMqHbmRPegYP1tjcKyrDbt9J"
},
{
"id": "did:example:123#key2",
"controller": "did:example:1234",
"type": "Ed25519VerificationKey2018",
"publicKeyBase58": "3M5RCDjPTWPkKSN3sxUmmMqHbmRPegYP1tjcKyrDbt9J"
},
{
"id": "did:example:123#key3",
"controller": "did:example:1234",
"type": "Ed25519VerificationKey2018",
"publicKeyBase58": "3M5RCDjPTWPkKSN3sxUmmMqHbmRPegYP1tjcKyrDbt9J"
},
{
"id": "did:example:123#key4",
"controller": "did:example:1234",
"type": "Ed25519VerificationKey2018",
"publicKeyBase58": "3M5RCDjPTWPkKSN3sxUmmMqHbmRPegYP1tjcKyrDbt9J"
},
{
"id": "did:example:123#key5",
"controller": "did:example:1234",
"type": "Ed25519VerificationKey2018",
"publicKeyBase58": "3M5RCDjPTWPkKSN3sxUmmMqHbmRPegYP1tjcKyrDbt9J"
},
{
"id": "did:example:123#key6",
"controller": "did:example:1234",
"type": "Ed25519VerificationKey2018",
"publicKeyBase58": "3M5RCDjPTWPkKSN3sxUmmMqHbmRPegYP1tjcKyrDbt9J"
}
],
"authentication": [
{
"id": "did:example:123#z6MkecaLyHuYWkayBDLw5ihndj3T1m6zKTGqau3A51G7RBf3EMB1",
"type": "Ed25519VerificationKey2018",
"controller": "did:example:123",
"publicKeyMultibase": "zAKJP3f7BD6W4iWEQ9jwndVTCBq8ua2Utt8EEjJ6Vxsf"
},
"did:example:123#key1",
{
"id": "did:example:123#z6MkecaLyHuYWkayBDLw5ihndj3T1m6zKTGqau3A51G7RBf3EMB2",
"type": "Ed25519VerificationKey2018",
"controller": "did:example:123",
"publicKeyMultibase": "zAKJP3f7BD6W4iWEQ9jwndVTCBq8ua2Utt8EEjJ6Vxsf"
},
"did:example:123#key4",
"did:example:123#key2",
{
"id": "did:example:123#z6MkecaLyHuYWkayBDLw5ihndj3T1m6zKTGqau3A51G7RBf3EMB3",
"type": "Ed25519VerificationKey2018",
"controller": "did:example:123",
"publicKeyMultibase": "zAKJP3f7BD6W4iWEQ9jwndVTCBq8ua2Utt8EEjJ6Vxsf"
},
{
"id": "did:example:123#z6MkecaLyHuYWkayBDLw5ihndj3T1m6zKTGqau3A51G7RBf3EMB4",
"type": "Ed25519VerificationKey2018",
"controller": "did:example:123",
"publicKeyMultibase": "zAKJP3f7BD6W4iWEQ9jwndVTCBq8ua2Utt8EEjJ6Vxsf"
}
],
"capabilityInvocation": [
{
"id": "did:example:123#z6MkhdmzFu659ZJ4XKj31vtEDmjvsi5yDZG5L7Caz63oP39k",
"type": "Ed25519VerificationKey2018",
"controller": "did:example:123",
"publicKeyMultibase": "z4BWwfeqdp1obQptLLMvPNgBw48p7og1ie6Hf9p5nTpNN"
},
"did:example:123#key1",
{
"id": "did:example:123#z6MkhdmzFu659ZJ4XKj31vtEDmjvsi5yDZG5L7Caz63oP39kEMB2",
"type": "Ed25519VerificationKey2018",
"controller": "did:example:123",
"publicKeyMultibase": "z4BWwfeqdp1obQptLLMvPNgBw48p7og1ie6Hf9p5nTpNN"
},
{
"id": "did:example:123#z6MkhdmzFu659ZJ4XKj31vtEDmjvsi5yDZG5L7Caz63oP39kEMB3",
"type": "Ed25519VerificationKey2018",
"controller": "did:example:123",
"publicKeyMultibase": "z4BWwfeqdp1obQptLLMvPNgBw48p7og1ie6Hf9p5nTpNN"
},
"did:example:123#key5"
],
"capabilityDelegation": [
{
"id": "did:example:123#z6Mkw94ByR26zMSkNdCUi6FNRsWnc2DFEeDXyBGJ5KTzSWyi",
"type": "Ed25519VerificationKey2018",
"controller": "did:example:123",
"publicKeyMultibase": "zHgo9PAmfeoxHG8Mn2XHXamxnnSwPpkyBHAMNF3VyXJCL"
},
"did:example:123#key6"
],
"assertionMethod": [
{
"id": "did:example:123#z6MkiukuAuQAE8ozxvmahnQGzApvtW7KT5XXKfojjwbdEomY",
"type": "Ed25519VerificationKey2018",
"controller": "did:example:123",
"publicKeyMultibase": "z5TVraf9itbKXrRvt2DSS95Gw4vqU3CHAdetoufdcKazA"
},
{
"id": "did:example:123#z6MkiukuAuQAE8ozxvmahnQGzApvtW7KT5XXKfojjwbdEomYEMB2",
"type": "Ed25519VerificationKey2018",
"controller": "did:example:123",
"publicKeyMultibase": "z5TVraf9itbKXrRvt2DSS95Gw4vqU3CHAdetoufdcKazA"
},
{
"id": "did:example:123#z6MkiukuAuQAE8ozxvmahnQGzApvtW7KT5XXKfojjwbdEomYEMB3",
"type": "Ed25519VerificationKey2018",
"controller": "did:example:123",
"publicKeyMultibase": "z5TVraf9itbKXrRvt2DSS95Gw4vqU3CHAdetoufdcKazA"
},
{
"id": "did:example:123#z6MkiukuAuQAE8ozxvmahnQGzApvtW7KT5XXKfojjwbdEomYEMB4",
"type": "Ed25519VerificationKey2018",
"controller": "did:example:123",
"publicKeyMultibase": "z5TVraf9itbKXrRvt2DSS95Gw4vqU3CHAdetoufdcKazA"
},
{
"id": "did:example:123#z6MkiukuAuQAE8ozxvmahnQGzApvtW7KT5XXKfojjwbdEomYEMB5",
"type": "Ed25519VerificationKey2018",
"controller": "did:example:123",
"publicKeyMultibase": "z5TVraf9itbKXrRvt2DSS95Gw4vqU3CHAdetoufdcKazA"
},
{
"id": "did:example:123#z6MkiukuAuQAE8ozxvmahnQGzApvtW7KT5XXKfojjwbdEomYEMB6",
"type": "Ed25519VerificationKey2018",
"controller": "did:example:123",
"publicKeyMultibase": "z5TVraf9itbKXrRvt2DSS95Gw4vqU3CHAdetoufdcKazA"
},
"did:example:123#key4",
{
"id": "did:example:123#z6MkiukuAuQAE8ozxvmahnQGzApvtW7KT5XXKfojjwbdEomYEMB7",
"type": "Ed25519VerificationKey2018",
"controller": "did:example:123",
"publicKeyMultibase": "z5TVraf9itbKXrRvt2DSS95Gw4vqU3CHAdetoufdcKazA"
}
],
"service": [{
"id":"did:example:123#linked-domain",
"type": "LinkedDomains",
"serviceEndpoint": "https://bar.example.com"
}]
}"#;

fn deserialize_json_document(c: &mut Criterion) {
let mut group = c.benchmark_group("deserialize_json_document");
for (json, name) in [
(JSON_DOC_SHORT, "short document"),
(JSON_DOC_DID_KEY, "did:key document"),
(JSON_DOCUMENT_LARGE, "large document"),
] {
group.throughput(Throughput::Bytes(json.as_bytes().len() as u64));
group.bench_with_input(
BenchmarkId::from_parameter(format!("{name}, document size: {} bytes", json.as_bytes().len())),
json,
|b, json| {
b.iter(|| {
let doc: Result<CoreDocument, _> = CoreDocument::from_json(json);
assert!(doc.is_ok(), "bench {name} failed: {:#?}", doc);
})
},
);
}
group.finish();
}

criterion_group!(benches, deserialize_json_document);
criterion_main!(benches);

0 comments on commit b2ffb07

Please sign in to comment.