Exemplo n.º 1
0
char filter(){
  fp_t abs = fp_add(fp_add(fp_mul(acc[0], acc[0]),
                           fp_mul(acc[1], acc[1])),
                    fp_mul(acc[2], acc[2]));
  ////////////////////////////////////////////////////////////////////////////////
  //idle state filter
  if (!(fp_cmp(abs, 10486)==1 /*0.32*/ ||
        fp_cmp(abs, 8847)==2 /*0.27*/)) { // if between 0.01 - 0.09
    return false;
  }

  ////////////////////////////////////////////////////////////////////////////////
  // def = directional equivalence filter
  const fp_t def_sensitivity = 2621; //0.08
  if (fp_cmp(acc[0], fp_sub(dir_filter_ref[0], def_sensitivity))==2 ||
      fp_cmp(acc[0], fp_add(dir_filter_ref[0], def_sensitivity))== 1 ||
      fp_cmp(acc[1], fp_sub(dir_filter_ref[1], def_sensitivity))==2 ||
      fp_cmp(acc[1], fp_add(dir_filter_ref[1], def_sensitivity))== 1 ||
      fp_cmp(acc[2], fp_sub(dir_filter_ref[2], def_sensitivity))==2 ||
      fp_cmp(acc[2], fp_add(dir_filter_ref[2], def_sensitivity))==1) {
    dir_filter_ref[0] = acc[0];
    dir_filter_ref[1] = acc[1];
    dir_filter_ref[2] = acc[2];
    return true;
  }
  return false;
}
Exemplo n.º 2
0
void ep_rhs(fp_t rhs, const ep_t p) {
	fp_t t0;
	fp_t t1;

	fp_null(t0);
	fp_null(t1);

	TRY {
		fp_new(t0);
		fp_new(t1);

		/* t0 = x1^2. */
		fp_sqr(t0, p->x);
		/* t1 = x1^3. */
		fp_mul(t1, t0, p->x);

		/* t1 = x1^3 + a * x1 + b. */
		switch (ep_curve_opt_a()) {
			case OPT_ZERO:
				break;
			case OPT_ONE:
				fp_add(t1, t1, p->x);
				break;
#if FP_RDC != MONTY
			case OPT_DIGIT:
				fp_mul_dig(t0, p->x, ep_curve_get_a()[0]);
				fp_add(t1, t1, t0);
				break;
#endif
			default:
				fp_mul(t0, p->x, ep_curve_get_a());
				fp_add(t1, t1, t0);
				break;
		}

		switch (ep_curve_opt_b()) {
			case OPT_ZERO:
				break;
			case OPT_ONE:
				fp_add_dig(t1, t1, 1);
				break;
#if FP_RDC != MONTY
			case OPT_DIGIT:
				fp_add_dig(t1, t1, ep_curve_get_b()[0]);
				break;
#endif
			default:
				fp_add(t1, t1, ep_curve_get_b());
				break;
		}

		fp_copy(rhs, t1);

	} CATCH_ANY {
		THROW(ERR_CAUGHT);
	} FINALLY {
		fp_free(t0);
		fp_free(t1);
	}
}
Exemplo n.º 3
0
char filter(){
  fp_t abs = fp_add(fp_add(fp_mul(acc[0], acc[0]),
                           fp_mul(acc[1], acc[1])),
                    fp_mul(acc[2], acc[2]));
  ////////////////////////////////////////////////////////////////////////////////
  //idle state filter
  if (!(fp_cmp(abs, d2fp(0.32))==1 ||
        fp_cmp(abs, d2fp(0.27))==-1)) { // if between 0.01 - 0.09
    return false;
  }

  ////////////////////////////////////////////////////////////////////////////////
  // def = directional equivalence filter
  fp_t def_sensitivity = d2fp(0.08);
  if (fp_cmp(acc[0], fp_sub(dir_filter_ref[0], def_sensitivity))==-1 ||
      fp_cmp(acc[0], fp_add(dir_filter_ref[0], def_sensitivity))== 1 ||
      fp_cmp(acc[1], fp_sub(dir_filter_ref[1], def_sensitivity))==-1 ||
      fp_cmp(acc[1], fp_add(dir_filter_ref[1], def_sensitivity))== 1 ||
      fp_cmp(acc[2], fp_sub(dir_filter_ref[2], def_sensitivity))==-1 ||
      fp_cmp(acc[2], fp_add(dir_filter_ref[2], def_sensitivity))==1) {
    dir_filter_ref[0] = acc[0];
    dir_filter_ref[1] = acc[1];
    dir_filter_ref[2] = acc[2];
    return true;
  }
  return false;
}
Exemplo n.º 4
0
/**
 * Compute the Miller loop for pairings of type G_2 x G_1 over the bits of a
 * given parameter.
 *
 * @param[out] r			- the result.
 * @param[out] t			- the resulting point.
 * @param[in] q				- the first pairing argument in affine coordinates.
 * @param[in] p				- the second pairing argument in affine coordinates.
 * @param[in] n 			- the number of pairings to evaluate.
 * @param[in] a				- the loop parameter.
 */
static void pp_mil_k12(fp12_t r, ep2_t *t, ep2_t *q, ep_t *p, int m, bn_t a) {
	fp12_t l;
	ep_t _p[m];
	int i, j;

	if (m == 0) {
		return;
	}

	fp12_null(l);

	TRY {
		fp12_new(l);

		for (j = 0; j < m; j++) {
			ep_null(_p[j]);
			ep_new(_p[j]);
#if EP_ADD == BASIC
			ep_neg(_p[j], p[i]);
#else
			fp_add(_p[j]->x, p[j]->x, p[j]->x);
			fp_add(_p[j]->x, _p[j]->x, p[j]->x);
			fp_neg(_p[j]->y, p[j]->y);
#endif
			ep2_copy(t[j], q[j]);
		}

		fp12_zero(l);

		/* Precomputing. */
		pp_dbl_k12(r, t[0], t[0], _p[0]);
		if (bn_get_bit(a, bn_bits(a) - 2)) {
			for (j = 0; j < m; j++) {
				pp_add_k12(l, t[j], q[j], p[j]);
				fp12_mul_dxs(r, r, l);
			}
		}

		for (i = bn_bits(a) - 3; i >= 0; i--) {
			fp12_sqr(r, r);
			for (j = 0; j < m; j++) {
				pp_dbl_k12(l, t[j], t[j], _p[j]);
				fp12_mul_dxs(r, r, l);
				if (bn_get_bit(a, i)) {
					pp_add_k12(l, t[j], q[j], p[j]);
					fp12_mul_dxs(r, r, l);
				}
			}
		}
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		fp12_free(l);
		for (j = 0; j < m; j++) {
			ep_free(_p[j]);
		}
	}
}
Exemplo n.º 5
0
void fp_add_test() {
    unsigned bias = 0b01111111;
    bit16 zero = construct(0b0, 0, 0);
    bit16 one = construct(0b0, 0b01111111, 0b0000000);
    bit16 onePointTwoFive = construct(0b0, 0b01111111, 0b0100000);
    bit16 threePointSevenFive = construct(0b0, 0b10000000, 0b1110000);
    bit16 twoTotheSeven = construct(0b0, 0b111 + bias, 0b0000000);
    bit16 twoTotheMinusSeven = construct(0b0, -0b111 + bias, 0b0000000);
    
    
    bit16 minusOnePointTwoFive = construct(0b0, 0b01111111, 0b0100000);
    bit16 minusThreePointSevenFive = construct(0b0, 0b10000000, 0b1110000);
    
    
    print16(SM_ADD(minusOnePointTwoFive, minusThreePointSevenFive));
    
    printf("1.25 + 3.75 = \n");
    print16(fp_add(onePointTwoFive, threePointSevenFive));
    printf("0 + 3.75 = \n");
    print16(fp_add(zero, threePointSevenFive));
    printf("0 + 0 = \n");
    print16(fp_add(zero, zero));
    
    
    printf("2^7 + 1 = \n");
    print16(fp_add(twoTotheSeven, 1));
    printf("2^7 + 2^-7 = \n");
    print16(fp_add(twoTotheSeven, twoTotheMinusSeven));
    
    return;
}
Exemplo n.º 6
0
int fp2_srt(fp2_t c, fp2_t a) {
	int r = 0;
	fp_t t1;
	fp_t t2;
	fp_t t3;

	fp_null(t1);
	fp_null(t2);
	fp_null(t3);

	TRY {
		fp_new(t1);
		fp_new(t2);
		fp_new(t3);

		/* t1 = a[0]^2 - u^2 * a[1]^2 */
		fp_sqr(t1, a[0]);
		fp_sqr(t2, a[1]);
		for (int i = -1; i > fp_prime_get_qnr(); i--) {
			fp_add(t1, t1, t2);
		}
		for (int i = 0; i <= fp_prime_get_qnr(); i++) {
			fp_sub(t1, t1, t2);
		}		
		fp_add(t1, t1, t2);

		if (fp_srt(t2, t1)) {
			/* t1 = (a_0 + sqrt(t1)) / 2 */
			fp_add(t1, a[0], t2);
			fp_set_dig(t3, 2);
			fp_inv(t3, t3);
			fp_mul(t1, t1, t3);

			if (!fp_srt(t3, t1)) {
				/* t1 = (a_0 - sqrt(t1)) / 2 */
				fp_sub(t1, a[0], t2);
				fp_set_dig(t3, 2);
				fp_inv(t3, t3);
				fp_mul(t1, t1, t3);
				fp_srt(t3, t1);
			}
			/* c_0 = sqrt(t1) */
			fp_copy(c[0], t3);
			/* c_1 = a_1 / (2 * sqrt(t1)) */
			fp_dbl(t3, t3);
			fp_inv(t3, t3);
			fp_mul(c[1], a[1], t3);
			r = 1;
		}
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		fp_free(t1);
		fp_free(t2);
		fp_free(t3);
	}
	return r;
}
Exemplo n.º 7
0
void fp2_norm_low(fp2_t c, fp2_t a) {
	fp2_t t;
	bn_t b;

	fp2_null(t);
	bn_null(b);

	TRY {
		fp2_new(t);
		bn_new(b);

#if FP_PRIME == 158
		fp_dbl(t[0], a[0]);
		fp_dbl(t[0], t[0]);
		fp_sub(t[0], t[0], a[1]);
		fp_dbl(t[1], a[1]);
		fp_dbl(t[1], t[1]);
		fp_add(c[1], a[0], t[1]);
		fp_copy(c[0], t[0]);
#elif defined(FP_QNRES)
		/* If p = 3 mod 8, (1 + i) is a QNR/CNR. */
		fp_neg(t[0], a[1]);
		fp_add(c[1], a[0], a[1]);
		fp_add(c[0], t[0], a[0]);
#else
		switch (fp_prime_get_mod8()) {
			case 3:
				/* If p = 3 mod 8, (1 + u) is a QNR/CNR. */
				fp_neg(t[0], a[1]);
				fp_add(c[1], a[0], a[1]);
				fp_add(c[0], t[0], a[0]);
				break;
			case 5:
				/* If p = 5 mod 8, (u) is a QNR/CNR. */
				fp2_mul_art(c, a);
				break;
			case 7:
				/* If p = 7 mod 8, we choose (2^(lg_4(b-1)) + u) as QNR/CNR. */
				fp2_mul_art(t, a);
				fp2_dbl(c, a);
				fp_prime_back(b, ep_curve_get_b());
				for (int i = 1; i < bn_bits(b) / 2; i++) {
					fp2_dbl(c, c);
				}
				fp2_add(c, c, t);
				break;
			default:
				THROW(ERR_NO_VALID);
				break;
		}
#endif
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		fp2_free(t);
		bn_free(b);
	}
}
Exemplo n.º 8
0
static int
tfm_rsa_private_calculate(fp_int * in, fp_int * p,  fp_int * q,
			  fp_int * dmp1, fp_int * dmq1, fp_int * iqmp,
			  fp_int * out)
{
    fp_int vp, vq, u;

    fp_init_multi(&vp, &vq, &u, NULL);

    /* vq = c ^ (d mod (q - 1)) mod q */
    /* vp = c ^ (d mod (p - 1)) mod p */
    fp_mod(in, p, &u);
    fp_exptmod(&u, dmp1, p, &vp);
    fp_mod(in, q, &u);
    fp_exptmod(&u, dmq1, q, &vq);

    /* C2 = 1/q mod p  (iqmp) */
    /* u = (vp - vq)C2 mod p. */
    fp_sub(&vp, &vq, &u);
    if (fp_isneg(&u))
	fp_add(&u, p, &u);
    fp_mul(&u, iqmp, &u);
    fp_mod(&u, p, &u);

    /* c ^ d mod n = vq + u q */
    fp_mul(&u, q, &u);
    fp_add(&u, &vq, out);

    fp_zero_multi(&vp, &vq, &u, NULL);

    return 0;
}
Exemplo n.º 9
0
void fp2_mul_basic(fp2_t c, fp2_t a, fp2_t b) {
	dv_t t0, t1, t2, t3, t4;

	dv_null(t0);
	dv_null(t1);
	dv_null(t2);
	dv_null(t3);
	dv_null(t4);

	TRY {
		dv_new(t0);
		dv_new(t1);
		dv_new(t2);
		dv_new(t3);
		dv_new(t4);

		/* Karatsuba algorithm. */

		/* t2 = a_0 + a_1, t1 = b0 + b1. */
		fp_add(t2, a[0], a[1]);
		fp_add(t1, b[0], b[1]);

		/* t3 = (a_0 + a_1) * (b0 + b1). */
		fp_muln_low(t3, t2, t1);

		/* t0 = a_0 * b0, t4 = a_1 * b1. */
		fp_muln_low(t0, a[0], b[0]);
		fp_muln_low(t4, a[1], b[1]);

		/* t2 = (a_0 * b0) + (a_1 * b1). */
		fp_addc_low(t2, t0, t4);

		/* t1 = (a_0 * b0) + u^2 * (a_1 * b1). */
		fp_subc_low(t1, t0, t4);

		/* t1 = u^2 * (a_1 * b1). */
		for (int i = -1; i > fp_prime_get_qnr(); i--) {
			fp_subc_low(t1, t1, t4);
		}
		/* c_0 = t1 mod p. */
		fp_rdc(c[0], t1);

		/* t4 = t3 - t2. */
		fp_subc_low(t4, t3, t2);

		/* c_1 = t4 mod p. */
		fp_rdc(c[1], t4);
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		dv_free(t0);
		dv_free(t1);
		dv_free(t2);
		dv_free(t3);
		dv_free(t4);
	}
}
Exemplo n.º 10
0
void fp2_sqr_basic(fp2_t c, fp2_t a) {
	fp_t t0, t1, t2;

	fp_null(t0);
	fp_null(t1);
	fp_null(t2);

	TRY {
		fp_new(t0);
		fp_new(t1);
		fp_new(t2);

		/* t0 = (a_0 + a_1). */
		fp_add(t0, a[0], a[1]);

		/* t1 = (a_0 - a_1). */
		fp_sub(t1, a[0], a[1]);

		/* t1 = a_0 + u^2 * a_1. */
		for (int i = -1; i > fp_prime_get_qnr(); i--) {
			fp_sub(t1, t1, a[1]);
		}
		for (int i = 0; i <= fp_prime_get_qnr(); i++) {
			fp_add(t1, t1, a[1]);
		}

		if (fp_prime_get_qnr() == -1) {
			/* t2 = 2 * a_0. */
			fp_dbl(t2, a[0]);
			/* c_1 = 2 * a_0 * a_1. */
			fp_mul(c[1], t2, a[1]);
			/* c_0 = a_0^2 + a_1^2 * u^2. */
			fp_mul(c[0], t0, t1);
		} else {
			/* c_1 = a_0 * a_1. */
			fp_mul(c[1], a[0], a[1]);
			/* c_0 = a_0^2 + a_1^2 * u^2. */
			fp_mul(c[0], t0, t1);
			for (int i = -1; i > fp_prime_get_qnr(); i--) {
				fp_add(c[0], c[0], c[1]);
			}
			for (int i = 0; i <= fp_prime_get_qnr(); i++) {
				fp_sub(c[0], c[0], c[1]);
			}			
			/* c_1 = 2 * a_0 * a_1. */
			fp_dbl(c[1], c[1]);
		}
		/* c = c_0 + c_1 * u. */
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		fp_free(t0);
		fp_free(t1);
		fp_free(t2);
	}
}
Exemplo n.º 11
0
void fp2_inv(fp2_t c, fp2_t a) {
	fp_t t0, t1;

	fp_null(t0);
	fp_null(t1);

	TRY {
		fp_new(t0);
		fp_new(t1);

		/* t0 = a_0^2, t1 = a_1^2. */
		fp_sqr(t0, a[0]);
		fp_sqr(t1, a[1]);

		/* t1 = 1/(a_0^2 + a_1^2). */
#ifndef FP_QNRES
		if (fp_prime_get_qnr() != -1) {
			if (fp_prime_get_qnr() == -2) {
				fp_dbl(t1, t1);
				fp_add(t0, t0, t1);
			} else {
				if (fp_prime_get_qnr() < 0) {
					fp_mul_dig(t1, t1, -fp_prime_get_qnr());
					fp_add(t0, t0, t1);
				} else {
					fp_mul_dig(t1, t1, fp_prime_get_qnr());
					fp_sub(t0, t0, t1);
				}
			}
		} else {
			fp_add(t0, t0, t1);
		}
#else
		fp_add(t0, t0, t1);
#endif

		fp_inv(t1, t0);

		/* c_0 = a_0/(a_0^2 + a_1^2). */
		fp_mul(c[0], a[0], t1);
		/* c_1 = - a_1/(a_0^2 + a_1^2). */
		fp_mul(c[1], a[1], t1);
		fp_neg(c[1], c[1]);
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		fp_free(t0);
		fp_free(t1);
	}
}
Exemplo n.º 12
0
void fp2_mul_nor_basic(fp2_t c, fp2_t a) {
	fp2_t t;
	bn_t b;

	fp2_null(t);
	bn_null(b);

	TRY {
		fp2_new(t);
		bn_new(b);

#ifdef FP_QNRES
		/* If p = 3 mod 8, (1 + i) is a QNR/CNR. */
		fp_neg(t[0], a[1]);
		fp_add(c[1], a[0], a[1]);
		fp_add(c[0], t[0], a[0]);
#else
		switch (fp_prime_get_mod8()) {
			case 3:
				/* If p = 3 mod 8, (1 + u) is a QNR/CNR. */
				fp_neg(t[0], a[1]);
				fp_add(c[1], a[0], a[1]);
				fp_add(c[0], t[0], a[0]);
				break;
			case 1:
			case 5:
				/* If p = 5 mod 8, (u) is a QNR/CNR. */
				fp2_mul_art(c, a);
				break;
			case 7:
				/* If p = 7 mod 8, we choose (4 + u) is a QNR/CNR. */
				fp2_mul_art(t, a);
				fp2_dbl(c, a);
				fp2_dbl(c, c);
				fp2_add(c, c, t);
				break;
			default:
				THROW(ERR_NO_VALID);
		}
#endif
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		fp2_free(t);
		bn_free(b);
	}
}
Exemplo n.º 13
0
void fp2_mul_art(fp2_t c, fp2_t a) {
	fp_t t;

	fp_null(t);

	TRY {
		fp_new(t);

#ifdef FP_QNRES
		/* (a_0 + a_1 * i) * i = -a_1 + a_0 * i. */
		fp_copy(t, a[0]);
		fp_neg(c[0], a[1]);
		fp_copy(c[1], t);
#else
		/* (a_0 + a_1 * u) * u = (a_1 * u^2) + a_0 * u. */
		fp_copy(t, a[0]);
		fp_neg(c[0], a[1]);
		for (int i = -1; i > fp_prime_get_qnr(); i--) {
			fp_sub(c[0], c[0], a[1]);
		}
		for (int i = 1; i <= fp_prime_get_qnr() + 1; i++) {
			fp_add(c[0], c[0], a[1]);
		}		
		fp_copy(c[1], t);
#endif
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		fp_free(t);
	}
}
Exemplo n.º 14
0
//Called at the end of the gesture,
//Returns the id of the recognized gesture or -1 if none.
char input_end(){
  fp_t prob;
  char recognized = -1; // which gesture has been recognized
  fp_t recogprob = -1; // probability of this gesture
  fp_t tmpgesture;
  char i, j;

  started = false;
  for (i = 0; i < 2; i++){
    prob = 0;
    // add probabilities
    for (j = 0; j < 8; j++){
      prob = fp_add(prob, s[j]);
    }
    if (fp_cmp(prob, recogprob)==1) {
      recogprob = prob;
      recognized = i;
    }
  }
  
  //printf("m->prob = %.30f\n", fp2d(recogprob));
  
  UARTSendArray("p=", 2);
  UARTSendInt(recogprob);
  //dir_filter_ref[0] = 0; //reset for next time
  return recognized;
}
Exemplo n.º 15
0
//Performs the next iteration of the HMM forward algorithm
fp_t forward_proc_inc(char o){
  fp_t ord = 0;
  fp_t sum;
  char k,l;

  if (started == false){
    for (l = 0; l < 8; l++){
      f[l] = b[(o<<3) + l]; // pi*b
      //f[l] = b[o][l];
    }
    for (l = 1; l < 8; l++){
      f[l] = 0;
    }
    return 0;
  }else{
    for (k = 0; k < 8; k++){
      sum = 0;
      for (l = 0; l < 8; l++){
        //sum = fp_add(sum, fp_mul(s[l], a[(l<<3) + k]));
        sum = fp_add(sum, fp_mul(s[l], b[(l<<3) +k]));
      }
      f[k] = fp_mul(sum, b[(o<<3)+k]);
      //f[k] = fp_mul(sum, b[o][k]);
      ord |= f[k];
    }
  }
  return ord;
}
Exemplo n.º 16
0
/* d = a + b (mod c) */
int fp_addmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d)
{
  fp_int tmp;
  fp_zero(&tmp);
  fp_add(a, b, &tmp);
  return fp_mod(&tmp, c, d);
}
Exemplo n.º 17
0
fp_t forward_proc_inc2(unsigned char o){
  fp_t ord = 0;
  fp_t sum;
  unsigned char k,l;

  if (started == false){
    for (l = 0; l < 8; l++){
      f2[l] = fp_mul(pi2[l],b2[(o<<3) + l]); // pi*b
      //f[l] = b[o][l];
    }
    return 0;
  }else{
    for (k = 0; k < 8; k++){
      sum = 0;
      for (l = 0; l < 8; l++){
        sum = fp_add(sum, fp_mul(s2[l], a2[(l<<3) + k]));
        //sum = fp_add(sum, fp_mul(s2[l], b2[(l<<3) +k]));
      }
      f2[k] = fp_mul(sum, b2[(o<<3)+k]);
      //f[k] = fp_mul(sum, b2[o][k]);
      ord |= f2[k];
    }
  }
  return ord;
}
Exemplo n.º 18
0
/* add */
static int add(void *a, void *b, void *c) {
    LTC_ARGCHK(a != NULL);
    LTC_ARGCHK(b != NULL);
    LTC_ARGCHK(c != NULL);
    fp_add(a, b, c);
    return CRYPT_OK;
}
Exemplo n.º 19
0
fixed SynthController::ProcessSample()
{
  const fixed noise = noiseGenerator_.ProcessSample();
  const fixed osc = oscillator_.ProcessSample();
  const fixed value=fp_add(noise,osc);
  const fixed envValue =envelope_.GetScaledValue();
  return fp_mul(value, envValue);
}
Exemplo n.º 20
0
/* Evaluates the following polynomial at x = a:
 * 
 * coefs[0] x^(n-1) + coefs[1] x^(n-1) + ... + coefs[n-1]
 * 
 * using the Horner scheme. This changes the order of evaluation to:
 * 
 * coefs[n-1] + (coefs[n-2] + (... + coefs[n-1] * x) * x) * x
 * 
 * which greatly reduces the number of required multiplies. */
fp_t fp_poly(fp_t coefs[], size_t n, fp_t a) {
  size_t i;
  fp_t out;
  
  out = coefs[0];
  for(i = 1; i < n; ++i) {
    out = fp_mul(a, out);
    out = fp_add(coefs[i], out);
  }
  
  return out;
}
Exemplo n.º 21
0
unsigned char derive_group2(){
  fp_t a, b, c, d;
  fp_t minDist = 0x7fff; //0x7fff;
  char minGroup=0;
  fp_t *ref;
  char i;

  for (i = 0; i < 14; i++){
    ref = quantizerMap2[i];
    a = fp_sub(ref[0], acc[0]);
    b = fp_sub(ref[1], acc[1]);
    c = fp_sub(ref[2], acc[2]);
    d = fp_add(fp_add(fp_mul(a,a), fp_mul(b,b)), fp_mul(c,c));
    if (fp_cmp(d, minDist) == 2){
      minDist = d;
      minGroup = i;
    }
  }
  /* UARTSendArray("group=", 6); */
  /* UARTSendInt(minGroup); */
  return minGroup;
}
Exemplo n.º 22
0
//Called at the end of the gesture,
//Returns the id of the recognized gesture or -1 if none.
char input_end(){
  fp_t prob, prob2;
  char recognized; // which gesture has been recognized
  char j;

  started = false;
    prob = 0;
    prob2 = 0;
    // add probabilities
    for (j = 0; j < 8; j++){
      prob = fp_add(prob, s[j]);
      prob2 = fp_add(prob2, s2[j]);
    }
    if (fp_cmp(prob, prob2)==1) {
      recognized = 0;
    } else {
    	recognized = 1;
    }
  //printf("\np = %d %d\n", prob, prob2);

  return recognized;
}
Exemplo n.º 23
0
int main() {
  int count = 0;
  int errors = 0;
  for (int i = 0; i < numbers_cnt; i++) {
    uint32_t v = v_add(numbers[i], 0x4B00U<<16);
    uint32_t fp = fp_add(numbers[i], 0x4B00U<<16, 0, 1);
    if (v != fp && errors < 10) {
      printf("flr: %08x => v %08x | fp %08x\n", numbers[i], v, fp);
    }
    errors += fp != v;
    count += 1;
  }
  printf("flr: errors: %d tests: %d\n", errors, count);
  return errors != 0;
}
Exemplo n.º 24
0
/* c = a mod b, 0 <= c < b  */
int fp_mod(fp_int *a, fp_int *b, fp_int *c)
{
   fp_int t;
   int    err;

   fp_zero(&t);
   if ((err = fp_div(a, b, NULL, &t)) != FP_OKAY) {
      return err;
   }
   if (t.sign != b->sign) {
      fp_add(&t, b, c);
   } else {
      fp_copy(&t, c);
  }
  return FP_OKAY;
}
Exemplo n.º 25
0
void set_filter(int channel, filterType_t type, fixed param1,fixed param2,int mix,bool bassyMapping)
{
    filter_t* flt=&filter[channel];

    if(flt->type!=type)		//reset only on filter type change   (maybe no reset would make interresting bugs)
    {
        flt->height[0]=0;		//idle state
        flt->height[1]=0;		//idle state
        flt->speed[0]=0;		//paused	(to avoid having it goind crazy)
        flt->speed[1]=0;		//paused	(to avoid having it goind crazy)
        flt->hipdelay[0]=0 ;
        flt->hipdelay[1]=0 ;
        flt->type=type;
    }


    flt->dirt=fp_mul(i2fp(100),i2fp(1)-param1)+fp_mul(i2fp(5000),param1) ;
    flt->mix =fp_mul(i2fp(mix),fp_inv_255) ;

    if (param1 != flt->parm1)
    {
        flt->parm1=param1;
        //adjust parm to get the most of the parameters, as the fx are more useful with near-limit parameters.
        if (bassyMapping)
        {
            static const fixed fpFreqDivider = fl2fp(1/22050.0f);
            static const fixed fpZeroSix = fl2fp(0.6f);
            static const fixed fpThreeOne = fl2fp(3.1f);

            fixed power = fp_add(fpZeroSix,fp_mul(param1,fpThreeOne));
            fixed frequency = fl2fp(pow(10.0f,fp2fl(power))) ;
            frequency=fp_mul(frequency,fpFreqDivider);
            flt->freq=frequency;
        }
        else
        {
            flt->freq=fp_mul(param1,param1);				//0 - .5 - 1   =>   0 - .25 - 1
        }
    }

    if (param2 != flt->parm2)
    {
        flt->parm2=param2;
        flt->reso=i2fp(1)-param2 ;
        flt->reso=fp_sub(fl2fp(1.f),fp_mul(flt->reso,fp_mul(flt->reso,flt->reso)));	//0 - .5 - 1   =>   0 - .93 - 1
    }
}
Exemplo n.º 26
0
int
main()
{
  if (fp_add (1, 1) != 2) fail ("fp_add 1+1");
  if (fp_sub (3, 2) != 1) fail ("fp_sub 3-2");
  if (fp_mul (2, 3) != 6) fail ("fp_mul 2*3");
  if (fp_div (3, 2) != 1.5) fail ("fp_div 3/2");
  if (fp_neg (1) != -1) fail ("fp_neg 1");

  if (dp_add (1, 1) != 2) fail ("dp_add 1+1");
  if (dp_sub (3, 2) != 1) fail ("dp_sub 3-2");
  if (dp_mul (2, 3) != 6) fail ("dp_mul 2*3");
  if (dp_div (3, 2) != 1.5) fail ("dp_div 3/2");
  if (dp_neg (1) != -1) fail ("dp_neg 1");

  if (fp_to_dp (1.5) != 1.5) fail ("fp_to_dp 1.5");
  if (dp_to_fp (1.5) != 1.5) fail ("dp_to_fp 1.5");

  if (floatsisf (1) != 1) fail ("floatsisf 1");
  if (floatsidf (1) != 1) fail ("floatsidf 1");
  if (fixsfsi (1.42) != 1) fail ("fixsfsi 1.42");
  if (fixunssfsi (1.42) != 1) fail ("fixunssfsi 1.42");
  if (fixdfsi (1.42) != 1) fail ("fixdfsi 1.42");
  if (fixunsdfsi (1.42) != 1) fail ("fixunsdfsi 1.42");

  if (eqsf2 (1, 1) == 0) fail ("eqsf2 1==1");
  if (eqsf2 (1, 2) != 0) fail ("eqsf2 1==2");
  if (nesf2 (1, 2) == 0) fail ("nesf2 1!=1");
  if (nesf2 (1, 1) != 0) fail ("nesf2 1!=1");
  if (gtsf2 (2, 1) == 0) fail ("gtsf2 2>1");
  if (gtsf2 (1, 1) != 0) fail ("gtsf2 1>1");
  if (gtsf2 (0, 1) != 0) fail ("gtsf2 0>1");
  if (gesf2 (2, 1) == 0) fail ("gesf2 2>=1");
  if (gesf2 (1, 1) == 0) fail ("gesf2 1>=1");
  if (gesf2 (0, 1) != 0) fail ("gesf2 0>=1");
  if (ltsf2 (1, 2) == 0) fail ("ltsf2 1<2");
  if (ltsf2 (1, 1) != 0) fail ("ltsf2 1<1");
  if (ltsf2 (1, 0) != 0) fail ("ltsf2 1<0");
  if (lesf2 (1, 2) == 0) fail ("lesf2 1<=2");
  if (lesf2 (1, 1) == 0) fail ("lesf2 1<=1");
  if (lesf2 (1, 0) != 0) fail ("lesf2 1<=0");

  if (fail_count != 0)
    abort ();
  exit (0);
}
Exemplo n.º 27
0
/* Computes log(x) and stores the result in out.
 * The current method of computation is as follows:
 * 1) Separate x into the form y * 10^n, where 1 <= y < 10.
 * 2) Approximate log(y) using a 5/5 Pade approximant.
 * 3) Compute log(10^n) via n * log(10).
 * 4) Since log(ab) = log(a) + log(b), compute final result by:
 *    log(y) + n log(10).
 */
fp_t fp_log(fp_t x) {
  fp_t num, div, tmp, p10;
  
  /* Extract the fractional part of x. */
  tmp      = x;
  tmp.expt = 0x7f;
  
  /* Compute the quotient approximation of frac(x) */
  num = fp_poly(QUOT_NUMERATOR, sizeof QUOT_NUMERATOR / sizeof QUOT_NUMERATOR[0], tmp);
  div = fp_poly(QUOT_DENOMINATOR, sizeof QUOT_DENOMINATOR / sizeof QUOT_DENOMINATOR[0], tmp);
  
  /* Compute the quotient. */
  tmp = fp_div(num, div);
  
  p10 = fp_fromint(x.expt - 0x7f);
  p10 = fp_mul(p10, FP_LOG10);
  
  tmp = fp_add(tmp, p10);
  return tmp;
}
Exemplo n.º 28
0
int ed_affine_is_valid(const fp_t x, const fp_t y) {
	fp_t tmpFP0;
	fp_t tmpFP1;
	fp_t tmpFP2;

	fp_null(tmpFP0);
	fp_null(tmpFP1);
	fp_null(tmpFP2);

	int r = 0;

	TRY {
		fp_new(tmpFP0);
		fp_new(tmpFP1);
		fp_new(tmpFP2);

		// a * X^2 + Y^2 - 1 - d * X^2 * Y^2 =?= 0
		fp_sqr(tmpFP0, x);
		fp_mul(tmpFP0, core_get()->ed_a, tmpFP0);
		fp_sqr(tmpFP1, y);
		fp_add(tmpFP1, tmpFP0, tmpFP1);
		fp_sub_dig(tmpFP1, tmpFP1, 1);
		fp_sqr(tmpFP0, x);
		fp_mul(tmpFP0, core_get()->ed_d, tmpFP0);
		fp_sqr(tmpFP2, y);
		fp_mul(tmpFP2, tmpFP0, tmpFP2);
		fp_sub(tmpFP0, tmpFP1, tmpFP2);

		r = fp_is_zero(tmpFP0);
	} CATCH_ANY {
		THROW(ERR_CAUGHT);
	} FINALLY {
		fp_free(tmpFP0);
		fp_free(tmpFP1);
		fp_free(tmpFP2);
	}
	return r;
}
Exemplo n.º 29
0
void pp_dbl_k2_basic(fp2_t l, ep_t r, ep_t p, ep_t q) {
	fp_t s;
	ep_t t;

	fp_null(s);
	ep_null(t);

	TRY {
		fp_new(s);
		ep_new(t);

		ep_copy(t, p);
		ep_dbl_slp_basic(r, s, p);
		fp_add(l[0], t->x, q->x);
		fp_mul(l[0], l[0], s);
		fp_sub(l[0], t->y, l[0]);
		fp_copy(l[1], q->y);
	} CATCH_ANY {
		THROW(ERR_CAUGHT);
	} FINALLY {
		fp_free(s);
		ep_free(t);
	}
}
Exemplo n.º 30
0
void fp_rdcs_low(dig_t *c, dig_t *a, dig_t *m) {
	align dig_t q[2 * FP_DIGS], _q[2 * FP_DIGS];
	align dig_t _r[2 * FP_DIGS], r[2 * FP_DIGS], t[2 * FP_DIGS];
	int *sform, len;
	int first, i, j, b0, d0, b1, d1;
	dig_t carry;

	sform = fp_prime_get_sps(&len);

	SPLIT(b0, d0, FP_BITS, FP_DIG_LOG);
	first = (d0) + (b0 == 0 ? 0 : 1);

	/* q = floor(a/b^k) */
	dv_zero(q, 2 * FP_DIGS);
	bn_rshd_low(q, a, 2 * FP_DIGS, d0);
	if (b0 > 0) {
		bn_rshb_low(q, q, 2 * FP_DIGS, b0);
	}

	/* r = a - qb^k. */
	dv_copy(r, a, first);
	if (b0 > 0) {
		r[first - 1] &= MASK(b0);
	}

	carry = 0;
	while (!fp_is_zero(q)) {
		dv_zero(_q, 2 * FP_DIGS);
		for (i = len - 1; i > 0; i--) {
			j = (sform[i] < 0 ? -sform[i] : sform[i]);
			SPLIT(b1, d1, j, FP_DIG_LOG);
			dv_zero(t, 2 * FP_DIGS);
			bn_lshd_low(t, q, FP_DIGS, d1);
			if (b1 > 0) {
				bn_lshb_low(t, t, 2 * FP_DIGS, b1);
			}
			if (sform[i] > 0) {
				bn_subn_low(_q, _q, t, 2 * FP_DIGS);
			} else {
				bn_addn_low(_q, _q, t, 2 * FP_DIGS);
			}
		}
		if (sform[0] > 0) {
			bn_subn_low(_q, _q, q, 2 * FP_DIGS);
		} else {
			bn_addn_low(_q, _q, q, 2 * FP_DIGS);
		}
		bn_rshd_low(q, _q, 2 * FP_DIGS, d0);
		if (b0 > 0) {
			bn_rshb_low(q, q, 2 * FP_DIGS, b0);
		}

		dv_copy(_r, _q, first);
		if (b0 > 0) {
			_r[first - 1] &= MASK(b0);
		}
		fp_add(r, r, _r);
	}
	while (fp_cmpn_low(r, m) != CMP_LT) {
		fp_subn_low(r, r, m);
	}
	fp_copy(c, r);
}