Calculate Modular Exponentiation (PowerMod) in Javascript (a^p % n)

Computing PowerMod is required for implementing public-key cryptography as it is used to encrypt and decrypt data. Calculating it by regular arithmetic (i.e. calculating pth power of a) doesn’t work with large numbers used for encryption. That’s where PowerMod helps, it calculates the result by keeping the numbers significantly small and within range of the integer data type.

There are different algorithms for calculating PowerMod. Following is based on the pseudocode from Wikipedia, it is called ‘Right-to-left binary method’.

// calculates   base^exponent % modulus
function powerMod(base, exponent, modulus) {
    if (modulus === 1) return 0;
    var result = 1;
    base = base % modulus;
    while (exponent > 0) {
        if (exponent % 2 === 1)  //odd number
            result = (result * base) % modulus;
        exponent = exponent >> 1; //divide by 2
        base = (base * base) % modulus;
    }
    return result;
}

Learn more at: https://en.wikipedia.org/wiki/Modular_exponentiation

Installing Visual Studio Code (VSCode) on Manjaro linux

It will be a two-step process. First, we will install Yaourt, then use it to install VS Code.

Installing Yaourt

Edit Pacman configuration file to include the repository for Yaourt

$ sudo nano /etc/pacman.conf

Add the following lines at the end of the file and save it

[archlinuxfr]
SigLevel = Never
Server = http://repo.archlinux.fr/$arch

Now refresh the repository database and install Yaourt

$ sudo pacman -Sy yaourt

Installing VS Code

Yaourt makes installing VS Code pretty easy, we just have to execute the following command.

$ sudo yaourt visual-studio-code-bin

Links

https://code.visualstudio.com/docs/setup/linux
https://aur.archlinux.org/packages/visual-studio-code-bin