summaryrefslogtreecommitdiff
path: root/core/crypto/bigint.js
diff options
context:
space:
mode:
Diffstat (limited to 'core/crypto/bigint.js')
-rw-r--r--core/crypto/bigint.js34
1 files changed, 34 insertions, 0 deletions
diff --git a/core/crypto/bigint.js b/core/crypto/bigint.js
new file mode 100644
index 0000000..d344326
--- /dev/null
+++ b/core/crypto/bigint.js
@@ -0,0 +1,34 @@
+export function modPow(b, e, m) {
+ let r = 1n;
+ b = b % m;
+ while (e > 0n) {
+ if ((e & 1n) === 1n) {
+ r = (r * b) % m;
+ }
+ e = e >> 1n;
+ b = (b * b) % m;
+ }
+ return r;
+}
+
+export function bigIntToU8Array(bigint, padLength=0) {
+ let hex = bigint.toString(16);
+ if (padLength === 0) {
+ padLength = Math.ceil(hex.length / 2);
+ }
+ hex = hex.padStart(padLength * 2, '0');
+ const length = hex.length / 2;
+ const arr = new Uint8Array(length);
+ for (let i = 0; i < length; i++) {
+ arr[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
+ }
+ return arr;
+}
+
+export function u8ArrayToBigInt(arr) {
+ let hex = '0x';
+ for (let i = 0; i < arr.length; i++) {
+ hex += arr[i].toString(16).padStart(2, '0');
+ }
+ return BigInt(hex);
+}