summaryrefslogtreecommitdiff
path: root/test/math/sqrtModCipolla.cpp
diff options
context:
space:
mode:
authorGloria Mundi <gloria@gloria-mundi.eu>2024-11-16 01:24:14 +0100
committerGloria Mundi <gloria@gloria-mundi.eu>2024-11-16 01:24:14 +0100
commit98567ec798aa8ca2cfbcb85c774dd470f30e30d4 (patch)
tree5113d5cc24d1ad5f93810b6442ce584a36950dc8 /test/math/sqrtModCipolla.cpp
parentad3856a6b766087df0036de0b556f4700a6498c9 (diff)
parent8d11c6c8213f46f0fa19826917c255edd5d43cb1 (diff)
mzuenni tests
Diffstat (limited to 'test/math/sqrtModCipolla.cpp')
-rw-r--r--test/math/sqrtModCipolla.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/test/math/sqrtModCipolla.cpp b/test/math/sqrtModCipolla.cpp
new file mode 100644
index 0000000..26d975b
--- /dev/null
+++ b/test/math/sqrtModCipolla.cpp
@@ -0,0 +1,48 @@
+#include "../util.h"
+#include <math/modPowIterativ.cpp>
+#include <math/legendre.cpp>
+mt19937 rng(123456789);
+#include <math/sqrtModCipolla.cpp>
+
+void stress_test(ll range) {
+ ll work = 0;
+ for (ll i = 0; i < 10'000; i++) {
+ ll p = Random::prime<ll>(range);
+ for (ll j = 0; j < 100; j++) {
+ ll x = Random::integer<ll>(0, p);
+ if (legendre(x, p) < 0) continue;
+
+ ll got = sqrtMod(x, p);
+ if (got < 0 || got >= p) cerr << "error: out of range" << FAIL;
+ if ((got * got) % p != x) cerr << "error: not root" << FAIL;
+ work++;
+ }
+ }
+ cerr << "stress tested: " << work << endl;
+}
+
+constexpr int N = 200'000;
+constexpr ll mod = 1'394'633'899;
+void performance_test() {
+ timer t;
+ hash_t hash = 0;
+ for (int operations = 0; operations < N; operations++) {
+ ll x;
+ do {
+ x = Random::integer<ll>(0, mod);
+ } while (legendre(x, mod) >= 0);
+ t.start();
+ hash += sqrtMod(x, mod);
+ t.stop();
+ }
+ if (t.time > 750) cerr << "too slow: " << t.time << FAIL;
+ cerr << "tested performance: " << t.time << "ms (hash: " << hash << ")" << endl;
+}
+
+
+int main() {
+ stress_test(1'000);
+ stress_test(1'000'000'000);
+ performance_test();
+}
+