Example #1
0
void sample_NTL_poly_div1(unsigned long length, unsigned long bits,
                          void* arg, unsigned long count)
{

       
    ZZX poly1;
    ZZX poly2;
    ZZX poly3;
    ZZ a;
    poly1.SetMaxLength(length);
    poly2.SetMaxLength(length);
    poly3.SetMaxLength(2*length-1);


   
   
   unsigned long r_count;    // how often to generate new random data
   
   if (count >= 10000) r_count = 100;
   else if (count >= 100) r_count = 10;
   else if (count >= 20) r_count = 4;
   else if (count >= 8) r_count = 2;
   else r_count = 1;
   
   unsigned long i;
   for (i = 0; i < count; i++)
   {
      if (i%r_count == 0)
      {
	    do
        {
           unsigned long j;
           for (j = 0; j < length; j++)
		   {
		      RandomBits(a,bits);
		      SetCoeff(poly1,j,a);
		   }
        } while (IsZero(poly1));
        unsigned long j;
        for (j = 0; j < length; j++)
		{
           RandomBits(a,bits);
		   SetCoeff(poly2,j,a);
		}
      }
      
      mul(poly3, poly1, poly2);
      prof_start();
      unsigned long count2;
      for (count2 = 0; count2 < r_count; count2++)
      {
         divide(poly2, poly3, poly1);
      }
      prof_stop();
      
      i += (r_count-1);
   }  
}
Example #2
0
int main()
{
   setbuf(stdout, NULL);

   for (long l = 256; l <= 16384; l *= 2) {
      // for (long n = 256; n <= 16384; n *= 2) {
      for (long idx = 0; idx < 13; idx ++) {
         long n  = 256*(1L << idx/2);
         if (idx & 1) n += n/2;
           SetSeed((ZZ(l) << 64) + ZZ(n));

	   ZZX a, b, c;

           a.SetLength(n);
           for (long i = 0; i < n; i++) RandomBits(a[i], l);
           a.normalize();

           b.SetLength(n);
           for (long i = 0; i < n; i++) RandomBits(b[i], l);
           b.normalize();


	   double t;

	   mul(c, a, b);

	   long iter = 1;
	   do {
	      t = GetTime();
	      for (long i = 0; i < iter; i++) mul(c, a, b);
	      t = GetTime() - t;
	      iter *= 2;
	   } while (t < 3);
	   iter /= 2;

	   t = GetTime();
	   for (long i = 0; i < iter; i++) mul(c, a, b);
	   t = GetTime()-t;
	   double NTLTime = t;


	   FlintZZX f_a(a), f_b(b), f_c(c);


	   fmpz_poly_mul(f_c.value, f_a.value, f_b.value);
	   t = GetTime();
	   for (long i = 0; i < iter; i++) fmpz_poly_mul(f_c.value, f_a.value, f_b.value);
	   t = GetTime()-t;
	   double FlintTime = t;

           printf("%8.2f", FlintTime/NTLTime);
      }

      printf("\n");
   }
}
Example #3
0
void sample_NTL_poly_div2(unsigned long length, unsigned long bits,
                          void* arg, unsigned long count)
{

       
    ZZX poly1;
    ZZX poly2;
    ZZX poly3;
    ZZ a;
    poly1.SetMaxLength(length);
    poly2.SetMaxLength(length);
    poly3.SetMaxLength(2*length-1);


   
   
   unsigned long r_count;    // how often to generate new random data
   
   if (count >= 1000) r_count = 100;
   else if (count >= 100) r_count = 10;
   else if (count >= 20) r_count = 5;
   else if (count >= 8) r_count = 2;
   else r_count = 1;
   
   unsigned long i;
   for (i = 0; i < count; i++)
   {
      if (i%r_count == 0)
      {
	    unsigned long j;
	    for (j = 0; j<length-1; j++)
		{
		RandomBits(a,bits);
		SetCoeff(poly1,j,a);
		}
		SetCoeff(poly1,length-1,1);
	    unsigned long j;
	    for (j = 0; j<2*length-1; j++)
		{
		RandomBits(a,bits);
		SetCoeff(poly3,j,a);
		}
      }
       prof_start();
       div(poly2, poly3, poly1);
       prof_stop();
   }
   
   
}
Example #4
0
void random(RR& z)
{
   NTL_TLS_LOCAL(RR, t);
   RandomBits(t.x, RR::prec); 
   t.e = -RR::prec;
   normalize(z, t);
}
// creates a chromosome randomly selecting the genes.
BinaryChromosome::BinaryChromosome(int nbits) {
  random = Random::Instance();
  RandomBits rand = RandomBits();
  bitstring = rand.make_random_bits(nbits);
  this->nbits = nbits;
  trace = false;
}
Example #6
0
void random(RR& z)
{
   static RR t;
   RandomBits(t.x, RR::prec);
   t.e = -RR::prec;
   normalize(z, t);
}
Example #7
0
/**
 * Construct a proof.
 */
void constructProof(Credential *credential, unsigned char *masterSecret) {
  unsigned char i;
  unsigned int rA_size;
  unsigned int rA_offset;
  rA_size = realSize(credential->signature.v, SIZE_V) - 1 - realSize(credential->signature.e, SIZE_E);
  if (rA_size > SIZE_R_A) { rA_size = SIZE_R_A; }
  rA_offset = SIZE_R_A - rA_size;

  // Generate random values for m~[i], e~, v~ and rA
  for (i = 0; i <= credential->size; i++) {
    if (disclosed(i) == 0) {
      // IMPORTANT: Correction to the length of mTilde to prevent overflows
      RandomBits(session.prove.mHat[i], LENGTH_M_ - 1);
    }
  }
  debugValues("mTilde", session.prove.mHat, SIZE_M_, SIZE_L);
  // IMPORTANT: Correction to the length of eTilde to prevent overflows
  RandomBits(public.prove.eHat, LENGTH_E_ - 1);
  debugValue("eTilde", public.prove.eHat, SIZE_E_);
  // IMPORTANT: Correction to the length of vTilde to prevent overflows
  RandomBits(public.prove.vHat, LENGTH_V_ - 1);
  debugValue("vTilde", public.prove.vHat, SIZE_V_);
  // IMPORTANT: Correction to the length of rA to prevent negative values
  RandomBits(public.prove.rA + rA_offset, rA_size * 8 - 1);
  for (i = 0; i < rA_offset; i++) {
    public.prove.rA[i] = 0x00; // Set first byte(s) of rA, since it's not set by RandomBits command
  }
Example #8
0
void init_PRNG(void) {
  unsigned char PRNG_output[SIZE_H + 4]; // 49 52 4D 41 (IRMA)
  RandomBits(PRNG_output, LENGTH_H);
  
  PRNG_output[SIZE_H] 		= 0x49;
  PRNG_output[SIZE_H + 1]	= 0x52;
  PRNG_output[SIZE_H + 2]	= 0x4D;
  PRNG_output[SIZE_H + 3]	= 41;
  
  SHA(SHA_256, session.prove.aesKey, SIZE_H + 4, PRNG_output);
  
  session.prove.ctrBlock = 0x00; 
}
Example #9
0
void RSA::cryptRSA (char* fileMText,char* fileKey,char *fileCrypt)
{
    string headCode="1000010010000100";/*head16bit*/
    ifstream filetext(fileMText);
    ifstream filekey(fileKey);
    ofstream filecrypt(fileCrypt);
    if(!filetext){
        cout<<"Fail!Filetext isn't exist";
        return;
    }
    if(!filekey){
        cout<<"Fail!Filekey isn't exist";
        return;
    }
    /*read key from fileKey*/
    string TEXT;
    ZZZ n,e;
    filekey>>TEXT;
    n=TEXT;
    filekey>>TEXT;
    e=TEXT;
    /*linked head16bit with bit random + KEY{128,192,256}*/
    TEXT=headCode;
    ZZ numbersite;
    RandomBits(numbersite,(2032-KEY));
    stringstream buffer;
    buffer<<numbersite;
    ZZZ code;
    code=buffer.str();
    int strlen=code.get_str(2).length();
    for(;strlen<2032-KEY;strlen++)TEXT+='1';
    TEXT+=code.get_str(2);
    filetext>>headCode;
    code=headCode;
    TEXT+=code.get_str(2);
    /*********************/
    /*C=M^e mod n*/
    code.set_str(TEXT,2);
    mpz_powm(code.get_mpz_t(),code.get_mpz_t(),e.get_mpz_t(),n.get_mpz_t());
    filecrypt<<convert_base64(code.get_str(2))<<endl;

    filecrypt.close();
    filetext.close();
    filekey.close();
}
Example #10
0
void sample_NTL_factor(unsigned long length, unsigned long bits,
                          void* arg, unsigned long count)
{
    ZZX poly1, poly2, poly3;
    ZZ a, c;
    vec_pair_ZZX_long factors;
    
    poly1.SetMaxLength(length);
    //poly2.SetMaxLength(length);
    //poly3.SetMaxLength(2*length-1);


   
   unsigned long r_count;    // how often to generate new random data
   
   if (count >= 1000) r_count = 100;
   else if (count >= 100) r_count = 10;
   else if (count >= 20) r_count = 5;
   else if (count >= 8) r_count = 2;
   else r_count = 1;
   
   unsigned long i;
   for (i = 0; i < count; i++)
   {
      if (i%r_count == 0)
      {
	    unsigned long j;
	    for (j = 0; j<length; j++)
		{
		RandomBits(a,bits);
		SetCoeff(poly1,j,a);
		//RandomBits(a,bits);
		//SetCoeff(poly2,j,a);
		}
		//mul(poly3, poly1, poly2);
      }
       prof_start();
       factor(c, factors, poly1);
       prof_stop();
   }
   

}
Example #11
0
void PrGlib::PrG_renew()
{
    /*random prime with NTL/ZZ.h*/
    ZZ numbersite;
    RandomBits(numbersite,PrG_length);
    stringstream buffer;
    buffer<<numbersite;
    this->PrG_number=buffer.str();
    /****************************/
    /*check and add bit such that bit was make have to equal size of prime was defined*/
    string ha=PrG_number.get_str(2);
    int strlen=ha.length();
    for(;strlen<this->PrG_length;strlen++)ha+='1';
    this->PrG_number.set_str(ha,2);
    /****************************/
    //if((this->PrG_number & 1)== 0) this->PrG_number=(this->PrG_number | 1);
    /*renew bitget,make to PrG_preprocess and PrG_index*/
    PrG_bitGet=new vector<bool>(this->PrG_sieve_size);
    PrG_preprocess();
    this->PrG_index=0;
}