Skip to content

Commit

Permalink
add 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 33026ed commit 0b1c7ad
Show file tree
Hide file tree
Showing 2 changed files with 30 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
25 changes: 25 additions & 0 deletions src/selftest.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,31 @@ 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;
__asm__ __volatile__("cpuid\n"
: "=b"(flags)
: "a"(CPU_FLAG_ENUMERATION), "c"(LEAF_NODE_ZERO)
: "rdx", "rbx", "cc");

int has_adx = (flags >> BIT_ADX) & 1;
int 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 0b1c7ad

Please sign in to comment.