/* * \brief Encrypts/decrypts a message using the RSA algorithm. * * \param result a field to populate with the result of your RSA calculation. * \param message the message to perform RSA on. (probably a cert in this case) * \param d the encryption key from the key_file passed in through the * command-line arguments * \param n the modulus for RSA from the modulus_file passed in through * the command-line arguments * * Fill in this function with your proj0 solution or see staff solutions. */ static void perform_rsa(mpz_t result, mpz_t message, mpz_t d, mpz_t n) { int hex = 16; mpz_t zero, one, two, tmp; char zero_str[] = "0"; char one_str[] = "1"; char two_str[] = "2"; mpz_init(zero); mpz_init(one); mpz_init(two); mpz_init(tmp); mpz_set_str(zero, zero_str, hex); mpz_set_str(one, one_str, hex); mpz_set_str(two, two_str, hex); mpz_set_str(tmp, zero_str, hex); // initilize result = 1; mpz_add(result, zero, one); while (mpz_cmp(d, zero) > 0){ mpz_mod(tmp, d, two); if (mpz_cmp(tmp, one) == 0) { // result = (result * message) % n; mpz_mul(tmp, result, message); mpz_mod(result, tmp, n); // d--; mpz_sub(d, d, one); } // d = d/2; mpz_div(d, d, two); // message = (message * message) % n; mpz_mul(tmp, message, message); mpz_mod(message, tmp, n); } mpz_clear(zero); mpz_clear(one); mpz_clear(two); mpz_clear(tmp); }
int checkpoly_siqs(siqs_poly *poly, mpz_t n) { //check that b^2 == N mod a //and that c == (b*b - n)/a mpz_t t1,t2,t3,t4; mpz_init(t1); mpz_init(t2); mpz_init(t3); mpz_init(t4); mpz_set(t1, n); mpz_tdiv_r(t3, t1, poly->mpz_poly_a); //zDiv(&t1,&poly->poly_a,&t2,&t3); mpz_mul(t2, poly->mpz_poly_b, poly->mpz_poly_b); //zMul(&poly->poly_b,&poly->poly_b,&t2); mpz_tdiv_r(t4, t2, poly->mpz_poly_a); //zDiv(&t2,&poly->poly_a,&t1,&t4); if (mpz_cmp(t3,t4) != 0) { printf("\nError in checkpoly: %s^2 !== N mod %s\n", mpz_conv2str(&gstr1.s, 10, poly->mpz_poly_b), mpz_conv2str(&gstr2.s, 10, poly->mpz_poly_a)); if (mpz_sgn(poly->mpz_poly_b) < 0) printf("b is negative\n"); } if (mpz_kronecker(n, poly->mpz_poly_a) != 1) printf("\nError in checkpoly: (a|N) != 1\n"); mpz_mul(t2, poly->mpz_poly_b, poly->mpz_poly_b); //zMul(&poly->poly_b,&poly->poly_b,&t2); mpz_sub(t2, t2, n); mpz_tdiv_q(t4, t2, poly->mpz_poly_a); //zDiv(&t2,&poly->poly_a,&t1,&t4); if (mpz_cmp(t4,poly->mpz_poly_c) != 0) printf("\nError in checkpoly: c != (b^2 - n)/a\n"); mpz_clear(t1); mpz_clear(t2); mpz_clear(t3); mpz_clear(t4); return 0; }
void BnetSRP3::getClientSecret( mpz_ptr result, mpz_t& B_ ) { mpz_t x, u; getClientPrivateKey(x); getScrambler(u, B_); mpz_t temp; mpz_init(temp); mpz_powm(temp, g, x, N); mpz_init(result); mpz_add(result, N, B_); mpz_sub(result, result, temp); mpz_clear(temp); mpz_init(temp); mpz_mul(temp, x, u); mpz_add(temp, temp, a); mpz_powm(result, result, temp, N); mpz_clear(temp); mpz_clear(x); mpz_clear(u); }
/* * It is a function that performs binomial theorem/expansion * (a+b)^n = {n "summation" k} [n k]*a^(n-k)b*^k * input: mpz_t n, mpz_t k and mpz_t *bin(pointer) to retrieve the output * output: mpz_t *bin as a pointer */ void binomial(mpz_t n, mpz_t k, mpz_t *bin){ mpz_t f1; mpz_t f2; mpz_t f3; mpz_t temp; mpz_t denominator; mpz_init(f1); mpz_init(f2); mpz_init(f3); mpz_init(temp); mpz_init(denominator); factorial(k, &f1); mpz_sub(temp, n, k); factorial(temp, &f2); mpz_mul(denominator, f1, f2); //gmp_printf("%Zd\n", denominator); factorial(n, &f3); //gmp_printf("%Zd\n", f3); mpz_cdiv_q(*bin, f3, denominator); }
int qsieve_extgcd(mpz_t x, mpz_t y, mpz_t xd, mpz_t yd, mpz_t z) { mpz_gcdext(TD, TB, TC, x, y); while(qsieve_getsize(TB) < 0) { mpz_add(TB, TB, y); mpz_sub(TC, TC, x); } if(z != xd && z != yd) if(TD!=z) mpz_set(z, TD); if(xd != yd) { if(TB!=xd) mpz_set(xd, TB); if(TC!=yd) mpz_set(yd, TC); } else { if(TB!=xd) mpz_set(xd, TB); } return qsieve_getsize(TD); }
void pp_init_spare(int target) { int i; pp_pp *pp, *top; mpq_t spare; QINIT(&spare, "pp_study spare"); top = pplist[0]; mpq_set_si(top->spare, -target, 1); for (i = 0; i < pplistsize; ++i) { int g; pp = pplist[i]; mpz_sub(mpq_numref(spare), pp->total, pp->min_discard); mpz_set(mpq_denref(spare), pp->denominator); mpq_canonicalize(spare); mpq_add(top->spare, top->spare, spare); } QCLEAR(&spare, "pp_study spare"); Dprintf("study: spare = %Qd\n", top->spare); }
/* See: * Konstantinou and Kontogeorgis (2008) https://arxiv.org/abs/0804.1652 * http://www.math.leidenuniv.nl/~psh/konto5.pdf */ static void ramanujan_root_to_hilbert_root(mpz_t r, mpz_t N, long D) { mpz_t A, t; if (D < 0) D = -D; if (D % 24 != 11) return; /* croak("Bad Ramanujan root D: %ld", D); */ mpz_init(A); mpz_init(t); if (!mpz_invert(t, r, N)) mpz_set_ui(t, 0); mpz_powm_ui(A, t, 6, N); mpz_mul_ui(A, A, 27); mpz_powm_ui(t, r, 6, N); mpz_sub(A, t, A); mpz_sub_ui(A, A, 6); mpz_powm_ui(r, A, 3, N); /* mpz_powm_ui(t, A, 3, N); * gmp_printf("Converted Ramanujan root %Zd to Hilbert %Zd\n", r, t); * mpz_set(r, t); */ mpz_clear(A); mpz_clear(t); }
void compute_B_C(QS_t * qs_inf, poly_t * poly_inf) { mpz_t * A_mpz = &poly_inf->A_mpz; mpz_t * B_mpz = &poly_inf->B_mpz; mpz_t * C = &poly_inf->C; unsigned long * B = poly_inf->B; mpz_t * mpz_n = &qs_inf->mpz_n; fmpz_to_mpz(*B_mpz, B); mpz_mul(*C, *B_mpz, *B_mpz); mpz_sub(*C, *C, *mpz_n); #if TEST_C mpz_t temp; mpz_init(temp); mpz_mod(temp, *C, *A_mpz); if (mpz_cmp_ui(temp, 0) != 0) gmp_printf("B^2 - n = %Zd is not divisible by A = %Zd\n", *C, *A_mpz); mpz_clear(temp); #endif mpz_divexact(*C, *C, *A_mpz); qs_inf->sieve_fill = 128-mpz_sizeinbase(*C, 2)+qs_inf->error_bits+13;// 16, 20, 20 }
// Normalize coefficient of JS static void NormalizeJS(int PK, int PL, int PM, int P) { int I, J; for (I = PL; I < PK; I++) { if (mpz_cmp_ui(aiJS[I], 0) != 0) /* (!BigNbrIsZero(aiJS[I])) */ { /* biT = aiJS[I]; */ mpz_set(biT, aiJS[I]); for (J = 1; J < P; J++) { /* SubtractBigNbrModN(aiJS[I - J * PM], biT, aiJS[I - J * PM], TestNbr, NumberLength); */ mpz_sub(aiJS[I - J * PM], aiJS[I - J * PM], biT); } /* aiJS[I] = 0; */ mpz_set_ui(aiJS[I], 0); } } for (I = 0; I < PK; I++) mpz_mod(aiJS[I], aiJS[I], TestNbr); }
int UnipolySubCoeffsOfXd(unipoly p1, unipoly p2, int d) { int i; mpz_t z1, z2; if ( UPDeg(p1)>=d ) if ( UPMax(p1)!=0 ) mpz_init_set_si(z1, p1[d].i); else mpz_init_set(z1, p1[d].z); else mpz_init(z1); if ( UPDeg(p2)>=d ) { if ( UPMax(p2)!=0 ) mpz_init_set_si(z2, p2[d].i); else mpz_init_set(z2, p2[d].z); mpz_sub(z1, z1, z2); mpz_clear(z2); } i = mpz_get_si(z1); mpz_clear(z1); return i; }
void calcterm2(int n,mpz_t r) { mpz_t z; calcterm(n,r); mpz_init(z); if(n&1) { mpz_ui_pow_ui(z,2,n/2); mpz_add(r,r,z); } else { mpz_ui_pow_ui(z,2,n/2-1); mpz_add(r,r,z); if(n>=4) { mpz_ui_pow_ui(z,2,n/2-2); mpz_add(r,r,z); } mpz_set_ui(z,0); calcterm(n/2,z); mpz_fdiv_q_ui(z,z,2); mpz_sub(r,r,z); } mpz_clear(z); }
/* * Compute the T_f transform modulo n. * * Because only one quarter of the possible hashes can be signed with * a given key, we need to transform the hash. First, we want to * ensure that the result is nonzero, so we shift the hash by 8 bits * and add a 1 to the end. The resulting number is called m'. * * Second, we want to multiply it by a number k whose Legendre symbols * (k|p) and (k|q) are known, so that (km'|p) = (k|p)(m'|p) = 1 and * (km'|q) = (k|q)(km'|q) = 1. Since we need both to be true * simultaneously, regardless of the values of (m'|p) and (m'|q), we * clearly need four possible values of k. * * As it happens, TI's keys all follow a precise format: they all have * p === 3 and q === 7 (mod 8). As a result, we know that * * (-1|p) = (-1|q) = -1 * * (2|p) = -1, (2|q) = 1 * * So TI has defined the following transformation functions: * * T_0(x) = -2x' * T_1(x) = -x' * T_2(x) = x' * T_3(x) = 2x' * * where x' = 256x + 1. * * In the usual case of p === 3 and q === 7 (mod 8), then, two of the * possible (T_f(m)|p) will equal 1: * * If (m'|p) = 1, then (T_0(m)|p) = (T_2(m)|p) = 1. * If (m'|p) = -1, then (T_1(m)|p) = (T_3(m)|p) = 1. * * Two of the possible (T_f(m)|q) will equal 1: * * If (m'|q) = 1, then (T_2(m)|q) = (T_3(m)|q) = 1. * If (m'|q) = -1, then (T_0(m)|q) = (T_1(m)|q) = 1. * * Thus we can choose exactly one f value with * (T_f(m)|p) = (T_f(m)|q) = 1. * * If r === 5 (mod 8) is a prime, (-1|r) = 1, while (2|r) = -1. Thus * a similar logic holds: * * If (m'|r) = 1, then (T_1(m)|r) = (T_2(m)|r) = 1. * If (m'|r) = -1, then (T_0(m)|r) = (T_3(m)|r) = 1. * * So if {p,q} === {3,5}, {5,7}, or {3,7} (mod 8), given any m, we can * pick an f with (T_f(m)|p) = (T_f(m)|q) = 1. * */ static void applyf(mpz_t res, /* mpz to store result */ const mpz_t m, /* MD5 hash */ const mpz_t n, /* public key */ int f) /* f (0, 1, 2, 3) */ { mpz_mul_ui(res, m, 256); mpz_add_ui(res, res, 1); switch (f) { case 0: mpz_add(res, res, res); case 1: mpz_sub(res, n, res); break; case 2: break; case 3: mpz_add(res, res, res); break; } }
void RSA::decrypt(char* msg, int32_t size) { boost::recursive_mutex::scoped_lock lockClass(rsaLock); mpz_t c, v1, v2, u2, tmp; mpz_init2(c, 1024); mpz_init2(v1, 1024); mpz_init2(v2, 1024); mpz_init2(u2, 1024); mpz_init2(tmp, 1024); mpz_import(c, 128, 1, 1, 0, 0, msg); mpz_mod(tmp, c, m_p); mpz_powm(v1, tmp, m_dp, m_p); mpz_mod(tmp, c, m_q); mpz_powm(v2, tmp, m_dq, m_q); mpz_sub(u2, v2, v1); mpz_mul(tmp, u2, m_u); mpz_mod(u2, tmp, m_q); if (mpz_cmp_si(u2, 0) < 0) { mpz_add(tmp, u2, m_q); mpz_set(u2, tmp); } mpz_mul(tmp, u2, m_p); mpz_set_ui(c, 0); mpz_add(c, v1, tmp); size_t count = (mpz_sizeinbase(c, 2) + 7) / 8; memset(msg, 0, 128 - count); mpz_export(&msg[128 - count], NULL, 1, 1, 0, 0, c); mpz_clear(c); mpz_clear(v1); mpz_clear(v2); mpz_clear(u2); mpz_clear(tmp); }
damgard_jurik_plaintext_t* damgard_jurik::decrypt(damgard_jurik_ciphertext_t* ct) { damgard_jurik_plaintext_t *res = new damgard_jurik_plaintext_t(); mpz_t c_r; mpz_init(c_r); mpz_powm(c_r, ct->text, prvkey->lambda, pubkey->n_j[ct->s + 1]); int i, j; mpz_t t1, t2, t3, i_lamda; mpz_init(t1); mpz_init(t2); mpz_init(t3); mpz_init(i_lamda); mpz_set_ui(i_lamda, 0); for (i = 1;i <= ct->s;++i) { mpz_mod(t1, c_r, pubkey->n_j[i + 1]); damgard_jurik_function_l(t1, t1, pubkey->n_j[1]); mpz_set(t2, i_lamda); for (j = 2;j <= i;j++) { mpz_sub_ui(i_lamda, i_lamda, 1); mpz_mul(t2, t2, i_lamda); mpz_mod(t2, t2, pubkey->n_j[i]); mpz_set(t3, pubkey->k_n[j]); mpz_invert(t3, t3, pubkey->n_j[i]); mpz_mul(t3, t2, t3); mpz_mod(t3, t3, pubkey->n_j[i]); mpz_mul(t3, t3, pubkey->n_j[j - 1]); mpz_mod(t3, t3, pubkey->n_j[i]); mpz_sub(t1, t1, t3); mpz_mod(t1, t1, pubkey->n_j[i]); } mpz_set(i_lamda, t1); } mpz_invert(t3, prvkey->lambda, pubkey->n_j[ct->s]); mpz_mul(res->text, i_lamda, t3); mpz_mod(res->text, res->text, pubkey->n_j[ct->s]); return res; }
void binomialCoefficient(mpz_ptr result, mpz_t top, mpz_t bottom) { mpz_t i; mpz_init(i); mpz_sub(i, top, bottom); mpz_add_ui(i, i, 1); mpz_t numerator; mpz_init_set_ui(numerator, 1); while (mpz_cmp(i, top) < 1) { mpz_mul(numerator, numerator, i); mpz_add_ui(i, i, 1); } mpz_set_ui(i, 2); mpz_t denominator; mpz_init_set_ui(denominator, 1); while (mpz_cmp(i, bottom) < 1) { mpz_mul(denominator, denominator, i); mpz_add_ui(i, i, 1); } mpz_fdiv_q(result, numerator, denominator); mpz_clear(i); mpz_clear(numerator); mpz_clear(denominator); }
void check_one (mpz_srcptr w, mpz_srcptr x, mpz_srcptr y) { mpz_t want, got; mpz_init (want); mpz_init (got); mpz_mul (want, x, y); mpz_add (want, w, want); mpz_set (got, w); mpz_addmul (got, x, y); MPZ_CHECK_FORMAT (got); if (mpz_cmp (want, got) != 0) { printf ("mpz_addmul fail\n"); fail: mpz_trace ("w", w); mpz_trace ("x", x); mpz_trace ("y", y); mpz_trace ("want", want); mpz_trace ("got ", got); abort (); } mpz_mul (want, x, y); mpz_sub (want, w, want); mpz_set (got, w); mpz_submul (got, x, y); MPZ_CHECK_FORMAT (got); if (mpz_cmp (want, got) != 0) { printf ("mpz_submul fail\n"); goto fail; } mpz_clear (want); mpz_clear (got); }
int descifra_caracter_afin(int cha, mpz_t a, mpz_t b , mpz_t m){ if(cha==10){ cha=32; } int cifint=0,mAux; mpz_t cif; mAux = mpz_get_ui(m); if(cha>64 && cha<91){ cha+=32; } if(cha<97 || cha>122){ if(cha == 32){ cha = mAux-1; }else{ printf("ERROR, esto no es una letra\n"); return(-1); } } if(cha!=(mAux-1)){ cha-=97; } mpz_init_set_ui(cif,cha); mpz_sub(cif,cif,b); mpz_mul(cif, cif,a); mpz_mod(cif,cif,m); cifint = mpz_get_ui(cif); mpz_clear(cif); if(cifint==mAux-1){ cifint = 32; }else{ cifint+=97; } return cifint; }
void check_one_ui (mpz_ptr w, mpz_ptr x, unsigned long y) { mpz_t want, got; mpz_init (want); mpz_init (got); mpz_mul_ui (want, x, (unsigned long) y); mpz_add (want, w, want); mpz_set (got, w); mpz_addmul_ui (got, x, (unsigned long) y); MPZ_CHECK_FORMAT (got); if (mpz_cmp (want, got) != 0) { printf ("mpz_addmul_ui fail\n"); fail: mpz_trace ("w", w); mpz_trace ("x", x); printf ("y=0x%lX %lu\n", y, y); mpz_trace ("want", want); mpz_trace ("got ", got); abort (); } mpz_mul_ui (want, x, y); mpz_sub (want, w, want); mpz_set (got, w); mpz_submul_ui (got, x, y); MPZ_CHECK_FORMAT (got); if (mpz_cmp (want, got) != 0) { printf ("mpz_submul_ui fail\n"); goto fail; } mpz_clear (want); mpz_clear (got); }
void getChebPolyCoeffs(mpz_t* chebCoeffsMatrix, int n, mp_prec_t prec){ int i,j; mpz_t temp; /*mp_prec_t prec;*/ /*prec = getToolPrecision();*/ mpz_init2(temp, prec); for (i=0;i<n;i++) for (j=0;j<n;j++) mpz_set_ui(chebCoeffsMatrix[i*n+j],0); if (n>0) mpz_set_ui(chebCoeffsMatrix[0],1); if (n>1) mpz_set_ui(chebCoeffsMatrix[n+1],1); for (i=2;i<n;i++){ mpz_neg(chebCoeffsMatrix[i*n],chebCoeffsMatrix[(i-2)*n]); /*put constant coeff of T_{n-2}*/ for (j=0;j<i;j++){ mpz_mul_ui(temp,chebCoeffsMatrix[(i-1)*n+j],2); mpz_sub(chebCoeffsMatrix[i*n+j+1],temp,chebCoeffsMatrix[(i-2)*n+j+1]); } } mpz_clear(temp); }
void nettle_mpz_set_str_256_s(mpz_t x, size_t length, const uint8_t *s) { if (!length) { mpz_set_ui(x, 0); return; } nettle_mpz_from_octets(x, length, s); if (s[0] & 0x80) { mpz_t t; mpz_init_set_ui(t, 1); mpz_mul_2exp(t, t, length*8); mpz_sub(x, x, t); mpz_clear(t); } }
static int transform_sections (mpz_t X1, mpz_t X2, mpz_t no_of_elements, gfc_expr * l_start, gfc_expr * l_end, gfc_expr * l_stride, gfc_expr * r_start, gfc_expr * r_stride) { if (NULL == l_start || NULL == l_end || NULL == r_start) return 1; /* TODO : Currently we check the dependency only when start, end and stride are constant. We could also check for equal (variable) values, and common subexpressions, eg. x vs. x+1. */ if (l_end->expr_type != EXPR_CONSTANT || l_start->expr_type != EXPR_CONSTANT || r_start->expr_type != EXPR_CONSTANT || ((NULL != l_stride) && (l_stride->expr_type != EXPR_CONSTANT)) || ((NULL != r_stride) && (r_stride->expr_type != EXPR_CONSTANT))) { return 1; } get_no_of_elements (no_of_elements, l_end, l_start, l_stride); mpz_sub (X1, r_start->value.integer, l_start->value.integer); if (l_stride != NULL) mpz_cdiv_q (X1, X1, l_stride->value.integer); if (r_stride == NULL) mpz_set (X2, no_of_elements); else mpz_mul (X2, no_of_elements, r_stride->value.integer); if (l_stride != NULL) mpz_cdiv_q (X2, X2, r_stride->value.integer); mpz_add (X2, X2, X1); return 0; }
//------------------------------------------------------------------------------ // Name: sub //------------------------------------------------------------------------------ knumber_base *knumber_integer::sub(knumber_base *rhs) { if(knumber_integer *const p = dynamic_cast<knumber_integer *>(rhs)) { mpz_sub(mpz_, mpz_, p->mpz_); return this; } else if(knumber_float *const p = dynamic_cast<knumber_float *>(rhs)) { knumber_float *f = new knumber_float(this); delete this; return f->sub(p); } else if(knumber_fraction *const p = dynamic_cast<knumber_fraction *>(rhs)) { knumber_fraction *q = new knumber_fraction(this); delete this; return q->sub(p); } else if(knumber_error *const p = dynamic_cast<knumber_error *>(rhs)) { knumber_base *e = p->clone(); delete this; return e->neg(); } Q_ASSERT(0); return 0; }
void check_random (int reps) { gmp_randstate_t rands; mpz_t a, d, r; int i; int want; gmp_randinit_default(rands); mpz_init (a); mpz_init (d); mpz_init (r); for (i = 0; i < reps; i++) { mpz_erandomb (a, rands, 512); mpz_erandomb_nonzero (d, rands, 512); mpz_fdiv_r (r, a, d); want = (mpz_sgn (r) == 0); check_one (a, d, want); mpz_sub (a, a, r); check_one (a, d, 1); if (mpz_cmpabs_ui (d, 1L) == 0) continue; mpz_add_ui (a, a, 1L); check_one (a, d, 0); } mpz_clear (a); mpz_clear (d); mpz_clear (r); gmp_randclear(rands); }
// base64 will be a pointer to the converted string long hex_to_base64(char *hex, char **base64) { mpz_t decimal; mpz_init_set_str (decimal, hex, 16); mpz_t base; mpz_init_set_ui(base, 64); mpz_t place_value; mpz_init(place_value); long power = 0; mpz_pow_ui(place_value, base, power); while(mpz_cmp(decimal, place_value) > 0) { power++; mpz_pow_ui(place_value, base, power); } long length = power; *base64 = (char *) malloc(length); if(power > 0) { power--; } while(power >= 0) { mpz_pow_ui(place_value, base, power); mpz_t quotient; mpz_init(quotient); mpz_fdiv_q(quotient, decimal, place_value); unsigned long quotient_ui = mpz_get_ui(quotient); char place_char = base64_table[quotient_ui]; (*base64)[(length - 1) - power] = place_char; mpz_t decrement; mpz_init(decrement); mpz_mul_ui(decrement, place_value, quotient_ui); mpz_sub(decimal, decimal, decrement); power--; } return length; }
/* Returns new Item that combines information from op1 and op2 via CRT. */ Item* CRT(Item* op1, Item* op2) { Item* res = new Item; // let n1, n2 be the moduli, and r1, r2 be the residues // res->modulus = t, where t = 0 mod n1, t = 1 mod n2 mpz_invert(res->modulus, op1->modulus, op2->modulus); mpz_mul(res->modulus, res->modulus, op1->modulus); // res->residue = r2 - r1 mpz_sub(res->residue, op2->residue, op1->residue); // res->residue = t * (r2 - r1) mpz_mul(res->residue, res->residue, res->modulus); // res->residue = r1 + t * (r2 - r1) mpz_add(res->residue, res->residue, op1->residue); // res->modulus = n1 * n2 mpz_mul(res->modulus, op1->modulus, op2->modulus); // res->residue = r1 mod n1, r2 = mod n2 mpz_mod(res->residue, res->residue, res->modulus); return res; }
void MultByOneMinusXExp (unipoly p, int exp) { int d =UPDeg(p), de; de = d+exp; if ( de>UPSize(p) ) { printf("Err:MultByOneMinusXExp\n");p=UnipolyChangeSize(p,de);} if ( UPMax(p)!=0 ) { if (d >= exp) for ( ; de>UPDeg(p) ; --de,--d ) p[de].i = -p[d].i; for ( ; d>=0 ; --de,--d) p[de].i = p[de].i - p[d].i; UPSetDeg(p, UPDeg(p)+exp); UPSetMax(p, 2*UPMax(p)); if ( UPMax(p)>INT_MAX/2 ) UnipolyResetMax(p); } else { if (d >= exp) for ( ; de>UPDeg(p) ; --de,--d ) mpz_neg(p[de].z, p[d].z); for ( ; d>=0 ; --de,--d) mpz_sub(p[de].z, p[de].z, p[d].z); UPSetDeg(p, UPDeg(p)+exp); } }
Vertex::Vertex(double x, double y, double z, double radius,int index,double scale) { mpz_t temp1,temp2; this->Index = index; this->Coordinates[1] = this->NormCoordinates[1] = x; this->Coordinates[2] = this->NormCoordinates[2] = y; this->Coordinates[3] = this->NormCoordinates[3] = z; this->Radius = this->BackRadius = radius; this->Weight = this->BackWeight = pow(x,2) + pow(y,2) + pow(z,2) - pow(radius,2); for(int i=1;i<4;i++) { mpz_init(this->V[i]); mpz_set_d(this->V[i],this->Coordinates[i]*scale); } mpz_init(temp1);mpz_init(temp2); mpz_init(this->V[4]); mpz_set_d(temp1,this->Radius*(scale)); mpz_mul(temp1,temp1,temp1); mpz_mul(temp2,this->V[3],this->V[3]), mpz_sub(temp1,temp2,temp1); mpz_mul(temp2,this->V[2],this->V[2]), mpz_add(temp1,temp2,temp1); mpz_mul(temp2,this->V[1],this->V[1]), mpz_add(this->V[4],temp2,temp1); mpz_clear(temp1);mpz_clear(temp2); this->Redinfo = 0; this->ranValue = 0; this->Hull = -1; this->AlphaStatus = -1; this->Coef = 1.0; this->Rho = 0; this->Mu1 = 0; this->Mu2 = 0; this->Repeats = -1; this->ufKey = -1; this->valid = true; selected = 0; }
void RSA::decrypt(char* msg, uint32 size) { mpz_t c,v1,v2,u2,tmp; mpz_init2(c, 1024); mpz_init2(v1, 1024); mpz_init2(v2, 1024); mpz_init2(u2, 1024); mpz_init2(tmp, 1024); mpz_import(c, size, 1, 1, 0, 0, msg); mpz_mod(tmp, c, m_p); mpz_powm(v1, tmp, m_dp, m_p); mpz_mod(tmp, c, m_q); mpz_powm(v2, tmp, m_dq, m_q); mpz_sub(u2, v2, v1); mpz_mul(tmp, u2, m_u); mpz_mod(u2, tmp, m_q); if(mpz_cmp_si(u2, 0) < 0){ mpz_add(tmp, u2, m_q); mpz_set(u2, tmp); } mpz_mul(tmp, u2, m_p); mpz_set_ui(c, 0); mpz_add(c, v1, tmp); size_t count = (mpz_sizeinbase(c, 2) + 7)/8; memset(msg, 0, size - count); mpz_export(&msg[size - count], NULL, 1, 1, 0, 0, c); mpz_clear(c); mpz_clear(v1); mpz_clear(v2); mpz_clear(u2); mpz_clear(tmp); }
void save_smooth_number(smooth_number_t n) { mpz_clear(tmp_matrix_row); /* tmp_matrix_row must be initialized already */ mpz_init2(tmp_matrix_row, nb_qr_primes); /* init a vector of *exactly* nb_qr_primes bits */ if (nb_smooth_numbers_found > nb_qr_primes + NB_VECTORS_OFFSET - 1) /* if we have sufficient smooth numbers, skip saving */ return; smooth_number_t tmp; mpz_init(tmp.value_x); mpz_init(tmp.value_x_squared); mpz_set(tmp.value_x, n.value_x); mpz_pow_ui(tmp.value_x_squared, n.value_x, 2); mpz_sub(tmp.value_x_squared, tmp.value_x_squared, N); /* x²-N. Saving this will enable us to not go through exponents * and reconstruct the original number */ /* otherwise we can reconstruct value_x_squared from the exponents vector, this is useful in the factoring step * to calculate the square modulo N from the factors directly. * It takes a lot of space, doesn't woth it anyways */ //tmp.factors_exp = calloc(nb_qr_primes, sizeof(uint64_t)); //memcpy(tmp.factors_exp, n.factors_exp, nb_qr_primes * sizeof(uint64_t)); //reconstruct_mpz(tmp.value_x_squared, tmp.factors_exp); smooth_numbers[nb_smooth_numbers_found++] = tmp; /*** reconstruct and saves the smooth number to the GF2 matrix ***//* already done in the sieving step */ /* uint64_t i; for(i=0; i<nb_qr_primes; i++) { if(n.factors_exp[i]&1) mpz_setbit(tmp_matrix_row, i); }*/ /* the coefficient vector in GF2 has already been constructed */ push_row(&matrix, n.factors_vect); }
static Variant HHVM_FUNCTION(gmp_sub, const Variant& dataA, const Variant& dataB) { mpz_t gmpDataA, gmpDataB, gmpReturn; if (!variantToGMPData(cs_GMP_FUNC_NAME_GMP_SUB, gmpDataA, dataA)) { return false; } if (!variantToGMPData(cs_GMP_FUNC_NAME_GMP_SUB, gmpDataB, dataB)) { mpz_clear(gmpDataA); return false; } mpz_init(gmpReturn); mpz_sub(gmpReturn, gmpDataA, gmpDataB); Variant ret = NEWOBJ(GMPResource)(gmpReturn); mpz_clear(gmpDataA); mpz_clear(gmpDataB); mpz_clear(gmpReturn); return ret; }