BigInt calculate_result(const BigInt c1, const BigInt c2, const BigInt d1, const BigInt d2, const BigInt order) { BigInt numerator, denominator, result; if(d1 == d2) { printf("Cannot calculate the discrete log\n"); return -1; } /* numerator = (c1 - c2) mod order */ numerator = (c1 - c2) % order; if(numerator < 0) numerator += order; /* denominator (d2 - d1)^(-1) mod order */ denominator = (d2 - d1) % order; if(denominator < 0) denominator += order; denominator = modInverse(denominator, order); /* result */ result = (numerator * denominator) % order; return result; }
int main() { fact[0]=1; for(long long i = 1; i <=1000001; i++ ) fact[i] = (fact[i-1]*i)%mod; int t,tt=1; scanf("%d",&t); while(tt<=t){ long long n,r,mult,inv,rslt; scanf("%lld %lld",&n,&r); if(r==0||n==r) { printf("Case %d: 1\n",tt++); continue; } mult=(fact[r]*fact[n-r])%mod; inv = modInverse(mult,mod); rslt=(fact[n]*inv)%mod; printf("Case %d: %lld\n",tt++,rslt); } return 0; }
void MainWindow::Inverter() { int modmul = modInverse(ui->A->currentText().toInt(), 26); int modadd = 26 - ((ui->B->currentText().toInt() * modmul) % 26); ui->A->setCurrentText(QString::number(modmul)); ui->B->setCurrentText(QString::number(modadd)); }
// use lagrange polynomials to find f(0) of the curve, which is the secret number TINT JoinShares(const TShares& shares) const { // make sure there is at elast the minimum number of shares assert(shares.size() >= size_t(c_sharesNeeded)); // Sigma summation loop TINT sum = 0; for (TINT j = 0; j < c_sharesNeeded; ++j) { TINT y_j = shares[(size_t)j][1]; TINT numerator = 1; TINT denominator = 1; // Pi product loop for (TINT m = 0; m < c_sharesNeeded; ++m) { if (m == j) continue; numerator = (numerator * shares[(size_t)m][0]) % c_prime; denominator = (denominator * (shares[(size_t)m][0] - shares[(size_t)j][0])) % c_prime; } sum = (c_prime + sum + y_j * numerator * modInverse(denominator, c_prime)) % c_prime; } return sum; }
int join_shares(int *xy_pairs, int n) { int secret = 0; long numerator; long denominator; long startposition; long nextposition; long value; int i; int j; for (i = 0; i < n; ++i) { numerator = 1; denominator = 1; for (j = 0; j < n; ++j) { if(i != j) { startposition = xy_pairs[i*2]; nextposition = xy_pairs[j*2]; numerator = (numerator * -nextposition) % prime; denominator = (denominator * (startposition - nextposition)) % prime; } } value = xy_pairs[i * 2 + 1]; secret = (secret + (value * numerator * modInverse(denominator))) % prime; } /* Sometimes we're getting negative numbers, and need to fix that */ secret = (secret + prime) % prime; return secret; }
//сложение точки P c собой же ECPoint ECPoint::Double(ECPoint & pnt) { ECPoint *p2 = new ECPoint(); (*p2).a = pnt.a; (*p2).b = pnt.b; (*p2).p = pnt.p; mpz_class dy = 3 * pnt.x * pnt.x + pnt.a; mpz_class dx = 2 * pnt.y; if (dx < 0) dx += pnt.p; if (dy < 0) dy += pnt.p; mpz_class m = (dy * modInverse(dx, pnt.p)) % pnt.p; (*p2).x = (m * m - pnt.x - pnt.x) % pnt.p; (*p2).y = (m * (pnt.x - (*p2).x) - pnt.y) % pnt.p; if ((*p2).x < 0) (*p2).x += pnt.p; if ((*p2).y < 0) (*p2).y += pnt.p; return (*p2); }
//сложение двух точек P1 и P2 ECPoint operator+(ECPoint & p1, ECPoint & p2) { ECPoint *p3 = new ECPoint(); p3->a = p1.a; p3->b = p1.b; p3->p = p1.p; mpz_class dy = p2.y-p1.y; mpz_class dx = p2.x-p1.x; if (dx < 0) dx = dx + p1.p; if (dy < 0) dy = dy + p1.p; mpz_class alpha = (dy * modInverse(dx,p1.p)) % p1.p; if (alpha < 0) alpha = alpha + p1.p; (*p3).x = (alpha * alpha - p1.x - p2.x) % p1.p; (*p3).y = (-p1.y + alpha * (p1.x - (*p3).x)) % p1.p; if ((*p3).x < 0) (*p3).x += p1.p; if ((*p3).y < 0) (*p3).y += p1.p; return *p3; }
void add_different() { temp=(((y_q-y_p)%p)*modInverse(x_q-x_p,p))%p; lambda = temp;//modInverse(temp,p); x_r = (pow(lambda,2)-x_p-y_p); x_r = x_r%p; y_r = (lambda*(x_p - x_r)- y_p) % p; printf("\n\n\tResultant x = %d\n\tResultant Y = %d",x_r,y_r); }
//precomputes factorial modulo M and inverse Facctorial modulo M void pre_fact() { invFact[0] = 1; fact[0] = 1; for ( lli i = 1; i <= LIM; i++ ) { fact[i] = (fact[i-1]*i)%M; invFact[i] = (invFact[i-1]*modInverse(i,M)) % M; } return; }
int InversiveGenerator::operator() (){ int randomNumber; if(this->getSeed() == 0){ randomNumber = this->b; }else{ randomNumber = (a*modInverse(this->getSeed(), this->getMax() )+b) % getMax(); } randomNumber = this->getMin() + randomNumber % (this->getMax()-this->getMin()+1); this->setSeed(randomNumber); return randomNumber; }
void publickey(int n) { xq = xp; yq = yp; for(int i = 0; i<n-1; i++) { if(xp == xq && yp == yq) { temp = (3*pow(xp,2)) + a; temp = temp%p; lambda = (temp*modInverse(2*yp,p))%p; } else { temp = (((yq-yp)%p)*modInverse(xq-xp,p))%p; lambda = temp; } xr = (pow(lambda,2)-xp-xq); xr = xr%p; yr = (lambda*(xp-xr)-yp)%p; if(xr<0) { xr=p+xr; } if(yr<0) { yr=p+yr; } xq = xr; yq = yr; } }
int main(void) { int t,n,i,mi; fact[0]=1; scanf("%d",&t); for(i=1;i<=2*1000000;i++){fact[i]=i*fact[i-1];fact[i]%=MOD;} while(t--){ scanf("%d",&n); mi=(int)((fact[n-1]*fact[n-1])%MOD); mi=modInverse(mi); ans=(mi*fact[2*n-1]); if(ans>MOD)ans%=MOD; printf("%lld\n",ans); } return 0; }
/* * 解密 * * @param key 密钥 { a, b } * @param src 待解密的字符串 * @param dest 经过解密后的字符串 */ char * decrypt(int* key, char* src, char* dest) { char *pSrc = src; char *pDest = dest; int arr[2] = { modInverse(key[0], WIDTH), -key[1] }; for (int i = 0; *pSrc; ++i, ++pSrc, ++pDest) { if (!isalpha(*pSrc)) continue; *pDest = unshift(arr, toupper(*pSrc)); } return dest; }
//обратное превращение числа из СОК в традиционную десятичную алгебру //происходит по Китайской теореме об остатках (см. RNS theory, p 216) mpz_class RNSNumber::getConventionalNumber() { mpz_class Mtot; Mtot = 1; for(int i = 0; i < this->system.base.size(); ++i) Mtot *= this->system.base[i]; std::vector<mpz_class> M; std::vector<mpz_class> Minv; for(int i = 0; i < this->system.base.size(); ++i) { M.push_back(Mtot/this->system.base[i]); Minv.push_back(modInverse(M[i], this->system.base[i])); } mpz_class SUM; SUM = 0; for(int i = 0; i < this->system.base.size(); ++i) { SUM += this->residues[i] * M[i] * Minv[i]; } return SUM%Mtot; }
void add_same() { temp = (3*(pow(x_p,2)) + 9); temp = temp % p; lambda=( temp *modInverse(2*y_p,p)) %p; //lambda = modInverse(temp,p); printf("%d",lambda); x_r = (pow(lambda,2)-x_p-y_p); x_r = x_r%p; while(x_r<=0) { x_r=x_r+p; } y_r = (lambda*(x_p - x_r)- y_p) % p; while(y_r<=0) { y_r=y_r+p; } printf("\n\n\tResultant x = %d\n\tResultant Y = %d",x_r,y_r); }
int main(int argc, char * * argv) { unsigned long p, q; unsigned long x, n, d, e; unsigned long i; FILE *pub = fopen("rsa.pub", "wb"); FILE *pri = fopen("rsa.pri", "wb"); bool found = false; bool random = true; bool debug = false; random = argc > 1 ? false : true; srand(time(0)); if (random) { p = rand() % 16383; printf("Using p=%lu\n", p); while (!isprime(p)) { p++; } while (q < p) { q = rand() % 16383 + 16380; } printf("Using q=%lu\n", q); while (!isprime(q)) { q++; } } else { printf("P (prime): "); scanf("%lu", &p); printf("Q (prime): "); scanf("%lu", &q); } n = p*q; x = totient(p, q); srand(time(0)); for (i = rand() % x; i < x && !found; i++) { if (isprime(i) && x % i != 0) { e = i; found = true; } } //printf("E (coprime): "); //scanf("%lu", &e); d = modInverse(e, x); printf("Public: n=%lu\te=%lu\n", n, e); printf("Private: n=%lu\td=%lu\n", n, d); fwrite(&n, sizeof(unsigned long), 1, pub); fwrite(&n, sizeof(unsigned long), 1, pri); fwrite(&e, sizeof(unsigned long), 1, pub); fwrite(&d, sizeof(unsigned long), 1, pri); fflush(pub); fflush(pri); fclose(pub); fclose(pri); if (debug) { printf("p=%lu\tq=%lu\tn=%lu\n" "x=%lu\td=%lu\n\n", p, q, n, x, d); } printf("Testing with value '15'\n"); unsigned long int enc = 0; enc = encrypt(15, n, e); printf("Encrypted: %lu\t", enc); enc = decrypt(enc, n, d); printf("Decrypted: %lu\n", enc); return 0; }
void dhke() { printf("\n\n\tENTER THE VALUES OF Xg AND Yg : "); scanf("%d%d",&x_g,&y_g); na = rand()%10+2; nb = rand()%10+2; //public key of A x_r = x_g; y_r = y_g; for(i=0;i<na-1;i++) { if(x_g == x_r && y_g == y_r) { temp = (3*(pow(x_g,2)) + 9); temp = temp % p; lambda=( temp *modInverse(2*y_g,p)) %p; } else { temp=(((y_r-y_g)%p)*modInverse(x_r-x_g,p))%p; lambda = temp;//modInverse(temp,p); } x_r = (pow(lambda,2)-x_g-x_r); x_r = x_r%p; y_r = (lambda*(x_g - x_r)- y_g) % p; } pa_x = x_r; pa_y = y_r; //public key of B x_r = x_g; y_r = y_g; for(i=0;i<nb-1;i++) { if(x_g == x_r && y_g == y_r) { temp = (3*(pow(x_g,2)) + 9); temp = temp % p; lambda=( temp *modInverse(2*y_g,p)) %p; } else { temp=(((y_r-y_g)%p)*modInverse(x_r-x_g,p))%p; lambda = temp;//modInverse(temp,p); } x_r = (pow(lambda,2)-x_g-x_r); x_r = x_r%p; y_r = (lambda*(x_g - x_r)- y_g) % p; } pb_x = x_r; pb_y = y_r; //secret key of A x_r = pb_x; y_r = pb_y; for(i=0;i<na;i++) { if(x_g == x_r && y_g == y_r) { temp = (3*(pow(x_g,2)) + 9); temp = temp % p; lambda=( temp *modInverse(2*y_g,p)) %p; } else { temp=(((y_r-y_g)%p)*modInverse(x_r-x_g,p))%p; lambda = temp;//modInverse(temp,p); } x_r = (pow(lambda,2)-x_g-x_r); x_r = x_r%p; y_r = (lambda*(x_g - x_r)- y_g) % p; } ka_x = x_r; ka_y = y_r; //secret of b x_r = pa_x; y_r = pa_y; for(i=0;i<nb;i++) { if(x_g == x_r && y_g == y_r) { temp = (3*(pow(x_g,2)) + 9); temp = temp % p; lambda=( temp *modInverse(2*y_g,p)) %p; } else { temp=(((y_r-y_g)%p)*modInverse(x_r-x_g,p))%p; lambda = temp;//modInverse(temp,p); } x_r = (pow(lambda,2)-x_g-x_r); x_r = x_r%p; y_r = (lambda*(x_g - x_r)- y_g) % p; } kb_x = x_r; kb_y = y_r; if(ka_x == kb_x && ka_y == kb_y) { printf("\n\n\tSUCESS EXCHANGE"); } else { printf("\n\n\tWRONG EXCHANGE"); } }