Rust and WASM did-method-plc tools and structures
1//! Benchmarks for validation operations 2 3use atproto_plc::{Did, DidBuilder, OperationChainValidator, SigningKey}; 4use criterion::{black_box, criterion_group, criterion_main, Criterion}; 5 6fn benchmark_did_validation(c: &mut Criterion) { 7 c.bench_function("valid did parse", |b| { 8 b.iter(|| Did::parse(black_box("did:plc:ewvi7nxzyoun6zhxrhs64oiz"))) 9 }); 10 11 c.bench_function("invalid did parse", |b| { 12 b.iter(|| Did::parse(black_box("did:plc:invalid"))) 13 }); 14} 15 16fn benchmark_key_generation(c: &mut Criterion) { 17 c.bench_function("p256 keygen", |b| { 18 b.iter(|| SigningKey::generate_p256()) 19 }); 20 21 c.bench_function("k256 keygen", |b| { 22 b.iter(|| SigningKey::generate_k256()) 23 }); 24} 25 26fn benchmark_did_creation(c: &mut Criterion) { 27 c.bench_function("create did with builder", |b| { 28 b.iter(|| { 29 let rotation_key = SigningKey::generate_p256(); 30 let signing_key = SigningKey::generate_k256(); 31 32 DidBuilder::new() 33 .add_rotation_key(rotation_key) 34 .add_verification_method("atproto".into(), signing_key) 35 .build() 36 }) 37 }); 38} 39 40fn benchmark_signature_operations(c: &mut Criterion) { 41 let key = SigningKey::generate_p256(); 42 let data = b"hello world"; 43 44 c.bench_function("sign with p256", |b| { 45 b.iter(|| key.sign(black_box(data))) 46 }); 47 48 let signature = key.sign(data).unwrap(); 49 let verifying_key = key.verifying_key(); 50 51 c.bench_function("verify signature", |b| { 52 b.iter(|| verifying_key.verify(black_box(data), black_box(&signature))) 53 }); 54} 55 56fn benchmark_operation_chain_validation(c: &mut Criterion) { 57 // Create a simple chain 58 let rotation_key = SigningKey::generate_p256(); 59 let (_, operation, _) = DidBuilder::new() 60 .add_rotation_key(rotation_key) 61 .build() 62 .unwrap(); 63 64 c.bench_function("validate single operation chain", |b| { 65 b.iter(|| OperationChainValidator::validate_chain(black_box(&[operation.clone()]))) 66 }); 67} 68 69fn benchmark_did_key_conversion(c: &mut Criterion) { 70 let key = SigningKey::generate_p256(); 71 let did_key = key.to_did_key(); 72 73 c.bench_function("to did:key", |b| { 74 b.iter(|| key.to_did_key()) 75 }); 76 77 c.bench_function("from did:key", |b| { 78 b.iter(|| atproto_plc::VerifyingKey::from_did_key(black_box(&did_key))) 79 }); 80} 81 82criterion_group!( 83 benches, 84 benchmark_did_validation, 85 benchmark_key_generation, 86 benchmark_did_creation, 87 benchmark_signature_operations, 88 benchmark_operation_chain_validation, 89 benchmark_did_key_conversion 90); 91criterion_main!(benches);