aboutsummaryrefslogtreecommitdiffstats
path: root/doc/examples
diff options
context:
space:
mode:
authorlloyd <[email protected]>2008-09-18 13:08:23 +0000
committerlloyd <[email protected]>2008-09-18 13:08:23 +0000
commit73cc211d479b60dfafbfd0a9d990ff36eea54307 (patch)
treea5c8252785c1a88db6c5d8c8e0cdeaa6609fad03 /doc/examples
parent8f404a0dc095ad0ab87eabac3f95f56adea71ce1 (diff)
Add a program that checks the ressol() implementation using a set of
randomly generated primes.
Diffstat (limited to 'doc/examples')
-rw-r--r--doc/examples/ressol.cpp77
1 files changed, 77 insertions, 0 deletions
diff --git a/doc/examples/ressol.cpp b/doc/examples/ressol.cpp
new file mode 100644
index 000000000..9fedc8115
--- /dev/null
+++ b/doc/examples/ressol.cpp
@@ -0,0 +1,77 @@
+#include <botan/numthry.h>
+
+using namespace Botan;
+
+#include <iostream>
+
+void test_ressol(const BigInt& p, RandomNumberGenerator& rng)
+ {
+ std::cout << p << std::endl;
+
+ // const BigInt p_16 = p / 16;
+
+ int noroot = 0, false_result = 0;
+
+ for(int j = 0; j != 1000; ++j)
+ {
+ BigInt x = random_integer(rng, 0, p);
+ //if(x % p_16 == 0)
+ //std::cout << "p = " << p << " x = " << x << "\n";
+
+ BigInt sqrt_x = ressol(x, p);
+
+ if(sqrt_x < 0)
+ {
+ ++noroot;
+ continue;
+ }
+
+ BigInt check = square(sqrt_x) % p;
+
+ if(check != x % p)
+ {
+ std::cout << "FAIL "
+ << "x = " << x << "; "
+ << "p = " << p << "; "
+ << "s = " << sqrt_x << "; "
+ << "s^2%p = " << check << "\n";
+ ++false_result;
+ }
+ }
+ /*
+ std::cout << "nomatch=" << nomatch << " "
+ << "noroot=" << noroot << " "
+ << "false=" << false_result << "\n";
+ */
+ }
+
+int main()
+ {
+ RandomNumberGenerator* rng = RandomNumberGenerator::make_rng();
+
+#if 0
+ std::cout << ressol(8, 17) << "\n";
+ std::cout << ressol_orig(8, 17) << "\n";
+#endif
+
+#if 1
+ for(int j = 16; j != 1024; ++j)
+ {
+ std::cout << "Round " << j << "\n";
+ BigInt p = random_prime(*rng, j);
+ test_ressol(p, *rng);
+ //printf("%d\n", j);
+
+
+ }
+#endif
+ /*
+ for(u32bit j = 9; j != PRIME_TABLE_SIZE; ++j)
+ {
+ std::cout << "PRIME[" << j << "] == " << PRIMES[j] << std::endl;
+ //printf("%d - ", PRIMES[j]);
+ test_ressol(PRIMES[j], *rng);
+ //printf("\n");
+ }
+ */
+ }