predicate_numerator *predicate_numerator_alloc(pred *preds, size_t sz) {
  predicate_numerator *pred_num = malloc(sizeof(predicate_numerator));
  assert(pred_num);

  pred_num->uniq_sz    = sz;
  pred_num->uniq_preds = preds;
  
  /* alloc mem for reverse index */
  for(uint32_t ar = 0; ar <= 2; ++ar) {
    uint64_t num = int_pow2(int_pow(K, ar));
    pred_num->uniq_pred_idx[ar] = malloc(num * sizeof(pred_idx_t)); 
    assert(pred_num->uniq_pred_idx[ar] != NULL);
    memset(pred_num->uniq_pred_idx[ar], 0xFF, num * sizeof(pred_idx_t));
  }
  
  /* construct reverse index */  
  for(size_t pidx = 0; pidx < pred_num->uniq_sz; ++pidx) {
    pred *p = pred_num->uniq_preds + pidx;
    assert(p->arity <= 2 && "predicate numerator supports predicates of arity <= 2 only");
    assert(p->data <= int_pow2(int_pow(K, p->arity)) && "predicate_numerator: broken predicate");
    pred_num->uniq_pred_idx[p->arity][p->data] = pidx;
  }
  
  return pred_num;
}
Ejemplo n.º 2
0
void pred_extensional(const pred *pred, uint32_t **ext, size_t *size) {
  *ext = malloc(int_pow(K, pred->arity) * sizeof(uint32_t));
  assert(*ext != NULL);
  size_t _size = 0;
  for(int64_t shift = int_pow(K, pred->arity) - 1; shift >= 0; --shift) {
    if(pred_compute(pred, shift)) {
      (*ext)[_size] = shift;
      ++_size;
    }
  }
  *size = _size;
}
Ejemplo n.º 3
0
static int64_t int_sin(int64_t a)
{
    if (a < 0)
        a = MY_PI - a;  // 0..inf
    a %= 2 * MY_PI;     // 0..2PI

    if (a >= MY_PI * 3 / 2)
        a -= 2 * MY_PI; // -PI / 2 .. 3PI / 2
    if (a >= MY_PI / 2)
        a = MY_PI - a;  // -PI / 2 ..  PI / 2

    return a - int_pow(a, 3) / 6 + int_pow(a, 5) / 120 - int_pow(a, 7) / 5040;
}
Ejemplo n.º 4
0
struct preconditioner *new_preconditioner(int n, int nlevels)
{
	struct preconditioner *precon = ec_malloc(sizeof(*precon));
	precon->level = 0;
	precon->nmg = new_int_array(nlevels);
	for (int i = 0; i < nlevels; ++i) {
		precon->nmg[i] = max(2, (n - 1) / int_pow(2, i) + 1);
		if (precon->nmg[i] == 2) {
			nlevels = i + 1;
			break;
		}
	}
	precon->nlevels = nlevels;
	precon->geom = new_ptr_array(nlevels, 0, NULL);
	precon->basis = new_ptr_array(2 * nlevels - 1, 0, NULL);
	precon->poisson_diag = new_ptr_array(nlevels, 0, NULL);
	precon->helmholtz_diag = new_ptr_array(nlevels, 0, NULL);
	precon->stiffsum_scale = new_ptr_array(nlevels, 0, NULL);
	precon->dirichlet_mask = new_ptr_array(nlevels, 0, NULL);
	precon->sendcounts = new_ptr_array(nlevels, 0, NULL);
	precon->senddispls = new_ptr_array(nlevels, 0, NULL);
	precon->poisson_eigen_max = new_1d_array(nlevels);
	precon->helmholtz_eigen_max = new_1d_array(nlevels);

	return precon;
}
/* (declare-fun p_name (E3 E3 ...) Bool) */
Z3_func_decl gen_pred(z3_wrapper *z3, const char *name, const pred *pred) {
  Z3_symbol symb = Z3_mk_string_symbol(z3->ctx, name);
  Z3_sort domain[pred->arity];
  for(size_t i = 0; i < pred->arity; ++i) {
      domain[i] = z3->Ek_sort;
  }
  Z3_sort range = Z3_mk_bool_sort(z3->ctx);
  Z3_func_decl p = Z3_mk_func_decl(z3->ctx, symb, pred->arity, domain, range);
  
  /* create assertions */
  for(size_t xs = 0; xs < int_pow(K, pred->arity); ++xs) {
    /* represent `xs` in the K-ary form,
     * with digits[0] being the highest digit. */
    uint32_t digits[pred->arity];
    get_K_digits(digits, pred->arity, xs);
    
    Z3_ast args[pred->arity];
    for(size_t i = 0; i < pred->arity; ++i) {
      args[i] = z3->Ek_consts[digits[i]];
    }
    Z3_ast phi = Z3_mk_app(z3->ctx, p, pred->arity, args);

    /* if the predicate equals to false on `xs` */
    if(!pred_compute(pred, xs)) {
      phi = Z3_mk_not(z3->ctx, phi);
    }

    /* printf("\n%s\n", Z3_ast_to_string(z3->ctx, phi)); */
    Z3_solver_assert(z3->ctx, z3->solver, phi);
  }

  return p;
}
Ejemplo n.º 6
0
Archivo: main.c Proyecto: caasi/UVaOJ
int int_pow(int num, int power)
{
	int tmp;
	if(power == 0)
		return 1;
	else
	{
		if(power % 2 == 0)
			return int_pow(num, power - 1) * num;
		else
		{
			tmp = int_pow(num, power / 2);
			return tmp * tmp;
		}
	}
}
Ejemplo n.º 7
0
static inline int geometric_series(int width, int depth)
{
	/*
	 * the main idea behind this formula is the following math base:
	 * a^n - b^n = (a-b)*(a^(n-1) + b*a^(n-2) + b^2*a^(n-3) ... +b^(n-1))
	 * if we set a = 1, b = w (width) we will turn it to:
	 *
	 *        1 - w^n = (1-w)*(1 + w + w^2 + ... +w^(n-1))     (1)
	 *        C = (1 + w + w^2 + ... +w^(n-1))                 (2)
	 *
	 * is perfectly a number of children in the tree with width w.
	 * So we can calculate C from (1):
	 *
	 *        1 - w^n = (1-w)*C =>  C = (1-w^n)/(1-w)          (3)
	 *
	 * (2) takes (n-1) sums and (n-2) multiplication (or (n-2) exponentiations).
	 * (3) takes n+1 multiplications (or one exponentiation), 2 subtractions,
	 * 1 division.
	 * However if more optimal exponentiation algorithm is used like this
	 * (https://en.wikipedia.org/wiki/Exponentiation_by_squaring) number of
	 * multiplications will be O(log(n)).
	 * In case of (2) we will still need all the intermediate powers which
	 * doesn't allow to benefit from efficient exponentiation.
	 * As it is now - int_pow(x, n) is a primitive O(n) algorithm.
	 *
	 * w = 1 is a special case that will lead to divide by 0.
	 * as C (2) corresponds to a full number of nodes in the tree including
	 * the root - we need to return analog in the w=1 tree which is
	 * (depth+1).
	 */
	return (width == 1) ?
		(depth+1) : (1 - (int_pow(width, (depth+1)))) / (1 - width);
}
Ejemplo n.º 8
0
const char *pred_print_extensional_ex(const pred *p) {
  assert(K <= 9 && "Only one-digit Ek elems are suppored");
  assert(p->arity <= 2);
  static char _str[64];
  char *str = _str;
  
  /* treat predicates of zero arity specially */
  if(p->arity == 0) {
    if(p->data != 0) sprintf(str, "{<>}");
    else sprintf(str, "{}");
    return str;
  }

  str += sprintf(str, "{");
  int flag = 0; /* if we've written at least one tuple. */
  for(int64_t shift = int_pow(K, p->arity) - 1; shift >= 0; --shift) {
    if(pred_compute(p, shift)) {
      if(flag) str += sprintf(str, ", ");
      uint32_t digits[p->arity];
      get_K_digits(digits, p->arity, shift);
      for(size_t i = 0; i < p->arity; ++i) {
        str += sprintf(str, "%u", digits[i]);
      }
      flag = 1;
    }
  }
  str += sprintf(str, "}");
  
  return _str;
}
Ejemplo n.º 9
0
// TODO: implement adaptive supersampling
RGBColour World::colourForPixelAt(int i, int j) {
	double d = viewport.getViewingDistance();

	if (ss_level == 1) {
		// no super-sampling
		double uValue = viewport.uAmount(i, 1, 0, false);
		double vValue = viewport.vAmount(j, 1, 0, false);
		vec3 direction = wAxis.scaled(-d) + uAxis.scaled(uValue) + vAxis.scaled(vValue);
		Ray theRay(cameraPosition, direction);

		return RGBColour(traceRay(theRay, 0.0, 0));
	}

	// 0.01 => x16, 1.2 => x64
	double thresholds[2] = {0.01, 1.2}; 


	double var = 0.0;
	int lvl_log = 2;

	RGBVec pixelColour;

	while (lvl_log <= ss_level) {
		pixelColour = RGBVec(0.0,0.0,0.0);

		int lvl = int_pow(2, lvl_log - 1); 

		double scale_factor = 1.0 / static_cast<double>(lvl*lvl);

		vec3 sum_x_sq(0.0,0.0,0.0);

		for (int a = 0; a < lvl; a++) {
			for (int b = 0; b < lvl; b++) {
				double uValue = viewport.uAmount(i, ss_level, a, lvl_log > 2);
				double vValue = viewport.vAmount(j, ss_level, b, lvl_log > 2);
				vec3 direction = wAxis.scaled(-d) + uAxis.scaled(uValue) + vAxis.scaled(vValue);	
				Ray theRay(cameraPosition, direction);
				RGBVec sample = traceRay(theRay, 0.0, 0).scaled(scale_factor);
				pixelColour += sample;
				if (ss_level > 2) sum_x_sq += sample.getVector().pointwise(sample.getVector());
			}
		}		

		if (lvl_log == 2 && ss_level > 2) {
			vec3 varvec = sum_x_sq.scaled(scale_factor) - pixelColour.getVector().pointwise(pixelColour.getVector());
			var = varvec.magnitude();
			if (i % 100 == 0 && j % 100 == 0) std::cout << "var: " << var << std::endl;
		}

		if (var < thresholds[lvl_log - 2] || lvl_log == ss_level) break;
		lvl_log++;
	}

	if (lvl_log == 2) renderStats.ss_x4++;
       	else if (lvl_log == 3) renderStats.ss_x16++;
	else if (lvl_log == 4) renderStats.ss_x64++;

	return RGBColour(pixelColour);
}	
Ejemplo n.º 10
0
void pred_print_extensional(char *str, const pred *pred) {
  for(int64_t shift = int_pow(K, pred->arity) - 1; shift >= 0; --shift) {
    if(pred_compute(pred, shift)) {
      str += sprintf(str, "1");
    } else {
      str += sprintf(str, "0");
    }
  }
}
/* given a model, return the tuple of characters corresponding to the
   specified state */
void get_state_tuple(TreeModel *mod, char *tuple, int state) {
  int alph_size = (int)strlen(mod->rate_matrix->states);
  int tuple_idx;
  for (tuple_idx = -1*mod->order; tuple_idx <= 0; tuple_idx++) {
    int projection = (state / int_pow(alph_size, -1 * tuple_idx)) % 
      alph_size;
    tuple[tuple_idx + mod->order] = mod->rate_matrix->states[projection];
  }
  tuple[mod->order+1] = '\0';
}
Ejemplo n.º 12
0
double ln_IE1(double x)
{
	long double v = 0.0L, s, y;
	int i = 1;

	if(x <= 0.0) return NAN;
	if(x == 1.0) return 0.0;

	if(x < 1.0)
	{ // Tylor series
		y = x - 1.0;
		
		do
		{
			s = 1.0L / i;
			s = (i & 1) ? s : -s;

			s *= int_pow(y, i);

			v += s;

			i++;

		} while(ABS(s) >= MIN_STEP);
	}
	else
	{ // Euler transformation
		y = x / (long double) (x - 1.0);

		do
		{
			s = 1.0L / y;
			s = int_pow(s, i);
			s /= i;

			i++;

			v += s;
		} while(ABS(s) >= MIN_STEP);
	}

	return (double) v;
}
Ejemplo n.º 13
0
static inline int dep(int total, int width)
{
	int i;
	int x = 0;

	for (i = 1; x < total-1; i++) {
		x += int_pow(width, i);
	}

	return i-1;
}
Ejemplo n.º 14
0
int pred_consistent(const pred *pred) {
  uint64_t shift = int_pow(K, pred->arity);
  /* check that the `struct pred` has enough bits to store the predicate's
   * content */
  if(shift > 64) return 0;

  /* check that all unnecessary bits in pred->data are set to zero */
  if(pred->data >= (uint64_t)1 << shift) return 0;
  
  return 1;
}
Ejemplo n.º 15
0
void fun_scan(const char *str, fun *fun) {
  unsigned k, ar;
  sscanf(str, "fun%u_%u_", &k, &ar);
  str += 7;
  assert(k == K);
  fun_init(fun, ar);
  for(int64_t tuple = int_pow(K, fun->arity) - 1; tuple >= 0; --tuple) {
    fun_set_val(fun, tuple, *str-'0');
    ++str;
  }
}
Ejemplo n.º 16
0
void fun_print_verbosely(FILE *fd, const fun *fun) {
  for(int64_t tuple = int_pow(K, fun->arity) - 1; tuple >= 0; --tuple) {
    uint32_t val = fun_compute(fun, tuple);
    if(val) {
      uint32_t digits[fun->arity];
      get_K_digits(digits, fun->arity, tuple);
      for(int i = 0; i < fun->arity; ++i) {
        fprintf(fd, "%d", digits[i]);
      }
      fprintf(fd, " :-> %d\n", val);
    }
  }
}
Ejemplo n.º 17
0
/** Test that `pred_construct` returns a valid predicate.
 */
void test_pred_construct() {
  srand(clock());

  uint64_t arity = 0;
  do {
  int shift = int_pow(K, arity);
    for(int j = 0; j < 2*shift; ++j) {
      char str[pred_print_extensional_size()];
      random_pred_extensional(arity, str);
      
      pred pred;
      assert(pred_construct(arity, str, &pred));
      assert(pred_consistent(&pred));
      
      char str2[pred_print_extensional_size()];
      pred_print_extensional(str2, &pred);
      assert(strcmp(str, str2) == 0);
    }
      
    ++arity;
  } while (int_pow(K, arity) <= 64);
}
Ejemplo n.º 18
0
char *fun_print(const fun *fun) {
  char *_str = malloc(fun_print_size(fun) * sizeof(char));
  
  char *str = _str;
  str += sprintf(str, "fun%u_%lu_", K, fun->arity);
  for(int64_t xs = int_pow(K, fun->arity) - 1; xs >= 0; --xs) {
    uint64_t offset, shift, mask;
    fun_offset_shift_mask(xs, &offset, &shift, &mask);
    uint64_t y = (fun->data[offset] & mask) >> shift;
    str += sprintf(str, "%ld", y);
  }
  return _str;
}
int basesToRow(int *previousBases, int norder, int alph_size) {
  int col = 0;
  int i;
	
  if(norder < 0) //Order must be positive
    die("Order of Markov Matrix must be zero or greater");
  if (alph_size <= 0) //Alphabet size must be at least 1
    die("Alphabet size must be at least 1");
	
  for (i = norder-1; i >= 0; i--)
    col += int_pow(alph_size, i) * (previousBases[norder-(i+1)]);

  return col;
}
Ejemplo n.º 20
0
/*
 * CIE lightness to PWM conversion.
 *
 * The CIE 1931 lightness formula is what actually describes how we perceive
 * light:
 *          Y = (L* / 902.3)           if L* ≤ 0.08856
 *          Y = ((L* + 16) / 116)^3    if L* > 0.08856
 *
 * Where Y is the luminance, the amount of light coming out of the screen, and
 * is a number between 0.0 and 1.0; and L* is the lightness, how bright a human
 * perceives the screen to be, and is a number between 0 and 100.
 *
 * The following function does the fixed point maths needed to implement the
 * above formula.
 */
static u64 cie1931(unsigned int lightness, unsigned int scale)
{
	u64 retval;

	lightness *= 100;
	if (lightness <= (8 * scale)) {
		retval = DIV_ROUND_CLOSEST_ULL(lightness * 10, 9023);
	} else {
		retval = int_pow((lightness + (16 * scale)) / 116, 3);
		retval = DIV_ROUND_CLOSEST_ULL(retval, (scale * scale));
	}

	return retval;
}
Ejemplo n.º 21
0
int pred_construct(uint32_t arity, const char *str, pred *pred) {
  pred->arity      = arity;
  pred->data       = 0;

  for(int pos = int_pow(K, arity) - 1; pos >= 0; --pos) {
    pred->data <<= 1;
    switch(*str) {
    case 0:     return 0;
    case '0':   break;
    case '1':   pred->data |= 1; break;
    default:    return 0;
    }
    ++str;
  }

  /* if the string have not finished yet */
  if(*str) return 0;
  return 1;
}
Ejemplo n.º 22
0
void get_function(z3_wrapper *z3, Z3_func_decl fun, uint32_t fun_arity, struct fun *kfun) {
  Z3_model m = Z3_solver_get_model(z3->ctx, z3->solver);
  assert(m);
  
  /* printf("------\n%s\n------\n", Z3_model_to_string(z3->ctx, m)); */
  
  Z3_model_inc_ref(z3->ctx, m);
  
  fun_init(kfun, fun_arity);
  for(size_t xs = 0; xs < int_pow(K, fun_arity); ++xs) {
    /* represent `xs` in the K-ary form,
     * with digits[0] being the highest digit. */
    uint32_t digits[fun_arity];
    get_K_digits(digits, fun_arity, xs);
    Z3_ast args[fun_arity];
    for(size_t i = 0; i < fun_arity; ++i) {
      args[i] = z3->Ek_consts[digits[i]];
    }
    
    /* eval func on given args */
    Z3_ast t = Z3_mk_app(z3->ctx, fun, fun_arity, args);
    Z3_ast res;
 
    assert(Z3_model_eval(z3->ctx, m, t, 1, &res) == Z3_TRUE);


    /* printf("%s == %s\n", Z3_ast_to_string(z3->ctx, t), Z3_ast_to_string(z3->ctx, res)); */
    
    /* interpret the result of function application */
    uint64_t y;
    sscanf(Z3_ast_to_string(z3->ctx, res), "V%lu", &y);
    fun_set_val(kfun, xs, y);
  }
  
  Z3_model_dec_ref(z3->ctx, m);
}
Ejemplo n.º 23
0
static inline int geometric_series(int width, int depth)
{
	return (1 - (int_pow(width, (depth+1)))) / (1 - width);
}
Ejemplo n.º 24
0
long double int_pow(long double x, int n) { return n == 0 ? 1.0L : (n < 0 ? 1.0L / int_pow(x, -n) : x * int_pow(x, n - 1)); }
Ejemplo n.º 25
0
int main(int arg, char *argv[])
{
	//socket variable
	SOCKET sock;
	const char *IP="192.168.128.3";
	init_TCP_client(&sock, IP, Port);
        get_RP_settings(&sock);
        printf("x0=%f\n",x0);
        printf("xf=%f\n",xf);
        printf("dec=%i\n",dec);
        printf("Nline=%i\n",Nline);
        printf("sector=%f\n",sector);
	printf("mode_RP=%i\n",mode_RP);

	char name[50];

	Npoint=(int)(2.0*(xf-x0)*125.0/1.48/((double)dec));
	if (Npoint>16384){Npoint=16384;}
	printf("Npoint = %i\n",Npoint);

	int powd, pad_len;
        if (power_two(Npoint,&powd)){powd++;}
        pad_len=int_pow(2,powd);
	init_table(pad_len);

	//gnuplot variable
	gnuplot_ctrl * h;
	int i, j;

	double **x = NULL;
        double **y = NULL;
        int **z = (int **)malloc(Nline*sizeof(int *));
	double *tmp = (double *)malloc(Npoint*sizeof(int));
	double *tmp2 = (double *)malloc(pad_len*sizeof(double));
	double **env = (double **)malloc(Nline*sizeof(double *));
	double *pad = (double *)malloc(pad_len*sizeof(double));
        x = malloc(Nline*sizeof(double *));
        y = malloc(Nline*sizeof(double *));

        for (i=0 ; i<Nline ; i++)
        {
                x[i]=malloc((Npoint)*sizeof(double));
                y[i]=malloc((Npoint)*sizeof(double));
                z[i]=malloc((Npoint)*sizeof(int));
		env[i]=(double *)malloc(pad_len*sizeof(double));
		if (env[i]==NULL) {printf("boulet!");}
		for (j=0 ; j<pad_len ; j++) {env[i][j]=0.0;}
        }

        init_xy(x,y);

	//gnuplot object
	h=gnuplot_init();
	gnuplot_cmd(h, "set pm3d map");
	gnuplot_cmd(h, "set palette gray");

	int k=1, line=0, l=0, temp;

	if (mode_RP==0)
	{
		int16_t *buff=(int16_t *)malloc((Npoint+1)*sizeof(int16_t));
		float fech, f0, fm;
		fech=125000000.0/dec;
		f0=3500000.0;
		fm=6500000.0;

	        i=0;
	        j=0;
	        while (1)
	        {
	                receive_int16_TCP_client(&sock, buff, Npoint+1);
	                j=i;
	                i=(int)buff[0];
	                if (j==63 && i==64) {break;}
	                if (j==2 && i==1) {break;}
	        }


		while(k)
		{
			temp=l/10;
			for (i=0 ; i<Nline ; i++)
			{
				if (receive_int16_TCP_client(&sock, buff, Npoint+1)==1)
				{
					i=Nline+2;
					k=0;
					break;
				}
	
				if(i<Nline+2)
				{
					line=(int)buff[0]-1;
					for (j=0 ; j<Npoint ; j++) {tmp[j]=(double)buff[j+1];}
					zero_padding(tmp, pad, Npoint, pad_len, 1); 
					tmp2=env[line];
                        		envelope(pad, tmp2, pad_len, fech, f0, fm, 0);
					if (temp*10==l) {z[line]=tmp[j];}
					//for (j=0 ; j<Npoint ; j++) {z[line][j]=(int)env[j];}
				}
			}
			gnuplot_matrix_double(h, env, Npoint, Nline);
			if (temp*10==l)
			{
				sprintf(name, "int%i.txt", l);
				writefile_double(env, Nline, Npoint, name);
			}
			l++;
		}
		free(buff);
	}

	else if (mode_RP==1)
	{
		char *buff=(char *)malloc((Npoint+1)*sizeof(char));
		while(k)
		{
			for (i=0 ; i<Nline ; i++)
			{
				if (receive_TCP_client(&sock, buff, Npoint+1)==1)
				{
					i=Nline+2;
					k=0;
					break;
				}
	
				if(i<Nline+2)
				{
					line=int_converter(buff[0])-1;
					for (j=0 ; j<Npoint ; j++) {z[line][j]=int_converter(buff[j+1]);}
				}
			}
			gnuplot_matrix(h, z, Npoint, Nline);
			temp=l/10;
			if (temp*10==l)
			{
				sprintf(name, "char%i.txt", l);
				writefile(z, Nline, Npoint, name);
			}
			l++;
		}
		free(buff);
	}

	else {printf("problem with RP settings\n");}


	/*int k=1, line=0, l=0;
	while(k)
	{
		for (i=0 ; i<Nline ; i++)
		{
			if (receive_TCP_client(&sock, buff, Npoint+1)==1)
			{
				i=Nline+2;
				k=0;
				break;
			}
			
			if (i<Nline+2)
			{
				line=int_converter(buff[0])-1;
				for (j=0 ; j<Npoint-1 ; j++)
				{
					//z[line][j]=(int)buff[j+1];
					z[line][j]=int_converter(buff[j+1]);
				}
			}
		}
		gnuplot_matrix(h, z, Npoint, Nline);	
		//sprintf(name, "main%i.txt",l);
		//writefile(z, Nline, Npoint, name);
		l++;
	}*/

	usleep(30);
	close(sock);
	free(x);
	free(y);
	free(z);
	free(env);
	free(pad);
	return 0;
}
Ejemplo n.º 26
0
// this is not optimal but we don't care
// for tiny n
int int_pow(int x, int n) {
	if (n == 0) return 1;
	return x * int_pow(x, n-1);
}
Ejemplo n.º 27
0
int main(int arg, char *argv[])
{
	int Npoint;

	//socket variable
	SOCKET sock;
	const char *IP="192.168.128.3";
	init_TCP_client(&sock, IP, Port);
	get_RP_settings(&sock);
	printf("r0=%f\n",r0);
        printf("rf=%f\n",rf);
        printf("dec=%i\n",dec);
        printf("Nline=%i\n",Nline);
        printf("sector=%f\n",sector);
	printf("mode_RP=%i\n",mode_RP);

	int l=0;

	Npoint=(int)(2.0*(rf-r0)*125.0/1.48/((double)dec));
        if (Npoint>16384) {Npoint=16384;}
	printf("Npoint = %i\n",Npoint);

	int powd, pad_len;
        if (power_two(Npoint,&powd)){powd++;}
        pad_len=int_pow(2,powd);
	init_table(pad_len);
	float fech=125000000.0/((float)dec);

	//gnuplot variable
	gnuplot_ctrl * h;
	double *y= (double *)malloc(Npoint*sizeof(double));
	int i;

	int Ymax=1.5;

	//gnuplot object
	h=gnuplot_init();
	gnuplot_setstyle(h,"lines");
	gnuplot_set_xlabel(h,"time (us)");
	gnuplot_set_ylabel(h,"signal");
	//gnuplot_cmd(h,"set yrange [0:%d]", 2*Ymax);
	gnuplot_cmd(h,"set xrange [0:%d]",Npoint-1);

	char name[30];

	if (mode_RP==0)
	{

		int powd, pad_len;
		if (power_two(Npoint,&powd)){powd++;}
        	pad_len=int_pow(2,powd);
		double *pad=NULL;
		pad=(double *)malloc(pad_len*sizeof(double));
		double *env=NULL;
		env=(double *)malloc(pad_len*sizeof(double));
	
		gnuplot_cmd(h,"set yrange [-0.01:1.5]");
		gnuplot_cmd(h,"set xrange [0:%d]",Npoint-1);
		int16_t *buff=(int16_t *)malloc((Npoint+1)*sizeof(int16_t));
		while(1)
		{
			if(receive_int16_TCP_client(&sock, buff, Npoint+1)==1){break;}
			for (i=1 ; i<Npoint+1 ; i++){y[i-1]=(double)(buff[i])/409.6;} //divide by 409.6 to have voltage value
			zero_padding(y, pad, Npoint, pad_len, 1);
			envelope(pad, env, pad_len, fech, fmin, fmax, 0);
			gnuplot_resetplot(h);
			//gnuplot_plot_x(h, y, Npoint, "Oscillo int16_t");
			gnuplot_plot_x(h, env, pad_len, "Oscillo int16_t");			
                        //sprintf(name, "int%i.txt", l);
                        //writefile(y, Npoint, name);
                        l++;

		}
		free(buff);
		free(pad);
		free(env);
	}

	else if (mode_RP==1)
	{
		char *buff=(char *)malloc((Npoint+1)*sizeof(char));
		while(1)
		{
			if(receive_TCP_client(&sock, buff, Npoint+1)==1){break;}
			for (i=1 ; i<Npoint+1 ; i++){y[i-1]=(double)(int_converter(buff[i]));} 
			gnuplot_resetplot(h);
			gnuplot_plot_x(h, y, Npoint, "Oscillo 256 gray");
			sprintf(name, "char%i.txt", l);
			//writefile(y, Npoint, name);
			l++;
		}
		free(buff);
	}

	else {printf("Problem of settings\n");}

	usleep(30);
	close(sock);
	free(y);

	return 0;
}
Ejemplo n.º 28
0
CAMLprim value int_math_int_pow_stub(value base, value exponent) {
  return (Val_long(int_pow(Long_val(base), Long_val(exponent))));
}
Ejemplo n.º 29
0
void *worker(void *params) { // life cycle of a cracking pthread
  uint64_t e_be; // storage for our "big-endian" version of e
  uint8_t buf[SHA1_DIGEST_LEN],
          der[RSA_EXP_DER_LEN + 1], // TODO: is the size of this right?
          optimum = *(uint8_t*)params;
  //char onion[BASE32_ONIONLEN];
  char onion[DESC_ID_V2_LEN_BASE32 + 1];
  //char identity_key_base32[DESC_ID_V2_LEN_BASE32 + 1];
  SHA_CTX hash, copy;
  RSA *rsa;

  while(!found) {
    // keys are only generated every so often
    // every 549,755,781,120 tries by default

    if(optimum)
      rsa = easygen(RSA_OPTM_BITLEN - RSA_PK_E_LENGTH * 8, RSA_PK_E_LENGTH,
                    der, RSA_OPT_DER_LEN, &hash);
    else
      rsa = easygen(RSA_KEYS_BITLEN, RSA_PK_E_LENGTH, der, RSA_EXP_DER_LEN,
                    &hash);

    if(!rsa) // if key generation fails (no [P]RNG seed?)
      error(X_KEY_GEN_FAILS);

    uint8_t e_bytes = RSA_PK_E_LENGTH; // number of bytes e occupies
    uint64_t e = RSA_PK_EXPONENT;      // public exponent
    uint64_t e_byte_thresh;

    int_pow(2, e_bytes * 8, &e_byte_thresh);
    e_byte_thresh++;

    uint8_t *e_ptr = ((uint8_t*)&e_be) + 8 - e_bytes;

    while((e <= elim) && !found) { // main loop
      // copy the relevant parts of our already set up context
      memcpy(&copy, &hash, SHA_REL_CTX_LEN); // 40 bytes here...
      copy.num = hash.num;                   // and don't forget the num (9)

      // convert e to big-endian format
      e_be = htobe64(e);

      // compute SHA1 digest (majority of loop time spent here!)
      SHA1_Update(&copy, e_ptr, e_bytes);
      SHA1_Final(buf, &copy);

      base32_onion(onion, buf); // base32-encode SHA1 digest
      loop++;                   // keep track of our tries...

      if(!regexec(regex, onion, 0, 0, 0)) { // check for a match

        // let our main thread know on which thread to wait
        lucky_thread = pthread_self();
        found = 1; // kill off our other threads, asynchronously

        if(monitor)
          printf("\n"); // keep our printing pretty!

        if(!BN_bin2bn(e_ptr, e_bytes, rsa->e)) // store our e in the actual key
          error(X_BIGNUM_FAILED);              // and make sure it got there

        if(!sane_key(rsa))        // check our key
          error(X_YOURE_UNLUCKY); // bad key :(

        print_onion(onion); // print our domain
        print_prkey(rsa);   // and more importantly the key

        RSA_free(rsa); // free up what's left

        return 0;
      }

      e += 2; // do *** NOT *** forget this!

      if(e == e_byte_thresh) { // ASN.1 stuff (hey, it could be worse!)
        // calculate our new threshold
        int_pow(2, ++e_bytes * 8, &e_byte_thresh);
        e_byte_thresh++;

        if(optimum) {
          RSA_free(rsa);
          easygen(RSA_OPTM_BITLEN - e_bytes * 8, e_bytes, der, RSA_OPT_DER_LEN,
                  &hash);

          if(!rsa)
            error(X_KEY_GEN_FAILS);
        } else {
          // play with our key structure (do not try this at home!)
          der[RSA_ADD_DER_OFF]++;
          der[RSA_EXP_DER_LEN - RSA_PK_E_LENGTH - 1]++;

          // and our prebuilt hash
          SHA1_Init(&hash); // TODO: move to a function
          SHA1_Update(&hash, der, RSA_EXP_DER_LEN - RSA_PK_E_LENGTH);
        }

        e_ptr--; // and move the pointer back
      }
    }
    RSA_free(rsa);
  }

  return 0;
}
Ejemplo n.º 30
0
CAMLprim value int_math_int64_pow_stub(value base, value exponent) {
  CAMLparam2(base, exponent);
  CAMLreturn(caml_copy_int64(int_pow(Int64_val(base), Int64_val(exponent))));
}