aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_bigint.cpp
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2016-12-30 23:53:43 -0500
committerJack Lloyd <[email protected]>2016-12-30 23:53:43 -0500
commitff7999b1f19c9dec472210d7c928b4ca2769ef28 (patch)
tree7879c0b1fec7f555ec29bdfc52ae2f3f32ddd9e8 /src/tests/test_bigint.cpp
parent122754bf3dd27ffb81262affc16c78b5a513ed9e (diff)
Add more tests for random prime and DL group generation
Diffstat (limited to 'src/tests/test_bigint.cpp')
-rw-r--r--src/tests/test_bigint.cpp85
1 files changed, 85 insertions, 0 deletions
diff --git a/src/tests/test_bigint.cpp b/src/tests/test_bigint.cpp
index 096788f17..1d442a8bb 100644
--- a/src/tests/test_bigint.cpp
+++ b/src/tests/test_bigint.cpp
@@ -30,7 +30,9 @@ class BigInt_Unit_Tests : public Test
results.push_back(test_bigint_sizes());
results.push_back(test_random_integer());
+ results.push_back(test_random_prime());
results.push_back(test_encode());
+ results.push_back(test_bigint_io());
return results;
}
@@ -80,6 +82,51 @@ class BigInt_Unit_Tests : public Test
return result;
}
+ Test::Result test_random_prime()
+ {
+ Test::Result result("BigInt prime generation");
+
+ result.test_throws("Invalid arg", []{ Botan::random_prime(Test::rng(), 0); });
+ result.test_throws("Invalid arg", []{ Botan::random_prime(Test::rng(), 1); });
+ result.test_throws("Invalid arg", []{ Botan::random_prime(Test::rng(), 2, 0); });
+ result.test_throws("Invalid arg", []{ Botan::random_prime(Test::rng(), 2, 1, 1, 3); });
+ result.test_throws("Invalid arg", []{ Botan::random_prime(Test::rng(), 2, 1, 0, 2); });
+
+ BigInt p = Botan::random_prime(Test::rng(), 2);
+ result.confirm("Only two 2-bit primes", p == 2 || p == 3);
+
+ p = Botan::random_prime(Test::rng(), 3);
+ result.confirm("Only two 3-bit primes", p == 5 || p == 7);
+
+ p = Botan::random_prime(Test::rng(), 4);
+ result.confirm("Only two 4-bit primes", p == 11 || p == 13);
+
+ for(size_t bits = 5; bits <= 32; ++bits)
+ {
+ p = Botan::random_prime(Test::rng(), bits);
+ result.test_eq("Expected bit size", p.bits(), bits);
+ result.test_eq("P is prime", Botan::is_prime(p, Test::rng()), true);
+ }
+
+ for(size_t bits = 5; bits <= 32; ++bits)
+ {
+ const BigInt last_p = p;
+ p = Botan::random_prime(Test::rng(), bits, last_p);
+
+ result.test_eq("Relatively prime", Botan::gcd(last_p, p), 1);
+ result.test_eq("Expected bit size", p.bits(), bits);
+ result.test_eq("P is prime", Botan::is_prime(p, Test::rng()), true);
+ }
+
+ const size_t safe_prime_bits = 65;
+ const BigInt safe_prime = Botan::random_safe_prime(Test::rng(), safe_prime_bits);
+ result.test_eq("Safe prime size", safe_prime.bits(), safe_prime_bits);
+ result.confirm("P is prime", Botan::is_prime(safe_prime, Test::rng()));
+ result.confirm("(P-1)/2 is prime", Botan::is_prime((safe_prime-1)/2, Test::rng()));
+
+ return result;
+ }
+
Test::Result test_random_integer()
{
Test::Result result("BigInt::random_integer");
@@ -164,6 +211,44 @@ class BigInt_Unit_Tests : public Test
return result;
}
+
+ Test::Result test_bigint_io()
+ {
+ Test::Result result("BigInt IO operators");
+
+ const std::map<std::string, Botan::BigInt> str_to_val = {
+ { "-13", -Botan::BigInt(13) },
+ { "0", Botan::BigInt(0) },
+ { "0x13", Botan::BigInt(0x13) },
+ { "1", Botan::BigInt(1) },
+ { "4294967297", Botan::BigInt(2147483648)*2 + 1 }
+ };
+
+ for(auto vec : str_to_val)
+ {
+ Botan::BigInt n;
+ std::istringstream iss;
+
+ iss.str(vec.first);
+ iss >> n;
+ result.test_eq("input '" + vec.first + "'", n, vec.second);
+ }
+
+ BigInt n = 33;
+
+ std::ostringstream oss;
+ oss << n;
+ result.test_eq("output 33 dec", oss.str(), "33");
+
+ oss.str("");
+ oss << std::hex << n;
+ result.test_eq("output 33 hex", oss.str(), "21");
+
+ result.test_throws("octal output not supported",
+ [&]{ oss << std::oct << n; });
+
+ return result;
+ }
};
BOTAN_REGISTER_TEST("bigint_unit", BigInt_Unit_Tests);