Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/weijiekoh/unsafemaths
A gas-optimised arithmetic library for Ethereum
https://github.com/weijiekoh/unsafemaths
Last synced: about 2 months ago
JSON representation
A gas-optimised arithmetic library for Ethereum
- Host: GitHub
- URL: https://github.com/weijiekoh/unsafemaths
- Owner: weijiekoh
- License: gpl-3.0
- Created: 2020-05-23T12:11:32.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-05-11T18:39:15.000Z (over 3 years ago)
- Last Synced: 2023-03-05T18:05:41.800Z (almost 2 years ago)
- Language: TypeScript
- Homepage:
- Size: 224 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# UnsafeMaths: a gas-optimised arithmetic library for Ethereum
This is OpenZepplin's
[SafeMath.sol](https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/SafeMath.sol),
rewritten in Aztec Protocol's [Huff](https://github.com/AztecProtocol/huff/)
domain-specific language for the Ethereum Virtual Machine.The goal of using Huff to rewrite SafeMath is not to replace it, but for purely
educational purposes. The code has simple tests which check for the same
arithmetic overflows which SafeMath does, but has neither been audited nor
formally verified. As its name suggests, **don't use this in production**!## Gas savings
If we use Remix and `ethereumjs-vm` to run each function, their reported
execution gas cost is as follows:| Operation | SafeMath gas cost | UnsafeMaths gas cost | Savings | Notes |
|-|-|-|-|-|
| `add(a, b)` | 281 | 117 | 164 | |
| `sub(a, b)` | 311 | 136 | 175 | |
| `mul(a, b)` | 388 | 196 | 192 | |
| `mul(a, b)` | 339 | 156 | 183 | `a == 0`, so return 0 immediately |
| `div(a, b)` | 320 | 179 | 141 | |
| `mod(a, b)` | 446 | 201 | 245 | |I also benchmarked each SafeMath and UnsafeMath function using Ganache, and
found the same gas savings per function, except for `mod()`:| Operation | SafeMath gas cost | UnsafeMaths gas cost | Savings | Notes |
|-|-|-|-|-|
| `add(a, b)` | 3323 | 3159 | 164 | |
| `sub(a, b)` | 3397 | 3222 | 175 | |
| `mul(a, b)` | 3496 | 3304 | 192 | |
| `mul(a, b)` | 3435 | 3252 | 183 | `a == 0` |
| `div(a, b)` | 3384 | 3243 | 141 | |
| `mod(a, b)` | 3505 | 3331 | 174 | |Note that the above gas costs exclude the base gas cost of 21000, and include
overhead which comes from using a wrapper contract to call an external
function.In both benchmarks, the SafeMath contract was compiled with optimisation enabled.
## Getting started
First, clone this repository, install dependencies, and build the source code:
```bash
git clone [email protected]:weijiekoh/UnsafeMaths.git &&
cd UnsafeMaths &&
npm i &&
npm run build
```Next, compile the contracts. You need `solc` v0.6.x either in your `$PATH` or
somewhere on your disk.```bash
SOLC=/path/to/solc/ ./scripts/compileSol.sh
```Test the Huff code as such:
```bash
npm run test
```To benchmark the gas consumption of UnsafeMaths versus OpenZepplin's SafeMath,
first run Ganache:```bash
npm run ganache-cli
```Next, run the following in a different terminal:
```
npm run test-gasBenchmarks
```## Compilation
To compile `UnsafeMaths.huff` into deployable bytecode, run:
```
npm run build &&
npm run compile
```UnsafeMaths' contract ABI is the same as that of `SafeMath.sol`.
## Credits
Many thanks to @wolflo0 for feedback and comments.