oas-document 1.1.1

OAS Identity Document types and validation for the Open Agent Specification
Documentation
// OAS Spec §5 — Document Operation Benchmarks
// Targets: validation < 100µs, serialization < 200µs

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use oas_crypto::keypair::OasKeyPair;
use oas_document::builder::DocumentBuilder;
use oas_document::conformance::ConformanceLevel;

fn make_signed_doc() -> (oas_document::OasDocument, OasKeyPair) {
    let kp = OasKeyPair::generate();
    let doc = DocumentBuilder::new("did:oas:test:hmr:alice", "hmr")
        .conformance_level(ConformanceLevel::L0)
        .add_verification_method(&kp)
        .build_and_sign(&kp, "2026-01-15T00:00:00Z")
        .unwrap_or_else(|e| panic!("build doc: {e}"));
    (doc, kp)
}

fn bench_document_build_and_sign(c: &mut Criterion) {
    let kp = OasKeyPair::generate();

    c.bench_function("document_build_and_sign", |b| {
        b.iter(|| {
            let _ = DocumentBuilder::new(black_box("did:oas:test:hmr:alice"), black_box("hmr"))
                .conformance_level(ConformanceLevel::L0)
                .add_verification_method(&kp)
                .build_and_sign(&kp, "2026-01-15T00:00:00Z");
        })
    });
}

fn bench_document_serialize(c: &mut Criterion) {
    let (doc, _) = make_signed_doc();

    c.bench_function("document_serialize_json", |b| {
        b.iter(|| {
            let _ = serde_json::to_string(black_box(&doc));
        })
    });
}

fn bench_document_deserialize(c: &mut Criterion) {
    let (doc, _) = make_signed_doc();
    let json = serde_json::to_string(&doc).unwrap_or_default();

    c.bench_function("document_deserialize_json", |b| {
        b.iter(|| {
            let _: Result<oas_document::OasDocument, _> = serde_json::from_str(black_box(&json));
        })
    });
}

fn bench_proof_verify(c: &mut Criterion) {
    let (doc, kp) = make_signed_doc();
    let proof = doc.proof.as_ref().unwrap_or_else(|| panic!("no proof"));
    let mut doc_json = serde_json::to_value(&doc).unwrap_or_default();
    if let Some(obj) = doc_json.as_object_mut() {
        obj.remove("proof");
    }

    c.bench_function("document_proof_verify", |b| {
        b.iter(|| {
            let _ = proof.verify(black_box(&doc_json), black_box(&kp.verifying_key_bytes()));
        })
    });
}

criterion_group!(
    benches,
    bench_document_build_and_sign,
    bench_document_serialize,
    bench_document_deserialize,
    bench_proof_verify,
);
criterion_main!(benches);