|
-
July 24th, 2012, 09:20 PM
#1
RSA modular exponentation sending out wrong modules
Hi guys, it's the first time for me in this forum and i hope you could help me somehow.
Right now i'm trying the first step of RSA: generating prime numbers (4 digits, as a start).
I allocate the memory, I random generate an uneven number, and now i have to do the 2^n % n-1 = 1 to see if the number is actually prime.
That's the code:
Code:
void PotenzaModulare(struct Vector* Esp, struct Vector* Base, struct Vector* result, struct Vector* Mod)
{
int i2;
int i;
struct Vector tmp;
struct Vector tmpBaseBase;
struct Vector tmpBase;
for (i2=0; i2<Esp->Dim; i2++)
{
tmpBase.Dim= DIM_PRIME;
tmpBase.Num = (int *)malloc(sizeof(int)*tmpBase.Dim);
Riempi(&tmpBase, 1);
tmpBaseBase.Dim= DIM_PRIME*6;
tmpBaseBase.Num = (int *)malloc(sizeof(int)*tmpBaseBase.Dim);
Riempi(&tmpBaseBase, 1);
tmp.Dim= DIM_PRIME*6;
tmp.Num = (int *)malloc(sizeof(int)*tmp.Dim);
Riempi(&tmp, 1);
for (i=0; i<DIM_PRIME;i++) tmpBase.Num[i] = Base->Num[i];
if(Esp->Num[i2]==1) //ESP IS A BINARY NUMBER
{
Riempi(&tmp, 1);
InitMoltiplicazione(result, Base, &tmp); //KARATSUBA MOLTIPLICATION
tmp.Dim=DIM_PRIME*2;
InvertiNumero(&tmp, tmp.Dim);
int i;
for(i=tmp.Dim-1; i>-1; i--)
if (tmp.Num[i] ==0) tmp.Dim--;
else break;
InvertiNumero(Mod, Mod->Dim); //REVERSE A NUMBER
if(tmp.Dim<Mod->Dim)
{
for(i=0; i<tmp.Dim; i++)
result->Num[result->Dim-1-i]=tmp.Num[i];
}
else
{
result->Num = LongMod(tmp.Num, Mod->Num, tmp.Dim, Mod->Dim);
SistemaModulo(result); //CLEANUP THE MODULE
PareggiaModulazione(result, DIM_PRIME); //ADDS 0s FOR NEXT KARATSUBA
}
tmp.Dim=DIM_PRIME*6;
InvertiNumero(Mod, Mod->Dim);
}
Riempi(&tmp, 1); //RESET A "Vector"
InitMoltiplicazione(&tmpBase, Base, &tmp);
tmp.Dim=DIM_PRIME*2;
InvertiNumero(&tmp, tmp.Dim);
for(i=tmp.Dim-1; i>-1; i--)
if (tmp.Num[i] ==0) tmp.Dim--;
else break;
if(tmp.Dim>=Mod->Dim)
{
InvertiNumero(Mod, Mod->Dim);
Base->Num = LongMod(tmp.Num, Mod->Num, tmp.Dim, Mod->Dim); <------*****
SistemaModulo(Base);
PareggiaModulazione(Base, DIM_PRIME);
InvertiNumero(Mod, Mod->Dim);
}
else
{
int IndiceWrite= Base->Dim-tmp.Dim;
for (int i=tmp.Dim-1; i>=0; i--)
Base->Num[IndiceWrite++]=tmp.Num[i];
}
}
*Useless things*
}
It's not really cleaned up, i know... sorry for that. Anyway what I'm trying to do here is this:
Code:
function modular_pow(base, exponent, modulus)
result := 1
while exponent > 0
if (exponent mod 2 == 1):
result := (result * base) mod modulus
exponent := exponent >> 1
base = (base * base) mod modulus
return result
"Vector" is a struct described as follows
Code:
struct Vector
{
int* Num; //Vettore di numeri, rappresentano un operando
int Dim;
};
and the "DIM_PRIME" variable is a #define with value 4
Now what's happening here is: the code works perfectly until a random run when he reaches the line with the <------*****
Sometimes it works, sometimes it doesn't and i really can't understand why.
I have a test project where to run the LongMod method and there it works perfectly with the provided inputs.
Here a screen of the situation:
need help.jpg
"Qui sbaglia" = Here it fails
"Qui no" = Here it doesn't
I'm starting to think that some memory leak is currupting my program causing the LongMod to fail but i don't really know where to look at... any help is greatly appreciated!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|