| 1 | export function modPow(b, e, m) { |
| 2 | let r = 1n; |
| 3 | b = b % m; |
| 4 | while (e > 0n) { |
| 5 | if ((e & 1n) === 1n) { |
| 6 | r = (r * b) % m; |
| 7 | } |
| 8 | e = e >> 1n; |
| 9 | b = (b * b) % m; |
| 10 | } |
| 11 | return r; |
| 12 | } |
| 13 | |
| 14 | export function bigIntToU8Array(bigint, padLength=0) { |
| 15 | let hex = bigint.toString(16); |
| 16 | if (padLength === 0) { |
| 17 | padLength = Math.ceil(hex.length / 2); |
| 18 | } |
| 19 | hex = hex.padStart(padLength * 2, '0'); |
| 20 | const length = hex.length / 2; |
| 21 | const arr = new Uint8Array(length); |
| 22 | for (let i = 0; i < length; i++) { |
| 23 | arr[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16); |
| 24 | } |
| 25 | return arr; |
| 26 | } |
| 27 | |
| 28 | export function u8ArrayToBigInt(arr) { |
| 29 | let hex = '0x'; |
| 30 | for (let i = 0; i < arr.length; i++) { |
| 31 | hex += arr[i].toString(16).padStart(2, '0'); |
| 32 | } |
| 33 | return BigInt(hex); |
| 34 | } |