summaryrefslogtreecommitdiff
path: root/test/math/recover.cpp
diff options
context:
space:
mode:
authorGloria Mundi <gloria@gloria-mundi.eu>2024-11-16 15:39:23 +0100
committerGloria Mundi <gloria@gloria-mundi.eu>2024-11-16 15:39:23 +0100
commit72bd993483453ed8ebc462f1a33385cd355d486f (patch)
treec5592ba1ed2fed79e26ba6158d097c9ceb43f061 /test/math/recover.cpp
parent98567ec798aa8ca2cfbcb85c774dd470f30e30d4 (diff)
parent35d485bcf6a9ed0a9542628ce4aa94a3326d0884 (diff)
merge mzuenni changes
Diffstat (limited to 'test/math/recover.cpp')
-rw-r--r--test/math/recover.cpp44
1 files changed, 44 insertions, 0 deletions
diff --git a/test/math/recover.cpp b/test/math/recover.cpp
new file mode 100644
index 0000000..6f89e5a
--- /dev/null
+++ b/test/math/recover.cpp
@@ -0,0 +1,44 @@
+#include "../util.h"
+#include <math/recover.cpp>
+#include <math/shortModInv.cpp>
+
+void stress_test() {
+ ll queries = 0;
+ timer t;
+ for (int i = 0; i < 500; i++) {
+ ll p = Random::prime<ll>(10000);
+ for (ll j = 0; 2*j*j < p; j++) {
+ for (ll b = 1; 2*b*b < p; b++) {
+ if (gcd(j, b) != 1) continue;
+ for (ll a : {-j, j}) {
+ ll c = a * multInv(b, p);
+
+ t.start();
+ auto [x, y] = recover(c, p);
+ t.stop();
+
+ if (a != x || b != y) cerr << "got: " << x << "/" << y << ", expected: " << a << "/" << b << FAIL;
+ queries++;
+ }
+ }
+ }
+ for (ll c = 0; c < p; c++) {
+ t.start();
+ auto [x, y] = recover(c, p);
+ t.stop();
+
+ if (y < 0) continue;
+ if (y == 0) cerr << "error: y=0" << FAIL;
+ ll got = (((x * multInv(y, p)) % p) + p) % p;
+ if (got != c) cerr << "got: " << got << ", expected: " << c << FAIL;
+ queries++;
+ }
+ }
+ cerr << "tested random queries: " << queries << endl;
+ if (t.time > 500) cerr << "too slow: " << t.time << FAIL;
+ cerr << "tested performance: " << t.time << "ms" << endl;
+}
+
+int main() {
+ stress_test();
+}