Debian Codegraph @

tests/crypto/cnv4-jit.c

raw symbols SCIP
1 // Copyright (c) 2019-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #include <stdint.h>
30 #include <stddef.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
35 #include "crypto/hash-ops.h"
36
37 extern volatile int use_v4_jit_flag;
38
39 static int test(const uint8_t *data, size_t len, uint64_t height)
40 {
41 char hash0[32], hash1[32];
42 use_v4_jit_flag = 0;
43 cn_slow_hash(data, len, hash0, 4, 0, height);
44 use_v4_jit_flag = 1;
45 cn_slow_hash(data, len, hash1, 4, 0, height);
46 return memcmp(hash0, hash1, 32);
47 }
48
49 int main(int argc, char **argv)
50 {
51 uint8_t data[64];
52 uint64_t start_height = 1788000;
53 uint64_t end_height = 1788001;
54
55 if (argc != 1 && argc != 2 && argc != 3)
56 {
57 fprintf(stderr, "usage: %s [<start_height> [<end_height>]]\n", argv[0]);
58 return 1;
59 }
60 if (argc > 1)
61 {
62 errno = 0;
63 start_height = strtoull(argv[1], NULL, 10);
64 if ((start_height == 0 && errno) || start_height == ULLONG_MAX)
65 {
66 fprintf(stderr, "invalid start_height\n");
67 return 1;
68 }
69 end_height = start_height;
70 if (argc > 2)
71 {
72 errno = 0;
73 end_height = strtoull(argv[2], NULL, 10);
74 if ((end_height == 0 && errno) || end_height == ULLONG_MAX)
75 {
76 fprintf(stderr, "invalid end_height\n");
77 return 1;
78 }
79 }
80 }
81
82 if (start_height == end_height)
83 {
84 uint64_t counter = 0;
85 while (1)
86 {
87 printf("\r%llu", (unsigned long long)counter);
88 fflush(stdout);
89 size_t offset = 0;
90 while (offset + 8 < sizeof(data))
91 {
92 memcpy(data + offset, &counter, sizeof(counter));
93 offset += 8;
94 }
95 if (test(data, sizeof(data), start_height))
96 {
97 fprintf(stderr, "\nFailure at height %llu, counter %llu\n", (unsigned long long)start_height, (unsigned long long)counter);
98 return 0;
99 }
100 ++counter;
101 }
102 }
103
104 memset(data, 0x42, sizeof(data));
105 for (uint64_t h = start_height; h < end_height; ++h)
106 {
107 printf("\r%llu/%llu", (unsigned long long)(h-start_height), (unsigned long long)(end_height-start_height));
108 fflush(stdout);
109 if (test(data, sizeof(data), h))
110 {
111 fprintf(stderr, "\nFailure at height %llu\n", (unsigned long long)h);
112 return 0;
113 }
114 }
115
116 printf("\r");
117
118 return 0;
119 }