/* * Class: edu_biu_scapi_primitives_dlog_miracl_MiraclDlogECF2m * Method: computeF2mExponentiateWithPrecomputedValues * Signature: (JJ[B)J * This function wraps the actual computation of the exponentation with precomputed values for the requested base for Dlog groups over F2m. It gets as a parameter * a pointer to the ebrick structure created by a previous call to initFpExponentiateWithPrecomputedValues. This implies that initFpExponentiateWithPrecomputedValues * MUST have been called prior to this function for the same base. */ JNIEXPORT jlong JNICALL Java_edu_biu_scapi_primitives_dlog_miracl_MiraclDlogECF2m_computeF2mExponentiateWithPrecomputedValues (JNIEnv * env, jobject, jlong mipp, jlong ebrick2Pointer, jbyteArray exponent){ //private native long computeF2mExponentiateWithPrecomputedValues(long mip, long ebrickPointer, byte[] exponent); //translate parameters to miracl notation miracl* mip = (miracl*)mipp; big exponentB = byteArrayToMiraclBig(env, mip, exponent); //(x,y) are the coordinates of the point which is the result of the exponentiation big x, y; x = mirvar(mip, 0); y = mirvar(mip, 0); //calculates the required exponent mul2_brick(mip, (ebrick2*)ebrick2Pointer, exponentB, x, y); epoint* p = new epoint(); p = epoint_init(mip); bool valid = epoint2_set(mip, x, y, 0, p); mirkill(x); mirkill(y); return (jlong)p; }
/* * Class: edu_biu_scapi_primitives_dlog_miracl_MiraclDlogECFp * Method: computeFpExponentiateWithPrecomputedValue * Signature: (JJ[B)J * * This function wraps the actual computation of the exponentation with precomputed values for the requested base for Dlog groups over Fp. It gets as a parameter * a pointer to the ebrick structure created by a previous call to initFpExponentiateWithPrecomputedValues. This implies that initFpExponentiateWithPrecomputedValues * MUST have been called prior to this function for the same base. */ JNIEXPORT jlong JNICALL Java_edu_biu_scapi_primitives_dlog_miracl_MiraclDlogECFp_computeFpExponentiateWithPrecomputedValues (JNIEnv * env, jobject, jlong m, jlong ebrickPointer, jbyteArray exponent){ //translate parameters to miracl notation miracl* mip = (miracl*)m; big exponentB = byteArrayToMiraclBig(env, mip, exponent); //(x,y) are the coordinates of the point which is the result of the exponentiation big x, y; x = mirvar(mip, 0); y = mirvar(mip, 0); //calculates the required exponent mul_brick(mip, (ebrick*)ebrickPointer, exponentB, x, y); //printf("The result of mul_brick(mip, exponentiations, exponent, x, y) is x=%d, y=%d\n", (*x).w,(*y).w); epoint* p = new epoint(); p = epoint_init(mip); epoint_set(mip, x, y, 0, p); mirkill(x); mirkill(y); return (jlong)p; }
/* * Class: edu_biu_scapi_primitives_dlog_miracl_MiraclDlogECF2m * Method: initF2mExponentiateWithPrecomputedValues * Signature: (JIIII[B[BJ[BII)J * * This function wraps the creation of an ebrick structure used to precompute exponentiations for a certain base for Dlog groups over Fp. It returns * a pointer to the ebrick structure which will be kept by the calling application (edu.biu.scapi...) in some data structure and will * be used for further calls to exponentiations with the same base. */ JNIEXPORT jlong JNICALL Java_edu_biu_scapi_primitives_dlog_miracl_MiraclDlogECF2m_initF2mExponentiateWithPrecomputedValues (JNIEnv * env, jobject, jlong mipp, jint mod, jint k1, jint k2, jint k3, jbyteArray a, jbyteArray b, jlong base, jint window, jint maxBits){ //translate parameters to miracl notation miracl* mip = (miracl*)mipp; big aB = byteArrayToMiraclBig(env, mip, a); big bB = byteArrayToMiraclBig(env, mip, b); //Get the coordinates (x,y) from the requested base point: big x, y; x = mirvar(mip, 0); y = mirvar(mip, 0); epoint2_get(mip, (epoint*)base, x, y); //Create a new structure to hold the precomputed values for given base and exponent ebrick2* exponentiations = new ebrick2(); //Perform precomputation ebrick2_init(mip, exponentiations, x, y, aB, bB, mod, k1, k2, k3, window, maxBits); //clean up mirkill(aB); mirkill(bB); //May be clan up also x and y mirkill(x); mirkill(y); //Return the pointer to the structure where the precomputed values are held return (jlong)exponentiations; }
/* calculate a pubKey out of a given priKey */ int SM2_standard_sign_keygeneration(unsigned char PriKey[], unsigned char Px[], unsigned char Py[]) { int i = 0; big d, PAx, PAy; epoint *PA; SM2_standard_init(); PA = epoint_init(); d = mirvar(0); PAx = mirvar(0); PAy = mirvar(0); bytes_to_big(SM2_NUMWORD, PriKey, d); ecurve_mult(d, G, PA); epoint_get(PA, PAx, PAy); big_to_bytes(SM2_NUMWORD, PAx, Px, TRUE); big_to_bytes(SM2_NUMWORD, PAy, Py, TRUE); i = Test_PubKey(PA); if (i) return i; else return 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; }
/* function validateF2mGenerator : This function checks if the accepted point is the generator of EC over F2m, by compare its values to the accepted x,y values * param m : miracl pointer * param generator : ellitic curve point to check * param xVal : x value of the generator * param yVal : y value of the generator * return : true if the generator is valid or not */ JNIEXPORT jboolean JNICALL Java_edu_biu_scapi_primitives_dlog_miracl_MiraclAdapterDlogEC_validateF2mGenerator (JNIEnv *env, jobject obj, jlong m, jlong generator, jbyteArray xVal, jbyteArray yVal){ /* convert the accepted parameters to MIRACL parameters*/ miracl* mip = (miracl*)m; big x = byteArrayToMiraclBig(env, mip, xVal); big y = byteArrayToMiraclBig(env, mip, yVal); /* get the point's x,y values */ big genX, genY; jboolean result; genX= mirvar(mip, 0); genY= mirvar(mip, 0); epoint2_get(mip, (epoint*)generator, genX, genY); /* check if the values are as expected, return the result */ if (mr_compare(genX, x)==0 && mr_compare(genY, y)==0) result = 1; else result = 0; mirkill(x); mirkill(y); mirkill(genX); mirkill(genY); return result; }
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; }
/*! \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; }
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; }
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; }
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; }
// Input: MyPrivKey = Your private key // HisPubKey = Someones public key // Output: MyPrivKey has been destroyed for security reasons // HisPubKey = the secret key int DH1080_comp(char *MyPrivKey, char *HisPubKey) { int i=0, len, iRet; unsigned char SHA256digest[35], base64_tmp[160]; big b_myPrivkey, b_HisPubkey, b_theKey; // Verify base64 strings if((strspn(MyPrivKey, B64ABC) != strlen(MyPrivKey)) || (strspn(HisPubKey, B64ABC) != strlen(HisPubKey))) { memset(MyPrivKey, 0x20, strlen(MyPrivKey)); memset(HisPubKey, 0x20, strlen(HisPubKey)); return 0; } b_HisPubkey=mirvar(0); b_theKey=mirvar(0); len=b64toh(HisPubKey, base64_tmp); bytes_to_big(len, base64_tmp, b_HisPubkey); if(DH_verifyPubKey(b_HisPubkey)) { b_myPrivkey=mirvar(0); len=b64toh(MyPrivKey, base64_tmp); bytes_to_big(len, base64_tmp, b_myPrivkey); memset(MyPrivKey, 0x20, strlen(MyPrivKey)); powmod(b_HisPubkey, b_myPrivkey, b_prime1080, b_theKey); mirkill(b_myPrivkey); len=big_to_bytes(sizeof(base64_tmp), b_theKey, base64_tmp, FALSE); SHA256_memory(base64_tmp, len, SHA256digest); htob64(SHA256digest, HisPubKey, 32); iRet=1; } else iRet=0; ZeroMemory(base64_tmp, sizeof(base64_tmp)); ZeroMemory(SHA256digest, sizeof(SHA256digest)); mirkill(b_theKey); mirkill(b_HisPubkey); return iRet; }
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; }
void mcl_ecpbs_import_pk(mcl_ecpbs_pk *pk) { FILE *fp; int compressed_y; big x; fp = fopen("keys/ec160.public", "rt"); if (fp == NULL) { printf("file ec.public does not exist\n"); exit(0); } x = mirvar(0); /* import the compressed y value */ fscanf(fp, "%d", &compressed_y); /* import the x coordinate on the curve */ innum(x, fp); fclose(fp); /* check if x is valid on the curve */ if (!epoint_x(x)) { printf( "Problem - imported x value of the public key is not on the active curve\n"); exit(0); } /* decompress point */ if (!epoint_set(x, x, compressed_y, pk->key)) { printf("Problem - public key point (x,y) is not on the curve\n"); exit(0); } mirkill(x); }
ZR SmallExp(int bits) { big t = mirvar(0); bigbits(bits, t); ZR zr(t); mr_free(t); return zr; }
void PairingGroup::init(ZR & r, char *value) { big x = mirvar(0); cinstr(x, value); r = ZR(x); //should copy this mr_free(x); }
void PairingGroup::init(ZR & r, int value) { big x = mirvar(value); r = ZR(x); //should copy this mr_free(x); return; }
/* * computes the loop of the algorithm. * for k=0 to h-1 * e=0 * for i=kw to kw+w-1 * if the bitIndex bit in ci is set: * calculate e += 2^(i-kw) * result = result *preComp[k][e] */ epoint* computeLoop(miracl* mip, big* exponentiations, int w, int h, epoint*** preComp, epoint* result, int bitIndex, int n, int field){ int e = 0, k, i, twoPow; big temp = mirvar(mip, 0); for (k=0; k<h; k++){ for (i=k*w; i<(k * w + w); i++){ if (i < n){ copy(exponentiations[i], temp); //check if the bit in bitIndex is set. //shift the big number bitIndex times sftbit(mip, temp, bitIndex*-1, temp); //check if the shifted big is divisible by two. if not - the first bit is set. if (subdivisible(mip, temp, 2) == 0){ twoPow = pow((double)2, i-k*w); e += twoPow; } } } //multiply operation depends on the field if (field == 1) ecurve_add(mip, preComp[k][e], result); else ecurve2_add(mip, preComp[k][e], result); e = 0; } mirkill(temp); return result; }
ZR PairingGroup::init(ZR_type t, int value) { big x = mirvar(value); ZR zr(x); // = new ZR(x); mr_free(x); return zr; }
/* * Returns the identity point */ epoint* getIdentity(miracl* mip, int field){ big x,y; epoint* identity = epoint_init(mip); x = mirvar(mip, 0); y = mirvar(mip, 0); //creation of the point depends on the field type if (field == 1) epoint_set(mip, x, y, 0, identity); else epoint2_set(mip, x, y, 0, identity); mirkill(x); mirkill(y); return identity; }
void ecurve_init(_MIPD_ big a,big b,big p,int type) { /* Initialize the active ecurve * * Asize indicate size of A * * Bsize indicate size of B */ int as; #ifndef MR_GENERIC_MT miracl *mr_mip=get_mip(); #endif if (mr_mip->ERNUM) return; MR_IN(93) prepare_monty(_MIPP_ p); mr_mip->Asize=size(a); if (mr_abs(mr_mip->Asize)==MR_TOOBIG) { if (mr_mip->Asize>=0) { /* big positive number - check it isn't minus something small */ copy(a,mr_mip->w1); divide(_MIPP_ mr_mip->w1,p,p); subtract(_MIPP_ p,mr_mip->w1,mr_mip->w1); as=size(mr_mip->w1); if (as<MR_TOOBIG) mr_mip->Asize=-as; else { if (mr_mip->A==NULL) mr_mip->A=mirvar(_MIPP_ 0); nres(_MIPP_ a,mr_mip->A); } } else { if (mr_mip->A==NULL) mr_mip->A=mirvar(_MIPP_ 0); nres(_MIPP_ a,mr_mip->A); } } mr_mip->Bsize=size(b); if (mr_abs(mr_mip->Bsize)==MR_TOOBIG) { if (mr_mip->B==NULL) mr_mip->B=mirvar(_MIPP_ 0); nres(_MIPP_ b,mr_mip->B); } mr_mip->coord=type; MR_OUT return; }
ZR ceillog(int base, int value) { // logb(x) ==> log(x) / log(b) big x = mirvar((int) ceil(log10(value) / log10(base))); ZR zr(x); mr_free(x); return zr; }
/* test if the big x is zero */ int Test_Zero(big x) { big zero; zero = mirvar(0); if (mr_compare(x, zero) == 0) return 1; else return 0; }
/* function createInfinityF2mPoint : This function creates the infinity point in F2m * param m : miracl pointer * return : true if the point is on the curve, false otherwise */ JNIEXPORT jlong JNICALL Java_edu_biu_scapi_primitives_dlog_miracl_MiraclDlogECF2m_createInfinityF2mPoint (JNIEnv *env, jobject obj, jlong m){ /* convert the accepted parameters to MIRACL parameters*/ miracl* mip = (miracl*)m; //create a point with the coordinates 0,0 which is the infinity point in miracl implementation big x,y; epoint* p = epoint_init(mip); x = mirvar(mip, 0); y = mirvar(mip, 0); epoint2_set(mip, x, y, 0, (epoint*)p); mirkill(x); mirkill(y); return (jlong) p; }
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; }
epoint* epoint_init(_MIPDO_ ) { /* initialise epoint to point at infinity. */ epoint *p; #ifndef MR_GENERIC_MT miracl *mr_mip=get_mip(); #endif if (mr_mip->ERNUM) return NULL; MR_IN(96) p=mr_alloc(_MIPP_ 1,sizeof(epoint)); p->X=mirvar(_MIPP_ 0); p->Y=mirvar(_MIPP_ 0); p->Z=NULL; /* only initialise when written to */ p->marker=MR_EPOINT_INFINITY; MR_OUT return p; }
/* function getXValue : This function return the x coordinate of the given point * param m : pointer to mip * param point : pointer to the point * return : the x coordinate of the given point */ JNIEXPORT jbyteArray JNICALL Java_edu_biu_scapi_primitives_dlog_miracl_ECF2mPointMiracl_getXValueF2mPoint (JNIEnv *env, jobject obj, jlong m, jlong point){ /* convert the accepted parameters to MIRACL parameters*/ miracl* mip = (miracl*)m; big x, y; jbyteArray xBytes; x= mirvar(mip, 0); y= mirvar(mip, 0); //get x, y values of the point epoint2_get(mip, (epoint*)point, x, y); xBytes = miraclBigToJbyteArray(env, mip, x); mirkill(x); mirkill(y); //return the bytes of x return xBytes; }
/* function isFpMember : This function checks if the accepted point is a point of the current elliptic curve (over Fp) * param m : miracl pointer * param point : ellitic curve point to check * return : true if the point is on the curve, false otherwise */ JNIEXPORT jboolean JNICALL Java_edu_biu_scapi_primitives_dlog_miracl_MiraclDlogECFp_isFpMember (JNIEnv *env, jobject obj, jlong m, jlong point){ int member = 0; /* convert the accepted parameters to MIRACL parameters*/ miracl* mip = (miracl*)m; /* get the x,y, values of the point */ big x,y; epoint* p = epoint_init(mip); x = mirvar(mip, 0); y = mirvar(mip, 0); epoint_get(mip, (epoint*)point, x, y); /* try to create another point with those values. if succeded - the point is in the curve */ if (epoint_set(mip, x, y, 0, p)==1) member = 1; mirkill(x); mirkill(y); return member; }
/* function invertF2mPoint : This function return the inverse of ec point * param m : miracl pointer * param p1 : ellitic curve point * return : the inverse point */ JNIEXPORT jlong JNICALL Java_edu_biu_scapi_primitives_dlog_miracl_MiraclDlogECF2m_invertF2mPoint (JNIEnv *env, jobject obj, jlong m, jlong p1){ /* convert the accepted parameters to MIRACL parameters*/ miracl* mip = (miracl*)m; big x, y; epoint* p2; x= mirvar(mip, 0); y= mirvar(mip, 0); //init the result point and copy p1 values to it p2 = epoint_init(mip); epoint2_get(mip, (epoint*)p1, x, y); epoint2_set(mip, x,y,0, p2); mirkill(x); mirkill(y); //inverse the point epoint2_negate(mip, p2); return (jlong)p2; // return the inverse }
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; }