integer string_to_integer(char* s) { integer result = create_integer((int)ceil(LOG_2_10*strlen(s)/COMPONENT_BITS)); set_zero_integer(result); integer digit = create_integer(1); int i; for (i = 0; s[i] != '\0'; i++) { multiply_small_integer(result, 10, result); digit.c[0] = s[i] - '0'; add_integer(result, digit, result); } free_integer(digit); return result; }
void mod_integer(integer left, integer right, integer result) { integer mod_two_power = create_integer(right.num_components + 1); int i, bit; set_zero_integer(result); set_zero_integer(mod_two_power); mod_two_power.c[0] = 1; for(i=0; i<left.num_components; i++) { for(bit=0; bit<COMPONENT_BITS; bit++) { if ((left.c[i] & (1 << bit)) != 0) { add_integer(result, mod_two_power, result); if (compare_integers(result, right) >= 0) { subtract_integer(result, right, result); } } shift_left_one_integer(mod_two_power); if (compare_integers(mod_two_power, right) >= 0) { subtract_integer(mod_two_power, right, mod_two_power); } } } free_integer(mod_two_power); }
integer hex_to_integer(unsigned char* data, unsigned int datalenBytes, unsigned int pad){ integer base=string_to_integer("256"); int siz=datalenBytes; integer radix=create_integer(siz); set_zero_integer(radix); radix.c[0]=1; integer result=create_integer(siz*2+pad); set_zero_integer(result); integer digit=create_integer(1); for(int i=datalenBytes-1; i>=0; i--){ digit.c[0]=data[i]; integer part_res=create_integer(1+radix.num_components+1); set_zero_integer(part_res); multiply_integer(digit,radix,part_res); add_integer(result,part_res,result); free_integer(part_res); integer radix_temp=create_integer(radix.num_components); copy_integer(radix,radix_temp); multiply_integer(radix_temp,base,radix); free_integer(radix_temp); } optimize_integer(result); return result; }
int pow_mod(unsigned char** result,unsigned char* p,unsigned char* q, unsigned char* r,unsigned int keyLengthBytes){ std::set_new_handler(newh); Array<integer> factors; integer div=hex_to_integer(q,keyLengthBytes,0); int siz=keyLengthBytes; integer power_of_two=create_integer(siz); set_zero_integer(power_of_two); integer zero=create_integer(1); set_zero_integer(zero); integer two=string_to_integer("2"); integer one=string_to_integer("1"); int i=0; integer div_temp=create_integer(div.num_components); integer rem=create_integer(div.num_components+2); while(compare_integers(div, zero) == 1){ set_zero_integer(rem); mod_integer(div, two,rem); copy_integer(div,div_temp); divide_small_integer(div_temp,2,div); if(compare_integers(rem, zero) == 1){ integer ptt=create_integer(power_of_two.num_components); copy_integer(power_of_two,ptt); factors.pushBack(ptt); } add_integer(power_of_two,one,power_of_two); i++; } free_integer(rem); free_integer(div_temp); Array<integer> partial_results; integer part_res=hex_to_integer(p,keyLengthBytes,2); integer ri=hex_to_integer(r,keyLengthBytes,1); integer idx = create_integer(part_res.num_components); set_zero_integer(idx); integer part_res_temp1=create_integer(part_res.num_components); integer part_res_temp2=create_integer(part_res.num_components); integer part_res_temp3=create_integer(part_res.num_components); for(int i=0; i<factors.size(); i++){ while(compare_integers(factors[i],idx)==1){ copy_integer(part_res,part_res_temp1); copy_integer(part_res,part_res_temp2); multiply_integer(part_res_temp1,part_res_temp2,part_res); copy_integer(part_res,part_res_temp3); mod_integer(part_res_temp3,ri,part_res); add_integer(idx,one,idx); } integer ptt=create_integer(part_res.num_components); copy_integer(part_res,ptt); partial_results.pushBack(ptt); } free_integer(part_res_temp1); free_integer(part_res_temp2); free_integer(part_res_temp3); integer resulti=create_integer(ri.num_components+1); set_zero_integer(resulti); resulti.c[0]=1; integer resulti_temp=create_integer(resulti.num_components); integer resulti_temp2=create_integer(resulti.num_components); for(int i=0; i<partial_results.size(); i++){ copy_integer(resulti,resulti_temp); multiply_integer(resulti_temp,partial_results[i],resulti); copy_integer(resulti,resulti_temp2); mod_integer(resulti_temp2,ri,resulti); } free_integer(resulti_temp); free_integer(resulti_temp2); int lol=integer_to_binary(result,resulti,keyLengthBytes); return lol; }