Exemplo n.º 1
0
void frand(_MIPD_ flash x)
{ /* generates random flash number 0<x<1 */
    int i;
#ifdef MR_FP
    mr_small dres;
#endif
#ifdef MR_OS_THREADS
    miracl *mr_mip=get_mip();
#endif
    if (mr_mip->ERNUM) return;

    MR_IN(46)

    zero(mr_mip->w6);
    mr_mip->w6->len=mr_mip->nib;
    for (i=0;i<mr_mip->nib;i++) 
    { /* generate a full width random number */
        if (mr_mip->base==0) mr_mip->w6->w[i]=brand(_MIPPO_ );
        else                 mr_mip->w6->w[i]=MR_REMAIN(brand(_MIPPO_ ),mr_mip->base);
    }
    mr_mip->check=OFF;
    bigrand(_MIPP_ mr_mip->w6,mr_mip->w5);
    mr_mip->check=ON;
    mround(_MIPP_ mr_mip->w5,mr_mip->w6,x);

    MR_OUT
}
Exemplo n.º 2
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;
}
Exemplo n.º 3
0
/**
 * 测试bigrand()和randint(int,int)的正确性
 * */
int main()
{
    // 设置种子
    srand((unsigned)time(NULL));
    // 生成[1000000,10000000]之间的10个随机数
    for(int i=0;i<10;i++)
    {
        printf("bigrand() = %u\n",bigrand());
        printf("randint(1000000,10000000) = %u\n",randint(1000000,10000000));
    }
    return 0;
}
Exemplo n.º 4
0
int main()
{
	clock_t start, finish;
	double duration;	
	int *array_data;
	int i;

	array_data = (int*) malloc (sizeof(int)*ARRAY_SIZE);
	if (array_data == NULL)
	{
		printf("memory(array_bitmap) allocate failed\n");
		goto _free_mem;
	}
	
	printf("Generate the test data...\n");
	srand(time(0));
	for (i = 0; i < ARRAY_SIZE; i++)
	{
		array_data[i] = bigrand()%RAND_MAX;
	}

	printf("Sorting data...\n");
	start = clock();
	isort1(array_data, ARRAY_SIZE);
	finish = clock();
	duration = (double)(finish - start) / CLOCKS_PER_SEC;
	printf("Test data number is %d\n", ARRAY_SIZE);
	printf("the duration is %f seconds\n\n", duration);

_free_mem:
	if (array_data != NULL)
	{
		free(array_data);
		array_data = NULL;
	}

	return 0;
}
Exemplo n.º 5
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;
}
Exemplo n.º 6
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;
}
Exemplo n.º 7
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;
}
Exemplo n.º 8
0
ZZn randn(void)
{ZZn z; bigrand(get_mip()->modulus,z.fn); return z;}
Exemplo n.º 9
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;
}   
Exemplo n.º 10
0
JNIEXPORT jobjectArray JNICALL
Java_com_sunshuzhou_experiment_1miracl_Verify_computeForServer(JNIEnv *env, jobject instance,
                                                               jstring ux_, jstring uy_,
                                                               jstring u1x_, jstring u1y_,
                                                               jstring wx_, jstring wy_,
                                                               jstring com1x_, jstring com1y_,
                                                               jstring N1_, jstring sid_,
                                                               jstring alpha_, jstring beta_,
                                                               jstring zeta_) {
    const char *ux = (*env)->GetStringUTFChars(env, ux_, 0);
    const char *uy = (*env)->GetStringUTFChars(env, uy_, 0);
    const char *u1x = (*env)->GetStringUTFChars(env, u1x_, 0);
    const char *u1y = (*env)->GetStringUTFChars(env, u1y_, 0);
    const char *wx = (*env)->GetStringUTFChars(env, wx_, 0);
    const char *wy = (*env)->GetStringUTFChars(env, wy_, 0);
    const char *com1x = (*env)->GetStringUTFChars(env, com1x_, 0);
    const char *com1y = (*env)->GetStringUTFChars(env, com1y_, 0);
    const char *N1 = (*env)->GetStringUTFChars(env, N1_, 0);
    const char *sid = (*env)->GetStringUTFChars(env, sid_, 0);
    const char *alpha = (*env)->GetStringUTFChars(env, alpha_, 0);
    const char *beta = (*env)->GetStringUTFChars(env, beta_, 0);
    const char *zeta = (*env)->GetStringUTFChars(env, zeta_, 0);

    big x, y, d, k1, N2, sum, big1;
    epoint *u, *u1, *w, *com1, *w1, *epoint1, *com, *K;
    int message_len, i;
    unsigned char key[300], tag[SHA1_HASH_SIZE], hexdigest[SHA1_HASH_SIZE * 2 + 1], message[1000], tempChars[300];
    jclass jclass1 = (*env)->FindClass(env, "java/lang/String");
    jobjectArray result;

    envirment_init();
    x = mirvar(0);
    y = mirvar(0);
    d = mirvar(0);
    k1 = mirvar(0);
    N2 = mirvar(0);
    sum = mirvar(0);
    big1 = mirvar(0);
    u = epoint_init();
    u1 = epoint_init();
    w = epoint_init();
    com1 = epoint_init();
    w1 = epoint_init();
    epoint1 = epoint_init();
    com = epoint_init();
    K = epoint_init();

    cinstr(x, ux);
    cinstr(y, uy);
    epoint_set(x, y, 0, u);
    cinstr(x, u1x);
    cinstr(y, u1y);
    epoint_set(x, y, 0, u1);
    cinstr(x, wx);
    cinstr(y, wy);
    epoint_set(x, y, 0, w);
    cinstr(x, com1x);
    cinstr(y, com1y);
    epoint_set(x, y, 0, com1);

    irand((long)time(0));
    bigrand(ECC_N, d);
    bigrand(ECC_N, k1);
    bigbits(80, N2);


    // sum = alpha + beta + zeta
    cinstr(big1, alpha);
    cinstr(sum, beta);
    add(big1, sum, sum);
    cinstr(big1, zeta);
    add(big1, sum, sum);

    // w1 = k1 * H
    ecurve_mult(k1, ECC_H, w1);

    // com = (alpha + beta + zeta) * u + d * H
    ecurve_mult(sum, u, com);
    ecurve_mult(d, ECC_H, epoint1);
    ecurve_add(epoint1, com);

    // K = d * w + k1 * (com1 - sum * u1)
    ecurve_mult(d, w, K);
    ecurve_mult(sum, u1, epoint1);
    ecurve_sub(epoint1, com1);
    ecurve_mult(k1, com1, com1);
    ecurve_add(com1, K);


    // K.y as key
    epoint_get(K, x, y);
    cotstr(y, key);
    // message: u.y || u1.y || w.y || com1.y || N1 || sid
    epoint_get(u, x, y);
    cotstr(y, message);
    message_len = strlen(message);
    epoint_get(u1, x, y);
    cotstr(y, &message[message_len]);
    message_len = strlen(message);
    epoint_get(w, x, y);
    cotstr(y, &message[message_len]);
    message_len = strlen(message);
    epoint_get(com1, x, y);
    cotstr(x, &message[message_len]);
    message_len = strlen(message);
    strcpy(&message[message_len], N1);
    message_len = strlen(message);
    strcpy(&message[message_len], sid);
    message_len = strlen(message);

    hmac_sha1(key, strlen(key), message, message_len, tag, SHA1_HASH_SIZE);

    for (i = 0; i < SHA1_HASH_SIZE; ++i) {
        sprintf(&hexdigest[i * 2], "%02x", tag[i]);
    }
    hexdigest[40] = '\0';

    (*env)->ReleaseStringUTFChars(env, ux_, ux);
    (*env)->ReleaseStringUTFChars(env, uy_, uy);
    (*env)->ReleaseStringUTFChars(env, u1x_, u1x);
    (*env)->ReleaseStringUTFChars(env, u1y_, u1y);
    (*env)->ReleaseStringUTFChars(env, wx_, wx);
    (*env)->ReleaseStringUTFChars(env, wy_, wy);
    (*env)->ReleaseStringUTFChars(env, com1x_, com1x);
    (*env)->ReleaseStringUTFChars(env, com1y_, com1y);
    (*env)->ReleaseStringUTFChars(env, N1_, N1);
    (*env)->ReleaseStringUTFChars(env, sid_, sid);
    (*env)->ReleaseStringUTFChars(env, alpha_, alpha);
    (*env)->ReleaseStringUTFChars(env, beta_, beta);
    (*env)->ReleaseStringUTFChars(env, zeta_, zeta);

    result = (*env)->NewObjectArray(env, 8, jclass1, (*env)->NewStringUTF(env, ""));
    epoint_get(w1, x, y);
    cotstr(x, tempChars);
    (*env)->SetObjectArrayElement(env, result, 0, (*env)->NewStringUTF(env, tempChars));
    cotstr(y, tempChars);
    (*env)->SetObjectArrayElement(env, result, 1, (*env)->NewStringUTF(env, tempChars));

    epoint_get(com, x, y);
    cotstr(x, tempChars);
    (*env)->SetObjectArrayElement(env, result, 2, (*env)->NewStringUTF(env, tempChars));
    cotstr(y, tempChars);
    (*env)->SetObjectArrayElement(env, result, 3, (*env)->NewStringUTF(env, tempChars));

    cotstr(N2, tempChars);
    (*env)->SetObjectArrayElement(env, result, 4, (*env)->NewStringUTF(env, tempChars));
    (*env)->SetObjectArrayElement(env, result, 5, (*env)->NewStringUTF(env, message));
    (*env)->SetObjectArrayElement(env, result, 6, (*env)->NewStringUTF(env, hexdigest));

    (*env)->SetObjectArrayElement(env, result, 7, (*env)->NewStringUTF(env, key));

    mirkill(x);
    mirkill(y);
    mirkill(d);
    mirkill(k1);
    mirkill(N2);
    mirkill(sum);
    mirkill(big1);

    return result;
}
Exemplo n.º 11
0
int main()
{
    FILE *fp;
    char ifname[13],ofname[13];
    big p,q,g,x,r,s,k,hash;
    long seed;
    int bits;
    miracl *mip;

/* get public data */
    fp=fopen("common.dss","r");
    if (fp==NULL)
    {
        printf("file common.dss does not exist\n");
        return 0;
    }

    fscanf(fp,"%d\n",&bits);
    mip=mirsys(3+bits/MIRACL,0);
    p=mirvar(0);
    q=mirvar(0);
    g=mirvar(0);
    x=mirvar(0);
    r=mirvar(0);
    s=mirvar(0);
    k=mirvar(0);
    hash=mirvar(0);

    mip->IOBASE=16;
    cinnum(p,fp);
    cinnum(q,fp);
    cinnum(g,fp);
    mip->IOBASE=10;
    fclose(fp);

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

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


/* get private key of signer */
    fp=fopen("private.dss","r");
    if (fp==NULL)
    {
        printf("file private.dss does not exist\n");
        return 0;
    }
    cinnum(x,fp);
    fclose(fp);

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

/* calculate s */
    xgcd(k,q,k,k,k);
    mad(x,r,hash,q,q,s);
    mad(s,k,k,q,q,s);
    fp=fopen(ofname,"w");
    cotnum(r,fp);
    cotnum(s,fp);
    fclose(fp);
    mirexit();
    return 0;
}
Exemplo n.º 12
0
int main()
{
    FILE *fp;
    int m,a,b,c,cf;
    miracl *mip;
    char ifname[13],ofname[13];
    big a2,a6,q,x,y,d,r,s,k,hash;
    epoint *g;
    long seed;
/* 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+m/MIRACL,0);
    a2=mirvar(0);
    a6=mirvar(0);
    q=mirvar(0);
    x=mirvar(0);
    y=mirvar(0);
    d=mirvar(0);
    r=mirvar(0);
    s=mirvar(0);
    k=mirvar(0);
    hash=mirvar(0);

    mip->IOBASE=16;
    cinnum(a2,fp);     /* curve parameters */
    cinnum(a6,fp);     /* curve parameters */
    cinnum(q,fp);     /* order of (x,y) */
    cinnum(x,fp);     /* (x,y) point on curve of order q */
    cinnum(y,fp);
    mip->IOBASE=10;
    fscanf(fp,"%d\n",&a);
    fscanf(fp,"%d\n",&b);
    fscanf(fp,"%d\n",&c);
    fclose(fp);

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

    ecurve2_init(m,a,b,c,a2,a6,FALSE,MR_PROJECTIVE);  /* initialise curve */

    g=epoint2_init();
    epoint2_set(x,y,0,g); /* set point of order q */

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

/* get private key of signer */
    fp=fopen("private.ecs","r");
    if (fp==NULL)
    {
        printf("file private.ecs does not exist\n");
        return 0;
    }
    cinnum(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(fp,hash);
    fclose(fp);

/* calculate s */
    xgcd(k,q,k,k,k);

    mad(d,r,hash,q,q,s);
    mad(s,k,k,q,q,s);
    fp=fopen(ofname,"w");
    cotnum(r,fp);
    cotnum(s,fp);
    fclose(fp);
    return 0;
}
Exemplo n.º 13
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;
}
Exemplo n.º 14
0
int randint(int n, int u)
{
	return n + bigrand() % (u - n + 1);
}
Exemplo n.º 15
0
/**
 * 生成[l,m]之间的随机数
 * */
unsigned int randint(int l, int m)
{
    return l + bigrand() % (m-l+1);
}