From 8909fb3df6fd282d6b8f24b288ef5d7ddbdb741a Mon Sep 17 00:00:00 2001 From: David Monniaux Date: Tue, 22 Jan 2019 09:46:30 +0100 Subject: SHA-2 from https://github.com/amosnier/sha-2 --- test/monniaux/sha-2/sha-256_run.c | 238 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 238 insertions(+) create mode 100644 test/monniaux/sha-2/sha-256_run.c (limited to 'test/monniaux/sha-2/sha-256_run.c') diff --git a/test/monniaux/sha-2/sha-256_run.c b/test/monniaux/sha-2/sha-256_run.c new file mode 100644 index 00000000..5b5031c3 --- /dev/null +++ b/test/monniaux/sha-2/sha-256_run.c @@ -0,0 +1,238 @@ +#include +#include +#include +#include + +#include "sha-256.h" + +struct string_vector { + const char *input; + const char *output; +}; + +static const struct string_vector STRING_VECTORS[] = { + { + "", + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + }, + { + "abc", + "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" + }, + { + "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", + "a8ae6e6ee929abea3afcfc5258c8ccd6f85273e0d4626d26c7279f3250f77c8e" + }, + { + "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde", + "057ee79ece0b9a849552ab8d3c335fe9a5f1c46ef5f1d9b190c295728628299c" + }, + { + "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0", + "2a6ad82f3620d3ebe9d678c812ae12312699d673240d5be8fac0910a70000d93" + }, + { + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1" + }, + { + "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno" + "ijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", + "cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1" + } +}; + +#define LARGE_MESSAGES 1 + +static uint8_t data1[] = { 0xbd }; +static uint8_t data2[] = { 0xc9, 0x8c, 0x8e, 0x55 }; +static uint8_t data7[1000]; +static uint8_t data8[1000]; +static uint8_t data9[1005]; +#if LARGE_MESSAGES +#define SIZEOF_DATA11 536870912 +#define SIZEOF_DATA12 1090519040 +#define SIZEOF_DATA13 1610612798 +static uint8_t * data11; +static uint8_t * data12; +static uint8_t * data13; +#endif + +struct vector { + const uint8_t *input; + size_t input_len; + const char *output; +}; + +static struct vector vectors[] = { + { + data1, + sizeof data1, + "68325720aabd7c82f30f554b313d0570c95accbb7dc4b5aae11204c08ffe732b" + }, + { + data2, + sizeof data2, + "7abc22c0ae5af26ce93dbb94433a0e0b2e119d014f8e7f65bd56c61ccccd9504" + }, + { + data7, + 55, + "02779466cdec163811d078815c633f21901413081449002f24aa3e80f0b88ef7" + }, + { + data7, + 56, + "d4817aa5497628e7c77e6b606107042bbba3130888c5f47a375e6179be789fbb" + }, + { + data7, + 57, + "65a16cb7861335d5ace3c60718b5052e44660726da4cd13bb745381b235a1785" + }, + { + data7, + 64, + "f5a5fd42d16a20302798ef6ed309979b43003d2320d9f0e8ea9831a92759fb4b" + }, + { + data7, + sizeof data7, + "541b3e9daa09b20bf85fa273e5cbd3e80185aa4ec298e765db87742b70138a53" + }, + { + data8, + sizeof data8, + "c2e686823489ced2017f6059b8b239318b6364f6dcd835d0a519105a1eadd6e4" + }, + { + data9, + sizeof data9, + "f4d62ddec0f3dd90ea1380fa16a5ff8dc4c54b21740650f24afc4120903552b0" + } +#if LARGE_MESSAGES + , + { + NULL, + 1000000, + "d29751f2649b32ff572b5e0a9f541ea660a50f94ff0beedfb0b692b924cc8025" + }, + { + NULL, + SIZEOF_DATA11, + "15a1868c12cc53951e182344277447cd0979536badcc512ad24c67e9b2d4f3dd" + }, + { + NULL, + SIZEOF_DATA12, + "461c19a93bd4344f9215f5ec64357090342bc66b15a148317d276e31cbc20b53" + }, + { + NULL, + SIZEOF_DATA13, + "c23ce8a7895f4b21ec0daf37920ac0a262a220045a03eb2dfed48ef9b05aabea" + } +#endif +}; + +static void construct_binary_messages(void) +{ + memset(data7, 0x00, sizeof data7); + memset(data8, 0x41, sizeof data8); + memset(data9, 0x55, sizeof data9); +#if LARGE_MESSAGES + /* + * Heap allocation as a workaround for some linkers not liking + * large BSS segments. + */ + data11 = malloc(SIZEOF_DATA11); + data12 = malloc(SIZEOF_DATA12); + data13 = malloc(SIZEOF_DATA13); + memset(data11, 0x5a, SIZEOF_DATA11); + memset(data12, 0x00, SIZEOF_DATA12); + memset(data13, 0x42, SIZEOF_DATA13); + vectors[9].input = data12; + vectors[10].input = data11; + vectors[11].input = data12; + vectors[12].input = data13; +#endif +} + +static void destruct_binary_messages(void) +{ +#if LARGE_MESSAGES + free(data11); + free(data12); + free(data13); +#endif +} + +static void hash_to_string(char string[65], const uint8_t hash[32]) +{ + size_t i; + for (i = 0; i < 32; i++) { + string += sprintf(string, "%02x", hash[i]); + } +} + +static int string_test(const char input[], const char output[]) +{ + uint8_t hash[32]; + char hash_string[65]; + calc_sha_256(hash, input, strlen(input)); + hash_to_string(hash_string, hash); + printf("input: %s\n", input); + printf("hash : %s\n", hash_string); + if (strcmp(output, hash_string)) { + printf("FAILURE!\n\n"); + return 1; + } else { + printf("SUCCESS!\n\n"); + return 0; + } +} + +/* + * Limitation: + * - The variable input_len will be truncated to its LONG_BIT least + * significant bits in the print output. This will never be a problem + * for values that in practice are less than 2^32 - 1. Rationale: ANSI + * C-compatibility and keeping it simple. + */ +static int test(const uint8_t * input, size_t input_len, const char output[]) +{ + uint8_t hash[32]; + char hash_string[65]; + calc_sha_256(hash, input, input_len); + hash_to_string(hash_string, hash); + printf("input starts with 0x%02x, length %lu\n", *input, (unsigned long) input_len); + printf("hash : %s\n", hash_string); + if (strcmp(output, hash_string)) { + printf("FAILURE!\n\n"); + return 1; + } else { + printf("SUCCESS!\n\n"); + return 0; + } +} + +int main(void) +{ + size_t i; + for (i = 0; i < (sizeof STRING_VECTORS / sizeof (struct string_vector)); i++) { + const struct string_vector *vector = &STRING_VECTORS[i]; + if (string_test(vector->input, vector->output)) + return 1; + } + construct_binary_messages(); + for (i = 0; i < (sizeof vectors / sizeof (struct vector)); i++) { + const struct vector *vector = &vectors[i]; + if (test(vector->input, vector->input_len, vector->output)) + { + destruct_binary_messages(); + return 1; + } + } + destruct_binary_messages(); + return 0; +} -- cgit From fbcbdb2585263ab5156660347bbe6ad3376221a0 Mon Sep 17 00:00:00 2001 From: David Monniaux Date: Tue, 22 Jan 2019 18:35:57 +0100 Subject: check malloc() return values --- test/monniaux/sha-2/sha-256_run.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'test/monniaux/sha-2/sha-256_run.c') diff --git a/test/monniaux/sha-2/sha-256_run.c b/test/monniaux/sha-2/sha-256_run.c index 5b5031c3..68fbcd3e 100644 --- a/test/monniaux/sha-2/sha-256_run.c +++ b/test/monniaux/sha-2/sha-256_run.c @@ -135,6 +135,15 @@ static struct vector vectors[] = { #endif }; +static void *my_malloc(size_t size) { + void *p=malloc(size); + if (p==0) { + fprintf(stderr, "malloc(%zu) failed\n", size); + abort(); + } + return p; +} + static void construct_binary_messages(void) { memset(data7, 0x00, sizeof data7); @@ -145,9 +154,9 @@ static void construct_binary_messages(void) * Heap allocation as a workaround for some linkers not liking * large BSS segments. */ - data11 = malloc(SIZEOF_DATA11); - data12 = malloc(SIZEOF_DATA12); - data13 = malloc(SIZEOF_DATA13); + data11 = my_malloc(SIZEOF_DATA11); + data12 = my_malloc(SIZEOF_DATA12); + data13 = my_malloc(SIZEOF_DATA13); memset(data11, 0x5a, SIZEOF_DATA11); memset(data12, 0x00, SIZEOF_DATA12); memset(data13, 0x42, SIZEOF_DATA13); -- cgit From 0511adf1024647271e230ca13c6f8801d0313efd Mon Sep 17 00:00:00 2001 From: David Monniaux Date: Tue, 22 Jan 2019 18:50:28 +0100 Subject: sha-2 benchmark works --- test/monniaux/sha-2/sha-256_run.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'test/monniaux/sha-2/sha-256_run.c') diff --git a/test/monniaux/sha-2/sha-256_run.c b/test/monniaux/sha-2/sha-256_run.c index 68fbcd3e..6017712c 100644 --- a/test/monniaux/sha-2/sha-256_run.c +++ b/test/monniaux/sha-2/sha-256_run.c @@ -2,7 +2,8 @@ #include #include #include - +#include +#include "../cycles.h" #include "sha-256.h" struct string_vector { @@ -42,7 +43,7 @@ static const struct string_vector STRING_VECTORS[] = { } }; -#define LARGE_MESSAGES 1 +#define LARGE_MESSAGES 0 static uint8_t data1[] = { 0xbd }; static uint8_t data2[] = { 0xc9, 0x8c, 0x8e, 0x55 }; @@ -135,6 +136,7 @@ static struct vector vectors[] = { #endif }; +#if LARGE_MESSAGES static void *my_malloc(size_t size) { void *p=malloc(size); if (p==0) { @@ -143,6 +145,7 @@ static void *my_malloc(size_t size) { } return p; } +#endif static void construct_binary_messages(void) { @@ -183,12 +186,26 @@ static void hash_to_string(char string[65], const uint8_t hash[32]) string += sprintf(string, "%02x", hash[i]); } } - + +static cycle_t cycle_total, cycle_start_time; + +static void cycle_count_start(void) { + cycle_start_time=get_cycle(); +} + +static void cycle_count_end(void) { + cycle_total += get_cycle()-cycle_start_time; +} + static int string_test(const char input[], const char output[]) { uint8_t hash[32]; char hash_string[65]; + + cycle_count_start(); calc_sha_256(hash, input, strlen(input)); + cycle_count_end(); + hash_to_string(hash_string, hash); printf("input: %s\n", input); printf("hash : %s\n", hash_string); @@ -227,6 +244,7 @@ static int test(const uint8_t * input, size_t input_len, const char output[]) int main(void) { + cycle_count_config(); size_t i; for (i = 0; i < (sizeof STRING_VECTORS / sizeof (struct string_vector)); i++) { const struct string_vector *vector = &STRING_VECTORS[i]; @@ -243,5 +261,6 @@ int main(void) } } destruct_binary_messages(); + printf("total cycles = %" PRIu64 "\n", cycle_total); return 0; } -- cgit From 1451c1269ec670e068ac0a4c083b116e35ae2bea Mon Sep 17 00:00:00 2001 From: David Monniaux Date: Tue, 22 Jan 2019 19:54:53 +0100 Subject: correct measurement --- test/monniaux/sha-2/sha-256_run.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'test/monniaux/sha-2/sha-256_run.c') diff --git a/test/monniaux/sha-2/sha-256_run.c b/test/monniaux/sha-2/sha-256_run.c index 6017712c..546b7dbc 100644 --- a/test/monniaux/sha-2/sha-256_run.c +++ b/test/monniaux/sha-2/sha-256_run.c @@ -44,6 +44,7 @@ static const struct string_vector STRING_VECTORS[] = { }; #define LARGE_MESSAGES 0 +#define LARGER_MESSAGES 0 static uint8_t data1[] = { 0xbd }; static uint8_t data2[] = { 0xc9, 0x8c, 0x8e, 0x55 }; @@ -110,14 +111,14 @@ static struct vector vectors[] = { data9, sizeof data9, "f4d62ddec0f3dd90ea1380fa16a5ff8dc4c54b21740650f24afc4120903552b0" - } + }, #if LARGE_MESSAGES - , { NULL, 1000000, "d29751f2649b32ff572b5e0a9f541ea660a50f94ff0beedfb0b692b924cc8025" }, +#if LARGER_MESSAGES { NULL, SIZEOF_DATA11, @@ -134,6 +135,7 @@ static struct vector vectors[] = { "c23ce8a7895f4b21ec0daf37920ac0a262a220045a03eb2dfed48ef9b05aabea" } #endif +#endif }; #if LARGE_MESSAGES @@ -153,6 +155,7 @@ static void construct_binary_messages(void) memset(data8, 0x41, sizeof data8); memset(data9, 0x55, sizeof data9); #if LARGE_MESSAGES +#if LARGER_MESSAGES /* * Heap allocation as a workaround for some linkers not liking * large BSS segments. @@ -167,15 +170,23 @@ static void construct_binary_messages(void) vectors[10].input = data11; vectors[11].input = data12; vectors[12].input = data13; +#else + vectors[9].input = data12 = my_malloc(vectors[9].input_len); + memset(data12, 0x00, vectors[9].input_len); +#endif #endif } static void destruct_binary_messages(void) { #if LARGE_MESSAGES +#if LARGER_MESSAGES free(data11); free(data12); free(data13); +#else + free(data12); +#endif #endif } @@ -229,7 +240,11 @@ static int test(const uint8_t * input, size_t input_len, const char output[]) { uint8_t hash[32]; char hash_string[65]; + + cycle_count_start(); calc_sha_256(hash, input, input_len); + cycle_count_end(); + hash_to_string(hash_string, hash); printf("input starts with 0x%02x, length %lu\n", *input, (unsigned long) input_len); printf("hash : %s\n", hash_string); -- cgit From 6742e1ba68cac885895f3bddcca8fe9454c11e63 Mon Sep 17 00:00:00 2001 From: David Monniaux Date: Sun, 17 Mar 2019 05:18:21 +0100 Subject: longer tests --- test/monniaux/sha-2/sha-256_run.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'test/monniaux/sha-2/sha-256_run.c') diff --git a/test/monniaux/sha-2/sha-256_run.c b/test/monniaux/sha-2/sha-256_run.c index 546b7dbc..982fc538 100644 --- a/test/monniaux/sha-2/sha-256_run.c +++ b/test/monniaux/sha-2/sha-256_run.c @@ -43,7 +43,7 @@ static const struct string_vector STRING_VECTORS[] = { } }; -#define LARGE_MESSAGES 0 +#define LARGE_MESSAGES 1 #define LARGER_MESSAGES 0 static uint8_t data1[] = { 0xbd }; @@ -115,8 +115,12 @@ static struct vector vectors[] = { #if LARGE_MESSAGES { NULL, + /* too big 1000000, "d29751f2649b32ff572b5e0a9f541ea660a50f94ff0beedfb0b692b924cc8025" + */ + 50000, + "5b4b67b5d68e02c992760de07640472efe53a7f7553865f83262d0a74efc3e5d" }, #if LARGER_MESSAGES { -- cgit From b2fd964630a1e4964d97bfe84a550a701bb936b7 Mon Sep 17 00:00:00 2001 From: David Monniaux Date: Sun, 17 Mar 2019 09:23:07 +0100 Subject: isolate the slow part --- test/monniaux/sha-2/sha-256_run.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'test/monniaux/sha-2/sha-256_run.c') diff --git a/test/monniaux/sha-2/sha-256_run.c b/test/monniaux/sha-2/sha-256_run.c index 982fc538..c3865991 100644 --- a/test/monniaux/sha-2/sha-256_run.c +++ b/test/monniaux/sha-2/sha-256_run.c @@ -268,15 +268,15 @@ int main(void) for (i = 0; i < (sizeof STRING_VECTORS / sizeof (struct string_vector)); i++) { const struct string_vector *vector = &STRING_VECTORS[i]; if (string_test(vector->input, vector->output)) - return 1; + {} /* DM return 1; */ } construct_binary_messages(); for (i = 0; i < (sizeof vectors / sizeof (struct vector)); i++) { const struct vector *vector = &vectors[i]; if (test(vector->input, vector->input_len, vector->output)) - { + { /* DM destruct_binary_messages(); - return 1; + return 1; */ } } destruct_binary_messages(); -- cgit From 76c41fd907a3f7e7d574da4c075f30656e3ede9f Mon Sep 17 00:00:00 2001 From: Cyril SIX Date: Fri, 17 May 2019 14:14:38 +0200 Subject: Measures to CSV done --- test/monniaux/sha-2/sha-256_run.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/monniaux/sha-2/sha-256_run.c') diff --git a/test/monniaux/sha-2/sha-256_run.c b/test/monniaux/sha-2/sha-256_run.c index c3865991..05a69d6b 100644 --- a/test/monniaux/sha-2/sha-256_run.c +++ b/test/monniaux/sha-2/sha-256_run.c @@ -280,6 +280,6 @@ int main(void) } } destruct_binary_messages(); - printf("total cycles = %" PRIu64 "\n", cycle_total); + printf("total cycles : %" PRIu64 "\n", cycle_total); return 0; } -- cgit From b288b587378984c3c419d26a13dcf93686d1b779 Mon Sep 17 00:00:00 2001 From: Cyril SIX Date: Wed, 17 Jul 2019 12:03:39 +0200 Subject: All working benches ported --- test/monniaux/sha-2/sha-256_run.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/monniaux/sha-2/sha-256_run.c') diff --git a/test/monniaux/sha-2/sha-256_run.c b/test/monniaux/sha-2/sha-256_run.c index 05a69d6b..1b6f7372 100644 --- a/test/monniaux/sha-2/sha-256_run.c +++ b/test/monniaux/sha-2/sha-256_run.c @@ -280,6 +280,6 @@ int main(void) } } destruct_binary_messages(); - printf("total cycles : %" PRIu64 "\n", cycle_total); + printf("time cycles: %" PRIu64 "\n", cycle_total); return 0; } -- cgit