-rw-r--r-- 1869 cryptattacktester-20230614/bruteforce2.cpp raw
#include "decoding.h"
#include "bit_vector.h"
#include "index.h"
#include "bruteforce2.h"
using namespace std;
vector<bit> bruteforce2(
const vector<bit> &bits,
const vector<bigint> ¶ms,
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<bit> result(n);
for (bigint v = 0;v <= w;++v) {
if (iters <= 0) break;
vector<bigint> positions;
vector<vector<bit>> ctHguess;
ctHguess.push_back(ct);
vector<bit> ctplus = ct;
for (bigint j = 0;j < v;++j) {
positions.push_back(n-k+j);
for (bigint i = 0;i < n-k;++i)
ctplus.at(i) ^= pk.at(i).at(positions.at(j)-(n-k));
ctHguess.push_back(ctplus);
}
while (iters > 0) {
--iters;
bit mismatch = bit_vector_hamming_weight_isnot(ctHguess.at(v),w-v);
for (bigint j = 0;j < n;++j)
result.at(j) &= mismatch;
for (bigint j = 0;j < v;++j)
result.at(positions.at(j)) = result.at(positions.at(j)).orn(mismatch);
for (bigint j = 0;j < n-k;++j)
result.at(j) = result.at(j) | ctHguess.at(v).at(j).andn(mismatch);
bigint j;
for (j = v-1;j >= 0;--j) {
positions.at(j) += 1;
if (positions.at(j) <= n-v+j) {
for (bigint i = 0;i < n-k;++i)
ctHguess.at(j+1).at(i) = ctHguess.at(j).at(i) ^ pk.at(i).at(positions.at(j)-(n-k));
while (++j < v) {
positions.at(j) = positions.at(j-1)+1;
for (bigint i = 0;i < n-k;++i)
ctHguess.at(j+1).at(i) = ctHguess.at(j).at(i) ^ pk.at(i).at(positions.at(j)-(n-k));
}
break;
}
}
if (j < 0) break;
}
}
return result;
}