关于C代码和Pollard的rho算法的对数问题

时间:2014-05-13 07:11:40

标签: c discrete-mathematics logarithm

此代码由C编写,来自pollard的rho算法,用于对数(来自wiki)。在此代码中,如果我将alpha = 2,beta = 5,N = 1019,则必须返回a = 681,b = 378,A = 301,B = 426和X = 1019。但我跑了,我只得到X = 1019,我得到(a,b,A,B)=(672,367,445,706)。你知道问题是什么吗?

wiki site

#include<stdio.h>
#include<math.h>

int alpha, beta, N;

void xab(int *x, int *a, int *b)
{
    switch(*x%3){
    case 0: *x=((*x)*(*x))%N;   *a=((*a)*2)%N;  *b=((*b)*2)%N;  break;
    case 1: *x=(alpha*(*x))%N;  *a=((*a)+1)%N;  break;
    case 2: *x=(beta*(*x))%N;   *b=((*b)+1)%N;  break;
    } 
}  

int main(void)
{    
    int x=1;    int a=0;    int b=0;
    int X=1;    int A=0;    int B=0;
    int i;

    scanf("%d %d %d", &alpha, &beta, &N);    

for(i=1;i<N;i++){
        printf("Code #%d\n", i);
        xab(&x,&a,&b);
        printf("x=%d a=%d b=%d\n", x, a, b);    
        xab(&X,&A,&B);  xab(&X,&A,&B);
        printf("X=%d A=%d B=%d\n\n", X, A, B);
        if(x==X) break;
    }
    return 0;
}

1 个答案:

答案 0 :(得分:0)

您没有正确复制new_xab功能。请注意,new_xab函数同时使用Nn N=1019n=1018。作为参考,这里有来自维基百科文章的三行

case 0: x = x*x     % N;  a =  a*2  % n;  b =  b*2  % n;  break;
case 1: x = x*alpha % N;  a = (a+1) % n;                  break;
case 2: x = x*beta  % N;                  b = (b+1) % n;  break;