• Stars
    star
    166
  • Rank 227,748 (Top 5 %)
  • Language
    Solidity
  • License
    MIT License
  • Created over 5 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

Elliptic Curve arithmetic operations written in Solidity

elliptic-curve-solidity npm version TravisCI Coverage Status

elliptic-curve-solidity is an open source implementation of Elliptic Curve arithmetic operations written in Solidity.

DISCLAIMER: This is experimental software. Use it at your own risk!

The solidity contracts have been generalized in order to support any elliptic curve based on prime numbers up to 256 bits.

elliptic-curve-solidity has been designed as a library with only pure functions aiming at decreasing gas consumption as much as possible. Additionally, gas consumption comparison can be found in the benchmark section. This library does not check whether the points passed as arguments to the library belong to the curve. However, the library exposes a method called isOnCurve that can be utilized before using the library functions.

It contains 2 solidity libraries:

  1. EllipticCurve.sol: provides main elliptic curve operations in affine and Jacobian coordinates.
  2. FastEcMul.sol: provides a fast elliptic curve multiplication by using scalar decomposition and wNAF scalar representation.

EllipticCurve library provides functions for:

  • Modular
    • inverse
    • exponentiation
  • Jacobian coordinates
    • addition
    • double
    • multiplication
  • Affine coordinates
    • inverse
    • addition
    • subtraction
    • multiplication
  • Auxiliary
    • conversion to affine coordinates
    • derive coordinate Y from compressed EC point
    • check if EC point is on curve

FastEcMul library provides support for:

  • Scalar decomposition
  • Simultaneous multiplication (computes 2 EC multiplications using wNAF scalar representation)

Supported curves

The elliptic-curve-solidity contract supports up to 256-bit curves. However, it has been extensively tested for the following curves:

  • secp256k1
  • secp224k1
  • secp192k1
  • secp256r1 (aka P256)
  • secp192r1 (aka P192)
  • secp224r1 (aka P224)

Known limitations:

  • deriveY function do not work with the curves secp224r1 and secp224k1 because of the selected derivation algorithm. The computations for this curve are done with a modulo prime p such that p mod 4 = 1, thus a more complex algorithm is required (e.g. Tonelli-Shanks algorithm). Note that deriveY is just an auxiliary function, and thus does not limit the functionality of curve arithmetic operations.
  • the library only supports elliptic curves with cofactor = 1 (all supported curves have a cofactor = 1).

Usage

EllipticCurve.sol library can be used directly by importing it.

The Secp256k1 example depicts how to use the library by providing a function to derive a public key from a secret key:

pragma solidity 0.6.12;

import "elliptic-curve-solidity/contracts/EllipticCurve.sol";


contract Secp256k1 {

  uint256 public constant GX = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798;
  uint256 public constant GY = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8;
  uint256 public constant AA = 0;
  uint256 public constant BB = 7;
  uint256 public constant PP = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F;

  function derivePubKey(uint256 privKey) public pure returns(uint256 qx, uint256 qy) {
    (qx, qy) = EllipticCurve.ecMul(
      privKey,
      GX,
      GY,
      AA,
      PP
    );
  }
}

The cost of a key derivation operation in Secp256k1 is around 550k gas.

ยท--------------------------------------------------|--------------------------ยท
|                      Gas                         ยท Block limit: 6721975 gas โ”‚
ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท
|                 ยท          100 gwei/gas           ยท     592.30 usd/eth      โ”‚
ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท
|  Method         ยท  Min     ยท  Max     ยท  Avg     ยท  # calls   ยท  usd (avg)  โ”‚
ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท
|  derivePubKey   ยท  476146  ยท  518863  ยท  499884  ยท        18  ยท      29.61  โ”‚
ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท

The cost of a simultaneous multiplication (using wNAF) consumes around 35% of the gas required by 2 EC multiplications.

Benchmark

Gas consumption and USD price estimation with a gas price of 100 Gwei, derived from ETH Gas Station:

ยท----------------------------------------|---------------------------|-------------|----------------------------ยท
|  Solc version: 0.6.12+commit.27d51765  ยท  Optimizer enabled: true  ยท  Runs: 200  ยท  Block limit: 6718946 gas  โ”‚
ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท
|  Methods                               ยท              100 gwei/gas               ยท       613.52 usd/eth       โ”‚
ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท
|  Contract       ยท  Method              ยท  Min        ยท  Max        ยท  Avg        ยท  # calls     ยท  usd (avg)  โ”‚
ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท
|  EllipticCurve  ยท  decomposeScalar     ยท      55811  ยท      65399  ยท      61939  ยท         134  ยท       3.80  โ”‚
ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท
|  EllipticCurve  ยท  deriveY             ยท      45275  ยท      55545  ยท      50410  ยท           4  ยท       3.09  โ”‚
ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท
|  EllipticCurve  ยท  ecAdd               ยท      24305  ยท      56323  ยท      49119  ยท         472  ยท       3.01  โ”‚
ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท
|  EllipticCurve  ยท  ecInv               ยท      22906  ยท      23074  ยท      22990  ยท           2  ยท       1.41  โ”‚
ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท
|  EllipticCurve  ยท  ecMul               ยท      24911  ยท     623087  ยท     350939  ยท         561  ยท      21.53  โ”‚
ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท
|  EllipticCurve  ยท  ecSimMul            ยท      76465  ยท     488165  ยท     243763  ยท         125  ยท      14.96  โ”‚
ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท
|  EllipticCurve  ยท  ecSub               ยท      42634  ยท      56236  ยท      49717  ยท         228  ยท       3.05  โ”‚
ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท
|  EllipticCurve  ยท  invMod              ยท      22153  ยท      49255  ยท      39627  ยท          12  ยท       2.43  โ”‚
ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท
|  EllipticCurve  ยท  isOnCurve           ยท      23400  ยท      24071  ยท      23623  ยท          16  ยท       1.45  โ”‚
ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท
|  EllipticCurve  ยท  toAffine            ยท      50145  ยท      50850  ยท      50498  ยท           4  ยท       3.10  โ”‚
ยท----------------------------------------|-------------|-------------|-------------|--------------|-------------ยท

Acknowledgements

Some functions of the contract are based on:

License

elliptic-curve-solidity is published under the MIT license.

More Repositories

1

witnet-rust

Open source Rust implementation of the Witnet decentralized oracle protocol, including full node and wallet backend ๐Ÿ‘๏ธ๐Ÿฆ€
Rust
180
star
2

vrf-solidity

Verifiable Random Function (VRF) library written in Solidity
Solidity
96
star
3

vrf-rs

Verifiable Random Function (VRF) library written in Rust
Rust
90
star
4

witnet-solidity-bridge

Witnet Bridge for EVM-compatible blockchains
Solidity
60
star
5

sheikah

A Witnet compatible desktop wallet and smart contracts development environment
Vue
37
star
6

protobuf-convert

Macros for convenient serialization of Rust data structures into/from Protocol Buffers 3
Rust
34
star
7

bls-solidity

Solidity
31
star
8

witnet-ethereum-block-relay

Witnet Block Relay for EVM-compatible blockchains
Solidity
11
star
9

witnet-pricefeed-tutorial

A totally decentralized BTC<>USD price feed example built with Solidity, Truffle and Witnet
JavaScript
11
star
10

my-wit-wallet

myWitWallet is a Witnet light wallet for mobile and desktop
Dart
10
star
11

documentation

Community-sourced documentation for the Witnet protocol and its ecosystem โ€” powered by GitBook
9
star
12

WIPs

Witnet Improvement Proposals
9
star
13

research

Research items and academic publications produced by the Witnet Foundation team
Python
8
star
14

witnet-price-router

Price feed examples using the Witnet decentralized oracle network
JavaScript
7
star
15

aragon-govern-discord-integration

Enables voting on Aragon Govern using Discord reactions. Powered by Witnet.
TypeScript
7
star
16

bls-signatures-rs

Open source implementation of Bonneh-Lynn-Shacham (BLS) signatures written in Rust
Rust
6
star
17

data-feeds-explorer

Catalog of Witnet-powered public data feeds and price feeds available on Ethereum, Conflux, Boba, Celo and other networks
TypeScript
5
star
18

truffle-box

Witnet-enabled Truffle Box โ€” Connect your Solidity contracts to the real world!
JavaScript
5
star
19

witnet-requests-js

Witnet Requests Javascript Library
JavaScript
5
star
20

ts-slip32

TypeScript implementation of the SLIP-0032 extended serialization format for BIP-32 wallets
TypeScript
5
star
21

async-jsonrpc-client

Event-driven JSON-RPC client with support for multiple transports
Rust
4
star
22

witnet.dart

A Dart library to interface with the Witnet Protocol.
Dart
3
star
23

website

Source code for the official Witnet website at https://witnet.io
Vue
3
star
24

witnet-radon-js

Utility library to manipulate Radon requests
TypeScript
3
star
25

witty-creatures

Vue
3
star
26

web3-jsonrpc-gateway

A Web3 JSON-RPC provider that handles accounts on its own using Ethers.js but delegates chain queries to a 3rd party service, e.g. Infura
TypeScript
3
star
27

witnet-polkadot

Pallets and tools enabling interoperation between the Witnet oracle and Polkadot or Kusama
Rust
2
star
28

witnet-operator-tools

Tools for node operators
Shell
1
star
29

genesis_block

1
star
30

witnet-integrations

A showcase of integrations of the Witnet oracle into other crypto projects
1
star
31

http-post-adapter

HTTP service that makes an HTTP-POST request with the arguments given
JavaScript
1
star
32

witnet-toolkit

TypeScript
1
star
33

witnet-design-system

TypeScript
1
star
34

witnet-solidity

JavaScript
1
star