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; }
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; }
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; }
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); } }
void SynthController::updateCutoff() { fixed envValue = envelope_.GetValue(); fixed envContrib = fp_sub(FP_ONE, envValue); envContrib = fp_mul(envContrib, cutoffEnvMod_); envContrib = fp_sub(FP_ONE, envContrib); fixed current = fp_mul(cutoff_, envContrib); hwParameters_.cutoff_ = int(fp2fl(current)*255); }
/* sub */ static int sub(void *a, void *b, void *c) { LTC_ARGCHK(a != NULL); LTC_ARGCHK(b != NULL); LTC_ARGCHK(c != NULL); fp_sub(a, b, c); return CRYPT_OK; }
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]); } fp_copy(c[1], t); #endif } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp_free(t); } }
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); } }
/* d = a - b (mod c) */ int fp_submod(fp_int *a, fp_int *b, fp_int *c, fp_int *d) { fp_int tmp; fp_zero(&tmp); fp_sub(a, b, &tmp); return fp_mod(&tmp, c, d); }
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; }
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); } }
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; }
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 } }
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); }
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; }
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); } }
/* a/b => cb + d == a */ int fp_div(fp_int *a, fp_int *b, fp_int *c, fp_int *d) { fp_int q, x, y, t1, t2; int n, t, i, norm, neg; /* is divisor zero ? */ if (fp_iszero (b) == 1) { return FP_VAL; } /* if a < b then q=0, r = a */ if (fp_cmp_mag (a, b) == FP_LT) { if (d != NULL) { fp_copy (a, d); } if (c != NULL) { fp_zero (c); } return FP_OKAY; } fp_init(&q); q.used = a->used + 2; fp_init(&t1); fp_init(&t2); fp_init_copy(&x, a); fp_init_copy(&y, b); /* fix the sign */ neg = (a->sign == b->sign) ? FP_ZPOS : FP_NEG; x.sign = y.sign = FP_ZPOS; /* normalize both x and y, ensure that y >= b/2, [b == 2**DIGIT_BIT] */ norm = fp_count_bits(&y) % DIGIT_BIT; if (norm < (int)(DIGIT_BIT-1)) { norm = (DIGIT_BIT-1) - norm; fp_mul_2d (&x, norm, &x); fp_mul_2d (&y, norm, &y); } else { norm = 0; } /* note hac does 0 based, so if used==5 then its 0,1,2,3,4, e.g. use 4 */ n = x.used - 1; t = y.used - 1; /* while (x >= y*b**n-t) do { q[n-t] += 1; x -= y*b**{n-t} } */ fp_lshd (&y, n - t); /* y = y*b**{n-t} */ while (fp_cmp (&x, &y) != FP_LT) { ++(q.dp[n - t]); fp_sub (&x, &y, &x); } /* reset y by shifting it back down */ fp_rshd (&y, n - t); /* step 3. for i from n down to (t + 1) */ for (i = n; i >= (t + 1); i--) { if (i > x.used) { continue; } /* step 3.1 if xi == yt then set q{i-t-1} to b-1, * otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */ if (x.dp[i] == y.dp[t]) { q.dp[i - t - 1] = ((((fp_word)1) << DIGIT_BIT) - 1); } else { fp_word tmp; tmp = ((fp_word) x.dp[i]) << ((fp_word) DIGIT_BIT); tmp |= ((fp_word) x.dp[i - 1]); tmp /= ((fp_word) y.dp[t]); q.dp[i - t - 1] = (fp_digit) (tmp); } /* while (q{i-t-1} * (yt * b + y{t-1})) > xi * b**2 + xi-1 * b + xi-2 do q{i-t-1} -= 1; */ q.dp[i - t - 1] = (q.dp[i - t - 1] + 1); do { q.dp[i - t - 1] = (q.dp[i - t - 1] - 1); /* find left hand */ fp_zero (&t1); t1.dp[0] = (t - 1 < 0) ? 0 : y.dp[t - 1]; t1.dp[1] = y.dp[t]; t1.used = 2; fp_mul_d (&t1, q.dp[i - t - 1], &t1); /* find right hand */ t2.dp[0] = (i - 2 < 0) ? 0 : x.dp[i - 2]; t2.dp[1] = (i - 1 < 0) ? 0 : x.dp[i - 1]; t2.dp[2] = x.dp[i]; t2.used = 3; } while (fp_cmp_mag(&t1, &t2) == FP_GT); /* step 3.3 x = x - q{i-t-1} * y * b**{i-t-1} */ fp_mul_d (&y, q.dp[i - t - 1], &t1); fp_lshd (&t1, i - t - 1); fp_sub (&x, &t1, &x); /* if x < 0 then { x = x + y*b**{i-t-1}; q{i-t-1} -= 1; } */ if (x.sign == FP_NEG) { fp_copy (&y, &t1); fp_lshd (&t1, i - t - 1); fp_add (&x, &t1, &x); q.dp[i - t - 1] = q.dp[i - t - 1] - 1; } } /* now q is the quotient and x is the remainder * [which we have to normalize] */ /* get sign before writing to c */ x.sign = x.used == 0 ? FP_ZPOS : a->sign; if (c != NULL) { fp_clamp (&q); fp_copy (&q, c); c->sign = neg; } if (d != NULL) { fp_div_2d (&x, norm, &x, NULL); /* the following is a kludge, essentially we were seeing the right remainder but with excess digits that should have been zero */ for (i = b->used; i < x.used; i++) { x.dp[i] = 0; } fp_clamp(&x); fp_copy (&x, d); } return FP_OKAY; }
void SynthController::SetParameterValue(const SynthController::Parameter& parameter, const fixed& value) { bool envSetup = false; switch(parameter) { case WAVE_SELECT: oscillator_.SetShape(value); break; case WAVE_LOOP_START: oscillator_.SetLoopStart(value); break; case WAVE_LOOP_END: oscillator_.SetLoopWidth(value); break; case WAVE_OCTAVE: { int32_t octave = 0; if (value < fl2fp(0.33f)) { octave = -1; } if (value > fl2fp(0.66f)) { octave = 1; } oscillator_.SetOctave(octave); break; } case ANALOG_OSC_ON: enableAnalog_ = value > fl2fp(0.5f); break; case ANALOG_PITCH_FINE_TUNE: analogPitchFineTune_ = fp2fl(value)*256 - 128 ; break; case PITCH_GLIDE: { const fixed GLIDE_MULTIPLIER = fl2fp(0.4f); fixed glideFactor = (value != 0 ) ? fp_sub(GLIDE_MULTIPLIER, fp_mul(value, GLIDE_MULTIPLIER)) : SET_GLIDE_OFF; anaPitchControl_.SetGlide(glideFactor); digiPitchControl_.SetGlide(glideFactor); break; } case NOISE_MIX: noiseMix_ = value; updateLevels(); break; case KEYBOARD_MODE: { NoteControl::Mode mode = (NoteControl::Mode)int(fp2fl(value)*NoteControl::LAST*0.999f); noteControl_.SetMode(mode); break; } case ENV_ATTACK: attack_ = MAX_ENVELOPE_TICK*fp2fl(value); envSetup = true; break; case ENV_DECAY: { fixed scaledDecay = fp_add(value, ENVELOPE_SCALE); decay_ = MAX_ENVELOPE_TICK*fp2fl(scaledDecay); envSetup = true; } break; case ENV_SUSTAIN: { fixed scaledSustain = fp_add(value, ENVELOPE_SCALE); if (scaledSustain > FP_ONE) { scaledSustain = FP_ONE; } sustain_ = scaledSustain; envSetup = true; } break; case ENV_RELEASE: release_ = MAX_ENVELOPE_TICK*fp2fl(value); envSetup = true; break; case VCA_ENV_GATER: envelope_.SetScaler(value); break; case VCF_ENV_MOD: cutoffEnvMod_ = value; break; case AMP_GAIN: gain_ = value; updateLevels(); break; } if (envSetup) { envelope_.Setup(attack_, decay_, sustain_, release_); } }
void SynthController::updateLevels() { noiseGenerator_.SetLevel(fp_mul(gain_,noiseMix_)); oscillator_.SetGain(fp_mul(gain_,fp_sub(FP_ONE,noiseMix_))); }
void pp_dbl_k12_projc_lazyr(fp12_t l, ep2_t r, ep2_t q, ep_t p) { fp2_t t0, t1, t2, t3, t4, t5, t6; dv2_t u0, u1; int one = 1, zero = 0; fp2_null(t0); fp2_null(t1); fp2_null(t2); fp2_null(t3); fp2_null(t4); fp2_null(t5); fp2_null(t6); dv2_null(u0); dv2_null(u1); TRY { fp2_new(t0); fp2_new(t1); fp2_new(t2); fp2_new(t3); fp2_new(t4); fp2_new(t5); fp2_new(t6); dv2_new(u0); dv2_new(u1); if (ep2_curve_is_twist() == EP_MTYPE) { one ^= 1; zero ^= 1; } if (ep_curve_opt_b() == RLC_TWO) { /* C = z1^2. */ fp2_sqr(t0, q->z); /* B = y1^2. */ fp2_sqr(t1, q->y); /* t5 = B + C. */ fp2_add(t5, t0, t1); /* t3 = E = 3b'C = 3C * (1 - i). */ fp2_dbl(t3, t0); fp2_add(t0, t0, t3); fp_add(t2[0], t0[0], t0[1]); fp_sub(t2[1], t0[1], t0[0]); /* t0 = x1^2. */ fp2_sqr(t0, q->x); /* t4 = A = (x1 * y1)/2. */ fp2_mul(t4, q->x, q->y); fp_hlv(t4[0], t4[0]); fp_hlv(t4[1], t4[1]); /* t3 = F = 3E. */ fp2_dbl(t3, t2); fp2_add(t3, t3, t2); /* x3 = A * (B - F). */ fp2_sub(r->x, t1, t3); fp2_mul(r->x, r->x, t4); /* G = (B + F)/2. */ fp2_add(t3, t1, t3); fp_hlv(t3[0], t3[0]); fp_hlv(t3[1], t3[1]); /* y3 = G^2 - 3E^2. */ fp2_sqrn_low(u0, t2); fp2_addd_low(u1, u0, u0); fp2_addd_low(u1, u1, u0); fp2_sqrn_low(u0, t3); fp2_subc_low(u0, u0, u1); /* H = (Y + Z)^2 - B - C. */ fp2_add(t3, q->y, q->z); fp2_sqr(t3, t3); fp2_sub(t3, t3, t5); fp2_rdcn_low(r->y, u0); /* z3 = B * H. */ fp2_mul(r->z, t1, t3); /* l11 = E - B. */ fp2_sub(l[1][1], t2, t1); /* l10 = (3 * xp) * t0. */ fp_mul(l[one][zero][0], p->x, t0[0]); fp_mul(l[one][zero][1], p->x, t0[1]); /* l01 = F * (-yp). */ fp_mul(l[zero][zero][0], t3[0], p->y); fp_mul(l[zero][zero][1], t3[1], p->y); } else { /* A = x1^2. */ fp2_sqr(t0, q->x); /* B = y1^2. */ fp2_sqr(t1, q->y); /* C = z1^2. */ fp2_sqr(t2, q->z); /* D = 3bC, for general b. */ fp2_dbl(t3, t2); fp2_add(t3, t3, t2); ep2_curve_get_b(t4); fp2_mul(t3, t3, t4); /* E = (x1 + y1)^2 - A - B. */ fp2_add(t4, q->x, q->y); fp2_sqr(t4, t4); fp2_sub(t4, t4, t0); fp2_sub(t4, t4, t1); /* F = (y1 + z1)^2 - B - C. */ fp2_add(t5, q->y, q->z); fp2_sqr(t5, t5); fp2_sub(t5, t5, t1); fp2_sub(t5, t5, t2); /* G = 3D. */ fp2_dbl(t6, t3); fp2_add(t6, t6, t3); /* x3 = E * (B - G). */ fp2_sub(r->x, t1, t6); fp2_mul(r->x, r->x, t4); /* y3 = (B + G)^2 -12D^2. */ fp2_add(t6, t6, t1); fp2_sqr(t6, t6); fp2_sqr(t2, t3); fp2_dbl(r->y, t2); fp2_dbl(t2, r->y); fp2_dbl(r->y, t2); fp2_add(r->y, r->y, t2); fp2_sub(r->y, t6, r->y); /* z3 = 4B * F. */ fp2_dbl(r->z, t1); fp2_dbl(r->z, r->z); fp2_mul(r->z, r->z, t5); /* l00 = D - B. */ fp2_sub(l[one][one], t3, t1); /* l10 = (3 * xp) * A. */ fp_mul(l[one][zero][0], p->x, t0[0]); fp_mul(l[one][zero][1], p->x, t0[1]); /* l01 = F * (-yp). */ fp_mul(l[zero][zero][0], t5[0], p->y); fp_mul(l[zero][zero][1], t5[1], p->y); } r->norm = 0; } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp2_free(t0); fp2_free(t1); fp2_free(t2); fp2_free(t3); fp2_free(t4); fp2_free(t5); fp2_free(t6); dv2_free(u0); dv2_free(u1); } }
void fp2_subm_low(fp2_t c, fp2_t a, fp2_t b) { fp_sub(c[0], a[0], b[0]); fp_sub(c[1], a[1], b[1]); }
void fp3_sqr_basic(fp3_t c, fp3_t a) { dv_t t0, t1, t2, t3, t4, t5; dv_null(t0); dv_null(t1); dv_null(t2); dv_null(t3); dv_null(t4); dv_null(t5); TRY { dv_new(t0); dv_new(t1); dv_new(t2); dv_new(t3); dv_new(t4); dv_new(t5); /* t0 = a_0^2. */ fp_sqrn_low(t0, a[0]); /* t1 = 2 * a_1 * a_2. */ fp_dbl(t2, a[1]); fp_muln_low(t1, t2, a[2]); /* t2 = a_2^2. */ fp_sqrn_low(t2, a[2]); /* t3 = (a_0 + a_2 + a_1)^2, t4 = (a_0 + a_2 - a_1)^2. */ fp_add(t3, a[0], a[2]); fp_add(t4, t3, a[1]); fp_sub(t5, t3, a[1]); fp_sqrn_low(t3, t4); fp_sqrn_low(t4, t5); /* t4 = (t4 + t3)/2. */ fp_addd_low(t4, t4, t3); fp_hlvd_low(t4, t4); /* t3 = t3 - t4 - t1. */ fp_addc_low(t5, t1, t4); fp_subc_low(t3, t3, t5); /* c_2 = t4 - t0 - t2. */ fp_addc_low(t5, t0, t2); fp_subc_low(t4, t4, t5); fp_rdc(c[2], t4); /* c_0 = t0 + t1 * B. */ fp_subc_low(t0, t0, t1); for (int i = -1; i > fp_prime_get_cnr(); i--) { fp_subc_low(t0, t0, t1); } fp_rdc(c[0], t0); /* c_1 = t3 + t2 * B. */ fp_subc_low(t3, t3, t2); for (int i = -1; i > fp_prime_get_cnr(); i--) { fp_subc_low(t3, t3, t2); } fp_rdc(c[1], t3); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { dv_free(t0); dv_free(t1); dv_free(t2); dv_free(t3); dv_free(t4); dv_free(t5); } }
void pp_dbl_k2_projc_lazyr(fp2_t l, ep_t r, ep_t p, ep_t q) { fp_t t0, t1, t2, t3, t4, t5; dv_t u0, u1; fp_null(t0); fp_null(t1); fp_null(t2); fp_null(t3); fp_null(t4); fp_null(t5); dv_null(u0); dv_null(u1); TRY { fp_new(t0); fp_new(t1); fp_new(t2); fp_new(t3); fp_new(t4); fp_new(t5); dv_new(u0); dv_new(u1); /* For these curves, we always can choose a = -3. */ /* dbl-2001-b formulas: 3M + 5S + 8add + 1*4 + 2*8 + 1*3 */ /* http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b */ /* t0 = delta = z1^2. */ fp_sqr(t0, p->z); /* t1 = gamma = y1^2. */ fp_sqr(t1, p->y); /* t2 = beta = x1 * y1^2. */ fp_mul(t2, p->x, t1); /* t3 = alpha = 3 * (x1 - z1^2) * (x1 + z1^2). */ fp_sub(t3, p->x, t0); fp_add(t4, p->x, t0); fp_mul(t4, t3, t4); fp_dbl(t3, t4); fp_add(t3, t3, t4); /* t2 = 4 * beta. */ fp_dbl(t2, t2); fp_dbl(t2, t2); /* z3 = (y1 + z1)^2 - gamma - delta. */ fp_add(r->z, p->y, p->z); fp_sqr(r->z, r->z); fp_sub(r->z, r->z, t1); fp_sub(r->z, r->z, t0); /* l0 = 2 * gamma - alpha * (delta * xq + x1). */ fp_dbl(t1, t1); fp_mul(t5, t0, q->x); fp_add(t5, t5, p->x); fp_mul(t5, t5, t3); fp_sub(l[0], t1, t5); /* x3 = alpha^2 - 8 * beta. */ fp_dbl(t5, t2); fp_sqr(r->x, t3); fp_sub(r->x, r->x, t5); /* y3 = alpha * (4 * beta - x3) - 8 * gamma^2. */ fp_sqrn_low(u0, t1); fp_addc_low(u0, u0, u0); fp_subm_low(r->y, t2, r->x); fp_muln_low(u1, r->y, t3); fp_subc_low(u1, u1, u0); fp_rdcn_low(r->y, u1); /* l1 = - z3 * delta * yq. */ fp_mul(l[1], r->z, t0); fp_mul(l[1], l[1], q->y); r->norm = 0; } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp_free(t0); fp_free(t1); fp_free(t2); fp_free(t3); fp_free(t4); fp_free(t5); dv_free(u0); dv_free(u1); } }
static int tfm_ecc_projective_dbl_point(ecc_point *P, ecc_point *R, void *modulus, void *Mp) { fp_int t1, t2; fp_digit mp; LTC_ARGCHK(P != NULL); LTC_ARGCHK(R != NULL); LTC_ARGCHK(modulus != NULL); LTC_ARGCHK(Mp != NULL); mp = *((fp_digit*)Mp); fp_init(&t1); fp_init(&t2); if (P != R) { fp_copy(P->x, R->x); fp_copy(P->y, R->y); fp_copy(P->z, R->z); } /* t1 = Z * Z */ fp_sqr(R->z, &t1); fp_montgomery_reduce(&t1, modulus, mp); /* Z = Y * Z */ fp_mul(R->z, R->y, R->z); fp_montgomery_reduce(R->z, modulus, mp); /* Z = 2Z */ fp_add(R->z, R->z, R->z); if (fp_cmp(R->z, modulus) != FP_LT) { fp_sub(R->z, modulus, R->z); } /* &t2 = X - T1 */ fp_sub(R->x, &t1, &t2); if (fp_cmp_d(&t2, 0) == FP_LT) { fp_add(&t2, modulus, &t2); } /* T1 = X + T1 */ fp_add(&t1, R->x, &t1); if (fp_cmp(&t1, modulus) != FP_LT) { fp_sub(&t1, modulus, &t1); } /* T2 = T1 * T2 */ fp_mul(&t1, &t2, &t2); fp_montgomery_reduce(&t2, modulus, mp); /* T1 = 2T2 */ fp_add(&t2, &t2, &t1); if (fp_cmp(&t1, modulus) != FP_LT) { fp_sub(&t1, modulus, &t1); } /* T1 = T1 + T2 */ fp_add(&t1, &t2, &t1); if (fp_cmp(&t1, modulus) != FP_LT) { fp_sub(&t1, modulus, &t1); } /* Y = 2Y */ fp_add(R->y, R->y, R->y); if (fp_cmp(R->y, modulus) != FP_LT) { fp_sub(R->y, modulus, R->y); } /* Y = Y * Y */ fp_sqr(R->y, R->y); fp_montgomery_reduce(R->y, modulus, mp); /* T2 = Y * Y */ fp_sqr(R->y, &t2); fp_montgomery_reduce(&t2, modulus, mp); /* T2 = T2/2 */ if (fp_isodd(&t2)) { fp_add(&t2, modulus, &t2); } fp_div_2(&t2, &t2); /* Y = Y * X */ fp_mul(R->y, R->x, R->y); fp_montgomery_reduce(R->y, modulus, mp); /* X = T1 * T1 */ fp_sqr(&t1, R->x); fp_montgomery_reduce(R->x, modulus, mp); /* X = X - Y */ fp_sub(R->x, R->y, R->x); if (fp_cmp_d(R->x, 0) == FP_LT) { fp_add(R->x, modulus, R->x); } /* X = X - Y */ fp_sub(R->x, R->y, R->x); if (fp_cmp_d(R->x, 0) == FP_LT) { fp_add(R->x, modulus, R->x); } /* Y = Y - X */ fp_sub(R->y, R->x, R->y); if (fp_cmp_d(R->y, 0) == FP_LT) { fp_add(R->y, modulus, R->y); } /* Y = Y * T1 */ fp_mul(R->y, &t1, R->y); fp_montgomery_reduce(R->y, modulus, mp); /* Y = Y - T2 */ fp_sub(R->y, &t2, R->y); if (fp_cmp_d(R->y, 0) == FP_LT) { fp_add(R->y, modulus, R->y); } return CRYPT_OK; }
/** Add two ECC points @param P The point to add @param Q The point to add @param R [out] The destination of the double @param modulus The modulus of the field the ECC curve is in @param mp The "b" value from montgomery_setup() @return CRYPT_OK on success */ static int tfm_ecc_projective_add_point(ecc_point *P, ecc_point *Q, ecc_point *R, void *modulus, void *Mp) { fp_int t1, t2, x, y, z; fp_digit mp; LTC_ARGCHK(P != NULL); LTC_ARGCHK(Q != NULL); LTC_ARGCHK(R != NULL); LTC_ARGCHK(modulus != NULL); LTC_ARGCHK(Mp != NULL); mp = *((fp_digit*)Mp); fp_init(&t1); fp_init(&t2); fp_init(&x); fp_init(&y); fp_init(&z); /* should we dbl instead? */ fp_sub(modulus, Q->y, &t1); if ( (fp_cmp(P->x, Q->x) == FP_EQ) && (Q->z != NULL && fp_cmp(P->z, Q->z) == FP_EQ) && (fp_cmp(P->y, Q->y) == FP_EQ || fp_cmp(P->y, &t1) == FP_EQ)) { return tfm_ecc_projective_dbl_point(P, R, modulus, Mp); } fp_copy(P->x, &x); fp_copy(P->y, &y); fp_copy(P->z, &z); /* if Z is one then these are no-operations */ if (Q->z != NULL) { /* T1 = Z' * Z' */ fp_sqr(Q->z, &t1); fp_montgomery_reduce(&t1, modulus, mp); /* X = X * T1 */ fp_mul(&t1, &x, &x); fp_montgomery_reduce(&x, modulus, mp); /* T1 = Z' * T1 */ fp_mul(Q->z, &t1, &t1); fp_montgomery_reduce(&t1, modulus, mp); /* Y = Y * T1 */ fp_mul(&t1, &y, &y); fp_montgomery_reduce(&y, modulus, mp); } /* T1 = Z*Z */ fp_sqr(&z, &t1); fp_montgomery_reduce(&t1, modulus, mp); /* T2 = X' * T1 */ fp_mul(Q->x, &t1, &t2); fp_montgomery_reduce(&t2, modulus, mp); /* T1 = Z * T1 */ fp_mul(&z, &t1, &t1); fp_montgomery_reduce(&t1, modulus, mp); /* T1 = Y' * T1 */ fp_mul(Q->y, &t1, &t1); fp_montgomery_reduce(&t1, modulus, mp); /* Y = Y - T1 */ fp_sub(&y, &t1, &y); if (fp_cmp_d(&y, 0) == FP_LT) { fp_add(&y, modulus, &y); } /* T1 = 2T1 */ fp_add(&t1, &t1, &t1); if (fp_cmp(&t1, modulus) != FP_LT) { fp_sub(&t1, modulus, &t1); } /* T1 = Y + T1 */ fp_add(&t1, &y, &t1); if (fp_cmp(&t1, modulus) != FP_LT) { fp_sub(&t1, modulus, &t1); } /* X = X - T2 */ fp_sub(&x, &t2, &x); if (fp_cmp_d(&x, 0) == FP_LT) { fp_add(&x, modulus, &x); } /* T2 = 2T2 */ fp_add(&t2, &t2, &t2); if (fp_cmp(&t2, modulus) != FP_LT) { fp_sub(&t2, modulus, &t2); } /* T2 = X + T2 */ fp_add(&t2, &x, &t2); if (fp_cmp(&t2, modulus) != FP_LT) { fp_sub(&t2, modulus, &t2); } /* if Z' != 1 */ if (Q->z != NULL) { /* Z = Z * Z' */ fp_mul(&z, Q->z, &z); fp_montgomery_reduce(&z, modulus, mp); } /* Z = Z * X */ fp_mul(&z, &x, &z); fp_montgomery_reduce(&z, modulus, mp); /* T1 = T1 * X */ fp_mul(&t1, &x, &t1); fp_montgomery_reduce(&t1, modulus, mp); /* X = X * X */ fp_sqr(&x, &x); fp_montgomery_reduce(&x, modulus, mp); /* T2 = T2 * x */ fp_mul(&t2, &x, &t2); fp_montgomery_reduce(&t2, modulus, mp); /* T1 = T1 * X */ fp_mul(&t1, &x, &t1); fp_montgomery_reduce(&t1, modulus, mp); /* X = Y*Y */ fp_sqr(&y, &x); fp_montgomery_reduce(&x, modulus, mp); /* X = X - T2 */ fp_sub(&x, &t2, &x); if (fp_cmp_d(&x, 0) == FP_LT) { fp_add(&x, modulus, &x); } /* T2 = T2 - X */ fp_sub(&t2, &x, &t2); if (fp_cmp_d(&t2, 0) == FP_LT) { fp_add(&t2, modulus, &t2); } /* T2 = T2 - X */ fp_sub(&t2, &x, &t2); if (fp_cmp_d(&t2, 0) == FP_LT) { fp_add(&t2, modulus, &t2); } /* T2 = T2 * Y */ fp_mul(&t2, &y, &t2); fp_montgomery_reduce(&t2, modulus, mp); /* Y = T2 - T1 */ fp_sub(&t2, &t1, &y); if (fp_cmp_d(&y, 0) == FP_LT) { fp_add(&y, modulus, &y); } /* Y = Y/2 */ if (fp_isodd(&y)) { fp_add(&y, modulus, &y); } fp_div_2(&y, &y); fp_copy(&x, R->x); fp_copy(&y, R->y); fp_copy(&z, R->z); return CRYPT_OK; }
/** * Doubles a point represented in projective coordinates on an ordinary prime * elliptic curve. * * @param r - the result. * @param p - the point to double. */ static void ep_dbl_projc_imp(ep_t r, const ep_t p) { fp_t t0, t1, t2, t3, t4, t5; fp_null(t1); fp_null(t2); fp_null(t3); fp_null(t4); fp_null(t5); TRY { fp_new(t0); fp_new(t1); fp_new(t2); fp_new(t3); fp_new(t4); fp_new(t5); if (!p->norm && ep_curve_opt_a() == OPT_MINUS3) { /* dbl-2001-b formulas: 3M + 5S + 8add + 1*4 + 2*8 + 1*3 */ /* http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b */ /* t0 = delta = z1^2. */ fp_sqr(t0, p->z); /* t1 = gamma = y1^2. */ fp_sqr(t1, p->y); /* t2 = beta = x1 * y1^2. */ fp_mul(t2, p->x, t1); /* t3 = alpha = 3 * (x1 - z1^2) * (x1 + z1^2). */ fp_sub(t3, p->x, t0); fp_add(t4, p->x, t0); fp_mul(t4, t3, t4); fp_dbl(t3, t4); fp_add(t3, t3, t4); /* x3 = alpha^2 - 8 * beta. */ fp_dbl(t2, t2); fp_dbl(t2, t2); fp_dbl(t5, t2); fp_sqr(r->x, t3); fp_sub(r->x, r->x, t5); /* z3 = (y1 + z1)^2 - gamma - delta. */ fp_add(r->z, p->y, p->z); fp_sqr(r->z, r->z); fp_sub(r->z, r->z, t1); fp_sub(r->z, r->z, t0); /* y3 = alpha * (4 * beta - x3) - 8 * gamma^2. */ fp_dbl(t1, t1); fp_sqr(t1, t1); fp_dbl(t1, t1); fp_sub(r->y, t2, r->x); fp_mul(r->y, r->y, t3); fp_sub(r->y, r->y, t1); } else if (ep_curve_opt_a() == OPT_ZERO) { /* dbl-2009-l formulas: 2M + 5S + 6add + 1*8 + 3*2 + 1*3. */ /* http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l */ /* A = X1^2 */ fp_sqr(t0, p->x); /* B = Y1^2 */ fp_sqr(t1, p->y); /* C = B^2 */ fp_sqr(t2, t1); /* D = 2*((X1+B)^2-A-C) */ fp_add(t1, t1, p->x); fp_sqr(t1, t1); fp_sub(t1, t1, t0); fp_sub(t1, t1, t2); fp_dbl(t1, t1); /* E = 3*A */ fp_dbl(t3, t0); fp_add(t0, t3, t0); /* F = E^2 */ fp_sqr(t3, t0); /* Z3 = 2*Y1*Z1 */ fp_mul(r->z, p->y, p->z); fp_dbl(r->z, r->z); /* X3 = F-2*D */ fp_sub(r->x, t3, t1); fp_sub(r->x, r->x, t1); /* Y3 = E*(D-X3)-8*C */ fp_sub(r->y, t1, r->x); fp_mul(r->y, r->y, t0); fp_dbl(t2, t2); fp_dbl(t2, t2); fp_dbl(t2, t2); fp_sub(r->y, r->y, t2); } else { /* dbl-2007-bl formulas: 1M + 8S + 1*a + 10add + 1*8 + 2*2 + 1*3 */ /* http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#doubling-dbl-2007-bl */ /* t0 = x1^2, t1 = y1^2, t2 = y1^4. */ fp_sqr(t0, p->x); fp_sqr(t1, p->y); fp_sqr(t2, t1); if (!p->norm) { /* t3 = z1^2. */ fp_sqr(t3, p->z); if (ep_curve_get_a() == OPT_ZERO) { /* z3 = 2 * y1 * z1. */ fp_mul(r->z, p->y, p->z); fp_dbl(r->z, r->z); } else { /* z3 = (y1 + z1)^2 - y1^2 - z1^2. */ fp_add(r->z, p->y, p->z); fp_sqr(r->z, r->z); fp_sub(r->z, r->z, t1); fp_sub(r->z, r->z, t3); } } else { /* z3 = 2 * y1. */ fp_dbl(r->z, p->y); } /* t4 = S = 2*((x1 + y1^2)^2 - x1^2 - y1^4). */ fp_add(t4, p->x, t1); fp_sqr(t4, t4); fp_sub(t4, t4, t0); fp_sub(t4, t4, t2); fp_dbl(t4, t4); /* t5 = M = 3 * x1^2 + a * z1^4. */ fp_dbl(t5, t0); fp_add(t5, t5, t0); if (!p->norm) { fp_sqr(t3, t3); switch (ep_curve_opt_a()) { case OPT_ZERO: break; case OPT_ONE: fp_add(t5, t5, ep_curve_get_a()); break; case OPT_DIGIT: fp_mul_dig(t1, t3, ep_curve_get_a()[0]); fp_add(t5, t5, t1); break; default: fp_mul(t1, ep_curve_get_a(), t3); fp_add(t5, t5, t1); break; } } else { switch (ep_curve_opt_a()) { case OPT_ZERO: break; case OPT_ONE: fp_add_dig(t5, t5, (dig_t)1); break; case OPT_DIGIT: fp_add_dig(t5, t5, ep_curve_get_a()[0]); break; default: fp_add(t5, t5, ep_curve_get_a()); break; } } /* x3 = T = M^2 - 2 * S. */ fp_sqr(r->x, t5); fp_dbl(t1, t4); fp_sub(r->x, r->x, t1); /* y3 = M * (S - T) - 8 * y1^4. */ fp_dbl(t2, t2); fp_dbl(t2, t2); fp_dbl(t2, t2); fp_sub(t4, t4, r->x); fp_mul(t5, t5, t4); fp_sub(r->y, t5, t2); } r->norm = 0; } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp_free(t0); fp_free(t1); fp_free(t2); fp_free(t3); fp_free(t4); fp_free(t5); } }
void pp_dbl_lit_k12(fp12_t l, ep_t r, ep_t p, ep2_t q) { fp_t t0, t1, t2, t3, t4, t5, t6; int one = 1, zero = 0; fp_null(t0); fp_null(t1); fp_null(t2); fp_null(t3); fp_null(t4); fp_null(t5); fp_null(t6); TRY { fp_new(t0); fp_new(t1); fp_new(t2); fp_new(t3); fp_new(t4); fp_new(t5); fp_new(t6); fp_sqr(t0, p->x); fp_sqr(t1, p->y); fp_sqr(t2, p->z); fp_mul(t4, ep_curve_get_b(), t2); fp_dbl(t3, t4); fp_add(t3, t3, t4); fp_add(t4, p->x, p->y); fp_sqr(t4, t4); fp_sub(t4, t4, t0); fp_sub(t4, t4, t1); fp_add(t5, p->y, p->z); fp_sqr(t5, t5); fp_sub(t5, t5, t1); fp_sub(t5, t5, t2); fp_dbl(t6, t3); fp_add(t6, t6, t3); fp_sub(r->x, t1, t6); fp_mul(r->x, r->x, t4); fp_add(r->y, t1, t6); fp_sqr(r->y, r->y); fp_sqr(t4, t3); fp_dbl(t6, t4); fp_add(t6, t6, t4); fp_dbl(t6, t6); fp_dbl(t6, t6); fp_sub(r->y, r->y, t6); fp_mul(r->z, t1, t5); fp_dbl(r->z, r->z); fp_dbl(r->z, r->z); r->norm = 0; if (ep2_curve_is_twist() == EP_MTYPE) { one ^= 1; zero ^= 1; } fp2_dbl(l[zero][one], q->x); fp2_add(l[zero][one], l[zero][one], q->x); fp_mul(l[zero][one][0], l[zero][one][0], t0); fp_mul(l[zero][one][1], l[zero][one][1], t0); fp_sub(l[zero][zero][0], t3, t1); fp_zero(l[zero][zero][1]); fp_mul(l[one][one][0], q->y[0], t5); fp_mul(l[one][one][1], q->y[1], t5); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp_free(t0); fp_free(t1); fp_free(t2); fp_free(t3); fp_free(t4); fp_free(t5); fp_free(t6); } }
/** * Computes the constants required for evaluating Frobenius maps. */ static void fp3_calc() { bn_t e; fp3_t t0, t1, t2; ctx_t *ctx = core_get(); bn_null(e); fp3_null(t0); fp3_null(t1); fp3_null(t2); TRY { bn_new(e); fp3_new(t0); fp3_new(t1); fp3_new(t2); fp_set_dig(ctx->fp3_base[0], -fp_prime_get_cnr()); fp_neg(ctx->fp3_base[0], ctx->fp3_base[0]); e->used = FP_DIGS; dv_copy(e->dp, fp_prime_get(), FP_DIGS); bn_sub_dig(e, e, 1); bn_div_dig(e, e, 3); fp_exp(ctx->fp3_base[0], ctx->fp3_base[0], e); fp_sqr(ctx->fp3_base[1], ctx->fp3_base[0]); fp3_zero(t0); fp_set_dig(t0[1], 1); dv_copy(e->dp, fp_prime_get(), FP_DIGS); bn_sub_dig(e, e, 1); bn_div_dig(e, e, 6); /* t0 = u^((p-1)/6). */ fp3_exp(t0, t0, e); fp_copy(ctx->fp3_p[0], t0[2]); fp3_sqr(t1, t0); fp_copy(ctx->fp3_p[1], t1[1]); fp3_mul(t2, t1, t0); fp_copy(ctx->fp3_p[2], t2[0]); fp3_sqr(t2, t1); fp_copy(ctx->fp3_p[3], t2[2]); fp3_mul(t2, t2, t0); fp_copy(ctx->fp3_p[4], t2[1]); fp_mul(ctx->fp3_p2[0], ctx->fp3_p[0], ctx->fp3_base[1]); fp_mul(t0[0], ctx->fp3_p2[0], ctx->fp3_p[0]); fp_neg(ctx->fp3_p2[0], t0[0]); for (int i = -1; i > fp_prime_get_cnr(); i--) { fp_sub(ctx->fp3_p2[0], ctx->fp3_p2[0], t0[0]); } fp_mul(ctx->fp3_p2[1], ctx->fp3_p[1], ctx->fp3_base[0]); fp_mul(ctx->fp3_p2[1], ctx->fp3_p2[1], ctx->fp3_p[1]); fp_sqr(ctx->fp3_p2[2], ctx->fp3_p[2]); fp_mul(ctx->fp3_p2[3], ctx->fp3_p[3], ctx->fp3_base[1]); fp_mul(t0[0], ctx->fp3_p2[3], ctx->fp3_p[3]); fp_neg(ctx->fp3_p2[3], t0[0]); for (int i = -1; i > fp_prime_get_cnr(); i--) { fp_sub(ctx->fp3_p2[3], ctx->fp3_p2[3], t0[0]); } fp_mul(ctx->fp3_p2[4], ctx->fp3_p[4], ctx->fp3_base[0]); fp_mul(ctx->fp3_p2[4], ctx->fp3_p2[4], ctx->fp3_p[4]); fp_mul(ctx->fp3_p3[0], ctx->fp3_p[0], ctx->fp3_base[0]); fp_mul(t0[0], ctx->fp3_p3[0], ctx->fp3_p2[0]); fp_neg(ctx->fp3_p3[0], t0[0]); for (int i = -1; i > fp_prime_get_cnr(); i--) { fp_sub(ctx->fp3_p3[0], ctx->fp3_p3[0], t0[0]); } fp_mul(ctx->fp3_p3[1], ctx->fp3_p[1], ctx->fp3_base[1]); fp_mul(t0[0], ctx->fp3_p3[1], ctx->fp3_p2[1]); fp_neg(ctx->fp3_p3[1], t0[0]); for (int i = -1; i > fp_prime_get_cnr(); i--) { fp_sub(ctx->fp3_p3[1], ctx->fp3_p3[1], t0[0]); } fp_mul(ctx->fp3_p3[2], ctx->fp3_p[2], ctx->fp3_p2[2]); fp_mul(ctx->fp3_p3[3], ctx->fp3_p[3], ctx->fp3_base[0]); fp_mul(t0[0], ctx->fp3_p3[3], ctx->fp3_p2[3]); fp_neg(ctx->fp3_p3[3], t0[0]); for (int i = -1; i > fp_prime_get_cnr(); i--) { fp_sub(ctx->fp3_p3[3], ctx->fp3_p3[3], t0[0]); } fp_mul(ctx->fp3_p3[4], ctx->fp3_p[4], ctx->fp3_base[1]); fp_mul(t0[0], ctx->fp3_p3[4], ctx->fp3_p2[4]); fp_neg(ctx->fp3_p3[4], t0[0]); for (int i = -1; i > fp_prime_get_cnr(); i--) { fp_sub(ctx->fp3_p3[4], ctx->fp3_p3[4], t0[0]); } for (int i = 0; i < 5; i++) { fp_mul(ctx->fp3_p4[i], ctx->fp3_p[i], ctx->fp3_p3[i]); fp_mul(ctx->fp3_p5[i], ctx->fp3_p2[i], ctx->fp3_p3[i]); } } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { bn_free(e); fp3_free(t0); fp3_free(t1); fp3_free(t2); } }
void fp3_sub_basic(fp2_t c, fp2_t a, fp2_t b) { fp_sub(c[0], a[0], b[0]); fp_sub(c[1], a[1], b[1]); fp_sub(c[2], a[2], b[2]); }
/** * Doubles a point represented in affine coordinates on an ordinary prime * elliptic curve. * * @param[out] r - the result. * @param[out] s - the slope. * @param[in] p - the point to double. */ static void ep_dbl_basic_imp(ep_t r, fp_t s, const ep_t p) { 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 = 1/2 * y1. */ fp_dbl(t0, p->y); fp_inv(t0, t0); /* t1 = 3 * x1^2 + a. */ fp_sqr(t1, p->x); fp_copy(t2, t1); fp_dbl(t1, t1); fp_add(t1, t1, t2); switch (ep_curve_opt_a()) { case OPT_ZERO: break; case OPT_ONE: fp_add_dig(t1, t1, (dig_t)1); break; #if FP_RDC != MONTY case OPT_DIGIT: fp_add_dig(t1, t1, ep_curve_get_a()[0]); break; #endif default: fp_add(t1, t1, ep_curve_get_a()); break; } /* t1 = (3 * x1^2 + a)/(2 * y1). */ fp_mul(t1, t1, t0); if (s != NULL) { fp_copy(s, t1); } /* t2 = t1^2. */ fp_sqr(t2, t1); /* x3 = t1^2 - 2 * x1. */ fp_dbl(t0, p->x); fp_sub(t0, t2, t0); /* y3 = t1 * (x1 - x3) - y1. */ fp_sub(t2, p->x, t0); fp_mul(t1, t1, t2); fp_sub(r->y, t1, p->y); fp_copy(r->x, t0); fp_copy(r->z, p->z); r->norm = 1; } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp_free(t0); fp_free(t1); fp_free(t2); } }