Exemple #1
0
int main(void) {
  nodeType *p,*q,*ad,*su,*mu, *headp ,*headq; 
  headp = NULL;
  headq = NULL;

  //pとqの二つの多項式を負の次数が入力されるまで入力
  p = inputpoly("p");
  q = inputpoly("q");

  ad = add(p, q);
  su = sub(p, q);
  mu = mul(p, q);

  printpoly("add", ad);
  printpoly("sub", su);
  printpoly("mul", mu);

  listmemfree(p);
  listmemfree(q);
  listmemfree(ad);
  listmemfree(su);
  listmemfree(mu);

  return 0;
}
Exemple #2
0
// Eingabe:
//   A: nxn-Matrix, die darst. Matrix einer Abb. bzgl. geg. Basis
//   V: Basis (->Groesse nxn)
// Korrektheit der Dimensionen wird nicht geprüft!
//
// Ausgabe:
//   elteiler: Die Elementarteilerfolge
//   B:        Vektoren mit Elementarteilern als Ordnungspolynom
void elteiler(matrix const& A, basis const& V, 
              vector<polynom>& elteiler, basis& B){
  int n=A.size();
  polynom m,tmp;
  vektor v, dummy;
  basis     U;
  gaussinfo Ugauss;     init(n, Ugauss);
  vector<K> f;
  while(U.size()<V.size()){
    minpoly(A,V,Ugauss,m,v,f);
#ifdef DEBUG
    printvektor(v); cout << "\t\t";
    printpoly(m); cout << "\t\t";
    printvektor(f); cout << endl;
#endif
    // Berechne vektor u
    vektor u(n,0);
    for(int i=0,j=0; i<elteiler.size();j+=deg(elteiler[i++])) {
      // fi <- Polynom, mit m*v = Sum fi*elteiler[i] (Siehe Handout Algorithmus RNF)
      polynom fi;   // mache aus f[x]...f[y] ein Polynom -> 
      fi.assign(&f[j], &f[j+deg(elteiler[i])]);
      trim(fi);  // loesche führende nullen
      add(u, mult(fi/m,A,B[i],tmp), u);  // u+= (fi/m)(A)*v
    }
    elteiler.push_back(m);
    B.push_back(v-u);
    U.push_back(v);  gauss(Ugauss,v,dummy);
    for(int i=1;i<deg(m);++i) { mult(A,v,v); U.push_back(v); /* B.push_back(v); */    gauss(Ugauss,v,dummy); }
  }
}
Exemple #3
0
int main(void)
{
    polynomial p1[MAXTERMS];
    readpoly(p1);
    printpoly(p1);
    
    return 0;
}
int main(int argc,char** argv)
{
	poly term[10];

	int expo[10]={0,1,2,3,4,5,6,7,8,9};
	float coef[10]={9,8,7,6,5,4,3,2,1,0};

	readpoly(expo,coef,term,10);
	printpoly(term,10);

	return 0;
}
Exemple #5
0
int polypoly(int argc, char **argv, int what) {
  if(argc!=3) {
    cout << "Syntax: test1_01 " << what << " <polynom> <polynom>"<< endl;
    return 1;
  }
  
  polynom p1,p2;
  ifstream file1(argv[1]);
  ifstream file2(argv[2]);
  if(!read(file1, p1) ||  !read(file2,p2)) {
    cout << "Fehler beim einlesen der Dateien"<< endl;
    return 1;
  }

  switch (what) {
  case 2: printpoly(p1+p2); break; // Hier passiert alles wichtige!
  case 3: printpoly(p1-p2); break;
  case 4: printpoly(p1*p2); break;
  }
  cout << endl;
  return 0;
}
Exemple #6
0
int polyprint(int argc, char **argv) {
  if(argc!=2) {
    cout << "Syntax: test1_01 1 <polynom>"<< endl;
    return 1;
  }
  
  polynom p;
  ifstream file1(argv[1]);
  if(!read(file1, p)) {
    cout << "Fehler beim einlesen der Dateien"<< endl;
    return 1;
  }

  printpoly(p); // Hier passiert alles wichtige!
  cout << endl;

  return 0;
}
int main(void)
{
	int deg;			// degree
	int i, j, k;
	int * coeff;		// array to store the coefficients as input by the user

	while(1)			// the program will anew as long as this loop remains true
	{
		printf("Please enter the maximum degree of the polynomial: ");
		scanf("%d", &deg);

		if(deg >= 0)												// checks and carries out actions only if input is correct (degree is positive)
		{
			coeff = (int*)calloc(deg+1, sizeof(int));				// this calls calloc and allocate the HEAP memory as required
			if (coeff != NULL)										// this checks for nullity and only carries out the following if nullity doesn't exist
			{
				printf("Please enter the coefficients: ");
				for(i=0; i<=deg ;i++)								// allows the user to input coefficients as many times as required
					scanf("%d", &coeff[i]);

				for (k=0; k <= deg; k++)							// this searches for the first non-zero coefficient
				{	
					if (coeff[k] != 0)
					{
						j = k;										// the value is recorded for further usage
						break;										// breaks the loop for the first term is found
					}
				}

				printf("The polynomial is ");
				printpoly(coeff, deg, j);							// sends the stored information to printpoly funtion
				printf("\n");

				printf("Its derivative is ");
				printderi(coeff, deg, j);							// sends the stored information to printderi function
				printf("\n\n");

				free(coeff);										// free the memory for new user inputs as the program anews
			}
		} else break;												// breaks the operation, and hence terminates the program
	}
	return 0;
}
int main(void)
{
	int deg;			// degree
	int i;
	int check;			// stores the integers 1 or 0 for detection of reciprocity, and hence use for cheking
	int coeff[10];		// array to store the coefficients as input by the user
	/* assumption is made such that the user never inputs a maximum degree n > 10 and that the user always inputs exactly the
	 * correct number of (integer) coefficients
	 */

		while(1)		// the program will anew as long as this loop remains true
		{
			// ask for the maximum degree
			printf("Please enter the maximum degree of the polynomial: ");
			scanf("%d", &deg);

			if (deg >= 0)										// checks and carries out actions only if input is correct (degree is positive)
			{
				// ask for the coefficients
				printf("Please enter the coefficients: ");
				for (i=0; i <= deg; i++)						// allows the user to input coefficients as many times as required
				{
					scanf("%d", &coeff[i]);
				}

				printf("The polynomial is ");
				printpoly(coeff, deg);							// sends the stored information to printpoly funtion

				printf("The reciprocal is ");
				printreci(coeff, deg);							// sends the stored information to printreci function

				printf("The polynomial is ");
				check = printreciprocity(coeff, deg);			// sends the stored information to printreciprocity function
				printf("\n");

				if(check == 1)									// breaks and terminates the program if self-reciprocal is detected
					break;

			} else break;										// breaks the operation, and hence terminates the program
		}
	return 0;
}
int main(void)		
{
	int maxdeg;		//maximum degree = maxdeg
	int a[10];		//array max size 10	
	char sign[10];	//array for signs (-ve or +ve)
	
	int i;
	int n;

	do {
	printf(" please enter maximum degree of the polynomial: ");	//prompt user for the max degree of polynomial
	scanf("%d", &maxdeg);
	
	if (maxdeg>=0)
		{
			printf("\n please enter the coefficients: ");	//prompt user for the coefficients

	
			for (i=0; i<=maxdeg; i++)		//assign coefficients to a space in the array
				{
					scanf("%c %d", &sign[i], &a[i] );		//signs get a space in the sign array, numbers get a space in the a array

				}

			printf("The polynomial is ");
			printpoly (maxdeg,a, sign);
	
			printf("The derivitive is ");
			derivative (maxdeg, a);

			


		}	//close if loop
	}	//close do loop
	while (maxdeg>=0);

	return 0;
}
Exemple #10
0
int main(void)
{
	while(degree>=0)
	{
		ptest = 0; /* ptest = 0 if no coefficient has been printed, once coeff has been printed, ptest = 1 */
	
		/* prompt the user for max degree of coefficients */
		printf("Please enter the maximum degree of the polynomial: ");
		scanf("%d", &degree);
		if(degree<0) /* if statement terminates program if -ve coefff has been entered */
		{
			break;
		}
	
		/* prompt the user for coefficients */
		printf("Please enter the coefficients: ");
		degree += 1; 
		for (i=0; i<degree; i++)
		{
			scanf("%d", &coefficient[i]);
		}


		/* print the polynomial */
		printf("The polynomial is ");
		printpoly(degree);

		ptest = 0;

		/* print the derivative */
		printf("\nThe derivative is ");
		printderiv(degree);
	
		printf("\n\n");

	}
	return 0;
}
//Comparison protocol based on ""
bool COM(bool disp, long long seed, unsigned p, FHEcontext &context) {
  ZZ seedZZ;
  seedZZ = seed;

  srand48(seed);
  SetSeed(seedZZ);

  FHESISecKey secretKey(context);
  const FHESIPubKey &publicKey(secretKey);
  
  long phim = context.zMstar.phiM();
  
  ZZ_pX ptxt1Poly, ptxt2Poly, sum, sumMult, prod, prod2, sumQuad;
  Plaintext resSum, resSumMult, resProd, resProdSwitch, resProd2, resSumQuad;

  //gen plaintext
  ptxt1Poly.rep.SetLength(phim);
  ptxt2Poly.rep.SetLength(phim);
  for (long i=0; i < phim; i++) {
    ptxt1Poly.rep[i] = RandomBnd(p);
    ptxt2Poly.rep[i] = RandomBnd(p);
  }

  //printpoly(ptxt1Poly, phim);

  ptxt1Poly.normalize();
  ptxt2Poly.normalize();
  
  #ifdef DEBUG
  cout<<"phim:"<<phim<<endl;
  cout<<"p1:"<<endl;
  printpoly(ptxt1Poly, phim);
  cout<<"p2:"<<endl;
  printpoly(ptxt2Poly, phim);
  #endif

  //plaintext operation
  sum = ptxt1Poly + ptxt2Poly;
  sumMult = ptxt2Poly * 7;
  prod = ptxt1Poly * ptxt2Poly;
  prod2 = prod * prod;
  sumQuad = prod2 * prod2 * 9; //\sum_((xy)^4)

  #ifdef DEBUG  
  cout<<"sum:"<<endl;
  printpoly(sum, phim);
  cout<<"prod2:"<<endl;
  printpoly(prod2, phim);
  #endif

  rem(prod, prod, to_ZZ_pX(context.zMstar.PhimX()));
  rem(prod2, prod2, to_ZZ_pX(context.zMstar.PhimX()));
  rem(sumQuad, sumQuad, to_ZZ_pX(context.zMstar.PhimX()));
  
  //encryption
  start = std::clock();
  Plaintext ptxt1(context, ptxt1Poly), ptxt2(context, ptxt2Poly);
  duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
  duration = duration/2;
  cout<<"Encryption:"<< duration <<'\n';

  Ciphertext ctxt1(publicKey), ctxt2(publicKey);
  publicKey.Encrypt(ctxt1, ptxt1);
  publicKey.Encrypt(ctxt2, ptxt2);

  Ciphertext cSum = ctxt1;
  cSum += ctxt2;

  Ciphertext cSumMult = ctxt2;
  start = std::clock();
  for (int i = 1; i < 7; i++) {
    cSumMult += ctxt2;
  }
  duration = ( std::clock() - start ) / (double) (CLOCKS_PER_SEC);
  duration = duration/6;
  cout<<"Addition:"<< duration <<'\n';

  Ciphertext cProd = ctxt1;
  cProd *= ctxt2;
 
  secretKey.Decrypt(resSum, cSum);
  secretKey.Decrypt(resSumMult, cSumMult);
  
  KeySwitchSI keySwitch(secretKey);
  keySwitch.ApplyKeySwitch(cProd);
  secretKey.Decrypt(resProd, cProd);

  cProd *= cProd;
  Ciphertext tmp = cProd;
  Ciphertext cSumQuad = cProd;
  
  keySwitch.ApplyKeySwitch(cProd);
  secretKey.Decrypt(resProd2, cProd);

  for (int i = 0; i < 8; i++) {
    cSumQuad += tmp;
  }
  keySwitch.ApplyKeySwitch(cSumQuad);  //apply key switch after summing all prod

  start = std::clock();
  cSumQuad *= cProd;
  duration = ( std::clock() - start ) / (double) (CLOCKS_PER_SEC);
  cout<<"HMult without key switch:"<< duration <<'\n';

  keySwitch.ApplyKeySwitch(cSumQuad);

  duration = ( std::clock() - start ) / (double) (CLOCKS_PER_SEC);
  cout<<"HMult with key switch:"<< duration <<'\n';

  start = std::clock();
  secretKey.Decrypt(resSumQuad, cSumQuad);
  duration = ( std::clock() - start ) / (double) (CLOCKS_PER_SEC);
  cout<<"Decryption:"<< duration <<'\n';
  
  //comparison
  bool success = ((resSum.message == sum) && (resSumMult.message == sumMult) &&
                  (resProd.message == prod) && (resProd2.message == prod2) &&
                  (resSumQuad == sumQuad));
  
  if (disp || !success) {
    cout << "Seed: " << seed << endl << endl;
    
    if (resSum.message != sum) {
      cout << "Add failed." << endl;
    }
    if (resSumMult.message != sumMult) {
      cout << "Adding multiple times failed." << endl;
    }
    if (resProd.message != prod) {
      cout << "Multiply failed." << endl;
    }
    if (resProd2.message != prod2) {
      cout << "Squaring failed." << endl;
    }
    if (resSumQuad.message != sumQuad) {
      cout << "Sum and quad failed." << endl;
    }
  }
  
  if (disp || !success) {
    cout << "Test " << (success ? "SUCCEEDED" : "FAILED") << endl;
  }

  return success;
}