Add tests for the cmov implementations
This commit is contained in:
parent
73596a85a2
commit
28609507e7
157
src/tests.c
157
src/tests.c
|
@ -5292,6 +5292,161 @@ void run_memczero_test(void) {
|
|||
CHECK(memcmp(buf1, buf2, sizeof(buf1)) == 0);
|
||||
}
|
||||
|
||||
void int_cmov_test(void) {
|
||||
int r = INT_MAX;
|
||||
int a = 0;
|
||||
|
||||
secp256k1_int_cmov(&r, &a, 0);
|
||||
CHECK(r == INT_MAX);
|
||||
|
||||
r = 0; a = INT_MAX;
|
||||
secp256k1_int_cmov(&r, &a, 1);
|
||||
CHECK(r == INT_MAX);
|
||||
|
||||
a = 0;
|
||||
secp256k1_int_cmov(&r, &a, 1);
|
||||
CHECK(r == 0);
|
||||
|
||||
a = 1;
|
||||
secp256k1_int_cmov(&r, &a, 1);
|
||||
CHECK(r == 1);
|
||||
|
||||
r = 1; a = 0;
|
||||
secp256k1_int_cmov(&r, &a, 0);
|
||||
CHECK(r == 1);
|
||||
|
||||
}
|
||||
|
||||
void fe_cmov_test(void) {
|
||||
static const secp256k1_fe zero = SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 0);
|
||||
static const secp256k1_fe one = SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 1);
|
||||
static const secp256k1_fe max = SECP256K1_FE_CONST(
|
||||
0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL,
|
||||
0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL
|
||||
);
|
||||
secp256k1_fe r = max;
|
||||
secp256k1_fe a = zero;
|
||||
|
||||
secp256k1_fe_cmov(&r, &a, 0);
|
||||
CHECK(memcmp(&r, &max, sizeof(r)) == 0);
|
||||
|
||||
r = zero; a = max;
|
||||
secp256k1_fe_cmov(&r, &a, 1);
|
||||
CHECK(memcmp(&r, &max, sizeof(r)) == 0);
|
||||
|
||||
a = zero;
|
||||
secp256k1_fe_cmov(&r, &a, 1);
|
||||
CHECK(memcmp(&r, &zero, sizeof(r)) == 0);
|
||||
|
||||
a = one;
|
||||
secp256k1_fe_cmov(&r, &a, 1);
|
||||
CHECK(memcmp(&r, &one, sizeof(r)) == 0);
|
||||
|
||||
r = one; a = zero;
|
||||
secp256k1_fe_cmov(&r, &a, 0);
|
||||
CHECK(memcmp(&r, &one, sizeof(r)) == 0);
|
||||
}
|
||||
|
||||
void fe_storage_cmov_test(void) {
|
||||
static const secp256k1_fe_storage zero = SECP256K1_FE_STORAGE_CONST(0, 0, 0, 0, 0, 0, 0, 0);
|
||||
static const secp256k1_fe_storage one = SECP256K1_FE_STORAGE_CONST(0, 0, 0, 0, 0, 0, 0, 1);
|
||||
static const secp256k1_fe_storage max = SECP256K1_FE_STORAGE_CONST(
|
||||
0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL,
|
||||
0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL
|
||||
);
|
||||
secp256k1_fe_storage r = max;
|
||||
secp256k1_fe_storage a = zero;
|
||||
|
||||
secp256k1_fe_storage_cmov(&r, &a, 0);
|
||||
CHECK(memcmp(&r, &max, sizeof(r)) == 0);
|
||||
|
||||
r = zero; a = max;
|
||||
secp256k1_fe_storage_cmov(&r, &a, 1);
|
||||
CHECK(memcmp(&r, &max, sizeof(r)) == 0);
|
||||
|
||||
a = zero;
|
||||
secp256k1_fe_storage_cmov(&r, &a, 1);
|
||||
CHECK(memcmp(&r, &zero, sizeof(r)) == 0);
|
||||
|
||||
a = one;
|
||||
secp256k1_fe_storage_cmov(&r, &a, 1);
|
||||
CHECK(memcmp(&r, &one, sizeof(r)) == 0);
|
||||
|
||||
r = one; a = zero;
|
||||
secp256k1_fe_storage_cmov(&r, &a, 0);
|
||||
CHECK(memcmp(&r, &one, sizeof(r)) == 0);
|
||||
}
|
||||
|
||||
void scalar_cmov_test(void) {
|
||||
static const secp256k1_scalar zero = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 0);
|
||||
static const secp256k1_scalar one = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 1);
|
||||
static const secp256k1_scalar max = SECP256K1_SCALAR_CONST(
|
||||
0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL,
|
||||
0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL
|
||||
);
|
||||
secp256k1_scalar r = max;
|
||||
secp256k1_scalar a = zero;
|
||||
|
||||
secp256k1_scalar_cmov(&r, &a, 0);
|
||||
CHECK(memcmp(&r, &max, sizeof(r)) == 0);
|
||||
|
||||
r = zero; a = max;
|
||||
secp256k1_scalar_cmov(&r, &a, 1);
|
||||
CHECK(memcmp(&r, &max, sizeof(r)) == 0);
|
||||
|
||||
a = zero;
|
||||
secp256k1_scalar_cmov(&r, &a, 1);
|
||||
CHECK(memcmp(&r, &zero, sizeof(r)) == 0);
|
||||
|
||||
a = one;
|
||||
secp256k1_scalar_cmov(&r, &a, 1);
|
||||
CHECK(memcmp(&r, &one, sizeof(r)) == 0);
|
||||
|
||||
r = one; a = zero;
|
||||
secp256k1_scalar_cmov(&r, &a, 0);
|
||||
CHECK(memcmp(&r, &one, sizeof(r)) == 0);
|
||||
}
|
||||
|
||||
void ge_storage_cmov_test(void) {
|
||||
static const secp256k1_ge_storage zero = SECP256K1_GE_STORAGE_CONST(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
||||
static const secp256k1_ge_storage one = SECP256K1_GE_STORAGE_CONST(0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1);
|
||||
static const secp256k1_ge_storage max = SECP256K1_GE_STORAGE_CONST(
|
||||
0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL,
|
||||
0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL,
|
||||
0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL,
|
||||
0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL
|
||||
);
|
||||
secp256k1_ge_storage r = max;
|
||||
secp256k1_ge_storage a = zero;
|
||||
|
||||
secp256k1_ge_storage_cmov(&r, &a, 0);
|
||||
CHECK(memcmp(&r, &max, sizeof(r)) == 0);
|
||||
|
||||
r = zero; a = max;
|
||||
secp256k1_ge_storage_cmov(&r, &a, 1);
|
||||
CHECK(memcmp(&r, &max, sizeof(r)) == 0);
|
||||
|
||||
a = zero;
|
||||
secp256k1_ge_storage_cmov(&r, &a, 1);
|
||||
CHECK(memcmp(&r, &zero, sizeof(r)) == 0);
|
||||
|
||||
a = one;
|
||||
secp256k1_ge_storage_cmov(&r, &a, 1);
|
||||
CHECK(memcmp(&r, &one, sizeof(r)) == 0);
|
||||
|
||||
r = one; a = zero;
|
||||
secp256k1_ge_storage_cmov(&r, &a, 0);
|
||||
CHECK(memcmp(&r, &one, sizeof(r)) == 0);
|
||||
}
|
||||
|
||||
void run_cmov_tests(void) {
|
||||
int_cmov_test();
|
||||
fe_cmov_test();
|
||||
fe_storage_cmov_test();
|
||||
scalar_cmov_test();
|
||||
ge_storage_cmov_test();
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
unsigned char seed16[16] = {0};
|
||||
unsigned char run32[32] = {0};
|
||||
|
@ -5431,6 +5586,8 @@ int main(int argc, char **argv) {
|
|||
/* util tests */
|
||||
run_memczero_test();
|
||||
|
||||
run_cmov_tests();
|
||||
|
||||
secp256k1_rand256(run32);
|
||||
printf("random run = %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n", run32[0], run32[1], run32[2], run32[3], run32[4], run32[5], run32[6], run32[7], run32[8], run32[9], run32[10], run32[11], run32[12], run32[13], run32[14], run32[15]);
|
||||
|
||||
|
|
Loading…
Reference in New Issue