-rw-r--r-- 1440 cryptattacktester-20231020/bruteforce.cpp raw
#include "decoding.h" #include "bruteforce.h" using namespace std; vector<bit> bruteforce( const vector<bit> &bits, const vector<bigint> &params, const vector<bigint> &attackparams ) { bigint n = params.at(0); bigint k = params.at(1); bigint w = params.at(2); bigint iters = attackparams.at(0); auto inputs = decoding_deserialize(bits,params); auto pk = inputs.first; auto ct = inputs.second; vector<vector<bit>> H; for (bigint i = 0;i < n-k;++i) { vector<bit> Hi; for (bigint j = 0;j < n-k;++j) Hi.push_back(bit(i == j)); Hi.insert(Hi.end(),pk.at(i).begin(),pk.at(i).end()); H.push_back(Hi); } vector<bit> result(n); vector<bigint> positions; for (bigint j = 0;j < w;++j) positions.push_back(j); for (bigint iter = 0;iter < iters;++iter) { vector<bit> Hguess(n-k); for (bigint i = 0;i < n-k;++i) for (bigint j = 0;j < w;++j) Hguess.at(i) ^= H.at(i).at(positions.at(j)); bit mismatch; for (bigint j = 0;j < n-k;++j) mismatch |= Hguess[j]^ct[j]; for (bigint j = 0;j < n;++j) result.at(j) &= mismatch; for (bigint j = 0;j < w;++j) result.at(positions.at(j)) = result.at(positions.at(j)).orn(mismatch); for (bigint j = w-1;j >= 0;--j) { positions.at(j) += 1; if (positions.at(j) <= n-w+j) { while (++j < w) positions.at(j) = positions.at(j-1)+1; break; } } } return result; }