aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_bigint.cpp
diff options
context:
space:
mode:
authorlloyd <[email protected]>2014-01-01 23:58:46 +0000
committerlloyd <[email protected]>2014-01-01 23:58:46 +0000
commit585356e222c5a6116532ba14cce44a2c6cd9c366 (patch)
tree90a50dec70a8044766fea6a5f9371b7e7c0eb0f3 /src/tests/test_bigint.cpp
parent48bd53d1918f9ee765313d62eacd054376c0b49e (diff)
Cull remaining mostly dubious examples. Also remove readme.txt
Diffstat (limited to 'src/tests/test_bigint.cpp')
-rw-r--r--src/tests/test_bigint.cpp237
1 files changed, 145 insertions, 92 deletions
diff --git a/src/tests/test_bigint.cpp b/src/tests/test_bigint.cpp
index 45a20cef6..7f866d212 100644
--- a/src/tests/test_bigint.cpp
+++ b/src/tests/test_bigint.cpp
@@ -11,126 +11,79 @@
#include <fstream>
#include <iostream>
#include <cstdlib>
+#include <iterator>
#include <botan/auto_rng.h>
#include <botan/bigint.h>
#include <botan/exceptn.h>
#include <botan/numthry.h>
-using namespace Botan;
-size_t check_add(const std::vector<std::string>&);
-size_t check_sub(const std::vector<std::string>&);
-size_t check_mul(const std::vector<std::string>&);
-size_t check_sqr(const std::vector<std::string>&);
-size_t check_div(const std::vector<std::string>&);
-size_t check_mod(const std::vector<std::string>&,
- Botan::RandomNumberGenerator& rng);
-size_t check_shr(const std::vector<std::string>&);
-size_t check_shl(const std::vector<std::string>&);
+using namespace Botan;
-size_t check_powmod(const std::vector<std::string>&);
-size_t check_primetest(const std::vector<std::string>&,
- Botan::RandomNumberGenerator&);
+namespace {
-size_t test_bigint()
+size_t test_make_prime()
{
- const std::string filename = CHECKS_DIR "/mp_valid.dat";
- std::ifstream test_data(filename.c_str());
+ AutoSeeded_RNG rng;
- if(!test_data)
- throw Botan::Stream_IO_Error("Couldn't open test file " + filename);
+ std::set<BigInt> primes;
- size_t total_errors = 0;
- size_t errors = 0, alg_count = 0;
- std::string algorithm;
- bool first = true;
- size_t counter = 0;
+ std::map<int, int> bit_count;
- AutoSeeded_RNG rng;
+ int not_new = 0;
- while(!test_data.eof())
+ while(primes.size() < 10000)
{
- if(test_data.bad() || test_data.fail())
- throw Botan::Stream_IO_Error("File I/O error reading from " +
- filename);
-
- std::string line;
- std::getline(test_data, line);
+ u32bit start_cnt = primes.size();
- strip(line);
- if(line.size() == 0) continue;
+ u32bit bits = 18;
- // Do line continuation
- while(line[line.size()-1] == '\\' && !test_data.eof())
- {
- line.replace(line.size()-1, 1, "");
- std::string nextline;
- std::getline(test_data, nextline);
- strip(nextline);
- if(nextline.size() == 0) continue;
- line += nextline;
- }
+ if(rng.next_byte() % 128 == 0)
+ bits -= rng.next_byte() % (bits-2);
- if(line[0] == '[' && line[line.size() - 1] == ']')
- {
- if(!first)
- test_report("Bigint " + algorithm, alg_count, errors);
+ bit_count[bits]++;
- algorithm = line.substr(1, line.size() - 2);
+ //std::cout << "random_prime(" << bits << ")\n";
- total_errors += errors;
- errors = 0;
- alg_count = 0;
- counter = 0;
+ BigInt p = random_prime(rng, bits);
- first = false;
- continue;
+ if(p.bits() != bits)
+ {
+ std::cout << "Asked for " << bits << " got " << p
+ << " " << p.bits() << " bits\n";
+ return 1;
}
- std::vector<std::string> substr = parse(line);
-
-#if DEBUG
- std::cout << "Testing: " << algorithm << std::endl;
-#endif
+ primes.insert(random_prime(rng, bits));
- size_t new_errors = 0;
- if(algorithm.find("Addition") != std::string::npos)
- new_errors = check_add(substr);
- else if(algorithm.find("Subtraction") != std::string::npos)
- new_errors = check_sub(substr);
- else if(algorithm.find("Multiplication") != std::string::npos)
- new_errors = check_mul(substr);
- else if(algorithm.find("Square") != std::string::npos)
- new_errors = check_sqr(substr);
- else if(algorithm.find("Division") != std::string::npos)
- new_errors = check_div(substr);
- else if(algorithm.find("Modulo") != std::string::npos)
- new_errors = check_mod(substr, rng);
- else if(algorithm.find("LeftShift") != std::string::npos)
- new_errors = check_shl(substr);
- else if(algorithm.find("RightShift") != std::string::npos)
- new_errors = check_shr(substr);
- else if(algorithm.find("ModExp") != std::string::npos)
- new_errors = check_powmod(substr);
- else if(algorithm.find("PrimeTest") != std::string::npos)
- new_errors = check_primetest(substr, rng);
+ if(primes.size() != start_cnt)
+ std::cout << primes.size() << "\n";
else
- std::cout << "Unknown MPI test " << algorithm << std::endl;
+ not_new++;
- counter++;
- alg_count++;
- errors += new_errors;
+ //std::cout << "miss: " << not_new << "\n";
- if(new_errors)
- std::cout << "ERROR: BigInt " << algorithm << " failed test #"
- << std::dec << alg_count << std::endl;
+ if(not_new % 100000 == 0)
+ {
+ for(std::map<int, int>::iterator i = bit_count.begin();
+ i != bit_count.end(); ++i)
+ std::cout << "bit_count[" << i->first << "] = "
+ << i->second << "\n";
+ std::copy(primes.begin(), primes.end(),
+ std::ostream_iterator<BigInt>(std::cout, " "));
+ }
}
- return total_errors;
+ std::cout << "Generated all? primes\n";
+ /*
+ for(u32bit j = 0; j != PRIME_TABLE_SIZE; ++j)
+ {
+ if(primes.count(PRIMES[j]) != 1)
+ std::cout << "Missing " << PRIMES[j] << "\n";
+ }
+ */
}
-namespace {
-
// c==expected, d==a op b, e==a op= b
size_t results(std::string op,
const BigInt& a, const BigInt& b,
@@ -163,8 +116,6 @@ size_t results(std::string op,
}
}
-}
-
size_t check_add(const std::vector<std::string>& args)
{
BigInt a(args[0]);
@@ -355,3 +306,105 @@ size_t check_primetest(const std::vector<std::string>& args,
}
return 0;
}
+
+}
+
+size_t test_bigint()
+ {
+ const std::string filename = CHECKS_DIR "/mp_valid.dat";
+ std::ifstream test_data(filename.c_str());
+
+ if(!test_data)
+ throw Botan::Stream_IO_Error("Couldn't open test file " + filename);
+
+ size_t total_errors = 0;
+ size_t errors = 0, alg_count = 0;
+ std::string algorithm;
+ bool first = true;
+ size_t counter = 0;
+
+ AutoSeeded_RNG rng;
+
+ while(!test_data.eof())
+ {
+ if(test_data.bad() || test_data.fail())
+ throw Botan::Stream_IO_Error("File I/O error reading from " +
+ filename);
+
+ std::string line;
+ std::getline(test_data, line);
+
+ strip(line);
+ if(line.size() == 0) continue;
+
+ // Do line continuation
+ while(line[line.size()-1] == '\\' && !test_data.eof())
+ {
+ line.replace(line.size()-1, 1, "");
+ std::string nextline;
+ std::getline(test_data, nextline);
+ strip(nextline);
+ if(nextline.size() == 0) continue;
+ line += nextline;
+ }
+
+ if(line[0] == '[' && line[line.size() - 1] == ']')
+ {
+ if(!first)
+ test_report("Bigint " + algorithm, alg_count, errors);
+
+ algorithm = line.substr(1, line.size() - 2);
+
+ total_errors += errors;
+ errors = 0;
+ alg_count = 0;
+ counter = 0;
+
+ first = false;
+ continue;
+ }
+
+ std::vector<std::string> substr = parse(line);
+
+#if DEBUG
+ std::cout << "Testing: " << algorithm << std::endl;
+#endif
+
+ size_t new_errors = 0;
+ if(algorithm.find("Addition") != std::string::npos)
+ new_errors = check_add(substr);
+ else if(algorithm.find("Subtraction") != std::string::npos)
+ new_errors = check_sub(substr);
+ else if(algorithm.find("Multiplication") != std::string::npos)
+ new_errors = check_mul(substr);
+ else if(algorithm.find("Square") != std::string::npos)
+ new_errors = check_sqr(substr);
+ else if(algorithm.find("Division") != std::string::npos)
+ new_errors = check_div(substr);
+ else if(algorithm.find("Modulo") != std::string::npos)
+ new_errors = check_mod(substr, rng);
+ else if(algorithm.find("LeftShift") != std::string::npos)
+ new_errors = check_shl(substr);
+ else if(algorithm.find("RightShift") != std::string::npos)
+ new_errors = check_shr(substr);
+ else if(algorithm.find("ModExp") != std::string::npos)
+ new_errors = check_powmod(substr);
+ else if(algorithm.find("PrimeTest") != std::string::npos)
+ new_errors = check_primetest(substr, rng);
+ else
+ std::cout << "Unknown MPI test " << algorithm << std::endl;
+
+ counter++;
+ alg_count++;
+ errors += new_errors;
+
+ if(new_errors)
+ std::cout << "ERROR: BigInt " << algorithm << " failed test #"
+ << std::dec << alg_count << std::endl;
+ }
+
+ //total_errors += test_make_prime();
+
+ return total_errors;
+ }
+