> For the complete documentation index, see [llms.txt](https://chihaolu.gitbook.io/all-in-one-solidity/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://chihaolu.gitbook.io/all-in-one-solidity/part-i-basic/chapter-5-han-shi-function/useful-function.md).

# 【Useful Function】

#### Mathematical Functions

`addmod(uint x, uint y, uint k)` returns (`uint`)：以高精度（arbitrary precision）計算 `(x + y) % k`

`mulmod(uint x, uint y, uint k)` returns (`uint`)：以高精度（arbitrary precision）計算 `(x * y) % k`

```solidity
pragma solidity ^0.8.11;

contract Test {   
   function callAddMod() public pure returns(uint){
      return addmod(4, 5, 3); // 0
   }
   function callMulMod() public pure returns(uint){
      return mulmod(4, 5, 3); // 2
   }
}
```

> Assert that `k != 0` starting from version 0.5.0.

#### Cryptographic Functions

* `blockhash(uint blockNumber)` returns (`bytes32)`- 給定一個區塊，返回它的 hash 值, 只有最近工作的256個塊的 hash 值
* `keccak256(bytes memory)` returns (`bytes32)` - 使用 Keccak-256 加密演算法計算之後的結果
* `sha256(bytes memory)` returns (`bytes32)` − 使用 SHA-256 加密演算法計算之後的結果。等同於 Ethereum-SHA3 hash 演算法
* `ripemd160(bytes memory)` returns (`bytes20)` − 使用 RIPEMD-160 加密演算法計算之後的結果
