Skip to content

Commit

Permalink
added runtime check for bmi2 and adx
Browse files Browse the repository at this point in the history
  • Loading branch information
dderjoel committed Jun 21, 2023
1 parent 0fd6f2a commit c010c15
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/secp256k1.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ static int secp256k1_context_is_proper(const secp256k1_context* ctx) {
}

void secp256k1_selftest(void) {
if (!SECP256K1_CHECKMEM_RUNNING()) {
if (!secp256k1_selftest_cpuid()) {
secp256k1_callback_call(&default_error_callback, "required CPU flags are not present.");
}
}
if (!secp256k1_selftest_passes()) {
secp256k1_callback_call(&default_error_callback, "self test failed");
}
Expand Down
28 changes: 28 additions & 0 deletions src/selftest.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,34 @@ static int secp256k1_selftest_sha256(void) {
return secp256k1_memcmp_var(out, output32, 32) == 0;
}

static int secp256k1_selftest_cpuid(void) {
int ret = 1;

#if defined(USE_ASM_X86_64)
/* getting the CPU flags from the cpu, more information in the Intel manual,
* Table 3-8 Information Returned by CPUID instruction (3-194, Vol.2A)
*/
const int CPU_FLAG_ENUMERATION = 7;
const int LEAF_NODE_ZERO = 0;

/* for the cpu self test, we need BMI2 and ADX support */
const int BIT_ADX = 19;
const int BIT_BMI2 = 8;
int flags = 0;
int has_adx = 0;
int has_bmi2 = 0;
__asm__ __volatile__("cpuid\n"
: "=b"(flags)
: "a"(CPU_FLAG_ENUMERATION), "c"(LEAF_NODE_ZERO)
: "rdx", "cc");

has_adx = (flags >> BIT_ADX) & 1;
has_bmi2 = (flags >> BIT_BMI2) & 1;
ret = has_adx && has_bmi2;
#endif
return ret;
}

static int secp256k1_selftest_passes(void) {
return secp256k1_selftest_sha256();
}
Expand Down

0 comments on commit c010c15

Please sign in to comment.