Example #1
0
void sftbit(_MIPD_ big x,int n,big z)
{ /* shift x by n bits */
    int m;
    mr_small sm;
#ifdef MR_OS_THREADS
    miracl *mr_mip=get_mip();
#endif
    if (mr_mip->ERNUM) return;
    copy(x,z);
    if (n==0) return;

    MR_IN(47)

    m=mr_abs(n);
    sm=mr_shiftbits((mr_small)1,m%mr_mip->lg2b);
    if (n>0)
    { /* shift left */

#ifndef MR_ALWAYS_BINARY
        if (mr_mip->base==mr_mip->base2)
        {
#endif
            mr_shift(_MIPP_ z,n/mr_mip->lg2b,z);
            mr_pmul(_MIPP_ z,sm,z);
#ifndef MR_ALWAYS_BINARY
        }
        else
        {
            expb2(_MIPP_ m,mr_mip->w1);
            multiply(_MIPP_ z,mr_mip->w1,z);
        }
#endif
    }
    else
    { /* shift right */

#ifndef MR_ALWAYS_BINARY
        if (mr_mip->base==mr_mip->base2)
        {
#endif
            mr_shift(_MIPP_ z,n/mr_mip->lg2b,z);
#ifdef MR_FP_ROUNDING
            mr_sdiv(_MIPP_ z,sm,mr_invert(sm),z);
#else
            mr_sdiv(_MIPP_ z,sm,z);
#endif

#ifndef MR_ALWAYS_BINARY
        }
        else
        {
            expb2(_MIPP_ m,mr_mip->w1);
            divide(_MIPP_ z,mr_mip->w1,z);
        }
#endif
    }
    MR_OUT
}
Example #2
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 #3
0
int main()
{
    FILE *fp;
    big q,p,p1,h,t,g,low,high;
    big pool[POOL_SIZE];
    BOOL fail;
    int i,j,p1bits,np;
    long seed,m,permutation;
    miracl *mip=mirsys(100,0);
    q=mirvar(0);
    p=mirvar(0);
    h=mirvar(0);
    t=mirvar(0);
    g=mirvar(0);
    p1=mirvar(0);
    low=mirvar(0);
    high=mirvar(0);
    gprime(10000);

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

/* find number of primes pa, pb, pc etc., that will be needed */

    np=1;
    while (p1bits/np >= OBITS) np++;
    np--;

/* find the high/low limits for these primes, so that 
   the generated prime p will be exactly PBITS in length */

    expb2(p1bits-1,t);
    nroot(t,np,low);      /* np-th integer root */
    incr(low,1,low);

    premult(t,2,t);
    decr(t,1,t);
    nroot(t,np,high);

    subtract(high,low,t);   /* raise low limit up to half-way...  */
    subdiv(t,2,t);
    subtract(high,t,low);

/* generate q  */
    forever
    { /* make sure leading two bits of q 11... */
        expb2(QBITS,q);
        bigbits(QBITS-2,t);
        subtract(q,t,q);
        nxprime(q,q);
        if (logb2(q)>QBITS) continue;
        break;
    }
    printf("q= (%d bits)\n",logb2(q));
    cotnum(q,stdout);

/* generate prime pool from which permutations of np 
   primes will be picked until a Lim-Lee prime is found */

    for (i=0;i<POOL_SIZE;i++)
    { /* generate the primes pa, pb, pc etc.. */
        pool[i]=mirvar(0);
        forever
        { 
            bigrand(high,p1);
            if (mr_compare(p1,low)<0) continue;
            nxprime(p1,p1);
            if (mr_compare(p1,high)>0) continue;
            copy(p1,pool[i]);  
            break;
        }
    }

/* The '1' bits in the permutation indicate which primes are 
   picked from the pool. If np=5, start at 11111, then 101111 etc */

    permutation=1L;
    for (i=0;i<np;i++) permutation<<=1;
    permutation-=1;     /* permuation = 2^np-1 */

/* generate p   */
    fail=FALSE;
    forever 
    {
        convert(1,p1);
        for (i=j=0,m=1L;j<np;i++,m<<=1)
        {
            if (i>=POOL_SIZE) 
            { /* ran out of primes... */
                fail=TRUE;
                break;
            }
            if (m&permutation) 
            {
                multiply(p1,pool[i],p1); 
                j++;
            }
        } 
        if (fail) break;
        printf(".");   
        premult(q,2,p);
        multiply(p,p1,p);
        incr(p,1,p);
        permutation=increment(permutation);
        if (logb2(p)!=PBITS) continue;
        if (isprime(p)) break; 
    } 

    if (fail)
    {
        printf("\nFailed - very unlikely! - try increasing POOL_SIZE\n");
        return 0;
    }

    printf("\np= (%d bits)\n",logb2(p));
    cotnum(p,stdout);

/* finally find g */
    do {
        decr(p,1,t);
        bigrand(t,h);
        divide(t,q,t);
        powmod(h,t,p,g);
    } while(size(g)==1);    
   
    printf("g= (%d bits)\n",logb2(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;
}