Convert test_lshift and test_rshift into file_tests.
BUG=31
Change-Id: Ic9acc851d9ad48a7b74f75d5854b7186730f9d3e
Reviewed-on: https://boringssl-review.googlesource.com/8524
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/crypto/bn/bn_test.cc b/crypto/bn/bn_test.cc
index 47c6a9d..fefc058 100644
--- a/crypto/bn/bn_test.cc
+++ b/crypto/bn/bn_test.cc
@@ -73,6 +73,7 @@
#endif
#include <errno.h>
+#include <limits.h>
#include <stdio.h>
#include <string.h>
@@ -98,8 +99,6 @@
static const int num1 = 50; // additional tests for some functions
static const int num2 = 5; // number of tests for slow functions
-static bool test_lshift(FILE *fp, BN_CTX *ctx, ScopedBIGNUM a);
-static bool test_rshift(FILE *fp, BN_CTX *ctx);
static bool test_sqr(FILE *fp, BN_CTX *ctx);
static bool test_mul(FILE *fp);
static bool test_div(FILE *fp, BN_CTX *ctx);
@@ -126,10 +125,6 @@
static bool test_asn1();
static bool RunTest(FileTest *t, void *arg);
-static const uint8_t kSample[] =
- "\xC6\x4F\x43\x04\x2A\xEA\xCA\x6E\x58\x36\x80\x5B\xE8\xC9"
- "\x9B\x04\x5D\x48\x36\xC2\xFD\x16\xC9\x64\xF0";
-
// A wrapper around puts that takes its arguments in the same order as our *_fp
// functions.
static void puts_fp(FILE *out, const char *m) {
@@ -186,28 +181,6 @@
"| grep -v 0 */\n");
puts_fp(bc_file.get(), "obase=16\nibase=16\n");
- message(bc_file.get(), "BN_lshift (fixed)");
- ScopedBIGNUM sample(BN_bin2bn(kSample, sizeof(kSample) - 1, NULL));
- if (!sample) {
- return 1;
- }
- if (!test_lshift(bc_file.get(), ctx.get(), std::move(sample))) {
- return 1;
- }
- flush_fp(bc_file.get());
-
- message(bc_file.get(), "BN_lshift");
- if (!test_lshift(bc_file.get(), ctx.get(), nullptr)) {
- return 1;
- }
- flush_fp(bc_file.get());
-
- message(bc_file.get(), "BN_rshift");
- if (!test_rshift(bc_file.get(), ctx.get())) {
- return 1;
- }
- flush_fp(bc_file.get());
-
message(bc_file.get(), "BN_sqr");
if (!test_sqr(bc_file.get(), ctx.get())) {
return 1;
@@ -322,6 +295,21 @@
return ret;
}
+static bool GetInt(FileTest *t, int *out, const char *attribute) {
+ ScopedBIGNUM ret = GetBIGNUM(t, attribute);
+ if (!ret) {
+ return false;
+ }
+
+ BN_ULONG word = BN_get_word(ret.get());
+ if (word > INT_MAX) {
+ return false;
+ }
+
+ *out = static_cast<int>(word);
+ return true;
+}
+
static bool ExpectBIGNUMsEqual(FileTest *t, const char *operation,
const BIGNUM *expected, const BIGNUM *actual) {
if (BN_cmp(expected, actual) == 0) {
@@ -409,6 +397,44 @@
return true;
}
+static bool TestLShift(FileTest *t, BN_CTX *ctx) {
+ ScopedBIGNUM a = GetBIGNUM(t, "A");
+ ScopedBIGNUM lshift = GetBIGNUM(t, "LShift");
+ int n;
+ if (!a || !lshift || !GetInt(t, &n, "N")) {
+ return false;
+ }
+
+ ScopedBIGNUM ret(BN_new());
+ if (!ret ||
+ !BN_lshift(ret.get(), a.get(), n) ||
+ !ExpectBIGNUMsEqual(t, "A << N", lshift.get(), ret.get()) ||
+ !BN_rshift(ret.get(), lshift.get(), n) ||
+ !ExpectBIGNUMsEqual(t, "A >> N", a.get(), ret.get())) {
+ return false;
+ }
+
+ return true;
+}
+
+static bool TestRShift(FileTest *t, BN_CTX *ctx) {
+ ScopedBIGNUM a = GetBIGNUM(t, "A");
+ ScopedBIGNUM rshift = GetBIGNUM(t, "RShift");
+ int n;
+ if (!a || !rshift || !GetInt(t, &n, "N")) {
+ return false;
+ }
+
+ ScopedBIGNUM ret(BN_new());
+ if (!ret ||
+ !BN_rshift(ret.get(), a.get(), n) ||
+ !ExpectBIGNUMsEqual(t, "A >> N", rshift.get(), ret.get())) {
+ return false;
+ }
+
+ return true;
+}
+
struct Test {
const char *name;
bool (*func)(FileTest *t, BN_CTX *ctx);
@@ -417,6 +443,8 @@
static const Test kTests[] = {
{"Sum", TestSum},
{"LShift1", TestLShift1},
+ {"LShift", TestLShift},
+ {"RShift", TestRShift},
};
static bool RunTest(FileTest *t, void *arg) {
@@ -522,92 +550,6 @@
return true;
}
-static bool test_rshift(FILE *fp, BN_CTX *ctx) {
- ScopedBIGNUM a(BN_new());
- ScopedBIGNUM b(BN_new());
- ScopedBIGNUM c(BN_new());
- ScopedBIGNUM d(BN_new());
- ScopedBIGNUM e(BN_new());
- if (!a || !b || !c || !d || !e || !BN_one(c.get()) ||
- !BN_rand(a.get(), 200, 0, 0)) {
- return false;
- }
- a->neg = rand_neg();
- for (int i = 0; i < num0; i++) {
- if (!BN_rshift(b.get(), a.get(), i + 1) ||
- !BN_add(c.get(), c.get(), c.get())) {
- return false;
- }
- if (fp != NULL) {
- BN_print_fp(fp, a.get());
- puts_fp(fp, " / ");
- BN_print_fp(fp, c.get());
- puts_fp(fp, " - ");
- BN_print_fp(fp, b.get());
- puts_fp(fp, "\n");
- }
- if (!BN_div(d.get(), e.get(), a.get(), c.get(), ctx) ||
- !BN_sub(d.get(), d.get(), b.get())) {
- return false;
- }
- if (!BN_is_zero(d.get())) {
- fprintf(stderr, "Right shift test failed!\n");
- return false;
- }
- }
- return true;
-}
-
-static bool test_lshift(FILE *fp, BN_CTX *ctx, ScopedBIGNUM a) {
- if (!a) {
- a.reset(BN_new());
- if (!a || !BN_rand(a.get(), 200, 0, 0)) {
- return false;
- }
- a->neg = rand_neg();
- }
-
- ScopedBIGNUM b(BN_new());
- ScopedBIGNUM c(BN_new());
- ScopedBIGNUM d(BN_new());
- if (!b || !c || !d || !BN_one(c.get())) {
- return false;
- }
-
- for (int i = 0; i < num0; i++) {
- if (!BN_lshift(b.get(), a.get(), i + 1) ||
- !BN_add(c.get(), c.get(), c.get())) {
- return false;
- }
- if (fp != NULL) {
- BN_print_fp(fp, a.get());
- puts_fp(fp, " * ");
- BN_print_fp(fp, c.get());
- puts_fp(fp, " - ");
- BN_print_fp(fp, b.get());
- puts_fp(fp, "\n");
- }
- if (!BN_mul(d.get(), a.get(), c.get(), ctx) ||
- !BN_sub(d.get(), d.get(), b.get())) {
- return false;
- }
- if (!BN_is_zero(d.get())) {
- fprintf(stderr, "Left shift test failed!\n");
- fprintf(stderr, "a=");
- BN_print_fp(stderr, a.get());
- fprintf(stderr, "\nb=");
- BN_print_fp(stderr, b.get());
- fprintf(stderr, "\nc=");
- BN_print_fp(stderr, c.get());
- fprintf(stderr, "\nd=");
- BN_print_fp(stderr, d.get());
- fprintf(stderr, "\n");
- return false;
- }
- }
- return true;
-}
-
static bool test_mul(FILE *fp) {
ScopedBN_CTX ctx(BN_CTX_new());
ScopedBIGNUM a(BN_new());