Example #1
0
int main()
{
	big p,q,dp,dq,m,c,m1;

	int i,romptr;
    miracl instance;                           /* sizeof(miracl)= 2124 bytes from the stack */
#ifndef MR_STATIC
	miracl *mr_mip=mirsys(&instance,WORDS*8,16);
	char *mem=memalloc(_MIPP_ ,7);   
#else
    miracl *mr_mip=mirsys(&instance,MR_STATIC*8,16); /* size of bigs is fixed */
    char mem[MR_BIG_RESERVE(7)];               /* reserve space on the stack for 7 bigs */
    memset(mem,0,MR_BIG_RESERVE(7));           /* clear this memory */

#endif

/* Initialise bigs */   

    p=mirvar_mem(_MIPP_ mem,0);
	q=mirvar_mem(_MIPP_ mem,1);
    dp=mirvar_mem(_MIPP_ mem,2);
	dq=mirvar_mem(_MIPP_ mem,3);
	m=mirvar_mem(_MIPP_ mem,4);
	c=mirvar_mem(_MIPP_ mem,5);
    m1=mirvar_mem(_MIPP_ mem,6);
  
    romptr=0;

    init_big_from_rom(p,WORDS,rom,256,&romptr);
    init_big_from_rom(q,WORDS,rom,256,&romptr);
    init_big_from_rom(dp,WORDS,rom,256,&romptr);
    init_big_from_rom(dq,WORDS,rom,256,&romptr);

    bigbits(_MIPP_ 512,c);

/* count clocks, instructions and CPI from here.. */
//for (i=0;i<100000;i++)
//{
    powmod(_MIPP_ c,dp,p,m);
    powmod(_MIPP_ c,dq,q,m1);
//}
/* to here... */

#ifndef MR_NO_STANDARD_IO
otnum(_MIPP_ m,stdout);
otnum(_MIPP_ m1,stdout);
#endif

#ifndef MR_STATIC
    memkill(_MIPP_ mem,7);
#else
    memset(mem,0,MR_BIG_RESERVE(6));        /* clear this stack memory */
#endif

    mirexit(_MIPPO_ );  /* clears workspace memory */
    return 0;
}
Example #2
0
/* SM2 self check */
int SM2_standard_selfcheck()
{
	//the private key
	unsigned char dA[32] = {0x39, 0x45, 0x20, 0x8f, 0x7b, 0x21, 0x44, 0xb1, 0x3f, 0x36, 0xe3, 0x8a, 0xc6, 0xd3, 0x9f,
							0x95, 0x88, 0x93, 0x93, 0x69, 0x28, 0x60, 0xb5, 0x1a, 0x42, 0xfb, 0x81, 0xef, 0x4d, 0xf7, 
							0xc5, 0xb8};
	unsigned char rand[32] = {0x59, 0x27, 0x6E, 0x27, 0xD5, 0x06, 0x86, 0x1A, 0x16, 0x68, 0x0F, 0x3A, 0xD9, 0xC0, 0x2D,
							  0xCC, 0xEF, 0x3C, 0xC1, 0xFA, 0x3C, 0xDB, 0xE4, 0xCE, 0x6D, 0x54, 0xB8, 0x0D, 0xEA, 0xC1, 
							  0xBC, 0x21};
	//the public key
	/* unsigned char xA[32] = {0x09, 0xf9, 0xdf, 0x31, 0x1e, 0x54, 0x21, 0xa1, 0x50, 0xdd, 0x7d, 0x16, 0x1e, 0x4b, 0xc5, 
							   0xc6, 0x72, 0x17, 0x9f, 0xad, 0x18, 0x33, 0xfc, 0x07, 0x6b, 0xb0, 0x8f, 0xf3, 0x56, 0xf3,
							   0x50, 0x20};
	unsigned char yA[32] = {0xcc, 0xea, 0x49, 0x0c, 0xe2, 0x67, 0x75, 0xa5, 0x2d, 0xc6, 0xea, 0x71, 0x8c, 0xc1, 0xaa,
							0x60, 0x0a, 0xed, 0x05, 0xfb, 0xf3, 0x5e, 0x08, 0x4a, 0x66, 0x32, 0xf6, 0x07, 0x2d, 0xa9, 
							0xad, 0x13};*/

	unsigned char xA[32], yA[32];
	unsigned char r[32], s[32];		// Signature

	unsigned char IDA[16] = {0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x31, 0x32, 0x33,
						 	 0x34, 0x35, 0x36, 0x37, 0x38};		//ASCII code of userA's identification
	int IDA_len = 16;
	unsigned char ENTLA[2] = {0x00, 0x80};		//the length of userA's identification, presentation in ASCII code

	unsigned char *message = "message digest";	//the message to be signed
	int len = strlen(message);		//the length of message
	unsigned char ZA[SM3_len / 8];		//ZA = Hash(ENTLA || IDA || a || b || Gx || Gy || xA|| yA)
	unsigned char Msg[210];		//210 = IDA_len + 2 + SM2_NUMWORD * 6
	
	int temp;

	mip = mirsys(10000, 16);
	mip->IOBASE = 16;

	temp = SM2_standard_sign_keygeneration(dA, xA, yA);
	if (temp)
		return temp;
	
	//ENTLA || IDA || a || b || Gx || Gy || xA || yA
	memcpy(Msg, ENTLA, 2);
	memcpy(Msg + 2, IDA, IDA_len);
	memcpy(Msg + 2 + IDA_len, SM2_a, SM2_NUMWORD);
	memcpy(Msg + 2 + IDA_len + SM2_NUMWORD, SM2_b, SM2_NUMWORD);
	memcpy(Msg + 2 + IDA_len + SM2_NUMWORD * 2, SM2_Gx, SM2_NUMWORD);
	memcpy(Msg + 2 + IDA_len + SM2_NUMWORD * 3, SM2_Gy, SM2_NUMWORD);
	memcpy(Msg + 2 + IDA_len + SM2_NUMWORD * 4, xA, SM2_NUMWORD);
	memcpy(Msg + 2 + IDA_len + SM2_NUMWORD * 5, yA, SM2_NUMWORD);
	SM3_256(Msg, 210, ZA);
	
	temp = SM2_standard_sign(message, len, ZA, rand, dA, r, s);
	if (temp)
		return temp;
	
	temp = SM2_standard_verify(message, len, ZA, xA, yA, r, s);
	if (temp)
		return temp;

	return 0;
}
Example #3
0
int main(int argc, char* argv[]){
	struct global_s global;
	int retcode;
	miracl *mr_mip;
	int retsock=0;

	mr_mip=mirsys(100, 0);




	global_init((char *)&global);
	
	//parse_cmd_lines(_MIPP_ argc, argv, (char *)&global);
	retcode=parse_cmd_lines(argc, argv, (char *)&global);
	if (retcode==-1){
		printf("argv error\n");
		return -1;
	};


	

	//retcode=main_skypeclient_tcpconnect(_MIPP_ (char *)&global );
	retcode=main_skypeclient_tcpconnect( (char *)&global );

	if (retcode==0){
		if(DEBUG_LEVEL>=1) printf("message delivered\n");
	};

	mirexit();

	return 0;
};
Example #4
0
int main()
{
    FILE *fp;
    int ep,m,a,b,c;
    miracl *mip;
    epoint *g,*public;
    char ifname[50],ofname[50];
    big a2,a6,q,x,y,v,u1,u2,r,s,hash;
/* get public data */
    fp=fopen("common2.ecs","r");
    if (fp==NULL)
    {
        printf("file common2.ecs does not exist\n");
        return 0;
    }
    fscanf(fp,"%d\n",&m);

    mip=mirsys(3+abs(m)/MIRACL,0);
    a2=mirvar(0);
    a6=mirvar(0);
    q=mirvar(0);
    x=mirvar(0);
    y=mirvar(0);
    v=mirvar(0);
    u1=mirvar(0);
    u2=mirvar(0);
    s=mirvar(0);
    r=mirvar(0);
    hash=mirvar(0);

    mip->IOBASE=16;
    cinnum(a2,fp);
    cinnum(a6,fp);
    cinnum(q,fp);
    cinnum(x,fp);
    cinnum(y,fp);
    mip->IOBASE=10;

    fscanf(fp,"%d\n",&a);
    fscanf(fp,"%d\n",&b);
    fscanf(fp,"%d\n",&c);

    fclose(fp);

    ecurve2_init(m,a,b,c,a2,a6,FALSE,MR_PROJECTIVE);  /* initialise curve */
    g=epoint2_init();
    epoint2_set(x,y,0,g); /* initialise point of order q */

/* get public key of signer */
    fp=fopen("public.ecs","r");
    if (fp==NULL)
    {
        printf("file public.ecs does not exist\n");
        return 0;
    }
    fscanf(fp,"%d",&ep);
    cinnum(x,fp);
    fclose(fp);

    public=epoint2_init();
Example #5
0
int main ()
{  /*  hailstone numbers  */

    int iter,r;
    big x,y,mx;
    mirsys(400,10);
    x=mirvar(0);
    y=mirvar(0);
    mx=mirvar(0);
    iter=0;
    printf("number = \n");
    innum(x,stdin);
    do
    { /* main loop */
        if (compare(x,mx)>0) copy(x,mx);
        r=subdiv(x,2,y);
        if (r!=0)
        { /* what goes up ... */
            premult(x,3,x);
            incr(x,1,x);
        }
        /* ... must come down */
        else copy(y,x);
        otnum(x,stdout);
        iter++;
    } while (size(x)!=1);
    printf("path length = %d \n",iter);
    printf("maximum = \n");
    otnum(mx,stdout);
    return 0;
}
Example #6
0
int main()
{ /* solve set of linear equations */
    int i,j,n;
    miracl *mip=mirsys(20,MAXBASE);
    do
    {
        printf("Order of Hilbert matrix H= ");
        scanf("%d",&n);
        getchar();
    } while (n<2 || n>49);
    for (i=0;i<n;i++)
    {
        AA[i][n]=mirvar(0);
        bb[i]=mirvar(1);
        for (j=0;j<n;j++)
        {
            AA[i][j]=mirvar(0);
            fconv(1,i+j+1,AA[i][j]);
        }
    }
    
    if (gauss(AA,bb,n))
    {
        printf("\nSolution is\n");
        for (i=0;i<n;i++)
        {
            printf("x[%d] = ",i+1);
            cotnum(bb[i],stdout);
        }
        if (mip->EXACT) printf("Result is exact!\n");
    }
    else printf("H is singular!\n");
    return 0;
}
/*! \brief Generate a random six digit one time password
 *
 *  Generates a random six digit one time password
 *
 *  @param  seedValue       random seed value
 *  @param  seedValueLength length of seedValue in bytes
 *  @return OTP             One Time Password
 */
AESGCM_EXPORT int generateOTP(char* seedValue, int seedValueLength)
{
    int OTP=0;
    miracl *mip=mirsys(100,0);
    big bigOTP, modValue;
    bigOTP=mirvar(0);
    modValue=mirvar(1000000);

    /* Crypto string RNG */
    csprng RNG;
    RNG = generateRNG(seedValue, seedValueLength);

    /* Generate randam 128 bit value */
    int i;
    char val[16];
    for (i=0; i<16; i++)
        val[i]=strong_rng(&RNG);

    /* Get the modulus */
    bytes_to_big(16,val, bigOTP);
    divide(_MIPP_ bigOTP,modValue,modValue);
    OTP = size(bigOTP);

    return OTP;
}
Example #8
0
PFC::PFC(int s)
{
	int mod_bits,words;
	if (s!=80)
	{
		cout << "No suitable curve available" << endl;
		exit(0);
	}
	mod_bits=2*s;

	if (mod_bits%MIRACL==0)
		words=(mod_bits/MIRACL);
	else
		words=(mod_bits/MIRACL)+1;

#ifdef MR_SIMPLE_BASE
	miracl *mip=mirsys((MIRACL/4)*words,16);
#else
	miracl *mip=mirsys(words,0); 
	mip->IOBASE=16;
#endif

	B=new Big;
	x=new Big;
	mod=new Big;
	ord=new Big;
	cof=new Big;
	npoints=new Big;
	trace=new Big;
	frob=new ZZn2;

	*B=curveB;
	S=s;
	*x=param;
	Big X=*x;

	*mod=X*X+1;
	*npoints=X*X-X+1;
	*trace=X+1;
	*cof=X*X+X+1;
	*ord=*npoints;
	ecurve(-3,*B,*mod,MR_PROJECTIVE);
	set_frobenius_constant(*frob);
	Big sru=pow((ZZn)-2,(*mod-1)/6);   // x^6+2 is irreducible
    set_zzn3(-2,sru);
	mip->TWIST=MR_QUADRATIC;   // twisted curve E'(ZZn3)
}
Example #9
0
int main()
{ /* program to find a trap-door prime */
    BOOL found;
    int i,spins;
    long seed;
    big pp[NPRIMES],q,p,t;
    FILE *fp;
    mirsys(50,0);
    for (i=0;i<NPRIMES;i++) pp[i]=mirvar(0);
    q=mirvar(0);
    t=mirvar(0);
    p=mirvar(0);
    printf("Enter 9 digit seed= ");
    scanf("%ld",&seed);
    getchar();
    irand(seed);
    printf("Enter 4 digit seed= ");
    scanf("%d",&spins);
    getchar();
    for (i=0;i<spins;i++) brand();
    convert(2,pp[0]);
    do
    {  /* find prime p = 2.pp[1].pp[2]....+1 */
        convert(2,p);
        for (i=1;i<NPRIMES-1;i++)
        { /* generate all but last prime */
            bigdig(i+6,10,q);
            nxprime(q,pp[i]);
            multiply(p,pp[i],p);
        }
        do
        { /* find last prime component such that p is prime */
            nxprime(q,q);
            copy(q,pp[NPRIMES-1]);
            multiply(p,pp[NPRIMES-1],t);
            incr(t,1,t);
        } while(!isprime(t));
        copy(t,p);
        found=TRUE;
        for (i=0;i<NPRIMES;i++)
        { /* check that PROOT is a primitive root */
            decr(p,1,q);
            divide(q,pp[i],q);
            powltr(PROOT,q,p,t);
            if (size(t)==1) 
            {
                found=FALSE;
                break;
            }
        }
    } while (!found);
    fp=fopen("prime.dat","wt");
    fprintf(fp,"%d\n",NPRIMES);
    for (i=0;i<NPRIMES;i++) cotnum(pp[i],fp);
    fclose(fp);
    printf("prime= \n");
    cotnum(p,stdout);
    return 0;
}
Example #10
0
int main()
{
    FILE *fp;
    big e,n,a,b,x,y,r;
    epoint *g;
    ebrick binst;
    int i,d,ndig,nb,best,time,store,base,bits;
    miracl *mip=mirsys(50,0);
    n=mirvar(0);
    e=mirvar(0);
    a=mirvar(0);
    b=mirvar(0);
    x=mirvar(0);
    y=mirvar(0);
    r=mirvar(0);

    fp=fopen("common.ecs","r");
    fscanf(fp,"%d\n",&bits);

    mip->IOBASE=16;
    cinnum(n,fp);
    cinnum(a,fp);
    cinnum(b,fp);
    cinnum(r,fp);
    cinnum(x,fp);
    cinnum(y,fp);
    mip->IOBASE=10;

    printf("modulus is %d bits in length\n",logb2(n));
    printf("Enter size of exponent in bits = ");
    scanf("%d",&nb);
    getchar();

    ebrick_init(&binst,x,y,a,b,n,nb);

    printf("%d big numbers have been precomputed and stored\n",binst.store);

    bigdig(nb,2,e);  /* random exponent */  

    printf("naive method\n");
    ecurve_init(a,b,n,MR_PROJECTIVE);
    g=epoint_init();
    epoint_set(x,y,0,g);
    ecurve_mult(e,g,g);
    epoint_get(g,x,y);
    cotnum(x,stdout);
    cotnum(y,stdout);

    printf("Brickel et al method\n");
    mul_brick(&binst,e,x,y);

    ebrick_end(&binst);
    
    cotnum(x,stdout);
    cotnum(y,stdout);

    return 0;
}
Example #11
0
PFC::PFC(int s, csprng *rng)
{
	int mod_bits,words;
	if (s!=80 && s!=128)
	{
		cout << "No suitable curve available" << endl;
		exit(0);
	}
	if (s==80)  mod_bits=512;
	if (s==128) mod_bits=1536;

	if (mod_bits%MIRACL==0)
		words=(mod_bits/MIRACL);
	else
		words=(mod_bits/MIRACL)+1;

#ifdef MR_SIMPLE_BASE
	miracl *mip=mirsys((MIRACL/4)*words,16);
#else
	miracl *mip=mirsys(words,0); 
	mip->IOBASE=16;
#endif
	mod=new Big;
	cof=new Big;
	ord=new Big;

	Big A=-3;
	Big B=0;
	if (s==80)
	{
		*cof=param_80;
		*ord=pow((Big)2,159)+pow((Big)2,17)+1;
	}
	if (s==128)
	{
		*cof=param_128;
		*ord=pow((Big)2,255)+pow((Big)2,41)+1;
	}

	S=s;
	*mod=2*(*cof)*(*ord)-1;
	ecurve(A,B,*mod,MR_PROJECTIVE);

	RNG=rng;
}
Example #12
0
PFC::PFC(int s, csprng *rng)
{
	int mod_bits,words;

	if (s!=80)
	{
		cout << "No suitable curve available" << endl;
		exit(0);
	}

	mod_bits=512;

	if (mod_bits%MIRACL==0)
		words=(mod_bits/MIRACL);
	else
		words=(mod_bits/MIRACL)+1;

#ifdef MR_SIMPLE_BASE
	miracl *mip=mirsys((MIRACL/4)*words,16);
#else
	miracl *mip=mirsys(words,0); 
	mip->IOBASE=16;
#endif

	B=new Big;
	mod=new Big;
	ord=new Big;
	cof=new Big;
	npoints=new Big;
	trace=new Big;

	*B=Btext;

	*cof=COFtext;
	*ord=pow((Big)2,159)+pow((Big)2,17)+1;
	*npoints=*cof*(*ord);

	S=s;
	*mod=MODtext;
	*trace=*mod+1-*npoints;

	ecurve(-3,*B,*mod,MR_PROJECTIVE);

	RNG=rng;
}
Example #13
0
PFC::PFC(int s)
{
	int t,u,v,b,words,mod_bits;

	if (s!=80 && s!=128)
	{
		cout << "No suitable curve available" << endl;
		exit(0);
	}

	if (s==80)  mod_bits=379;
	if (s==128) mod_bits=1223;

	words=(mod_bits/MIRACL)+1;

#ifdef MR_SIMPLE_BASE
	miracl *mip=mirsys((MIRACL/4)*words,16);
#else
	miracl *mip=mirsys(words,0); 
	mip->IOBASE=16;
#endif

	ord=new Big;

	S=s;
	M=mod_bits;
	if (s==80)
	{
		t=253; u=251; v=59; B=1; CF=1; 
		*ord=pow((Big)2,M)+pow((Big)2,(M+1)/2)+1; //TYPE=1
	}
	if (s==128)
	{
		t=255; u=0; v=0; B=0; CF=5;
		*ord=pow((Big)2,M)+pow((Big)2,(M+1)/2)+1; //TYPE=1
	}
	*ord/=CF;
	
#ifdef MR_AFFINE_ONLY
	ecurve2(-M,t,u,v,(Big)1,(Big)B,TRUE,MR_AFFINE);
#else
	ecurve2(-M,t,u,v,(Big)1,(Big)B,TRUE,MR_PROJECTIVE);
#endif
}
Example #14
0
void envirment_init() {
    big a, b, p, x, y;

#if MIRACL==16
    #ifdef MR_FLASH
        miracl *mip = mirsys(500,10);    /* initialise system to base 10, 500 digits per "big" */
    #else
        miracl *mip = mirsys(5000,10);   /* bigger numbers possible if no flash arithmetic     */
    #endif
#else
    miracl *mip = mirsys(5000,10);  /* 5000 digits per "big" */
#endif
    // init
    a = mirvar(-3);
    b = mirvar(0);
    ECC_N = mirvar(0);
    p = mirvar(0);
    x = mirvar(0);
    y = mirvar(0);
    ECC_G = epoint_init();
    ECC_H = epoint_init();

    mip->IOBASE = 10;
    // init curve
    cinstr(b, bChar);
    cinstr(ECC_N, nChar);
    cinstr(p, pChar);
    ecurve_init(a, b, p, MR_PROJECTIVE);

    // init point:  G, H
    cinstr(x, gxChar);
    cinstr(y, gyChar);
    epoint_set(x, y, 0, ECC_G);
    cinstr(x, hxChar);
    cinstr(y, hyChar);
    epoint_set(x, y, 0, ECC_H);
    mip->IOBASE = 16;
    mirkill(a);
    mirkill(b);
    mirkill(p);
    mirkill(x);
    mirkill(y);
}
Example #15
0
int main()
{
    FILE *fp;
    big p,q,g,x,y;
    long seed;
    int bits;
    miracl *mip;
/* get common data */
    fp=fopen("common.dss","rt");
    if (fp==NULL)
    {
        printf("file common.dss does not exist\n");
        return 0;
    }
    fscanf(fp,"%d\n",&bits);

    mip=mirsys(bits/4,16);    /* use Hex internally */
    p=mirvar(0);
    q=mirvar(0);
    g=mirvar(0);
    x=mirvar(0);
    y=mirvar(0);

    innum(p,fp);
    innum(q,fp);
    innum(g,fp);
    fclose(fp);

/* randomise */
    printf("Enter 9 digit random number seed  = ");
    scanf("%ld",&seed);
    getchar();
    irand(seed);

    powmod(g,q,p,y);
    if (size(y)!=1)
    {
        printf("Problem - generator g is not of order q\n");
        return 0;
    }

/* generate public/private keys */
    bigrand(q,x);
    powmod(g,x,p,y);
    printf("public key = ");
    otnum(y,stdout);
    fp=fopen("public.dss","wt");
    otnum(y,fp);
    fclose(fp);
    fp=fopen("private.dss","wt");
    otnum(x,fp);
    fclose(fp);
    mirexit();
    return 0;
}
Example #16
0
int main()
{
    FILE *fp; 
    big e,n,g,a;
    brick binst;
    int window,nb,bits;
    miracl *mip=mirsys(100,0);
    n=mirvar(0);
    e=mirvar(0);
    a=mirvar(0);
    g=mirvar(0);
    fp=fopen("common.dss","rt");
    fscanf(fp,"%d\n",&bits);
    mip->IOBASE=16;
    cinnum(n,fp);
    cinnum(g,fp);
    cinnum(g,fp);  
    mip->IOBASE=10;  

    printf("modulus is %d bits in length\n",logb2(n));
    printf("Enter size of exponent in bits = ");
    scanf("%d",&nb);
    getchar();
    printf("Enter window size in bits (1-10)= ");
    scanf("%d",&window);
    getchar();

    if (!brick_init(&binst,g,n,window,nb))
    {
        printf("Failed to initialize\n");
        return 0;
    }

    printf("%d big numbers have been precomputed and stored\n",(1<<window));

    bigbits(nb,e);  /* random exponent */  

    printf("naive method\n");
    powmod(g,e,n,a);
    cotnum(a,stdout);

    printf("Comb method\n");
    pow_brick(&binst,e,a);

    brick_end(&binst);
    
    cotnum(a,stdout);

    return 0;
}
Example #17
0
File: fact.c Project: asgene/sm2
int main()
{ /* calculate factorial of number */
    big nf;           /* declare "big" variable nf */
    int n;
#if MIRACL==16
#ifdef MR_FLASH
    mirsys(500,10);    /* initialise system to base 10, 500 digits per "big" */
#else
    mirsys(5000,10);   /* bigger numbers possible if no flash arithmetic     */
#endif
#else
    mirsys(5000,10);  /* 5000 digits per "big" */
#endif
    nf=mirvar(1);     /* initialise "big" variable nf=1 */
    printf("factorial program\n");
    printf("input number n= \n");
    scanf("%d",&n);
    getchar();
    while (n>1) premult(nf,n--,nf);   /* nf=n!=n*(n-1)*(n-2)*....3*2*1  */
    printf("n!= \n");
    otnum(nf,stdout); /* output result */ 
    return 0;
}
Example #18
0
BOOL DH1080_Init()
{
	initb64();
    mip=mirsys(256, 0);
	if(mip==NULL) return FALSE;

	b_prime1080=mirvar(0);
	b_1=mirvar(0);

	bytes_to_big(DH1080_PRIME_BYTES, prime1080, b_prime1080);

	lgconv(1, b_1);

	return TRUE;
}
Example #19
0
int main()
{ /* Brents example program */
    flash x,pi;
    miracl *mip=mirsys(-35,0);
    x=mirvar(0);
    pi=mirvar(0);
    mip->RPOINT=ON;
    printf("Calculating pi..\n");
    fpi(pi);
    cotnum(pi,stdout); /* output pi */
    printf("Calculating exp(pi*(163/9)^0.5)\n");
    fconv(163,9,x);
    froot(x,2,x);
    fmul(x,pi,x);
    fexp(x,x);
    cotnum(x,stdout);
    printf("Calculating exp(pi*(163)^0.5)\n");
    fpower(x,3,x);
    cotnum(x,stdout);
    return 0;
}
Example #20
0
int main()
{
    FILE *fp;
    int m,a,b,c;
    big e,a2,a6,x,y,r;
    epoint *g;
    ebrick2 binst;
    int i,d,ndig,nb,best,time,store,base;
    miracl *mip=mirsys(50,0);
    e=mirvar(0);
    a2=mirvar(0);
    a6=mirvar(0);
    x=mirvar(0);
    y=mirvar(0);
    r=mirvar(0);

    fp=fopen("common2.ecs","r");
    fscanf(fp,"%d\n",&m);
    mip->IOBASE=16;
    cinnum(a2,fp);
    cinnum(a6,fp);
    cinnum(r,fp);
    cinnum(x,fp);
    cinnum(y,fp);
    mip->IOBASE=10;

    fscanf(fp,"%d\n",&a);
    fscanf(fp,"%d\n",&b);
    fscanf(fp,"%d\n",&c);
    
    printf("modulus is %d bits in length\n",m);
    printf("Enter size of exponent in bits = ");
    scanf("%d",&nb);
    getchar();

    ebrick2_init(&binst,x,y,a2,a6,m,a,b,c,nb);

    printf("%d big numbers have been precomputed and stored\n",binst.store);

    bigdig(nb,2,e);  /* random exponent */  

    printf("naive method\n");
    ecurve2_init(m,a,b,c,a2,a6,FALSE,MR_PROJECTIVE);
    g=epoint2_init();
    epoint2_set(x,y,0,g);
    ecurve2_mult(e,g,g);
    epoint2_get(g,x,y);
    cotnum(x,stdout);
    cotnum(y,stdout);

    zero(x); zero(y);
    printf("Brickel et al method\n");
    mul2_brick(&binst,e,x,y);

    ebrick2_end(&binst);
    
    cotnum(x,stdout);
    cotnum(y,stdout);

    return 0;
}
Example #21
0
int main()
{ /*  MIRACL rational calculator */
    int i,j,k,p,q,c,hpos;
    BOOL over,help;
    screen();
#if MIRACL==16
    mip=mirsys(10,0);      /*** 16-bit computer ***/
#else
    mip=mirsys(6,0);       /*** 32-bit computer ***/
#endif
    mip->ERCON=TRUE;
    x=mirvar(0);
    for (i=0;i<=top;i++) y[i]=mirvar(0);
    m=mirvar(0);
    t=mirvar(0);
    radeg=mirvar(0);
    loge2=mirvar(0);
    loge10=mirvar(0);
    eps=mirvar(0);
    mip->pi=mirvar(0);
    cinstr(mip->pi,cpi);            /* read in constants */
    fpmul(mip->pi,1,180,radeg);
    cinstr(loge2,clg2);
    cinstr(loge10,clg10);
    cinstr(eps,ceps);
    help=OFF;
    show(TRUE);
    p=6;
    q=0;
    flag=OFF;
    newx=OFF;
    over=FALSE;


    setopts();
    clrall();
    drawit();
    while (!over)
    { /* main loop */
        if (mip->ERNUM)
        {
            aprint(ORDINARY,4+5*p,6+3*q,keys[q][p]);
            p=5,q=0;
        }
        if (width==80 || !help)
        {
            aprint(INVER,4+5*p,6+3*q,keys[q][p]);
            curser(1,24);
            c=gethit();
            aprint(ORDINARY,4+5*p,6+3*q,keys[q][p]);
        }
        else while ((c=gethit())!='H') ;
        result=TRUE;
        if ((k=arrow(c))!=0)
        { /* arrow key hit */
            if (k==1 && q>0) q--;
            if (k==2 && q<5) q++;
            if (k==3 && p<6) p++;
            if (k==4 && p>0) p--;
            continue;
        }
        if (c=='H')
        { /* switch help on/off */
            help=!help;
            for (i=1;i<=24;i++)
            {
                if (width==80) hpos=41;
                else           hpos=1;
                if (help) aprint(HELPCOL,hpos,i,htext[i-1]);
                else lclr(hpos,i);
            }
            if (width==40 && !help) drawit();
            continue;
        }            
        if (c>='A' && c<='F')
        { /* hex only */
            if (!next(c)) putchar(BELL);
            else show(FALSE);
            continue;
        }
        for (j=0;j<6;j++)
            for (i=0;i<7;i++)
                if (c==qkeys[j][i]) p=i,q=j,c=' ';
        if (c==8 || c==127) p=6,q=1,c=' ';       /* aliases */
        if (c==',' || c=='a') p=5,q=5,c=' ';
        if (c=='O' || c==ESC) p=6,q=0,c=' ';
        if (c==13)  p=6,q=5,c=' ';
        if (c=='[' || c=='{') p=3,q=5,c=' ';
        if (c==']' || c=='}') p=4,q=5,c=' ';
        if (c=='d') p=5,q=2,c=' ';
        if (c=='b') p=5,q=3,c=' ';
        if (c=='^') p=3,q=2,c=' ';
        if (c==' ') over=act(p,q);
        else        continue;
        absol(x,t);
        if (fcomp(t,eps)<0) zero(x);
        if (result)
        { /* output result to display */
            cotstr(x,mip->IOBUFF);
            just((char *)mip->IOBUFF);
            if (mip->ERNUM<0)
            { /* convert to radix and try again */
                mip->ERNUM=0;
                mip->RPOINT=ON;
                cotstr(x,mip->IOBUFF);
                putchar(BELL);
                just((char *)mip->IOBUFF);
            }
            clr();
        }
        if (newx)
        { /* update display */
            getstat();
            show(FALSE);
        }
    }
    curser(1,24);
    restore();
    return 0;
}
Example #22
0
int main()
{
    FILE *fp;
    int ep,bits;
    epoint *g,*w;
    big a,b,p,q,x,y,d;
    long seed;
    miracl instance;
    miracl *mip=&instance;
    char mem[MR_BIG_RESERVE(7)];            /* reserve space on the stack for 7 bigs */
    char mem1[MR_ECP_RESERVE(2)];           /* and two elliptic curve points         */
    memset(mem,0,MR_BIG_RESERVE(7));
    memset(mem1,0,MR_ECP_RESERVE(2));
    
#ifndef MR_EDWARDS	
    fp=fopen("common.ecs","rt");
    if (fp==NULL)
    {
        printf("file common.ecs does not exist\n");
        return 0;
    }
    fscanf(fp,"%d\n",&bits); 
#else
    fp=fopen("edwards.ecs","rt");
    if (fp==NULL)
    {
        printf("file edwards.ecs does not exist\n");
        return 0;
    }
    fscanf(fp,"%d\n",&bits); 
#endif

    mirsys(mip,bits/4,16);                 /* Use Hex internally */
    a=mirvar_mem(mip,mem,0);
    b=mirvar_mem(mip,mem,1);
    p=mirvar_mem(mip,mem,2);
    q=mirvar_mem(mip,mem,3);
    x=mirvar_mem(mip,mem,4);
    y=mirvar_mem(mip,mem,5);
    d=mirvar_mem(mip,mem,6);

    innum(mip,p,fp);
    innum(mip,a,fp);
    innum(mip,b,fp);
    innum(mip,q,fp);
    innum(mip,x,fp);
    innum(mip,y,fp);
    
    fclose(fp);

/* randomise */
    printf("Enter 9 digit random number seed  = ");
    scanf("%ld",&seed);
    getchar();
    irand(mip,seed);

    ecurve_init(mip,a,b,p,MR_PROJECTIVE);  /* initialise curve */

    g=epoint_init_mem(mip,mem1,0);
    w=epoint_init_mem(mip,mem1,1);

    if (!epoint_set(mip,x,y,0,g)) /* initialise point of order q */
    {
        printf("Problem - point (x,y) is not on the curve\n");
        exit(0);
    }

    ecurve_mult(mip,q,g,w);
    if (!point_at_infinity(w))
    {
        printf("Problem - point (x,y) is not of order q\n");
        exit(0);
    }

/* generate public/private keys */

    bigrand(mip,q,d);
    ecurve_mult(mip,d,g,g);
    
    ep=epoint_get(mip,g,x,x); /* compress point */

    printf("public key = %d ",ep);
    otnum(mip,x,stdout);

    fp=fopen("public.ecs","wt");
    fprintf(fp,"%d ",ep);
    otnum(mip,x,fp);
    fclose(fp);

    fp=fopen("private.ecs","wt");
    otnum(mip,d,fp);
    fclose(fp);
/* clear all memory used */
    memset(mem,0,MR_BIG_RESERVE(7));
    memset(mem1,0,MR_ECP_RESERVE(2));
 
    return 0;
}
Example #23
0
int main()
{
    FILE *fp;
    char ifname[50],ofname[50];
    big a,b,p,q,x,y,d,r,s,k,hash;
    epoint *g;
    long seed;
    int bits;
    miracl instance;
    miracl *mip=&instance;
    char mem[MR_BIG_RESERVE(11)];            /* reserve space on the stack for 11 bigs */
    char mem1[MR_ECP_RESERVE(1)];            /* and one elliptic curve points         */
    memset(mem,0,MR_BIG_RESERVE(11));
    memset(mem1,0,MR_ECP_RESERVE(1));
 

/* get public data */

#ifndef MR_EDWARDS	
    fp=fopen("common.ecs","rt");
    if (fp==NULL)
    {
        printf("file common.ecs does not exist\n");
        return 0;
    }
    fscanf(fp,"%d\n",&bits); 
#else
    fp=fopen("edwards.ecs","rt");
    if (fp==NULL)
    {
        printf("file edwards.ecs does not exist\n");
        return 0;
    }
    fscanf(fp,"%d\n",&bits); 
#endif


    mirsys(mip,bits/4,16);   /* Use Hex internally */

    a=mirvar_mem(mip,mem,0);
    b=mirvar_mem(mip,mem,1);
    p=mirvar_mem(mip,mem,2);
    q=mirvar_mem(mip,mem,3);
    x=mirvar_mem(mip,mem,4);
    y=mirvar_mem(mip,mem,5);
    d=mirvar_mem(mip,mem,6);
    r=mirvar_mem(mip,mem,7);
    s=mirvar_mem(mip,mem,8);
    k=mirvar_mem(mip,mem,9);
    hash=mirvar_mem(mip,mem,10);

    innum(mip,p,fp);     /* modulus        */
    innum(mip,a,fp);     /* curve parameters */
    innum(mip,b,fp);     
    innum(mip,q,fp);     /* order of (x,y) */
    innum(mip,x,fp);     /* (x,y) point on curve of order q */
    innum(mip,y,fp);
    fclose(fp);

/* randomise */
    printf("Enter 9 digit random number seed  = ");
    scanf("%ld",&seed);
    getchar();
    irand(mip,seed);

    ecurve_init(mip,a,b,p,MR_PROJECTIVE);  /* initialise curve */
    g=epoint_init_mem(mip,mem1,0);
    epoint_set(mip,x,y,0,g); /* initialise point of order q */

/* calculate r - this can be done offline, 
   and hence amortized to almost nothing   */
    bigrand(mip,q,k);
    ecurve_mult(mip,k,g,g);      /* see ebrick.c for method to speed this up */
    epoint_get(mip,g,r,r);
    divide(mip,r,q,q);

/* get private key of signer */
    fp=fopen("private.ecs","rt");
    if (fp==NULL)
    {
        printf("file private.ecs does not exist\n");
        return 0;
    }
    innum(mip,d,fp);
    fclose(fp);

/* calculate message digest */
    printf("file to be signed = ");
    gets(ifname);
    strcpy(ofname,ifname);
    strip(ofname);
    strcat(ofname,".ecs");
    if ((fp=fopen(ifname,"rb"))==NULL)
    {
        printf("Unable to open file %s\n",ifname);
        return 0;
    }

    hashing(mip,fp,hash);
    fclose(fp);
/* calculate s */
    xgcd(mip,k,q,k,k,k);

    mad(mip,d,r,hash,q,q,s);
    mad(mip,s,k,k,q,q,s);
    fp=fopen(ofname,"wt");
    otnum(mip,r,fp);
    otnum(mip,s,fp);
    fclose(fp);

    memset(mem,0,MR_BIG_RESERVE(11));
    memset(mem1,0,MR_ECP_RESERVE(1));
 
    return 0;
}
Example #24
0
int main()
{
    FILE *fp;
    big p,q,h,g,n,s,t;
    long seed;
    miracl *mip=mirsys(100,0);
    p=mirvar(0);
    q=mirvar(0);
    h=mirvar(0);
    g=mirvar(0);
    n=mirvar(0);
    s=mirvar(0);
    t=mirvar(0);

/* randomise */
    printf("Enter 9 digit random number seed  = ");
    scanf("%ld",&seed);
    getchar();
    irand(seed);

/* generate q */
    forever 
    {
        bigbits(QBITS,q);
        nxprime(q,q);
        if (logb2(q)>QBITS) continue;
        break;
    }
    printf("q= ");
    cotnum(q,stdout);

/* generate p */
    expb2(PBITS,t);
    decr(t,1,t);
    premult(q,2,n);
    divide(t,n,t);
    expb2(PBITS-1,s);
    decr(s,1,s);
    divide(s,n,s);
    forever 
    {
        bigrand(t,p);
        if (mr_compare(p,s)<=0) continue;
        premult(p,2,p);
        multiply(p,q,p);
        incr(p,1,p);
        copy(p,n);
        if (isprime(p)) break;
    } 
    printf("p= ");
    cotnum(p,stdout);

/* generate g */
    do {
        decr(p,1,t);
        bigrand(t,h);
        divide(t,q,t);
        powmod(h,t,p,g);
    } while (size(g)==1);    
    printf("g= ");
    cotnum(g,stdout);


    fp=fopen("common.dss","wt");
    fprintf(fp,"%d\n",PBITS);
    mip->IOBASE=16;
    cotnum(p,fp);
    cotnum(q,fp);
    cotnum(g,fp);
    fclose(fp);
    return 0;
}
Example #25
0
int main()
{
    int i;
    FILE *fp;
    big K,rid,id,w,a,b,n,q1;
    miracl *mip=mirsys(200,256);
    for (i=0;i<NPRIMES;i++)
    {
        pp[i]=mirvar(0);
        rem[i]=mirvar(0);
    }
    w=mirvar(0);
    n=mirvar(0);
    a=mirvar(0);
    b=mirvar(0);
    p=mirvar(0);
    p1=mirvar(0);     
    q1=mirvar(0);
    K=mirvar(0);
    lim1=mirvar(0);
    lim2=mirvar(0);
    id=mirvar(0);
    rid=mirvar(0);
    order=mirvar(0);

    printf("Enter ID= ");
    innum(rid,stdin);
    getprime("trap1.dat");
    copy(p,n);
    getprime("trap2.dat");
   
    multiply(n,p,n);
    printf("\ncomposite =\n");
    cotnum(n,stdout);

    premult(rid,256,id);   
    while (jack(id,n)!=1)
    { /* bad identity - id=256*rid+i */
        printf("No Discrete Log. for this ID -- incrementing\n");
        incr(id,1,id);
    }

    getprime("trap1.dat");
    copy(p1,q1);
    pollard(id,b);
    getprime("trap2.dat");
    pollard(id,a);

    xgcd(p1,q1,K,K,K); 
    subtract(b,a,w);
    mad(w,K,w,q1,q1,w);
    if(size(w)<0) add_r(w,q1,w);
    subdiv(w,2,w);
    multiply(w,p1,w);
    add_r(w,a,w);

    fp=fopen("secret.dat","w");
    otnum(rid,fp);
    cotnum(w,fp);
    cotnum(n,fp);
    fclose(fp);
    printf("\nDiscrete log (secret key) \n");
    cotnum(w,stdout);
    powltr(PROOT,w,n,id);
    subdiv(id,256,id);
    otstr(id,mip->IOBUFF);
    printf("Check Identity= %s\n",mip->IOBUFF);
    return 0;
}
Example #26
0
int main(int argc, char* argv[]){
	struct global_s global;
	int retcode;
	miracl *mr_mip;
	int retsock=0;
	//char our_public_ip[0x100];

	//mr_mip=mirsys(_MIPP_ 100, 0);
	mr_mip=mirsys(100, 0);




	global_init((char *)&global);
	
	//parse_cmd_lines(_MIPP_ argc, argv, (char *)&global);
	retcode=parse_cmd_lines(argc, argv, (char *)&global);
	if (retcode==-1){
		printf("argv error\n");
		return -1;
	};

	/////////////
	// new code
	///////////

	// relay
	retcode=skyrel_main((char *)&global, &retsock);
	if (retcode==-1){
		printf("Not found good relays\n");
		return -1;
	};

	printf("retsock main: 0x%08X\n",retsock);
	printf("Relay node ip: %s:%d\n",global.relayip,global.relayport);
	printf("Got connid: 0x%08X\n",global.connid);

	//strcpy(our_public_ip,"95.52.137.99");


	// push
	retcode=skypush_main((char *)&global,global.user_snodeip,global.user_snodeport,global.our_public_ip);
	if (retcode==-1){
		printf("Bad answer from user snode\n");
		return -1;
	};




	// recv
	retcode=skyrel_answer((char *)&global, &retsock);
	if (retcode==-1){
		printf("Remote peer fail relay\n");
		//return -1;
	};
	
	
	
	//Sleep(1000);

	/////////////////
	// end new code
	/////////////////

	

	//retcode=main_skypeclient_tcpconnect(_MIPP_ (char *)&global );
	retcode=main_skypeclient_tcpconnect( (char *)&global );

	if (retcode==0){
		if(DEBUG_LEVEL>=1) printf("message delivered\n");
	};

	mirexit();

	return 0;
};
Example #27
0
int main()
{
    int ia,ib,promptr;
    epoint *PA,*PB;
    big A,B,a,b,q,pa,pb,key,x,y;
    ebrick2 binst;
    miracl instance;      /* create miracl workspace on the stack */

/* Specify base 16 here so that HEX can be read in directly without a base-change */

    miracl *mip=mirsys(&instance,WORDS*HEXDIGS,16); /* size of bigs is fixed */
    char mem_big[MR_BIG_RESERVE(10)];         /* we need 10 bigs... */
    char mem_ecp[MR_ECP_RESERVE(2)];          /* ..and two elliptic curve points */
 	memset(mem_big, 0, MR_BIG_RESERVE(10));   /* clear the memory */
	memset(mem_ecp, 0, MR_ECP_RESERVE(2));

    A=mirvar_mem(mip, mem_big, 0);       /* Initialise big numbers */
    B=mirvar_mem(mip, mem_big, 1);
    pa=mirvar_mem(mip, mem_big, 2);
    pb=mirvar_mem(mip, mem_big, 3);
    key=mirvar_mem(mip, mem_big, 4);
    x=mirvar_mem(mip, mem_big, 5);
    y=mirvar_mem(mip, mem_big, 6);
    q=mirvar_mem(mip,mem_big,7);
    a=mirvar_mem(mip, mem_big, 8);
    b=mirvar_mem(mip, mem_big, 9);

    PA=epoint_init_mem(mip, mem_ecp, 0); /* initialise Elliptic Curve points */
    PB=epoint_init_mem(mip, mem_ecp, 1);

    irand(mip, 3L);                      /* change parameter for different random numbers */
    promptr=0;
    init_big_from_rom(B,WORDS,rom,WORDS*4,&promptr);  /* Read in curve parameter B from ROM */
                                                 /* don't need q or G(x,y) (we have precomputed table from it) */
    init_big_from_rom(q,WORDS,rom,WORDS*4,&promptr);
    init_big_from_rom(x,WORDS,rom,WORDS*4,&promptr);
    init_big_from_rom(y,WORDS,rom,WORDS*4,&promptr);

    convert(mip,1,A);                            /* set A=1 */

/* Create precomputation instance from precomputed table in ROM */

    ebrick2_init(&binst,prom,A,B,CURVE_M,CURVE_A,CURVE_B,CURVE_C,WINDOW,CURVE_M);

/* offline calculations */

    bigbits(mip,CURVE_M,a);  /* A's random number */

    ia=mul2_brick(mip,&binst,a,pa,pa);    /* a*G =(pa,ya), ia is sign of ya */

    bigbits(mip,CURVE_M,b);  /* B's random number */
    ib=mul2_brick(mip,&binst,b,pb,pb);    /* b*G =(pb,yb), ib is sign of yb */

/* online calculations */
    ecurve2_init(mip,CURVE_M,CURVE_A,CURVE_B,CURVE_C,A,B,FALSE,MR_PROJECTIVE);

    epoint2_set(mip,pb,pb,ib,PB); /* decompress PB */
    ecurve2_mult(mip,a,PB,PB);
    epoint2_get(mip,PB,key,key);

/* since internal base is HEX, can use otnum instead of cotnum - avoiding a base change */

printf("Alice's Key= ");
otnum(mip,key,stdout);

    epoint2_set(mip,pa,pa,ia,PB); /* decompress PA */
    ecurve2_mult(mip,b,PB,PB);
    epoint2_get(mip,PB,key,key);

printf("Bob's Key=   ");
otnum(mip,key,stdout);

/* clear the memory */

	memset(mem_big, 0, MR_BIG_RESERVE(10));
	memset(mem_ecp, 0, MR_ECP_RESERVE(2));

	return 0;
}
Example #28
0
int main()
{  /*  encode using public key  */
    big e,m,y,ke,mn,mx;
    FILE *ifile;
    FILE *ofile;
    static char line[500];
    static char buff[256];
    char ifname[13],ofname[13];
    BOOL fli,last;
    int i,ipt,klen;
    mip=mirsys(100,0);
    e=mirvar(0);
    m=mirvar(0);
    y=mirvar(0);
    ke=mirvar(0);
    mn=mirvar(0);
    mx=mirvar(0);
    if ((ifile=fopen("public.key","rt"))==NULL)
    {
        printf("Unable to open file public.key\n");
        return 0;
    }
    mip->IOBASE=16;
    cinnum(ke,ifile);
    fclose(ifile);
    nroot(ke,3,mn);
    multiply(mn,mn,m);
    multiply(mn,m,mx);
    subtract(mx,m,mx);
    klen=0;
    copy(mx,m);
    while (size(m)>0)
    { /* find key length in characters */
        klen++;
        subdiv(m,128,m);
    }
    klen--;
    printf("file to be encoded = ");
    gets(ifname);
    fli=FALSE;
    if (strlen(ifname)>0) fli=TRUE;
    if (fli)
    { /* set up input file */
        strcpy(ofname,ifname);
        strip(ofname);
        strcat(ofname,".rsa");
        if ((ifile=fopen(ifname,"rt"))==NULL)
        {
            printf("Unable to open file %s\n",ifname);
            return 0;
        }
        printf("encoding message\n");
    }
    else
    { /* accept input from keyboard */
        ifile=stdin;
        do
        {
            printf("output filename = ");
            gets(ofname); 
        } while (strlen(ofname)==0);
        strip(ofname);    
        strcat(ofname,".rsa");
        printf("input message - finish with cntrl z\n");
    }
    ofile=fopen(ofname,"wt");
    ipt=0;
    last=FALSE;
    while (!last)
    { /* encode line by line */
        if (fgets(&line[ipt],132,ifile)==NULL) last=TRUE;
        if (line[ipt]==EOF) last=TRUE;
        ipt=strlen(line);
        if (ipt<klen && !last) continue;
        while (ipt>=klen)
        { /* chop up into klen-sized chunks and encode */
            for (i=0;i<klen;i++)
                buff[i]=line[i];
            buff[klen]='\0';
            for (i=klen;i<=ipt;i++)
                line[i-klen]=line[i];
            ipt-=klen;
            mip->IOBASE=128;
            cinstr(m,buff);
            power(m,3,ke,e);
            mip->IOBASE=16;
            cotnum(e,ofile);
        }
        if (last && ipt>0)
        { /* now deal with left overs */
            mip->IOBASE=128;
            cinstr(m,line);
            if (compare(m,mn)<0)
            { /* pad out with random number if necessary */
                bigrand(mn,y);
                multiply(mn,mn,e);
                subtract(e,y,e);
                multiply(mn,e,y);
                add(m,y,m);
            }
            power(m,3,ke,e);
            mip->IOBASE=16;
            cotnum(e,ofile);
        }
    }
    fclose(ofile);
    if (fli) fclose(ifile);
    return 0;
}   
Example #29
0
int main()
{  /*  factoring program using Brents method */
    long k,r,i,m,iter;
    big x,y,z,n,q,ys,c3;
    miracl *mip=mirsys(50,0);
    x=mirvar(mip,0);
    y=mirvar(mip,0);
    ys=mirvar(mip,0);
    z=mirvar(mip,0);
    n=mirvar(mip,0);
    q=mirvar(mip,0);
    c3=mirvar(mip,3);
    printf("input number to be factored\n");
    cinnum(mip,n,stdin);
    if (isprime(mip,n))
    {
        printf("this number is prime!\n");
        return 0;
    }
    m=10L;
    r=1L;
    iter=0L;
    do
    {
        printf("iterations=%5ld",iter);
        convert(mip,1,q);
        do
        {
            copy(y,x);
            for (i=1L;i<=r;i++)
                mad(mip,y,y,c3,n,n,y);
            k=0;
            do
            {
                iter++;
                if (iter%10==0) printf("\b\b\b\b\b%5ld",iter);
                fflush(stdout);  
                copy(y,ys);
                for (i=1L;i<=mr_min(m,r-k);i++)
                {
                    mad(mip,y,y,c3,n,n,y);
                    subtract(mip,y,x,z);
                    mad(mip,z,q,q,n,n,q);
                }
                egcd(mip,q,n,z);
                k+=m;
            } while (k<r && size(z)==1);
            r*=2;
        } while (size(z)==1);
        if (compare(z,n)==0) do 
        { /* back-track */
            mad(mip,ys,ys,c3,n,n,ys);
            subtract(mip,ys,x,z);
        } while (egcd(mip,z,n,z)==1);
        if (!isprime(mip,z))
             printf("\ncomposite factor ");
        else printf("\nprime factor     ");
        cotnum(mip,z,stdout);
        if (compare(z,n)==0) return 0;
        divide(mip,n,z,n);
        divide(mip,y,n,n);
    } while (!isprime(mip,n));
    printf("prime factor     ");
    cotnum(mip,n,stdout);
    return 0;
}
Example #30
0
BOOL BaseOT::Miracl_Init(int secparam, BYTE* seed) {
	//secparam = 163;
	m_SecParam = secparam;
	miracl *mip = mirsys(secparam, 2);
	//miracl *mip=mirsys(MR_ROUNDUP(abs(163),4),16);  
	char *ecp = NULL, *ecb = NULL, *ecx = ecx160, *ecy = ecy160;

	m_BB = new Big();
	m_BA = new Big();
	m_BP = new Big();
	
	switch (secparam)
	{
	case 160:
		ecp = ecp160;	ecb = ecb160;	ecx = ecx160;	ecy = ecy160;	break;
	case 163: 
		ecx = ecx163;	ecy = ecy163;	m_nM = 163;	m_nA = 7;	m_nB = 6;	m_nC = 3;	*m_BA = 1;	break;
	case 192:
		ecp = ecp192;	ecb = ecb192;	ecx = ecx192;	ecy = ecy192;	break;
	case 224:
		ecp = ecp224;	ecb = ecb224;	ecx = ecx224;	ecy = ecy224;	break;
	case 233: 
		ecx = ecx233;	ecy = ecy233;	m_nM = 233;	m_nA = 74;	m_nB = 0;	m_nC = 0;	*m_BA = 0;	break;
	case 256:
		ecp = ecp256;	ecb = ecb256;	ecx = ecx256;	ecy = ecy256;	break;
	case 283: 
		ecx = ecx283;	ecy = ecy283;	m_nM = 283;	m_nA = 12;	m_nB = 7;	m_nC = 5;	*m_BA = 0;	break;
	default:
		ecp = ecp192;	ecb = ecb192;	ecx = ecx192;	ecy = ecy192;	m_SecParam = 192; break;
	}
	//seed the miracl rnd generator
	irand((long)(*seed));

	//Change the base to read in the parameters
	mip->IOBASE = 16;
	*m_BB = 1;

	if(m_SecParam == 160 || m_SecParam == 192 || m_SecParam == 224 || m_SecParam == 256)
	{
		mip->IOBASE = 16;
		*m_BA = -3;
		*m_BB = ecb;
		*m_BP = ecp;
		ecurve(*m_BA, *m_BB, *m_BP, MR_BEST);
		m_bUsePrimeField = true;
	} 
	else
	{
		ecurve2_init(m_nM, m_nA, m_nB, m_nC, m_BA->getbig(), m_BB->getbig(), false, MR_BEST);
		m_bUsePrimeField = false;
	}

	m_X = new Big();
	m_Y = new Big();
	*m_X = ecx;
	*m_Y = ecy;
	//change the base back
	mip->IOBASE = 10;



	return true;
}