#include <stdio.h>
#include <stdlib.h>
#include "mirdef.h"
#include "miracl.h"
typedef struct Zp
{
bigvalue1;
bigvalue2;
struct Zp*next;
}pair;
void shanks(big, big, big, big, big );
int insert(pair *, big, big);
void find_elem_list(int ,int , pair *, big , big );
int search(pair * ,big , big , big );
void main(void)
{
int number_of_factors,k,i,comp;
big y, a, p, result, dummy_base, dummy_exp, comparator, gamma,pminus1, pi, dummy,zero;
big zj, delta, j, temp, bee, uno, *x_base, *moduli_base, *x_ptr,*moduli_ptr;
pair factorization;
pair *ptemp, bees;
mirsys (155, 10);
dummy_base=mirvar(1);
dummy_exp =mirvar(1);
dummy=mirvar(0);
comparator=mirvar(0);
pminus1=mirvar(1);
gamma=mirvar(0);
pi=mirvar(0);
y=mirvar(1);
a=mirvar(1);
p=mirvar(1);
zj=mirvar(0);
delta=mirvar(0);
temp=mirvar(0);
bee=mirvar(0);
uno=mirvar(1);
result=mirvar(0);
zero=mirvar(0);
factorization.value1=mirvar(0);
factorization.value2=mirvar(0);
factorization.next=NULL;
bees.value1=mirvar(0);
bees.value2=mirvar(0);
bees.next=NULL;
printf ("This program solves the Discrete Logarithm Problem");
printf (" using the Pohlig-Hellman Algorithm\n");
printf(" x \n");
printf ("It solves the equation y=a (mod p) for the value x given yand a \n");
printf ("Enter the value of y: "); innum (y,stdin);
printf ("Enter the value of a: "); innum (a,stdin);
printf ("Enter the value of p: "); innum (p,stdin);
decr(p,1,pminus1);
number_of_factors=1;
printf ("Enter the factors of p-1 and the powers to which they areraised\n");
printf ("(When you are done entering all the prime factors pleaseenter 0):\n");
while(1)
{
printf ("Enter the prime factor #%d: ",number_of_factors);
innum(dummy_base,stdin);
if (0==compare(dummy_base,comparator))
break;
printf ("Enter the exponent for prime factor#%d: ", number_of_factors );
innum(dummy_exp,stdin);
insert(&factorization, dummy_exp,dummy_base);
number_of_factors++;
}
x_base = (big *)calloc((number_of_factors-1), sizeof(big) );
moduli_base = (big *)calloc( (number_of_factors-1), sizeof(big));
x_ptr=x_base;
moduli_ptr=moduli_base;
for(k=0;k<(number_of_factors-1);k++)
{
(*x_ptr)=mirvar(0);
(*moduli_ptr)=mirvar(0);
x_ptr++;
moduli_ptr++;
}
for ( i = 1 , moduli_ptr = moduli_base , x_ptr=x_base;
i < number_of_factors;
i++, x_ptr++,moduli_ptr++ )
{
copy(pminus1,dummy);
find_elem_list(i,(number_of_factors-1),&factorization,pi,dummy_exp);
divide(dummy,pi,dummy);
powmod(a,dummy,p,gamma);
copy(y,zj);
comp = -1;
for( j=mirvar(1) ; compare(j,dummy_exp) !=(1) ; incr(j,1,j) )
{
powmod(pi,j,pminus1,temp);
copy(pminus1,dummy);
divide(dummy,temp,dummy);
powmod(zj,dummy,p,delta);
shanks(gamma,delta,p,pi,bee);
insert(&bees,bee,j);
decr(j,1,temp);
powmod(pi,temp,pminus1,temp);
multiply(bee,temp,temp);
powmod(a,temp,p,temp);
xgcd(temp,p,temp,temp,temp);
powmod2(temp,uno,zj,uno,p,zj);
}
powmod(pi,dummy_exp,p,dummy);
bee=mirvar(0);
comp=-1;
for( ptemp=bees.next, copy(zero,j) ;compare(j,dummy_exp) == (-1); incr(j,1,j) )
{
powmod2(ptemp->value1,uno,pi,j,dummy,temp);
add(bee,temp,bee);
ptemp =ptemp->next;
}
copy(bee,*x_ptr);
copy(dummy,*moduli_ptr);
free(&bees);
bees.value1=mirvar(0);
bees.value2=mirvar(0);
bees.next=NULL;
}
crt_init((number_of_factors-1),moduli_base);
crt(x_base,result);
crt_end();
printf("Thus, the exponent is "); otnum(result,stdout);
}
void find_elem_list(int elem_num,int maximum, pair *factors, bigbase, big exponent)
{
int i;
pair *ptemp;
ptemp=factors;
if (elem_num>maximum)
{
printf ("This is not a possibleelement\n");
exit(0);
}
i=0;
while (1)
{
if (i==elem_num)
{
copy(ptemp->value1,exponent);
copy(ptemp->value2,base);
return;
}
ptemp=ptemp->next;
i++;
}
}
void shanks(big alpha, big beta, big p, big pi, big ret)
{
int comp;
big j,i,dummy;
big t;
bigm; /* Variables Declaration */
big pminus;
big ein; /* Internal Joke */
pair list1,list2;
t =mirvar(0); /* Variables initialization */
list1.value1 = mirvar(0);
list1.value2 = mirvar(0);
list2.value1 = mirvar(0);
list2.value2 = mirvar(0);
list1.next = NULL;
list2.next = NULL;
m = mirvar(0);
j = mirvar(0);
ein = mirvar(1);
pminus = mirvar(0);
dummy = mirvar(0);
decr(p,1,pminus);
nroot(pi,2,m);
incr(m,1,m);
comp = (-1);
while ( comp == (-1) ) {
comp = compare(j,m);
multiply(m,j,t);
powmod(alpha,t,p,t);
insert(&list1,j,t);
incr(j,1,j);
}
i = mirvar(0);
comp = (-1);
while ( comp == (-1) ) {
comp = compare(i,m);
powmod(alpha,i,p,t);
xgcd(t,p,t,t,t);
powmod2(t,ein,beta,ein,p,t);
if (search (&list1, t, m,j)==1)
{
mad(m,j,i,pminus,dummy,t);
copy(t,ret);
return;
}
incr(i,1,i);
}
printf("Not Found Error!\n");
}
int insert(pair *plist, big el1, big el2)
{
pair *ptemp = NULL;
pair *pnew = NULL;
pair *Zprevious = NULL;
pair *pchange = NULL;
if ( (pnew = (pair *)malloc(sizeof(pair))) == NULL ) {
printf("Error - out of memory.\n");
exit(1);
}
else {
pnew->value1 = mirvar(0);
pnew->value2 = mirvar(0);
copy(el1,pnew->value1);
copy(el2,pnew->value2);
pnew->next = NULL;
}
if (plist->next==NULL) {
plist->next = pnew;
}
else {
Zprevious = plist;
ptemp = plist;
while (ptemp->next != NULL) {
ptemp=ptemp->next;
if ( (-1) ==compare(pnew->value2,ptemp->value2) ) {
pchange =ptemp;
Zprevious->next = pnew;
pnew->next =pchange;
return(0);
}
Zprevious=ptemp;
}
ptemp->next = pnew;
}
return(1);
}
int search(pair *list2,big el, big m, big ret)
{
big top,bottom,middle;
pair *temp2;
big i,neg1;
big temp;
top = mirvar(0);
bottom = mirvar(0);
middle = mirvar(0);
neg1 = mirvar(-1);
temp=mirvar(0);
i=mirvar(0);
decr(m,1,top);
while( compare(top,bottom) != (-1) )
{
add(top,bottom,temp);
subdiv(temp,2,middle);
for (i=mirvar(-1), temp2 =list2->next; (compare(i,middle) == (-1)) ;
temp2 = temp2->next,incr(i,1,i));
if ( compare(el,temp2->value2) ==0 ) {
copy(temp2->value1,ret);
return(1);
}
else if (compare(el,temp2->value2) == 1 )
incr(middle,1,bottom);
else
decr(middle,1,top);
}
return(0);
}
老哥,你这代码好多错误。这儿是啥情况
好像有很多问题,我正在修改,打扰了。
你这个基本都是这个big搞错了,我觉得可以改的给你改了。上面发的那张图上的我没动,怕给你搞错了