PRBMath – PRBMath公司区块链毕设代写

区块链毕设代写本文提供国外最新区块链项目源码下载,包括solidity,eth,fabric等blockchain区块链,PRBMath – PRBMath公司区块链毕设代写 是一篇很好的国外资料

PRBMath PRBMath - PRBMath公司 PRBMath - PRBMath公司 PRBMath - PRBMath公司 PRBMath - PRBMath公司

Smart contract library for advanced fixed-point math, which operates with signed 59.18-decimal fixed-point and unsigned 60.18-decimal fixed-point numbers. The name stems from the fact that there can be up to 59/60 digits in the integer part and up to 18 decimals in the fractional part. The numbers are bound by the minimum and the maximum values permitted by the Solidity types int256 and uint256.

  • Designed for Solidity >=0.8.0
  • Offers advanced math functions like logarithms, exponentials, powers and square roots
  • Operates with signed and unsigned denary fixed-point numbers, with 18 trailing decimals
  • Bakes in overflow-safe multiplication and division
  • Gas efficient, but still user-friendly
  • Well-documented via NatSpec comments
  • Thoroughly tested with Hardhat and Waffle

I created this because I wanted a fixed-point math library that is at the same time practical, intuitive and efficient. I looked at ABDKMath64x64, which is fast, but I didn’t like that it operates with binary numbers and it limits the precision to int128. I then looked at Fixidity, which operates with denary numbers and has wide precision, but is slow and susceptible to phantom overflow.

Caveat Emptor

This is experimental software and is provided on an “as is” and “as available” basis. I do not give any warranties and will not be liable for any loss, direct or indirect through continued use of this codebase.

Installation

With yarn:

$ yarn add prb-math

Or npm:

npm install prb-math

I adhere to semver, so your contracts won’t break unexpectedly when upgrading to a newer minor version of prb-math.

Usage

Once installed, you can use the libraries like this:

PRBMathSD59x18.sol

pragma solidity >=0.8.0;  import "prb-math/contracts/PRBMathSD59x18.sol";  contract SignedConsumer {   using PRBMathSD59x18 for int256;    function signedLog2(int256 x) external pure returns (int256 result) {     result = x.log2();   }    function signedExp(int256 x) external pure returns (int256 result) {     result = PRBMathSD59x18.exp(x);   }    /// @notice Calculates x*y÷1e18 while handling possible intermediary overflow.   /// @dev Try this with x = type(int256).max and y = 5e17.   function signedMul(int256 x, int256 y) external pure returns (int256 result) {     result = PRBMathSD59x18.mulDiv(x, y);   }    /// @dev Note that "y" is a basic uint256 integer, not a fixed-point number.   function signedPow(int256 x, uint256 y) external pure returns (int256 result) {     result = x.pow(y);   }    /// @dev Assuming that 1e18 = 100% and 1e16 = 1%.   function signedYield(int256 principal, int256 apr) external pure returns (int256 result) {     result = principal.mul(apr);   } } 

PRBMathUD60x18.sol

pragma solidity >=0.8.0;  import "prb-math/contracts/PRBMathUD60x18.sol";  contract UnsignedConsumer {   using PRBMathUD60x18 for uint256;    /// @dev Note that "x" must be greater than or equal to 1e18, lest the result would be negative, and negative   /// numbers are not supported by the unsigned 60.18-decimal fixed-point representation.   function unsignedLog2(uint256 x) external pure returns (uint256 result) {     result = x.log2();   }    function unsignedExp(uint256 x) external pure returns (uint256 result) {     result = PRBMathUD60x18.exp(x);   }    /// @notice Calculates x*y÷1e18 while handling possible intermediary overflow.   /// @dev Try this with x = type(uint256).max and y = 5e17.   function unsignedMul(uint256 x, uint256 y) external pure returns (uint256 result) {     result = PRBMathUD60x18.mul(x, y);   }    /// @dev Note that "y" is a basic uint256 integer, not a fixed-point number.   function unsignedPow(uint256 x, uint256 y) external pure returns (uint256 result) {     result = x.pow(y);   }    /// @dev Assuming that 1e18 = 100% and 1e16 = 1%.   function unsignedYield(uint256 principal, uint256 apr) external pure returns (uint256 result) {     result = principal.mul(apr);   } } 

Gas Efficiency

PRBMath is faster than ABDKMath for abs, exp, exp2, gm, inv, ln, log2. Conversely, PRBMath is slower than ABDKMath for avg, div, mul, pow and sqrt. There are two technical reasons why PRBMath lags behind ABDKMath’s mul and div functions:

  1. PRBMath operates with 256-bit word sizes, so it has to account for possible intermediary overflow. ABDKMath operates with 128-bit word sizes.
  2. PRBMath rounds up instead of truncating in certain cases (see Listing 6 and text above it), which does it slightly more precise than ABDKMath but comes at a gas cost.

PRBMath

Based on v1.0.0 of the library.

SD59x18 Min Max Avg UD60x18 Min Max Avg
abs 68 72 70 n/a n/a n/a n/a
avg 57 57 57 avg 57 57 57
ceil 82 117 101 ceil 78 78 78
div 431 483 451 div 205 205 205
exp 35 3272 2507 exp 2065 3220 2529
exp2 60 3156 2247 exp2 1975 3130 2417
floor 82 117 101 floor 43 43 43
frac 23 23 23 frac 23 23 23
gm 26 892 690 gm 26 893 691
inv 40 40 40 inv 40 40 40
ln 463 7306 4724 ln 419 6902 3814
log10 104 9074 4337 log10 503 8695 4571
log2 377 7241 4243 log2 330 6825 3426
mul 455 463 459 mul 219 275 247
pow 293 24745 5681 pow 83 24535 5471
sqrt 140 839 716 sqrt 114 846 710

ABDKMath64x64

Based on v3.0 of the library. See abdk-gas-estimations.

Method Min Max Avg
abs 88 92 90
avg 41 41 41
div 168 168 168
exp 77 3780 2687
exp2 77 3600 2746
gavg 166 875 719
inv 157 157 157
ln 7074 7164 7126
log2 6972 7062 7024
mul 111 111 111
pow 303 4740 1792
sqrt 129 809 699

Contributing

Familiarity with Hardhat, Ethers and Waffle and TypeScript is requisite.

Pre Requisites

Before running any command, make sure to install dependencies:

$ yarn install

Compile

Compile the smart contracts with Hardhat:

$ yarn compile

TypeChain

Compile the smart contracts and generate TypeChain artifacts:

$ yarn typechain

Lint Solidity

Lint the Solidity code:

$ yarn lint:sol

Lint TypeScript

Lint the TypeScript code:

$ yarn lint:ts

Test

Run the Mocha tests:

$ yarn test

Coverage

Generate the code coverage report:

$ yarn coverage

Clean

Delete the smart contract artifacts, the coverage reports and the Hardhat cache:

$ yarn clean

Security

While I set a high bar for code quality and test coverage, you shouldn’t assume that this library is completely safe to use. The contracts have not been audited by a security researcher. If you discover any security issues, please report them via Keybase.

Acknowledgements

I am grateful to:

  • Mikhail Vladimirov for the insights he shared in his Math in Solidity series.
  • Remco Bloemen for his work on overflow-safe multiplication and division and for responding to the questions I asked him while developing the library.

License

The library is released under the WTFPL License.

docs: annotate gas comparison with version of libraries


PRBMathPRBMath - PRBMath公司<PRBMath>>PRBMath - PRBMath公司

高级定点数学的智能合约库,使用有符号的59.18十进制定点和无符号的60.18十进制定点进行运算。这个名字源于这样一个事实:整数部分最多可以有59/60位,小数部分最多可以有18位小数。这些数字由坚固性类型int256和uint256允许的最小值和最大值绑定。我创建这个是因为我想要一个既实用、直观又高效的定点数学库。我看了ABDKMath64x64,它很快,但我不喜欢它用二进制数操作,它将精度限制为int128。然后,我研究了固定性,它与二进制数一起工作,精度很高,但速度慢,易受幻象溢出的影响。

  • 许可证
  • 设计用于Solidity&gt;=0.8.0
  • 提供高级数学函数,如对数,指数、幂和平方根
  • 与有符号和无符号的二元定点数一起工作,18个尾随小数
  • 在溢出安全的乘法和除法中烘焙
  • 非常高效,但仍然是用户友好的
  • 通过NatSpec注释进行了详细的记录

这是一个实验性软件,按“原样”和“可用”提供。我不提供任何保证,也不承担任何损失,直接或间接通过继续使用此代码库。

注意清空器安装

与纱线:

使用

或npm:

$ yarn add prb-math

我坚持semver,因此在升级到较新的prb math次要版本时,您的合同不会意外中断。

npm install prb-math

安装后,可以使用如下库:

PRBMathSD59x18.sol

对于abs、exp、exp2、gm、inv、ln、log2,PRBMath比ABDKMath快。相反,对于avg、div、mul、pow和sqrt,PRBMath比ABDKMath慢。PRBMath落后于ABDKMath的mul和div函数有两个技术原因:

PRBMathUD60x18.sol

pragma solidity >=0.8.0;  import "prb-math/contracts/PRBMathSD59x18.sol";  contract SignedConsumer {   using PRBMathSD59x18 for int256;    function signedLog2(int256 x) external pure returns (int256 result) {     result = x.log2();   }    function signedExp(int256 x) external pure returns (int256 result) {     result = PRBMathSD59x18.exp(x);   }    /// @notice Calculates x*y÷1e18 while handling possible intermediary overflow.   /// @dev Try this with x = type(int256).max and y = 5e17.   function signedMul(int256 x, int256 y) external pure returns (int256 result) {     result = PRBMathSD59x18.mulDiv(x, y);   }    /// @dev Note that "y" is a basic uint256 integer, not a fixed-point number.   function signedPow(int256 x, uint256 y) external pure returns (int256 result) {     result = x.pow(y);   }    /// @dev Assuming that 1e18 = 100% and 1e16 = 1%.   function signedYield(int256 principal, int256 apr) external pure returns (int256 result) {     result = principal.mul(apr);   } } 

PRBMath

pragma solidity >=0.8.0;  import "prb-math/contracts/PRBMathUD60x18.sol";  contract UnsignedConsumer {   using PRBMathUD60x18 for uint256;    /// @dev Note that "x" must be greater than or equal to 1e18, lest the result would be negative, and negative   /// numbers are not supported by the unsigned 60.18-decimal fixed-point representation.   function unsignedLog2(uint256 x) external pure returns (uint256 result) {     result = x.log2();   }    function unsignedExp(uint256 x) external pure returns (uint256 result) {     result = PRBMathUD60x18.exp(x);   }    /// @notice Calculates x*y÷1e18 while handling possible intermediary overflow.   /// @dev Try this with x = type(uint256).max and y = 5e17.   function unsignedMul(uint256 x, uint256 y) external pure returns (uint256 result) {     result = PRBMathUD60x18.mul(x, y);   }    /// @dev Note that "y" is a basic uint256 integer, not a fixed-point number.   function unsignedPow(uint256 x, uint256 y) external pure returns (uint256 result) {     result = x.pow(y);   }    /// @dev Assuming that 1e18 = 100% and 1e16 = 1%.   function unsignedYield(uint256 principal, uint256 apr) external pure returns (uint256 result) {     result = principal.mul(apr);   } } 

ABDKMath64x64

基于库的v1.0.0。

  1. 使用Hardhat进行了彻底的测试Waffle
  2. PRBMath以256位字长运行,因此它必须考虑可能的中间溢出。ABDKMath使用128位字大小进行操作。

先决条件

编译

类型链

Lint Solidity

Lint TypeScript

测试

覆盖

清除

安全

确认

  • 许可证
  • 设计用于Solidity&gt;=0.8.0
  • 提供高级数学函数,如对数,指数、幂和平方根
  • 与有符号和无符号的二元定点数一起工作,18个尾随小数
  • 在溢出安全的乘法和除法中烘焙
  • 非常高效,但仍然是用户友好的
  • 通过NatSpec注释进行了详细的记录
  • 使用Hardhat进行了彻底的测试Waffle
  • PRBMath以256位字长运行,因此它必须考虑可能的中间溢出。ABDKMath使用128位字大小进行操作。
  • 在某些情况下,PRBMath会舍入而不是截断(请参见清单6和上面的文本),这比ABDKMath稍微精确一些,但要付出代价。
  • 米哈伊尔·弗拉基米罗夫(Mikhail Vladimirov)在《坚实的数学》(Math in Solidity)系列中分享了他的见解。
  • 感谢Remco Blomen在溢出安全乘法和除法方面的工作,感谢他在开发库时回答了我的问题。<68 中欧 exp2 60 3156 2247 exp2

    <1975

    <3130

    <2417 地板

    <82 <117

    <101 <43 <43 frac 23 23 gm 26 892 690 26

    <893 691 inv 40 ln 463 7306 <4724 ln

    <419 6902

    <3814 log10

    <104

    <9074

    <4337

    <503

    <8695

    <4571 log2

    377 7241

    >

    4243辆

    24535 5471 sqrt

    <140

    <839 <716

    <114

    <846

    <710

    <88

    <92

    <90

    <41

    <41

    <41

    168年对数2

    基于库的v3.0。见abdk天然气估算。

    SD59x18 Min Max Avg UD60x18 Min Max Avg
    中欧 exp2 60 3156 2247 n/a n/a exp2 地板
    <117 57 <43 <43 avg frac 23 23
    gm 26 892 690 26 691 inv 40
    ln 463 7306 <4724 div ln 6902 log10
    exp log2 7241 4243辆 24535 5471 sqrt <716
    60 3156 2247 exp2 1975 3130 2417
    floor 82 117 101 floor 43 43 43
    frac 23 23 23 frac 23 23 23
    gm 26 892 690 gm 26 893 691
    inv 40 40 40 inv 40 40 40
    ln 463 7306 4724 ln 419 6902 3814
    log10 104 9074 4337 log10 503 8695 4571
    log2 377 7241 4243 log2 330 6825 3426
    mul 455 463 459 mul 219 275 247
    pow 293 24745 5681 pow 83 24535 5471
    sqrt 140 839 716 sqrt 114 846 710

    先决条件

    编译

    类型链

    Lint Solidity

    Lint TypeScript

    测试

    覆盖

    清除

    安全

    确认

  • 许可证

    必须熟悉安全帽、以太网、华夫饼和打字。

    Method Min Max Avg
    abs 88 92 90
    avg 41 41 41
    div 168 168 168
    exp 77 3780 2687
    exp2 77 3600 2746
    gavg 166 875 719
    inv 157 157 157
    ln 7074 7164 7126
    log2 6972 7062 7024
    mul 111 111 111
    pow 303 4740 1792
    sqrt 129 809 699

    先决条件

    在运行任何命令之前,请确保安装依赖项:

    编译

    使用Hardhat编译智能契约:

    $ yarn install

    类型链

    编译智能契约并生成TypeChain工件:

    $ yarn compile

    Lint Solidity

    Lint实体代码:

    $ yarn typechain

    Lint TypeScript

    Lint TypeScript代码:

    $ yarn lint:sol

    测试

    运行Mocha测试:

    $ yarn lint:ts

    覆盖

    生成代码覆盖率报告:

    $ yarn test

    清除

    删除智能合约工件、覆盖率报告和Hardhat缓存:

    $ yarn coverage

    安全

    虽然我为代码质量和测试覆盖率设置了一个较高的标杆,但您不应该认为这个库是完全安全的。这些合同没有经过安全研究员的审核。如果您发现任何安全问题,请通过Keybase报告。

    $ yarn clean

    确认

    我非常感谢:

    Acknowledgements

    该库是根据WTFPL许可证发布的。

    • 在某些情况下,PRBMath会舍入而不是截断(请参见清单6和上面的文本),这比ABDKMath稍微精确一些,但要付出代价。
    • 米哈伊尔·弗拉基米罗夫(Mikhail Vladimirov)在《坚实的数学》(Math in Solidity)系列中分享了他的见解。

    License

    The library is released under the WTFPL License.

  • docs: annotate gas comparison with version of libraries

    部分转自网络,侵权联系删除区块链源码网

    www.interchains.cc

    https://www.interchains.cc/21728.html

    区块链毕设网(www.interchains.cc)全网最靠谱的原创区块链毕设代做网站 部分资料来自网络,侵权联系删除! 最全最大的区块链源码站 ! QQ3039046426
    区块链知识分享网, 以太坊dapp资源网, 区块链教程, fabric教程下载, 区块链书籍下载, 区块链资料下载, 区块链视频教程下载, 区块链基础教程, 区块链入门教程, 区块链资源 » PRBMath – PRBMath公司区块链毕设代写

    提供最优质的资源集合

    立即查看 了解详情