⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eigamal.cpp

📁 椭圆曲线加密
💻 CPP
📖 第 1 页 / 共 2 页
字号:
 

int main()
{
    mip =mirsys(100,0);
    unsigned char mod_str;
    int i,j;
    int pw[NPRIMES];    
    big pf[NPRIMES],logmod[NPRIMES];
    for (i=0;i<NPRIMES;i++) 
    {
        pf[i]=mirvar(0);
        logmod[i]=mirvar(0);
    }    
    
    big_chinese bc;    
    
    big k=mirvar(0);
    big p=mirvar(0);
    big a=mirvar(0);
    big b=mirvar(0);
    big p1=mirvar(0);        
    big xA=mirvar(0);
    big xB=mirvar(0);

    epoint* A = epoint_init();     
    epoint* At = epoint_init(); 
    epoint* B = epoint_init(); 
    epoint* Q= epoint_init(); 
    epoint* R= epoint_init(); 
    epoint* Rj= epoint_init(); 

    big c=mirvar(0);    
    big N=mirvar(0);
    order=mirvar(0);
    rholim=mirvar(2);
    irand(41563436);    
   
    mip->IOBASE=16; 
    // --------------------- Elliptic Curve Definition --------------------- 
    printf("In progress...\n");
                            
    cinstr(a,"1");   
    cinstr(b,"0");    
    cinstr(p,"ACC00CF0775153B19E037CE879D332BB");    
      
    cinstr(xA,"18650A0922FC3EC0B778347B20EE6619"); 
    cinstr(xB,"0D85FA1C5BC8982485ACD9FA9B1BEBEC");        
   

    ecurve_init(a,b,p,MR_AFFINE);
    epoint_set(xA,xA,0,A);
    epoint_set(xB,xB,1,B);


    // --------------------- Order factors ---------------------     
    mip->IOBASE=16;
    cinstr(p1,"566006783BA8A9D8CF01BE743CE9995E");                        
    cinstr(pf[0],"2"); pw[0]=1;
    cinstr(pf[1],"3"); pw[1]=1;
    cinstr(pf[2],"7"); pw[2]=1;
    cinstr(pf[3],"D"); pw[3]=2;        
    cinstr(pf[4],"7F"); pw[4]=1;
    cinstr(pf[5],"D3"); pw[5]=1;
    cinstr(pf[6],"1DF75"); pw[6]=1;
    cinstr(pf[7],"5978F"); pw[7]=1;
    cinstr(pf[8],"1F374C47"); pw[8]=1;    
    cinstr(pf[9],"5F73FD8D3"); pw[9]=1;   
                    
    
    // --------------------- Pohlig Hellman --------------------- 
    // If ord(A) = p1^e1 * p2^e2 * ... * pn^en there is a group 
    // isomorphism such:   f : <A> -> Cp1^e1 * ... * Cpn^en 
    // where Cpi^ei are subgroup of <A> of order pi^ei.
    //
    //  The projection of f to Cpi^ei is given by:
    //          fi : <A> -> Fpi^ei
    //               R -> (N/pi^ei)*R
    //   Moreover fi is a group homomorphism so because of linearity
    //   ("lin閍rit? en francais mais j'ai quelques doutes sur son
    //   equivalent en anglais):  B = k*A so fi(B) = fi(k*A) = k*fi(A)
    //   but we are in Cpi^ei so k is determined modulo (pi^ei). 
    //
    //   Using CRT we will find p mod (p1^e1* ... * pn^en)...
    //   If you are really interested in PH algo you sould read more :)

    for (i=0;i<NPRIMES;i++)
    {         
        // ui = 0
        zero(logmod[i]); 
        // Q = (n/pi)*A                      
        copy(p1,N);       
        divide(N,pf[i],N);
        ecurve_mult(N,A,Q);
        // R(0) = B
        epoint_copy(B,Rj);
                        
        // For j=0 to (e-1)
        for (j=0;j<pw[i];j++)
        {                 
                // R = (n/pi^(j+1))*Rj              
                ecurve_mult(N,Rj,R);
                // Solve R = kj*Q   
                copy(pf[i],order);                                                                         
                rho(Q,R,k); 
                // c = kj*pi^j       
                powltr(j,pf[i],p1,c);
                power(pf[i],j,p1,c);                               
                multiply(k,c,c);
                // Rj+1 = Rj - kj*pi^j*A
                ecurve_mult(c,A,At);
                ecurve_sub(At,Rj);                
                // ui = ui + kj*pi^j           
                add(logmod[i],c,logmod[i]);
                // N = (n/pi^(j+2))                
                divide(N,pf[i],N);                           
        }
        power(pf[i],pw[i],p1,pf[i]); 
        // Interface
        cotstr(pf[i],mod_str);               
        printf("# Solved over C(%s)\n#> ",mod_str); 
        cotnum(logmod[i],stdout);       
    }                
    
    // ---------  Chinese remainder thereom ---------        
    crt_init(&bc,NPRIMES,pf);
    crt(&bc,logmod,k);   
    crt_end(&bc);

    // -------------------- User-friendly Interface :) -------------------- 
          
    ecurve_mult(k,A,Q);            
    if (epoint_comp(Q,B))
    {
        printf("\nDiscrete logarithm: "); 
        cotnum(k,stdout); 
    }
    else
    { 
        printf("Wrong solution !? :x");
    }                
    getch();


    // -----------------------------------------------------------------
    epoint_free(A);
    epoint_free(At);     
    epoint_free(B); 
    epoint_free(Q);
    epoint_free(R); 
    epoint_free(Rj);
    
    for (i=0;i<NPRIMES;i++) 
    {
        mirkill(pf[i]);
        mirkill(logmod[i]);
    }      
    mirkill(k);
    mirkill(p);
    mirkill(a);
    mirkill(b);
    mirkill(p1);        
    mirkill(xA);
    mirkill(xB);
    mirkill(c);
    mirkill(N);
    mirexit(); 
    return(0);  
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -