• Stars
    star
    144
  • Rank 255,590 (Top 6 %)
  • Language
    Solidity
  • License
    MIT License
  • Created about 3 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Solidity library with basic trigonometry functions

Solidity Trigonometry

Solidity library offering basic trigonometry functions where inputs and outputs are integers. Inputs are specified in radians scaled by 1e18, and similarly outputs are scaled by 1e18. Each invocation of the sin() and cos() functions cost around 1600–1700 gas (see the testNoReverts costs in .gas-snapshot for more info).

This implementation is based off the Solidity trigonometry library written by Lefteris Karapetsas which can be found here. Compared to Lefteris' implementation, this version makes the following changes:

  • Uses a 32 bits instead of 16 bits for improved accuracy
  • Updated for Solidity 0.8.x
  • Various gas optimizations
  • Change inputs/outputs to standard trig format (scaled by 1e18) instead of requiring the integer format used by the algorithm

The original implementation by Lefteris is based off Dave Dribin's trigint C library, which in turn is based on an article by Scott Dattalo.

Usage

When using this library, it's recommended to wrap input values (which are in radians) between 2 * PI * 1e18 and 4 * PI * 1e18 to avoid precision errors. This is equivalent to wrapping standard values between 0 and 2Ï€. There is some flexibility on that range, but it should stay within reasonable bounds.

To use this in a Foundry project, install it with:

forge install https://github.com/mds1/solidity-trigonometry

To use this in a dapptools project, install it with:

dapp install https://github.com/mds1/solidity-trigonometry

There is currently no npm package, so for projects using npm for package management, such as Hardhat projects, use:

yarn add https://github.com/mds1/solidity-trigonometry.git

Development

Setup

This library is developed with Foundry. If you don't have Foundry installed, run the command below to get foundryup, the Foundry toolchain installer:

curl -L https://foundry.paradigm.xyz | bash

Then in a new terminal session or after reloading your PATH, run foundryup to get the latest forge and cast binaries.

Testing

Run tests with forge test, and update gas snapshots with FOUNDRY_FUZZ_RUNS=50000 forge snapshot (this will take a while to run since that many FFI runs can be slow).

NOTE: Tests are configured to run with the --ffi flag enabled for fuzz testing, so review the test commands before executing them to ensure you aren't running any malicious code on your machine.

More Repositories

1

evm-diff

Diff EVM-compatible chains in a friendly format
TypeScript
276
star
2

solidity-generators

Solidity library offering linspace, arange, and logspace methods to generate evenly spaced arrays.
Solidity
119
star
3

convex-shutdown-simulation

Simulates a call to Convex Finance's system shutdown method, which uses about 16M gas
TypeScript
58
star
4

path-planning

Hierarchical D* Lite: A real-time 3D path planning algorithm
TeX
56
star
5

Sweeposaurus

Sweeposaurus helps you sweep all of your tokens to another address 🦖
Vue
39
star
6

Cancel-Ethereum-Transactions

The easiest way to cancel pending transactions on Ethereum.
Vue
24
star
7

solidity-fuzzing-benchmarks

Solidity contracts and tests used to benchmark performance of fuzzers
Solidity
21
star
8

multicall3-frontend

https://multicall3.com
TypeScript
13
star
9

drai

DRAI: Dollar-Pegged RAI
Solidity
13
star
10

forge-trace-and-script-issue

Solidity
10
star
11

lottery-dapp-template

Ethereum dapp template, including the front end, for a simple lottery smart contract
JavaScript
9
star
12

simple-zksync-demo

A very simple demo of zkSync
Vue
6
star
13

state-diff-aggregator

Generate the net state diff from a series of transactions.
TypeScript
6
star
14

ETH-Dashboard

Customizable Ethereum dashboard. Located at https://ethdashboard.com/
Vue
5
star
15

alfred-ethereum-gas-prices

Alfred workflow to get current Ethereum gas prices
Python
3
star
16

KeyStats

Ethereum address statistics
CSS
2
star
17

send-eth-tx

Simple webapp to send an Ethereum transaction via MetaMask, with ENS support
JavaScript
2
star
18

alfred-etherscan-search

Alfred workflow to search Etherscan
Python
2
star
19

Matt-Solomon

My personal website, hosted at https://mattsolomon.dev
SCSS
2
star
20

Bancor-Fiat-On-Ramp

Submission for Bancor's "Fiat On-Ramp For Liquidity Pools" Hackathon Challenge
JavaScript
1
star
21

zksync-demo

JavaScript
1
star
22

CompreWallet

The comprehensive Ethereum smart contract wallet that can interact with any app
JavaScript
1
star