int main () {
    GenPrime();
   // for(int i = 1; i <= 100; i++) if(prime[i]) printf("%d ",i);
    while(scanf("%d",&N) && N){
                         if(!Check()) printf("Goldbach's conjecture is wrong.\n");
                         else printf("%d = %d + %d\n", N, x, y);
                         }
    return 0;
    }
コード例 #2
0
ファイル: keygen.C プロジェクト: Allen-smith/ctf-tools
void
make_rsa_key(rsa_pub &pub, rsa_priv &priv, long bits, ZZ& e)
{
  pub.e = e;

  do
    {
      GenPrime(priv.p, bits/2);
    }
  while (!IsOne(GCD(priv.p-1, pub.e)));

  do
    {
      GenPrime(priv.q, bits/2);
    }
  while (!IsOne(GCD(priv.q-1, pub.e)));

  pub.N = priv.p * priv.q;

  priv.d = InvMod(pub.e, (priv.p-1)*(priv.q-1));

  rem(priv.dp1, priv.d, priv.p-1);
  rem(priv.dq1, priv.d, priv.q-1);
}
コード例 #3
0
ファイル: sprime.cpp プロジェクト: cenhao/coding
int main() {
	freopen("sprime.in", "r", stdin);
	freopen("sprime.out", "w", stdout);

	int n;
	scanf("%d", &n);

	int end = 1;
	for (int i=0; i<n; ++i) { end *= 10; }
	GenPrime(int(sqrt(end))+1);

	int p1[] = {2, 3, 5, 7};
	for (auto p : p1) {
		Solve(p, n);
	}

	return 0;
}
コード例 #4
0
ファイル: G3410-94.cpp プロジェクト: vster/GOST-R-3410
void genparams( ZZ &p, ZZ &q, ZZ &a)
{

	long err = 80;	
	GenPrime(q, N, err);

	ZZ m;
	RandomLen(m, L-N);

	cout << "\nGenerating p, q and a...\n";
	long NumTrials = 20;
	for (long i = 0; i < 10000; i++, m++)
	{
		p = q * m + 1;
		
		if (ProbPrime(p, NumTrials))
		{
			// cout << "\ni = " << i << endl;
			// cout << "OK" << endl;
			break;
		}
	}

	ZZ d, f;
//	ZZ f1 = ((p-1) * InvMod(q, p)) % p;
	ZZ f1 = m;	// = (p-1)/q
	for( d = 2; a == 0; d++ )
	{
		f = PowerMod(d%p, f1%p, p);
		if ( f >  1 )
		{	
			a = f;
			// cout << a << " ";
			break;
		}
	}
	cout << "\np = \n"; show_dec_in_hex (p, L);	cout << endl;
	cout << "\nq = \n"; show_dec_in_hex (q, N);	cout << endl;
	cout << "\na = \n"; show_dec_in_hex (a, L);	cout << endl;
}
コード例 #5
0
ファイル: mat_ZZ.c プロジェクト: tvtritin/Fuzzy-extractor
void inv(ZZ& d_out, mat_ZZ& x_out, const mat_ZZ& A, long deterministic)
{
    long n = A.NumRows();

    if (A.NumCols() != n)
        Error("solve: nonsquare matrix");

    if (n == 0) {
        set(d_out);
        x_out.SetDims(0, 0);
        return;
    }

    zz_pBak zbak;
    zbak.save();

    ZZ_pBak Zbak;
    Zbak.save();

    mat_ZZ x(INIT_SIZE, n, n);
    ZZ d, d1;

    ZZ d_prod, x_prod;
    set(d_prod);
    set(x_prod);

    long d_instable = 1;
    long x_instable = 1;

    long gp_cnt = 0;

    long check = 0;


    mat_ZZ y;

    long i;
    long bound = 2+DetBound(A);

    for (i = 0; ; i++) {
        if ((check || IsZero(d)) && !d_instable) {
            if (NumBits(d_prod) > bound) {
                break;
            }
            else if (!deterministic &&
                     bound > 1000 && NumBits(d_prod) < 0.25*bound) {

                ZZ P;

                long plen = 90 + NumBits(max(bound, NumBits(d)));
                GenPrime(P, plen, 90 + 2*NumBits(gp_cnt++));

                ZZ_p::init(P);

                mat_ZZ_p AA;
                conv(AA, A);

                ZZ_p dd;
                determinant(dd, AA);

                if (CRT(d, d_prod, rep(dd), P))
                    d_instable = 1;
                else
                    break;
            }
        }


        zz_p::FFTInit(i);
        long p = zz_p::modulus();

        mat_zz_p AA;
        conv(AA, A);

        if (!check) {
            mat_zz_p xx;

            zz_p dd;

            inv(dd, xx, AA);

            d_instable = CRT(d, d_prod, rep(dd), p);
            if (!IsZero(dd)) {
                mul(xx, xx, dd);
                x_instable = CRT(x, x_prod, xx);
            }
            else
                x_instable = 1;

            if (!d_instable && !x_instable) {
                mul(y, x, A);
                if (IsDiag(y, n, d)) {
                    d1 = d;
                    check = 1;
                }
            }
        }
        else {
            zz_p dd;
            determinant(dd, AA);
            d_instable = CRT(d, d_prod, rep(dd), p);
        }
    }

    if (check && d1 != d) {
        mul(x, x, d);
        ExactDiv(x, d1);
    }

    d_out = d;
    if (check) x_out = x;

    zbak.restore();
    Zbak.restore();
}
コード例 #6
0
ファイル: mat_ZZ.c プロジェクト: tvtritin/Fuzzy-extractor
void determinant(ZZ& rres, const mat_ZZ& a, long deterministic)
{
    long n = a.NumRows();
    if (a.NumCols() != n)
        Error("determinant: nonsquare matrix");

    if (n == 0) {
        set(rres);
        return;
    }

    zz_pBak zbak;
    zbak.save();

    ZZ_pBak Zbak;
    Zbak.save();

    long instable = 1;

    long gp_cnt = 0;

    long bound = 2+DetBound(a);

    ZZ res, prod;

    clear(res);
    set(prod);


    long i;
    for (i = 0; ; i++) {
        if (NumBits(prod) > bound)
            break;

        if (!deterministic &&
                !instable && bound > 1000 && NumBits(prod) < 0.25*bound) {
            ZZ P;


            long plen = 90 + NumBits(max(bound, NumBits(res)));
            GenPrime(P, plen, 90 + 2*NumBits(gp_cnt++));

            ZZ_p::init(P);

            mat_ZZ_p A;
            conv(A, a);

            ZZ_p t;
            determinant(t, A);

            if (CRT(res, prod, rep(t), P))
                instable = 1;
            else
                break;
        }


        zz_p::FFTInit(i);
        long p = zz_p::modulus();

        mat_zz_p A;
        conv(A, a);

        zz_p t;
        determinant(t, A);

        instable = CRT(res, prod, rep(t), p);
    }

    rres = res;

    zbak.restore();
    Zbak.restore();
}
コード例 #7
0
void CharPoly(ZZX& gg, const mat_ZZ& a, long deterministic)
{
   long n = a.NumRows();
   if (a.NumCols() != n)
      LogicError("CharPoly: nonsquare matrix");

   if (n == 0) {
      set(gg);
      return;
   }


   if (n == 1) {
      ZZ t;
      SetX(gg);
      negate(t, a(1, 1));
      SetCoeff(gg, 0, t);
      return;
   }

   long bound = 2 + CharPolyBound(a);

   zz_pBak bak;
   bak.save();

   ZZ_pBak bak1;
   bak1.save();

   ZZX g;
   ZZ prod;

   clear(g);
   set(prod);

   long i;

   long instable = 1;

   long gp_cnt = 0;

   for (i = 0; ; i++) {
      if (NumBits(prod) > bound)
         break;

      if (!deterministic &&
          !instable && bound > 1000 && NumBits(prod) < 0.25*bound) {
         long plen = 90 + NumBits(max(bound, MaxBits(g)));

         ZZ P;

         GenPrime(P, plen, 90 + 2*NumBits(gp_cnt++));

         ZZ_p::init(P);
         mat_ZZ_p A;
         ZZ_pX G;
         conv(A, a);
         CharPoly(G, A);

         if (CRT(g, prod, G))
            instable = 1;
         else
            break;
      }

      zz_p::FFTInit(i);

      mat_zz_p A;
      zz_pX G;
      conv(A, a);
      CharPoly(G, A);
      instable = CRT(g, prod, G);
   }

   gg = g;

   bak.restore();
   bak1.restore();
}
コード例 #8
0
ファイル: ZZXCharPoly.cpp プロジェクト: shayne-fletcher/cppf
NTL_START_IMPL


void CharPolyMod(ZZX& gg, const ZZX& a, const ZZX& f, long deterministic)
{
   if (!IsOne(LeadCoeff(f)) || deg(f) < 1 || deg(a) >= deg(f))
      Error("CharPolyMod: bad args");


   if (IsZero(a)) {
      clear(gg);
      SetCoeff(gg, deg(f));
      return;
   }

   long bound = 2 + CharPolyBound(a, f);

   long gp_cnt = 0;

   zz_pBak bak;
   bak.save();

   ZZ_pBak bak1;
   bak1.save();

   ZZX g;
   ZZ prod;

   clear(g);
   set(prod);

   long i;

   long instable = 1;

   for (i = 0; ; i++) {
      if (NumBits(prod) > bound)
         break;

      if (!deterministic &&
          !instable && bound > 1000 && NumBits(prod) < 0.25*bound) {
         long plen = 90 + NumBits(max(bound, MaxBits(g)));

         ZZ P;

         GenPrime(P, plen, 90 + 2*NumBits(gp_cnt++));

         ZZ_p::init(P);
         ZZ_pX G, A, F;
         conv(A, a);
         conv(F, f);
         CharPolyMod(G, A, F);

         if (CRT(g, prod, G))
            instable = 1;
         else
            break;
      }

      zz_p::FFTInit(i);

      zz_pX G, A, F;
      conv(A, a);
      conv(F, f);
      CharPolyMod(G, A, F);
      instable = CRT(g, prod, G);
   }

   gg = g;

   bak.restore();
   bak1.restore();
}
コード例 #9
0
/**************************
//ElGamal HE system
//len: length of params p,q
**************************/
void ElGamal(int len=512){
  ZZ n, n2, p, q, g, x, lamda;
  ZZ p1, q1, p1q1;
  ZZ bA;
  
  ZZ m1, m2, k1, k2;
  ZZ BSm, HEm; //baseline and HE result
  ZZ c, r1, r2, t1, t2, cm1, cm2;
  ZZ r, t;

  //key gen
  start = std::clock();

  GenPrime(q, len);
  RandomBnd(g, q);
  RandomBnd(x, q);
  PowerMod(bA,g,x, q);

  duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
  cout<<"ElGamal Setup:"<< duration <<'\n';

  //Enc
  RandomBnd(m1, q);
  RandomBnd(m2, q);

  start = std::clock();

  RandomBnd(k1, q); //B
  PowerMod(r1,g,k1, q);
  PowerMod(t1,bA,k1, q);
  MulMod(t1, t1, m1, q);

  RandomBnd(k2, q); //B
  PowerMod(r2,g,k2, q);
  PowerMod(t2,bA,k2, q);
  MulMod(t2, t2, m2, q);

  duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
  cout<<"ElGamal Enc:"<< duration/2 <<'\n';

  //Evaluation
  start = std::clock();

  MulMod(r,r1,r2,q);
  MulMod(t,t1,t2,q);

  duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
  cout<<"ElGamal Eval:"<< duration/2 <<'\n';

  //Dec  
  start = std::clock();

  PowerMod(HEm,r,x,q);
  InvMod(HEm, HEm, q);
  MulMod(HEm,HEm,t,q);
  
  duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
  cout<<"ElGamal Dec:"<< duration/2 <<'\n';

  //baseline
  MulMod(BSm,m1,m2,q);

  assert(BSm==HEm);
}
コード例 #10
0
/**************************
//Goldwasser-Micali HE system
//len: length of params p,q
**************************/
void GM(int len=512){
  ZZ N, p, q, x;

  long m1, m2;
  ZZ BSm, HEm; //baseline and HE result
  ZZ c, c1, c2, cm1, cm2, r;

  //key gen
  start = std::clock();

  GenPrime(p, len);
  GenPrime(q, len);
  mul(N, p, q);
  
  do{
  RandomBnd(x, N);
  } while( (Jacobi(x,p)==1) || (Jacobi(x,q)==1));

  cout<<"Jac:"<<Jacobi(x,p)<<Jacobi(x,q)<<endl;
  duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
  cout<<"GM Setup:"<< duration <<'\n';

  //Enc
  RandomBnd(m1,2);
  RandomBnd(m2,2);

  start = std::clock();
  RandomBnd(r, N);
  MulMod(cm1,r,r,N);

  if(m1==1)
   MulMod(cm1,cm1,x,N);
  
  RandomBnd(r, N);
  MulMod(cm2,r,r,N);

  if(m2==1)
   MulMod(cm2,cm2,x,N);

  duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
  cout<<"GM Enc:"<< duration <<'\n';

  //Evaluation
  start = std::clock();
  MulMod(c,cm1,cm2,N);
  duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
  cout<<"GM Eval:"<< duration <<'\n';

  //Dec  
  start = std::clock();
  HEm=Jacobi(c,p);
  duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
  cout<<"GM Dec:"<< duration <<'\n';

  if(HEm==1)
    HEm=0;
  else
    HEm=1;

  //baseline
  BSm=m1^m2;  //xor

  //cout<<"plaintext:"<<m1<<m2<<BSm<<endl;
  assert(BSm==HEm);
}
コード例 #11
0
/**************************
//Paillier HE system
//len: length of params p,q
**************************/
void Paillier(int len=512){
  ZZ n, n2, p, q, g, lamda;
  ZZ p1, q1, p1q1;
  ZZ miu;
  
  ZZ m1, m2;
  ZZ BSm, HEm; //baseline and HE result
  ZZ c, c1, c2, cm1, cm2, r;

  //key gen
  start = std::clock();

  GenPrime(p, len);
  GenPrime(q, len);
  mul(n, p, q);
  mul(n2, n, n);

  sub(p1,p,1);
  sub(q1,q,1);
  GCD(lamda,p1,q1);
  mul(p1q1,p1,q1);
  div(lamda, p1q1, lamda);

  RandomBnd(g, n2);

  PowerMod(miu,g,lamda,n2);
  sub(miu, miu, 1);
  div(miu,miu,n); //should add 1?
  InvMod(miu, miu, n);
  
  duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
  cout<<"Pailler Setup:"<< duration <<'\n';

  //Enc
  start = std::clock();
  RandomBnd(m1,n);
  RandomBnd(m2,n);

  RandomBnd(r,n); //enc m1
  PowerMod(c1, g,m1,n2);
  PowerMod(c2, r,n,n2);
  MulMod(cm1, c1,c2, n2);

  RandomBnd(r,n); //enc m2
  PowerMod(c1, g,m2,n2);
  PowerMod(c2, r,n,n2);
  MulMod(cm2, c1,c2, n2);

  duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
  cout<<"Pailler Enc:"<< duration/2 <<'\n';

  //Evaluation
  start = std::clock();
  c=cm1;
  for(int i=0; i<TIMES; i++)
  	MulMod(c,c,cm2,n2);
  duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
  cout<<"Pailler Eval:"<< duration <<'\n';

  //c=cm2;
  //Dec  
  start = std::clock();
  PowerMod(c,c,lamda,n2);
  sub(c,c,1);
  div(c,c,n); //should add 1?
  MulMod(HEm, c, miu, n);  

  duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
  cout<<"Pailler Dec:"<< duration <<'\n';

  //baseline
  BSm=m1;
  for(int i=0; i<TIMES; i++)
  	AddMod(BSm,BSm,m2,n);

  assert(BSm==HEm);
}
コード例 #12
0
ファイル: QuickTest.c プロジェクト: fionser/NTL
int main()
{
   SetSeed(ZZ(0));


   cerr << "This is NTL version " << NTL_VERSION << "\n"; 

   cerr << "Hardware charactersitics:\n";
   cerr << "NTL_BITS_PER_LONG = " << NTL_BITS_PER_LONG << "\n";
   cerr << "NTL_ZZ_NBITS = " << NTL_ZZ_NBITS << "\n";
   cerr << "NTL_SP_NBITS = " << NTL_SP_NBITS << "\n";

#ifdef NTL_HAVE_LL_TYPE
   cerr << "NTL_HAVE_LL_TYPE\n";
#endif

#ifdef NTL_LONGDOUBLE_SP_MULMOD
   cerr << "NTL_LONGDOUBLE_SP_MULMOD\n";
#endif

#ifdef NTL_LONGLONG_SP_MULMOD
   cerr << "NTL_LONGLONG_SP_MULMOD\n";
#endif

   cerr << "\n";

   


   cerr << "Basic Configuration Options:\n";



#ifdef NTL_LEGACY_NO_NAMESPACE
   cerr << "NTL_LEGACY_NO_NAMESPACE\n";
#endif


#ifdef NTL_LEGACY_INPUT_ERROR
   cerr << "NTL_LEGACY_INPUT_ERROR\n";
#endif


#ifdef NTL_THREADS
   cerr << "NTL_THREADS\n";
#endif


#ifdef NTL_EXCEPTIONS
   cerr << "NTL_EXCEPTIONS\n";
#endif

#ifdef NTL_THREAD_BOOST
   cerr << "NTL_THREAD_BOOST\n";
#endif


#ifdef NTL_LEGACY_SP_MULMOD
   cout << "NTL_LEGACY_SP_MULMOD\n";
#endif


#ifdef NTL_DISABLE_LONGDOUBLE
   cout << "NTL_DISABLE_LONGDOUBLE\n";
#endif


#ifdef NTL_DISABLE_LONGLONG
   cout << "NTL_DISABLE_LONGLONG\n";
#endif

#ifdef NTL_MAXIMIZE_SP_NBITS
   cout << "NTL_MAXIMIZE_SP_NBITS\n";
#endif




#ifdef NTL_GMP_LIP
   cerr << "NTL_GMP_LIP\n";
#endif


#ifdef NTL_GF2X_LIB
   cerr << "NTL_GF2X_LIB\n";
#endif


#ifdef NTL_PCLMUL
   cerr << "NTL_PCLMUL\n";
#endif


#ifdef NTL_LONG_LONG_TYPE
   cerr << "NTL_LONG_LONG_TYPE: ";
   cerr << make_string(NTL_LONG_LONG_TYPE) << "\n";
#endif

#ifdef NTL_UNSIGNED_LONG_LONG_TYPE
   cerr << "NTL_UNSIGNED_LONG_LONG_TYPE: ";
   cerr << make_string(NTL_UNSIGNED_LONG_LONG_TYPE) << "\n";
#endif


#ifdef NTL_X86_FIX
   cerr << "NTL_X86_FIX\n";
#endif

#ifdef NTL_NO_X86_FIX
   cerr << "NTL_NO_X86_FIX\n";
#endif

#ifdef NTL_NO_INIT_TRANS
   cerr << "NTL_NO_INIT_TRANS\n";
#endif

#ifdef NTL_CLEAN_INT
   cerr << "NTL_CLEAN_INT\n";
#endif

#ifdef NTL_CLEAN_PTR
   cerr << "NTL_CLEAN_PTR\n";
#endif

#ifdef NTL_RANGE_CHECK
   cerr << "NTL_RANGE_CHECK\n";
#endif


cerr << "\n";
cerr << "Resolution of double-word types:\n";
cerr << make_string(NTL_LL_TYPE) << "\n";
cerr << make_string(NTL_ULL_TYPE) << "\n";


cerr << "\n";
cerr << "Performance Options:\n";

#ifdef NTL_LONG_LONG
   cerr << "NTL_LONG_LONG\n";
#endif

#ifdef NTL_AVOID_FLOAT
   cerr << "NTL_AVOID_FLOAT\n";
#endif


#ifdef NTL_SPMM_ULL
   cerr << "NTL_SPMM_ULL\n";
#endif


#ifdef NTL_SPMM_ASM
   cerr << "NTL_SPMM_ASM\n";
#endif




#ifdef NTL_AVOID_BRANCHING
   cerr << "NTL_AVOID_BRANCHING\n";
#endif


#ifdef NTL_FFT_BIGTAB
   cout << "NTL_FFT_BIGTAB\n";
#endif

#ifdef NTL_FFT_LAZYMUL
   cout << "NTL_FFT_LAZYMUL\n";
#endif





#ifdef NTL_TBL_REM
   cerr << "NTL_TBL_REM\n";
#endif


#ifdef NTL_TBL_REM_LL
   cerr << "NTL_TBL_REM_LL\n";
#endif

#ifdef NTL_CRT_ALTCODE
   cerr << "NTL_CRT_ALTCODE\n";
#endif

#ifdef NTL_CRT_ALTCODE_SMALL
   cerr << "NTL_CRT_ALTCODE_SMALL\n";
#endif

#ifdef NTL_GF2X_ALTCODE
   cerr << "NTL_GF2X_ALTCODE\n";
#endif

#ifdef NTL_GF2X_ALTCODE1
   cerr << "NTL_GF2X_ALTCODE1\n";
#endif


#ifdef NTL_GF2X_NOINLINE
   cerr << "NTL_GF2X_NOINLINE\n";
#endif

   cerr << "\n\n";

   cerr << "running tests";

   long n, k, i;

   n = 250;
   k = 16000;

   ZZ p;


   for (i = 0; i < 15; i++) {
      // cerr << n << "/" << k; 
      cerr << ".";
      RandomLen(p, k);
      ZZ_p::init(p);  
    

      ZZ_pX a, b, c, c1;


      random(a, n);
      random(b, n);

      FFTMul(c, a, b);
      //cerr << ZZ_pInfo->FFTInfo->NumPrimes;

      c1 = conv<ZZ_pX>( KarMul( conv<ZZX>(a), conv<ZZX>(b) ) );

      if (c1 != c) {
         cerr << "ZZ_pX mul failed!\n";
         return 1;
      }

      n = long(n * 1.35);
      k = long(k / 1.414);
   }


   // small prime tests...I've made some changes in v5.3
   // that should be checked on various platforms, so 
   // we might as well check them here.

   if (SmallModulusTest(17, 1000)) {
      cerr << "first SmallModulusTest failed!!\n";
      return 1;
   }

   if (SmallModulusTest((1L << (NTL_SP_NBITS))-1, 1000)) {
      cerr << "second SmallModulusTest failed!!\n";
      return 1;
   }

   // Test gf2x code....

   if (GF2X_test()) {
      cerr << "GF2X test failed!\n";
      return 1;
   }
   

   cerr << "OK\n";

   ZZ x1, x2, x3, x4;
   double t;

   RandomLen(x1, 1024);
   RandomBnd(x2, x1);
   RandomBnd(x3, x1);

   mul(x4, x2, x3);

   t = GetTime();
   for (i = 0; i < 100000; i++)
      mul(x4, x2, x3);
   t = GetTime()-t;

   cerr << "time for 1024-bit mul: " << t*10 << "us";
   cerr << "\n";

   rem(x2, x4, x1);

   t = GetTime();
   for (i = 0; i < 100000; i++)
      rem(x2, x4, x1);
   t = GetTime()-t;

   cerr << "time for 2048/1024-bit rem: " << t*10 << "us";
   cerr << "\n";
   

   GenPrime(p, 1024);
   RandomBnd(x1, p);
   if (IsZero(x1)) set(x1);

   InvMod(x2, x1, p);

   t = GetTime();
   for (i = 0; i < 1000; i++)
      InvMod(x2, x1, p);
   t = GetTime()-t;

   cerr << "time for 1024-bit modular inverse: " << t*1000 << "us";
   cerr << "\n";



   // test modulus switching
   
   n = 1024;
   k = 1024;
   RandomLen(p, k);

   ZZ_p::init(p);
   if (!IsOdd(p)) p++;

   ZZ_pX j1, j2, j3;

   random(j1, n);
   random(j2, n);

   mul(j3, j1, j2);

   t = GetTime();
   for (i = 0; i < 200; i++) mul(j3, j1, j2);
   t = GetTime()-t;

   cerr << "time to multiply degree 1023 polynomials\n   modulo a 1024-bit number: ";
   cerr << (t/200) << "s";
   cerr << "\n";

   GF2X_time();

   return 0;
}
コード例 #13
0
ファイル: QuickTest.cpp プロジェクト: shayne-fletcher/cppf
int main()
{


   cerr << "This is NTL version " << NTL_VERSION << "\n"; 

   cerr << "Basic Configuration Options:\n";


#ifdef NTL_STD_CXX
   cerr << "NTL_STD_CXX\n";
#endif

#ifdef NTL_PSTD_NNS
   cerr << "NTL_PSTD_NNS\n";
#endif

#ifdef NTL_PSTD_NHF
   cerr << "NTL_PSTD_NHF\n";
#endif

#ifdef NTL_PSTD_NTN
   cerr << "NTL_PSTD_NTN\n";
#endif

#ifdef NTL_GMP_LIP
   cerr << "NTL_GMP_LIP\n";
#endif

#ifdef NTL_GMP_HACK
   cerr << "NTL_GMP_HACK\n";
#endif

#ifdef NTL_GF2X_LIB
   cerr << "NTL_GF2X_LIB\n";
#endif


#ifdef NTL_LONG_LONG_TYPE
   cerr << "NTL_LONG_LONG_TYPE: ";
   cerr << make_string(NTL_LONG_LONG_TYPE) << "\n";
#endif

#ifdef NTL_UNSIGNED_LONG_LONG_TYPE
   cerr << "NTL_UNSIGNED_LONG_LONG_TYPE: ";
   cerr << make_string(NTL_UNSIGNED_LONG_LONG_TYPE) << "\n";
#endif

#ifdef NTL_CXX_ONLY
   cerr << "NTL_CXX_ONLY\n";
#endif


#ifdef NTL_X86_FIX
   cerr << "NTL_X86_FIX\n";
#endif

#ifdef NTL_NO_X86_FIX
   cerr << "NTL_NO_X86_FIX\n";
#endif

#ifdef NTL_NO_INIT_TRANS
   cerr << "NTL_NO_INIT_TRANS\n";
#endif

#ifdef NTL_CLEAN_INT
   cerr << "NTL_CLEAN_INT\n";
#endif

#ifdef NTL_CLEAN_PTR
   cerr << "NTL_CLEAN_PTR\n";
#endif

#ifdef NTL_RANGE_CHECK
   cerr << "NTL_RANGE_CHECK\n";
#endif


cerr << "\n";
cerr << "Resolution of double-word types:\n";
cerr << make_string(NTL_LL_TYPE) << "\n";
cerr << make_string(NTL_ULL_TYPE) << "\n";


cerr << "\n";
cerr << "Performance Options:\n";

#ifdef NTL_LONG_LONG
   cerr << "NTL_LONG_LONG\n";
#endif

#ifdef NTL_AVOID_FLOAT
   cerr << "NTL_AVOID_FLOAT\n";
#endif

#ifdef NTL_SPMM_UL
   cerr << "NTL_SPMM_UL\n";
#endif


#ifdef NTL_SPMM_ULL
   cerr << "NTL_SPMM_ULL\n";
#endif


#ifdef NTL_SPMM_ASM
   cerr << "NTL_SPMM_ASM\n";
#endif




#ifdef NTL_AVOID_BRANCHING
   cerr << "NTL_AVOID_BRANCHING\n";
#endif



#ifdef NTL_TBL_REM
   cerr << "NTL_TBL_REM\n";
#endif


#ifdef NTL_GF2X_ALTCODE
   cerr << "NTL_GF2X_ALTCODE\n";
#endif

#ifdef NTL_GF2X_ALTCODE1
   cerr << "NTL_GF2X_ALTCODE1\n";
#endif


#ifdef NTL_GF2X_NOINLINE
   cerr << "NTL_GF2X_NOINLINE\n";
#endif

   cerr << "\n\n";

   if (_ntl_gmp_hack)
      cerr << "using GMP hack\n\n";

   cerr << "running tests...";

   long n, k;

   n = 200;
   k = 10*NTL_ZZ_NBITS;

   ZZ p;

   GenPrime(p, k);


   ZZ_p::init(p);         // initialization

   ZZ_pX f, g, h, r1, r2, r3;

   random(g, n);    // g = random polynomial of degree < n
   random(h, n);    // h =             "   "
   random(f, n);    // f =             "   "

   // SetCoeff(f, n);  // Sets coefficient of X^n to 1
   
   ZZ_p lc;

   do {
      random(lc);
   } while (IsZero(lc));

   SetCoeff(f, n, lc);


   // For doing arithmetic mod f quickly, one must pre-compute
   // some information.

   ZZ_pXModulus F;
   build(F, f);

   PlainMul(r1, g, h);  // this uses classical arithmetic
   PlainRem(r1, r1, f);

   MulMod(r2, g, h, F);  // this uses the FFT

   MulMod(r3, g, h, f);  // uses FFT, but slower

   // compare the results...

   if (r1 != r2) {
      cerr << "r1 != r2!!\n";
      return 1;
   }
   else if (r1 != r3) {
      cerr << "r1 != r3!!\n";
      return 1;
   }


   // small prime tests...I've made some changes in v5.3
   // that should be checked on various platforms, so 
   // we might as well check them here.

   if (SmallModulusTest(17, 1000)) {
      cerr << "first SmallModulusTest failed!!\n";
      return 1;
   }

   if (SmallModulusTest((1L << (NTL_SP_NBITS))-1, 1000)) {
      cerr << "second SmallModulusTest failed!!\n";
      return 1;
   }

   // Test gf2x code....

   if (GF2X_test()) {
      cerr << "GF2X test failed!\n";
      return 1;
   }
   

   cerr << "OK\n";

   ZZ x1, x2, x3, x4;
   double t;
   long i;

   RandomLen(x1, 1024);
   RandomBnd(x2, x1);
   RandomBnd(x3, x1);

   mul(x4, x2, x3);

   t = GetTime();
   for (i = 0; i < 100000; i++)
      mul(x4, x2, x3);
   t = GetTime()-t;

   cerr << "time for 1024-bit mul: " << t*10 << "us";

   if (_ntl_gmp_hack) {
      _ntl_gmp_hack = 0;
      mul(x4, x2, x3);

      t = GetTime();
      for (i = 0; i < 100000; i++)
         mul(x4, x2, x3);
      t = GetTime()-t;

      cerr << " (" << (t*10) << "us without GMP)"; 

      _ntl_gmp_hack = 1;
   }

   cerr << "\n";

   rem(x2, x4, x1);

   t = GetTime();
   for (i = 0; i < 100000; i++)
      rem(x2, x4, x1);
   t = GetTime()-t;

   cerr << "time for 2048/1024-bit rem: " << t*10 << "us";

   if (_ntl_gmp_hack) {
      _ntl_gmp_hack = 0;
      rem(x2, x4, x1);
   
      t = GetTime();
      for (i = 0; i < 100000; i++)
         rem(x2, x4, x1);
      t = GetTime()-t;
      cerr << " (" << (t*10) << "us without GMP)"; 

      _ntl_gmp_hack = 1;
   }

   cerr << "\n";
   

   GenPrime(p, 1024);
   RandomBnd(x1, p);
   if (IsZero(x1)) set(x1);

   InvMod(x2, x1, p);

   t = GetTime();
   for (i = 0; i < 1000; i++)
      InvMod(x2, x1, p);
   t = GetTime()-t;

   cerr << "time for 1024-bit modular inverse: " << t*1000 << "us";

   if (_ntl_gmp_hack) {
      _ntl_gmp_hack = 0;
      InvMod(x2, x1, p);
   
      t = GetTime();
      for (i = 0; i < 1000; i++)
         InvMod(x2, x1, p);
      t = GetTime()-t;
         cerr << " (" << (t*1000) << "us without GMP)"; 

      _ntl_gmp_hack = 1;
   }

   cerr << "\n";



   // test modulus switching
   
   n = 1024;
   k = 1024;
   RandomLen(p, k);

   ZZ_p::init(p);
   ZZ_pInfo->check();

   ZZ_pX j1, j2, j3;

   random(j1, n);
   random(j2, n);

   t = GetTime();
   for (i = 0; i < 20; i++) mul(j3, j1, j2);
   t = GetTime()-t;

   cerr << "time to multiply degree 1023 polynomials\n   modulo a 1024-bit number: ";
   cerr << (t/20) << "s";

   if (_ntl_gmp_hack) {
      _ntl_gmp_hack = 0;

      ZZ_p::init(p);
      ZZ_pInfo->check();

      t = GetTime();
      for (i = 0; i < 20; i++) mul(j3, j1, j2);
      t = GetTime()-t;

      cerr << " (" << (t/20) << "s without GMP)";
      _ntl_gmp_hack = 1;
   }

   cerr << "\n";

   GF2X_time();

   return 0;
}
コード例 #14
0
ファイル: fft_mult.cpp プロジェクト: vernamlab/DHS-LTV
void mymult(){
	ZZX mya, myb, c0, c1, x;
	ZZ q;
	int k = to_long(euler_toient(to_ZZ(Modulus_M)));

	GenPrime(q, Max_Prime);
	RandomPolyGen(mya, k, 1, q);
	RandomPolyGen(myb, k, Max_Prime, q);

	long da = deg(mya);
	long db = deg(myb);
	long bound = 2 + NumBits(min(da, db)+1) + MaxBits(mya) + MaxBits(myb);


	ZZ prod;
	set(prod);
	int prime_num = GetPrimeNumber(bound, prod);
	cout << prime_num << endl;


	long mk = NextPowerOfTwo(2*da+1);

	zz_p::FFTInit(0);
	long p = zz_p::modulus();

	fftRep R1[prime_num];
	fftRep R2[prime_num];
	fftRep R3[prime_num];
	fftRep R4[prime_num];

	int size = 256;
	fftRep Rm[prime_num][size];

	for(int i=0; i<prime_num; i++)
		for(int j=0; j<size; j++)
			Rm[i][j].SetSize(mk);

	for(int i=0; i<prime_num; i++){
		zz_p::FFTInit(i);
		R1[i].SetSize(mk);
		R2[i].SetSize(mk);
		R3[i].SetSize(mk);
		R4[i].SetSize(mk);
	}

	myTimer tm;
	tm.Start();
	CalculateFFTValues(R1, mya, prime_num, db);
	tm.Stop();
	tm.ShowTime("My FFT:\t");

	CalculateFFTValues(R2, myb, prime_num, db);

	tm.Start();


	for(int i=0; i<prime_num; i++)
		for(int j=0; j<size; j++)
			Rm[i][j] = R2[i];

	for(int j=0; j<size; j++){
		CalculateFFTValues(R1, mya, prime_num, db);
		for(int i=0; i<prime_num; i++){
			zz_p::FFTInit(i);
			mul(R3[i], R1[i], Rm[i][j]);
			add(R4[i], R4[i], R3[i]);
		}
	}
	CalculateFFTValues(R4, myb, prime_num, db);

	tm.Stop();
	tm.ShowTime("My FFT:\t");
}