github.com/klaytn/klaytn@v1.12.1/contracts/precompiledContracts/precompiled.sol (about)

     1  // Copyright 2019 The klaytn Authors
     2  // This file is part of the klaytn library.
     3  //
     4  // The klaytn library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The klaytn library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  pragma solidity ^0.4.24;
    18  
    19  contract PrecompiledEcrecover {
    20      function callEcrecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) public pure returns (address) {
    21          return ecrecover(hash, v, r, s);
    22      }
    23  }
    24  
    25  contract PrecompiledSha256Hash {
    26      function callSha256(bytes memory data) public pure returns (bytes32) {
    27          return sha256(data);
    28      }
    29  }
    30  
    31  contract PrecompiledRipemd160Hash {
    32      function callRipemd160(bytes memory data) public pure returns (bytes32) {
    33          return ripemd160(data);
    34      }
    35  }
    36  
    37  contract PrecompiledDatacopy {
    38      function callDatacopy(bytes memory data) public returns (bytes memory) {
    39          bytes memory ret = new bytes(data.length);
    40          assembly {
    41              let len := mload(data)
    42              if iszero(call(gas, 0x04, 0, add(data, 0x20), len, add(ret,0x20), len)) {
    43                  invalid()
    44              }
    45          }
    46  
    47          return ret;
    48      }
    49  }
    50  
    51  contract PrecompiledBigModExp {
    52      function callBigModExp(bytes32 base, bytes32 exponent, bytes32 modulus) public returns (bytes32 result) {
    53          assembly {
    54              // free memory pointer
    55              let memPtr := mload(0x40)
    56  
    57              // length of base, exponent, modulus
    58              mstore(memPtr, 0x20)
    59              mstore(add(memPtr, 0x20), 0x20)
    60              mstore(add(memPtr, 0x40), 0x20)
    61  
    62              // assign base, exponent, modulus
    63              mstore(add(memPtr, 0x60), base)
    64              mstore(add(memPtr, 0x80), exponent)
    65              mstore(add(memPtr, 0xa0), modulus)
    66  
    67              // call the precompiled contract BigModExp (0x05)
    68              let success := call(gas, 0x05, 0x0, memPtr, 0xc0, memPtr, 0x20)
    69              switch success
    70              case 0 {
    71                  revert(0x0, 0x0)
    72              } default {
    73                  result := mload(memPtr)
    74              }
    75          }
    76      }
    77  }
    78  
    79  contract PrecompiledBn256Add {
    80      function callBn256Add(bytes32 ax, bytes32 ay, bytes32 bx, bytes32 by) public returns (bytes32[2] memory result) {
    81          bytes32[4] memory input;
    82          input[0] = ax;
    83          input[1] = ay;
    84          input[2] = bx;
    85          input[3] = by;
    86          assembly {
    87              let success := call(gas, 0x06, 0, input, 0x80, result, 0x40)
    88              switch success
    89              case 0 {
    90                  revert(0,0)
    91              }
    92          }
    93      }
    94  }
    95  
    96  contract PrecompiledBn256ScalarMul {
    97      function callBn256ScalarMul(bytes32 x, bytes32 y, bytes32 scalar) public returns (bytes32[2] memory result) {
    98          bytes32[3] memory input;
    99          input[0] = x;
   100          input[1] = y;
   101          input[2] = scalar;
   102          assembly {
   103              let success := call(gas, 0x07, 0, input, 0x60, result, 0x40)
   104              switch success
   105              case 0 {
   106                  revert(0,0)
   107              }
   108          }
   109      }
   110  }
   111  
   112  contract PrecompiledBn256Pairing {
   113      function callBn256Pairing(bytes memory input) public returns (bytes32 result) {
   114          // input is a serialized bytes stream of (a1, b1, a2, b2, ..., ak, bk) from (G_1 x G_2)^k
   115          uint256 len = input.length;
   116          require(len % 192 == 0);
   117          assembly {
   118              let memPtr := mload(0x40)
   119              let success := call(gas, 0x08, 0, add(input, 0x20), len, memPtr, 0x20)
   120              switch success
   121              case 0 {
   122                  revert(0,0)
   123              } default {
   124                  result := mload(memPtr)
   125              }
   126          }
   127      }
   128  }