ash-core 2.3.6

DEPRECATED — Use `ashcore` instead. This crate is End of Life (EOL).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//! Benchmark Tests for ASH Rust SDK
//!
//! Tests performance thresholds for various operations.
//! Note: These are not true benchmarks but performance validation tests.
//! Thresholds are set conservatively for debug builds; release builds will be faster.

use ash_core::{
    ash_build_proof, ash_verify_proof, ash_derive_client_secret,
    ash_canonicalize_json, ash_canonicalize_query,
    ash_hash_body, ash_generate_nonce, ash_generate_context_id,
    ash_build_proof_scoped, ash_verify_proof_scoped,
};
use std::time::Instant;

// =========================================================================
// PROOF GENERATION BENCHMARKS
// =========================================================================

mod proof_generation {
    use super::*;

    #[test]
    fn test_proof_generation_performance() {
        let nonce = "a".repeat(64);
        let secret = ash_derive_client_secret(&nonce, "ctx", "POST|/|").unwrap();
        let body_hash = "b".repeat(64);

        let iterations = 10000;
        let start = Instant::now();

        for i in 0..iterations {
            let timestamp = format!("{}", 1700000000 + i);
            let _ = ash_build_proof(&secret, &timestamp, "POST|/|", &body_hash).unwrap();
        }

        let elapsed = start.elapsed();
        let ops_per_sec = iterations as f64 / elapsed.as_secs_f64();

        println!("Proof generation: {} ops/sec", ops_per_sec as u64);

        // Rust should achieve at least 50,000 proofs/sec
        assert!(ops_per_sec > 10000.0, "Proof generation too slow: {} ops/sec", ops_per_sec);
    }

    #[test]
    fn test_scoped_proof_generation_performance() {
        let nonce = "a".repeat(64);
        let secret = ash_derive_client_secret(&nonce, "ctx", "POST|/|").unwrap();
        let payload = r#"{"amount":100,"currency":"USD","memo":"test"}"#;
        let scope = vec!["amount", "currency"];

        let iterations = 5000;
        let start = Instant::now();

        for i in 0..iterations {
            let timestamp = format!("{}", 1700000000 + i);
            let _ = ash_build_proof_scoped(&secret, &timestamp, "POST|/|", payload, &scope).unwrap();
        }

        let elapsed = start.elapsed();
        let ops_per_sec = iterations as f64 / elapsed.as_secs_f64();

        println!("Scoped proof generation: {} ops/sec", ops_per_sec as u64);
        assert!(ops_per_sec > 3000.0, "Scoped proof generation too slow");
    }
}

// =========================================================================
// PROOF VERIFICATION BENCHMARKS
// =========================================================================

mod proof_verification {
    use super::*;

    #[test]
    fn test_proof_verification_performance() {
        let nonce = "a".repeat(64);
        let context_id = "ctx_test";
        let binding = "POST|/api|";
        let timestamp = "1700000000";
        let body_hash = "b".repeat(64);

        let secret = ash_derive_client_secret(&nonce, context_id, binding).unwrap();
        let proof = ash_build_proof(&secret, timestamp, binding, &body_hash).unwrap();

        let iterations = 10000;
        let start = Instant::now();

        for _ in 0..iterations {
            let _ = ash_verify_proof(&nonce, context_id, binding, timestamp, &body_hash, &proof).unwrap();
        }

        let elapsed = start.elapsed();
        let ops_per_sec = iterations as f64 / elapsed.as_secs_f64();

        println!("Proof verification: {} ops/sec", ops_per_sec as u64);
        assert!(ops_per_sec > 1000.0, "Proof verification too slow");
    }

    #[test]
    fn test_scoped_verification_performance() {
        let nonce = "a".repeat(64);
        let context_id = "ctx_test";
        let binding = "POST|/api|";
        let timestamp = "1700000000";
        let payload = r#"{"amount":100,"currency":"USD"}"#;
        let scope = vec!["amount"];

        let secret = ash_derive_client_secret(&nonce, context_id, binding).unwrap();
        let (proof, scope_hash) = ash_build_proof_scoped(&secret, timestamp, binding, payload, &scope).unwrap();

        let iterations = 5000;
        let start = Instant::now();

        for _ in 0..iterations {
            let _ = ash_verify_proof_scoped(&nonce, context_id, binding, timestamp, payload, &scope, &scope_hash, &proof).unwrap();
        }

        let elapsed = start.elapsed();
        let ops_per_sec = iterations as f64 / elapsed.as_secs_f64();

        println!("Scoped verification: {} ops/sec", ops_per_sec as u64);
        assert!(ops_per_sec > 500.0, "Scoped verification too slow");
    }
}

// =========================================================================
// JSON CANONICALIZATION BENCHMARKS
// =========================================================================

mod json_canonicalization {
    use super::*;

    #[test]
    fn test_small_json_canonicalization() {
        let json = r#"{"key":"value"}"#;

        let iterations = 50000;
        let start = Instant::now();

        for _ in 0..iterations {
            let _ = ash_canonicalize_json(json).unwrap();
        }

        let elapsed = start.elapsed();
        let ops_per_sec = iterations as f64 / elapsed.as_secs_f64();

        println!("Small JSON canonicalization: {} ops/sec", ops_per_sec as u64);
        assert!(ops_per_sec > 5000.0, "Small JSON canonicalization too slow");
    }

    #[test]
    fn test_medium_json_canonicalization() {
        let json = r#"{"user":{"name":"John","email":"john@example.com","age":30},"items":[{"id":1,"name":"Item1"},{"id":2,"name":"Item2"}],"metadata":{"created":"2024-01-01","updated":"2024-01-02"}}"#;

        let iterations = 20000;
        let start = Instant::now();

        for _ in 0..iterations {
            let _ = ash_canonicalize_json(json).unwrap();
        }

        let elapsed = start.elapsed();
        let ops_per_sec = iterations as f64 / elapsed.as_secs_f64();

        println!("Medium JSON canonicalization: {} ops/sec", ops_per_sec as u64);
        assert!(ops_per_sec > 1000.0, "Medium JSON canonicalization too slow");
    }

    #[test]
    fn test_large_json_canonicalization() {
        // Create a larger JSON with many keys
        let mut json = String::from("{");
        for i in 0..100 {
            if i > 0 { json.push(','); }
            json.push_str(&format!(r#""key{}":{}"#, i, i));
        }
        json.push('}');

        let iterations = 5000;
        let start = Instant::now();

        for _ in 0..iterations {
            let _ = ash_canonicalize_json(&json).unwrap();
        }

        let elapsed = start.elapsed();
        let ops_per_sec = iterations as f64 / elapsed.as_secs_f64();

        println!("Large JSON canonicalization: {} ops/sec", ops_per_sec as u64);
        assert!(ops_per_sec > 1000.0, "Large JSON canonicalization too slow");
    }
}

// =========================================================================
// QUERY CANONICALIZATION BENCHMARKS
// =========================================================================

mod query_canonicalization {
    use super::*;

    #[test]
    fn test_simple_query_canonicalization() {
        let query = "a=1&b=2";

        let iterations = 100000;
        let start = Instant::now();

        for _ in 0..iterations {
            let _ = ash_canonicalize_query(query).unwrap();
        }

        let elapsed = start.elapsed();
        let ops_per_sec = iterations as f64 / elapsed.as_secs_f64();

        println!("Simple query canonicalization: {} ops/sec", ops_per_sec as u64);
        assert!(ops_per_sec > 10000.0, "Simple query canonicalization too slow");
    }

    #[test]
    fn test_complex_query_canonicalization() {
        let query = "z=3&a=1&m=2&page=1&limit=10&sort=desc&filter=active&include=metadata";

        let iterations = 50000;
        let start = Instant::now();

        for _ in 0..iterations {
            let _ = ash_canonicalize_query(query).unwrap();
        }

        let elapsed = start.elapsed();
        let ops_per_sec = iterations as f64 / elapsed.as_secs_f64();

        println!("Complex query canonicalization: {} ops/sec", ops_per_sec as u64);
        assert!(ops_per_sec > 2000.0, "Complex query canonicalization too slow");
    }
}

// =========================================================================
// HASHING BENCHMARKS
// =========================================================================

mod hashing {
    use super::*;

    #[test]
    fn test_small_body_hash() {
        let body = "small body content";

        let iterations = 100000;
        let start = Instant::now();

        for _ in 0..iterations {
            let _ = ash_hash_body(body);
        }

        let elapsed = start.elapsed();
        let ops_per_sec = iterations as f64 / elapsed.as_secs_f64();

        println!("Small body hash: {} ops/sec", ops_per_sec as u64);
        assert!(ops_per_sec > 10000.0, "Small body hashing too slow");
    }

    #[test]
    fn test_10kb_body_hash() {
        let body = "x".repeat(10 * 1024);

        let iterations = 10000;
        let start = Instant::now();

        for _ in 0..iterations {
            let _ = ash_hash_body(&body);
        }

        let elapsed = start.elapsed();
        let ops_per_sec = iterations as f64 / elapsed.as_secs_f64();

        println!("10KB body hash: {} ops/sec", ops_per_sec as u64);
        assert!(ops_per_sec > 500.0, "10KB body hashing too slow");
    }

    #[test]
    fn test_100kb_body_hash() {
        let body = "x".repeat(100 * 1024);

        let iterations = 2000;
        let start = Instant::now();

        for _ in 0..iterations {
            let _ = ash_hash_body(&body);
        }

        let elapsed = start.elapsed();
        let ops_per_sec = iterations as f64 / elapsed.as_secs_f64();

        println!("100KB body hash: {} ops/sec", ops_per_sec as u64);
        assert!(ops_per_sec > 50.0, "100KB body hashing too slow");
    }
}

// =========================================================================
// CLIENT SECRET DERIVATION BENCHMARKS
// =========================================================================

mod secret_derivation {
    use super::*;

    #[test]
    fn test_secret_derivation_performance() {
        let nonce = "a".repeat(64);

        let iterations = 20000;
        let start = Instant::now();

        for i in 0..iterations {
            let context = format!("ctx_{}", i);
            let binding = format!("GET|/api/{}|", i);
            let _ = ash_derive_client_secret(&nonce, &context, &binding).unwrap();
        }

        let elapsed = start.elapsed();
        let ops_per_sec = iterations as f64 / elapsed.as_secs_f64();

        println!("Secret derivation: {} ops/sec", ops_per_sec as u64);
        assert!(ops_per_sec > 10000.0, "Secret derivation too slow");
    }
}

// =========================================================================
// NONCE GENERATION BENCHMARKS
// =========================================================================

mod nonce_generation {
    use super::*;

    #[test]
    fn test_nonce_generation_performance() {
        let iterations = 50000;
        let start = Instant::now();

        for _ in 0..iterations {
            let _ = ash_generate_nonce(32);
        }

        let elapsed = start.elapsed();
        let ops_per_sec = iterations as f64 / elapsed.as_secs_f64();

        println!("Nonce generation: {} ops/sec", ops_per_sec as u64);
        assert!(ops_per_sec > 50000.0, "Nonce generation too slow");
    }

    #[test]
    fn test_context_id_generation_performance() {
        let iterations = 50000;
        let start = Instant::now();

        for _ in 0..iterations {
            let _ = ash_generate_context_id();
        }

        let elapsed = start.elapsed();
        let ops_per_sec = iterations as f64 / elapsed.as_secs_f64();

        println!("Context ID generation: {} ops/sec", ops_per_sec as u64);
        assert!(ops_per_sec > 50000.0, "Context ID generation too slow");
    }
}

// =========================================================================
// END-TO-END WORKFLOW BENCHMARKS
// =========================================================================

mod end_to_end {
    use super::*;

    #[test]
    fn test_full_workflow_performance() {
        let iterations = 5000;
        let start = Instant::now();

        for i in 0..iterations {
            // Generate nonce
            let nonce = ash_generate_nonce(32).unwrap();

            // Generate context
            let context_id = ash_generate_context_id().unwrap();
            let binding = "POST|/api/data|";

            // Derive secret
            let secret = ash_derive_client_secret(&nonce, &context_id, binding).unwrap();

            // Hash body
            let body = format!(r#"{{"index":{}}}"#, i);
            let body_hash = ash_hash_body(&body);

            // Build proof
            let timestamp = format!("{}", 1700000000 + i);
            let proof = ash_build_proof(&secret, &timestamp, binding, &body_hash).unwrap();

            // Verify proof
            let valid = ash_verify_proof(&nonce, &context_id, binding, &timestamp, &body_hash, &proof).unwrap();
            assert!(valid);
        }

        let elapsed = start.elapsed();
        let ops_per_sec = iterations as f64 / elapsed.as_secs_f64();

        println!("Full workflow: {} ops/sec", ops_per_sec as u64);
        assert!(ops_per_sec > 2000.0, "Full workflow too slow");
    }
}

// =========================================================================
// MEMORY STABILITY
// =========================================================================

mod memory_stability {
    use super::*;

    #[test]
    fn test_no_memory_growth() {
        // Run many operations and verify we don't run out of memory
        let nonce = "a".repeat(64);
        let secret = ash_derive_client_secret(&nonce, "ctx", "POST|/|").unwrap();
        let body_hash = "b".repeat(64);

        for i in 0..100000 {
            let timestamp = format!("{}", 1700000000 + i);
            let proof = ash_build_proof(&secret, &timestamp, "POST|/|", &body_hash).unwrap();
            let _ = ash_verify_proof(&nonce, "ctx", "POST|/|", &timestamp, &body_hash, &proof);

            // Drop proof each iteration - should not accumulate memory
        }

        // If we get here without OOM, memory is stable
    }

    #[test]
    fn test_json_canonicalization_memory_stability() {
        for i in 0..50000 {
            let json = format!(r#"{{"index":{}}}"#, i);
            let _ = ash_canonicalize_json(&json);
        }
    }
}