/* d:输入状态差分的最大汉明重量 */ void offLine_table_construct_v3(u32 d){ //创建输出流 //建立输出文件 string part="Grain_(l,d)_("; string curr_DIR=DIR_REDUCE_V3+part+int_2_string(KSLen_Reduced)+","+int_2_string(d)+")"+ FILE_SUFFIX+WITH_PREFIX+"_KSD_"+int_2_string(MAX_KSD)+"_HM_"+int_2_string(MAX_KSD_HM) +"_N_"+int_2_string(STATE_NUM)+"\\"; //建立目录,如果不存在则自动建立目录 char *tag; for(tag=(char*)curr_DIR.c_str();*tag;tag++){ if(*tag=='\\'){ char buf[1024],path[1024]; strcpy(buf,curr_DIR.c_str()); buf[strlen(curr_DIR.c_str())-strlen(tag)+1]=NULL; strcpy(path,buf); if(access(path,6)==-1) mkdir(path); } } for(int i=1;i<=d;i++){ //枚举所有输入差分的汉明重量为i的差分 u32 state_Len=STATE_REDUCE-SP; //总的状态数 减去sampling resistance的大小 cout<<"Grain reduce--开始处理输入差分汉明重量为:"<<i<<"的情况."<<endl; double time[4]={0}; start_cal(); combination_for_search_grain_reduce_v3(state_Len,i,curr_DIR); end_cal(time); cout<<"Grain reduce--输入差分汉明重量为:"<<i<<"的情况已经处理完毕."<<endl; printf("Time: %d hours, %d minutes, %f seconds.\n\n", (int)time[0],(int)time[1],time[2]); } }
void cal_all_KSD_construct_table(u32 d, u64* N_array){ //随机源的建立 srand((unsigned)time(NULL)); rc4_setup(); //建立目录,如果不存在则自动建立目录 cout<<"----------------------d:"<<d<<"--------------------"<<endl; string part="(d,l)_("; string curr_DIR=DIR+part+int_2_string(d)+","+int_2_string(KSLen)+")\\"; make_DIR(curr_DIR); for(int i=1;i<=d;i++){ cout<<"开始处理输入差分汉明重量为:"<<i<<"的情况."<<endl; combination_for_search_imp(LEN*8,i,N_array[i-1],curr_DIR); cout<<"输入差分汉明重量为:"<<i<<"的情况已经处理完毕.\n\n"<<endl; } }
/************************************************ 函数名:set_front_block 功能:设置前景方块 备注:无 ************************************************/ void UIBlock::set_front_block(FrontBlock _block) { front_block = _block; if (front_block.type == FrontBlockType::air) front_sprite->setVisible(false); else { front_sprite->setVisible(true); front_sprite->setTexture("texture\\block\\"+int_2_string(static_cast<ushort>(front_block.type)) + ".png"); } }
//Strategy II bool find_near_collision(Online_Data_Reduce *data_A,Online_Data_Reduce *data_B,unsigned long long set_size){ string subdir="Grain_(l,d)_("+int_2_string(KSLen_Reduced)+",4)"+ FILE_SUFFIX+"\\"; for(int i=0;i<set_size;i++){ if((i+1) % 1000 ==0){ cout<<"Collision finding proceed "<<setprecision(3)<<(double)i*100/set_size<<"%..."<<endl; } Online_Data_Reduce val_A=data_A[i]; for(int j=0;j<set_size;j++){ Online_Data_Reduce val_B=data_B[j]; //记录当前输入差分 u8 curr_state_Diff[STATE_BYTE]; for(int k=0;k<STATE_BYTE;k++) curr_state_Diff[k]=val_A.state[k]^val_B.state[k]; //记录当前输出差分 u8 curr_KS_Diff[KSLen_Reduced]; for(int k=0;k<KSLen_Reduced;k++) curr_KS_Diff[k]=val_A.KS[k]^val_B.KS[k]; //根据当前输出差分查表,并在表中寻找匹配的内部状态差分。 string tableName=char2HexString(curr_KS_Diff,KSLen_Reduced); tableName=DIR_REDUCE_V1+subdir+tableName+".txt"; ifstream infile; infile.open(tableName.c_str()); if(infile){ char val[2048]; while(infile.getline(val,sizeof(val))){ string str(val); //extract cube size string::size_type pos=str.find(" "); string str_state_Diff=str.substr(0,pos); if(state_comp(curr_state_Diff,STATE_BYTE,str_state_Diff)) return true; } }else{ cout<<"文件:"<<tableName<<"没有找到."<<endl; } //cout<<"file fail to load."<<endl; infile.close(); } } cout<<"Collision finding complete...fail to find match."<<endl; return false; }
void analyze_collected_data_v2(u32 d){ rc4_setup(); //random select a key and IV and run the initial phase ECRYPT_ctx_reduce* ctx_reduce=new ECRYPT_ctx_reduce; u8* key_R=new u8[4](); u8* IV_R=new u8[3](); for(int i=0;i<4;i++){ key_R[i]= rc4(); } for(int i=0;i<3;i++){ IV_R[i]= rc4(); } ECRYPT_keysetup_reduce(ctx_reduce,key_R,32,24); ECRYPT_ivsetup_reduce(ctx_reduce,IV_R); unsigned long long set_size=ceil(pow((double)2,DATA_SET)); cout<<"Data set size:2^{"<<DATA_SET<<"}"<<endl; Online_Data_Reduce *data_A=new Online_Data_Reduce[set_size]; Online_Data_Reduce *data_B=new Online_Data_Reduce[set_size]; collect_sets_v2_with_prefix(ctx_reduce,data_A,data_B,1,set_size); //collect_sets_v2_no_prefix(ctx_reduce,data_A,data_B,1,set_size); //分析已收集的数据 string part="Analyze_data"; string curr_DIR=DIR_REDUCE_TEST_V2+part+"_(l,d)_(" +int_2_string(KSLen_Reduced)+","+int_2_string(d)+")"+WITH_PREFIX+"\\"; //建立目录,如果不存在则自动建立目录 char *tag; for(tag=(char*)curr_DIR.c_str();*tag;tag++){ if(*tag=='\\'){ char buf[1024],path[1024]; strcpy(buf,curr_DIR.c_str()); buf[strlen(curr_DIR.c_str())-strlen(tag)+1]=NULL; strcpy(path,buf); if(access(path,6)==-1) mkdir(path); } } for(int i=0;i<set_size;i++){ if((i+1) % 20000 ==0){ cout<<"Analyze proceed "<<setprecision(3)<<(double)i*100/set_size<<"%..."<<endl; } for(int j=0;j<set_size;j++){ //计算当前输出差分 u8 curr_KS_Diff[KSLen_Reduced]; for(int k=0;k<KSLen_Reduced;k++) curr_KS_Diff[k]=((data_A[i]).KS[k])^((data_B[j]).KS[k]); //计算当前对应的状态差分 u8 curr_state_Diff[STATE_BYTE]; for(int k=0;k<STATE_BYTE;k++) curr_state_Diff[k]=((data_A[i]).state[k])^((data_B[j]).state[k]); curr_state_Diff[1]&=0x03; curr_state_Diff[2]&=0xe0; if(Hamming_weight_of_state(curr_state_Diff,STATE_BYTE)<=d){ /*if(Hamming_weight_of_state(curr_KS_Diff,KSLen_Reduced)<=MAX_KSD_HM){*/ //把满足这样条件的KSD和ISD都记录下来 string KSD=char2HexString(curr_KS_Diff,KSLen_Reduced); string ISD=char2HexString(curr_state_Diff,STATE_BYTE); string fileName=curr_DIR+KSD+".txt"; ofstream outfile; outfile.open(fileName.c_str(),ofstream::app); outfile<<fixed<<showpoint; if(outfile){ outfile<<ISD<<"\n"; } outfile.close(); } } } cout<<"Analyze complete."<<endl; }