Пример #1
0
inline bool isComposite(long long unsigned int base, long long unsigned int number)
{
	if(number % 2 == 0)
		return true;

	unsigned int power = 0;
	long long unsigned int temp = number - 1,
		 act = 0;
	while(temp % 2 == 0 && ++ power)
		temp /= 2;

	act = fastPower(base, temp, number);
	if(act == 1 || act == number - 1)
		return false;

	for(unsigned int r = 1; r < power; ++ r)
	{
		act = multiply(act, act, number);
		if(act == 1)
			return true;

		if(act == number - 1)
			return false;
	}

	return true;
}
Пример #2
0
// incrementalZeroTest sets each res[i], for i=0..n-1, to
// a ciphertext in which each slot is 0 or 1 according
// to whether or not bits 0..i of corresponding slot in ctxt
// is zero (1 if not zero, 0 if zero).
// It is assumed that res and each res[i] is already initialized
// by the caller.
// Complexity: O(d + n log d) smart automorphisms
//             O(n d) 
void incrementalZeroTest(Ctxt* res[], const EncryptedArray& ea,
			 const Ctxt& ctxt, long n)
{
  FHE_TIMER_START;
  long nslots = ea.size();
  long d = ea.getDegree();

  // compute linearized polynomial coefficients

  vector< vector<ZZX> > Coeff;
  Coeff.resize(n);

  for (long i = 0; i < n; i++) {
    // coeffients for mask on bits 0..i
    // L[j] = X^j for j = 0..i, L[j] = 0 for j = i+1..d-1

    vector<ZZX> L;
    L.resize(d);

    for (long j = 0; j <= i; j++) 
      SetCoeff(L[j], j);

    vector<ZZX> C;

    ea.buildLinPolyCoeffs(C, L);

    Coeff[i].resize(d);
    for (long j = 0; j < d; j++) {
      // Coeff[i][j] = to the encoding that has C[j] in all slots
      // FIXME: maybe encrtpted array should have this functionality
      //        built in
      vector<ZZX> T;
      T.resize(nslots);
      for (long s = 0; s < nslots; s++) T[s] = C[j];
      ea.encode(Coeff[i][j], T);
    }
  }

  vector<Ctxt> Conj(d, ctxt);
  // initialize Cong[j] to ctxt^{2^j}
  for (long j = 0; j < d; j++) {
    Conj[j].smartAutomorph(1L << j);
  }

  for (long i = 0; i < n; i++) {
    res[i]->clear();
    for (long j = 0; j < d; j++) {
      Ctxt tmp = Conj[j];
      tmp.multByConstant(Coeff[i][j]);
      *res[i] += tmp;
    }

    // *res[i] now has 0..i in each slot
    // next, we raise to the power 2^d-1

    fastPower(*res[i], d);
  }
  FHE_TIMER_STOP;
}
Пример #3
0
int main(void)
{
    scanf("%d", &patterns);
    for(int p = 0; p < patterns; ++ p)
    {
        scanf("%s", pattern);
        sum += len[p] = strlen(pattern);
        for(int i = 0; pattern[i]; ++ i)
            hash[p] = hash[p] * 31 + pattern[i] - 'a';

        //printf("%d: %llu\n", p, hash[p]);
    }

    scanf("%s", text);
    temphash = 0;
    for(int t = 0; t < sum; ++ t)
        temphash = 31 * temphash + text[t] - 'a';

    ++ mapped[temphash];
    //printf(":%d: %llu\n", 0, temphash);
    for(int t = 1; text[t + sum - 1]; ++ t)
    {
        temphash = (temphash - (text[t - 1] - 'a') * fastPower(31, sum - 1)) * 31 + text[t + sum - 1] - 'a';
        ++ mapped[temphash];
        //printf(":%d: %llu\n", t, temphash);
    }

    do
    {
        temphash = 0;
        for(int p = 0; p < patterns; ++ p)
            temphash = temphash * fastPower(31, len[perm[p]]) + hash[perm[p]];

        result += mapped[temphash];
        //printf(".%llu\n", temphash);
    }
    while(std::next_permutation(perm, perm + patterns));

    printf("%llu\n", result);
    return 0;
}
Пример #4
0
 /*
  * @param a, b, n: 32bit integers
  * @return: An integer
  */
 int fastPower(int a, int b, int n) {
     // write your code here
     // (ab)%n=a%n * b%n %n
     if (n==0)
         return 1%b;
     if (n==1)
         return a%b;
     long res=fastPower(a,b,n/2);
     res = (res*res)%b;
     if (n%2)
         return (res*(a%b))%b;
     return (int)res%b;
 }
Пример #5
0
long long unsigned int fastPower(long long unsigned int b, long long unsigned int p, long long unsigned int MOD)
{
	if(p == 0 || b == 1)
		return 1;

	long long unsigned int result = fastPower(b, p / 2, MOD);
	result = multiply(result, result, MOD);

	if(p % 2)
		result = multiply(result, b, MOD);

	return result;
}
Пример #6
0
 /*
  * @param a, b, n: 32bit integers
  * @return: An integer
  */
 int fastPower(int a, int b, int n) {
     if (n == 0) {
         return 1 % b;
     }
     if (n == 1) {
         return a % b;
     }
     long long temp = fastPower(a, b, n / 2);
     if (n % 2 == 0) {
         return (temp * temp) % b;
     } else {
         return ((temp * temp) % b * a) % b;
     }
 }
Пример #7
0
inline
long long unsigned int fastPower(long long unsigned int b, long long unsigned int p)
{
    if(!p)
        return 1LLU;

    long long unsigned int mid = fastPower(b, p / 2);
    mid *= mid;

    if(p&1)
        mid *= b;

    return mid;
}
Пример #8
0
Matrix fastPower(Matrix base, long long unsigned int power, long long unsigned int MOD)
{
	if(power == 0)
		return Matrix();

	Matrix R = fastPower(base, power / 2, MOD);
	Matrix Temp;
	Temp.A = (R.A * R.A + R.B * R.C) % MOD;
	Temp.B = (R.A * R.B + R.B * R.D) % MOD;
	Temp.C = (R.C * R.A + R.D * R.C) % MOD;
	Temp.D = (R.C * R.B + R.D * R.D) % MOD;
	R = Temp;
	if(power % 2)
	{
		Temp = Matrix();
		Temp.A = (R.A * base.A + R.B * base.C) % MOD;
		Temp.B = (R.A * base.B + R.B * base.D) % MOD;
		Temp.C = (R.C * base.A + R.D * base.C) % MOD;
		Temp.D = (R.C * base.B + R.D * base.D) % MOD;
		R = Temp;
	}

	return R;
}
Пример #9
0
inline long long unsigned int fibonacci(long long unsigned int which, long long unsigned int MOD)
{
	return fastPower(Matrix(1,1,1,0), which, MOD).A;
}