Convert test_lshift1 and test_rshift1 to FileTest.

This took some finesse. I merged the lshift1 and rshift1 test vectors as
one counted down and the other up. The rshift1 vectors were all rounded
to even numbers, with the test handling the odd case. Finally, each run
only tested positive or negative (it wasn't re-randomized), so I added
both positive and negative versions of each test vector.

BUG=31

Change-Id: Ic7de45ab797074547c44c2e4ff8089b1feec5d57
Reviewed-on: https://boringssl-review.googlesource.com/8522
Reviewed-by: Adam Langley <agl@google.com>
This commit is contained in:
David Benjamin 2016-06-26 15:18:28 -04:00 committed by Adam Langley
parent dca125efb5
commit 3058103d70
2 changed files with 1260 additions and 88 deletions

View File

@ -98,9 +98,7 @@ static const int num0 = 100; // number of tests
static const int num1 = 50; // additional tests for some functions
static const int num2 = 5; // number of tests for slow functions
static bool test_lshift1(FILE *fp);
static bool test_lshift(FILE *fp, BN_CTX *ctx, ScopedBIGNUM a);
static bool test_rshift1(FILE *fp);
static bool test_rshift(FILE *fp, BN_CTX *ctx);
static bool test_sqr(FILE *fp, BN_CTX *ctx);
static bool test_mul(FILE *fp);
@ -188,12 +186,6 @@ int main(int argc, char *argv[]) {
"| grep -v 0 */\n");
puts_fp(bc_file.get(), "obase=16\nibase=16\n");
message(bc_file.get(), "BN_lshift1");
if (!test_lshift1(bc_file.get())) {
return 1;
}
flush_fp(bc_file.get());
message(bc_file.get(), "BN_lshift (fixed)");
ScopedBIGNUM sample(BN_bin2bn(kSample, sizeof(kSample) - 1, NULL));
if (!sample) {
@ -210,12 +202,6 @@ int main(int argc, char *argv[]) {
}
flush_fp(bc_file.get());
message(bc_file.get(), "BN_rshift1");
if (!test_rshift1(bc_file.get())) {
return 1;
}
flush_fp(bc_file.get());
message(bc_file.get(), "BN_rshift");
if (!test_rshift(bc_file.get(), ctx.get())) {
return 1;
@ -312,7 +298,7 @@ int main(int argc, char *argv[]) {
return 1;
}
return FileTestMain(RunTest, nullptr, argv[0]);
return FileTestMain(RunTest, ctx.get(), argv[0]);
}
static int HexToBIGNUM(ScopedBIGNUM *out, const char *in) {
@ -355,7 +341,7 @@ static bool ExpectBIGNUMsEqual(FileTest *t, const char *operation,
return false;
}
static bool TestSum(FileTest *t) {
static bool TestSum(FileTest *t, BN_CTX *ctx) {
ScopedBIGNUM a = GetBIGNUM(t, "A");
ScopedBIGNUM b = GetBIGNUM(t, "B");
ScopedBIGNUM sum = GetBIGNUM(t, "Sum");
@ -377,21 +363,69 @@ static bool TestSum(FileTest *t) {
return true;
}
static bool TestLShift1(FileTest *t, BN_CTX *ctx) {
ScopedBIGNUM a = GetBIGNUM(t, "A");
ScopedBIGNUM lshift1 = GetBIGNUM(t, "LShift1");
if (!a || !lshift1) {
return false;
}
ScopedBIGNUM ret(BN_new()), two(BN_new());
if (!ret || !two ||
!BN_set_word(two.get(), 2) ||
!BN_add(ret.get(), a.get(), a.get()) ||
!ExpectBIGNUMsEqual(t, "A + A", lshift1.get(), ret.get()) ||
!BN_mul(ret.get(), a.get(), two.get(), ctx) ||
!ExpectBIGNUMsEqual(t, "A * 2", lshift1.get(), ret.get()) ||
!BN_div(ret.get(), nullptr /* rem */, lshift1.get(), two.get(), ctx) ||
!ExpectBIGNUMsEqual(t, "LShift1 / 2", a.get(), ret.get()) ||
!BN_lshift1(ret.get(), a.get()) ||
!ExpectBIGNUMsEqual(t, "A << 1", lshift1.get(), ret.get()) ||
!BN_rshift1(ret.get(), lshift1.get()) ||
!ExpectBIGNUMsEqual(t, "LShift >> 1", a.get(), ret.get()) ||
!BN_rshift1(ret.get(), lshift1.get()) ||
!ExpectBIGNUMsEqual(t, "LShift >> 1", a.get(), ret.get())) {
return false;
}
// Set the LSB to 1 and test rshift1 again.
if (BN_is_negative(lshift1.get())) {
if (!BN_sub(lshift1.get(), lshift1.get(), BN_value_one())) {
return false;
}
} else {
if (!BN_add(lshift1.get(), lshift1.get(), BN_value_one())) {
return false;
}
}
if (!BN_div(ret.get(), nullptr /* rem */, lshift1.get(), two.get(), ctx) ||
!ExpectBIGNUMsEqual(t, "(LShift1 | 1) / 2", a.get(), ret.get()) ||
!BN_rshift1(ret.get(), lshift1.get()) ||
!ExpectBIGNUMsEqual(t, "(LShift | 1) >> 1", a.get(), ret.get())) {
return false;
}
return true;
}
struct Test {
const char *name;
bool (*func)(FileTest *t);
bool (*func)(FileTest *t, BN_CTX *ctx);
};
static const Test kTests[] = {
{"Sum", TestSum},
{"LShift1", TestLShift1},
};
static bool RunTest(FileTest *t, void *arg) {
BN_CTX *ctx = reinterpret_cast<BN_CTX *>(arg);
for (const Test &test : kTests) {
if (t->GetType() != test.name) {
continue;
}
return test.func(t);
return test.func(t, ctx);
}
t->PrintLine("Unknown test type: %s", t->GetType().c_str());
return false;
@ -488,41 +522,6 @@ static bool test_div(FILE *fp, BN_CTX *ctx) {
return true;
}
static bool test_lshift1(FILE *fp) {
ScopedBIGNUM a(BN_new());
ScopedBIGNUM b(BN_new());
ScopedBIGNUM c(BN_new());
if (!a || !b || !c || !BN_rand(a.get(), 200, 0, 0)) {
return false;
}
a->neg = rand_neg();
for (int i = 0; i < num0; i++) {
if (!BN_lshift1(b.get(), a.get())) {
return false;
}
if (fp != NULL) {
BN_print_fp(fp, a.get());
puts_fp(fp, " * 2");
puts_fp(fp, " - ");
BN_print_fp(fp, b.get());
puts_fp(fp, "\n");
}
if (!BN_add(c.get(), a.get(), a.get()) ||
!BN_sub(a.get(), b.get(), c.get())) {
return false;
}
if (!BN_is_zero(a.get())) {
fprintf(stderr, "Left shift one test failed!\n");
return false;
}
if (!BN_copy(a.get(), b.get())) {
return false;
}
}
return true;
}
static bool test_rshift(FILE *fp, BN_CTX *ctx) {
ScopedBIGNUM a(BN_new());
ScopedBIGNUM b(BN_new());
@ -559,41 +558,6 @@ static bool test_rshift(FILE *fp, BN_CTX *ctx) {
return true;
}
static bool test_rshift1(FILE *fp) {
ScopedBIGNUM a(BN_new());
ScopedBIGNUM b(BN_new());
ScopedBIGNUM c(BN_new());
if (!a || !b || !c || !BN_rand(a.get(), 200, 0, 0)) {
return false;
}
a->neg = rand_neg();
for (int i = 0; i < num0; i++) {
if (!BN_rshift1(b.get(), a.get())) {
return false;
}
if (fp != NULL) {
BN_print_fp(fp, a.get());
puts_fp(fp, " / 2");
puts_fp(fp, " - ");
BN_print_fp(fp, b.get());
puts_fp(fp, "\n");
}
if (!BN_sub(c.get(), a.get(), b.get()) ||
!BN_sub(c.get(), c.get(), b.get())) {
return false;
}
if (!BN_is_zero(c.get()) && !BN_abs_is_word(c.get(), 1)) {
fprintf(stderr, "Right shift one test failed!\n");
return false;
}
if (!BN_copy(a.get(), b.get())) {
return false;
}
}
return true;
}
static bool test_lshift(FILE *fp, BN_CTX *ctx, ScopedBIGNUM a) {
if (!a) {
a.reset(BN_new());

File diff suppressed because it is too large Load Diff