solidity-math-utils 1.12.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -3,10 +3,10 @@
3
3
  This package consists of the following modules:
4
4
  - [IntegralMath](#integralmath) - a set of functions, each of which returning an integer result
5
5
  - [FractionMath](#fractionmath) - a set of functions, each of which returning a rational result
6
- - [AnalyticMath](#analyticmath) - a set of exponential and logarithmic functions
7
- - [AdvancedMath](#advancedmath) - a function for solving xA^x = B, given rational values of A and B
6
+ - [AnalyticMath](#analyticmath) - a set of functions for exponential and logarithmic operations
7
+ - [AdvancedMath](#advancedmath) - a set of functions for solving equations of the form xA^x = B
8
8
  - [BondingCurve](#bondingcurve) - a set of functions implementing the bonding-curve mechanism
9
- - [DynamicCurve](#dynamiccurve) - a function for equalizing the weights in a bonding-curve model
9
+ - [DynamicCurve](#dynamiccurve) - a set of functions for equalizing the weights in a bonding-curve model
10
10
 
11
11
  ### Class Hierarchy
12
12
  ```
@@ -23,12 +23,6 @@ AnalyticMath < - - - - AdvancedMath
23
23
  BondingCurve DynamicCurve
24
24
  ```
25
25
 
26
- Note that some of these modules are implemented as `library`, while others are implemented as `contract`.
27
-
28
- Nevertheless, they can all be regarded as *libraries*, since the reason for this is purely technical.
29
-
30
- A library cannot utilize non-constant state variables, nor can it extend (inherit) another library.
31
-
32
26
  <br/><br/>
33
27
 
34
28
  ---
@@ -46,6 +40,7 @@ This module implements the following interface:
46
40
  - `function roundDiv(uint256 n, uint256 d)` => `(uint256)`
47
41
  - `function mulDivF(uint256 x, uint256 y, uint256 z)` => `(uint256)`
48
42
  - `function mulDivC(uint256 x, uint256 y, uint256 z)` => `(uint256)`
43
+ - `function mulDivR(uint256 x, uint256 y, uint256 z)` => `(uint256)`
49
44
  - `function mulDivExF(uint256 x, uint256 y, uint256 z, uint256 w)` => `(uint256)`
50
45
  - `function mulDivExC(uint256 x, uint256 y, uint256 z, uint256 w)` => `(uint256)`
51
46
 
@@ -59,7 +54,7 @@ Function `floorCbrt(n)` computes the largest integer smaller than or equal to th
59
54
 
60
55
  Function `ceilCbrt(n)` computes the smallest integer larger than or equal to the cubic root of `n`.
61
56
 
62
- Function `roundDiv(n, d)` computes the nearest integer to the quotient of `n` and `d` (or `n / d`).
57
+ Function `roundDiv(n, d)` computes the nearest integer (half being rounded upwards) to `n / d`.
63
58
 
64
59
  Function `minFactor(x, y)` computes the smallest integer `z` such that `x * y / z <= 2 ^ 256 - 1`.
65
60
 
@@ -67,13 +62,13 @@ Function `mulDivF(x, y, z)` computes the largest integer smaller than or equal t
67
62
 
68
63
  Function `mulDivC(x, y, z)` computes the smallest integer larger than or equal to `x * y / z`.
69
64
 
65
+ Function `mulDivR(x, y, z)` computes the nearest integer (half being rounded upwards) to `x * y / z`.
66
+
70
67
  Function `mulDivExF(x, y, z, w)` computes the largest integer smaller than or equal to `(x * y) / (z * w)`.
71
68
 
72
69
  Function `mulDivExC(x, y, z, w)` computes the smallest integer larger than or equal to `(x * y) / (z * w)`.
73
70
 
74
- Note that function `mulDivF` and function `mulDivC` revert when the **actual** result is larger than 256 bits.
75
-
76
- Note that function `mulDivExF` and function `mulDivExC` revert when the **actual** result is larger than 256 bits.
71
+ Note that each one of the 'mulDiv' functions reverts when the **actual** result is larger than 256 bits.
77
72
 
78
73
  Note that function `floorSqrt` and function `ceilSqrt` are guaranteed to return the correct output for every input.
79
74
 
@@ -96,22 +91,23 @@ For example, `floorCbrt(7)` returns 1, but the actual cubic root of 7 is ~1.91,
96
91
  ## FractionMath
97
92
 
98
93
  This module implements the following interface:
99
- - `function poweredRatio(uint256 n, uint256 d, uint256 exp, bool fast)` => `(uint256, uint256)`
94
+ - `function poweredRatioExact(uint256 n, uint256 d, uint256 exp)` => `(uint256, uint256)`
95
+ - `function poweredRatioQuick(uint256 n, uint256 d, uint256 exp)` => `(uint256, uint256)`
100
96
  - `function productRatio(uint256 xn, uint256 yn, uint256 xd, uint256 yd)` => `(uint256, uint256)`
101
97
  - `function reducedRatio(uint256 n, uint256 d, uint256 max)` => `(uint256, uint256)`
102
98
  - `function normalizedRatio(uint256 n, uint256 d, uint256 scale)` => `(uint256, uint256)`
103
99
 
104
100
  ### Powered Ratio
105
101
 
106
- Function `poweredRatio` computes the power of a given ratio by a given exponent.
102
+ Function `poweredRatioExact` opts for accuracy and function `poweredRatioQuick` opts for performance.
103
+
104
+ Each one of the two 'poweredRatio' functions computes the power of a given ratio by a given exponent.
107
105
 
108
106
  In order to avoid multiplication overflow, it may truncate the intermediate result on each iteration.
109
107
 
110
108
  Subsequently, the larger the input exponent is, the lower the accuracy of the output is likely to be.
111
109
 
112
- The input argument `fast` allows to opt for either performance (using `true`) or accuracy (using `false`).
113
-
114
- This library defines a maximum exponent of 4 bits (i.e., 15), which can be customized to fit the system requirements.
110
+ This module defines a maximum exponent of 4 bits (i.e., 15), which can be customized to fit the system requirements.
115
111
 
116
112
  ### Product Ratio
117
113
 
@@ -121,7 +117,7 @@ If either one of the intermediate components is larger than 256 bits, then both
121
117
 
122
118
  ### Reduced Ratio
123
119
 
124
- Function `reducedRatio` computes the nearest ratio whose components (numerator and denominator) are not larger than the input threshold.
120
+ Function `reducedRatio` computes the nearest ratio whose components (numerator and denominator) fit up to a given threshold.
125
121
 
126
122
  Note that function `reducedRatio` is not meant to replace GCD, nor does it strive to achieve better accuracy.
127
123
 
@@ -137,13 +133,13 @@ However, without knowing specific characteristics of that ratio (e.g., each one
137
133
 
138
134
  ### Normalized Ratio
139
135
 
140
- Function `normalizedRatio` computes the nearest ratio whose sum of components (numerator + denominator) equals the input scale.
136
+ Function `normalizedRatio` computes the nearest ratio whose components (numerator and denominator) sum up to a given scale.
141
137
 
142
138
  Note that the output ratio can be larger than the input ratio in some cases, and smaller than the input ratio in other cases.
143
139
 
144
140
  For example:
145
- - `normalizedRatio(12, 34, 100)` returns `(26 74)`; the output ratio is smaller than the input ratio (26 / 74 = 0.351 < 0.352 = 12 / 34)
146
- - `normalizedRatio(1234, 5678, 100)` returns `(18 82)`; the output ratio is larger than the input ratio (18 / 82 = 0.219 > 0.217 = 1234 / 5678)
141
+ - `normalizedRatio(12, 34, 100)` returns `(26, 74)`; the output ratio is smaller than the input ratio (26 / 74 = 0.351 < 0.352 = 12 / 34)
142
+ - `normalizedRatio(1234, 5678, 100)` returns `(18, 82)`; the output ratio is larger than the input ratio (18 / 82 = 0.219 > 0.217 = 1234 / 5678)
147
143
 
148
144
  Keep in mind that it is an important consideration to take when choosing to use this function.
149
145
 
@@ -159,21 +155,11 @@ The same consideration applies for all the other functions in this module.
159
155
 
160
156
  ## AnalyticMath
161
157
 
162
- This module is implemented as a contract, because a library cannot utilize non-constant state variables.
163
-
164
- It implements the following interface:
158
+ This module implements the following interface:
165
159
  - `function pow(uint256 a, uint256 b, uint256 c, uint256 d)` => `(uint256, uint256)`
166
160
  - `function log(uint256 a, uint256 b)` => `(uint256, uint256)`
167
161
  - `function exp(uint256 a, uint256 b)` => `(uint256, uint256)`
168
162
 
169
- ### Initialization
170
-
171
- Prior to using any of these functions, internal data structure must be initialized by executing function `init`.
172
-
173
- Function `init` should be executed either during construction or after construction (if too large for the constructor).
174
-
175
- Thus, any contract which inherits this contract can choose whether or not to call function `init` in its constructor.
176
-
177
163
  ### Exponentiation
178
164
 
179
165
  Function `pow(a, b, c, d)` approximates the power of `a / b` by `c / d`.
@@ -196,9 +182,9 @@ Function `exp(a, b)` approximates the natural exponentiation of `a / b`.
196
182
 
197
183
  The output of this function is guaranteed to be smaller than or equal to the actual value of exp(a / b).
198
184
 
199
- ### Contract Customization
185
+ ### Module Customization
200
186
 
201
- This contract can be customized to support different input ranges (as a tradeoff with accuracy and/or performance).
187
+ This module can be customized to support different input ranges (as a tradeoff with accuracy and/or performance).
202
188
 
203
189
  The full customization manual can be found [here](#customization).
204
190
 
@@ -210,28 +196,21 @@ The full customization manual can be found [here](#customization).
210
196
 
211
197
  ## AdvancedMath
212
198
 
213
- This module is implemented as a contract, because a library cannot utilize non-constant state variables.
214
-
215
- It implements the following interface:
216
- - `function solve(uint256 a, uint256 b, uint256 c, uint256 d)` => `(uint256, uint256)`
217
-
218
- ### Initialization
219
-
220
- Prior to using any of these functions, internal data structure must be initialized by executing function `init`.
221
-
222
- Function `init` should be executed either during construction or after construction (if too large for the constructor).
223
-
224
- Thus, any contract which inherits this contract can choose whether or not to call function `init` in its constructor.
199
+ This module implements the following interface:
200
+ - `function solveExact(uint256 a, uint256 b, uint256 c, uint256 d)` => `(uint256, uint256)`
201
+ - `function solveQuick(uint256 a, uint256 b, uint256 c, uint256 d)` => `(uint256, uint256)`
225
202
 
226
203
  ### Equation Solving
227
204
 
228
- Function `solve(a, b, c, d)` computes a value of x which satisfies the equation x * (a / b) ^ x = c / d.
205
+ Function `solveExact` opts for accuracy and function `solveQuick` opts for performance.
206
+
207
+ Each one of these functions computes a value of x which satisfies the equation x * (a / b) ^ x = c / d.
229
208
 
230
- A detailed description of how this function works can be found [here](readme/AdvancedMath.pdf).
209
+ A detailed description of how each one of these functions works can be found [here](readme/AdvancedMath.pdf).
231
210
 
232
- ### Contract Customization
211
+ ### Module Customization
233
212
 
234
- This contract can be customized to support different input ranges (as a tradeoff with accuracy and/or performance).
213
+ This module can be customized to support different input ranges (as a tradeoff with accuracy and/or performance).
235
214
 
236
215
  The full customization manual can be found [here](#customization).
237
216
 
@@ -243,9 +222,7 @@ The full customization manual can be found [here](#customization).
243
222
 
244
223
  ## BondingCurve
245
224
 
246
- This module is implemented as a contract, because a library cannot extend (inherit) a contract.
247
-
248
- It implements the following interface:
225
+ This module implements the following interface:
249
226
  - `function buy(uint256 supply, uint256 balance, uint256 weight, uint256 amount)` => `(uint256)`
250
227
  - `function sell(uint256 supply, uint256 balance, uint256 weight, uint256 amount)` => `(uint256)`
251
228
  - `function convert(uint256 balance1, uint256 weight1, uint256 balance2, uint256 weight2, uint256 amount)` => `(uint256)`
@@ -253,14 +230,6 @@ It implements the following interface:
253
230
  - `function withdraw(uint256 supply, uint256 balance, uint256 weights, uint256 amount)` => `(uint256)`
254
231
  - `function invest(uint256 supply, uint256 balance, uint256 weights, uint256 amount)` => `(uint256)`
255
232
 
256
- ### Initialization
257
-
258
- Prior to using any of these functions, internal data structure must be initialized by executing function `init`.
259
-
260
- Function `init` should be executed either during construction or after construction (if too large for the constructor).
261
-
262
- Thus, any contract which inherits this contract can choose whether or not to call function `init` in its constructor.
263
-
264
233
  ### Functionality
265
234
 
266
235
  ```
@@ -286,18 +255,11 @@ The bonding-curve model was conceived by [Bancor](https://github.com/bancorproto
286
255
 
287
256
  ## DynamicCurve
288
257
 
289
- This module is implemented as a contract, because a library cannot extend (inherit) a contract.
290
-
291
- It implements the following interface:
292
- - `function equalize(uint256 t, uint256 s, uint256 r, uint256 q, uint256 p)` => `(uint256, uint256)`
293
-
294
- ### Initialization
295
-
296
- Prior to using any of these functions, internal data structure must be initialized by executing function `init`.
297
-
298
- Function `init` should be executed either during construction or after construction (if too large for the constructor).
258
+ This module implements the following interface:
259
+ - `function equalizeExact(uint256 t, uint256 s, uint256 r, uint256 q, uint256 p)` => `(uint256, uint256)`
260
+ - `function equalizeQuick(uint256 t, uint256 s, uint256 r, uint256 q, uint256 p)` => `(uint256, uint256)`
299
261
 
300
- Thus, any contract which inherits this contract can choose whether or not to call function `init` in its constructor.
262
+ Function `equalizeExact` opts for accuracy and function `equalizeQuick` opts for performance.
301
263
 
302
264
  ### Equalization
303
265
 
@@ -355,8 +317,8 @@ The dynamic-curve method was conceived by [Bancor](https://github.com/bancorprot
355
317
 
356
318
  ### Prerequisites
357
319
 
358
- - `node 16.13.0`
359
- - `yarn 1.22.10` or `npm 8.1.0`
320
+ - `node 20.17.0`
321
+ - `yarn 1.22.22` or `npm 10.8.2`
360
322
 
361
323
  ### Installation
362
324
 
@@ -384,11 +346,11 @@ The dynamic-curve method was conceived by [Bancor](https://github.com/bancorprot
384
346
 
385
347
  ### Prerequisites
386
348
 
387
- - `python 3.9`
349
+ - `python 3.12.3`
388
350
 
389
351
  ### Execution
390
352
 
391
- In order to allow rapid testing and verification, all modules (contracts and libraries) have been ported from Solidity to Python:
353
+ In order to allow rapid testing and verification, all modules have been ported from Solidity to Python:
392
354
  - The emulation modules themselves are located under [FixedPoint](project/emulation/FixedPoint)
393
355
  - The corresponding floating-point functionality is located under [FloatPoint](project/emulation/FloatPoint)
394
356
  - A set of unit-tests for various functions (one per function) is located under [emulation](project/emulation)
@@ -401,27 +363,23 @@ In order to allow rapid testing and verification, all modules (contracts and lib
401
363
 
402
364
  ## Customization
403
365
 
404
- All customization parameters are located in [constants.py](project/emulation/AutoGenerate/common/constants.py).
366
+ All customization parameters are located in [constants.py](project/customization/constants.py).
405
367
 
406
368
  When modifying **any** of them, one should regenerate **all** the code.
407
369
 
408
370
  The following scripts generate the code for [AnalyticMath.sol](project/contracts/AnalyticMath.sol):
409
- - [PrintLn2ScalingFactors.py ](project/emulation/AutoGenerate/PrintLn2ScalingFactors.py )
410
- - [PrintOptimalThresholds.py ](project/emulation/AutoGenerate/PrintOptimalThresholds.py )
411
- - [PrintFunctionGeneralExp.py](project/emulation/AutoGenerate/PrintFunctionGeneralExp.py)
412
- - [PrintFunctionOptimalLog.py](project/emulation/AutoGenerate/PrintFunctionOptimalLog.py)
413
- - [PrintFunctionOptimalExp.py](project/emulation/AutoGenerate/PrintFunctionOptimalExp.py)
414
- - [PrintMaxExpArray.py ](project/emulation/AutoGenerate/PrintMaxExpArray.py )
371
+ - [PrintAnalyticMathConstants.py ](project/customization/PrintAnalyticMathConstants.py )
372
+ - [PrintAnalyticMathOptimalLog.py](project/customization/PrintAnalyticMathOptimalLog.py)
373
+ - [PrintAnalyticMathOptimalExp.py](project/customization/PrintAnalyticMathOptimalExp.py)
415
374
 
416
375
  The following scripts generate the code for [AdvancedMath.sol](project/contracts/AdvancedMath.sol):
417
- - [PrintLambertFactors.py ](project/emulation/AutoGenerate/PrintLambertFactors.py )
418
- - [PrintFunctionLambertNeg1.py](project/emulation/AutoGenerate/PrintFunctionLambertNeg1.py)
419
- - [PrintFunctionLambertPos1.py](project/emulation/AutoGenerate/PrintFunctionLambertPos1.py)
420
- - [PrintLambertArray.py ](project/emulation/AutoGenerate/PrintLambertArray.py )
376
+ - [PrintAdvancedMathConstants.py ](project/customization/PrintAdvancedMathConstants.py )
377
+ - [PrintAdvancedMathLambertNeg1.py](project/customization/PrintAdvancedMathLambertNeg1.py)
378
+ - [PrintAdvancedMathLambertPos1.py](project/customization/PrintAdvancedMathLambertPos1.py)
421
379
 
422
380
  In order to retain the [testing infrastructure](#testing), one should proceed by:
423
- - Running the script [PrintTestConstants.py](project/emulation/AutoGenerate/PrintTestConstants.py)
424
- - Pasting the printout into [AnalyticMathConstants.js](project/tests/helpers/AnalyticMathConstants.js)
381
+ - Running the script [PrintTestConstants.py](project/customization/PrintTestConstants.py)
382
+ - Pasting the printout into [Constants.js](project/tests/helpers/Constants.js)
425
383
 
426
384
  In order to retain the [emulation infrastructure](#emulation), one should proceed by:
427
385
  - Porting all changes from [AnalyticMath.sol](project/contracts/AnalyticMath.sol) to [AnalyticMath.py](project/emulation/FixedPoint/AnalyticMath.py)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solidity-math-utils",
3
- "version": "1.12.0",
3
+ "version": "2.0.1",
4
4
  "description": "Solidity Mathematical Utilities",
5
5
  "repository": {
6
6
  "type": "git",
@@ -21,7 +21,7 @@
21
21
  "@nomiclabs/hardhat-truffle5": "2.0.7",
22
22
  "@nomiclabs/hardhat-web3": "2.0.0",
23
23
  "decimal.js": "10.4.3",
24
- "hardhat": "2.22.8",
25
- "solidity-coverage": "0.8.12"
24
+ "hardhat": "2.22.17",
25
+ "solidity-coverage": "0.8.14"
26
26
  }
27
27
  }