MCPcopy Create free account
hub / github.com/ElementsProject/lightning / main

Function main

bitcoin/test/run-secret_eq_consttime.c:111–163  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

109#define ITERATIONS 1000
110
111int main(int argc, char *argv[])
112{
113 const char *v;
114 int const_success, nonconst_success = ITERATIONS, i;
115
116 common_setup(argv[0]);
117
118 /* no point running this under valgrind. */
119 v = getenv("VALGRIND");
120 if (v && atoi(v) == 1)
121 goto exit;
122
123 /* this sometimes trips under CI */
124 v = getenv("SLOW_MACHINE");
125 if (v && atoi(v) == 1)
126 goto exit;
127
128 s1 = calloc(RUNS, sizeof(*s1));
129 s2 = calloc(RUNS, sizeof(*s2));
130
131 /* When not loaded, this should pass over 50% of the time. */
132 const_success = 0;
133 for (i = 0; i < ITERATIONS; i++)
134 const_success += secret_time_test(const_time_test, true);
135
136 printf("=> Within 5%% %u/%u times\n", const_success, i);
137
138 /* This fails without -O2 or above, at least here (x86 Ubuntu gcc 7.3) */
139 if (strstr(COPTFLAGS, "-O2") || strstr(COPTFLAGS, "-O3")) {
140 /* Should show measurable differences at least 1/2 the time. */
141 nonconst_success = 0;
142 for (i = 0; i < 1000; i++)
143 nonconst_success
144 += secret_time_test(nonconst_time_test, false);
145
146 printf("=> More than 5%% slower %u/%u times\n",
147 nonconst_success, i);
148 }
149
150 /* Now, check loadavg: if we weren't alone, that could explain results */
151 if (const_success < ITERATIONS / 2)
152 errx(1, "Only const time %u/%u?", const_success, i);
153
154 if (nonconst_success < ITERATIONS / 2)
155 errx(1, "memcmp seemed const time %u/%u?",
156 nonconst_success, i);
157 free(s1);
158 free(s2);
159
160exit:
161 common_shutdown();
162 return 0;
163}

Callers

nothing calls this directly

Calls 4

common_setupFunction · 0.85
secret_time_testFunction · 0.85
errxFunction · 0.85
common_shutdownFunction · 0.85

Tested by

no test coverage detected