-rw-r--r-- 9457 cryptattacktester-20231020/isd2_params.cpp raw
#include <cassert>
#include "selection.h"
#include "isd2_params.h"
using namespace std;
bool isd2_params_valid(const vector<bigint> ¶ms,const vector<bigint> &attackparams)
{
bigint N = params.at(0);
bigint K_orig = params.at(1);
bigint W = params.at(2);
bigint pos = 0;
bigint ITERS = attackparams.at(pos++);
bigint RESET = attackparams.at(pos++);
bigint X = attackparams.at(pos++);
bigint YX = attackparams.at(pos++); auto Y = X+YX;
bigint PIJ = attackparams.at(pos++);
bigint PI = attackparams.at(pos++);
bigint L0 = attackparams.at(pos++);
bigint L1 = attackparams.at(pos++);
bigint CHECKPI = attackparams.at(pos++);
bigint CHECKSUM = attackparams.at(pos++);
bigint D = attackparams.at(pos++);
bigint Z = attackparams.at(pos++);
bigint QU0 = attackparams.at(pos++);
bigint QF0 = attackparams.at(pos++); auto PE0 = QF0*QU0;
bigint WI0 = attackparams.at(pos++);
bigint QU1 = attackparams.at(pos++);
bigint QF1 = attackparams.at(pos++); auto PE1 = QF1*QU1;
bigint WI1 = attackparams.at(pos++);
bigint FW = attackparams.at(pos++);
if (attackparams.size() != pos) return 0;
bigint K = K_orig-FW;
if (ITERS < 1) return 0;
if (RESET < 1) return 0;
if (ITERS%RESET != 0) return 0;
if (PIJ < 1) return 0;
if (PIJ*4 > W) return 0;
if (PI < 2) return 0;
if (PI > 2*PIJ) return 0;
if (PI%2 != 0) return 0;
if (L0 < 1) return 0;
if (L1 < 1) return 0;
bigint L = L0+L1;
if (K+L < W) return 0;
if (L > N-K-W) return 0;
if (X < 1) return 0;
if (X > N-K-L) return 0;
if (Y < X) return 0;
if (Y > K+L) return 0;
if (Z < 0) return 0;
if (Z > K+L-PIJ*4) return 0;
// guarantees PIJ <= (K+L-Z)/4
// guarantees PI <= (K+L-Z)/2
if (CHECKPI < 0) return 0;
if (CHECKPI > 1) return 0;
if (CHECKSUM < 0) return 0;
if (CHECKSUM > 1) return 0;
if (CHECKSUM+CHECKPI == 0) return 0;
if (D < 1) return 0;
if (((D-1)>>L0) >= 1) return 0;
if (QU0 < 1) return 0;
if (PE0 < 1) return 0;
if (WI0 < 1) return 0;
if (QU1 < 1) return 0;
if (PE1 < 1) return 0;
if (WI1 < 1) return 0;
if (FW < 0) return 0;
if (FW > 1) return 0;
return 1;
}
void isd2_params(const vector<bigint> ¶ms,map<string,string> &S,int (*callback)(const vector<bigint> &))
{
bigint N = params.at(0);
bigint K_orig = params.at(1);
bigint W = params.at(2);
bigint FWmin = 0;
bigint FWmax = 1;
selection_constrain(S,"FW",FWmin,FWmax);
for (bigint FW = 0;FW <= 1;++FW) {
if (FW < FWmin) continue;
if (FW > FWmax) continue;
bigint K = K_orig-FW;
bigint PIJmin = 1;
bigint PIJmax = 3;
selection_constrain(S,"PIJ",PIJmin,PIJmax);
for (bigint PIJ = PIJmin;PIJ <= PIJmax;++PIJ) {
if (PIJ < 1) continue;
if (PIJ*4 > W) continue;
bigint PImin = 2;
bigint PImax = 2*PIJ;
selection_constrain(S,"PI",PImin,PImax);
for (bigint PI = PImin;PI <= PImax;PI += 2) {
if (PI < 2) continue;
if (PI > 2*PIJ) continue;
if (PI%2 != 0) continue;
bigint Lguess = nbits(binomial(K/2,PIJ));
bigint L0min = 1;
bigint L0max = 5;
selection_constrain(S,"L0",L0min,L0max);
for (bigint L0 = L0min;L0 <= L0max;++L0) {
if (L0 < 1) continue;
bigint L1min = 1;
bigint L1max = 5;
selection_constrain(S,"L1",L1min,L1max);
for (bigint L1 = L1min;L1 <= L1max;++L1) {
if (L1 < 1) continue;
bigint L = L0+L1;
if (K+L < W) continue;
if (L > N-K-W) continue;
bigint ITERSmin = 1;
bigint ITERSmax = (16*binomial(N,W))/(binomial(K/2,PI)*binomial(K-K/2,PI)*binomial(N-K-L0-L1,W-PI*2));
if (ITERSmax > 1024) ITERSmax = 1024;
selection_constrain(S,"I",ITERSmin,ITERSmax);
if (ITERSmin < 1) continue;
for (bigint ITERS = ITERSmin;ITERS <= ITERSmax;ITERS *= 2) {
if (ITERS < 1) break;
bigint Zmin = 0;
bigint Zmax = L;
selection_constrain(S,"Z",Zmin,Zmax);
for (bigint Z = Zmin;Z <= Zmax;Z += (L > 0 ? L : bigint(1))) {
if (Z < 0) continue;
if (Z > K+L-4*PIJ) continue;
bigint Xmin = 1;
bigint Xmax = 8;
selection_constrain(S,"X",Xmin,Xmax);
if (Xmin < 1) continue;
for (bigint X = Xmin;X <= Xmax;X *= 2) {
if (X < 1) break;
if (X > N-K-L) continue;
bigint RESETmin = 1;
bigint RESETmax = 1024;
if (X > 1) RESETmin = 2;
selection_constrain(S,"RE",RESETmin,RESETmax);
if (RESETmin < 1) continue;
for (bigint RESET = RESETmin;RESET <= RESETmax;RESET *= 2) {
if (RESET < 1) break;
if (ITERS%RESET != 0) continue;
bigint YXdiffmin = 1;
while ((RESET>>YXdiffmin) > 0) ++YXdiffmin;
bigint YXdiffmax = YXdiffmin+5;
if (RESET == 1) YXdiffmin = YXdiffmax = 1;
selection_constrain(S,"YX",YXdiffmin,YXdiffmax);
if (YXdiffmin < 0) continue;
for (bigint YXdiff = YXdiffmin;YXdiff <= YXdiffmax;++YXdiff) {
if (YXdiff < 1) break;
bigint Y = X+YXdiff;
if (Y < X) continue;
if (Y > K+L) continue;
bigint Dmin = 1;
bigint Dmax = 8;
selection_constrain(S,"D",Dmin,Dmax);
if (Dmin < 1) continue;
for (bigint D = Dmin;D <= Dmax;D *= 2) {
if (D < 1) break;
if (((D-1)>>L0) >= 1) continue;
bigint QF0min = 1;
bigint QF0max = 8;
selection_constrain(S,"QF0",QF0min,QF0max);
if (QF0min < 1) continue;
for (bigint QF0 = QF0min;QF0 <= QF0max;QF0 *= 2) {
if (QF0 < 1) break;
bigint QU0min = 1;
bigint QU0max = 8;
selection_constrain(S,"QU0",QU0min,QU0max);
if (QU0min < 1) continue;
for (bigint QU0 = QU0min;QU0 <= QU0max;QU0 *= 2) {
if (QU0 < 1) break;
bigint WI0min = 1;
bigint WI0max = 3;
selection_constrain(S,"WI0",WI0min,WI0max);
for (bigint WI0 = WI0min;WI0 <= WI0max;++WI0) {
if (WI0 < 1) continue;
bigint QF1min = 1;
bigint QF1max = 8;
selection_constrain(S,"QF1",QF1min,QF1max);
if (QF1min < 1) continue;
for (bigint QF1 = QF1min;QF1 <= QF1max;QF1 *= 2) {
if (QF1 < 1) break;
bigint QU1min = 1;
bigint QU1max = 8;
selection_constrain(S,"QU1",QU1min,QU1max);
if (QU1min < 1) continue;
for (bigint QU1 = QU1min;QU1 <= QU1max;QU1 *= 2) {
if (QU1 < 1) break;
bigint WI1min = 1;
bigint WI1max = 3;
selection_constrain(S,"WI1",WI1min,WI1max);
for (bigint WI1 = WI1min;WI1 <= WI1max;++WI1) {
if (WI1 < 1) continue;
bigint CHECKPImin = 0;
bigint CHECKPImax = 1;
selection_constrain(S,"CP",CHECKPImin,CHECKPImax);
for (bigint CHECKPI = CHECKPImin;CHECKPI <= CHECKPImax;++CHECKPI) {
if (CHECKPI < 0) continue;
if (CHECKPI > 1) continue;
bigint CHECKSUMmin = 0;
bigint CHECKSUMmax = 1;
selection_constrain(S,"CS",CHECKSUMmin,CHECKSUMmax);
for (bigint CHECKSUM = 0;CHECKSUM <= 1;++CHECKSUM) {
if (CHECKSUM < 0) continue;
if (CHECKSUM > 1) continue;
if (CHECKSUM == 0 && CHECKPI == 0) continue;
vector<bigint> Q{ITERS,RESET,X,Y-X,PIJ,PI,L0,L1,CHECKPI,CHECKSUM,D,Z,QU0,QF0,WI0,QU1,QF1,WI1,FW};
if (callback(Q) < 0) return;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}