Skip to content

Instantly share code, notes, and snippets.

@lhartikk
Created February 10, 2021 15:26
Show Gist options
  • Save lhartikk/09e3a1ef1d8701e258fc91c19bc9e02f to your computer and use it in GitHub Desktop.
Save lhartikk/09e3a1ef1d8701e258fc91c19bc9e02f to your computer and use it in GitHub Desktop.
contract Prime {
function isPrime(uint256 n) public view returns (bool){
if (n < 2) {
return false;
}
if (n == 2) {
return true;
}
return expmod(2, n - 1, n) == 1;
}
// use precompile expmod to calculate modular exponentiation
function expmod(uint256 base, uint256 e, uint256 m) internal view returns (uint o) {
assembly {
// define pointer
let p := mload(0x40)
// store data assembly-favouring ways
mstore(p, 0x20) // Length of Base
mstore(add(p, 0x20), 0x20) // Length of Exponent
mstore(add(p, 0x40), 0x20) // Length of Modulus
mstore(add(p, 0x60), base) // Base
mstore(add(p, 0x80), e) // Exponent
mstore(add(p, 0xa0), m) // Modulus
if iszero(staticcall(sub(gas, 2000), 0x05, p, 0xc0, p, 0x20)) {
revert(0, 0)
}
// data
o := mload(p)
}}
}
@git-josip
Copy link

@eucliss
Call 0x05 precompile (modular exponentation) w/ the following Args.
This is precompiled contract for ModeExp (https://www.rareskills.io/post/solidity-precompiles)

         *      args and revert on failure.
         
         *      Args:
         *      gas,
         *      precomipled contract address,
         *      memory pointer of begin of calldata,
         *      size of call data (callDataSize),
         *      pointer for where to copy return,
         *      size of return data
         */

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment