{"id":21715839,"url":"https://github.com/weijiekoh/unsafemaths","last_synced_at":"2026-05-15T23:40:10.702Z","repository":{"id":51500693,"uuid":"266329199","full_name":"weijiekoh/UnsafeMaths","owner":"weijiekoh","description":"A gas-optimised arithmetic library for Ethereum","archived":false,"fork":false,"pushed_at":"2021-05-11T18:39:15.000Z","size":229,"stargazers_count":1,"open_issues_count":2,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-25T17:43:56.899Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/weijiekoh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-05-23T12:11:32.000Z","updated_at":"2021-08-06T14:14:57.000Z","dependencies_parsed_at":"2022-08-21T12:40:12.864Z","dependency_job_id":null,"html_url":"https://github.com/weijiekoh/UnsafeMaths","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weijiekoh%2FUnsafeMaths","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weijiekoh%2FUnsafeMaths/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weijiekoh%2FUnsafeMaths/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weijiekoh%2FUnsafeMaths/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/weijiekoh","download_url":"https://codeload.github.com/weijiekoh/UnsafeMaths/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244677591,"owners_count":20492063,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-26T00:47:24.117Z","updated_at":"2026-05-15T23:40:05.680Z","avatar_url":"https://github.com/weijiekoh.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# UnsafeMaths: a gas-optimised arithmetic library for Ethereum\n\nThis is OpenZepplin's\n[SafeMath.sol](https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/SafeMath.sol),\nrewritten in Aztec Protocol's [Huff](https://github.com/AztecProtocol/huff/)\ndomain-specific language for the Ethereum Virtual Machine.\n\nThe goal of using Huff to rewrite SafeMath is not to replace it, but for purely\neducational purposes. The code has simple tests which check for the same\narithmetic overflows which SafeMath does, but has neither been audited nor\nformally verified. As its name suggests, **don't use this in production**!\n\n## Gas savings\n\nIf we use Remix and `ethereumjs-vm` to run each function, their reported\nexecution gas cost is as follows:\n\n| Operation | SafeMath gas cost | UnsafeMaths gas cost | Savings | Notes |\n|-|-|-|-|-|\n| `add(a, b)` | 281 | 117 | 164 | |\n| `sub(a, b)` | 311 | 136 | 175 | |\n| `mul(a, b)` | 388 | 196 | 192 | |\n| `mul(a, b)` | 339 | 156 | 183 | `a == 0`, so return 0 immediately |\n| `div(a, b)` | 320 | 179 | 141 | |\n| `mod(a, b)` | 446 | 201 | 245 | |\n\nI also benchmarked each SafeMath and UnsafeMath function using Ganache, and\nfound the same gas savings per function, except for `mod()`:\n\n| Operation | SafeMath gas cost | UnsafeMaths gas cost | Savings | Notes |\n|-|-|-|-|-|\n| `add(a, b)` | 3323 | 3159 | 164 | |\n| `sub(a, b)` | 3397 | 3222 | 175 | |\n| `mul(a, b)` | 3496 | 3304 | 192 | |\n| `mul(a, b)` | 3435 | 3252 | 183 | `a == 0` |\n| `div(a, b)` | 3384 | 3243 | 141 | |\n| `mod(a, b)` | 3505 | 3331 | 174 | |\n\nNote that the above gas costs exclude the base gas cost of 21000, and include\noverhead which comes from using a wrapper contract to call an external\nfunction.\n\nIn both benchmarks, the SafeMath contract was compiled with optimisation enabled.\n\n## Getting started\n\nFirst, clone this repository, install dependencies, and build the source code:\n\n```bash\ngit clone git@github.com:weijiekoh/UnsafeMaths.git \u0026\u0026\ncd UnsafeMaths \u0026\u0026\nnpm i \u0026\u0026\nnpm run build\n```\n\nNext, compile the contracts. You need `solc` v0.6.x either in your `$PATH` or\nsomewhere on your disk. \n\n```bash\nSOLC=/path/to/solc/ ./scripts/compileSol.sh\n```\n\nTest the Huff code as such:\n\n```bash\nnpm run test\n```\n\nTo benchmark the gas consumption of UnsafeMaths versus OpenZepplin's SafeMath,\nfirst run Ganache:\n\n\n```bash\nnpm run ganache-cli\n```\n\nNext, run the following in a different terminal:\n\n```\nnpm run test-gasBenchmarks\n```\n\n## Compilation\n\nTo compile `UnsafeMaths.huff` into deployable bytecode, run:\n\n```\nnpm run build \u0026\u0026\nnpm run compile\n```\n\nUnsafeMaths' contract ABI is the same as that of `SafeMath.sol`.\n\n## Credits\n\nMany thanks to @wolflo0 for feedback and comments.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fweijiekoh%2Funsafemaths","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fweijiekoh%2Funsafemaths","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fweijiekoh%2Funsafemaths/lists"}