1 module dcrypt.benchmark.AEADCipherBenchmark;
2 
3 import dcrypt.benchmark.Benchmark;
4 import dcrypt.aead.aead;
5 import std.algorithm: swap;
6 
7 public class AEADCipherBenchmark: Benchmark {
8 	
9 	private IAEADEngine cipher;
10 	 
11 	this (IAEADEngine c){
12 		cipher = c;
13 	}
14 
15 	@property
16 	static string[] header() {
17 		return ["algorithm", "AAD MB/s", "encrypt MB/s", "decrypt MB/s"];
18 	}
19 
20 	override string[] benchmark(ulong length) {
21 
22 		cipher.start(true, new ubyte[16], new ubyte[16]);
23 
24 
25 		double aadSpeed = getSpeed(length)*1e-6;
26 
27 		double encrSpeed = getSpeed(length)*1e-6;
28 
29 		cipher.start(true, new ubyte[16], new ubyte[16]);
30 		
31 		//double decrSpeed = getSpeed(length)*1e-6;
32 
33 		return [cipher.name, numberFormat(aadSpeed), numberFormat(encrSpeed)];
34 			 //decrSpeed);
35 	}
36 
37 	private double getAADSpeed(ulong length) {
38 		ubyte[64] blockA;
39 		StopWatch sw;
40 		sw.reset();
41 		sw.start();
42 		foreach(size_t i; 0 .. length/blockA.length) {
43 			cipher.processAADBytes(blockA);
44 		}
45 		sw.stop();
46 		
47 		double speed = 1e9 * length / sw.peek().nsecs();
48 		return speed;
49 	}
50 	
51 	private double getSpeed(ulong length) {
52 		ubyte[64] blockA;
53 		ubyte[128] blockB;
54 		StopWatch sw;
55 		sw.reset();
56 		sw.start();
57 		foreach(size_t i; 0 .. length/blockA.length) {
58 			cipher.processBytes(blockA, blockB);
59 		}
60 		ubyte[16] macBuf;
61 		cipher.finish(macBuf, blockB);
62 		sw.stop();
63 		
64 		double speed = 1e9 * length / sw.peek().nsecs();
65 		return speed;
66 	}
67 	
68 }