@gkucmierz/utils 1.5.1 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package-lock.json +2 -2
- package/package.json +1 -1
- package/src/pow-mod.mjs +16 -0
package/package-lock.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gkucmierz/utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0",
|
|
4
4
|
"lockfileVersion": 2,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "@gkucmierz/utils",
|
|
9
|
-
"version": "1.
|
|
9
|
+
"version": "1.6.0",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"devDependencies": {
|
|
12
12
|
"husky": "^8.0.1",
|
package/package.json
CHANGED
package/src/pow-mod.mjs
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
|
|
2
|
+
// implementation of power function with modulus like native python pow
|
|
3
|
+
|
|
2
4
|
export const powMod = (base, exponent, modulus) => {
|
|
3
5
|
if (modulus === 1) return 0;
|
|
4
6
|
let result = 1;
|
|
@@ -12,3 +14,17 @@ export const powMod = (base, exponent, modulus) => {
|
|
|
12
14
|
}
|
|
13
15
|
return result;
|
|
14
16
|
};
|
|
17
|
+
|
|
18
|
+
export const powModBI = (base, exponent, modulus) => {
|
|
19
|
+
if (modulus === 1n) return 0n;
|
|
20
|
+
let result = 1n;
|
|
21
|
+
base = base % modulus;
|
|
22
|
+
while (exponent > 0n) {
|
|
23
|
+
if (exponent % 2n === 1n) {
|
|
24
|
+
result = (result * base) % modulus;
|
|
25
|
+
}
|
|
26
|
+
exponent = exponent / 2n;
|
|
27
|
+
base = (base * base) % modulus;
|
|
28
|
+
}
|
|
29
|
+
return result;
|
|
30
|
+
};
|