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

Function secret_time_test

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

Returns true if test result is expected: we consider 5% "same". */

Source from the content-addressed store, hash-verified

78
79/* Returns true if test result is expected: we consider 5% "same". */
80static bool secret_time_test(struct timerel (*test)(struct secret *s1,
81 struct secret *s2,
82 size_t off),
83 bool should_be_const)
84{
85 struct timerel firstbyte_time, lastbyte_time, diff;
86
87 firstbyte_time = test(s1, s2, 0);
88 lastbyte_time = test(s1, s2, sizeof(s1->data)-1);
89
90 if (verbose)
91 printf("First byte %u psec vs last byte %u psec\n",
92 (int)time_to_nsec(time_divide(firstbyte_time, RUNS/1000)),
93 (int)time_to_nsec(time_divide(lastbyte_time, RUNS/1000)));
94
95 /* If they differ by more than 5%, get upset. */
96 if (time_less(firstbyte_time, lastbyte_time))
97 diff = time_sub(lastbyte_time, firstbyte_time);
98 else {
99 /* If the lastbyte test was faster, that's a fail it we expected
100 * it to be slower... */
101 if (!should_be_const)
102 return false;
103 diff = time_sub(firstbyte_time, lastbyte_time);
104 }
105
106 return time_less(time_multiply(diff, 20), firstbyte_time) == should_be_const;
107}
108
109#define ITERATIONS 1000
110

Callers 1

mainFunction · 0.85

Calls 6

time_to_nsecFunction · 0.85
time_divideFunction · 0.85
time_lessFunction · 0.85
time_subFunction · 0.85
time_multiplyFunction · 0.85
testFunction · 0.50

Tested by

no test coverage detected