staticbool biModIterative(uint32_t *num, const uint32_t *denum, uint32_t *tmp, uint32_t *state1, uint32_t *state2, uint32_t step) //num %= denum where num is RSA_LEN * 2 and denum is RSA_LEN and tmp is RSA_LEN + limb_sz //will need to be called till it returns true (up to RSA_LEN * 2 + 2 times)
{
uint32_t bitsh = *state1, limbsh = *state2; bool ret = false;
int64_t t;
int32_t i;
//first step is init if (!step) { //initially set it up left shifted as far as possible
memcpy(tmp + 1, denum, RSA_BYTES);
tmp[0] = 0;
bitsh = 32;
limbsh = RSA_LIMBS - 1; goto out;
}
//second is shifting denum if (step == 1) { while (!(tmp[RSA_LIMBS] & 0x80000000)) { for (i = RSA_LIMBS; i > 0; i--) {
tmp[i] <<= 1; if (tmp[i - 1] & 0x80000000)
tmp[i]++;
} //no need to adjust tmp[0] as it is still zero
bitsh++;
} goto out;
}
//all future steps do the division
//check if we should subtract (uses less space than subtracting and unroling it later) for (i = RSA_LIMBS; i >= 0; i--) { if (num[limbsh + i] < tmp[i]) goto dont_subtract; if (num[limbsh + i] > tmp[i]) break;
}
//subtract
t = 0; for (i = 0; i <= RSA_LIMBS; i++) {
t += (uint64_t)num[limbsh + i];
t -= (uint64_t)tmp[i];
num[limbsh + i] = t;
t >>= 32;
}
//carry the subtraction's carry to the end for (i = RSA_LIMBS + limbsh + 1; i < RSA_LIMBS * 2; i++) {
t += (uint64_t)num[i];
num[i] = t;
t >>= 32;
}
dont_subtract: //handle bitshifts/refills if (!bitsh) { // tmp = denum << 32 if (!limbsh) {
ret = true; goto out;
}
//zero the result on first call if (!step)
memset(ret, 0, RSA_BYTES * 2);
//produce a partial sum & add it in
c = 0; for (j = 0; j < RSA_LIMBS; j++) {
r = (uint64_t)a[step] * b[j] + c + ret[step + j];
ret[step + j] = r;
c = r >> 32;
}
//carry the carry to the end for (j = step + RSA_LIMBS; j < RSA_LIMBS * 2; j++) {
r = (uint64_t)ret[j] + c;
ret[j] = r;
c = r >> 32;
}
}
/* *PiecewiseRSA: *normalRSApublicopwith65537exponentdoes34operations.17mulsand17mods,asfollows: *16x{mul,mod}tocalculatea^65536modc *1x{mul,mod}tocalculatea^65537modc *webreakupeachmulandmoditselfintomoresteps.mulneedsRSA_LIMBSsteps,andmodneedsuptoRSA_LEN*2+2steps *soifweallocateRSA_LEN*3stepvaluestomod,eachmul-modpairwilluse<=RSA_LEN*4stepvalues *andthewholeopetaionwillneed<=RSA_LEN*4*34stepvalues,whichfitsintoauint32.cool.Infact *somevalueswillbeskipped,butthismakeslifeeasier,really.Callthisfuncwith*stepP=0,andkeepcallingtill *outputstepPiszero.We'llcalleachoftheRSA_LEN*4piecesagigastep,andhave17ofthemasseenabove.Each *willbelogicallyseparatedinto4megasteps.FirstwillcontaintheMUL,last3theMODandmaybethememcpy. *Inthefirst16gigasteps,theverylaststepofthegigastepwillbeusedforthememcpycall. * *Theinitialnon-iterativeRSAlogiclooksasfollows,shownhereforclarity: * *memcpy(state->tmpB,a,RSA_BYTES); *for(i=0;i<16;i++){ *biMul(state->tmpA,state->tmpB,state->tmpB); *biMod(state->tmpA,c,state->tmpB); *memcpy(state->tmpB,state->tmpA,RSA_BYTES); *} * *//calculate a ^ 65537 mod c into state->tmpA [ at this point this means do state->tmpA = (state->tmpB * a) % c ] *biMul(state->tmpA,state->tmpB,a); *biMod(state->tmpA,c,state->tmpB); * *//return result *returnstate->tmpA; *
*/
if (!megaSubstep) { // first megastep of the gigastep - MUL
biMulIterative(state->tmpA, state->tmpB, gigastep == 16 ? a : state->tmpB, gigastepSubstep); if (gigastepSubstep == RSA_LIMBS - 1) //MUL is done - do mod next
step = gigastepBase + RSA_LEN + 1; else//More of MUL is left to do
step++;
} elseif (gigastepSubstep != RSA_LEN * 4 - 1){ // second part of gigastep - MOD if (biModIterative(state->tmpA, c, state->tmpB, state1, state2, gigastepSubstep - RSA_LEN)) { //MOD is done if (gigastep == 16) // we're done
step = 0; else// last part of the gigastep is a copy
step = gigastepBase + RSA_LEN * 4 - 1 + 1;
} else
step++;
} else { //last part - memcpy
memcpy(state->tmpB, state->tmpA, RSA_BYTES);
step++;
}
}
memcpy(state->tmpC, a, RSA_BYTES); //tC will hold our powers of a
memset(state->tmpA, 0, RSA_BYTES * 2); //tA will hold result
state->tmpA[0] = 1;
for (i = 0; i < RSA_LEN; i++) { //if the bit is set, multiply the current power of A into result if (b[i / 32] & (1 << (i % 32))) {
memcpy(state->tmpB, state->tmpA, RSA_BYTES);
biMul(state->tmpA, state->tmpB, state->tmpC);
biMod(state->tmpA, c, state->tmpB);
}
//calculate the next power of a and modulus it #ifdefined(RSA_SUPPORT_PRIV_OP_LOWRAM)
memcpy(state->tmpB, state->tmpA, RSA_BYTES); //save tA
biMul(state->tmpA, state->tmpC, state->tmpC);
biMod(state->tmpA, c, state->tmpC);
memcpy(state->tmpC, state->tmpA, RSA_BYTES);
memcpy(state->tmpA, state->tmpB, RSA_BYTES); //restore tA #elifdefined (RSA_SUPPORT_PRIV_OP_BIGRAM)
memcpy(state->tmpB, state->tmpC, RSA_BYTES);
biMul(state->tmpC, state->tmpB, state->tmpB);
biMod(state->tmpC, c, state->tmpB); #endif
}
return state->tmpA;
} #endif
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.13 Sekunden
(vorverarbeitet am 2026-06-27)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.