-rw-r--r-- 2585 cryptattacktester-20231020/circuitexample.cpp raw
#include <iostream>
#include "problem.h"
#include "attack.h"
using namespace std;
static bigint maxcost = 1073741824;
static bool cacheinit = 0;
static problem Ecached;
static vector<bigint> Pcached;
static vector<bool> pub;
static vector<bool> sec;
int attack_handle(const problem &E,const vector<bigint> &P,const attack &A,const vector<bigint> &Q)
{
  bigint predictedcost = A.cost(P,Q);
  if (predictedcost > maxcost) {
    cout << "circuitexample ";
    cout << "output";
    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;
    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;
    auto ps = E.psgen(P);
    pub = ps.first;
    sec = ps.second;
    cacheinit = 1;
  
    cout << "circuitexample ";
    cout << "input";
    cout << " problem=" << E.name;
    for (bigint j = 0;j < P.size();++j)
      cout << (j ? ',' : ' ') << E.paramnames.at(j) << "=" << P.at(j);
    cout << " pub ";
    for (bigint j = 0;j < pub.size();++j)
      cout << pub.at(j);
    cout << " sec ";
    for (bigint j = 0;j < sec.size();++j)
      cout << sec.at(j);
    cout << '\n';
  }
  
  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();
  
  cout << "circuitexample ";
  cout << "output";
  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;
  cout << " circuit " << cost;
  cout << " attack ";
  for (bigint j = 0;j < attackoutput.size();++j)
    cout << attackoutput.at(j).value();
  cout << '\n' << flush;
  return 1;
}