-rw-r--r-- 1824 cryptattacktester-20231020/collisiontest.cpp raw
#include <cassert>
#include <iostream>
#include <vector>
#include <algorithm>
#include "random.h"
#include "collision_prob.h"
using namespace std;
int main()
{
  for (bigint X = 1;X < 10;++X)
    for (bigint Y = 1;Y < 10;++Y)
      for (bigint L = 0;L < 5;++L) {
        bigfloat p = exp2(-bigfloat(L));
        for (bigint r = 1;r < 5;++r) {
          bigfloat prediction = collision_average(X,Y,r,p);
          bigint trials = ceil_as_bigint(bigfloat(10000)/prediction);
          bigint successes = 0; 
          for (bigint trial = 0;trial < trials;++trial) {
            vector<bigint> Z;
            for (bigint j = 0;j < X;++j) {
              bigint s = 0;
              for (bigint k = 0;k < L;++k) s = 2*s+random_bool();
              Z.push_back(2*s);
            }
            for (bigint j = 0;j < Y;++j) {
              bigint s = 0;
              for (bigint k = 0;k < L;++k) s = 2*s+random_bool();
              Z.push_back(2*s+1);
            }
            sort(Z.begin(),Z.end());
            for (bigint i = 0;i < X+Y;++i)
              for (bigint j = i+1;j < X+Y && j <= i+r;++j) {
                bigint s0 = Z.at(i);
                bigint s1 = Z.at(j);
                if (s0&1) continue;
                if (s1 != s0+1) continue;
                ++successes;
              }
          }
          bigfloat succper = bigfloat(successes)/bigfloat(trials);
          bigfloat ratio = succper/prediction;
          cout << "collisiontest"
          << " X " << X
          << " Y " << Y
          << " L " << L
          << " r " << r
          << " prediction " << prediction
          << " trials " << trials
          << " succper " << succper
          << " ratio " << ratio
          << "\n" << flush;
          assert(ratio > 0.9);
          assert(ratio < 1.1);
        }
      }
  return 0;
}