#include #include #include "platform.h" #include "xparameters.h" #include "xio.h" #include #include #include /*int main() { init_platform(); print("Hello World\n\r"); cleanup_platform(); return 0; }*/ typedef unsigned long long field_t; #define FIELD_M 7 #define FIELD_W 3 #define MSB 31 #define FIELD_P ((1UL << MSB) - 1) #define BYTESIZE (MSB * FIELD_M / 8 + 1) #define MULALGO0 static field_t con_one[FIELD_M] = {1, 0x68768cde, 0x5f6ca339, 0x5b9f5d37, 0x354d412e, 0x5f57d0ba, 0x47d860c5}; field_t CURVECONST_A[FIELD_M] ={2147483644,0,0,0,0,0,0}; field_t CURVECONST_B[FIELD_M] ={ 2147483435,0,0,0,0,0,0}; field_t CURVE_PT_X[FIELD_M] ={ 1265161714,1539753404,708434454,1832286394,1731473315,1007441496,1511235565}; field_t CURVE_PT_Y[FIELD_M] ={373113286, 1537269188,1290843001,1831071987,1155799427,2060698802,573726986}; typedef struct _point_t{ field_t x[FIELD_M]; field_t y[FIELD_M]; field_t z[FIELD_M]; // used only in projective coordinate system, else ignored }point_t; unsigned int timestamp(void) { unsigned int bottom; unsigned int top; asm volatile("xorl %%eax,%%eax\n cpuid \n" ::: "%eax", "%ebx", "%ecx", "%edx"); // flush pipeline asm volatile("rdtsc\n" : "=a" (bottom), "=d" (top) ); // read rdtsc asm volatile("xorl %%eax,%%eax\n cpuid \n" ::: "%eax", "%ebx", "%ecx", "%edx"); // flush pipeline again return bottom; } void copy_element(field_t *d, field_t *s) { memcpy(d, s, FIELD_M*sizeof(field_t)); } void init_element(field_t a[5]) { memset(a, 0, FIELD_M*sizeof(field_t)); } static void fillzeros(point_t *dest) { memset(dest, 0, sizeof(point_t)); } void copypoint(point_t *dest, point_t *src) { memcpy(dest, src, sizeof(point_t)); } field_t baseadd(field_t a, field_t b) { field_t c1 = a + b; field_t c2 = c1 - FIELD_P; return ((c1 >= FIELD_P) ? c2 : c1); } static field_t basesub(field_t a, field_t b) { field_t c; c = (baseadd(a, FIELD_P - b)); return c; } /*#if defined(MULALGO0) static void sanitycheck() { if(FIELD_P >= (1U<<31)-1){ print("Wrong Multiplier Configured\n"); //exit(-1); } }*/ /* * This function can be used when the field cardinality is less than 2^32 */ static field_t basemul(field_t a, field_t b) { unsigned long long pr, q1; pr = a * (unsigned long long)b; q1 = pr / FIELD_P; // printf("%016llx\n", pr); return (pr - (q1 * FIELD_P)); } static field_t basemulx2(field_t a) { field_t msb = a >> (MSB - 1); a = a << 1; return (a & FIELD_P) + msb; } /* Constant multiplication with 3 */ inline static field_t basemulx3(field_t a) { field_t a2; field_t msb = a >> (MSB - 1); a2 = ((a << 1) + msb) & FIELD_P; return baseadd(a2 , a); } /* Constant multiplication with 4 */ inline static field_t basemulx4(field_t a) { field_t msb = a >> (MSB - 2); a = a << 2; return (a & FIELD_P) + msb; } /* Constant multiplication with 8 */ inline static field_t basemulx8(field_t a) { field_t msb = a >> (MSB - 3); a = a << 3; return (a & FIELD_P) + msb; } #define ISEVEN(x) ((x & 1ULL) == 0) /* Binary Euclidean Algorithm to find the Inverse of element a in GF(FIELD_P) */ static field_t baseinverse(field_t a) { field_t u, v, e; field_t x1, x2; field_t Pby2; if(a == 0) return 0; Pby2 = (FIELD_P + 1) >> 1; u = a; v = FIELD_P; x1 = 1ULL; x2 = 0ULL; while(u != 1 && v != 1){ while(ISEVEN(u)){ u = u >> 1; if (ISEVEN(x1)) x1 = x1 >> 1; else{ x1 = ((x1 - 1) >> 1) + Pby2; } } while(ISEVEN(v)){ v = v >> 1; if (ISEVEN(x2)) x2 = x2 >> 1; else{ x2 = ((x2 - 1) >> 1) + Pby2; } } if(u >= v){ u = basesub(u, v); x1 = basesub(x1, x2); }else{ v = basesub(v, u); x2 = basesub(x2,x1); } } if (u == 1){ return x1 ; } else{ return x2; } } static void oefadd(field_t *res, field_t *a, field_t *b) { int i; for(i=0; iy); // a = y1^2 /* B <- [[4 . x1] . A] */ oefmulx4(temp1, pt->x); // 4.x1 oefmul(b, temp1, a); // temp1.a /* D <- (3 . X1^2) + (CURVE_A . z1^4) */ oefsqr(c, pt->x); // x1^2 oefmulx3(temp1, c); // 3.x1^2 oefsqr(temp2, pt->z); // z1^2 oefsqr(c, temp2); // z1^4 oefmul(temp2, CURVECONST_A, c); // CURVE_A.z1^4 oefadd(d, temp2, temp1); // 3.z1^2 + CURVE_A.z1^4 /* C <- [8 . [A^2]] */ oefsqr(temp1, a); // a^2 oefmulx8(c, temp1); // 8.a^2 /* X3 <- [[D^2] - [2B]] */ oefsqr(temp1, d); // d^2 oefmulx2(temp2, b); // 2.b oefsub(pt->x, temp1, temp2); // d^2 - 2b /* Z3 <- [2[Y1 . Z1]] */ oefmul(temp1, pt->y, pt->z);// y1.z1 oefmulx2(pt->z, temp1); // 2.y1.z1 /* Y3 <- [[D(B - X3)] - C] */ oefsub(temp1, b, pt->x); // b - x3 oefmul(temp2, temp1, d); // d(b - x3) oefsub(pt->y, temp2, c); // d(b - x3) - c } static void pt_add(point_t *pt1, point_t *pt2) { field_t a[FIELD_M], b[FIELD_M], c[FIELD_M], d[FIELD_M], e[FIELD_M]; field_t f[FIELD_M], g[FIELD_M], h[FIELD_M], i[FIELD_M]; oefsqr(a, pt1->z); // a = z1^2 oefmul(b, pt1->z, a); // b = z1.a oefmul(c, pt2->x, a); // c = x2.a oefmul(d, pt2->y, b); // d = y2.b oefsub(e, c, pt1->x); // e = c - x1 oefsub(f, d, pt1->y); // f = d - y1 oefsqr(g, e); // g = e^2 oefmul(h, g, e); // h = g.e oefmul(i, g, pt1->x); // i = x1.g // x3 = f^2 - (h + 2i) oefsqr(a, f); // f^2 oefsub(b, a, h); // f^2 - h oefmulx2(c, i); // 2i oefsub(pt1->x, b, c); // f^2 - h - 2i // y3 = f(i - x3) - y1.h oefsub(a, i, pt1->x); // i - x3 oefmul(b, f, a); // f(i-x3) oefmul(c, h, pt1->y); // y1.h oefsub(pt1->y, b, c); // f(i-x3) - y1.h // z3 = z1.e copy_element(d, pt1->z); oefmul(pt1->z, e, d); // e.z1 } void smul(field_t *k, point_t *basepoint, point_t *res) { int word, msb, i, j, found=0; int malloced; //int deb,seal; //Xuint32 status; //XGpio pin; //status=XGpio_Initialize(&pin,XPAR_GENERIC_GPIO_DEVICE_ID); //if (status != XST_SUCCESS) //return XST_FAILURE; //XGpio_SetDataDirection(&pin,1,0x0); point_t q; field_t zinv[FIELD_M], zinv2[FIELD_M], zinv3[FIELD_M]; malloced = 0; if(basepoint == NULL){ malloced = 1; basepoint = (point_t *) malloc(sizeof(point_t)); // initilize basepoint copy_element(basepoint->x, CURVE_PT_X); copy_element(basepoint->y, CURVE_PT_Y); } init_element(basepoint->z); basepoint->z[0] = 1; // find the msb in k word = FIELD_M - 1; msb = sizeof(field_t)*8 - 1; while(1){ if(k[word] >> msb){ copypoint(&q, basepoint); break; } msb--; if(msb < 0){ msb = sizeof(field_t)*8-1; word--; if(word < 0){ /* We have not found a leading MSB so fill q with 0s */ fillzeros(&q); break; } } } // double and add for(i=word; i>=0; --i){ for(j=msb-1; j>=0; --j){ //seal=1; //XGpio_DiscreteWrite(&pin, 1, seal); pt_double(&q); //seal=!seal; //XGpio_DiscreteWrite(&pin, 1, seal); if((k[i] >> j) & 1){ //seal=!seal; //XGpio_DiscreteWrite(&pin, 1, seal); pt_add(&q, basepoint); //seal=!seal; //XGpio_DiscreteWrite(&pin, 1, seal); } } msb = sizeof(field_t)*8; } // convert to affine coordinates oefinverse(zinv, q.z); // zinv oefsqr(zinv2, zinv); // zinv^2 oefmul(zinv3, zinv2, zinv ); // zinv^3 oefmul(res->x, q.x, zinv2); // x.(zinv^2) oefmul(res->y, q.y, zinv3); // y.(zinv^3) init_element(res->z); res->z[0] = 1; if(malloced) free(basepoint); } int main() { // init_platform(); int x_cor,x_cor1; field_t alpha1[FIELD_M]={0xFFFF5555,0x8888DDDD,0,0,0,0,0}; point_t C1; int i; smul(alpha1,NULL,&C1); return 0; }