Created
August 1, 2024 04:37
-
-
Save ardislu/a716f4a4c4e4fcc9b2f9e0b0a861054f to your computer and use it in GitHub Desktop.
Minimal example of calling the `modexp` precompile (0x05) using Solidity.
This file contains hidden or 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
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.26; | |
contract ModExp { | |
function modExp(bytes calldata base, bytes calldata exponent, bytes calldata modulus) public returns (bytes memory) { | |
(, bytes memory result) = address(5).call(abi.encodePacked(base.length, exponent.length, modulus.length, base, exponent, modulus)); | |
return result; | |
} | |
function example() external returns (bytes memory) { | |
// TypeError: Built-in binary operator ** cannot be applied to types int_const 3 and int_const 1000000. | |
// return (3 ** 1000000) % 8191; | |
// Returns bytes "0x09af" (== 2479), as expected | |
return this.modExp(hex"03", hex"0f4240", hex"1fff"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment