Ver código fonte

[test] Add speed tests for digest algorithms

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12 anos atrás
pai
commit
681a219caa

+ 35
- 0
src/tests/digest_test.c Ver arquivo

@@ -25,8 +25,10 @@ FILE_LICENCE ( GPL2_OR_LATER );
25 25
  *
26 26
  */
27 27
 
28
+#include <stdlib.h>
28 29
 #include <string.h>
29 30
 #include <ipxe/crypto.h>
31
+#include <ipxe/profile.h>
30 32
 #include "digest_test.h"
31 33
 
32 34
 /**
@@ -68,3 +70,36 @@ int digest_test ( struct digest_algorithm *digest,
68 70
 	/* Compare against expected output */
69 71
 	return ( memcmp ( expected, out, sizeof ( out ) ) == 0 );
70 72
 }
73
+
74
+/**
75
+ * Calculate digest algorithm cost
76
+ *
77
+ * @v digest		Digest algorithm
78
+ * @ret cost		Cost (in cycles per byte)
79
+ */
80
+unsigned long digest_cost ( struct digest_algorithm *digest ) {
81
+	static uint8_t random[8192]; /* Too large for stack */
82
+	uint8_t ctx[digest->ctxsize];
83
+	uint8_t out[digest->digestsize];
84
+	union profiler profiler;
85
+	unsigned long long elapsed;
86
+	unsigned long cost;
87
+	unsigned int i;
88
+
89
+	/* Fill buffer with pseudo-random data */
90
+	srand ( 0x1234568 );
91
+	for ( i = 0 ; i < sizeof ( random ) ; i++ )
92
+		random[i] = rand();
93
+
94
+	/* Time digest calculation */
95
+	profile ( &profiler );
96
+	digest_init ( digest, ctx );
97
+	digest_update ( digest, ctx, random, sizeof ( random ) );
98
+	digest_final ( digest, ctx, out );
99
+	elapsed = profile ( &profiler );
100
+
101
+	/* Round to nearest whole number of cycles per byte */
102
+	cost = ( ( elapsed + ( sizeof ( random ) / 2 ) ) / sizeof ( random ) );
103
+
104
+	return cost;
105
+}

+ 1
- 0
src/tests/digest_test.h Ver arquivo

@@ -19,6 +19,7 @@ struct digest_test_fragments {
19 19
 extern int digest_test ( struct digest_algorithm *digest,
20 20
 			 struct digest_test_fragments *fragments,
21 21
 			 void *data, size_t len, void *expected );
22
+extern unsigned long digest_cost ( struct digest_algorithm *digest );
22 23
 
23 24
 /**
24 25
  * Report digest test result

+ 6
- 0
src/tests/md5_test.c Ver arquivo

@@ -70,9 +70,11 @@ static struct digest_test_fragments md5_test_fragments[] = {
70 70
 static void md5_test_exec ( void ) {
71 71
 	struct digest_algorithm *digest = &md5_algorithm;
72 72
 	struct md5_test_vector *test;
73
+	unsigned long cost;
73 74
 	unsigned int i;
74 75
 	unsigned int j;
75 76
 
77
+	/* Correctness test */
76 78
 	for ( i = 0 ; i < ( sizeof ( md5_test_vectors ) /
77 79
 			    sizeof ( md5_test_vectors[0] ) ) ; i++ ) {
78 80
 		test = &md5_test_vectors[i];
@@ -85,6 +87,10 @@ static void md5_test_exec ( void ) {
85 87
 				    test->data, test->len, test->digest );
86 88
 		}
87 89
 	}
90
+
91
+	/* Speed test */
92
+	cost = digest_cost ( digest );
93
+	DBG ( "MD5 required %ld cycles per byte\n", cost );
88 94
 }
89 95
 
90 96
 /** MD5 self-test */

+ 6
- 0
src/tests/sha1_test.c Ver arquivo

@@ -75,9 +75,11 @@ static struct digest_test_fragments sha1_test_fragments[] = {
75 75
 static void sha1_test_exec ( void ) {
76 76
 	struct digest_algorithm *digest = &sha1_algorithm;
77 77
 	struct sha1_test_vector *test;
78
+	unsigned long cost;
78 79
 	unsigned int i;
79 80
 	unsigned int j;
80 81
 
82
+	/* Correctness test */
81 83
 	for ( i = 0 ; i < ( sizeof ( sha1_test_vectors ) /
82 84
 			    sizeof ( sha1_test_vectors[0] ) ) ; i++ ) {
83 85
 		test = &sha1_test_vectors[i];
@@ -90,6 +92,10 @@ static void sha1_test_exec ( void ) {
90 92
 				    test->data, test->len, test->digest );
91 93
 		}
92 94
 	}
95
+
96
+	/* Speed test */
97
+	cost = digest_cost ( digest );
98
+	DBG ( "SHA1 required %ld cycles per byte\n", cost );
93 99
 }
94 100
 
95 101
 /** SHA-1 self-test */

+ 6
- 0
src/tests/sha256_test.c Ver arquivo

@@ -78,9 +78,11 @@ static struct digest_test_fragments sha256_test_fragments[] = {
78 78
 static void sha256_test_exec ( void ) {
79 79
 	struct digest_algorithm *digest = &sha256_algorithm;
80 80
 	struct sha256_test_vector *test;
81
+	unsigned long cost;
81 82
 	unsigned int i;
82 83
 	unsigned int j;
83 84
 
85
+	/* Correctness test */
84 86
 	for ( i = 0 ; i < ( sizeof ( sha256_test_vectors ) /
85 87
 			    sizeof ( sha256_test_vectors[0] ) ) ; i++ ) {
86 88
 		test = &sha256_test_vectors[i];
@@ -93,6 +95,10 @@ static void sha256_test_exec ( void ) {
93 95
 				    test->data, test->len, test->digest );
94 96
 		}
95 97
 	}
98
+
99
+	/* Speed test */
100
+	cost = digest_cost ( digest );
101
+	DBG ( "SHA256 required %ld cycles per byte\n", cost );
96 102
 }
97 103
 
98 104
 /** SHA-256 self-test */

Carregando…
Cancelar
Salvar