-rw-r--r-- 1771 cryptattacktester-20230614/circuitcost.cpp raw
#include <iostream>
#include "problem.h"
#include "attack.h"
using namespace std;
static bool cacheinit = 0;
static problem Ecached;
static vector<bigint> Pcached;
static vector<bool> pub;
int attack_handle(const problem &E,const vector<bigint> &P,const attack &A,const vector<bigint> &Q)
{
bigint maxcost = 1073741824;
selection_constrain(attack_selection,"maxcost",maxcost,maxcost);
bigint predictedcost = A.cost(P,Q);
cout << "circuitcost";
cout << " problem=" << E.name;
for (bigint j = 0;j < P.size();++j)
cout << (j ? ',' : ' ') << E.paramnames.at(j) << "=" << P.at(j);
cout << " attack=";
cout << A.name;
for (bigint j = 0;j < Q.size();++j)
cout << (j ? ',' : ' ') << A.paramnames.at(j) << "=" << Q.at(j);
cout << " prediction " << predictedcost;
if (predictedcost > maxcost) {
cout << " skipping\n" << flush;
return 1;
}
if (cacheinit) {
if (E.psgen != Ecached.psgen) cacheinit = 0;
if (E.paramnames != Ecached.paramnames) cacheinit = 0;
if (P != Pcached) cacheinit = 0;
}
if (!cacheinit) {
Ecached = E;
Pcached = P;
pub = E.psgen(P).first;
cacheinit = 1;
}
vector<bit> pubbit;
for (bigint j = 0;j < pub.size();++j)
pubbit.push_back(bit(pub.at(j)));
vector<bigint> Pbigint;
for (bigint j = 0;j < P.size();++j)
Pbigint.push_back((bigint) (P.at(j)));
vector<bigint> Qbigint;
for (bigint j = 0;j < Q.size();++j)
Qbigint.push_back((bigint) (Q.at(j)));
bit::clear_all();
vector<bit> attackoutput = A.circuit(pubbit,Pbigint,Qbigint);
bigint cost = bit::ops();
for (auto i: bit_ops_selectors)
cout << " " << bit::opsname(i) << " " << bit::ops(i);
if (cost != predictedcost) cout << " [1;31mALERT[0m";
cout << '\n' << flush;
return 1;
}