/* Divide a polynomial by a double. The double is passed in by the user. @param polynomial *a address of the polynomial 'a' @param double_coeff used to divide the polynomial @param polynomial *out address of the resultant polynomial @return status is ok if polynomial is valid else returns illegalPoly_math. */ pError divide(polynomial *a, double double_coeff, polynomial *out){ pError status = ok; //run only if the polynomial is valid. if( a->valid == TRUE ){ //sets up the array of coefficients that will be passed to the createPoly func. int size = a->length; double data[size]; //loop through poly 'a' and each element //multiplied by the double coeff for(int i = 0; i < a->length; i++){ //at each iteration store the evaluation to result data[i] = (a->coeff[i])/(double_coeff); //stop 0 from being printed as negitive if (double_coeff < 0 && a->coeff[i] == 0) { data[i] = 0; } } createPolynomial(out, size, data); } else { status = illegalPoly_math; //poly is invalid, return error out->valid = FALSE; } verify(status); //check if ay erros need to be printed return status; }
/* Subtract two polynomials. The function makes sure that the output polynomial will have the correct size. (ie. the biggest of the two input poly). The resulting polynomial is sent back to where the output polynomial was declared through pass by reference. @param polynomial *a address of the first polynomial @param polynomial *b address of the second polynomial @param polynomial *out address of the resultant polynomial @return status is ok if everything went fine else returns illegalPoly_math if one of the polynomials are invalid. */ pError subtract(polynomial *a, polynomial *b, polynomial *out){ pError status = ok; //only run if polynomials are valid if( a->valid == TRUE && b->valid == TRUE){ //sets up the array of coefficients that will be passed to the createPoly func. int size = MAX(a->length, b->length); //we check the max size to remove boundry errors. double data[size]; //loop through both polynomials, subtract each element for(int i = 0; i < size; i++){ //check if each arrays are finished if( i < a->length && i < b->length){ //store the sum of the current elements //in the new polynomial to be returned data[i] = a->coeff[i] - b->coeff[i]; }else{ //throw in whatever's left if one of the arrays are exhausted if( a->length > b->length){ data[i] = a->coeff[i]; }else data[i] = b->coeff[i]; } } createPolynomial(out, size, data); //creates the polynomial. } else { status = illegalPoly_math; //poly is invalid, return error out->valid = FALSE; } verify(status); //check if any errors need to be printed return status; }
void test1() { Polynomial* pol = createPolynomial(3); Polynomial* pol2 = createPolynomial(0); addToPolynomial(pol,-1,1); addToPolynomial(pol,-1,2); addToPolynomial(pol,-1,3); printPolynomial(pol); printf("\n"); addToPolynomial(pol2,5,4); addToPolynomial(pol2,-1,3); addToPolynomial(pol2,5,2); printPolynomial(pol2); printf("\n"); Polynomial* pol3 = mergePolynomials(pol,pol2); printPolynomial(pol3); }
/* function checkPolynomialValidity : Check if the degree pf the polynom is n-k, if Q(i)=ei for all i=1,…,n and if Q(0)=e. * param polynomial : array of the polynom coefficients * param k : number of true statments. the degree of the polynom should be n-k * param verifierChallenge : pointer to the verifier element * param proverChallenges : array that holds the coeficients of elements in the field * param t : degree of the challenges polynomials. * return jboolean : true if all checks return true. */ JNIEXPORT jboolean JNICALL Java_edu_biu_scapi_interactiveMidProtocols_sigmaProtocol_orMultiple_SigmaORMultipleVerifierComputation_checkPolynomialValidity (JNIEnv *env, jobject, jobjectArray polynomial, jint k, jlong verifierChallenge, jobjectArray proverChallenges){ bool valid = true; GF2EX* polynom = new GF2EX; //Create the polynomial out of the coefficeints array. *polynom = createPolynomial(env, polynomial); //check if the degree of the polynomial os n-k, while n is the number of challenges. int size = env -> GetArrayLength(proverChallenges); if (deg(*polynom) != (size - k)){ valid = false; } //check if Q(0)=e. GF2E zero = to_GF2E(0); GF2E e = eval(*polynom, zero); //Q(0) GF2E* challengePointer = (GF2E*) verifierChallenge; if (e != *challengePointer){ valid = false; } //for each one of the challenges, check that Q(i)=ei for (int i = 0; i<size; i++){ //create the challenge element out of the byte array. jbyteArray challenge = (jbyteArray) env -> GetObjectArrayElement(proverChallenges, i); GF2E challengeElement = convertBytesToGF2E(env, challenge); //create the index element GF2E indexElement = generateIndexPolynomial(i+1); //compute Q(i) GF2E result = eval(*polynom, indexElement); //check that Q(i)=ei if (result != challengeElement){ valid = false; } } delete(challengePointer); delete(polynom); return valid; }
int main(){ polynomial a, b, c, sum, difference, product, quotient, normal; double coeff; int order; //these will be converted into dynamic arrays later on. //they just store the coeffs to be used for the polynomials. double data1[3]; data1[2] = 5; double data2[3] = {5.2, -2, 4}; double data3[7]= {5.0, 2.0, 20, 0, 5, 2, 40.2}; //clear the screen to focus on this single session's tests system("clear"); ////////////////////////////////////////////////////////////////// //creating base polynomials we'll be using for the entirity of //these test cases. // //NOTE: second parameter of initalisePolynomial(poly, size) // MUST match the test case arrays above. ////////////////////////////////////////////////////////////////// createPolynomial(&a, 3, data1); createPolynomial(&b, 3, data2); createPolynomial(&c, 7, data3); printf("--------------------------------------------------------------------------------\n"); printf("These are the starting polynomials which will be used for the later operations\n"); printf("--------------------------------------------------------------------------------\n"); printf("Polynomial (A): "); verify(printPolynomial(&a)); printf("Polynomial (B): "); verify(printPolynomial(&b)); printf("Polynomial (C): "); verify(printPolynomial(&c)); ////////////////////////////////////////////////////////////////// //testing add/subtract polynomials of same length ////////////////////////////////////////////////////////////////// printf("--------------------------------------------------------------------------------\n"); printf("testing add/subtract polynomials of same length\n"); printf("--------------------------------------------------------------------------------\n"); printf("Polynomial (A): "); verify(printPolynomial(&a)); printf("Polynomial (B): "); verify(printPolynomial(&b)); //add printf("A + B = "); verify(add(&a, &b, &sum)); verify(printPolynomial(&sum)); //subtract printf("A - B = "); verify(subtract(&a, &b, &difference)); verify(printPolynomial(&difference)); deletePolynomial(&sum); deletePolynomial(&difference); ////////////////////////////////////////////////////////////////// //testing add/subtract polynomials of different length ////////////////////////////////////////////////////////////////// printf("--------------------------------------------------------------------------------\n"); printf("testing add/subtract polynomials of different length\n"); printf("--------------------------------------------------------------------------------\n"); printf("Polynomial (A): "); verify(printPolynomial(&a)); printf("Polynomial (C): "); verify(printPolynomial(&c)); printf("A + C = "); verify(add(&a, &c, &sum)); verify(printPolynomial(&sum)); printf("A - C = "); verify(subtract(&a, &c, &difference)); verify(printPolynomial(&difference)); deletePolynomial(&sum); deletePolynomial(&difference); ////////////////////////////////////////////////////////////////// //testing multiplcation/division by a scalar ////////////////////////////////////////////////////////////////// printf("--------------------------------------------------------------------------------\n"); printf("testing multiplcation/division by a scalar\n"); printf("--------------------------------------------------------------------------------\n"); printf("Polynomial (A): "); printPolynomial(&a); printf("Polynomial (C): "); printPolynomial(&c); coeff = 5; //multiply printf("A * %.2lf = ", coeff); multiply(&a, coeff, &product); printPolynomial(&product); //divide printf("C / %.2lf = ", coeff); divide(&c, coeff, "ient); printPolynomial("ient); deletePolynomial(&product); deletePolynomial("ient); ////////////////////////////////////////////////////////////////// //testing for normalising polynomials ////////////////////////////////////////////////////////////////// printf("--------------------------------------------------------------------------------\n"); printf("testing for normalising polynomials\n"); printf("--------------------------------------------------------------------------------\n"); printf("Polynomial (A): "); printPolynomial(&a); printf("Polynomial (C): "); printPolynomial(&c); //test1 printf("Noramlised (A) = "); normalise(&a, &normal); printPolynomial(&normal); deletePolynomial(&normal); //test2 printf("Noramlised (C) = "); normalise(&c, &normal); printPolynomial(&normal); deletePolynomial(&normal); ////////////////////////////////////////////////////////////////// //testing for order of poly ////////////////////////////////////////////////////////////////// printf("--------------------------------------------------------------------------------\n"); printf("testing for order of poly\n"); printf("--------------------------------------------------------------------------------\n"); printf("Polynomial (A): "); printPolynomial(&a); getOrder(&a, &order); printf("Order: %d\n", order); ////////////////////////////////////////////////////////////////// //delete original polynomials //end of testing. ////////////////////////////////////////////////////////////////// deletePolynomial(&a); deletePolynomial(&b); deletePolynomial(&c); return EXIT_SUCCESS; }