-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |