Skip to content

Commit

Permalink
Create math.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Jul 21, 2024
1 parent bf473c6 commit 345c43a
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions utils/math.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { BigInteger } from 'big-integer';

class Math {
static async generatePrime(bits) {
// Generate a prime number of a given bit length
let prime;
do {
prime = BigInteger.random(bits);
} while (!prime.isPrime());
return prime.toString();
}

static async modularExponentiation(base, exponent, modulus) {
// Perform modular exponentiation
return BigInteger.modPow(base, exponent, modulus).toString();
}

static async extendedGCD(a, b) {
// Compute the extended greatest common divisor
const gcd = BigInteger.gcd(a, b);
const x = BigInteger.modInverse(a, b);
const y = BigInteger.modInverse(b, a);
return { gcd: gcd.toString(), x: x.toString(), y: y.toString() };
}

static async isPrime(number) {
// Check if a number is prime
return BigInteger(number).isPrime();
}

static async randomInteger(min, max) {
// Generate a random integer within a range
return BigInteger.randomBetween(min, max).toString();
}
}

export default Math;

0 comments on commit 345c43a

Please sign in to comment.