File tree Expand file tree Collapse file tree 3 files changed +28
-1
lines changed
Expand file tree Collapse file tree 3 files changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -63,7 +63,7 @@ library DSMath {
6363 * @notice The function that computes square roots using the Babylonian method
6464 * @dev The original implementation can be found here github.com/abdk-consulting/abdk-libraries-solidity
6565 */
66- function sqrt (uint256 x ) private pure returns (uint128 ) {
66+ function sqrt (uint256 x ) internal pure returns (uint128 ) {
6767 unchecked {
6868 if (x == 0 ) {
6969 return 0 ;
Original file line number Diff line number Diff line change @@ -7,4 +7,8 @@ contract DSMathMock {
77 function rpow (uint256 x_ , uint256 n_ , uint256 b_ ) external pure returns (uint256 ) {
88 return DSMath.rpow (x_, n_, b_);
99 }
10+
11+ function sqrt (uint256 x_ ) external pure returns (uint256 ) {
12+ return DSMath.sqrt (x_);
13+ }
1014}
Original file line number Diff line number Diff line change @@ -36,4 +36,27 @@ describe("DSMath", () => {
3636 }
3737 } ) ;
3838 } ) ;
39+
40+ describe ( "sqrt" , ( ) => {
41+ it ( "should compute square roots properly" , async ( ) => {
42+ const cases = [
43+ [ "0" , "0" ] ,
44+ [ "1" , "1" ] ,
45+ [ "2" , "1" ] ,
46+ [ "3" , "1" ] ,
47+ [ "4" , "2" ] ,
48+ [ "15" , "3" ] ,
49+ [ "16" , "4" ] ,
50+ [ "17" , "4" ] ,
51+ [ "105" , "10" ] ,
52+ [ "1787926567434221169891766654055630785600812306453167569" , "1337133713371337133713371337" ] ,
53+ [ toBN ( 2 ) . pow ( 254 ) , toBN ( 2 ) . pow ( 127 ) ] ,
54+ [ toBN ( 2 ) . pow ( 256 ) . minus ( 1 ) , toBN ( 2 ) . pow ( 128 ) . minus ( 1 ) ] ,
55+ ] ;
56+
57+ for ( const [ square , root ] of cases ) {
58+ assertBNequal ( await dsMath . sqrt ( square ) , root ) ;
59+ }
60+ } ) ;
61+ } ) ;
3962} ) ;
You can’t perform that action at this time.
0 commit comments