Exemplo n.º 1
0
void PFC::restore(char * bytes,G2& w)
{
	int i,j,len,m;
	int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1);
	
	ZZn a,b,c;
	Big X=*x;
	if (w.ptable!=NULL) return;

	m=2*(bits(X)-2+ham(X));
	len=m*3*bytes_per_big;

	w.ptable=new ZZn3[m];
	for (i=j=0;i<m;i++)
	{
		a=from_binary(bytes_per_big,&bytes[j]);
		j+=bytes_per_big;
		b=from_binary(bytes_per_big,&bytes[j]);
		j+=bytes_per_big;
		c=from_binary(bytes_per_big,&bytes[j]);
		j+=bytes_per_big;
		w.ptable[i].set(a,b,c);
	}
	for (i=0;i<len;i++) bytes[i]=0;
	
	delete [] bytes;
}
Exemplo n.º 2
0
void G2::restore(char *bytes)
{
	int i,j,n=(1<<WINDOW_SIZE);
	int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1);
	int len=n*4*bytes_per_big;
	ZZn2 a,b;
	Big x,y;
	if (mtable!=NULL) return;

	mtable=new ECn2[1<<WINDOW_SIZE];
	for (i=j=0;i<n;i++)
	{
		x=from_binary(bytes_per_big,&bytes[j]);
		j+=bytes_per_big;
		y=from_binary(bytes_per_big,&bytes[j]);
		j+=bytes_per_big;
		a.set(x,y);
		x=from_binary(bytes_per_big,&bytes[j]);
		j+=bytes_per_big;
		y=from_binary(bytes_per_big,&bytes[j]);
		j+=bytes_per_big;
		b.set(x,y);
		mtable[i].set(a,b);
	}
	delete [] bytes;
}
Exemplo n.º 3
0
void G2::restore(char *bytes)
{
	int i,j,n=(1<<WINDOW_SIZE);
	int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1);
	int len=n*2*bytes_per_big;
	Big x,y,B;
	if (mtable!=NULL) return;

	mtable=new ECn[1<<WINDOW_SIZE];
	B=getB();
	B=-B;
	ecurve((Big)-3,B,get_modulus(),MR_PROJECTIVE);  // move to twist	
	for (i=j=0;i<n;i++)
	{
		x=from_binary(bytes_per_big,&bytes[j]);
		j+=bytes_per_big;
		y=from_binary(bytes_per_big,&bytes[j]);
		j+=bytes_per_big;

		mtable[i].set(x,y);

	}
	B=-B;
	ecurve((Big)-3,B,get_modulus(),MR_PROJECTIVE);  // move back
	delete [] bytes;
}
Exemplo n.º 4
0
void PFC::restore(char * bytes,G2& w)
{
	int i,j,len,m;
	int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1);
	
	Big a,b,n;
	Big X=*x;
	if (w.ptable!=NULL) return;

	if (X<0) n=-(6*X+2);
    else n=6*X+2;

	m=2*(bits(n)+ham(n));  // number of entries in ptable
	len=m*2*bytes_per_big;

	w.ptable=new ZZn2[m];
	for (i=j=0;i<m;i++)
	{
		a=from_binary(bytes_per_big,&bytes[j]);
		j+=bytes_per_big;
		b=from_binary(bytes_per_big,&bytes[j]);
		j+=bytes_per_big;
		w.ptable[i].set(a,b);
	}
	for (i=0;i<len;i++) bytes[i]=0;
	
	delete [] bytes;
}
Exemplo n.º 5
0
Big H2(ZZn12 x)
{ // Compress and hash an Fp12 to a big number
    sha256 sh;
    ZZn4 u;
    ZZn2 h,l;
    Big a,hash,p,xx[4];
    char s[HASH_LEN];
    int i,j,m;

    shs256_init(&sh);
    x.get(u);  // compress to single ZZn4
    u.get(l,h);
    xx[0]=real(l); xx[1]=imaginary(l); xx[2]=real(h); xx[3]=imaginary(h);
 
    for (i=0;i<4;i++)
    {
        a=xx[i];
        while (a>0)
        {
            m=a%256;
            shs256_process(&sh,m);
            a/=256;
        }
    }
    shs256_hash(&sh,s);
    hash=from_binary(HASH_LEN,s);
    return hash;
}
Exemplo n.º 6
0
Big H2(ZZn6 y)
{ // Hash and compress an Fp6 to a big number
    sha sh;
    ZZn u,v,w;
	ZZn2 x;
    Big a,h,p,xx[2];
    char s[HASH_LEN];
    int i,j,m;

    shs_init(&sh);
	y.get(x);
    x.get(u,v);
    xx[0]=u; xx[1]=v;
   
    for (i=0;i<2;i++)
    {
        a=xx[i];
        while (a>0)
        {
            m=a%256;
            shs_process(&sh,m);
            a/=256;
        }
    }
    shs_hash(&sh,s);
    h=from_binary(HASH_LEN,s);
    return h;
}
Exemplo n.º 7
0
int G2::spill(char *& bytes)
{
	int i,j,n=(1<<WINDOW_SIZE);
	int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1);
	int len=n*2*bytes_per_big;
	Big x,y;

	if (mtable==NULL) return 0;

	bytes=new char[len];

	for (i=j=0;i<n;i++)
	{
		mtable[i].get(x,y);		
		to_binary(x,bytes_per_big,&bytes[j],TRUE);
		x=from_binary(bytes_per_big,&bytes[j]);

		j+=bytes_per_big;
		to_binary(y,bytes_per_big,&bytes[j],TRUE);
		j+=bytes_per_big;
	}
	delete [] mtable; 
	mtable=NULL;
	return len;
}
Exemplo n.º 8
0
Big H2(ZZn18 x)
{ // Compress and hash an Fp18 to a big number
    sha256 sh;
    ZZn6 u;
    ZZn3 h,l;
    Big a,hash;
	ZZn xx[6];
    char s[HASH_LEN];
    int i,j,m;

    shs256_init(&sh);
    x.get(u);  // compress to single ZZn6
    u.get(l,h);
	l.get(xx[0],xx[1],xx[2]);
	h.get(xx[3],xx[4],xx[5]);
    
    for (i=0;i<6;i++)
    {
        a=(Big)xx[i];
        while (a>0)
        {
            m=a%256;
            shs256_process(&sh,m);
            a/=256;
        }
    }
    shs256_hash(&sh,s);
    hash=from_binary(HASH_LEN,s);
    return hash;
}
Exemplo n.º 9
0
Big bytesToBig(string str, int *counter)
{
	int pos = str.find_first_of(':');
	int max = MAX_LEN;
//	cout << "pos of elem => " << pos << endl;
	int len = atoi( str.substr(0, pos).c_str() );
	int add_zeroes = PAD_SIZE;
	const char *elem = str.substr(pos+1, pos + len).c_str();
	char elem2[max + 1];
	memset(elem2, 0, max);
	// right justify elem2 before call to 'from_binary'
	if(len < BIG_SIZE) {
		// need to prepend additional zero's
		add_zeroes += (BIG_SIZE - len);
	}

	for(int i = add_zeroes; i < BIG_SIZE + add_zeroes; i++)
		elem2[i] = elem[i-add_zeroes];

//	printf("bytesToBig before => ");
//	_printf_buffer_as_hex((uint8_t *) elem, len);
//	cout << "len => " << len << endl;
//	printf("bytesToBig before2 => ");
//	_printf_buffer_as_hex((uint8_t *) elem2, MAX_LEN);
	Big x = from_binary(max, (char *) elem2);
	*counter  = pos + len + 1;
	return x;
}
Exemplo n.º 10
0
Big H2(GF2m4x x)
{ 
    sha256 sh;
    Big a,hash;
	GF2m xx[4];
    char s[HASH_LEN];
    int i,j,m;

    shs256_init(&sh);
    x.get(xx[0],xx[1],xx[2],xx[3]); 
 
    for (i=0;i<4;i++)
    {
        a=xx[i];
        while (a>0)
        {
            m=a%256;
            shs256_process(&sh,m);
            a/=256;
        }
    }
    shs256_hash(&sh,s);
    hash=from_binary(HASH_LEN,s);
    return hash;
}
Exemplo n.º 11
0
Big PFC::finish_hash_to_group(void)
{
	Big hash;
	char s[HASH_LEN];
    shs256_hash(&SH,s);
    hash=from_binary(HASH_LEN,s);
	return hash%(*ord);
}
Exemplo n.º 12
0
Big PFC::finish_hash_to_group(void)
{
	Big hash;
	Big o=pow((Big)2,2*S);
	char s[HASH_LEN];
    shs256_hash(&SH,s);
    hash=from_binary(HASH_LEN,s);
	return hash%o;
}
Exemplo n.º 13
0
Big PFC::finish_hash_to_aes_key(void)
{
	Big hash;
	char s[HASH_LEN];
    shs256_hash(&SH,s);
	Big m=pow((Big)2,S);
    hash=from_binary(HASH_LEN,s);
	return hash%m;
}
Exemplo n.º 14
0
void G2::restore(char *bytes)
{
	int i,j,n=(1<<WINDOW_SIZE);
	int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1);
	int len=n*6*bytes_per_big;
	ZZn3 x,y;
	ZZn a,b,c;
	if (mtable!=NULL) return;

	mtable=new ECn3[1<<WINDOW_SIZE];
	for (i=j=0;i<n;i++)
	{
		a=from_binary(bytes_per_big,&bytes[j]);
		j+=bytes_per_big;
		b=from_binary(bytes_per_big,&bytes[j]);
		j+=bytes_per_big;
		c=from_binary(bytes_per_big,&bytes[j]);
		j+=bytes_per_big;
		x.set(a,b,c);
		a=from_binary(bytes_per_big,&bytes[j]);
		j+=bytes_per_big;
		b=from_binary(bytes_per_big,&bytes[j]);
		j+=bytes_per_big;
		c=from_binary(bytes_per_big,&bytes[j]);
		j+=bytes_per_big;
		y.set(a,b,c);
		mtable[i].set(x,y);
	}
	delete [] bytes;
}
Exemplo n.º 15
0
static Big hash(ifstream &fp)
{ /* compute hash function */
    char ch,s[20];
    char uch;
    int i;
    Big h;
    sha_r sh;
    shs_init(&sh);
    forever 
    { /* read in bytes from message file */
        fp.get(ch);
        if (fp.eof()) break;
        shs_process(&sh,ch);
    }
    shs_hash(&sh,s);
    h=from_binary(20,s);
    return h;
}
Exemplo n.º 16
0
void PFC::restore(char * bytes,G1& w)
{
	int i,j,n=2*(bits(*ord-1)-2+ham(*ord));
	int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1);
	int len=n*bytes_per_big;
	Big x;

	if (w.ptable!=NULL) return;

	w.ptable=new ZZn[n];
	for (i=j=0;i<n;i++)
	{
		x=from_binary(bytes_per_big,&bytes[j]);
		w.ptable[i]=x;
		j+=bytes_per_big;
	}
	for (i=0;i<len;i++) bytes[i]=0;
	delete [] bytes;
}
Exemplo n.º 17
0
int main()
try {
    cout << "Enter input file name: ";
    string iname;
    cin >> iname;
    to_binary(iname);

    cout << "Enter output file name: ";
    string oname;
    cin >> oname;
    from_binary(oname);

    cout << "Done!\n";
}
catch (exception& e) {
    cerr << "exception: " << e.what() << endl;
}
catch (...) {
    cerr << "exception\n";
}
Exemplo n.º 18
0
Big H2(ZZn2 y)
{ // Hash and compress an Fp2 to a big number
    sha256 sh;
    Big a,h;
    char s[HASH_LEN];
    int m;

    shs256_init(&sh);
	y.get(a);
   
    while (a>0)
    {
        m=a%256;
        shs256_process(&sh,m);
        a/=256;
    }
    shs256_hash(&sh,s);
    h=from_binary(HASH_LEN,s);
    return h;
}
Exemplo n.º 19
0
void GT::restore(char *bytes)
{
	int i,j,n=(1<<WINDOW_SIZE);
	int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1);
	int len=n*12*bytes_per_big;
	ZZn4 a,b,c;
	ZZn2 f,s;
	Big x,y;
	if (etable!=NULL) return;

	etable=new ZZn12[1<<WINDOW_SIZE];
	for (i=j=0;i<n;i++)
	{
		x=from_binary(bytes_per_big,&bytes[j]);
		j+=bytes_per_big;
		y=from_binary(bytes_per_big,&bytes[j]);
		j+=bytes_per_big;
		f.set(x,y);
		x=from_binary(bytes_per_big,&bytes[j]);
		j+=bytes_per_big;
		y=from_binary(bytes_per_big,&bytes[j]);
		j+=bytes_per_big;
		s.set(x,y);
		a.set(f,s);
		x=from_binary(bytes_per_big,&bytes[j]);
		j+=bytes_per_big;
		y=from_binary(bytes_per_big,&bytes[j]);
		j+=bytes_per_big;
		f.set(x,y);
		x=from_binary(bytes_per_big,&bytes[j]);
		j+=bytes_per_big;
		y=from_binary(bytes_per_big,&bytes[j]);
		j+=bytes_per_big;
		s.set(x,y);
		b.set(f,s);
		x=from_binary(bytes_per_big,&bytes[j]);
		j+=bytes_per_big;
		y=from_binary(bytes_per_big,&bytes[j]);
		j+=bytes_per_big;
		f.set(x,y);
		x=from_binary(bytes_per_big,&bytes[j]);
		j+=bytes_per_big;
		y=from_binary(bytes_per_big,&bytes[j]);
		j+=bytes_per_big;
		s.set(x,y);
		c.set(f,s);
		etable[i].set(a,b,c);
	}
	delete [] bytes;
}
void main()
{
    int a,b,c,d,w,n,o ;
    int x[32], y[32], z[32] ;
    
    o = 0 ;
    w = 9 ; /* word size in bits */
    /* note that x, y, and z must be at least as large as w and may be larger */
    
    b = 1 ; /* add one to each */
    for( c=250;c<260;c++ )
    {
        if ( c > 255 )  /* exceeds 8 bits */
            d = c-512 ;
        else
            d =  c ;
        printf( "%d %d ", d,b ) ;
        to_binary( d,w,x,&o ) ;
        if ( o )
            printf( "%d Overflow in to_binary! ",d ) ;
        to_binary( b,w,y,&o ) ;
        if ( o )
            printf( "%d Overflow in to_binary! ",b ) ;
        adder( x,y,z,&o,w ) ;
        from_binary( z,w,&a ) ;
        printf( "%d ", a ) ;
        if ( o )
            printf( "Overflow in adder! " ) ;
        printf( "\n" ) ;
    }
    
    b = -1 ; /* add -1 to each */
    for( c=250;c<260;c++ )
    {
        if ( c > 255 )
            d = c-512 ;
        else
            d =  c ;
        printf( "%d %d ", d,b ) ;
        to_binary( d,w,x,&o ) ;
        if ( o )
            printf( "%d Overflow in to_binary! ",d ) ;
        to_binary( b,w,y,&o ) ;
        if ( o )
            printf( "%d Overflow in to_binary! ",d ) ;
        adder( x,y,z,&o,w ) ;
        from_binary( z,w,&a ) ;
        printf( "%d ", a ) ;
        if ( o )
            printf( "Overflow in adder! " ) ;
        printf( "\n" ) ;
    }
    
    
    for( w=8; w<13; w=w+4 )          /* word sizes 8 and 12 bits */
        for( a=99; a >-100; a=a-99 )  /* 99, 0, -99 */
            for( b=4; b>-5; b=b-4 )    /* 4, 0, -4 */
            {
                to_binary( a,w,x,&o ) ;
                if ( o )
                    printf( "%d Overflow in to_binary! ",a ) ;
                to_binary( b,w,y,&o ) ;
                if ( o )
                    printf( "%d Overflow in to_binary! ",b ) ;
                mult( x,y,z,&o,w ) ;
                printf( "%d %d ", a,b ) ;
                from_binary( z,w,&n ) ;
                printf( "%d ", n ) ;
                if ( o )
                    printf( "Overflow in multiplier! " ) ;
                printf( "\n" ) ;
            }
}
void mult ( int *x, int *y, int *z, int *o, int w )
{
    /* x is an input array of w ints, either 0 or 1 representing the first multiplicand */
    /* y is an input array of w ints, either 0 or 1 representing the second multicand */
    /* z is an output array of w ints, either 0 or 1 representing the product of x and y */
    /* o is an output = 1 if an overflow occurred */
    /* w is an input = to the width in bits of x, y, z */
    
    int resultAdders[w][w]; // have a 2 D array to store multiplication result later to be added
    int placeHolder;
    
    // create results adder
    for (int i = 0; i < w; i++) {
        for (int j = 0; j < w; j++) {
            resultAdders[i][j] = 0;
        }
    }
    
    for (int i = 0; i < w; i++) {
        if (y[i]) {
            int k = 0;
            for (int j = i; j < w; j++) {
                resultAdders[i][j] = x[k];
                k++;
            }
        }
    }
    
    
    
    //    // testing
    //    for (int i = 0; i < w; i++) {
    //        for (int j = 0; j < w; j++) {
    //            int d = resultAdders[i][j];
    //            printf("%d", d);
    //        }
    //        printf("\n");
    //    }
    
    
    
    
    // create temp array with 0s
    for (int i = 0; i < w; i++) {
        z[i] = 0;
    }
    
    
    // Add the results
    for (int i = 0; i < w; i++) {
        adder(z, resultAdders[i], z, &placeHolder, w);
    }
    
    
    
    // overflow when sign rules are violated: neg * neg = neg, pos * pos = neg, pos * neg = pos, neg * pos  = pos.
    if (x[w-1] & y[w-1]) {
        if (x[w-1] & z[w-1]) { // overflow: neg * neg = neg
            *o = 1;
        }
        else
        {
            *o = 0;
        }
    } else if (x[w-1] ^ y[w-1]) {
        if (~x[w-1] & (y[w-1] ^ z[w-1])) { // overflow: pos * neg = pos
            *o = 1;
        }
        else if (~y[w-1] & (x[w-1] ^ z[w-1])) { // overflow: neg * pos = pos
            *o = 1;
        }
        else
        {
            *o = 0;
        }
        
    } else {
        if (x[w-1] ^ z[w-1]) { // overflow: pos * pos = neg
            *o = 1;
        }
        else
        {
            *o = 0;
        }
        
    }
    
    int isZeroX, isZeroY, isZeroZ;
    from_binary(x, w, &isZeroX);
    from_binary(y, w, &isZeroY);
    from_binary(z, w, &isZeroZ);
    if (isZeroX == 0 ||  isZeroY == 0) { // if either x or y is zero, the output should be zero
        if (isZeroZ == 0) {
            *o = 0;
        }
    }
}
Exemplo n.º 22
0
 raw_integer_t(const xint::binary_t &b, std::size_t bits = 0): length(1),
     negative(false), changed(false) { from_binary(b, bits); }