-
Notifications
You must be signed in to change notification settings - Fork 128
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
1 parent
eafc83a
commit 38fbcca
Showing
1 changed file
with
23 additions
and
12 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 |
---|---|---|
@@ -1,33 +1,44 @@ | ||
const exEuclidean = require('./extended_euclidean'); | ||
const fastExp = require('./fast_exp'); | ||
|
||
|
||
/** | ||
* Calculates modular inverse of a number | ||
* Calculates modular inverse of a number using Fermet's theorem | ||
* | ||
* @param {Number} a Number for which inverse needs to be found | ||
* @param {Number} m Mod val | ||
* @return {Number} Modular Inverse | ||
*/ | ||
const modularInverse = (a, m) => { | ||
const result = exEuclidean(a, m); | ||
if (result.gcd !== 1) { | ||
const fermetModularInverse = (a, m) => { | ||
if (a === 0 || m === 0) { | ||
return null; | ||
} | ||
|
||
console.log(result); | ||
|
||
return (result.x + m) % m; | ||
return fastExp(a, m - 2, m); | ||
}; | ||
|
||
module.exports = modularInverse; | ||
|
||
/** | ||
* Calculates modular inverse of a number using Fermet's theorem | ||
* Calculates modular inverse of a number | ||
* | ||
* @param {Number} a Number for which inverse needs to be found | ||
* @param {Number} m Mod val | ||
* @return {Number} Modular Inverse | ||
*/ | ||
const fermetModularInverse = (a, m) => fastExp(a, m - 2, m); | ||
const modularInverse = (a, m) => { | ||
if (a === 0 || m === 0) { | ||
return null; | ||
} | ||
|
||
const result = exEuclidean(a, m); | ||
|
||
if (result.gcd !== 1) { | ||
return null; | ||
} | ||
|
||
return (result.x + m) % m; | ||
}; | ||
|
||
module.exports = fermetModularInverse; | ||
module.exports = { | ||
fermetModularInverse, | ||
modularInverse | ||
}; |