bool KG_GeneratePvtY(int level, char* keytemplate, char* pvt_text, char* y_text) { char tmp[256] = ""; BigInt p, p1, pub, pvt, y, temp, temp2; int size, keysystem; if(level == 29) { char basepoint_text[10] = "", public_x_text[100] = "", public_y_text[100] = ""; KG_GenerateEcdsaParameters(keytemplate, pvt_text, basepoint_text, public_x_text, public_y_text); sprintf(y_text, "%s,%s,%s", basepoint_text, public_x_text, public_y_text); return true; } else if(level >= 20) { keysystem = KS_SHORTV3; level = level - 20; if(level > 8) return false; } else if(level >= 10) { keysystem = KS_V3; level = level - 10; if(level > 8) return false; } else { keysystem = KS_V2; if(level<0 or level>3) return false; } size = level + 4; p = BigInt_Create(); p1 = BigInt_Create(); pub = BigInt_Create(); pvt = BigInt_Create(); y = BigInt_Create(); temp = BigInt_Create(); temp2 = BigInt_Create(); BigInt_Set(temp, 1); BigInt_Shift(temp, size * 8, p); BigInt_Set(temp, primeoffsets[level]); BigInt_Add(p, temp, temp2); BigInt_Copy(p, temp2); BigInt_Subtract(p, BigInt_One(), p1); sprintf(tmp, "%u Level Public Key", level); GenerateKeyNumberFromString(tmp, p, &pub, keysystem, ((keysystem == KS_V3 || keysystem == KS_SHORTV3) ? level + 1 : 0)); GenerateKeyNumberFromString(keytemplate, p, &pvt, keysystem, ((keysystem == KS_V3 || keysystem == KS_SHORTV3) ? level + 1 : 0)); BigInt_ToString(pvt, 16, pvt_text); BigInt_PowerModulus(pub, pvt, p, y); BigInt_ToString(y, 16, y_text); BigInt_Destroy(temp2); BigInt_Destroy(temp); BigInt_Destroy(y); BigInt_Destroy(pvt); BigInt_Destroy(pub); BigInt_Destroy(p1); BigInt_Destroy(p); return true; }
int BigInt_Divide(BigInt a, BigInt b, BigInt answer, BigInt remainder) { //TODO: fix something here (memleak temp1 or t) int compare, i, signa, signb; WORKING_DIGIT high, low, t; BigInt temp1, temp2; temp1=BigInt_Create(); temp2=BigInt_Create(); signa=a->negative; a->negative=0; signb=b->negative; b->negative=0; /* Check for divide-by-zero, it's not allowed */ if(b->length==1 && b->digits[0]==0) return 0; /* Compare a and b, to see if we can take a shortcut */ compare=BigInt_Compare_SignOptional(a, b, 1); if(compare<0) { BigInt_Set(answer, 0); BigInt_Copy(remainder, a); return 1; } else if(compare==0) { BigInt_Set(answer, 1); BigInt_Set(remainder, 0); return 1; } BigInt_Realloc(answer, a->length, 0); BigInt_Set(remainder, 0); for(i=1; i<=a->length; ++i) { /* remainder=(remainder<<BITS_PER_DIGIT)+a->digits[a->length-i]; */ BigInt_Copy(temp1, remainder); BigInt_Shift(temp1, BITS_PER_DIGIT, remainder); remainder->digits[0]=a->digits[a->length-i]; if(BigInt_Compare_SignOptional(remainder, b, 1)>=0) { high=OVERFLOW_DIGIT; low=0; while(low<high) { t=((high-low)/2)+low; /* if ((b*t)>remainder) high=t; else low=t+1; */ BigInt_Set(temp2, t); BigInt_Multiply(b, temp2, temp1); if(BigInt_Compare(temp1, remainder)>0) high=t; else low=t+1; } t=low-1; answer->digits[a->length-i]=(DIGIT)(t); /* remainder=remainder-(b*t) */ BigInt_Set(temp2, t); BigInt_Multiply(b, temp2, temp1); BigInt_Subtract(remainder, temp1, temp2); BigInt_Copy(remainder, temp2); } else answer->digits[a->length-i]=0; } a->negative=signa; b->negative=signb; answer->negative=(a->negative ^ b->negative); BigInt_FindMSD(answer); BigInt_Destroy(temp2); BigInt_Destroy(temp1); return 1; }