Example #1
0
void applyLinPoly(GF2E& beta, const vec_GF2E& C, const GF2E& alpha, long p)
{
   long d = GF2E::degree();
   assert(d == C.length());

   GF2E gamma, res;

   gamma = to_GF2E(GF2X(1, 1));
   res = C[0]*alpha;
   for (long i = 1; i < d; i++) {
      gamma = power(gamma, p);
      res += C[i]*to_GF2E(CompMod(rep(alpha), rep(gamma), GF2E::modulus()));
   }

   beta = res;
}
Example #2
0
/* function convertBytesToGF2E : crate GF2E element from byte array
 * param env				   : jni environment
 * param byteArr			   : bytes of the element
 * return GF2E				   : the created element.
 */
GF2E convertBytesToGF2E(JNIEnv * env, jbyteArray byteArr){
	//convert to native object.
	jbyte* bytes = env->GetByteArrayElements(byteArr, 0);
	
	//translate the bytes into a GF2X element.
	GF2X e; 
	GF2XFromBytes(e, (unsigned char*)bytes, env->GetArrayLength(byteArr));
	env->ReleaseByteArrayElements(byteArr, bytes, 0);
	
	//convert the GF2X to GF2E
	return to_GF2E(e);
}
void EvaluationHashFunction::generateFieldElement(unsigned char* inputByteElement, GF2E& outputElement)
{

	//first create a GF2X
	GF2X polynomialElement; 

	//translate the bytes into a GF2X element
	GF2XFromBytes(polynomialElement, inputByteElement, 8);

	
	//convert the GF2X to GF2E
	outputElement = to_GF2E(polynomialElement);
}
Example #4
0
GF2E generateIndexPolynomial(int i){
	
	ZZ index;
	index = i;
	unsigned char* indexBytes = new unsigned char[4];
	BytesFromZZ(indexBytes, index, 4);
	
	GF2X indexPoly;
	GF2XFromBytes(indexPoly, (unsigned char*)indexBytes, 4);
	
	delete (indexBytes);
	
	return to_GF2E(indexPoly);
}
Example #5
0
/* 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;
}
Example #6
0
/* function interpolate		: Interpolate the points to get a polynomial.
 * param t				    : polynomials degree
 * param challenge		    : verifier's challenge
 * param fieldElements		: pointers to the pre calculated GF2E polynomials.
 * param sampledIndexes		: indexes of the pre calculated GF2E polynomials, such that the points are (sampledIndexes[i], fieldElements[i]).
 * return jlong				: pointer to the interpolated polynomial.
 */
jlong interpolate(JNIEnv * env, jbyteArray challenge, jlongArray fieldElements, jintArray sampledIndexes){
	  //convert to native objects
	  jint* indexes = env->GetIntArrayElements(sampledIndexes, 0);
	
	  //Create vectors of polynomials to the interpolate function.
	  vec_GF2E xVector; //the x coordinates
	  vec_GF2E yVector; //the y coordinates

	  int size = env->GetArrayLength(sampledIndexes);
	 
	  //set the length of the arrays to the number of points + the point (0,e)
	  xVector.SetLength(size+1);
	  yVector.SetLength(size+1);

	  //put the first point in the coordinates arrays.
	  yVector[0] = convertBytesToGF2E(env, challenge);
	  xVector[0] = to_GF2E(0);
	  
	  jlong* bElements  = env->GetLongArrayElements(fieldElements, 0); 
	  
	  //put all the other point in the coordinates arrays.
	  for (int i=0; i<size; i++){
		 
		 //put the challenge polynomial in y array
		 GF2E element = (*(GF2E*)bElements[i]); 
		 yVector[i+1] = element; 
		 
		 //put the index polynomial in x array
		 xVector[i+1] = generateIndexPolynomial(indexes[i]);
	  }

	  
	  //create a GF2EX polynomial 
	  GF2EX* polynomial = new GF2EX;
	  
	  //interpolate the points, put the result polynomial in the created polynomial and return it.
	  interpolate(*polynomial, xVector, yVector);
	  
	  //free the allocated memory
	  env->ReleaseIntArrayElements(sampledIndexes, indexes, 0);
	  env->ReleaseLongArrayElements(fieldElements, bElements, 0);
	  return (jlong)polynomial;
}
NTL_CLOSE_NNS

NTL_CLIENT

int main()
{
   GF2X p;

   BuildIrred(p, 200);

   GF2E::init(p);

   GF2EX f;

   SetCoeff(f, 41);
   SetCoeff(f, 1);
   SetCoeff(f, 0);

   GF2X a;
   SetCoeff(a, 117);
   SetCoeff(a, 10);
   SetCoeff(a, 0);

   GF2EX g, h;
   SetX(g);
   SetCoeff(g, 0, to_GF2E(a));

   MinPolyMod(h, g, f);

   f = h;

   vec_pair_GF2EX_long u;

   CanZass(u, f, 1);

   cerr << "factorization pattern:";
   long i;

   for (i = 0; i < u.length(); i++) {
      cerr << " ";
      long k = u[i].b;
      if (k > 1)
         cerr << k << "*";
      cerr << deg(u[i].a);
   }

   cerr << "\n\n\n";

   GF2EX ff;
   mul(ff, u);

   if (f != ff || u.length() != 11) {
      cerr << "GF2EXTest NOT OK\n";
      return 1;
   }

   {

   cerr << "multiplication test...\n";

   BuildIrred(p, 512);
   GF2E::init(p);

   GF2EX A, B, C, C1;


   random(A, 512);
   random(B, 512);

   double t;
   long i;

   t = GetTime();
   for (i = 0; i < 10; i++) PlainMul(C, A, B);
   t = GetTime() - t;
   cerr << "time for plain mul of degree 511 over GF(2^512): " << (t/10) << "s\n";

   t = GetTime();
   for (i = 0; i < 10; i++) mul(C1, A, B);
   t = GetTime() - t;
   cerr << "time for karatsuba mul of degree 511 over GF(2^512): " << (t/10) << "s\n";

   if (C != C1) {
      cerr << "GF2EXTest NOT OK\n";
      return 1;
   }

   }

   {

   cerr << "multiplication test...\n";

   BuildIrred(p, 16);
   GF2E::init(p);

   GF2EX A, B, C, C1;


   random(A, 512);
   random(B, 512);

   double t;

   t = GetTime();
   for (i = 0; i < 10; i++) PlainMul(C, A, B);
   t = GetTime() - t;
   cerr << "time for plain mul of degree 511 over GF(2^16): " << (t/10) << "s\n";

   t = GetTime();
   for (i = 0; i < 10; i++) mul(C1, A, B);
   t = GetTime() - t;
   cerr << "time for karatsuba mul of degree 511 over GF(2^16): " << (t/10) << "s\n";

   if (C != C1) {
      cerr << "GF2EXTest NOT OK\n";
      return 1;
   }

   }

   cerr << "GF2EXTest OK\n";
   return 0;
}