Esempio n. 1
0
bool lemur::index::InvDocList::getMoreMem() {
  int ldiff = lastid-begin;
  int enddiff = end-begin;
  int bigger = size*2;

  if (hascache) {
    int pow = logb2(bigger);
    if (pow > 22)
      return false;

    lemur::api::LOC_T* loc = (lemur::api::LOC_T *)cache->getMoreMem(pow, (int *)begin, pow-1);
    if (loc == NULL)
      return false;
    begin = loc;
  } else {
    lemur::api::LOC_T* old = begin;
    //    begin = (int*) malloc(bigger);
    // use new/delete[] so an exception will be thrown if out of memory
    begin = new lemur::api::LOC_T[bigger/sizeof(lemur::api::LOC_T)];
    memcpy(begin, old, size);
    //free(old);
    delete[](old);
  }
  lastid = begin+ldiff;
  freq = lastid+1;
  end = begin+enddiff;
  size = bigger;
  return true;
}
Esempio n. 2
0
void primemod(int bits,big p)
{
    do {
        printf("%d bit prime.....\n",bits);
        bigbits(bits,p);
        nxprime(p,p);
    } while (logb2(p)!=bits);
}
Esempio n. 3
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;
}
Esempio n. 4
0
void zzn3_powl(_MIPD_ zzn3 *x,big e,zzn3 *w)
{
    int i,s;
    zzn3 t1,t3,t4;
    t1.a=mr_mip->w7;
    t1.b=mr_mip->w8;
    t1.c=mr_mip->w9;
    t3.a=mr_mip->w10;
    t3.b=mr_mip->w11;
    t3.c=mr_mip->w12;
    t4.a=mr_mip->w13;
    t4.b=mr_mip->w14;
    t4.c=mr_mip->w15;

    zzn3_from_int(_MIPP_ 1,&t1);

    s=size(e);
    if (s==0)
    {
        zzn3_copy(&t1,w);
        return;
    }
    zzn3_copy(x,w);
    if (s==1 || s==(-1)) return;

    i=logb2(_MIPP_ e)-1;

    zzn3_copy(w,&t3);
    zzn3_mul(_MIPP_ w,w,&t4);
    zzn3_add(_MIPP_ &t4,&t4,&t4);
    zzn3_sub(_MIPP_ &t4,&t1,&t4);

    while (i--)
    {
        if (mr_testbit(_MIPP_ e,i))
        {
            zzn3_mul(_MIPP_ &t3,&t4,&t3);
            zzn3_add(_MIPP_ &t3,&t3,&t3);
            zzn3_sub(_MIPP_ &t3,w,&t3);
            zzn3_mul(_MIPP_ &t4,&t4,&t4);
            zzn3_add(_MIPP_ &t4,&t4,&t4);
            zzn3_sub(_MIPP_ &t4,&t1,&t4);
        }
        else
        {
            zzn3_mul(_MIPP_ &t4,&t3,&t4);
            zzn3_add(_MIPP_ &t4,&t4,&t4);
            zzn3_sub(_MIPP_ &t4,w,&t4);
            zzn3_mul(_MIPP_ &t3,&t3,&t3);
            zzn3_add(_MIPP_ &t3,&t3,&t3);
            zzn3_sub(_MIPP_ &t3,&t1,&t3);
        }
    }
    zzn3_copy(&t3,w);
}
Esempio n. 5
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;
}
Esempio n. 6
0
void lemur::index::InvDocList::resetFree() {
  // free the memory
  if (hascache) {
    int pow = logb2(size);
    cache->freeMem((int *)begin, pow);   
  } else if (begin != NULL) {
    //    free(begin);
    delete[](begin);
  } 

  begin = end = lastid = freq = NULL;
  size = 0;
  df = 0;
  uid = -1;
}
Esempio n. 7
0
uint encodeGamma(uint* output, uint pos, uint value) {
	uint l = logb2(value);
	uint i;	
	for(i = 0; i < l; i++) {
		bitclean(output,pos);
		pos++;
	}
	bitset(output,pos);
	pos++;
	for(i = 0; i < l; i++) {
		if(value & (1 << i))
			bitset(output,pos);
		else
			bitclean(output,pos);
		pos++;
	}
	
	return 2*l+1;
}
Esempio n. 8
0
float  Noise::LimitLevel(Point3 dp, float &smw ) {
#ifdef DOAA
#define epsilon	0.00001f

	if (filter) {
		float m = (float(fabs(dp.x) + fabs(dp.y) + fabs(dp.z))/3.0f)/size;
		if ( m < epsilon ) m = epsilon;
		float l = logb2(1/m);
		float smWidth = m*.2f;
		if (smWidth>.4f) smWidth = .4f;
		smw = smWidth;
		return  (levels<l)?levels:l;	  
		}
	else 
#endif 
		{
		smw = 0.0f;
		return levels;
		}
	}
Esempio n. 9
0
int main()
{
    FILE *fp;
    big e,n,a,b,x,y,r;
    epoint *g;
    ebrick binst;
    int nb,bits,window,len,bptr,m,i,j;
    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);
#ifndef MR_EDWARDS
    fp=fopen("common.ecs","rt");
#else
    fp=fopen("edwards.ecs","rt");
#endif
	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 max. size of exponent in bits = ");
    scanf("%d",&nb);
    getchar();
    printf("Enter window size in bits (1-10)= ");
    scanf("%d",&window);
    getchar();

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

/* Print out the precomputed table (for use in ecdhp.c ?) 
   In which case make sure that MR_SPECIAL is defined and 
   active in the build of this program, so MR_COMBA must
   also be defined as the number of words in the modulus *

len=MR_ROUNDUP(bits,MIRACL);
bptr=0;
for (i=0;i<2*(1<<window);i++)
{
    for (j=0;j<len;j++)
    {
        printf("0x%x,",binst.table[bptr++]);
    }
    printf("\n");
}

*/

    printf("%d elliptic curve points have been precomputed and stored\n",(1<< window));

    bigbits(nb,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("Comb method\n");
    mul_brick(&binst,e,x,y);

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

    return 0;
}
Esempio n. 10
0
void fast_tate_pairing(_MIPD_ zzn2 *Qx,zzn2 *Qy,epoint *P,big T,big fr,big delta,zzn4 *w,zzn4* res)
{
    int i,j,nb;
    zzn2 Ax,Ay,Az,Qz;
    zzn Px,Py;

#ifndef MR_STATIC
    char *mem=memalloc(_MIPP_ 10);
#else
    char mem[MR_BIG_RESERVE(10)];
    memset(mem,0,MR_BIG_RESERVE(10));
#endif

#ifdef MR_COUNT_OPS
    fpa=fpc=fpx=0;
#endif

    Ax.a=mirvar_mem(_MIPP_ mem,0);
    Ax.b=mirvar_mem(_MIPP_ mem,1);
    Ay.a=mirvar_mem(_MIPP_ mem,2);
    Ay.b=mirvar_mem(_MIPP_ mem,3);
    Az.a=mirvar_mem(_MIPP_ mem,4);
    Az.b=mirvar_mem(_MIPP_ mem,5);
    Qz.a=mirvar_mem(_MIPP_ mem,6);
    Qz.b=mirvar_mem(_MIPP_ mem,7);
    Px=mirvar_mem(_MIPP_ mem,8);
    Py=mirvar_mem(_MIPP_ mem,9);

    copy(P->X,Px);
    copy(P->Y,Py);

    nres_modadd(_MIPP_ Px,Px,Px); /* removes a factor of 2 from g() */
    nres_modadd(_MIPP_ Py,Py,Py);

    zzn2_from_int(_MIPP_ 1,&Qz);
    zzn2_copy(Qx,&Ax);
    zzn2_copy(Qy,&Ay);
    zzn2_copy(&Qz,&Az);

    zzn4_from_int(_MIPP_ 1,res);

    /* Simple Miller loop */
    nb=logb2(_MIPP_ T);
    for (i=nb-2; i>=0; i--)
    {
        zzn4_mul(_MIPP_ res,res,res);
        g(_MIPP_ &Ax,&Ay,&Az,&Ax,&Ay,&Az,Px,Py,w);
        zzn4_mul(_MIPP_ res,w,res);
        if (mr_testbit(_MIPP_ T,i))
        {
            g(_MIPP_ &Ax,&Ay,&Az,Qx,Qy,&Qz,Px,Py,w);
            zzn4_mul(_MIPP_ res,w,res);
        }
    }

#ifdef MR_COUNT_OPS
    printf("Millers loop Cost\n");
    printf("fpc= %d\n",fpc);
    printf("fpa= %d\n",fpa);
    printf("fpx= %d\n",fpx);

    fpa=fpc=fpx=0;
#endif

    zzn4_copy(res,w);
    zzn4_powq(_MIPP_ fr,w);
    zzn4_powq(_MIPP_ fr,w);
    zzn4_inv(_MIPP_ res);
    zzn4_mul(_MIPP_ res,w,res); /* ^(p*p-1) */

    res->unitary=TRUE;

    zzn4_mul(_MIPP_ res,res,res);
    zzn4_copy(res,w);
    zzn4_mul(_MIPP_ res,res,res);
    zzn4_mul(_MIPP_ res,res,res);
    zzn4_mul(_MIPP_ res,res,res);
    zzn4_mul(_MIPP_ res,res,res);
    zzn4_mul(_MIPP_ res,w,res);    /* res=powu(res,CF) for CF=34 */

#ifndef MR_STATIC
    memkill(_MIPP_ mem,10);
#else
    memset(mem,0,MR_BIG_RESERVE(10));
#endif
}
Esempio n. 11
0
void zzn4_powu(_MIPD_ zzn4 *x,big k,zzn4 *u)
{
    zzn4 t[5],u2;
    big k3;
    int i,j,n,nb,nbw,nzs;
#ifndef MR_STATIC
    char *mem=memalloc(_MIPP_ 25);
#else
    char mem[MR_BIG_RESERVE(25)];
    memset(mem,0,MR_BIG_RESERVE(25));
#endif

    if (size(k)==0)
    {
        zzn4_from_int(_MIPP_ 1,u);
        return;
    }
    zzn4_copy(x,u);
    if (size(k)==1) return;

    for (j=i=0; i<5; i++)
    {
        t[i].x.a=mirvar_mem(_MIPP_ mem,j++);
        t[i].x.b=mirvar_mem(_MIPP_ mem,j++);
        t[i].y.a=mirvar_mem(_MIPP_ mem,j++);
        t[i].y.b=mirvar_mem(_MIPP_ mem,j++);
        t[i].unitary=FALSE;
    }
    u2.x.a=mirvar_mem(_MIPP_ mem,j++);
    u2.x.b=mirvar_mem(_MIPP_ mem,j++);
    u2.y.a=mirvar_mem(_MIPP_ mem,j++);
    u2.y.b=mirvar_mem(_MIPP_ mem,j++);
    u2.unitary=FALSE;
    k3=mirvar_mem(_MIPP_ mem,j);

    premult(_MIPP_ k,3,k3);
    zzn4_mul(_MIPP_ u,u,&u2);
    zzn4_copy(u,&t[0]);

    for (i=1; i<=4; i++)
        zzn4_mul(_MIPP_ &u2,&t[i-1],&t[i]);

    nb=logb2(_MIPP_ k3);

    for (i=nb-2; i>=1;)
    {
        n=mr_naf_window(_MIPP_ k,k3,i,&nbw,&nzs,5);

        for (j=0; j<nbw; j++) zzn4_mul(_MIPP_ u,u,u);
        if (n>0)            zzn4_mul(_MIPP_ u,&t[n/2],u);
        if (n<0)
        {
            zzn4_conj(_MIPP_ &t[-n/2],&u2);
            zzn4_mul(_MIPP_ u,&u2,u);
        }
        i-=nbw;
        if (nzs)
        {
            for (j=0; j<nzs; j++) zzn4_mul(_MIPP_ u,u,u);
            i-=nzs;
        }
    }

#ifndef MR_STATIC
    memkill(_MIPP_ mem,25);
#else
    memset(mem,0,MR_BIG_RESERVE(25));
#endif
}
Esempio n. 12
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((char *)"trap1.dat");
    copy(p,n);
    getprime((char *)"trap2.dat");
   
    multiply(n,p,n);
    printf("\ncomposite =\n");
    cotnum(n,stdout);
    printf("(%d bits)\n",logb2(n));
    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((char *)"trap1.dat");
    copy(p1,q1);
    pollard(id,b);
    getprime((char *)"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(w,q1,w);
    subdiv(w,2,w);
    multiply(w,p1,w);
    add(w,a,w);

    fp=fopen("secret.dat","wt");
    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;
}
Esempio n. 13
0
void power4(_MIPD_ big *a,big k,big *u)
{ 
    int i,j,m,nb,n,nbw,nzs;
    big u2[4],t[4][4];
#ifndef MR_STATIC
    char *mem=(char *)memalloc(_MIPP_ 20);
#else
    char mem[MR_BIG_RESERVE(20)];       
    memset(mem,0,MR_BIG_RESERVE(20));
#endif
    m=0;
    for (i=0;i<4;i++)
        for (j=0;j<4;j++) t[i][j]=mirvar_mem(_MIPP_ mem,m++);

    u2[0]=mirvar_mem(_MIPP_ mem,16);
    u2[1]=mirvar_mem(_MIPP_ mem,17);
    u2[2]=mirvar_mem(_MIPP_ mem,18);
    u2[3]=mirvar_mem(_MIPP_ mem,19);

    if(size(k)==0) 
    {
        convert(_MIPP_ 1,u[0]);
        zero(u[1]);
        zero(u[2]);
        zero(u[3]);
#ifndef MR_STATIC
        memkill(_MIPP_ mem,20);
#else
        memset(mem,0,MR_BIG_RESERVE(20));
#endif
        return;
    }

    for (i=0;i<4;i++)
        fcopy2(a[i],u[i]);

    if (size(k)==1)
    {
#ifndef MR_STATIC
        memkill(_MIPP_ mem,20);
#else
        memset(mem,0,MR_BIG_RESERVE(20));
#endif
        return;
    }

    square4(_MIPP_ u,u2);
    for (i=0;i<4;i++)	
        fcopy2(u[i],t[0][i]);	

    for(i=1;i<4;i++)
        mult4(_MIPP_ u2,t[i-1],t[i]);

    nb=logb2(_MIPP_ k);
    if(nb>1) for(i=nb-2;i>=0;)
    {			
        n=mr_window(_MIPP_ k,i,&nbw,&nzs,3); /* small 3 bit window to save RAM */
        for(j=0;j<nbw;j++)
            square4(_MIPP_ u,u);
        if(n>0)	mult4(_MIPP_ u,t[n/2],u);
        i-=nbw;
        if(nzs!=0)
        {
            for (j=0;j<nzs;j++) square4(_MIPP_ u,u);
            i-=nzs;	 				
        }				
    }	

#ifndef MR_STATIC
    memkill(_MIPP_ mem,20);
#else
    memset(mem,0,MR_BIG_RESERVE(20));
#endif
}
Esempio n. 14
0
/* 
 * function encodeByteArrayToPoint		: Encodes the given byte array into a point. 
 *										  If the given byte array can not be encoded to a point, returns 0.
 * param dlog							: Pointer to the native Dlog object.
 * param binaryString					: The byte array to encode.
 * param k								: k is the maximum length of a string to be converted to a Group Element of this group. 
 *										  If a string exceeds the k length it cannot be converted.
 * return								: The created point or 0 if the point cannot be created.
 */
JNIEXPORT jlong JNICALL Java_edu_biu_scapi_primitives_dlog_miracl_MiraclDlogECFp_encodeByteArrayToPoint
  (JNIEnv * env, jobject, jlong m, jbyteArray binaryString, jint k){
	   //Pseudo-code:
		/*If the length of binaryString exceeds k then throw IndexOutOfBoundsException.

          Let L be the length in bytes of p

          Choose a random byte array r of length L – k – 2 bytes 

          Prepare a string newString of the following form: r || binaryString || binaryString.length (where || denotes concatenation) (i.e., the least significant byte of newString is the length of binaryString in bytes)

          Convert the result to a BigInteger (bIString)

          Compute the elliptic curve equation for this x and see if there exists a y such that (x,y) satisfies the equation.

          If yes, return (x,y)

          Else, go back to step 3 (choose a random r etc.) up to 80 times (This is an arbitrary hard-coded number).

          If did not find y such that (x,y) satisfies the equation after 80 trials then return null.
		 */

	   /* convert the accepted parameters to MIRACL parameters*/
	   miracl* mip = (miracl*)m;
	 
	  jbyte* string  = (jbyte*) env->GetByteArrayElements(binaryString, 0);
	  int len = env->GetArrayLength(binaryString);
 
	  if (len > k){
		  env ->ReleaseByteArrayElements(binaryString, string, 0);
		  return 0;
	  }
	
	  big x, p;
	  x = mirvar(mip, 0);
	  p = mip->modulus;
	  
	  int l = logb2(mip, p)/8;
	  
	  char* randomArray = new char[l-k-2];
	  char* newString = new char[l - k - 1 + len];
	  memcpy(newString+l-k-2, string, len);

	  newString[l - k - 2 + len] = (char) len;

	  
	  int counter = 0;
	  bool success = 0;

	  csprng rng;  
      srand(time(0));
	  long seed;  
	  char raw = rand();
	  time((time_t*)&seed);  
	  strong_init(&rng,1,&raw,seed);                 
	  do{
		  
			for (int i=0; i<l-k-2; i++){
				  randomArray[i] = strong_rng(&rng);
			}
			
			memcpy(newString, randomArray, l-k-2);
			
			bytes_to_big(mip, l - k - 1 + len, newString, x);
			
			//If the number is negative, make it positive.
			if(exsign(x)== -1){
				absol(x, x);
			}
			//epoint_x returns true if the given x value leads to a valid point on the curve.
			//if failed, go back to choose a random r etc.
			success = epoint_x(mip, x);
			counter++;
	  } while((!success) && (counter <= 80)); //we limit the amount of times we try to 80 which is an arbitrary number.
	  
	  epoint* point = 0;
	  if (success){
		point = epoint_init(mip);
		epoint_set(mip, x, x, 0, point);
	  }

	  char* temp = new char[l - k - 1 + len];
	  big_to_bytes(mip,l - k - 1 + len , x, temp, 1);
	  
	  //Delete the allocated memory.
	  env ->ReleaseByteArrayElements(binaryString, string, 0);
	  
	  mirkill(x);
	 
	  delete(randomArray);
	  delete(newString);
	  
	  //Return the created point.
	  return (long) point;
}
Esempio n. 15
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;
}
Esempio n. 16
0
void fast_tate_pairing(_MIPD_ epoint *P,zzn3 *Qx,zzn3 *Qy,big q,big cf,zzn6 *w,zzn6* res)
{
    int i,j,n,nb,nbw,nzs;
    epoint *t[4],*A,*P2;
    zzn6 zn[4];
    big work[4];

#ifndef MR_STATIC
    char *mem=memalloc(_MIPP_ 28);
	char *mem1=ecp_memalloc(_MIPP_ 6);
#else
    char mem[MR_BIG_RESERVE(28)];  
    char mem1[MR_ECP_RESERVE(6)]; 
    memset(mem,0,MR_BIG_RESERVE(28));
    memset(mem1,0,MR_ECP_RESERVE(6));
#endif

    for (i=0;i<4;i++)
        t[i]=epoint_init_mem(_MIPP_ mem1,i);
    A=epoint_init_mem(_MIPP_ mem1,4);
    P2=epoint_init_mem(_MIPP_ mem1,5);
  
    for (j=i=0;i<4;i++)
    {
        work[i]=mirvar_mem(_MIPP_ mem,j++);
        zn[i].x.a=mirvar_mem(_MIPP_ mem,j++);
        zn[i].x.b=mirvar_mem(_MIPP_ mem,j++);
        zn[i].x.c=mirvar_mem(_MIPP_ mem,j++);
        zn[i].y.a=mirvar_mem(_MIPP_ mem,j++);
        zn[i].y.b=mirvar_mem(_MIPP_ mem,j++);
        zn[i].y.c=mirvar_mem(_MIPP_ mem,j++);
        zn[i].unitary=FALSE;
    }

    zzn6_from_int(_MIPP_ 1,&zn[0]); 
    epoint_copy(P,A);
    epoint_copy(P,P2);
    epoint_copy(P,t[0]);

    g(_MIPP_ P2,P2,Qx,Qy,res);
    epoint_norm(_MIPP_ P2);

    for (i=1;i<4;i++)
    {
        g(_MIPP_ A,P2,Qx,Qy,w);
        epoint_copy(A,t[i]);
        zzn6_mul(_MIPP_ &zn[i-1],w,&zn[i]);
        zzn6_mul(_MIPP_ &zn[i],res,&zn[i]);
    }

    epoint_multi_norm(_MIPP_ 4,work,t);

    epoint_copy(P,A);
    zzn6_from_int(_MIPP_ 1,res);
 
    nb=logb2(_MIPP_ q);
    for (i=nb-2;i>=0;i-=(nbw+nzs))
    {
        n=mr_window(_MIPP_ q,i,&nbw,&nzs,3);
        for (j=0;j<nbw;j++)
        {
            zzn6_mul(_MIPP_ res,res,res);
            g(_MIPP_ A,A,Qx,Qy,w);
            zzn6_mul(_MIPP_ res,w,res);
        }
        if (n>0)
        {
            zzn6_mul(_MIPP_ res,&zn[n/2],res);
            g(_MIPP_ A,t[n/2],Qx,Qy,w);
            zzn6_mul(_MIPP_ res,w,res);
        }
        for (j=0;j<nzs;j++) 
        {
            zzn6_mul(_MIPP_ res,res,res);
            g(_MIPP_ A,A,Qx,Qy,w);
            zzn6_mul(_MIPP_ res,w,res);
        }
    }

    zzn6_copy(res,w);
    zzn6_powq(_MIPP_ w);
    zzn6_mul(_MIPP_ res,w,res);

    zzn6_copy(res,w);
    zzn6_powq(_MIPP_ w);
    zzn6_powq(_MIPP_ w);
    zzn6_powq(_MIPP_ w);

    zzn6_inv(_MIPP_ res);
    zzn6_mul(_MIPP_ res,w,res);

    res->unitary=TRUE;

#ifndef MR_STATIC      
    memkill(_MIPP_ mem,28);
    ecp_memkill(_MIPP_ mem1,6);
#else
    memset(mem,0,MR_BIG_RESERVE(28)); 
    memset(mem1,0,MR_ECP_RESERVE(6));
#endif
}
Esempio n. 17
0
epoint* computeLL(miracl* mip, epoint** elements, big* exponents, int n, int field){
		
	big bigExp =  mirvar(mip, 0);
	big two = mirvar(mip, 2);
	big zero = mirvar(mip, 0);
	int t = 0, w, h, i, j;
	epoint*** preComp;
	epoint* result;

	//get the biggest exponent
	for (i=0; i<n; i++)
		if (mr_compare(bigExp, exponents[i]) < 0)
			bigExp = exponents[i];
	//num of bitf in the biggest exponent
	t = logb2(mip, bigExp);

	//choose w according to the value of t
	w = getLLW(t);
		
	//h = n/w
	if ((n % w) == 0){
		h = n / w;
	} else{
		h = ((int) (n / w)) + 1;
	}
		
	//printf("n is: %d\n", n);
	//printf("t is: %d\n", t);
	//printf("w is: %d\n", w);
	//printf("h is: %d\n", h);

	//creates pre computation table
	preComp = createLLPreCompTable(mip, elements, w, h, n, field);
		
	result = getIdentity(mip, field); //holds the computation result		
		
	//computes the loop of the computation
	result = computeLoop(mip, exponents, w, h, preComp, result, t-1, n, field);
	
	//third part of computation
	for (j=t-2; j>=0; j--){
		//operate y^2 differently. depends on the field type
		if (field==1)
			ecurve_mult(mip, two, result, result);
		else
			ecurve2_mult(mip, two, result, result);
		//computes the loop of the computation
		result = computeLoop(mip, exponents, w, h, preComp, result, j, n, field);
	}
		
	//free the allocated memeory
	mirkill(two);
	mirkill(zero);

	for (i=0; i<h; i++){
		for (j=0; j<pow((double)2, w); j++){
			epoint_free(preComp[i][j]);
		}
		free(preComp[i]);
	}
	free(preComp);

	return result;
}
Esempio n. 18
0
 sgn=norm(_MIPP_ TAN,y);
 ftrunc(_MIPP_ y,y,mr_mip->w10);
 n=size(y);
 if (n!=0) build(_MIPP_ y,tan1);
 if (size(mr_mip->w10)==0)
 {
     insign(sgn,y);
     MR_OUT
     return;
 }
 sqrn=isqrt(mr_mip->lg2b*mr_mip->workprec,mr_mip->lg2b);
 nsq=0;
 copy(mr_mip->w10,mr_mip->w8);
 frecip(_MIPP_ mr_mip->w10,mr_mip->w10);
 ftrunc(_MIPP_ mr_mip->w10,mr_mip->w10,mr_mip->w10);
 m=logb2(_MIPP_ mr_mip->w10);
 if (m<sqrn)
 { /* scale fraction down */
     nsq=sqrn-m;
     expint(_MIPP_ 2,nsq,mr_mip->w10);
     fdiv(_MIPP_ mr_mip->w8,mr_mip->w10,mr_mip->w8);
 }
 zero(mr_mip->w10);
 fmul(_MIPP_ mr_mip->w8,mr_mip->w8,mr_mip->w9);
 negify(mr_mip->w9,mr_mip->w9);
 op[0]=0x4B;    /* set up for x/(y+C) */
 op[1]=op[3]=1;
 op[2]=0;
 for (m=sqrn;m>1;m--)
 { /* Unwind C.F for tan(x)=z/(1-z^2/(3-z^2/(5-...)))))) */
     op[4]=2*m-1;
Esempio n. 19
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;
}