Created
February 10, 2021 15:26
-
-
Save lhartikk/09e3a1ef1d8701e258fc91c19bc9e02f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
}} | |
} |
@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
Hey man, can you explain the
if iszero(staticcall(sub(gas, 2000), 0x05, p, 0xc0, p, 0x20)) { revert(0, 0) }
I dont quite understand why staticcall is using 0x05 as the address